intel_display.c 373 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564
  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 <drm/drm_plane_helper.h>
  42. #include <drm/drm_rect.h>
  43. #include <linux/dma_remapping.h>
  44. /* Primary plane formats supported by all gen */
  45. #define COMMON_PRIMARY_FORMATS \
  46. DRM_FORMAT_C8, \
  47. DRM_FORMAT_RGB565, \
  48. DRM_FORMAT_XRGB8888, \
  49. DRM_FORMAT_ARGB8888
  50. /* Primary plane formats for gen <= 3 */
  51. static const uint32_t intel_primary_formats_gen2[] = {
  52. COMMON_PRIMARY_FORMATS,
  53. DRM_FORMAT_XRGB1555,
  54. DRM_FORMAT_ARGB1555,
  55. };
  56. /* Primary plane formats for gen >= 4 */
  57. static const uint32_t intel_primary_formats_gen4[] = {
  58. COMMON_PRIMARY_FORMATS, \
  59. DRM_FORMAT_XBGR8888,
  60. DRM_FORMAT_ABGR8888,
  61. DRM_FORMAT_XRGB2101010,
  62. DRM_FORMAT_ARGB2101010,
  63. DRM_FORMAT_XBGR2101010,
  64. DRM_FORMAT_ABGR2101010,
  65. };
  66. /* Cursor formats */
  67. static const uint32_t intel_cursor_formats[] = {
  68. DRM_FORMAT_ARGB8888,
  69. };
  70. #define DIV_ROUND_CLOSEST_ULL(ll, d) \
  71. ({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
  72. static void intel_increase_pllclock(struct drm_device *dev,
  73. enum pipe pipe);
  74. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  75. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  76. struct intel_crtc_config *pipe_config);
  77. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  78. struct intel_crtc_config *pipe_config);
  79. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  80. int x, int y, struct drm_framebuffer *old_fb);
  81. static int intel_framebuffer_init(struct drm_device *dev,
  82. struct intel_framebuffer *ifb,
  83. struct drm_mode_fb_cmd2 *mode_cmd,
  84. struct drm_i915_gem_object *obj);
  85. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  86. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  87. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  88. struct intel_link_m_n *m_n,
  89. struct intel_link_m_n *m2_n2);
  90. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  91. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  92. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  93. static void vlv_prepare_pll(struct intel_crtc *crtc);
  94. static void chv_prepare_pll(struct intel_crtc *crtc);
  95. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  96. {
  97. if (!connector->mst_port)
  98. return connector->encoder;
  99. else
  100. return &connector->mst_port->mst_encoders[pipe]->base;
  101. }
  102. typedef struct {
  103. int min, max;
  104. } intel_range_t;
  105. typedef struct {
  106. int dot_limit;
  107. int p2_slow, p2_fast;
  108. } intel_p2_t;
  109. typedef struct intel_limit intel_limit_t;
  110. struct intel_limit {
  111. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  112. intel_p2_t p2;
  113. };
  114. int
  115. intel_pch_rawclk(struct drm_device *dev)
  116. {
  117. struct drm_i915_private *dev_priv = dev->dev_private;
  118. WARN_ON(!HAS_PCH_SPLIT(dev));
  119. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  120. }
  121. static inline u32 /* units of 100MHz */
  122. intel_fdi_link_freq(struct drm_device *dev)
  123. {
  124. if (IS_GEN5(dev)) {
  125. struct drm_i915_private *dev_priv = dev->dev_private;
  126. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  127. } else
  128. return 27;
  129. }
  130. static const intel_limit_t intel_limits_i8xx_dac = {
  131. .dot = { .min = 25000, .max = 350000 },
  132. .vco = { .min = 908000, .max = 1512000 },
  133. .n = { .min = 2, .max = 16 },
  134. .m = { .min = 96, .max = 140 },
  135. .m1 = { .min = 18, .max = 26 },
  136. .m2 = { .min = 6, .max = 16 },
  137. .p = { .min = 4, .max = 128 },
  138. .p1 = { .min = 2, .max = 33 },
  139. .p2 = { .dot_limit = 165000,
  140. .p2_slow = 4, .p2_fast = 2 },
  141. };
  142. static const intel_limit_t intel_limits_i8xx_dvo = {
  143. .dot = { .min = 25000, .max = 350000 },
  144. .vco = { .min = 908000, .max = 1512000 },
  145. .n = { .min = 2, .max = 16 },
  146. .m = { .min = 96, .max = 140 },
  147. .m1 = { .min = 18, .max = 26 },
  148. .m2 = { .min = 6, .max = 16 },
  149. .p = { .min = 4, .max = 128 },
  150. .p1 = { .min = 2, .max = 33 },
  151. .p2 = { .dot_limit = 165000,
  152. .p2_slow = 4, .p2_fast = 4 },
  153. };
  154. static const intel_limit_t intel_limits_i8xx_lvds = {
  155. .dot = { .min = 25000, .max = 350000 },
  156. .vco = { .min = 908000, .max = 1512000 },
  157. .n = { .min = 2, .max = 16 },
  158. .m = { .min = 96, .max = 140 },
  159. .m1 = { .min = 18, .max = 26 },
  160. .m2 = { .min = 6, .max = 16 },
  161. .p = { .min = 4, .max = 128 },
  162. .p1 = { .min = 1, .max = 6 },
  163. .p2 = { .dot_limit = 165000,
  164. .p2_slow = 14, .p2_fast = 7 },
  165. };
  166. static const intel_limit_t intel_limits_i9xx_sdvo = {
  167. .dot = { .min = 20000, .max = 400000 },
  168. .vco = { .min = 1400000, .max = 2800000 },
  169. .n = { .min = 1, .max = 6 },
  170. .m = { .min = 70, .max = 120 },
  171. .m1 = { .min = 8, .max = 18 },
  172. .m2 = { .min = 3, .max = 7 },
  173. .p = { .min = 5, .max = 80 },
  174. .p1 = { .min = 1, .max = 8 },
  175. .p2 = { .dot_limit = 200000,
  176. .p2_slow = 10, .p2_fast = 5 },
  177. };
  178. static const intel_limit_t intel_limits_i9xx_lvds = {
  179. .dot = { .min = 20000, .max = 400000 },
  180. .vco = { .min = 1400000, .max = 2800000 },
  181. .n = { .min = 1, .max = 6 },
  182. .m = { .min = 70, .max = 120 },
  183. .m1 = { .min = 8, .max = 18 },
  184. .m2 = { .min = 3, .max = 7 },
  185. .p = { .min = 7, .max = 98 },
  186. .p1 = { .min = 1, .max = 8 },
  187. .p2 = { .dot_limit = 112000,
  188. .p2_slow = 14, .p2_fast = 7 },
  189. };
  190. static const intel_limit_t intel_limits_g4x_sdvo = {
  191. .dot = { .min = 25000, .max = 270000 },
  192. .vco = { .min = 1750000, .max = 3500000},
  193. .n = { .min = 1, .max = 4 },
  194. .m = { .min = 104, .max = 138 },
  195. .m1 = { .min = 17, .max = 23 },
  196. .m2 = { .min = 5, .max = 11 },
  197. .p = { .min = 10, .max = 30 },
  198. .p1 = { .min = 1, .max = 3},
  199. .p2 = { .dot_limit = 270000,
  200. .p2_slow = 10,
  201. .p2_fast = 10
  202. },
  203. };
  204. static const intel_limit_t intel_limits_g4x_hdmi = {
  205. .dot = { .min = 22000, .max = 400000 },
  206. .vco = { .min = 1750000, .max = 3500000},
  207. .n = { .min = 1, .max = 4 },
  208. .m = { .min = 104, .max = 138 },
  209. .m1 = { .min = 16, .max = 23 },
  210. .m2 = { .min = 5, .max = 11 },
  211. .p = { .min = 5, .max = 80 },
  212. .p1 = { .min = 1, .max = 8},
  213. .p2 = { .dot_limit = 165000,
  214. .p2_slow = 10, .p2_fast = 5 },
  215. };
  216. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  217. .dot = { .min = 20000, .max = 115000 },
  218. .vco = { .min = 1750000, .max = 3500000 },
  219. .n = { .min = 1, .max = 3 },
  220. .m = { .min = 104, .max = 138 },
  221. .m1 = { .min = 17, .max = 23 },
  222. .m2 = { .min = 5, .max = 11 },
  223. .p = { .min = 28, .max = 112 },
  224. .p1 = { .min = 2, .max = 8 },
  225. .p2 = { .dot_limit = 0,
  226. .p2_slow = 14, .p2_fast = 14
  227. },
  228. };
  229. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  230. .dot = { .min = 80000, .max = 224000 },
  231. .vco = { .min = 1750000, .max = 3500000 },
  232. .n = { .min = 1, .max = 3 },
  233. .m = { .min = 104, .max = 138 },
  234. .m1 = { .min = 17, .max = 23 },
  235. .m2 = { .min = 5, .max = 11 },
  236. .p = { .min = 14, .max = 42 },
  237. .p1 = { .min = 2, .max = 6 },
  238. .p2 = { .dot_limit = 0,
  239. .p2_slow = 7, .p2_fast = 7
  240. },
  241. };
  242. static const intel_limit_t intel_limits_pineview_sdvo = {
  243. .dot = { .min = 20000, .max = 400000},
  244. .vco = { .min = 1700000, .max = 3500000 },
  245. /* Pineview's Ncounter is a ring counter */
  246. .n = { .min = 3, .max = 6 },
  247. .m = { .min = 2, .max = 256 },
  248. /* Pineview only has one combined m divider, which we treat as m2. */
  249. .m1 = { .min = 0, .max = 0 },
  250. .m2 = { .min = 0, .max = 254 },
  251. .p = { .min = 5, .max = 80 },
  252. .p1 = { .min = 1, .max = 8 },
  253. .p2 = { .dot_limit = 200000,
  254. .p2_slow = 10, .p2_fast = 5 },
  255. };
  256. static const intel_limit_t intel_limits_pineview_lvds = {
  257. .dot = { .min = 20000, .max = 400000 },
  258. .vco = { .min = 1700000, .max = 3500000 },
  259. .n = { .min = 3, .max = 6 },
  260. .m = { .min = 2, .max = 256 },
  261. .m1 = { .min = 0, .max = 0 },
  262. .m2 = { .min = 0, .max = 254 },
  263. .p = { .min = 7, .max = 112 },
  264. .p1 = { .min = 1, .max = 8 },
  265. .p2 = { .dot_limit = 112000,
  266. .p2_slow = 14, .p2_fast = 14 },
  267. };
  268. /* Ironlake / Sandybridge
  269. *
  270. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  271. * the range value for them is (actual_value - 2).
  272. */
  273. static const intel_limit_t intel_limits_ironlake_dac = {
  274. .dot = { .min = 25000, .max = 350000 },
  275. .vco = { .min = 1760000, .max = 3510000 },
  276. .n = { .min = 1, .max = 5 },
  277. .m = { .min = 79, .max = 127 },
  278. .m1 = { .min = 12, .max = 22 },
  279. .m2 = { .min = 5, .max = 9 },
  280. .p = { .min = 5, .max = 80 },
  281. .p1 = { .min = 1, .max = 8 },
  282. .p2 = { .dot_limit = 225000,
  283. .p2_slow = 10, .p2_fast = 5 },
  284. };
  285. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  286. .dot = { .min = 25000, .max = 350000 },
  287. .vco = { .min = 1760000, .max = 3510000 },
  288. .n = { .min = 1, .max = 3 },
  289. .m = { .min = 79, .max = 118 },
  290. .m1 = { .min = 12, .max = 22 },
  291. .m2 = { .min = 5, .max = 9 },
  292. .p = { .min = 28, .max = 112 },
  293. .p1 = { .min = 2, .max = 8 },
  294. .p2 = { .dot_limit = 225000,
  295. .p2_slow = 14, .p2_fast = 14 },
  296. };
  297. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  298. .dot = { .min = 25000, .max = 350000 },
  299. .vco = { .min = 1760000, .max = 3510000 },
  300. .n = { .min = 1, .max = 3 },
  301. .m = { .min = 79, .max = 127 },
  302. .m1 = { .min = 12, .max = 22 },
  303. .m2 = { .min = 5, .max = 9 },
  304. .p = { .min = 14, .max = 56 },
  305. .p1 = { .min = 2, .max = 8 },
  306. .p2 = { .dot_limit = 225000,
  307. .p2_slow = 7, .p2_fast = 7 },
  308. };
  309. /* LVDS 100mhz refclk limits. */
  310. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  311. .dot = { .min = 25000, .max = 350000 },
  312. .vco = { .min = 1760000, .max = 3510000 },
  313. .n = { .min = 1, .max = 2 },
  314. .m = { .min = 79, .max = 126 },
  315. .m1 = { .min = 12, .max = 22 },
  316. .m2 = { .min = 5, .max = 9 },
  317. .p = { .min = 28, .max = 112 },
  318. .p1 = { .min = 2, .max = 8 },
  319. .p2 = { .dot_limit = 225000,
  320. .p2_slow = 14, .p2_fast = 14 },
  321. };
  322. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  323. .dot = { .min = 25000, .max = 350000 },
  324. .vco = { .min = 1760000, .max = 3510000 },
  325. .n = { .min = 1, .max = 3 },
  326. .m = { .min = 79, .max = 126 },
  327. .m1 = { .min = 12, .max = 22 },
  328. .m2 = { .min = 5, .max = 9 },
  329. .p = { .min = 14, .max = 42 },
  330. .p1 = { .min = 2, .max = 6 },
  331. .p2 = { .dot_limit = 225000,
  332. .p2_slow = 7, .p2_fast = 7 },
  333. };
  334. static const intel_limit_t intel_limits_vlv = {
  335. /*
  336. * These are the data rate limits (measured in fast clocks)
  337. * since those are the strictest limits we have. The fast
  338. * clock and actual rate limits are more relaxed, so checking
  339. * them would make no difference.
  340. */
  341. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  342. .vco = { .min = 4000000, .max = 6000000 },
  343. .n = { .min = 1, .max = 7 },
  344. .m1 = { .min = 2, .max = 3 },
  345. .m2 = { .min = 11, .max = 156 },
  346. .p1 = { .min = 2, .max = 3 },
  347. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  348. };
  349. static const intel_limit_t intel_limits_chv = {
  350. /*
  351. * These are the data rate limits (measured in fast clocks)
  352. * since those are the strictest limits we have. The fast
  353. * clock and actual rate limits are more relaxed, so checking
  354. * them would make no difference.
  355. */
  356. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  357. .vco = { .min = 4860000, .max = 6700000 },
  358. .n = { .min = 1, .max = 1 },
  359. .m1 = { .min = 2, .max = 2 },
  360. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  361. .p1 = { .min = 2, .max = 4 },
  362. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  363. };
  364. static void vlv_clock(int refclk, intel_clock_t *clock)
  365. {
  366. clock->m = clock->m1 * clock->m2;
  367. clock->p = clock->p1 * clock->p2;
  368. if (WARN_ON(clock->n == 0 || clock->p == 0))
  369. return;
  370. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  371. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  372. }
  373. /**
  374. * Returns whether any output on the specified pipe is of the specified type
  375. */
  376. static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  377. {
  378. struct drm_device *dev = crtc->dev;
  379. struct intel_encoder *encoder;
  380. for_each_encoder_on_crtc(dev, crtc, encoder)
  381. if (encoder->type == type)
  382. return true;
  383. return false;
  384. }
  385. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  386. int refclk)
  387. {
  388. struct drm_device *dev = crtc->dev;
  389. const intel_limit_t *limit;
  390. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  391. if (intel_is_dual_link_lvds(dev)) {
  392. if (refclk == 100000)
  393. limit = &intel_limits_ironlake_dual_lvds_100m;
  394. else
  395. limit = &intel_limits_ironlake_dual_lvds;
  396. } else {
  397. if (refclk == 100000)
  398. limit = &intel_limits_ironlake_single_lvds_100m;
  399. else
  400. limit = &intel_limits_ironlake_single_lvds;
  401. }
  402. } else
  403. limit = &intel_limits_ironlake_dac;
  404. return limit;
  405. }
  406. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  407. {
  408. struct drm_device *dev = crtc->dev;
  409. const intel_limit_t *limit;
  410. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  411. if (intel_is_dual_link_lvds(dev))
  412. limit = &intel_limits_g4x_dual_channel_lvds;
  413. else
  414. limit = &intel_limits_g4x_single_channel_lvds;
  415. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  416. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  417. limit = &intel_limits_g4x_hdmi;
  418. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  419. limit = &intel_limits_g4x_sdvo;
  420. } else /* The option is for other outputs */
  421. limit = &intel_limits_i9xx_sdvo;
  422. return limit;
  423. }
  424. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  425. {
  426. struct drm_device *dev = crtc->dev;
  427. const intel_limit_t *limit;
  428. if (HAS_PCH_SPLIT(dev))
  429. limit = intel_ironlake_limit(crtc, refclk);
  430. else if (IS_G4X(dev)) {
  431. limit = intel_g4x_limit(crtc);
  432. } else if (IS_PINEVIEW(dev)) {
  433. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  434. limit = &intel_limits_pineview_lvds;
  435. else
  436. limit = &intel_limits_pineview_sdvo;
  437. } else if (IS_CHERRYVIEW(dev)) {
  438. limit = &intel_limits_chv;
  439. } else if (IS_VALLEYVIEW(dev)) {
  440. limit = &intel_limits_vlv;
  441. } else if (!IS_GEN2(dev)) {
  442. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  443. limit = &intel_limits_i9xx_lvds;
  444. else
  445. limit = &intel_limits_i9xx_sdvo;
  446. } else {
  447. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  448. limit = &intel_limits_i8xx_lvds;
  449. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
  450. limit = &intel_limits_i8xx_dvo;
  451. else
  452. limit = &intel_limits_i8xx_dac;
  453. }
  454. return limit;
  455. }
  456. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  457. static void pineview_clock(int refclk, intel_clock_t *clock)
  458. {
  459. clock->m = clock->m2 + 2;
  460. clock->p = clock->p1 * clock->p2;
  461. if (WARN_ON(clock->n == 0 || clock->p == 0))
  462. return;
  463. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  464. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  465. }
  466. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  467. {
  468. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  469. }
  470. static void i9xx_clock(int refclk, intel_clock_t *clock)
  471. {
  472. clock->m = i9xx_dpll_compute_m(clock);
  473. clock->p = clock->p1 * clock->p2;
  474. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  475. return;
  476. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  477. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  478. }
  479. static void chv_clock(int refclk, intel_clock_t *clock)
  480. {
  481. clock->m = clock->m1 * clock->m2;
  482. clock->p = clock->p1 * clock->p2;
  483. if (WARN_ON(clock->n == 0 || clock->p == 0))
  484. return;
  485. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  486. clock->n << 22);
  487. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  488. }
  489. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  490. /**
  491. * Returns whether the given set of divisors are valid for a given refclk with
  492. * the given connectors.
  493. */
  494. static bool intel_PLL_is_valid(struct drm_device *dev,
  495. const intel_limit_t *limit,
  496. const intel_clock_t *clock)
  497. {
  498. if (clock->n < limit->n.min || limit->n.max < clock->n)
  499. INTELPllInvalid("n out of range\n");
  500. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  501. INTELPllInvalid("p1 out of range\n");
  502. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  503. INTELPllInvalid("m2 out of range\n");
  504. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  505. INTELPllInvalid("m1 out of range\n");
  506. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  507. if (clock->m1 <= clock->m2)
  508. INTELPllInvalid("m1 <= m2\n");
  509. if (!IS_VALLEYVIEW(dev)) {
  510. if (clock->p < limit->p.min || limit->p.max < clock->p)
  511. INTELPllInvalid("p out of range\n");
  512. if (clock->m < limit->m.min || limit->m.max < clock->m)
  513. INTELPllInvalid("m out of range\n");
  514. }
  515. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  516. INTELPllInvalid("vco out of range\n");
  517. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  518. * connector, etc., rather than just a single range.
  519. */
  520. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  521. INTELPllInvalid("dot out of range\n");
  522. return true;
  523. }
  524. static bool
  525. i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  526. int target, int refclk, intel_clock_t *match_clock,
  527. intel_clock_t *best_clock)
  528. {
  529. struct drm_device *dev = crtc->dev;
  530. intel_clock_t clock;
  531. int err = target;
  532. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  533. /*
  534. * For LVDS just rely on its current settings for dual-channel.
  535. * We haven't figured out how to reliably set up different
  536. * single/dual channel state, if we even can.
  537. */
  538. if (intel_is_dual_link_lvds(dev))
  539. clock.p2 = limit->p2.p2_fast;
  540. else
  541. clock.p2 = limit->p2.p2_slow;
  542. } else {
  543. if (target < limit->p2.dot_limit)
  544. clock.p2 = limit->p2.p2_slow;
  545. else
  546. clock.p2 = limit->p2.p2_fast;
  547. }
  548. memset(best_clock, 0, sizeof(*best_clock));
  549. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  550. clock.m1++) {
  551. for (clock.m2 = limit->m2.min;
  552. clock.m2 <= limit->m2.max; clock.m2++) {
  553. if (clock.m2 >= clock.m1)
  554. break;
  555. for (clock.n = limit->n.min;
  556. clock.n <= limit->n.max; clock.n++) {
  557. for (clock.p1 = limit->p1.min;
  558. clock.p1 <= limit->p1.max; clock.p1++) {
  559. int this_err;
  560. i9xx_clock(refclk, &clock);
  561. if (!intel_PLL_is_valid(dev, limit,
  562. &clock))
  563. continue;
  564. if (match_clock &&
  565. clock.p != match_clock->p)
  566. continue;
  567. this_err = abs(clock.dot - target);
  568. if (this_err < err) {
  569. *best_clock = clock;
  570. err = this_err;
  571. }
  572. }
  573. }
  574. }
  575. }
  576. return (err != target);
  577. }
  578. static bool
  579. pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  580. int target, int refclk, intel_clock_t *match_clock,
  581. intel_clock_t *best_clock)
  582. {
  583. struct drm_device *dev = crtc->dev;
  584. intel_clock_t clock;
  585. int err = target;
  586. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  587. /*
  588. * For LVDS just rely on its current settings for dual-channel.
  589. * We haven't figured out how to reliably set up different
  590. * single/dual channel state, if we even can.
  591. */
  592. if (intel_is_dual_link_lvds(dev))
  593. clock.p2 = limit->p2.p2_fast;
  594. else
  595. clock.p2 = limit->p2.p2_slow;
  596. } else {
  597. if (target < limit->p2.dot_limit)
  598. clock.p2 = limit->p2.p2_slow;
  599. else
  600. clock.p2 = limit->p2.p2_fast;
  601. }
  602. memset(best_clock, 0, sizeof(*best_clock));
  603. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  604. clock.m1++) {
  605. for (clock.m2 = limit->m2.min;
  606. clock.m2 <= limit->m2.max; clock.m2++) {
  607. for (clock.n = limit->n.min;
  608. clock.n <= limit->n.max; clock.n++) {
  609. for (clock.p1 = limit->p1.min;
  610. clock.p1 <= limit->p1.max; clock.p1++) {
  611. int this_err;
  612. pineview_clock(refclk, &clock);
  613. if (!intel_PLL_is_valid(dev, limit,
  614. &clock))
  615. continue;
  616. if (match_clock &&
  617. clock.p != match_clock->p)
  618. continue;
  619. this_err = abs(clock.dot - target);
  620. if (this_err < err) {
  621. *best_clock = clock;
  622. err = this_err;
  623. }
  624. }
  625. }
  626. }
  627. }
  628. return (err != target);
  629. }
  630. static bool
  631. g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  632. int target, int refclk, intel_clock_t *match_clock,
  633. intel_clock_t *best_clock)
  634. {
  635. struct drm_device *dev = crtc->dev;
  636. intel_clock_t clock;
  637. int max_n;
  638. bool found;
  639. /* approximately equals target * 0.00585 */
  640. int err_most = (target >> 8) + (target >> 9);
  641. found = false;
  642. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  643. if (intel_is_dual_link_lvds(dev))
  644. clock.p2 = limit->p2.p2_fast;
  645. else
  646. clock.p2 = limit->p2.p2_slow;
  647. } else {
  648. if (target < limit->p2.dot_limit)
  649. clock.p2 = limit->p2.p2_slow;
  650. else
  651. clock.p2 = limit->p2.p2_fast;
  652. }
  653. memset(best_clock, 0, sizeof(*best_clock));
  654. max_n = limit->n.max;
  655. /* based on hardware requirement, prefer smaller n to precision */
  656. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  657. /* based on hardware requirement, prefere larger m1,m2 */
  658. for (clock.m1 = limit->m1.max;
  659. clock.m1 >= limit->m1.min; clock.m1--) {
  660. for (clock.m2 = limit->m2.max;
  661. clock.m2 >= limit->m2.min; clock.m2--) {
  662. for (clock.p1 = limit->p1.max;
  663. clock.p1 >= limit->p1.min; clock.p1--) {
  664. int this_err;
  665. i9xx_clock(refclk, &clock);
  666. if (!intel_PLL_is_valid(dev, limit,
  667. &clock))
  668. continue;
  669. this_err = abs(clock.dot - target);
  670. if (this_err < err_most) {
  671. *best_clock = clock;
  672. err_most = this_err;
  673. max_n = clock.n;
  674. found = true;
  675. }
  676. }
  677. }
  678. }
  679. }
  680. return found;
  681. }
  682. static bool
  683. vlv_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. unsigned int bestppm = 1000000;
  690. /* min update 19.2 MHz */
  691. int max_n = min(limit->n.max, refclk / 19200);
  692. bool found = false;
  693. target *= 5; /* fast clock */
  694. memset(best_clock, 0, sizeof(*best_clock));
  695. /* based on hardware requirement, prefer smaller n to precision */
  696. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  697. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  698. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  699. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  700. clock.p = clock.p1 * clock.p2;
  701. /* based on hardware requirement, prefer bigger m1,m2 values */
  702. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  703. unsigned int ppm, diff;
  704. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  705. refclk * clock.m1);
  706. vlv_clock(refclk, &clock);
  707. if (!intel_PLL_is_valid(dev, limit,
  708. &clock))
  709. continue;
  710. diff = abs(clock.dot - target);
  711. ppm = div_u64(1000000ULL * diff, target);
  712. if (ppm < 100 && clock.p > best_clock->p) {
  713. bestppm = 0;
  714. *best_clock = clock;
  715. found = true;
  716. }
  717. if (bestppm >= 10 && ppm < bestppm - 10) {
  718. bestppm = ppm;
  719. *best_clock = clock;
  720. found = true;
  721. }
  722. }
  723. }
  724. }
  725. }
  726. return found;
  727. }
  728. static bool
  729. chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  730. int target, int refclk, intel_clock_t *match_clock,
  731. intel_clock_t *best_clock)
  732. {
  733. struct drm_device *dev = crtc->dev;
  734. intel_clock_t clock;
  735. uint64_t m2;
  736. int found = false;
  737. memset(best_clock, 0, sizeof(*best_clock));
  738. /*
  739. * Based on hardware doc, the n always set to 1, and m1 always
  740. * set to 2. If requires to support 200Mhz refclk, we need to
  741. * revisit this because n may not 1 anymore.
  742. */
  743. clock.n = 1, clock.m1 = 2;
  744. target *= 5; /* fast clock */
  745. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  746. for (clock.p2 = limit->p2.p2_fast;
  747. clock.p2 >= limit->p2.p2_slow;
  748. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  749. clock.p = clock.p1 * clock.p2;
  750. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  751. clock.n) << 22, refclk * clock.m1);
  752. if (m2 > INT_MAX/clock.m1)
  753. continue;
  754. clock.m2 = m2;
  755. chv_clock(refclk, &clock);
  756. if (!intel_PLL_is_valid(dev, limit, &clock))
  757. continue;
  758. /* based on hardware requirement, prefer bigger p
  759. */
  760. if (clock.p > best_clock->p) {
  761. *best_clock = clock;
  762. found = true;
  763. }
  764. }
  765. }
  766. return found;
  767. }
  768. bool intel_crtc_active(struct drm_crtc *crtc)
  769. {
  770. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  771. /* Be paranoid as we can arrive here with only partial
  772. * state retrieved from the hardware during setup.
  773. *
  774. * We can ditch the adjusted_mode.crtc_clock check as soon
  775. * as Haswell has gained clock readout/fastboot support.
  776. *
  777. * We can ditch the crtc->primary->fb check as soon as we can
  778. * properly reconstruct framebuffers.
  779. */
  780. return intel_crtc->active && crtc->primary->fb &&
  781. intel_crtc->config.adjusted_mode.crtc_clock;
  782. }
  783. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  784. enum pipe pipe)
  785. {
  786. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  787. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  788. return intel_crtc->config.cpu_transcoder;
  789. }
  790. static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
  791. {
  792. struct drm_i915_private *dev_priv = dev->dev_private;
  793. u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
  794. frame = I915_READ(frame_reg);
  795. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  796. WARN(1, "vblank wait on pipe %c timed out\n",
  797. pipe_name(pipe));
  798. }
  799. /**
  800. * intel_wait_for_vblank - wait for vblank on a given pipe
  801. * @dev: drm device
  802. * @pipe: pipe to wait for
  803. *
  804. * Wait for vblank to occur on a given pipe. Needed for various bits of
  805. * mode setting code.
  806. */
  807. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  808. {
  809. struct drm_i915_private *dev_priv = dev->dev_private;
  810. int pipestat_reg = PIPESTAT(pipe);
  811. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  812. g4x_wait_for_vblank(dev, pipe);
  813. return;
  814. }
  815. /* Clear existing vblank status. Note this will clear any other
  816. * sticky status fields as well.
  817. *
  818. * This races with i915_driver_irq_handler() with the result
  819. * that either function could miss a vblank event. Here it is not
  820. * fatal, as we will either wait upon the next vblank interrupt or
  821. * timeout. Generally speaking intel_wait_for_vblank() is only
  822. * called during modeset at which time the GPU should be idle and
  823. * should *not* be performing page flips and thus not waiting on
  824. * vblanks...
  825. * Currently, the result of us stealing a vblank from the irq
  826. * handler is that a single frame will be skipped during swapbuffers.
  827. */
  828. I915_WRITE(pipestat_reg,
  829. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  830. /* Wait for vblank interrupt bit to set */
  831. if (wait_for(I915_READ(pipestat_reg) &
  832. PIPE_VBLANK_INTERRUPT_STATUS,
  833. 50))
  834. DRM_DEBUG_KMS("vblank wait on pipe %c timed out\n",
  835. pipe_name(pipe));
  836. }
  837. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  838. {
  839. struct drm_i915_private *dev_priv = dev->dev_private;
  840. u32 reg = PIPEDSL(pipe);
  841. u32 line1, line2;
  842. u32 line_mask;
  843. if (IS_GEN2(dev))
  844. line_mask = DSL_LINEMASK_GEN2;
  845. else
  846. line_mask = DSL_LINEMASK_GEN3;
  847. line1 = I915_READ(reg) & line_mask;
  848. mdelay(5);
  849. line2 = I915_READ(reg) & line_mask;
  850. return line1 == line2;
  851. }
  852. /*
  853. * intel_wait_for_pipe_off - wait for pipe to turn off
  854. * @dev: drm device
  855. * @pipe: pipe to wait for
  856. *
  857. * After disabling a pipe, we can't wait for vblank in the usual way,
  858. * spinning on the vblank interrupt status bit, since we won't actually
  859. * see an interrupt when the pipe is disabled.
  860. *
  861. * On Gen4 and above:
  862. * wait for the pipe register state bit to turn off
  863. *
  864. * Otherwise:
  865. * wait for the display line value to settle (it usually
  866. * ends up stopping at the start of the next frame).
  867. *
  868. */
  869. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  870. {
  871. struct drm_i915_private *dev_priv = dev->dev_private;
  872. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  873. pipe);
  874. if (INTEL_INFO(dev)->gen >= 4) {
  875. int reg = PIPECONF(cpu_transcoder);
  876. /* Wait for the Pipe State to go off */
  877. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  878. 100))
  879. WARN(1, "pipe_off wait timed out\n");
  880. } else {
  881. /* Wait for the display line to settle */
  882. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  883. WARN(1, "pipe_off wait timed out\n");
  884. }
  885. }
  886. /*
  887. * ibx_digital_port_connected - is the specified port connected?
  888. * @dev_priv: i915 private structure
  889. * @port: the port to test
  890. *
  891. * Returns true if @port is connected, false otherwise.
  892. */
  893. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  894. struct intel_digital_port *port)
  895. {
  896. u32 bit;
  897. if (HAS_PCH_IBX(dev_priv->dev)) {
  898. switch (port->port) {
  899. case PORT_B:
  900. bit = SDE_PORTB_HOTPLUG;
  901. break;
  902. case PORT_C:
  903. bit = SDE_PORTC_HOTPLUG;
  904. break;
  905. case PORT_D:
  906. bit = SDE_PORTD_HOTPLUG;
  907. break;
  908. default:
  909. return true;
  910. }
  911. } else {
  912. switch (port->port) {
  913. case PORT_B:
  914. bit = SDE_PORTB_HOTPLUG_CPT;
  915. break;
  916. case PORT_C:
  917. bit = SDE_PORTC_HOTPLUG_CPT;
  918. break;
  919. case PORT_D:
  920. bit = SDE_PORTD_HOTPLUG_CPT;
  921. break;
  922. default:
  923. return true;
  924. }
  925. }
  926. return I915_READ(SDEISR) & bit;
  927. }
  928. static const char *state_string(bool enabled)
  929. {
  930. return enabled ? "on" : "off";
  931. }
  932. /* Only for pre-ILK configs */
  933. void assert_pll(struct drm_i915_private *dev_priv,
  934. enum pipe pipe, bool state)
  935. {
  936. int reg;
  937. u32 val;
  938. bool cur_state;
  939. reg = DPLL(pipe);
  940. val = I915_READ(reg);
  941. cur_state = !!(val & DPLL_VCO_ENABLE);
  942. WARN(cur_state != state,
  943. "PLL state assertion failure (expected %s, current %s)\n",
  944. state_string(state), state_string(cur_state));
  945. }
  946. /* XXX: the dsi pll is shared between MIPI DSI ports */
  947. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  948. {
  949. u32 val;
  950. bool cur_state;
  951. mutex_lock(&dev_priv->dpio_lock);
  952. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  953. mutex_unlock(&dev_priv->dpio_lock);
  954. cur_state = val & DSI_PLL_VCO_EN;
  955. WARN(cur_state != state,
  956. "DSI PLL state assertion failure (expected %s, current %s)\n",
  957. state_string(state), state_string(cur_state));
  958. }
  959. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  960. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  961. struct intel_shared_dpll *
  962. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  963. {
  964. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  965. if (crtc->config.shared_dpll < 0)
  966. return NULL;
  967. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  968. }
  969. /* For ILK+ */
  970. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  971. struct intel_shared_dpll *pll,
  972. bool state)
  973. {
  974. bool cur_state;
  975. struct intel_dpll_hw_state hw_state;
  976. if (WARN (!pll,
  977. "asserting DPLL %s with no DPLL\n", state_string(state)))
  978. return;
  979. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  980. WARN(cur_state != state,
  981. "%s assertion failure (expected %s, current %s)\n",
  982. pll->name, state_string(state), state_string(cur_state));
  983. }
  984. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  985. enum pipe pipe, bool state)
  986. {
  987. int reg;
  988. u32 val;
  989. bool cur_state;
  990. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  991. pipe);
  992. if (HAS_DDI(dev_priv->dev)) {
  993. /* DDI does not have a specific FDI_TX register */
  994. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  995. val = I915_READ(reg);
  996. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  997. } else {
  998. reg = FDI_TX_CTL(pipe);
  999. val = I915_READ(reg);
  1000. cur_state = !!(val & FDI_TX_ENABLE);
  1001. }
  1002. WARN(cur_state != state,
  1003. "FDI TX state assertion failure (expected %s, current %s)\n",
  1004. state_string(state), state_string(cur_state));
  1005. }
  1006. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1007. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1008. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1009. enum pipe pipe, bool state)
  1010. {
  1011. int reg;
  1012. u32 val;
  1013. bool cur_state;
  1014. reg = FDI_RX_CTL(pipe);
  1015. val = I915_READ(reg);
  1016. cur_state = !!(val & FDI_RX_ENABLE);
  1017. WARN(cur_state != state,
  1018. "FDI RX state assertion failure (expected %s, current %s)\n",
  1019. state_string(state), state_string(cur_state));
  1020. }
  1021. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1022. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1023. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1024. enum pipe pipe)
  1025. {
  1026. int reg;
  1027. u32 val;
  1028. /* ILK FDI PLL is always enabled */
  1029. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1030. return;
  1031. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1032. if (HAS_DDI(dev_priv->dev))
  1033. return;
  1034. reg = FDI_TX_CTL(pipe);
  1035. val = I915_READ(reg);
  1036. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1037. }
  1038. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1039. enum pipe pipe, bool state)
  1040. {
  1041. int reg;
  1042. u32 val;
  1043. bool cur_state;
  1044. reg = FDI_RX_CTL(pipe);
  1045. val = I915_READ(reg);
  1046. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1047. WARN(cur_state != state,
  1048. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1049. state_string(state), state_string(cur_state));
  1050. }
  1051. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1052. enum pipe pipe)
  1053. {
  1054. struct drm_device *dev = dev_priv->dev;
  1055. int pp_reg;
  1056. u32 val;
  1057. enum pipe panel_pipe = PIPE_A;
  1058. bool locked = true;
  1059. if (WARN_ON(HAS_DDI(dev)))
  1060. return;
  1061. if (HAS_PCH_SPLIT(dev)) {
  1062. u32 port_sel;
  1063. pp_reg = PCH_PP_CONTROL;
  1064. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1065. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1066. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1067. panel_pipe = PIPE_B;
  1068. /* XXX: else fix for eDP */
  1069. } else if (IS_VALLEYVIEW(dev)) {
  1070. /* presumably write lock depends on pipe, not port select */
  1071. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1072. panel_pipe = pipe;
  1073. } else {
  1074. pp_reg = PP_CONTROL;
  1075. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1076. panel_pipe = PIPE_B;
  1077. }
  1078. val = I915_READ(pp_reg);
  1079. if (!(val & PANEL_POWER_ON) ||
  1080. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1081. locked = false;
  1082. WARN(panel_pipe == pipe && locked,
  1083. "panel assertion failure, pipe %c regs locked\n",
  1084. pipe_name(pipe));
  1085. }
  1086. static void assert_cursor(struct drm_i915_private *dev_priv,
  1087. enum pipe pipe, bool state)
  1088. {
  1089. struct drm_device *dev = dev_priv->dev;
  1090. bool cur_state;
  1091. if (IS_845G(dev) || IS_I865G(dev))
  1092. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1093. else
  1094. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1095. WARN(cur_state != state,
  1096. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1097. pipe_name(pipe), state_string(state), state_string(cur_state));
  1098. }
  1099. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1100. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1101. void assert_pipe(struct drm_i915_private *dev_priv,
  1102. enum pipe pipe, bool state)
  1103. {
  1104. int reg;
  1105. u32 val;
  1106. bool cur_state;
  1107. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1108. pipe);
  1109. /* if we need the pipe A quirk it must be always on */
  1110. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  1111. state = true;
  1112. if (!intel_display_power_enabled(dev_priv,
  1113. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1114. cur_state = false;
  1115. } else {
  1116. reg = PIPECONF(cpu_transcoder);
  1117. val = I915_READ(reg);
  1118. cur_state = !!(val & PIPECONF_ENABLE);
  1119. }
  1120. WARN(cur_state != state,
  1121. "pipe %c assertion failure (expected %s, current %s)\n",
  1122. pipe_name(pipe), state_string(state), state_string(cur_state));
  1123. }
  1124. static void assert_plane(struct drm_i915_private *dev_priv,
  1125. enum plane plane, bool state)
  1126. {
  1127. int reg;
  1128. u32 val;
  1129. bool cur_state;
  1130. reg = DSPCNTR(plane);
  1131. val = I915_READ(reg);
  1132. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1133. WARN(cur_state != state,
  1134. "plane %c assertion failure (expected %s, current %s)\n",
  1135. plane_name(plane), state_string(state), state_string(cur_state));
  1136. }
  1137. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1138. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1139. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1140. enum pipe pipe)
  1141. {
  1142. struct drm_device *dev = dev_priv->dev;
  1143. int reg, i;
  1144. u32 val;
  1145. int cur_pipe;
  1146. /* Primary planes are fixed to pipes on gen4+ */
  1147. if (INTEL_INFO(dev)->gen >= 4) {
  1148. reg = DSPCNTR(pipe);
  1149. val = I915_READ(reg);
  1150. WARN(val & DISPLAY_PLANE_ENABLE,
  1151. "plane %c assertion failure, should be disabled but not\n",
  1152. plane_name(pipe));
  1153. return;
  1154. }
  1155. /* Need to check both planes against the pipe */
  1156. for_each_pipe(dev_priv, i) {
  1157. reg = DSPCNTR(i);
  1158. val = I915_READ(reg);
  1159. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1160. DISPPLANE_SEL_PIPE_SHIFT;
  1161. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1162. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1163. plane_name(i), pipe_name(pipe));
  1164. }
  1165. }
  1166. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1167. enum pipe pipe)
  1168. {
  1169. struct drm_device *dev = dev_priv->dev;
  1170. int reg, sprite;
  1171. u32 val;
  1172. if (IS_VALLEYVIEW(dev)) {
  1173. for_each_sprite(pipe, sprite) {
  1174. reg = SPCNTR(pipe, sprite);
  1175. val = I915_READ(reg);
  1176. WARN(val & SP_ENABLE,
  1177. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1178. sprite_name(pipe, sprite), pipe_name(pipe));
  1179. }
  1180. } else if (INTEL_INFO(dev)->gen >= 7) {
  1181. reg = SPRCTL(pipe);
  1182. val = I915_READ(reg);
  1183. WARN(val & SPRITE_ENABLE,
  1184. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1185. plane_name(pipe), pipe_name(pipe));
  1186. } else if (INTEL_INFO(dev)->gen >= 5) {
  1187. reg = DVSCNTR(pipe);
  1188. val = I915_READ(reg);
  1189. WARN(val & DVS_ENABLE,
  1190. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1191. plane_name(pipe), pipe_name(pipe));
  1192. }
  1193. }
  1194. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1195. {
  1196. u32 val;
  1197. bool enabled;
  1198. WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1199. val = I915_READ(PCH_DREF_CONTROL);
  1200. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1201. DREF_SUPERSPREAD_SOURCE_MASK));
  1202. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1203. }
  1204. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1205. enum pipe pipe)
  1206. {
  1207. int reg;
  1208. u32 val;
  1209. bool enabled;
  1210. reg = PCH_TRANSCONF(pipe);
  1211. val = I915_READ(reg);
  1212. enabled = !!(val & TRANS_ENABLE);
  1213. WARN(enabled,
  1214. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1215. pipe_name(pipe));
  1216. }
  1217. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1218. enum pipe pipe, u32 port_sel, u32 val)
  1219. {
  1220. if ((val & DP_PORT_EN) == 0)
  1221. return false;
  1222. if (HAS_PCH_CPT(dev_priv->dev)) {
  1223. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1224. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1225. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1226. return false;
  1227. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1228. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1229. return false;
  1230. } else {
  1231. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1232. return false;
  1233. }
  1234. return true;
  1235. }
  1236. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1237. enum pipe pipe, u32 val)
  1238. {
  1239. if ((val & SDVO_ENABLE) == 0)
  1240. return false;
  1241. if (HAS_PCH_CPT(dev_priv->dev)) {
  1242. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1243. return false;
  1244. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1245. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1246. return false;
  1247. } else {
  1248. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1249. return false;
  1250. }
  1251. return true;
  1252. }
  1253. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1254. enum pipe pipe, u32 val)
  1255. {
  1256. if ((val & LVDS_PORT_EN) == 0)
  1257. return false;
  1258. if (HAS_PCH_CPT(dev_priv->dev)) {
  1259. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1260. return false;
  1261. } else {
  1262. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1263. return false;
  1264. }
  1265. return true;
  1266. }
  1267. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1268. enum pipe pipe, u32 val)
  1269. {
  1270. if ((val & ADPA_DAC_ENABLE) == 0)
  1271. return false;
  1272. if (HAS_PCH_CPT(dev_priv->dev)) {
  1273. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1274. return false;
  1275. } else {
  1276. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1277. return false;
  1278. }
  1279. return true;
  1280. }
  1281. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1282. enum pipe pipe, int reg, u32 port_sel)
  1283. {
  1284. u32 val = I915_READ(reg);
  1285. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1286. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1287. reg, pipe_name(pipe));
  1288. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1289. && (val & DP_PIPEB_SELECT),
  1290. "IBX PCH dp port still using transcoder B\n");
  1291. }
  1292. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1293. enum pipe pipe, int reg)
  1294. {
  1295. u32 val = I915_READ(reg);
  1296. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1297. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1298. reg, pipe_name(pipe));
  1299. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1300. && (val & SDVO_PIPE_B_SELECT),
  1301. "IBX PCH hdmi port still using transcoder B\n");
  1302. }
  1303. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1304. enum pipe pipe)
  1305. {
  1306. int reg;
  1307. u32 val;
  1308. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1309. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1310. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1311. reg = PCH_ADPA;
  1312. val = I915_READ(reg);
  1313. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1314. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1315. pipe_name(pipe));
  1316. reg = PCH_LVDS;
  1317. val = I915_READ(reg);
  1318. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1319. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1320. pipe_name(pipe));
  1321. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1322. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1323. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1324. }
  1325. static void intel_init_dpio(struct drm_device *dev)
  1326. {
  1327. struct drm_i915_private *dev_priv = dev->dev_private;
  1328. if (!IS_VALLEYVIEW(dev))
  1329. return;
  1330. /*
  1331. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1332. * CHV x1 PHY (DP/HDMI D)
  1333. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1334. */
  1335. if (IS_CHERRYVIEW(dev)) {
  1336. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1337. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1338. } else {
  1339. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1340. }
  1341. }
  1342. static void vlv_enable_pll(struct intel_crtc *crtc)
  1343. {
  1344. struct drm_device *dev = crtc->base.dev;
  1345. struct drm_i915_private *dev_priv = dev->dev_private;
  1346. int reg = DPLL(crtc->pipe);
  1347. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1348. assert_pipe_disabled(dev_priv, crtc->pipe);
  1349. /* No really, not for ILK+ */
  1350. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1351. /* PLL is protected by panel, make sure we can write it */
  1352. if (IS_MOBILE(dev_priv->dev))
  1353. assert_panel_unlocked(dev_priv, crtc->pipe);
  1354. I915_WRITE(reg, dpll);
  1355. POSTING_READ(reg);
  1356. udelay(150);
  1357. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1358. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1359. I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
  1360. POSTING_READ(DPLL_MD(crtc->pipe));
  1361. /* We do this three times for luck */
  1362. I915_WRITE(reg, dpll);
  1363. POSTING_READ(reg);
  1364. udelay(150); /* wait for warmup */
  1365. I915_WRITE(reg, dpll);
  1366. POSTING_READ(reg);
  1367. udelay(150); /* wait for warmup */
  1368. I915_WRITE(reg, dpll);
  1369. POSTING_READ(reg);
  1370. udelay(150); /* wait for warmup */
  1371. }
  1372. static void chv_enable_pll(struct intel_crtc *crtc)
  1373. {
  1374. struct drm_device *dev = crtc->base.dev;
  1375. struct drm_i915_private *dev_priv = dev->dev_private;
  1376. int pipe = crtc->pipe;
  1377. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1378. u32 tmp;
  1379. assert_pipe_disabled(dev_priv, crtc->pipe);
  1380. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1381. mutex_lock(&dev_priv->dpio_lock);
  1382. /* Enable back the 10bit clock to display controller */
  1383. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1384. tmp |= DPIO_DCLKP_EN;
  1385. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1386. /*
  1387. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1388. */
  1389. udelay(1);
  1390. /* Enable PLL */
  1391. I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
  1392. /* Check PLL is locked */
  1393. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1394. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1395. /* not sure when this should be written */
  1396. I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
  1397. POSTING_READ(DPLL_MD(pipe));
  1398. mutex_unlock(&dev_priv->dpio_lock);
  1399. }
  1400. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1401. {
  1402. struct drm_device *dev = crtc->base.dev;
  1403. struct drm_i915_private *dev_priv = dev->dev_private;
  1404. int reg = DPLL(crtc->pipe);
  1405. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1406. assert_pipe_disabled(dev_priv, crtc->pipe);
  1407. /* No really, not for ILK+ */
  1408. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1409. /* PLL is protected by panel, make sure we can write it */
  1410. if (IS_MOBILE(dev) && !IS_I830(dev))
  1411. assert_panel_unlocked(dev_priv, crtc->pipe);
  1412. I915_WRITE(reg, dpll);
  1413. /* Wait for the clocks to stabilize. */
  1414. POSTING_READ(reg);
  1415. udelay(150);
  1416. if (INTEL_INFO(dev)->gen >= 4) {
  1417. I915_WRITE(DPLL_MD(crtc->pipe),
  1418. crtc->config.dpll_hw_state.dpll_md);
  1419. } else {
  1420. /* The pixel multiplier can only be updated once the
  1421. * DPLL is enabled and the clocks are stable.
  1422. *
  1423. * So write it again.
  1424. */
  1425. I915_WRITE(reg, dpll);
  1426. }
  1427. /* We do this three times for luck */
  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. I915_WRITE(reg, dpll);
  1435. POSTING_READ(reg);
  1436. udelay(150); /* wait for warmup */
  1437. }
  1438. /**
  1439. * i9xx_disable_pll - disable a PLL
  1440. * @dev_priv: i915 private structure
  1441. * @pipe: pipe PLL to disable
  1442. *
  1443. * Disable the PLL for @pipe, making sure the pipe is off first.
  1444. *
  1445. * Note! This is for pre-ILK only.
  1446. */
  1447. static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1448. {
  1449. /* Don't disable pipe A or pipe A PLLs if needed */
  1450. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1451. return;
  1452. /* Make sure the pipe isn't still relying on us */
  1453. assert_pipe_disabled(dev_priv, pipe);
  1454. I915_WRITE(DPLL(pipe), 0);
  1455. POSTING_READ(DPLL(pipe));
  1456. }
  1457. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1458. {
  1459. u32 val = 0;
  1460. /* Make sure the pipe isn't still relying on us */
  1461. assert_pipe_disabled(dev_priv, pipe);
  1462. /*
  1463. * Leave integrated clock source and reference clock enabled for pipe B.
  1464. * The latter is needed for VGA hotplug / manual detection.
  1465. */
  1466. if (pipe == PIPE_B)
  1467. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1468. I915_WRITE(DPLL(pipe), val);
  1469. POSTING_READ(DPLL(pipe));
  1470. }
  1471. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1472. {
  1473. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1474. u32 val;
  1475. /* Make sure the pipe isn't still relying on us */
  1476. assert_pipe_disabled(dev_priv, pipe);
  1477. /* Set PLL en = 0 */
  1478. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1479. if (pipe != PIPE_A)
  1480. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1481. I915_WRITE(DPLL(pipe), val);
  1482. POSTING_READ(DPLL(pipe));
  1483. mutex_lock(&dev_priv->dpio_lock);
  1484. /* Disable 10bit clock to display controller */
  1485. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1486. val &= ~DPIO_DCLKP_EN;
  1487. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1488. /* disable left/right clock distribution */
  1489. if (pipe != PIPE_B) {
  1490. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1491. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1492. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1493. } else {
  1494. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1495. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1496. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1497. }
  1498. mutex_unlock(&dev_priv->dpio_lock);
  1499. }
  1500. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1501. struct intel_digital_port *dport)
  1502. {
  1503. u32 port_mask;
  1504. int dpll_reg;
  1505. switch (dport->port) {
  1506. case PORT_B:
  1507. port_mask = DPLL_PORTB_READY_MASK;
  1508. dpll_reg = DPLL(0);
  1509. break;
  1510. case PORT_C:
  1511. port_mask = DPLL_PORTC_READY_MASK;
  1512. dpll_reg = DPLL(0);
  1513. break;
  1514. case PORT_D:
  1515. port_mask = DPLL_PORTD_READY_MASK;
  1516. dpll_reg = DPIO_PHY_STATUS;
  1517. break;
  1518. default:
  1519. BUG();
  1520. }
  1521. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1522. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1523. port_name(dport->port), I915_READ(dpll_reg));
  1524. }
  1525. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1526. {
  1527. struct drm_device *dev = crtc->base.dev;
  1528. struct drm_i915_private *dev_priv = dev->dev_private;
  1529. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1530. if (WARN_ON(pll == NULL))
  1531. return;
  1532. WARN_ON(!pll->refcount);
  1533. if (pll->active == 0) {
  1534. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1535. WARN_ON(pll->on);
  1536. assert_shared_dpll_disabled(dev_priv, pll);
  1537. pll->mode_set(dev_priv, pll);
  1538. }
  1539. }
  1540. /**
  1541. * intel_enable_shared_dpll - enable PCH PLL
  1542. * @dev_priv: i915 private structure
  1543. * @pipe: pipe PLL to enable
  1544. *
  1545. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1546. * drives the transcoder clock.
  1547. */
  1548. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1549. {
  1550. struct drm_device *dev = crtc->base.dev;
  1551. struct drm_i915_private *dev_priv = dev->dev_private;
  1552. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1553. if (WARN_ON(pll == NULL))
  1554. return;
  1555. if (WARN_ON(pll->refcount == 0))
  1556. return;
  1557. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1558. pll->name, pll->active, pll->on,
  1559. crtc->base.base.id);
  1560. if (pll->active++) {
  1561. WARN_ON(!pll->on);
  1562. assert_shared_dpll_enabled(dev_priv, pll);
  1563. return;
  1564. }
  1565. WARN_ON(pll->on);
  1566. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1567. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1568. pll->enable(dev_priv, pll);
  1569. pll->on = true;
  1570. }
  1571. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1572. {
  1573. struct drm_device *dev = crtc->base.dev;
  1574. struct drm_i915_private *dev_priv = dev->dev_private;
  1575. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1576. /* PCH only available on ILK+ */
  1577. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1578. if (WARN_ON(pll == NULL))
  1579. return;
  1580. if (WARN_ON(pll->refcount == 0))
  1581. return;
  1582. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1583. pll->name, pll->active, pll->on,
  1584. crtc->base.base.id);
  1585. if (WARN_ON(pll->active == 0)) {
  1586. assert_shared_dpll_disabled(dev_priv, pll);
  1587. return;
  1588. }
  1589. assert_shared_dpll_enabled(dev_priv, pll);
  1590. WARN_ON(!pll->on);
  1591. if (--pll->active)
  1592. return;
  1593. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1594. pll->disable(dev_priv, pll);
  1595. pll->on = false;
  1596. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1597. }
  1598. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1599. enum pipe pipe)
  1600. {
  1601. struct drm_device *dev = dev_priv->dev;
  1602. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1603. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1604. uint32_t reg, val, pipeconf_val;
  1605. /* PCH only available on ILK+ */
  1606. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1607. /* Make sure PCH DPLL is enabled */
  1608. assert_shared_dpll_enabled(dev_priv,
  1609. intel_crtc_to_shared_dpll(intel_crtc));
  1610. /* FDI must be feeding us bits for PCH ports */
  1611. assert_fdi_tx_enabled(dev_priv, pipe);
  1612. assert_fdi_rx_enabled(dev_priv, pipe);
  1613. if (HAS_PCH_CPT(dev)) {
  1614. /* Workaround: Set the timing override bit before enabling the
  1615. * pch transcoder. */
  1616. reg = TRANS_CHICKEN2(pipe);
  1617. val = I915_READ(reg);
  1618. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1619. I915_WRITE(reg, val);
  1620. }
  1621. reg = PCH_TRANSCONF(pipe);
  1622. val = I915_READ(reg);
  1623. pipeconf_val = I915_READ(PIPECONF(pipe));
  1624. if (HAS_PCH_IBX(dev_priv->dev)) {
  1625. /*
  1626. * make the BPC in transcoder be consistent with
  1627. * that in pipeconf reg.
  1628. */
  1629. val &= ~PIPECONF_BPC_MASK;
  1630. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1631. }
  1632. val &= ~TRANS_INTERLACE_MASK;
  1633. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1634. if (HAS_PCH_IBX(dev_priv->dev) &&
  1635. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1636. val |= TRANS_LEGACY_INTERLACED_ILK;
  1637. else
  1638. val |= TRANS_INTERLACED;
  1639. else
  1640. val |= TRANS_PROGRESSIVE;
  1641. I915_WRITE(reg, val | TRANS_ENABLE);
  1642. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1643. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1644. }
  1645. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1646. enum transcoder cpu_transcoder)
  1647. {
  1648. u32 val, pipeconf_val;
  1649. /* PCH only available on ILK+ */
  1650. BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
  1651. /* FDI must be feeding us bits for PCH ports */
  1652. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1653. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1654. /* Workaround: set timing override bit. */
  1655. val = I915_READ(_TRANSA_CHICKEN2);
  1656. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1657. I915_WRITE(_TRANSA_CHICKEN2, val);
  1658. val = TRANS_ENABLE;
  1659. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1660. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1661. PIPECONF_INTERLACED_ILK)
  1662. val |= TRANS_INTERLACED;
  1663. else
  1664. val |= TRANS_PROGRESSIVE;
  1665. I915_WRITE(LPT_TRANSCONF, val);
  1666. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1667. DRM_ERROR("Failed to enable PCH transcoder\n");
  1668. }
  1669. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1670. enum pipe pipe)
  1671. {
  1672. struct drm_device *dev = dev_priv->dev;
  1673. uint32_t reg, val;
  1674. /* FDI relies on the transcoder */
  1675. assert_fdi_tx_disabled(dev_priv, pipe);
  1676. assert_fdi_rx_disabled(dev_priv, pipe);
  1677. /* Ports must be off as well */
  1678. assert_pch_ports_disabled(dev_priv, pipe);
  1679. reg = PCH_TRANSCONF(pipe);
  1680. val = I915_READ(reg);
  1681. val &= ~TRANS_ENABLE;
  1682. I915_WRITE(reg, val);
  1683. /* wait for PCH transcoder off, transcoder state */
  1684. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1685. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1686. if (!HAS_PCH_IBX(dev)) {
  1687. /* Workaround: Clear the timing override chicken bit again. */
  1688. reg = TRANS_CHICKEN2(pipe);
  1689. val = I915_READ(reg);
  1690. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1691. I915_WRITE(reg, val);
  1692. }
  1693. }
  1694. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1695. {
  1696. u32 val;
  1697. val = I915_READ(LPT_TRANSCONF);
  1698. val &= ~TRANS_ENABLE;
  1699. I915_WRITE(LPT_TRANSCONF, val);
  1700. /* wait for PCH transcoder off, transcoder state */
  1701. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1702. DRM_ERROR("Failed to disable PCH transcoder\n");
  1703. /* Workaround: clear timing override bit. */
  1704. val = I915_READ(_TRANSA_CHICKEN2);
  1705. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1706. I915_WRITE(_TRANSA_CHICKEN2, val);
  1707. }
  1708. /**
  1709. * intel_enable_pipe - enable a pipe, asserting requirements
  1710. * @crtc: crtc responsible for the pipe
  1711. *
  1712. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1713. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1714. */
  1715. static void intel_enable_pipe(struct intel_crtc *crtc)
  1716. {
  1717. struct drm_device *dev = crtc->base.dev;
  1718. struct drm_i915_private *dev_priv = dev->dev_private;
  1719. enum pipe pipe = crtc->pipe;
  1720. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1721. pipe);
  1722. enum pipe pch_transcoder;
  1723. int reg;
  1724. u32 val;
  1725. assert_planes_disabled(dev_priv, pipe);
  1726. assert_cursor_disabled(dev_priv, pipe);
  1727. assert_sprites_disabled(dev_priv, pipe);
  1728. if (HAS_PCH_LPT(dev_priv->dev))
  1729. pch_transcoder = TRANSCODER_A;
  1730. else
  1731. pch_transcoder = pipe;
  1732. /*
  1733. * A pipe without a PLL won't actually be able to drive bits from
  1734. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1735. * need the check.
  1736. */
  1737. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1738. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
  1739. assert_dsi_pll_enabled(dev_priv);
  1740. else
  1741. assert_pll_enabled(dev_priv, pipe);
  1742. else {
  1743. if (crtc->config.has_pch_encoder) {
  1744. /* if driving the PCH, we need FDI enabled */
  1745. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1746. assert_fdi_tx_pll_enabled(dev_priv,
  1747. (enum pipe) cpu_transcoder);
  1748. }
  1749. /* FIXME: assert CPU port conditions for SNB+ */
  1750. }
  1751. reg = PIPECONF(cpu_transcoder);
  1752. val = I915_READ(reg);
  1753. if (val & PIPECONF_ENABLE) {
  1754. WARN_ON(!(pipe == PIPE_A &&
  1755. dev_priv->quirks & QUIRK_PIPEA_FORCE));
  1756. return;
  1757. }
  1758. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1759. POSTING_READ(reg);
  1760. }
  1761. /**
  1762. * intel_disable_pipe - disable a pipe, asserting requirements
  1763. * @dev_priv: i915 private structure
  1764. * @pipe: pipe to disable
  1765. *
  1766. * Disable @pipe, making sure that various hardware specific requirements
  1767. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1768. *
  1769. * @pipe should be %PIPE_A or %PIPE_B.
  1770. *
  1771. * Will wait until the pipe has shut down before returning.
  1772. */
  1773. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1774. enum pipe pipe)
  1775. {
  1776. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1777. pipe);
  1778. int reg;
  1779. u32 val;
  1780. /*
  1781. * Make sure planes won't keep trying to pump pixels to us,
  1782. * or we might hang the display.
  1783. */
  1784. assert_planes_disabled(dev_priv, pipe);
  1785. assert_cursor_disabled(dev_priv, pipe);
  1786. assert_sprites_disabled(dev_priv, pipe);
  1787. /* Don't disable pipe A or pipe A PLLs if needed */
  1788. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1789. return;
  1790. reg = PIPECONF(cpu_transcoder);
  1791. val = I915_READ(reg);
  1792. if ((val & PIPECONF_ENABLE) == 0)
  1793. return;
  1794. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1795. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1796. }
  1797. /*
  1798. * Plane regs are double buffered, going from enabled->disabled needs a
  1799. * trigger in order to latch. The display address reg provides this.
  1800. */
  1801. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1802. enum plane plane)
  1803. {
  1804. struct drm_device *dev = dev_priv->dev;
  1805. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1806. I915_WRITE(reg, I915_READ(reg));
  1807. POSTING_READ(reg);
  1808. }
  1809. /**
  1810. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1811. * @plane: plane to be enabled
  1812. * @crtc: crtc for the plane
  1813. *
  1814. * Enable @plane on @crtc, making sure that the pipe is running first.
  1815. */
  1816. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1817. struct drm_crtc *crtc)
  1818. {
  1819. struct drm_device *dev = plane->dev;
  1820. struct drm_i915_private *dev_priv = dev->dev_private;
  1821. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1822. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1823. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1824. if (intel_crtc->primary_enabled)
  1825. return;
  1826. intel_crtc->primary_enabled = true;
  1827. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1828. crtc->x, crtc->y);
  1829. /*
  1830. * BDW signals flip done immediately if the plane
  1831. * is disabled, even if the plane enable is already
  1832. * armed to occur at the next vblank :(
  1833. */
  1834. if (IS_BROADWELL(dev))
  1835. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1836. }
  1837. /**
  1838. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1839. * @plane: plane to be disabled
  1840. * @crtc: crtc for the plane
  1841. *
  1842. * Disable @plane on @crtc, making sure that the pipe is running first.
  1843. */
  1844. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1845. struct drm_crtc *crtc)
  1846. {
  1847. struct drm_device *dev = plane->dev;
  1848. struct drm_i915_private *dev_priv = dev->dev_private;
  1849. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1850. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1851. if (!intel_crtc->primary_enabled)
  1852. return;
  1853. intel_crtc->primary_enabled = false;
  1854. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1855. crtc->x, crtc->y);
  1856. }
  1857. static bool need_vtd_wa(struct drm_device *dev)
  1858. {
  1859. #ifdef CONFIG_INTEL_IOMMU
  1860. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1861. return true;
  1862. #endif
  1863. return false;
  1864. }
  1865. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1866. {
  1867. int tile_height;
  1868. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1869. return ALIGN(height, tile_height);
  1870. }
  1871. int
  1872. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1873. struct drm_i915_gem_object *obj,
  1874. struct intel_engine_cs *pipelined)
  1875. {
  1876. struct drm_i915_private *dev_priv = dev->dev_private;
  1877. u32 alignment;
  1878. int ret;
  1879. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1880. switch (obj->tiling_mode) {
  1881. case I915_TILING_NONE:
  1882. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1883. alignment = 128 * 1024;
  1884. else if (INTEL_INFO(dev)->gen >= 4)
  1885. alignment = 4 * 1024;
  1886. else
  1887. alignment = 64 * 1024;
  1888. break;
  1889. case I915_TILING_X:
  1890. /* pin() will align the object as required by fence */
  1891. alignment = 0;
  1892. break;
  1893. case I915_TILING_Y:
  1894. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1895. return -EINVAL;
  1896. default:
  1897. BUG();
  1898. }
  1899. /* Note that the w/a also requires 64 PTE of padding following the
  1900. * bo. We currently fill all unused PTE with the shadow page and so
  1901. * we should always have valid PTE following the scanout preventing
  1902. * the VT-d warning.
  1903. */
  1904. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1905. alignment = 256 * 1024;
  1906. dev_priv->mm.interruptible = false;
  1907. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1908. if (ret)
  1909. goto err_interruptible;
  1910. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1911. * fence, whereas 965+ only requires a fence if using
  1912. * framebuffer compression. For simplicity, we always install
  1913. * a fence as the cost is not that onerous.
  1914. */
  1915. ret = i915_gem_object_get_fence(obj);
  1916. if (ret)
  1917. goto err_unpin;
  1918. i915_gem_object_pin_fence(obj);
  1919. dev_priv->mm.interruptible = true;
  1920. return 0;
  1921. err_unpin:
  1922. i915_gem_object_unpin_from_display_plane(obj);
  1923. err_interruptible:
  1924. dev_priv->mm.interruptible = true;
  1925. return ret;
  1926. }
  1927. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1928. {
  1929. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1930. i915_gem_object_unpin_fence(obj);
  1931. i915_gem_object_unpin_from_display_plane(obj);
  1932. }
  1933. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1934. * is assumed to be a power-of-two. */
  1935. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1936. unsigned int tiling_mode,
  1937. unsigned int cpp,
  1938. unsigned int pitch)
  1939. {
  1940. if (tiling_mode != I915_TILING_NONE) {
  1941. unsigned int tile_rows, tiles;
  1942. tile_rows = *y / 8;
  1943. *y %= 8;
  1944. tiles = *x / (512/cpp);
  1945. *x %= 512/cpp;
  1946. return tile_rows * pitch * 8 + tiles * 4096;
  1947. } else {
  1948. unsigned int offset;
  1949. offset = *y * pitch + *x * cpp;
  1950. *y = 0;
  1951. *x = (offset & 4095) / cpp;
  1952. return offset & -4096;
  1953. }
  1954. }
  1955. int intel_format_to_fourcc(int format)
  1956. {
  1957. switch (format) {
  1958. case DISPPLANE_8BPP:
  1959. return DRM_FORMAT_C8;
  1960. case DISPPLANE_BGRX555:
  1961. return DRM_FORMAT_XRGB1555;
  1962. case DISPPLANE_BGRX565:
  1963. return DRM_FORMAT_RGB565;
  1964. default:
  1965. case DISPPLANE_BGRX888:
  1966. return DRM_FORMAT_XRGB8888;
  1967. case DISPPLANE_RGBX888:
  1968. return DRM_FORMAT_XBGR8888;
  1969. case DISPPLANE_BGRX101010:
  1970. return DRM_FORMAT_XRGB2101010;
  1971. case DISPPLANE_RGBX101010:
  1972. return DRM_FORMAT_XBGR2101010;
  1973. }
  1974. }
  1975. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  1976. struct intel_plane_config *plane_config)
  1977. {
  1978. struct drm_device *dev = crtc->base.dev;
  1979. struct drm_i915_gem_object *obj = NULL;
  1980. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  1981. u32 base = plane_config->base;
  1982. if (plane_config->size == 0)
  1983. return false;
  1984. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  1985. plane_config->size);
  1986. if (!obj)
  1987. return false;
  1988. if (plane_config->tiled) {
  1989. obj->tiling_mode = I915_TILING_X;
  1990. obj->stride = crtc->base.primary->fb->pitches[0];
  1991. }
  1992. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  1993. mode_cmd.width = crtc->base.primary->fb->width;
  1994. mode_cmd.height = crtc->base.primary->fb->height;
  1995. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  1996. mutex_lock(&dev->struct_mutex);
  1997. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  1998. &mode_cmd, obj)) {
  1999. DRM_DEBUG_KMS("intel fb init failed\n");
  2000. goto out_unref_obj;
  2001. }
  2002. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2003. mutex_unlock(&dev->struct_mutex);
  2004. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2005. return true;
  2006. out_unref_obj:
  2007. drm_gem_object_unreference(&obj->base);
  2008. mutex_unlock(&dev->struct_mutex);
  2009. return false;
  2010. }
  2011. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2012. struct intel_plane_config *plane_config)
  2013. {
  2014. struct drm_device *dev = intel_crtc->base.dev;
  2015. struct drm_crtc *c;
  2016. struct intel_crtc *i;
  2017. struct drm_i915_gem_object *obj;
  2018. if (!intel_crtc->base.primary->fb)
  2019. return;
  2020. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2021. return;
  2022. kfree(intel_crtc->base.primary->fb);
  2023. intel_crtc->base.primary->fb = NULL;
  2024. /*
  2025. * Failed to alloc the obj, check to see if we should share
  2026. * an fb with another CRTC instead
  2027. */
  2028. for_each_crtc(dev, c) {
  2029. i = to_intel_crtc(c);
  2030. if (c == &intel_crtc->base)
  2031. continue;
  2032. if (!i->active)
  2033. continue;
  2034. obj = intel_fb_obj(c->primary->fb);
  2035. if (obj == NULL)
  2036. continue;
  2037. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2038. drm_framebuffer_reference(c->primary->fb);
  2039. intel_crtc->base.primary->fb = c->primary->fb;
  2040. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2041. break;
  2042. }
  2043. }
  2044. }
  2045. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2046. struct drm_framebuffer *fb,
  2047. int x, int y)
  2048. {
  2049. struct drm_device *dev = crtc->dev;
  2050. struct drm_i915_private *dev_priv = dev->dev_private;
  2051. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2052. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2053. int plane = intel_crtc->plane;
  2054. unsigned long linear_offset;
  2055. u32 dspcntr;
  2056. u32 reg = DSPCNTR(plane);
  2057. int pixel_size;
  2058. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2059. if (!intel_crtc->primary_enabled) {
  2060. I915_WRITE(reg, 0);
  2061. if (INTEL_INFO(dev)->gen >= 4)
  2062. I915_WRITE(DSPSURF(plane), 0);
  2063. else
  2064. I915_WRITE(DSPADDR(plane), 0);
  2065. POSTING_READ(reg);
  2066. return;
  2067. }
  2068. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2069. dspcntr |= DISPLAY_PLANE_ENABLE;
  2070. if (INTEL_INFO(dev)->gen < 4) {
  2071. if (intel_crtc->pipe == PIPE_B)
  2072. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2073. /* pipesrc and dspsize control the size that is scaled from,
  2074. * which should always be the user's requested size.
  2075. */
  2076. I915_WRITE(DSPSIZE(plane),
  2077. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  2078. (intel_crtc->config.pipe_src_w - 1));
  2079. I915_WRITE(DSPPOS(plane), 0);
  2080. }
  2081. switch (fb->pixel_format) {
  2082. case DRM_FORMAT_C8:
  2083. dspcntr |= DISPPLANE_8BPP;
  2084. break;
  2085. case DRM_FORMAT_XRGB1555:
  2086. case DRM_FORMAT_ARGB1555:
  2087. dspcntr |= DISPPLANE_BGRX555;
  2088. break;
  2089. case DRM_FORMAT_RGB565:
  2090. dspcntr |= DISPPLANE_BGRX565;
  2091. break;
  2092. case DRM_FORMAT_XRGB8888:
  2093. case DRM_FORMAT_ARGB8888:
  2094. dspcntr |= DISPPLANE_BGRX888;
  2095. break;
  2096. case DRM_FORMAT_XBGR8888:
  2097. case DRM_FORMAT_ABGR8888:
  2098. dspcntr |= DISPPLANE_RGBX888;
  2099. break;
  2100. case DRM_FORMAT_XRGB2101010:
  2101. case DRM_FORMAT_ARGB2101010:
  2102. dspcntr |= DISPPLANE_BGRX101010;
  2103. break;
  2104. case DRM_FORMAT_XBGR2101010:
  2105. case DRM_FORMAT_ABGR2101010:
  2106. dspcntr |= DISPPLANE_RGBX101010;
  2107. break;
  2108. default:
  2109. BUG();
  2110. }
  2111. if (INTEL_INFO(dev)->gen >= 4 &&
  2112. obj->tiling_mode != I915_TILING_NONE)
  2113. dspcntr |= DISPPLANE_TILED;
  2114. if (IS_G4X(dev))
  2115. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2116. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  2117. if (INTEL_INFO(dev)->gen >= 4) {
  2118. intel_crtc->dspaddr_offset =
  2119. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2120. fb->bits_per_pixel / 8,
  2121. fb->pitches[0]);
  2122. linear_offset -= intel_crtc->dspaddr_offset;
  2123. } else {
  2124. intel_crtc->dspaddr_offset = linear_offset;
  2125. }
  2126. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2127. dspcntr |= DISPPLANE_ROTATE_180;
  2128. x += (intel_crtc->config.pipe_src_w - 1);
  2129. y += (intel_crtc->config.pipe_src_h - 1);
  2130. /* Finding the last pixel of the last line of the display
  2131. data and adding to linear_offset*/
  2132. linear_offset +=
  2133. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2134. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2135. }
  2136. I915_WRITE(reg, dspcntr);
  2137. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2138. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2139. fb->pitches[0]);
  2140. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2141. if (INTEL_INFO(dev)->gen >= 4) {
  2142. I915_WRITE(DSPSURF(plane),
  2143. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2144. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2145. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2146. } else
  2147. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2148. POSTING_READ(reg);
  2149. }
  2150. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2151. struct drm_framebuffer *fb,
  2152. int x, int y)
  2153. {
  2154. struct drm_device *dev = crtc->dev;
  2155. struct drm_i915_private *dev_priv = dev->dev_private;
  2156. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2157. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2158. int plane = intel_crtc->plane;
  2159. unsigned long linear_offset;
  2160. u32 dspcntr;
  2161. u32 reg = DSPCNTR(plane);
  2162. int pixel_size;
  2163. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2164. if (!intel_crtc->primary_enabled) {
  2165. I915_WRITE(reg, 0);
  2166. I915_WRITE(DSPSURF(plane), 0);
  2167. POSTING_READ(reg);
  2168. return;
  2169. }
  2170. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2171. dspcntr |= DISPLAY_PLANE_ENABLE;
  2172. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2173. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2174. switch (fb->pixel_format) {
  2175. case DRM_FORMAT_C8:
  2176. dspcntr |= DISPPLANE_8BPP;
  2177. break;
  2178. case DRM_FORMAT_RGB565:
  2179. dspcntr |= DISPPLANE_BGRX565;
  2180. break;
  2181. case DRM_FORMAT_XRGB8888:
  2182. case DRM_FORMAT_ARGB8888:
  2183. dspcntr |= DISPPLANE_BGRX888;
  2184. break;
  2185. case DRM_FORMAT_XBGR8888:
  2186. case DRM_FORMAT_ABGR8888:
  2187. dspcntr |= DISPPLANE_RGBX888;
  2188. break;
  2189. case DRM_FORMAT_XRGB2101010:
  2190. case DRM_FORMAT_ARGB2101010:
  2191. dspcntr |= DISPPLANE_BGRX101010;
  2192. break;
  2193. case DRM_FORMAT_XBGR2101010:
  2194. case DRM_FORMAT_ABGR2101010:
  2195. dspcntr |= DISPPLANE_RGBX101010;
  2196. break;
  2197. default:
  2198. BUG();
  2199. }
  2200. if (obj->tiling_mode != I915_TILING_NONE)
  2201. dspcntr |= DISPPLANE_TILED;
  2202. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2203. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2204. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  2205. intel_crtc->dspaddr_offset =
  2206. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2207. fb->bits_per_pixel / 8,
  2208. fb->pitches[0]);
  2209. linear_offset -= intel_crtc->dspaddr_offset;
  2210. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2211. dspcntr |= DISPPLANE_ROTATE_180;
  2212. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2213. x += (intel_crtc->config.pipe_src_w - 1);
  2214. y += (intel_crtc->config.pipe_src_h - 1);
  2215. /* Finding the last pixel of the last line of the display
  2216. data and adding to linear_offset*/
  2217. linear_offset +=
  2218. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2219. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2220. }
  2221. }
  2222. I915_WRITE(reg, dspcntr);
  2223. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2224. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2225. fb->pitches[0]);
  2226. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2227. I915_WRITE(DSPSURF(plane),
  2228. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2229. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2230. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2231. } else {
  2232. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2233. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2234. }
  2235. POSTING_READ(reg);
  2236. }
  2237. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2238. static int
  2239. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2240. int x, int y, enum mode_set_atomic state)
  2241. {
  2242. struct drm_device *dev = crtc->dev;
  2243. struct drm_i915_private *dev_priv = dev->dev_private;
  2244. if (dev_priv->display.disable_fbc)
  2245. dev_priv->display.disable_fbc(dev);
  2246. intel_increase_pllclock(dev, to_intel_crtc(crtc)->pipe);
  2247. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2248. return 0;
  2249. }
  2250. void intel_display_handle_reset(struct drm_device *dev)
  2251. {
  2252. struct drm_i915_private *dev_priv = dev->dev_private;
  2253. struct drm_crtc *crtc;
  2254. /*
  2255. * Flips in the rings have been nuked by the reset,
  2256. * so complete all pending flips so that user space
  2257. * will get its events and not get stuck.
  2258. *
  2259. * Also update the base address of all primary
  2260. * planes to the the last fb to make sure we're
  2261. * showing the correct fb after a reset.
  2262. *
  2263. * Need to make two loops over the crtcs so that we
  2264. * don't try to grab a crtc mutex before the
  2265. * pending_flip_queue really got woken up.
  2266. */
  2267. for_each_crtc(dev, crtc) {
  2268. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2269. enum plane plane = intel_crtc->plane;
  2270. intel_prepare_page_flip(dev, plane);
  2271. intel_finish_page_flip_plane(dev, plane);
  2272. }
  2273. for_each_crtc(dev, crtc) {
  2274. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2275. drm_modeset_lock(&crtc->mutex, NULL);
  2276. /*
  2277. * FIXME: Once we have proper support for primary planes (and
  2278. * disabling them without disabling the entire crtc) allow again
  2279. * a NULL crtc->primary->fb.
  2280. */
  2281. if (intel_crtc->active && crtc->primary->fb)
  2282. dev_priv->display.update_primary_plane(crtc,
  2283. crtc->primary->fb,
  2284. crtc->x,
  2285. crtc->y);
  2286. drm_modeset_unlock(&crtc->mutex);
  2287. }
  2288. }
  2289. static int
  2290. intel_finish_fb(struct drm_framebuffer *old_fb)
  2291. {
  2292. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2293. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2294. bool was_interruptible = dev_priv->mm.interruptible;
  2295. int ret;
  2296. /* Big Hammer, we also need to ensure that any pending
  2297. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2298. * current scanout is retired before unpinning the old
  2299. * framebuffer.
  2300. *
  2301. * This should only fail upon a hung GPU, in which case we
  2302. * can safely continue.
  2303. */
  2304. dev_priv->mm.interruptible = false;
  2305. ret = i915_gem_object_finish_gpu(obj);
  2306. dev_priv->mm.interruptible = was_interruptible;
  2307. return ret;
  2308. }
  2309. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2310. {
  2311. struct drm_device *dev = crtc->dev;
  2312. struct drm_i915_private *dev_priv = dev->dev_private;
  2313. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2314. unsigned long flags;
  2315. bool pending;
  2316. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2317. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2318. return false;
  2319. spin_lock_irqsave(&dev->event_lock, flags);
  2320. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2321. spin_unlock_irqrestore(&dev->event_lock, flags);
  2322. return pending;
  2323. }
  2324. static int
  2325. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  2326. struct drm_framebuffer *fb)
  2327. {
  2328. struct drm_device *dev = crtc->dev;
  2329. struct drm_i915_private *dev_priv = dev->dev_private;
  2330. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2331. enum pipe pipe = intel_crtc->pipe;
  2332. struct drm_framebuffer *old_fb = crtc->primary->fb;
  2333. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2334. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
  2335. int ret;
  2336. if (intel_crtc_has_pending_flip(crtc)) {
  2337. DRM_ERROR("pipe is still busy with an old pageflip\n");
  2338. return -EBUSY;
  2339. }
  2340. /* no fb bound */
  2341. if (!fb) {
  2342. DRM_ERROR("No FB bound\n");
  2343. return 0;
  2344. }
  2345. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  2346. DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
  2347. plane_name(intel_crtc->plane),
  2348. INTEL_INFO(dev)->num_pipes);
  2349. return -EINVAL;
  2350. }
  2351. mutex_lock(&dev->struct_mutex);
  2352. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  2353. if (ret == 0)
  2354. i915_gem_track_fb(old_obj, obj,
  2355. INTEL_FRONTBUFFER_PRIMARY(pipe));
  2356. mutex_unlock(&dev->struct_mutex);
  2357. if (ret != 0) {
  2358. DRM_ERROR("pin & fence failed\n");
  2359. return ret;
  2360. }
  2361. /*
  2362. * Update pipe size and adjust fitter if needed: the reason for this is
  2363. * that in compute_mode_changes we check the native mode (not the pfit
  2364. * mode) to see if we can flip rather than do a full mode set. In the
  2365. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2366. * pfit state, we'll end up with a big fb scanned out into the wrong
  2367. * sized surface.
  2368. *
  2369. * To fix this properly, we need to hoist the checks up into
  2370. * compute_mode_changes (or above), check the actual pfit state and
  2371. * whether the platform allows pfit disable with pipe active, and only
  2372. * then update the pipesrc and pfit state, even on the flip path.
  2373. */
  2374. if (i915.fastboot) {
  2375. const struct drm_display_mode *adjusted_mode =
  2376. &intel_crtc->config.adjusted_mode;
  2377. I915_WRITE(PIPESRC(intel_crtc->pipe),
  2378. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2379. (adjusted_mode->crtc_vdisplay - 1));
  2380. if (!intel_crtc->config.pch_pfit.enabled &&
  2381. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2382. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2383. I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
  2384. I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
  2385. I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
  2386. }
  2387. intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2388. intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2389. }
  2390. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2391. if (intel_crtc->active)
  2392. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  2393. crtc->primary->fb = fb;
  2394. crtc->x = x;
  2395. crtc->y = y;
  2396. if (old_fb) {
  2397. if (intel_crtc->active && old_fb != fb)
  2398. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2399. mutex_lock(&dev->struct_mutex);
  2400. intel_unpin_fb_obj(old_obj);
  2401. mutex_unlock(&dev->struct_mutex);
  2402. }
  2403. mutex_lock(&dev->struct_mutex);
  2404. intel_update_fbc(dev);
  2405. mutex_unlock(&dev->struct_mutex);
  2406. return 0;
  2407. }
  2408. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2409. {
  2410. struct drm_device *dev = crtc->dev;
  2411. struct drm_i915_private *dev_priv = dev->dev_private;
  2412. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2413. int pipe = intel_crtc->pipe;
  2414. u32 reg, temp;
  2415. /* enable normal train */
  2416. reg = FDI_TX_CTL(pipe);
  2417. temp = I915_READ(reg);
  2418. if (IS_IVYBRIDGE(dev)) {
  2419. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2420. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2421. } else {
  2422. temp &= ~FDI_LINK_TRAIN_NONE;
  2423. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2424. }
  2425. I915_WRITE(reg, temp);
  2426. reg = FDI_RX_CTL(pipe);
  2427. temp = I915_READ(reg);
  2428. if (HAS_PCH_CPT(dev)) {
  2429. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2430. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2431. } else {
  2432. temp &= ~FDI_LINK_TRAIN_NONE;
  2433. temp |= FDI_LINK_TRAIN_NONE;
  2434. }
  2435. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2436. /* wait one idle pattern time */
  2437. POSTING_READ(reg);
  2438. udelay(1000);
  2439. /* IVB wants error correction enabled */
  2440. if (IS_IVYBRIDGE(dev))
  2441. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2442. FDI_FE_ERRC_ENABLE);
  2443. }
  2444. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2445. {
  2446. return crtc->base.enabled && crtc->active &&
  2447. crtc->config.has_pch_encoder;
  2448. }
  2449. static void ivb_modeset_global_resources(struct drm_device *dev)
  2450. {
  2451. struct drm_i915_private *dev_priv = dev->dev_private;
  2452. struct intel_crtc *pipe_B_crtc =
  2453. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2454. struct intel_crtc *pipe_C_crtc =
  2455. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2456. uint32_t temp;
  2457. /*
  2458. * When everything is off disable fdi C so that we could enable fdi B
  2459. * with all lanes. Note that we don't care about enabled pipes without
  2460. * an enabled pch encoder.
  2461. */
  2462. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2463. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2464. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2465. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2466. temp = I915_READ(SOUTH_CHICKEN1);
  2467. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2468. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2469. I915_WRITE(SOUTH_CHICKEN1, temp);
  2470. }
  2471. }
  2472. /* The FDI link training functions for ILK/Ibexpeak. */
  2473. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2474. {
  2475. struct drm_device *dev = crtc->dev;
  2476. struct drm_i915_private *dev_priv = dev->dev_private;
  2477. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2478. int pipe = intel_crtc->pipe;
  2479. u32 reg, temp, tries;
  2480. /* FDI needs bits from pipe first */
  2481. assert_pipe_enabled(dev_priv, pipe);
  2482. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2483. for train result */
  2484. reg = FDI_RX_IMR(pipe);
  2485. temp = I915_READ(reg);
  2486. temp &= ~FDI_RX_SYMBOL_LOCK;
  2487. temp &= ~FDI_RX_BIT_LOCK;
  2488. I915_WRITE(reg, temp);
  2489. I915_READ(reg);
  2490. udelay(150);
  2491. /* enable CPU FDI TX and PCH FDI RX */
  2492. reg = FDI_TX_CTL(pipe);
  2493. temp = I915_READ(reg);
  2494. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2495. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2496. temp &= ~FDI_LINK_TRAIN_NONE;
  2497. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2498. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2499. reg = FDI_RX_CTL(pipe);
  2500. temp = I915_READ(reg);
  2501. temp &= ~FDI_LINK_TRAIN_NONE;
  2502. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2503. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2504. POSTING_READ(reg);
  2505. udelay(150);
  2506. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2507. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2508. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2509. FDI_RX_PHASE_SYNC_POINTER_EN);
  2510. reg = FDI_RX_IIR(pipe);
  2511. for (tries = 0; tries < 5; tries++) {
  2512. temp = I915_READ(reg);
  2513. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2514. if ((temp & FDI_RX_BIT_LOCK)) {
  2515. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2516. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2517. break;
  2518. }
  2519. }
  2520. if (tries == 5)
  2521. DRM_ERROR("FDI train 1 fail!\n");
  2522. /* Train 2 */
  2523. reg = FDI_TX_CTL(pipe);
  2524. temp = I915_READ(reg);
  2525. temp &= ~FDI_LINK_TRAIN_NONE;
  2526. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2527. I915_WRITE(reg, temp);
  2528. reg = FDI_RX_CTL(pipe);
  2529. temp = I915_READ(reg);
  2530. temp &= ~FDI_LINK_TRAIN_NONE;
  2531. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2532. I915_WRITE(reg, temp);
  2533. POSTING_READ(reg);
  2534. udelay(150);
  2535. reg = FDI_RX_IIR(pipe);
  2536. for (tries = 0; tries < 5; tries++) {
  2537. temp = I915_READ(reg);
  2538. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2539. if (temp & FDI_RX_SYMBOL_LOCK) {
  2540. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2541. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2542. break;
  2543. }
  2544. }
  2545. if (tries == 5)
  2546. DRM_ERROR("FDI train 2 fail!\n");
  2547. DRM_DEBUG_KMS("FDI train done\n");
  2548. }
  2549. static const int snb_b_fdi_train_param[] = {
  2550. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2551. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2552. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2553. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2554. };
  2555. /* The FDI link training functions for SNB/Cougarpoint. */
  2556. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2557. {
  2558. struct drm_device *dev = crtc->dev;
  2559. struct drm_i915_private *dev_priv = dev->dev_private;
  2560. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2561. int pipe = intel_crtc->pipe;
  2562. u32 reg, temp, i, retry;
  2563. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2564. for train result */
  2565. reg = FDI_RX_IMR(pipe);
  2566. temp = I915_READ(reg);
  2567. temp &= ~FDI_RX_SYMBOL_LOCK;
  2568. temp &= ~FDI_RX_BIT_LOCK;
  2569. I915_WRITE(reg, temp);
  2570. POSTING_READ(reg);
  2571. udelay(150);
  2572. /* enable CPU FDI TX and PCH FDI RX */
  2573. reg = FDI_TX_CTL(pipe);
  2574. temp = I915_READ(reg);
  2575. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2576. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2577. temp &= ~FDI_LINK_TRAIN_NONE;
  2578. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2579. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2580. /* SNB-B */
  2581. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2582. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2583. I915_WRITE(FDI_RX_MISC(pipe),
  2584. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2585. reg = FDI_RX_CTL(pipe);
  2586. temp = I915_READ(reg);
  2587. if (HAS_PCH_CPT(dev)) {
  2588. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2589. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2590. } else {
  2591. temp &= ~FDI_LINK_TRAIN_NONE;
  2592. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2593. }
  2594. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2595. POSTING_READ(reg);
  2596. udelay(150);
  2597. for (i = 0; i < 4; i++) {
  2598. reg = FDI_TX_CTL(pipe);
  2599. temp = I915_READ(reg);
  2600. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2601. temp |= snb_b_fdi_train_param[i];
  2602. I915_WRITE(reg, temp);
  2603. POSTING_READ(reg);
  2604. udelay(500);
  2605. for (retry = 0; retry < 5; retry++) {
  2606. reg = FDI_RX_IIR(pipe);
  2607. temp = I915_READ(reg);
  2608. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2609. if (temp & FDI_RX_BIT_LOCK) {
  2610. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2611. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2612. break;
  2613. }
  2614. udelay(50);
  2615. }
  2616. if (retry < 5)
  2617. break;
  2618. }
  2619. if (i == 4)
  2620. DRM_ERROR("FDI train 1 fail!\n");
  2621. /* Train 2 */
  2622. reg = FDI_TX_CTL(pipe);
  2623. temp = I915_READ(reg);
  2624. temp &= ~FDI_LINK_TRAIN_NONE;
  2625. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2626. if (IS_GEN6(dev)) {
  2627. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2628. /* SNB-B */
  2629. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2630. }
  2631. I915_WRITE(reg, temp);
  2632. reg = FDI_RX_CTL(pipe);
  2633. temp = I915_READ(reg);
  2634. if (HAS_PCH_CPT(dev)) {
  2635. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2636. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2637. } else {
  2638. temp &= ~FDI_LINK_TRAIN_NONE;
  2639. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2640. }
  2641. I915_WRITE(reg, temp);
  2642. POSTING_READ(reg);
  2643. udelay(150);
  2644. for (i = 0; i < 4; i++) {
  2645. reg = FDI_TX_CTL(pipe);
  2646. temp = I915_READ(reg);
  2647. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2648. temp |= snb_b_fdi_train_param[i];
  2649. I915_WRITE(reg, temp);
  2650. POSTING_READ(reg);
  2651. udelay(500);
  2652. for (retry = 0; retry < 5; retry++) {
  2653. reg = FDI_RX_IIR(pipe);
  2654. temp = I915_READ(reg);
  2655. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2656. if (temp & FDI_RX_SYMBOL_LOCK) {
  2657. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2658. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2659. break;
  2660. }
  2661. udelay(50);
  2662. }
  2663. if (retry < 5)
  2664. break;
  2665. }
  2666. if (i == 4)
  2667. DRM_ERROR("FDI train 2 fail!\n");
  2668. DRM_DEBUG_KMS("FDI train done.\n");
  2669. }
  2670. /* Manual link training for Ivy Bridge A0 parts */
  2671. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2672. {
  2673. struct drm_device *dev = crtc->dev;
  2674. struct drm_i915_private *dev_priv = dev->dev_private;
  2675. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2676. int pipe = intel_crtc->pipe;
  2677. u32 reg, temp, i, j;
  2678. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2679. for train result */
  2680. reg = FDI_RX_IMR(pipe);
  2681. temp = I915_READ(reg);
  2682. temp &= ~FDI_RX_SYMBOL_LOCK;
  2683. temp &= ~FDI_RX_BIT_LOCK;
  2684. I915_WRITE(reg, temp);
  2685. POSTING_READ(reg);
  2686. udelay(150);
  2687. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2688. I915_READ(FDI_RX_IIR(pipe)));
  2689. /* Try each vswing and preemphasis setting twice before moving on */
  2690. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2691. /* disable first in case we need to retry */
  2692. reg = FDI_TX_CTL(pipe);
  2693. temp = I915_READ(reg);
  2694. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2695. temp &= ~FDI_TX_ENABLE;
  2696. I915_WRITE(reg, temp);
  2697. reg = FDI_RX_CTL(pipe);
  2698. temp = I915_READ(reg);
  2699. temp &= ~FDI_LINK_TRAIN_AUTO;
  2700. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2701. temp &= ~FDI_RX_ENABLE;
  2702. I915_WRITE(reg, temp);
  2703. /* enable CPU FDI TX and PCH FDI RX */
  2704. reg = FDI_TX_CTL(pipe);
  2705. temp = I915_READ(reg);
  2706. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2707. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2708. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2709. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2710. temp |= snb_b_fdi_train_param[j/2];
  2711. temp |= FDI_COMPOSITE_SYNC;
  2712. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2713. I915_WRITE(FDI_RX_MISC(pipe),
  2714. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2715. reg = FDI_RX_CTL(pipe);
  2716. temp = I915_READ(reg);
  2717. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2718. temp |= FDI_COMPOSITE_SYNC;
  2719. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2720. POSTING_READ(reg);
  2721. udelay(1); /* should be 0.5us */
  2722. for (i = 0; i < 4; i++) {
  2723. reg = FDI_RX_IIR(pipe);
  2724. temp = I915_READ(reg);
  2725. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2726. if (temp & FDI_RX_BIT_LOCK ||
  2727. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2728. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2729. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2730. i);
  2731. break;
  2732. }
  2733. udelay(1); /* should be 0.5us */
  2734. }
  2735. if (i == 4) {
  2736. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2737. continue;
  2738. }
  2739. /* Train 2 */
  2740. reg = FDI_TX_CTL(pipe);
  2741. temp = I915_READ(reg);
  2742. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2743. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2744. I915_WRITE(reg, temp);
  2745. reg = FDI_RX_CTL(pipe);
  2746. temp = I915_READ(reg);
  2747. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2748. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2749. I915_WRITE(reg, temp);
  2750. POSTING_READ(reg);
  2751. udelay(2); /* should be 1.5us */
  2752. for (i = 0; i < 4; i++) {
  2753. reg = FDI_RX_IIR(pipe);
  2754. temp = I915_READ(reg);
  2755. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2756. if (temp & FDI_RX_SYMBOL_LOCK ||
  2757. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2758. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2759. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2760. i);
  2761. goto train_done;
  2762. }
  2763. udelay(2); /* should be 1.5us */
  2764. }
  2765. if (i == 4)
  2766. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2767. }
  2768. train_done:
  2769. DRM_DEBUG_KMS("FDI train done.\n");
  2770. }
  2771. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2772. {
  2773. struct drm_device *dev = intel_crtc->base.dev;
  2774. struct drm_i915_private *dev_priv = dev->dev_private;
  2775. int pipe = intel_crtc->pipe;
  2776. u32 reg, temp;
  2777. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2778. reg = FDI_RX_CTL(pipe);
  2779. temp = I915_READ(reg);
  2780. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2781. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2782. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2783. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2784. POSTING_READ(reg);
  2785. udelay(200);
  2786. /* Switch from Rawclk to PCDclk */
  2787. temp = I915_READ(reg);
  2788. I915_WRITE(reg, temp | FDI_PCDCLK);
  2789. POSTING_READ(reg);
  2790. udelay(200);
  2791. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2792. reg = FDI_TX_CTL(pipe);
  2793. temp = I915_READ(reg);
  2794. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2795. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2796. POSTING_READ(reg);
  2797. udelay(100);
  2798. }
  2799. }
  2800. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2801. {
  2802. struct drm_device *dev = intel_crtc->base.dev;
  2803. struct drm_i915_private *dev_priv = dev->dev_private;
  2804. int pipe = intel_crtc->pipe;
  2805. u32 reg, temp;
  2806. /* Switch from PCDclk to Rawclk */
  2807. reg = FDI_RX_CTL(pipe);
  2808. temp = I915_READ(reg);
  2809. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2810. /* Disable CPU FDI TX PLL */
  2811. reg = FDI_TX_CTL(pipe);
  2812. temp = I915_READ(reg);
  2813. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2814. POSTING_READ(reg);
  2815. udelay(100);
  2816. reg = FDI_RX_CTL(pipe);
  2817. temp = I915_READ(reg);
  2818. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2819. /* Wait for the clocks to turn off. */
  2820. POSTING_READ(reg);
  2821. udelay(100);
  2822. }
  2823. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2824. {
  2825. struct drm_device *dev = crtc->dev;
  2826. struct drm_i915_private *dev_priv = dev->dev_private;
  2827. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2828. int pipe = intel_crtc->pipe;
  2829. u32 reg, temp;
  2830. /* disable CPU FDI tx and PCH FDI rx */
  2831. reg = FDI_TX_CTL(pipe);
  2832. temp = I915_READ(reg);
  2833. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2834. POSTING_READ(reg);
  2835. reg = FDI_RX_CTL(pipe);
  2836. temp = I915_READ(reg);
  2837. temp &= ~(0x7 << 16);
  2838. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2839. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2840. POSTING_READ(reg);
  2841. udelay(100);
  2842. /* Ironlake workaround, disable clock pointer after downing FDI */
  2843. if (HAS_PCH_IBX(dev))
  2844. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2845. /* still set train pattern 1 */
  2846. reg = FDI_TX_CTL(pipe);
  2847. temp = I915_READ(reg);
  2848. temp &= ~FDI_LINK_TRAIN_NONE;
  2849. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2850. I915_WRITE(reg, temp);
  2851. reg = FDI_RX_CTL(pipe);
  2852. temp = I915_READ(reg);
  2853. if (HAS_PCH_CPT(dev)) {
  2854. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2855. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2856. } else {
  2857. temp &= ~FDI_LINK_TRAIN_NONE;
  2858. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2859. }
  2860. /* BPC in FDI rx is consistent with that in PIPECONF */
  2861. temp &= ~(0x07 << 16);
  2862. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2863. I915_WRITE(reg, temp);
  2864. POSTING_READ(reg);
  2865. udelay(100);
  2866. }
  2867. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  2868. {
  2869. struct intel_crtc *crtc;
  2870. /* Note that we don't need to be called with mode_config.lock here
  2871. * as our list of CRTC objects is static for the lifetime of the
  2872. * device and so cannot disappear as we iterate. Similarly, we can
  2873. * happily treat the predicates as racy, atomic checks as userspace
  2874. * cannot claim and pin a new fb without at least acquring the
  2875. * struct_mutex and so serialising with us.
  2876. */
  2877. for_each_intel_crtc(dev, crtc) {
  2878. if (atomic_read(&crtc->unpin_work_count) == 0)
  2879. continue;
  2880. if (crtc->unpin_work)
  2881. intel_wait_for_vblank(dev, crtc->pipe);
  2882. return true;
  2883. }
  2884. return false;
  2885. }
  2886. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2887. {
  2888. struct drm_device *dev = crtc->dev;
  2889. struct drm_i915_private *dev_priv = dev->dev_private;
  2890. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  2891. WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  2892. !intel_crtc_has_pending_flip(crtc),
  2893. 60*HZ) == 0);
  2894. if (crtc->primary->fb) {
  2895. mutex_lock(&dev->struct_mutex);
  2896. intel_finish_fb(crtc->primary->fb);
  2897. mutex_unlock(&dev->struct_mutex);
  2898. }
  2899. }
  2900. /* Program iCLKIP clock to the desired frequency */
  2901. static void lpt_program_iclkip(struct drm_crtc *crtc)
  2902. {
  2903. struct drm_device *dev = crtc->dev;
  2904. struct drm_i915_private *dev_priv = dev->dev_private;
  2905. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  2906. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  2907. u32 temp;
  2908. mutex_lock(&dev_priv->dpio_lock);
  2909. /* It is necessary to ungate the pixclk gate prior to programming
  2910. * the divisors, and gate it back when it is done.
  2911. */
  2912. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  2913. /* Disable SSCCTL */
  2914. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  2915. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  2916. SBI_SSCCTL_DISABLE,
  2917. SBI_ICLK);
  2918. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  2919. if (clock == 20000) {
  2920. auxdiv = 1;
  2921. divsel = 0x41;
  2922. phaseinc = 0x20;
  2923. } else {
  2924. /* The iCLK virtual clock root frequency is in MHz,
  2925. * but the adjusted_mode->crtc_clock in in KHz. To get the
  2926. * divisors, it is necessary to divide one by another, so we
  2927. * convert the virtual clock precision to KHz here for higher
  2928. * precision.
  2929. */
  2930. u32 iclk_virtual_root_freq = 172800 * 1000;
  2931. u32 iclk_pi_range = 64;
  2932. u32 desired_divisor, msb_divisor_value, pi_value;
  2933. desired_divisor = (iclk_virtual_root_freq / clock);
  2934. msb_divisor_value = desired_divisor / iclk_pi_range;
  2935. pi_value = desired_divisor % iclk_pi_range;
  2936. auxdiv = 0;
  2937. divsel = msb_divisor_value - 2;
  2938. phaseinc = pi_value;
  2939. }
  2940. /* This should not happen with any sane values */
  2941. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  2942. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  2943. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  2944. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  2945. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  2946. clock,
  2947. auxdiv,
  2948. divsel,
  2949. phasedir,
  2950. phaseinc);
  2951. /* Program SSCDIVINTPHASE6 */
  2952. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  2953. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  2954. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  2955. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  2956. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  2957. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  2958. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  2959. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  2960. /* Program SSCAUXDIV */
  2961. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  2962. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  2963. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  2964. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  2965. /* Enable modulator and associated divider */
  2966. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  2967. temp &= ~SBI_SSCCTL_DISABLE;
  2968. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  2969. /* Wait for initialization time */
  2970. udelay(24);
  2971. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  2972. mutex_unlock(&dev_priv->dpio_lock);
  2973. }
  2974. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  2975. enum pipe pch_transcoder)
  2976. {
  2977. struct drm_device *dev = crtc->base.dev;
  2978. struct drm_i915_private *dev_priv = dev->dev_private;
  2979. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  2980. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  2981. I915_READ(HTOTAL(cpu_transcoder)));
  2982. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  2983. I915_READ(HBLANK(cpu_transcoder)));
  2984. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  2985. I915_READ(HSYNC(cpu_transcoder)));
  2986. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  2987. I915_READ(VTOTAL(cpu_transcoder)));
  2988. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  2989. I915_READ(VBLANK(cpu_transcoder)));
  2990. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  2991. I915_READ(VSYNC(cpu_transcoder)));
  2992. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  2993. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  2994. }
  2995. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  2996. {
  2997. struct drm_i915_private *dev_priv = dev->dev_private;
  2998. uint32_t temp;
  2999. temp = I915_READ(SOUTH_CHICKEN1);
  3000. if (temp & FDI_BC_BIFURCATION_SELECT)
  3001. return;
  3002. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3003. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3004. temp |= FDI_BC_BIFURCATION_SELECT;
  3005. DRM_DEBUG_KMS("enabling fdi C rx\n");
  3006. I915_WRITE(SOUTH_CHICKEN1, temp);
  3007. POSTING_READ(SOUTH_CHICKEN1);
  3008. }
  3009. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3010. {
  3011. struct drm_device *dev = intel_crtc->base.dev;
  3012. struct drm_i915_private *dev_priv = dev->dev_private;
  3013. switch (intel_crtc->pipe) {
  3014. case PIPE_A:
  3015. break;
  3016. case PIPE_B:
  3017. if (intel_crtc->config.fdi_lanes > 2)
  3018. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  3019. else
  3020. cpt_enable_fdi_bc_bifurcation(dev);
  3021. break;
  3022. case PIPE_C:
  3023. cpt_enable_fdi_bc_bifurcation(dev);
  3024. break;
  3025. default:
  3026. BUG();
  3027. }
  3028. }
  3029. /*
  3030. * Enable PCH resources required for PCH ports:
  3031. * - PCH PLLs
  3032. * - FDI training & RX/TX
  3033. * - update transcoder timings
  3034. * - DP transcoding bits
  3035. * - transcoder
  3036. */
  3037. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3038. {
  3039. struct drm_device *dev = crtc->dev;
  3040. struct drm_i915_private *dev_priv = dev->dev_private;
  3041. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3042. int pipe = intel_crtc->pipe;
  3043. u32 reg, temp;
  3044. assert_pch_transcoder_disabled(dev_priv, pipe);
  3045. if (IS_IVYBRIDGE(dev))
  3046. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3047. /* Write the TU size bits before fdi link training, so that error
  3048. * detection works. */
  3049. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3050. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3051. /* For PCH output, training FDI link */
  3052. dev_priv->display.fdi_link_train(crtc);
  3053. /* We need to program the right clock selection before writing the pixel
  3054. * mutliplier into the DPLL. */
  3055. if (HAS_PCH_CPT(dev)) {
  3056. u32 sel;
  3057. temp = I915_READ(PCH_DPLL_SEL);
  3058. temp |= TRANS_DPLL_ENABLE(pipe);
  3059. sel = TRANS_DPLLB_SEL(pipe);
  3060. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  3061. temp |= sel;
  3062. else
  3063. temp &= ~sel;
  3064. I915_WRITE(PCH_DPLL_SEL, temp);
  3065. }
  3066. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3067. * transcoder, and we actually should do this to not upset any PCH
  3068. * transcoder that already use the clock when we share it.
  3069. *
  3070. * Note that enable_shared_dpll tries to do the right thing, but
  3071. * get_shared_dpll unconditionally resets the pll - we need that to have
  3072. * the right LVDS enable sequence. */
  3073. intel_enable_shared_dpll(intel_crtc);
  3074. /* set transcoder timing, panel must allow it */
  3075. assert_panel_unlocked(dev_priv, pipe);
  3076. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3077. intel_fdi_normal_train(crtc);
  3078. /* For PCH DP, enable TRANS_DP_CTL */
  3079. if (HAS_PCH_CPT(dev) &&
  3080. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  3081. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  3082. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3083. reg = TRANS_DP_CTL(pipe);
  3084. temp = I915_READ(reg);
  3085. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3086. TRANS_DP_SYNC_MASK |
  3087. TRANS_DP_BPC_MASK);
  3088. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3089. TRANS_DP_ENH_FRAMING);
  3090. temp |= bpc << 9; /* same format but at 11:9 */
  3091. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3092. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3093. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3094. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3095. switch (intel_trans_dp_port_sel(crtc)) {
  3096. case PCH_DP_B:
  3097. temp |= TRANS_DP_PORT_SEL_B;
  3098. break;
  3099. case PCH_DP_C:
  3100. temp |= TRANS_DP_PORT_SEL_C;
  3101. break;
  3102. case PCH_DP_D:
  3103. temp |= TRANS_DP_PORT_SEL_D;
  3104. break;
  3105. default:
  3106. BUG();
  3107. }
  3108. I915_WRITE(reg, temp);
  3109. }
  3110. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3111. }
  3112. static void lpt_pch_enable(struct drm_crtc *crtc)
  3113. {
  3114. struct drm_device *dev = crtc->dev;
  3115. struct drm_i915_private *dev_priv = dev->dev_private;
  3116. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3117. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3118. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3119. lpt_program_iclkip(crtc);
  3120. /* Set transcoder timing. */
  3121. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3122. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3123. }
  3124. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3125. {
  3126. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3127. if (pll == NULL)
  3128. return;
  3129. if (pll->refcount == 0) {
  3130. WARN(1, "bad %s refcount\n", pll->name);
  3131. return;
  3132. }
  3133. if (--pll->refcount == 0) {
  3134. WARN_ON(pll->on);
  3135. WARN_ON(pll->active);
  3136. }
  3137. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3138. }
  3139. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3140. {
  3141. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3142. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3143. enum intel_dpll_id i;
  3144. if (pll) {
  3145. DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
  3146. crtc->base.base.id, pll->name);
  3147. intel_put_shared_dpll(crtc);
  3148. }
  3149. if (HAS_PCH_IBX(dev_priv->dev)) {
  3150. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3151. i = (enum intel_dpll_id) crtc->pipe;
  3152. pll = &dev_priv->shared_dplls[i];
  3153. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3154. crtc->base.base.id, pll->name);
  3155. WARN_ON(pll->refcount);
  3156. goto found;
  3157. }
  3158. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3159. pll = &dev_priv->shared_dplls[i];
  3160. /* Only want to check enabled timings first */
  3161. if (pll->refcount == 0)
  3162. continue;
  3163. if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
  3164. sizeof(pll->hw_state)) == 0) {
  3165. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
  3166. crtc->base.base.id,
  3167. pll->name, pll->refcount, pll->active);
  3168. goto found;
  3169. }
  3170. }
  3171. /* Ok no matching timings, maybe there's a free one? */
  3172. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3173. pll = &dev_priv->shared_dplls[i];
  3174. if (pll->refcount == 0) {
  3175. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3176. crtc->base.base.id, pll->name);
  3177. goto found;
  3178. }
  3179. }
  3180. return NULL;
  3181. found:
  3182. if (pll->refcount == 0)
  3183. pll->hw_state = crtc->config.dpll_hw_state;
  3184. crtc->config.shared_dpll = i;
  3185. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3186. pipe_name(crtc->pipe));
  3187. pll->refcount++;
  3188. return pll;
  3189. }
  3190. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3191. {
  3192. struct drm_i915_private *dev_priv = dev->dev_private;
  3193. int dslreg = PIPEDSL(pipe);
  3194. u32 temp;
  3195. temp = I915_READ(dslreg);
  3196. udelay(500);
  3197. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3198. if (wait_for(I915_READ(dslreg) != temp, 5))
  3199. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3200. }
  3201. }
  3202. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3203. {
  3204. struct drm_device *dev = crtc->base.dev;
  3205. struct drm_i915_private *dev_priv = dev->dev_private;
  3206. int pipe = crtc->pipe;
  3207. if (crtc->config.pch_pfit.enabled) {
  3208. /* Force use of hard-coded filter coefficients
  3209. * as some pre-programmed values are broken,
  3210. * e.g. x201.
  3211. */
  3212. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3213. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3214. PF_PIPE_SEL_IVB(pipe));
  3215. else
  3216. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3217. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3218. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3219. }
  3220. }
  3221. static void intel_enable_planes(struct drm_crtc *crtc)
  3222. {
  3223. struct drm_device *dev = crtc->dev;
  3224. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3225. struct drm_plane *plane;
  3226. struct intel_plane *intel_plane;
  3227. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3228. intel_plane = to_intel_plane(plane);
  3229. if (intel_plane->pipe == pipe)
  3230. intel_plane_restore(&intel_plane->base);
  3231. }
  3232. }
  3233. static void intel_disable_planes(struct drm_crtc *crtc)
  3234. {
  3235. struct drm_device *dev = crtc->dev;
  3236. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3237. struct drm_plane *plane;
  3238. struct intel_plane *intel_plane;
  3239. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3240. intel_plane = to_intel_plane(plane);
  3241. if (intel_plane->pipe == pipe)
  3242. intel_plane_disable(&intel_plane->base);
  3243. }
  3244. }
  3245. void hsw_enable_ips(struct intel_crtc *crtc)
  3246. {
  3247. struct drm_device *dev = crtc->base.dev;
  3248. struct drm_i915_private *dev_priv = dev->dev_private;
  3249. if (!crtc->config.ips_enabled)
  3250. return;
  3251. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3252. intel_wait_for_vblank(dev, crtc->pipe);
  3253. assert_plane_enabled(dev_priv, crtc->plane);
  3254. if (IS_BROADWELL(dev)) {
  3255. mutex_lock(&dev_priv->rps.hw_lock);
  3256. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3257. mutex_unlock(&dev_priv->rps.hw_lock);
  3258. /* Quoting Art Runyan: "its not safe to expect any particular
  3259. * value in IPS_CTL bit 31 after enabling IPS through the
  3260. * mailbox." Moreover, the mailbox may return a bogus state,
  3261. * so we need to just enable it and continue on.
  3262. */
  3263. } else {
  3264. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3265. /* The bit only becomes 1 in the next vblank, so this wait here
  3266. * is essentially intel_wait_for_vblank. If we don't have this
  3267. * and don't wait for vblanks until the end of crtc_enable, then
  3268. * the HW state readout code will complain that the expected
  3269. * IPS_CTL value is not the one we read. */
  3270. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3271. DRM_ERROR("Timed out waiting for IPS enable\n");
  3272. }
  3273. }
  3274. void hsw_disable_ips(struct intel_crtc *crtc)
  3275. {
  3276. struct drm_device *dev = crtc->base.dev;
  3277. struct drm_i915_private *dev_priv = dev->dev_private;
  3278. if (!crtc->config.ips_enabled)
  3279. return;
  3280. assert_plane_enabled(dev_priv, crtc->plane);
  3281. if (IS_BROADWELL(dev)) {
  3282. mutex_lock(&dev_priv->rps.hw_lock);
  3283. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3284. mutex_unlock(&dev_priv->rps.hw_lock);
  3285. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3286. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3287. DRM_ERROR("Timed out waiting for IPS disable\n");
  3288. } else {
  3289. I915_WRITE(IPS_CTL, 0);
  3290. POSTING_READ(IPS_CTL);
  3291. }
  3292. /* We need to wait for a vblank before we can disable the plane. */
  3293. intel_wait_for_vblank(dev, crtc->pipe);
  3294. }
  3295. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3296. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3297. {
  3298. struct drm_device *dev = crtc->dev;
  3299. struct drm_i915_private *dev_priv = dev->dev_private;
  3300. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3301. enum pipe pipe = intel_crtc->pipe;
  3302. int palreg = PALETTE(pipe);
  3303. int i;
  3304. bool reenable_ips = false;
  3305. /* The clocks have to be on to load the palette. */
  3306. if (!crtc->enabled || !intel_crtc->active)
  3307. return;
  3308. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3309. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  3310. assert_dsi_pll_enabled(dev_priv);
  3311. else
  3312. assert_pll_enabled(dev_priv, pipe);
  3313. }
  3314. /* use legacy palette for Ironlake */
  3315. if (!HAS_GMCH_DISPLAY(dev))
  3316. palreg = LGC_PALETTE(pipe);
  3317. /* Workaround : Do not read or write the pipe palette/gamma data while
  3318. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3319. */
  3320. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3321. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3322. GAMMA_MODE_MODE_SPLIT)) {
  3323. hsw_disable_ips(intel_crtc);
  3324. reenable_ips = true;
  3325. }
  3326. for (i = 0; i < 256; i++) {
  3327. I915_WRITE(palreg + 4 * i,
  3328. (intel_crtc->lut_r[i] << 16) |
  3329. (intel_crtc->lut_g[i] << 8) |
  3330. intel_crtc->lut_b[i]);
  3331. }
  3332. if (reenable_ips)
  3333. hsw_enable_ips(intel_crtc);
  3334. }
  3335. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3336. {
  3337. if (!enable && intel_crtc->overlay) {
  3338. struct drm_device *dev = intel_crtc->base.dev;
  3339. struct drm_i915_private *dev_priv = dev->dev_private;
  3340. mutex_lock(&dev->struct_mutex);
  3341. dev_priv->mm.interruptible = false;
  3342. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3343. dev_priv->mm.interruptible = true;
  3344. mutex_unlock(&dev->struct_mutex);
  3345. }
  3346. /* Let userspace switch the overlay on again. In most cases userspace
  3347. * has to recompute where to put it anyway.
  3348. */
  3349. }
  3350. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3351. {
  3352. struct drm_device *dev = crtc->dev;
  3353. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3354. int pipe = intel_crtc->pipe;
  3355. drm_vblank_on(dev, pipe);
  3356. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3357. intel_enable_planes(crtc);
  3358. intel_crtc_update_cursor(crtc, true);
  3359. intel_crtc_dpms_overlay(intel_crtc, true);
  3360. hsw_enable_ips(intel_crtc);
  3361. mutex_lock(&dev->struct_mutex);
  3362. intel_update_fbc(dev);
  3363. mutex_unlock(&dev->struct_mutex);
  3364. /*
  3365. * FIXME: Once we grow proper nuclear flip support out of this we need
  3366. * to compute the mask of flip planes precisely. For the time being
  3367. * consider this a flip from a NULL plane.
  3368. */
  3369. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3370. }
  3371. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3372. {
  3373. struct drm_device *dev = crtc->dev;
  3374. struct drm_i915_private *dev_priv = dev->dev_private;
  3375. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3376. int pipe = intel_crtc->pipe;
  3377. int plane = intel_crtc->plane;
  3378. intel_crtc_wait_for_pending_flips(crtc);
  3379. if (dev_priv->fbc.plane == plane)
  3380. intel_disable_fbc(dev);
  3381. hsw_disable_ips(intel_crtc);
  3382. intel_crtc_dpms_overlay(intel_crtc, false);
  3383. intel_crtc_update_cursor(crtc, false);
  3384. intel_disable_planes(crtc);
  3385. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3386. /*
  3387. * FIXME: Once we grow proper nuclear flip support out of this we need
  3388. * to compute the mask of flip planes precisely. For the time being
  3389. * consider this a flip to a NULL plane.
  3390. */
  3391. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3392. drm_vblank_off(dev, pipe);
  3393. }
  3394. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3395. {
  3396. struct drm_device *dev = crtc->dev;
  3397. struct drm_i915_private *dev_priv = dev->dev_private;
  3398. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3399. struct intel_encoder *encoder;
  3400. int pipe = intel_crtc->pipe;
  3401. WARN_ON(!crtc->enabled);
  3402. if (intel_crtc->active)
  3403. return;
  3404. if (intel_crtc->config.has_pch_encoder)
  3405. intel_prepare_shared_dpll(intel_crtc);
  3406. if (intel_crtc->config.has_dp_encoder)
  3407. intel_dp_set_m_n(intel_crtc);
  3408. intel_set_pipe_timings(intel_crtc);
  3409. if (intel_crtc->config.has_pch_encoder) {
  3410. intel_cpu_transcoder_set_m_n(intel_crtc,
  3411. &intel_crtc->config.fdi_m_n, NULL);
  3412. }
  3413. ironlake_set_pipeconf(crtc);
  3414. intel_crtc->active = true;
  3415. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3416. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3417. for_each_encoder_on_crtc(dev, crtc, encoder)
  3418. if (encoder->pre_enable)
  3419. encoder->pre_enable(encoder);
  3420. if (intel_crtc->config.has_pch_encoder) {
  3421. /* Note: FDI PLL enabling _must_ be done before we enable the
  3422. * cpu pipes, hence this is separate from all the other fdi/pch
  3423. * enabling. */
  3424. ironlake_fdi_pll_enable(intel_crtc);
  3425. } else {
  3426. assert_fdi_tx_disabled(dev_priv, pipe);
  3427. assert_fdi_rx_disabled(dev_priv, pipe);
  3428. }
  3429. ironlake_pfit_enable(intel_crtc);
  3430. /*
  3431. * On ILK+ LUT must be loaded before the pipe is running but with
  3432. * clocks enabled
  3433. */
  3434. intel_crtc_load_lut(crtc);
  3435. intel_update_watermarks(crtc);
  3436. intel_enable_pipe(intel_crtc);
  3437. if (intel_crtc->config.has_pch_encoder)
  3438. ironlake_pch_enable(crtc);
  3439. for_each_encoder_on_crtc(dev, crtc, encoder)
  3440. encoder->enable(encoder);
  3441. if (HAS_PCH_CPT(dev))
  3442. cpt_verify_modeset(dev, intel_crtc->pipe);
  3443. intel_crtc_enable_planes(crtc);
  3444. }
  3445. /* IPS only exists on ULT machines and is tied to pipe A. */
  3446. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3447. {
  3448. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3449. }
  3450. /*
  3451. * This implements the workaround described in the "notes" section of the mode
  3452. * set sequence documentation. When going from no pipes or single pipe to
  3453. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3454. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3455. */
  3456. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3457. {
  3458. struct drm_device *dev = crtc->base.dev;
  3459. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3460. /* We want to get the other_active_crtc only if there's only 1 other
  3461. * active crtc. */
  3462. for_each_intel_crtc(dev, crtc_it) {
  3463. if (!crtc_it->active || crtc_it == crtc)
  3464. continue;
  3465. if (other_active_crtc)
  3466. return;
  3467. other_active_crtc = crtc_it;
  3468. }
  3469. if (!other_active_crtc)
  3470. return;
  3471. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3472. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3473. }
  3474. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3475. {
  3476. struct drm_device *dev = crtc->dev;
  3477. struct drm_i915_private *dev_priv = dev->dev_private;
  3478. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3479. struct intel_encoder *encoder;
  3480. int pipe = intel_crtc->pipe;
  3481. WARN_ON(!crtc->enabled);
  3482. if (intel_crtc->active)
  3483. return;
  3484. if (intel_crtc_to_shared_dpll(intel_crtc))
  3485. intel_enable_shared_dpll(intel_crtc);
  3486. if (intel_crtc->config.has_dp_encoder)
  3487. intel_dp_set_m_n(intel_crtc);
  3488. intel_set_pipe_timings(intel_crtc);
  3489. if (intel_crtc->config.has_pch_encoder) {
  3490. intel_cpu_transcoder_set_m_n(intel_crtc,
  3491. &intel_crtc->config.fdi_m_n, NULL);
  3492. }
  3493. haswell_set_pipeconf(crtc);
  3494. intel_set_pipe_csc(crtc);
  3495. intel_crtc->active = true;
  3496. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3497. for_each_encoder_on_crtc(dev, crtc, encoder)
  3498. if (encoder->pre_enable)
  3499. encoder->pre_enable(encoder);
  3500. if (intel_crtc->config.has_pch_encoder) {
  3501. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3502. dev_priv->display.fdi_link_train(crtc);
  3503. }
  3504. intel_ddi_enable_pipe_clock(intel_crtc);
  3505. ironlake_pfit_enable(intel_crtc);
  3506. /*
  3507. * On ILK+ LUT must be loaded before the pipe is running but with
  3508. * clocks enabled
  3509. */
  3510. intel_crtc_load_lut(crtc);
  3511. intel_ddi_set_pipe_settings(crtc);
  3512. intel_ddi_enable_transcoder_func(crtc);
  3513. intel_update_watermarks(crtc);
  3514. intel_enable_pipe(intel_crtc);
  3515. if (intel_crtc->config.has_pch_encoder)
  3516. lpt_pch_enable(crtc);
  3517. if (intel_crtc->config.dp_encoder_is_mst)
  3518. intel_ddi_set_vc_payload_alloc(crtc, true);
  3519. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3520. encoder->enable(encoder);
  3521. intel_opregion_notify_encoder(encoder, true);
  3522. }
  3523. /* If we change the relative order between pipe/planes enabling, we need
  3524. * to change the workaround. */
  3525. haswell_mode_set_planes_workaround(intel_crtc);
  3526. intel_crtc_enable_planes(crtc);
  3527. }
  3528. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3529. {
  3530. struct drm_device *dev = crtc->base.dev;
  3531. struct drm_i915_private *dev_priv = dev->dev_private;
  3532. int pipe = crtc->pipe;
  3533. /* To avoid upsetting the power well on haswell only disable the pfit if
  3534. * it's in use. The hw state code will make sure we get this right. */
  3535. if (crtc->config.pch_pfit.enabled) {
  3536. I915_WRITE(PF_CTL(pipe), 0);
  3537. I915_WRITE(PF_WIN_POS(pipe), 0);
  3538. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3539. }
  3540. }
  3541. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3542. {
  3543. struct drm_device *dev = crtc->dev;
  3544. struct drm_i915_private *dev_priv = dev->dev_private;
  3545. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3546. struct intel_encoder *encoder;
  3547. int pipe = intel_crtc->pipe;
  3548. u32 reg, temp;
  3549. if (!intel_crtc->active)
  3550. return;
  3551. intel_crtc_disable_planes(crtc);
  3552. for_each_encoder_on_crtc(dev, crtc, encoder)
  3553. encoder->disable(encoder);
  3554. if (intel_crtc->config.has_pch_encoder)
  3555. intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
  3556. intel_disable_pipe(dev_priv, pipe);
  3557. if (intel_crtc->config.dp_encoder_is_mst)
  3558. intel_ddi_set_vc_payload_alloc(crtc, false);
  3559. ironlake_pfit_disable(intel_crtc);
  3560. for_each_encoder_on_crtc(dev, crtc, encoder)
  3561. if (encoder->post_disable)
  3562. encoder->post_disable(encoder);
  3563. if (intel_crtc->config.has_pch_encoder) {
  3564. ironlake_fdi_disable(crtc);
  3565. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3566. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3567. if (HAS_PCH_CPT(dev)) {
  3568. /* disable TRANS_DP_CTL */
  3569. reg = TRANS_DP_CTL(pipe);
  3570. temp = I915_READ(reg);
  3571. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3572. TRANS_DP_PORT_SEL_MASK);
  3573. temp |= TRANS_DP_PORT_SEL_NONE;
  3574. I915_WRITE(reg, temp);
  3575. /* disable DPLL_SEL */
  3576. temp = I915_READ(PCH_DPLL_SEL);
  3577. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3578. I915_WRITE(PCH_DPLL_SEL, temp);
  3579. }
  3580. /* disable PCH DPLL */
  3581. intel_disable_shared_dpll(intel_crtc);
  3582. ironlake_fdi_pll_disable(intel_crtc);
  3583. }
  3584. intel_crtc->active = false;
  3585. intel_update_watermarks(crtc);
  3586. mutex_lock(&dev->struct_mutex);
  3587. intel_update_fbc(dev);
  3588. mutex_unlock(&dev->struct_mutex);
  3589. }
  3590. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3591. {
  3592. struct drm_device *dev = crtc->dev;
  3593. struct drm_i915_private *dev_priv = dev->dev_private;
  3594. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3595. struct intel_encoder *encoder;
  3596. int pipe = intel_crtc->pipe;
  3597. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3598. if (!intel_crtc->active)
  3599. return;
  3600. intel_crtc_disable_planes(crtc);
  3601. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3602. intel_opregion_notify_encoder(encoder, false);
  3603. encoder->disable(encoder);
  3604. }
  3605. if (intel_crtc->config.has_pch_encoder)
  3606. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
  3607. intel_disable_pipe(dev_priv, pipe);
  3608. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3609. ironlake_pfit_disable(intel_crtc);
  3610. intel_ddi_disable_pipe_clock(intel_crtc);
  3611. if (intel_crtc->config.has_pch_encoder) {
  3612. lpt_disable_pch_transcoder(dev_priv);
  3613. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3614. intel_ddi_fdi_disable(crtc);
  3615. }
  3616. for_each_encoder_on_crtc(dev, crtc, encoder)
  3617. if (encoder->post_disable)
  3618. encoder->post_disable(encoder);
  3619. intel_crtc->active = false;
  3620. intel_update_watermarks(crtc);
  3621. mutex_lock(&dev->struct_mutex);
  3622. intel_update_fbc(dev);
  3623. mutex_unlock(&dev->struct_mutex);
  3624. if (intel_crtc_to_shared_dpll(intel_crtc))
  3625. intel_disable_shared_dpll(intel_crtc);
  3626. }
  3627. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3628. {
  3629. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3630. intel_put_shared_dpll(intel_crtc);
  3631. }
  3632. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3633. {
  3634. struct drm_device *dev = crtc->base.dev;
  3635. struct drm_i915_private *dev_priv = dev->dev_private;
  3636. struct intel_crtc_config *pipe_config = &crtc->config;
  3637. if (!crtc->config.gmch_pfit.control)
  3638. return;
  3639. /*
  3640. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3641. * according to register description and PRM.
  3642. */
  3643. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3644. assert_pipe_disabled(dev_priv, crtc->pipe);
  3645. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3646. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3647. /* Border color in case we don't scale up to the full screen. Black by
  3648. * default, change to something else for debugging. */
  3649. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3650. }
  3651. static enum intel_display_power_domain port_to_power_domain(enum port port)
  3652. {
  3653. switch (port) {
  3654. case PORT_A:
  3655. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3656. case PORT_B:
  3657. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3658. case PORT_C:
  3659. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3660. case PORT_D:
  3661. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3662. default:
  3663. WARN_ON_ONCE(1);
  3664. return POWER_DOMAIN_PORT_OTHER;
  3665. }
  3666. }
  3667. #define for_each_power_domain(domain, mask) \
  3668. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3669. if ((1 << (domain)) & (mask))
  3670. enum intel_display_power_domain
  3671. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3672. {
  3673. struct drm_device *dev = intel_encoder->base.dev;
  3674. struct intel_digital_port *intel_dig_port;
  3675. switch (intel_encoder->type) {
  3676. case INTEL_OUTPUT_UNKNOWN:
  3677. /* Only DDI platforms should ever use this output type */
  3678. WARN_ON_ONCE(!HAS_DDI(dev));
  3679. case INTEL_OUTPUT_DISPLAYPORT:
  3680. case INTEL_OUTPUT_HDMI:
  3681. case INTEL_OUTPUT_EDP:
  3682. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3683. return port_to_power_domain(intel_dig_port->port);
  3684. case INTEL_OUTPUT_DP_MST:
  3685. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  3686. return port_to_power_domain(intel_dig_port->port);
  3687. case INTEL_OUTPUT_ANALOG:
  3688. return POWER_DOMAIN_PORT_CRT;
  3689. case INTEL_OUTPUT_DSI:
  3690. return POWER_DOMAIN_PORT_DSI;
  3691. default:
  3692. return POWER_DOMAIN_PORT_OTHER;
  3693. }
  3694. }
  3695. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3696. {
  3697. struct drm_device *dev = crtc->dev;
  3698. struct intel_encoder *intel_encoder;
  3699. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3700. enum pipe pipe = intel_crtc->pipe;
  3701. unsigned long mask;
  3702. enum transcoder transcoder;
  3703. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3704. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3705. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3706. if (intel_crtc->config.pch_pfit.enabled ||
  3707. intel_crtc->config.pch_pfit.force_thru)
  3708. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3709. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3710. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3711. return mask;
  3712. }
  3713. void intel_display_set_init_power(struct drm_i915_private *dev_priv,
  3714. bool enable)
  3715. {
  3716. if (dev_priv->power_domains.init_power_on == enable)
  3717. return;
  3718. if (enable)
  3719. intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
  3720. else
  3721. intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
  3722. dev_priv->power_domains.init_power_on = enable;
  3723. }
  3724. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3725. {
  3726. struct drm_i915_private *dev_priv = dev->dev_private;
  3727. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3728. struct intel_crtc *crtc;
  3729. /*
  3730. * First get all needed power domains, then put all unneeded, to avoid
  3731. * any unnecessary toggling of the power wells.
  3732. */
  3733. for_each_intel_crtc(dev, crtc) {
  3734. enum intel_display_power_domain domain;
  3735. if (!crtc->base.enabled)
  3736. continue;
  3737. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3738. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3739. intel_display_power_get(dev_priv, domain);
  3740. }
  3741. for_each_intel_crtc(dev, crtc) {
  3742. enum intel_display_power_domain domain;
  3743. for_each_power_domain(domain, crtc->enabled_power_domains)
  3744. intel_display_power_put(dev_priv, domain);
  3745. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  3746. }
  3747. intel_display_set_init_power(dev_priv, false);
  3748. }
  3749. /* returns HPLL frequency in kHz */
  3750. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  3751. {
  3752. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  3753. /* Obtain SKU information */
  3754. mutex_lock(&dev_priv->dpio_lock);
  3755. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  3756. CCK_FUSE_HPLL_FREQ_MASK;
  3757. mutex_unlock(&dev_priv->dpio_lock);
  3758. return vco_freq[hpll_freq] * 1000;
  3759. }
  3760. static void vlv_update_cdclk(struct drm_device *dev)
  3761. {
  3762. struct drm_i915_private *dev_priv = dev->dev_private;
  3763. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  3764. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz",
  3765. dev_priv->vlv_cdclk_freq);
  3766. /*
  3767. * Program the gmbus_freq based on the cdclk frequency.
  3768. * BSpec erroneously claims we should aim for 4MHz, but
  3769. * in fact 1MHz is the correct frequency.
  3770. */
  3771. I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
  3772. }
  3773. /* Adjust CDclk dividers to allow high res or save power if possible */
  3774. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  3775. {
  3776. struct drm_i915_private *dev_priv = dev->dev_private;
  3777. u32 val, cmd;
  3778. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  3779. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  3780. cmd = 2;
  3781. else if (cdclk == 266667)
  3782. cmd = 1;
  3783. else
  3784. cmd = 0;
  3785. mutex_lock(&dev_priv->rps.hw_lock);
  3786. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3787. val &= ~DSPFREQGUAR_MASK;
  3788. val |= (cmd << DSPFREQGUAR_SHIFT);
  3789. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3790. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3791. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  3792. 50)) {
  3793. DRM_ERROR("timed out waiting for CDclk change\n");
  3794. }
  3795. mutex_unlock(&dev_priv->rps.hw_lock);
  3796. if (cdclk == 400000) {
  3797. u32 divider, vco;
  3798. vco = valleyview_get_vco(dev_priv);
  3799. divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
  3800. mutex_lock(&dev_priv->dpio_lock);
  3801. /* adjust cdclk divider */
  3802. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3803. val &= ~DISPLAY_FREQUENCY_VALUES;
  3804. val |= divider;
  3805. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  3806. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  3807. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  3808. 50))
  3809. DRM_ERROR("timed out waiting for CDclk change\n");
  3810. mutex_unlock(&dev_priv->dpio_lock);
  3811. }
  3812. mutex_lock(&dev_priv->dpio_lock);
  3813. /* adjust self-refresh exit latency value */
  3814. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  3815. val &= ~0x7f;
  3816. /*
  3817. * For high bandwidth configs, we set a higher latency in the bunit
  3818. * so that the core display fetch happens in time to avoid underruns.
  3819. */
  3820. if (cdclk == 400000)
  3821. val |= 4500 / 250; /* 4.5 usec */
  3822. else
  3823. val |= 3000 / 250; /* 3.0 usec */
  3824. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  3825. mutex_unlock(&dev_priv->dpio_lock);
  3826. vlv_update_cdclk(dev);
  3827. }
  3828. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  3829. {
  3830. struct drm_i915_private *dev_priv = dev->dev_private;
  3831. u32 val, cmd;
  3832. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  3833. switch (cdclk) {
  3834. case 400000:
  3835. cmd = 3;
  3836. break;
  3837. case 333333:
  3838. case 320000:
  3839. cmd = 2;
  3840. break;
  3841. case 266667:
  3842. cmd = 1;
  3843. break;
  3844. case 200000:
  3845. cmd = 0;
  3846. break;
  3847. default:
  3848. WARN_ON(1);
  3849. return;
  3850. }
  3851. mutex_lock(&dev_priv->rps.hw_lock);
  3852. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3853. val &= ~DSPFREQGUAR_MASK_CHV;
  3854. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  3855. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3856. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3857. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  3858. 50)) {
  3859. DRM_ERROR("timed out waiting for CDclk change\n");
  3860. }
  3861. mutex_unlock(&dev_priv->rps.hw_lock);
  3862. vlv_update_cdclk(dev);
  3863. }
  3864. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  3865. int max_pixclk)
  3866. {
  3867. int vco = valleyview_get_vco(dev_priv);
  3868. int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
  3869. /* FIXME: Punit isn't quite ready yet */
  3870. if (IS_CHERRYVIEW(dev_priv->dev))
  3871. return 400000;
  3872. /*
  3873. * Really only a few cases to deal with, as only 4 CDclks are supported:
  3874. * 200MHz
  3875. * 267MHz
  3876. * 320/333MHz (depends on HPLL freq)
  3877. * 400MHz
  3878. * So we check to see whether we're above 90% of the lower bin and
  3879. * adjust if needed.
  3880. *
  3881. * We seem to get an unstable or solid color picture at 200MHz.
  3882. * Not sure what's wrong. For now use 200MHz only when all pipes
  3883. * are off.
  3884. */
  3885. if (max_pixclk > freq_320*9/10)
  3886. return 400000;
  3887. else if (max_pixclk > 266667*9/10)
  3888. return freq_320;
  3889. else if (max_pixclk > 0)
  3890. return 266667;
  3891. else
  3892. return 200000;
  3893. }
  3894. /* compute the max pixel clock for new configuration */
  3895. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  3896. {
  3897. struct drm_device *dev = dev_priv->dev;
  3898. struct intel_crtc *intel_crtc;
  3899. int max_pixclk = 0;
  3900. for_each_intel_crtc(dev, intel_crtc) {
  3901. if (intel_crtc->new_enabled)
  3902. max_pixclk = max(max_pixclk,
  3903. intel_crtc->new_config->adjusted_mode.crtc_clock);
  3904. }
  3905. return max_pixclk;
  3906. }
  3907. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  3908. unsigned *prepare_pipes)
  3909. {
  3910. struct drm_i915_private *dev_priv = dev->dev_private;
  3911. struct intel_crtc *intel_crtc;
  3912. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3913. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  3914. dev_priv->vlv_cdclk_freq)
  3915. return;
  3916. /* disable/enable all currently active pipes while we change cdclk */
  3917. for_each_intel_crtc(dev, intel_crtc)
  3918. if (intel_crtc->base.enabled)
  3919. *prepare_pipes |= (1 << intel_crtc->pipe);
  3920. }
  3921. static void valleyview_modeset_global_resources(struct drm_device *dev)
  3922. {
  3923. struct drm_i915_private *dev_priv = dev->dev_private;
  3924. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3925. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  3926. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  3927. if (IS_CHERRYVIEW(dev))
  3928. cherryview_set_cdclk(dev, req_cdclk);
  3929. else
  3930. valleyview_set_cdclk(dev, req_cdclk);
  3931. }
  3932. modeset_update_crtc_power_domains(dev);
  3933. }
  3934. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  3935. {
  3936. struct drm_device *dev = crtc->dev;
  3937. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3938. struct intel_encoder *encoder;
  3939. int pipe = intel_crtc->pipe;
  3940. bool is_dsi;
  3941. WARN_ON(!crtc->enabled);
  3942. if (intel_crtc->active)
  3943. return;
  3944. is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
  3945. if (!is_dsi) {
  3946. if (IS_CHERRYVIEW(dev))
  3947. chv_prepare_pll(intel_crtc);
  3948. else
  3949. vlv_prepare_pll(intel_crtc);
  3950. }
  3951. if (intel_crtc->config.has_dp_encoder)
  3952. intel_dp_set_m_n(intel_crtc);
  3953. intel_set_pipe_timings(intel_crtc);
  3954. i9xx_set_pipeconf(intel_crtc);
  3955. intel_crtc->active = true;
  3956. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3957. for_each_encoder_on_crtc(dev, crtc, encoder)
  3958. if (encoder->pre_pll_enable)
  3959. encoder->pre_pll_enable(encoder);
  3960. if (!is_dsi) {
  3961. if (IS_CHERRYVIEW(dev))
  3962. chv_enable_pll(intel_crtc);
  3963. else
  3964. vlv_enable_pll(intel_crtc);
  3965. }
  3966. for_each_encoder_on_crtc(dev, crtc, encoder)
  3967. if (encoder->pre_enable)
  3968. encoder->pre_enable(encoder);
  3969. i9xx_pfit_enable(intel_crtc);
  3970. intel_crtc_load_lut(crtc);
  3971. intel_update_watermarks(crtc);
  3972. intel_enable_pipe(intel_crtc);
  3973. for_each_encoder_on_crtc(dev, crtc, encoder)
  3974. encoder->enable(encoder);
  3975. intel_crtc_enable_planes(crtc);
  3976. /* Underruns don't raise interrupts, so check manually. */
  3977. i9xx_check_fifo_underruns(dev);
  3978. }
  3979. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  3980. {
  3981. struct drm_device *dev = crtc->base.dev;
  3982. struct drm_i915_private *dev_priv = dev->dev_private;
  3983. I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
  3984. I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
  3985. }
  3986. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  3987. {
  3988. struct drm_device *dev = crtc->dev;
  3989. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3990. struct intel_encoder *encoder;
  3991. int pipe = intel_crtc->pipe;
  3992. WARN_ON(!crtc->enabled);
  3993. if (intel_crtc->active)
  3994. return;
  3995. i9xx_set_pll_dividers(intel_crtc);
  3996. if (intel_crtc->config.has_dp_encoder)
  3997. intel_dp_set_m_n(intel_crtc);
  3998. intel_set_pipe_timings(intel_crtc);
  3999. i9xx_set_pipeconf(intel_crtc);
  4000. intel_crtc->active = true;
  4001. if (!IS_GEN2(dev))
  4002. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  4003. for_each_encoder_on_crtc(dev, crtc, encoder)
  4004. if (encoder->pre_enable)
  4005. encoder->pre_enable(encoder);
  4006. i9xx_enable_pll(intel_crtc);
  4007. i9xx_pfit_enable(intel_crtc);
  4008. intel_crtc_load_lut(crtc);
  4009. intel_update_watermarks(crtc);
  4010. intel_enable_pipe(intel_crtc);
  4011. for_each_encoder_on_crtc(dev, crtc, encoder)
  4012. encoder->enable(encoder);
  4013. intel_crtc_enable_planes(crtc);
  4014. /*
  4015. * Gen2 reports pipe underruns whenever all planes are disabled.
  4016. * So don't enable underrun reporting before at least some planes
  4017. * are enabled.
  4018. * FIXME: Need to fix the logic to work when we turn off all planes
  4019. * but leave the pipe running.
  4020. */
  4021. if (IS_GEN2(dev))
  4022. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  4023. /* Underruns don't raise interrupts, so check manually. */
  4024. i9xx_check_fifo_underruns(dev);
  4025. }
  4026. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4027. {
  4028. struct drm_device *dev = crtc->base.dev;
  4029. struct drm_i915_private *dev_priv = dev->dev_private;
  4030. if (!crtc->config.gmch_pfit.control)
  4031. return;
  4032. assert_pipe_disabled(dev_priv, crtc->pipe);
  4033. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4034. I915_READ(PFIT_CONTROL));
  4035. I915_WRITE(PFIT_CONTROL, 0);
  4036. }
  4037. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4038. {
  4039. struct drm_device *dev = crtc->dev;
  4040. struct drm_i915_private *dev_priv = dev->dev_private;
  4041. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4042. struct intel_encoder *encoder;
  4043. int pipe = intel_crtc->pipe;
  4044. if (!intel_crtc->active)
  4045. return;
  4046. /*
  4047. * Gen2 reports pipe underruns whenever all planes are disabled.
  4048. * So diasble underrun reporting before all the planes get disabled.
  4049. * FIXME: Need to fix the logic to work when we turn off all planes
  4050. * but leave the pipe running.
  4051. */
  4052. if (IS_GEN2(dev))
  4053. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  4054. /*
  4055. * Vblank time updates from the shadow to live plane control register
  4056. * are blocked if the memory self-refresh mode is active at that
  4057. * moment. So to make sure the plane gets truly disabled, disable
  4058. * first the self-refresh mode. The self-refresh enable bit in turn
  4059. * will be checked/applied by the HW only at the next frame start
  4060. * event which is after the vblank start event, so we need to have a
  4061. * wait-for-vblank between disabling the plane and the pipe.
  4062. */
  4063. intel_set_memory_cxsr(dev_priv, false);
  4064. intel_crtc_disable_planes(crtc);
  4065. for_each_encoder_on_crtc(dev, crtc, encoder)
  4066. encoder->disable(encoder);
  4067. /*
  4068. * On gen2 planes are double buffered but the pipe isn't, so we must
  4069. * wait for planes to fully turn off before disabling the pipe.
  4070. * We also need to wait on all gmch platforms because of the
  4071. * self-refresh mode constraint explained above.
  4072. */
  4073. intel_wait_for_vblank(dev, pipe);
  4074. intel_disable_pipe(dev_priv, pipe);
  4075. i9xx_pfit_disable(intel_crtc);
  4076. for_each_encoder_on_crtc(dev, crtc, encoder)
  4077. if (encoder->post_disable)
  4078. encoder->post_disable(encoder);
  4079. if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
  4080. if (IS_CHERRYVIEW(dev))
  4081. chv_disable_pll(dev_priv, pipe);
  4082. else if (IS_VALLEYVIEW(dev))
  4083. vlv_disable_pll(dev_priv, pipe);
  4084. else
  4085. i9xx_disable_pll(dev_priv, pipe);
  4086. }
  4087. if (!IS_GEN2(dev))
  4088. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  4089. intel_crtc->active = false;
  4090. intel_update_watermarks(crtc);
  4091. mutex_lock(&dev->struct_mutex);
  4092. intel_update_fbc(dev);
  4093. mutex_unlock(&dev->struct_mutex);
  4094. }
  4095. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4096. {
  4097. }
  4098. static void intel_crtc_update_sarea(struct drm_crtc *crtc,
  4099. bool enabled)
  4100. {
  4101. struct drm_device *dev = crtc->dev;
  4102. struct drm_i915_master_private *master_priv;
  4103. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4104. int pipe = intel_crtc->pipe;
  4105. if (!dev->primary->master)
  4106. return;
  4107. master_priv = dev->primary->master->driver_priv;
  4108. if (!master_priv->sarea_priv)
  4109. return;
  4110. switch (pipe) {
  4111. case 0:
  4112. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  4113. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  4114. break;
  4115. case 1:
  4116. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  4117. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  4118. break;
  4119. default:
  4120. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  4121. break;
  4122. }
  4123. }
  4124. /* Master function to enable/disable CRTC and corresponding power wells */
  4125. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4126. {
  4127. struct drm_device *dev = crtc->dev;
  4128. struct drm_i915_private *dev_priv = dev->dev_private;
  4129. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4130. enum intel_display_power_domain domain;
  4131. unsigned long domains;
  4132. if (enable) {
  4133. if (!intel_crtc->active) {
  4134. domains = get_crtc_power_domains(crtc);
  4135. for_each_power_domain(domain, domains)
  4136. intel_display_power_get(dev_priv, domain);
  4137. intel_crtc->enabled_power_domains = domains;
  4138. dev_priv->display.crtc_enable(crtc);
  4139. }
  4140. } else {
  4141. if (intel_crtc->active) {
  4142. dev_priv->display.crtc_disable(crtc);
  4143. domains = intel_crtc->enabled_power_domains;
  4144. for_each_power_domain(domain, domains)
  4145. intel_display_power_put(dev_priv, domain);
  4146. intel_crtc->enabled_power_domains = 0;
  4147. }
  4148. }
  4149. }
  4150. /**
  4151. * Sets the power management mode of the pipe and plane.
  4152. */
  4153. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4154. {
  4155. struct drm_device *dev = crtc->dev;
  4156. struct intel_encoder *intel_encoder;
  4157. bool enable = false;
  4158. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4159. enable |= intel_encoder->connectors_active;
  4160. intel_crtc_control(crtc, enable);
  4161. intel_crtc_update_sarea(crtc, enable);
  4162. }
  4163. static void intel_crtc_disable(struct drm_crtc *crtc)
  4164. {
  4165. struct drm_device *dev = crtc->dev;
  4166. struct drm_connector *connector;
  4167. struct drm_i915_private *dev_priv = dev->dev_private;
  4168. struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
  4169. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  4170. /* crtc should still be enabled when we disable it. */
  4171. WARN_ON(!crtc->enabled);
  4172. dev_priv->display.crtc_disable(crtc);
  4173. intel_crtc_update_sarea(crtc, false);
  4174. dev_priv->display.off(crtc);
  4175. if (crtc->primary->fb) {
  4176. mutex_lock(&dev->struct_mutex);
  4177. intel_unpin_fb_obj(old_obj);
  4178. i915_gem_track_fb(old_obj, NULL,
  4179. INTEL_FRONTBUFFER_PRIMARY(pipe));
  4180. mutex_unlock(&dev->struct_mutex);
  4181. crtc->primary->fb = NULL;
  4182. }
  4183. /* Update computed state. */
  4184. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4185. if (!connector->encoder || !connector->encoder->crtc)
  4186. continue;
  4187. if (connector->encoder->crtc != crtc)
  4188. continue;
  4189. connector->dpms = DRM_MODE_DPMS_OFF;
  4190. to_intel_encoder(connector->encoder)->connectors_active = false;
  4191. }
  4192. }
  4193. void intel_encoder_destroy(struct drm_encoder *encoder)
  4194. {
  4195. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4196. drm_encoder_cleanup(encoder);
  4197. kfree(intel_encoder);
  4198. }
  4199. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4200. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4201. * state of the entire output pipe. */
  4202. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4203. {
  4204. if (mode == DRM_MODE_DPMS_ON) {
  4205. encoder->connectors_active = true;
  4206. intel_crtc_update_dpms(encoder->base.crtc);
  4207. } else {
  4208. encoder->connectors_active = false;
  4209. intel_crtc_update_dpms(encoder->base.crtc);
  4210. }
  4211. }
  4212. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4213. * internal consistency). */
  4214. static void intel_connector_check_state(struct intel_connector *connector)
  4215. {
  4216. if (connector->get_hw_state(connector)) {
  4217. struct intel_encoder *encoder = connector->encoder;
  4218. struct drm_crtc *crtc;
  4219. bool encoder_enabled;
  4220. enum pipe pipe;
  4221. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4222. connector->base.base.id,
  4223. connector->base.name);
  4224. /* there is no real hw state for MST connectors */
  4225. if (connector->mst_port)
  4226. return;
  4227. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4228. "wrong connector dpms state\n");
  4229. WARN(connector->base.encoder != &encoder->base,
  4230. "active connector not linked to encoder\n");
  4231. if (encoder) {
  4232. WARN(!encoder->connectors_active,
  4233. "encoder->connectors_active not set\n");
  4234. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4235. WARN(!encoder_enabled, "encoder not enabled\n");
  4236. if (WARN_ON(!encoder->base.crtc))
  4237. return;
  4238. crtc = encoder->base.crtc;
  4239. WARN(!crtc->enabled, "crtc not enabled\n");
  4240. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4241. WARN(pipe != to_intel_crtc(crtc)->pipe,
  4242. "encoder active on the wrong pipe\n");
  4243. }
  4244. }
  4245. }
  4246. /* Even simpler default implementation, if there's really no special case to
  4247. * consider. */
  4248. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4249. {
  4250. /* All the simple cases only support two dpms states. */
  4251. if (mode != DRM_MODE_DPMS_ON)
  4252. mode = DRM_MODE_DPMS_OFF;
  4253. if (mode == connector->dpms)
  4254. return;
  4255. connector->dpms = mode;
  4256. /* Only need to change hw state when actually enabled */
  4257. if (connector->encoder)
  4258. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4259. intel_modeset_check_state(connector->dev);
  4260. }
  4261. /* Simple connector->get_hw_state implementation for encoders that support only
  4262. * one connector and no cloning and hence the encoder state determines the state
  4263. * of the connector. */
  4264. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4265. {
  4266. enum pipe pipe = 0;
  4267. struct intel_encoder *encoder = connector->encoder;
  4268. return encoder->get_hw_state(encoder, &pipe);
  4269. }
  4270. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4271. struct intel_crtc_config *pipe_config)
  4272. {
  4273. struct drm_i915_private *dev_priv = dev->dev_private;
  4274. struct intel_crtc *pipe_B_crtc =
  4275. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4276. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4277. pipe_name(pipe), pipe_config->fdi_lanes);
  4278. if (pipe_config->fdi_lanes > 4) {
  4279. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4280. pipe_name(pipe), pipe_config->fdi_lanes);
  4281. return false;
  4282. }
  4283. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4284. if (pipe_config->fdi_lanes > 2) {
  4285. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4286. pipe_config->fdi_lanes);
  4287. return false;
  4288. } else {
  4289. return true;
  4290. }
  4291. }
  4292. if (INTEL_INFO(dev)->num_pipes == 2)
  4293. return true;
  4294. /* Ivybridge 3 pipe is really complicated */
  4295. switch (pipe) {
  4296. case PIPE_A:
  4297. return true;
  4298. case PIPE_B:
  4299. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4300. pipe_config->fdi_lanes > 2) {
  4301. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4302. pipe_name(pipe), pipe_config->fdi_lanes);
  4303. return false;
  4304. }
  4305. return true;
  4306. case PIPE_C:
  4307. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4308. pipe_B_crtc->config.fdi_lanes <= 2) {
  4309. if (pipe_config->fdi_lanes > 2) {
  4310. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4311. pipe_name(pipe), pipe_config->fdi_lanes);
  4312. return false;
  4313. }
  4314. } else {
  4315. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4316. return false;
  4317. }
  4318. return true;
  4319. default:
  4320. BUG();
  4321. }
  4322. }
  4323. #define RETRY 1
  4324. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4325. struct intel_crtc_config *pipe_config)
  4326. {
  4327. struct drm_device *dev = intel_crtc->base.dev;
  4328. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4329. int lane, link_bw, fdi_dotclock;
  4330. bool setup_ok, needs_recompute = false;
  4331. retry:
  4332. /* FDI is a binary signal running at ~2.7GHz, encoding
  4333. * each output octet as 10 bits. The actual frequency
  4334. * is stored as a divider into a 100MHz clock, and the
  4335. * mode pixel clock is stored in units of 1KHz.
  4336. * Hence the bw of each lane in terms of the mode signal
  4337. * is:
  4338. */
  4339. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4340. fdi_dotclock = adjusted_mode->crtc_clock;
  4341. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4342. pipe_config->pipe_bpp);
  4343. pipe_config->fdi_lanes = lane;
  4344. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4345. link_bw, &pipe_config->fdi_m_n);
  4346. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4347. intel_crtc->pipe, pipe_config);
  4348. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4349. pipe_config->pipe_bpp -= 2*3;
  4350. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4351. pipe_config->pipe_bpp);
  4352. needs_recompute = true;
  4353. pipe_config->bw_constrained = true;
  4354. goto retry;
  4355. }
  4356. if (needs_recompute)
  4357. return RETRY;
  4358. return setup_ok ? 0 : -EINVAL;
  4359. }
  4360. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4361. struct intel_crtc_config *pipe_config)
  4362. {
  4363. pipe_config->ips_enabled = i915.enable_ips &&
  4364. hsw_crtc_supports_ips(crtc) &&
  4365. pipe_config->pipe_bpp <= 24;
  4366. }
  4367. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4368. struct intel_crtc_config *pipe_config)
  4369. {
  4370. struct drm_device *dev = crtc->base.dev;
  4371. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4372. /* FIXME should check pixel clock limits on all platforms */
  4373. if (INTEL_INFO(dev)->gen < 4) {
  4374. struct drm_i915_private *dev_priv = dev->dev_private;
  4375. int clock_limit =
  4376. dev_priv->display.get_display_clock_speed(dev);
  4377. /*
  4378. * Enable pixel doubling when the dot clock
  4379. * is > 90% of the (display) core speed.
  4380. *
  4381. * GDG double wide on either pipe,
  4382. * otherwise pipe A only.
  4383. */
  4384. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4385. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4386. clock_limit *= 2;
  4387. pipe_config->double_wide = true;
  4388. }
  4389. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4390. return -EINVAL;
  4391. }
  4392. /*
  4393. * Pipe horizontal size must be even in:
  4394. * - DVO ganged mode
  4395. * - LVDS dual channel mode
  4396. * - Double wide pipe
  4397. */
  4398. if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4399. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4400. pipe_config->pipe_src_w &= ~1;
  4401. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4402. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4403. */
  4404. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4405. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4406. return -EINVAL;
  4407. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4408. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4409. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4410. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4411. * for lvds. */
  4412. pipe_config->pipe_bpp = 8*3;
  4413. }
  4414. if (HAS_IPS(dev))
  4415. hsw_compute_ips_config(crtc, pipe_config);
  4416. /*
  4417. * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
  4418. * old clock survives for now.
  4419. */
  4420. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
  4421. pipe_config->shared_dpll = crtc->config.shared_dpll;
  4422. if (pipe_config->has_pch_encoder)
  4423. return ironlake_fdi_compute_config(crtc, pipe_config);
  4424. return 0;
  4425. }
  4426. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4427. {
  4428. struct drm_i915_private *dev_priv = dev->dev_private;
  4429. int vco = valleyview_get_vco(dev_priv);
  4430. u32 val;
  4431. int divider;
  4432. /* FIXME: Punit isn't quite ready yet */
  4433. if (IS_CHERRYVIEW(dev))
  4434. return 400000;
  4435. mutex_lock(&dev_priv->dpio_lock);
  4436. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4437. mutex_unlock(&dev_priv->dpio_lock);
  4438. divider = val & DISPLAY_FREQUENCY_VALUES;
  4439. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4440. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4441. "cdclk change in progress\n");
  4442. return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
  4443. }
  4444. static int i945_get_display_clock_speed(struct drm_device *dev)
  4445. {
  4446. return 400000;
  4447. }
  4448. static int i915_get_display_clock_speed(struct drm_device *dev)
  4449. {
  4450. return 333000;
  4451. }
  4452. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4453. {
  4454. return 200000;
  4455. }
  4456. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4457. {
  4458. u16 gcfgc = 0;
  4459. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4460. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4461. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4462. return 267000;
  4463. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4464. return 333000;
  4465. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4466. return 444000;
  4467. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4468. return 200000;
  4469. default:
  4470. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4471. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4472. return 133000;
  4473. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4474. return 167000;
  4475. }
  4476. }
  4477. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4478. {
  4479. u16 gcfgc = 0;
  4480. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4481. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4482. return 133000;
  4483. else {
  4484. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4485. case GC_DISPLAY_CLOCK_333_MHZ:
  4486. return 333000;
  4487. default:
  4488. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4489. return 190000;
  4490. }
  4491. }
  4492. }
  4493. static int i865_get_display_clock_speed(struct drm_device *dev)
  4494. {
  4495. return 266000;
  4496. }
  4497. static int i855_get_display_clock_speed(struct drm_device *dev)
  4498. {
  4499. u16 hpllcc = 0;
  4500. /* Assume that the hardware is in the high speed state. This
  4501. * should be the default.
  4502. */
  4503. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4504. case GC_CLOCK_133_200:
  4505. case GC_CLOCK_100_200:
  4506. return 200000;
  4507. case GC_CLOCK_166_250:
  4508. return 250000;
  4509. case GC_CLOCK_100_133:
  4510. return 133000;
  4511. }
  4512. /* Shouldn't happen */
  4513. return 0;
  4514. }
  4515. static int i830_get_display_clock_speed(struct drm_device *dev)
  4516. {
  4517. return 133000;
  4518. }
  4519. static void
  4520. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4521. {
  4522. while (*num > DATA_LINK_M_N_MASK ||
  4523. *den > DATA_LINK_M_N_MASK) {
  4524. *num >>= 1;
  4525. *den >>= 1;
  4526. }
  4527. }
  4528. static void compute_m_n(unsigned int m, unsigned int n,
  4529. uint32_t *ret_m, uint32_t *ret_n)
  4530. {
  4531. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4532. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4533. intel_reduce_m_n_ratio(ret_m, ret_n);
  4534. }
  4535. void
  4536. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4537. int pixel_clock, int link_clock,
  4538. struct intel_link_m_n *m_n)
  4539. {
  4540. m_n->tu = 64;
  4541. compute_m_n(bits_per_pixel * pixel_clock,
  4542. link_clock * nlanes * 8,
  4543. &m_n->gmch_m, &m_n->gmch_n);
  4544. compute_m_n(pixel_clock, link_clock,
  4545. &m_n->link_m, &m_n->link_n);
  4546. }
  4547. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4548. {
  4549. if (i915.panel_use_ssc >= 0)
  4550. return i915.panel_use_ssc != 0;
  4551. return dev_priv->vbt.lvds_use_ssc
  4552. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4553. }
  4554. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  4555. {
  4556. struct drm_device *dev = crtc->dev;
  4557. struct drm_i915_private *dev_priv = dev->dev_private;
  4558. int refclk;
  4559. if (IS_VALLEYVIEW(dev)) {
  4560. refclk = 100000;
  4561. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4562. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4563. refclk = dev_priv->vbt.lvds_ssc_freq;
  4564. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4565. } else if (!IS_GEN2(dev)) {
  4566. refclk = 96000;
  4567. } else {
  4568. refclk = 48000;
  4569. }
  4570. return refclk;
  4571. }
  4572. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4573. {
  4574. return (1 << dpll->n) << 16 | dpll->m2;
  4575. }
  4576. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4577. {
  4578. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4579. }
  4580. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4581. intel_clock_t *reduced_clock)
  4582. {
  4583. struct drm_device *dev = crtc->base.dev;
  4584. u32 fp, fp2 = 0;
  4585. if (IS_PINEVIEW(dev)) {
  4586. fp = pnv_dpll_compute_fp(&crtc->config.dpll);
  4587. if (reduced_clock)
  4588. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4589. } else {
  4590. fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
  4591. if (reduced_clock)
  4592. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4593. }
  4594. crtc->config.dpll_hw_state.fp0 = fp;
  4595. crtc->lowfreq_avail = false;
  4596. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4597. reduced_clock && i915.powersave) {
  4598. crtc->config.dpll_hw_state.fp1 = fp2;
  4599. crtc->lowfreq_avail = true;
  4600. } else {
  4601. crtc->config.dpll_hw_state.fp1 = fp;
  4602. }
  4603. }
  4604. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4605. pipe)
  4606. {
  4607. u32 reg_val;
  4608. /*
  4609. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4610. * and set it to a reasonable value instead.
  4611. */
  4612. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4613. reg_val &= 0xffffff00;
  4614. reg_val |= 0x00000030;
  4615. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4616. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4617. reg_val &= 0x8cffffff;
  4618. reg_val = 0x8c000000;
  4619. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4620. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4621. reg_val &= 0xffffff00;
  4622. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4623. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4624. reg_val &= 0x00ffffff;
  4625. reg_val |= 0xb0000000;
  4626. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4627. }
  4628. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4629. struct intel_link_m_n *m_n)
  4630. {
  4631. struct drm_device *dev = crtc->base.dev;
  4632. struct drm_i915_private *dev_priv = dev->dev_private;
  4633. int pipe = crtc->pipe;
  4634. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4635. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4636. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4637. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4638. }
  4639. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4640. struct intel_link_m_n *m_n,
  4641. struct intel_link_m_n *m2_n2)
  4642. {
  4643. struct drm_device *dev = crtc->base.dev;
  4644. struct drm_i915_private *dev_priv = dev->dev_private;
  4645. int pipe = crtc->pipe;
  4646. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4647. if (INTEL_INFO(dev)->gen >= 5) {
  4648. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4649. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4650. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4651. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4652. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  4653. * for gen < 8) and if DRRS is supported (to make sure the
  4654. * registers are not unnecessarily accessed).
  4655. */
  4656. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  4657. crtc->config.has_drrs) {
  4658. I915_WRITE(PIPE_DATA_M2(transcoder),
  4659. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  4660. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  4661. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  4662. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  4663. }
  4664. } else {
  4665. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4666. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4667. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4668. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4669. }
  4670. }
  4671. void intel_dp_set_m_n(struct intel_crtc *crtc)
  4672. {
  4673. if (crtc->config.has_pch_encoder)
  4674. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4675. else
  4676. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
  4677. &crtc->config.dp_m2_n2);
  4678. }
  4679. static void vlv_update_pll(struct intel_crtc *crtc)
  4680. {
  4681. u32 dpll, dpll_md;
  4682. /*
  4683. * Enable DPIO clock input. We should never disable the reference
  4684. * clock for pipe B, since VGA hotplug / manual detection depends
  4685. * on it.
  4686. */
  4687. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4688. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4689. /* We should never disable this, set it here for state tracking */
  4690. if (crtc->pipe == PIPE_B)
  4691. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4692. dpll |= DPLL_VCO_ENABLE;
  4693. crtc->config.dpll_hw_state.dpll = dpll;
  4694. dpll_md = (crtc->config.pixel_multiplier - 1)
  4695. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4696. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4697. }
  4698. static void vlv_prepare_pll(struct intel_crtc *crtc)
  4699. {
  4700. struct drm_device *dev = crtc->base.dev;
  4701. struct drm_i915_private *dev_priv = dev->dev_private;
  4702. int pipe = crtc->pipe;
  4703. u32 mdiv;
  4704. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4705. u32 coreclk, reg_val;
  4706. mutex_lock(&dev_priv->dpio_lock);
  4707. bestn = crtc->config.dpll.n;
  4708. bestm1 = crtc->config.dpll.m1;
  4709. bestm2 = crtc->config.dpll.m2;
  4710. bestp1 = crtc->config.dpll.p1;
  4711. bestp2 = crtc->config.dpll.p2;
  4712. /* See eDP HDMI DPIO driver vbios notes doc */
  4713. /* PLL B needs special handling */
  4714. if (pipe == PIPE_B)
  4715. vlv_pllb_recal_opamp(dev_priv, pipe);
  4716. /* Set up Tx target for periodic Rcomp update */
  4717. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4718. /* Disable target IRef on PLL */
  4719. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4720. reg_val &= 0x00ffffff;
  4721. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4722. /* Disable fast lock */
  4723. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4724. /* Set idtafcrecal before PLL is enabled */
  4725. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4726. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4727. mdiv |= ((bestn << DPIO_N_SHIFT));
  4728. mdiv |= (1 << DPIO_K_SHIFT);
  4729. /*
  4730. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4731. * but we don't support that).
  4732. * Note: don't use the DAC post divider as it seems unstable.
  4733. */
  4734. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4735. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4736. mdiv |= DPIO_ENABLE_CALIBRATION;
  4737. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4738. /* Set HBR and RBR LPF coefficients */
  4739. if (crtc->config.port_clock == 162000 ||
  4740. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
  4741. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
  4742. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4743. 0x009f0003);
  4744. else
  4745. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4746. 0x00d0000f);
  4747. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
  4748. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
  4749. /* Use SSC source */
  4750. if (pipe == PIPE_A)
  4751. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4752. 0x0df40000);
  4753. else
  4754. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4755. 0x0df70000);
  4756. } else { /* HDMI or VGA */
  4757. /* Use bend source */
  4758. if (pipe == PIPE_A)
  4759. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4760. 0x0df70000);
  4761. else
  4762. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4763. 0x0df40000);
  4764. }
  4765. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  4766. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  4767. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
  4768. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
  4769. coreclk |= 0x01000000;
  4770. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  4771. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  4772. mutex_unlock(&dev_priv->dpio_lock);
  4773. }
  4774. static void chv_update_pll(struct intel_crtc *crtc)
  4775. {
  4776. crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  4777. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  4778. DPLL_VCO_ENABLE;
  4779. if (crtc->pipe != PIPE_A)
  4780. crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4781. crtc->config.dpll_hw_state.dpll_md =
  4782. (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4783. }
  4784. static void chv_prepare_pll(struct intel_crtc *crtc)
  4785. {
  4786. struct drm_device *dev = crtc->base.dev;
  4787. struct drm_i915_private *dev_priv = dev->dev_private;
  4788. int pipe = crtc->pipe;
  4789. int dpll_reg = DPLL(crtc->pipe);
  4790. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  4791. u32 loopfilter, intcoeff;
  4792. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  4793. int refclk;
  4794. bestn = crtc->config.dpll.n;
  4795. bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
  4796. bestm1 = crtc->config.dpll.m1;
  4797. bestm2 = crtc->config.dpll.m2 >> 22;
  4798. bestp1 = crtc->config.dpll.p1;
  4799. bestp2 = crtc->config.dpll.p2;
  4800. /*
  4801. * Enable Refclk and SSC
  4802. */
  4803. I915_WRITE(dpll_reg,
  4804. crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  4805. mutex_lock(&dev_priv->dpio_lock);
  4806. /* p1 and p2 divider */
  4807. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  4808. 5 << DPIO_CHV_S1_DIV_SHIFT |
  4809. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  4810. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  4811. 1 << DPIO_CHV_K_DIV_SHIFT);
  4812. /* Feedback post-divider - m2 */
  4813. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  4814. /* Feedback refclk divider - n and m1 */
  4815. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  4816. DPIO_CHV_M1_DIV_BY_2 |
  4817. 1 << DPIO_CHV_N_DIV_SHIFT);
  4818. /* M2 fraction division */
  4819. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  4820. /* M2 fraction division enable */
  4821. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  4822. DPIO_CHV_FRAC_DIV_EN |
  4823. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  4824. /* Loop filter */
  4825. refclk = i9xx_get_refclk(&crtc->base, 0);
  4826. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  4827. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  4828. if (refclk == 100000)
  4829. intcoeff = 11;
  4830. else if (refclk == 38400)
  4831. intcoeff = 10;
  4832. else
  4833. intcoeff = 9;
  4834. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  4835. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  4836. /* AFC Recal */
  4837. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  4838. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  4839. DPIO_AFC_RECAL);
  4840. mutex_unlock(&dev_priv->dpio_lock);
  4841. }
  4842. static void i9xx_update_pll(struct intel_crtc *crtc,
  4843. intel_clock_t *reduced_clock,
  4844. int num_connectors)
  4845. {
  4846. struct drm_device *dev = crtc->base.dev;
  4847. struct drm_i915_private *dev_priv = dev->dev_private;
  4848. u32 dpll;
  4849. bool is_sdvo;
  4850. struct dpll *clock = &crtc->config.dpll;
  4851. i9xx_update_pll_dividers(crtc, reduced_clock);
  4852. is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
  4853. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  4854. dpll = DPLL_VGA_MODE_DIS;
  4855. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
  4856. dpll |= DPLLB_MODE_LVDS;
  4857. else
  4858. dpll |= DPLLB_MODE_DAC_SERIAL;
  4859. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  4860. dpll |= (crtc->config.pixel_multiplier - 1)
  4861. << SDVO_MULTIPLIER_SHIFT_HIRES;
  4862. }
  4863. if (is_sdvo)
  4864. dpll |= DPLL_SDVO_HIGH_SPEED;
  4865. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
  4866. dpll |= DPLL_SDVO_HIGH_SPEED;
  4867. /* compute bitmask from p1 value */
  4868. if (IS_PINEVIEW(dev))
  4869. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4870. else {
  4871. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4872. if (IS_G4X(dev) && reduced_clock)
  4873. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4874. }
  4875. switch (clock->p2) {
  4876. case 5:
  4877. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4878. break;
  4879. case 7:
  4880. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4881. break;
  4882. case 10:
  4883. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4884. break;
  4885. case 14:
  4886. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4887. break;
  4888. }
  4889. if (INTEL_INFO(dev)->gen >= 4)
  4890. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4891. if (crtc->config.sdvo_tv_clock)
  4892. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4893. else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4894. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4895. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4896. else
  4897. dpll |= PLL_REF_INPUT_DREFCLK;
  4898. dpll |= DPLL_VCO_ENABLE;
  4899. crtc->config.dpll_hw_state.dpll = dpll;
  4900. if (INTEL_INFO(dev)->gen >= 4) {
  4901. u32 dpll_md = (crtc->config.pixel_multiplier - 1)
  4902. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4903. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4904. }
  4905. }
  4906. static void i8xx_update_pll(struct intel_crtc *crtc,
  4907. intel_clock_t *reduced_clock,
  4908. int num_connectors)
  4909. {
  4910. struct drm_device *dev = crtc->base.dev;
  4911. struct drm_i915_private *dev_priv = dev->dev_private;
  4912. u32 dpll;
  4913. struct dpll *clock = &crtc->config.dpll;
  4914. i9xx_update_pll_dividers(crtc, reduced_clock);
  4915. dpll = DPLL_VGA_MODE_DIS;
  4916. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
  4917. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4918. } else {
  4919. if (clock->p1 == 2)
  4920. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4921. else
  4922. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4923. if (clock->p2 == 4)
  4924. dpll |= PLL_P2_DIVIDE_BY_4;
  4925. }
  4926. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
  4927. dpll |= DPLL_DVO_2X_MODE;
  4928. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4929. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4930. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4931. else
  4932. dpll |= PLL_REF_INPUT_DREFCLK;
  4933. dpll |= DPLL_VCO_ENABLE;
  4934. crtc->config.dpll_hw_state.dpll = dpll;
  4935. }
  4936. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  4937. {
  4938. struct drm_device *dev = intel_crtc->base.dev;
  4939. struct drm_i915_private *dev_priv = dev->dev_private;
  4940. enum pipe pipe = intel_crtc->pipe;
  4941. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  4942. struct drm_display_mode *adjusted_mode =
  4943. &intel_crtc->config.adjusted_mode;
  4944. uint32_t crtc_vtotal, crtc_vblank_end;
  4945. int vsyncshift = 0;
  4946. /* We need to be careful not to changed the adjusted mode, for otherwise
  4947. * the hw state checker will get angry at the mismatch. */
  4948. crtc_vtotal = adjusted_mode->crtc_vtotal;
  4949. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  4950. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4951. /* the chip adds 2 halflines automatically */
  4952. crtc_vtotal -= 1;
  4953. crtc_vblank_end -= 1;
  4954. if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  4955. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  4956. else
  4957. vsyncshift = adjusted_mode->crtc_hsync_start -
  4958. adjusted_mode->crtc_htotal / 2;
  4959. if (vsyncshift < 0)
  4960. vsyncshift += adjusted_mode->crtc_htotal;
  4961. }
  4962. if (INTEL_INFO(dev)->gen > 3)
  4963. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  4964. I915_WRITE(HTOTAL(cpu_transcoder),
  4965. (adjusted_mode->crtc_hdisplay - 1) |
  4966. ((adjusted_mode->crtc_htotal - 1) << 16));
  4967. I915_WRITE(HBLANK(cpu_transcoder),
  4968. (adjusted_mode->crtc_hblank_start - 1) |
  4969. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4970. I915_WRITE(HSYNC(cpu_transcoder),
  4971. (adjusted_mode->crtc_hsync_start - 1) |
  4972. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4973. I915_WRITE(VTOTAL(cpu_transcoder),
  4974. (adjusted_mode->crtc_vdisplay - 1) |
  4975. ((crtc_vtotal - 1) << 16));
  4976. I915_WRITE(VBLANK(cpu_transcoder),
  4977. (adjusted_mode->crtc_vblank_start - 1) |
  4978. ((crtc_vblank_end - 1) << 16));
  4979. I915_WRITE(VSYNC(cpu_transcoder),
  4980. (adjusted_mode->crtc_vsync_start - 1) |
  4981. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4982. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  4983. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  4984. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  4985. * bits. */
  4986. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  4987. (pipe == PIPE_B || pipe == PIPE_C))
  4988. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  4989. /* pipesrc controls the size that is scaled from, which should
  4990. * always be the user's requested size.
  4991. */
  4992. I915_WRITE(PIPESRC(pipe),
  4993. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  4994. (intel_crtc->config.pipe_src_h - 1));
  4995. }
  4996. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  4997. struct intel_crtc_config *pipe_config)
  4998. {
  4999. struct drm_device *dev = crtc->base.dev;
  5000. struct drm_i915_private *dev_priv = dev->dev_private;
  5001. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5002. uint32_t tmp;
  5003. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5004. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5005. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5006. tmp = I915_READ(HBLANK(cpu_transcoder));
  5007. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5008. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5009. tmp = I915_READ(HSYNC(cpu_transcoder));
  5010. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5011. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5012. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5013. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5014. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5015. tmp = I915_READ(VBLANK(cpu_transcoder));
  5016. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5017. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5018. tmp = I915_READ(VSYNC(cpu_transcoder));
  5019. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5020. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5021. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5022. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5023. pipe_config->adjusted_mode.crtc_vtotal += 1;
  5024. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  5025. }
  5026. tmp = I915_READ(PIPESRC(crtc->pipe));
  5027. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5028. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5029. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  5030. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  5031. }
  5032. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5033. struct intel_crtc_config *pipe_config)
  5034. {
  5035. mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  5036. mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
  5037. mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  5038. mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  5039. mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  5040. mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  5041. mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  5042. mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  5043. mode->flags = pipe_config->adjusted_mode.flags;
  5044. mode->clock = pipe_config->adjusted_mode.crtc_clock;
  5045. mode->flags |= pipe_config->adjusted_mode.flags;
  5046. }
  5047. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5048. {
  5049. struct drm_device *dev = intel_crtc->base.dev;
  5050. struct drm_i915_private *dev_priv = dev->dev_private;
  5051. uint32_t pipeconf;
  5052. pipeconf = 0;
  5053. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  5054. I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
  5055. pipeconf |= PIPECONF_ENABLE;
  5056. if (intel_crtc->config.double_wide)
  5057. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5058. /* only g4x and later have fancy bpc/dither controls */
  5059. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5060. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5061. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  5062. pipeconf |= PIPECONF_DITHER_EN |
  5063. PIPECONF_DITHER_TYPE_SP;
  5064. switch (intel_crtc->config.pipe_bpp) {
  5065. case 18:
  5066. pipeconf |= PIPECONF_6BPC;
  5067. break;
  5068. case 24:
  5069. pipeconf |= PIPECONF_8BPC;
  5070. break;
  5071. case 30:
  5072. pipeconf |= PIPECONF_10BPC;
  5073. break;
  5074. default:
  5075. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5076. BUG();
  5077. }
  5078. }
  5079. if (HAS_PIPE_CXSR(dev)) {
  5080. if (intel_crtc->lowfreq_avail) {
  5081. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5082. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5083. } else {
  5084. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5085. }
  5086. }
  5087. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5088. if (INTEL_INFO(dev)->gen < 4 ||
  5089. intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  5090. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5091. else
  5092. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5093. } else
  5094. pipeconf |= PIPECONF_PROGRESSIVE;
  5095. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  5096. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5097. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5098. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5099. }
  5100. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  5101. int x, int y,
  5102. struct drm_framebuffer *fb)
  5103. {
  5104. struct drm_device *dev = crtc->dev;
  5105. struct drm_i915_private *dev_priv = dev->dev_private;
  5106. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5107. int refclk, num_connectors = 0;
  5108. intel_clock_t clock, reduced_clock;
  5109. bool ok, has_reduced_clock = false;
  5110. bool is_lvds = false, is_dsi = false;
  5111. struct intel_encoder *encoder;
  5112. const intel_limit_t *limit;
  5113. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5114. switch (encoder->type) {
  5115. case INTEL_OUTPUT_LVDS:
  5116. is_lvds = true;
  5117. break;
  5118. case INTEL_OUTPUT_DSI:
  5119. is_dsi = true;
  5120. break;
  5121. }
  5122. num_connectors++;
  5123. }
  5124. if (is_dsi)
  5125. return 0;
  5126. if (!intel_crtc->config.clock_set) {
  5127. refclk = i9xx_get_refclk(crtc, num_connectors);
  5128. /*
  5129. * Returns a set of divisors for the desired target clock with
  5130. * the given refclk, or FALSE. The returned values represent
  5131. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5132. * 2) / p1 / p2.
  5133. */
  5134. limit = intel_limit(crtc, refclk);
  5135. ok = dev_priv->display.find_dpll(limit, crtc,
  5136. intel_crtc->config.port_clock,
  5137. refclk, NULL, &clock);
  5138. if (!ok) {
  5139. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5140. return -EINVAL;
  5141. }
  5142. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5143. /*
  5144. * Ensure we match the reduced clock's P to the target
  5145. * clock. If the clocks don't match, we can't switch
  5146. * the display clock by using the FP0/FP1. In such case
  5147. * we will disable the LVDS downclock feature.
  5148. */
  5149. has_reduced_clock =
  5150. dev_priv->display.find_dpll(limit, crtc,
  5151. dev_priv->lvds_downclock,
  5152. refclk, &clock,
  5153. &reduced_clock);
  5154. }
  5155. /* Compat-code for transition, will disappear. */
  5156. intel_crtc->config.dpll.n = clock.n;
  5157. intel_crtc->config.dpll.m1 = clock.m1;
  5158. intel_crtc->config.dpll.m2 = clock.m2;
  5159. intel_crtc->config.dpll.p1 = clock.p1;
  5160. intel_crtc->config.dpll.p2 = clock.p2;
  5161. }
  5162. if (IS_GEN2(dev)) {
  5163. i8xx_update_pll(intel_crtc,
  5164. has_reduced_clock ? &reduced_clock : NULL,
  5165. num_connectors);
  5166. } else if (IS_CHERRYVIEW(dev)) {
  5167. chv_update_pll(intel_crtc);
  5168. } else if (IS_VALLEYVIEW(dev)) {
  5169. vlv_update_pll(intel_crtc);
  5170. } else {
  5171. i9xx_update_pll(intel_crtc,
  5172. has_reduced_clock ? &reduced_clock : NULL,
  5173. num_connectors);
  5174. }
  5175. return 0;
  5176. }
  5177. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5178. struct intel_crtc_config *pipe_config)
  5179. {
  5180. struct drm_device *dev = crtc->base.dev;
  5181. struct drm_i915_private *dev_priv = dev->dev_private;
  5182. uint32_t tmp;
  5183. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5184. return;
  5185. tmp = I915_READ(PFIT_CONTROL);
  5186. if (!(tmp & PFIT_ENABLE))
  5187. return;
  5188. /* Check whether the pfit is attached to our pipe. */
  5189. if (INTEL_INFO(dev)->gen < 4) {
  5190. if (crtc->pipe != PIPE_B)
  5191. return;
  5192. } else {
  5193. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5194. return;
  5195. }
  5196. pipe_config->gmch_pfit.control = tmp;
  5197. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5198. if (INTEL_INFO(dev)->gen < 5)
  5199. pipe_config->gmch_pfit.lvds_border_bits =
  5200. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5201. }
  5202. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5203. struct intel_crtc_config *pipe_config)
  5204. {
  5205. struct drm_device *dev = crtc->base.dev;
  5206. struct drm_i915_private *dev_priv = dev->dev_private;
  5207. int pipe = pipe_config->cpu_transcoder;
  5208. intel_clock_t clock;
  5209. u32 mdiv;
  5210. int refclk = 100000;
  5211. /* In case of MIPI DPLL will not even be used */
  5212. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5213. return;
  5214. mutex_lock(&dev_priv->dpio_lock);
  5215. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5216. mutex_unlock(&dev_priv->dpio_lock);
  5217. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5218. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5219. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5220. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5221. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5222. vlv_clock(refclk, &clock);
  5223. /* clock.dot is the fast clock */
  5224. pipe_config->port_clock = clock.dot / 5;
  5225. }
  5226. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5227. struct intel_plane_config *plane_config)
  5228. {
  5229. struct drm_device *dev = crtc->base.dev;
  5230. struct drm_i915_private *dev_priv = dev->dev_private;
  5231. u32 val, base, offset;
  5232. int pipe = crtc->pipe, plane = crtc->plane;
  5233. int fourcc, pixel_format;
  5234. int aligned_height;
  5235. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5236. if (!crtc->base.primary->fb) {
  5237. DRM_DEBUG_KMS("failed to alloc fb\n");
  5238. return;
  5239. }
  5240. val = I915_READ(DSPCNTR(plane));
  5241. if (INTEL_INFO(dev)->gen >= 4)
  5242. if (val & DISPPLANE_TILED)
  5243. plane_config->tiled = true;
  5244. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5245. fourcc = intel_format_to_fourcc(pixel_format);
  5246. crtc->base.primary->fb->pixel_format = fourcc;
  5247. crtc->base.primary->fb->bits_per_pixel =
  5248. drm_format_plane_cpp(fourcc, 0) * 8;
  5249. if (INTEL_INFO(dev)->gen >= 4) {
  5250. if (plane_config->tiled)
  5251. offset = I915_READ(DSPTILEOFF(plane));
  5252. else
  5253. offset = I915_READ(DSPLINOFF(plane));
  5254. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5255. } else {
  5256. base = I915_READ(DSPADDR(plane));
  5257. }
  5258. plane_config->base = base;
  5259. val = I915_READ(PIPESRC(pipe));
  5260. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5261. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5262. val = I915_READ(DSPSTRIDE(pipe));
  5263. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  5264. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5265. plane_config->tiled);
  5266. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  5267. aligned_height);
  5268. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5269. pipe, plane, crtc->base.primary->fb->width,
  5270. crtc->base.primary->fb->height,
  5271. crtc->base.primary->fb->bits_per_pixel, base,
  5272. crtc->base.primary->fb->pitches[0],
  5273. plane_config->size);
  5274. }
  5275. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5276. struct intel_crtc_config *pipe_config)
  5277. {
  5278. struct drm_device *dev = crtc->base.dev;
  5279. struct drm_i915_private *dev_priv = dev->dev_private;
  5280. int pipe = pipe_config->cpu_transcoder;
  5281. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5282. intel_clock_t clock;
  5283. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5284. int refclk = 100000;
  5285. mutex_lock(&dev_priv->dpio_lock);
  5286. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5287. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5288. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5289. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5290. mutex_unlock(&dev_priv->dpio_lock);
  5291. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5292. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5293. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5294. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5295. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5296. chv_clock(refclk, &clock);
  5297. /* clock.dot is the fast clock */
  5298. pipe_config->port_clock = clock.dot / 5;
  5299. }
  5300. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5301. struct intel_crtc_config *pipe_config)
  5302. {
  5303. struct drm_device *dev = crtc->base.dev;
  5304. struct drm_i915_private *dev_priv = dev->dev_private;
  5305. uint32_t tmp;
  5306. if (!intel_display_power_enabled(dev_priv,
  5307. POWER_DOMAIN_PIPE(crtc->pipe)))
  5308. return false;
  5309. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5310. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5311. tmp = I915_READ(PIPECONF(crtc->pipe));
  5312. if (!(tmp & PIPECONF_ENABLE))
  5313. return false;
  5314. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5315. switch (tmp & PIPECONF_BPC_MASK) {
  5316. case PIPECONF_6BPC:
  5317. pipe_config->pipe_bpp = 18;
  5318. break;
  5319. case PIPECONF_8BPC:
  5320. pipe_config->pipe_bpp = 24;
  5321. break;
  5322. case PIPECONF_10BPC:
  5323. pipe_config->pipe_bpp = 30;
  5324. break;
  5325. default:
  5326. break;
  5327. }
  5328. }
  5329. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5330. pipe_config->limited_color_range = true;
  5331. if (INTEL_INFO(dev)->gen < 4)
  5332. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5333. intel_get_pipe_timings(crtc, pipe_config);
  5334. i9xx_get_pfit_config(crtc, pipe_config);
  5335. if (INTEL_INFO(dev)->gen >= 4) {
  5336. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5337. pipe_config->pixel_multiplier =
  5338. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5339. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5340. pipe_config->dpll_hw_state.dpll_md = tmp;
  5341. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5342. tmp = I915_READ(DPLL(crtc->pipe));
  5343. pipe_config->pixel_multiplier =
  5344. ((tmp & SDVO_MULTIPLIER_MASK)
  5345. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5346. } else {
  5347. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5348. * port and will be fixed up in the encoder->get_config
  5349. * function. */
  5350. pipe_config->pixel_multiplier = 1;
  5351. }
  5352. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5353. if (!IS_VALLEYVIEW(dev)) {
  5354. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5355. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5356. } else {
  5357. /* Mask out read-only status bits. */
  5358. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5359. DPLL_PORTC_READY_MASK |
  5360. DPLL_PORTB_READY_MASK);
  5361. }
  5362. if (IS_CHERRYVIEW(dev))
  5363. chv_crtc_clock_get(crtc, pipe_config);
  5364. else if (IS_VALLEYVIEW(dev))
  5365. vlv_crtc_clock_get(crtc, pipe_config);
  5366. else
  5367. i9xx_crtc_clock_get(crtc, pipe_config);
  5368. return true;
  5369. }
  5370. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5371. {
  5372. struct drm_i915_private *dev_priv = dev->dev_private;
  5373. struct intel_encoder *encoder;
  5374. u32 val, final;
  5375. bool has_lvds = false;
  5376. bool has_cpu_edp = false;
  5377. bool has_panel = false;
  5378. bool has_ck505 = false;
  5379. bool can_ssc = false;
  5380. /* We need to take the global config into account */
  5381. for_each_intel_encoder(dev, encoder) {
  5382. switch (encoder->type) {
  5383. case INTEL_OUTPUT_LVDS:
  5384. has_panel = true;
  5385. has_lvds = true;
  5386. break;
  5387. case INTEL_OUTPUT_EDP:
  5388. has_panel = true;
  5389. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5390. has_cpu_edp = true;
  5391. break;
  5392. }
  5393. }
  5394. if (HAS_PCH_IBX(dev)) {
  5395. has_ck505 = dev_priv->vbt.display_clock_mode;
  5396. can_ssc = has_ck505;
  5397. } else {
  5398. has_ck505 = false;
  5399. can_ssc = true;
  5400. }
  5401. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5402. has_panel, has_lvds, has_ck505);
  5403. /* Ironlake: try to setup display ref clock before DPLL
  5404. * enabling. This is only under driver's control after
  5405. * PCH B stepping, previous chipset stepping should be
  5406. * ignoring this setting.
  5407. */
  5408. val = I915_READ(PCH_DREF_CONTROL);
  5409. /* As we must carefully and slowly disable/enable each source in turn,
  5410. * compute the final state we want first and check if we need to
  5411. * make any changes at all.
  5412. */
  5413. final = val;
  5414. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5415. if (has_ck505)
  5416. final |= DREF_NONSPREAD_CK505_ENABLE;
  5417. else
  5418. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5419. final &= ~DREF_SSC_SOURCE_MASK;
  5420. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5421. final &= ~DREF_SSC1_ENABLE;
  5422. if (has_panel) {
  5423. final |= DREF_SSC_SOURCE_ENABLE;
  5424. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5425. final |= DREF_SSC1_ENABLE;
  5426. if (has_cpu_edp) {
  5427. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5428. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5429. else
  5430. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5431. } else
  5432. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5433. } else {
  5434. final |= DREF_SSC_SOURCE_DISABLE;
  5435. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5436. }
  5437. if (final == val)
  5438. return;
  5439. /* Always enable nonspread source */
  5440. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5441. if (has_ck505)
  5442. val |= DREF_NONSPREAD_CK505_ENABLE;
  5443. else
  5444. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5445. if (has_panel) {
  5446. val &= ~DREF_SSC_SOURCE_MASK;
  5447. val |= DREF_SSC_SOURCE_ENABLE;
  5448. /* SSC must be turned on before enabling the CPU output */
  5449. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5450. DRM_DEBUG_KMS("Using SSC on panel\n");
  5451. val |= DREF_SSC1_ENABLE;
  5452. } else
  5453. val &= ~DREF_SSC1_ENABLE;
  5454. /* Get SSC going before enabling the outputs */
  5455. I915_WRITE(PCH_DREF_CONTROL, val);
  5456. POSTING_READ(PCH_DREF_CONTROL);
  5457. udelay(200);
  5458. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5459. /* Enable CPU source on CPU attached eDP */
  5460. if (has_cpu_edp) {
  5461. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5462. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5463. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5464. } else
  5465. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5466. } else
  5467. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5468. I915_WRITE(PCH_DREF_CONTROL, val);
  5469. POSTING_READ(PCH_DREF_CONTROL);
  5470. udelay(200);
  5471. } else {
  5472. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5473. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5474. /* Turn off CPU output */
  5475. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5476. I915_WRITE(PCH_DREF_CONTROL, val);
  5477. POSTING_READ(PCH_DREF_CONTROL);
  5478. udelay(200);
  5479. /* Turn off the SSC source */
  5480. val &= ~DREF_SSC_SOURCE_MASK;
  5481. val |= DREF_SSC_SOURCE_DISABLE;
  5482. /* Turn off SSC1 */
  5483. val &= ~DREF_SSC1_ENABLE;
  5484. I915_WRITE(PCH_DREF_CONTROL, val);
  5485. POSTING_READ(PCH_DREF_CONTROL);
  5486. udelay(200);
  5487. }
  5488. BUG_ON(val != final);
  5489. }
  5490. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5491. {
  5492. uint32_t tmp;
  5493. tmp = I915_READ(SOUTH_CHICKEN2);
  5494. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5495. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5496. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5497. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5498. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5499. tmp = I915_READ(SOUTH_CHICKEN2);
  5500. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5501. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5502. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5503. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5504. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5505. }
  5506. /* WaMPhyProgramming:hsw */
  5507. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5508. {
  5509. uint32_t tmp;
  5510. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5511. tmp &= ~(0xFF << 24);
  5512. tmp |= (0x12 << 24);
  5513. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5514. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5515. tmp |= (1 << 11);
  5516. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5517. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5518. tmp |= (1 << 11);
  5519. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5520. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5521. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5522. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5523. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5524. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5525. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5526. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5527. tmp &= ~(7 << 13);
  5528. tmp |= (5 << 13);
  5529. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5530. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5531. tmp &= ~(7 << 13);
  5532. tmp |= (5 << 13);
  5533. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5534. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5535. tmp &= ~0xFF;
  5536. tmp |= 0x1C;
  5537. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5538. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5539. tmp &= ~0xFF;
  5540. tmp |= 0x1C;
  5541. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5542. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5543. tmp &= ~(0xFF << 16);
  5544. tmp |= (0x1C << 16);
  5545. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5546. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5547. tmp &= ~(0xFF << 16);
  5548. tmp |= (0x1C << 16);
  5549. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5550. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5551. tmp |= (1 << 27);
  5552. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5553. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5554. tmp |= (1 << 27);
  5555. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5556. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5557. tmp &= ~(0xF << 28);
  5558. tmp |= (4 << 28);
  5559. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5560. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5561. tmp &= ~(0xF << 28);
  5562. tmp |= (4 << 28);
  5563. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5564. }
  5565. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5566. * Programming" based on the parameters passed:
  5567. * - Sequence to enable CLKOUT_DP
  5568. * - Sequence to enable CLKOUT_DP without spread
  5569. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5570. */
  5571. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5572. bool with_fdi)
  5573. {
  5574. struct drm_i915_private *dev_priv = dev->dev_private;
  5575. uint32_t reg, tmp;
  5576. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5577. with_spread = true;
  5578. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5579. with_fdi, "LP PCH doesn't have FDI\n"))
  5580. with_fdi = false;
  5581. mutex_lock(&dev_priv->dpio_lock);
  5582. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5583. tmp &= ~SBI_SSCCTL_DISABLE;
  5584. tmp |= SBI_SSCCTL_PATHALT;
  5585. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5586. udelay(24);
  5587. if (with_spread) {
  5588. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5589. tmp &= ~SBI_SSCCTL_PATHALT;
  5590. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5591. if (with_fdi) {
  5592. lpt_reset_fdi_mphy(dev_priv);
  5593. lpt_program_fdi_mphy(dev_priv);
  5594. }
  5595. }
  5596. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5597. SBI_GEN0 : SBI_DBUFF0;
  5598. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5599. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5600. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5601. mutex_unlock(&dev_priv->dpio_lock);
  5602. }
  5603. /* Sequence to disable CLKOUT_DP */
  5604. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5605. {
  5606. struct drm_i915_private *dev_priv = dev->dev_private;
  5607. uint32_t reg, tmp;
  5608. mutex_lock(&dev_priv->dpio_lock);
  5609. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5610. SBI_GEN0 : SBI_DBUFF0;
  5611. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5612. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5613. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5614. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5615. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5616. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5617. tmp |= SBI_SSCCTL_PATHALT;
  5618. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5619. udelay(32);
  5620. }
  5621. tmp |= SBI_SSCCTL_DISABLE;
  5622. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5623. }
  5624. mutex_unlock(&dev_priv->dpio_lock);
  5625. }
  5626. static void lpt_init_pch_refclk(struct drm_device *dev)
  5627. {
  5628. struct intel_encoder *encoder;
  5629. bool has_vga = false;
  5630. for_each_intel_encoder(dev, encoder) {
  5631. switch (encoder->type) {
  5632. case INTEL_OUTPUT_ANALOG:
  5633. has_vga = true;
  5634. break;
  5635. }
  5636. }
  5637. if (has_vga)
  5638. lpt_enable_clkout_dp(dev, true, true);
  5639. else
  5640. lpt_disable_clkout_dp(dev);
  5641. }
  5642. /*
  5643. * Initialize reference clocks when the driver loads
  5644. */
  5645. void intel_init_pch_refclk(struct drm_device *dev)
  5646. {
  5647. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5648. ironlake_init_pch_refclk(dev);
  5649. else if (HAS_PCH_LPT(dev))
  5650. lpt_init_pch_refclk(dev);
  5651. }
  5652. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5653. {
  5654. struct drm_device *dev = crtc->dev;
  5655. struct drm_i915_private *dev_priv = dev->dev_private;
  5656. struct intel_encoder *encoder;
  5657. int num_connectors = 0;
  5658. bool is_lvds = false;
  5659. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5660. switch (encoder->type) {
  5661. case INTEL_OUTPUT_LVDS:
  5662. is_lvds = true;
  5663. break;
  5664. }
  5665. num_connectors++;
  5666. }
  5667. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5668. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5669. dev_priv->vbt.lvds_ssc_freq);
  5670. return dev_priv->vbt.lvds_ssc_freq;
  5671. }
  5672. return 120000;
  5673. }
  5674. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5675. {
  5676. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5677. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5678. int pipe = intel_crtc->pipe;
  5679. uint32_t val;
  5680. val = 0;
  5681. switch (intel_crtc->config.pipe_bpp) {
  5682. case 18:
  5683. val |= PIPECONF_6BPC;
  5684. break;
  5685. case 24:
  5686. val |= PIPECONF_8BPC;
  5687. break;
  5688. case 30:
  5689. val |= PIPECONF_10BPC;
  5690. break;
  5691. case 36:
  5692. val |= PIPECONF_12BPC;
  5693. break;
  5694. default:
  5695. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5696. BUG();
  5697. }
  5698. if (intel_crtc->config.dither)
  5699. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5700. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5701. val |= PIPECONF_INTERLACED_ILK;
  5702. else
  5703. val |= PIPECONF_PROGRESSIVE;
  5704. if (intel_crtc->config.limited_color_range)
  5705. val |= PIPECONF_COLOR_RANGE_SELECT;
  5706. I915_WRITE(PIPECONF(pipe), val);
  5707. POSTING_READ(PIPECONF(pipe));
  5708. }
  5709. /*
  5710. * Set up the pipe CSC unit.
  5711. *
  5712. * Currently only full range RGB to limited range RGB conversion
  5713. * is supported, but eventually this should handle various
  5714. * RGB<->YCbCr scenarios as well.
  5715. */
  5716. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  5717. {
  5718. struct drm_device *dev = crtc->dev;
  5719. struct drm_i915_private *dev_priv = dev->dev_private;
  5720. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5721. int pipe = intel_crtc->pipe;
  5722. uint16_t coeff = 0x7800; /* 1.0 */
  5723. /*
  5724. * TODO: Check what kind of values actually come out of the pipe
  5725. * with these coeff/postoff values and adjust to get the best
  5726. * accuracy. Perhaps we even need to take the bpc value into
  5727. * consideration.
  5728. */
  5729. if (intel_crtc->config.limited_color_range)
  5730. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  5731. /*
  5732. * GY/GU and RY/RU should be the other way around according
  5733. * to BSpec, but reality doesn't agree. Just set them up in
  5734. * a way that results in the correct picture.
  5735. */
  5736. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  5737. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  5738. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  5739. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  5740. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  5741. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  5742. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  5743. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  5744. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  5745. if (INTEL_INFO(dev)->gen > 6) {
  5746. uint16_t postoff = 0;
  5747. if (intel_crtc->config.limited_color_range)
  5748. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  5749. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  5750. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  5751. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  5752. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  5753. } else {
  5754. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  5755. if (intel_crtc->config.limited_color_range)
  5756. mode |= CSC_BLACK_SCREEN_OFFSET;
  5757. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  5758. }
  5759. }
  5760. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  5761. {
  5762. struct drm_device *dev = crtc->dev;
  5763. struct drm_i915_private *dev_priv = dev->dev_private;
  5764. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5765. enum pipe pipe = intel_crtc->pipe;
  5766. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5767. uint32_t val;
  5768. val = 0;
  5769. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  5770. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5771. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5772. val |= PIPECONF_INTERLACED_ILK;
  5773. else
  5774. val |= PIPECONF_PROGRESSIVE;
  5775. I915_WRITE(PIPECONF(cpu_transcoder), val);
  5776. POSTING_READ(PIPECONF(cpu_transcoder));
  5777. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  5778. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  5779. if (IS_BROADWELL(dev)) {
  5780. val = 0;
  5781. switch (intel_crtc->config.pipe_bpp) {
  5782. case 18:
  5783. val |= PIPEMISC_DITHER_6_BPC;
  5784. break;
  5785. case 24:
  5786. val |= PIPEMISC_DITHER_8_BPC;
  5787. break;
  5788. case 30:
  5789. val |= PIPEMISC_DITHER_10_BPC;
  5790. break;
  5791. case 36:
  5792. val |= PIPEMISC_DITHER_12_BPC;
  5793. break;
  5794. default:
  5795. /* Case prevented by pipe_config_set_bpp. */
  5796. BUG();
  5797. }
  5798. if (intel_crtc->config.dither)
  5799. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  5800. I915_WRITE(PIPEMISC(pipe), val);
  5801. }
  5802. }
  5803. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  5804. intel_clock_t *clock,
  5805. bool *has_reduced_clock,
  5806. intel_clock_t *reduced_clock)
  5807. {
  5808. struct drm_device *dev = crtc->dev;
  5809. struct drm_i915_private *dev_priv = dev->dev_private;
  5810. struct intel_encoder *intel_encoder;
  5811. int refclk;
  5812. const intel_limit_t *limit;
  5813. bool ret, is_lvds = false;
  5814. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5815. switch (intel_encoder->type) {
  5816. case INTEL_OUTPUT_LVDS:
  5817. is_lvds = true;
  5818. break;
  5819. }
  5820. }
  5821. refclk = ironlake_get_refclk(crtc);
  5822. /*
  5823. * Returns a set of divisors for the desired target clock with the given
  5824. * refclk, or FALSE. The returned values represent the clock equation:
  5825. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  5826. */
  5827. limit = intel_limit(crtc, refclk);
  5828. ret = dev_priv->display.find_dpll(limit, crtc,
  5829. to_intel_crtc(crtc)->config.port_clock,
  5830. refclk, NULL, clock);
  5831. if (!ret)
  5832. return false;
  5833. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5834. /*
  5835. * Ensure we match the reduced clock's P to the target clock.
  5836. * If the clocks don't match, we can't switch the display clock
  5837. * by using the FP0/FP1. In such case we will disable the LVDS
  5838. * downclock feature.
  5839. */
  5840. *has_reduced_clock =
  5841. dev_priv->display.find_dpll(limit, crtc,
  5842. dev_priv->lvds_downclock,
  5843. refclk, clock,
  5844. reduced_clock);
  5845. }
  5846. return true;
  5847. }
  5848. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  5849. {
  5850. /*
  5851. * Account for spread spectrum to avoid
  5852. * oversubscribing the link. Max center spread
  5853. * is 2.5%; use 5% for safety's sake.
  5854. */
  5855. u32 bps = target_clock * bpp * 21 / 20;
  5856. return DIV_ROUND_UP(bps, link_bw * 8);
  5857. }
  5858. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  5859. {
  5860. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  5861. }
  5862. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  5863. u32 *fp,
  5864. intel_clock_t *reduced_clock, u32 *fp2)
  5865. {
  5866. struct drm_crtc *crtc = &intel_crtc->base;
  5867. struct drm_device *dev = crtc->dev;
  5868. struct drm_i915_private *dev_priv = dev->dev_private;
  5869. struct intel_encoder *intel_encoder;
  5870. uint32_t dpll;
  5871. int factor, num_connectors = 0;
  5872. bool is_lvds = false, is_sdvo = false;
  5873. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5874. switch (intel_encoder->type) {
  5875. case INTEL_OUTPUT_LVDS:
  5876. is_lvds = true;
  5877. break;
  5878. case INTEL_OUTPUT_SDVO:
  5879. case INTEL_OUTPUT_HDMI:
  5880. is_sdvo = true;
  5881. break;
  5882. }
  5883. num_connectors++;
  5884. }
  5885. /* Enable autotuning of the PLL clock (if permissible) */
  5886. factor = 21;
  5887. if (is_lvds) {
  5888. if ((intel_panel_use_ssc(dev_priv) &&
  5889. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  5890. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  5891. factor = 25;
  5892. } else if (intel_crtc->config.sdvo_tv_clock)
  5893. factor = 20;
  5894. if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
  5895. *fp |= FP_CB_TUNE;
  5896. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  5897. *fp2 |= FP_CB_TUNE;
  5898. dpll = 0;
  5899. if (is_lvds)
  5900. dpll |= DPLLB_MODE_LVDS;
  5901. else
  5902. dpll |= DPLLB_MODE_DAC_SERIAL;
  5903. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  5904. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  5905. if (is_sdvo)
  5906. dpll |= DPLL_SDVO_HIGH_SPEED;
  5907. if (intel_crtc->config.has_dp_encoder)
  5908. dpll |= DPLL_SDVO_HIGH_SPEED;
  5909. /* compute bitmask from p1 value */
  5910. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5911. /* also FPA1 */
  5912. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5913. switch (intel_crtc->config.dpll.p2) {
  5914. case 5:
  5915. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5916. break;
  5917. case 7:
  5918. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5919. break;
  5920. case 10:
  5921. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5922. break;
  5923. case 14:
  5924. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5925. break;
  5926. }
  5927. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5928. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5929. else
  5930. dpll |= PLL_REF_INPUT_DREFCLK;
  5931. return dpll | DPLL_VCO_ENABLE;
  5932. }
  5933. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  5934. int x, int y,
  5935. struct drm_framebuffer *fb)
  5936. {
  5937. struct drm_device *dev = crtc->dev;
  5938. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5939. int num_connectors = 0;
  5940. intel_clock_t clock, reduced_clock;
  5941. u32 dpll = 0, fp = 0, fp2 = 0;
  5942. bool ok, has_reduced_clock = false;
  5943. bool is_lvds = false;
  5944. struct intel_encoder *encoder;
  5945. struct intel_shared_dpll *pll;
  5946. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5947. switch (encoder->type) {
  5948. case INTEL_OUTPUT_LVDS:
  5949. is_lvds = true;
  5950. break;
  5951. }
  5952. num_connectors++;
  5953. }
  5954. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  5955. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  5956. ok = ironlake_compute_clocks(crtc, &clock,
  5957. &has_reduced_clock, &reduced_clock);
  5958. if (!ok && !intel_crtc->config.clock_set) {
  5959. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5960. return -EINVAL;
  5961. }
  5962. /* Compat-code for transition, will disappear. */
  5963. if (!intel_crtc->config.clock_set) {
  5964. intel_crtc->config.dpll.n = clock.n;
  5965. intel_crtc->config.dpll.m1 = clock.m1;
  5966. intel_crtc->config.dpll.m2 = clock.m2;
  5967. intel_crtc->config.dpll.p1 = clock.p1;
  5968. intel_crtc->config.dpll.p2 = clock.p2;
  5969. }
  5970. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  5971. if (intel_crtc->config.has_pch_encoder) {
  5972. fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
  5973. if (has_reduced_clock)
  5974. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  5975. dpll = ironlake_compute_dpll(intel_crtc,
  5976. &fp, &reduced_clock,
  5977. has_reduced_clock ? &fp2 : NULL);
  5978. intel_crtc->config.dpll_hw_state.dpll = dpll;
  5979. intel_crtc->config.dpll_hw_state.fp0 = fp;
  5980. if (has_reduced_clock)
  5981. intel_crtc->config.dpll_hw_state.fp1 = fp2;
  5982. else
  5983. intel_crtc->config.dpll_hw_state.fp1 = fp;
  5984. pll = intel_get_shared_dpll(intel_crtc);
  5985. if (pll == NULL) {
  5986. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  5987. pipe_name(intel_crtc->pipe));
  5988. return -EINVAL;
  5989. }
  5990. } else
  5991. intel_put_shared_dpll(intel_crtc);
  5992. if (is_lvds && has_reduced_clock && i915.powersave)
  5993. intel_crtc->lowfreq_avail = true;
  5994. else
  5995. intel_crtc->lowfreq_avail = false;
  5996. return 0;
  5997. }
  5998. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  5999. struct intel_link_m_n *m_n)
  6000. {
  6001. struct drm_device *dev = crtc->base.dev;
  6002. struct drm_i915_private *dev_priv = dev->dev_private;
  6003. enum pipe pipe = crtc->pipe;
  6004. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6005. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6006. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6007. & ~TU_SIZE_MASK;
  6008. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6009. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6010. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6011. }
  6012. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6013. enum transcoder transcoder,
  6014. struct intel_link_m_n *m_n,
  6015. struct intel_link_m_n *m2_n2)
  6016. {
  6017. struct drm_device *dev = crtc->base.dev;
  6018. struct drm_i915_private *dev_priv = dev->dev_private;
  6019. enum pipe pipe = crtc->pipe;
  6020. if (INTEL_INFO(dev)->gen >= 5) {
  6021. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6022. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6023. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6024. & ~TU_SIZE_MASK;
  6025. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6026. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6027. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6028. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6029. * gen < 8) and if DRRS is supported (to make sure the
  6030. * registers are not unnecessarily read).
  6031. */
  6032. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6033. crtc->config.has_drrs) {
  6034. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6035. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6036. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6037. & ~TU_SIZE_MASK;
  6038. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6039. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6040. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6041. }
  6042. } else {
  6043. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6044. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6045. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6046. & ~TU_SIZE_MASK;
  6047. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6048. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6049. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6050. }
  6051. }
  6052. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6053. struct intel_crtc_config *pipe_config)
  6054. {
  6055. if (crtc->config.has_pch_encoder)
  6056. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6057. else
  6058. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6059. &pipe_config->dp_m_n,
  6060. &pipe_config->dp_m2_n2);
  6061. }
  6062. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6063. struct intel_crtc_config *pipe_config)
  6064. {
  6065. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6066. &pipe_config->fdi_m_n, NULL);
  6067. }
  6068. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6069. struct intel_crtc_config *pipe_config)
  6070. {
  6071. struct drm_device *dev = crtc->base.dev;
  6072. struct drm_i915_private *dev_priv = dev->dev_private;
  6073. uint32_t tmp;
  6074. tmp = I915_READ(PF_CTL(crtc->pipe));
  6075. if (tmp & PF_ENABLE) {
  6076. pipe_config->pch_pfit.enabled = true;
  6077. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6078. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6079. /* We currently do not free assignements of panel fitters on
  6080. * ivb/hsw (since we don't use the higher upscaling modes which
  6081. * differentiates them) so just WARN about this case for now. */
  6082. if (IS_GEN7(dev)) {
  6083. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6084. PF_PIPE_SEL_IVB(crtc->pipe));
  6085. }
  6086. }
  6087. }
  6088. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  6089. struct intel_plane_config *plane_config)
  6090. {
  6091. struct drm_device *dev = crtc->base.dev;
  6092. struct drm_i915_private *dev_priv = dev->dev_private;
  6093. u32 val, base, offset;
  6094. int pipe = crtc->pipe, plane = crtc->plane;
  6095. int fourcc, pixel_format;
  6096. int aligned_height;
  6097. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  6098. if (!crtc->base.primary->fb) {
  6099. DRM_DEBUG_KMS("failed to alloc fb\n");
  6100. return;
  6101. }
  6102. val = I915_READ(DSPCNTR(plane));
  6103. if (INTEL_INFO(dev)->gen >= 4)
  6104. if (val & DISPPLANE_TILED)
  6105. plane_config->tiled = true;
  6106. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6107. fourcc = intel_format_to_fourcc(pixel_format);
  6108. crtc->base.primary->fb->pixel_format = fourcc;
  6109. crtc->base.primary->fb->bits_per_pixel =
  6110. drm_format_plane_cpp(fourcc, 0) * 8;
  6111. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6112. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6113. offset = I915_READ(DSPOFFSET(plane));
  6114. } else {
  6115. if (plane_config->tiled)
  6116. offset = I915_READ(DSPTILEOFF(plane));
  6117. else
  6118. offset = I915_READ(DSPLINOFF(plane));
  6119. }
  6120. plane_config->base = base;
  6121. val = I915_READ(PIPESRC(pipe));
  6122. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  6123. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  6124. val = I915_READ(DSPSTRIDE(pipe));
  6125. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  6126. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  6127. plane_config->tiled);
  6128. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  6129. aligned_height);
  6130. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6131. pipe, plane, crtc->base.primary->fb->width,
  6132. crtc->base.primary->fb->height,
  6133. crtc->base.primary->fb->bits_per_pixel, base,
  6134. crtc->base.primary->fb->pitches[0],
  6135. plane_config->size);
  6136. }
  6137. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6138. struct intel_crtc_config *pipe_config)
  6139. {
  6140. struct drm_device *dev = crtc->base.dev;
  6141. struct drm_i915_private *dev_priv = dev->dev_private;
  6142. uint32_t tmp;
  6143. if (!intel_display_power_enabled(dev_priv,
  6144. POWER_DOMAIN_PIPE(crtc->pipe)))
  6145. return false;
  6146. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6147. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6148. tmp = I915_READ(PIPECONF(crtc->pipe));
  6149. if (!(tmp & PIPECONF_ENABLE))
  6150. return false;
  6151. switch (tmp & PIPECONF_BPC_MASK) {
  6152. case PIPECONF_6BPC:
  6153. pipe_config->pipe_bpp = 18;
  6154. break;
  6155. case PIPECONF_8BPC:
  6156. pipe_config->pipe_bpp = 24;
  6157. break;
  6158. case PIPECONF_10BPC:
  6159. pipe_config->pipe_bpp = 30;
  6160. break;
  6161. case PIPECONF_12BPC:
  6162. pipe_config->pipe_bpp = 36;
  6163. break;
  6164. default:
  6165. break;
  6166. }
  6167. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6168. pipe_config->limited_color_range = true;
  6169. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6170. struct intel_shared_dpll *pll;
  6171. pipe_config->has_pch_encoder = true;
  6172. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6173. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6174. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6175. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6176. if (HAS_PCH_IBX(dev_priv->dev)) {
  6177. pipe_config->shared_dpll =
  6178. (enum intel_dpll_id) crtc->pipe;
  6179. } else {
  6180. tmp = I915_READ(PCH_DPLL_SEL);
  6181. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6182. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6183. else
  6184. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6185. }
  6186. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6187. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6188. &pipe_config->dpll_hw_state));
  6189. tmp = pipe_config->dpll_hw_state.dpll;
  6190. pipe_config->pixel_multiplier =
  6191. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6192. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6193. ironlake_pch_clock_get(crtc, pipe_config);
  6194. } else {
  6195. pipe_config->pixel_multiplier = 1;
  6196. }
  6197. intel_get_pipe_timings(crtc, pipe_config);
  6198. ironlake_get_pfit_config(crtc, pipe_config);
  6199. return true;
  6200. }
  6201. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6202. {
  6203. struct drm_device *dev = dev_priv->dev;
  6204. struct intel_crtc *crtc;
  6205. for_each_intel_crtc(dev, crtc)
  6206. WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6207. pipe_name(crtc->pipe));
  6208. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6209. WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6210. WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6211. WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6212. WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6213. WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6214. "CPU PWM1 enabled\n");
  6215. if (IS_HASWELL(dev))
  6216. WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6217. "CPU PWM2 enabled\n");
  6218. WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6219. "PCH PWM1 enabled\n");
  6220. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6221. "Utility pin enabled\n");
  6222. WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6223. /*
  6224. * In theory we can still leave IRQs enabled, as long as only the HPD
  6225. * interrupts remain enabled. We used to check for that, but since it's
  6226. * gen-specific and since we only disable LCPLL after we fully disable
  6227. * the interrupts, the check below should be enough.
  6228. */
  6229. WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6230. }
  6231. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6232. {
  6233. struct drm_device *dev = dev_priv->dev;
  6234. if (IS_HASWELL(dev))
  6235. return I915_READ(D_COMP_HSW);
  6236. else
  6237. return I915_READ(D_COMP_BDW);
  6238. }
  6239. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6240. {
  6241. struct drm_device *dev = dev_priv->dev;
  6242. if (IS_HASWELL(dev)) {
  6243. mutex_lock(&dev_priv->rps.hw_lock);
  6244. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6245. val))
  6246. DRM_ERROR("Failed to write to D_COMP\n");
  6247. mutex_unlock(&dev_priv->rps.hw_lock);
  6248. } else {
  6249. I915_WRITE(D_COMP_BDW, val);
  6250. POSTING_READ(D_COMP_BDW);
  6251. }
  6252. }
  6253. /*
  6254. * This function implements pieces of two sequences from BSpec:
  6255. * - Sequence for display software to disable LCPLL
  6256. * - Sequence for display software to allow package C8+
  6257. * The steps implemented here are just the steps that actually touch the LCPLL
  6258. * register. Callers should take care of disabling all the display engine
  6259. * functions, doing the mode unset, fixing interrupts, etc.
  6260. */
  6261. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6262. bool switch_to_fclk, bool allow_power_down)
  6263. {
  6264. uint32_t val;
  6265. assert_can_disable_lcpll(dev_priv);
  6266. val = I915_READ(LCPLL_CTL);
  6267. if (switch_to_fclk) {
  6268. val |= LCPLL_CD_SOURCE_FCLK;
  6269. I915_WRITE(LCPLL_CTL, val);
  6270. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6271. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6272. DRM_ERROR("Switching to FCLK failed\n");
  6273. val = I915_READ(LCPLL_CTL);
  6274. }
  6275. val |= LCPLL_PLL_DISABLE;
  6276. I915_WRITE(LCPLL_CTL, val);
  6277. POSTING_READ(LCPLL_CTL);
  6278. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6279. DRM_ERROR("LCPLL still locked\n");
  6280. val = hsw_read_dcomp(dev_priv);
  6281. val |= D_COMP_COMP_DISABLE;
  6282. hsw_write_dcomp(dev_priv, val);
  6283. ndelay(100);
  6284. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6285. 1))
  6286. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6287. if (allow_power_down) {
  6288. val = I915_READ(LCPLL_CTL);
  6289. val |= LCPLL_POWER_DOWN_ALLOW;
  6290. I915_WRITE(LCPLL_CTL, val);
  6291. POSTING_READ(LCPLL_CTL);
  6292. }
  6293. }
  6294. /*
  6295. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6296. * source.
  6297. */
  6298. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6299. {
  6300. uint32_t val;
  6301. unsigned long irqflags;
  6302. val = I915_READ(LCPLL_CTL);
  6303. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6304. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6305. return;
  6306. /*
  6307. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6308. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6309. *
  6310. * The other problem is that hsw_restore_lcpll() is called as part of
  6311. * the runtime PM resume sequence, so we can't just call
  6312. * gen6_gt_force_wake_get() because that function calls
  6313. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6314. * while we are on the resume sequence. So to solve this problem we have
  6315. * to call special forcewake code that doesn't touch runtime PM and
  6316. * doesn't enable the forcewake delayed work.
  6317. */
  6318. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6319. if (dev_priv->uncore.forcewake_count++ == 0)
  6320. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6321. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6322. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6323. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6324. I915_WRITE(LCPLL_CTL, val);
  6325. POSTING_READ(LCPLL_CTL);
  6326. }
  6327. val = hsw_read_dcomp(dev_priv);
  6328. val |= D_COMP_COMP_FORCE;
  6329. val &= ~D_COMP_COMP_DISABLE;
  6330. hsw_write_dcomp(dev_priv, val);
  6331. val = I915_READ(LCPLL_CTL);
  6332. val &= ~LCPLL_PLL_DISABLE;
  6333. I915_WRITE(LCPLL_CTL, val);
  6334. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6335. DRM_ERROR("LCPLL not locked yet\n");
  6336. if (val & LCPLL_CD_SOURCE_FCLK) {
  6337. val = I915_READ(LCPLL_CTL);
  6338. val &= ~LCPLL_CD_SOURCE_FCLK;
  6339. I915_WRITE(LCPLL_CTL, val);
  6340. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6341. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6342. DRM_ERROR("Switching back to LCPLL failed\n");
  6343. }
  6344. /* See the big comment above. */
  6345. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6346. if (--dev_priv->uncore.forcewake_count == 0)
  6347. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6348. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6349. }
  6350. /*
  6351. * Package states C8 and deeper are really deep PC states that can only be
  6352. * reached when all the devices on the system allow it, so even if the graphics
  6353. * device allows PC8+, it doesn't mean the system will actually get to these
  6354. * states. Our driver only allows PC8+ when going into runtime PM.
  6355. *
  6356. * The requirements for PC8+ are that all the outputs are disabled, the power
  6357. * well is disabled and most interrupts are disabled, and these are also
  6358. * requirements for runtime PM. When these conditions are met, we manually do
  6359. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6360. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6361. * hang the machine.
  6362. *
  6363. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6364. * the state of some registers, so when we come back from PC8+ we need to
  6365. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6366. * need to take care of the registers kept by RC6. Notice that this happens even
  6367. * if we don't put the device in PCI D3 state (which is what currently happens
  6368. * because of the runtime PM support).
  6369. *
  6370. * For more, read "Display Sequences for Package C8" on the hardware
  6371. * documentation.
  6372. */
  6373. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6374. {
  6375. struct drm_device *dev = dev_priv->dev;
  6376. uint32_t val;
  6377. DRM_DEBUG_KMS("Enabling package C8+\n");
  6378. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6379. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6380. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6381. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6382. }
  6383. lpt_disable_clkout_dp(dev);
  6384. hsw_disable_lcpll(dev_priv, true, true);
  6385. }
  6386. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6387. {
  6388. struct drm_device *dev = dev_priv->dev;
  6389. uint32_t val;
  6390. DRM_DEBUG_KMS("Disabling package C8+\n");
  6391. hsw_restore_lcpll(dev_priv);
  6392. lpt_init_pch_refclk(dev);
  6393. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6394. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6395. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6396. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6397. }
  6398. intel_prepare_ddi(dev);
  6399. }
  6400. static void snb_modeset_global_resources(struct drm_device *dev)
  6401. {
  6402. modeset_update_crtc_power_domains(dev);
  6403. }
  6404. static void haswell_modeset_global_resources(struct drm_device *dev)
  6405. {
  6406. modeset_update_crtc_power_domains(dev);
  6407. }
  6408. static int haswell_crtc_mode_set(struct drm_crtc *crtc,
  6409. int x, int y,
  6410. struct drm_framebuffer *fb)
  6411. {
  6412. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6413. if (!intel_ddi_pll_select(intel_crtc))
  6414. return -EINVAL;
  6415. intel_crtc->lowfreq_avail = false;
  6416. return 0;
  6417. }
  6418. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  6419. enum port port,
  6420. struct intel_crtc_config *pipe_config)
  6421. {
  6422. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6423. switch (pipe_config->ddi_pll_sel) {
  6424. case PORT_CLK_SEL_WRPLL1:
  6425. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6426. break;
  6427. case PORT_CLK_SEL_WRPLL2:
  6428. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6429. break;
  6430. }
  6431. }
  6432. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6433. struct intel_crtc_config *pipe_config)
  6434. {
  6435. struct drm_device *dev = crtc->base.dev;
  6436. struct drm_i915_private *dev_priv = dev->dev_private;
  6437. struct intel_shared_dpll *pll;
  6438. enum port port;
  6439. uint32_t tmp;
  6440. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6441. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  6442. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  6443. if (pipe_config->shared_dpll >= 0) {
  6444. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6445. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6446. &pipe_config->dpll_hw_state));
  6447. }
  6448. /*
  6449. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6450. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6451. * the PCH transcoder is on.
  6452. */
  6453. if ((port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6454. pipe_config->has_pch_encoder = true;
  6455. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6456. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6457. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6458. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6459. }
  6460. }
  6461. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6462. struct intel_crtc_config *pipe_config)
  6463. {
  6464. struct drm_device *dev = crtc->base.dev;
  6465. struct drm_i915_private *dev_priv = dev->dev_private;
  6466. enum intel_display_power_domain pfit_domain;
  6467. uint32_t tmp;
  6468. if (!intel_display_power_enabled(dev_priv,
  6469. POWER_DOMAIN_PIPE(crtc->pipe)))
  6470. return false;
  6471. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6472. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6473. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6474. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6475. enum pipe trans_edp_pipe;
  6476. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6477. default:
  6478. WARN(1, "unknown pipe linked to edp transcoder\n");
  6479. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6480. case TRANS_DDI_EDP_INPUT_A_ON:
  6481. trans_edp_pipe = PIPE_A;
  6482. break;
  6483. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6484. trans_edp_pipe = PIPE_B;
  6485. break;
  6486. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6487. trans_edp_pipe = PIPE_C;
  6488. break;
  6489. }
  6490. if (trans_edp_pipe == crtc->pipe)
  6491. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6492. }
  6493. if (!intel_display_power_enabled(dev_priv,
  6494. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6495. return false;
  6496. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6497. if (!(tmp & PIPECONF_ENABLE))
  6498. return false;
  6499. haswell_get_ddi_port_state(crtc, pipe_config);
  6500. intel_get_pipe_timings(crtc, pipe_config);
  6501. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6502. if (intel_display_power_enabled(dev_priv, pfit_domain))
  6503. ironlake_get_pfit_config(crtc, pipe_config);
  6504. if (IS_HASWELL(dev))
  6505. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6506. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6507. pipe_config->pixel_multiplier = 1;
  6508. return true;
  6509. }
  6510. static struct {
  6511. int clock;
  6512. u32 config;
  6513. } hdmi_audio_clock[] = {
  6514. { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  6515. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  6516. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  6517. { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  6518. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  6519. { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  6520. { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  6521. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  6522. { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  6523. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  6524. };
  6525. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  6526. static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
  6527. {
  6528. int i;
  6529. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  6530. if (mode->clock == hdmi_audio_clock[i].clock)
  6531. break;
  6532. }
  6533. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  6534. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
  6535. i = 1;
  6536. }
  6537. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  6538. hdmi_audio_clock[i].clock,
  6539. hdmi_audio_clock[i].config);
  6540. return hdmi_audio_clock[i].config;
  6541. }
  6542. static bool intel_eld_uptodate(struct drm_connector *connector,
  6543. int reg_eldv, uint32_t bits_eldv,
  6544. int reg_elda, uint32_t bits_elda,
  6545. int reg_edid)
  6546. {
  6547. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6548. uint8_t *eld = connector->eld;
  6549. uint32_t i;
  6550. i = I915_READ(reg_eldv);
  6551. i &= bits_eldv;
  6552. if (!eld[0])
  6553. return !i;
  6554. if (!i)
  6555. return false;
  6556. i = I915_READ(reg_elda);
  6557. i &= ~bits_elda;
  6558. I915_WRITE(reg_elda, i);
  6559. for (i = 0; i < eld[2]; i++)
  6560. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  6561. return false;
  6562. return true;
  6563. }
  6564. static void g4x_write_eld(struct drm_connector *connector,
  6565. struct drm_crtc *crtc,
  6566. struct drm_display_mode *mode)
  6567. {
  6568. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6569. uint8_t *eld = connector->eld;
  6570. uint32_t eldv;
  6571. uint32_t len;
  6572. uint32_t i;
  6573. i = I915_READ(G4X_AUD_VID_DID);
  6574. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  6575. eldv = G4X_ELDV_DEVCL_DEVBLC;
  6576. else
  6577. eldv = G4X_ELDV_DEVCTG;
  6578. if (intel_eld_uptodate(connector,
  6579. G4X_AUD_CNTL_ST, eldv,
  6580. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  6581. G4X_HDMIW_HDMIEDID))
  6582. return;
  6583. i = I915_READ(G4X_AUD_CNTL_ST);
  6584. i &= ~(eldv | G4X_ELD_ADDR);
  6585. len = (i >> 9) & 0x1f; /* ELD buffer size */
  6586. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6587. if (!eld[0])
  6588. return;
  6589. len = min_t(uint8_t, eld[2], len);
  6590. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6591. for (i = 0; i < len; i++)
  6592. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  6593. i = I915_READ(G4X_AUD_CNTL_ST);
  6594. i |= eldv;
  6595. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6596. }
  6597. static void haswell_write_eld(struct drm_connector *connector,
  6598. struct drm_crtc *crtc,
  6599. struct drm_display_mode *mode)
  6600. {
  6601. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6602. uint8_t *eld = connector->eld;
  6603. uint32_t eldv;
  6604. uint32_t i;
  6605. int len;
  6606. int pipe = to_intel_crtc(crtc)->pipe;
  6607. int tmp;
  6608. int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
  6609. int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
  6610. int aud_config = HSW_AUD_CFG(pipe);
  6611. int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
  6612. /* Audio output enable */
  6613. DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
  6614. tmp = I915_READ(aud_cntrl_st2);
  6615. tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
  6616. I915_WRITE(aud_cntrl_st2, tmp);
  6617. POSTING_READ(aud_cntrl_st2);
  6618. assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  6619. /* Set ELD valid state */
  6620. tmp = I915_READ(aud_cntrl_st2);
  6621. DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
  6622. tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
  6623. I915_WRITE(aud_cntrl_st2, tmp);
  6624. tmp = I915_READ(aud_cntrl_st2);
  6625. DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
  6626. /* Enable HDMI mode */
  6627. tmp = I915_READ(aud_config);
  6628. DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
  6629. /* clear N_programing_enable and N_value_index */
  6630. tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
  6631. I915_WRITE(aud_config, tmp);
  6632. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6633. eldv = AUDIO_ELD_VALID_A << (pipe * 4);
  6634. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6635. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6636. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6637. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6638. } else {
  6639. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6640. }
  6641. if (intel_eld_uptodate(connector,
  6642. aud_cntrl_st2, eldv,
  6643. aud_cntl_st, IBX_ELD_ADDRESS,
  6644. hdmiw_hdmiedid))
  6645. return;
  6646. i = I915_READ(aud_cntrl_st2);
  6647. i &= ~eldv;
  6648. I915_WRITE(aud_cntrl_st2, i);
  6649. if (!eld[0])
  6650. return;
  6651. i = I915_READ(aud_cntl_st);
  6652. i &= ~IBX_ELD_ADDRESS;
  6653. I915_WRITE(aud_cntl_st, i);
  6654. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  6655. DRM_DEBUG_DRIVER("port num:%d\n", i);
  6656. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6657. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6658. for (i = 0; i < len; i++)
  6659. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6660. i = I915_READ(aud_cntrl_st2);
  6661. i |= eldv;
  6662. I915_WRITE(aud_cntrl_st2, i);
  6663. }
  6664. static void ironlake_write_eld(struct drm_connector *connector,
  6665. struct drm_crtc *crtc,
  6666. struct drm_display_mode *mode)
  6667. {
  6668. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6669. uint8_t *eld = connector->eld;
  6670. uint32_t eldv;
  6671. uint32_t i;
  6672. int len;
  6673. int hdmiw_hdmiedid;
  6674. int aud_config;
  6675. int aud_cntl_st;
  6676. int aud_cntrl_st2;
  6677. int pipe = to_intel_crtc(crtc)->pipe;
  6678. if (HAS_PCH_IBX(connector->dev)) {
  6679. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  6680. aud_config = IBX_AUD_CFG(pipe);
  6681. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  6682. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  6683. } else if (IS_VALLEYVIEW(connector->dev)) {
  6684. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  6685. aud_config = VLV_AUD_CFG(pipe);
  6686. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  6687. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  6688. } else {
  6689. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  6690. aud_config = CPT_AUD_CFG(pipe);
  6691. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  6692. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  6693. }
  6694. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6695. if (IS_VALLEYVIEW(connector->dev)) {
  6696. struct intel_encoder *intel_encoder;
  6697. struct intel_digital_port *intel_dig_port;
  6698. intel_encoder = intel_attached_encoder(connector);
  6699. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  6700. i = intel_dig_port->port;
  6701. } else {
  6702. i = I915_READ(aud_cntl_st);
  6703. i = (i >> 29) & DIP_PORT_SEL_MASK;
  6704. /* DIP_Port_Select, 0x1 = PortB */
  6705. }
  6706. if (!i) {
  6707. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  6708. /* operate blindly on all ports */
  6709. eldv = IBX_ELD_VALIDB;
  6710. eldv |= IBX_ELD_VALIDB << 4;
  6711. eldv |= IBX_ELD_VALIDB << 8;
  6712. } else {
  6713. DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
  6714. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  6715. }
  6716. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6717. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6718. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6719. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6720. } else {
  6721. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6722. }
  6723. if (intel_eld_uptodate(connector,
  6724. aud_cntrl_st2, eldv,
  6725. aud_cntl_st, IBX_ELD_ADDRESS,
  6726. hdmiw_hdmiedid))
  6727. return;
  6728. i = I915_READ(aud_cntrl_st2);
  6729. i &= ~eldv;
  6730. I915_WRITE(aud_cntrl_st2, i);
  6731. if (!eld[0])
  6732. return;
  6733. i = I915_READ(aud_cntl_st);
  6734. i &= ~IBX_ELD_ADDRESS;
  6735. I915_WRITE(aud_cntl_st, i);
  6736. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6737. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6738. for (i = 0; i < len; i++)
  6739. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6740. i = I915_READ(aud_cntrl_st2);
  6741. i |= eldv;
  6742. I915_WRITE(aud_cntrl_st2, i);
  6743. }
  6744. void intel_write_eld(struct drm_encoder *encoder,
  6745. struct drm_display_mode *mode)
  6746. {
  6747. struct drm_crtc *crtc = encoder->crtc;
  6748. struct drm_connector *connector;
  6749. struct drm_device *dev = encoder->dev;
  6750. struct drm_i915_private *dev_priv = dev->dev_private;
  6751. connector = drm_select_eld(encoder, mode);
  6752. if (!connector)
  6753. return;
  6754. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6755. connector->base.id,
  6756. connector->name,
  6757. connector->encoder->base.id,
  6758. connector->encoder->name);
  6759. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  6760. if (dev_priv->display.write_eld)
  6761. dev_priv->display.write_eld(connector, crtc, mode);
  6762. }
  6763. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6764. {
  6765. struct drm_device *dev = crtc->dev;
  6766. struct drm_i915_private *dev_priv = dev->dev_private;
  6767. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6768. uint32_t cntl = 0, size = 0;
  6769. if (base) {
  6770. unsigned int width = intel_crtc->cursor_width;
  6771. unsigned int height = intel_crtc->cursor_height;
  6772. unsigned int stride = roundup_pow_of_two(width) * 4;
  6773. switch (stride) {
  6774. default:
  6775. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  6776. width, stride);
  6777. stride = 256;
  6778. /* fallthrough */
  6779. case 256:
  6780. case 512:
  6781. case 1024:
  6782. case 2048:
  6783. break;
  6784. }
  6785. cntl |= CURSOR_ENABLE |
  6786. CURSOR_GAMMA_ENABLE |
  6787. CURSOR_FORMAT_ARGB |
  6788. CURSOR_STRIDE(stride);
  6789. size = (height << 12) | width;
  6790. }
  6791. if (intel_crtc->cursor_cntl != 0 &&
  6792. (intel_crtc->cursor_base != base ||
  6793. intel_crtc->cursor_size != size ||
  6794. intel_crtc->cursor_cntl != cntl)) {
  6795. /* On these chipsets we can only modify the base/size/stride
  6796. * whilst the cursor is disabled.
  6797. */
  6798. I915_WRITE(_CURACNTR, 0);
  6799. POSTING_READ(_CURACNTR);
  6800. intel_crtc->cursor_cntl = 0;
  6801. }
  6802. if (intel_crtc->cursor_base != base)
  6803. I915_WRITE(_CURABASE, base);
  6804. if (intel_crtc->cursor_size != size) {
  6805. I915_WRITE(CURSIZE, size);
  6806. intel_crtc->cursor_size = size;
  6807. }
  6808. if (intel_crtc->cursor_cntl != cntl) {
  6809. I915_WRITE(_CURACNTR, cntl);
  6810. POSTING_READ(_CURACNTR);
  6811. intel_crtc->cursor_cntl = cntl;
  6812. }
  6813. }
  6814. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6815. {
  6816. struct drm_device *dev = crtc->dev;
  6817. struct drm_i915_private *dev_priv = dev->dev_private;
  6818. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6819. int pipe = intel_crtc->pipe;
  6820. uint32_t cntl;
  6821. cntl = 0;
  6822. if (base) {
  6823. cntl = MCURSOR_GAMMA_ENABLE;
  6824. switch (intel_crtc->cursor_width) {
  6825. case 64:
  6826. cntl |= CURSOR_MODE_64_ARGB_AX;
  6827. break;
  6828. case 128:
  6829. cntl |= CURSOR_MODE_128_ARGB_AX;
  6830. break;
  6831. case 256:
  6832. cntl |= CURSOR_MODE_256_ARGB_AX;
  6833. break;
  6834. default:
  6835. WARN_ON(1);
  6836. return;
  6837. }
  6838. cntl |= pipe << 28; /* Connect to correct pipe */
  6839. }
  6840. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6841. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6842. if (intel_crtc->cursor_cntl != cntl) {
  6843. I915_WRITE(CURCNTR(pipe), cntl);
  6844. POSTING_READ(CURCNTR(pipe));
  6845. intel_crtc->cursor_cntl = cntl;
  6846. }
  6847. /* and commit changes on next vblank */
  6848. I915_WRITE(CURBASE(pipe), base);
  6849. POSTING_READ(CURBASE(pipe));
  6850. }
  6851. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6852. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6853. bool on)
  6854. {
  6855. struct drm_device *dev = crtc->dev;
  6856. struct drm_i915_private *dev_priv = dev->dev_private;
  6857. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6858. int pipe = intel_crtc->pipe;
  6859. int x = crtc->cursor_x;
  6860. int y = crtc->cursor_y;
  6861. u32 base = 0, pos = 0;
  6862. if (on)
  6863. base = intel_crtc->cursor_addr;
  6864. if (x >= intel_crtc->config.pipe_src_w)
  6865. base = 0;
  6866. if (y >= intel_crtc->config.pipe_src_h)
  6867. base = 0;
  6868. if (x < 0) {
  6869. if (x + intel_crtc->cursor_width <= 0)
  6870. base = 0;
  6871. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  6872. x = -x;
  6873. }
  6874. pos |= x << CURSOR_X_SHIFT;
  6875. if (y < 0) {
  6876. if (y + intel_crtc->cursor_height <= 0)
  6877. base = 0;
  6878. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  6879. y = -y;
  6880. }
  6881. pos |= y << CURSOR_Y_SHIFT;
  6882. if (base == 0 && intel_crtc->cursor_base == 0)
  6883. return;
  6884. I915_WRITE(CURPOS(pipe), pos);
  6885. if (IS_845G(dev) || IS_I865G(dev))
  6886. i845_update_cursor(crtc, base);
  6887. else
  6888. i9xx_update_cursor(crtc, base);
  6889. intel_crtc->cursor_base = base;
  6890. }
  6891. static bool cursor_size_ok(struct drm_device *dev,
  6892. uint32_t width, uint32_t height)
  6893. {
  6894. if (width == 0 || height == 0)
  6895. return false;
  6896. /*
  6897. * 845g/865g are special in that they are only limited by
  6898. * the width of their cursors, the height is arbitrary up to
  6899. * the precision of the register. Everything else requires
  6900. * square cursors, limited to a few power-of-two sizes.
  6901. */
  6902. if (IS_845G(dev) || IS_I865G(dev)) {
  6903. if ((width & 63) != 0)
  6904. return false;
  6905. if (width > (IS_845G(dev) ? 64 : 512))
  6906. return false;
  6907. if (height > 1023)
  6908. return false;
  6909. } else {
  6910. switch (width | height) {
  6911. case 256:
  6912. case 128:
  6913. if (IS_GEN2(dev))
  6914. return false;
  6915. case 64:
  6916. break;
  6917. default:
  6918. return false;
  6919. }
  6920. }
  6921. return true;
  6922. }
  6923. /*
  6924. * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
  6925. *
  6926. * Note that the object's reference will be consumed if the update fails. If
  6927. * the update succeeds, the reference of the old object (if any) will be
  6928. * consumed.
  6929. */
  6930. static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
  6931. struct drm_i915_gem_object *obj,
  6932. uint32_t width, uint32_t height)
  6933. {
  6934. struct drm_device *dev = crtc->dev;
  6935. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6936. enum pipe pipe = intel_crtc->pipe;
  6937. unsigned old_width, stride;
  6938. uint32_t addr;
  6939. int ret;
  6940. /* if we want to turn off the cursor ignore width and height */
  6941. if (!obj) {
  6942. DRM_DEBUG_KMS("cursor off\n");
  6943. addr = 0;
  6944. obj = NULL;
  6945. mutex_lock(&dev->struct_mutex);
  6946. goto finish;
  6947. }
  6948. /* Check for which cursor types we support */
  6949. if (!cursor_size_ok(dev, width, height)) {
  6950. DRM_DEBUG("Cursor dimension not supported\n");
  6951. return -EINVAL;
  6952. }
  6953. stride = roundup_pow_of_two(width) * 4;
  6954. if (obj->base.size < stride * height) {
  6955. DRM_DEBUG_KMS("buffer is too small\n");
  6956. ret = -ENOMEM;
  6957. goto fail;
  6958. }
  6959. /* we only need to pin inside GTT if cursor is non-phy */
  6960. mutex_lock(&dev->struct_mutex);
  6961. if (!INTEL_INFO(dev)->cursor_needs_physical) {
  6962. unsigned alignment;
  6963. if (obj->tiling_mode) {
  6964. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  6965. ret = -EINVAL;
  6966. goto fail_locked;
  6967. }
  6968. /* Note that the w/a also requires 2 PTE of padding following
  6969. * the bo. We currently fill all unused PTE with the shadow
  6970. * page and so we should always have valid PTE following the
  6971. * cursor preventing the VT-d warning.
  6972. */
  6973. alignment = 0;
  6974. if (need_vtd_wa(dev))
  6975. alignment = 64*1024;
  6976. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  6977. if (ret) {
  6978. DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
  6979. goto fail_locked;
  6980. }
  6981. ret = i915_gem_object_put_fence(obj);
  6982. if (ret) {
  6983. DRM_DEBUG_KMS("failed to release fence for cursor");
  6984. goto fail_unpin;
  6985. }
  6986. addr = i915_gem_obj_ggtt_offset(obj);
  6987. } else {
  6988. int align = IS_I830(dev) ? 16 * 1024 : 256;
  6989. ret = i915_gem_object_attach_phys(obj, align);
  6990. if (ret) {
  6991. DRM_DEBUG_KMS("failed to attach phys object\n");
  6992. goto fail_locked;
  6993. }
  6994. addr = obj->phys_handle->busaddr;
  6995. }
  6996. finish:
  6997. if (intel_crtc->cursor_bo) {
  6998. if (!INTEL_INFO(dev)->cursor_needs_physical)
  6999. i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
  7000. }
  7001. i915_gem_track_fb(intel_crtc->cursor_bo, obj,
  7002. INTEL_FRONTBUFFER_CURSOR(pipe));
  7003. mutex_unlock(&dev->struct_mutex);
  7004. old_width = intel_crtc->cursor_width;
  7005. intel_crtc->cursor_addr = addr;
  7006. intel_crtc->cursor_bo = obj;
  7007. intel_crtc->cursor_width = width;
  7008. intel_crtc->cursor_height = height;
  7009. if (intel_crtc->active) {
  7010. if (old_width != width)
  7011. intel_update_watermarks(crtc);
  7012. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  7013. }
  7014. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
  7015. return 0;
  7016. fail_unpin:
  7017. i915_gem_object_unpin_from_display_plane(obj);
  7018. fail_locked:
  7019. mutex_unlock(&dev->struct_mutex);
  7020. fail:
  7021. drm_gem_object_unreference_unlocked(&obj->base);
  7022. return ret;
  7023. }
  7024. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  7025. u16 *blue, uint32_t start, uint32_t size)
  7026. {
  7027. int end = (start + size > 256) ? 256 : start + size, i;
  7028. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7029. for (i = start; i < end; i++) {
  7030. intel_crtc->lut_r[i] = red[i] >> 8;
  7031. intel_crtc->lut_g[i] = green[i] >> 8;
  7032. intel_crtc->lut_b[i] = blue[i] >> 8;
  7033. }
  7034. intel_crtc_load_lut(crtc);
  7035. }
  7036. /* VESA 640x480x72Hz mode to set on the pipe */
  7037. static struct drm_display_mode load_detect_mode = {
  7038. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7039. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7040. };
  7041. struct drm_framebuffer *
  7042. __intel_framebuffer_create(struct drm_device *dev,
  7043. struct drm_mode_fb_cmd2 *mode_cmd,
  7044. struct drm_i915_gem_object *obj)
  7045. {
  7046. struct intel_framebuffer *intel_fb;
  7047. int ret;
  7048. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7049. if (!intel_fb) {
  7050. drm_gem_object_unreference_unlocked(&obj->base);
  7051. return ERR_PTR(-ENOMEM);
  7052. }
  7053. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7054. if (ret)
  7055. goto err;
  7056. return &intel_fb->base;
  7057. err:
  7058. drm_gem_object_unreference_unlocked(&obj->base);
  7059. kfree(intel_fb);
  7060. return ERR_PTR(ret);
  7061. }
  7062. static struct drm_framebuffer *
  7063. intel_framebuffer_create(struct drm_device *dev,
  7064. struct drm_mode_fb_cmd2 *mode_cmd,
  7065. struct drm_i915_gem_object *obj)
  7066. {
  7067. struct drm_framebuffer *fb;
  7068. int ret;
  7069. ret = i915_mutex_lock_interruptible(dev);
  7070. if (ret)
  7071. return ERR_PTR(ret);
  7072. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7073. mutex_unlock(&dev->struct_mutex);
  7074. return fb;
  7075. }
  7076. static u32
  7077. intel_framebuffer_pitch_for_width(int width, int bpp)
  7078. {
  7079. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7080. return ALIGN(pitch, 64);
  7081. }
  7082. static u32
  7083. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7084. {
  7085. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7086. return PAGE_ALIGN(pitch * mode->vdisplay);
  7087. }
  7088. static struct drm_framebuffer *
  7089. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7090. struct drm_display_mode *mode,
  7091. int depth, int bpp)
  7092. {
  7093. struct drm_i915_gem_object *obj;
  7094. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7095. obj = i915_gem_alloc_object(dev,
  7096. intel_framebuffer_size_for_mode(mode, bpp));
  7097. if (obj == NULL)
  7098. return ERR_PTR(-ENOMEM);
  7099. mode_cmd.width = mode->hdisplay;
  7100. mode_cmd.height = mode->vdisplay;
  7101. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7102. bpp);
  7103. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7104. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7105. }
  7106. static struct drm_framebuffer *
  7107. mode_fits_in_fbdev(struct drm_device *dev,
  7108. struct drm_display_mode *mode)
  7109. {
  7110. #ifdef CONFIG_DRM_I915_FBDEV
  7111. struct drm_i915_private *dev_priv = dev->dev_private;
  7112. struct drm_i915_gem_object *obj;
  7113. struct drm_framebuffer *fb;
  7114. if (!dev_priv->fbdev)
  7115. return NULL;
  7116. if (!dev_priv->fbdev->fb)
  7117. return NULL;
  7118. obj = dev_priv->fbdev->fb->obj;
  7119. BUG_ON(!obj);
  7120. fb = &dev_priv->fbdev->fb->base;
  7121. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7122. fb->bits_per_pixel))
  7123. return NULL;
  7124. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7125. return NULL;
  7126. return fb;
  7127. #else
  7128. return NULL;
  7129. #endif
  7130. }
  7131. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7132. struct drm_display_mode *mode,
  7133. struct intel_load_detect_pipe *old,
  7134. struct drm_modeset_acquire_ctx *ctx)
  7135. {
  7136. struct intel_crtc *intel_crtc;
  7137. struct intel_encoder *intel_encoder =
  7138. intel_attached_encoder(connector);
  7139. struct drm_crtc *possible_crtc;
  7140. struct drm_encoder *encoder = &intel_encoder->base;
  7141. struct drm_crtc *crtc = NULL;
  7142. struct drm_device *dev = encoder->dev;
  7143. struct drm_framebuffer *fb;
  7144. struct drm_mode_config *config = &dev->mode_config;
  7145. int ret, i = -1;
  7146. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7147. connector->base.id, connector->name,
  7148. encoder->base.id, encoder->name);
  7149. retry:
  7150. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7151. if (ret)
  7152. goto fail_unlock;
  7153. /*
  7154. * Algorithm gets a little messy:
  7155. *
  7156. * - if the connector already has an assigned crtc, use it (but make
  7157. * sure it's on first)
  7158. *
  7159. * - try to find the first unused crtc that can drive this connector,
  7160. * and use that if we find one
  7161. */
  7162. /* See if we already have a CRTC for this connector */
  7163. if (encoder->crtc) {
  7164. crtc = encoder->crtc;
  7165. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7166. if (ret)
  7167. goto fail_unlock;
  7168. old->dpms_mode = connector->dpms;
  7169. old->load_detect_temp = false;
  7170. /* Make sure the crtc and connector are running */
  7171. if (connector->dpms != DRM_MODE_DPMS_ON)
  7172. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7173. return true;
  7174. }
  7175. /* Find an unused one (if possible) */
  7176. for_each_crtc(dev, possible_crtc) {
  7177. i++;
  7178. if (!(encoder->possible_crtcs & (1 << i)))
  7179. continue;
  7180. if (possible_crtc->enabled)
  7181. continue;
  7182. /* This can occur when applying the pipe A quirk on resume. */
  7183. if (to_intel_crtc(possible_crtc)->new_enabled)
  7184. continue;
  7185. crtc = possible_crtc;
  7186. break;
  7187. }
  7188. /*
  7189. * If we didn't find an unused CRTC, don't use any.
  7190. */
  7191. if (!crtc) {
  7192. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7193. goto fail_unlock;
  7194. }
  7195. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7196. if (ret)
  7197. goto fail_unlock;
  7198. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7199. to_intel_connector(connector)->new_encoder = intel_encoder;
  7200. intel_crtc = to_intel_crtc(crtc);
  7201. intel_crtc->new_enabled = true;
  7202. intel_crtc->new_config = &intel_crtc->config;
  7203. old->dpms_mode = connector->dpms;
  7204. old->load_detect_temp = true;
  7205. old->release_fb = NULL;
  7206. if (!mode)
  7207. mode = &load_detect_mode;
  7208. /* We need a framebuffer large enough to accommodate all accesses
  7209. * that the plane may generate whilst we perform load detection.
  7210. * We can not rely on the fbcon either being present (we get called
  7211. * during its initialisation to detect all boot displays, or it may
  7212. * not even exist) or that it is large enough to satisfy the
  7213. * requested mode.
  7214. */
  7215. fb = mode_fits_in_fbdev(dev, mode);
  7216. if (fb == NULL) {
  7217. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7218. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7219. old->release_fb = fb;
  7220. } else
  7221. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7222. if (IS_ERR(fb)) {
  7223. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7224. goto fail;
  7225. }
  7226. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7227. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7228. if (old->release_fb)
  7229. old->release_fb->funcs->destroy(old->release_fb);
  7230. goto fail;
  7231. }
  7232. /* let the connector get through one full cycle before testing */
  7233. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7234. return true;
  7235. fail:
  7236. intel_crtc->new_enabled = crtc->enabled;
  7237. if (intel_crtc->new_enabled)
  7238. intel_crtc->new_config = &intel_crtc->config;
  7239. else
  7240. intel_crtc->new_config = NULL;
  7241. fail_unlock:
  7242. if (ret == -EDEADLK) {
  7243. drm_modeset_backoff(ctx);
  7244. goto retry;
  7245. }
  7246. return false;
  7247. }
  7248. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7249. struct intel_load_detect_pipe *old)
  7250. {
  7251. struct intel_encoder *intel_encoder =
  7252. intel_attached_encoder(connector);
  7253. struct drm_encoder *encoder = &intel_encoder->base;
  7254. struct drm_crtc *crtc = encoder->crtc;
  7255. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7256. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7257. connector->base.id, connector->name,
  7258. encoder->base.id, encoder->name);
  7259. if (old->load_detect_temp) {
  7260. to_intel_connector(connector)->new_encoder = NULL;
  7261. intel_encoder->new_crtc = NULL;
  7262. intel_crtc->new_enabled = false;
  7263. intel_crtc->new_config = NULL;
  7264. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7265. if (old->release_fb) {
  7266. drm_framebuffer_unregister_private(old->release_fb);
  7267. drm_framebuffer_unreference(old->release_fb);
  7268. }
  7269. return;
  7270. }
  7271. /* Switch crtc and encoder back off if necessary */
  7272. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7273. connector->funcs->dpms(connector, old->dpms_mode);
  7274. }
  7275. static int i9xx_pll_refclk(struct drm_device *dev,
  7276. const struct intel_crtc_config *pipe_config)
  7277. {
  7278. struct drm_i915_private *dev_priv = dev->dev_private;
  7279. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7280. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7281. return dev_priv->vbt.lvds_ssc_freq;
  7282. else if (HAS_PCH_SPLIT(dev))
  7283. return 120000;
  7284. else if (!IS_GEN2(dev))
  7285. return 96000;
  7286. else
  7287. return 48000;
  7288. }
  7289. /* Returns the clock of the currently programmed mode of the given pipe. */
  7290. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7291. struct intel_crtc_config *pipe_config)
  7292. {
  7293. struct drm_device *dev = crtc->base.dev;
  7294. struct drm_i915_private *dev_priv = dev->dev_private;
  7295. int pipe = pipe_config->cpu_transcoder;
  7296. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7297. u32 fp;
  7298. intel_clock_t clock;
  7299. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7300. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7301. fp = pipe_config->dpll_hw_state.fp0;
  7302. else
  7303. fp = pipe_config->dpll_hw_state.fp1;
  7304. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7305. if (IS_PINEVIEW(dev)) {
  7306. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7307. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7308. } else {
  7309. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7310. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7311. }
  7312. if (!IS_GEN2(dev)) {
  7313. if (IS_PINEVIEW(dev))
  7314. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7315. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7316. else
  7317. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7318. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7319. switch (dpll & DPLL_MODE_MASK) {
  7320. case DPLLB_MODE_DAC_SERIAL:
  7321. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7322. 5 : 10;
  7323. break;
  7324. case DPLLB_MODE_LVDS:
  7325. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7326. 7 : 14;
  7327. break;
  7328. default:
  7329. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7330. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7331. return;
  7332. }
  7333. if (IS_PINEVIEW(dev))
  7334. pineview_clock(refclk, &clock);
  7335. else
  7336. i9xx_clock(refclk, &clock);
  7337. } else {
  7338. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7339. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7340. if (is_lvds) {
  7341. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7342. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7343. if (lvds & LVDS_CLKB_POWER_UP)
  7344. clock.p2 = 7;
  7345. else
  7346. clock.p2 = 14;
  7347. } else {
  7348. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7349. clock.p1 = 2;
  7350. else {
  7351. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7352. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7353. }
  7354. if (dpll & PLL_P2_DIVIDE_BY_4)
  7355. clock.p2 = 4;
  7356. else
  7357. clock.p2 = 2;
  7358. }
  7359. i9xx_clock(refclk, &clock);
  7360. }
  7361. /*
  7362. * This value includes pixel_multiplier. We will use
  7363. * port_clock to compute adjusted_mode.crtc_clock in the
  7364. * encoder's get_config() function.
  7365. */
  7366. pipe_config->port_clock = clock.dot;
  7367. }
  7368. int intel_dotclock_calculate(int link_freq,
  7369. const struct intel_link_m_n *m_n)
  7370. {
  7371. /*
  7372. * The calculation for the data clock is:
  7373. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7374. * But we want to avoid losing precison if possible, so:
  7375. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7376. *
  7377. * and the link clock is simpler:
  7378. * link_clock = (m * link_clock) / n
  7379. */
  7380. if (!m_n->link_n)
  7381. return 0;
  7382. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7383. }
  7384. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7385. struct intel_crtc_config *pipe_config)
  7386. {
  7387. struct drm_device *dev = crtc->base.dev;
  7388. /* read out port_clock from the DPLL */
  7389. i9xx_crtc_clock_get(crtc, pipe_config);
  7390. /*
  7391. * This value does not include pixel_multiplier.
  7392. * We will check that port_clock and adjusted_mode.crtc_clock
  7393. * agree once we know their relationship in the encoder's
  7394. * get_config() function.
  7395. */
  7396. pipe_config->adjusted_mode.crtc_clock =
  7397. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7398. &pipe_config->fdi_m_n);
  7399. }
  7400. /** Returns the currently programmed mode of the given pipe. */
  7401. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7402. struct drm_crtc *crtc)
  7403. {
  7404. struct drm_i915_private *dev_priv = dev->dev_private;
  7405. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7406. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7407. struct drm_display_mode *mode;
  7408. struct intel_crtc_config pipe_config;
  7409. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7410. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7411. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7412. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7413. enum pipe pipe = intel_crtc->pipe;
  7414. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7415. if (!mode)
  7416. return NULL;
  7417. /*
  7418. * Construct a pipe_config sufficient for getting the clock info
  7419. * back out of crtc_clock_get.
  7420. *
  7421. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7422. * to use a real value here instead.
  7423. */
  7424. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7425. pipe_config.pixel_multiplier = 1;
  7426. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7427. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7428. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7429. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7430. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7431. mode->hdisplay = (htot & 0xffff) + 1;
  7432. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7433. mode->hsync_start = (hsync & 0xffff) + 1;
  7434. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7435. mode->vdisplay = (vtot & 0xffff) + 1;
  7436. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7437. mode->vsync_start = (vsync & 0xffff) + 1;
  7438. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7439. drm_mode_set_name(mode);
  7440. return mode;
  7441. }
  7442. static void intel_increase_pllclock(struct drm_device *dev,
  7443. enum pipe pipe)
  7444. {
  7445. struct drm_i915_private *dev_priv = dev->dev_private;
  7446. int dpll_reg = DPLL(pipe);
  7447. int dpll;
  7448. if (!HAS_GMCH_DISPLAY(dev))
  7449. return;
  7450. if (!dev_priv->lvds_downclock_avail)
  7451. return;
  7452. dpll = I915_READ(dpll_reg);
  7453. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  7454. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  7455. assert_panel_unlocked(dev_priv, pipe);
  7456. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  7457. I915_WRITE(dpll_reg, dpll);
  7458. intel_wait_for_vblank(dev, pipe);
  7459. dpll = I915_READ(dpll_reg);
  7460. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  7461. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  7462. }
  7463. }
  7464. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7465. {
  7466. struct drm_device *dev = crtc->dev;
  7467. struct drm_i915_private *dev_priv = dev->dev_private;
  7468. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7469. if (!HAS_GMCH_DISPLAY(dev))
  7470. return;
  7471. if (!dev_priv->lvds_downclock_avail)
  7472. return;
  7473. /*
  7474. * Since this is called by a timer, we should never get here in
  7475. * the manual case.
  7476. */
  7477. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7478. int pipe = intel_crtc->pipe;
  7479. int dpll_reg = DPLL(pipe);
  7480. int dpll;
  7481. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7482. assert_panel_unlocked(dev_priv, pipe);
  7483. dpll = I915_READ(dpll_reg);
  7484. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7485. I915_WRITE(dpll_reg, dpll);
  7486. intel_wait_for_vblank(dev, pipe);
  7487. dpll = I915_READ(dpll_reg);
  7488. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7489. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7490. }
  7491. }
  7492. void intel_mark_busy(struct drm_device *dev)
  7493. {
  7494. struct drm_i915_private *dev_priv = dev->dev_private;
  7495. if (dev_priv->mm.busy)
  7496. return;
  7497. intel_runtime_pm_get(dev_priv);
  7498. i915_update_gfx_val(dev_priv);
  7499. dev_priv->mm.busy = true;
  7500. }
  7501. void intel_mark_idle(struct drm_device *dev)
  7502. {
  7503. struct drm_i915_private *dev_priv = dev->dev_private;
  7504. struct drm_crtc *crtc;
  7505. if (!dev_priv->mm.busy)
  7506. return;
  7507. dev_priv->mm.busy = false;
  7508. if (!i915.powersave)
  7509. goto out;
  7510. for_each_crtc(dev, crtc) {
  7511. if (!crtc->primary->fb)
  7512. continue;
  7513. intel_decrease_pllclock(crtc);
  7514. }
  7515. if (INTEL_INFO(dev)->gen >= 6)
  7516. gen6_rps_idle(dev->dev_private);
  7517. out:
  7518. intel_runtime_pm_put(dev_priv);
  7519. }
  7520. /**
  7521. * intel_mark_fb_busy - mark given planes as busy
  7522. * @dev: DRM device
  7523. * @frontbuffer_bits: bits for the affected planes
  7524. * @ring: optional ring for asynchronous commands
  7525. *
  7526. * This function gets called every time the screen contents change. It can be
  7527. * used to keep e.g. the update rate at the nominal refresh rate with DRRS.
  7528. */
  7529. static void intel_mark_fb_busy(struct drm_device *dev,
  7530. unsigned frontbuffer_bits,
  7531. struct intel_engine_cs *ring)
  7532. {
  7533. struct drm_i915_private *dev_priv = dev->dev_private;
  7534. enum pipe pipe;
  7535. if (!i915.powersave)
  7536. return;
  7537. for_each_pipe(dev_priv, pipe) {
  7538. if (!(frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe)))
  7539. continue;
  7540. intel_increase_pllclock(dev, pipe);
  7541. if (ring && intel_fbc_enabled(dev))
  7542. ring->fbc_dirty = true;
  7543. }
  7544. }
  7545. /**
  7546. * intel_fb_obj_invalidate - invalidate frontbuffer object
  7547. * @obj: GEM object to invalidate
  7548. * @ring: set for asynchronous rendering
  7549. *
  7550. * This function gets called every time rendering on the given object starts and
  7551. * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must
  7552. * be invalidated. If @ring is non-NULL any subsequent invalidation will be delayed
  7553. * until the rendering completes or a flip on this frontbuffer plane is
  7554. * scheduled.
  7555. */
  7556. void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj,
  7557. struct intel_engine_cs *ring)
  7558. {
  7559. struct drm_device *dev = obj->base.dev;
  7560. struct drm_i915_private *dev_priv = dev->dev_private;
  7561. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  7562. if (!obj->frontbuffer_bits)
  7563. return;
  7564. if (ring) {
  7565. mutex_lock(&dev_priv->fb_tracking.lock);
  7566. dev_priv->fb_tracking.busy_bits
  7567. |= obj->frontbuffer_bits;
  7568. dev_priv->fb_tracking.flip_bits
  7569. &= ~obj->frontbuffer_bits;
  7570. mutex_unlock(&dev_priv->fb_tracking.lock);
  7571. }
  7572. intel_mark_fb_busy(dev, obj->frontbuffer_bits, ring);
  7573. intel_edp_psr_invalidate(dev, obj->frontbuffer_bits);
  7574. }
  7575. /**
  7576. * intel_frontbuffer_flush - flush frontbuffer
  7577. * @dev: DRM device
  7578. * @frontbuffer_bits: frontbuffer plane tracking bits
  7579. *
  7580. * This function gets called every time rendering on the given planes has
  7581. * completed and frontbuffer caching can be started again. Flushes will get
  7582. * delayed if they're blocked by some oustanding asynchronous rendering.
  7583. *
  7584. * Can be called without any locks held.
  7585. */
  7586. void intel_frontbuffer_flush(struct drm_device *dev,
  7587. unsigned frontbuffer_bits)
  7588. {
  7589. struct drm_i915_private *dev_priv = dev->dev_private;
  7590. /* Delay flushing when rings are still busy.*/
  7591. mutex_lock(&dev_priv->fb_tracking.lock);
  7592. frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits;
  7593. mutex_unlock(&dev_priv->fb_tracking.lock);
  7594. intel_mark_fb_busy(dev, frontbuffer_bits, NULL);
  7595. intel_edp_psr_flush(dev, frontbuffer_bits);
  7596. if (IS_GEN8(dev))
  7597. gen8_fbc_sw_flush(dev, FBC_REND_CACHE_CLEAN);
  7598. }
  7599. /**
  7600. * intel_fb_obj_flush - flush frontbuffer object
  7601. * @obj: GEM object to flush
  7602. * @retire: set when retiring asynchronous rendering
  7603. *
  7604. * This function gets called every time rendering on the given object has
  7605. * completed and frontbuffer caching can be started again. If @retire is true
  7606. * then any delayed flushes will be unblocked.
  7607. */
  7608. void intel_fb_obj_flush(struct drm_i915_gem_object *obj,
  7609. bool retire)
  7610. {
  7611. struct drm_device *dev = obj->base.dev;
  7612. struct drm_i915_private *dev_priv = dev->dev_private;
  7613. unsigned frontbuffer_bits;
  7614. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  7615. if (!obj->frontbuffer_bits)
  7616. return;
  7617. frontbuffer_bits = obj->frontbuffer_bits;
  7618. if (retire) {
  7619. mutex_lock(&dev_priv->fb_tracking.lock);
  7620. /* Filter out new bits since rendering started. */
  7621. frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
  7622. dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
  7623. mutex_unlock(&dev_priv->fb_tracking.lock);
  7624. }
  7625. intel_frontbuffer_flush(dev, frontbuffer_bits);
  7626. }
  7627. /**
  7628. * intel_frontbuffer_flip_prepare - prepare asnychronous frontbuffer flip
  7629. * @dev: DRM device
  7630. * @frontbuffer_bits: frontbuffer plane tracking bits
  7631. *
  7632. * This function gets called after scheduling a flip on @obj. The actual
  7633. * frontbuffer flushing will be delayed until completion is signalled with
  7634. * intel_frontbuffer_flip_complete. If an invalidate happens in between this
  7635. * flush will be cancelled.
  7636. *
  7637. * Can be called without any locks held.
  7638. */
  7639. void intel_frontbuffer_flip_prepare(struct drm_device *dev,
  7640. unsigned frontbuffer_bits)
  7641. {
  7642. struct drm_i915_private *dev_priv = dev->dev_private;
  7643. mutex_lock(&dev_priv->fb_tracking.lock);
  7644. dev_priv->fb_tracking.flip_bits
  7645. |= frontbuffer_bits;
  7646. mutex_unlock(&dev_priv->fb_tracking.lock);
  7647. }
  7648. /**
  7649. * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flush
  7650. * @dev: DRM device
  7651. * @frontbuffer_bits: frontbuffer plane tracking bits
  7652. *
  7653. * This function gets called after the flip has been latched and will complete
  7654. * on the next vblank. It will execute the fush if it hasn't been cancalled yet.
  7655. *
  7656. * Can be called without any locks held.
  7657. */
  7658. void intel_frontbuffer_flip_complete(struct drm_device *dev,
  7659. unsigned frontbuffer_bits)
  7660. {
  7661. struct drm_i915_private *dev_priv = dev->dev_private;
  7662. mutex_lock(&dev_priv->fb_tracking.lock);
  7663. /* Mask any cancelled flips. */
  7664. frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
  7665. dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
  7666. mutex_unlock(&dev_priv->fb_tracking.lock);
  7667. intel_frontbuffer_flush(dev, frontbuffer_bits);
  7668. }
  7669. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7670. {
  7671. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7672. struct drm_device *dev = crtc->dev;
  7673. struct intel_unpin_work *work;
  7674. unsigned long flags;
  7675. spin_lock_irqsave(&dev->event_lock, flags);
  7676. work = intel_crtc->unpin_work;
  7677. intel_crtc->unpin_work = NULL;
  7678. spin_unlock_irqrestore(&dev->event_lock, flags);
  7679. if (work) {
  7680. cancel_work_sync(&work->work);
  7681. kfree(work);
  7682. }
  7683. drm_crtc_cleanup(crtc);
  7684. kfree(intel_crtc);
  7685. }
  7686. static void intel_unpin_work_fn(struct work_struct *__work)
  7687. {
  7688. struct intel_unpin_work *work =
  7689. container_of(__work, struct intel_unpin_work, work);
  7690. struct drm_device *dev = work->crtc->dev;
  7691. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7692. mutex_lock(&dev->struct_mutex);
  7693. intel_unpin_fb_obj(work->old_fb_obj);
  7694. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7695. drm_gem_object_unreference(&work->old_fb_obj->base);
  7696. intel_update_fbc(dev);
  7697. mutex_unlock(&dev->struct_mutex);
  7698. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7699. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7700. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7701. kfree(work);
  7702. }
  7703. static void do_intel_finish_page_flip(struct drm_device *dev,
  7704. struct drm_crtc *crtc)
  7705. {
  7706. struct drm_i915_private *dev_priv = dev->dev_private;
  7707. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7708. struct intel_unpin_work *work;
  7709. unsigned long flags;
  7710. /* Ignore early vblank irqs */
  7711. if (intel_crtc == NULL)
  7712. return;
  7713. spin_lock_irqsave(&dev->event_lock, flags);
  7714. work = intel_crtc->unpin_work;
  7715. /* Ensure we don't miss a work->pending update ... */
  7716. smp_rmb();
  7717. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7718. spin_unlock_irqrestore(&dev->event_lock, flags);
  7719. return;
  7720. }
  7721. /* and that the unpin work is consistent wrt ->pending. */
  7722. smp_rmb();
  7723. intel_crtc->unpin_work = NULL;
  7724. if (work->event)
  7725. drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
  7726. drm_crtc_vblank_put(crtc);
  7727. spin_unlock_irqrestore(&dev->event_lock, flags);
  7728. wake_up_all(&dev_priv->pending_flip_queue);
  7729. queue_work(dev_priv->wq, &work->work);
  7730. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  7731. }
  7732. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7733. {
  7734. struct drm_i915_private *dev_priv = dev->dev_private;
  7735. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7736. do_intel_finish_page_flip(dev, crtc);
  7737. }
  7738. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7739. {
  7740. struct drm_i915_private *dev_priv = dev->dev_private;
  7741. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7742. do_intel_finish_page_flip(dev, crtc);
  7743. }
  7744. /* Is 'a' after or equal to 'b'? */
  7745. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7746. {
  7747. return !((a - b) & 0x80000000);
  7748. }
  7749. static bool page_flip_finished(struct intel_crtc *crtc)
  7750. {
  7751. struct drm_device *dev = crtc->base.dev;
  7752. struct drm_i915_private *dev_priv = dev->dev_private;
  7753. /*
  7754. * The relevant registers doen't exist on pre-ctg.
  7755. * As the flip done interrupt doesn't trigger for mmio
  7756. * flips on gmch platforms, a flip count check isn't
  7757. * really needed there. But since ctg has the registers,
  7758. * include it in the check anyway.
  7759. */
  7760. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7761. return true;
  7762. /*
  7763. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7764. * used the same base address. In that case the mmio flip might
  7765. * have completed, but the CS hasn't even executed the flip yet.
  7766. *
  7767. * A flip count check isn't enough as the CS might have updated
  7768. * the base address just after start of vblank, but before we
  7769. * managed to process the interrupt. This means we'd complete the
  7770. * CS flip too soon.
  7771. *
  7772. * Combining both checks should get us a good enough result. It may
  7773. * still happen that the CS flip has been executed, but has not
  7774. * yet actually completed. But in case the base address is the same
  7775. * anyway, we don't really care.
  7776. */
  7777. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7778. crtc->unpin_work->gtt_offset &&
  7779. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7780. crtc->unpin_work->flip_count);
  7781. }
  7782. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7783. {
  7784. struct drm_i915_private *dev_priv = dev->dev_private;
  7785. struct intel_crtc *intel_crtc =
  7786. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7787. unsigned long flags;
  7788. /* NB: An MMIO update of the plane base pointer will also
  7789. * generate a page-flip completion irq, i.e. every modeset
  7790. * is also accompanied by a spurious intel_prepare_page_flip().
  7791. */
  7792. spin_lock_irqsave(&dev->event_lock, flags);
  7793. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7794. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7795. spin_unlock_irqrestore(&dev->event_lock, flags);
  7796. }
  7797. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7798. {
  7799. /* Ensure that the work item is consistent when activating it ... */
  7800. smp_wmb();
  7801. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7802. /* and that it is marked active as soon as the irq could fire. */
  7803. smp_wmb();
  7804. }
  7805. static int intel_gen2_queue_flip(struct drm_device *dev,
  7806. struct drm_crtc *crtc,
  7807. struct drm_framebuffer *fb,
  7808. struct drm_i915_gem_object *obj,
  7809. struct intel_engine_cs *ring,
  7810. uint32_t flags)
  7811. {
  7812. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7813. u32 flip_mask;
  7814. int ret;
  7815. ret = intel_ring_begin(ring, 6);
  7816. if (ret)
  7817. return ret;
  7818. /* Can't queue multiple flips, so wait for the previous
  7819. * one to finish before executing the next.
  7820. */
  7821. if (intel_crtc->plane)
  7822. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7823. else
  7824. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7825. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7826. intel_ring_emit(ring, MI_NOOP);
  7827. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7828. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7829. intel_ring_emit(ring, fb->pitches[0]);
  7830. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7831. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7832. intel_mark_page_flip_active(intel_crtc);
  7833. __intel_ring_advance(ring);
  7834. return 0;
  7835. }
  7836. static int intel_gen3_queue_flip(struct drm_device *dev,
  7837. struct drm_crtc *crtc,
  7838. struct drm_framebuffer *fb,
  7839. struct drm_i915_gem_object *obj,
  7840. struct intel_engine_cs *ring,
  7841. uint32_t flags)
  7842. {
  7843. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7844. u32 flip_mask;
  7845. int ret;
  7846. ret = intel_ring_begin(ring, 6);
  7847. if (ret)
  7848. return ret;
  7849. if (intel_crtc->plane)
  7850. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7851. else
  7852. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7853. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7854. intel_ring_emit(ring, MI_NOOP);
  7855. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7856. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7857. intel_ring_emit(ring, fb->pitches[0]);
  7858. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7859. intel_ring_emit(ring, MI_NOOP);
  7860. intel_mark_page_flip_active(intel_crtc);
  7861. __intel_ring_advance(ring);
  7862. return 0;
  7863. }
  7864. static int intel_gen4_queue_flip(struct drm_device *dev,
  7865. struct drm_crtc *crtc,
  7866. struct drm_framebuffer *fb,
  7867. struct drm_i915_gem_object *obj,
  7868. struct intel_engine_cs *ring,
  7869. uint32_t flags)
  7870. {
  7871. struct drm_i915_private *dev_priv = dev->dev_private;
  7872. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7873. uint32_t pf, pipesrc;
  7874. int ret;
  7875. ret = intel_ring_begin(ring, 4);
  7876. if (ret)
  7877. return ret;
  7878. /* i965+ uses the linear or tiled offsets from the
  7879. * Display Registers (which do not change across a page-flip)
  7880. * so we need only reprogram the base address.
  7881. */
  7882. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7883. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7884. intel_ring_emit(ring, fb->pitches[0]);
  7885. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7886. obj->tiling_mode);
  7887. /* XXX Enabling the panel-fitter across page-flip is so far
  7888. * untested on non-native modes, so ignore it for now.
  7889. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7890. */
  7891. pf = 0;
  7892. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7893. intel_ring_emit(ring, pf | pipesrc);
  7894. intel_mark_page_flip_active(intel_crtc);
  7895. __intel_ring_advance(ring);
  7896. return 0;
  7897. }
  7898. static int intel_gen6_queue_flip(struct drm_device *dev,
  7899. struct drm_crtc *crtc,
  7900. struct drm_framebuffer *fb,
  7901. struct drm_i915_gem_object *obj,
  7902. struct intel_engine_cs *ring,
  7903. uint32_t flags)
  7904. {
  7905. struct drm_i915_private *dev_priv = dev->dev_private;
  7906. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7907. uint32_t pf, pipesrc;
  7908. int ret;
  7909. ret = intel_ring_begin(ring, 4);
  7910. if (ret)
  7911. return ret;
  7912. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7913. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7914. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7915. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7916. /* Contrary to the suggestions in the documentation,
  7917. * "Enable Panel Fitter" does not seem to be required when page
  7918. * flipping with a non-native mode, and worse causes a normal
  7919. * modeset to fail.
  7920. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7921. */
  7922. pf = 0;
  7923. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7924. intel_ring_emit(ring, pf | pipesrc);
  7925. intel_mark_page_flip_active(intel_crtc);
  7926. __intel_ring_advance(ring);
  7927. return 0;
  7928. }
  7929. static int intel_gen7_queue_flip(struct drm_device *dev,
  7930. struct drm_crtc *crtc,
  7931. struct drm_framebuffer *fb,
  7932. struct drm_i915_gem_object *obj,
  7933. struct intel_engine_cs *ring,
  7934. uint32_t flags)
  7935. {
  7936. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7937. uint32_t plane_bit = 0;
  7938. int len, ret;
  7939. switch (intel_crtc->plane) {
  7940. case PLANE_A:
  7941. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7942. break;
  7943. case PLANE_B:
  7944. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7945. break;
  7946. case PLANE_C:
  7947. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7948. break;
  7949. default:
  7950. WARN_ONCE(1, "unknown plane in flip command\n");
  7951. return -ENODEV;
  7952. }
  7953. len = 4;
  7954. if (ring->id == RCS) {
  7955. len += 6;
  7956. /*
  7957. * On Gen 8, SRM is now taking an extra dword to accommodate
  7958. * 48bits addresses, and we need a NOOP for the batch size to
  7959. * stay even.
  7960. */
  7961. if (IS_GEN8(dev))
  7962. len += 2;
  7963. }
  7964. /*
  7965. * BSpec MI_DISPLAY_FLIP for IVB:
  7966. * "The full packet must be contained within the same cache line."
  7967. *
  7968. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7969. * cacheline, if we ever start emitting more commands before
  7970. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7971. * then do the cacheline alignment, and finally emit the
  7972. * MI_DISPLAY_FLIP.
  7973. */
  7974. ret = intel_ring_cacheline_align(ring);
  7975. if (ret)
  7976. return ret;
  7977. ret = intel_ring_begin(ring, len);
  7978. if (ret)
  7979. return ret;
  7980. /* Unmask the flip-done completion message. Note that the bspec says that
  7981. * we should do this for both the BCS and RCS, and that we must not unmask
  7982. * more than one flip event at any time (or ensure that one flip message
  7983. * can be sent by waiting for flip-done prior to queueing new flips).
  7984. * Experimentation says that BCS works despite DERRMR masking all
  7985. * flip-done completion events and that unmasking all planes at once
  7986. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7987. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7988. */
  7989. if (ring->id == RCS) {
  7990. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7991. intel_ring_emit(ring, DERRMR);
  7992. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7993. DERRMR_PIPEB_PRI_FLIP_DONE |
  7994. DERRMR_PIPEC_PRI_FLIP_DONE));
  7995. if (IS_GEN8(dev))
  7996. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7997. MI_SRM_LRM_GLOBAL_GTT);
  7998. else
  7999. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  8000. MI_SRM_LRM_GLOBAL_GTT);
  8001. intel_ring_emit(ring, DERRMR);
  8002. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  8003. if (IS_GEN8(dev)) {
  8004. intel_ring_emit(ring, 0);
  8005. intel_ring_emit(ring, MI_NOOP);
  8006. }
  8007. }
  8008. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  8009. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  8010. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8011. intel_ring_emit(ring, (MI_NOOP));
  8012. intel_mark_page_flip_active(intel_crtc);
  8013. __intel_ring_advance(ring);
  8014. return 0;
  8015. }
  8016. static bool use_mmio_flip(struct intel_engine_cs *ring,
  8017. struct drm_i915_gem_object *obj)
  8018. {
  8019. /*
  8020. * This is not being used for older platforms, because
  8021. * non-availability of flip done interrupt forces us to use
  8022. * CS flips. Older platforms derive flip done using some clever
  8023. * tricks involving the flip_pending status bits and vblank irqs.
  8024. * So using MMIO flips there would disrupt this mechanism.
  8025. */
  8026. if (ring == NULL)
  8027. return true;
  8028. if (INTEL_INFO(ring->dev)->gen < 5)
  8029. return false;
  8030. if (i915.use_mmio_flip < 0)
  8031. return false;
  8032. else if (i915.use_mmio_flip > 0)
  8033. return true;
  8034. else if (i915.enable_execlists)
  8035. return true;
  8036. else
  8037. return ring != obj->ring;
  8038. }
  8039. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  8040. {
  8041. struct drm_device *dev = intel_crtc->base.dev;
  8042. struct drm_i915_private *dev_priv = dev->dev_private;
  8043. struct intel_framebuffer *intel_fb =
  8044. to_intel_framebuffer(intel_crtc->base.primary->fb);
  8045. struct drm_i915_gem_object *obj = intel_fb->obj;
  8046. u32 dspcntr;
  8047. u32 reg;
  8048. intel_mark_page_flip_active(intel_crtc);
  8049. reg = DSPCNTR(intel_crtc->plane);
  8050. dspcntr = I915_READ(reg);
  8051. if (INTEL_INFO(dev)->gen >= 4) {
  8052. if (obj->tiling_mode != I915_TILING_NONE)
  8053. dspcntr |= DISPPLANE_TILED;
  8054. else
  8055. dspcntr &= ~DISPPLANE_TILED;
  8056. }
  8057. I915_WRITE(reg, dspcntr);
  8058. I915_WRITE(DSPSURF(intel_crtc->plane),
  8059. intel_crtc->unpin_work->gtt_offset);
  8060. POSTING_READ(DSPSURF(intel_crtc->plane));
  8061. }
  8062. static int intel_postpone_flip(struct drm_i915_gem_object *obj)
  8063. {
  8064. struct intel_engine_cs *ring;
  8065. int ret;
  8066. lockdep_assert_held(&obj->base.dev->struct_mutex);
  8067. if (!obj->last_write_seqno)
  8068. return 0;
  8069. ring = obj->ring;
  8070. if (i915_seqno_passed(ring->get_seqno(ring, true),
  8071. obj->last_write_seqno))
  8072. return 0;
  8073. ret = i915_gem_check_olr(ring, obj->last_write_seqno);
  8074. if (ret)
  8075. return ret;
  8076. if (WARN_ON(!ring->irq_get(ring)))
  8077. return 0;
  8078. return 1;
  8079. }
  8080. void intel_notify_mmio_flip(struct intel_engine_cs *ring)
  8081. {
  8082. struct drm_i915_private *dev_priv = to_i915(ring->dev);
  8083. struct intel_crtc *intel_crtc;
  8084. unsigned long irq_flags;
  8085. u32 seqno;
  8086. seqno = ring->get_seqno(ring, false);
  8087. spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
  8088. for_each_intel_crtc(ring->dev, intel_crtc) {
  8089. struct intel_mmio_flip *mmio_flip;
  8090. mmio_flip = &intel_crtc->mmio_flip;
  8091. if (mmio_flip->seqno == 0)
  8092. continue;
  8093. if (ring->id != mmio_flip->ring_id)
  8094. continue;
  8095. if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
  8096. intel_do_mmio_flip(intel_crtc);
  8097. mmio_flip->seqno = 0;
  8098. ring->irq_put(ring);
  8099. }
  8100. }
  8101. spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
  8102. }
  8103. static int intel_queue_mmio_flip(struct drm_device *dev,
  8104. struct drm_crtc *crtc,
  8105. struct drm_framebuffer *fb,
  8106. struct drm_i915_gem_object *obj,
  8107. struct intel_engine_cs *ring,
  8108. uint32_t flags)
  8109. {
  8110. struct drm_i915_private *dev_priv = dev->dev_private;
  8111. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8112. unsigned long irq_flags;
  8113. int ret;
  8114. if (WARN_ON(intel_crtc->mmio_flip.seqno))
  8115. return -EBUSY;
  8116. ret = intel_postpone_flip(obj);
  8117. if (ret < 0)
  8118. return ret;
  8119. if (ret == 0) {
  8120. intel_do_mmio_flip(intel_crtc);
  8121. return 0;
  8122. }
  8123. spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
  8124. intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
  8125. intel_crtc->mmio_flip.ring_id = obj->ring->id;
  8126. spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
  8127. /*
  8128. * Double check to catch cases where irq fired before
  8129. * mmio flip data was ready
  8130. */
  8131. intel_notify_mmio_flip(obj->ring);
  8132. return 0;
  8133. }
  8134. static int intel_default_queue_flip(struct drm_device *dev,
  8135. struct drm_crtc *crtc,
  8136. struct drm_framebuffer *fb,
  8137. struct drm_i915_gem_object *obj,
  8138. struct intel_engine_cs *ring,
  8139. uint32_t flags)
  8140. {
  8141. return -ENODEV;
  8142. }
  8143. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8144. struct drm_framebuffer *fb,
  8145. struct drm_pending_vblank_event *event,
  8146. uint32_t page_flip_flags)
  8147. {
  8148. struct drm_device *dev = crtc->dev;
  8149. struct drm_i915_private *dev_priv = dev->dev_private;
  8150. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8151. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8152. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8153. enum pipe pipe = intel_crtc->pipe;
  8154. struct intel_unpin_work *work;
  8155. struct intel_engine_cs *ring;
  8156. unsigned long flags;
  8157. int ret;
  8158. /*
  8159. * drm_mode_page_flip_ioctl() should already catch this, but double
  8160. * check to be safe. In the future we may enable pageflipping from
  8161. * a disabled primary plane.
  8162. */
  8163. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8164. return -EBUSY;
  8165. /* Can't change pixel format via MI display flips. */
  8166. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8167. return -EINVAL;
  8168. /*
  8169. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8170. * Note that pitch changes could also affect these register.
  8171. */
  8172. if (INTEL_INFO(dev)->gen > 3 &&
  8173. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8174. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8175. return -EINVAL;
  8176. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8177. goto out_hang;
  8178. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8179. if (work == NULL)
  8180. return -ENOMEM;
  8181. work->event = event;
  8182. work->crtc = crtc;
  8183. work->old_fb_obj = intel_fb_obj(old_fb);
  8184. INIT_WORK(&work->work, intel_unpin_work_fn);
  8185. ret = drm_crtc_vblank_get(crtc);
  8186. if (ret)
  8187. goto free_work;
  8188. /* We borrow the event spin lock for protecting unpin_work */
  8189. spin_lock_irqsave(&dev->event_lock, flags);
  8190. if (intel_crtc->unpin_work) {
  8191. spin_unlock_irqrestore(&dev->event_lock, flags);
  8192. kfree(work);
  8193. drm_crtc_vblank_put(crtc);
  8194. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8195. return -EBUSY;
  8196. }
  8197. intel_crtc->unpin_work = work;
  8198. spin_unlock_irqrestore(&dev->event_lock, flags);
  8199. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8200. flush_workqueue(dev_priv->wq);
  8201. ret = i915_mutex_lock_interruptible(dev);
  8202. if (ret)
  8203. goto cleanup;
  8204. /* Reference the objects for the scheduled work. */
  8205. drm_gem_object_reference(&work->old_fb_obj->base);
  8206. drm_gem_object_reference(&obj->base);
  8207. crtc->primary->fb = fb;
  8208. work->pending_flip_obj = obj;
  8209. work->enable_stall_check = true;
  8210. atomic_inc(&intel_crtc->unpin_work_count);
  8211. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8212. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8213. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8214. if (IS_VALLEYVIEW(dev)) {
  8215. ring = &dev_priv->ring[BCS];
  8216. if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
  8217. /* vlv: DISPLAY_FLIP fails to change tiling */
  8218. ring = NULL;
  8219. } else if (IS_IVYBRIDGE(dev)) {
  8220. ring = &dev_priv->ring[BCS];
  8221. } else if (INTEL_INFO(dev)->gen >= 7) {
  8222. ring = obj->ring;
  8223. if (ring == NULL || ring->id != RCS)
  8224. ring = &dev_priv->ring[BCS];
  8225. } else {
  8226. ring = &dev_priv->ring[RCS];
  8227. }
  8228. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  8229. if (ret)
  8230. goto cleanup_pending;
  8231. work->gtt_offset =
  8232. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8233. if (use_mmio_flip(ring, obj))
  8234. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8235. page_flip_flags);
  8236. else
  8237. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8238. page_flip_flags);
  8239. if (ret)
  8240. goto cleanup_unpin;
  8241. i915_gem_track_fb(work->old_fb_obj, obj,
  8242. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8243. intel_disable_fbc(dev);
  8244. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8245. mutex_unlock(&dev->struct_mutex);
  8246. trace_i915_flip_request(intel_crtc->plane, obj);
  8247. return 0;
  8248. cleanup_unpin:
  8249. intel_unpin_fb_obj(obj);
  8250. cleanup_pending:
  8251. atomic_dec(&intel_crtc->unpin_work_count);
  8252. crtc->primary->fb = old_fb;
  8253. drm_gem_object_unreference(&work->old_fb_obj->base);
  8254. drm_gem_object_unreference(&obj->base);
  8255. mutex_unlock(&dev->struct_mutex);
  8256. cleanup:
  8257. spin_lock_irqsave(&dev->event_lock, flags);
  8258. intel_crtc->unpin_work = NULL;
  8259. spin_unlock_irqrestore(&dev->event_lock, flags);
  8260. drm_crtc_vblank_put(crtc);
  8261. free_work:
  8262. kfree(work);
  8263. if (ret == -EIO) {
  8264. out_hang:
  8265. intel_crtc_wait_for_pending_flips(crtc);
  8266. ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
  8267. if (ret == 0 && event)
  8268. drm_send_vblank_event(dev, pipe, event);
  8269. }
  8270. return ret;
  8271. }
  8272. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8273. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8274. .load_lut = intel_crtc_load_lut,
  8275. };
  8276. /**
  8277. * intel_modeset_update_staged_output_state
  8278. *
  8279. * Updates the staged output configuration state, e.g. after we've read out the
  8280. * current hw state.
  8281. */
  8282. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8283. {
  8284. struct intel_crtc *crtc;
  8285. struct intel_encoder *encoder;
  8286. struct intel_connector *connector;
  8287. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8288. base.head) {
  8289. connector->new_encoder =
  8290. to_intel_encoder(connector->base.encoder);
  8291. }
  8292. for_each_intel_encoder(dev, encoder) {
  8293. encoder->new_crtc =
  8294. to_intel_crtc(encoder->base.crtc);
  8295. }
  8296. for_each_intel_crtc(dev, crtc) {
  8297. crtc->new_enabled = crtc->base.enabled;
  8298. if (crtc->new_enabled)
  8299. crtc->new_config = &crtc->config;
  8300. else
  8301. crtc->new_config = NULL;
  8302. }
  8303. }
  8304. /**
  8305. * intel_modeset_commit_output_state
  8306. *
  8307. * This function copies the stage display pipe configuration to the real one.
  8308. */
  8309. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8310. {
  8311. struct intel_crtc *crtc;
  8312. struct intel_encoder *encoder;
  8313. struct intel_connector *connector;
  8314. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8315. base.head) {
  8316. connector->base.encoder = &connector->new_encoder->base;
  8317. }
  8318. for_each_intel_encoder(dev, encoder) {
  8319. encoder->base.crtc = &encoder->new_crtc->base;
  8320. }
  8321. for_each_intel_crtc(dev, crtc) {
  8322. crtc->base.enabled = crtc->new_enabled;
  8323. }
  8324. }
  8325. static void
  8326. connected_sink_compute_bpp(struct intel_connector *connector,
  8327. struct intel_crtc_config *pipe_config)
  8328. {
  8329. int bpp = pipe_config->pipe_bpp;
  8330. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8331. connector->base.base.id,
  8332. connector->base.name);
  8333. /* Don't use an invalid EDID bpc value */
  8334. if (connector->base.display_info.bpc &&
  8335. connector->base.display_info.bpc * 3 < bpp) {
  8336. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8337. bpp, connector->base.display_info.bpc*3);
  8338. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8339. }
  8340. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8341. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8342. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8343. bpp);
  8344. pipe_config->pipe_bpp = 24;
  8345. }
  8346. }
  8347. static int
  8348. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8349. struct drm_framebuffer *fb,
  8350. struct intel_crtc_config *pipe_config)
  8351. {
  8352. struct drm_device *dev = crtc->base.dev;
  8353. struct intel_connector *connector;
  8354. int bpp;
  8355. switch (fb->pixel_format) {
  8356. case DRM_FORMAT_C8:
  8357. bpp = 8*3; /* since we go through a colormap */
  8358. break;
  8359. case DRM_FORMAT_XRGB1555:
  8360. case DRM_FORMAT_ARGB1555:
  8361. /* checked in intel_framebuffer_init already */
  8362. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8363. return -EINVAL;
  8364. case DRM_FORMAT_RGB565:
  8365. bpp = 6*3; /* min is 18bpp */
  8366. break;
  8367. case DRM_FORMAT_XBGR8888:
  8368. case DRM_FORMAT_ABGR8888:
  8369. /* checked in intel_framebuffer_init already */
  8370. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8371. return -EINVAL;
  8372. case DRM_FORMAT_XRGB8888:
  8373. case DRM_FORMAT_ARGB8888:
  8374. bpp = 8*3;
  8375. break;
  8376. case DRM_FORMAT_XRGB2101010:
  8377. case DRM_FORMAT_ARGB2101010:
  8378. case DRM_FORMAT_XBGR2101010:
  8379. case DRM_FORMAT_ABGR2101010:
  8380. /* checked in intel_framebuffer_init already */
  8381. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8382. return -EINVAL;
  8383. bpp = 10*3;
  8384. break;
  8385. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8386. default:
  8387. DRM_DEBUG_KMS("unsupported depth\n");
  8388. return -EINVAL;
  8389. }
  8390. pipe_config->pipe_bpp = bpp;
  8391. /* Clamp display bpp to EDID value */
  8392. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8393. base.head) {
  8394. if (!connector->new_encoder ||
  8395. connector->new_encoder->new_crtc != crtc)
  8396. continue;
  8397. connected_sink_compute_bpp(connector, pipe_config);
  8398. }
  8399. return bpp;
  8400. }
  8401. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8402. {
  8403. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8404. "type: 0x%x flags: 0x%x\n",
  8405. mode->crtc_clock,
  8406. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8407. mode->crtc_hsync_end, mode->crtc_htotal,
  8408. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8409. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8410. }
  8411. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8412. struct intel_crtc_config *pipe_config,
  8413. const char *context)
  8414. {
  8415. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8416. context, pipe_name(crtc->pipe));
  8417. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8418. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8419. pipe_config->pipe_bpp, pipe_config->dither);
  8420. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8421. pipe_config->has_pch_encoder,
  8422. pipe_config->fdi_lanes,
  8423. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8424. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8425. pipe_config->fdi_m_n.tu);
  8426. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8427. pipe_config->has_dp_encoder,
  8428. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8429. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8430. pipe_config->dp_m_n.tu);
  8431. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8432. pipe_config->has_dp_encoder,
  8433. pipe_config->dp_m2_n2.gmch_m,
  8434. pipe_config->dp_m2_n2.gmch_n,
  8435. pipe_config->dp_m2_n2.link_m,
  8436. pipe_config->dp_m2_n2.link_n,
  8437. pipe_config->dp_m2_n2.tu);
  8438. DRM_DEBUG_KMS("requested mode:\n");
  8439. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  8440. DRM_DEBUG_KMS("adjusted mode:\n");
  8441. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  8442. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  8443. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8444. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8445. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8446. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8447. pipe_config->gmch_pfit.control,
  8448. pipe_config->gmch_pfit.pgm_ratios,
  8449. pipe_config->gmch_pfit.lvds_border_bits);
  8450. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8451. pipe_config->pch_pfit.pos,
  8452. pipe_config->pch_pfit.size,
  8453. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8454. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8455. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8456. }
  8457. static bool encoders_cloneable(const struct intel_encoder *a,
  8458. const struct intel_encoder *b)
  8459. {
  8460. /* masks could be asymmetric, so check both ways */
  8461. return a == b || (a->cloneable & (1 << b->type) &&
  8462. b->cloneable & (1 << a->type));
  8463. }
  8464. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8465. struct intel_encoder *encoder)
  8466. {
  8467. struct drm_device *dev = crtc->base.dev;
  8468. struct intel_encoder *source_encoder;
  8469. for_each_intel_encoder(dev, source_encoder) {
  8470. if (source_encoder->new_crtc != crtc)
  8471. continue;
  8472. if (!encoders_cloneable(encoder, source_encoder))
  8473. return false;
  8474. }
  8475. return true;
  8476. }
  8477. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8478. {
  8479. struct drm_device *dev = crtc->base.dev;
  8480. struct intel_encoder *encoder;
  8481. for_each_intel_encoder(dev, encoder) {
  8482. if (encoder->new_crtc != crtc)
  8483. continue;
  8484. if (!check_single_encoder_cloning(crtc, encoder))
  8485. return false;
  8486. }
  8487. return true;
  8488. }
  8489. static struct intel_crtc_config *
  8490. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8491. struct drm_framebuffer *fb,
  8492. struct drm_display_mode *mode)
  8493. {
  8494. struct drm_device *dev = crtc->dev;
  8495. struct intel_encoder *encoder;
  8496. struct intel_crtc_config *pipe_config;
  8497. int plane_bpp, ret = -EINVAL;
  8498. bool retry = true;
  8499. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8500. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8501. return ERR_PTR(-EINVAL);
  8502. }
  8503. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8504. if (!pipe_config)
  8505. return ERR_PTR(-ENOMEM);
  8506. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  8507. drm_mode_copy(&pipe_config->requested_mode, mode);
  8508. pipe_config->cpu_transcoder =
  8509. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8510. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8511. /*
  8512. * Sanitize sync polarity flags based on requested ones. If neither
  8513. * positive or negative polarity is requested, treat this as meaning
  8514. * negative polarity.
  8515. */
  8516. if (!(pipe_config->adjusted_mode.flags &
  8517. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8518. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8519. if (!(pipe_config->adjusted_mode.flags &
  8520. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8521. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8522. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8523. * plane pixel format and any sink constraints into account. Returns the
  8524. * source plane bpp so that dithering can be selected on mismatches
  8525. * after encoders and crtc also have had their say. */
  8526. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8527. fb, pipe_config);
  8528. if (plane_bpp < 0)
  8529. goto fail;
  8530. /*
  8531. * Determine the real pipe dimensions. Note that stereo modes can
  8532. * increase the actual pipe size due to the frame doubling and
  8533. * insertion of additional space for blanks between the frame. This
  8534. * is stored in the crtc timings. We use the requested mode to do this
  8535. * computation to clearly distinguish it from the adjusted mode, which
  8536. * can be changed by the connectors in the below retry loop.
  8537. */
  8538. drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
  8539. pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
  8540. pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
  8541. encoder_retry:
  8542. /* Ensure the port clock defaults are reset when retrying. */
  8543. pipe_config->port_clock = 0;
  8544. pipe_config->pixel_multiplier = 1;
  8545. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8546. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  8547. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8548. * adjust it according to limitations or connector properties, and also
  8549. * a chance to reject the mode entirely.
  8550. */
  8551. for_each_intel_encoder(dev, encoder) {
  8552. if (&encoder->new_crtc->base != crtc)
  8553. continue;
  8554. if (!(encoder->compute_config(encoder, pipe_config))) {
  8555. DRM_DEBUG_KMS("Encoder config failure\n");
  8556. goto fail;
  8557. }
  8558. }
  8559. /* Set default port clock if not overwritten by the encoder. Needs to be
  8560. * done afterwards in case the encoder adjusts the mode. */
  8561. if (!pipe_config->port_clock)
  8562. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  8563. * pipe_config->pixel_multiplier;
  8564. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8565. if (ret < 0) {
  8566. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8567. goto fail;
  8568. }
  8569. if (ret == RETRY) {
  8570. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8571. ret = -EINVAL;
  8572. goto fail;
  8573. }
  8574. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8575. retry = false;
  8576. goto encoder_retry;
  8577. }
  8578. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8579. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8580. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8581. return pipe_config;
  8582. fail:
  8583. kfree(pipe_config);
  8584. return ERR_PTR(ret);
  8585. }
  8586. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8587. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8588. static void
  8589. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8590. unsigned *prepare_pipes, unsigned *disable_pipes)
  8591. {
  8592. struct intel_crtc *intel_crtc;
  8593. struct drm_device *dev = crtc->dev;
  8594. struct intel_encoder *encoder;
  8595. struct intel_connector *connector;
  8596. struct drm_crtc *tmp_crtc;
  8597. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8598. /* Check which crtcs have changed outputs connected to them, these need
  8599. * to be part of the prepare_pipes mask. We don't (yet) support global
  8600. * modeset across multiple crtcs, so modeset_pipes will only have one
  8601. * bit set at most. */
  8602. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8603. base.head) {
  8604. if (connector->base.encoder == &connector->new_encoder->base)
  8605. continue;
  8606. if (connector->base.encoder) {
  8607. tmp_crtc = connector->base.encoder->crtc;
  8608. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8609. }
  8610. if (connector->new_encoder)
  8611. *prepare_pipes |=
  8612. 1 << connector->new_encoder->new_crtc->pipe;
  8613. }
  8614. for_each_intel_encoder(dev, encoder) {
  8615. if (encoder->base.crtc == &encoder->new_crtc->base)
  8616. continue;
  8617. if (encoder->base.crtc) {
  8618. tmp_crtc = encoder->base.crtc;
  8619. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8620. }
  8621. if (encoder->new_crtc)
  8622. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8623. }
  8624. /* Check for pipes that will be enabled/disabled ... */
  8625. for_each_intel_crtc(dev, intel_crtc) {
  8626. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8627. continue;
  8628. if (!intel_crtc->new_enabled)
  8629. *disable_pipes |= 1 << intel_crtc->pipe;
  8630. else
  8631. *prepare_pipes |= 1 << intel_crtc->pipe;
  8632. }
  8633. /* set_mode is also used to update properties on life display pipes. */
  8634. intel_crtc = to_intel_crtc(crtc);
  8635. if (intel_crtc->new_enabled)
  8636. *prepare_pipes |= 1 << intel_crtc->pipe;
  8637. /*
  8638. * For simplicity do a full modeset on any pipe where the output routing
  8639. * changed. We could be more clever, but that would require us to be
  8640. * more careful with calling the relevant encoder->mode_set functions.
  8641. */
  8642. if (*prepare_pipes)
  8643. *modeset_pipes = *prepare_pipes;
  8644. /* ... and mask these out. */
  8645. *modeset_pipes &= ~(*disable_pipes);
  8646. *prepare_pipes &= ~(*disable_pipes);
  8647. /*
  8648. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8649. * obies this rule, but the modeset restore mode of
  8650. * intel_modeset_setup_hw_state does not.
  8651. */
  8652. *modeset_pipes &= 1 << intel_crtc->pipe;
  8653. *prepare_pipes &= 1 << intel_crtc->pipe;
  8654. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8655. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8656. }
  8657. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8658. {
  8659. struct drm_encoder *encoder;
  8660. struct drm_device *dev = crtc->dev;
  8661. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8662. if (encoder->crtc == crtc)
  8663. return true;
  8664. return false;
  8665. }
  8666. static void
  8667. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8668. {
  8669. struct intel_encoder *intel_encoder;
  8670. struct intel_crtc *intel_crtc;
  8671. struct drm_connector *connector;
  8672. for_each_intel_encoder(dev, intel_encoder) {
  8673. if (!intel_encoder->base.crtc)
  8674. continue;
  8675. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8676. if (prepare_pipes & (1 << intel_crtc->pipe))
  8677. intel_encoder->connectors_active = false;
  8678. }
  8679. intel_modeset_commit_output_state(dev);
  8680. /* Double check state. */
  8681. for_each_intel_crtc(dev, intel_crtc) {
  8682. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8683. WARN_ON(intel_crtc->new_config &&
  8684. intel_crtc->new_config != &intel_crtc->config);
  8685. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8686. }
  8687. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8688. if (!connector->encoder || !connector->encoder->crtc)
  8689. continue;
  8690. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8691. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8692. struct drm_property *dpms_property =
  8693. dev->mode_config.dpms_property;
  8694. connector->dpms = DRM_MODE_DPMS_ON;
  8695. drm_object_property_set_value(&connector->base,
  8696. dpms_property,
  8697. DRM_MODE_DPMS_ON);
  8698. intel_encoder = to_intel_encoder(connector->encoder);
  8699. intel_encoder->connectors_active = true;
  8700. }
  8701. }
  8702. }
  8703. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8704. {
  8705. int diff;
  8706. if (clock1 == clock2)
  8707. return true;
  8708. if (!clock1 || !clock2)
  8709. return false;
  8710. diff = abs(clock1 - clock2);
  8711. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8712. return true;
  8713. return false;
  8714. }
  8715. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8716. list_for_each_entry((intel_crtc), \
  8717. &(dev)->mode_config.crtc_list, \
  8718. base.head) \
  8719. if (mask & (1 <<(intel_crtc)->pipe))
  8720. static bool
  8721. intel_pipe_config_compare(struct drm_device *dev,
  8722. struct intel_crtc_config *current_config,
  8723. struct intel_crtc_config *pipe_config)
  8724. {
  8725. #define PIPE_CONF_CHECK_X(name) \
  8726. if (current_config->name != pipe_config->name) { \
  8727. DRM_ERROR("mismatch in " #name " " \
  8728. "(expected 0x%08x, found 0x%08x)\n", \
  8729. current_config->name, \
  8730. pipe_config->name); \
  8731. return false; \
  8732. }
  8733. #define PIPE_CONF_CHECK_I(name) \
  8734. if (current_config->name != pipe_config->name) { \
  8735. DRM_ERROR("mismatch in " #name " " \
  8736. "(expected %i, found %i)\n", \
  8737. current_config->name, \
  8738. pipe_config->name); \
  8739. return false; \
  8740. }
  8741. /* This is required for BDW+ where there is only one set of registers for
  8742. * switching between high and low RR.
  8743. * This macro can be used whenever a comparison has to be made between one
  8744. * hw state and multiple sw state variables.
  8745. */
  8746. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  8747. if ((current_config->name != pipe_config->name) && \
  8748. (current_config->alt_name != pipe_config->name)) { \
  8749. DRM_ERROR("mismatch in " #name " " \
  8750. "(expected %i or %i, found %i)\n", \
  8751. current_config->name, \
  8752. current_config->alt_name, \
  8753. pipe_config->name); \
  8754. return false; \
  8755. }
  8756. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8757. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8758. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8759. "(expected %i, found %i)\n", \
  8760. current_config->name & (mask), \
  8761. pipe_config->name & (mask)); \
  8762. return false; \
  8763. }
  8764. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8765. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8766. DRM_ERROR("mismatch in " #name " " \
  8767. "(expected %i, found %i)\n", \
  8768. current_config->name, \
  8769. pipe_config->name); \
  8770. return false; \
  8771. }
  8772. #define PIPE_CONF_QUIRK(quirk) \
  8773. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8774. PIPE_CONF_CHECK_I(cpu_transcoder);
  8775. PIPE_CONF_CHECK_I(has_pch_encoder);
  8776. PIPE_CONF_CHECK_I(fdi_lanes);
  8777. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8778. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8779. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8780. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8781. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8782. PIPE_CONF_CHECK_I(has_dp_encoder);
  8783. if (INTEL_INFO(dev)->gen < 8) {
  8784. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8785. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8786. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8787. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8788. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8789. if (current_config->has_drrs) {
  8790. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  8791. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  8792. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  8793. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  8794. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  8795. }
  8796. } else {
  8797. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  8798. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  8799. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  8800. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  8801. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  8802. }
  8803. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  8804. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  8805. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  8806. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  8807. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  8808. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  8809. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  8810. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  8811. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  8812. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  8813. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  8814. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  8815. PIPE_CONF_CHECK_I(pixel_multiplier);
  8816. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8817. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8818. IS_VALLEYVIEW(dev))
  8819. PIPE_CONF_CHECK_I(limited_color_range);
  8820. PIPE_CONF_CHECK_I(has_audio);
  8821. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8822. DRM_MODE_FLAG_INTERLACE);
  8823. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8824. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8825. DRM_MODE_FLAG_PHSYNC);
  8826. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8827. DRM_MODE_FLAG_NHSYNC);
  8828. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8829. DRM_MODE_FLAG_PVSYNC);
  8830. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8831. DRM_MODE_FLAG_NVSYNC);
  8832. }
  8833. PIPE_CONF_CHECK_I(pipe_src_w);
  8834. PIPE_CONF_CHECK_I(pipe_src_h);
  8835. /*
  8836. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8837. * screen. Since we don't yet re-compute the pipe config when moving
  8838. * just the lvds port away to another pipe the sw tracking won't match.
  8839. *
  8840. * Proper atomic modesets with recomputed global state will fix this.
  8841. * Until then just don't check gmch state for inherited modes.
  8842. */
  8843. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8844. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8845. /* pfit ratios are autocomputed by the hw on gen4+ */
  8846. if (INTEL_INFO(dev)->gen < 4)
  8847. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8848. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8849. }
  8850. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8851. if (current_config->pch_pfit.enabled) {
  8852. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8853. PIPE_CONF_CHECK_I(pch_pfit.size);
  8854. }
  8855. /* BDW+ don't expose a synchronous way to read the state */
  8856. if (IS_HASWELL(dev))
  8857. PIPE_CONF_CHECK_I(ips_enabled);
  8858. PIPE_CONF_CHECK_I(double_wide);
  8859. PIPE_CONF_CHECK_X(ddi_pll_sel);
  8860. PIPE_CONF_CHECK_I(shared_dpll);
  8861. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8862. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8863. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8864. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8865. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  8866. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8867. PIPE_CONF_CHECK_I(pipe_bpp);
  8868. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  8869. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8870. #undef PIPE_CONF_CHECK_X
  8871. #undef PIPE_CONF_CHECK_I
  8872. #undef PIPE_CONF_CHECK_I_ALT
  8873. #undef PIPE_CONF_CHECK_FLAGS
  8874. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8875. #undef PIPE_CONF_QUIRK
  8876. return true;
  8877. }
  8878. static void
  8879. check_connector_state(struct drm_device *dev)
  8880. {
  8881. struct intel_connector *connector;
  8882. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8883. base.head) {
  8884. /* This also checks the encoder/connector hw state with the
  8885. * ->get_hw_state callbacks. */
  8886. intel_connector_check_state(connector);
  8887. WARN(&connector->new_encoder->base != connector->base.encoder,
  8888. "connector's staged encoder doesn't match current encoder\n");
  8889. }
  8890. }
  8891. static void
  8892. check_encoder_state(struct drm_device *dev)
  8893. {
  8894. struct intel_encoder *encoder;
  8895. struct intel_connector *connector;
  8896. for_each_intel_encoder(dev, encoder) {
  8897. bool enabled = false;
  8898. bool active = false;
  8899. enum pipe pipe, tracked_pipe;
  8900. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8901. encoder->base.base.id,
  8902. encoder->base.name);
  8903. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  8904. "encoder's stage crtc doesn't match current crtc\n");
  8905. WARN(encoder->connectors_active && !encoder->base.crtc,
  8906. "encoder's active_connectors set, but no crtc\n");
  8907. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8908. base.head) {
  8909. if (connector->base.encoder != &encoder->base)
  8910. continue;
  8911. enabled = true;
  8912. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  8913. active = true;
  8914. }
  8915. /*
  8916. * for MST connectors if we unplug the connector is gone
  8917. * away but the encoder is still connected to a crtc
  8918. * until a modeset happens in response to the hotplug.
  8919. */
  8920. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  8921. continue;
  8922. WARN(!!encoder->base.crtc != enabled,
  8923. "encoder's enabled state mismatch "
  8924. "(expected %i, found %i)\n",
  8925. !!encoder->base.crtc, enabled);
  8926. WARN(active && !encoder->base.crtc,
  8927. "active encoder with no crtc\n");
  8928. WARN(encoder->connectors_active != active,
  8929. "encoder's computed active state doesn't match tracked active state "
  8930. "(expected %i, found %i)\n", active, encoder->connectors_active);
  8931. active = encoder->get_hw_state(encoder, &pipe);
  8932. WARN(active != encoder->connectors_active,
  8933. "encoder's hw state doesn't match sw tracking "
  8934. "(expected %i, found %i)\n",
  8935. encoder->connectors_active, active);
  8936. if (!encoder->base.crtc)
  8937. continue;
  8938. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  8939. WARN(active && pipe != tracked_pipe,
  8940. "active encoder's pipe doesn't match"
  8941. "(expected %i, found %i)\n",
  8942. tracked_pipe, pipe);
  8943. }
  8944. }
  8945. static void
  8946. check_crtc_state(struct drm_device *dev)
  8947. {
  8948. struct drm_i915_private *dev_priv = dev->dev_private;
  8949. struct intel_crtc *crtc;
  8950. struct intel_encoder *encoder;
  8951. struct intel_crtc_config pipe_config;
  8952. for_each_intel_crtc(dev, crtc) {
  8953. bool enabled = false;
  8954. bool active = false;
  8955. memset(&pipe_config, 0, sizeof(pipe_config));
  8956. DRM_DEBUG_KMS("[CRTC:%d]\n",
  8957. crtc->base.base.id);
  8958. WARN(crtc->active && !crtc->base.enabled,
  8959. "active crtc, but not enabled in sw tracking\n");
  8960. for_each_intel_encoder(dev, encoder) {
  8961. if (encoder->base.crtc != &crtc->base)
  8962. continue;
  8963. enabled = true;
  8964. if (encoder->connectors_active)
  8965. active = true;
  8966. }
  8967. WARN(active != crtc->active,
  8968. "crtc's computed active state doesn't match tracked active state "
  8969. "(expected %i, found %i)\n", active, crtc->active);
  8970. WARN(enabled != crtc->base.enabled,
  8971. "crtc's computed enabled state doesn't match tracked enabled state "
  8972. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  8973. active = dev_priv->display.get_pipe_config(crtc,
  8974. &pipe_config);
  8975. /* hw state is inconsistent with the pipe A quirk */
  8976. if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  8977. active = crtc->active;
  8978. for_each_intel_encoder(dev, encoder) {
  8979. enum pipe pipe;
  8980. if (encoder->base.crtc != &crtc->base)
  8981. continue;
  8982. if (encoder->get_hw_state(encoder, &pipe))
  8983. encoder->get_config(encoder, &pipe_config);
  8984. }
  8985. WARN(crtc->active != active,
  8986. "crtc active state doesn't match with hw state "
  8987. "(expected %i, found %i)\n", crtc->active, active);
  8988. if (active &&
  8989. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  8990. WARN(1, "pipe state doesn't match!\n");
  8991. intel_dump_pipe_config(crtc, &pipe_config,
  8992. "[hw state]");
  8993. intel_dump_pipe_config(crtc, &crtc->config,
  8994. "[sw state]");
  8995. }
  8996. }
  8997. }
  8998. static void
  8999. check_shared_dpll_state(struct drm_device *dev)
  9000. {
  9001. struct drm_i915_private *dev_priv = dev->dev_private;
  9002. struct intel_crtc *crtc;
  9003. struct intel_dpll_hw_state dpll_hw_state;
  9004. int i;
  9005. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9006. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9007. int enabled_crtcs = 0, active_crtcs = 0;
  9008. bool active;
  9009. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9010. DRM_DEBUG_KMS("%s\n", pll->name);
  9011. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9012. WARN(pll->active > pll->refcount,
  9013. "more active pll users than references: %i vs %i\n",
  9014. pll->active, pll->refcount);
  9015. WARN(pll->active && !pll->on,
  9016. "pll in active use but not on in sw tracking\n");
  9017. WARN(pll->on && !pll->active,
  9018. "pll in on but not on in use in sw tracking\n");
  9019. WARN(pll->on != active,
  9020. "pll on state mismatch (expected %i, found %i)\n",
  9021. pll->on, active);
  9022. for_each_intel_crtc(dev, crtc) {
  9023. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  9024. enabled_crtcs++;
  9025. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9026. active_crtcs++;
  9027. }
  9028. WARN(pll->active != active_crtcs,
  9029. "pll active crtcs mismatch (expected %i, found %i)\n",
  9030. pll->active, active_crtcs);
  9031. WARN(pll->refcount != enabled_crtcs,
  9032. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9033. pll->refcount, enabled_crtcs);
  9034. WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
  9035. sizeof(dpll_hw_state)),
  9036. "pll hw state mismatch\n");
  9037. }
  9038. }
  9039. void
  9040. intel_modeset_check_state(struct drm_device *dev)
  9041. {
  9042. check_connector_state(dev);
  9043. check_encoder_state(dev);
  9044. check_crtc_state(dev);
  9045. check_shared_dpll_state(dev);
  9046. }
  9047. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  9048. int dotclock)
  9049. {
  9050. /*
  9051. * FDI already provided one idea for the dotclock.
  9052. * Yell if the encoder disagrees.
  9053. */
  9054. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  9055. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9056. pipe_config->adjusted_mode.crtc_clock, dotclock);
  9057. }
  9058. static void update_scanline_offset(struct intel_crtc *crtc)
  9059. {
  9060. struct drm_device *dev = crtc->base.dev;
  9061. /*
  9062. * The scanline counter increments at the leading edge of hsync.
  9063. *
  9064. * On most platforms it starts counting from vtotal-1 on the
  9065. * first active line. That means the scanline counter value is
  9066. * always one less than what we would expect. Ie. just after
  9067. * start of vblank, which also occurs at start of hsync (on the
  9068. * last active line), the scanline counter will read vblank_start-1.
  9069. *
  9070. * On gen2 the scanline counter starts counting from 1 instead
  9071. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9072. * to keep the value positive), instead of adding one.
  9073. *
  9074. * On HSW+ the behaviour of the scanline counter depends on the output
  9075. * type. For DP ports it behaves like most other platforms, but on HDMI
  9076. * there's an extra 1 line difference. So we need to add two instead of
  9077. * one to the value.
  9078. */
  9079. if (IS_GEN2(dev)) {
  9080. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  9081. int vtotal;
  9082. vtotal = mode->crtc_vtotal;
  9083. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9084. vtotal /= 2;
  9085. crtc->scanline_offset = vtotal - 1;
  9086. } else if (HAS_DDI(dev) &&
  9087. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
  9088. crtc->scanline_offset = 2;
  9089. } else
  9090. crtc->scanline_offset = 1;
  9091. }
  9092. static int __intel_set_mode(struct drm_crtc *crtc,
  9093. struct drm_display_mode *mode,
  9094. int x, int y, struct drm_framebuffer *fb)
  9095. {
  9096. struct drm_device *dev = crtc->dev;
  9097. struct drm_i915_private *dev_priv = dev->dev_private;
  9098. struct drm_display_mode *saved_mode;
  9099. struct intel_crtc_config *pipe_config = NULL;
  9100. struct intel_crtc *intel_crtc;
  9101. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  9102. int ret = 0;
  9103. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9104. if (!saved_mode)
  9105. return -ENOMEM;
  9106. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  9107. &prepare_pipes, &disable_pipes);
  9108. *saved_mode = crtc->mode;
  9109. /* Hack: Because we don't (yet) support global modeset on multiple
  9110. * crtcs, we don't keep track of the new mode for more than one crtc.
  9111. * Hence simply check whether any bit is set in modeset_pipes in all the
  9112. * pieces of code that are not yet converted to deal with mutliple crtcs
  9113. * changing their mode at the same time. */
  9114. if (modeset_pipes) {
  9115. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9116. if (IS_ERR(pipe_config)) {
  9117. ret = PTR_ERR(pipe_config);
  9118. pipe_config = NULL;
  9119. goto out;
  9120. }
  9121. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9122. "[modeset]");
  9123. to_intel_crtc(crtc)->new_config = pipe_config;
  9124. }
  9125. /*
  9126. * See if the config requires any additional preparation, e.g.
  9127. * to adjust global state with pipes off. We need to do this
  9128. * here so we can get the modeset_pipe updated config for the new
  9129. * mode set on this crtc. For other crtcs we need to use the
  9130. * adjusted_mode bits in the crtc directly.
  9131. */
  9132. if (IS_VALLEYVIEW(dev)) {
  9133. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9134. /* may have added more to prepare_pipes than we should */
  9135. prepare_pipes &= ~disable_pipes;
  9136. }
  9137. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9138. intel_crtc_disable(&intel_crtc->base);
  9139. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9140. if (intel_crtc->base.enabled)
  9141. dev_priv->display.crtc_disable(&intel_crtc->base);
  9142. }
  9143. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9144. * to set it here already despite that we pass it down the callchain.
  9145. */
  9146. if (modeset_pipes) {
  9147. crtc->mode = *mode;
  9148. /* mode_set/enable/disable functions rely on a correct pipe
  9149. * config. */
  9150. to_intel_crtc(crtc)->config = *pipe_config;
  9151. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  9152. /*
  9153. * Calculate and store various constants which
  9154. * are later needed by vblank and swap-completion
  9155. * timestamping. They are derived from true hwmode.
  9156. */
  9157. drm_calc_timestamping_constants(crtc,
  9158. &pipe_config->adjusted_mode);
  9159. }
  9160. /* Only after disabling all output pipelines that will be changed can we
  9161. * update the the output configuration. */
  9162. intel_modeset_update_state(dev, prepare_pipes);
  9163. if (dev_priv->display.modeset_global_resources)
  9164. dev_priv->display.modeset_global_resources(dev);
  9165. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9166. * on the DPLL.
  9167. */
  9168. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9169. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9170. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
  9171. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9172. mutex_lock(&dev->struct_mutex);
  9173. ret = intel_pin_and_fence_fb_obj(dev,
  9174. obj,
  9175. NULL);
  9176. if (ret != 0) {
  9177. DRM_ERROR("pin & fence failed\n");
  9178. mutex_unlock(&dev->struct_mutex);
  9179. goto done;
  9180. }
  9181. if (old_fb)
  9182. intel_unpin_fb_obj(old_obj);
  9183. i915_gem_track_fb(old_obj, obj,
  9184. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9185. mutex_unlock(&dev->struct_mutex);
  9186. crtc->primary->fb = fb;
  9187. crtc->x = x;
  9188. crtc->y = y;
  9189. ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
  9190. x, y, fb);
  9191. if (ret)
  9192. goto done;
  9193. }
  9194. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9195. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9196. update_scanline_offset(intel_crtc);
  9197. dev_priv->display.crtc_enable(&intel_crtc->base);
  9198. }
  9199. /* FIXME: add subpixel order */
  9200. done:
  9201. if (ret && crtc->enabled)
  9202. crtc->mode = *saved_mode;
  9203. out:
  9204. kfree(pipe_config);
  9205. kfree(saved_mode);
  9206. return ret;
  9207. }
  9208. static int intel_set_mode(struct drm_crtc *crtc,
  9209. struct drm_display_mode *mode,
  9210. int x, int y, struct drm_framebuffer *fb)
  9211. {
  9212. int ret;
  9213. ret = __intel_set_mode(crtc, mode, x, y, fb);
  9214. if (ret == 0)
  9215. intel_modeset_check_state(crtc->dev);
  9216. return ret;
  9217. }
  9218. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9219. {
  9220. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9221. }
  9222. #undef for_each_intel_crtc_masked
  9223. static void intel_set_config_free(struct intel_set_config *config)
  9224. {
  9225. if (!config)
  9226. return;
  9227. kfree(config->save_connector_encoders);
  9228. kfree(config->save_encoder_crtcs);
  9229. kfree(config->save_crtc_enabled);
  9230. kfree(config);
  9231. }
  9232. static int intel_set_config_save_state(struct drm_device *dev,
  9233. struct intel_set_config *config)
  9234. {
  9235. struct drm_crtc *crtc;
  9236. struct drm_encoder *encoder;
  9237. struct drm_connector *connector;
  9238. int count;
  9239. config->save_crtc_enabled =
  9240. kcalloc(dev->mode_config.num_crtc,
  9241. sizeof(bool), GFP_KERNEL);
  9242. if (!config->save_crtc_enabled)
  9243. return -ENOMEM;
  9244. config->save_encoder_crtcs =
  9245. kcalloc(dev->mode_config.num_encoder,
  9246. sizeof(struct drm_crtc *), GFP_KERNEL);
  9247. if (!config->save_encoder_crtcs)
  9248. return -ENOMEM;
  9249. config->save_connector_encoders =
  9250. kcalloc(dev->mode_config.num_connector,
  9251. sizeof(struct drm_encoder *), GFP_KERNEL);
  9252. if (!config->save_connector_encoders)
  9253. return -ENOMEM;
  9254. /* Copy data. Note that driver private data is not affected.
  9255. * Should anything bad happen only the expected state is
  9256. * restored, not the drivers personal bookkeeping.
  9257. */
  9258. count = 0;
  9259. for_each_crtc(dev, crtc) {
  9260. config->save_crtc_enabled[count++] = crtc->enabled;
  9261. }
  9262. count = 0;
  9263. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9264. config->save_encoder_crtcs[count++] = encoder->crtc;
  9265. }
  9266. count = 0;
  9267. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9268. config->save_connector_encoders[count++] = connector->encoder;
  9269. }
  9270. return 0;
  9271. }
  9272. static void intel_set_config_restore_state(struct drm_device *dev,
  9273. struct intel_set_config *config)
  9274. {
  9275. struct intel_crtc *crtc;
  9276. struct intel_encoder *encoder;
  9277. struct intel_connector *connector;
  9278. int count;
  9279. count = 0;
  9280. for_each_intel_crtc(dev, crtc) {
  9281. crtc->new_enabled = config->save_crtc_enabled[count++];
  9282. if (crtc->new_enabled)
  9283. crtc->new_config = &crtc->config;
  9284. else
  9285. crtc->new_config = NULL;
  9286. }
  9287. count = 0;
  9288. for_each_intel_encoder(dev, encoder) {
  9289. encoder->new_crtc =
  9290. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9291. }
  9292. count = 0;
  9293. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9294. connector->new_encoder =
  9295. to_intel_encoder(config->save_connector_encoders[count++]);
  9296. }
  9297. }
  9298. static bool
  9299. is_crtc_connector_off(struct drm_mode_set *set)
  9300. {
  9301. int i;
  9302. if (set->num_connectors == 0)
  9303. return false;
  9304. if (WARN_ON(set->connectors == NULL))
  9305. return false;
  9306. for (i = 0; i < set->num_connectors; i++)
  9307. if (set->connectors[i]->encoder &&
  9308. set->connectors[i]->encoder->crtc == set->crtc &&
  9309. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9310. return true;
  9311. return false;
  9312. }
  9313. static void
  9314. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9315. struct intel_set_config *config)
  9316. {
  9317. /* We should be able to check here if the fb has the same properties
  9318. * and then just flip_or_move it */
  9319. if (is_crtc_connector_off(set)) {
  9320. config->mode_changed = true;
  9321. } else if (set->crtc->primary->fb != set->fb) {
  9322. /*
  9323. * If we have no fb, we can only flip as long as the crtc is
  9324. * active, otherwise we need a full mode set. The crtc may
  9325. * be active if we've only disabled the primary plane, or
  9326. * in fastboot situations.
  9327. */
  9328. if (set->crtc->primary->fb == NULL) {
  9329. struct intel_crtc *intel_crtc =
  9330. to_intel_crtc(set->crtc);
  9331. if (intel_crtc->active) {
  9332. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9333. config->fb_changed = true;
  9334. } else {
  9335. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9336. config->mode_changed = true;
  9337. }
  9338. } else if (set->fb == NULL) {
  9339. config->mode_changed = true;
  9340. } else if (set->fb->pixel_format !=
  9341. set->crtc->primary->fb->pixel_format) {
  9342. config->mode_changed = true;
  9343. } else {
  9344. config->fb_changed = true;
  9345. }
  9346. }
  9347. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9348. config->fb_changed = true;
  9349. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9350. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9351. drm_mode_debug_printmodeline(&set->crtc->mode);
  9352. drm_mode_debug_printmodeline(set->mode);
  9353. config->mode_changed = true;
  9354. }
  9355. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9356. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9357. }
  9358. static int
  9359. intel_modeset_stage_output_state(struct drm_device *dev,
  9360. struct drm_mode_set *set,
  9361. struct intel_set_config *config)
  9362. {
  9363. struct intel_connector *connector;
  9364. struct intel_encoder *encoder;
  9365. struct intel_crtc *crtc;
  9366. int ro;
  9367. /* The upper layers ensure that we either disable a crtc or have a list
  9368. * of connectors. For paranoia, double-check this. */
  9369. WARN_ON(!set->fb && (set->num_connectors != 0));
  9370. WARN_ON(set->fb && (set->num_connectors == 0));
  9371. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9372. base.head) {
  9373. /* Otherwise traverse passed in connector list and get encoders
  9374. * for them. */
  9375. for (ro = 0; ro < set->num_connectors; ro++) {
  9376. if (set->connectors[ro] == &connector->base) {
  9377. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9378. break;
  9379. }
  9380. }
  9381. /* If we disable the crtc, disable all its connectors. Also, if
  9382. * the connector is on the changing crtc but not on the new
  9383. * connector list, disable it. */
  9384. if ((!set->fb || ro == set->num_connectors) &&
  9385. connector->base.encoder &&
  9386. connector->base.encoder->crtc == set->crtc) {
  9387. connector->new_encoder = NULL;
  9388. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9389. connector->base.base.id,
  9390. connector->base.name);
  9391. }
  9392. if (&connector->new_encoder->base != connector->base.encoder) {
  9393. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  9394. config->mode_changed = true;
  9395. }
  9396. }
  9397. /* connector->new_encoder is now updated for all connectors. */
  9398. /* Update crtc of enabled connectors. */
  9399. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9400. base.head) {
  9401. struct drm_crtc *new_crtc;
  9402. if (!connector->new_encoder)
  9403. continue;
  9404. new_crtc = connector->new_encoder->base.crtc;
  9405. for (ro = 0; ro < set->num_connectors; ro++) {
  9406. if (set->connectors[ro] == &connector->base)
  9407. new_crtc = set->crtc;
  9408. }
  9409. /* Make sure the new CRTC will work with the encoder */
  9410. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9411. new_crtc)) {
  9412. return -EINVAL;
  9413. }
  9414. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9415. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9416. connector->base.base.id,
  9417. connector->base.name,
  9418. new_crtc->base.id);
  9419. }
  9420. /* Check for any encoders that needs to be disabled. */
  9421. for_each_intel_encoder(dev, encoder) {
  9422. int num_connectors = 0;
  9423. list_for_each_entry(connector,
  9424. &dev->mode_config.connector_list,
  9425. base.head) {
  9426. if (connector->new_encoder == encoder) {
  9427. WARN_ON(!connector->new_encoder->new_crtc);
  9428. num_connectors++;
  9429. }
  9430. }
  9431. if (num_connectors == 0)
  9432. encoder->new_crtc = NULL;
  9433. else if (num_connectors > 1)
  9434. return -EINVAL;
  9435. /* Only now check for crtc changes so we don't miss encoders
  9436. * that will be disabled. */
  9437. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9438. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  9439. config->mode_changed = true;
  9440. }
  9441. }
  9442. /* Now we've also updated encoder->new_crtc for all encoders. */
  9443. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9444. base.head) {
  9445. if (connector->new_encoder)
  9446. if (connector->new_encoder != connector->encoder)
  9447. connector->encoder = connector->new_encoder;
  9448. }
  9449. for_each_intel_crtc(dev, crtc) {
  9450. crtc->new_enabled = false;
  9451. for_each_intel_encoder(dev, encoder) {
  9452. if (encoder->new_crtc == crtc) {
  9453. crtc->new_enabled = true;
  9454. break;
  9455. }
  9456. }
  9457. if (crtc->new_enabled != crtc->base.enabled) {
  9458. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  9459. crtc->new_enabled ? "en" : "dis");
  9460. config->mode_changed = true;
  9461. }
  9462. if (crtc->new_enabled)
  9463. crtc->new_config = &crtc->config;
  9464. else
  9465. crtc->new_config = NULL;
  9466. }
  9467. return 0;
  9468. }
  9469. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9470. {
  9471. struct drm_device *dev = crtc->base.dev;
  9472. struct intel_encoder *encoder;
  9473. struct intel_connector *connector;
  9474. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9475. pipe_name(crtc->pipe));
  9476. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9477. if (connector->new_encoder &&
  9478. connector->new_encoder->new_crtc == crtc)
  9479. connector->new_encoder = NULL;
  9480. }
  9481. for_each_intel_encoder(dev, encoder) {
  9482. if (encoder->new_crtc == crtc)
  9483. encoder->new_crtc = NULL;
  9484. }
  9485. crtc->new_enabled = false;
  9486. crtc->new_config = NULL;
  9487. }
  9488. static int intel_crtc_set_config(struct drm_mode_set *set)
  9489. {
  9490. struct drm_device *dev;
  9491. struct drm_mode_set save_set;
  9492. struct intel_set_config *config;
  9493. int ret;
  9494. BUG_ON(!set);
  9495. BUG_ON(!set->crtc);
  9496. BUG_ON(!set->crtc->helper_private);
  9497. /* Enforce sane interface api - has been abused by the fb helper. */
  9498. BUG_ON(!set->mode && set->fb);
  9499. BUG_ON(set->fb && set->num_connectors == 0);
  9500. if (set->fb) {
  9501. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9502. set->crtc->base.id, set->fb->base.id,
  9503. (int)set->num_connectors, set->x, set->y);
  9504. } else {
  9505. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9506. }
  9507. dev = set->crtc->dev;
  9508. ret = -ENOMEM;
  9509. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9510. if (!config)
  9511. goto out_config;
  9512. ret = intel_set_config_save_state(dev, config);
  9513. if (ret)
  9514. goto out_config;
  9515. save_set.crtc = set->crtc;
  9516. save_set.mode = &set->crtc->mode;
  9517. save_set.x = set->crtc->x;
  9518. save_set.y = set->crtc->y;
  9519. save_set.fb = set->crtc->primary->fb;
  9520. /* Compute whether we need a full modeset, only an fb base update or no
  9521. * change at all. In the future we might also check whether only the
  9522. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9523. * such cases. */
  9524. intel_set_config_compute_mode_changes(set, config);
  9525. ret = intel_modeset_stage_output_state(dev, set, config);
  9526. if (ret)
  9527. goto fail;
  9528. if (config->mode_changed) {
  9529. ret = intel_set_mode(set->crtc, set->mode,
  9530. set->x, set->y, set->fb);
  9531. } else if (config->fb_changed) {
  9532. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9533. intel_crtc_wait_for_pending_flips(set->crtc);
  9534. ret = intel_pipe_set_base(set->crtc,
  9535. set->x, set->y, set->fb);
  9536. /*
  9537. * We need to make sure the primary plane is re-enabled if it
  9538. * has previously been turned off.
  9539. */
  9540. if (!intel_crtc->primary_enabled && ret == 0) {
  9541. WARN_ON(!intel_crtc->active);
  9542. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9543. }
  9544. /*
  9545. * In the fastboot case this may be our only check of the
  9546. * state after boot. It would be better to only do it on
  9547. * the first update, but we don't have a nice way of doing that
  9548. * (and really, set_config isn't used much for high freq page
  9549. * flipping, so increasing its cost here shouldn't be a big
  9550. * deal).
  9551. */
  9552. if (i915.fastboot && ret == 0)
  9553. intel_modeset_check_state(set->crtc->dev);
  9554. }
  9555. if (ret) {
  9556. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9557. set->crtc->base.id, ret);
  9558. fail:
  9559. intel_set_config_restore_state(dev, config);
  9560. /*
  9561. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9562. * force the pipe off to avoid oopsing in the modeset code
  9563. * due to fb==NULL. This should only happen during boot since
  9564. * we don't yet reconstruct the FB from the hardware state.
  9565. */
  9566. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9567. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9568. /* Try to restore the config */
  9569. if (config->mode_changed &&
  9570. intel_set_mode(save_set.crtc, save_set.mode,
  9571. save_set.x, save_set.y, save_set.fb))
  9572. DRM_ERROR("failed to restore config after modeset failure\n");
  9573. }
  9574. out_config:
  9575. intel_set_config_free(config);
  9576. return ret;
  9577. }
  9578. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9579. .gamma_set = intel_crtc_gamma_set,
  9580. .set_config = intel_crtc_set_config,
  9581. .destroy = intel_crtc_destroy,
  9582. .page_flip = intel_crtc_page_flip,
  9583. };
  9584. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9585. struct intel_shared_dpll *pll,
  9586. struct intel_dpll_hw_state *hw_state)
  9587. {
  9588. uint32_t val;
  9589. if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9590. return false;
  9591. val = I915_READ(PCH_DPLL(pll->id));
  9592. hw_state->dpll = val;
  9593. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9594. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9595. return val & DPLL_VCO_ENABLE;
  9596. }
  9597. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9598. struct intel_shared_dpll *pll)
  9599. {
  9600. I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
  9601. I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
  9602. }
  9603. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9604. struct intel_shared_dpll *pll)
  9605. {
  9606. /* PCH refclock must be enabled first */
  9607. ibx_assert_pch_refclk_enabled(dev_priv);
  9608. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9609. /* Wait for the clocks to stabilize. */
  9610. POSTING_READ(PCH_DPLL(pll->id));
  9611. udelay(150);
  9612. /* The pixel multiplier can only be updated once the
  9613. * DPLL is enabled and the clocks are stable.
  9614. *
  9615. * So write it again.
  9616. */
  9617. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9618. POSTING_READ(PCH_DPLL(pll->id));
  9619. udelay(200);
  9620. }
  9621. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9622. struct intel_shared_dpll *pll)
  9623. {
  9624. struct drm_device *dev = dev_priv->dev;
  9625. struct intel_crtc *crtc;
  9626. /* Make sure no transcoder isn't still depending on us. */
  9627. for_each_intel_crtc(dev, crtc) {
  9628. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9629. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9630. }
  9631. I915_WRITE(PCH_DPLL(pll->id), 0);
  9632. POSTING_READ(PCH_DPLL(pll->id));
  9633. udelay(200);
  9634. }
  9635. static char *ibx_pch_dpll_names[] = {
  9636. "PCH DPLL A",
  9637. "PCH DPLL B",
  9638. };
  9639. static void ibx_pch_dpll_init(struct drm_device *dev)
  9640. {
  9641. struct drm_i915_private *dev_priv = dev->dev_private;
  9642. int i;
  9643. dev_priv->num_shared_dpll = 2;
  9644. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9645. dev_priv->shared_dplls[i].id = i;
  9646. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9647. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9648. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9649. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9650. dev_priv->shared_dplls[i].get_hw_state =
  9651. ibx_pch_dpll_get_hw_state;
  9652. }
  9653. }
  9654. static void intel_shared_dpll_init(struct drm_device *dev)
  9655. {
  9656. struct drm_i915_private *dev_priv = dev->dev_private;
  9657. if (HAS_DDI(dev))
  9658. intel_ddi_pll_init(dev);
  9659. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9660. ibx_pch_dpll_init(dev);
  9661. else
  9662. dev_priv->num_shared_dpll = 0;
  9663. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9664. }
  9665. static int
  9666. intel_primary_plane_disable(struct drm_plane *plane)
  9667. {
  9668. struct drm_device *dev = plane->dev;
  9669. struct intel_crtc *intel_crtc;
  9670. if (!plane->fb)
  9671. return 0;
  9672. BUG_ON(!plane->crtc);
  9673. intel_crtc = to_intel_crtc(plane->crtc);
  9674. /*
  9675. * Even though we checked plane->fb above, it's still possible that
  9676. * the primary plane has been implicitly disabled because the crtc
  9677. * coordinates given weren't visible, or because we detected
  9678. * that it was 100% covered by a sprite plane. Or, the CRTC may be
  9679. * off and we've set a fb, but haven't actually turned on the CRTC yet.
  9680. * In either case, we need to unpin the FB and let the fb pointer get
  9681. * updated, but otherwise we don't need to touch the hardware.
  9682. */
  9683. if (!intel_crtc->primary_enabled)
  9684. goto disable_unpin;
  9685. intel_crtc_wait_for_pending_flips(plane->crtc);
  9686. intel_disable_primary_hw_plane(plane, plane->crtc);
  9687. disable_unpin:
  9688. mutex_lock(&dev->struct_mutex);
  9689. i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
  9690. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9691. intel_unpin_fb_obj(intel_fb_obj(plane->fb));
  9692. mutex_unlock(&dev->struct_mutex);
  9693. plane->fb = NULL;
  9694. return 0;
  9695. }
  9696. static int
  9697. intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
  9698. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9699. unsigned int crtc_w, unsigned int crtc_h,
  9700. uint32_t src_x, uint32_t src_y,
  9701. uint32_t src_w, uint32_t src_h)
  9702. {
  9703. struct drm_device *dev = crtc->dev;
  9704. struct drm_i915_private *dev_priv = dev->dev_private;
  9705. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9706. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9707. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9708. struct drm_rect dest = {
  9709. /* integer pixels */
  9710. .x1 = crtc_x,
  9711. .y1 = crtc_y,
  9712. .x2 = crtc_x + crtc_w,
  9713. .y2 = crtc_y + crtc_h,
  9714. };
  9715. struct drm_rect src = {
  9716. /* 16.16 fixed point */
  9717. .x1 = src_x,
  9718. .y1 = src_y,
  9719. .x2 = src_x + src_w,
  9720. .y2 = src_y + src_h,
  9721. };
  9722. const struct drm_rect clip = {
  9723. /* integer pixels */
  9724. .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
  9725. .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
  9726. };
  9727. const struct {
  9728. int crtc_x, crtc_y;
  9729. unsigned int crtc_w, crtc_h;
  9730. uint32_t src_x, src_y, src_w, src_h;
  9731. } orig = {
  9732. .crtc_x = crtc_x,
  9733. .crtc_y = crtc_y,
  9734. .crtc_w = crtc_w,
  9735. .crtc_h = crtc_h,
  9736. .src_x = src_x,
  9737. .src_y = src_y,
  9738. .src_w = src_w,
  9739. .src_h = src_h,
  9740. };
  9741. struct intel_plane *intel_plane = to_intel_plane(plane);
  9742. bool visible;
  9743. int ret;
  9744. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9745. &src, &dest, &clip,
  9746. DRM_PLANE_HELPER_NO_SCALING,
  9747. DRM_PLANE_HELPER_NO_SCALING,
  9748. false, true, &visible);
  9749. if (ret)
  9750. return ret;
  9751. /*
  9752. * If the CRTC isn't enabled, we're just pinning the framebuffer,
  9753. * updating the fb pointer, and returning without touching the
  9754. * hardware. This allows us to later do a drmModeSetCrtc with fb=-1 to
  9755. * turn on the display with all planes setup as desired.
  9756. */
  9757. if (!crtc->enabled) {
  9758. mutex_lock(&dev->struct_mutex);
  9759. /*
  9760. * If we already called setplane while the crtc was disabled,
  9761. * we may have an fb pinned; unpin it.
  9762. */
  9763. if (plane->fb)
  9764. intel_unpin_fb_obj(old_obj);
  9765. i915_gem_track_fb(old_obj, obj,
  9766. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9767. /* Pin and return without programming hardware */
  9768. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  9769. mutex_unlock(&dev->struct_mutex);
  9770. return ret;
  9771. }
  9772. intel_crtc_wait_for_pending_flips(crtc);
  9773. /*
  9774. * If clipping results in a non-visible primary plane, we'll disable
  9775. * the primary plane. Note that this is a bit different than what
  9776. * happens if userspace explicitly disables the plane by passing fb=0
  9777. * because plane->fb still gets set and pinned.
  9778. */
  9779. if (!visible) {
  9780. mutex_lock(&dev->struct_mutex);
  9781. /*
  9782. * Try to pin the new fb first so that we can bail out if we
  9783. * fail.
  9784. */
  9785. if (plane->fb != fb) {
  9786. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  9787. if (ret) {
  9788. mutex_unlock(&dev->struct_mutex);
  9789. return ret;
  9790. }
  9791. }
  9792. i915_gem_track_fb(old_obj, obj,
  9793. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9794. if (intel_crtc->primary_enabled)
  9795. intel_disable_primary_hw_plane(plane, crtc);
  9796. if (plane->fb != fb)
  9797. if (plane->fb)
  9798. intel_unpin_fb_obj(old_obj);
  9799. mutex_unlock(&dev->struct_mutex);
  9800. } else {
  9801. if (intel_crtc && intel_crtc->active &&
  9802. intel_crtc->primary_enabled) {
  9803. /*
  9804. * FBC does not work on some platforms for rotated
  9805. * planes, so disable it when rotation is not 0 and
  9806. * update it when rotation is set back to 0.
  9807. *
  9808. * FIXME: This is redundant with the fbc update done in
  9809. * the primary plane enable function except that that
  9810. * one is done too late. We eventually need to unify
  9811. * this.
  9812. */
  9813. if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9814. dev_priv->fbc.plane == intel_crtc->plane &&
  9815. intel_plane->rotation != BIT(DRM_ROTATE_0)) {
  9816. intel_disable_fbc(dev);
  9817. }
  9818. }
  9819. ret = intel_pipe_set_base(crtc, src.x1, src.y1, fb);
  9820. if (ret)
  9821. return ret;
  9822. if (!intel_crtc->primary_enabled)
  9823. intel_enable_primary_hw_plane(plane, crtc);
  9824. }
  9825. intel_plane->crtc_x = orig.crtc_x;
  9826. intel_plane->crtc_y = orig.crtc_y;
  9827. intel_plane->crtc_w = orig.crtc_w;
  9828. intel_plane->crtc_h = orig.crtc_h;
  9829. intel_plane->src_x = orig.src_x;
  9830. intel_plane->src_y = orig.src_y;
  9831. intel_plane->src_w = orig.src_w;
  9832. intel_plane->src_h = orig.src_h;
  9833. intel_plane->obj = obj;
  9834. return 0;
  9835. }
  9836. /* Common destruction function for both primary and cursor planes */
  9837. static void intel_plane_destroy(struct drm_plane *plane)
  9838. {
  9839. struct intel_plane *intel_plane = to_intel_plane(plane);
  9840. drm_plane_cleanup(plane);
  9841. kfree(intel_plane);
  9842. }
  9843. static const struct drm_plane_funcs intel_primary_plane_funcs = {
  9844. .update_plane = intel_primary_plane_setplane,
  9845. .disable_plane = intel_primary_plane_disable,
  9846. .destroy = intel_plane_destroy,
  9847. .set_property = intel_plane_set_property
  9848. };
  9849. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  9850. int pipe)
  9851. {
  9852. struct intel_plane *primary;
  9853. const uint32_t *intel_primary_formats;
  9854. int num_formats;
  9855. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  9856. if (primary == NULL)
  9857. return NULL;
  9858. primary->can_scale = false;
  9859. primary->max_downscale = 1;
  9860. primary->pipe = pipe;
  9861. primary->plane = pipe;
  9862. primary->rotation = BIT(DRM_ROTATE_0);
  9863. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  9864. primary->plane = !pipe;
  9865. if (INTEL_INFO(dev)->gen <= 3) {
  9866. intel_primary_formats = intel_primary_formats_gen2;
  9867. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  9868. } else {
  9869. intel_primary_formats = intel_primary_formats_gen4;
  9870. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  9871. }
  9872. drm_universal_plane_init(dev, &primary->base, 0,
  9873. &intel_primary_plane_funcs,
  9874. intel_primary_formats, num_formats,
  9875. DRM_PLANE_TYPE_PRIMARY);
  9876. if (INTEL_INFO(dev)->gen >= 4) {
  9877. if (!dev->mode_config.rotation_property)
  9878. dev->mode_config.rotation_property =
  9879. drm_mode_create_rotation_property(dev,
  9880. BIT(DRM_ROTATE_0) |
  9881. BIT(DRM_ROTATE_180));
  9882. if (dev->mode_config.rotation_property)
  9883. drm_object_attach_property(&primary->base.base,
  9884. dev->mode_config.rotation_property,
  9885. primary->rotation);
  9886. }
  9887. return &primary->base;
  9888. }
  9889. static int
  9890. intel_cursor_plane_disable(struct drm_plane *plane)
  9891. {
  9892. if (!plane->fb)
  9893. return 0;
  9894. BUG_ON(!plane->crtc);
  9895. return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
  9896. }
  9897. static int
  9898. intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
  9899. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9900. unsigned int crtc_w, unsigned int crtc_h,
  9901. uint32_t src_x, uint32_t src_y,
  9902. uint32_t src_w, uint32_t src_h)
  9903. {
  9904. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9905. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  9906. struct drm_i915_gem_object *obj = intel_fb->obj;
  9907. struct drm_rect dest = {
  9908. /* integer pixels */
  9909. .x1 = crtc_x,
  9910. .y1 = crtc_y,
  9911. .x2 = crtc_x + crtc_w,
  9912. .y2 = crtc_y + crtc_h,
  9913. };
  9914. struct drm_rect src = {
  9915. /* 16.16 fixed point */
  9916. .x1 = src_x,
  9917. .y1 = src_y,
  9918. .x2 = src_x + src_w,
  9919. .y2 = src_y + src_h,
  9920. };
  9921. const struct drm_rect clip = {
  9922. /* integer pixels */
  9923. .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
  9924. .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
  9925. };
  9926. bool visible;
  9927. int ret;
  9928. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9929. &src, &dest, &clip,
  9930. DRM_PLANE_HELPER_NO_SCALING,
  9931. DRM_PLANE_HELPER_NO_SCALING,
  9932. true, true, &visible);
  9933. if (ret)
  9934. return ret;
  9935. crtc->cursor_x = crtc_x;
  9936. crtc->cursor_y = crtc_y;
  9937. if (fb != crtc->cursor->fb) {
  9938. return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
  9939. } else {
  9940. intel_crtc_update_cursor(crtc, visible);
  9941. intel_frontbuffer_flip(crtc->dev,
  9942. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
  9943. return 0;
  9944. }
  9945. }
  9946. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  9947. .update_plane = intel_cursor_plane_update,
  9948. .disable_plane = intel_cursor_plane_disable,
  9949. .destroy = intel_plane_destroy,
  9950. };
  9951. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  9952. int pipe)
  9953. {
  9954. struct intel_plane *cursor;
  9955. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  9956. if (cursor == NULL)
  9957. return NULL;
  9958. cursor->can_scale = false;
  9959. cursor->max_downscale = 1;
  9960. cursor->pipe = pipe;
  9961. cursor->plane = pipe;
  9962. drm_universal_plane_init(dev, &cursor->base, 0,
  9963. &intel_cursor_plane_funcs,
  9964. intel_cursor_formats,
  9965. ARRAY_SIZE(intel_cursor_formats),
  9966. DRM_PLANE_TYPE_CURSOR);
  9967. return &cursor->base;
  9968. }
  9969. static void intel_crtc_init(struct drm_device *dev, int pipe)
  9970. {
  9971. struct drm_i915_private *dev_priv = dev->dev_private;
  9972. struct intel_crtc *intel_crtc;
  9973. struct drm_plane *primary = NULL;
  9974. struct drm_plane *cursor = NULL;
  9975. int i, ret;
  9976. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  9977. if (intel_crtc == NULL)
  9978. return;
  9979. primary = intel_primary_plane_create(dev, pipe);
  9980. if (!primary)
  9981. goto fail;
  9982. cursor = intel_cursor_plane_create(dev, pipe);
  9983. if (!cursor)
  9984. goto fail;
  9985. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  9986. cursor, &intel_crtc_funcs);
  9987. if (ret)
  9988. goto fail;
  9989. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  9990. for (i = 0; i < 256; i++) {
  9991. intel_crtc->lut_r[i] = i;
  9992. intel_crtc->lut_g[i] = i;
  9993. intel_crtc->lut_b[i] = i;
  9994. }
  9995. /*
  9996. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  9997. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  9998. */
  9999. intel_crtc->pipe = pipe;
  10000. intel_crtc->plane = pipe;
  10001. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  10002. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  10003. intel_crtc->plane = !pipe;
  10004. }
  10005. intel_crtc->cursor_base = ~0;
  10006. intel_crtc->cursor_cntl = ~0;
  10007. intel_crtc->cursor_size = ~0;
  10008. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  10009. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  10010. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  10011. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  10012. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  10013. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  10014. return;
  10015. fail:
  10016. if (primary)
  10017. drm_plane_cleanup(primary);
  10018. if (cursor)
  10019. drm_plane_cleanup(cursor);
  10020. kfree(intel_crtc);
  10021. }
  10022. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  10023. {
  10024. struct drm_encoder *encoder = connector->base.encoder;
  10025. struct drm_device *dev = connector->base.dev;
  10026. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  10027. if (!encoder)
  10028. return INVALID_PIPE;
  10029. return to_intel_crtc(encoder->crtc)->pipe;
  10030. }
  10031. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  10032. struct drm_file *file)
  10033. {
  10034. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  10035. struct drm_crtc *drmmode_crtc;
  10036. struct intel_crtc *crtc;
  10037. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  10038. return -ENODEV;
  10039. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  10040. if (!drmmode_crtc) {
  10041. DRM_ERROR("no such CRTC id\n");
  10042. return -ENOENT;
  10043. }
  10044. crtc = to_intel_crtc(drmmode_crtc);
  10045. pipe_from_crtc_id->pipe = crtc->pipe;
  10046. return 0;
  10047. }
  10048. static int intel_encoder_clones(struct intel_encoder *encoder)
  10049. {
  10050. struct drm_device *dev = encoder->base.dev;
  10051. struct intel_encoder *source_encoder;
  10052. int index_mask = 0;
  10053. int entry = 0;
  10054. for_each_intel_encoder(dev, source_encoder) {
  10055. if (encoders_cloneable(encoder, source_encoder))
  10056. index_mask |= (1 << entry);
  10057. entry++;
  10058. }
  10059. return index_mask;
  10060. }
  10061. static bool has_edp_a(struct drm_device *dev)
  10062. {
  10063. struct drm_i915_private *dev_priv = dev->dev_private;
  10064. if (!IS_MOBILE(dev))
  10065. return false;
  10066. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  10067. return false;
  10068. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  10069. return false;
  10070. return true;
  10071. }
  10072. const char *intel_output_name(int output)
  10073. {
  10074. static const char *names[] = {
  10075. [INTEL_OUTPUT_UNUSED] = "Unused",
  10076. [INTEL_OUTPUT_ANALOG] = "Analog",
  10077. [INTEL_OUTPUT_DVO] = "DVO",
  10078. [INTEL_OUTPUT_SDVO] = "SDVO",
  10079. [INTEL_OUTPUT_LVDS] = "LVDS",
  10080. [INTEL_OUTPUT_TVOUT] = "TV",
  10081. [INTEL_OUTPUT_HDMI] = "HDMI",
  10082. [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
  10083. [INTEL_OUTPUT_EDP] = "eDP",
  10084. [INTEL_OUTPUT_DSI] = "DSI",
  10085. [INTEL_OUTPUT_UNKNOWN] = "Unknown",
  10086. };
  10087. if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
  10088. return "Invalid";
  10089. return names[output];
  10090. }
  10091. static bool intel_crt_present(struct drm_device *dev)
  10092. {
  10093. struct drm_i915_private *dev_priv = dev->dev_private;
  10094. if (IS_ULT(dev))
  10095. return false;
  10096. if (IS_CHERRYVIEW(dev))
  10097. return false;
  10098. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10099. return false;
  10100. return true;
  10101. }
  10102. static void intel_setup_outputs(struct drm_device *dev)
  10103. {
  10104. struct drm_i915_private *dev_priv = dev->dev_private;
  10105. struct intel_encoder *encoder;
  10106. bool dpd_is_edp = false;
  10107. intel_lvds_init(dev);
  10108. if (intel_crt_present(dev))
  10109. intel_crt_init(dev);
  10110. if (HAS_DDI(dev)) {
  10111. int found;
  10112. /* Haswell uses DDI functions to detect digital outputs */
  10113. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10114. /* DDI A only supports eDP */
  10115. if (found)
  10116. intel_ddi_init(dev, PORT_A);
  10117. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10118. * register */
  10119. found = I915_READ(SFUSE_STRAP);
  10120. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10121. intel_ddi_init(dev, PORT_B);
  10122. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10123. intel_ddi_init(dev, PORT_C);
  10124. if (found & SFUSE_STRAP_DDID_DETECTED)
  10125. intel_ddi_init(dev, PORT_D);
  10126. } else if (HAS_PCH_SPLIT(dev)) {
  10127. int found;
  10128. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10129. if (has_edp_a(dev))
  10130. intel_dp_init(dev, DP_A, PORT_A);
  10131. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10132. /* PCH SDVOB multiplex with HDMIB */
  10133. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10134. if (!found)
  10135. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10136. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10137. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10138. }
  10139. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10140. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10141. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10142. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10143. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10144. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10145. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10146. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10147. } else if (IS_VALLEYVIEW(dev)) {
  10148. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
  10149. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10150. PORT_B);
  10151. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
  10152. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10153. }
  10154. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
  10155. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10156. PORT_C);
  10157. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
  10158. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10159. }
  10160. if (IS_CHERRYVIEW(dev)) {
  10161. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
  10162. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10163. PORT_D);
  10164. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10165. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10166. }
  10167. }
  10168. intel_dsi_init(dev);
  10169. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10170. bool found = false;
  10171. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10172. DRM_DEBUG_KMS("probing SDVOB\n");
  10173. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10174. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10175. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10176. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10177. }
  10178. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10179. intel_dp_init(dev, DP_B, PORT_B);
  10180. }
  10181. /* Before G4X SDVOC doesn't have its own detect register */
  10182. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10183. DRM_DEBUG_KMS("probing SDVOC\n");
  10184. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10185. }
  10186. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10187. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10188. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10189. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10190. }
  10191. if (SUPPORTS_INTEGRATED_DP(dev))
  10192. intel_dp_init(dev, DP_C, PORT_C);
  10193. }
  10194. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10195. (I915_READ(DP_D) & DP_DETECTED))
  10196. intel_dp_init(dev, DP_D, PORT_D);
  10197. } else if (IS_GEN2(dev))
  10198. intel_dvo_init(dev);
  10199. if (SUPPORTS_TV(dev))
  10200. intel_tv_init(dev);
  10201. intel_edp_psr_init(dev);
  10202. for_each_intel_encoder(dev, encoder) {
  10203. encoder->base.possible_crtcs = encoder->crtc_mask;
  10204. encoder->base.possible_clones =
  10205. intel_encoder_clones(encoder);
  10206. }
  10207. intel_init_pch_refclk(dev);
  10208. drm_helper_move_panel_connectors_to_head(dev);
  10209. }
  10210. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10211. {
  10212. struct drm_device *dev = fb->dev;
  10213. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10214. drm_framebuffer_cleanup(fb);
  10215. mutex_lock(&dev->struct_mutex);
  10216. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10217. drm_gem_object_unreference(&intel_fb->obj->base);
  10218. mutex_unlock(&dev->struct_mutex);
  10219. kfree(intel_fb);
  10220. }
  10221. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10222. struct drm_file *file,
  10223. unsigned int *handle)
  10224. {
  10225. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10226. struct drm_i915_gem_object *obj = intel_fb->obj;
  10227. return drm_gem_handle_create(file, &obj->base, handle);
  10228. }
  10229. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10230. .destroy = intel_user_framebuffer_destroy,
  10231. .create_handle = intel_user_framebuffer_create_handle,
  10232. };
  10233. static int intel_framebuffer_init(struct drm_device *dev,
  10234. struct intel_framebuffer *intel_fb,
  10235. struct drm_mode_fb_cmd2 *mode_cmd,
  10236. struct drm_i915_gem_object *obj)
  10237. {
  10238. int aligned_height;
  10239. int pitch_limit;
  10240. int ret;
  10241. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10242. if (obj->tiling_mode == I915_TILING_Y) {
  10243. DRM_DEBUG("hardware does not support tiling Y\n");
  10244. return -EINVAL;
  10245. }
  10246. if (mode_cmd->pitches[0] & 63) {
  10247. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  10248. mode_cmd->pitches[0]);
  10249. return -EINVAL;
  10250. }
  10251. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10252. pitch_limit = 32*1024;
  10253. } else if (INTEL_INFO(dev)->gen >= 4) {
  10254. if (obj->tiling_mode)
  10255. pitch_limit = 16*1024;
  10256. else
  10257. pitch_limit = 32*1024;
  10258. } else if (INTEL_INFO(dev)->gen >= 3) {
  10259. if (obj->tiling_mode)
  10260. pitch_limit = 8*1024;
  10261. else
  10262. pitch_limit = 16*1024;
  10263. } else
  10264. /* XXX DSPC is limited to 4k tiled */
  10265. pitch_limit = 8*1024;
  10266. if (mode_cmd->pitches[0] > pitch_limit) {
  10267. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  10268. obj->tiling_mode ? "tiled" : "linear",
  10269. mode_cmd->pitches[0], pitch_limit);
  10270. return -EINVAL;
  10271. }
  10272. if (obj->tiling_mode != I915_TILING_NONE &&
  10273. mode_cmd->pitches[0] != obj->stride) {
  10274. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10275. mode_cmd->pitches[0], obj->stride);
  10276. return -EINVAL;
  10277. }
  10278. /* Reject formats not supported by any plane early. */
  10279. switch (mode_cmd->pixel_format) {
  10280. case DRM_FORMAT_C8:
  10281. case DRM_FORMAT_RGB565:
  10282. case DRM_FORMAT_XRGB8888:
  10283. case DRM_FORMAT_ARGB8888:
  10284. break;
  10285. case DRM_FORMAT_XRGB1555:
  10286. case DRM_FORMAT_ARGB1555:
  10287. if (INTEL_INFO(dev)->gen > 3) {
  10288. DRM_DEBUG("unsupported pixel format: %s\n",
  10289. drm_get_format_name(mode_cmd->pixel_format));
  10290. return -EINVAL;
  10291. }
  10292. break;
  10293. case DRM_FORMAT_XBGR8888:
  10294. case DRM_FORMAT_ABGR8888:
  10295. case DRM_FORMAT_XRGB2101010:
  10296. case DRM_FORMAT_ARGB2101010:
  10297. case DRM_FORMAT_XBGR2101010:
  10298. case DRM_FORMAT_ABGR2101010:
  10299. if (INTEL_INFO(dev)->gen < 4) {
  10300. DRM_DEBUG("unsupported pixel format: %s\n",
  10301. drm_get_format_name(mode_cmd->pixel_format));
  10302. return -EINVAL;
  10303. }
  10304. break;
  10305. case DRM_FORMAT_YUYV:
  10306. case DRM_FORMAT_UYVY:
  10307. case DRM_FORMAT_YVYU:
  10308. case DRM_FORMAT_VYUY:
  10309. if (INTEL_INFO(dev)->gen < 5) {
  10310. DRM_DEBUG("unsupported pixel format: %s\n",
  10311. drm_get_format_name(mode_cmd->pixel_format));
  10312. return -EINVAL;
  10313. }
  10314. break;
  10315. default:
  10316. DRM_DEBUG("unsupported pixel format: %s\n",
  10317. drm_get_format_name(mode_cmd->pixel_format));
  10318. return -EINVAL;
  10319. }
  10320. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10321. if (mode_cmd->offsets[0] != 0)
  10322. return -EINVAL;
  10323. aligned_height = intel_align_height(dev, mode_cmd->height,
  10324. obj->tiling_mode);
  10325. /* FIXME drm helper for size checks (especially planar formats)? */
  10326. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10327. return -EINVAL;
  10328. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10329. intel_fb->obj = obj;
  10330. intel_fb->obj->framebuffer_references++;
  10331. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10332. if (ret) {
  10333. DRM_ERROR("framebuffer init failed %d\n", ret);
  10334. return ret;
  10335. }
  10336. return 0;
  10337. }
  10338. static struct drm_framebuffer *
  10339. intel_user_framebuffer_create(struct drm_device *dev,
  10340. struct drm_file *filp,
  10341. struct drm_mode_fb_cmd2 *mode_cmd)
  10342. {
  10343. struct drm_i915_gem_object *obj;
  10344. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10345. mode_cmd->handles[0]));
  10346. if (&obj->base == NULL)
  10347. return ERR_PTR(-ENOENT);
  10348. return intel_framebuffer_create(dev, mode_cmd, obj);
  10349. }
  10350. #ifndef CONFIG_DRM_I915_FBDEV
  10351. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10352. {
  10353. }
  10354. #endif
  10355. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10356. .fb_create = intel_user_framebuffer_create,
  10357. .output_poll_changed = intel_fbdev_output_poll_changed,
  10358. };
  10359. /* Set up chip specific display functions */
  10360. static void intel_init_display(struct drm_device *dev)
  10361. {
  10362. struct drm_i915_private *dev_priv = dev->dev_private;
  10363. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10364. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10365. else if (IS_CHERRYVIEW(dev))
  10366. dev_priv->display.find_dpll = chv_find_best_dpll;
  10367. else if (IS_VALLEYVIEW(dev))
  10368. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10369. else if (IS_PINEVIEW(dev))
  10370. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10371. else
  10372. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10373. if (HAS_DDI(dev)) {
  10374. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10375. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10376. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  10377. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10378. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10379. dev_priv->display.off = ironlake_crtc_off;
  10380. dev_priv->display.update_primary_plane =
  10381. ironlake_update_primary_plane;
  10382. } else if (HAS_PCH_SPLIT(dev)) {
  10383. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10384. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10385. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  10386. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10387. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  10388. dev_priv->display.off = ironlake_crtc_off;
  10389. dev_priv->display.update_primary_plane =
  10390. ironlake_update_primary_plane;
  10391. } else if (IS_VALLEYVIEW(dev)) {
  10392. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10393. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10394. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  10395. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10396. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10397. dev_priv->display.off = i9xx_crtc_off;
  10398. dev_priv->display.update_primary_plane =
  10399. i9xx_update_primary_plane;
  10400. } else {
  10401. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10402. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10403. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  10404. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  10405. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10406. dev_priv->display.off = i9xx_crtc_off;
  10407. dev_priv->display.update_primary_plane =
  10408. i9xx_update_primary_plane;
  10409. }
  10410. /* Returns the core display clock speed */
  10411. if (IS_VALLEYVIEW(dev))
  10412. dev_priv->display.get_display_clock_speed =
  10413. valleyview_get_display_clock_speed;
  10414. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10415. dev_priv->display.get_display_clock_speed =
  10416. i945_get_display_clock_speed;
  10417. else if (IS_I915G(dev))
  10418. dev_priv->display.get_display_clock_speed =
  10419. i915_get_display_clock_speed;
  10420. else if (IS_I945GM(dev) || IS_845G(dev))
  10421. dev_priv->display.get_display_clock_speed =
  10422. i9xx_misc_get_display_clock_speed;
  10423. else if (IS_PINEVIEW(dev))
  10424. dev_priv->display.get_display_clock_speed =
  10425. pnv_get_display_clock_speed;
  10426. else if (IS_I915GM(dev))
  10427. dev_priv->display.get_display_clock_speed =
  10428. i915gm_get_display_clock_speed;
  10429. else if (IS_I865G(dev))
  10430. dev_priv->display.get_display_clock_speed =
  10431. i865_get_display_clock_speed;
  10432. else if (IS_I85X(dev))
  10433. dev_priv->display.get_display_clock_speed =
  10434. i855_get_display_clock_speed;
  10435. else /* 852, 830 */
  10436. dev_priv->display.get_display_clock_speed =
  10437. i830_get_display_clock_speed;
  10438. if (IS_G4X(dev)) {
  10439. dev_priv->display.write_eld = g4x_write_eld;
  10440. } else if (IS_GEN5(dev)) {
  10441. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10442. dev_priv->display.write_eld = ironlake_write_eld;
  10443. } else if (IS_GEN6(dev)) {
  10444. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10445. dev_priv->display.write_eld = ironlake_write_eld;
  10446. dev_priv->display.modeset_global_resources =
  10447. snb_modeset_global_resources;
  10448. } else if (IS_IVYBRIDGE(dev)) {
  10449. /* FIXME: detect B0+ stepping and use auto training */
  10450. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10451. dev_priv->display.write_eld = ironlake_write_eld;
  10452. dev_priv->display.modeset_global_resources =
  10453. ivb_modeset_global_resources;
  10454. } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
  10455. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10456. dev_priv->display.write_eld = haswell_write_eld;
  10457. dev_priv->display.modeset_global_resources =
  10458. haswell_modeset_global_resources;
  10459. } else if (IS_VALLEYVIEW(dev)) {
  10460. dev_priv->display.modeset_global_resources =
  10461. valleyview_modeset_global_resources;
  10462. dev_priv->display.write_eld = ironlake_write_eld;
  10463. }
  10464. /* Default just returns -ENODEV to indicate unsupported */
  10465. dev_priv->display.queue_flip = intel_default_queue_flip;
  10466. switch (INTEL_INFO(dev)->gen) {
  10467. case 2:
  10468. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10469. break;
  10470. case 3:
  10471. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  10472. break;
  10473. case 4:
  10474. case 5:
  10475. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  10476. break;
  10477. case 6:
  10478. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  10479. break;
  10480. case 7:
  10481. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  10482. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  10483. break;
  10484. }
  10485. intel_panel_init_backlight_funcs(dev);
  10486. }
  10487. /*
  10488. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  10489. * resume, or other times. This quirk makes sure that's the case for
  10490. * affected systems.
  10491. */
  10492. static void quirk_pipea_force(struct drm_device *dev)
  10493. {
  10494. struct drm_i915_private *dev_priv = dev->dev_private;
  10495. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  10496. DRM_INFO("applying pipe a force quirk\n");
  10497. }
  10498. /*
  10499. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  10500. */
  10501. static void quirk_ssc_force_disable(struct drm_device *dev)
  10502. {
  10503. struct drm_i915_private *dev_priv = dev->dev_private;
  10504. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  10505. DRM_INFO("applying lvds SSC disable quirk\n");
  10506. }
  10507. /*
  10508. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  10509. * brightness value
  10510. */
  10511. static void quirk_invert_brightness(struct drm_device *dev)
  10512. {
  10513. struct drm_i915_private *dev_priv = dev->dev_private;
  10514. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  10515. DRM_INFO("applying inverted panel brightness quirk\n");
  10516. }
  10517. /* Some VBT's incorrectly indicate no backlight is present */
  10518. static void quirk_backlight_present(struct drm_device *dev)
  10519. {
  10520. struct drm_i915_private *dev_priv = dev->dev_private;
  10521. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  10522. DRM_INFO("applying backlight present quirk\n");
  10523. }
  10524. struct intel_quirk {
  10525. int device;
  10526. int subsystem_vendor;
  10527. int subsystem_device;
  10528. void (*hook)(struct drm_device *dev);
  10529. };
  10530. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  10531. struct intel_dmi_quirk {
  10532. void (*hook)(struct drm_device *dev);
  10533. const struct dmi_system_id (*dmi_id_list)[];
  10534. };
  10535. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  10536. {
  10537. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  10538. return 1;
  10539. }
  10540. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  10541. {
  10542. .dmi_id_list = &(const struct dmi_system_id[]) {
  10543. {
  10544. .callback = intel_dmi_reverse_brightness,
  10545. .ident = "NCR Corporation",
  10546. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  10547. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  10548. },
  10549. },
  10550. { } /* terminating entry */
  10551. },
  10552. .hook = quirk_invert_brightness,
  10553. },
  10554. };
  10555. static struct intel_quirk intel_quirks[] = {
  10556. /* HP Mini needs pipe A force quirk (LP: #322104) */
  10557. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  10558. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  10559. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  10560. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  10561. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  10562. /* Lenovo U160 cannot use SSC on LVDS */
  10563. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  10564. /* Sony Vaio Y cannot use SSC on LVDS */
  10565. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  10566. /* Acer Aspire 5734Z must invert backlight brightness */
  10567. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  10568. /* Acer/eMachines G725 */
  10569. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  10570. /* Acer/eMachines e725 */
  10571. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  10572. /* Acer/Packard Bell NCL20 */
  10573. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  10574. /* Acer Aspire 4736Z */
  10575. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  10576. /* Acer Aspire 5336 */
  10577. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  10578. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  10579. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  10580. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  10581. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  10582. /* HP Chromebook 14 (Celeron 2955U) */
  10583. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  10584. };
  10585. static void intel_init_quirks(struct drm_device *dev)
  10586. {
  10587. struct pci_dev *d = dev->pdev;
  10588. int i;
  10589. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  10590. struct intel_quirk *q = &intel_quirks[i];
  10591. if (d->device == q->device &&
  10592. (d->subsystem_vendor == q->subsystem_vendor ||
  10593. q->subsystem_vendor == PCI_ANY_ID) &&
  10594. (d->subsystem_device == q->subsystem_device ||
  10595. q->subsystem_device == PCI_ANY_ID))
  10596. q->hook(dev);
  10597. }
  10598. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  10599. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  10600. intel_dmi_quirks[i].hook(dev);
  10601. }
  10602. }
  10603. /* Disable the VGA plane that we never use */
  10604. static void i915_disable_vga(struct drm_device *dev)
  10605. {
  10606. struct drm_i915_private *dev_priv = dev->dev_private;
  10607. u8 sr1;
  10608. u32 vga_reg = i915_vgacntrl_reg(dev);
  10609. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  10610. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  10611. outb(SR01, VGA_SR_INDEX);
  10612. sr1 = inb(VGA_SR_DATA);
  10613. outb(sr1 | 1<<5, VGA_SR_DATA);
  10614. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  10615. udelay(300);
  10616. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  10617. POSTING_READ(vga_reg);
  10618. }
  10619. void intel_modeset_init_hw(struct drm_device *dev)
  10620. {
  10621. intel_prepare_ddi(dev);
  10622. if (IS_VALLEYVIEW(dev))
  10623. vlv_update_cdclk(dev);
  10624. intel_init_clock_gating(dev);
  10625. intel_enable_gt_powersave(dev);
  10626. }
  10627. void intel_modeset_suspend_hw(struct drm_device *dev)
  10628. {
  10629. intel_suspend_hw(dev);
  10630. }
  10631. void intel_modeset_init(struct drm_device *dev)
  10632. {
  10633. struct drm_i915_private *dev_priv = dev->dev_private;
  10634. int sprite, ret;
  10635. enum pipe pipe;
  10636. struct intel_crtc *crtc;
  10637. drm_mode_config_init(dev);
  10638. dev->mode_config.min_width = 0;
  10639. dev->mode_config.min_height = 0;
  10640. dev->mode_config.preferred_depth = 24;
  10641. dev->mode_config.prefer_shadow = 1;
  10642. dev->mode_config.funcs = &intel_mode_funcs;
  10643. intel_init_quirks(dev);
  10644. intel_init_pm(dev);
  10645. if (INTEL_INFO(dev)->num_pipes == 0)
  10646. return;
  10647. intel_init_display(dev);
  10648. if (IS_GEN2(dev)) {
  10649. dev->mode_config.max_width = 2048;
  10650. dev->mode_config.max_height = 2048;
  10651. } else if (IS_GEN3(dev)) {
  10652. dev->mode_config.max_width = 4096;
  10653. dev->mode_config.max_height = 4096;
  10654. } else {
  10655. dev->mode_config.max_width = 8192;
  10656. dev->mode_config.max_height = 8192;
  10657. }
  10658. if (IS_845G(dev) || IS_I865G(dev)) {
  10659. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  10660. dev->mode_config.cursor_height = 1023;
  10661. } else if (IS_GEN2(dev)) {
  10662. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  10663. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  10664. } else {
  10665. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  10666. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  10667. }
  10668. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  10669. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  10670. INTEL_INFO(dev)->num_pipes,
  10671. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  10672. for_each_pipe(dev_priv, pipe) {
  10673. intel_crtc_init(dev, pipe);
  10674. for_each_sprite(pipe, sprite) {
  10675. ret = intel_plane_init(dev, pipe, sprite);
  10676. if (ret)
  10677. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  10678. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  10679. }
  10680. }
  10681. intel_init_dpio(dev);
  10682. intel_shared_dpll_init(dev);
  10683. /* Just disable it once at startup */
  10684. i915_disable_vga(dev);
  10685. intel_setup_outputs(dev);
  10686. /* Just in case the BIOS is doing something questionable. */
  10687. intel_disable_fbc(dev);
  10688. drm_modeset_lock_all(dev);
  10689. intel_modeset_setup_hw_state(dev, false);
  10690. drm_modeset_unlock_all(dev);
  10691. for_each_intel_crtc(dev, crtc) {
  10692. if (!crtc->active)
  10693. continue;
  10694. /*
  10695. * Note that reserving the BIOS fb up front prevents us
  10696. * from stuffing other stolen allocations like the ring
  10697. * on top. This prevents some ugliness at boot time, and
  10698. * can even allow for smooth boot transitions if the BIOS
  10699. * fb is large enough for the active pipe configuration.
  10700. */
  10701. if (dev_priv->display.get_plane_config) {
  10702. dev_priv->display.get_plane_config(crtc,
  10703. &crtc->plane_config);
  10704. /*
  10705. * If the fb is shared between multiple heads, we'll
  10706. * just get the first one.
  10707. */
  10708. intel_find_plane_obj(crtc, &crtc->plane_config);
  10709. }
  10710. }
  10711. }
  10712. static void intel_enable_pipe_a(struct drm_device *dev)
  10713. {
  10714. struct intel_connector *connector;
  10715. struct drm_connector *crt = NULL;
  10716. struct intel_load_detect_pipe load_detect_temp;
  10717. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  10718. /* We can't just switch on the pipe A, we need to set things up with a
  10719. * proper mode and output configuration. As a gross hack, enable pipe A
  10720. * by enabling the load detect pipe once. */
  10721. list_for_each_entry(connector,
  10722. &dev->mode_config.connector_list,
  10723. base.head) {
  10724. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  10725. crt = &connector->base;
  10726. break;
  10727. }
  10728. }
  10729. if (!crt)
  10730. return;
  10731. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  10732. intel_release_load_detect_pipe(crt, &load_detect_temp);
  10733. }
  10734. static bool
  10735. intel_check_plane_mapping(struct intel_crtc *crtc)
  10736. {
  10737. struct drm_device *dev = crtc->base.dev;
  10738. struct drm_i915_private *dev_priv = dev->dev_private;
  10739. u32 reg, val;
  10740. if (INTEL_INFO(dev)->num_pipes == 1)
  10741. return true;
  10742. reg = DSPCNTR(!crtc->plane);
  10743. val = I915_READ(reg);
  10744. if ((val & DISPLAY_PLANE_ENABLE) &&
  10745. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  10746. return false;
  10747. return true;
  10748. }
  10749. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  10750. {
  10751. struct drm_device *dev = crtc->base.dev;
  10752. struct drm_i915_private *dev_priv = dev->dev_private;
  10753. u32 reg;
  10754. /* Clear any frame start delays used for debugging left by the BIOS */
  10755. reg = PIPECONF(crtc->config.cpu_transcoder);
  10756. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  10757. /* restore vblank interrupts to correct state */
  10758. if (crtc->active)
  10759. drm_vblank_on(dev, crtc->pipe);
  10760. else
  10761. drm_vblank_off(dev, crtc->pipe);
  10762. /* We need to sanitize the plane -> pipe mapping first because this will
  10763. * disable the crtc (and hence change the state) if it is wrong. Note
  10764. * that gen4+ has a fixed plane -> pipe mapping. */
  10765. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  10766. struct intel_connector *connector;
  10767. bool plane;
  10768. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  10769. crtc->base.base.id);
  10770. /* Pipe has the wrong plane attached and the plane is active.
  10771. * Temporarily change the plane mapping and disable everything
  10772. * ... */
  10773. plane = crtc->plane;
  10774. crtc->plane = !plane;
  10775. crtc->primary_enabled = true;
  10776. dev_priv->display.crtc_disable(&crtc->base);
  10777. crtc->plane = plane;
  10778. /* ... and break all links. */
  10779. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10780. base.head) {
  10781. if (connector->encoder->base.crtc != &crtc->base)
  10782. continue;
  10783. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10784. connector->base.encoder = NULL;
  10785. }
  10786. /* multiple connectors may have the same encoder:
  10787. * handle them and break crtc link separately */
  10788. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10789. base.head)
  10790. if (connector->encoder->base.crtc == &crtc->base) {
  10791. connector->encoder->base.crtc = NULL;
  10792. connector->encoder->connectors_active = false;
  10793. }
  10794. WARN_ON(crtc->active);
  10795. crtc->base.enabled = false;
  10796. }
  10797. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  10798. crtc->pipe == PIPE_A && !crtc->active) {
  10799. /* BIOS forgot to enable pipe A, this mostly happens after
  10800. * resume. Force-enable the pipe to fix this, the update_dpms
  10801. * call below we restore the pipe to the right state, but leave
  10802. * the required bits on. */
  10803. intel_enable_pipe_a(dev);
  10804. }
  10805. /* Adjust the state of the output pipe according to whether we
  10806. * have active connectors/encoders. */
  10807. intel_crtc_update_dpms(&crtc->base);
  10808. if (crtc->active != crtc->base.enabled) {
  10809. struct intel_encoder *encoder;
  10810. /* This can happen either due to bugs in the get_hw_state
  10811. * functions or because the pipe is force-enabled due to the
  10812. * pipe A quirk. */
  10813. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  10814. crtc->base.base.id,
  10815. crtc->base.enabled ? "enabled" : "disabled",
  10816. crtc->active ? "enabled" : "disabled");
  10817. crtc->base.enabled = crtc->active;
  10818. /* Because we only establish the connector -> encoder ->
  10819. * crtc links if something is active, this means the
  10820. * crtc is now deactivated. Break the links. connector
  10821. * -> encoder links are only establish when things are
  10822. * actually up, hence no need to break them. */
  10823. WARN_ON(crtc->active);
  10824. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  10825. WARN_ON(encoder->connectors_active);
  10826. encoder->base.crtc = NULL;
  10827. }
  10828. }
  10829. if (crtc->active || IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen < 5) {
  10830. /*
  10831. * We start out with underrun reporting disabled to avoid races.
  10832. * For correct bookkeeping mark this on active crtcs.
  10833. *
  10834. * Also on gmch platforms we dont have any hardware bits to
  10835. * disable the underrun reporting. Which means we need to start
  10836. * out with underrun reporting disabled also on inactive pipes,
  10837. * since otherwise we'll complain about the garbage we read when
  10838. * e.g. coming up after runtime pm.
  10839. *
  10840. * No protection against concurrent access is required - at
  10841. * worst a fifo underrun happens which also sets this to false.
  10842. */
  10843. crtc->cpu_fifo_underrun_disabled = true;
  10844. crtc->pch_fifo_underrun_disabled = true;
  10845. update_scanline_offset(crtc);
  10846. }
  10847. }
  10848. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  10849. {
  10850. struct intel_connector *connector;
  10851. struct drm_device *dev = encoder->base.dev;
  10852. /* We need to check both for a crtc link (meaning that the
  10853. * encoder is active and trying to read from a pipe) and the
  10854. * pipe itself being active. */
  10855. bool has_active_crtc = encoder->base.crtc &&
  10856. to_intel_crtc(encoder->base.crtc)->active;
  10857. if (encoder->connectors_active && !has_active_crtc) {
  10858. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  10859. encoder->base.base.id,
  10860. encoder->base.name);
  10861. /* Connector is active, but has no active pipe. This is
  10862. * fallout from our resume register restoring. Disable
  10863. * the encoder manually again. */
  10864. if (encoder->base.crtc) {
  10865. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  10866. encoder->base.base.id,
  10867. encoder->base.name);
  10868. encoder->disable(encoder);
  10869. if (encoder->post_disable)
  10870. encoder->post_disable(encoder);
  10871. }
  10872. encoder->base.crtc = NULL;
  10873. encoder->connectors_active = false;
  10874. /* Inconsistent output/port/pipe state happens presumably due to
  10875. * a bug in one of the get_hw_state functions. Or someplace else
  10876. * in our code, like the register restore mess on resume. Clamp
  10877. * things to off as a safer default. */
  10878. list_for_each_entry(connector,
  10879. &dev->mode_config.connector_list,
  10880. base.head) {
  10881. if (connector->encoder != encoder)
  10882. continue;
  10883. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10884. connector->base.encoder = NULL;
  10885. }
  10886. }
  10887. /* Enabled encoders without active connectors will be fixed in
  10888. * the crtc fixup. */
  10889. }
  10890. void i915_redisable_vga_power_on(struct drm_device *dev)
  10891. {
  10892. struct drm_i915_private *dev_priv = dev->dev_private;
  10893. u32 vga_reg = i915_vgacntrl_reg(dev);
  10894. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  10895. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  10896. i915_disable_vga(dev);
  10897. }
  10898. }
  10899. void i915_redisable_vga(struct drm_device *dev)
  10900. {
  10901. struct drm_i915_private *dev_priv = dev->dev_private;
  10902. /* This function can be called both from intel_modeset_setup_hw_state or
  10903. * at a very early point in our resume sequence, where the power well
  10904. * structures are not yet restored. Since this function is at a very
  10905. * paranoid "someone might have enabled VGA while we were not looking"
  10906. * level, just check if the power well is enabled instead of trying to
  10907. * follow the "don't touch the power well if we don't need it" policy
  10908. * the rest of the driver uses. */
  10909. if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
  10910. return;
  10911. i915_redisable_vga_power_on(dev);
  10912. }
  10913. static bool primary_get_hw_state(struct intel_crtc *crtc)
  10914. {
  10915. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  10916. if (!crtc->active)
  10917. return false;
  10918. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  10919. }
  10920. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  10921. {
  10922. struct drm_i915_private *dev_priv = dev->dev_private;
  10923. enum pipe pipe;
  10924. struct intel_crtc *crtc;
  10925. struct intel_encoder *encoder;
  10926. struct intel_connector *connector;
  10927. int i;
  10928. for_each_intel_crtc(dev, crtc) {
  10929. memset(&crtc->config, 0, sizeof(crtc->config));
  10930. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  10931. crtc->active = dev_priv->display.get_pipe_config(crtc,
  10932. &crtc->config);
  10933. crtc->base.enabled = crtc->active;
  10934. crtc->primary_enabled = primary_get_hw_state(crtc);
  10935. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  10936. crtc->base.base.id,
  10937. crtc->active ? "enabled" : "disabled");
  10938. }
  10939. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10940. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10941. pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
  10942. pll->active = 0;
  10943. for_each_intel_crtc(dev, crtc) {
  10944. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10945. pll->active++;
  10946. }
  10947. pll->refcount = pll->active;
  10948. DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
  10949. pll->name, pll->refcount, pll->on);
  10950. if (pll->refcount)
  10951. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  10952. }
  10953. for_each_intel_encoder(dev, encoder) {
  10954. pipe = 0;
  10955. if (encoder->get_hw_state(encoder, &pipe)) {
  10956. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10957. encoder->base.crtc = &crtc->base;
  10958. encoder->get_config(encoder, &crtc->config);
  10959. } else {
  10960. encoder->base.crtc = NULL;
  10961. }
  10962. encoder->connectors_active = false;
  10963. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  10964. encoder->base.base.id,
  10965. encoder->base.name,
  10966. encoder->base.crtc ? "enabled" : "disabled",
  10967. pipe_name(pipe));
  10968. }
  10969. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10970. base.head) {
  10971. if (connector->get_hw_state(connector)) {
  10972. connector->base.dpms = DRM_MODE_DPMS_ON;
  10973. connector->encoder->connectors_active = true;
  10974. connector->base.encoder = &connector->encoder->base;
  10975. } else {
  10976. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10977. connector->base.encoder = NULL;
  10978. }
  10979. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  10980. connector->base.base.id,
  10981. connector->base.name,
  10982. connector->base.encoder ? "enabled" : "disabled");
  10983. }
  10984. }
  10985. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  10986. * and i915 state tracking structures. */
  10987. void intel_modeset_setup_hw_state(struct drm_device *dev,
  10988. bool force_restore)
  10989. {
  10990. struct drm_i915_private *dev_priv = dev->dev_private;
  10991. enum pipe pipe;
  10992. struct intel_crtc *crtc;
  10993. struct intel_encoder *encoder;
  10994. int i;
  10995. intel_modeset_readout_hw_state(dev);
  10996. /*
  10997. * Now that we have the config, copy it to each CRTC struct
  10998. * Note that this could go away if we move to using crtc_config
  10999. * checking everywhere.
  11000. */
  11001. for_each_intel_crtc(dev, crtc) {
  11002. if (crtc->active && i915.fastboot) {
  11003. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  11004. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11005. crtc->base.base.id);
  11006. drm_mode_debug_printmodeline(&crtc->base.mode);
  11007. }
  11008. }
  11009. /* HW state is read out, now we need to sanitize this mess. */
  11010. for_each_intel_encoder(dev, encoder) {
  11011. intel_sanitize_encoder(encoder);
  11012. }
  11013. for_each_pipe(dev_priv, pipe) {
  11014. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11015. intel_sanitize_crtc(crtc);
  11016. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  11017. }
  11018. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11019. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11020. if (!pll->on || pll->active)
  11021. continue;
  11022. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11023. pll->disable(dev_priv, pll);
  11024. pll->on = false;
  11025. }
  11026. if (HAS_PCH_SPLIT(dev))
  11027. ilk_wm_get_hw_state(dev);
  11028. if (force_restore) {
  11029. i915_redisable_vga(dev);
  11030. /*
  11031. * We need to use raw interfaces for restoring state to avoid
  11032. * checking (bogus) intermediate states.
  11033. */
  11034. for_each_pipe(dev_priv, pipe) {
  11035. struct drm_crtc *crtc =
  11036. dev_priv->pipe_to_crtc_mapping[pipe];
  11037. __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  11038. crtc->primary->fb);
  11039. }
  11040. } else {
  11041. intel_modeset_update_staged_output_state(dev);
  11042. }
  11043. intel_modeset_check_state(dev);
  11044. }
  11045. void intel_modeset_gem_init(struct drm_device *dev)
  11046. {
  11047. struct drm_crtc *c;
  11048. struct drm_i915_gem_object *obj;
  11049. mutex_lock(&dev->struct_mutex);
  11050. intel_init_gt_powersave(dev);
  11051. mutex_unlock(&dev->struct_mutex);
  11052. intel_modeset_init_hw(dev);
  11053. intel_setup_overlay(dev);
  11054. /*
  11055. * Make sure any fbs we allocated at startup are properly
  11056. * pinned & fenced. When we do the allocation it's too early
  11057. * for this.
  11058. */
  11059. mutex_lock(&dev->struct_mutex);
  11060. for_each_crtc(dev, c) {
  11061. obj = intel_fb_obj(c->primary->fb);
  11062. if (obj == NULL)
  11063. continue;
  11064. if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
  11065. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11066. to_intel_crtc(c)->pipe);
  11067. drm_framebuffer_unreference(c->primary->fb);
  11068. c->primary->fb = NULL;
  11069. }
  11070. }
  11071. mutex_unlock(&dev->struct_mutex);
  11072. }
  11073. void intel_connector_unregister(struct intel_connector *intel_connector)
  11074. {
  11075. struct drm_connector *connector = &intel_connector->base;
  11076. intel_panel_destroy_backlight(connector);
  11077. drm_connector_unregister(connector);
  11078. }
  11079. void intel_modeset_cleanup(struct drm_device *dev)
  11080. {
  11081. struct drm_i915_private *dev_priv = dev->dev_private;
  11082. struct drm_connector *connector;
  11083. /*
  11084. * Interrupts and polling as the first thing to avoid creating havoc.
  11085. * Too much stuff here (turning of rps, connectors, ...) would
  11086. * experience fancy races otherwise.
  11087. */
  11088. drm_irq_uninstall(dev);
  11089. intel_hpd_cancel_work(dev_priv);
  11090. dev_priv->pm._irqs_disabled = true;
  11091. /*
  11092. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11093. * poll handlers. Hence disable polling after hpd handling is shut down.
  11094. */
  11095. drm_kms_helper_poll_fini(dev);
  11096. mutex_lock(&dev->struct_mutex);
  11097. intel_unregister_dsm_handler();
  11098. intel_disable_fbc(dev);
  11099. intel_disable_gt_powersave(dev);
  11100. ironlake_teardown_rc6(dev);
  11101. mutex_unlock(&dev->struct_mutex);
  11102. /* flush any delayed tasks or pending work */
  11103. flush_scheduled_work();
  11104. /* destroy the backlight and sysfs files before encoders/connectors */
  11105. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11106. struct intel_connector *intel_connector;
  11107. intel_connector = to_intel_connector(connector);
  11108. intel_connector->unregister(intel_connector);
  11109. }
  11110. drm_mode_config_cleanup(dev);
  11111. intel_cleanup_overlay(dev);
  11112. mutex_lock(&dev->struct_mutex);
  11113. intel_cleanup_gt_powersave(dev);
  11114. mutex_unlock(&dev->struct_mutex);
  11115. }
  11116. /*
  11117. * Return which encoder is currently attached for connector.
  11118. */
  11119. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11120. {
  11121. return &intel_attached_encoder(connector)->base;
  11122. }
  11123. void intel_connector_attach_encoder(struct intel_connector *connector,
  11124. struct intel_encoder *encoder)
  11125. {
  11126. connector->encoder = encoder;
  11127. drm_mode_connector_attach_encoder(&connector->base,
  11128. &encoder->base);
  11129. }
  11130. /*
  11131. * set vga decode state - true == enable VGA decode
  11132. */
  11133. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11134. {
  11135. struct drm_i915_private *dev_priv = dev->dev_private;
  11136. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11137. u16 gmch_ctrl;
  11138. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11139. DRM_ERROR("failed to read control word\n");
  11140. return -EIO;
  11141. }
  11142. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11143. return 0;
  11144. if (state)
  11145. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11146. else
  11147. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11148. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11149. DRM_ERROR("failed to write control word\n");
  11150. return -EIO;
  11151. }
  11152. return 0;
  11153. }
  11154. struct intel_display_error_state {
  11155. u32 power_well_driver;
  11156. int num_transcoders;
  11157. struct intel_cursor_error_state {
  11158. u32 control;
  11159. u32 position;
  11160. u32 base;
  11161. u32 size;
  11162. } cursor[I915_MAX_PIPES];
  11163. struct intel_pipe_error_state {
  11164. bool power_domain_on;
  11165. u32 source;
  11166. u32 stat;
  11167. } pipe[I915_MAX_PIPES];
  11168. struct intel_plane_error_state {
  11169. u32 control;
  11170. u32 stride;
  11171. u32 size;
  11172. u32 pos;
  11173. u32 addr;
  11174. u32 surface;
  11175. u32 tile_offset;
  11176. } plane[I915_MAX_PIPES];
  11177. struct intel_transcoder_error_state {
  11178. bool power_domain_on;
  11179. enum transcoder cpu_transcoder;
  11180. u32 conf;
  11181. u32 htotal;
  11182. u32 hblank;
  11183. u32 hsync;
  11184. u32 vtotal;
  11185. u32 vblank;
  11186. u32 vsync;
  11187. } transcoder[4];
  11188. };
  11189. struct intel_display_error_state *
  11190. intel_display_capture_error_state(struct drm_device *dev)
  11191. {
  11192. struct drm_i915_private *dev_priv = dev->dev_private;
  11193. struct intel_display_error_state *error;
  11194. int transcoders[] = {
  11195. TRANSCODER_A,
  11196. TRANSCODER_B,
  11197. TRANSCODER_C,
  11198. TRANSCODER_EDP,
  11199. };
  11200. int i;
  11201. if (INTEL_INFO(dev)->num_pipes == 0)
  11202. return NULL;
  11203. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11204. if (error == NULL)
  11205. return NULL;
  11206. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11207. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11208. for_each_pipe(dev_priv, i) {
  11209. error->pipe[i].power_domain_on =
  11210. intel_display_power_enabled_unlocked(dev_priv,
  11211. POWER_DOMAIN_PIPE(i));
  11212. if (!error->pipe[i].power_domain_on)
  11213. continue;
  11214. error->cursor[i].control = I915_READ(CURCNTR(i));
  11215. error->cursor[i].position = I915_READ(CURPOS(i));
  11216. error->cursor[i].base = I915_READ(CURBASE(i));
  11217. error->plane[i].control = I915_READ(DSPCNTR(i));
  11218. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11219. if (INTEL_INFO(dev)->gen <= 3) {
  11220. error->plane[i].size = I915_READ(DSPSIZE(i));
  11221. error->plane[i].pos = I915_READ(DSPPOS(i));
  11222. }
  11223. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11224. error->plane[i].addr = I915_READ(DSPADDR(i));
  11225. if (INTEL_INFO(dev)->gen >= 4) {
  11226. error->plane[i].surface = I915_READ(DSPSURF(i));
  11227. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11228. }
  11229. error->pipe[i].source = I915_READ(PIPESRC(i));
  11230. if (HAS_GMCH_DISPLAY(dev))
  11231. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11232. }
  11233. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11234. if (HAS_DDI(dev_priv->dev))
  11235. error->num_transcoders++; /* Account for eDP. */
  11236. for (i = 0; i < error->num_transcoders; i++) {
  11237. enum transcoder cpu_transcoder = transcoders[i];
  11238. error->transcoder[i].power_domain_on =
  11239. intel_display_power_enabled_unlocked(dev_priv,
  11240. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11241. if (!error->transcoder[i].power_domain_on)
  11242. continue;
  11243. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11244. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11245. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11246. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11247. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11248. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11249. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11250. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11251. }
  11252. return error;
  11253. }
  11254. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11255. void
  11256. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11257. struct drm_device *dev,
  11258. struct intel_display_error_state *error)
  11259. {
  11260. struct drm_i915_private *dev_priv = dev->dev_private;
  11261. int i;
  11262. if (!error)
  11263. return;
  11264. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11265. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11266. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11267. error->power_well_driver);
  11268. for_each_pipe(dev_priv, i) {
  11269. err_printf(m, "Pipe [%d]:\n", i);
  11270. err_printf(m, " Power: %s\n",
  11271. error->pipe[i].power_domain_on ? "on" : "off");
  11272. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11273. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11274. err_printf(m, "Plane [%d]:\n", i);
  11275. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11276. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11277. if (INTEL_INFO(dev)->gen <= 3) {
  11278. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11279. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11280. }
  11281. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11282. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11283. if (INTEL_INFO(dev)->gen >= 4) {
  11284. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11285. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11286. }
  11287. err_printf(m, "Cursor [%d]:\n", i);
  11288. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11289. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11290. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11291. }
  11292. for (i = 0; i < error->num_transcoders; i++) {
  11293. err_printf(m, "CPU transcoder: %c\n",
  11294. transcoder_name(error->transcoder[i].cpu_transcoder));
  11295. err_printf(m, " Power: %s\n",
  11296. error->transcoder[i].power_domain_on ? "on" : "off");
  11297. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11298. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11299. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11300. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11301. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11302. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11303. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11304. }
  11305. }
  11306. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11307. {
  11308. struct intel_crtc *crtc;
  11309. for_each_intel_crtc(dev, crtc) {
  11310. struct intel_unpin_work *work;
  11311. unsigned long irqflags;
  11312. spin_lock_irqsave(&dev->event_lock, irqflags);
  11313. work = crtc->unpin_work;
  11314. if (work && work->event &&
  11315. work->event->base.file_priv == file) {
  11316. kfree(work->event);
  11317. work->event = NULL;
  11318. }
  11319. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  11320. }
  11321. }