intel_display.c 341 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_dp_helper.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include <linux/dma_remapping.h>
  42. #define DIV_ROUND_CLOSEST_ULL(ll, d) \
  43. ({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
  44. static void intel_increase_pllclock(struct drm_crtc *crtc);
  45. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  46. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  47. struct intel_crtc_config *pipe_config);
  48. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  49. struct intel_crtc_config *pipe_config);
  50. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  51. int x, int y, struct drm_framebuffer *old_fb);
  52. static int intel_framebuffer_init(struct drm_device *dev,
  53. struct intel_framebuffer *ifb,
  54. struct drm_mode_fb_cmd2 *mode_cmd,
  55. struct drm_i915_gem_object *obj);
  56. 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. typedef struct intel_limit intel_limit_t;
  64. struct intel_limit {
  65. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  66. intel_p2_t p2;
  67. };
  68. int
  69. intel_pch_rawclk(struct drm_device *dev)
  70. {
  71. struct drm_i915_private *dev_priv = dev->dev_private;
  72. WARN_ON(!HAS_PCH_SPLIT(dev));
  73. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  74. }
  75. static inline u32 /* units of 100MHz */
  76. intel_fdi_link_freq(struct drm_device *dev)
  77. {
  78. if (IS_GEN5(dev)) {
  79. struct drm_i915_private *dev_priv = dev->dev_private;
  80. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  81. } else
  82. return 27;
  83. }
  84. static const intel_limit_t intel_limits_i8xx_dac = {
  85. .dot = { .min = 25000, .max = 350000 },
  86. .vco = { .min = 908000, .max = 1512000 },
  87. .n = { .min = 2, .max = 16 },
  88. .m = { .min = 96, .max = 140 },
  89. .m1 = { .min = 18, .max = 26 },
  90. .m2 = { .min = 6, .max = 16 },
  91. .p = { .min = 4, .max = 128 },
  92. .p1 = { .min = 2, .max = 33 },
  93. .p2 = { .dot_limit = 165000,
  94. .p2_slow = 4, .p2_fast = 2 },
  95. };
  96. static const intel_limit_t intel_limits_i8xx_dvo = {
  97. .dot = { .min = 25000, .max = 350000 },
  98. .vco = { .min = 908000, .max = 1512000 },
  99. .n = { .min = 2, .max = 16 },
  100. .m = { .min = 96, .max = 140 },
  101. .m1 = { .min = 18, .max = 26 },
  102. .m2 = { .min = 6, .max = 16 },
  103. .p = { .min = 4, .max = 128 },
  104. .p1 = { .min = 2, .max = 33 },
  105. .p2 = { .dot_limit = 165000,
  106. .p2_slow = 4, .p2_fast = 4 },
  107. };
  108. static const intel_limit_t intel_limits_i8xx_lvds = {
  109. .dot = { .min = 25000, .max = 350000 },
  110. .vco = { .min = 908000, .max = 1512000 },
  111. .n = { .min = 2, .max = 16 },
  112. .m = { .min = 96, .max = 140 },
  113. .m1 = { .min = 18, .max = 26 },
  114. .m2 = { .min = 6, .max = 16 },
  115. .p = { .min = 4, .max = 128 },
  116. .p1 = { .min = 1, .max = 6 },
  117. .p2 = { .dot_limit = 165000,
  118. .p2_slow = 14, .p2_fast = 7 },
  119. };
  120. static const intel_limit_t intel_limits_i9xx_sdvo = {
  121. .dot = { .min = 20000, .max = 400000 },
  122. .vco = { .min = 1400000, .max = 2800000 },
  123. .n = { .min = 1, .max = 6 },
  124. .m = { .min = 70, .max = 120 },
  125. .m1 = { .min = 8, .max = 18 },
  126. .m2 = { .min = 3, .max = 7 },
  127. .p = { .min = 5, .max = 80 },
  128. .p1 = { .min = 1, .max = 8 },
  129. .p2 = { .dot_limit = 200000,
  130. .p2_slow = 10, .p2_fast = 5 },
  131. };
  132. static const intel_limit_t intel_limits_i9xx_lvds = {
  133. .dot = { .min = 20000, .max = 400000 },
  134. .vco = { .min = 1400000, .max = 2800000 },
  135. .n = { .min = 1, .max = 6 },
  136. .m = { .min = 70, .max = 120 },
  137. .m1 = { .min = 8, .max = 18 },
  138. .m2 = { .min = 3, .max = 7 },
  139. .p = { .min = 7, .max = 98 },
  140. .p1 = { .min = 1, .max = 8 },
  141. .p2 = { .dot_limit = 112000,
  142. .p2_slow = 14, .p2_fast = 7 },
  143. };
  144. static const intel_limit_t intel_limits_g4x_sdvo = {
  145. .dot = { .min = 25000, .max = 270000 },
  146. .vco = { .min = 1750000, .max = 3500000},
  147. .n = { .min = 1, .max = 4 },
  148. .m = { .min = 104, .max = 138 },
  149. .m1 = { .min = 17, .max = 23 },
  150. .m2 = { .min = 5, .max = 11 },
  151. .p = { .min = 10, .max = 30 },
  152. .p1 = { .min = 1, .max = 3},
  153. .p2 = { .dot_limit = 270000,
  154. .p2_slow = 10,
  155. .p2_fast = 10
  156. },
  157. };
  158. static const intel_limit_t intel_limits_g4x_hdmi = {
  159. .dot = { .min = 22000, .max = 400000 },
  160. .vco = { .min = 1750000, .max = 3500000},
  161. .n = { .min = 1, .max = 4 },
  162. .m = { .min = 104, .max = 138 },
  163. .m1 = { .min = 16, .max = 23 },
  164. .m2 = { .min = 5, .max = 11 },
  165. .p = { .min = 5, .max = 80 },
  166. .p1 = { .min = 1, .max = 8},
  167. .p2 = { .dot_limit = 165000,
  168. .p2_slow = 10, .p2_fast = 5 },
  169. };
  170. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  171. .dot = { .min = 20000, .max = 115000 },
  172. .vco = { .min = 1750000, .max = 3500000 },
  173. .n = { .min = 1, .max = 3 },
  174. .m = { .min = 104, .max = 138 },
  175. .m1 = { .min = 17, .max = 23 },
  176. .m2 = { .min = 5, .max = 11 },
  177. .p = { .min = 28, .max = 112 },
  178. .p1 = { .min = 2, .max = 8 },
  179. .p2 = { .dot_limit = 0,
  180. .p2_slow = 14, .p2_fast = 14
  181. },
  182. };
  183. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  184. .dot = { .min = 80000, .max = 224000 },
  185. .vco = { .min = 1750000, .max = 3500000 },
  186. .n = { .min = 1, .max = 3 },
  187. .m = { .min = 104, .max = 138 },
  188. .m1 = { .min = 17, .max = 23 },
  189. .m2 = { .min = 5, .max = 11 },
  190. .p = { .min = 14, .max = 42 },
  191. .p1 = { .min = 2, .max = 6 },
  192. .p2 = { .dot_limit = 0,
  193. .p2_slow = 7, .p2_fast = 7
  194. },
  195. };
  196. static const intel_limit_t intel_limits_pineview_sdvo = {
  197. .dot = { .min = 20000, .max = 400000},
  198. .vco = { .min = 1700000, .max = 3500000 },
  199. /* Pineview's Ncounter is a ring counter */
  200. .n = { .min = 3, .max = 6 },
  201. .m = { .min = 2, .max = 256 },
  202. /* Pineview only has one combined m divider, which we treat as m2. */
  203. .m1 = { .min = 0, .max = 0 },
  204. .m2 = { .min = 0, .max = 254 },
  205. .p = { .min = 5, .max = 80 },
  206. .p1 = { .min = 1, .max = 8 },
  207. .p2 = { .dot_limit = 200000,
  208. .p2_slow = 10, .p2_fast = 5 },
  209. };
  210. static const intel_limit_t intel_limits_pineview_lvds = {
  211. .dot = { .min = 20000, .max = 400000 },
  212. .vco = { .min = 1700000, .max = 3500000 },
  213. .n = { .min = 3, .max = 6 },
  214. .m = { .min = 2, .max = 256 },
  215. .m1 = { .min = 0, .max = 0 },
  216. .m2 = { .min = 0, .max = 254 },
  217. .p = { .min = 7, .max = 112 },
  218. .p1 = { .min = 1, .max = 8 },
  219. .p2 = { .dot_limit = 112000,
  220. .p2_slow = 14, .p2_fast = 14 },
  221. };
  222. /* Ironlake / Sandybridge
  223. *
  224. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  225. * the range value for them is (actual_value - 2).
  226. */
  227. static const intel_limit_t intel_limits_ironlake_dac = {
  228. .dot = { .min = 25000, .max = 350000 },
  229. .vco = { .min = 1760000, .max = 3510000 },
  230. .n = { .min = 1, .max = 5 },
  231. .m = { .min = 79, .max = 127 },
  232. .m1 = { .min = 12, .max = 22 },
  233. .m2 = { .min = 5, .max = 9 },
  234. .p = { .min = 5, .max = 80 },
  235. .p1 = { .min = 1, .max = 8 },
  236. .p2 = { .dot_limit = 225000,
  237. .p2_slow = 10, .p2_fast = 5 },
  238. };
  239. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  240. .dot = { .min = 25000, .max = 350000 },
  241. .vco = { .min = 1760000, .max = 3510000 },
  242. .n = { .min = 1, .max = 3 },
  243. .m = { .min = 79, .max = 118 },
  244. .m1 = { .min = 12, .max = 22 },
  245. .m2 = { .min = 5, .max = 9 },
  246. .p = { .min = 28, .max = 112 },
  247. .p1 = { .min = 2, .max = 8 },
  248. .p2 = { .dot_limit = 225000,
  249. .p2_slow = 14, .p2_fast = 14 },
  250. };
  251. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  252. .dot = { .min = 25000, .max = 350000 },
  253. .vco = { .min = 1760000, .max = 3510000 },
  254. .n = { .min = 1, .max = 3 },
  255. .m = { .min = 79, .max = 127 },
  256. .m1 = { .min = 12, .max = 22 },
  257. .m2 = { .min = 5, .max = 9 },
  258. .p = { .min = 14, .max = 56 },
  259. .p1 = { .min = 2, .max = 8 },
  260. .p2 = { .dot_limit = 225000,
  261. .p2_slow = 7, .p2_fast = 7 },
  262. };
  263. /* LVDS 100mhz refclk limits. */
  264. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  265. .dot = { .min = 25000, .max = 350000 },
  266. .vco = { .min = 1760000, .max = 3510000 },
  267. .n = { .min = 1, .max = 2 },
  268. .m = { .min = 79, .max = 126 },
  269. .m1 = { .min = 12, .max = 22 },
  270. .m2 = { .min = 5, .max = 9 },
  271. .p = { .min = 28, .max = 112 },
  272. .p1 = { .min = 2, .max = 8 },
  273. .p2 = { .dot_limit = 225000,
  274. .p2_slow = 14, .p2_fast = 14 },
  275. };
  276. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  277. .dot = { .min = 25000, .max = 350000 },
  278. .vco = { .min = 1760000, .max = 3510000 },
  279. .n = { .min = 1, .max = 3 },
  280. .m = { .min = 79, .max = 126 },
  281. .m1 = { .min = 12, .max = 22 },
  282. .m2 = { .min = 5, .max = 9 },
  283. .p = { .min = 14, .max = 42 },
  284. .p1 = { .min = 2, .max = 6 },
  285. .p2 = { .dot_limit = 225000,
  286. .p2_slow = 7, .p2_fast = 7 },
  287. };
  288. static const intel_limit_t intel_limits_vlv = {
  289. /*
  290. * These are the data rate limits (measured in fast clocks)
  291. * since those are the strictest limits we have. The fast
  292. * clock and actual rate limits are more relaxed, so checking
  293. * them would make no difference.
  294. */
  295. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  296. .vco = { .min = 4000000, .max = 6000000 },
  297. .n = { .min = 1, .max = 7 },
  298. .m1 = { .min = 2, .max = 3 },
  299. .m2 = { .min = 11, .max = 156 },
  300. .p1 = { .min = 2, .max = 3 },
  301. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  302. };
  303. static const intel_limit_t intel_limits_chv = {
  304. /*
  305. * These are the data rate limits (measured in fast clocks)
  306. * since those are the strictest limits we have. The fast
  307. * clock and actual rate limits are more relaxed, so checking
  308. * them would make no difference.
  309. */
  310. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  311. .vco = { .min = 4860000, .max = 6700000 },
  312. .n = { .min = 1, .max = 1 },
  313. .m1 = { .min = 2, .max = 2 },
  314. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  315. .p1 = { .min = 2, .max = 4 },
  316. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  317. };
  318. static void vlv_clock(int refclk, intel_clock_t *clock)
  319. {
  320. clock->m = clock->m1 * clock->m2;
  321. clock->p = clock->p1 * clock->p2;
  322. if (WARN_ON(clock->n == 0 || clock->p == 0))
  323. return;
  324. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  325. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  326. }
  327. /**
  328. * Returns whether any output on the specified pipe is of the specified type
  329. */
  330. static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  331. {
  332. struct drm_device *dev = crtc->dev;
  333. struct intel_encoder *encoder;
  334. for_each_encoder_on_crtc(dev, crtc, encoder)
  335. if (encoder->type == type)
  336. return true;
  337. return false;
  338. }
  339. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  340. int refclk)
  341. {
  342. struct drm_device *dev = crtc->dev;
  343. const intel_limit_t *limit;
  344. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  345. if (intel_is_dual_link_lvds(dev)) {
  346. if (refclk == 100000)
  347. limit = &intel_limits_ironlake_dual_lvds_100m;
  348. else
  349. limit = &intel_limits_ironlake_dual_lvds;
  350. } else {
  351. if (refclk == 100000)
  352. limit = &intel_limits_ironlake_single_lvds_100m;
  353. else
  354. limit = &intel_limits_ironlake_single_lvds;
  355. }
  356. } else
  357. limit = &intel_limits_ironlake_dac;
  358. return limit;
  359. }
  360. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  361. {
  362. struct drm_device *dev = crtc->dev;
  363. const intel_limit_t *limit;
  364. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  365. if (intel_is_dual_link_lvds(dev))
  366. limit = &intel_limits_g4x_dual_channel_lvds;
  367. else
  368. limit = &intel_limits_g4x_single_channel_lvds;
  369. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  370. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  371. limit = &intel_limits_g4x_hdmi;
  372. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  373. limit = &intel_limits_g4x_sdvo;
  374. } else /* The option is for other outputs */
  375. limit = &intel_limits_i9xx_sdvo;
  376. return limit;
  377. }
  378. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  379. {
  380. struct drm_device *dev = crtc->dev;
  381. const intel_limit_t *limit;
  382. if (HAS_PCH_SPLIT(dev))
  383. limit = intel_ironlake_limit(crtc, refclk);
  384. else if (IS_G4X(dev)) {
  385. limit = intel_g4x_limit(crtc);
  386. } else if (IS_PINEVIEW(dev)) {
  387. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  388. limit = &intel_limits_pineview_lvds;
  389. else
  390. limit = &intel_limits_pineview_sdvo;
  391. } else if (IS_CHERRYVIEW(dev)) {
  392. limit = &intel_limits_chv;
  393. } else if (IS_VALLEYVIEW(dev)) {
  394. limit = &intel_limits_vlv;
  395. } else if (!IS_GEN2(dev)) {
  396. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  397. limit = &intel_limits_i9xx_lvds;
  398. else
  399. limit = &intel_limits_i9xx_sdvo;
  400. } else {
  401. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  402. limit = &intel_limits_i8xx_lvds;
  403. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
  404. limit = &intel_limits_i8xx_dvo;
  405. else
  406. limit = &intel_limits_i8xx_dac;
  407. }
  408. return limit;
  409. }
  410. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  411. static void pineview_clock(int refclk, intel_clock_t *clock)
  412. {
  413. clock->m = clock->m2 + 2;
  414. clock->p = clock->p1 * clock->p2;
  415. if (WARN_ON(clock->n == 0 || clock->p == 0))
  416. return;
  417. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  418. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  419. }
  420. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  421. {
  422. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  423. }
  424. static void i9xx_clock(int refclk, intel_clock_t *clock)
  425. {
  426. clock->m = i9xx_dpll_compute_m(clock);
  427. clock->p = clock->p1 * clock->p2;
  428. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  429. return;
  430. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  431. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  432. }
  433. static void chv_clock(int refclk, intel_clock_t *clock)
  434. {
  435. clock->m = clock->m1 * clock->m2;
  436. clock->p = clock->p1 * clock->p2;
  437. if (WARN_ON(clock->n == 0 || clock->p == 0))
  438. return;
  439. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  440. clock->n << 22);
  441. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  442. }
  443. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  444. /**
  445. * Returns whether the given set of divisors are valid for a given refclk with
  446. * the given connectors.
  447. */
  448. static bool intel_PLL_is_valid(struct drm_device *dev,
  449. const intel_limit_t *limit,
  450. const intel_clock_t *clock)
  451. {
  452. if (clock->n < limit->n.min || limit->n.max < clock->n)
  453. INTELPllInvalid("n out of range\n");
  454. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  455. INTELPllInvalid("p1 out of range\n");
  456. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  457. INTELPllInvalid("m2 out of range\n");
  458. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  459. INTELPllInvalid("m1 out of range\n");
  460. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  461. if (clock->m1 <= clock->m2)
  462. INTELPllInvalid("m1 <= m2\n");
  463. if (!IS_VALLEYVIEW(dev)) {
  464. if (clock->p < limit->p.min || limit->p.max < clock->p)
  465. INTELPllInvalid("p out of range\n");
  466. if (clock->m < limit->m.min || limit->m.max < clock->m)
  467. INTELPllInvalid("m out of range\n");
  468. }
  469. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  470. INTELPllInvalid("vco out of range\n");
  471. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  472. * connector, etc., rather than just a single range.
  473. */
  474. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  475. INTELPllInvalid("dot out of range\n");
  476. return true;
  477. }
  478. static bool
  479. i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  480. int target, int refclk, intel_clock_t *match_clock,
  481. intel_clock_t *best_clock)
  482. {
  483. struct drm_device *dev = crtc->dev;
  484. intel_clock_t clock;
  485. int err = target;
  486. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  487. /*
  488. * For LVDS just rely on its current settings for dual-channel.
  489. * We haven't figured out how to reliably set up different
  490. * single/dual channel state, if we even can.
  491. */
  492. if (intel_is_dual_link_lvds(dev))
  493. clock.p2 = limit->p2.p2_fast;
  494. else
  495. clock.p2 = limit->p2.p2_slow;
  496. } else {
  497. if (target < limit->p2.dot_limit)
  498. clock.p2 = limit->p2.p2_slow;
  499. else
  500. clock.p2 = limit->p2.p2_fast;
  501. }
  502. memset(best_clock, 0, sizeof(*best_clock));
  503. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  504. clock.m1++) {
  505. for (clock.m2 = limit->m2.min;
  506. clock.m2 <= limit->m2.max; clock.m2++) {
  507. if (clock.m2 >= clock.m1)
  508. break;
  509. for (clock.n = limit->n.min;
  510. clock.n <= limit->n.max; clock.n++) {
  511. for (clock.p1 = limit->p1.min;
  512. clock.p1 <= limit->p1.max; clock.p1++) {
  513. int this_err;
  514. i9xx_clock(refclk, &clock);
  515. if (!intel_PLL_is_valid(dev, limit,
  516. &clock))
  517. continue;
  518. if (match_clock &&
  519. clock.p != match_clock->p)
  520. continue;
  521. this_err = abs(clock.dot - target);
  522. if (this_err < err) {
  523. *best_clock = clock;
  524. err = this_err;
  525. }
  526. }
  527. }
  528. }
  529. }
  530. return (err != target);
  531. }
  532. static bool
  533. pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  534. int target, int refclk, intel_clock_t *match_clock,
  535. intel_clock_t *best_clock)
  536. {
  537. struct drm_device *dev = crtc->dev;
  538. intel_clock_t clock;
  539. int err = target;
  540. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  541. /*
  542. * For LVDS just rely on its current settings for dual-channel.
  543. * We haven't figured out how to reliably set up different
  544. * single/dual channel state, if we even can.
  545. */
  546. if (intel_is_dual_link_lvds(dev))
  547. clock.p2 = limit->p2.p2_fast;
  548. else
  549. clock.p2 = limit->p2.p2_slow;
  550. } else {
  551. if (target < limit->p2.dot_limit)
  552. clock.p2 = limit->p2.p2_slow;
  553. else
  554. clock.p2 = limit->p2.p2_fast;
  555. }
  556. memset(best_clock, 0, sizeof(*best_clock));
  557. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  558. clock.m1++) {
  559. for (clock.m2 = limit->m2.min;
  560. clock.m2 <= limit->m2.max; clock.m2++) {
  561. for (clock.n = limit->n.min;
  562. clock.n <= limit->n.max; clock.n++) {
  563. for (clock.p1 = limit->p1.min;
  564. clock.p1 <= limit->p1.max; clock.p1++) {
  565. int this_err;
  566. pineview_clock(refclk, &clock);
  567. if (!intel_PLL_is_valid(dev, limit,
  568. &clock))
  569. continue;
  570. if (match_clock &&
  571. clock.p != match_clock->p)
  572. continue;
  573. this_err = abs(clock.dot - target);
  574. if (this_err < err) {
  575. *best_clock = clock;
  576. err = this_err;
  577. }
  578. }
  579. }
  580. }
  581. }
  582. return (err != target);
  583. }
  584. static bool
  585. g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  586. int target, int refclk, intel_clock_t *match_clock,
  587. intel_clock_t *best_clock)
  588. {
  589. struct drm_device *dev = crtc->dev;
  590. intel_clock_t clock;
  591. int max_n;
  592. bool found;
  593. /* approximately equals target * 0.00585 */
  594. int err_most = (target >> 8) + (target >> 9);
  595. found = false;
  596. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  597. if (intel_is_dual_link_lvds(dev))
  598. clock.p2 = limit->p2.p2_fast;
  599. else
  600. clock.p2 = limit->p2.p2_slow;
  601. } else {
  602. if (target < limit->p2.dot_limit)
  603. clock.p2 = limit->p2.p2_slow;
  604. else
  605. clock.p2 = limit->p2.p2_fast;
  606. }
  607. memset(best_clock, 0, sizeof(*best_clock));
  608. max_n = limit->n.max;
  609. /* based on hardware requirement, prefer smaller n to precision */
  610. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  611. /* based on hardware requirement, prefere larger m1,m2 */
  612. for (clock.m1 = limit->m1.max;
  613. clock.m1 >= limit->m1.min; clock.m1--) {
  614. for (clock.m2 = limit->m2.max;
  615. clock.m2 >= limit->m2.min; clock.m2--) {
  616. for (clock.p1 = limit->p1.max;
  617. clock.p1 >= limit->p1.min; clock.p1--) {
  618. int this_err;
  619. i9xx_clock(refclk, &clock);
  620. if (!intel_PLL_is_valid(dev, limit,
  621. &clock))
  622. continue;
  623. this_err = abs(clock.dot - target);
  624. if (this_err < err_most) {
  625. *best_clock = clock;
  626. err_most = this_err;
  627. max_n = clock.n;
  628. found = true;
  629. }
  630. }
  631. }
  632. }
  633. }
  634. return found;
  635. }
  636. static bool
  637. vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  638. int target, int refclk, intel_clock_t *match_clock,
  639. intel_clock_t *best_clock)
  640. {
  641. struct drm_device *dev = crtc->dev;
  642. intel_clock_t clock;
  643. unsigned int bestppm = 1000000;
  644. /* min update 19.2 MHz */
  645. int max_n = min(limit->n.max, refclk / 19200);
  646. bool found = false;
  647. target *= 5; /* fast clock */
  648. memset(best_clock, 0, sizeof(*best_clock));
  649. /* based on hardware requirement, prefer smaller n to precision */
  650. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  651. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  652. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  653. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  654. clock.p = clock.p1 * clock.p2;
  655. /* based on hardware requirement, prefer bigger m1,m2 values */
  656. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  657. unsigned int ppm, diff;
  658. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  659. refclk * clock.m1);
  660. vlv_clock(refclk, &clock);
  661. if (!intel_PLL_is_valid(dev, limit,
  662. &clock))
  663. continue;
  664. diff = abs(clock.dot - target);
  665. ppm = div_u64(1000000ULL * diff, target);
  666. if (ppm < 100 && clock.p > best_clock->p) {
  667. bestppm = 0;
  668. *best_clock = clock;
  669. found = true;
  670. }
  671. if (bestppm >= 10 && ppm < bestppm - 10) {
  672. bestppm = ppm;
  673. *best_clock = clock;
  674. found = true;
  675. }
  676. }
  677. }
  678. }
  679. }
  680. return found;
  681. }
  682. static bool
  683. chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  684. int target, int refclk, intel_clock_t *match_clock,
  685. intel_clock_t *best_clock)
  686. {
  687. struct drm_device *dev = crtc->dev;
  688. intel_clock_t clock;
  689. uint64_t m2;
  690. int found = false;
  691. memset(best_clock, 0, sizeof(*best_clock));
  692. /*
  693. * Based on hardware doc, the n always set to 1, and m1 always
  694. * set to 2. If requires to support 200Mhz refclk, we need to
  695. * revisit this because n may not 1 anymore.
  696. */
  697. clock.n = 1, clock.m1 = 2;
  698. target *= 5; /* fast clock */
  699. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  700. for (clock.p2 = limit->p2.p2_fast;
  701. clock.p2 >= limit->p2.p2_slow;
  702. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  703. clock.p = clock.p1 * clock.p2;
  704. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  705. clock.n) << 22, refclk * clock.m1);
  706. if (m2 > INT_MAX/clock.m1)
  707. continue;
  708. clock.m2 = m2;
  709. chv_clock(refclk, &clock);
  710. if (!intel_PLL_is_valid(dev, limit, &clock))
  711. continue;
  712. /* based on hardware requirement, prefer bigger p
  713. */
  714. if (clock.p > best_clock->p) {
  715. *best_clock = clock;
  716. found = true;
  717. }
  718. }
  719. }
  720. return found;
  721. }
  722. bool intel_crtc_active(struct drm_crtc *crtc)
  723. {
  724. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  725. /* Be paranoid as we can arrive here with only partial
  726. * state retrieved from the hardware during setup.
  727. *
  728. * We can ditch the adjusted_mode.crtc_clock check as soon
  729. * as Haswell has gained clock readout/fastboot support.
  730. *
  731. * We can ditch the crtc->primary->fb check as soon as we can
  732. * properly reconstruct framebuffers.
  733. */
  734. return intel_crtc->active && crtc->primary->fb &&
  735. intel_crtc->config.adjusted_mode.crtc_clock;
  736. }
  737. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  738. enum pipe pipe)
  739. {
  740. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  741. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  742. return intel_crtc->config.cpu_transcoder;
  743. }
  744. static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
  745. {
  746. struct drm_i915_private *dev_priv = dev->dev_private;
  747. u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
  748. frame = I915_READ(frame_reg);
  749. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  750. WARN(1, "vblank wait timed out\n");
  751. }
  752. /**
  753. * intel_wait_for_vblank - wait for vblank on a given pipe
  754. * @dev: drm device
  755. * @pipe: pipe to wait for
  756. *
  757. * Wait for vblank to occur on a given pipe. Needed for various bits of
  758. * mode setting code.
  759. */
  760. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  761. {
  762. struct drm_i915_private *dev_priv = dev->dev_private;
  763. int pipestat_reg = PIPESTAT(pipe);
  764. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  765. g4x_wait_for_vblank(dev, pipe);
  766. return;
  767. }
  768. /* Clear existing vblank status. Note this will clear any other
  769. * sticky status fields as well.
  770. *
  771. * This races with i915_driver_irq_handler() with the result
  772. * that either function could miss a vblank event. Here it is not
  773. * fatal, as we will either wait upon the next vblank interrupt or
  774. * timeout. Generally speaking intel_wait_for_vblank() is only
  775. * called during modeset at which time the GPU should be idle and
  776. * should *not* be performing page flips and thus not waiting on
  777. * vblanks...
  778. * Currently, the result of us stealing a vblank from the irq
  779. * handler is that a single frame will be skipped during swapbuffers.
  780. */
  781. I915_WRITE(pipestat_reg,
  782. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  783. /* Wait for vblank interrupt bit to set */
  784. if (wait_for(I915_READ(pipestat_reg) &
  785. PIPE_VBLANK_INTERRUPT_STATUS,
  786. 50))
  787. DRM_DEBUG_KMS("vblank wait timed out\n");
  788. }
  789. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  790. {
  791. struct drm_i915_private *dev_priv = dev->dev_private;
  792. u32 reg = PIPEDSL(pipe);
  793. u32 line1, line2;
  794. u32 line_mask;
  795. if (IS_GEN2(dev))
  796. line_mask = DSL_LINEMASK_GEN2;
  797. else
  798. line_mask = DSL_LINEMASK_GEN3;
  799. line1 = I915_READ(reg) & line_mask;
  800. mdelay(5);
  801. line2 = I915_READ(reg) & line_mask;
  802. return line1 == line2;
  803. }
  804. /*
  805. * intel_wait_for_pipe_off - wait for pipe to turn off
  806. * @dev: drm device
  807. * @pipe: pipe to wait for
  808. *
  809. * After disabling a pipe, we can't wait for vblank in the usual way,
  810. * spinning on the vblank interrupt status bit, since we won't actually
  811. * see an interrupt when the pipe is disabled.
  812. *
  813. * On Gen4 and above:
  814. * wait for the pipe register state bit to turn off
  815. *
  816. * Otherwise:
  817. * wait for the display line value to settle (it usually
  818. * ends up stopping at the start of the next frame).
  819. *
  820. */
  821. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  822. {
  823. struct drm_i915_private *dev_priv = dev->dev_private;
  824. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  825. pipe);
  826. if (INTEL_INFO(dev)->gen >= 4) {
  827. int reg = PIPECONF(cpu_transcoder);
  828. /* Wait for the Pipe State to go off */
  829. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  830. 100))
  831. WARN(1, "pipe_off wait timed out\n");
  832. } else {
  833. /* Wait for the display line to settle */
  834. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  835. WARN(1, "pipe_off wait timed out\n");
  836. }
  837. }
  838. /*
  839. * ibx_digital_port_connected - is the specified port connected?
  840. * @dev_priv: i915 private structure
  841. * @port: the port to test
  842. *
  843. * Returns true if @port is connected, false otherwise.
  844. */
  845. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  846. struct intel_digital_port *port)
  847. {
  848. u32 bit;
  849. if (HAS_PCH_IBX(dev_priv->dev)) {
  850. switch(port->port) {
  851. case PORT_B:
  852. bit = SDE_PORTB_HOTPLUG;
  853. break;
  854. case PORT_C:
  855. bit = SDE_PORTC_HOTPLUG;
  856. break;
  857. case PORT_D:
  858. bit = SDE_PORTD_HOTPLUG;
  859. break;
  860. default:
  861. return true;
  862. }
  863. } else {
  864. switch(port->port) {
  865. case PORT_B:
  866. bit = SDE_PORTB_HOTPLUG_CPT;
  867. break;
  868. case PORT_C:
  869. bit = SDE_PORTC_HOTPLUG_CPT;
  870. break;
  871. case PORT_D:
  872. bit = SDE_PORTD_HOTPLUG_CPT;
  873. break;
  874. default:
  875. return true;
  876. }
  877. }
  878. return I915_READ(SDEISR) & bit;
  879. }
  880. static const char *state_string(bool enabled)
  881. {
  882. return enabled ? "on" : "off";
  883. }
  884. /* Only for pre-ILK configs */
  885. void assert_pll(struct drm_i915_private *dev_priv,
  886. enum pipe pipe, bool state)
  887. {
  888. int reg;
  889. u32 val;
  890. bool cur_state;
  891. reg = DPLL(pipe);
  892. val = I915_READ(reg);
  893. cur_state = !!(val & DPLL_VCO_ENABLE);
  894. WARN(cur_state != state,
  895. "PLL state assertion failure (expected %s, current %s)\n",
  896. state_string(state), state_string(cur_state));
  897. }
  898. /* XXX: the dsi pll is shared between MIPI DSI ports */
  899. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  900. {
  901. u32 val;
  902. bool cur_state;
  903. mutex_lock(&dev_priv->dpio_lock);
  904. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  905. mutex_unlock(&dev_priv->dpio_lock);
  906. cur_state = val & DSI_PLL_VCO_EN;
  907. WARN(cur_state != state,
  908. "DSI PLL state assertion failure (expected %s, current %s)\n",
  909. state_string(state), state_string(cur_state));
  910. }
  911. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  912. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  913. struct intel_shared_dpll *
  914. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  915. {
  916. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  917. if (crtc->config.shared_dpll < 0)
  918. return NULL;
  919. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  920. }
  921. /* For ILK+ */
  922. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  923. struct intel_shared_dpll *pll,
  924. bool state)
  925. {
  926. bool cur_state;
  927. struct intel_dpll_hw_state hw_state;
  928. if (HAS_PCH_LPT(dev_priv->dev)) {
  929. DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
  930. return;
  931. }
  932. if (WARN (!pll,
  933. "asserting DPLL %s with no DPLL\n", state_string(state)))
  934. return;
  935. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  936. WARN(cur_state != state,
  937. "%s assertion failure (expected %s, current %s)\n",
  938. pll->name, state_string(state), state_string(cur_state));
  939. }
  940. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  941. enum pipe pipe, bool state)
  942. {
  943. int reg;
  944. u32 val;
  945. bool cur_state;
  946. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  947. pipe);
  948. if (HAS_DDI(dev_priv->dev)) {
  949. /* DDI does not have a specific FDI_TX register */
  950. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  951. val = I915_READ(reg);
  952. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  953. } else {
  954. reg = FDI_TX_CTL(pipe);
  955. val = I915_READ(reg);
  956. cur_state = !!(val & FDI_TX_ENABLE);
  957. }
  958. WARN(cur_state != state,
  959. "FDI TX state assertion failure (expected %s, current %s)\n",
  960. state_string(state), state_string(cur_state));
  961. }
  962. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  963. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  964. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  965. enum pipe pipe, bool state)
  966. {
  967. int reg;
  968. u32 val;
  969. bool cur_state;
  970. reg = FDI_RX_CTL(pipe);
  971. val = I915_READ(reg);
  972. cur_state = !!(val & FDI_RX_ENABLE);
  973. WARN(cur_state != state,
  974. "FDI RX state assertion failure (expected %s, current %s)\n",
  975. state_string(state), state_string(cur_state));
  976. }
  977. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  978. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  979. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  980. enum pipe pipe)
  981. {
  982. int reg;
  983. u32 val;
  984. /* ILK FDI PLL is always enabled */
  985. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  986. return;
  987. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  988. if (HAS_DDI(dev_priv->dev))
  989. return;
  990. reg = FDI_TX_CTL(pipe);
  991. val = I915_READ(reg);
  992. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  993. }
  994. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  995. enum pipe pipe, bool state)
  996. {
  997. int reg;
  998. u32 val;
  999. bool cur_state;
  1000. reg = FDI_RX_CTL(pipe);
  1001. val = I915_READ(reg);
  1002. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1003. WARN(cur_state != state,
  1004. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1005. state_string(state), state_string(cur_state));
  1006. }
  1007. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1008. enum pipe pipe)
  1009. {
  1010. int pp_reg, lvds_reg;
  1011. u32 val;
  1012. enum pipe panel_pipe = PIPE_A;
  1013. bool locked = true;
  1014. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  1015. pp_reg = PCH_PP_CONTROL;
  1016. lvds_reg = PCH_LVDS;
  1017. } else {
  1018. pp_reg = PP_CONTROL;
  1019. lvds_reg = LVDS;
  1020. }
  1021. val = I915_READ(pp_reg);
  1022. if (!(val & PANEL_POWER_ON) ||
  1023. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  1024. locked = false;
  1025. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  1026. panel_pipe = PIPE_B;
  1027. WARN(panel_pipe == pipe && locked,
  1028. "panel assertion failure, pipe %c regs locked\n",
  1029. pipe_name(pipe));
  1030. }
  1031. static void assert_cursor(struct drm_i915_private *dev_priv,
  1032. enum pipe pipe, bool state)
  1033. {
  1034. struct drm_device *dev = dev_priv->dev;
  1035. bool cur_state;
  1036. if (IS_845G(dev) || IS_I865G(dev))
  1037. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1038. else if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev))
  1039. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1040. else
  1041. cur_state = I915_READ(CURCNTR_IVB(pipe)) & CURSOR_MODE;
  1042. WARN(cur_state != state,
  1043. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1044. pipe_name(pipe), state_string(state), state_string(cur_state));
  1045. }
  1046. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1047. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1048. void assert_pipe(struct drm_i915_private *dev_priv,
  1049. enum pipe pipe, bool state)
  1050. {
  1051. int reg;
  1052. u32 val;
  1053. bool cur_state;
  1054. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1055. pipe);
  1056. /* if we need the pipe A quirk it must be always on */
  1057. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  1058. state = true;
  1059. if (!intel_display_power_enabled(dev_priv,
  1060. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1061. cur_state = false;
  1062. } else {
  1063. reg = PIPECONF(cpu_transcoder);
  1064. val = I915_READ(reg);
  1065. cur_state = !!(val & PIPECONF_ENABLE);
  1066. }
  1067. WARN(cur_state != state,
  1068. "pipe %c assertion failure (expected %s, current %s)\n",
  1069. pipe_name(pipe), state_string(state), state_string(cur_state));
  1070. }
  1071. static void assert_plane(struct drm_i915_private *dev_priv,
  1072. enum plane plane, bool state)
  1073. {
  1074. int reg;
  1075. u32 val;
  1076. bool cur_state;
  1077. reg = DSPCNTR(plane);
  1078. val = I915_READ(reg);
  1079. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1080. WARN(cur_state != state,
  1081. "plane %c assertion failure (expected %s, current %s)\n",
  1082. plane_name(plane), state_string(state), state_string(cur_state));
  1083. }
  1084. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1085. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1086. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1087. enum pipe pipe)
  1088. {
  1089. struct drm_device *dev = dev_priv->dev;
  1090. int reg, i;
  1091. u32 val;
  1092. int cur_pipe;
  1093. /* Primary planes are fixed to pipes on gen4+ */
  1094. if (INTEL_INFO(dev)->gen >= 4) {
  1095. reg = DSPCNTR(pipe);
  1096. val = I915_READ(reg);
  1097. WARN(val & DISPLAY_PLANE_ENABLE,
  1098. "plane %c assertion failure, should be disabled but not\n",
  1099. plane_name(pipe));
  1100. return;
  1101. }
  1102. /* Need to check both planes against the pipe */
  1103. for_each_pipe(i) {
  1104. reg = DSPCNTR(i);
  1105. val = I915_READ(reg);
  1106. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1107. DISPPLANE_SEL_PIPE_SHIFT;
  1108. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1109. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1110. plane_name(i), pipe_name(pipe));
  1111. }
  1112. }
  1113. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1114. enum pipe pipe)
  1115. {
  1116. struct drm_device *dev = dev_priv->dev;
  1117. int reg, sprite;
  1118. u32 val;
  1119. if (IS_VALLEYVIEW(dev)) {
  1120. for_each_sprite(pipe, sprite) {
  1121. reg = SPCNTR(pipe, sprite);
  1122. val = I915_READ(reg);
  1123. WARN(val & SP_ENABLE,
  1124. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1125. sprite_name(pipe, sprite), pipe_name(pipe));
  1126. }
  1127. } else if (INTEL_INFO(dev)->gen >= 7) {
  1128. reg = SPRCTL(pipe);
  1129. val = I915_READ(reg);
  1130. WARN(val & SPRITE_ENABLE,
  1131. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1132. plane_name(pipe), pipe_name(pipe));
  1133. } else if (INTEL_INFO(dev)->gen >= 5) {
  1134. reg = DVSCNTR(pipe);
  1135. val = I915_READ(reg);
  1136. WARN(val & DVS_ENABLE,
  1137. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1138. plane_name(pipe), pipe_name(pipe));
  1139. }
  1140. }
  1141. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1142. {
  1143. u32 val;
  1144. bool enabled;
  1145. WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1146. val = I915_READ(PCH_DREF_CONTROL);
  1147. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1148. DREF_SUPERSPREAD_SOURCE_MASK));
  1149. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1150. }
  1151. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1152. enum pipe pipe)
  1153. {
  1154. int reg;
  1155. u32 val;
  1156. bool enabled;
  1157. reg = PCH_TRANSCONF(pipe);
  1158. val = I915_READ(reg);
  1159. enabled = !!(val & TRANS_ENABLE);
  1160. WARN(enabled,
  1161. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1162. pipe_name(pipe));
  1163. }
  1164. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1165. enum pipe pipe, u32 port_sel, u32 val)
  1166. {
  1167. if ((val & DP_PORT_EN) == 0)
  1168. return false;
  1169. if (HAS_PCH_CPT(dev_priv->dev)) {
  1170. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1171. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1172. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1173. return false;
  1174. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1175. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1176. return false;
  1177. } else {
  1178. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1179. return false;
  1180. }
  1181. return true;
  1182. }
  1183. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1184. enum pipe pipe, u32 val)
  1185. {
  1186. if ((val & SDVO_ENABLE) == 0)
  1187. return false;
  1188. if (HAS_PCH_CPT(dev_priv->dev)) {
  1189. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1190. return false;
  1191. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1192. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1193. return false;
  1194. } else {
  1195. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1196. return false;
  1197. }
  1198. return true;
  1199. }
  1200. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1201. enum pipe pipe, u32 val)
  1202. {
  1203. if ((val & LVDS_PORT_EN) == 0)
  1204. return false;
  1205. if (HAS_PCH_CPT(dev_priv->dev)) {
  1206. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1207. return false;
  1208. } else {
  1209. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1210. return false;
  1211. }
  1212. return true;
  1213. }
  1214. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1215. enum pipe pipe, u32 val)
  1216. {
  1217. if ((val & ADPA_DAC_ENABLE) == 0)
  1218. return false;
  1219. if (HAS_PCH_CPT(dev_priv->dev)) {
  1220. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1221. return false;
  1222. } else {
  1223. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1224. return false;
  1225. }
  1226. return true;
  1227. }
  1228. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1229. enum pipe pipe, int reg, u32 port_sel)
  1230. {
  1231. u32 val = I915_READ(reg);
  1232. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1233. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1234. reg, pipe_name(pipe));
  1235. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1236. && (val & DP_PIPEB_SELECT),
  1237. "IBX PCH dp port still using transcoder B\n");
  1238. }
  1239. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1240. enum pipe pipe, int reg)
  1241. {
  1242. u32 val = I915_READ(reg);
  1243. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1244. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1245. reg, pipe_name(pipe));
  1246. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1247. && (val & SDVO_PIPE_B_SELECT),
  1248. "IBX PCH hdmi port still using transcoder B\n");
  1249. }
  1250. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1251. enum pipe pipe)
  1252. {
  1253. int reg;
  1254. u32 val;
  1255. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1256. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1257. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1258. reg = PCH_ADPA;
  1259. val = I915_READ(reg);
  1260. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1261. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1262. pipe_name(pipe));
  1263. reg = PCH_LVDS;
  1264. val = I915_READ(reg);
  1265. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1266. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1267. pipe_name(pipe));
  1268. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1269. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1270. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1271. }
  1272. static void intel_init_dpio(struct drm_device *dev)
  1273. {
  1274. struct drm_i915_private *dev_priv = dev->dev_private;
  1275. if (!IS_VALLEYVIEW(dev))
  1276. return;
  1277. /*
  1278. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1279. * CHV x1 PHY (DP/HDMI D)
  1280. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1281. */
  1282. if (IS_CHERRYVIEW(dev)) {
  1283. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1284. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1285. } else {
  1286. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1287. }
  1288. }
  1289. static void intel_reset_dpio(struct drm_device *dev)
  1290. {
  1291. struct drm_i915_private *dev_priv = dev->dev_private;
  1292. if (!IS_VALLEYVIEW(dev))
  1293. return;
  1294. /*
  1295. * Enable the CRI clock source so we can get at the display and the
  1296. * reference clock for VGA hotplug / manual detection.
  1297. */
  1298. I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
  1299. DPLL_REFA_CLK_ENABLE_VLV |
  1300. DPLL_INTEGRATED_CRI_CLK_VLV);
  1301. if (IS_CHERRYVIEW(dev)) {
  1302. enum dpio_phy phy;
  1303. u32 val;
  1304. for (phy = DPIO_PHY0; phy < I915_NUM_PHYS_VLV; phy++) {
  1305. /* Poll for phypwrgood signal */
  1306. if (wait_for(I915_READ(DISPLAY_PHY_STATUS) &
  1307. PHY_POWERGOOD(phy), 1))
  1308. DRM_ERROR("Display PHY %d is not power up\n", phy);
  1309. /*
  1310. * Deassert common lane reset for PHY.
  1311. *
  1312. * This should only be done on init and resume from S3
  1313. * with both PLLs disabled, or we risk losing DPIO and
  1314. * PLL synchronization.
  1315. */
  1316. val = I915_READ(DISPLAY_PHY_CONTROL);
  1317. I915_WRITE(DISPLAY_PHY_CONTROL,
  1318. PHY_COM_LANE_RESET_DEASSERT(phy, val));
  1319. }
  1320. } else {
  1321. /*
  1322. * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
  1323. * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
  1324. * a. GUnit 0x2110 bit[0] set to 1 (def 0)
  1325. * b. The other bits such as sfr settings / modesel may all
  1326. * be set to 0.
  1327. *
  1328. * This should only be done on init and resume from S3 with
  1329. * both PLLs disabled, or we risk losing DPIO and PLL
  1330. * synchronization.
  1331. */
  1332. I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
  1333. }
  1334. }
  1335. static void vlv_enable_pll(struct intel_crtc *crtc)
  1336. {
  1337. struct drm_device *dev = crtc->base.dev;
  1338. struct drm_i915_private *dev_priv = dev->dev_private;
  1339. int reg = DPLL(crtc->pipe);
  1340. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1341. assert_pipe_disabled(dev_priv, crtc->pipe);
  1342. /* No really, not for ILK+ */
  1343. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1344. /* PLL is protected by panel, make sure we can write it */
  1345. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1346. assert_panel_unlocked(dev_priv, crtc->pipe);
  1347. I915_WRITE(reg, dpll);
  1348. POSTING_READ(reg);
  1349. udelay(150);
  1350. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1351. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1352. I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
  1353. POSTING_READ(DPLL_MD(crtc->pipe));
  1354. /* We do this three times for luck */
  1355. I915_WRITE(reg, dpll);
  1356. POSTING_READ(reg);
  1357. udelay(150); /* wait for warmup */
  1358. I915_WRITE(reg, dpll);
  1359. POSTING_READ(reg);
  1360. udelay(150); /* wait for warmup */
  1361. I915_WRITE(reg, dpll);
  1362. POSTING_READ(reg);
  1363. udelay(150); /* wait for warmup */
  1364. }
  1365. static void chv_enable_pll(struct intel_crtc *crtc)
  1366. {
  1367. struct drm_device *dev = crtc->base.dev;
  1368. struct drm_i915_private *dev_priv = dev->dev_private;
  1369. int pipe = crtc->pipe;
  1370. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1371. int dpll = DPLL(crtc->pipe);
  1372. u32 tmp;
  1373. assert_pipe_disabled(dev_priv, crtc->pipe);
  1374. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1375. mutex_lock(&dev_priv->dpio_lock);
  1376. /* Enable back the 10bit clock to display controller */
  1377. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1378. tmp |= DPIO_DCLKP_EN;
  1379. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1380. /*
  1381. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1382. */
  1383. udelay(1);
  1384. /* Enable PLL */
  1385. tmp = I915_READ(dpll);
  1386. tmp |= DPLL_VCO_ENABLE;
  1387. I915_WRITE(dpll, tmp);
  1388. /* Check PLL is locked */
  1389. if (wait_for(((I915_READ(dpll) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1390. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1391. /* Deassert soft data lane reset*/
  1392. tmp = vlv_dpio_read(dev_priv, pipe, VLV_PCS_DW0(port));
  1393. tmp |= (DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET);
  1394. vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW0(port), tmp);
  1395. mutex_unlock(&dev_priv->dpio_lock);
  1396. }
  1397. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1398. {
  1399. struct drm_device *dev = crtc->base.dev;
  1400. struct drm_i915_private *dev_priv = dev->dev_private;
  1401. int reg = DPLL(crtc->pipe);
  1402. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1403. assert_pipe_disabled(dev_priv, crtc->pipe);
  1404. /* No really, not for ILK+ */
  1405. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1406. /* PLL is protected by panel, make sure we can write it */
  1407. if (IS_MOBILE(dev) && !IS_I830(dev))
  1408. assert_panel_unlocked(dev_priv, crtc->pipe);
  1409. I915_WRITE(reg, dpll);
  1410. /* Wait for the clocks to stabilize. */
  1411. POSTING_READ(reg);
  1412. udelay(150);
  1413. if (INTEL_INFO(dev)->gen >= 4) {
  1414. I915_WRITE(DPLL_MD(crtc->pipe),
  1415. crtc->config.dpll_hw_state.dpll_md);
  1416. } else {
  1417. /* The pixel multiplier can only be updated once the
  1418. * DPLL is enabled and the clocks are stable.
  1419. *
  1420. * So write it again.
  1421. */
  1422. I915_WRITE(reg, dpll);
  1423. }
  1424. /* We do this three times for luck */
  1425. I915_WRITE(reg, dpll);
  1426. POSTING_READ(reg);
  1427. udelay(150); /* wait for warmup */
  1428. I915_WRITE(reg, dpll);
  1429. POSTING_READ(reg);
  1430. udelay(150); /* wait for warmup */
  1431. I915_WRITE(reg, dpll);
  1432. POSTING_READ(reg);
  1433. udelay(150); /* wait for warmup */
  1434. }
  1435. /**
  1436. * i9xx_disable_pll - disable a PLL
  1437. * @dev_priv: i915 private structure
  1438. * @pipe: pipe PLL to disable
  1439. *
  1440. * Disable the PLL for @pipe, making sure the pipe is off first.
  1441. *
  1442. * Note! This is for pre-ILK only.
  1443. */
  1444. static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1445. {
  1446. /* Don't disable pipe A or pipe A PLLs if needed */
  1447. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1448. return;
  1449. /* Make sure the pipe isn't still relying on us */
  1450. assert_pipe_disabled(dev_priv, pipe);
  1451. I915_WRITE(DPLL(pipe), 0);
  1452. POSTING_READ(DPLL(pipe));
  1453. }
  1454. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1455. {
  1456. u32 val = 0;
  1457. /* Make sure the pipe isn't still relying on us */
  1458. assert_pipe_disabled(dev_priv, pipe);
  1459. /*
  1460. * Leave integrated clock source and reference clock enabled for pipe B.
  1461. * The latter is needed for VGA hotplug / manual detection.
  1462. */
  1463. if (pipe == PIPE_B)
  1464. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1465. I915_WRITE(DPLL(pipe), val);
  1466. POSTING_READ(DPLL(pipe));
  1467. }
  1468. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1469. {
  1470. int dpll = DPLL(pipe);
  1471. u32 val;
  1472. /* Set PLL en = 0 */
  1473. val = I915_READ(dpll);
  1474. val &= ~DPLL_VCO_ENABLE;
  1475. I915_WRITE(dpll, val);
  1476. }
  1477. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1478. struct intel_digital_port *dport)
  1479. {
  1480. u32 port_mask;
  1481. int dpll_reg;
  1482. switch (dport->port) {
  1483. case PORT_B:
  1484. port_mask = DPLL_PORTB_READY_MASK;
  1485. dpll_reg = DPLL(0);
  1486. break;
  1487. case PORT_C:
  1488. port_mask = DPLL_PORTC_READY_MASK;
  1489. dpll_reg = DPLL(0);
  1490. break;
  1491. case PORT_D:
  1492. port_mask = DPLL_PORTD_READY_MASK;
  1493. dpll_reg = DPIO_PHY_STATUS;
  1494. break;
  1495. default:
  1496. BUG();
  1497. }
  1498. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1499. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1500. port_name(dport->port), I915_READ(dpll_reg));
  1501. }
  1502. /**
  1503. * ironlake_enable_shared_dpll - enable PCH PLL
  1504. * @dev_priv: i915 private structure
  1505. * @pipe: pipe PLL to enable
  1506. *
  1507. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1508. * drives the transcoder clock.
  1509. */
  1510. static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
  1511. {
  1512. struct drm_device *dev = crtc->base.dev;
  1513. struct drm_i915_private *dev_priv = dev->dev_private;
  1514. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1515. /* PCH PLLs only available on ILK, SNB and IVB */
  1516. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1517. if (WARN_ON(pll == NULL))
  1518. return;
  1519. if (WARN_ON(pll->refcount == 0))
  1520. return;
  1521. DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
  1522. pll->name, pll->active, pll->on,
  1523. crtc->base.base.id);
  1524. if (pll->active++) {
  1525. WARN_ON(!pll->on);
  1526. assert_shared_dpll_enabled(dev_priv, pll);
  1527. return;
  1528. }
  1529. WARN_ON(pll->on);
  1530. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1531. pll->enable(dev_priv, pll);
  1532. pll->on = true;
  1533. }
  1534. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1535. {
  1536. struct drm_device *dev = crtc->base.dev;
  1537. struct drm_i915_private *dev_priv = dev->dev_private;
  1538. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1539. /* PCH only available on ILK+ */
  1540. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1541. if (WARN_ON(pll == NULL))
  1542. return;
  1543. if (WARN_ON(pll->refcount == 0))
  1544. return;
  1545. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1546. pll->name, pll->active, pll->on,
  1547. crtc->base.base.id);
  1548. if (WARN_ON(pll->active == 0)) {
  1549. assert_shared_dpll_disabled(dev_priv, pll);
  1550. return;
  1551. }
  1552. assert_shared_dpll_enabled(dev_priv, pll);
  1553. WARN_ON(!pll->on);
  1554. if (--pll->active)
  1555. return;
  1556. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1557. pll->disable(dev_priv, pll);
  1558. pll->on = false;
  1559. }
  1560. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1561. enum pipe pipe)
  1562. {
  1563. struct drm_device *dev = dev_priv->dev;
  1564. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1565. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1566. uint32_t reg, val, pipeconf_val;
  1567. /* PCH only available on ILK+ */
  1568. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1569. /* Make sure PCH DPLL is enabled */
  1570. assert_shared_dpll_enabled(dev_priv,
  1571. intel_crtc_to_shared_dpll(intel_crtc));
  1572. /* FDI must be feeding us bits for PCH ports */
  1573. assert_fdi_tx_enabled(dev_priv, pipe);
  1574. assert_fdi_rx_enabled(dev_priv, pipe);
  1575. if (HAS_PCH_CPT(dev)) {
  1576. /* Workaround: Set the timing override bit before enabling the
  1577. * pch transcoder. */
  1578. reg = TRANS_CHICKEN2(pipe);
  1579. val = I915_READ(reg);
  1580. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1581. I915_WRITE(reg, val);
  1582. }
  1583. reg = PCH_TRANSCONF(pipe);
  1584. val = I915_READ(reg);
  1585. pipeconf_val = I915_READ(PIPECONF(pipe));
  1586. if (HAS_PCH_IBX(dev_priv->dev)) {
  1587. /*
  1588. * make the BPC in transcoder be consistent with
  1589. * that in pipeconf reg.
  1590. */
  1591. val &= ~PIPECONF_BPC_MASK;
  1592. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1593. }
  1594. val &= ~TRANS_INTERLACE_MASK;
  1595. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1596. if (HAS_PCH_IBX(dev_priv->dev) &&
  1597. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1598. val |= TRANS_LEGACY_INTERLACED_ILK;
  1599. else
  1600. val |= TRANS_INTERLACED;
  1601. else
  1602. val |= TRANS_PROGRESSIVE;
  1603. I915_WRITE(reg, val | TRANS_ENABLE);
  1604. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1605. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1606. }
  1607. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1608. enum transcoder cpu_transcoder)
  1609. {
  1610. u32 val, pipeconf_val;
  1611. /* PCH only available on ILK+ */
  1612. BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
  1613. /* FDI must be feeding us bits for PCH ports */
  1614. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1615. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1616. /* Workaround: set timing override bit. */
  1617. val = I915_READ(_TRANSA_CHICKEN2);
  1618. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1619. I915_WRITE(_TRANSA_CHICKEN2, val);
  1620. val = TRANS_ENABLE;
  1621. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1622. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1623. PIPECONF_INTERLACED_ILK)
  1624. val |= TRANS_INTERLACED;
  1625. else
  1626. val |= TRANS_PROGRESSIVE;
  1627. I915_WRITE(LPT_TRANSCONF, val);
  1628. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1629. DRM_ERROR("Failed to enable PCH transcoder\n");
  1630. }
  1631. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1632. enum pipe pipe)
  1633. {
  1634. struct drm_device *dev = dev_priv->dev;
  1635. uint32_t reg, val;
  1636. /* FDI relies on the transcoder */
  1637. assert_fdi_tx_disabled(dev_priv, pipe);
  1638. assert_fdi_rx_disabled(dev_priv, pipe);
  1639. /* Ports must be off as well */
  1640. assert_pch_ports_disabled(dev_priv, pipe);
  1641. reg = PCH_TRANSCONF(pipe);
  1642. val = I915_READ(reg);
  1643. val &= ~TRANS_ENABLE;
  1644. I915_WRITE(reg, val);
  1645. /* wait for PCH transcoder off, transcoder state */
  1646. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1647. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1648. if (!HAS_PCH_IBX(dev)) {
  1649. /* Workaround: Clear the timing override chicken bit again. */
  1650. reg = TRANS_CHICKEN2(pipe);
  1651. val = I915_READ(reg);
  1652. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1653. I915_WRITE(reg, val);
  1654. }
  1655. }
  1656. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1657. {
  1658. u32 val;
  1659. val = I915_READ(LPT_TRANSCONF);
  1660. val &= ~TRANS_ENABLE;
  1661. I915_WRITE(LPT_TRANSCONF, val);
  1662. /* wait for PCH transcoder off, transcoder state */
  1663. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1664. DRM_ERROR("Failed to disable PCH transcoder\n");
  1665. /* Workaround: clear timing override bit. */
  1666. val = I915_READ(_TRANSA_CHICKEN2);
  1667. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1668. I915_WRITE(_TRANSA_CHICKEN2, val);
  1669. }
  1670. /**
  1671. * intel_enable_pipe - enable a pipe, asserting requirements
  1672. * @crtc: crtc responsible for the pipe
  1673. *
  1674. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1675. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1676. */
  1677. static void intel_enable_pipe(struct intel_crtc *crtc)
  1678. {
  1679. struct drm_device *dev = crtc->base.dev;
  1680. struct drm_i915_private *dev_priv = dev->dev_private;
  1681. enum pipe pipe = crtc->pipe;
  1682. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1683. pipe);
  1684. enum pipe pch_transcoder;
  1685. int reg;
  1686. u32 val;
  1687. assert_planes_disabled(dev_priv, pipe);
  1688. assert_cursor_disabled(dev_priv, pipe);
  1689. assert_sprites_disabled(dev_priv, pipe);
  1690. if (HAS_PCH_LPT(dev_priv->dev))
  1691. pch_transcoder = TRANSCODER_A;
  1692. else
  1693. pch_transcoder = pipe;
  1694. /*
  1695. * A pipe without a PLL won't actually be able to drive bits from
  1696. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1697. * need the check.
  1698. */
  1699. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1700. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
  1701. assert_dsi_pll_enabled(dev_priv);
  1702. else
  1703. assert_pll_enabled(dev_priv, pipe);
  1704. else {
  1705. if (crtc->config.has_pch_encoder) {
  1706. /* if driving the PCH, we need FDI enabled */
  1707. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1708. assert_fdi_tx_pll_enabled(dev_priv,
  1709. (enum pipe) cpu_transcoder);
  1710. }
  1711. /* FIXME: assert CPU port conditions for SNB+ */
  1712. }
  1713. reg = PIPECONF(cpu_transcoder);
  1714. val = I915_READ(reg);
  1715. if (val & PIPECONF_ENABLE) {
  1716. WARN_ON(!(pipe == PIPE_A &&
  1717. dev_priv->quirks & QUIRK_PIPEA_FORCE));
  1718. return;
  1719. }
  1720. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1721. POSTING_READ(reg);
  1722. }
  1723. /**
  1724. * intel_disable_pipe - disable a pipe, asserting requirements
  1725. * @dev_priv: i915 private structure
  1726. * @pipe: pipe to disable
  1727. *
  1728. * Disable @pipe, making sure that various hardware specific requirements
  1729. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1730. *
  1731. * @pipe should be %PIPE_A or %PIPE_B.
  1732. *
  1733. * Will wait until the pipe has shut down before returning.
  1734. */
  1735. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1736. enum pipe pipe)
  1737. {
  1738. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1739. pipe);
  1740. int reg;
  1741. u32 val;
  1742. /*
  1743. * Make sure planes won't keep trying to pump pixels to us,
  1744. * or we might hang the display.
  1745. */
  1746. assert_planes_disabled(dev_priv, pipe);
  1747. assert_cursor_disabled(dev_priv, pipe);
  1748. assert_sprites_disabled(dev_priv, pipe);
  1749. /* Don't disable pipe A or pipe A PLLs if needed */
  1750. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1751. return;
  1752. reg = PIPECONF(cpu_transcoder);
  1753. val = I915_READ(reg);
  1754. if ((val & PIPECONF_ENABLE) == 0)
  1755. return;
  1756. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1757. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1758. }
  1759. /*
  1760. * Plane regs are double buffered, going from enabled->disabled needs a
  1761. * trigger in order to latch. The display address reg provides this.
  1762. */
  1763. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1764. enum plane plane)
  1765. {
  1766. struct drm_device *dev = dev_priv->dev;
  1767. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1768. I915_WRITE(reg, I915_READ(reg));
  1769. POSTING_READ(reg);
  1770. }
  1771. /**
  1772. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1773. * @dev_priv: i915 private structure
  1774. * @plane: plane to enable
  1775. * @pipe: pipe being fed
  1776. *
  1777. * Enable @plane on @pipe, making sure that @pipe is running first.
  1778. */
  1779. static void intel_enable_primary_hw_plane(struct drm_i915_private *dev_priv,
  1780. enum plane plane, enum pipe pipe)
  1781. {
  1782. struct intel_crtc *intel_crtc =
  1783. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1784. int reg;
  1785. u32 val;
  1786. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1787. assert_pipe_enabled(dev_priv, pipe);
  1788. if (intel_crtc->primary_enabled)
  1789. return;
  1790. intel_crtc->primary_enabled = true;
  1791. reg = DSPCNTR(plane);
  1792. val = I915_READ(reg);
  1793. WARN_ON(val & DISPLAY_PLANE_ENABLE);
  1794. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1795. intel_flush_primary_plane(dev_priv, plane);
  1796. intel_wait_for_vblank(dev_priv->dev, pipe);
  1797. }
  1798. /**
  1799. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1800. * @dev_priv: i915 private structure
  1801. * @plane: plane to disable
  1802. * @pipe: pipe consuming the data
  1803. *
  1804. * Disable @plane; should be an independent operation.
  1805. */
  1806. static void intel_disable_primary_hw_plane(struct drm_i915_private *dev_priv,
  1807. enum plane plane, enum pipe pipe)
  1808. {
  1809. struct intel_crtc *intel_crtc =
  1810. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1811. int reg;
  1812. u32 val;
  1813. if (!intel_crtc->primary_enabled)
  1814. return;
  1815. intel_crtc->primary_enabled = false;
  1816. reg = DSPCNTR(plane);
  1817. val = I915_READ(reg);
  1818. WARN_ON((val & DISPLAY_PLANE_ENABLE) == 0);
  1819. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1820. intel_flush_primary_plane(dev_priv, plane);
  1821. intel_wait_for_vblank(dev_priv->dev, pipe);
  1822. }
  1823. static bool need_vtd_wa(struct drm_device *dev)
  1824. {
  1825. #ifdef CONFIG_INTEL_IOMMU
  1826. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1827. return true;
  1828. #endif
  1829. return false;
  1830. }
  1831. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1832. {
  1833. int tile_height;
  1834. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1835. return ALIGN(height, tile_height);
  1836. }
  1837. int
  1838. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1839. struct drm_i915_gem_object *obj,
  1840. struct intel_ring_buffer *pipelined)
  1841. {
  1842. struct drm_i915_private *dev_priv = dev->dev_private;
  1843. u32 alignment;
  1844. int ret;
  1845. switch (obj->tiling_mode) {
  1846. case I915_TILING_NONE:
  1847. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1848. alignment = 128 * 1024;
  1849. else if (INTEL_INFO(dev)->gen >= 4)
  1850. alignment = 4 * 1024;
  1851. else
  1852. alignment = 64 * 1024;
  1853. break;
  1854. case I915_TILING_X:
  1855. /* pin() will align the object as required by fence */
  1856. alignment = 0;
  1857. break;
  1858. case I915_TILING_Y:
  1859. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1860. return -EINVAL;
  1861. default:
  1862. BUG();
  1863. }
  1864. /* Note that the w/a also requires 64 PTE of padding following the
  1865. * bo. We currently fill all unused PTE with the shadow page and so
  1866. * we should always have valid PTE following the scanout preventing
  1867. * the VT-d warning.
  1868. */
  1869. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1870. alignment = 256 * 1024;
  1871. dev_priv->mm.interruptible = false;
  1872. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1873. if (ret)
  1874. goto err_interruptible;
  1875. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1876. * fence, whereas 965+ only requires a fence if using
  1877. * framebuffer compression. For simplicity, we always install
  1878. * a fence as the cost is not that onerous.
  1879. */
  1880. ret = i915_gem_object_get_fence(obj);
  1881. if (ret)
  1882. goto err_unpin;
  1883. i915_gem_object_pin_fence(obj);
  1884. dev_priv->mm.interruptible = true;
  1885. return 0;
  1886. err_unpin:
  1887. i915_gem_object_unpin_from_display_plane(obj);
  1888. err_interruptible:
  1889. dev_priv->mm.interruptible = true;
  1890. return ret;
  1891. }
  1892. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1893. {
  1894. i915_gem_object_unpin_fence(obj);
  1895. i915_gem_object_unpin_from_display_plane(obj);
  1896. }
  1897. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1898. * is assumed to be a power-of-two. */
  1899. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1900. unsigned int tiling_mode,
  1901. unsigned int cpp,
  1902. unsigned int pitch)
  1903. {
  1904. if (tiling_mode != I915_TILING_NONE) {
  1905. unsigned int tile_rows, tiles;
  1906. tile_rows = *y / 8;
  1907. *y %= 8;
  1908. tiles = *x / (512/cpp);
  1909. *x %= 512/cpp;
  1910. return tile_rows * pitch * 8 + tiles * 4096;
  1911. } else {
  1912. unsigned int offset;
  1913. offset = *y * pitch + *x * cpp;
  1914. *y = 0;
  1915. *x = (offset & 4095) / cpp;
  1916. return offset & -4096;
  1917. }
  1918. }
  1919. int intel_format_to_fourcc(int format)
  1920. {
  1921. switch (format) {
  1922. case DISPPLANE_8BPP:
  1923. return DRM_FORMAT_C8;
  1924. case DISPPLANE_BGRX555:
  1925. return DRM_FORMAT_XRGB1555;
  1926. case DISPPLANE_BGRX565:
  1927. return DRM_FORMAT_RGB565;
  1928. default:
  1929. case DISPPLANE_BGRX888:
  1930. return DRM_FORMAT_XRGB8888;
  1931. case DISPPLANE_RGBX888:
  1932. return DRM_FORMAT_XBGR8888;
  1933. case DISPPLANE_BGRX101010:
  1934. return DRM_FORMAT_XRGB2101010;
  1935. case DISPPLANE_RGBX101010:
  1936. return DRM_FORMAT_XBGR2101010;
  1937. }
  1938. }
  1939. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  1940. struct intel_plane_config *plane_config)
  1941. {
  1942. struct drm_device *dev = crtc->base.dev;
  1943. struct drm_i915_gem_object *obj = NULL;
  1944. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  1945. u32 base = plane_config->base;
  1946. if (plane_config->size == 0)
  1947. return false;
  1948. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  1949. plane_config->size);
  1950. if (!obj)
  1951. return false;
  1952. if (plane_config->tiled) {
  1953. obj->tiling_mode = I915_TILING_X;
  1954. obj->stride = crtc->base.primary->fb->pitches[0];
  1955. }
  1956. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  1957. mode_cmd.width = crtc->base.primary->fb->width;
  1958. mode_cmd.height = crtc->base.primary->fb->height;
  1959. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  1960. mutex_lock(&dev->struct_mutex);
  1961. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  1962. &mode_cmd, obj)) {
  1963. DRM_DEBUG_KMS("intel fb init failed\n");
  1964. goto out_unref_obj;
  1965. }
  1966. mutex_unlock(&dev->struct_mutex);
  1967. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  1968. return true;
  1969. out_unref_obj:
  1970. drm_gem_object_unreference(&obj->base);
  1971. mutex_unlock(&dev->struct_mutex);
  1972. return false;
  1973. }
  1974. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  1975. struct intel_plane_config *plane_config)
  1976. {
  1977. struct drm_device *dev = intel_crtc->base.dev;
  1978. struct drm_crtc *c;
  1979. struct intel_crtc *i;
  1980. struct intel_framebuffer *fb;
  1981. if (!intel_crtc->base.primary->fb)
  1982. return;
  1983. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  1984. return;
  1985. kfree(intel_crtc->base.primary->fb);
  1986. intel_crtc->base.primary->fb = NULL;
  1987. /*
  1988. * Failed to alloc the obj, check to see if we should share
  1989. * an fb with another CRTC instead
  1990. */
  1991. list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
  1992. i = to_intel_crtc(c);
  1993. if (c == &intel_crtc->base)
  1994. continue;
  1995. if (!i->active || !c->primary->fb)
  1996. continue;
  1997. fb = to_intel_framebuffer(c->primary->fb);
  1998. if (i915_gem_obj_ggtt_offset(fb->obj) == plane_config->base) {
  1999. drm_framebuffer_reference(c->primary->fb);
  2000. intel_crtc->base.primary->fb = c->primary->fb;
  2001. break;
  2002. }
  2003. }
  2004. }
  2005. static int i9xx_update_primary_plane(struct drm_crtc *crtc,
  2006. struct drm_framebuffer *fb,
  2007. int x, int y)
  2008. {
  2009. struct drm_device *dev = crtc->dev;
  2010. struct drm_i915_private *dev_priv = dev->dev_private;
  2011. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2012. struct intel_framebuffer *intel_fb;
  2013. struct drm_i915_gem_object *obj;
  2014. int plane = intel_crtc->plane;
  2015. unsigned long linear_offset;
  2016. u32 dspcntr;
  2017. u32 reg;
  2018. intel_fb = to_intel_framebuffer(fb);
  2019. obj = intel_fb->obj;
  2020. reg = DSPCNTR(plane);
  2021. dspcntr = I915_READ(reg);
  2022. /* Mask out pixel format bits in case we change it */
  2023. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  2024. switch (fb->pixel_format) {
  2025. case DRM_FORMAT_C8:
  2026. dspcntr |= DISPPLANE_8BPP;
  2027. break;
  2028. case DRM_FORMAT_XRGB1555:
  2029. case DRM_FORMAT_ARGB1555:
  2030. dspcntr |= DISPPLANE_BGRX555;
  2031. break;
  2032. case DRM_FORMAT_RGB565:
  2033. dspcntr |= DISPPLANE_BGRX565;
  2034. break;
  2035. case DRM_FORMAT_XRGB8888:
  2036. case DRM_FORMAT_ARGB8888:
  2037. dspcntr |= DISPPLANE_BGRX888;
  2038. break;
  2039. case DRM_FORMAT_XBGR8888:
  2040. case DRM_FORMAT_ABGR8888:
  2041. dspcntr |= DISPPLANE_RGBX888;
  2042. break;
  2043. case DRM_FORMAT_XRGB2101010:
  2044. case DRM_FORMAT_ARGB2101010:
  2045. dspcntr |= DISPPLANE_BGRX101010;
  2046. break;
  2047. case DRM_FORMAT_XBGR2101010:
  2048. case DRM_FORMAT_ABGR2101010:
  2049. dspcntr |= DISPPLANE_RGBX101010;
  2050. break;
  2051. default:
  2052. BUG();
  2053. }
  2054. if (INTEL_INFO(dev)->gen >= 4) {
  2055. if (obj->tiling_mode != I915_TILING_NONE)
  2056. dspcntr |= DISPPLANE_TILED;
  2057. else
  2058. dspcntr &= ~DISPPLANE_TILED;
  2059. }
  2060. if (IS_G4X(dev))
  2061. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2062. I915_WRITE(reg, dspcntr);
  2063. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  2064. if (INTEL_INFO(dev)->gen >= 4) {
  2065. intel_crtc->dspaddr_offset =
  2066. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2067. fb->bits_per_pixel / 8,
  2068. fb->pitches[0]);
  2069. linear_offset -= intel_crtc->dspaddr_offset;
  2070. } else {
  2071. intel_crtc->dspaddr_offset = linear_offset;
  2072. }
  2073. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2074. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2075. fb->pitches[0]);
  2076. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2077. if (INTEL_INFO(dev)->gen >= 4) {
  2078. I915_WRITE(DSPSURF(plane),
  2079. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2080. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2081. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2082. } else
  2083. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2084. POSTING_READ(reg);
  2085. return 0;
  2086. }
  2087. static int ironlake_update_primary_plane(struct drm_crtc *crtc,
  2088. struct drm_framebuffer *fb,
  2089. int x, int y)
  2090. {
  2091. struct drm_device *dev = crtc->dev;
  2092. struct drm_i915_private *dev_priv = dev->dev_private;
  2093. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2094. struct intel_framebuffer *intel_fb;
  2095. struct drm_i915_gem_object *obj;
  2096. int plane = intel_crtc->plane;
  2097. unsigned long linear_offset;
  2098. u32 dspcntr;
  2099. u32 reg;
  2100. intel_fb = to_intel_framebuffer(fb);
  2101. obj = intel_fb->obj;
  2102. reg = DSPCNTR(plane);
  2103. dspcntr = I915_READ(reg);
  2104. /* Mask out pixel format bits in case we change it */
  2105. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  2106. switch (fb->pixel_format) {
  2107. case DRM_FORMAT_C8:
  2108. dspcntr |= DISPPLANE_8BPP;
  2109. break;
  2110. case DRM_FORMAT_RGB565:
  2111. dspcntr |= DISPPLANE_BGRX565;
  2112. break;
  2113. case DRM_FORMAT_XRGB8888:
  2114. case DRM_FORMAT_ARGB8888:
  2115. dspcntr |= DISPPLANE_BGRX888;
  2116. break;
  2117. case DRM_FORMAT_XBGR8888:
  2118. case DRM_FORMAT_ABGR8888:
  2119. dspcntr |= DISPPLANE_RGBX888;
  2120. break;
  2121. case DRM_FORMAT_XRGB2101010:
  2122. case DRM_FORMAT_ARGB2101010:
  2123. dspcntr |= DISPPLANE_BGRX101010;
  2124. break;
  2125. case DRM_FORMAT_XBGR2101010:
  2126. case DRM_FORMAT_ABGR2101010:
  2127. dspcntr |= DISPPLANE_RGBX101010;
  2128. break;
  2129. default:
  2130. BUG();
  2131. }
  2132. if (obj->tiling_mode != I915_TILING_NONE)
  2133. dspcntr |= DISPPLANE_TILED;
  2134. else
  2135. dspcntr &= ~DISPPLANE_TILED;
  2136. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2137. dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
  2138. else
  2139. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2140. I915_WRITE(reg, dspcntr);
  2141. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  2142. intel_crtc->dspaddr_offset =
  2143. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2144. fb->bits_per_pixel / 8,
  2145. fb->pitches[0]);
  2146. linear_offset -= intel_crtc->dspaddr_offset;
  2147. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2148. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2149. fb->pitches[0]);
  2150. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2151. I915_WRITE(DSPSURF(plane),
  2152. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2153. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2154. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2155. } else {
  2156. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2157. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2158. }
  2159. POSTING_READ(reg);
  2160. return 0;
  2161. }
  2162. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2163. static int
  2164. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2165. int x, int y, enum mode_set_atomic state)
  2166. {
  2167. struct drm_device *dev = crtc->dev;
  2168. struct drm_i915_private *dev_priv = dev->dev_private;
  2169. if (dev_priv->display.disable_fbc)
  2170. dev_priv->display.disable_fbc(dev);
  2171. intel_increase_pllclock(crtc);
  2172. return dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2173. }
  2174. void intel_display_handle_reset(struct drm_device *dev)
  2175. {
  2176. struct drm_i915_private *dev_priv = dev->dev_private;
  2177. struct drm_crtc *crtc;
  2178. /*
  2179. * Flips in the rings have been nuked by the reset,
  2180. * so complete all pending flips so that user space
  2181. * will get its events and not get stuck.
  2182. *
  2183. * Also update the base address of all primary
  2184. * planes to the the last fb to make sure we're
  2185. * showing the correct fb after a reset.
  2186. *
  2187. * Need to make two loops over the crtcs so that we
  2188. * don't try to grab a crtc mutex before the
  2189. * pending_flip_queue really got woken up.
  2190. */
  2191. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2192. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2193. enum plane plane = intel_crtc->plane;
  2194. intel_prepare_page_flip(dev, plane);
  2195. intel_finish_page_flip_plane(dev, plane);
  2196. }
  2197. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2198. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2199. mutex_lock(&crtc->mutex);
  2200. /*
  2201. * FIXME: Once we have proper support for primary planes (and
  2202. * disabling them without disabling the entire crtc) allow again
  2203. * a NULL crtc->primary->fb.
  2204. */
  2205. if (intel_crtc->active && crtc->primary->fb)
  2206. dev_priv->display.update_primary_plane(crtc,
  2207. crtc->primary->fb,
  2208. crtc->x,
  2209. crtc->y);
  2210. mutex_unlock(&crtc->mutex);
  2211. }
  2212. }
  2213. static int
  2214. intel_finish_fb(struct drm_framebuffer *old_fb)
  2215. {
  2216. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  2217. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2218. bool was_interruptible = dev_priv->mm.interruptible;
  2219. int ret;
  2220. /* Big Hammer, we also need to ensure that any pending
  2221. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2222. * current scanout is retired before unpinning the old
  2223. * framebuffer.
  2224. *
  2225. * This should only fail upon a hung GPU, in which case we
  2226. * can safely continue.
  2227. */
  2228. dev_priv->mm.interruptible = false;
  2229. ret = i915_gem_object_finish_gpu(obj);
  2230. dev_priv->mm.interruptible = was_interruptible;
  2231. return ret;
  2232. }
  2233. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2234. {
  2235. struct drm_device *dev = crtc->dev;
  2236. struct drm_i915_private *dev_priv = dev->dev_private;
  2237. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2238. unsigned long flags;
  2239. bool pending;
  2240. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2241. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2242. return false;
  2243. spin_lock_irqsave(&dev->event_lock, flags);
  2244. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2245. spin_unlock_irqrestore(&dev->event_lock, flags);
  2246. return pending;
  2247. }
  2248. static int
  2249. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  2250. struct drm_framebuffer *fb)
  2251. {
  2252. struct drm_device *dev = crtc->dev;
  2253. struct drm_i915_private *dev_priv = dev->dev_private;
  2254. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2255. struct drm_framebuffer *old_fb;
  2256. int ret;
  2257. if (intel_crtc_has_pending_flip(crtc)) {
  2258. DRM_ERROR("pipe is still busy with an old pageflip\n");
  2259. return -EBUSY;
  2260. }
  2261. /* no fb bound */
  2262. if (!fb) {
  2263. DRM_ERROR("No FB bound\n");
  2264. return 0;
  2265. }
  2266. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  2267. DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
  2268. plane_name(intel_crtc->plane),
  2269. INTEL_INFO(dev)->num_pipes);
  2270. return -EINVAL;
  2271. }
  2272. mutex_lock(&dev->struct_mutex);
  2273. ret = intel_pin_and_fence_fb_obj(dev,
  2274. to_intel_framebuffer(fb)->obj,
  2275. NULL);
  2276. mutex_unlock(&dev->struct_mutex);
  2277. if (ret != 0) {
  2278. DRM_ERROR("pin & fence failed\n");
  2279. return ret;
  2280. }
  2281. /*
  2282. * Update pipe size and adjust fitter if needed: the reason for this is
  2283. * that in compute_mode_changes we check the native mode (not the pfit
  2284. * mode) to see if we can flip rather than do a full mode set. In the
  2285. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2286. * pfit state, we'll end up with a big fb scanned out into the wrong
  2287. * sized surface.
  2288. *
  2289. * To fix this properly, we need to hoist the checks up into
  2290. * compute_mode_changes (or above), check the actual pfit state and
  2291. * whether the platform allows pfit disable with pipe active, and only
  2292. * then update the pipesrc and pfit state, even on the flip path.
  2293. */
  2294. if (i915.fastboot) {
  2295. const struct drm_display_mode *adjusted_mode =
  2296. &intel_crtc->config.adjusted_mode;
  2297. I915_WRITE(PIPESRC(intel_crtc->pipe),
  2298. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2299. (adjusted_mode->crtc_vdisplay - 1));
  2300. if (!intel_crtc->config.pch_pfit.enabled &&
  2301. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2302. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2303. I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
  2304. I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
  2305. I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
  2306. }
  2307. intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2308. intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2309. }
  2310. ret = dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2311. if (ret) {
  2312. mutex_lock(&dev->struct_mutex);
  2313. intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
  2314. mutex_unlock(&dev->struct_mutex);
  2315. DRM_ERROR("failed to update base address\n");
  2316. return ret;
  2317. }
  2318. old_fb = crtc->primary->fb;
  2319. crtc->primary->fb = fb;
  2320. crtc->x = x;
  2321. crtc->y = y;
  2322. if (old_fb) {
  2323. if (intel_crtc->active && old_fb != fb)
  2324. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2325. mutex_lock(&dev->struct_mutex);
  2326. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  2327. mutex_unlock(&dev->struct_mutex);
  2328. }
  2329. mutex_lock(&dev->struct_mutex);
  2330. intel_update_fbc(dev);
  2331. intel_edp_psr_update(dev);
  2332. mutex_unlock(&dev->struct_mutex);
  2333. return 0;
  2334. }
  2335. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2336. {
  2337. struct drm_device *dev = crtc->dev;
  2338. struct drm_i915_private *dev_priv = dev->dev_private;
  2339. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2340. int pipe = intel_crtc->pipe;
  2341. u32 reg, temp;
  2342. /* enable normal train */
  2343. reg = FDI_TX_CTL(pipe);
  2344. temp = I915_READ(reg);
  2345. if (IS_IVYBRIDGE(dev)) {
  2346. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2347. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2348. } else {
  2349. temp &= ~FDI_LINK_TRAIN_NONE;
  2350. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2351. }
  2352. I915_WRITE(reg, temp);
  2353. reg = FDI_RX_CTL(pipe);
  2354. temp = I915_READ(reg);
  2355. if (HAS_PCH_CPT(dev)) {
  2356. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2357. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2358. } else {
  2359. temp &= ~FDI_LINK_TRAIN_NONE;
  2360. temp |= FDI_LINK_TRAIN_NONE;
  2361. }
  2362. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2363. /* wait one idle pattern time */
  2364. POSTING_READ(reg);
  2365. udelay(1000);
  2366. /* IVB wants error correction enabled */
  2367. if (IS_IVYBRIDGE(dev))
  2368. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2369. FDI_FE_ERRC_ENABLE);
  2370. }
  2371. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2372. {
  2373. return crtc->base.enabled && crtc->active &&
  2374. crtc->config.has_pch_encoder;
  2375. }
  2376. static void ivb_modeset_global_resources(struct drm_device *dev)
  2377. {
  2378. struct drm_i915_private *dev_priv = dev->dev_private;
  2379. struct intel_crtc *pipe_B_crtc =
  2380. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2381. struct intel_crtc *pipe_C_crtc =
  2382. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2383. uint32_t temp;
  2384. /*
  2385. * When everything is off disable fdi C so that we could enable fdi B
  2386. * with all lanes. Note that we don't care about enabled pipes without
  2387. * an enabled pch encoder.
  2388. */
  2389. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2390. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2391. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2392. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2393. temp = I915_READ(SOUTH_CHICKEN1);
  2394. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2395. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2396. I915_WRITE(SOUTH_CHICKEN1, temp);
  2397. }
  2398. }
  2399. /* The FDI link training functions for ILK/Ibexpeak. */
  2400. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2401. {
  2402. struct drm_device *dev = crtc->dev;
  2403. struct drm_i915_private *dev_priv = dev->dev_private;
  2404. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2405. int pipe = intel_crtc->pipe;
  2406. u32 reg, temp, tries;
  2407. /* FDI needs bits from pipe first */
  2408. assert_pipe_enabled(dev_priv, pipe);
  2409. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2410. for train result */
  2411. reg = FDI_RX_IMR(pipe);
  2412. temp = I915_READ(reg);
  2413. temp &= ~FDI_RX_SYMBOL_LOCK;
  2414. temp &= ~FDI_RX_BIT_LOCK;
  2415. I915_WRITE(reg, temp);
  2416. I915_READ(reg);
  2417. udelay(150);
  2418. /* enable CPU FDI TX and PCH FDI RX */
  2419. reg = FDI_TX_CTL(pipe);
  2420. temp = I915_READ(reg);
  2421. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2422. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2423. temp &= ~FDI_LINK_TRAIN_NONE;
  2424. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2425. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2426. reg = FDI_RX_CTL(pipe);
  2427. temp = I915_READ(reg);
  2428. temp &= ~FDI_LINK_TRAIN_NONE;
  2429. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2430. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2431. POSTING_READ(reg);
  2432. udelay(150);
  2433. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2434. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2435. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2436. FDI_RX_PHASE_SYNC_POINTER_EN);
  2437. reg = FDI_RX_IIR(pipe);
  2438. for (tries = 0; tries < 5; tries++) {
  2439. temp = I915_READ(reg);
  2440. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2441. if ((temp & FDI_RX_BIT_LOCK)) {
  2442. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2443. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2444. break;
  2445. }
  2446. }
  2447. if (tries == 5)
  2448. DRM_ERROR("FDI train 1 fail!\n");
  2449. /* Train 2 */
  2450. reg = FDI_TX_CTL(pipe);
  2451. temp = I915_READ(reg);
  2452. temp &= ~FDI_LINK_TRAIN_NONE;
  2453. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2454. I915_WRITE(reg, temp);
  2455. reg = FDI_RX_CTL(pipe);
  2456. temp = I915_READ(reg);
  2457. temp &= ~FDI_LINK_TRAIN_NONE;
  2458. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2459. I915_WRITE(reg, temp);
  2460. POSTING_READ(reg);
  2461. udelay(150);
  2462. reg = FDI_RX_IIR(pipe);
  2463. for (tries = 0; tries < 5; tries++) {
  2464. temp = I915_READ(reg);
  2465. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2466. if (temp & FDI_RX_SYMBOL_LOCK) {
  2467. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2468. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2469. break;
  2470. }
  2471. }
  2472. if (tries == 5)
  2473. DRM_ERROR("FDI train 2 fail!\n");
  2474. DRM_DEBUG_KMS("FDI train done\n");
  2475. }
  2476. static const int snb_b_fdi_train_param[] = {
  2477. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2478. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2479. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2480. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2481. };
  2482. /* The FDI link training functions for SNB/Cougarpoint. */
  2483. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2484. {
  2485. struct drm_device *dev = crtc->dev;
  2486. struct drm_i915_private *dev_priv = dev->dev_private;
  2487. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2488. int pipe = intel_crtc->pipe;
  2489. u32 reg, temp, i, retry;
  2490. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2491. for train result */
  2492. reg = FDI_RX_IMR(pipe);
  2493. temp = I915_READ(reg);
  2494. temp &= ~FDI_RX_SYMBOL_LOCK;
  2495. temp &= ~FDI_RX_BIT_LOCK;
  2496. I915_WRITE(reg, temp);
  2497. POSTING_READ(reg);
  2498. udelay(150);
  2499. /* enable CPU FDI TX and PCH FDI RX */
  2500. reg = FDI_TX_CTL(pipe);
  2501. temp = I915_READ(reg);
  2502. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2503. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2504. temp &= ~FDI_LINK_TRAIN_NONE;
  2505. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2506. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2507. /* SNB-B */
  2508. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2509. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2510. I915_WRITE(FDI_RX_MISC(pipe),
  2511. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2512. reg = FDI_RX_CTL(pipe);
  2513. temp = I915_READ(reg);
  2514. if (HAS_PCH_CPT(dev)) {
  2515. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2516. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2517. } else {
  2518. temp &= ~FDI_LINK_TRAIN_NONE;
  2519. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2520. }
  2521. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2522. POSTING_READ(reg);
  2523. udelay(150);
  2524. for (i = 0; i < 4; i++) {
  2525. reg = FDI_TX_CTL(pipe);
  2526. temp = I915_READ(reg);
  2527. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2528. temp |= snb_b_fdi_train_param[i];
  2529. I915_WRITE(reg, temp);
  2530. POSTING_READ(reg);
  2531. udelay(500);
  2532. for (retry = 0; retry < 5; retry++) {
  2533. reg = FDI_RX_IIR(pipe);
  2534. temp = I915_READ(reg);
  2535. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2536. if (temp & FDI_RX_BIT_LOCK) {
  2537. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2538. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2539. break;
  2540. }
  2541. udelay(50);
  2542. }
  2543. if (retry < 5)
  2544. break;
  2545. }
  2546. if (i == 4)
  2547. DRM_ERROR("FDI train 1 fail!\n");
  2548. /* Train 2 */
  2549. reg = FDI_TX_CTL(pipe);
  2550. temp = I915_READ(reg);
  2551. temp &= ~FDI_LINK_TRAIN_NONE;
  2552. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2553. if (IS_GEN6(dev)) {
  2554. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2555. /* SNB-B */
  2556. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2557. }
  2558. I915_WRITE(reg, temp);
  2559. reg = FDI_RX_CTL(pipe);
  2560. temp = I915_READ(reg);
  2561. if (HAS_PCH_CPT(dev)) {
  2562. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2563. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2564. } else {
  2565. temp &= ~FDI_LINK_TRAIN_NONE;
  2566. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2567. }
  2568. I915_WRITE(reg, temp);
  2569. POSTING_READ(reg);
  2570. udelay(150);
  2571. for (i = 0; i < 4; i++) {
  2572. reg = FDI_TX_CTL(pipe);
  2573. temp = I915_READ(reg);
  2574. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2575. temp |= snb_b_fdi_train_param[i];
  2576. I915_WRITE(reg, temp);
  2577. POSTING_READ(reg);
  2578. udelay(500);
  2579. for (retry = 0; retry < 5; retry++) {
  2580. reg = FDI_RX_IIR(pipe);
  2581. temp = I915_READ(reg);
  2582. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2583. if (temp & FDI_RX_SYMBOL_LOCK) {
  2584. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2585. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2586. break;
  2587. }
  2588. udelay(50);
  2589. }
  2590. if (retry < 5)
  2591. break;
  2592. }
  2593. if (i == 4)
  2594. DRM_ERROR("FDI train 2 fail!\n");
  2595. DRM_DEBUG_KMS("FDI train done.\n");
  2596. }
  2597. /* Manual link training for Ivy Bridge A0 parts */
  2598. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2599. {
  2600. struct drm_device *dev = crtc->dev;
  2601. struct drm_i915_private *dev_priv = dev->dev_private;
  2602. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2603. int pipe = intel_crtc->pipe;
  2604. u32 reg, temp, i, j;
  2605. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2606. for train result */
  2607. reg = FDI_RX_IMR(pipe);
  2608. temp = I915_READ(reg);
  2609. temp &= ~FDI_RX_SYMBOL_LOCK;
  2610. temp &= ~FDI_RX_BIT_LOCK;
  2611. I915_WRITE(reg, temp);
  2612. POSTING_READ(reg);
  2613. udelay(150);
  2614. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2615. I915_READ(FDI_RX_IIR(pipe)));
  2616. /* Try each vswing and preemphasis setting twice before moving on */
  2617. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2618. /* disable first in case we need to retry */
  2619. reg = FDI_TX_CTL(pipe);
  2620. temp = I915_READ(reg);
  2621. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2622. temp &= ~FDI_TX_ENABLE;
  2623. I915_WRITE(reg, temp);
  2624. reg = FDI_RX_CTL(pipe);
  2625. temp = I915_READ(reg);
  2626. temp &= ~FDI_LINK_TRAIN_AUTO;
  2627. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2628. temp &= ~FDI_RX_ENABLE;
  2629. I915_WRITE(reg, temp);
  2630. /* enable CPU FDI TX and PCH FDI RX */
  2631. reg = FDI_TX_CTL(pipe);
  2632. temp = I915_READ(reg);
  2633. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2634. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2635. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2636. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2637. temp |= snb_b_fdi_train_param[j/2];
  2638. temp |= FDI_COMPOSITE_SYNC;
  2639. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2640. I915_WRITE(FDI_RX_MISC(pipe),
  2641. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2642. reg = FDI_RX_CTL(pipe);
  2643. temp = I915_READ(reg);
  2644. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2645. temp |= FDI_COMPOSITE_SYNC;
  2646. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2647. POSTING_READ(reg);
  2648. udelay(1); /* should be 0.5us */
  2649. for (i = 0; i < 4; i++) {
  2650. reg = FDI_RX_IIR(pipe);
  2651. temp = I915_READ(reg);
  2652. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2653. if (temp & FDI_RX_BIT_LOCK ||
  2654. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2655. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2656. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2657. i);
  2658. break;
  2659. }
  2660. udelay(1); /* should be 0.5us */
  2661. }
  2662. if (i == 4) {
  2663. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2664. continue;
  2665. }
  2666. /* Train 2 */
  2667. reg = FDI_TX_CTL(pipe);
  2668. temp = I915_READ(reg);
  2669. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2670. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2671. I915_WRITE(reg, temp);
  2672. reg = FDI_RX_CTL(pipe);
  2673. temp = I915_READ(reg);
  2674. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2675. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2676. I915_WRITE(reg, temp);
  2677. POSTING_READ(reg);
  2678. udelay(2); /* should be 1.5us */
  2679. for (i = 0; i < 4; i++) {
  2680. reg = FDI_RX_IIR(pipe);
  2681. temp = I915_READ(reg);
  2682. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2683. if (temp & FDI_RX_SYMBOL_LOCK ||
  2684. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2685. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2686. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2687. i);
  2688. goto train_done;
  2689. }
  2690. udelay(2); /* should be 1.5us */
  2691. }
  2692. if (i == 4)
  2693. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2694. }
  2695. train_done:
  2696. DRM_DEBUG_KMS("FDI train done.\n");
  2697. }
  2698. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2699. {
  2700. struct drm_device *dev = intel_crtc->base.dev;
  2701. struct drm_i915_private *dev_priv = dev->dev_private;
  2702. int pipe = intel_crtc->pipe;
  2703. u32 reg, temp;
  2704. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2705. reg = FDI_RX_CTL(pipe);
  2706. temp = I915_READ(reg);
  2707. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2708. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2709. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2710. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2711. POSTING_READ(reg);
  2712. udelay(200);
  2713. /* Switch from Rawclk to PCDclk */
  2714. temp = I915_READ(reg);
  2715. I915_WRITE(reg, temp | FDI_PCDCLK);
  2716. POSTING_READ(reg);
  2717. udelay(200);
  2718. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2719. reg = FDI_TX_CTL(pipe);
  2720. temp = I915_READ(reg);
  2721. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2722. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2723. POSTING_READ(reg);
  2724. udelay(100);
  2725. }
  2726. }
  2727. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2728. {
  2729. struct drm_device *dev = intel_crtc->base.dev;
  2730. struct drm_i915_private *dev_priv = dev->dev_private;
  2731. int pipe = intel_crtc->pipe;
  2732. u32 reg, temp;
  2733. /* Switch from PCDclk to Rawclk */
  2734. reg = FDI_RX_CTL(pipe);
  2735. temp = I915_READ(reg);
  2736. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2737. /* Disable CPU FDI TX PLL */
  2738. reg = FDI_TX_CTL(pipe);
  2739. temp = I915_READ(reg);
  2740. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2741. POSTING_READ(reg);
  2742. udelay(100);
  2743. reg = FDI_RX_CTL(pipe);
  2744. temp = I915_READ(reg);
  2745. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2746. /* Wait for the clocks to turn off. */
  2747. POSTING_READ(reg);
  2748. udelay(100);
  2749. }
  2750. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2751. {
  2752. struct drm_device *dev = crtc->dev;
  2753. struct drm_i915_private *dev_priv = dev->dev_private;
  2754. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2755. int pipe = intel_crtc->pipe;
  2756. u32 reg, temp;
  2757. /* disable CPU FDI tx and PCH FDI rx */
  2758. reg = FDI_TX_CTL(pipe);
  2759. temp = I915_READ(reg);
  2760. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2761. POSTING_READ(reg);
  2762. reg = FDI_RX_CTL(pipe);
  2763. temp = I915_READ(reg);
  2764. temp &= ~(0x7 << 16);
  2765. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2766. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2767. POSTING_READ(reg);
  2768. udelay(100);
  2769. /* Ironlake workaround, disable clock pointer after downing FDI */
  2770. if (HAS_PCH_IBX(dev)) {
  2771. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2772. }
  2773. /* still set train pattern 1 */
  2774. reg = FDI_TX_CTL(pipe);
  2775. temp = I915_READ(reg);
  2776. temp &= ~FDI_LINK_TRAIN_NONE;
  2777. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2778. I915_WRITE(reg, temp);
  2779. reg = FDI_RX_CTL(pipe);
  2780. temp = I915_READ(reg);
  2781. if (HAS_PCH_CPT(dev)) {
  2782. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2783. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2784. } else {
  2785. temp &= ~FDI_LINK_TRAIN_NONE;
  2786. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2787. }
  2788. /* BPC in FDI rx is consistent with that in PIPECONF */
  2789. temp &= ~(0x07 << 16);
  2790. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2791. I915_WRITE(reg, temp);
  2792. POSTING_READ(reg);
  2793. udelay(100);
  2794. }
  2795. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  2796. {
  2797. struct intel_crtc *crtc;
  2798. /* Note that we don't need to be called with mode_config.lock here
  2799. * as our list of CRTC objects is static for the lifetime of the
  2800. * device and so cannot disappear as we iterate. Similarly, we can
  2801. * happily treat the predicates as racy, atomic checks as userspace
  2802. * cannot claim and pin a new fb without at least acquring the
  2803. * struct_mutex and so serialising with us.
  2804. */
  2805. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
  2806. if (atomic_read(&crtc->unpin_work_count) == 0)
  2807. continue;
  2808. if (crtc->unpin_work)
  2809. intel_wait_for_vblank(dev, crtc->pipe);
  2810. return true;
  2811. }
  2812. return false;
  2813. }
  2814. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2815. {
  2816. struct drm_device *dev = crtc->dev;
  2817. struct drm_i915_private *dev_priv = dev->dev_private;
  2818. if (crtc->primary->fb == NULL)
  2819. return;
  2820. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  2821. wait_event(dev_priv->pending_flip_queue,
  2822. !intel_crtc_has_pending_flip(crtc));
  2823. mutex_lock(&dev->struct_mutex);
  2824. intel_finish_fb(crtc->primary->fb);
  2825. mutex_unlock(&dev->struct_mutex);
  2826. }
  2827. /* Program iCLKIP clock to the desired frequency */
  2828. static void lpt_program_iclkip(struct drm_crtc *crtc)
  2829. {
  2830. struct drm_device *dev = crtc->dev;
  2831. struct drm_i915_private *dev_priv = dev->dev_private;
  2832. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  2833. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  2834. u32 temp;
  2835. mutex_lock(&dev_priv->dpio_lock);
  2836. /* It is necessary to ungate the pixclk gate prior to programming
  2837. * the divisors, and gate it back when it is done.
  2838. */
  2839. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  2840. /* Disable SSCCTL */
  2841. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  2842. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  2843. SBI_SSCCTL_DISABLE,
  2844. SBI_ICLK);
  2845. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  2846. if (clock == 20000) {
  2847. auxdiv = 1;
  2848. divsel = 0x41;
  2849. phaseinc = 0x20;
  2850. } else {
  2851. /* The iCLK virtual clock root frequency is in MHz,
  2852. * but the adjusted_mode->crtc_clock in in KHz. To get the
  2853. * divisors, it is necessary to divide one by another, so we
  2854. * convert the virtual clock precision to KHz here for higher
  2855. * precision.
  2856. */
  2857. u32 iclk_virtual_root_freq = 172800 * 1000;
  2858. u32 iclk_pi_range = 64;
  2859. u32 desired_divisor, msb_divisor_value, pi_value;
  2860. desired_divisor = (iclk_virtual_root_freq / clock);
  2861. msb_divisor_value = desired_divisor / iclk_pi_range;
  2862. pi_value = desired_divisor % iclk_pi_range;
  2863. auxdiv = 0;
  2864. divsel = msb_divisor_value - 2;
  2865. phaseinc = pi_value;
  2866. }
  2867. /* This should not happen with any sane values */
  2868. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  2869. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  2870. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  2871. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  2872. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  2873. clock,
  2874. auxdiv,
  2875. divsel,
  2876. phasedir,
  2877. phaseinc);
  2878. /* Program SSCDIVINTPHASE6 */
  2879. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  2880. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  2881. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  2882. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  2883. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  2884. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  2885. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  2886. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  2887. /* Program SSCAUXDIV */
  2888. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  2889. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  2890. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  2891. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  2892. /* Enable modulator and associated divider */
  2893. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  2894. temp &= ~SBI_SSCCTL_DISABLE;
  2895. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  2896. /* Wait for initialization time */
  2897. udelay(24);
  2898. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  2899. mutex_unlock(&dev_priv->dpio_lock);
  2900. }
  2901. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  2902. enum pipe pch_transcoder)
  2903. {
  2904. struct drm_device *dev = crtc->base.dev;
  2905. struct drm_i915_private *dev_priv = dev->dev_private;
  2906. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  2907. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  2908. I915_READ(HTOTAL(cpu_transcoder)));
  2909. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  2910. I915_READ(HBLANK(cpu_transcoder)));
  2911. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  2912. I915_READ(HSYNC(cpu_transcoder)));
  2913. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  2914. I915_READ(VTOTAL(cpu_transcoder)));
  2915. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  2916. I915_READ(VBLANK(cpu_transcoder)));
  2917. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  2918. I915_READ(VSYNC(cpu_transcoder)));
  2919. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  2920. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  2921. }
  2922. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  2923. {
  2924. struct drm_i915_private *dev_priv = dev->dev_private;
  2925. uint32_t temp;
  2926. temp = I915_READ(SOUTH_CHICKEN1);
  2927. if (temp & FDI_BC_BIFURCATION_SELECT)
  2928. return;
  2929. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2930. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2931. temp |= FDI_BC_BIFURCATION_SELECT;
  2932. DRM_DEBUG_KMS("enabling fdi C rx\n");
  2933. I915_WRITE(SOUTH_CHICKEN1, temp);
  2934. POSTING_READ(SOUTH_CHICKEN1);
  2935. }
  2936. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  2937. {
  2938. struct drm_device *dev = intel_crtc->base.dev;
  2939. struct drm_i915_private *dev_priv = dev->dev_private;
  2940. switch (intel_crtc->pipe) {
  2941. case PIPE_A:
  2942. break;
  2943. case PIPE_B:
  2944. if (intel_crtc->config.fdi_lanes > 2)
  2945. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  2946. else
  2947. cpt_enable_fdi_bc_bifurcation(dev);
  2948. break;
  2949. case PIPE_C:
  2950. cpt_enable_fdi_bc_bifurcation(dev);
  2951. break;
  2952. default:
  2953. BUG();
  2954. }
  2955. }
  2956. /*
  2957. * Enable PCH resources required for PCH ports:
  2958. * - PCH PLLs
  2959. * - FDI training & RX/TX
  2960. * - update transcoder timings
  2961. * - DP transcoding bits
  2962. * - transcoder
  2963. */
  2964. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2965. {
  2966. struct drm_device *dev = crtc->dev;
  2967. struct drm_i915_private *dev_priv = dev->dev_private;
  2968. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2969. int pipe = intel_crtc->pipe;
  2970. u32 reg, temp;
  2971. assert_pch_transcoder_disabled(dev_priv, pipe);
  2972. if (IS_IVYBRIDGE(dev))
  2973. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  2974. /* Write the TU size bits before fdi link training, so that error
  2975. * detection works. */
  2976. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2977. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2978. /* For PCH output, training FDI link */
  2979. dev_priv->display.fdi_link_train(crtc);
  2980. /* We need to program the right clock selection before writing the pixel
  2981. * mutliplier into the DPLL. */
  2982. if (HAS_PCH_CPT(dev)) {
  2983. u32 sel;
  2984. temp = I915_READ(PCH_DPLL_SEL);
  2985. temp |= TRANS_DPLL_ENABLE(pipe);
  2986. sel = TRANS_DPLLB_SEL(pipe);
  2987. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  2988. temp |= sel;
  2989. else
  2990. temp &= ~sel;
  2991. I915_WRITE(PCH_DPLL_SEL, temp);
  2992. }
  2993. /* XXX: pch pll's can be enabled any time before we enable the PCH
  2994. * transcoder, and we actually should do this to not upset any PCH
  2995. * transcoder that already use the clock when we share it.
  2996. *
  2997. * Note that enable_shared_dpll tries to do the right thing, but
  2998. * get_shared_dpll unconditionally resets the pll - we need that to have
  2999. * the right LVDS enable sequence. */
  3000. ironlake_enable_shared_dpll(intel_crtc);
  3001. /* set transcoder timing, panel must allow it */
  3002. assert_panel_unlocked(dev_priv, pipe);
  3003. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3004. intel_fdi_normal_train(crtc);
  3005. /* For PCH DP, enable TRANS_DP_CTL */
  3006. if (HAS_PCH_CPT(dev) &&
  3007. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  3008. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  3009. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3010. reg = TRANS_DP_CTL(pipe);
  3011. temp = I915_READ(reg);
  3012. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3013. TRANS_DP_SYNC_MASK |
  3014. TRANS_DP_BPC_MASK);
  3015. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3016. TRANS_DP_ENH_FRAMING);
  3017. temp |= bpc << 9; /* same format but at 11:9 */
  3018. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3019. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3020. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3021. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3022. switch (intel_trans_dp_port_sel(crtc)) {
  3023. case PCH_DP_B:
  3024. temp |= TRANS_DP_PORT_SEL_B;
  3025. break;
  3026. case PCH_DP_C:
  3027. temp |= TRANS_DP_PORT_SEL_C;
  3028. break;
  3029. case PCH_DP_D:
  3030. temp |= TRANS_DP_PORT_SEL_D;
  3031. break;
  3032. default:
  3033. BUG();
  3034. }
  3035. I915_WRITE(reg, temp);
  3036. }
  3037. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3038. }
  3039. static void lpt_pch_enable(struct drm_crtc *crtc)
  3040. {
  3041. struct drm_device *dev = crtc->dev;
  3042. struct drm_i915_private *dev_priv = dev->dev_private;
  3043. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3044. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3045. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3046. lpt_program_iclkip(crtc);
  3047. /* Set transcoder timing. */
  3048. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3049. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3050. }
  3051. static void intel_put_shared_dpll(struct intel_crtc *crtc)
  3052. {
  3053. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3054. if (pll == NULL)
  3055. return;
  3056. if (pll->refcount == 0) {
  3057. WARN(1, "bad %s refcount\n", pll->name);
  3058. return;
  3059. }
  3060. if (--pll->refcount == 0) {
  3061. WARN_ON(pll->on);
  3062. WARN_ON(pll->active);
  3063. }
  3064. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3065. }
  3066. static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3067. {
  3068. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3069. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3070. enum intel_dpll_id i;
  3071. if (pll) {
  3072. DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
  3073. crtc->base.base.id, pll->name);
  3074. intel_put_shared_dpll(crtc);
  3075. }
  3076. if (HAS_PCH_IBX(dev_priv->dev)) {
  3077. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3078. i = (enum intel_dpll_id) crtc->pipe;
  3079. pll = &dev_priv->shared_dplls[i];
  3080. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3081. crtc->base.base.id, pll->name);
  3082. goto found;
  3083. }
  3084. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3085. pll = &dev_priv->shared_dplls[i];
  3086. /* Only want to check enabled timings first */
  3087. if (pll->refcount == 0)
  3088. continue;
  3089. if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
  3090. sizeof(pll->hw_state)) == 0) {
  3091. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
  3092. crtc->base.base.id,
  3093. pll->name, pll->refcount, pll->active);
  3094. goto found;
  3095. }
  3096. }
  3097. /* Ok no matching timings, maybe there's a free one? */
  3098. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3099. pll = &dev_priv->shared_dplls[i];
  3100. if (pll->refcount == 0) {
  3101. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3102. crtc->base.base.id, pll->name);
  3103. goto found;
  3104. }
  3105. }
  3106. return NULL;
  3107. found:
  3108. crtc->config.shared_dpll = i;
  3109. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3110. pipe_name(crtc->pipe));
  3111. if (pll->active == 0) {
  3112. memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
  3113. sizeof(pll->hw_state));
  3114. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  3115. WARN_ON(pll->on);
  3116. assert_shared_dpll_disabled(dev_priv, pll);
  3117. pll->mode_set(dev_priv, pll);
  3118. }
  3119. pll->refcount++;
  3120. return pll;
  3121. }
  3122. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3123. {
  3124. struct drm_i915_private *dev_priv = dev->dev_private;
  3125. int dslreg = PIPEDSL(pipe);
  3126. u32 temp;
  3127. temp = I915_READ(dslreg);
  3128. udelay(500);
  3129. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3130. if (wait_for(I915_READ(dslreg) != temp, 5))
  3131. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3132. }
  3133. }
  3134. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3135. {
  3136. struct drm_device *dev = crtc->base.dev;
  3137. struct drm_i915_private *dev_priv = dev->dev_private;
  3138. int pipe = crtc->pipe;
  3139. if (crtc->config.pch_pfit.enabled) {
  3140. /* Force use of hard-coded filter coefficients
  3141. * as some pre-programmed values are broken,
  3142. * e.g. x201.
  3143. */
  3144. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3145. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3146. PF_PIPE_SEL_IVB(pipe));
  3147. else
  3148. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3149. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3150. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3151. }
  3152. }
  3153. static void intel_enable_planes(struct drm_crtc *crtc)
  3154. {
  3155. struct drm_device *dev = crtc->dev;
  3156. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3157. struct drm_plane *plane;
  3158. struct intel_plane *intel_plane;
  3159. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3160. intel_plane = to_intel_plane(plane);
  3161. if (intel_plane->pipe == pipe)
  3162. intel_plane_restore(&intel_plane->base);
  3163. }
  3164. }
  3165. static void intel_disable_planes(struct drm_crtc *crtc)
  3166. {
  3167. struct drm_device *dev = crtc->dev;
  3168. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3169. struct drm_plane *plane;
  3170. struct intel_plane *intel_plane;
  3171. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3172. intel_plane = to_intel_plane(plane);
  3173. if (intel_plane->pipe == pipe)
  3174. intel_plane_disable(&intel_plane->base);
  3175. }
  3176. }
  3177. void hsw_enable_ips(struct intel_crtc *crtc)
  3178. {
  3179. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3180. if (!crtc->config.ips_enabled)
  3181. return;
  3182. /* We can only enable IPS after we enable a plane and wait for a vblank.
  3183. * We guarantee that the plane is enabled by calling intel_enable_ips
  3184. * only after intel_enable_plane. And intel_enable_plane already waits
  3185. * for a vblank, so all we need to do here is to enable the IPS bit. */
  3186. assert_plane_enabled(dev_priv, crtc->plane);
  3187. if (IS_BROADWELL(crtc->base.dev)) {
  3188. mutex_lock(&dev_priv->rps.hw_lock);
  3189. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3190. mutex_unlock(&dev_priv->rps.hw_lock);
  3191. /* Quoting Art Runyan: "its not safe to expect any particular
  3192. * value in IPS_CTL bit 31 after enabling IPS through the
  3193. * mailbox." Moreover, the mailbox may return a bogus state,
  3194. * so we need to just enable it and continue on.
  3195. */
  3196. } else {
  3197. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3198. /* The bit only becomes 1 in the next vblank, so this wait here
  3199. * is essentially intel_wait_for_vblank. If we don't have this
  3200. * and don't wait for vblanks until the end of crtc_enable, then
  3201. * the HW state readout code will complain that the expected
  3202. * IPS_CTL value is not the one we read. */
  3203. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3204. DRM_ERROR("Timed out waiting for IPS enable\n");
  3205. }
  3206. }
  3207. void hsw_disable_ips(struct intel_crtc *crtc)
  3208. {
  3209. struct drm_device *dev = crtc->base.dev;
  3210. struct drm_i915_private *dev_priv = dev->dev_private;
  3211. if (!crtc->config.ips_enabled)
  3212. return;
  3213. assert_plane_enabled(dev_priv, crtc->plane);
  3214. if (IS_BROADWELL(dev)) {
  3215. mutex_lock(&dev_priv->rps.hw_lock);
  3216. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3217. mutex_unlock(&dev_priv->rps.hw_lock);
  3218. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3219. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3220. DRM_ERROR("Timed out waiting for IPS disable\n");
  3221. } else {
  3222. I915_WRITE(IPS_CTL, 0);
  3223. POSTING_READ(IPS_CTL);
  3224. }
  3225. /* We need to wait for a vblank before we can disable the plane. */
  3226. intel_wait_for_vblank(dev, crtc->pipe);
  3227. }
  3228. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3229. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3230. {
  3231. struct drm_device *dev = crtc->dev;
  3232. struct drm_i915_private *dev_priv = dev->dev_private;
  3233. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3234. enum pipe pipe = intel_crtc->pipe;
  3235. int palreg = PALETTE(pipe);
  3236. int i;
  3237. bool reenable_ips = false;
  3238. /* The clocks have to be on to load the palette. */
  3239. if (!crtc->enabled || !intel_crtc->active)
  3240. return;
  3241. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3242. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  3243. assert_dsi_pll_enabled(dev_priv);
  3244. else
  3245. assert_pll_enabled(dev_priv, pipe);
  3246. }
  3247. /* use legacy palette for Ironlake */
  3248. if (HAS_PCH_SPLIT(dev))
  3249. palreg = LGC_PALETTE(pipe);
  3250. /* Workaround : Do not read or write the pipe palette/gamma data while
  3251. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3252. */
  3253. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3254. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3255. GAMMA_MODE_MODE_SPLIT)) {
  3256. hsw_disable_ips(intel_crtc);
  3257. reenable_ips = true;
  3258. }
  3259. for (i = 0; i < 256; i++) {
  3260. I915_WRITE(palreg + 4 * i,
  3261. (intel_crtc->lut_r[i] << 16) |
  3262. (intel_crtc->lut_g[i] << 8) |
  3263. intel_crtc->lut_b[i]);
  3264. }
  3265. if (reenable_ips)
  3266. hsw_enable_ips(intel_crtc);
  3267. }
  3268. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3269. {
  3270. if (!enable && intel_crtc->overlay) {
  3271. struct drm_device *dev = intel_crtc->base.dev;
  3272. struct drm_i915_private *dev_priv = dev->dev_private;
  3273. mutex_lock(&dev->struct_mutex);
  3274. dev_priv->mm.interruptible = false;
  3275. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3276. dev_priv->mm.interruptible = true;
  3277. mutex_unlock(&dev->struct_mutex);
  3278. }
  3279. /* Let userspace switch the overlay on again. In most cases userspace
  3280. * has to recompute where to put it anyway.
  3281. */
  3282. }
  3283. /**
  3284. * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
  3285. * cursor plane briefly if not already running after enabling the display
  3286. * plane.
  3287. * This workaround avoids occasional blank screens when self refresh is
  3288. * enabled.
  3289. */
  3290. static void
  3291. g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
  3292. {
  3293. u32 cntl = I915_READ(CURCNTR(pipe));
  3294. if ((cntl & CURSOR_MODE) == 0) {
  3295. u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
  3296. I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
  3297. I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
  3298. intel_wait_for_vblank(dev_priv->dev, pipe);
  3299. I915_WRITE(CURCNTR(pipe), cntl);
  3300. I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
  3301. I915_WRITE(FW_BLC_SELF, fw_bcl_self);
  3302. }
  3303. }
  3304. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3305. {
  3306. struct drm_device *dev = crtc->dev;
  3307. struct drm_i915_private *dev_priv = dev->dev_private;
  3308. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3309. int pipe = intel_crtc->pipe;
  3310. int plane = intel_crtc->plane;
  3311. intel_enable_primary_hw_plane(dev_priv, plane, pipe);
  3312. intel_enable_planes(crtc);
  3313. /* The fixup needs to happen before cursor is enabled */
  3314. if (IS_G4X(dev))
  3315. g4x_fixup_plane(dev_priv, pipe);
  3316. intel_crtc_update_cursor(crtc, true);
  3317. intel_crtc_dpms_overlay(intel_crtc, true);
  3318. hsw_enable_ips(intel_crtc);
  3319. mutex_lock(&dev->struct_mutex);
  3320. intel_update_fbc(dev);
  3321. mutex_unlock(&dev->struct_mutex);
  3322. }
  3323. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3324. {
  3325. struct drm_device *dev = crtc->dev;
  3326. struct drm_i915_private *dev_priv = dev->dev_private;
  3327. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3328. int pipe = intel_crtc->pipe;
  3329. int plane = intel_crtc->plane;
  3330. intel_crtc_wait_for_pending_flips(crtc);
  3331. drm_vblank_off(dev, pipe);
  3332. if (dev_priv->fbc.plane == plane)
  3333. intel_disable_fbc(dev);
  3334. hsw_disable_ips(intel_crtc);
  3335. intel_crtc_dpms_overlay(intel_crtc, false);
  3336. intel_crtc_update_cursor(crtc, false);
  3337. intel_disable_planes(crtc);
  3338. intel_disable_primary_hw_plane(dev_priv, plane, pipe);
  3339. }
  3340. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3341. {
  3342. struct drm_device *dev = crtc->dev;
  3343. struct drm_i915_private *dev_priv = dev->dev_private;
  3344. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3345. struct intel_encoder *encoder;
  3346. int pipe = intel_crtc->pipe;
  3347. WARN_ON(!crtc->enabled);
  3348. if (intel_crtc->active)
  3349. return;
  3350. intel_crtc->active = true;
  3351. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3352. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3353. for_each_encoder_on_crtc(dev, crtc, encoder)
  3354. if (encoder->pre_enable)
  3355. encoder->pre_enable(encoder);
  3356. if (intel_crtc->config.has_pch_encoder) {
  3357. /* Note: FDI PLL enabling _must_ be done before we enable the
  3358. * cpu pipes, hence this is separate from all the other fdi/pch
  3359. * enabling. */
  3360. ironlake_fdi_pll_enable(intel_crtc);
  3361. } else {
  3362. assert_fdi_tx_disabled(dev_priv, pipe);
  3363. assert_fdi_rx_disabled(dev_priv, pipe);
  3364. }
  3365. ironlake_pfit_enable(intel_crtc);
  3366. /*
  3367. * On ILK+ LUT must be loaded before the pipe is running but with
  3368. * clocks enabled
  3369. */
  3370. intel_crtc_load_lut(crtc);
  3371. intel_update_watermarks(crtc);
  3372. intel_enable_pipe(intel_crtc);
  3373. if (intel_crtc->config.has_pch_encoder)
  3374. ironlake_pch_enable(crtc);
  3375. for_each_encoder_on_crtc(dev, crtc, encoder)
  3376. encoder->enable(encoder);
  3377. if (HAS_PCH_CPT(dev))
  3378. cpt_verify_modeset(dev, intel_crtc->pipe);
  3379. intel_crtc_enable_planes(crtc);
  3380. /*
  3381. * There seems to be a race in PCH platform hw (at least on some
  3382. * outputs) where an enabled pipe still completes any pageflip right
  3383. * away (as if the pipe is off) instead of waiting for vblank. As soon
  3384. * as the first vblank happend, everything works as expected. Hence just
  3385. * wait for one vblank before returning to avoid strange things
  3386. * happening.
  3387. */
  3388. intel_wait_for_vblank(dev, intel_crtc->pipe);
  3389. }
  3390. /* IPS only exists on ULT machines and is tied to pipe A. */
  3391. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3392. {
  3393. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3394. }
  3395. /*
  3396. * This implements the workaround described in the "notes" section of the mode
  3397. * set sequence documentation. When going from no pipes or single pipe to
  3398. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3399. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3400. */
  3401. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3402. {
  3403. struct drm_device *dev = crtc->base.dev;
  3404. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3405. /* We want to get the other_active_crtc only if there's only 1 other
  3406. * active crtc. */
  3407. list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
  3408. if (!crtc_it->active || crtc_it == crtc)
  3409. continue;
  3410. if (other_active_crtc)
  3411. return;
  3412. other_active_crtc = crtc_it;
  3413. }
  3414. if (!other_active_crtc)
  3415. return;
  3416. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3417. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3418. }
  3419. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3420. {
  3421. struct drm_device *dev = crtc->dev;
  3422. struct drm_i915_private *dev_priv = dev->dev_private;
  3423. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3424. struct intel_encoder *encoder;
  3425. int pipe = intel_crtc->pipe;
  3426. WARN_ON(!crtc->enabled);
  3427. if (intel_crtc->active)
  3428. return;
  3429. intel_crtc->active = true;
  3430. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3431. if (intel_crtc->config.has_pch_encoder)
  3432. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3433. if (intel_crtc->config.has_pch_encoder)
  3434. dev_priv->display.fdi_link_train(crtc);
  3435. for_each_encoder_on_crtc(dev, crtc, encoder)
  3436. if (encoder->pre_enable)
  3437. encoder->pre_enable(encoder);
  3438. intel_ddi_enable_pipe_clock(intel_crtc);
  3439. ironlake_pfit_enable(intel_crtc);
  3440. /*
  3441. * On ILK+ LUT must be loaded before the pipe is running but with
  3442. * clocks enabled
  3443. */
  3444. intel_crtc_load_lut(crtc);
  3445. intel_ddi_set_pipe_settings(crtc);
  3446. intel_ddi_enable_transcoder_func(crtc);
  3447. intel_update_watermarks(crtc);
  3448. intel_enable_pipe(intel_crtc);
  3449. if (intel_crtc->config.has_pch_encoder)
  3450. lpt_pch_enable(crtc);
  3451. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3452. encoder->enable(encoder);
  3453. intel_opregion_notify_encoder(encoder, true);
  3454. }
  3455. /* If we change the relative order between pipe/planes enabling, we need
  3456. * to change the workaround. */
  3457. haswell_mode_set_planes_workaround(intel_crtc);
  3458. intel_crtc_enable_planes(crtc);
  3459. }
  3460. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3461. {
  3462. struct drm_device *dev = crtc->base.dev;
  3463. struct drm_i915_private *dev_priv = dev->dev_private;
  3464. int pipe = crtc->pipe;
  3465. /* To avoid upsetting the power well on haswell only disable the pfit if
  3466. * it's in use. The hw state code will make sure we get this right. */
  3467. if (crtc->config.pch_pfit.enabled) {
  3468. I915_WRITE(PF_CTL(pipe), 0);
  3469. I915_WRITE(PF_WIN_POS(pipe), 0);
  3470. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3471. }
  3472. }
  3473. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3474. {
  3475. struct drm_device *dev = crtc->dev;
  3476. struct drm_i915_private *dev_priv = dev->dev_private;
  3477. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3478. struct intel_encoder *encoder;
  3479. int pipe = intel_crtc->pipe;
  3480. u32 reg, temp;
  3481. if (!intel_crtc->active)
  3482. return;
  3483. intel_crtc_disable_planes(crtc);
  3484. for_each_encoder_on_crtc(dev, crtc, encoder)
  3485. encoder->disable(encoder);
  3486. if (intel_crtc->config.has_pch_encoder)
  3487. intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
  3488. intel_disable_pipe(dev_priv, pipe);
  3489. ironlake_pfit_disable(intel_crtc);
  3490. for_each_encoder_on_crtc(dev, crtc, encoder)
  3491. if (encoder->post_disable)
  3492. encoder->post_disable(encoder);
  3493. if (intel_crtc->config.has_pch_encoder) {
  3494. ironlake_fdi_disable(crtc);
  3495. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3496. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3497. if (HAS_PCH_CPT(dev)) {
  3498. /* disable TRANS_DP_CTL */
  3499. reg = TRANS_DP_CTL(pipe);
  3500. temp = I915_READ(reg);
  3501. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3502. TRANS_DP_PORT_SEL_MASK);
  3503. temp |= TRANS_DP_PORT_SEL_NONE;
  3504. I915_WRITE(reg, temp);
  3505. /* disable DPLL_SEL */
  3506. temp = I915_READ(PCH_DPLL_SEL);
  3507. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3508. I915_WRITE(PCH_DPLL_SEL, temp);
  3509. }
  3510. /* disable PCH DPLL */
  3511. intel_disable_shared_dpll(intel_crtc);
  3512. ironlake_fdi_pll_disable(intel_crtc);
  3513. }
  3514. intel_crtc->active = false;
  3515. intel_update_watermarks(crtc);
  3516. mutex_lock(&dev->struct_mutex);
  3517. intel_update_fbc(dev);
  3518. mutex_unlock(&dev->struct_mutex);
  3519. }
  3520. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3521. {
  3522. struct drm_device *dev = crtc->dev;
  3523. struct drm_i915_private *dev_priv = dev->dev_private;
  3524. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3525. struct intel_encoder *encoder;
  3526. int pipe = intel_crtc->pipe;
  3527. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3528. if (!intel_crtc->active)
  3529. return;
  3530. intel_crtc_disable_planes(crtc);
  3531. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3532. intel_opregion_notify_encoder(encoder, false);
  3533. encoder->disable(encoder);
  3534. }
  3535. if (intel_crtc->config.has_pch_encoder)
  3536. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
  3537. intel_disable_pipe(dev_priv, pipe);
  3538. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3539. ironlake_pfit_disable(intel_crtc);
  3540. intel_ddi_disable_pipe_clock(intel_crtc);
  3541. for_each_encoder_on_crtc(dev, crtc, encoder)
  3542. if (encoder->post_disable)
  3543. encoder->post_disable(encoder);
  3544. if (intel_crtc->config.has_pch_encoder) {
  3545. lpt_disable_pch_transcoder(dev_priv);
  3546. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3547. intel_ddi_fdi_disable(crtc);
  3548. }
  3549. intel_crtc->active = false;
  3550. intel_update_watermarks(crtc);
  3551. mutex_lock(&dev->struct_mutex);
  3552. intel_update_fbc(dev);
  3553. mutex_unlock(&dev->struct_mutex);
  3554. }
  3555. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3556. {
  3557. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3558. intel_put_shared_dpll(intel_crtc);
  3559. }
  3560. static void haswell_crtc_off(struct drm_crtc *crtc)
  3561. {
  3562. intel_ddi_put_crtc_pll(crtc);
  3563. }
  3564. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3565. {
  3566. struct drm_device *dev = crtc->base.dev;
  3567. struct drm_i915_private *dev_priv = dev->dev_private;
  3568. struct intel_crtc_config *pipe_config = &crtc->config;
  3569. if (!crtc->config.gmch_pfit.control)
  3570. return;
  3571. /*
  3572. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3573. * according to register description and PRM.
  3574. */
  3575. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3576. assert_pipe_disabled(dev_priv, crtc->pipe);
  3577. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3578. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3579. /* Border color in case we don't scale up to the full screen. Black by
  3580. * default, change to something else for debugging. */
  3581. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3582. }
  3583. #define for_each_power_domain(domain, mask) \
  3584. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3585. if ((1 << (domain)) & (mask))
  3586. enum intel_display_power_domain
  3587. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3588. {
  3589. struct drm_device *dev = intel_encoder->base.dev;
  3590. struct intel_digital_port *intel_dig_port;
  3591. switch (intel_encoder->type) {
  3592. case INTEL_OUTPUT_UNKNOWN:
  3593. /* Only DDI platforms should ever use this output type */
  3594. WARN_ON_ONCE(!HAS_DDI(dev));
  3595. case INTEL_OUTPUT_DISPLAYPORT:
  3596. case INTEL_OUTPUT_HDMI:
  3597. case INTEL_OUTPUT_EDP:
  3598. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3599. switch (intel_dig_port->port) {
  3600. case PORT_A:
  3601. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3602. case PORT_B:
  3603. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3604. case PORT_C:
  3605. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3606. case PORT_D:
  3607. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3608. default:
  3609. WARN_ON_ONCE(1);
  3610. return POWER_DOMAIN_PORT_OTHER;
  3611. }
  3612. case INTEL_OUTPUT_ANALOG:
  3613. return POWER_DOMAIN_PORT_CRT;
  3614. case INTEL_OUTPUT_DSI:
  3615. return POWER_DOMAIN_PORT_DSI;
  3616. default:
  3617. return POWER_DOMAIN_PORT_OTHER;
  3618. }
  3619. }
  3620. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3621. {
  3622. struct drm_device *dev = crtc->dev;
  3623. struct intel_encoder *intel_encoder;
  3624. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3625. enum pipe pipe = intel_crtc->pipe;
  3626. bool pfit_enabled = intel_crtc->config.pch_pfit.enabled;
  3627. unsigned long mask;
  3628. enum transcoder transcoder;
  3629. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3630. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3631. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3632. if (pfit_enabled)
  3633. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3634. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3635. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3636. return mask;
  3637. }
  3638. void intel_display_set_init_power(struct drm_i915_private *dev_priv,
  3639. bool enable)
  3640. {
  3641. if (dev_priv->power_domains.init_power_on == enable)
  3642. return;
  3643. if (enable)
  3644. intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
  3645. else
  3646. intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
  3647. dev_priv->power_domains.init_power_on = enable;
  3648. }
  3649. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3650. {
  3651. struct drm_i915_private *dev_priv = dev->dev_private;
  3652. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3653. struct intel_crtc *crtc;
  3654. /*
  3655. * First get all needed power domains, then put all unneeded, to avoid
  3656. * any unnecessary toggling of the power wells.
  3657. */
  3658. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
  3659. enum intel_display_power_domain domain;
  3660. if (!crtc->base.enabled)
  3661. continue;
  3662. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3663. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3664. intel_display_power_get(dev_priv, domain);
  3665. }
  3666. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
  3667. enum intel_display_power_domain domain;
  3668. for_each_power_domain(domain, crtc->enabled_power_domains)
  3669. intel_display_power_put(dev_priv, domain);
  3670. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  3671. }
  3672. intel_display_set_init_power(dev_priv, false);
  3673. }
  3674. int valleyview_get_vco(struct drm_i915_private *dev_priv)
  3675. {
  3676. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  3677. /* Obtain SKU information */
  3678. mutex_lock(&dev_priv->dpio_lock);
  3679. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  3680. CCK_FUSE_HPLL_FREQ_MASK;
  3681. mutex_unlock(&dev_priv->dpio_lock);
  3682. return vco_freq[hpll_freq];
  3683. }
  3684. /* Adjust CDclk dividers to allow high res or save power if possible */
  3685. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  3686. {
  3687. struct drm_i915_private *dev_priv = dev->dev_private;
  3688. u32 val, cmd;
  3689. WARN_ON(valleyview_cur_cdclk(dev_priv) != dev_priv->vlv_cdclk_freq);
  3690. dev_priv->vlv_cdclk_freq = cdclk;
  3691. if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
  3692. cmd = 2;
  3693. else if (cdclk == 266)
  3694. cmd = 1;
  3695. else
  3696. cmd = 0;
  3697. mutex_lock(&dev_priv->rps.hw_lock);
  3698. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3699. val &= ~DSPFREQGUAR_MASK;
  3700. val |= (cmd << DSPFREQGUAR_SHIFT);
  3701. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3702. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3703. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  3704. 50)) {
  3705. DRM_ERROR("timed out waiting for CDclk change\n");
  3706. }
  3707. mutex_unlock(&dev_priv->rps.hw_lock);
  3708. if (cdclk == 400) {
  3709. u32 divider, vco;
  3710. vco = valleyview_get_vco(dev_priv);
  3711. divider = ((vco << 1) / cdclk) - 1;
  3712. mutex_lock(&dev_priv->dpio_lock);
  3713. /* adjust cdclk divider */
  3714. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3715. val &= ~0xf;
  3716. val |= divider;
  3717. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  3718. mutex_unlock(&dev_priv->dpio_lock);
  3719. }
  3720. mutex_lock(&dev_priv->dpio_lock);
  3721. /* adjust self-refresh exit latency value */
  3722. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  3723. val &= ~0x7f;
  3724. /*
  3725. * For high bandwidth configs, we set a higher latency in the bunit
  3726. * so that the core display fetch happens in time to avoid underruns.
  3727. */
  3728. if (cdclk == 400)
  3729. val |= 4500 / 250; /* 4.5 usec */
  3730. else
  3731. val |= 3000 / 250; /* 3.0 usec */
  3732. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  3733. mutex_unlock(&dev_priv->dpio_lock);
  3734. /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
  3735. intel_i2c_reset(dev);
  3736. }
  3737. int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
  3738. {
  3739. int cur_cdclk, vco;
  3740. int divider;
  3741. vco = valleyview_get_vco(dev_priv);
  3742. mutex_lock(&dev_priv->dpio_lock);
  3743. divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3744. mutex_unlock(&dev_priv->dpio_lock);
  3745. divider &= 0xf;
  3746. cur_cdclk = (vco << 1) / (divider + 1);
  3747. return cur_cdclk;
  3748. }
  3749. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  3750. int max_pixclk)
  3751. {
  3752. /*
  3753. * Really only a few cases to deal with, as only 4 CDclks are supported:
  3754. * 200MHz
  3755. * 267MHz
  3756. * 320MHz
  3757. * 400MHz
  3758. * So we check to see whether we're above 90% of the lower bin and
  3759. * adjust if needed.
  3760. */
  3761. if (max_pixclk > 288000) {
  3762. return 400;
  3763. } else if (max_pixclk > 240000) {
  3764. return 320;
  3765. } else
  3766. return 266;
  3767. /* Looks like the 200MHz CDclk freq doesn't work on some configs */
  3768. }
  3769. /* compute the max pixel clock for new configuration */
  3770. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  3771. {
  3772. struct drm_device *dev = dev_priv->dev;
  3773. struct intel_crtc *intel_crtc;
  3774. int max_pixclk = 0;
  3775. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  3776. base.head) {
  3777. if (intel_crtc->new_enabled)
  3778. max_pixclk = max(max_pixclk,
  3779. intel_crtc->new_config->adjusted_mode.crtc_clock);
  3780. }
  3781. return max_pixclk;
  3782. }
  3783. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  3784. unsigned *prepare_pipes)
  3785. {
  3786. struct drm_i915_private *dev_priv = dev->dev_private;
  3787. struct intel_crtc *intel_crtc;
  3788. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3789. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  3790. dev_priv->vlv_cdclk_freq)
  3791. return;
  3792. /* disable/enable all currently active pipes while we change cdclk */
  3793. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  3794. base.head)
  3795. if (intel_crtc->base.enabled)
  3796. *prepare_pipes |= (1 << intel_crtc->pipe);
  3797. }
  3798. static void valleyview_modeset_global_resources(struct drm_device *dev)
  3799. {
  3800. struct drm_i915_private *dev_priv = dev->dev_private;
  3801. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3802. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  3803. if (req_cdclk != dev_priv->vlv_cdclk_freq)
  3804. valleyview_set_cdclk(dev, req_cdclk);
  3805. modeset_update_crtc_power_domains(dev);
  3806. }
  3807. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  3808. {
  3809. struct drm_device *dev = crtc->dev;
  3810. struct drm_i915_private *dev_priv = dev->dev_private;
  3811. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3812. struct intel_encoder *encoder;
  3813. int pipe = intel_crtc->pipe;
  3814. bool is_dsi;
  3815. WARN_ON(!crtc->enabled);
  3816. if (intel_crtc->active)
  3817. return;
  3818. intel_crtc->active = true;
  3819. for_each_encoder_on_crtc(dev, crtc, encoder)
  3820. if (encoder->pre_pll_enable)
  3821. encoder->pre_pll_enable(encoder);
  3822. is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
  3823. if (!is_dsi) {
  3824. if (IS_CHERRYVIEW(dev))
  3825. chv_enable_pll(intel_crtc);
  3826. else
  3827. vlv_enable_pll(intel_crtc);
  3828. }
  3829. for_each_encoder_on_crtc(dev, crtc, encoder)
  3830. if (encoder->pre_enable)
  3831. encoder->pre_enable(encoder);
  3832. i9xx_pfit_enable(intel_crtc);
  3833. intel_crtc_load_lut(crtc);
  3834. intel_update_watermarks(crtc);
  3835. intel_enable_pipe(intel_crtc);
  3836. intel_wait_for_vblank(dev_priv->dev, pipe);
  3837. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3838. for_each_encoder_on_crtc(dev, crtc, encoder)
  3839. encoder->enable(encoder);
  3840. intel_crtc_enable_planes(crtc);
  3841. }
  3842. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  3843. {
  3844. struct drm_device *dev = crtc->dev;
  3845. struct drm_i915_private *dev_priv = dev->dev_private;
  3846. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3847. struct intel_encoder *encoder;
  3848. int pipe = intel_crtc->pipe;
  3849. WARN_ON(!crtc->enabled);
  3850. if (intel_crtc->active)
  3851. return;
  3852. intel_crtc->active = true;
  3853. for_each_encoder_on_crtc(dev, crtc, encoder)
  3854. if (encoder->pre_enable)
  3855. encoder->pre_enable(encoder);
  3856. i9xx_enable_pll(intel_crtc);
  3857. i9xx_pfit_enable(intel_crtc);
  3858. intel_crtc_load_lut(crtc);
  3859. intel_update_watermarks(crtc);
  3860. intel_enable_pipe(intel_crtc);
  3861. intel_wait_for_vblank(dev_priv->dev, pipe);
  3862. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3863. for_each_encoder_on_crtc(dev, crtc, encoder)
  3864. encoder->enable(encoder);
  3865. intel_crtc_enable_planes(crtc);
  3866. }
  3867. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  3868. {
  3869. struct drm_device *dev = crtc->base.dev;
  3870. struct drm_i915_private *dev_priv = dev->dev_private;
  3871. if (!crtc->config.gmch_pfit.control)
  3872. return;
  3873. assert_pipe_disabled(dev_priv, crtc->pipe);
  3874. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  3875. I915_READ(PFIT_CONTROL));
  3876. I915_WRITE(PFIT_CONTROL, 0);
  3877. }
  3878. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  3879. {
  3880. struct drm_device *dev = crtc->dev;
  3881. struct drm_i915_private *dev_priv = dev->dev_private;
  3882. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3883. struct intel_encoder *encoder;
  3884. int pipe = intel_crtc->pipe;
  3885. if (!intel_crtc->active)
  3886. return;
  3887. intel_crtc_disable_planes(crtc);
  3888. for_each_encoder_on_crtc(dev, crtc, encoder)
  3889. encoder->disable(encoder);
  3890. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  3891. intel_disable_pipe(dev_priv, pipe);
  3892. i9xx_pfit_disable(intel_crtc);
  3893. for_each_encoder_on_crtc(dev, crtc, encoder)
  3894. if (encoder->post_disable)
  3895. encoder->post_disable(encoder);
  3896. if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
  3897. if (IS_CHERRYVIEW(dev))
  3898. chv_disable_pll(dev_priv, pipe);
  3899. else if (IS_VALLEYVIEW(dev))
  3900. vlv_disable_pll(dev_priv, pipe);
  3901. else
  3902. i9xx_disable_pll(dev_priv, pipe);
  3903. }
  3904. intel_crtc->active = false;
  3905. intel_update_watermarks(crtc);
  3906. intel_update_fbc(dev);
  3907. }
  3908. static void i9xx_crtc_off(struct drm_crtc *crtc)
  3909. {
  3910. }
  3911. static void intel_crtc_update_sarea(struct drm_crtc *crtc,
  3912. bool enabled)
  3913. {
  3914. struct drm_device *dev = crtc->dev;
  3915. struct drm_i915_master_private *master_priv;
  3916. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3917. int pipe = intel_crtc->pipe;
  3918. if (!dev->primary->master)
  3919. return;
  3920. master_priv = dev->primary->master->driver_priv;
  3921. if (!master_priv->sarea_priv)
  3922. return;
  3923. switch (pipe) {
  3924. case 0:
  3925. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  3926. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  3927. break;
  3928. case 1:
  3929. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  3930. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  3931. break;
  3932. default:
  3933. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  3934. break;
  3935. }
  3936. }
  3937. /**
  3938. * Sets the power management mode of the pipe and plane.
  3939. */
  3940. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  3941. {
  3942. struct drm_device *dev = crtc->dev;
  3943. struct drm_i915_private *dev_priv = dev->dev_private;
  3944. struct intel_encoder *intel_encoder;
  3945. bool enable = false;
  3946. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3947. enable |= intel_encoder->connectors_active;
  3948. if (enable)
  3949. dev_priv->display.crtc_enable(crtc);
  3950. else
  3951. dev_priv->display.crtc_disable(crtc);
  3952. intel_crtc_update_sarea(crtc, enable);
  3953. }
  3954. static void intel_crtc_disable(struct drm_crtc *crtc)
  3955. {
  3956. struct drm_device *dev = crtc->dev;
  3957. struct drm_connector *connector;
  3958. struct drm_i915_private *dev_priv = dev->dev_private;
  3959. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3960. /* crtc should still be enabled when we disable it. */
  3961. WARN_ON(!crtc->enabled);
  3962. dev_priv->display.crtc_disable(crtc);
  3963. intel_crtc->eld_vld = false;
  3964. intel_crtc_update_sarea(crtc, false);
  3965. dev_priv->display.off(crtc);
  3966. assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
  3967. assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  3968. assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
  3969. if (crtc->primary->fb) {
  3970. mutex_lock(&dev->struct_mutex);
  3971. intel_unpin_fb_obj(to_intel_framebuffer(crtc->primary->fb)->obj);
  3972. mutex_unlock(&dev->struct_mutex);
  3973. crtc->primary->fb = NULL;
  3974. }
  3975. /* Update computed state. */
  3976. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  3977. if (!connector->encoder || !connector->encoder->crtc)
  3978. continue;
  3979. if (connector->encoder->crtc != crtc)
  3980. continue;
  3981. connector->dpms = DRM_MODE_DPMS_OFF;
  3982. to_intel_encoder(connector->encoder)->connectors_active = false;
  3983. }
  3984. }
  3985. void intel_encoder_destroy(struct drm_encoder *encoder)
  3986. {
  3987. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  3988. drm_encoder_cleanup(encoder);
  3989. kfree(intel_encoder);
  3990. }
  3991. /* Simple dpms helper for encoders with just one connector, no cloning and only
  3992. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  3993. * state of the entire output pipe. */
  3994. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  3995. {
  3996. if (mode == DRM_MODE_DPMS_ON) {
  3997. encoder->connectors_active = true;
  3998. intel_crtc_update_dpms(encoder->base.crtc);
  3999. } else {
  4000. encoder->connectors_active = false;
  4001. intel_crtc_update_dpms(encoder->base.crtc);
  4002. }
  4003. }
  4004. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4005. * internal consistency). */
  4006. static void intel_connector_check_state(struct intel_connector *connector)
  4007. {
  4008. if (connector->get_hw_state(connector)) {
  4009. struct intel_encoder *encoder = connector->encoder;
  4010. struct drm_crtc *crtc;
  4011. bool encoder_enabled;
  4012. enum pipe pipe;
  4013. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4014. connector->base.base.id,
  4015. drm_get_connector_name(&connector->base));
  4016. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4017. "wrong connector dpms state\n");
  4018. WARN(connector->base.encoder != &encoder->base,
  4019. "active connector not linked to encoder\n");
  4020. WARN(!encoder->connectors_active,
  4021. "encoder->connectors_active not set\n");
  4022. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4023. WARN(!encoder_enabled, "encoder not enabled\n");
  4024. if (WARN_ON(!encoder->base.crtc))
  4025. return;
  4026. crtc = encoder->base.crtc;
  4027. WARN(!crtc->enabled, "crtc not enabled\n");
  4028. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4029. WARN(pipe != to_intel_crtc(crtc)->pipe,
  4030. "encoder active on the wrong pipe\n");
  4031. }
  4032. }
  4033. /* Even simpler default implementation, if there's really no special case to
  4034. * consider. */
  4035. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4036. {
  4037. /* All the simple cases only support two dpms states. */
  4038. if (mode != DRM_MODE_DPMS_ON)
  4039. mode = DRM_MODE_DPMS_OFF;
  4040. if (mode == connector->dpms)
  4041. return;
  4042. connector->dpms = mode;
  4043. /* Only need to change hw state when actually enabled */
  4044. if (connector->encoder)
  4045. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4046. intel_modeset_check_state(connector->dev);
  4047. }
  4048. /* Simple connector->get_hw_state implementation for encoders that support only
  4049. * one connector and no cloning and hence the encoder state determines the state
  4050. * of the connector. */
  4051. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4052. {
  4053. enum pipe pipe = 0;
  4054. struct intel_encoder *encoder = connector->encoder;
  4055. return encoder->get_hw_state(encoder, &pipe);
  4056. }
  4057. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4058. struct intel_crtc_config *pipe_config)
  4059. {
  4060. struct drm_i915_private *dev_priv = dev->dev_private;
  4061. struct intel_crtc *pipe_B_crtc =
  4062. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4063. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4064. pipe_name(pipe), pipe_config->fdi_lanes);
  4065. if (pipe_config->fdi_lanes > 4) {
  4066. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4067. pipe_name(pipe), pipe_config->fdi_lanes);
  4068. return false;
  4069. }
  4070. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4071. if (pipe_config->fdi_lanes > 2) {
  4072. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4073. pipe_config->fdi_lanes);
  4074. return false;
  4075. } else {
  4076. return true;
  4077. }
  4078. }
  4079. if (INTEL_INFO(dev)->num_pipes == 2)
  4080. return true;
  4081. /* Ivybridge 3 pipe is really complicated */
  4082. switch (pipe) {
  4083. case PIPE_A:
  4084. return true;
  4085. case PIPE_B:
  4086. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4087. pipe_config->fdi_lanes > 2) {
  4088. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4089. pipe_name(pipe), pipe_config->fdi_lanes);
  4090. return false;
  4091. }
  4092. return true;
  4093. case PIPE_C:
  4094. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4095. pipe_B_crtc->config.fdi_lanes <= 2) {
  4096. if (pipe_config->fdi_lanes > 2) {
  4097. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4098. pipe_name(pipe), pipe_config->fdi_lanes);
  4099. return false;
  4100. }
  4101. } else {
  4102. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4103. return false;
  4104. }
  4105. return true;
  4106. default:
  4107. BUG();
  4108. }
  4109. }
  4110. #define RETRY 1
  4111. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4112. struct intel_crtc_config *pipe_config)
  4113. {
  4114. struct drm_device *dev = intel_crtc->base.dev;
  4115. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4116. int lane, link_bw, fdi_dotclock;
  4117. bool setup_ok, needs_recompute = false;
  4118. retry:
  4119. /* FDI is a binary signal running at ~2.7GHz, encoding
  4120. * each output octet as 10 bits. The actual frequency
  4121. * is stored as a divider into a 100MHz clock, and the
  4122. * mode pixel clock is stored in units of 1KHz.
  4123. * Hence the bw of each lane in terms of the mode signal
  4124. * is:
  4125. */
  4126. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4127. fdi_dotclock = adjusted_mode->crtc_clock;
  4128. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4129. pipe_config->pipe_bpp);
  4130. pipe_config->fdi_lanes = lane;
  4131. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4132. link_bw, &pipe_config->fdi_m_n);
  4133. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4134. intel_crtc->pipe, pipe_config);
  4135. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4136. pipe_config->pipe_bpp -= 2*3;
  4137. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4138. pipe_config->pipe_bpp);
  4139. needs_recompute = true;
  4140. pipe_config->bw_constrained = true;
  4141. goto retry;
  4142. }
  4143. if (needs_recompute)
  4144. return RETRY;
  4145. return setup_ok ? 0 : -EINVAL;
  4146. }
  4147. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4148. struct intel_crtc_config *pipe_config)
  4149. {
  4150. pipe_config->ips_enabled = i915.enable_ips &&
  4151. hsw_crtc_supports_ips(crtc) &&
  4152. pipe_config->pipe_bpp <= 24;
  4153. }
  4154. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4155. struct intel_crtc_config *pipe_config)
  4156. {
  4157. struct drm_device *dev = crtc->base.dev;
  4158. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4159. /* FIXME should check pixel clock limits on all platforms */
  4160. if (INTEL_INFO(dev)->gen < 4) {
  4161. struct drm_i915_private *dev_priv = dev->dev_private;
  4162. int clock_limit =
  4163. dev_priv->display.get_display_clock_speed(dev);
  4164. /*
  4165. * Enable pixel doubling when the dot clock
  4166. * is > 90% of the (display) core speed.
  4167. *
  4168. * GDG double wide on either pipe,
  4169. * otherwise pipe A only.
  4170. */
  4171. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4172. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4173. clock_limit *= 2;
  4174. pipe_config->double_wide = true;
  4175. }
  4176. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4177. return -EINVAL;
  4178. }
  4179. /*
  4180. * Pipe horizontal size must be even in:
  4181. * - DVO ganged mode
  4182. * - LVDS dual channel mode
  4183. * - Double wide pipe
  4184. */
  4185. if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4186. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4187. pipe_config->pipe_src_w &= ~1;
  4188. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4189. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4190. */
  4191. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4192. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4193. return -EINVAL;
  4194. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4195. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4196. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4197. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4198. * for lvds. */
  4199. pipe_config->pipe_bpp = 8*3;
  4200. }
  4201. if (HAS_IPS(dev))
  4202. hsw_compute_ips_config(crtc, pipe_config);
  4203. /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
  4204. * clock survives for now. */
  4205. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  4206. pipe_config->shared_dpll = crtc->config.shared_dpll;
  4207. if (pipe_config->has_pch_encoder)
  4208. return ironlake_fdi_compute_config(crtc, pipe_config);
  4209. return 0;
  4210. }
  4211. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4212. {
  4213. return 400000; /* FIXME */
  4214. }
  4215. static int i945_get_display_clock_speed(struct drm_device *dev)
  4216. {
  4217. return 400000;
  4218. }
  4219. static int i915_get_display_clock_speed(struct drm_device *dev)
  4220. {
  4221. return 333000;
  4222. }
  4223. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4224. {
  4225. return 200000;
  4226. }
  4227. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4228. {
  4229. u16 gcfgc = 0;
  4230. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4231. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4232. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4233. return 267000;
  4234. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4235. return 333000;
  4236. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4237. return 444000;
  4238. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4239. return 200000;
  4240. default:
  4241. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4242. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4243. return 133000;
  4244. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4245. return 167000;
  4246. }
  4247. }
  4248. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4249. {
  4250. u16 gcfgc = 0;
  4251. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4252. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4253. return 133000;
  4254. else {
  4255. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4256. case GC_DISPLAY_CLOCK_333_MHZ:
  4257. return 333000;
  4258. default:
  4259. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4260. return 190000;
  4261. }
  4262. }
  4263. }
  4264. static int i865_get_display_clock_speed(struct drm_device *dev)
  4265. {
  4266. return 266000;
  4267. }
  4268. static int i855_get_display_clock_speed(struct drm_device *dev)
  4269. {
  4270. u16 hpllcc = 0;
  4271. /* Assume that the hardware is in the high speed state. This
  4272. * should be the default.
  4273. */
  4274. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4275. case GC_CLOCK_133_200:
  4276. case GC_CLOCK_100_200:
  4277. return 200000;
  4278. case GC_CLOCK_166_250:
  4279. return 250000;
  4280. case GC_CLOCK_100_133:
  4281. return 133000;
  4282. }
  4283. /* Shouldn't happen */
  4284. return 0;
  4285. }
  4286. static int i830_get_display_clock_speed(struct drm_device *dev)
  4287. {
  4288. return 133000;
  4289. }
  4290. static void
  4291. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4292. {
  4293. while (*num > DATA_LINK_M_N_MASK ||
  4294. *den > DATA_LINK_M_N_MASK) {
  4295. *num >>= 1;
  4296. *den >>= 1;
  4297. }
  4298. }
  4299. static void compute_m_n(unsigned int m, unsigned int n,
  4300. uint32_t *ret_m, uint32_t *ret_n)
  4301. {
  4302. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4303. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4304. intel_reduce_m_n_ratio(ret_m, ret_n);
  4305. }
  4306. void
  4307. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4308. int pixel_clock, int link_clock,
  4309. struct intel_link_m_n *m_n)
  4310. {
  4311. m_n->tu = 64;
  4312. compute_m_n(bits_per_pixel * pixel_clock,
  4313. link_clock * nlanes * 8,
  4314. &m_n->gmch_m, &m_n->gmch_n);
  4315. compute_m_n(pixel_clock, link_clock,
  4316. &m_n->link_m, &m_n->link_n);
  4317. }
  4318. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4319. {
  4320. if (i915.panel_use_ssc >= 0)
  4321. return i915.panel_use_ssc != 0;
  4322. return dev_priv->vbt.lvds_use_ssc
  4323. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4324. }
  4325. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  4326. {
  4327. struct drm_device *dev = crtc->dev;
  4328. struct drm_i915_private *dev_priv = dev->dev_private;
  4329. int refclk;
  4330. if (IS_VALLEYVIEW(dev)) {
  4331. refclk = 100000;
  4332. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4333. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4334. refclk = dev_priv->vbt.lvds_ssc_freq;
  4335. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4336. } else if (!IS_GEN2(dev)) {
  4337. refclk = 96000;
  4338. } else {
  4339. refclk = 48000;
  4340. }
  4341. return refclk;
  4342. }
  4343. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4344. {
  4345. return (1 << dpll->n) << 16 | dpll->m2;
  4346. }
  4347. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4348. {
  4349. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4350. }
  4351. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4352. intel_clock_t *reduced_clock)
  4353. {
  4354. struct drm_device *dev = crtc->base.dev;
  4355. struct drm_i915_private *dev_priv = dev->dev_private;
  4356. int pipe = crtc->pipe;
  4357. u32 fp, fp2 = 0;
  4358. if (IS_PINEVIEW(dev)) {
  4359. fp = pnv_dpll_compute_fp(&crtc->config.dpll);
  4360. if (reduced_clock)
  4361. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4362. } else {
  4363. fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
  4364. if (reduced_clock)
  4365. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4366. }
  4367. I915_WRITE(FP0(pipe), fp);
  4368. crtc->config.dpll_hw_state.fp0 = fp;
  4369. crtc->lowfreq_avail = false;
  4370. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4371. reduced_clock && i915.powersave) {
  4372. I915_WRITE(FP1(pipe), fp2);
  4373. crtc->config.dpll_hw_state.fp1 = fp2;
  4374. crtc->lowfreq_avail = true;
  4375. } else {
  4376. I915_WRITE(FP1(pipe), fp);
  4377. crtc->config.dpll_hw_state.fp1 = fp;
  4378. }
  4379. }
  4380. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4381. pipe)
  4382. {
  4383. u32 reg_val;
  4384. /*
  4385. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4386. * and set it to a reasonable value instead.
  4387. */
  4388. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4389. reg_val &= 0xffffff00;
  4390. reg_val |= 0x00000030;
  4391. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4392. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4393. reg_val &= 0x8cffffff;
  4394. reg_val = 0x8c000000;
  4395. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4396. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4397. reg_val &= 0xffffff00;
  4398. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4399. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4400. reg_val &= 0x00ffffff;
  4401. reg_val |= 0xb0000000;
  4402. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4403. }
  4404. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4405. struct intel_link_m_n *m_n)
  4406. {
  4407. struct drm_device *dev = crtc->base.dev;
  4408. struct drm_i915_private *dev_priv = dev->dev_private;
  4409. int pipe = crtc->pipe;
  4410. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4411. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4412. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4413. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4414. }
  4415. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4416. struct intel_link_m_n *m_n)
  4417. {
  4418. struct drm_device *dev = crtc->base.dev;
  4419. struct drm_i915_private *dev_priv = dev->dev_private;
  4420. int pipe = crtc->pipe;
  4421. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4422. if (INTEL_INFO(dev)->gen >= 5) {
  4423. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4424. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4425. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4426. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4427. } else {
  4428. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4429. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4430. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4431. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4432. }
  4433. }
  4434. static void intel_dp_set_m_n(struct intel_crtc *crtc)
  4435. {
  4436. if (crtc->config.has_pch_encoder)
  4437. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4438. else
  4439. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4440. }
  4441. static void vlv_update_pll(struct intel_crtc *crtc)
  4442. {
  4443. struct drm_device *dev = crtc->base.dev;
  4444. struct drm_i915_private *dev_priv = dev->dev_private;
  4445. int pipe = crtc->pipe;
  4446. u32 dpll, mdiv;
  4447. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4448. u32 coreclk, reg_val, dpll_md;
  4449. mutex_lock(&dev_priv->dpio_lock);
  4450. bestn = crtc->config.dpll.n;
  4451. bestm1 = crtc->config.dpll.m1;
  4452. bestm2 = crtc->config.dpll.m2;
  4453. bestp1 = crtc->config.dpll.p1;
  4454. bestp2 = crtc->config.dpll.p2;
  4455. /* See eDP HDMI DPIO driver vbios notes doc */
  4456. /* PLL B needs special handling */
  4457. if (pipe)
  4458. vlv_pllb_recal_opamp(dev_priv, pipe);
  4459. /* Set up Tx target for periodic Rcomp update */
  4460. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4461. /* Disable target IRef on PLL */
  4462. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4463. reg_val &= 0x00ffffff;
  4464. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4465. /* Disable fast lock */
  4466. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4467. /* Set idtafcrecal before PLL is enabled */
  4468. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4469. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4470. mdiv |= ((bestn << DPIO_N_SHIFT));
  4471. mdiv |= (1 << DPIO_K_SHIFT);
  4472. /*
  4473. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4474. * but we don't support that).
  4475. * Note: don't use the DAC post divider as it seems unstable.
  4476. */
  4477. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4478. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4479. mdiv |= DPIO_ENABLE_CALIBRATION;
  4480. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4481. /* Set HBR and RBR LPF coefficients */
  4482. if (crtc->config.port_clock == 162000 ||
  4483. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
  4484. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
  4485. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4486. 0x009f0003);
  4487. else
  4488. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4489. 0x00d0000f);
  4490. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
  4491. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
  4492. /* Use SSC source */
  4493. if (!pipe)
  4494. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4495. 0x0df40000);
  4496. else
  4497. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4498. 0x0df70000);
  4499. } else { /* HDMI or VGA */
  4500. /* Use bend source */
  4501. if (!pipe)
  4502. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4503. 0x0df70000);
  4504. else
  4505. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4506. 0x0df40000);
  4507. }
  4508. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  4509. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  4510. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
  4511. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
  4512. coreclk |= 0x01000000;
  4513. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  4514. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  4515. /*
  4516. * Enable DPIO clock input. We should never disable the reference
  4517. * clock for pipe B, since VGA hotplug / manual detection depends
  4518. * on it.
  4519. */
  4520. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4521. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4522. /* We should never disable this, set it here for state tracking */
  4523. if (pipe == PIPE_B)
  4524. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4525. dpll |= DPLL_VCO_ENABLE;
  4526. crtc->config.dpll_hw_state.dpll = dpll;
  4527. dpll_md = (crtc->config.pixel_multiplier - 1)
  4528. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4529. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4530. mutex_unlock(&dev_priv->dpio_lock);
  4531. }
  4532. static void chv_update_pll(struct intel_crtc *crtc)
  4533. {
  4534. struct drm_device *dev = crtc->base.dev;
  4535. struct drm_i915_private *dev_priv = dev->dev_private;
  4536. int pipe = crtc->pipe;
  4537. int dpll_reg = DPLL(crtc->pipe);
  4538. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  4539. u32 val, loopfilter, intcoeff;
  4540. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  4541. int refclk;
  4542. mutex_lock(&dev_priv->dpio_lock);
  4543. bestn = crtc->config.dpll.n;
  4544. bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
  4545. bestm1 = crtc->config.dpll.m1;
  4546. bestm2 = crtc->config.dpll.m2 >> 22;
  4547. bestp1 = crtc->config.dpll.p1;
  4548. bestp2 = crtc->config.dpll.p2;
  4549. /*
  4550. * Enable Refclk and SSC
  4551. */
  4552. val = I915_READ(dpll_reg);
  4553. val |= (DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV);
  4554. I915_WRITE(dpll_reg, val);
  4555. /* Propagate soft reset to data lane reset */
  4556. val = vlv_dpio_read(dev_priv, pipe, VLV_PCS_DW0(port));
  4557. val &= ~(DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET);
  4558. vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW0(port), val);
  4559. /* Disable 10bit clock to display controller */
  4560. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  4561. val &= ~DPIO_DCLKP_EN;
  4562. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  4563. /* p1 and p2 divider */
  4564. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  4565. 5 << DPIO_CHV_S1_DIV_SHIFT |
  4566. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  4567. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  4568. 1 << DPIO_CHV_K_DIV_SHIFT);
  4569. /* Feedback post-divider - m2 */
  4570. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  4571. /* Feedback refclk divider - n and m1 */
  4572. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  4573. DPIO_CHV_M1_DIV_BY_2 |
  4574. 1 << DPIO_CHV_N_DIV_SHIFT);
  4575. /* M2 fraction division */
  4576. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  4577. /* M2 fraction division enable */
  4578. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  4579. DPIO_CHV_FRAC_DIV_EN |
  4580. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  4581. /* Loop filter */
  4582. refclk = i9xx_get_refclk(&crtc->base, 0);
  4583. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  4584. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  4585. if (refclk == 100000)
  4586. intcoeff = 11;
  4587. else if (refclk == 38400)
  4588. intcoeff = 10;
  4589. else
  4590. intcoeff = 9;
  4591. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  4592. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  4593. /* AFC Recal */
  4594. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  4595. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  4596. DPIO_AFC_RECAL);
  4597. mutex_unlock(&dev_priv->dpio_lock);
  4598. }
  4599. static void i9xx_update_pll(struct intel_crtc *crtc,
  4600. intel_clock_t *reduced_clock,
  4601. int num_connectors)
  4602. {
  4603. struct drm_device *dev = crtc->base.dev;
  4604. struct drm_i915_private *dev_priv = dev->dev_private;
  4605. u32 dpll;
  4606. bool is_sdvo;
  4607. struct dpll *clock = &crtc->config.dpll;
  4608. i9xx_update_pll_dividers(crtc, reduced_clock);
  4609. is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
  4610. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  4611. dpll = DPLL_VGA_MODE_DIS;
  4612. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
  4613. dpll |= DPLLB_MODE_LVDS;
  4614. else
  4615. dpll |= DPLLB_MODE_DAC_SERIAL;
  4616. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  4617. dpll |= (crtc->config.pixel_multiplier - 1)
  4618. << SDVO_MULTIPLIER_SHIFT_HIRES;
  4619. }
  4620. if (is_sdvo)
  4621. dpll |= DPLL_SDVO_HIGH_SPEED;
  4622. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
  4623. dpll |= DPLL_SDVO_HIGH_SPEED;
  4624. /* compute bitmask from p1 value */
  4625. if (IS_PINEVIEW(dev))
  4626. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4627. else {
  4628. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4629. if (IS_G4X(dev) && reduced_clock)
  4630. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4631. }
  4632. switch (clock->p2) {
  4633. case 5:
  4634. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4635. break;
  4636. case 7:
  4637. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4638. break;
  4639. case 10:
  4640. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4641. break;
  4642. case 14:
  4643. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4644. break;
  4645. }
  4646. if (INTEL_INFO(dev)->gen >= 4)
  4647. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4648. if (crtc->config.sdvo_tv_clock)
  4649. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4650. else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4651. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4652. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4653. else
  4654. dpll |= PLL_REF_INPUT_DREFCLK;
  4655. dpll |= DPLL_VCO_ENABLE;
  4656. crtc->config.dpll_hw_state.dpll = dpll;
  4657. if (INTEL_INFO(dev)->gen >= 4) {
  4658. u32 dpll_md = (crtc->config.pixel_multiplier - 1)
  4659. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4660. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4661. }
  4662. }
  4663. static void i8xx_update_pll(struct intel_crtc *crtc,
  4664. intel_clock_t *reduced_clock,
  4665. int num_connectors)
  4666. {
  4667. struct drm_device *dev = crtc->base.dev;
  4668. struct drm_i915_private *dev_priv = dev->dev_private;
  4669. u32 dpll;
  4670. struct dpll *clock = &crtc->config.dpll;
  4671. i9xx_update_pll_dividers(crtc, reduced_clock);
  4672. dpll = DPLL_VGA_MODE_DIS;
  4673. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
  4674. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4675. } else {
  4676. if (clock->p1 == 2)
  4677. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4678. else
  4679. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4680. if (clock->p2 == 4)
  4681. dpll |= PLL_P2_DIVIDE_BY_4;
  4682. }
  4683. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
  4684. dpll |= DPLL_DVO_2X_MODE;
  4685. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4686. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4687. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4688. else
  4689. dpll |= PLL_REF_INPUT_DREFCLK;
  4690. dpll |= DPLL_VCO_ENABLE;
  4691. crtc->config.dpll_hw_state.dpll = dpll;
  4692. }
  4693. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  4694. {
  4695. struct drm_device *dev = intel_crtc->base.dev;
  4696. struct drm_i915_private *dev_priv = dev->dev_private;
  4697. enum pipe pipe = intel_crtc->pipe;
  4698. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  4699. struct drm_display_mode *adjusted_mode =
  4700. &intel_crtc->config.adjusted_mode;
  4701. uint32_t crtc_vtotal, crtc_vblank_end;
  4702. int vsyncshift = 0;
  4703. /* We need to be careful not to changed the adjusted mode, for otherwise
  4704. * the hw state checker will get angry at the mismatch. */
  4705. crtc_vtotal = adjusted_mode->crtc_vtotal;
  4706. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  4707. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4708. /* the chip adds 2 halflines automatically */
  4709. crtc_vtotal -= 1;
  4710. crtc_vblank_end -= 1;
  4711. if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  4712. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  4713. else
  4714. vsyncshift = adjusted_mode->crtc_hsync_start -
  4715. adjusted_mode->crtc_htotal / 2;
  4716. if (vsyncshift < 0)
  4717. vsyncshift += adjusted_mode->crtc_htotal;
  4718. }
  4719. if (INTEL_INFO(dev)->gen > 3)
  4720. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  4721. I915_WRITE(HTOTAL(cpu_transcoder),
  4722. (adjusted_mode->crtc_hdisplay - 1) |
  4723. ((adjusted_mode->crtc_htotal - 1) << 16));
  4724. I915_WRITE(HBLANK(cpu_transcoder),
  4725. (adjusted_mode->crtc_hblank_start - 1) |
  4726. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4727. I915_WRITE(HSYNC(cpu_transcoder),
  4728. (adjusted_mode->crtc_hsync_start - 1) |
  4729. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4730. I915_WRITE(VTOTAL(cpu_transcoder),
  4731. (adjusted_mode->crtc_vdisplay - 1) |
  4732. ((crtc_vtotal - 1) << 16));
  4733. I915_WRITE(VBLANK(cpu_transcoder),
  4734. (adjusted_mode->crtc_vblank_start - 1) |
  4735. ((crtc_vblank_end - 1) << 16));
  4736. I915_WRITE(VSYNC(cpu_transcoder),
  4737. (adjusted_mode->crtc_vsync_start - 1) |
  4738. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4739. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  4740. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  4741. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  4742. * bits. */
  4743. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  4744. (pipe == PIPE_B || pipe == PIPE_C))
  4745. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  4746. /* pipesrc controls the size that is scaled from, which should
  4747. * always be the user's requested size.
  4748. */
  4749. I915_WRITE(PIPESRC(pipe),
  4750. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  4751. (intel_crtc->config.pipe_src_h - 1));
  4752. }
  4753. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  4754. struct intel_crtc_config *pipe_config)
  4755. {
  4756. struct drm_device *dev = crtc->base.dev;
  4757. struct drm_i915_private *dev_priv = dev->dev_private;
  4758. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  4759. uint32_t tmp;
  4760. tmp = I915_READ(HTOTAL(cpu_transcoder));
  4761. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  4762. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  4763. tmp = I915_READ(HBLANK(cpu_transcoder));
  4764. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  4765. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  4766. tmp = I915_READ(HSYNC(cpu_transcoder));
  4767. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  4768. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  4769. tmp = I915_READ(VTOTAL(cpu_transcoder));
  4770. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  4771. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  4772. tmp = I915_READ(VBLANK(cpu_transcoder));
  4773. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  4774. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  4775. tmp = I915_READ(VSYNC(cpu_transcoder));
  4776. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  4777. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  4778. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  4779. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  4780. pipe_config->adjusted_mode.crtc_vtotal += 1;
  4781. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  4782. }
  4783. tmp = I915_READ(PIPESRC(crtc->pipe));
  4784. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  4785. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  4786. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  4787. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  4788. }
  4789. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  4790. struct intel_crtc_config *pipe_config)
  4791. {
  4792. mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  4793. mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
  4794. mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  4795. mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  4796. mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  4797. mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  4798. mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  4799. mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  4800. mode->flags = pipe_config->adjusted_mode.flags;
  4801. mode->clock = pipe_config->adjusted_mode.crtc_clock;
  4802. mode->flags |= pipe_config->adjusted_mode.flags;
  4803. }
  4804. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  4805. {
  4806. struct drm_device *dev = intel_crtc->base.dev;
  4807. struct drm_i915_private *dev_priv = dev->dev_private;
  4808. uint32_t pipeconf;
  4809. pipeconf = 0;
  4810. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  4811. I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
  4812. pipeconf |= PIPECONF_ENABLE;
  4813. if (intel_crtc->config.double_wide)
  4814. pipeconf |= PIPECONF_DOUBLE_WIDE;
  4815. /* only g4x and later have fancy bpc/dither controls */
  4816. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  4817. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  4818. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  4819. pipeconf |= PIPECONF_DITHER_EN |
  4820. PIPECONF_DITHER_TYPE_SP;
  4821. switch (intel_crtc->config.pipe_bpp) {
  4822. case 18:
  4823. pipeconf |= PIPECONF_6BPC;
  4824. break;
  4825. case 24:
  4826. pipeconf |= PIPECONF_8BPC;
  4827. break;
  4828. case 30:
  4829. pipeconf |= PIPECONF_10BPC;
  4830. break;
  4831. default:
  4832. /* Case prevented by intel_choose_pipe_bpp_dither. */
  4833. BUG();
  4834. }
  4835. }
  4836. if (HAS_PIPE_CXSR(dev)) {
  4837. if (intel_crtc->lowfreq_avail) {
  4838. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4839. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4840. } else {
  4841. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4842. }
  4843. }
  4844. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  4845. if (INTEL_INFO(dev)->gen < 4 ||
  4846. intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  4847. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4848. else
  4849. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  4850. } else
  4851. pipeconf |= PIPECONF_PROGRESSIVE;
  4852. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  4853. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  4854. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  4855. POSTING_READ(PIPECONF(intel_crtc->pipe));
  4856. }
  4857. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  4858. int x, int y,
  4859. struct drm_framebuffer *fb)
  4860. {
  4861. struct drm_device *dev = crtc->dev;
  4862. struct drm_i915_private *dev_priv = dev->dev_private;
  4863. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4864. int pipe = intel_crtc->pipe;
  4865. int plane = intel_crtc->plane;
  4866. int refclk, num_connectors = 0;
  4867. intel_clock_t clock, reduced_clock;
  4868. u32 dspcntr;
  4869. bool ok, has_reduced_clock = false;
  4870. bool is_lvds = false, is_dsi = false;
  4871. struct intel_encoder *encoder;
  4872. const intel_limit_t *limit;
  4873. int ret;
  4874. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4875. switch (encoder->type) {
  4876. case INTEL_OUTPUT_LVDS:
  4877. is_lvds = true;
  4878. break;
  4879. case INTEL_OUTPUT_DSI:
  4880. is_dsi = true;
  4881. break;
  4882. }
  4883. num_connectors++;
  4884. }
  4885. if (is_dsi)
  4886. goto skip_dpll;
  4887. if (!intel_crtc->config.clock_set) {
  4888. refclk = i9xx_get_refclk(crtc, num_connectors);
  4889. /*
  4890. * Returns a set of divisors for the desired target clock with
  4891. * the given refclk, or FALSE. The returned values represent
  4892. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  4893. * 2) / p1 / p2.
  4894. */
  4895. limit = intel_limit(crtc, refclk);
  4896. ok = dev_priv->display.find_dpll(limit, crtc,
  4897. intel_crtc->config.port_clock,
  4898. refclk, NULL, &clock);
  4899. if (!ok) {
  4900. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4901. return -EINVAL;
  4902. }
  4903. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4904. /*
  4905. * Ensure we match the reduced clock's P to the target
  4906. * clock. If the clocks don't match, we can't switch
  4907. * the display clock by using the FP0/FP1. In such case
  4908. * we will disable the LVDS downclock feature.
  4909. */
  4910. has_reduced_clock =
  4911. dev_priv->display.find_dpll(limit, crtc,
  4912. dev_priv->lvds_downclock,
  4913. refclk, &clock,
  4914. &reduced_clock);
  4915. }
  4916. /* Compat-code for transition, will disappear. */
  4917. intel_crtc->config.dpll.n = clock.n;
  4918. intel_crtc->config.dpll.m1 = clock.m1;
  4919. intel_crtc->config.dpll.m2 = clock.m2;
  4920. intel_crtc->config.dpll.p1 = clock.p1;
  4921. intel_crtc->config.dpll.p2 = clock.p2;
  4922. }
  4923. if (IS_GEN2(dev)) {
  4924. i8xx_update_pll(intel_crtc,
  4925. has_reduced_clock ? &reduced_clock : NULL,
  4926. num_connectors);
  4927. } else if (IS_CHERRYVIEW(dev)) {
  4928. chv_update_pll(intel_crtc);
  4929. } else if (IS_VALLEYVIEW(dev)) {
  4930. vlv_update_pll(intel_crtc);
  4931. } else {
  4932. i9xx_update_pll(intel_crtc,
  4933. has_reduced_clock ? &reduced_clock : NULL,
  4934. num_connectors);
  4935. }
  4936. skip_dpll:
  4937. /* Set up the display plane register */
  4938. dspcntr = DISPPLANE_GAMMA_ENABLE;
  4939. if (!IS_VALLEYVIEW(dev)) {
  4940. if (pipe == 0)
  4941. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  4942. else
  4943. dspcntr |= DISPPLANE_SEL_PIPE_B;
  4944. }
  4945. if (intel_crtc->config.has_dp_encoder)
  4946. intel_dp_set_m_n(intel_crtc);
  4947. intel_set_pipe_timings(intel_crtc);
  4948. /* pipesrc and dspsize control the size that is scaled from,
  4949. * which should always be the user's requested size.
  4950. */
  4951. I915_WRITE(DSPSIZE(plane),
  4952. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  4953. (intel_crtc->config.pipe_src_w - 1));
  4954. I915_WRITE(DSPPOS(plane), 0);
  4955. i9xx_set_pipeconf(intel_crtc);
  4956. I915_WRITE(DSPCNTR(plane), dspcntr);
  4957. POSTING_READ(DSPCNTR(plane));
  4958. ret = intel_pipe_set_base(crtc, x, y, fb);
  4959. return ret;
  4960. }
  4961. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  4962. struct intel_crtc_config *pipe_config)
  4963. {
  4964. struct drm_device *dev = crtc->base.dev;
  4965. struct drm_i915_private *dev_priv = dev->dev_private;
  4966. uint32_t tmp;
  4967. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  4968. return;
  4969. tmp = I915_READ(PFIT_CONTROL);
  4970. if (!(tmp & PFIT_ENABLE))
  4971. return;
  4972. /* Check whether the pfit is attached to our pipe. */
  4973. if (INTEL_INFO(dev)->gen < 4) {
  4974. if (crtc->pipe != PIPE_B)
  4975. return;
  4976. } else {
  4977. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  4978. return;
  4979. }
  4980. pipe_config->gmch_pfit.control = tmp;
  4981. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  4982. if (INTEL_INFO(dev)->gen < 5)
  4983. pipe_config->gmch_pfit.lvds_border_bits =
  4984. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  4985. }
  4986. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  4987. struct intel_crtc_config *pipe_config)
  4988. {
  4989. struct drm_device *dev = crtc->base.dev;
  4990. struct drm_i915_private *dev_priv = dev->dev_private;
  4991. int pipe = pipe_config->cpu_transcoder;
  4992. intel_clock_t clock;
  4993. u32 mdiv;
  4994. int refclk = 100000;
  4995. mutex_lock(&dev_priv->dpio_lock);
  4996. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  4997. mutex_unlock(&dev_priv->dpio_lock);
  4998. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  4999. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5000. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5001. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5002. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5003. vlv_clock(refclk, &clock);
  5004. /* clock.dot is the fast clock */
  5005. pipe_config->port_clock = clock.dot / 5;
  5006. }
  5007. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5008. struct intel_plane_config *plane_config)
  5009. {
  5010. struct drm_device *dev = crtc->base.dev;
  5011. struct drm_i915_private *dev_priv = dev->dev_private;
  5012. u32 val, base, offset;
  5013. int pipe = crtc->pipe, plane = crtc->plane;
  5014. int fourcc, pixel_format;
  5015. int aligned_height;
  5016. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5017. if (!crtc->base.primary->fb) {
  5018. DRM_DEBUG_KMS("failed to alloc fb\n");
  5019. return;
  5020. }
  5021. val = I915_READ(DSPCNTR(plane));
  5022. if (INTEL_INFO(dev)->gen >= 4)
  5023. if (val & DISPPLANE_TILED)
  5024. plane_config->tiled = true;
  5025. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5026. fourcc = intel_format_to_fourcc(pixel_format);
  5027. crtc->base.primary->fb->pixel_format = fourcc;
  5028. crtc->base.primary->fb->bits_per_pixel =
  5029. drm_format_plane_cpp(fourcc, 0) * 8;
  5030. if (INTEL_INFO(dev)->gen >= 4) {
  5031. if (plane_config->tiled)
  5032. offset = I915_READ(DSPTILEOFF(plane));
  5033. else
  5034. offset = I915_READ(DSPLINOFF(plane));
  5035. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5036. } else {
  5037. base = I915_READ(DSPADDR(plane));
  5038. }
  5039. plane_config->base = base;
  5040. val = I915_READ(PIPESRC(pipe));
  5041. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5042. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5043. val = I915_READ(DSPSTRIDE(pipe));
  5044. crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
  5045. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5046. plane_config->tiled);
  5047. plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
  5048. aligned_height, PAGE_SIZE);
  5049. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5050. pipe, plane, crtc->base.primary->fb->width,
  5051. crtc->base.primary->fb->height,
  5052. crtc->base.primary->fb->bits_per_pixel, base,
  5053. crtc->base.primary->fb->pitches[0],
  5054. plane_config->size);
  5055. }
  5056. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5057. struct intel_crtc_config *pipe_config)
  5058. {
  5059. struct drm_device *dev = crtc->base.dev;
  5060. struct drm_i915_private *dev_priv = dev->dev_private;
  5061. int pipe = pipe_config->cpu_transcoder;
  5062. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5063. intel_clock_t clock;
  5064. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5065. int refclk = 100000;
  5066. mutex_lock(&dev_priv->dpio_lock);
  5067. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5068. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5069. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5070. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5071. mutex_unlock(&dev_priv->dpio_lock);
  5072. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5073. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5074. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5075. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5076. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5077. chv_clock(refclk, &clock);
  5078. /* clock.dot is the fast clock */
  5079. pipe_config->port_clock = clock.dot / 5;
  5080. }
  5081. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5082. struct intel_crtc_config *pipe_config)
  5083. {
  5084. struct drm_device *dev = crtc->base.dev;
  5085. struct drm_i915_private *dev_priv = dev->dev_private;
  5086. uint32_t tmp;
  5087. if (!intel_display_power_enabled(dev_priv,
  5088. POWER_DOMAIN_PIPE(crtc->pipe)))
  5089. return false;
  5090. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5091. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5092. tmp = I915_READ(PIPECONF(crtc->pipe));
  5093. if (!(tmp & PIPECONF_ENABLE))
  5094. return false;
  5095. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5096. switch (tmp & PIPECONF_BPC_MASK) {
  5097. case PIPECONF_6BPC:
  5098. pipe_config->pipe_bpp = 18;
  5099. break;
  5100. case PIPECONF_8BPC:
  5101. pipe_config->pipe_bpp = 24;
  5102. break;
  5103. case PIPECONF_10BPC:
  5104. pipe_config->pipe_bpp = 30;
  5105. break;
  5106. default:
  5107. break;
  5108. }
  5109. }
  5110. if (INTEL_INFO(dev)->gen < 4)
  5111. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5112. intel_get_pipe_timings(crtc, pipe_config);
  5113. i9xx_get_pfit_config(crtc, pipe_config);
  5114. if (INTEL_INFO(dev)->gen >= 4) {
  5115. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5116. pipe_config->pixel_multiplier =
  5117. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5118. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5119. pipe_config->dpll_hw_state.dpll_md = tmp;
  5120. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5121. tmp = I915_READ(DPLL(crtc->pipe));
  5122. pipe_config->pixel_multiplier =
  5123. ((tmp & SDVO_MULTIPLIER_MASK)
  5124. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5125. } else {
  5126. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5127. * port and will be fixed up in the encoder->get_config
  5128. * function. */
  5129. pipe_config->pixel_multiplier = 1;
  5130. }
  5131. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5132. if (!IS_VALLEYVIEW(dev)) {
  5133. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5134. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5135. } else {
  5136. /* Mask out read-only status bits. */
  5137. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5138. DPLL_PORTC_READY_MASK |
  5139. DPLL_PORTB_READY_MASK);
  5140. }
  5141. if (IS_CHERRYVIEW(dev))
  5142. chv_crtc_clock_get(crtc, pipe_config);
  5143. else if (IS_VALLEYVIEW(dev))
  5144. vlv_crtc_clock_get(crtc, pipe_config);
  5145. else
  5146. i9xx_crtc_clock_get(crtc, pipe_config);
  5147. return true;
  5148. }
  5149. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5150. {
  5151. struct drm_i915_private *dev_priv = dev->dev_private;
  5152. struct drm_mode_config *mode_config = &dev->mode_config;
  5153. struct intel_encoder *encoder;
  5154. u32 val, final;
  5155. bool has_lvds = false;
  5156. bool has_cpu_edp = false;
  5157. bool has_panel = false;
  5158. bool has_ck505 = false;
  5159. bool can_ssc = false;
  5160. /* We need to take the global config into account */
  5161. list_for_each_entry(encoder, &mode_config->encoder_list,
  5162. base.head) {
  5163. switch (encoder->type) {
  5164. case INTEL_OUTPUT_LVDS:
  5165. has_panel = true;
  5166. has_lvds = true;
  5167. break;
  5168. case INTEL_OUTPUT_EDP:
  5169. has_panel = true;
  5170. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5171. has_cpu_edp = true;
  5172. break;
  5173. }
  5174. }
  5175. if (HAS_PCH_IBX(dev)) {
  5176. has_ck505 = dev_priv->vbt.display_clock_mode;
  5177. can_ssc = has_ck505;
  5178. } else {
  5179. has_ck505 = false;
  5180. can_ssc = true;
  5181. }
  5182. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5183. has_panel, has_lvds, has_ck505);
  5184. /* Ironlake: try to setup display ref clock before DPLL
  5185. * enabling. This is only under driver's control after
  5186. * PCH B stepping, previous chipset stepping should be
  5187. * ignoring this setting.
  5188. */
  5189. val = I915_READ(PCH_DREF_CONTROL);
  5190. /* As we must carefully and slowly disable/enable each source in turn,
  5191. * compute the final state we want first and check if we need to
  5192. * make any changes at all.
  5193. */
  5194. final = val;
  5195. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5196. if (has_ck505)
  5197. final |= DREF_NONSPREAD_CK505_ENABLE;
  5198. else
  5199. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5200. final &= ~DREF_SSC_SOURCE_MASK;
  5201. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5202. final &= ~DREF_SSC1_ENABLE;
  5203. if (has_panel) {
  5204. final |= DREF_SSC_SOURCE_ENABLE;
  5205. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5206. final |= DREF_SSC1_ENABLE;
  5207. if (has_cpu_edp) {
  5208. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5209. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5210. else
  5211. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5212. } else
  5213. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5214. } else {
  5215. final |= DREF_SSC_SOURCE_DISABLE;
  5216. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5217. }
  5218. if (final == val)
  5219. return;
  5220. /* Always enable nonspread source */
  5221. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5222. if (has_ck505)
  5223. val |= DREF_NONSPREAD_CK505_ENABLE;
  5224. else
  5225. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5226. if (has_panel) {
  5227. val &= ~DREF_SSC_SOURCE_MASK;
  5228. val |= DREF_SSC_SOURCE_ENABLE;
  5229. /* SSC must be turned on before enabling the CPU output */
  5230. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5231. DRM_DEBUG_KMS("Using SSC on panel\n");
  5232. val |= DREF_SSC1_ENABLE;
  5233. } else
  5234. val &= ~DREF_SSC1_ENABLE;
  5235. /* Get SSC going before enabling the outputs */
  5236. I915_WRITE(PCH_DREF_CONTROL, val);
  5237. POSTING_READ(PCH_DREF_CONTROL);
  5238. udelay(200);
  5239. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5240. /* Enable CPU source on CPU attached eDP */
  5241. if (has_cpu_edp) {
  5242. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5243. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5244. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5245. }
  5246. else
  5247. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5248. } else
  5249. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5250. I915_WRITE(PCH_DREF_CONTROL, val);
  5251. POSTING_READ(PCH_DREF_CONTROL);
  5252. udelay(200);
  5253. } else {
  5254. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5255. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5256. /* Turn off CPU output */
  5257. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5258. I915_WRITE(PCH_DREF_CONTROL, val);
  5259. POSTING_READ(PCH_DREF_CONTROL);
  5260. udelay(200);
  5261. /* Turn off the SSC source */
  5262. val &= ~DREF_SSC_SOURCE_MASK;
  5263. val |= DREF_SSC_SOURCE_DISABLE;
  5264. /* Turn off SSC1 */
  5265. val &= ~DREF_SSC1_ENABLE;
  5266. I915_WRITE(PCH_DREF_CONTROL, val);
  5267. POSTING_READ(PCH_DREF_CONTROL);
  5268. udelay(200);
  5269. }
  5270. BUG_ON(val != final);
  5271. }
  5272. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5273. {
  5274. uint32_t tmp;
  5275. tmp = I915_READ(SOUTH_CHICKEN2);
  5276. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5277. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5278. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5279. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5280. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5281. tmp = I915_READ(SOUTH_CHICKEN2);
  5282. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5283. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5284. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5285. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5286. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5287. }
  5288. /* WaMPhyProgramming:hsw */
  5289. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5290. {
  5291. uint32_t tmp;
  5292. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5293. tmp &= ~(0xFF << 24);
  5294. tmp |= (0x12 << 24);
  5295. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5296. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5297. tmp |= (1 << 11);
  5298. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5299. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5300. tmp |= (1 << 11);
  5301. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5302. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5303. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5304. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5305. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5306. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5307. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5308. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5309. tmp &= ~(7 << 13);
  5310. tmp |= (5 << 13);
  5311. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5312. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5313. tmp &= ~(7 << 13);
  5314. tmp |= (5 << 13);
  5315. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5316. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5317. tmp &= ~0xFF;
  5318. tmp |= 0x1C;
  5319. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5320. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5321. tmp &= ~0xFF;
  5322. tmp |= 0x1C;
  5323. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5324. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5325. tmp &= ~(0xFF << 16);
  5326. tmp |= (0x1C << 16);
  5327. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5328. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5329. tmp &= ~(0xFF << 16);
  5330. tmp |= (0x1C << 16);
  5331. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5332. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5333. tmp |= (1 << 27);
  5334. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5335. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5336. tmp |= (1 << 27);
  5337. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5338. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5339. tmp &= ~(0xF << 28);
  5340. tmp |= (4 << 28);
  5341. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5342. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5343. tmp &= ~(0xF << 28);
  5344. tmp |= (4 << 28);
  5345. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5346. }
  5347. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5348. * Programming" based on the parameters passed:
  5349. * - Sequence to enable CLKOUT_DP
  5350. * - Sequence to enable CLKOUT_DP without spread
  5351. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5352. */
  5353. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5354. bool with_fdi)
  5355. {
  5356. struct drm_i915_private *dev_priv = dev->dev_private;
  5357. uint32_t reg, tmp;
  5358. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5359. with_spread = true;
  5360. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5361. with_fdi, "LP PCH doesn't have FDI\n"))
  5362. with_fdi = false;
  5363. mutex_lock(&dev_priv->dpio_lock);
  5364. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5365. tmp &= ~SBI_SSCCTL_DISABLE;
  5366. tmp |= SBI_SSCCTL_PATHALT;
  5367. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5368. udelay(24);
  5369. if (with_spread) {
  5370. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5371. tmp &= ~SBI_SSCCTL_PATHALT;
  5372. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5373. if (with_fdi) {
  5374. lpt_reset_fdi_mphy(dev_priv);
  5375. lpt_program_fdi_mphy(dev_priv);
  5376. }
  5377. }
  5378. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5379. SBI_GEN0 : SBI_DBUFF0;
  5380. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5381. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5382. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5383. mutex_unlock(&dev_priv->dpio_lock);
  5384. }
  5385. /* Sequence to disable CLKOUT_DP */
  5386. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5387. {
  5388. struct drm_i915_private *dev_priv = dev->dev_private;
  5389. uint32_t reg, tmp;
  5390. mutex_lock(&dev_priv->dpio_lock);
  5391. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5392. SBI_GEN0 : SBI_DBUFF0;
  5393. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5394. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5395. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5396. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5397. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5398. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5399. tmp |= SBI_SSCCTL_PATHALT;
  5400. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5401. udelay(32);
  5402. }
  5403. tmp |= SBI_SSCCTL_DISABLE;
  5404. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5405. }
  5406. mutex_unlock(&dev_priv->dpio_lock);
  5407. }
  5408. static void lpt_init_pch_refclk(struct drm_device *dev)
  5409. {
  5410. struct drm_mode_config *mode_config = &dev->mode_config;
  5411. struct intel_encoder *encoder;
  5412. bool has_vga = false;
  5413. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  5414. switch (encoder->type) {
  5415. case INTEL_OUTPUT_ANALOG:
  5416. has_vga = true;
  5417. break;
  5418. }
  5419. }
  5420. if (has_vga)
  5421. lpt_enable_clkout_dp(dev, true, true);
  5422. else
  5423. lpt_disable_clkout_dp(dev);
  5424. }
  5425. /*
  5426. * Initialize reference clocks when the driver loads
  5427. */
  5428. void intel_init_pch_refclk(struct drm_device *dev)
  5429. {
  5430. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5431. ironlake_init_pch_refclk(dev);
  5432. else if (HAS_PCH_LPT(dev))
  5433. lpt_init_pch_refclk(dev);
  5434. }
  5435. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5436. {
  5437. struct drm_device *dev = crtc->dev;
  5438. struct drm_i915_private *dev_priv = dev->dev_private;
  5439. struct intel_encoder *encoder;
  5440. int num_connectors = 0;
  5441. bool is_lvds = false;
  5442. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5443. switch (encoder->type) {
  5444. case INTEL_OUTPUT_LVDS:
  5445. is_lvds = true;
  5446. break;
  5447. }
  5448. num_connectors++;
  5449. }
  5450. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5451. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5452. dev_priv->vbt.lvds_ssc_freq);
  5453. return dev_priv->vbt.lvds_ssc_freq;
  5454. }
  5455. return 120000;
  5456. }
  5457. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5458. {
  5459. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5460. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5461. int pipe = intel_crtc->pipe;
  5462. uint32_t val;
  5463. val = 0;
  5464. switch (intel_crtc->config.pipe_bpp) {
  5465. case 18:
  5466. val |= PIPECONF_6BPC;
  5467. break;
  5468. case 24:
  5469. val |= PIPECONF_8BPC;
  5470. break;
  5471. case 30:
  5472. val |= PIPECONF_10BPC;
  5473. break;
  5474. case 36:
  5475. val |= PIPECONF_12BPC;
  5476. break;
  5477. default:
  5478. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5479. BUG();
  5480. }
  5481. if (intel_crtc->config.dither)
  5482. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5483. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5484. val |= PIPECONF_INTERLACED_ILK;
  5485. else
  5486. val |= PIPECONF_PROGRESSIVE;
  5487. if (intel_crtc->config.limited_color_range)
  5488. val |= PIPECONF_COLOR_RANGE_SELECT;
  5489. I915_WRITE(PIPECONF(pipe), val);
  5490. POSTING_READ(PIPECONF(pipe));
  5491. }
  5492. /*
  5493. * Set up the pipe CSC unit.
  5494. *
  5495. * Currently only full range RGB to limited range RGB conversion
  5496. * is supported, but eventually this should handle various
  5497. * RGB<->YCbCr scenarios as well.
  5498. */
  5499. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  5500. {
  5501. struct drm_device *dev = crtc->dev;
  5502. struct drm_i915_private *dev_priv = dev->dev_private;
  5503. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5504. int pipe = intel_crtc->pipe;
  5505. uint16_t coeff = 0x7800; /* 1.0 */
  5506. /*
  5507. * TODO: Check what kind of values actually come out of the pipe
  5508. * with these coeff/postoff values and adjust to get the best
  5509. * accuracy. Perhaps we even need to take the bpc value into
  5510. * consideration.
  5511. */
  5512. if (intel_crtc->config.limited_color_range)
  5513. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  5514. /*
  5515. * GY/GU and RY/RU should be the other way around according
  5516. * to BSpec, but reality doesn't agree. Just set them up in
  5517. * a way that results in the correct picture.
  5518. */
  5519. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  5520. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  5521. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  5522. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  5523. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  5524. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  5525. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  5526. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  5527. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  5528. if (INTEL_INFO(dev)->gen > 6) {
  5529. uint16_t postoff = 0;
  5530. if (intel_crtc->config.limited_color_range)
  5531. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  5532. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  5533. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  5534. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  5535. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  5536. } else {
  5537. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  5538. if (intel_crtc->config.limited_color_range)
  5539. mode |= CSC_BLACK_SCREEN_OFFSET;
  5540. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  5541. }
  5542. }
  5543. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  5544. {
  5545. struct drm_device *dev = crtc->dev;
  5546. struct drm_i915_private *dev_priv = dev->dev_private;
  5547. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5548. enum pipe pipe = intel_crtc->pipe;
  5549. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5550. uint32_t val;
  5551. val = 0;
  5552. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  5553. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5554. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5555. val |= PIPECONF_INTERLACED_ILK;
  5556. else
  5557. val |= PIPECONF_PROGRESSIVE;
  5558. I915_WRITE(PIPECONF(cpu_transcoder), val);
  5559. POSTING_READ(PIPECONF(cpu_transcoder));
  5560. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  5561. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  5562. if (IS_BROADWELL(dev)) {
  5563. val = 0;
  5564. switch (intel_crtc->config.pipe_bpp) {
  5565. case 18:
  5566. val |= PIPEMISC_DITHER_6_BPC;
  5567. break;
  5568. case 24:
  5569. val |= PIPEMISC_DITHER_8_BPC;
  5570. break;
  5571. case 30:
  5572. val |= PIPEMISC_DITHER_10_BPC;
  5573. break;
  5574. case 36:
  5575. val |= PIPEMISC_DITHER_12_BPC;
  5576. break;
  5577. default:
  5578. /* Case prevented by pipe_config_set_bpp. */
  5579. BUG();
  5580. }
  5581. if (intel_crtc->config.dither)
  5582. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  5583. I915_WRITE(PIPEMISC(pipe), val);
  5584. }
  5585. }
  5586. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  5587. intel_clock_t *clock,
  5588. bool *has_reduced_clock,
  5589. intel_clock_t *reduced_clock)
  5590. {
  5591. struct drm_device *dev = crtc->dev;
  5592. struct drm_i915_private *dev_priv = dev->dev_private;
  5593. struct intel_encoder *intel_encoder;
  5594. int refclk;
  5595. const intel_limit_t *limit;
  5596. bool ret, is_lvds = false;
  5597. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5598. switch (intel_encoder->type) {
  5599. case INTEL_OUTPUT_LVDS:
  5600. is_lvds = true;
  5601. break;
  5602. }
  5603. }
  5604. refclk = ironlake_get_refclk(crtc);
  5605. /*
  5606. * Returns a set of divisors for the desired target clock with the given
  5607. * refclk, or FALSE. The returned values represent the clock equation:
  5608. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  5609. */
  5610. limit = intel_limit(crtc, refclk);
  5611. ret = dev_priv->display.find_dpll(limit, crtc,
  5612. to_intel_crtc(crtc)->config.port_clock,
  5613. refclk, NULL, clock);
  5614. if (!ret)
  5615. return false;
  5616. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5617. /*
  5618. * Ensure we match the reduced clock's P to the target clock.
  5619. * If the clocks don't match, we can't switch the display clock
  5620. * by using the FP0/FP1. In such case we will disable the LVDS
  5621. * downclock feature.
  5622. */
  5623. *has_reduced_clock =
  5624. dev_priv->display.find_dpll(limit, crtc,
  5625. dev_priv->lvds_downclock,
  5626. refclk, clock,
  5627. reduced_clock);
  5628. }
  5629. return true;
  5630. }
  5631. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  5632. {
  5633. /*
  5634. * Account for spread spectrum to avoid
  5635. * oversubscribing the link. Max center spread
  5636. * is 2.5%; use 5% for safety's sake.
  5637. */
  5638. u32 bps = target_clock * bpp * 21 / 20;
  5639. return DIV_ROUND_UP(bps, link_bw * 8);
  5640. }
  5641. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  5642. {
  5643. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  5644. }
  5645. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  5646. u32 *fp,
  5647. intel_clock_t *reduced_clock, u32 *fp2)
  5648. {
  5649. struct drm_crtc *crtc = &intel_crtc->base;
  5650. struct drm_device *dev = crtc->dev;
  5651. struct drm_i915_private *dev_priv = dev->dev_private;
  5652. struct intel_encoder *intel_encoder;
  5653. uint32_t dpll;
  5654. int factor, num_connectors = 0;
  5655. bool is_lvds = false, is_sdvo = false;
  5656. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5657. switch (intel_encoder->type) {
  5658. case INTEL_OUTPUT_LVDS:
  5659. is_lvds = true;
  5660. break;
  5661. case INTEL_OUTPUT_SDVO:
  5662. case INTEL_OUTPUT_HDMI:
  5663. is_sdvo = true;
  5664. break;
  5665. }
  5666. num_connectors++;
  5667. }
  5668. /* Enable autotuning of the PLL clock (if permissible) */
  5669. factor = 21;
  5670. if (is_lvds) {
  5671. if ((intel_panel_use_ssc(dev_priv) &&
  5672. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  5673. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  5674. factor = 25;
  5675. } else if (intel_crtc->config.sdvo_tv_clock)
  5676. factor = 20;
  5677. if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
  5678. *fp |= FP_CB_TUNE;
  5679. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  5680. *fp2 |= FP_CB_TUNE;
  5681. dpll = 0;
  5682. if (is_lvds)
  5683. dpll |= DPLLB_MODE_LVDS;
  5684. else
  5685. dpll |= DPLLB_MODE_DAC_SERIAL;
  5686. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  5687. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  5688. if (is_sdvo)
  5689. dpll |= DPLL_SDVO_HIGH_SPEED;
  5690. if (intel_crtc->config.has_dp_encoder)
  5691. dpll |= DPLL_SDVO_HIGH_SPEED;
  5692. /* compute bitmask from p1 value */
  5693. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5694. /* also FPA1 */
  5695. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5696. switch (intel_crtc->config.dpll.p2) {
  5697. case 5:
  5698. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5699. break;
  5700. case 7:
  5701. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5702. break;
  5703. case 10:
  5704. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5705. break;
  5706. case 14:
  5707. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5708. break;
  5709. }
  5710. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5711. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5712. else
  5713. dpll |= PLL_REF_INPUT_DREFCLK;
  5714. return dpll | DPLL_VCO_ENABLE;
  5715. }
  5716. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  5717. int x, int y,
  5718. struct drm_framebuffer *fb)
  5719. {
  5720. struct drm_device *dev = crtc->dev;
  5721. struct drm_i915_private *dev_priv = dev->dev_private;
  5722. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5723. int pipe = intel_crtc->pipe;
  5724. int plane = intel_crtc->plane;
  5725. int num_connectors = 0;
  5726. intel_clock_t clock, reduced_clock;
  5727. u32 dpll = 0, fp = 0, fp2 = 0;
  5728. bool ok, has_reduced_clock = false;
  5729. bool is_lvds = false;
  5730. struct intel_encoder *encoder;
  5731. struct intel_shared_dpll *pll;
  5732. int ret;
  5733. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5734. switch (encoder->type) {
  5735. case INTEL_OUTPUT_LVDS:
  5736. is_lvds = true;
  5737. break;
  5738. }
  5739. num_connectors++;
  5740. }
  5741. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  5742. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  5743. ok = ironlake_compute_clocks(crtc, &clock,
  5744. &has_reduced_clock, &reduced_clock);
  5745. if (!ok && !intel_crtc->config.clock_set) {
  5746. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5747. return -EINVAL;
  5748. }
  5749. /* Compat-code for transition, will disappear. */
  5750. if (!intel_crtc->config.clock_set) {
  5751. intel_crtc->config.dpll.n = clock.n;
  5752. intel_crtc->config.dpll.m1 = clock.m1;
  5753. intel_crtc->config.dpll.m2 = clock.m2;
  5754. intel_crtc->config.dpll.p1 = clock.p1;
  5755. intel_crtc->config.dpll.p2 = clock.p2;
  5756. }
  5757. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  5758. if (intel_crtc->config.has_pch_encoder) {
  5759. fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
  5760. if (has_reduced_clock)
  5761. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  5762. dpll = ironlake_compute_dpll(intel_crtc,
  5763. &fp, &reduced_clock,
  5764. has_reduced_clock ? &fp2 : NULL);
  5765. intel_crtc->config.dpll_hw_state.dpll = dpll;
  5766. intel_crtc->config.dpll_hw_state.fp0 = fp;
  5767. if (has_reduced_clock)
  5768. intel_crtc->config.dpll_hw_state.fp1 = fp2;
  5769. else
  5770. intel_crtc->config.dpll_hw_state.fp1 = fp;
  5771. pll = intel_get_shared_dpll(intel_crtc);
  5772. if (pll == NULL) {
  5773. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  5774. pipe_name(pipe));
  5775. return -EINVAL;
  5776. }
  5777. } else
  5778. intel_put_shared_dpll(intel_crtc);
  5779. if (intel_crtc->config.has_dp_encoder)
  5780. intel_dp_set_m_n(intel_crtc);
  5781. if (is_lvds && has_reduced_clock && i915.powersave)
  5782. intel_crtc->lowfreq_avail = true;
  5783. else
  5784. intel_crtc->lowfreq_avail = false;
  5785. intel_set_pipe_timings(intel_crtc);
  5786. if (intel_crtc->config.has_pch_encoder) {
  5787. intel_cpu_transcoder_set_m_n(intel_crtc,
  5788. &intel_crtc->config.fdi_m_n);
  5789. }
  5790. ironlake_set_pipeconf(crtc);
  5791. /* Set up the display plane register */
  5792. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
  5793. POSTING_READ(DSPCNTR(plane));
  5794. ret = intel_pipe_set_base(crtc, x, y, fb);
  5795. return ret;
  5796. }
  5797. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  5798. struct intel_link_m_n *m_n)
  5799. {
  5800. struct drm_device *dev = crtc->base.dev;
  5801. struct drm_i915_private *dev_priv = dev->dev_private;
  5802. enum pipe pipe = crtc->pipe;
  5803. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  5804. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  5805. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  5806. & ~TU_SIZE_MASK;
  5807. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  5808. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  5809. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5810. }
  5811. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  5812. enum transcoder transcoder,
  5813. struct intel_link_m_n *m_n)
  5814. {
  5815. struct drm_device *dev = crtc->base.dev;
  5816. struct drm_i915_private *dev_priv = dev->dev_private;
  5817. enum pipe pipe = crtc->pipe;
  5818. if (INTEL_INFO(dev)->gen >= 5) {
  5819. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  5820. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  5821. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  5822. & ~TU_SIZE_MASK;
  5823. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  5824. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  5825. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5826. } else {
  5827. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  5828. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  5829. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  5830. & ~TU_SIZE_MASK;
  5831. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  5832. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  5833. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5834. }
  5835. }
  5836. void intel_dp_get_m_n(struct intel_crtc *crtc,
  5837. struct intel_crtc_config *pipe_config)
  5838. {
  5839. if (crtc->config.has_pch_encoder)
  5840. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  5841. else
  5842. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  5843. &pipe_config->dp_m_n);
  5844. }
  5845. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  5846. struct intel_crtc_config *pipe_config)
  5847. {
  5848. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  5849. &pipe_config->fdi_m_n);
  5850. }
  5851. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  5852. struct intel_crtc_config *pipe_config)
  5853. {
  5854. struct drm_device *dev = crtc->base.dev;
  5855. struct drm_i915_private *dev_priv = dev->dev_private;
  5856. uint32_t tmp;
  5857. tmp = I915_READ(PF_CTL(crtc->pipe));
  5858. if (tmp & PF_ENABLE) {
  5859. pipe_config->pch_pfit.enabled = true;
  5860. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  5861. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  5862. /* We currently do not free assignements of panel fitters on
  5863. * ivb/hsw (since we don't use the higher upscaling modes which
  5864. * differentiates them) so just WARN about this case for now. */
  5865. if (IS_GEN7(dev)) {
  5866. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  5867. PF_PIPE_SEL_IVB(crtc->pipe));
  5868. }
  5869. }
  5870. }
  5871. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  5872. struct intel_plane_config *plane_config)
  5873. {
  5874. struct drm_device *dev = crtc->base.dev;
  5875. struct drm_i915_private *dev_priv = dev->dev_private;
  5876. u32 val, base, offset;
  5877. int pipe = crtc->pipe, plane = crtc->plane;
  5878. int fourcc, pixel_format;
  5879. int aligned_height;
  5880. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5881. if (!crtc->base.primary->fb) {
  5882. DRM_DEBUG_KMS("failed to alloc fb\n");
  5883. return;
  5884. }
  5885. val = I915_READ(DSPCNTR(plane));
  5886. if (INTEL_INFO(dev)->gen >= 4)
  5887. if (val & DISPPLANE_TILED)
  5888. plane_config->tiled = true;
  5889. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5890. fourcc = intel_format_to_fourcc(pixel_format);
  5891. crtc->base.primary->fb->pixel_format = fourcc;
  5892. crtc->base.primary->fb->bits_per_pixel =
  5893. drm_format_plane_cpp(fourcc, 0) * 8;
  5894. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5895. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5896. offset = I915_READ(DSPOFFSET(plane));
  5897. } else {
  5898. if (plane_config->tiled)
  5899. offset = I915_READ(DSPTILEOFF(plane));
  5900. else
  5901. offset = I915_READ(DSPLINOFF(plane));
  5902. }
  5903. plane_config->base = base;
  5904. val = I915_READ(PIPESRC(pipe));
  5905. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5906. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5907. val = I915_READ(DSPSTRIDE(pipe));
  5908. crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
  5909. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5910. plane_config->tiled);
  5911. plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
  5912. aligned_height, PAGE_SIZE);
  5913. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5914. pipe, plane, crtc->base.primary->fb->width,
  5915. crtc->base.primary->fb->height,
  5916. crtc->base.primary->fb->bits_per_pixel, base,
  5917. crtc->base.primary->fb->pitches[0],
  5918. plane_config->size);
  5919. }
  5920. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  5921. struct intel_crtc_config *pipe_config)
  5922. {
  5923. struct drm_device *dev = crtc->base.dev;
  5924. struct drm_i915_private *dev_priv = dev->dev_private;
  5925. uint32_t tmp;
  5926. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5927. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5928. tmp = I915_READ(PIPECONF(crtc->pipe));
  5929. if (!(tmp & PIPECONF_ENABLE))
  5930. return false;
  5931. switch (tmp & PIPECONF_BPC_MASK) {
  5932. case PIPECONF_6BPC:
  5933. pipe_config->pipe_bpp = 18;
  5934. break;
  5935. case PIPECONF_8BPC:
  5936. pipe_config->pipe_bpp = 24;
  5937. break;
  5938. case PIPECONF_10BPC:
  5939. pipe_config->pipe_bpp = 30;
  5940. break;
  5941. case PIPECONF_12BPC:
  5942. pipe_config->pipe_bpp = 36;
  5943. break;
  5944. default:
  5945. break;
  5946. }
  5947. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  5948. struct intel_shared_dpll *pll;
  5949. pipe_config->has_pch_encoder = true;
  5950. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  5951. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  5952. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  5953. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  5954. if (HAS_PCH_IBX(dev_priv->dev)) {
  5955. pipe_config->shared_dpll =
  5956. (enum intel_dpll_id) crtc->pipe;
  5957. } else {
  5958. tmp = I915_READ(PCH_DPLL_SEL);
  5959. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  5960. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  5961. else
  5962. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  5963. }
  5964. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  5965. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  5966. &pipe_config->dpll_hw_state));
  5967. tmp = pipe_config->dpll_hw_state.dpll;
  5968. pipe_config->pixel_multiplier =
  5969. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  5970. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  5971. ironlake_pch_clock_get(crtc, pipe_config);
  5972. } else {
  5973. pipe_config->pixel_multiplier = 1;
  5974. }
  5975. intel_get_pipe_timings(crtc, pipe_config);
  5976. ironlake_get_pfit_config(crtc, pipe_config);
  5977. return true;
  5978. }
  5979. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  5980. {
  5981. struct drm_device *dev = dev_priv->dev;
  5982. struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
  5983. struct intel_crtc *crtc;
  5984. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
  5985. WARN(crtc->active, "CRTC for pipe %c enabled\n",
  5986. pipe_name(crtc->pipe));
  5987. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  5988. WARN(plls->spll_refcount, "SPLL enabled\n");
  5989. WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
  5990. WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
  5991. WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  5992. WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  5993. "CPU PWM1 enabled\n");
  5994. WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  5995. "CPU PWM2 enabled\n");
  5996. WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  5997. "PCH PWM1 enabled\n");
  5998. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  5999. "Utility pin enabled\n");
  6000. WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6001. /*
  6002. * In theory we can still leave IRQs enabled, as long as only the HPD
  6003. * interrupts remain enabled. We used to check for that, but since it's
  6004. * gen-specific and since we only disable LCPLL after we fully disable
  6005. * the interrupts, the check below should be enough.
  6006. */
  6007. WARN(!dev_priv->pm.irqs_disabled, "IRQs enabled\n");
  6008. }
  6009. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6010. {
  6011. struct drm_device *dev = dev_priv->dev;
  6012. if (IS_HASWELL(dev)) {
  6013. mutex_lock(&dev_priv->rps.hw_lock);
  6014. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6015. val))
  6016. DRM_ERROR("Failed to disable D_COMP\n");
  6017. mutex_unlock(&dev_priv->rps.hw_lock);
  6018. } else {
  6019. I915_WRITE(D_COMP, val);
  6020. }
  6021. POSTING_READ(D_COMP);
  6022. }
  6023. /*
  6024. * This function implements pieces of two sequences from BSpec:
  6025. * - Sequence for display software to disable LCPLL
  6026. * - Sequence for display software to allow package C8+
  6027. * The steps implemented here are just the steps that actually touch the LCPLL
  6028. * register. Callers should take care of disabling all the display engine
  6029. * functions, doing the mode unset, fixing interrupts, etc.
  6030. */
  6031. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6032. bool switch_to_fclk, bool allow_power_down)
  6033. {
  6034. uint32_t val;
  6035. assert_can_disable_lcpll(dev_priv);
  6036. val = I915_READ(LCPLL_CTL);
  6037. if (switch_to_fclk) {
  6038. val |= LCPLL_CD_SOURCE_FCLK;
  6039. I915_WRITE(LCPLL_CTL, val);
  6040. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6041. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6042. DRM_ERROR("Switching to FCLK failed\n");
  6043. val = I915_READ(LCPLL_CTL);
  6044. }
  6045. val |= LCPLL_PLL_DISABLE;
  6046. I915_WRITE(LCPLL_CTL, val);
  6047. POSTING_READ(LCPLL_CTL);
  6048. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6049. DRM_ERROR("LCPLL still locked\n");
  6050. val = I915_READ(D_COMP);
  6051. val |= D_COMP_COMP_DISABLE;
  6052. hsw_write_dcomp(dev_priv, val);
  6053. ndelay(100);
  6054. if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
  6055. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6056. if (allow_power_down) {
  6057. val = I915_READ(LCPLL_CTL);
  6058. val |= LCPLL_POWER_DOWN_ALLOW;
  6059. I915_WRITE(LCPLL_CTL, val);
  6060. POSTING_READ(LCPLL_CTL);
  6061. }
  6062. }
  6063. /*
  6064. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6065. * source.
  6066. */
  6067. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6068. {
  6069. uint32_t val;
  6070. unsigned long irqflags;
  6071. val = I915_READ(LCPLL_CTL);
  6072. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6073. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6074. return;
  6075. /*
  6076. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6077. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6078. *
  6079. * The other problem is that hsw_restore_lcpll() is called as part of
  6080. * the runtime PM resume sequence, so we can't just call
  6081. * gen6_gt_force_wake_get() because that function calls
  6082. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6083. * while we are on the resume sequence. So to solve this problem we have
  6084. * to call special forcewake code that doesn't touch runtime PM and
  6085. * doesn't enable the forcewake delayed work.
  6086. */
  6087. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6088. if (dev_priv->uncore.forcewake_count++ == 0)
  6089. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6090. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6091. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6092. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6093. I915_WRITE(LCPLL_CTL, val);
  6094. POSTING_READ(LCPLL_CTL);
  6095. }
  6096. val = I915_READ(D_COMP);
  6097. val |= D_COMP_COMP_FORCE;
  6098. val &= ~D_COMP_COMP_DISABLE;
  6099. hsw_write_dcomp(dev_priv, val);
  6100. val = I915_READ(LCPLL_CTL);
  6101. val &= ~LCPLL_PLL_DISABLE;
  6102. I915_WRITE(LCPLL_CTL, val);
  6103. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6104. DRM_ERROR("LCPLL not locked yet\n");
  6105. if (val & LCPLL_CD_SOURCE_FCLK) {
  6106. val = I915_READ(LCPLL_CTL);
  6107. val &= ~LCPLL_CD_SOURCE_FCLK;
  6108. I915_WRITE(LCPLL_CTL, val);
  6109. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6110. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6111. DRM_ERROR("Switching back to LCPLL failed\n");
  6112. }
  6113. /* See the big comment above. */
  6114. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6115. if (--dev_priv->uncore.forcewake_count == 0)
  6116. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6117. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6118. }
  6119. /*
  6120. * Package states C8 and deeper are really deep PC states that can only be
  6121. * reached when all the devices on the system allow it, so even if the graphics
  6122. * device allows PC8+, it doesn't mean the system will actually get to these
  6123. * states. Our driver only allows PC8+ when going into runtime PM.
  6124. *
  6125. * The requirements for PC8+ are that all the outputs are disabled, the power
  6126. * well is disabled and most interrupts are disabled, and these are also
  6127. * requirements for runtime PM. When these conditions are met, we manually do
  6128. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6129. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6130. * hang the machine.
  6131. *
  6132. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6133. * the state of some registers, so when we come back from PC8+ we need to
  6134. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6135. * need to take care of the registers kept by RC6. Notice that this happens even
  6136. * if we don't put the device in PCI D3 state (which is what currently happens
  6137. * because of the runtime PM support).
  6138. *
  6139. * For more, read "Display Sequences for Package C8" on the hardware
  6140. * documentation.
  6141. */
  6142. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6143. {
  6144. struct drm_device *dev = dev_priv->dev;
  6145. uint32_t val;
  6146. DRM_DEBUG_KMS("Enabling package C8+\n");
  6147. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6148. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6149. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6150. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6151. }
  6152. lpt_disable_clkout_dp(dev);
  6153. hsw_disable_lcpll(dev_priv, true, true);
  6154. }
  6155. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6156. {
  6157. struct drm_device *dev = dev_priv->dev;
  6158. uint32_t val;
  6159. DRM_DEBUG_KMS("Disabling package C8+\n");
  6160. hsw_restore_lcpll(dev_priv);
  6161. lpt_init_pch_refclk(dev);
  6162. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6163. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6164. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6165. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6166. }
  6167. intel_prepare_ddi(dev);
  6168. }
  6169. static void snb_modeset_global_resources(struct drm_device *dev)
  6170. {
  6171. modeset_update_crtc_power_domains(dev);
  6172. }
  6173. static void haswell_modeset_global_resources(struct drm_device *dev)
  6174. {
  6175. modeset_update_crtc_power_domains(dev);
  6176. }
  6177. static int haswell_crtc_mode_set(struct drm_crtc *crtc,
  6178. int x, int y,
  6179. struct drm_framebuffer *fb)
  6180. {
  6181. struct drm_device *dev = crtc->dev;
  6182. struct drm_i915_private *dev_priv = dev->dev_private;
  6183. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6184. int plane = intel_crtc->plane;
  6185. int ret;
  6186. if (!intel_ddi_pll_select(intel_crtc))
  6187. return -EINVAL;
  6188. intel_ddi_pll_enable(intel_crtc);
  6189. if (intel_crtc->config.has_dp_encoder)
  6190. intel_dp_set_m_n(intel_crtc);
  6191. intel_crtc->lowfreq_avail = false;
  6192. intel_set_pipe_timings(intel_crtc);
  6193. if (intel_crtc->config.has_pch_encoder) {
  6194. intel_cpu_transcoder_set_m_n(intel_crtc,
  6195. &intel_crtc->config.fdi_m_n);
  6196. }
  6197. haswell_set_pipeconf(crtc);
  6198. intel_set_pipe_csc(crtc);
  6199. /* Set up the display plane register */
  6200. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
  6201. POSTING_READ(DSPCNTR(plane));
  6202. ret = intel_pipe_set_base(crtc, x, y, fb);
  6203. return ret;
  6204. }
  6205. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6206. struct intel_crtc_config *pipe_config)
  6207. {
  6208. struct drm_device *dev = crtc->base.dev;
  6209. struct drm_i915_private *dev_priv = dev->dev_private;
  6210. enum intel_display_power_domain pfit_domain;
  6211. uint32_t tmp;
  6212. if (!intel_display_power_enabled(dev_priv,
  6213. POWER_DOMAIN_PIPE(crtc->pipe)))
  6214. return false;
  6215. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6216. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6217. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6218. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6219. enum pipe trans_edp_pipe;
  6220. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6221. default:
  6222. WARN(1, "unknown pipe linked to edp transcoder\n");
  6223. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6224. case TRANS_DDI_EDP_INPUT_A_ON:
  6225. trans_edp_pipe = PIPE_A;
  6226. break;
  6227. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6228. trans_edp_pipe = PIPE_B;
  6229. break;
  6230. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6231. trans_edp_pipe = PIPE_C;
  6232. break;
  6233. }
  6234. if (trans_edp_pipe == crtc->pipe)
  6235. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6236. }
  6237. if (!intel_display_power_enabled(dev_priv,
  6238. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6239. return false;
  6240. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6241. if (!(tmp & PIPECONF_ENABLE))
  6242. return false;
  6243. /*
  6244. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6245. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6246. * the PCH transcoder is on.
  6247. */
  6248. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6249. if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
  6250. I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6251. pipe_config->has_pch_encoder = true;
  6252. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6253. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6254. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6255. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6256. }
  6257. intel_get_pipe_timings(crtc, pipe_config);
  6258. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6259. if (intel_display_power_enabled(dev_priv, pfit_domain))
  6260. ironlake_get_pfit_config(crtc, pipe_config);
  6261. if (IS_HASWELL(dev))
  6262. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6263. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6264. pipe_config->pixel_multiplier = 1;
  6265. return true;
  6266. }
  6267. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  6268. int x, int y,
  6269. struct drm_framebuffer *fb)
  6270. {
  6271. struct drm_device *dev = crtc->dev;
  6272. struct drm_i915_private *dev_priv = dev->dev_private;
  6273. struct intel_encoder *encoder;
  6274. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6275. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  6276. int pipe = intel_crtc->pipe;
  6277. int ret;
  6278. drm_vblank_pre_modeset(dev, pipe);
  6279. ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
  6280. drm_vblank_post_modeset(dev, pipe);
  6281. if (ret != 0)
  6282. return ret;
  6283. for_each_encoder_on_crtc(dev, crtc, encoder) {
  6284. DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
  6285. encoder->base.base.id,
  6286. drm_get_encoder_name(&encoder->base),
  6287. mode->base.id, mode->name);
  6288. if (encoder->mode_set)
  6289. encoder->mode_set(encoder);
  6290. }
  6291. return 0;
  6292. }
  6293. static struct {
  6294. int clock;
  6295. u32 config;
  6296. } hdmi_audio_clock[] = {
  6297. { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  6298. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  6299. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  6300. { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  6301. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  6302. { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  6303. { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  6304. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  6305. { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  6306. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  6307. };
  6308. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  6309. static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
  6310. {
  6311. int i;
  6312. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  6313. if (mode->clock == hdmi_audio_clock[i].clock)
  6314. break;
  6315. }
  6316. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  6317. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
  6318. i = 1;
  6319. }
  6320. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  6321. hdmi_audio_clock[i].clock,
  6322. hdmi_audio_clock[i].config);
  6323. return hdmi_audio_clock[i].config;
  6324. }
  6325. static bool intel_eld_uptodate(struct drm_connector *connector,
  6326. int reg_eldv, uint32_t bits_eldv,
  6327. int reg_elda, uint32_t bits_elda,
  6328. int reg_edid)
  6329. {
  6330. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6331. uint8_t *eld = connector->eld;
  6332. uint32_t i;
  6333. i = I915_READ(reg_eldv);
  6334. i &= bits_eldv;
  6335. if (!eld[0])
  6336. return !i;
  6337. if (!i)
  6338. return false;
  6339. i = I915_READ(reg_elda);
  6340. i &= ~bits_elda;
  6341. I915_WRITE(reg_elda, i);
  6342. for (i = 0; i < eld[2]; i++)
  6343. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  6344. return false;
  6345. return true;
  6346. }
  6347. static void g4x_write_eld(struct drm_connector *connector,
  6348. struct drm_crtc *crtc,
  6349. struct drm_display_mode *mode)
  6350. {
  6351. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6352. uint8_t *eld = connector->eld;
  6353. uint32_t eldv;
  6354. uint32_t len;
  6355. uint32_t i;
  6356. i = I915_READ(G4X_AUD_VID_DID);
  6357. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  6358. eldv = G4X_ELDV_DEVCL_DEVBLC;
  6359. else
  6360. eldv = G4X_ELDV_DEVCTG;
  6361. if (intel_eld_uptodate(connector,
  6362. G4X_AUD_CNTL_ST, eldv,
  6363. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  6364. G4X_HDMIW_HDMIEDID))
  6365. return;
  6366. i = I915_READ(G4X_AUD_CNTL_ST);
  6367. i &= ~(eldv | G4X_ELD_ADDR);
  6368. len = (i >> 9) & 0x1f; /* ELD buffer size */
  6369. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6370. if (!eld[0])
  6371. return;
  6372. len = min_t(uint8_t, eld[2], len);
  6373. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6374. for (i = 0; i < len; i++)
  6375. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  6376. i = I915_READ(G4X_AUD_CNTL_ST);
  6377. i |= eldv;
  6378. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6379. }
  6380. static void haswell_write_eld(struct drm_connector *connector,
  6381. struct drm_crtc *crtc,
  6382. struct drm_display_mode *mode)
  6383. {
  6384. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6385. uint8_t *eld = connector->eld;
  6386. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6387. uint32_t eldv;
  6388. uint32_t i;
  6389. int len;
  6390. int pipe = to_intel_crtc(crtc)->pipe;
  6391. int tmp;
  6392. int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
  6393. int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
  6394. int aud_config = HSW_AUD_CFG(pipe);
  6395. int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
  6396. /* Audio output enable */
  6397. DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
  6398. tmp = I915_READ(aud_cntrl_st2);
  6399. tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
  6400. I915_WRITE(aud_cntrl_st2, tmp);
  6401. POSTING_READ(aud_cntrl_st2);
  6402. assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  6403. /* Set ELD valid state */
  6404. tmp = I915_READ(aud_cntrl_st2);
  6405. DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
  6406. tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
  6407. I915_WRITE(aud_cntrl_st2, tmp);
  6408. tmp = I915_READ(aud_cntrl_st2);
  6409. DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
  6410. /* Enable HDMI mode */
  6411. tmp = I915_READ(aud_config);
  6412. DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
  6413. /* clear N_programing_enable and N_value_index */
  6414. tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
  6415. I915_WRITE(aud_config, tmp);
  6416. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6417. eldv = AUDIO_ELD_VALID_A << (pipe * 4);
  6418. intel_crtc->eld_vld = true;
  6419. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6420. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6421. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6422. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6423. } else {
  6424. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6425. }
  6426. if (intel_eld_uptodate(connector,
  6427. aud_cntrl_st2, eldv,
  6428. aud_cntl_st, IBX_ELD_ADDRESS,
  6429. hdmiw_hdmiedid))
  6430. return;
  6431. i = I915_READ(aud_cntrl_st2);
  6432. i &= ~eldv;
  6433. I915_WRITE(aud_cntrl_st2, i);
  6434. if (!eld[0])
  6435. return;
  6436. i = I915_READ(aud_cntl_st);
  6437. i &= ~IBX_ELD_ADDRESS;
  6438. I915_WRITE(aud_cntl_st, i);
  6439. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  6440. DRM_DEBUG_DRIVER("port num:%d\n", i);
  6441. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6442. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6443. for (i = 0; i < len; i++)
  6444. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6445. i = I915_READ(aud_cntrl_st2);
  6446. i |= eldv;
  6447. I915_WRITE(aud_cntrl_st2, i);
  6448. }
  6449. static void ironlake_write_eld(struct drm_connector *connector,
  6450. struct drm_crtc *crtc,
  6451. struct drm_display_mode *mode)
  6452. {
  6453. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6454. uint8_t *eld = connector->eld;
  6455. uint32_t eldv;
  6456. uint32_t i;
  6457. int len;
  6458. int hdmiw_hdmiedid;
  6459. int aud_config;
  6460. int aud_cntl_st;
  6461. int aud_cntrl_st2;
  6462. int pipe = to_intel_crtc(crtc)->pipe;
  6463. if (HAS_PCH_IBX(connector->dev)) {
  6464. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  6465. aud_config = IBX_AUD_CFG(pipe);
  6466. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  6467. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  6468. } else if (IS_VALLEYVIEW(connector->dev)) {
  6469. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  6470. aud_config = VLV_AUD_CFG(pipe);
  6471. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  6472. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  6473. } else {
  6474. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  6475. aud_config = CPT_AUD_CFG(pipe);
  6476. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  6477. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  6478. }
  6479. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6480. if (IS_VALLEYVIEW(connector->dev)) {
  6481. struct intel_encoder *intel_encoder;
  6482. struct intel_digital_port *intel_dig_port;
  6483. intel_encoder = intel_attached_encoder(connector);
  6484. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  6485. i = intel_dig_port->port;
  6486. } else {
  6487. i = I915_READ(aud_cntl_st);
  6488. i = (i >> 29) & DIP_PORT_SEL_MASK;
  6489. /* DIP_Port_Select, 0x1 = PortB */
  6490. }
  6491. if (!i) {
  6492. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  6493. /* operate blindly on all ports */
  6494. eldv = IBX_ELD_VALIDB;
  6495. eldv |= IBX_ELD_VALIDB << 4;
  6496. eldv |= IBX_ELD_VALIDB << 8;
  6497. } else {
  6498. DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
  6499. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  6500. }
  6501. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6502. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6503. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6504. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6505. } else {
  6506. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6507. }
  6508. if (intel_eld_uptodate(connector,
  6509. aud_cntrl_st2, eldv,
  6510. aud_cntl_st, IBX_ELD_ADDRESS,
  6511. hdmiw_hdmiedid))
  6512. return;
  6513. i = I915_READ(aud_cntrl_st2);
  6514. i &= ~eldv;
  6515. I915_WRITE(aud_cntrl_st2, i);
  6516. if (!eld[0])
  6517. return;
  6518. i = I915_READ(aud_cntl_st);
  6519. i &= ~IBX_ELD_ADDRESS;
  6520. I915_WRITE(aud_cntl_st, i);
  6521. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6522. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6523. for (i = 0; i < len; i++)
  6524. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6525. i = I915_READ(aud_cntrl_st2);
  6526. i |= eldv;
  6527. I915_WRITE(aud_cntrl_st2, i);
  6528. }
  6529. void intel_write_eld(struct drm_encoder *encoder,
  6530. struct drm_display_mode *mode)
  6531. {
  6532. struct drm_crtc *crtc = encoder->crtc;
  6533. struct drm_connector *connector;
  6534. struct drm_device *dev = encoder->dev;
  6535. struct drm_i915_private *dev_priv = dev->dev_private;
  6536. connector = drm_select_eld(encoder, mode);
  6537. if (!connector)
  6538. return;
  6539. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6540. connector->base.id,
  6541. drm_get_connector_name(connector),
  6542. connector->encoder->base.id,
  6543. drm_get_encoder_name(connector->encoder));
  6544. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  6545. if (dev_priv->display.write_eld)
  6546. dev_priv->display.write_eld(connector, crtc, mode);
  6547. }
  6548. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6549. {
  6550. struct drm_device *dev = crtc->dev;
  6551. struct drm_i915_private *dev_priv = dev->dev_private;
  6552. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6553. bool visible = base != 0;
  6554. u32 cntl;
  6555. if (intel_crtc->cursor_visible == visible)
  6556. return;
  6557. cntl = I915_READ(_CURACNTR);
  6558. if (visible) {
  6559. /* On these chipsets we can only modify the base whilst
  6560. * the cursor is disabled.
  6561. */
  6562. I915_WRITE(_CURABASE, base);
  6563. cntl &= ~(CURSOR_FORMAT_MASK);
  6564. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  6565. cntl |= CURSOR_ENABLE |
  6566. CURSOR_GAMMA_ENABLE |
  6567. CURSOR_FORMAT_ARGB;
  6568. } else
  6569. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  6570. I915_WRITE(_CURACNTR, cntl);
  6571. intel_crtc->cursor_visible = visible;
  6572. }
  6573. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6574. {
  6575. struct drm_device *dev = crtc->dev;
  6576. struct drm_i915_private *dev_priv = dev->dev_private;
  6577. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6578. int pipe = intel_crtc->pipe;
  6579. bool visible = base != 0;
  6580. if (intel_crtc->cursor_visible != visible) {
  6581. int16_t width = intel_crtc->cursor_width;
  6582. uint32_t cntl = I915_READ(CURCNTR(pipe));
  6583. if (base) {
  6584. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  6585. cntl |= MCURSOR_GAMMA_ENABLE;
  6586. switch (width) {
  6587. case 64:
  6588. cntl |= CURSOR_MODE_64_ARGB_AX;
  6589. break;
  6590. case 128:
  6591. cntl |= CURSOR_MODE_128_ARGB_AX;
  6592. break;
  6593. case 256:
  6594. cntl |= CURSOR_MODE_256_ARGB_AX;
  6595. break;
  6596. default:
  6597. WARN_ON(1);
  6598. return;
  6599. }
  6600. cntl |= pipe << 28; /* Connect to correct pipe */
  6601. } else {
  6602. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  6603. cntl |= CURSOR_MODE_DISABLE;
  6604. }
  6605. I915_WRITE(CURCNTR(pipe), cntl);
  6606. intel_crtc->cursor_visible = visible;
  6607. }
  6608. /* and commit changes on next vblank */
  6609. POSTING_READ(CURCNTR(pipe));
  6610. I915_WRITE(CURBASE(pipe), base);
  6611. POSTING_READ(CURBASE(pipe));
  6612. }
  6613. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  6614. {
  6615. struct drm_device *dev = crtc->dev;
  6616. struct drm_i915_private *dev_priv = dev->dev_private;
  6617. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6618. int pipe = intel_crtc->pipe;
  6619. bool visible = base != 0;
  6620. if (intel_crtc->cursor_visible != visible) {
  6621. int16_t width = intel_crtc->cursor_width;
  6622. uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
  6623. if (base) {
  6624. cntl &= ~CURSOR_MODE;
  6625. cntl |= MCURSOR_GAMMA_ENABLE;
  6626. switch (width) {
  6627. case 64:
  6628. cntl |= CURSOR_MODE_64_ARGB_AX;
  6629. break;
  6630. case 128:
  6631. cntl |= CURSOR_MODE_128_ARGB_AX;
  6632. break;
  6633. case 256:
  6634. cntl |= CURSOR_MODE_256_ARGB_AX;
  6635. break;
  6636. default:
  6637. WARN_ON(1);
  6638. return;
  6639. }
  6640. } else {
  6641. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  6642. cntl |= CURSOR_MODE_DISABLE;
  6643. }
  6644. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6645. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6646. cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
  6647. }
  6648. I915_WRITE(CURCNTR_IVB(pipe), cntl);
  6649. intel_crtc->cursor_visible = visible;
  6650. }
  6651. /* and commit changes on next vblank */
  6652. POSTING_READ(CURCNTR_IVB(pipe));
  6653. I915_WRITE(CURBASE_IVB(pipe), base);
  6654. POSTING_READ(CURBASE_IVB(pipe));
  6655. }
  6656. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6657. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6658. bool on)
  6659. {
  6660. struct drm_device *dev = crtc->dev;
  6661. struct drm_i915_private *dev_priv = dev->dev_private;
  6662. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6663. int pipe = intel_crtc->pipe;
  6664. int x = intel_crtc->cursor_x;
  6665. int y = intel_crtc->cursor_y;
  6666. u32 base = 0, pos = 0;
  6667. bool visible;
  6668. if (on)
  6669. base = intel_crtc->cursor_addr;
  6670. if (x >= intel_crtc->config.pipe_src_w)
  6671. base = 0;
  6672. if (y >= intel_crtc->config.pipe_src_h)
  6673. base = 0;
  6674. if (x < 0) {
  6675. if (x + intel_crtc->cursor_width <= 0)
  6676. base = 0;
  6677. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  6678. x = -x;
  6679. }
  6680. pos |= x << CURSOR_X_SHIFT;
  6681. if (y < 0) {
  6682. if (y + intel_crtc->cursor_height <= 0)
  6683. base = 0;
  6684. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  6685. y = -y;
  6686. }
  6687. pos |= y << CURSOR_Y_SHIFT;
  6688. visible = base != 0;
  6689. if (!visible && !intel_crtc->cursor_visible)
  6690. return;
  6691. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6692. I915_WRITE(CURPOS_IVB(pipe), pos);
  6693. ivb_update_cursor(crtc, base);
  6694. } else {
  6695. I915_WRITE(CURPOS(pipe), pos);
  6696. if (IS_845G(dev) || IS_I865G(dev))
  6697. i845_update_cursor(crtc, base);
  6698. else
  6699. i9xx_update_cursor(crtc, base);
  6700. }
  6701. }
  6702. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  6703. struct drm_file *file,
  6704. uint32_t handle,
  6705. uint32_t width, uint32_t height)
  6706. {
  6707. struct drm_device *dev = crtc->dev;
  6708. struct drm_i915_private *dev_priv = dev->dev_private;
  6709. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6710. struct drm_i915_gem_object *obj;
  6711. unsigned old_width;
  6712. uint32_t addr;
  6713. int ret;
  6714. /* if we want to turn off the cursor ignore width and height */
  6715. if (!handle) {
  6716. DRM_DEBUG_KMS("cursor off\n");
  6717. addr = 0;
  6718. obj = NULL;
  6719. mutex_lock(&dev->struct_mutex);
  6720. goto finish;
  6721. }
  6722. /* Check for which cursor types we support */
  6723. if (!((width == 64 && height == 64) ||
  6724. (width == 128 && height == 128 && !IS_GEN2(dev)) ||
  6725. (width == 256 && height == 256 && !IS_GEN2(dev)))) {
  6726. DRM_DEBUG("Cursor dimension not supported\n");
  6727. return -EINVAL;
  6728. }
  6729. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  6730. if (&obj->base == NULL)
  6731. return -ENOENT;
  6732. if (obj->base.size < width * height * 4) {
  6733. DRM_DEBUG_KMS("buffer is to small\n");
  6734. ret = -ENOMEM;
  6735. goto fail;
  6736. }
  6737. /* we only need to pin inside GTT if cursor is non-phy */
  6738. mutex_lock(&dev->struct_mutex);
  6739. if (!INTEL_INFO(dev)->cursor_needs_physical) {
  6740. unsigned alignment;
  6741. if (obj->tiling_mode) {
  6742. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  6743. ret = -EINVAL;
  6744. goto fail_locked;
  6745. }
  6746. /* Note that the w/a also requires 2 PTE of padding following
  6747. * the bo. We currently fill all unused PTE with the shadow
  6748. * page and so we should always have valid PTE following the
  6749. * cursor preventing the VT-d warning.
  6750. */
  6751. alignment = 0;
  6752. if (need_vtd_wa(dev))
  6753. alignment = 64*1024;
  6754. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  6755. if (ret) {
  6756. DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
  6757. goto fail_locked;
  6758. }
  6759. ret = i915_gem_object_put_fence(obj);
  6760. if (ret) {
  6761. DRM_DEBUG_KMS("failed to release fence for cursor");
  6762. goto fail_unpin;
  6763. }
  6764. addr = i915_gem_obj_ggtt_offset(obj);
  6765. } else {
  6766. int align = IS_I830(dev) ? 16 * 1024 : 256;
  6767. ret = i915_gem_attach_phys_object(dev, obj,
  6768. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  6769. align);
  6770. if (ret) {
  6771. DRM_DEBUG_KMS("failed to attach phys object\n");
  6772. goto fail_locked;
  6773. }
  6774. addr = obj->phys_obj->handle->busaddr;
  6775. }
  6776. if (IS_GEN2(dev))
  6777. I915_WRITE(CURSIZE, (height << 12) | width);
  6778. finish:
  6779. if (intel_crtc->cursor_bo) {
  6780. if (INTEL_INFO(dev)->cursor_needs_physical) {
  6781. if (intel_crtc->cursor_bo != obj)
  6782. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  6783. } else
  6784. i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
  6785. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  6786. }
  6787. mutex_unlock(&dev->struct_mutex);
  6788. old_width = intel_crtc->cursor_width;
  6789. intel_crtc->cursor_addr = addr;
  6790. intel_crtc->cursor_bo = obj;
  6791. intel_crtc->cursor_width = width;
  6792. intel_crtc->cursor_height = height;
  6793. if (intel_crtc->active) {
  6794. if (old_width != width)
  6795. intel_update_watermarks(crtc);
  6796. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6797. }
  6798. return 0;
  6799. fail_unpin:
  6800. i915_gem_object_unpin_from_display_plane(obj);
  6801. fail_locked:
  6802. mutex_unlock(&dev->struct_mutex);
  6803. fail:
  6804. drm_gem_object_unreference_unlocked(&obj->base);
  6805. return ret;
  6806. }
  6807. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  6808. {
  6809. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6810. intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
  6811. intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
  6812. if (intel_crtc->active)
  6813. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6814. return 0;
  6815. }
  6816. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  6817. u16 *blue, uint32_t start, uint32_t size)
  6818. {
  6819. int end = (start + size > 256) ? 256 : start + size, i;
  6820. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6821. for (i = start; i < end; i++) {
  6822. intel_crtc->lut_r[i] = red[i] >> 8;
  6823. intel_crtc->lut_g[i] = green[i] >> 8;
  6824. intel_crtc->lut_b[i] = blue[i] >> 8;
  6825. }
  6826. intel_crtc_load_lut(crtc);
  6827. }
  6828. /* VESA 640x480x72Hz mode to set on the pipe */
  6829. static struct drm_display_mode load_detect_mode = {
  6830. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  6831. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  6832. };
  6833. struct drm_framebuffer *
  6834. __intel_framebuffer_create(struct drm_device *dev,
  6835. struct drm_mode_fb_cmd2 *mode_cmd,
  6836. struct drm_i915_gem_object *obj)
  6837. {
  6838. struct intel_framebuffer *intel_fb;
  6839. int ret;
  6840. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6841. if (!intel_fb) {
  6842. drm_gem_object_unreference_unlocked(&obj->base);
  6843. return ERR_PTR(-ENOMEM);
  6844. }
  6845. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  6846. if (ret)
  6847. goto err;
  6848. return &intel_fb->base;
  6849. err:
  6850. drm_gem_object_unreference_unlocked(&obj->base);
  6851. kfree(intel_fb);
  6852. return ERR_PTR(ret);
  6853. }
  6854. static struct drm_framebuffer *
  6855. intel_framebuffer_create(struct drm_device *dev,
  6856. struct drm_mode_fb_cmd2 *mode_cmd,
  6857. struct drm_i915_gem_object *obj)
  6858. {
  6859. struct drm_framebuffer *fb;
  6860. int ret;
  6861. ret = i915_mutex_lock_interruptible(dev);
  6862. if (ret)
  6863. return ERR_PTR(ret);
  6864. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  6865. mutex_unlock(&dev->struct_mutex);
  6866. return fb;
  6867. }
  6868. static u32
  6869. intel_framebuffer_pitch_for_width(int width, int bpp)
  6870. {
  6871. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  6872. return ALIGN(pitch, 64);
  6873. }
  6874. static u32
  6875. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  6876. {
  6877. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  6878. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  6879. }
  6880. static struct drm_framebuffer *
  6881. intel_framebuffer_create_for_mode(struct drm_device *dev,
  6882. struct drm_display_mode *mode,
  6883. int depth, int bpp)
  6884. {
  6885. struct drm_i915_gem_object *obj;
  6886. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  6887. obj = i915_gem_alloc_object(dev,
  6888. intel_framebuffer_size_for_mode(mode, bpp));
  6889. if (obj == NULL)
  6890. return ERR_PTR(-ENOMEM);
  6891. mode_cmd.width = mode->hdisplay;
  6892. mode_cmd.height = mode->vdisplay;
  6893. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  6894. bpp);
  6895. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  6896. return intel_framebuffer_create(dev, &mode_cmd, obj);
  6897. }
  6898. static struct drm_framebuffer *
  6899. mode_fits_in_fbdev(struct drm_device *dev,
  6900. struct drm_display_mode *mode)
  6901. {
  6902. #ifdef CONFIG_DRM_I915_FBDEV
  6903. struct drm_i915_private *dev_priv = dev->dev_private;
  6904. struct drm_i915_gem_object *obj;
  6905. struct drm_framebuffer *fb;
  6906. if (!dev_priv->fbdev)
  6907. return NULL;
  6908. if (!dev_priv->fbdev->fb)
  6909. return NULL;
  6910. obj = dev_priv->fbdev->fb->obj;
  6911. BUG_ON(!obj);
  6912. fb = &dev_priv->fbdev->fb->base;
  6913. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  6914. fb->bits_per_pixel))
  6915. return NULL;
  6916. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  6917. return NULL;
  6918. return fb;
  6919. #else
  6920. return NULL;
  6921. #endif
  6922. }
  6923. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  6924. struct drm_display_mode *mode,
  6925. struct intel_load_detect_pipe *old)
  6926. {
  6927. struct intel_crtc *intel_crtc;
  6928. struct intel_encoder *intel_encoder =
  6929. intel_attached_encoder(connector);
  6930. struct drm_crtc *possible_crtc;
  6931. struct drm_encoder *encoder = &intel_encoder->base;
  6932. struct drm_crtc *crtc = NULL;
  6933. struct drm_device *dev = encoder->dev;
  6934. struct drm_framebuffer *fb;
  6935. int i = -1;
  6936. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6937. connector->base.id, drm_get_connector_name(connector),
  6938. encoder->base.id, drm_get_encoder_name(encoder));
  6939. /*
  6940. * Algorithm gets a little messy:
  6941. *
  6942. * - if the connector already has an assigned crtc, use it (but make
  6943. * sure it's on first)
  6944. *
  6945. * - try to find the first unused crtc that can drive this connector,
  6946. * and use that if we find one
  6947. */
  6948. /* See if we already have a CRTC for this connector */
  6949. if (encoder->crtc) {
  6950. crtc = encoder->crtc;
  6951. mutex_lock(&crtc->mutex);
  6952. old->dpms_mode = connector->dpms;
  6953. old->load_detect_temp = false;
  6954. /* Make sure the crtc and connector are running */
  6955. if (connector->dpms != DRM_MODE_DPMS_ON)
  6956. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  6957. return true;
  6958. }
  6959. /* Find an unused one (if possible) */
  6960. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  6961. i++;
  6962. if (!(encoder->possible_crtcs & (1 << i)))
  6963. continue;
  6964. if (!possible_crtc->enabled) {
  6965. crtc = possible_crtc;
  6966. break;
  6967. }
  6968. }
  6969. /*
  6970. * If we didn't find an unused CRTC, don't use any.
  6971. */
  6972. if (!crtc) {
  6973. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  6974. return false;
  6975. }
  6976. mutex_lock(&crtc->mutex);
  6977. intel_encoder->new_crtc = to_intel_crtc(crtc);
  6978. to_intel_connector(connector)->new_encoder = intel_encoder;
  6979. intel_crtc = to_intel_crtc(crtc);
  6980. intel_crtc->new_enabled = true;
  6981. intel_crtc->new_config = &intel_crtc->config;
  6982. old->dpms_mode = connector->dpms;
  6983. old->load_detect_temp = true;
  6984. old->release_fb = NULL;
  6985. if (!mode)
  6986. mode = &load_detect_mode;
  6987. /* We need a framebuffer large enough to accommodate all accesses
  6988. * that the plane may generate whilst we perform load detection.
  6989. * We can not rely on the fbcon either being present (we get called
  6990. * during its initialisation to detect all boot displays, or it may
  6991. * not even exist) or that it is large enough to satisfy the
  6992. * requested mode.
  6993. */
  6994. fb = mode_fits_in_fbdev(dev, mode);
  6995. if (fb == NULL) {
  6996. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  6997. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  6998. old->release_fb = fb;
  6999. } else
  7000. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7001. if (IS_ERR(fb)) {
  7002. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7003. goto fail;
  7004. }
  7005. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7006. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7007. if (old->release_fb)
  7008. old->release_fb->funcs->destroy(old->release_fb);
  7009. goto fail;
  7010. }
  7011. /* let the connector get through one full cycle before testing */
  7012. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7013. return true;
  7014. fail:
  7015. intel_crtc->new_enabled = crtc->enabled;
  7016. if (intel_crtc->new_enabled)
  7017. intel_crtc->new_config = &intel_crtc->config;
  7018. else
  7019. intel_crtc->new_config = NULL;
  7020. mutex_unlock(&crtc->mutex);
  7021. return false;
  7022. }
  7023. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7024. struct intel_load_detect_pipe *old)
  7025. {
  7026. struct intel_encoder *intel_encoder =
  7027. intel_attached_encoder(connector);
  7028. struct drm_encoder *encoder = &intel_encoder->base;
  7029. struct drm_crtc *crtc = encoder->crtc;
  7030. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7031. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7032. connector->base.id, drm_get_connector_name(connector),
  7033. encoder->base.id, drm_get_encoder_name(encoder));
  7034. if (old->load_detect_temp) {
  7035. to_intel_connector(connector)->new_encoder = NULL;
  7036. intel_encoder->new_crtc = NULL;
  7037. intel_crtc->new_enabled = false;
  7038. intel_crtc->new_config = NULL;
  7039. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7040. if (old->release_fb) {
  7041. drm_framebuffer_unregister_private(old->release_fb);
  7042. drm_framebuffer_unreference(old->release_fb);
  7043. }
  7044. mutex_unlock(&crtc->mutex);
  7045. return;
  7046. }
  7047. /* Switch crtc and encoder back off if necessary */
  7048. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7049. connector->funcs->dpms(connector, old->dpms_mode);
  7050. mutex_unlock(&crtc->mutex);
  7051. }
  7052. static int i9xx_pll_refclk(struct drm_device *dev,
  7053. const struct intel_crtc_config *pipe_config)
  7054. {
  7055. struct drm_i915_private *dev_priv = dev->dev_private;
  7056. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7057. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7058. return dev_priv->vbt.lvds_ssc_freq;
  7059. else if (HAS_PCH_SPLIT(dev))
  7060. return 120000;
  7061. else if (!IS_GEN2(dev))
  7062. return 96000;
  7063. else
  7064. return 48000;
  7065. }
  7066. /* Returns the clock of the currently programmed mode of the given pipe. */
  7067. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7068. struct intel_crtc_config *pipe_config)
  7069. {
  7070. struct drm_device *dev = crtc->base.dev;
  7071. struct drm_i915_private *dev_priv = dev->dev_private;
  7072. int pipe = pipe_config->cpu_transcoder;
  7073. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7074. u32 fp;
  7075. intel_clock_t clock;
  7076. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7077. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7078. fp = pipe_config->dpll_hw_state.fp0;
  7079. else
  7080. fp = pipe_config->dpll_hw_state.fp1;
  7081. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7082. if (IS_PINEVIEW(dev)) {
  7083. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7084. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7085. } else {
  7086. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7087. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7088. }
  7089. if (!IS_GEN2(dev)) {
  7090. if (IS_PINEVIEW(dev))
  7091. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7092. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7093. else
  7094. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7095. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7096. switch (dpll & DPLL_MODE_MASK) {
  7097. case DPLLB_MODE_DAC_SERIAL:
  7098. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7099. 5 : 10;
  7100. break;
  7101. case DPLLB_MODE_LVDS:
  7102. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7103. 7 : 14;
  7104. break;
  7105. default:
  7106. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7107. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7108. return;
  7109. }
  7110. if (IS_PINEVIEW(dev))
  7111. pineview_clock(refclk, &clock);
  7112. else
  7113. i9xx_clock(refclk, &clock);
  7114. } else {
  7115. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7116. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7117. if (is_lvds) {
  7118. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7119. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7120. if (lvds & LVDS_CLKB_POWER_UP)
  7121. clock.p2 = 7;
  7122. else
  7123. clock.p2 = 14;
  7124. } else {
  7125. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7126. clock.p1 = 2;
  7127. else {
  7128. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7129. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7130. }
  7131. if (dpll & PLL_P2_DIVIDE_BY_4)
  7132. clock.p2 = 4;
  7133. else
  7134. clock.p2 = 2;
  7135. }
  7136. i9xx_clock(refclk, &clock);
  7137. }
  7138. /*
  7139. * This value includes pixel_multiplier. We will use
  7140. * port_clock to compute adjusted_mode.crtc_clock in the
  7141. * encoder's get_config() function.
  7142. */
  7143. pipe_config->port_clock = clock.dot;
  7144. }
  7145. int intel_dotclock_calculate(int link_freq,
  7146. const struct intel_link_m_n *m_n)
  7147. {
  7148. /*
  7149. * The calculation for the data clock is:
  7150. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7151. * But we want to avoid losing precison if possible, so:
  7152. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7153. *
  7154. * and the link clock is simpler:
  7155. * link_clock = (m * link_clock) / n
  7156. */
  7157. if (!m_n->link_n)
  7158. return 0;
  7159. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7160. }
  7161. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7162. struct intel_crtc_config *pipe_config)
  7163. {
  7164. struct drm_device *dev = crtc->base.dev;
  7165. /* read out port_clock from the DPLL */
  7166. i9xx_crtc_clock_get(crtc, pipe_config);
  7167. /*
  7168. * This value does not include pixel_multiplier.
  7169. * We will check that port_clock and adjusted_mode.crtc_clock
  7170. * agree once we know their relationship in the encoder's
  7171. * get_config() function.
  7172. */
  7173. pipe_config->adjusted_mode.crtc_clock =
  7174. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7175. &pipe_config->fdi_m_n);
  7176. }
  7177. /** Returns the currently programmed mode of the given pipe. */
  7178. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7179. struct drm_crtc *crtc)
  7180. {
  7181. struct drm_i915_private *dev_priv = dev->dev_private;
  7182. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7183. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7184. struct drm_display_mode *mode;
  7185. struct intel_crtc_config pipe_config;
  7186. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7187. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7188. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7189. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7190. enum pipe pipe = intel_crtc->pipe;
  7191. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7192. if (!mode)
  7193. return NULL;
  7194. /*
  7195. * Construct a pipe_config sufficient for getting the clock info
  7196. * back out of crtc_clock_get.
  7197. *
  7198. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7199. * to use a real value here instead.
  7200. */
  7201. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7202. pipe_config.pixel_multiplier = 1;
  7203. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7204. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7205. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7206. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7207. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7208. mode->hdisplay = (htot & 0xffff) + 1;
  7209. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7210. mode->hsync_start = (hsync & 0xffff) + 1;
  7211. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7212. mode->vdisplay = (vtot & 0xffff) + 1;
  7213. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7214. mode->vsync_start = (vsync & 0xffff) + 1;
  7215. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7216. drm_mode_set_name(mode);
  7217. return mode;
  7218. }
  7219. static void intel_increase_pllclock(struct drm_crtc *crtc)
  7220. {
  7221. struct drm_device *dev = crtc->dev;
  7222. struct drm_i915_private *dev_priv = dev->dev_private;
  7223. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7224. int pipe = intel_crtc->pipe;
  7225. int dpll_reg = DPLL(pipe);
  7226. int dpll;
  7227. if (HAS_PCH_SPLIT(dev))
  7228. return;
  7229. if (!dev_priv->lvds_downclock_avail)
  7230. return;
  7231. dpll = I915_READ(dpll_reg);
  7232. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  7233. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  7234. assert_panel_unlocked(dev_priv, pipe);
  7235. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  7236. I915_WRITE(dpll_reg, dpll);
  7237. intel_wait_for_vblank(dev, pipe);
  7238. dpll = I915_READ(dpll_reg);
  7239. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  7240. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  7241. }
  7242. }
  7243. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7244. {
  7245. struct drm_device *dev = crtc->dev;
  7246. struct drm_i915_private *dev_priv = dev->dev_private;
  7247. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7248. if (HAS_PCH_SPLIT(dev))
  7249. return;
  7250. if (!dev_priv->lvds_downclock_avail)
  7251. return;
  7252. /*
  7253. * Since this is called by a timer, we should never get here in
  7254. * the manual case.
  7255. */
  7256. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7257. int pipe = intel_crtc->pipe;
  7258. int dpll_reg = DPLL(pipe);
  7259. int dpll;
  7260. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7261. assert_panel_unlocked(dev_priv, pipe);
  7262. dpll = I915_READ(dpll_reg);
  7263. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7264. I915_WRITE(dpll_reg, dpll);
  7265. intel_wait_for_vblank(dev, pipe);
  7266. dpll = I915_READ(dpll_reg);
  7267. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7268. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7269. }
  7270. }
  7271. void intel_mark_busy(struct drm_device *dev)
  7272. {
  7273. struct drm_i915_private *dev_priv = dev->dev_private;
  7274. if (dev_priv->mm.busy)
  7275. return;
  7276. intel_runtime_pm_get(dev_priv);
  7277. i915_update_gfx_val(dev_priv);
  7278. dev_priv->mm.busy = true;
  7279. }
  7280. void intel_mark_idle(struct drm_device *dev)
  7281. {
  7282. struct drm_i915_private *dev_priv = dev->dev_private;
  7283. struct drm_crtc *crtc;
  7284. if (!dev_priv->mm.busy)
  7285. return;
  7286. dev_priv->mm.busy = false;
  7287. if (!i915.powersave)
  7288. goto out;
  7289. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  7290. if (!crtc->primary->fb)
  7291. continue;
  7292. intel_decrease_pllclock(crtc);
  7293. }
  7294. if (INTEL_INFO(dev)->gen >= 6)
  7295. gen6_rps_idle(dev->dev_private);
  7296. out:
  7297. intel_runtime_pm_put(dev_priv);
  7298. }
  7299. void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
  7300. struct intel_ring_buffer *ring)
  7301. {
  7302. struct drm_device *dev = obj->base.dev;
  7303. struct drm_crtc *crtc;
  7304. if (!i915.powersave)
  7305. return;
  7306. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  7307. if (!crtc->primary->fb)
  7308. continue;
  7309. if (to_intel_framebuffer(crtc->primary->fb)->obj != obj)
  7310. continue;
  7311. intel_increase_pllclock(crtc);
  7312. if (ring && intel_fbc_enabled(dev))
  7313. ring->fbc_dirty = true;
  7314. }
  7315. }
  7316. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7317. {
  7318. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7319. struct drm_device *dev = crtc->dev;
  7320. struct intel_unpin_work *work;
  7321. unsigned long flags;
  7322. spin_lock_irqsave(&dev->event_lock, flags);
  7323. work = intel_crtc->unpin_work;
  7324. intel_crtc->unpin_work = NULL;
  7325. spin_unlock_irqrestore(&dev->event_lock, flags);
  7326. if (work) {
  7327. cancel_work_sync(&work->work);
  7328. kfree(work);
  7329. }
  7330. intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
  7331. drm_crtc_cleanup(crtc);
  7332. kfree(intel_crtc);
  7333. }
  7334. static void intel_unpin_work_fn(struct work_struct *__work)
  7335. {
  7336. struct intel_unpin_work *work =
  7337. container_of(__work, struct intel_unpin_work, work);
  7338. struct drm_device *dev = work->crtc->dev;
  7339. mutex_lock(&dev->struct_mutex);
  7340. intel_unpin_fb_obj(work->old_fb_obj);
  7341. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7342. drm_gem_object_unreference(&work->old_fb_obj->base);
  7343. intel_update_fbc(dev);
  7344. mutex_unlock(&dev->struct_mutex);
  7345. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7346. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7347. kfree(work);
  7348. }
  7349. static void do_intel_finish_page_flip(struct drm_device *dev,
  7350. struct drm_crtc *crtc)
  7351. {
  7352. struct drm_i915_private *dev_priv = dev->dev_private;
  7353. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7354. struct intel_unpin_work *work;
  7355. unsigned long flags;
  7356. /* Ignore early vblank irqs */
  7357. if (intel_crtc == NULL)
  7358. return;
  7359. spin_lock_irqsave(&dev->event_lock, flags);
  7360. work = intel_crtc->unpin_work;
  7361. /* Ensure we don't miss a work->pending update ... */
  7362. smp_rmb();
  7363. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7364. spin_unlock_irqrestore(&dev->event_lock, flags);
  7365. return;
  7366. }
  7367. /* and that the unpin work is consistent wrt ->pending. */
  7368. smp_rmb();
  7369. intel_crtc->unpin_work = NULL;
  7370. if (work->event)
  7371. drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
  7372. drm_vblank_put(dev, intel_crtc->pipe);
  7373. spin_unlock_irqrestore(&dev->event_lock, flags);
  7374. wake_up_all(&dev_priv->pending_flip_queue);
  7375. queue_work(dev_priv->wq, &work->work);
  7376. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  7377. }
  7378. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7379. {
  7380. struct drm_i915_private *dev_priv = dev->dev_private;
  7381. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7382. do_intel_finish_page_flip(dev, crtc);
  7383. }
  7384. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7385. {
  7386. struct drm_i915_private *dev_priv = dev->dev_private;
  7387. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7388. do_intel_finish_page_flip(dev, crtc);
  7389. }
  7390. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7391. {
  7392. struct drm_i915_private *dev_priv = dev->dev_private;
  7393. struct intel_crtc *intel_crtc =
  7394. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7395. unsigned long flags;
  7396. /* NB: An MMIO update of the plane base pointer will also
  7397. * generate a page-flip completion irq, i.e. every modeset
  7398. * is also accompanied by a spurious intel_prepare_page_flip().
  7399. */
  7400. spin_lock_irqsave(&dev->event_lock, flags);
  7401. if (intel_crtc->unpin_work)
  7402. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7403. spin_unlock_irqrestore(&dev->event_lock, flags);
  7404. }
  7405. inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7406. {
  7407. /* Ensure that the work item is consistent when activating it ... */
  7408. smp_wmb();
  7409. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7410. /* and that it is marked active as soon as the irq could fire. */
  7411. smp_wmb();
  7412. }
  7413. static int intel_gen2_queue_flip(struct drm_device *dev,
  7414. struct drm_crtc *crtc,
  7415. struct drm_framebuffer *fb,
  7416. struct drm_i915_gem_object *obj,
  7417. uint32_t flags)
  7418. {
  7419. struct drm_i915_private *dev_priv = dev->dev_private;
  7420. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7421. u32 flip_mask;
  7422. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  7423. int ret;
  7424. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  7425. if (ret)
  7426. goto err;
  7427. ret = intel_ring_begin(ring, 6);
  7428. if (ret)
  7429. goto err_unpin;
  7430. /* Can't queue multiple flips, so wait for the previous
  7431. * one to finish before executing the next.
  7432. */
  7433. if (intel_crtc->plane)
  7434. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7435. else
  7436. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7437. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7438. intel_ring_emit(ring, MI_NOOP);
  7439. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7440. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7441. intel_ring_emit(ring, fb->pitches[0]);
  7442. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  7443. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7444. intel_mark_page_flip_active(intel_crtc);
  7445. __intel_ring_advance(ring);
  7446. return 0;
  7447. err_unpin:
  7448. intel_unpin_fb_obj(obj);
  7449. err:
  7450. return ret;
  7451. }
  7452. static int intel_gen3_queue_flip(struct drm_device *dev,
  7453. struct drm_crtc *crtc,
  7454. struct drm_framebuffer *fb,
  7455. struct drm_i915_gem_object *obj,
  7456. uint32_t flags)
  7457. {
  7458. struct drm_i915_private *dev_priv = dev->dev_private;
  7459. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7460. u32 flip_mask;
  7461. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  7462. int ret;
  7463. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  7464. if (ret)
  7465. goto err;
  7466. ret = intel_ring_begin(ring, 6);
  7467. if (ret)
  7468. goto err_unpin;
  7469. if (intel_crtc->plane)
  7470. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7471. else
  7472. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7473. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7474. intel_ring_emit(ring, MI_NOOP);
  7475. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7476. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7477. intel_ring_emit(ring, fb->pitches[0]);
  7478. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  7479. intel_ring_emit(ring, MI_NOOP);
  7480. intel_mark_page_flip_active(intel_crtc);
  7481. __intel_ring_advance(ring);
  7482. return 0;
  7483. err_unpin:
  7484. intel_unpin_fb_obj(obj);
  7485. err:
  7486. return ret;
  7487. }
  7488. static int intel_gen4_queue_flip(struct drm_device *dev,
  7489. struct drm_crtc *crtc,
  7490. struct drm_framebuffer *fb,
  7491. struct drm_i915_gem_object *obj,
  7492. uint32_t flags)
  7493. {
  7494. struct drm_i915_private *dev_priv = dev->dev_private;
  7495. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7496. uint32_t pf, pipesrc;
  7497. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  7498. int ret;
  7499. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  7500. if (ret)
  7501. goto err;
  7502. ret = intel_ring_begin(ring, 4);
  7503. if (ret)
  7504. goto err_unpin;
  7505. /* i965+ uses the linear or tiled offsets from the
  7506. * Display Registers (which do not change across a page-flip)
  7507. * so we need only reprogram the base address.
  7508. */
  7509. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7510. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7511. intel_ring_emit(ring, fb->pitches[0]);
  7512. intel_ring_emit(ring,
  7513. (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
  7514. obj->tiling_mode);
  7515. /* XXX Enabling the panel-fitter across page-flip is so far
  7516. * untested on non-native modes, so ignore it for now.
  7517. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7518. */
  7519. pf = 0;
  7520. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7521. intel_ring_emit(ring, pf | pipesrc);
  7522. intel_mark_page_flip_active(intel_crtc);
  7523. __intel_ring_advance(ring);
  7524. return 0;
  7525. err_unpin:
  7526. intel_unpin_fb_obj(obj);
  7527. err:
  7528. return ret;
  7529. }
  7530. static int intel_gen6_queue_flip(struct drm_device *dev,
  7531. struct drm_crtc *crtc,
  7532. struct drm_framebuffer *fb,
  7533. struct drm_i915_gem_object *obj,
  7534. uint32_t flags)
  7535. {
  7536. struct drm_i915_private *dev_priv = dev->dev_private;
  7537. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7538. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  7539. uint32_t pf, pipesrc;
  7540. int ret;
  7541. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  7542. if (ret)
  7543. goto err;
  7544. ret = intel_ring_begin(ring, 4);
  7545. if (ret)
  7546. goto err_unpin;
  7547. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7548. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7549. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7550. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  7551. /* Contrary to the suggestions in the documentation,
  7552. * "Enable Panel Fitter" does not seem to be required when page
  7553. * flipping with a non-native mode, and worse causes a normal
  7554. * modeset to fail.
  7555. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7556. */
  7557. pf = 0;
  7558. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7559. intel_ring_emit(ring, pf | pipesrc);
  7560. intel_mark_page_flip_active(intel_crtc);
  7561. __intel_ring_advance(ring);
  7562. return 0;
  7563. err_unpin:
  7564. intel_unpin_fb_obj(obj);
  7565. err:
  7566. return ret;
  7567. }
  7568. static int intel_gen7_queue_flip(struct drm_device *dev,
  7569. struct drm_crtc *crtc,
  7570. struct drm_framebuffer *fb,
  7571. struct drm_i915_gem_object *obj,
  7572. uint32_t flags)
  7573. {
  7574. struct drm_i915_private *dev_priv = dev->dev_private;
  7575. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7576. struct intel_ring_buffer *ring;
  7577. uint32_t plane_bit = 0;
  7578. int len, ret;
  7579. ring = obj->ring;
  7580. if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
  7581. ring = &dev_priv->ring[BCS];
  7582. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  7583. if (ret)
  7584. goto err;
  7585. switch(intel_crtc->plane) {
  7586. case PLANE_A:
  7587. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7588. break;
  7589. case PLANE_B:
  7590. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7591. break;
  7592. case PLANE_C:
  7593. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7594. break;
  7595. default:
  7596. WARN_ONCE(1, "unknown plane in flip command\n");
  7597. ret = -ENODEV;
  7598. goto err_unpin;
  7599. }
  7600. len = 4;
  7601. if (ring->id == RCS) {
  7602. len += 6;
  7603. /*
  7604. * On Gen 8, SRM is now taking an extra dword to accommodate
  7605. * 48bits addresses, and we need a NOOP for the batch size to
  7606. * stay even.
  7607. */
  7608. if (IS_GEN8(dev))
  7609. len += 2;
  7610. }
  7611. /*
  7612. * BSpec MI_DISPLAY_FLIP for IVB:
  7613. * "The full packet must be contained within the same cache line."
  7614. *
  7615. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7616. * cacheline, if we ever start emitting more commands before
  7617. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7618. * then do the cacheline alignment, and finally emit the
  7619. * MI_DISPLAY_FLIP.
  7620. */
  7621. ret = intel_ring_cacheline_align(ring);
  7622. if (ret)
  7623. goto err_unpin;
  7624. ret = intel_ring_begin(ring, len);
  7625. if (ret)
  7626. goto err_unpin;
  7627. /* Unmask the flip-done completion message. Note that the bspec says that
  7628. * we should do this for both the BCS and RCS, and that we must not unmask
  7629. * more than one flip event at any time (or ensure that one flip message
  7630. * can be sent by waiting for flip-done prior to queueing new flips).
  7631. * Experimentation says that BCS works despite DERRMR masking all
  7632. * flip-done completion events and that unmasking all planes at once
  7633. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7634. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7635. */
  7636. if (ring->id == RCS) {
  7637. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7638. intel_ring_emit(ring, DERRMR);
  7639. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7640. DERRMR_PIPEB_PRI_FLIP_DONE |
  7641. DERRMR_PIPEC_PRI_FLIP_DONE));
  7642. if (IS_GEN8(dev))
  7643. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7644. MI_SRM_LRM_GLOBAL_GTT);
  7645. else
  7646. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7647. MI_SRM_LRM_GLOBAL_GTT);
  7648. intel_ring_emit(ring, DERRMR);
  7649. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7650. if (IS_GEN8(dev)) {
  7651. intel_ring_emit(ring, 0);
  7652. intel_ring_emit(ring, MI_NOOP);
  7653. }
  7654. }
  7655. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7656. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7657. intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  7658. intel_ring_emit(ring, (MI_NOOP));
  7659. intel_mark_page_flip_active(intel_crtc);
  7660. __intel_ring_advance(ring);
  7661. return 0;
  7662. err_unpin:
  7663. intel_unpin_fb_obj(obj);
  7664. err:
  7665. return ret;
  7666. }
  7667. static int intel_default_queue_flip(struct drm_device *dev,
  7668. struct drm_crtc *crtc,
  7669. struct drm_framebuffer *fb,
  7670. struct drm_i915_gem_object *obj,
  7671. uint32_t flags)
  7672. {
  7673. return -ENODEV;
  7674. }
  7675. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  7676. struct drm_framebuffer *fb,
  7677. struct drm_pending_vblank_event *event,
  7678. uint32_t page_flip_flags)
  7679. {
  7680. struct drm_device *dev = crtc->dev;
  7681. struct drm_i915_private *dev_priv = dev->dev_private;
  7682. struct drm_framebuffer *old_fb = crtc->primary->fb;
  7683. struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
  7684. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7685. struct intel_unpin_work *work;
  7686. unsigned long flags;
  7687. int ret;
  7688. /* Can't change pixel format via MI display flips. */
  7689. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  7690. return -EINVAL;
  7691. /*
  7692. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  7693. * Note that pitch changes could also affect these register.
  7694. */
  7695. if (INTEL_INFO(dev)->gen > 3 &&
  7696. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  7697. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  7698. return -EINVAL;
  7699. if (i915_terminally_wedged(&dev_priv->gpu_error))
  7700. goto out_hang;
  7701. work = kzalloc(sizeof(*work), GFP_KERNEL);
  7702. if (work == NULL)
  7703. return -ENOMEM;
  7704. work->event = event;
  7705. work->crtc = crtc;
  7706. work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
  7707. INIT_WORK(&work->work, intel_unpin_work_fn);
  7708. ret = drm_vblank_get(dev, intel_crtc->pipe);
  7709. if (ret)
  7710. goto free_work;
  7711. /* We borrow the event spin lock for protecting unpin_work */
  7712. spin_lock_irqsave(&dev->event_lock, flags);
  7713. if (intel_crtc->unpin_work) {
  7714. spin_unlock_irqrestore(&dev->event_lock, flags);
  7715. kfree(work);
  7716. drm_vblank_put(dev, intel_crtc->pipe);
  7717. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  7718. return -EBUSY;
  7719. }
  7720. intel_crtc->unpin_work = work;
  7721. spin_unlock_irqrestore(&dev->event_lock, flags);
  7722. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  7723. flush_workqueue(dev_priv->wq);
  7724. ret = i915_mutex_lock_interruptible(dev);
  7725. if (ret)
  7726. goto cleanup;
  7727. /* Reference the objects for the scheduled work. */
  7728. drm_gem_object_reference(&work->old_fb_obj->base);
  7729. drm_gem_object_reference(&obj->base);
  7730. crtc->primary->fb = fb;
  7731. work->pending_flip_obj = obj;
  7732. work->enable_stall_check = true;
  7733. atomic_inc(&intel_crtc->unpin_work_count);
  7734. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  7735. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
  7736. if (ret)
  7737. goto cleanup_pending;
  7738. intel_disable_fbc(dev);
  7739. intel_mark_fb_busy(obj, NULL);
  7740. mutex_unlock(&dev->struct_mutex);
  7741. trace_i915_flip_request(intel_crtc->plane, obj);
  7742. return 0;
  7743. cleanup_pending:
  7744. atomic_dec(&intel_crtc->unpin_work_count);
  7745. crtc->primary->fb = old_fb;
  7746. drm_gem_object_unreference(&work->old_fb_obj->base);
  7747. drm_gem_object_unreference(&obj->base);
  7748. mutex_unlock(&dev->struct_mutex);
  7749. cleanup:
  7750. spin_lock_irqsave(&dev->event_lock, flags);
  7751. intel_crtc->unpin_work = NULL;
  7752. spin_unlock_irqrestore(&dev->event_lock, flags);
  7753. drm_vblank_put(dev, intel_crtc->pipe);
  7754. free_work:
  7755. kfree(work);
  7756. if (ret == -EIO) {
  7757. out_hang:
  7758. intel_crtc_wait_for_pending_flips(crtc);
  7759. ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
  7760. if (ret == 0 && event)
  7761. drm_send_vblank_event(dev, intel_crtc->pipe, event);
  7762. }
  7763. return ret;
  7764. }
  7765. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  7766. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  7767. .load_lut = intel_crtc_load_lut,
  7768. };
  7769. /**
  7770. * intel_modeset_update_staged_output_state
  7771. *
  7772. * Updates the staged output configuration state, e.g. after we've read out the
  7773. * current hw state.
  7774. */
  7775. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  7776. {
  7777. struct intel_crtc *crtc;
  7778. struct intel_encoder *encoder;
  7779. struct intel_connector *connector;
  7780. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7781. base.head) {
  7782. connector->new_encoder =
  7783. to_intel_encoder(connector->base.encoder);
  7784. }
  7785. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7786. base.head) {
  7787. encoder->new_crtc =
  7788. to_intel_crtc(encoder->base.crtc);
  7789. }
  7790. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  7791. base.head) {
  7792. crtc->new_enabled = crtc->base.enabled;
  7793. if (crtc->new_enabled)
  7794. crtc->new_config = &crtc->config;
  7795. else
  7796. crtc->new_config = NULL;
  7797. }
  7798. }
  7799. /**
  7800. * intel_modeset_commit_output_state
  7801. *
  7802. * This function copies the stage display pipe configuration to the real one.
  7803. */
  7804. static void intel_modeset_commit_output_state(struct drm_device *dev)
  7805. {
  7806. struct intel_crtc *crtc;
  7807. struct intel_encoder *encoder;
  7808. struct intel_connector *connector;
  7809. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7810. base.head) {
  7811. connector->base.encoder = &connector->new_encoder->base;
  7812. }
  7813. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7814. base.head) {
  7815. encoder->base.crtc = &encoder->new_crtc->base;
  7816. }
  7817. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  7818. base.head) {
  7819. crtc->base.enabled = crtc->new_enabled;
  7820. }
  7821. }
  7822. static void
  7823. connected_sink_compute_bpp(struct intel_connector * connector,
  7824. struct intel_crtc_config *pipe_config)
  7825. {
  7826. int bpp = pipe_config->pipe_bpp;
  7827. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  7828. connector->base.base.id,
  7829. drm_get_connector_name(&connector->base));
  7830. /* Don't use an invalid EDID bpc value */
  7831. if (connector->base.display_info.bpc &&
  7832. connector->base.display_info.bpc * 3 < bpp) {
  7833. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  7834. bpp, connector->base.display_info.bpc*3);
  7835. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  7836. }
  7837. /* Clamp bpp to 8 on screens without EDID 1.4 */
  7838. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  7839. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  7840. bpp);
  7841. pipe_config->pipe_bpp = 24;
  7842. }
  7843. }
  7844. static int
  7845. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  7846. struct drm_framebuffer *fb,
  7847. struct intel_crtc_config *pipe_config)
  7848. {
  7849. struct drm_device *dev = crtc->base.dev;
  7850. struct intel_connector *connector;
  7851. int bpp;
  7852. switch (fb->pixel_format) {
  7853. case DRM_FORMAT_C8:
  7854. bpp = 8*3; /* since we go through a colormap */
  7855. break;
  7856. case DRM_FORMAT_XRGB1555:
  7857. case DRM_FORMAT_ARGB1555:
  7858. /* checked in intel_framebuffer_init already */
  7859. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  7860. return -EINVAL;
  7861. case DRM_FORMAT_RGB565:
  7862. bpp = 6*3; /* min is 18bpp */
  7863. break;
  7864. case DRM_FORMAT_XBGR8888:
  7865. case DRM_FORMAT_ABGR8888:
  7866. /* checked in intel_framebuffer_init already */
  7867. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  7868. return -EINVAL;
  7869. case DRM_FORMAT_XRGB8888:
  7870. case DRM_FORMAT_ARGB8888:
  7871. bpp = 8*3;
  7872. break;
  7873. case DRM_FORMAT_XRGB2101010:
  7874. case DRM_FORMAT_ARGB2101010:
  7875. case DRM_FORMAT_XBGR2101010:
  7876. case DRM_FORMAT_ABGR2101010:
  7877. /* checked in intel_framebuffer_init already */
  7878. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  7879. return -EINVAL;
  7880. bpp = 10*3;
  7881. break;
  7882. /* TODO: gen4+ supports 16 bpc floating point, too. */
  7883. default:
  7884. DRM_DEBUG_KMS("unsupported depth\n");
  7885. return -EINVAL;
  7886. }
  7887. pipe_config->pipe_bpp = bpp;
  7888. /* Clamp display bpp to EDID value */
  7889. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7890. base.head) {
  7891. if (!connector->new_encoder ||
  7892. connector->new_encoder->new_crtc != crtc)
  7893. continue;
  7894. connected_sink_compute_bpp(connector, pipe_config);
  7895. }
  7896. return bpp;
  7897. }
  7898. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  7899. {
  7900. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  7901. "type: 0x%x flags: 0x%x\n",
  7902. mode->crtc_clock,
  7903. mode->crtc_hdisplay, mode->crtc_hsync_start,
  7904. mode->crtc_hsync_end, mode->crtc_htotal,
  7905. mode->crtc_vdisplay, mode->crtc_vsync_start,
  7906. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  7907. }
  7908. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  7909. struct intel_crtc_config *pipe_config,
  7910. const char *context)
  7911. {
  7912. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  7913. context, pipe_name(crtc->pipe));
  7914. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  7915. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  7916. pipe_config->pipe_bpp, pipe_config->dither);
  7917. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  7918. pipe_config->has_pch_encoder,
  7919. pipe_config->fdi_lanes,
  7920. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  7921. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  7922. pipe_config->fdi_m_n.tu);
  7923. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  7924. pipe_config->has_dp_encoder,
  7925. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  7926. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  7927. pipe_config->dp_m_n.tu);
  7928. DRM_DEBUG_KMS("requested mode:\n");
  7929. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  7930. DRM_DEBUG_KMS("adjusted mode:\n");
  7931. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  7932. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  7933. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  7934. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  7935. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  7936. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  7937. pipe_config->gmch_pfit.control,
  7938. pipe_config->gmch_pfit.pgm_ratios,
  7939. pipe_config->gmch_pfit.lvds_border_bits);
  7940. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  7941. pipe_config->pch_pfit.pos,
  7942. pipe_config->pch_pfit.size,
  7943. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  7944. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  7945. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  7946. }
  7947. static bool encoders_cloneable(const struct intel_encoder *a,
  7948. const struct intel_encoder *b)
  7949. {
  7950. /* masks could be asymmetric, so check both ways */
  7951. return a == b || (a->cloneable & (1 << b->type) &&
  7952. b->cloneable & (1 << a->type));
  7953. }
  7954. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  7955. struct intel_encoder *encoder)
  7956. {
  7957. struct drm_device *dev = crtc->base.dev;
  7958. struct intel_encoder *source_encoder;
  7959. list_for_each_entry(source_encoder,
  7960. &dev->mode_config.encoder_list, base.head) {
  7961. if (source_encoder->new_crtc != crtc)
  7962. continue;
  7963. if (!encoders_cloneable(encoder, source_encoder))
  7964. return false;
  7965. }
  7966. return true;
  7967. }
  7968. static bool check_encoder_cloning(struct intel_crtc *crtc)
  7969. {
  7970. struct drm_device *dev = crtc->base.dev;
  7971. struct intel_encoder *encoder;
  7972. list_for_each_entry(encoder,
  7973. &dev->mode_config.encoder_list, base.head) {
  7974. if (encoder->new_crtc != crtc)
  7975. continue;
  7976. if (!check_single_encoder_cloning(crtc, encoder))
  7977. return false;
  7978. }
  7979. return true;
  7980. }
  7981. static struct intel_crtc_config *
  7982. intel_modeset_pipe_config(struct drm_crtc *crtc,
  7983. struct drm_framebuffer *fb,
  7984. struct drm_display_mode *mode)
  7985. {
  7986. struct drm_device *dev = crtc->dev;
  7987. struct intel_encoder *encoder;
  7988. struct intel_crtc_config *pipe_config;
  7989. int plane_bpp, ret = -EINVAL;
  7990. bool retry = true;
  7991. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  7992. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  7993. return ERR_PTR(-EINVAL);
  7994. }
  7995. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  7996. if (!pipe_config)
  7997. return ERR_PTR(-ENOMEM);
  7998. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  7999. drm_mode_copy(&pipe_config->requested_mode, mode);
  8000. pipe_config->cpu_transcoder =
  8001. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8002. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8003. /*
  8004. * Sanitize sync polarity flags based on requested ones. If neither
  8005. * positive or negative polarity is requested, treat this as meaning
  8006. * negative polarity.
  8007. */
  8008. if (!(pipe_config->adjusted_mode.flags &
  8009. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8010. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8011. if (!(pipe_config->adjusted_mode.flags &
  8012. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8013. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8014. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8015. * plane pixel format and any sink constraints into account. Returns the
  8016. * source plane bpp so that dithering can be selected on mismatches
  8017. * after encoders and crtc also have had their say. */
  8018. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8019. fb, pipe_config);
  8020. if (plane_bpp < 0)
  8021. goto fail;
  8022. /*
  8023. * Determine the real pipe dimensions. Note that stereo modes can
  8024. * increase the actual pipe size due to the frame doubling and
  8025. * insertion of additional space for blanks between the frame. This
  8026. * is stored in the crtc timings. We use the requested mode to do this
  8027. * computation to clearly distinguish it from the adjusted mode, which
  8028. * can be changed by the connectors in the below retry loop.
  8029. */
  8030. drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
  8031. pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
  8032. pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
  8033. encoder_retry:
  8034. /* Ensure the port clock defaults are reset when retrying. */
  8035. pipe_config->port_clock = 0;
  8036. pipe_config->pixel_multiplier = 1;
  8037. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8038. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  8039. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8040. * adjust it according to limitations or connector properties, and also
  8041. * a chance to reject the mode entirely.
  8042. */
  8043. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8044. base.head) {
  8045. if (&encoder->new_crtc->base != crtc)
  8046. continue;
  8047. if (!(encoder->compute_config(encoder, pipe_config))) {
  8048. DRM_DEBUG_KMS("Encoder config failure\n");
  8049. goto fail;
  8050. }
  8051. }
  8052. /* Set default port clock if not overwritten by the encoder. Needs to be
  8053. * done afterwards in case the encoder adjusts the mode. */
  8054. if (!pipe_config->port_clock)
  8055. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  8056. * pipe_config->pixel_multiplier;
  8057. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8058. if (ret < 0) {
  8059. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8060. goto fail;
  8061. }
  8062. if (ret == RETRY) {
  8063. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8064. ret = -EINVAL;
  8065. goto fail;
  8066. }
  8067. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8068. retry = false;
  8069. goto encoder_retry;
  8070. }
  8071. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8072. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8073. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8074. return pipe_config;
  8075. fail:
  8076. kfree(pipe_config);
  8077. return ERR_PTR(ret);
  8078. }
  8079. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8080. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8081. static void
  8082. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8083. unsigned *prepare_pipes, unsigned *disable_pipes)
  8084. {
  8085. struct intel_crtc *intel_crtc;
  8086. struct drm_device *dev = crtc->dev;
  8087. struct intel_encoder *encoder;
  8088. struct intel_connector *connector;
  8089. struct drm_crtc *tmp_crtc;
  8090. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8091. /* Check which crtcs have changed outputs connected to them, these need
  8092. * to be part of the prepare_pipes mask. We don't (yet) support global
  8093. * modeset across multiple crtcs, so modeset_pipes will only have one
  8094. * bit set at most. */
  8095. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8096. base.head) {
  8097. if (connector->base.encoder == &connector->new_encoder->base)
  8098. continue;
  8099. if (connector->base.encoder) {
  8100. tmp_crtc = connector->base.encoder->crtc;
  8101. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8102. }
  8103. if (connector->new_encoder)
  8104. *prepare_pipes |=
  8105. 1 << connector->new_encoder->new_crtc->pipe;
  8106. }
  8107. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8108. base.head) {
  8109. if (encoder->base.crtc == &encoder->new_crtc->base)
  8110. continue;
  8111. if (encoder->base.crtc) {
  8112. tmp_crtc = encoder->base.crtc;
  8113. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8114. }
  8115. if (encoder->new_crtc)
  8116. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8117. }
  8118. /* Check for pipes that will be enabled/disabled ... */
  8119. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  8120. base.head) {
  8121. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8122. continue;
  8123. if (!intel_crtc->new_enabled)
  8124. *disable_pipes |= 1 << intel_crtc->pipe;
  8125. else
  8126. *prepare_pipes |= 1 << intel_crtc->pipe;
  8127. }
  8128. /* set_mode is also used to update properties on life display pipes. */
  8129. intel_crtc = to_intel_crtc(crtc);
  8130. if (intel_crtc->new_enabled)
  8131. *prepare_pipes |= 1 << intel_crtc->pipe;
  8132. /*
  8133. * For simplicity do a full modeset on any pipe where the output routing
  8134. * changed. We could be more clever, but that would require us to be
  8135. * more careful with calling the relevant encoder->mode_set functions.
  8136. */
  8137. if (*prepare_pipes)
  8138. *modeset_pipes = *prepare_pipes;
  8139. /* ... and mask these out. */
  8140. *modeset_pipes &= ~(*disable_pipes);
  8141. *prepare_pipes &= ~(*disable_pipes);
  8142. /*
  8143. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8144. * obies this rule, but the modeset restore mode of
  8145. * intel_modeset_setup_hw_state does not.
  8146. */
  8147. *modeset_pipes &= 1 << intel_crtc->pipe;
  8148. *prepare_pipes &= 1 << intel_crtc->pipe;
  8149. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8150. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8151. }
  8152. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8153. {
  8154. struct drm_encoder *encoder;
  8155. struct drm_device *dev = crtc->dev;
  8156. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8157. if (encoder->crtc == crtc)
  8158. return true;
  8159. return false;
  8160. }
  8161. static void
  8162. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8163. {
  8164. struct intel_encoder *intel_encoder;
  8165. struct intel_crtc *intel_crtc;
  8166. struct drm_connector *connector;
  8167. list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
  8168. base.head) {
  8169. if (!intel_encoder->base.crtc)
  8170. continue;
  8171. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8172. if (prepare_pipes & (1 << intel_crtc->pipe))
  8173. intel_encoder->connectors_active = false;
  8174. }
  8175. intel_modeset_commit_output_state(dev);
  8176. /* Double check state. */
  8177. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  8178. base.head) {
  8179. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8180. WARN_ON(intel_crtc->new_config &&
  8181. intel_crtc->new_config != &intel_crtc->config);
  8182. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8183. }
  8184. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8185. if (!connector->encoder || !connector->encoder->crtc)
  8186. continue;
  8187. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8188. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8189. struct drm_property *dpms_property =
  8190. dev->mode_config.dpms_property;
  8191. connector->dpms = DRM_MODE_DPMS_ON;
  8192. drm_object_property_set_value(&connector->base,
  8193. dpms_property,
  8194. DRM_MODE_DPMS_ON);
  8195. intel_encoder = to_intel_encoder(connector->encoder);
  8196. intel_encoder->connectors_active = true;
  8197. }
  8198. }
  8199. }
  8200. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8201. {
  8202. int diff;
  8203. if (clock1 == clock2)
  8204. return true;
  8205. if (!clock1 || !clock2)
  8206. return false;
  8207. diff = abs(clock1 - clock2);
  8208. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8209. return true;
  8210. return false;
  8211. }
  8212. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8213. list_for_each_entry((intel_crtc), \
  8214. &(dev)->mode_config.crtc_list, \
  8215. base.head) \
  8216. if (mask & (1 <<(intel_crtc)->pipe))
  8217. static bool
  8218. intel_pipe_config_compare(struct drm_device *dev,
  8219. struct intel_crtc_config *current_config,
  8220. struct intel_crtc_config *pipe_config)
  8221. {
  8222. #define PIPE_CONF_CHECK_X(name) \
  8223. if (current_config->name != pipe_config->name) { \
  8224. DRM_ERROR("mismatch in " #name " " \
  8225. "(expected 0x%08x, found 0x%08x)\n", \
  8226. current_config->name, \
  8227. pipe_config->name); \
  8228. return false; \
  8229. }
  8230. #define PIPE_CONF_CHECK_I(name) \
  8231. if (current_config->name != pipe_config->name) { \
  8232. DRM_ERROR("mismatch in " #name " " \
  8233. "(expected %i, found %i)\n", \
  8234. current_config->name, \
  8235. pipe_config->name); \
  8236. return false; \
  8237. }
  8238. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8239. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8240. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8241. "(expected %i, found %i)\n", \
  8242. current_config->name & (mask), \
  8243. pipe_config->name & (mask)); \
  8244. return false; \
  8245. }
  8246. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8247. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8248. DRM_ERROR("mismatch in " #name " " \
  8249. "(expected %i, found %i)\n", \
  8250. current_config->name, \
  8251. pipe_config->name); \
  8252. return false; \
  8253. }
  8254. #define PIPE_CONF_QUIRK(quirk) \
  8255. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8256. PIPE_CONF_CHECK_I(cpu_transcoder);
  8257. PIPE_CONF_CHECK_I(has_pch_encoder);
  8258. PIPE_CONF_CHECK_I(fdi_lanes);
  8259. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8260. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8261. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8262. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8263. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8264. PIPE_CONF_CHECK_I(has_dp_encoder);
  8265. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8266. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8267. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8268. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8269. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8270. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  8271. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  8272. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  8273. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  8274. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  8275. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  8276. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  8277. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  8278. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  8279. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  8280. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  8281. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  8282. PIPE_CONF_CHECK_I(pixel_multiplier);
  8283. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8284. DRM_MODE_FLAG_INTERLACE);
  8285. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8286. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8287. DRM_MODE_FLAG_PHSYNC);
  8288. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8289. DRM_MODE_FLAG_NHSYNC);
  8290. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8291. DRM_MODE_FLAG_PVSYNC);
  8292. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8293. DRM_MODE_FLAG_NVSYNC);
  8294. }
  8295. PIPE_CONF_CHECK_I(pipe_src_w);
  8296. PIPE_CONF_CHECK_I(pipe_src_h);
  8297. /*
  8298. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8299. * screen. Since we don't yet re-compute the pipe config when moving
  8300. * just the lvds port away to another pipe the sw tracking won't match.
  8301. *
  8302. * Proper atomic modesets with recomputed global state will fix this.
  8303. * Until then just don't check gmch state for inherited modes.
  8304. */
  8305. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8306. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8307. /* pfit ratios are autocomputed by the hw on gen4+ */
  8308. if (INTEL_INFO(dev)->gen < 4)
  8309. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8310. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8311. }
  8312. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8313. if (current_config->pch_pfit.enabled) {
  8314. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8315. PIPE_CONF_CHECK_I(pch_pfit.size);
  8316. }
  8317. /* BDW+ don't expose a synchronous way to read the state */
  8318. if (IS_HASWELL(dev))
  8319. PIPE_CONF_CHECK_I(ips_enabled);
  8320. PIPE_CONF_CHECK_I(double_wide);
  8321. PIPE_CONF_CHECK_I(shared_dpll);
  8322. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8323. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8324. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8325. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8326. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8327. PIPE_CONF_CHECK_I(pipe_bpp);
  8328. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  8329. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8330. #undef PIPE_CONF_CHECK_X
  8331. #undef PIPE_CONF_CHECK_I
  8332. #undef PIPE_CONF_CHECK_FLAGS
  8333. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8334. #undef PIPE_CONF_QUIRK
  8335. return true;
  8336. }
  8337. static void
  8338. check_connector_state(struct drm_device *dev)
  8339. {
  8340. struct intel_connector *connector;
  8341. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8342. base.head) {
  8343. /* This also checks the encoder/connector hw state with the
  8344. * ->get_hw_state callbacks. */
  8345. intel_connector_check_state(connector);
  8346. WARN(&connector->new_encoder->base != connector->base.encoder,
  8347. "connector's staged encoder doesn't match current encoder\n");
  8348. }
  8349. }
  8350. static void
  8351. check_encoder_state(struct drm_device *dev)
  8352. {
  8353. struct intel_encoder *encoder;
  8354. struct intel_connector *connector;
  8355. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8356. base.head) {
  8357. bool enabled = false;
  8358. bool active = false;
  8359. enum pipe pipe, tracked_pipe;
  8360. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8361. encoder->base.base.id,
  8362. drm_get_encoder_name(&encoder->base));
  8363. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  8364. "encoder's stage crtc doesn't match current crtc\n");
  8365. WARN(encoder->connectors_active && !encoder->base.crtc,
  8366. "encoder's active_connectors set, but no crtc\n");
  8367. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8368. base.head) {
  8369. if (connector->base.encoder != &encoder->base)
  8370. continue;
  8371. enabled = true;
  8372. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  8373. active = true;
  8374. }
  8375. WARN(!!encoder->base.crtc != enabled,
  8376. "encoder's enabled state mismatch "
  8377. "(expected %i, found %i)\n",
  8378. !!encoder->base.crtc, enabled);
  8379. WARN(active && !encoder->base.crtc,
  8380. "active encoder with no crtc\n");
  8381. WARN(encoder->connectors_active != active,
  8382. "encoder's computed active state doesn't match tracked active state "
  8383. "(expected %i, found %i)\n", active, encoder->connectors_active);
  8384. active = encoder->get_hw_state(encoder, &pipe);
  8385. WARN(active != encoder->connectors_active,
  8386. "encoder's hw state doesn't match sw tracking "
  8387. "(expected %i, found %i)\n",
  8388. encoder->connectors_active, active);
  8389. if (!encoder->base.crtc)
  8390. continue;
  8391. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  8392. WARN(active && pipe != tracked_pipe,
  8393. "active encoder's pipe doesn't match"
  8394. "(expected %i, found %i)\n",
  8395. tracked_pipe, pipe);
  8396. }
  8397. }
  8398. static void
  8399. check_crtc_state(struct drm_device *dev)
  8400. {
  8401. struct drm_i915_private *dev_priv = dev->dev_private;
  8402. struct intel_crtc *crtc;
  8403. struct intel_encoder *encoder;
  8404. struct intel_crtc_config pipe_config;
  8405. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  8406. base.head) {
  8407. bool enabled = false;
  8408. bool active = false;
  8409. memset(&pipe_config, 0, sizeof(pipe_config));
  8410. DRM_DEBUG_KMS("[CRTC:%d]\n",
  8411. crtc->base.base.id);
  8412. WARN(crtc->active && !crtc->base.enabled,
  8413. "active crtc, but not enabled in sw tracking\n");
  8414. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8415. base.head) {
  8416. if (encoder->base.crtc != &crtc->base)
  8417. continue;
  8418. enabled = true;
  8419. if (encoder->connectors_active)
  8420. active = true;
  8421. }
  8422. WARN(active != crtc->active,
  8423. "crtc's computed active state doesn't match tracked active state "
  8424. "(expected %i, found %i)\n", active, crtc->active);
  8425. WARN(enabled != crtc->base.enabled,
  8426. "crtc's computed enabled state doesn't match tracked enabled state "
  8427. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  8428. active = dev_priv->display.get_pipe_config(crtc,
  8429. &pipe_config);
  8430. /* hw state is inconsistent with the pipe A quirk */
  8431. if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  8432. active = crtc->active;
  8433. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8434. base.head) {
  8435. enum pipe pipe;
  8436. if (encoder->base.crtc != &crtc->base)
  8437. continue;
  8438. if (encoder->get_hw_state(encoder, &pipe))
  8439. encoder->get_config(encoder, &pipe_config);
  8440. }
  8441. WARN(crtc->active != active,
  8442. "crtc active state doesn't match with hw state "
  8443. "(expected %i, found %i)\n", crtc->active, active);
  8444. if (active &&
  8445. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  8446. WARN(1, "pipe state doesn't match!\n");
  8447. intel_dump_pipe_config(crtc, &pipe_config,
  8448. "[hw state]");
  8449. intel_dump_pipe_config(crtc, &crtc->config,
  8450. "[sw state]");
  8451. }
  8452. }
  8453. }
  8454. static void
  8455. check_shared_dpll_state(struct drm_device *dev)
  8456. {
  8457. struct drm_i915_private *dev_priv = dev->dev_private;
  8458. struct intel_crtc *crtc;
  8459. struct intel_dpll_hw_state dpll_hw_state;
  8460. int i;
  8461. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  8462. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  8463. int enabled_crtcs = 0, active_crtcs = 0;
  8464. bool active;
  8465. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  8466. DRM_DEBUG_KMS("%s\n", pll->name);
  8467. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  8468. WARN(pll->active > pll->refcount,
  8469. "more active pll users than references: %i vs %i\n",
  8470. pll->active, pll->refcount);
  8471. WARN(pll->active && !pll->on,
  8472. "pll in active use but not on in sw tracking\n");
  8473. WARN(pll->on && !pll->active,
  8474. "pll in on but not on in use in sw tracking\n");
  8475. WARN(pll->on != active,
  8476. "pll on state mismatch (expected %i, found %i)\n",
  8477. pll->on, active);
  8478. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  8479. base.head) {
  8480. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  8481. enabled_crtcs++;
  8482. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  8483. active_crtcs++;
  8484. }
  8485. WARN(pll->active != active_crtcs,
  8486. "pll active crtcs mismatch (expected %i, found %i)\n",
  8487. pll->active, active_crtcs);
  8488. WARN(pll->refcount != enabled_crtcs,
  8489. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  8490. pll->refcount, enabled_crtcs);
  8491. WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
  8492. sizeof(dpll_hw_state)),
  8493. "pll hw state mismatch\n");
  8494. }
  8495. }
  8496. void
  8497. intel_modeset_check_state(struct drm_device *dev)
  8498. {
  8499. check_connector_state(dev);
  8500. check_encoder_state(dev);
  8501. check_crtc_state(dev);
  8502. check_shared_dpll_state(dev);
  8503. }
  8504. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  8505. int dotclock)
  8506. {
  8507. /*
  8508. * FDI already provided one idea for the dotclock.
  8509. * Yell if the encoder disagrees.
  8510. */
  8511. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  8512. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  8513. pipe_config->adjusted_mode.crtc_clock, dotclock);
  8514. }
  8515. static int __intel_set_mode(struct drm_crtc *crtc,
  8516. struct drm_display_mode *mode,
  8517. int x, int y, struct drm_framebuffer *fb)
  8518. {
  8519. struct drm_device *dev = crtc->dev;
  8520. struct drm_i915_private *dev_priv = dev->dev_private;
  8521. struct drm_display_mode *saved_mode;
  8522. struct intel_crtc_config *pipe_config = NULL;
  8523. struct intel_crtc *intel_crtc;
  8524. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  8525. int ret = 0;
  8526. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  8527. if (!saved_mode)
  8528. return -ENOMEM;
  8529. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  8530. &prepare_pipes, &disable_pipes);
  8531. *saved_mode = crtc->mode;
  8532. /* Hack: Because we don't (yet) support global modeset on multiple
  8533. * crtcs, we don't keep track of the new mode for more than one crtc.
  8534. * Hence simply check whether any bit is set in modeset_pipes in all the
  8535. * pieces of code that are not yet converted to deal with mutliple crtcs
  8536. * changing their mode at the same time. */
  8537. if (modeset_pipes) {
  8538. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  8539. if (IS_ERR(pipe_config)) {
  8540. ret = PTR_ERR(pipe_config);
  8541. pipe_config = NULL;
  8542. goto out;
  8543. }
  8544. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  8545. "[modeset]");
  8546. to_intel_crtc(crtc)->new_config = pipe_config;
  8547. }
  8548. /*
  8549. * See if the config requires any additional preparation, e.g.
  8550. * to adjust global state with pipes off. We need to do this
  8551. * here so we can get the modeset_pipe updated config for the new
  8552. * mode set on this crtc. For other crtcs we need to use the
  8553. * adjusted_mode bits in the crtc directly.
  8554. */
  8555. if (IS_VALLEYVIEW(dev)) {
  8556. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  8557. /* may have added more to prepare_pipes than we should */
  8558. prepare_pipes &= ~disable_pipes;
  8559. }
  8560. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  8561. intel_crtc_disable(&intel_crtc->base);
  8562. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  8563. if (intel_crtc->base.enabled)
  8564. dev_priv->display.crtc_disable(&intel_crtc->base);
  8565. }
  8566. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  8567. * to set it here already despite that we pass it down the callchain.
  8568. */
  8569. if (modeset_pipes) {
  8570. crtc->mode = *mode;
  8571. /* mode_set/enable/disable functions rely on a correct pipe
  8572. * config. */
  8573. to_intel_crtc(crtc)->config = *pipe_config;
  8574. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  8575. /*
  8576. * Calculate and store various constants which
  8577. * are later needed by vblank and swap-completion
  8578. * timestamping. They are derived from true hwmode.
  8579. */
  8580. drm_calc_timestamping_constants(crtc,
  8581. &pipe_config->adjusted_mode);
  8582. }
  8583. /* Only after disabling all output pipelines that will be changed can we
  8584. * update the the output configuration. */
  8585. intel_modeset_update_state(dev, prepare_pipes);
  8586. if (dev_priv->display.modeset_global_resources)
  8587. dev_priv->display.modeset_global_resources(dev);
  8588. /* Set up the DPLL and any encoders state that needs to adjust or depend
  8589. * on the DPLL.
  8590. */
  8591. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  8592. ret = intel_crtc_mode_set(&intel_crtc->base,
  8593. x, y, fb);
  8594. if (ret)
  8595. goto done;
  8596. }
  8597. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  8598. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
  8599. dev_priv->display.crtc_enable(&intel_crtc->base);
  8600. /* FIXME: add subpixel order */
  8601. done:
  8602. if (ret && crtc->enabled)
  8603. crtc->mode = *saved_mode;
  8604. out:
  8605. kfree(pipe_config);
  8606. kfree(saved_mode);
  8607. return ret;
  8608. }
  8609. static int intel_set_mode(struct drm_crtc *crtc,
  8610. struct drm_display_mode *mode,
  8611. int x, int y, struct drm_framebuffer *fb)
  8612. {
  8613. int ret;
  8614. ret = __intel_set_mode(crtc, mode, x, y, fb);
  8615. if (ret == 0)
  8616. intel_modeset_check_state(crtc->dev);
  8617. return ret;
  8618. }
  8619. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  8620. {
  8621. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  8622. }
  8623. #undef for_each_intel_crtc_masked
  8624. static void intel_set_config_free(struct intel_set_config *config)
  8625. {
  8626. if (!config)
  8627. return;
  8628. kfree(config->save_connector_encoders);
  8629. kfree(config->save_encoder_crtcs);
  8630. kfree(config->save_crtc_enabled);
  8631. kfree(config);
  8632. }
  8633. static int intel_set_config_save_state(struct drm_device *dev,
  8634. struct intel_set_config *config)
  8635. {
  8636. struct drm_crtc *crtc;
  8637. struct drm_encoder *encoder;
  8638. struct drm_connector *connector;
  8639. int count;
  8640. config->save_crtc_enabled =
  8641. kcalloc(dev->mode_config.num_crtc,
  8642. sizeof(bool), GFP_KERNEL);
  8643. if (!config->save_crtc_enabled)
  8644. return -ENOMEM;
  8645. config->save_encoder_crtcs =
  8646. kcalloc(dev->mode_config.num_encoder,
  8647. sizeof(struct drm_crtc *), GFP_KERNEL);
  8648. if (!config->save_encoder_crtcs)
  8649. return -ENOMEM;
  8650. config->save_connector_encoders =
  8651. kcalloc(dev->mode_config.num_connector,
  8652. sizeof(struct drm_encoder *), GFP_KERNEL);
  8653. if (!config->save_connector_encoders)
  8654. return -ENOMEM;
  8655. /* Copy data. Note that driver private data is not affected.
  8656. * Should anything bad happen only the expected state is
  8657. * restored, not the drivers personal bookkeeping.
  8658. */
  8659. count = 0;
  8660. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  8661. config->save_crtc_enabled[count++] = crtc->enabled;
  8662. }
  8663. count = 0;
  8664. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  8665. config->save_encoder_crtcs[count++] = encoder->crtc;
  8666. }
  8667. count = 0;
  8668. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8669. config->save_connector_encoders[count++] = connector->encoder;
  8670. }
  8671. return 0;
  8672. }
  8673. static void intel_set_config_restore_state(struct drm_device *dev,
  8674. struct intel_set_config *config)
  8675. {
  8676. struct intel_crtc *crtc;
  8677. struct intel_encoder *encoder;
  8678. struct intel_connector *connector;
  8679. int count;
  8680. count = 0;
  8681. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
  8682. crtc->new_enabled = config->save_crtc_enabled[count++];
  8683. if (crtc->new_enabled)
  8684. crtc->new_config = &crtc->config;
  8685. else
  8686. crtc->new_config = NULL;
  8687. }
  8688. count = 0;
  8689. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  8690. encoder->new_crtc =
  8691. to_intel_crtc(config->save_encoder_crtcs[count++]);
  8692. }
  8693. count = 0;
  8694. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  8695. connector->new_encoder =
  8696. to_intel_encoder(config->save_connector_encoders[count++]);
  8697. }
  8698. }
  8699. static bool
  8700. is_crtc_connector_off(struct drm_mode_set *set)
  8701. {
  8702. int i;
  8703. if (set->num_connectors == 0)
  8704. return false;
  8705. if (WARN_ON(set->connectors == NULL))
  8706. return false;
  8707. for (i = 0; i < set->num_connectors; i++)
  8708. if (set->connectors[i]->encoder &&
  8709. set->connectors[i]->encoder->crtc == set->crtc &&
  8710. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  8711. return true;
  8712. return false;
  8713. }
  8714. static void
  8715. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  8716. struct intel_set_config *config)
  8717. {
  8718. /* We should be able to check here if the fb has the same properties
  8719. * and then just flip_or_move it */
  8720. if (is_crtc_connector_off(set)) {
  8721. config->mode_changed = true;
  8722. } else if (set->crtc->primary->fb != set->fb) {
  8723. /* If we have no fb then treat it as a full mode set */
  8724. if (set->crtc->primary->fb == NULL) {
  8725. struct intel_crtc *intel_crtc =
  8726. to_intel_crtc(set->crtc);
  8727. if (intel_crtc->active && i915.fastboot) {
  8728. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  8729. config->fb_changed = true;
  8730. } else {
  8731. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  8732. config->mode_changed = true;
  8733. }
  8734. } else if (set->fb == NULL) {
  8735. config->mode_changed = true;
  8736. } else if (set->fb->pixel_format !=
  8737. set->crtc->primary->fb->pixel_format) {
  8738. config->mode_changed = true;
  8739. } else {
  8740. config->fb_changed = true;
  8741. }
  8742. }
  8743. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  8744. config->fb_changed = true;
  8745. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  8746. DRM_DEBUG_KMS("modes are different, full mode set\n");
  8747. drm_mode_debug_printmodeline(&set->crtc->mode);
  8748. drm_mode_debug_printmodeline(set->mode);
  8749. config->mode_changed = true;
  8750. }
  8751. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  8752. set->crtc->base.id, config->mode_changed, config->fb_changed);
  8753. }
  8754. static int
  8755. intel_modeset_stage_output_state(struct drm_device *dev,
  8756. struct drm_mode_set *set,
  8757. struct intel_set_config *config)
  8758. {
  8759. struct intel_connector *connector;
  8760. struct intel_encoder *encoder;
  8761. struct intel_crtc *crtc;
  8762. int ro;
  8763. /* The upper layers ensure that we either disable a crtc or have a list
  8764. * of connectors. For paranoia, double-check this. */
  8765. WARN_ON(!set->fb && (set->num_connectors != 0));
  8766. WARN_ON(set->fb && (set->num_connectors == 0));
  8767. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8768. base.head) {
  8769. /* Otherwise traverse passed in connector list and get encoders
  8770. * for them. */
  8771. for (ro = 0; ro < set->num_connectors; ro++) {
  8772. if (set->connectors[ro] == &connector->base) {
  8773. connector->new_encoder = connector->encoder;
  8774. break;
  8775. }
  8776. }
  8777. /* If we disable the crtc, disable all its connectors. Also, if
  8778. * the connector is on the changing crtc but not on the new
  8779. * connector list, disable it. */
  8780. if ((!set->fb || ro == set->num_connectors) &&
  8781. connector->base.encoder &&
  8782. connector->base.encoder->crtc == set->crtc) {
  8783. connector->new_encoder = NULL;
  8784. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  8785. connector->base.base.id,
  8786. drm_get_connector_name(&connector->base));
  8787. }
  8788. if (&connector->new_encoder->base != connector->base.encoder) {
  8789. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  8790. config->mode_changed = true;
  8791. }
  8792. }
  8793. /* connector->new_encoder is now updated for all connectors. */
  8794. /* Update crtc of enabled connectors. */
  8795. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8796. base.head) {
  8797. struct drm_crtc *new_crtc;
  8798. if (!connector->new_encoder)
  8799. continue;
  8800. new_crtc = connector->new_encoder->base.crtc;
  8801. for (ro = 0; ro < set->num_connectors; ro++) {
  8802. if (set->connectors[ro] == &connector->base)
  8803. new_crtc = set->crtc;
  8804. }
  8805. /* Make sure the new CRTC will work with the encoder */
  8806. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  8807. new_crtc)) {
  8808. return -EINVAL;
  8809. }
  8810. connector->encoder->new_crtc = to_intel_crtc(new_crtc);
  8811. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  8812. connector->base.base.id,
  8813. drm_get_connector_name(&connector->base),
  8814. new_crtc->base.id);
  8815. }
  8816. /* Check for any encoders that needs to be disabled. */
  8817. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8818. base.head) {
  8819. int num_connectors = 0;
  8820. list_for_each_entry(connector,
  8821. &dev->mode_config.connector_list,
  8822. base.head) {
  8823. if (connector->new_encoder == encoder) {
  8824. WARN_ON(!connector->new_encoder->new_crtc);
  8825. num_connectors++;
  8826. }
  8827. }
  8828. if (num_connectors == 0)
  8829. encoder->new_crtc = NULL;
  8830. else if (num_connectors > 1)
  8831. return -EINVAL;
  8832. /* Only now check for crtc changes so we don't miss encoders
  8833. * that will be disabled. */
  8834. if (&encoder->new_crtc->base != encoder->base.crtc) {
  8835. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  8836. config->mode_changed = true;
  8837. }
  8838. }
  8839. /* Now we've also updated encoder->new_crtc for all encoders. */
  8840. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  8841. base.head) {
  8842. crtc->new_enabled = false;
  8843. list_for_each_entry(encoder,
  8844. &dev->mode_config.encoder_list,
  8845. base.head) {
  8846. if (encoder->new_crtc == crtc) {
  8847. crtc->new_enabled = true;
  8848. break;
  8849. }
  8850. }
  8851. if (crtc->new_enabled != crtc->base.enabled) {
  8852. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  8853. crtc->new_enabled ? "en" : "dis");
  8854. config->mode_changed = true;
  8855. }
  8856. if (crtc->new_enabled)
  8857. crtc->new_config = &crtc->config;
  8858. else
  8859. crtc->new_config = NULL;
  8860. }
  8861. return 0;
  8862. }
  8863. static void disable_crtc_nofb(struct intel_crtc *crtc)
  8864. {
  8865. struct drm_device *dev = crtc->base.dev;
  8866. struct intel_encoder *encoder;
  8867. struct intel_connector *connector;
  8868. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  8869. pipe_name(crtc->pipe));
  8870. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  8871. if (connector->new_encoder &&
  8872. connector->new_encoder->new_crtc == crtc)
  8873. connector->new_encoder = NULL;
  8874. }
  8875. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  8876. if (encoder->new_crtc == crtc)
  8877. encoder->new_crtc = NULL;
  8878. }
  8879. crtc->new_enabled = false;
  8880. crtc->new_config = NULL;
  8881. }
  8882. static int intel_crtc_set_config(struct drm_mode_set *set)
  8883. {
  8884. struct drm_device *dev;
  8885. struct drm_mode_set save_set;
  8886. struct intel_set_config *config;
  8887. int ret;
  8888. BUG_ON(!set);
  8889. BUG_ON(!set->crtc);
  8890. BUG_ON(!set->crtc->helper_private);
  8891. /* Enforce sane interface api - has been abused by the fb helper. */
  8892. BUG_ON(!set->mode && set->fb);
  8893. BUG_ON(set->fb && set->num_connectors == 0);
  8894. if (set->fb) {
  8895. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  8896. set->crtc->base.id, set->fb->base.id,
  8897. (int)set->num_connectors, set->x, set->y);
  8898. } else {
  8899. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  8900. }
  8901. dev = set->crtc->dev;
  8902. ret = -ENOMEM;
  8903. config = kzalloc(sizeof(*config), GFP_KERNEL);
  8904. if (!config)
  8905. goto out_config;
  8906. ret = intel_set_config_save_state(dev, config);
  8907. if (ret)
  8908. goto out_config;
  8909. save_set.crtc = set->crtc;
  8910. save_set.mode = &set->crtc->mode;
  8911. save_set.x = set->crtc->x;
  8912. save_set.y = set->crtc->y;
  8913. save_set.fb = set->crtc->primary->fb;
  8914. /* Compute whether we need a full modeset, only an fb base update or no
  8915. * change at all. In the future we might also check whether only the
  8916. * mode changed, e.g. for LVDS where we only change the panel fitter in
  8917. * such cases. */
  8918. intel_set_config_compute_mode_changes(set, config);
  8919. ret = intel_modeset_stage_output_state(dev, set, config);
  8920. if (ret)
  8921. goto fail;
  8922. if (config->mode_changed) {
  8923. ret = intel_set_mode(set->crtc, set->mode,
  8924. set->x, set->y, set->fb);
  8925. } else if (config->fb_changed) {
  8926. intel_crtc_wait_for_pending_flips(set->crtc);
  8927. ret = intel_pipe_set_base(set->crtc,
  8928. set->x, set->y, set->fb);
  8929. /*
  8930. * In the fastboot case this may be our only check of the
  8931. * state after boot. It would be better to only do it on
  8932. * the first update, but we don't have a nice way of doing that
  8933. * (and really, set_config isn't used much for high freq page
  8934. * flipping, so increasing its cost here shouldn't be a big
  8935. * deal).
  8936. */
  8937. if (i915.fastboot && ret == 0)
  8938. intel_modeset_check_state(set->crtc->dev);
  8939. }
  8940. if (ret) {
  8941. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  8942. set->crtc->base.id, ret);
  8943. fail:
  8944. intel_set_config_restore_state(dev, config);
  8945. /*
  8946. * HACK: if the pipe was on, but we didn't have a framebuffer,
  8947. * force the pipe off to avoid oopsing in the modeset code
  8948. * due to fb==NULL. This should only happen during boot since
  8949. * we don't yet reconstruct the FB from the hardware state.
  8950. */
  8951. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  8952. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  8953. /* Try to restore the config */
  8954. if (config->mode_changed &&
  8955. intel_set_mode(save_set.crtc, save_set.mode,
  8956. save_set.x, save_set.y, save_set.fb))
  8957. DRM_ERROR("failed to restore config after modeset failure\n");
  8958. }
  8959. out_config:
  8960. intel_set_config_free(config);
  8961. return ret;
  8962. }
  8963. static const struct drm_crtc_funcs intel_crtc_funcs = {
  8964. .cursor_set = intel_crtc_cursor_set,
  8965. .cursor_move = intel_crtc_cursor_move,
  8966. .gamma_set = intel_crtc_gamma_set,
  8967. .set_config = intel_crtc_set_config,
  8968. .destroy = intel_crtc_destroy,
  8969. .page_flip = intel_crtc_page_flip,
  8970. };
  8971. static void intel_cpu_pll_init(struct drm_device *dev)
  8972. {
  8973. if (HAS_DDI(dev))
  8974. intel_ddi_pll_init(dev);
  8975. }
  8976. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  8977. struct intel_shared_dpll *pll,
  8978. struct intel_dpll_hw_state *hw_state)
  8979. {
  8980. uint32_t val;
  8981. val = I915_READ(PCH_DPLL(pll->id));
  8982. hw_state->dpll = val;
  8983. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  8984. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  8985. return val & DPLL_VCO_ENABLE;
  8986. }
  8987. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  8988. struct intel_shared_dpll *pll)
  8989. {
  8990. I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
  8991. I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
  8992. }
  8993. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  8994. struct intel_shared_dpll *pll)
  8995. {
  8996. /* PCH refclock must be enabled first */
  8997. ibx_assert_pch_refclk_enabled(dev_priv);
  8998. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  8999. /* Wait for the clocks to stabilize. */
  9000. POSTING_READ(PCH_DPLL(pll->id));
  9001. udelay(150);
  9002. /* The pixel multiplier can only be updated once the
  9003. * DPLL is enabled and the clocks are stable.
  9004. *
  9005. * So write it again.
  9006. */
  9007. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9008. POSTING_READ(PCH_DPLL(pll->id));
  9009. udelay(200);
  9010. }
  9011. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9012. struct intel_shared_dpll *pll)
  9013. {
  9014. struct drm_device *dev = dev_priv->dev;
  9015. struct intel_crtc *crtc;
  9016. /* Make sure no transcoder isn't still depending on us. */
  9017. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
  9018. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9019. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9020. }
  9021. I915_WRITE(PCH_DPLL(pll->id), 0);
  9022. POSTING_READ(PCH_DPLL(pll->id));
  9023. udelay(200);
  9024. }
  9025. static char *ibx_pch_dpll_names[] = {
  9026. "PCH DPLL A",
  9027. "PCH DPLL B",
  9028. };
  9029. static void ibx_pch_dpll_init(struct drm_device *dev)
  9030. {
  9031. struct drm_i915_private *dev_priv = dev->dev_private;
  9032. int i;
  9033. dev_priv->num_shared_dpll = 2;
  9034. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9035. dev_priv->shared_dplls[i].id = i;
  9036. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9037. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9038. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9039. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9040. dev_priv->shared_dplls[i].get_hw_state =
  9041. ibx_pch_dpll_get_hw_state;
  9042. }
  9043. }
  9044. static void intel_shared_dpll_init(struct drm_device *dev)
  9045. {
  9046. struct drm_i915_private *dev_priv = dev->dev_private;
  9047. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9048. ibx_pch_dpll_init(dev);
  9049. else
  9050. dev_priv->num_shared_dpll = 0;
  9051. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9052. }
  9053. static void intel_crtc_init(struct drm_device *dev, int pipe)
  9054. {
  9055. struct drm_i915_private *dev_priv = dev->dev_private;
  9056. struct intel_crtc *intel_crtc;
  9057. int i;
  9058. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  9059. if (intel_crtc == NULL)
  9060. return;
  9061. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  9062. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  9063. for (i = 0; i < 256; i++) {
  9064. intel_crtc->lut_r[i] = i;
  9065. intel_crtc->lut_g[i] = i;
  9066. intel_crtc->lut_b[i] = i;
  9067. }
  9068. /*
  9069. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  9070. * is hooked to plane B. Hence we want plane A feeding pipe B.
  9071. */
  9072. intel_crtc->pipe = pipe;
  9073. intel_crtc->plane = pipe;
  9074. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  9075. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  9076. intel_crtc->plane = !pipe;
  9077. }
  9078. init_waitqueue_head(&intel_crtc->vbl_wait);
  9079. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  9080. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  9081. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  9082. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  9083. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  9084. }
  9085. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  9086. {
  9087. struct drm_encoder *encoder = connector->base.encoder;
  9088. WARN_ON(!mutex_is_locked(&connector->base.dev->mode_config.mutex));
  9089. if (!encoder)
  9090. return INVALID_PIPE;
  9091. return to_intel_crtc(encoder->crtc)->pipe;
  9092. }
  9093. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  9094. struct drm_file *file)
  9095. {
  9096. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  9097. struct drm_mode_object *drmmode_obj;
  9098. struct intel_crtc *crtc;
  9099. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  9100. return -ENODEV;
  9101. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  9102. DRM_MODE_OBJECT_CRTC);
  9103. if (!drmmode_obj) {
  9104. DRM_ERROR("no such CRTC id\n");
  9105. return -ENOENT;
  9106. }
  9107. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  9108. pipe_from_crtc_id->pipe = crtc->pipe;
  9109. return 0;
  9110. }
  9111. static int intel_encoder_clones(struct intel_encoder *encoder)
  9112. {
  9113. struct drm_device *dev = encoder->base.dev;
  9114. struct intel_encoder *source_encoder;
  9115. int index_mask = 0;
  9116. int entry = 0;
  9117. list_for_each_entry(source_encoder,
  9118. &dev->mode_config.encoder_list, base.head) {
  9119. if (encoders_cloneable(encoder, source_encoder))
  9120. index_mask |= (1 << entry);
  9121. entry++;
  9122. }
  9123. return index_mask;
  9124. }
  9125. static bool has_edp_a(struct drm_device *dev)
  9126. {
  9127. struct drm_i915_private *dev_priv = dev->dev_private;
  9128. if (!IS_MOBILE(dev))
  9129. return false;
  9130. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  9131. return false;
  9132. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  9133. return false;
  9134. return true;
  9135. }
  9136. const char *intel_output_name(int output)
  9137. {
  9138. static const char *names[] = {
  9139. [INTEL_OUTPUT_UNUSED] = "Unused",
  9140. [INTEL_OUTPUT_ANALOG] = "Analog",
  9141. [INTEL_OUTPUT_DVO] = "DVO",
  9142. [INTEL_OUTPUT_SDVO] = "SDVO",
  9143. [INTEL_OUTPUT_LVDS] = "LVDS",
  9144. [INTEL_OUTPUT_TVOUT] = "TV",
  9145. [INTEL_OUTPUT_HDMI] = "HDMI",
  9146. [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
  9147. [INTEL_OUTPUT_EDP] = "eDP",
  9148. [INTEL_OUTPUT_DSI] = "DSI",
  9149. [INTEL_OUTPUT_UNKNOWN] = "Unknown",
  9150. };
  9151. if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
  9152. return "Invalid";
  9153. return names[output];
  9154. }
  9155. static void intel_setup_outputs(struct drm_device *dev)
  9156. {
  9157. struct drm_i915_private *dev_priv = dev->dev_private;
  9158. struct intel_encoder *encoder;
  9159. bool dpd_is_edp = false;
  9160. intel_lvds_init(dev);
  9161. if (!IS_ULT(dev) && !IS_CHERRYVIEW(dev))
  9162. intel_crt_init(dev);
  9163. if (HAS_DDI(dev)) {
  9164. int found;
  9165. /* Haswell uses DDI functions to detect digital outputs */
  9166. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  9167. /* DDI A only supports eDP */
  9168. if (found)
  9169. intel_ddi_init(dev, PORT_A);
  9170. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  9171. * register */
  9172. found = I915_READ(SFUSE_STRAP);
  9173. if (found & SFUSE_STRAP_DDIB_DETECTED)
  9174. intel_ddi_init(dev, PORT_B);
  9175. if (found & SFUSE_STRAP_DDIC_DETECTED)
  9176. intel_ddi_init(dev, PORT_C);
  9177. if (found & SFUSE_STRAP_DDID_DETECTED)
  9178. intel_ddi_init(dev, PORT_D);
  9179. } else if (HAS_PCH_SPLIT(dev)) {
  9180. int found;
  9181. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  9182. if (has_edp_a(dev))
  9183. intel_dp_init(dev, DP_A, PORT_A);
  9184. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  9185. /* PCH SDVOB multiplex with HDMIB */
  9186. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  9187. if (!found)
  9188. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  9189. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  9190. intel_dp_init(dev, PCH_DP_B, PORT_B);
  9191. }
  9192. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  9193. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  9194. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  9195. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  9196. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  9197. intel_dp_init(dev, PCH_DP_C, PORT_C);
  9198. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  9199. intel_dp_init(dev, PCH_DP_D, PORT_D);
  9200. } else if (IS_VALLEYVIEW(dev)) {
  9201. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
  9202. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  9203. PORT_B);
  9204. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
  9205. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  9206. }
  9207. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
  9208. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  9209. PORT_C);
  9210. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
  9211. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  9212. }
  9213. intel_dsi_init(dev);
  9214. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  9215. bool found = false;
  9216. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  9217. DRM_DEBUG_KMS("probing SDVOB\n");
  9218. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  9219. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  9220. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  9221. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  9222. }
  9223. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  9224. intel_dp_init(dev, DP_B, PORT_B);
  9225. }
  9226. /* Before G4X SDVOC doesn't have its own detect register */
  9227. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  9228. DRM_DEBUG_KMS("probing SDVOC\n");
  9229. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  9230. }
  9231. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  9232. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  9233. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  9234. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  9235. }
  9236. if (SUPPORTS_INTEGRATED_DP(dev))
  9237. intel_dp_init(dev, DP_C, PORT_C);
  9238. }
  9239. if (SUPPORTS_INTEGRATED_DP(dev) &&
  9240. (I915_READ(DP_D) & DP_DETECTED))
  9241. intel_dp_init(dev, DP_D, PORT_D);
  9242. } else if (IS_GEN2(dev))
  9243. intel_dvo_init(dev);
  9244. if (SUPPORTS_TV(dev))
  9245. intel_tv_init(dev);
  9246. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  9247. encoder->base.possible_crtcs = encoder->crtc_mask;
  9248. encoder->base.possible_clones =
  9249. intel_encoder_clones(encoder);
  9250. }
  9251. intel_init_pch_refclk(dev);
  9252. drm_helper_move_panel_connectors_to_head(dev);
  9253. }
  9254. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  9255. {
  9256. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  9257. drm_framebuffer_cleanup(fb);
  9258. WARN_ON(!intel_fb->obj->framebuffer_references--);
  9259. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  9260. kfree(intel_fb);
  9261. }
  9262. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  9263. struct drm_file *file,
  9264. unsigned int *handle)
  9265. {
  9266. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  9267. struct drm_i915_gem_object *obj = intel_fb->obj;
  9268. return drm_gem_handle_create(file, &obj->base, handle);
  9269. }
  9270. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  9271. .destroy = intel_user_framebuffer_destroy,
  9272. .create_handle = intel_user_framebuffer_create_handle,
  9273. };
  9274. static int intel_framebuffer_init(struct drm_device *dev,
  9275. struct intel_framebuffer *intel_fb,
  9276. struct drm_mode_fb_cmd2 *mode_cmd,
  9277. struct drm_i915_gem_object *obj)
  9278. {
  9279. int aligned_height;
  9280. int pitch_limit;
  9281. int ret;
  9282. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  9283. if (obj->tiling_mode == I915_TILING_Y) {
  9284. DRM_DEBUG("hardware does not support tiling Y\n");
  9285. return -EINVAL;
  9286. }
  9287. if (mode_cmd->pitches[0] & 63) {
  9288. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  9289. mode_cmd->pitches[0]);
  9290. return -EINVAL;
  9291. }
  9292. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  9293. pitch_limit = 32*1024;
  9294. } else if (INTEL_INFO(dev)->gen >= 4) {
  9295. if (obj->tiling_mode)
  9296. pitch_limit = 16*1024;
  9297. else
  9298. pitch_limit = 32*1024;
  9299. } else if (INTEL_INFO(dev)->gen >= 3) {
  9300. if (obj->tiling_mode)
  9301. pitch_limit = 8*1024;
  9302. else
  9303. pitch_limit = 16*1024;
  9304. } else
  9305. /* XXX DSPC is limited to 4k tiled */
  9306. pitch_limit = 8*1024;
  9307. if (mode_cmd->pitches[0] > pitch_limit) {
  9308. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  9309. obj->tiling_mode ? "tiled" : "linear",
  9310. mode_cmd->pitches[0], pitch_limit);
  9311. return -EINVAL;
  9312. }
  9313. if (obj->tiling_mode != I915_TILING_NONE &&
  9314. mode_cmd->pitches[0] != obj->stride) {
  9315. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  9316. mode_cmd->pitches[0], obj->stride);
  9317. return -EINVAL;
  9318. }
  9319. /* Reject formats not supported by any plane early. */
  9320. switch (mode_cmd->pixel_format) {
  9321. case DRM_FORMAT_C8:
  9322. case DRM_FORMAT_RGB565:
  9323. case DRM_FORMAT_XRGB8888:
  9324. case DRM_FORMAT_ARGB8888:
  9325. break;
  9326. case DRM_FORMAT_XRGB1555:
  9327. case DRM_FORMAT_ARGB1555:
  9328. if (INTEL_INFO(dev)->gen > 3) {
  9329. DRM_DEBUG("unsupported pixel format: %s\n",
  9330. drm_get_format_name(mode_cmd->pixel_format));
  9331. return -EINVAL;
  9332. }
  9333. break;
  9334. case DRM_FORMAT_XBGR8888:
  9335. case DRM_FORMAT_ABGR8888:
  9336. case DRM_FORMAT_XRGB2101010:
  9337. case DRM_FORMAT_ARGB2101010:
  9338. case DRM_FORMAT_XBGR2101010:
  9339. case DRM_FORMAT_ABGR2101010:
  9340. if (INTEL_INFO(dev)->gen < 4) {
  9341. DRM_DEBUG("unsupported pixel format: %s\n",
  9342. drm_get_format_name(mode_cmd->pixel_format));
  9343. return -EINVAL;
  9344. }
  9345. break;
  9346. case DRM_FORMAT_YUYV:
  9347. case DRM_FORMAT_UYVY:
  9348. case DRM_FORMAT_YVYU:
  9349. case DRM_FORMAT_VYUY:
  9350. if (INTEL_INFO(dev)->gen < 5) {
  9351. DRM_DEBUG("unsupported pixel format: %s\n",
  9352. drm_get_format_name(mode_cmd->pixel_format));
  9353. return -EINVAL;
  9354. }
  9355. break;
  9356. default:
  9357. DRM_DEBUG("unsupported pixel format: %s\n",
  9358. drm_get_format_name(mode_cmd->pixel_format));
  9359. return -EINVAL;
  9360. }
  9361. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  9362. if (mode_cmd->offsets[0] != 0)
  9363. return -EINVAL;
  9364. aligned_height = intel_align_height(dev, mode_cmd->height,
  9365. obj->tiling_mode);
  9366. /* FIXME drm helper for size checks (especially planar formats)? */
  9367. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  9368. return -EINVAL;
  9369. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  9370. intel_fb->obj = obj;
  9371. intel_fb->obj->framebuffer_references++;
  9372. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  9373. if (ret) {
  9374. DRM_ERROR("framebuffer init failed %d\n", ret);
  9375. return ret;
  9376. }
  9377. return 0;
  9378. }
  9379. static struct drm_framebuffer *
  9380. intel_user_framebuffer_create(struct drm_device *dev,
  9381. struct drm_file *filp,
  9382. struct drm_mode_fb_cmd2 *mode_cmd)
  9383. {
  9384. struct drm_i915_gem_object *obj;
  9385. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  9386. mode_cmd->handles[0]));
  9387. if (&obj->base == NULL)
  9388. return ERR_PTR(-ENOENT);
  9389. return intel_framebuffer_create(dev, mode_cmd, obj);
  9390. }
  9391. #ifndef CONFIG_DRM_I915_FBDEV
  9392. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  9393. {
  9394. }
  9395. #endif
  9396. static const struct drm_mode_config_funcs intel_mode_funcs = {
  9397. .fb_create = intel_user_framebuffer_create,
  9398. .output_poll_changed = intel_fbdev_output_poll_changed,
  9399. };
  9400. /* Set up chip specific display functions */
  9401. static void intel_init_display(struct drm_device *dev)
  9402. {
  9403. struct drm_i915_private *dev_priv = dev->dev_private;
  9404. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  9405. dev_priv->display.find_dpll = g4x_find_best_dpll;
  9406. else if (IS_CHERRYVIEW(dev))
  9407. dev_priv->display.find_dpll = chv_find_best_dpll;
  9408. else if (IS_VALLEYVIEW(dev))
  9409. dev_priv->display.find_dpll = vlv_find_best_dpll;
  9410. else if (IS_PINEVIEW(dev))
  9411. dev_priv->display.find_dpll = pnv_find_best_dpll;
  9412. else
  9413. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  9414. if (HAS_DDI(dev)) {
  9415. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  9416. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  9417. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  9418. dev_priv->display.crtc_enable = haswell_crtc_enable;
  9419. dev_priv->display.crtc_disable = haswell_crtc_disable;
  9420. dev_priv->display.off = haswell_crtc_off;
  9421. dev_priv->display.update_primary_plane =
  9422. ironlake_update_primary_plane;
  9423. } else if (HAS_PCH_SPLIT(dev)) {
  9424. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  9425. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  9426. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  9427. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  9428. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  9429. dev_priv->display.off = ironlake_crtc_off;
  9430. dev_priv->display.update_primary_plane =
  9431. ironlake_update_primary_plane;
  9432. } else if (IS_VALLEYVIEW(dev)) {
  9433. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  9434. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  9435. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  9436. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  9437. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  9438. dev_priv->display.off = i9xx_crtc_off;
  9439. dev_priv->display.update_primary_plane =
  9440. i9xx_update_primary_plane;
  9441. } else {
  9442. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  9443. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  9444. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  9445. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  9446. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  9447. dev_priv->display.off = i9xx_crtc_off;
  9448. dev_priv->display.update_primary_plane =
  9449. i9xx_update_primary_plane;
  9450. }
  9451. /* Returns the core display clock speed */
  9452. if (IS_VALLEYVIEW(dev))
  9453. dev_priv->display.get_display_clock_speed =
  9454. valleyview_get_display_clock_speed;
  9455. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  9456. dev_priv->display.get_display_clock_speed =
  9457. i945_get_display_clock_speed;
  9458. else if (IS_I915G(dev))
  9459. dev_priv->display.get_display_clock_speed =
  9460. i915_get_display_clock_speed;
  9461. else if (IS_I945GM(dev) || IS_845G(dev))
  9462. dev_priv->display.get_display_clock_speed =
  9463. i9xx_misc_get_display_clock_speed;
  9464. else if (IS_PINEVIEW(dev))
  9465. dev_priv->display.get_display_clock_speed =
  9466. pnv_get_display_clock_speed;
  9467. else if (IS_I915GM(dev))
  9468. dev_priv->display.get_display_clock_speed =
  9469. i915gm_get_display_clock_speed;
  9470. else if (IS_I865G(dev))
  9471. dev_priv->display.get_display_clock_speed =
  9472. i865_get_display_clock_speed;
  9473. else if (IS_I85X(dev))
  9474. dev_priv->display.get_display_clock_speed =
  9475. i855_get_display_clock_speed;
  9476. else /* 852, 830 */
  9477. dev_priv->display.get_display_clock_speed =
  9478. i830_get_display_clock_speed;
  9479. if (HAS_PCH_SPLIT(dev)) {
  9480. if (IS_GEN5(dev)) {
  9481. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  9482. dev_priv->display.write_eld = ironlake_write_eld;
  9483. } else if (IS_GEN6(dev)) {
  9484. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  9485. dev_priv->display.write_eld = ironlake_write_eld;
  9486. dev_priv->display.modeset_global_resources =
  9487. snb_modeset_global_resources;
  9488. } else if (IS_IVYBRIDGE(dev)) {
  9489. /* FIXME: detect B0+ stepping and use auto training */
  9490. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  9491. dev_priv->display.write_eld = ironlake_write_eld;
  9492. dev_priv->display.modeset_global_resources =
  9493. ivb_modeset_global_resources;
  9494. } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
  9495. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  9496. dev_priv->display.write_eld = haswell_write_eld;
  9497. dev_priv->display.modeset_global_resources =
  9498. haswell_modeset_global_resources;
  9499. }
  9500. } else if (IS_G4X(dev)) {
  9501. dev_priv->display.write_eld = g4x_write_eld;
  9502. } else if (IS_VALLEYVIEW(dev)) {
  9503. dev_priv->display.modeset_global_resources =
  9504. valleyview_modeset_global_resources;
  9505. dev_priv->display.write_eld = ironlake_write_eld;
  9506. }
  9507. /* Default just returns -ENODEV to indicate unsupported */
  9508. dev_priv->display.queue_flip = intel_default_queue_flip;
  9509. switch (INTEL_INFO(dev)->gen) {
  9510. case 2:
  9511. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  9512. break;
  9513. case 3:
  9514. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  9515. break;
  9516. case 4:
  9517. case 5:
  9518. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  9519. break;
  9520. case 6:
  9521. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  9522. break;
  9523. case 7:
  9524. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  9525. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  9526. break;
  9527. }
  9528. intel_panel_init_backlight_funcs(dev);
  9529. }
  9530. /*
  9531. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  9532. * resume, or other times. This quirk makes sure that's the case for
  9533. * affected systems.
  9534. */
  9535. static void quirk_pipea_force(struct drm_device *dev)
  9536. {
  9537. struct drm_i915_private *dev_priv = dev->dev_private;
  9538. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  9539. DRM_INFO("applying pipe a force quirk\n");
  9540. }
  9541. /*
  9542. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  9543. */
  9544. static void quirk_ssc_force_disable(struct drm_device *dev)
  9545. {
  9546. struct drm_i915_private *dev_priv = dev->dev_private;
  9547. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  9548. DRM_INFO("applying lvds SSC disable quirk\n");
  9549. }
  9550. /*
  9551. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  9552. * brightness value
  9553. */
  9554. static void quirk_invert_brightness(struct drm_device *dev)
  9555. {
  9556. struct drm_i915_private *dev_priv = dev->dev_private;
  9557. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  9558. DRM_INFO("applying inverted panel brightness quirk\n");
  9559. }
  9560. struct intel_quirk {
  9561. int device;
  9562. int subsystem_vendor;
  9563. int subsystem_device;
  9564. void (*hook)(struct drm_device *dev);
  9565. };
  9566. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  9567. struct intel_dmi_quirk {
  9568. void (*hook)(struct drm_device *dev);
  9569. const struct dmi_system_id (*dmi_id_list)[];
  9570. };
  9571. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  9572. {
  9573. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  9574. return 1;
  9575. }
  9576. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  9577. {
  9578. .dmi_id_list = &(const struct dmi_system_id[]) {
  9579. {
  9580. .callback = intel_dmi_reverse_brightness,
  9581. .ident = "NCR Corporation",
  9582. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  9583. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  9584. },
  9585. },
  9586. { } /* terminating entry */
  9587. },
  9588. .hook = quirk_invert_brightness,
  9589. },
  9590. };
  9591. static struct intel_quirk intel_quirks[] = {
  9592. /* HP Mini needs pipe A force quirk (LP: #322104) */
  9593. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  9594. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  9595. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  9596. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  9597. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  9598. /* 830 needs to leave pipe A & dpll A up */
  9599. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  9600. /* Lenovo U160 cannot use SSC on LVDS */
  9601. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  9602. /* Sony Vaio Y cannot use SSC on LVDS */
  9603. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  9604. /* Acer Aspire 5734Z must invert backlight brightness */
  9605. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  9606. /* Acer/eMachines G725 */
  9607. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  9608. /* Acer/eMachines e725 */
  9609. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  9610. /* Acer/Packard Bell NCL20 */
  9611. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  9612. /* Acer Aspire 4736Z */
  9613. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  9614. /* Acer Aspire 5336 */
  9615. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  9616. };
  9617. static void intel_init_quirks(struct drm_device *dev)
  9618. {
  9619. struct pci_dev *d = dev->pdev;
  9620. int i;
  9621. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  9622. struct intel_quirk *q = &intel_quirks[i];
  9623. if (d->device == q->device &&
  9624. (d->subsystem_vendor == q->subsystem_vendor ||
  9625. q->subsystem_vendor == PCI_ANY_ID) &&
  9626. (d->subsystem_device == q->subsystem_device ||
  9627. q->subsystem_device == PCI_ANY_ID))
  9628. q->hook(dev);
  9629. }
  9630. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  9631. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  9632. intel_dmi_quirks[i].hook(dev);
  9633. }
  9634. }
  9635. /* Disable the VGA plane that we never use */
  9636. static void i915_disable_vga(struct drm_device *dev)
  9637. {
  9638. struct drm_i915_private *dev_priv = dev->dev_private;
  9639. u8 sr1;
  9640. u32 vga_reg = i915_vgacntrl_reg(dev);
  9641. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  9642. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  9643. outb(SR01, VGA_SR_INDEX);
  9644. sr1 = inb(VGA_SR_DATA);
  9645. outb(sr1 | 1<<5, VGA_SR_DATA);
  9646. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  9647. udelay(300);
  9648. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  9649. POSTING_READ(vga_reg);
  9650. }
  9651. void intel_modeset_init_hw(struct drm_device *dev)
  9652. {
  9653. intel_prepare_ddi(dev);
  9654. intel_init_clock_gating(dev);
  9655. intel_reset_dpio(dev);
  9656. intel_enable_gt_powersave(dev);
  9657. }
  9658. void intel_modeset_suspend_hw(struct drm_device *dev)
  9659. {
  9660. intel_suspend_hw(dev);
  9661. }
  9662. void intel_modeset_init(struct drm_device *dev)
  9663. {
  9664. struct drm_i915_private *dev_priv = dev->dev_private;
  9665. int sprite, ret;
  9666. enum pipe pipe;
  9667. struct intel_crtc *crtc;
  9668. drm_mode_config_init(dev);
  9669. dev->mode_config.min_width = 0;
  9670. dev->mode_config.min_height = 0;
  9671. dev->mode_config.preferred_depth = 24;
  9672. dev->mode_config.prefer_shadow = 1;
  9673. dev->mode_config.funcs = &intel_mode_funcs;
  9674. intel_init_quirks(dev);
  9675. intel_init_pm(dev);
  9676. if (INTEL_INFO(dev)->num_pipes == 0)
  9677. return;
  9678. intel_init_display(dev);
  9679. if (IS_GEN2(dev)) {
  9680. dev->mode_config.max_width = 2048;
  9681. dev->mode_config.max_height = 2048;
  9682. } else if (IS_GEN3(dev)) {
  9683. dev->mode_config.max_width = 4096;
  9684. dev->mode_config.max_height = 4096;
  9685. } else {
  9686. dev->mode_config.max_width = 8192;
  9687. dev->mode_config.max_height = 8192;
  9688. }
  9689. if (IS_GEN2(dev)) {
  9690. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  9691. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  9692. } else {
  9693. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  9694. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  9695. }
  9696. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  9697. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  9698. INTEL_INFO(dev)->num_pipes,
  9699. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  9700. for_each_pipe(pipe) {
  9701. intel_crtc_init(dev, pipe);
  9702. for_each_sprite(pipe, sprite) {
  9703. ret = intel_plane_init(dev, pipe, sprite);
  9704. if (ret)
  9705. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  9706. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  9707. }
  9708. }
  9709. intel_init_dpio(dev);
  9710. intel_reset_dpio(dev);
  9711. intel_cpu_pll_init(dev);
  9712. intel_shared_dpll_init(dev);
  9713. /* Just disable it once at startup */
  9714. i915_disable_vga(dev);
  9715. intel_setup_outputs(dev);
  9716. /* Just in case the BIOS is doing something questionable. */
  9717. intel_disable_fbc(dev);
  9718. mutex_lock(&dev->mode_config.mutex);
  9719. intel_modeset_setup_hw_state(dev, false);
  9720. mutex_unlock(&dev->mode_config.mutex);
  9721. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  9722. base.head) {
  9723. if (!crtc->active)
  9724. continue;
  9725. /*
  9726. * Note that reserving the BIOS fb up front prevents us
  9727. * from stuffing other stolen allocations like the ring
  9728. * on top. This prevents some ugliness at boot time, and
  9729. * can even allow for smooth boot transitions if the BIOS
  9730. * fb is large enough for the active pipe configuration.
  9731. */
  9732. if (dev_priv->display.get_plane_config) {
  9733. dev_priv->display.get_plane_config(crtc,
  9734. &crtc->plane_config);
  9735. /*
  9736. * If the fb is shared between multiple heads, we'll
  9737. * just get the first one.
  9738. */
  9739. intel_find_plane_obj(crtc, &crtc->plane_config);
  9740. }
  9741. }
  9742. }
  9743. static void
  9744. intel_connector_break_all_links(struct intel_connector *connector)
  9745. {
  9746. connector->base.dpms = DRM_MODE_DPMS_OFF;
  9747. connector->base.encoder = NULL;
  9748. connector->encoder->connectors_active = false;
  9749. connector->encoder->base.crtc = NULL;
  9750. }
  9751. static void intel_enable_pipe_a(struct drm_device *dev)
  9752. {
  9753. struct intel_connector *connector;
  9754. struct drm_connector *crt = NULL;
  9755. struct intel_load_detect_pipe load_detect_temp;
  9756. /* We can't just switch on the pipe A, we need to set things up with a
  9757. * proper mode and output configuration. As a gross hack, enable pipe A
  9758. * by enabling the load detect pipe once. */
  9759. list_for_each_entry(connector,
  9760. &dev->mode_config.connector_list,
  9761. base.head) {
  9762. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  9763. crt = &connector->base;
  9764. break;
  9765. }
  9766. }
  9767. if (!crt)
  9768. return;
  9769. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
  9770. intel_release_load_detect_pipe(crt, &load_detect_temp);
  9771. }
  9772. static bool
  9773. intel_check_plane_mapping(struct intel_crtc *crtc)
  9774. {
  9775. struct drm_device *dev = crtc->base.dev;
  9776. struct drm_i915_private *dev_priv = dev->dev_private;
  9777. u32 reg, val;
  9778. if (INTEL_INFO(dev)->num_pipes == 1)
  9779. return true;
  9780. reg = DSPCNTR(!crtc->plane);
  9781. val = I915_READ(reg);
  9782. if ((val & DISPLAY_PLANE_ENABLE) &&
  9783. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  9784. return false;
  9785. return true;
  9786. }
  9787. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  9788. {
  9789. struct drm_device *dev = crtc->base.dev;
  9790. struct drm_i915_private *dev_priv = dev->dev_private;
  9791. u32 reg;
  9792. /* Clear any frame start delays used for debugging left by the BIOS */
  9793. reg = PIPECONF(crtc->config.cpu_transcoder);
  9794. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  9795. /* We need to sanitize the plane -> pipe mapping first because this will
  9796. * disable the crtc (and hence change the state) if it is wrong. Note
  9797. * that gen4+ has a fixed plane -> pipe mapping. */
  9798. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  9799. struct intel_connector *connector;
  9800. bool plane;
  9801. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  9802. crtc->base.base.id);
  9803. /* Pipe has the wrong plane attached and the plane is active.
  9804. * Temporarily change the plane mapping and disable everything
  9805. * ... */
  9806. plane = crtc->plane;
  9807. crtc->plane = !plane;
  9808. dev_priv->display.crtc_disable(&crtc->base);
  9809. crtc->plane = plane;
  9810. /* ... and break all links. */
  9811. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9812. base.head) {
  9813. if (connector->encoder->base.crtc != &crtc->base)
  9814. continue;
  9815. intel_connector_break_all_links(connector);
  9816. }
  9817. WARN_ON(crtc->active);
  9818. crtc->base.enabled = false;
  9819. }
  9820. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  9821. crtc->pipe == PIPE_A && !crtc->active) {
  9822. /* BIOS forgot to enable pipe A, this mostly happens after
  9823. * resume. Force-enable the pipe to fix this, the update_dpms
  9824. * call below we restore the pipe to the right state, but leave
  9825. * the required bits on. */
  9826. intel_enable_pipe_a(dev);
  9827. }
  9828. /* Adjust the state of the output pipe according to whether we
  9829. * have active connectors/encoders. */
  9830. intel_crtc_update_dpms(&crtc->base);
  9831. if (crtc->active != crtc->base.enabled) {
  9832. struct intel_encoder *encoder;
  9833. /* This can happen either due to bugs in the get_hw_state
  9834. * functions or because the pipe is force-enabled due to the
  9835. * pipe A quirk. */
  9836. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  9837. crtc->base.base.id,
  9838. crtc->base.enabled ? "enabled" : "disabled",
  9839. crtc->active ? "enabled" : "disabled");
  9840. crtc->base.enabled = crtc->active;
  9841. /* Because we only establish the connector -> encoder ->
  9842. * crtc links if something is active, this means the
  9843. * crtc is now deactivated. Break the links. connector
  9844. * -> encoder links are only establish when things are
  9845. * actually up, hence no need to break them. */
  9846. WARN_ON(crtc->active);
  9847. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  9848. WARN_ON(encoder->connectors_active);
  9849. encoder->base.crtc = NULL;
  9850. }
  9851. }
  9852. if (crtc->active) {
  9853. /*
  9854. * We start out with underrun reporting disabled to avoid races.
  9855. * For correct bookkeeping mark this on active crtcs.
  9856. *
  9857. * No protection against concurrent access is required - at
  9858. * worst a fifo underrun happens which also sets this to false.
  9859. */
  9860. crtc->cpu_fifo_underrun_disabled = true;
  9861. crtc->pch_fifo_underrun_disabled = true;
  9862. }
  9863. }
  9864. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  9865. {
  9866. struct intel_connector *connector;
  9867. struct drm_device *dev = encoder->base.dev;
  9868. /* We need to check both for a crtc link (meaning that the
  9869. * encoder is active and trying to read from a pipe) and the
  9870. * pipe itself being active. */
  9871. bool has_active_crtc = encoder->base.crtc &&
  9872. to_intel_crtc(encoder->base.crtc)->active;
  9873. if (encoder->connectors_active && !has_active_crtc) {
  9874. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  9875. encoder->base.base.id,
  9876. drm_get_encoder_name(&encoder->base));
  9877. /* Connector is active, but has no active pipe. This is
  9878. * fallout from our resume register restoring. Disable
  9879. * the encoder manually again. */
  9880. if (encoder->base.crtc) {
  9881. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  9882. encoder->base.base.id,
  9883. drm_get_encoder_name(&encoder->base));
  9884. encoder->disable(encoder);
  9885. }
  9886. /* Inconsistent output/port/pipe state happens presumably due to
  9887. * a bug in one of the get_hw_state functions. Or someplace else
  9888. * in our code, like the register restore mess on resume. Clamp
  9889. * things to off as a safer default. */
  9890. list_for_each_entry(connector,
  9891. &dev->mode_config.connector_list,
  9892. base.head) {
  9893. if (connector->encoder != encoder)
  9894. continue;
  9895. intel_connector_break_all_links(connector);
  9896. }
  9897. }
  9898. /* Enabled encoders without active connectors will be fixed in
  9899. * the crtc fixup. */
  9900. }
  9901. void i915_redisable_vga_power_on(struct drm_device *dev)
  9902. {
  9903. struct drm_i915_private *dev_priv = dev->dev_private;
  9904. u32 vga_reg = i915_vgacntrl_reg(dev);
  9905. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  9906. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  9907. i915_disable_vga(dev);
  9908. }
  9909. }
  9910. void i915_redisable_vga(struct drm_device *dev)
  9911. {
  9912. struct drm_i915_private *dev_priv = dev->dev_private;
  9913. /* This function can be called both from intel_modeset_setup_hw_state or
  9914. * at a very early point in our resume sequence, where the power well
  9915. * structures are not yet restored. Since this function is at a very
  9916. * paranoid "someone might have enabled VGA while we were not looking"
  9917. * level, just check if the power well is enabled instead of trying to
  9918. * follow the "don't touch the power well if we don't need it" policy
  9919. * the rest of the driver uses. */
  9920. if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
  9921. return;
  9922. i915_redisable_vga_power_on(dev);
  9923. }
  9924. static bool primary_get_hw_state(struct intel_crtc *crtc)
  9925. {
  9926. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  9927. if (!crtc->active)
  9928. return false;
  9929. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  9930. }
  9931. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  9932. {
  9933. struct drm_i915_private *dev_priv = dev->dev_private;
  9934. enum pipe pipe;
  9935. struct intel_crtc *crtc;
  9936. struct intel_encoder *encoder;
  9937. struct intel_connector *connector;
  9938. int i;
  9939. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  9940. base.head) {
  9941. memset(&crtc->config, 0, sizeof(crtc->config));
  9942. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  9943. crtc->active = dev_priv->display.get_pipe_config(crtc,
  9944. &crtc->config);
  9945. crtc->base.enabled = crtc->active;
  9946. crtc->primary_enabled = primary_get_hw_state(crtc);
  9947. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  9948. crtc->base.base.id,
  9949. crtc->active ? "enabled" : "disabled");
  9950. }
  9951. /* FIXME: Smash this into the new shared dpll infrastructure. */
  9952. if (HAS_DDI(dev))
  9953. intel_ddi_setup_hw_pll_state(dev);
  9954. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9955. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9956. pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
  9957. pll->active = 0;
  9958. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  9959. base.head) {
  9960. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9961. pll->active++;
  9962. }
  9963. pll->refcount = pll->active;
  9964. DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
  9965. pll->name, pll->refcount, pll->on);
  9966. }
  9967. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  9968. base.head) {
  9969. pipe = 0;
  9970. if (encoder->get_hw_state(encoder, &pipe)) {
  9971. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  9972. encoder->base.crtc = &crtc->base;
  9973. encoder->get_config(encoder, &crtc->config);
  9974. } else {
  9975. encoder->base.crtc = NULL;
  9976. }
  9977. encoder->connectors_active = false;
  9978. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  9979. encoder->base.base.id,
  9980. drm_get_encoder_name(&encoder->base),
  9981. encoder->base.crtc ? "enabled" : "disabled",
  9982. pipe_name(pipe));
  9983. }
  9984. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9985. base.head) {
  9986. if (connector->get_hw_state(connector)) {
  9987. connector->base.dpms = DRM_MODE_DPMS_ON;
  9988. connector->encoder->connectors_active = true;
  9989. connector->base.encoder = &connector->encoder->base;
  9990. } else {
  9991. connector->base.dpms = DRM_MODE_DPMS_OFF;
  9992. connector->base.encoder = NULL;
  9993. }
  9994. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  9995. connector->base.base.id,
  9996. drm_get_connector_name(&connector->base),
  9997. connector->base.encoder ? "enabled" : "disabled");
  9998. }
  9999. }
  10000. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  10001. * and i915 state tracking structures. */
  10002. void intel_modeset_setup_hw_state(struct drm_device *dev,
  10003. bool force_restore)
  10004. {
  10005. struct drm_i915_private *dev_priv = dev->dev_private;
  10006. enum pipe pipe;
  10007. struct intel_crtc *crtc;
  10008. struct intel_encoder *encoder;
  10009. int i;
  10010. intel_modeset_readout_hw_state(dev);
  10011. /*
  10012. * Now that we have the config, copy it to each CRTC struct
  10013. * Note that this could go away if we move to using crtc_config
  10014. * checking everywhere.
  10015. */
  10016. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  10017. base.head) {
  10018. if (crtc->active && i915.fastboot) {
  10019. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  10020. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  10021. crtc->base.base.id);
  10022. drm_mode_debug_printmodeline(&crtc->base.mode);
  10023. }
  10024. }
  10025. /* HW state is read out, now we need to sanitize this mess. */
  10026. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  10027. base.head) {
  10028. intel_sanitize_encoder(encoder);
  10029. }
  10030. for_each_pipe(pipe) {
  10031. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10032. intel_sanitize_crtc(crtc);
  10033. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  10034. }
  10035. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10036. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10037. if (!pll->on || pll->active)
  10038. continue;
  10039. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  10040. pll->disable(dev_priv, pll);
  10041. pll->on = false;
  10042. }
  10043. if (HAS_PCH_SPLIT(dev))
  10044. ilk_wm_get_hw_state(dev);
  10045. if (force_restore) {
  10046. i915_redisable_vga(dev);
  10047. /*
  10048. * We need to use raw interfaces for restoring state to avoid
  10049. * checking (bogus) intermediate states.
  10050. */
  10051. for_each_pipe(pipe) {
  10052. struct drm_crtc *crtc =
  10053. dev_priv->pipe_to_crtc_mapping[pipe];
  10054. __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  10055. crtc->primary->fb);
  10056. }
  10057. } else {
  10058. intel_modeset_update_staged_output_state(dev);
  10059. }
  10060. intel_modeset_check_state(dev);
  10061. }
  10062. void intel_modeset_gem_init(struct drm_device *dev)
  10063. {
  10064. struct drm_crtc *c;
  10065. struct intel_framebuffer *fb;
  10066. mutex_lock(&dev->struct_mutex);
  10067. intel_init_gt_powersave(dev);
  10068. mutex_unlock(&dev->struct_mutex);
  10069. intel_modeset_init_hw(dev);
  10070. intel_setup_overlay(dev);
  10071. /*
  10072. * Make sure any fbs we allocated at startup are properly
  10073. * pinned & fenced. When we do the allocation it's too early
  10074. * for this.
  10075. */
  10076. mutex_lock(&dev->struct_mutex);
  10077. list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
  10078. if (!c->primary->fb)
  10079. continue;
  10080. fb = to_intel_framebuffer(c->primary->fb);
  10081. if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL)) {
  10082. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  10083. to_intel_crtc(c)->pipe);
  10084. drm_framebuffer_unreference(c->primary->fb);
  10085. c->primary->fb = NULL;
  10086. }
  10087. }
  10088. mutex_unlock(&dev->struct_mutex);
  10089. }
  10090. void intel_connector_unregister(struct intel_connector *intel_connector)
  10091. {
  10092. struct drm_connector *connector = &intel_connector->base;
  10093. intel_panel_destroy_backlight(connector);
  10094. drm_sysfs_connector_remove(connector);
  10095. }
  10096. void intel_modeset_cleanup(struct drm_device *dev)
  10097. {
  10098. struct drm_i915_private *dev_priv = dev->dev_private;
  10099. struct drm_crtc *crtc;
  10100. struct drm_connector *connector;
  10101. /*
  10102. * Interrupts and polling as the first thing to avoid creating havoc.
  10103. * Too much stuff here (turning of rps, connectors, ...) would
  10104. * experience fancy races otherwise.
  10105. */
  10106. drm_irq_uninstall(dev);
  10107. cancel_work_sync(&dev_priv->hotplug_work);
  10108. /*
  10109. * Due to the hpd irq storm handling the hotplug work can re-arm the
  10110. * poll handlers. Hence disable polling after hpd handling is shut down.
  10111. */
  10112. drm_kms_helper_poll_fini(dev);
  10113. mutex_lock(&dev->struct_mutex);
  10114. intel_unregister_dsm_handler();
  10115. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  10116. /* Skip inactive CRTCs */
  10117. if (!crtc->primary->fb)
  10118. continue;
  10119. intel_increase_pllclock(crtc);
  10120. }
  10121. intel_disable_fbc(dev);
  10122. intel_disable_gt_powersave(dev);
  10123. ironlake_teardown_rc6(dev);
  10124. mutex_unlock(&dev->struct_mutex);
  10125. /* flush any delayed tasks or pending work */
  10126. flush_scheduled_work();
  10127. /* destroy the backlight and sysfs files before encoders/connectors */
  10128. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10129. struct intel_connector *intel_connector;
  10130. intel_connector = to_intel_connector(connector);
  10131. intel_connector->unregister(intel_connector);
  10132. }
  10133. drm_mode_config_cleanup(dev);
  10134. intel_cleanup_overlay(dev);
  10135. mutex_lock(&dev->struct_mutex);
  10136. intel_cleanup_gt_powersave(dev);
  10137. mutex_unlock(&dev->struct_mutex);
  10138. }
  10139. /*
  10140. * Return which encoder is currently attached for connector.
  10141. */
  10142. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  10143. {
  10144. return &intel_attached_encoder(connector)->base;
  10145. }
  10146. void intel_connector_attach_encoder(struct intel_connector *connector,
  10147. struct intel_encoder *encoder)
  10148. {
  10149. connector->encoder = encoder;
  10150. drm_mode_connector_attach_encoder(&connector->base,
  10151. &encoder->base);
  10152. }
  10153. /*
  10154. * set vga decode state - true == enable VGA decode
  10155. */
  10156. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  10157. {
  10158. struct drm_i915_private *dev_priv = dev->dev_private;
  10159. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  10160. u16 gmch_ctrl;
  10161. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  10162. DRM_ERROR("failed to read control word\n");
  10163. return -EIO;
  10164. }
  10165. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  10166. return 0;
  10167. if (state)
  10168. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  10169. else
  10170. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  10171. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  10172. DRM_ERROR("failed to write control word\n");
  10173. return -EIO;
  10174. }
  10175. return 0;
  10176. }
  10177. struct intel_display_error_state {
  10178. u32 power_well_driver;
  10179. int num_transcoders;
  10180. struct intel_cursor_error_state {
  10181. u32 control;
  10182. u32 position;
  10183. u32 base;
  10184. u32 size;
  10185. } cursor[I915_MAX_PIPES];
  10186. struct intel_pipe_error_state {
  10187. bool power_domain_on;
  10188. u32 source;
  10189. u32 stat;
  10190. } pipe[I915_MAX_PIPES];
  10191. struct intel_plane_error_state {
  10192. u32 control;
  10193. u32 stride;
  10194. u32 size;
  10195. u32 pos;
  10196. u32 addr;
  10197. u32 surface;
  10198. u32 tile_offset;
  10199. } plane[I915_MAX_PIPES];
  10200. struct intel_transcoder_error_state {
  10201. bool power_domain_on;
  10202. enum transcoder cpu_transcoder;
  10203. u32 conf;
  10204. u32 htotal;
  10205. u32 hblank;
  10206. u32 hsync;
  10207. u32 vtotal;
  10208. u32 vblank;
  10209. u32 vsync;
  10210. } transcoder[4];
  10211. };
  10212. struct intel_display_error_state *
  10213. intel_display_capture_error_state(struct drm_device *dev)
  10214. {
  10215. struct drm_i915_private *dev_priv = dev->dev_private;
  10216. struct intel_display_error_state *error;
  10217. int transcoders[] = {
  10218. TRANSCODER_A,
  10219. TRANSCODER_B,
  10220. TRANSCODER_C,
  10221. TRANSCODER_EDP,
  10222. };
  10223. int i;
  10224. if (INTEL_INFO(dev)->num_pipes == 0)
  10225. return NULL;
  10226. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  10227. if (error == NULL)
  10228. return NULL;
  10229. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  10230. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  10231. for_each_pipe(i) {
  10232. error->pipe[i].power_domain_on =
  10233. intel_display_power_enabled_sw(dev_priv,
  10234. POWER_DOMAIN_PIPE(i));
  10235. if (!error->pipe[i].power_domain_on)
  10236. continue;
  10237. if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
  10238. error->cursor[i].control = I915_READ(CURCNTR(i));
  10239. error->cursor[i].position = I915_READ(CURPOS(i));
  10240. error->cursor[i].base = I915_READ(CURBASE(i));
  10241. } else {
  10242. error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
  10243. error->cursor[i].position = I915_READ(CURPOS_IVB(i));
  10244. error->cursor[i].base = I915_READ(CURBASE_IVB(i));
  10245. }
  10246. error->plane[i].control = I915_READ(DSPCNTR(i));
  10247. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  10248. if (INTEL_INFO(dev)->gen <= 3) {
  10249. error->plane[i].size = I915_READ(DSPSIZE(i));
  10250. error->plane[i].pos = I915_READ(DSPPOS(i));
  10251. }
  10252. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  10253. error->plane[i].addr = I915_READ(DSPADDR(i));
  10254. if (INTEL_INFO(dev)->gen >= 4) {
  10255. error->plane[i].surface = I915_READ(DSPSURF(i));
  10256. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  10257. }
  10258. error->pipe[i].source = I915_READ(PIPESRC(i));
  10259. if (!HAS_PCH_SPLIT(dev))
  10260. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  10261. }
  10262. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  10263. if (HAS_DDI(dev_priv->dev))
  10264. error->num_transcoders++; /* Account for eDP. */
  10265. for (i = 0; i < error->num_transcoders; i++) {
  10266. enum transcoder cpu_transcoder = transcoders[i];
  10267. error->transcoder[i].power_domain_on =
  10268. intel_display_power_enabled_sw(dev_priv,
  10269. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  10270. if (!error->transcoder[i].power_domain_on)
  10271. continue;
  10272. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  10273. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  10274. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  10275. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  10276. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  10277. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  10278. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  10279. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  10280. }
  10281. return error;
  10282. }
  10283. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  10284. void
  10285. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  10286. struct drm_device *dev,
  10287. struct intel_display_error_state *error)
  10288. {
  10289. int i;
  10290. if (!error)
  10291. return;
  10292. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  10293. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  10294. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  10295. error->power_well_driver);
  10296. for_each_pipe(i) {
  10297. err_printf(m, "Pipe [%d]:\n", i);
  10298. err_printf(m, " Power: %s\n",
  10299. error->pipe[i].power_domain_on ? "on" : "off");
  10300. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  10301. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  10302. err_printf(m, "Plane [%d]:\n", i);
  10303. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  10304. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  10305. if (INTEL_INFO(dev)->gen <= 3) {
  10306. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  10307. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  10308. }
  10309. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  10310. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  10311. if (INTEL_INFO(dev)->gen >= 4) {
  10312. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  10313. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  10314. }
  10315. err_printf(m, "Cursor [%d]:\n", i);
  10316. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  10317. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  10318. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  10319. }
  10320. for (i = 0; i < error->num_transcoders; i++) {
  10321. err_printf(m, "CPU transcoder: %c\n",
  10322. transcoder_name(error->transcoder[i].cpu_transcoder));
  10323. err_printf(m, " Power: %s\n",
  10324. error->transcoder[i].power_domain_on ? "on" : "off");
  10325. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  10326. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  10327. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  10328. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  10329. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  10330. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  10331. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  10332. }
  10333. }