intel_display.c 369 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399
  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 intel_dp_set_m_n(struct intel_crtc *crtc);
  86. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  87. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  88. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  89. struct intel_link_m_n *m_n);
  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 struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  95. {
  96. if (!connector->mst_port)
  97. return connector->encoder;
  98. else
  99. return &connector->mst_port->mst_encoders[pipe]->base;
  100. }
  101. typedef struct {
  102. int min, max;
  103. } intel_range_t;
  104. typedef struct {
  105. int dot_limit;
  106. int p2_slow, p2_fast;
  107. } intel_p2_t;
  108. typedef struct intel_limit intel_limit_t;
  109. struct intel_limit {
  110. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  111. intel_p2_t p2;
  112. };
  113. int
  114. intel_pch_rawclk(struct drm_device *dev)
  115. {
  116. struct drm_i915_private *dev_priv = dev->dev_private;
  117. WARN_ON(!HAS_PCH_SPLIT(dev));
  118. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  119. }
  120. static inline u32 /* units of 100MHz */
  121. intel_fdi_link_freq(struct drm_device *dev)
  122. {
  123. if (IS_GEN5(dev)) {
  124. struct drm_i915_private *dev_priv = dev->dev_private;
  125. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  126. } else
  127. return 27;
  128. }
  129. static const intel_limit_t intel_limits_i8xx_dac = {
  130. .dot = { .min = 25000, .max = 350000 },
  131. .vco = { .min = 908000, .max = 1512000 },
  132. .n = { .min = 2, .max = 16 },
  133. .m = { .min = 96, .max = 140 },
  134. .m1 = { .min = 18, .max = 26 },
  135. .m2 = { .min = 6, .max = 16 },
  136. .p = { .min = 4, .max = 128 },
  137. .p1 = { .min = 2, .max = 33 },
  138. .p2 = { .dot_limit = 165000,
  139. .p2_slow = 4, .p2_fast = 2 },
  140. };
  141. static const intel_limit_t intel_limits_i8xx_dvo = {
  142. .dot = { .min = 25000, .max = 350000 },
  143. .vco = { .min = 908000, .max = 1512000 },
  144. .n = { .min = 2, .max = 16 },
  145. .m = { .min = 96, .max = 140 },
  146. .m1 = { .min = 18, .max = 26 },
  147. .m2 = { .min = 6, .max = 16 },
  148. .p = { .min = 4, .max = 128 },
  149. .p1 = { .min = 2, .max = 33 },
  150. .p2 = { .dot_limit = 165000,
  151. .p2_slow = 4, .p2_fast = 4 },
  152. };
  153. static const intel_limit_t intel_limits_i8xx_lvds = {
  154. .dot = { .min = 25000, .max = 350000 },
  155. .vco = { .min = 908000, .max = 1512000 },
  156. .n = { .min = 2, .max = 16 },
  157. .m = { .min = 96, .max = 140 },
  158. .m1 = { .min = 18, .max = 26 },
  159. .m2 = { .min = 6, .max = 16 },
  160. .p = { .min = 4, .max = 128 },
  161. .p1 = { .min = 1, .max = 6 },
  162. .p2 = { .dot_limit = 165000,
  163. .p2_slow = 14, .p2_fast = 7 },
  164. };
  165. static const intel_limit_t intel_limits_i9xx_sdvo = {
  166. .dot = { .min = 20000, .max = 400000 },
  167. .vco = { .min = 1400000, .max = 2800000 },
  168. .n = { .min = 1, .max = 6 },
  169. .m = { .min = 70, .max = 120 },
  170. .m1 = { .min = 8, .max = 18 },
  171. .m2 = { .min = 3, .max = 7 },
  172. .p = { .min = 5, .max = 80 },
  173. .p1 = { .min = 1, .max = 8 },
  174. .p2 = { .dot_limit = 200000,
  175. .p2_slow = 10, .p2_fast = 5 },
  176. };
  177. static const intel_limit_t intel_limits_i9xx_lvds = {
  178. .dot = { .min = 20000, .max = 400000 },
  179. .vco = { .min = 1400000, .max = 2800000 },
  180. .n = { .min = 1, .max = 6 },
  181. .m = { .min = 70, .max = 120 },
  182. .m1 = { .min = 8, .max = 18 },
  183. .m2 = { .min = 3, .max = 7 },
  184. .p = { .min = 7, .max = 98 },
  185. .p1 = { .min = 1, .max = 8 },
  186. .p2 = { .dot_limit = 112000,
  187. .p2_slow = 14, .p2_fast = 7 },
  188. };
  189. static const intel_limit_t intel_limits_g4x_sdvo = {
  190. .dot = { .min = 25000, .max = 270000 },
  191. .vco = { .min = 1750000, .max = 3500000},
  192. .n = { .min = 1, .max = 4 },
  193. .m = { .min = 104, .max = 138 },
  194. .m1 = { .min = 17, .max = 23 },
  195. .m2 = { .min = 5, .max = 11 },
  196. .p = { .min = 10, .max = 30 },
  197. .p1 = { .min = 1, .max = 3},
  198. .p2 = { .dot_limit = 270000,
  199. .p2_slow = 10,
  200. .p2_fast = 10
  201. },
  202. };
  203. static const intel_limit_t intel_limits_g4x_hdmi = {
  204. .dot = { .min = 22000, .max = 400000 },
  205. .vco = { .min = 1750000, .max = 3500000},
  206. .n = { .min = 1, .max = 4 },
  207. .m = { .min = 104, .max = 138 },
  208. .m1 = { .min = 16, .max = 23 },
  209. .m2 = { .min = 5, .max = 11 },
  210. .p = { .min = 5, .max = 80 },
  211. .p1 = { .min = 1, .max = 8},
  212. .p2 = { .dot_limit = 165000,
  213. .p2_slow = 10, .p2_fast = 5 },
  214. };
  215. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  216. .dot = { .min = 20000, .max = 115000 },
  217. .vco = { .min = 1750000, .max = 3500000 },
  218. .n = { .min = 1, .max = 3 },
  219. .m = { .min = 104, .max = 138 },
  220. .m1 = { .min = 17, .max = 23 },
  221. .m2 = { .min = 5, .max = 11 },
  222. .p = { .min = 28, .max = 112 },
  223. .p1 = { .min = 2, .max = 8 },
  224. .p2 = { .dot_limit = 0,
  225. .p2_slow = 14, .p2_fast = 14
  226. },
  227. };
  228. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  229. .dot = { .min = 80000, .max = 224000 },
  230. .vco = { .min = 1750000, .max = 3500000 },
  231. .n = { .min = 1, .max = 3 },
  232. .m = { .min = 104, .max = 138 },
  233. .m1 = { .min = 17, .max = 23 },
  234. .m2 = { .min = 5, .max = 11 },
  235. .p = { .min = 14, .max = 42 },
  236. .p1 = { .min = 2, .max = 6 },
  237. .p2 = { .dot_limit = 0,
  238. .p2_slow = 7, .p2_fast = 7
  239. },
  240. };
  241. static const intel_limit_t intel_limits_pineview_sdvo = {
  242. .dot = { .min = 20000, .max = 400000},
  243. .vco = { .min = 1700000, .max = 3500000 },
  244. /* Pineview's Ncounter is a ring counter */
  245. .n = { .min = 3, .max = 6 },
  246. .m = { .min = 2, .max = 256 },
  247. /* Pineview only has one combined m divider, which we treat as m2. */
  248. .m1 = { .min = 0, .max = 0 },
  249. .m2 = { .min = 0, .max = 254 },
  250. .p = { .min = 5, .max = 80 },
  251. .p1 = { .min = 1, .max = 8 },
  252. .p2 = { .dot_limit = 200000,
  253. .p2_slow = 10, .p2_fast = 5 },
  254. };
  255. static const intel_limit_t intel_limits_pineview_lvds = {
  256. .dot = { .min = 20000, .max = 400000 },
  257. .vco = { .min = 1700000, .max = 3500000 },
  258. .n = { .min = 3, .max = 6 },
  259. .m = { .min = 2, .max = 256 },
  260. .m1 = { .min = 0, .max = 0 },
  261. .m2 = { .min = 0, .max = 254 },
  262. .p = { .min = 7, .max = 112 },
  263. .p1 = { .min = 1, .max = 8 },
  264. .p2 = { .dot_limit = 112000,
  265. .p2_slow = 14, .p2_fast = 14 },
  266. };
  267. /* Ironlake / Sandybridge
  268. *
  269. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  270. * the range value for them is (actual_value - 2).
  271. */
  272. static const intel_limit_t intel_limits_ironlake_dac = {
  273. .dot = { .min = 25000, .max = 350000 },
  274. .vco = { .min = 1760000, .max = 3510000 },
  275. .n = { .min = 1, .max = 5 },
  276. .m = { .min = 79, .max = 127 },
  277. .m1 = { .min = 12, .max = 22 },
  278. .m2 = { .min = 5, .max = 9 },
  279. .p = { .min = 5, .max = 80 },
  280. .p1 = { .min = 1, .max = 8 },
  281. .p2 = { .dot_limit = 225000,
  282. .p2_slow = 10, .p2_fast = 5 },
  283. };
  284. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  285. .dot = { .min = 25000, .max = 350000 },
  286. .vco = { .min = 1760000, .max = 3510000 },
  287. .n = { .min = 1, .max = 3 },
  288. .m = { .min = 79, .max = 118 },
  289. .m1 = { .min = 12, .max = 22 },
  290. .m2 = { .min = 5, .max = 9 },
  291. .p = { .min = 28, .max = 112 },
  292. .p1 = { .min = 2, .max = 8 },
  293. .p2 = { .dot_limit = 225000,
  294. .p2_slow = 14, .p2_fast = 14 },
  295. };
  296. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  297. .dot = { .min = 25000, .max = 350000 },
  298. .vco = { .min = 1760000, .max = 3510000 },
  299. .n = { .min = 1, .max = 3 },
  300. .m = { .min = 79, .max = 127 },
  301. .m1 = { .min = 12, .max = 22 },
  302. .m2 = { .min = 5, .max = 9 },
  303. .p = { .min = 14, .max = 56 },
  304. .p1 = { .min = 2, .max = 8 },
  305. .p2 = { .dot_limit = 225000,
  306. .p2_slow = 7, .p2_fast = 7 },
  307. };
  308. /* LVDS 100mhz refclk limits. */
  309. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  310. .dot = { .min = 25000, .max = 350000 },
  311. .vco = { .min = 1760000, .max = 3510000 },
  312. .n = { .min = 1, .max = 2 },
  313. .m = { .min = 79, .max = 126 },
  314. .m1 = { .min = 12, .max = 22 },
  315. .m2 = { .min = 5, .max = 9 },
  316. .p = { .min = 28, .max = 112 },
  317. .p1 = { .min = 2, .max = 8 },
  318. .p2 = { .dot_limit = 225000,
  319. .p2_slow = 14, .p2_fast = 14 },
  320. };
  321. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  322. .dot = { .min = 25000, .max = 350000 },
  323. .vco = { .min = 1760000, .max = 3510000 },
  324. .n = { .min = 1, .max = 3 },
  325. .m = { .min = 79, .max = 126 },
  326. .m1 = { .min = 12, .max = 22 },
  327. .m2 = { .min = 5, .max = 9 },
  328. .p = { .min = 14, .max = 42 },
  329. .p1 = { .min = 2, .max = 6 },
  330. .p2 = { .dot_limit = 225000,
  331. .p2_slow = 7, .p2_fast = 7 },
  332. };
  333. static const intel_limit_t intel_limits_vlv = {
  334. /*
  335. * These are the data rate limits (measured in fast clocks)
  336. * since those are the strictest limits we have. The fast
  337. * clock and actual rate limits are more relaxed, so checking
  338. * them would make no difference.
  339. */
  340. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  341. .vco = { .min = 4000000, .max = 6000000 },
  342. .n = { .min = 1, .max = 7 },
  343. .m1 = { .min = 2, .max = 3 },
  344. .m2 = { .min = 11, .max = 156 },
  345. .p1 = { .min = 2, .max = 3 },
  346. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  347. };
  348. static const intel_limit_t intel_limits_chv = {
  349. /*
  350. * These are the data rate limits (measured in fast clocks)
  351. * since those are the strictest limits we have. The fast
  352. * clock and actual rate limits are more relaxed, so checking
  353. * them would make no difference.
  354. */
  355. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  356. .vco = { .min = 4860000, .max = 6700000 },
  357. .n = { .min = 1, .max = 1 },
  358. .m1 = { .min = 2, .max = 2 },
  359. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  360. .p1 = { .min = 2, .max = 4 },
  361. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  362. };
  363. static void vlv_clock(int refclk, intel_clock_t *clock)
  364. {
  365. clock->m = clock->m1 * clock->m2;
  366. clock->p = clock->p1 * clock->p2;
  367. if (WARN_ON(clock->n == 0 || clock->p == 0))
  368. return;
  369. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  370. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  371. }
  372. /**
  373. * Returns whether any output on the specified pipe is of the specified type
  374. */
  375. static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  376. {
  377. struct drm_device *dev = crtc->dev;
  378. struct intel_encoder *encoder;
  379. for_each_encoder_on_crtc(dev, crtc, encoder)
  380. if (encoder->type == type)
  381. return true;
  382. return false;
  383. }
  384. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  385. int refclk)
  386. {
  387. struct drm_device *dev = crtc->dev;
  388. const intel_limit_t *limit;
  389. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  390. if (intel_is_dual_link_lvds(dev)) {
  391. if (refclk == 100000)
  392. limit = &intel_limits_ironlake_dual_lvds_100m;
  393. else
  394. limit = &intel_limits_ironlake_dual_lvds;
  395. } else {
  396. if (refclk == 100000)
  397. limit = &intel_limits_ironlake_single_lvds_100m;
  398. else
  399. limit = &intel_limits_ironlake_single_lvds;
  400. }
  401. } else
  402. limit = &intel_limits_ironlake_dac;
  403. return limit;
  404. }
  405. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  406. {
  407. struct drm_device *dev = crtc->dev;
  408. const intel_limit_t *limit;
  409. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  410. if (intel_is_dual_link_lvds(dev))
  411. limit = &intel_limits_g4x_dual_channel_lvds;
  412. else
  413. limit = &intel_limits_g4x_single_channel_lvds;
  414. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  415. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  416. limit = &intel_limits_g4x_hdmi;
  417. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  418. limit = &intel_limits_g4x_sdvo;
  419. } else /* The option is for other outputs */
  420. limit = &intel_limits_i9xx_sdvo;
  421. return limit;
  422. }
  423. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  424. {
  425. struct drm_device *dev = crtc->dev;
  426. const intel_limit_t *limit;
  427. if (HAS_PCH_SPLIT(dev))
  428. limit = intel_ironlake_limit(crtc, refclk);
  429. else if (IS_G4X(dev)) {
  430. limit = intel_g4x_limit(crtc);
  431. } else if (IS_PINEVIEW(dev)) {
  432. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  433. limit = &intel_limits_pineview_lvds;
  434. else
  435. limit = &intel_limits_pineview_sdvo;
  436. } else if (IS_CHERRYVIEW(dev)) {
  437. limit = &intel_limits_chv;
  438. } else if (IS_VALLEYVIEW(dev)) {
  439. limit = &intel_limits_vlv;
  440. } else if (!IS_GEN2(dev)) {
  441. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  442. limit = &intel_limits_i9xx_lvds;
  443. else
  444. limit = &intel_limits_i9xx_sdvo;
  445. } else {
  446. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  447. limit = &intel_limits_i8xx_lvds;
  448. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
  449. limit = &intel_limits_i8xx_dvo;
  450. else
  451. limit = &intel_limits_i8xx_dac;
  452. }
  453. return limit;
  454. }
  455. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  456. static void pineview_clock(int refclk, intel_clock_t *clock)
  457. {
  458. clock->m = clock->m2 + 2;
  459. clock->p = clock->p1 * clock->p2;
  460. if (WARN_ON(clock->n == 0 || clock->p == 0))
  461. return;
  462. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  463. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  464. }
  465. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  466. {
  467. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  468. }
  469. static void i9xx_clock(int refclk, intel_clock_t *clock)
  470. {
  471. clock->m = i9xx_dpll_compute_m(clock);
  472. clock->p = clock->p1 * clock->p2;
  473. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  474. return;
  475. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  476. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  477. }
  478. static void chv_clock(int refclk, intel_clock_t *clock)
  479. {
  480. clock->m = clock->m1 * clock->m2;
  481. clock->p = clock->p1 * clock->p2;
  482. if (WARN_ON(clock->n == 0 || clock->p == 0))
  483. return;
  484. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  485. clock->n << 22);
  486. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  487. }
  488. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  489. /**
  490. * Returns whether the given set of divisors are valid for a given refclk with
  491. * the given connectors.
  492. */
  493. static bool intel_PLL_is_valid(struct drm_device *dev,
  494. const intel_limit_t *limit,
  495. const intel_clock_t *clock)
  496. {
  497. if (clock->n < limit->n.min || limit->n.max < clock->n)
  498. INTELPllInvalid("n out of range\n");
  499. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  500. INTELPllInvalid("p1 out of range\n");
  501. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  502. INTELPllInvalid("m2 out of range\n");
  503. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  504. INTELPllInvalid("m1 out of range\n");
  505. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  506. if (clock->m1 <= clock->m2)
  507. INTELPllInvalid("m1 <= m2\n");
  508. if (!IS_VALLEYVIEW(dev)) {
  509. if (clock->p < limit->p.min || limit->p.max < clock->p)
  510. INTELPllInvalid("p out of range\n");
  511. if (clock->m < limit->m.min || limit->m.max < clock->m)
  512. INTELPllInvalid("m out of range\n");
  513. }
  514. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  515. INTELPllInvalid("vco out of range\n");
  516. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  517. * connector, etc., rather than just a single range.
  518. */
  519. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  520. INTELPllInvalid("dot out of range\n");
  521. return true;
  522. }
  523. static bool
  524. i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  525. int target, int refclk, intel_clock_t *match_clock,
  526. intel_clock_t *best_clock)
  527. {
  528. struct drm_device *dev = crtc->dev;
  529. intel_clock_t clock;
  530. int err = target;
  531. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  532. /*
  533. * For LVDS just rely on its current settings for dual-channel.
  534. * We haven't figured out how to reliably set up different
  535. * single/dual channel state, if we even can.
  536. */
  537. if (intel_is_dual_link_lvds(dev))
  538. clock.p2 = limit->p2.p2_fast;
  539. else
  540. clock.p2 = limit->p2.p2_slow;
  541. } else {
  542. if (target < limit->p2.dot_limit)
  543. clock.p2 = limit->p2.p2_slow;
  544. else
  545. clock.p2 = limit->p2.p2_fast;
  546. }
  547. memset(best_clock, 0, sizeof(*best_clock));
  548. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  549. clock.m1++) {
  550. for (clock.m2 = limit->m2.min;
  551. clock.m2 <= limit->m2.max; clock.m2++) {
  552. if (clock.m2 >= clock.m1)
  553. break;
  554. for (clock.n = limit->n.min;
  555. clock.n <= limit->n.max; clock.n++) {
  556. for (clock.p1 = limit->p1.min;
  557. clock.p1 <= limit->p1.max; clock.p1++) {
  558. int this_err;
  559. i9xx_clock(refclk, &clock);
  560. if (!intel_PLL_is_valid(dev, limit,
  561. &clock))
  562. continue;
  563. if (match_clock &&
  564. clock.p != match_clock->p)
  565. continue;
  566. this_err = abs(clock.dot - target);
  567. if (this_err < err) {
  568. *best_clock = clock;
  569. err = this_err;
  570. }
  571. }
  572. }
  573. }
  574. }
  575. return (err != target);
  576. }
  577. static bool
  578. pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  579. int target, int refclk, intel_clock_t *match_clock,
  580. intel_clock_t *best_clock)
  581. {
  582. struct drm_device *dev = crtc->dev;
  583. intel_clock_t clock;
  584. int err = target;
  585. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  586. /*
  587. * For LVDS just rely on its current settings for dual-channel.
  588. * We haven't figured out how to reliably set up different
  589. * single/dual channel state, if we even can.
  590. */
  591. if (intel_is_dual_link_lvds(dev))
  592. clock.p2 = limit->p2.p2_fast;
  593. else
  594. clock.p2 = limit->p2.p2_slow;
  595. } else {
  596. if (target < limit->p2.dot_limit)
  597. clock.p2 = limit->p2.p2_slow;
  598. else
  599. clock.p2 = limit->p2.p2_fast;
  600. }
  601. memset(best_clock, 0, sizeof(*best_clock));
  602. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  603. clock.m1++) {
  604. for (clock.m2 = limit->m2.min;
  605. clock.m2 <= limit->m2.max; clock.m2++) {
  606. for (clock.n = limit->n.min;
  607. clock.n <= limit->n.max; clock.n++) {
  608. for (clock.p1 = limit->p1.min;
  609. clock.p1 <= limit->p1.max; clock.p1++) {
  610. int this_err;
  611. pineview_clock(refclk, &clock);
  612. if (!intel_PLL_is_valid(dev, limit,
  613. &clock))
  614. continue;
  615. if (match_clock &&
  616. clock.p != match_clock->p)
  617. continue;
  618. this_err = abs(clock.dot - target);
  619. if (this_err < err) {
  620. *best_clock = clock;
  621. err = this_err;
  622. }
  623. }
  624. }
  625. }
  626. }
  627. return (err != target);
  628. }
  629. static bool
  630. g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  631. int target, int refclk, intel_clock_t *match_clock,
  632. intel_clock_t *best_clock)
  633. {
  634. struct drm_device *dev = crtc->dev;
  635. intel_clock_t clock;
  636. int max_n;
  637. bool found;
  638. /* approximately equals target * 0.00585 */
  639. int err_most = (target >> 8) + (target >> 9);
  640. found = false;
  641. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  642. if (intel_is_dual_link_lvds(dev))
  643. clock.p2 = limit->p2.p2_fast;
  644. else
  645. clock.p2 = limit->p2.p2_slow;
  646. } else {
  647. if (target < limit->p2.dot_limit)
  648. clock.p2 = limit->p2.p2_slow;
  649. else
  650. clock.p2 = limit->p2.p2_fast;
  651. }
  652. memset(best_clock, 0, sizeof(*best_clock));
  653. max_n = limit->n.max;
  654. /* based on hardware requirement, prefer smaller n to precision */
  655. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  656. /* based on hardware requirement, prefere larger m1,m2 */
  657. for (clock.m1 = limit->m1.max;
  658. clock.m1 >= limit->m1.min; clock.m1--) {
  659. for (clock.m2 = limit->m2.max;
  660. clock.m2 >= limit->m2.min; clock.m2--) {
  661. for (clock.p1 = limit->p1.max;
  662. clock.p1 >= limit->p1.min; clock.p1--) {
  663. int this_err;
  664. i9xx_clock(refclk, &clock);
  665. if (!intel_PLL_is_valid(dev, limit,
  666. &clock))
  667. continue;
  668. this_err = abs(clock.dot - target);
  669. if (this_err < err_most) {
  670. *best_clock = clock;
  671. err_most = this_err;
  672. max_n = clock.n;
  673. found = true;
  674. }
  675. }
  676. }
  677. }
  678. }
  679. return found;
  680. }
  681. static bool
  682. vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  683. int target, int refclk, intel_clock_t *match_clock,
  684. intel_clock_t *best_clock)
  685. {
  686. struct drm_device *dev = crtc->dev;
  687. intel_clock_t clock;
  688. unsigned int bestppm = 1000000;
  689. /* min update 19.2 MHz */
  690. int max_n = min(limit->n.max, refclk / 19200);
  691. bool found = false;
  692. target *= 5; /* fast clock */
  693. memset(best_clock, 0, sizeof(*best_clock));
  694. /* based on hardware requirement, prefer smaller n to precision */
  695. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  696. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  697. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  698. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  699. clock.p = clock.p1 * clock.p2;
  700. /* based on hardware requirement, prefer bigger m1,m2 values */
  701. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  702. unsigned int ppm, diff;
  703. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  704. refclk * clock.m1);
  705. vlv_clock(refclk, &clock);
  706. if (!intel_PLL_is_valid(dev, limit,
  707. &clock))
  708. continue;
  709. diff = abs(clock.dot - target);
  710. ppm = div_u64(1000000ULL * diff, target);
  711. if (ppm < 100 && clock.p > best_clock->p) {
  712. bestppm = 0;
  713. *best_clock = clock;
  714. found = true;
  715. }
  716. if (bestppm >= 10 && ppm < bestppm - 10) {
  717. bestppm = ppm;
  718. *best_clock = clock;
  719. found = true;
  720. }
  721. }
  722. }
  723. }
  724. }
  725. return found;
  726. }
  727. static bool
  728. chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  729. int target, int refclk, intel_clock_t *match_clock,
  730. intel_clock_t *best_clock)
  731. {
  732. struct drm_device *dev = crtc->dev;
  733. intel_clock_t clock;
  734. uint64_t m2;
  735. int found = false;
  736. memset(best_clock, 0, sizeof(*best_clock));
  737. /*
  738. * Based on hardware doc, the n always set to 1, and m1 always
  739. * set to 2. If requires to support 200Mhz refclk, we need to
  740. * revisit this because n may not 1 anymore.
  741. */
  742. clock.n = 1, clock.m1 = 2;
  743. target *= 5; /* fast clock */
  744. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  745. for (clock.p2 = limit->p2.p2_fast;
  746. clock.p2 >= limit->p2.p2_slow;
  747. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  748. clock.p = clock.p1 * clock.p2;
  749. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  750. clock.n) << 22, refclk * clock.m1);
  751. if (m2 > INT_MAX/clock.m1)
  752. continue;
  753. clock.m2 = m2;
  754. chv_clock(refclk, &clock);
  755. if (!intel_PLL_is_valid(dev, limit, &clock))
  756. continue;
  757. /* based on hardware requirement, prefer bigger p
  758. */
  759. if (clock.p > best_clock->p) {
  760. *best_clock = clock;
  761. found = true;
  762. }
  763. }
  764. }
  765. return found;
  766. }
  767. bool intel_crtc_active(struct drm_crtc *crtc)
  768. {
  769. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  770. /* Be paranoid as we can arrive here with only partial
  771. * state retrieved from the hardware during setup.
  772. *
  773. * We can ditch the adjusted_mode.crtc_clock check as soon
  774. * as Haswell has gained clock readout/fastboot support.
  775. *
  776. * We can ditch the crtc->primary->fb check as soon as we can
  777. * properly reconstruct framebuffers.
  778. */
  779. return intel_crtc->active && crtc->primary->fb &&
  780. intel_crtc->config.adjusted_mode.crtc_clock;
  781. }
  782. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  783. enum pipe pipe)
  784. {
  785. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  786. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  787. return intel_crtc->config.cpu_transcoder;
  788. }
  789. static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
  790. {
  791. struct drm_i915_private *dev_priv = dev->dev_private;
  792. u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
  793. frame = I915_READ(frame_reg);
  794. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  795. WARN(1, "vblank wait timed out\n");
  796. }
  797. /**
  798. * intel_wait_for_vblank - wait for vblank on a given pipe
  799. * @dev: drm device
  800. * @pipe: pipe to wait for
  801. *
  802. * Wait for vblank to occur on a given pipe. Needed for various bits of
  803. * mode setting code.
  804. */
  805. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  806. {
  807. struct drm_i915_private *dev_priv = dev->dev_private;
  808. int pipestat_reg = PIPESTAT(pipe);
  809. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  810. g4x_wait_for_vblank(dev, pipe);
  811. return;
  812. }
  813. /* Clear existing vblank status. Note this will clear any other
  814. * sticky status fields as well.
  815. *
  816. * This races with i915_driver_irq_handler() with the result
  817. * that either function could miss a vblank event. Here it is not
  818. * fatal, as we will either wait upon the next vblank interrupt or
  819. * timeout. Generally speaking intel_wait_for_vblank() is only
  820. * called during modeset at which time the GPU should be idle and
  821. * should *not* be performing page flips and thus not waiting on
  822. * vblanks...
  823. * Currently, the result of us stealing a vblank from the irq
  824. * handler is that a single frame will be skipped during swapbuffers.
  825. */
  826. I915_WRITE(pipestat_reg,
  827. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  828. /* Wait for vblank interrupt bit to set */
  829. if (wait_for(I915_READ(pipestat_reg) &
  830. PIPE_VBLANK_INTERRUPT_STATUS,
  831. 50))
  832. DRM_DEBUG_KMS("vblank wait timed out\n");
  833. }
  834. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  835. {
  836. struct drm_i915_private *dev_priv = dev->dev_private;
  837. u32 reg = PIPEDSL(pipe);
  838. u32 line1, line2;
  839. u32 line_mask;
  840. if (IS_GEN2(dev))
  841. line_mask = DSL_LINEMASK_GEN2;
  842. else
  843. line_mask = DSL_LINEMASK_GEN3;
  844. line1 = I915_READ(reg) & line_mask;
  845. mdelay(5);
  846. line2 = I915_READ(reg) & line_mask;
  847. return line1 == line2;
  848. }
  849. /*
  850. * intel_wait_for_pipe_off - wait for pipe to turn off
  851. * @dev: drm device
  852. * @pipe: pipe to wait for
  853. *
  854. * After disabling a pipe, we can't wait for vblank in the usual way,
  855. * spinning on the vblank interrupt status bit, since we won't actually
  856. * see an interrupt when the pipe is disabled.
  857. *
  858. * On Gen4 and above:
  859. * wait for the pipe register state bit to turn off
  860. *
  861. * Otherwise:
  862. * wait for the display line value to settle (it usually
  863. * ends up stopping at the start of the next frame).
  864. *
  865. */
  866. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  867. {
  868. struct drm_i915_private *dev_priv = dev->dev_private;
  869. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  870. pipe);
  871. if (INTEL_INFO(dev)->gen >= 4) {
  872. int reg = PIPECONF(cpu_transcoder);
  873. /* Wait for the Pipe State to go off */
  874. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  875. 100))
  876. WARN(1, "pipe_off wait timed out\n");
  877. } else {
  878. /* Wait for the display line to settle */
  879. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  880. WARN(1, "pipe_off wait timed out\n");
  881. }
  882. }
  883. /*
  884. * ibx_digital_port_connected - is the specified port connected?
  885. * @dev_priv: i915 private structure
  886. * @port: the port to test
  887. *
  888. * Returns true if @port is connected, false otherwise.
  889. */
  890. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  891. struct intel_digital_port *port)
  892. {
  893. u32 bit;
  894. if (HAS_PCH_IBX(dev_priv->dev)) {
  895. switch (port->port) {
  896. case PORT_B:
  897. bit = SDE_PORTB_HOTPLUG;
  898. break;
  899. case PORT_C:
  900. bit = SDE_PORTC_HOTPLUG;
  901. break;
  902. case PORT_D:
  903. bit = SDE_PORTD_HOTPLUG;
  904. break;
  905. default:
  906. return true;
  907. }
  908. } else {
  909. switch (port->port) {
  910. case PORT_B:
  911. bit = SDE_PORTB_HOTPLUG_CPT;
  912. break;
  913. case PORT_C:
  914. bit = SDE_PORTC_HOTPLUG_CPT;
  915. break;
  916. case PORT_D:
  917. bit = SDE_PORTD_HOTPLUG_CPT;
  918. break;
  919. default:
  920. return true;
  921. }
  922. }
  923. return I915_READ(SDEISR) & bit;
  924. }
  925. static const char *state_string(bool enabled)
  926. {
  927. return enabled ? "on" : "off";
  928. }
  929. /* Only for pre-ILK configs */
  930. void assert_pll(struct drm_i915_private *dev_priv,
  931. enum pipe pipe, bool state)
  932. {
  933. int reg;
  934. u32 val;
  935. bool cur_state;
  936. reg = DPLL(pipe);
  937. val = I915_READ(reg);
  938. cur_state = !!(val & DPLL_VCO_ENABLE);
  939. WARN(cur_state != state,
  940. "PLL state assertion failure (expected %s, current %s)\n",
  941. state_string(state), state_string(cur_state));
  942. }
  943. /* XXX: the dsi pll is shared between MIPI DSI ports */
  944. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  945. {
  946. u32 val;
  947. bool cur_state;
  948. mutex_lock(&dev_priv->dpio_lock);
  949. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  950. mutex_unlock(&dev_priv->dpio_lock);
  951. cur_state = val & DSI_PLL_VCO_EN;
  952. WARN(cur_state != state,
  953. "DSI PLL state assertion failure (expected %s, current %s)\n",
  954. state_string(state), state_string(cur_state));
  955. }
  956. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  957. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  958. struct intel_shared_dpll *
  959. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  960. {
  961. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  962. if (crtc->config.shared_dpll < 0)
  963. return NULL;
  964. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  965. }
  966. /* For ILK+ */
  967. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  968. struct intel_shared_dpll *pll,
  969. bool state)
  970. {
  971. bool cur_state;
  972. struct intel_dpll_hw_state hw_state;
  973. if (WARN (!pll,
  974. "asserting DPLL %s with no DPLL\n", state_string(state)))
  975. return;
  976. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  977. WARN(cur_state != state,
  978. "%s assertion failure (expected %s, current %s)\n",
  979. pll->name, state_string(state), state_string(cur_state));
  980. }
  981. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  982. enum pipe pipe, bool state)
  983. {
  984. int reg;
  985. u32 val;
  986. bool cur_state;
  987. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  988. pipe);
  989. if (HAS_DDI(dev_priv->dev)) {
  990. /* DDI does not have a specific FDI_TX register */
  991. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  992. val = I915_READ(reg);
  993. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  994. } else {
  995. reg = FDI_TX_CTL(pipe);
  996. val = I915_READ(reg);
  997. cur_state = !!(val & FDI_TX_ENABLE);
  998. }
  999. WARN(cur_state != state,
  1000. "FDI TX state assertion failure (expected %s, current %s)\n",
  1001. state_string(state), state_string(cur_state));
  1002. }
  1003. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1004. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1005. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1006. enum pipe pipe, bool state)
  1007. {
  1008. int reg;
  1009. u32 val;
  1010. bool cur_state;
  1011. reg = FDI_RX_CTL(pipe);
  1012. val = I915_READ(reg);
  1013. cur_state = !!(val & FDI_RX_ENABLE);
  1014. WARN(cur_state != state,
  1015. "FDI RX state assertion failure (expected %s, current %s)\n",
  1016. state_string(state), state_string(cur_state));
  1017. }
  1018. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1019. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1020. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1021. enum pipe pipe)
  1022. {
  1023. int reg;
  1024. u32 val;
  1025. /* ILK FDI PLL is always enabled */
  1026. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1027. return;
  1028. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1029. if (HAS_DDI(dev_priv->dev))
  1030. return;
  1031. reg = FDI_TX_CTL(pipe);
  1032. val = I915_READ(reg);
  1033. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1034. }
  1035. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1036. enum pipe pipe, bool state)
  1037. {
  1038. int reg;
  1039. u32 val;
  1040. bool cur_state;
  1041. reg = FDI_RX_CTL(pipe);
  1042. val = I915_READ(reg);
  1043. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1044. WARN(cur_state != state,
  1045. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1046. state_string(state), state_string(cur_state));
  1047. }
  1048. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1049. enum pipe pipe)
  1050. {
  1051. int pp_reg, lvds_reg;
  1052. u32 val;
  1053. enum pipe panel_pipe = PIPE_A;
  1054. bool locked = true;
  1055. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  1056. pp_reg = PCH_PP_CONTROL;
  1057. lvds_reg = PCH_LVDS;
  1058. } else {
  1059. pp_reg = PP_CONTROL;
  1060. lvds_reg = LVDS;
  1061. }
  1062. val = I915_READ(pp_reg);
  1063. if (!(val & PANEL_POWER_ON) ||
  1064. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  1065. locked = false;
  1066. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  1067. panel_pipe = PIPE_B;
  1068. WARN(panel_pipe == pipe && locked,
  1069. "panel assertion failure, pipe %c regs locked\n",
  1070. pipe_name(pipe));
  1071. }
  1072. static void assert_cursor(struct drm_i915_private *dev_priv,
  1073. enum pipe pipe, bool state)
  1074. {
  1075. struct drm_device *dev = dev_priv->dev;
  1076. bool cur_state;
  1077. if (IS_845G(dev) || IS_I865G(dev))
  1078. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1079. else
  1080. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1081. WARN(cur_state != state,
  1082. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1083. pipe_name(pipe), state_string(state), state_string(cur_state));
  1084. }
  1085. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1086. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1087. void assert_pipe(struct drm_i915_private *dev_priv,
  1088. enum pipe pipe, bool state)
  1089. {
  1090. int reg;
  1091. u32 val;
  1092. bool cur_state;
  1093. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1094. pipe);
  1095. /* if we need the pipe A quirk it must be always on */
  1096. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  1097. state = true;
  1098. if (!intel_display_power_enabled(dev_priv,
  1099. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1100. cur_state = false;
  1101. } else {
  1102. reg = PIPECONF(cpu_transcoder);
  1103. val = I915_READ(reg);
  1104. cur_state = !!(val & PIPECONF_ENABLE);
  1105. }
  1106. WARN(cur_state != state,
  1107. "pipe %c assertion failure (expected %s, current %s)\n",
  1108. pipe_name(pipe), state_string(state), state_string(cur_state));
  1109. }
  1110. static void assert_plane(struct drm_i915_private *dev_priv,
  1111. enum plane plane, bool state)
  1112. {
  1113. int reg;
  1114. u32 val;
  1115. bool cur_state;
  1116. reg = DSPCNTR(plane);
  1117. val = I915_READ(reg);
  1118. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1119. WARN(cur_state != state,
  1120. "plane %c assertion failure (expected %s, current %s)\n",
  1121. plane_name(plane), state_string(state), state_string(cur_state));
  1122. }
  1123. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1124. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1125. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1126. enum pipe pipe)
  1127. {
  1128. struct drm_device *dev = dev_priv->dev;
  1129. int reg, i;
  1130. u32 val;
  1131. int cur_pipe;
  1132. /* Primary planes are fixed to pipes on gen4+ */
  1133. if (INTEL_INFO(dev)->gen >= 4) {
  1134. reg = DSPCNTR(pipe);
  1135. val = I915_READ(reg);
  1136. WARN(val & DISPLAY_PLANE_ENABLE,
  1137. "plane %c assertion failure, should be disabled but not\n",
  1138. plane_name(pipe));
  1139. return;
  1140. }
  1141. /* Need to check both planes against the pipe */
  1142. for_each_pipe(i) {
  1143. reg = DSPCNTR(i);
  1144. val = I915_READ(reg);
  1145. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1146. DISPPLANE_SEL_PIPE_SHIFT;
  1147. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1148. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1149. plane_name(i), pipe_name(pipe));
  1150. }
  1151. }
  1152. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1153. enum pipe pipe)
  1154. {
  1155. struct drm_device *dev = dev_priv->dev;
  1156. int reg, sprite;
  1157. u32 val;
  1158. if (IS_VALLEYVIEW(dev)) {
  1159. for_each_sprite(pipe, sprite) {
  1160. reg = SPCNTR(pipe, sprite);
  1161. val = I915_READ(reg);
  1162. WARN(val & SP_ENABLE,
  1163. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1164. sprite_name(pipe, sprite), pipe_name(pipe));
  1165. }
  1166. } else if (INTEL_INFO(dev)->gen >= 7) {
  1167. reg = SPRCTL(pipe);
  1168. val = I915_READ(reg);
  1169. WARN(val & SPRITE_ENABLE,
  1170. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1171. plane_name(pipe), pipe_name(pipe));
  1172. } else if (INTEL_INFO(dev)->gen >= 5) {
  1173. reg = DVSCNTR(pipe);
  1174. val = I915_READ(reg);
  1175. WARN(val & DVS_ENABLE,
  1176. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1177. plane_name(pipe), pipe_name(pipe));
  1178. }
  1179. }
  1180. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1181. {
  1182. u32 val;
  1183. bool enabled;
  1184. WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1185. val = I915_READ(PCH_DREF_CONTROL);
  1186. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1187. DREF_SUPERSPREAD_SOURCE_MASK));
  1188. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1189. }
  1190. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1191. enum pipe pipe)
  1192. {
  1193. int reg;
  1194. u32 val;
  1195. bool enabled;
  1196. reg = PCH_TRANSCONF(pipe);
  1197. val = I915_READ(reg);
  1198. enabled = !!(val & TRANS_ENABLE);
  1199. WARN(enabled,
  1200. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1201. pipe_name(pipe));
  1202. }
  1203. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1204. enum pipe pipe, u32 port_sel, u32 val)
  1205. {
  1206. if ((val & DP_PORT_EN) == 0)
  1207. return false;
  1208. if (HAS_PCH_CPT(dev_priv->dev)) {
  1209. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1210. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1211. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1212. return false;
  1213. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1214. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1215. return false;
  1216. } else {
  1217. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1218. return false;
  1219. }
  1220. return true;
  1221. }
  1222. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1223. enum pipe pipe, u32 val)
  1224. {
  1225. if ((val & SDVO_ENABLE) == 0)
  1226. return false;
  1227. if (HAS_PCH_CPT(dev_priv->dev)) {
  1228. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1229. return false;
  1230. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1231. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1232. return false;
  1233. } else {
  1234. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1235. return false;
  1236. }
  1237. return true;
  1238. }
  1239. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1240. enum pipe pipe, u32 val)
  1241. {
  1242. if ((val & LVDS_PORT_EN) == 0)
  1243. return false;
  1244. if (HAS_PCH_CPT(dev_priv->dev)) {
  1245. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1246. return false;
  1247. } else {
  1248. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1249. return false;
  1250. }
  1251. return true;
  1252. }
  1253. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1254. enum pipe pipe, u32 val)
  1255. {
  1256. if ((val & ADPA_DAC_ENABLE) == 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 & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1263. return false;
  1264. }
  1265. return true;
  1266. }
  1267. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1268. enum pipe pipe, int reg, u32 port_sel)
  1269. {
  1270. u32 val = I915_READ(reg);
  1271. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1272. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1273. reg, pipe_name(pipe));
  1274. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1275. && (val & DP_PIPEB_SELECT),
  1276. "IBX PCH dp port still using transcoder B\n");
  1277. }
  1278. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1279. enum pipe pipe, int reg)
  1280. {
  1281. u32 val = I915_READ(reg);
  1282. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1283. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1284. reg, pipe_name(pipe));
  1285. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1286. && (val & SDVO_PIPE_B_SELECT),
  1287. "IBX PCH hdmi port still using transcoder B\n");
  1288. }
  1289. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1290. enum pipe pipe)
  1291. {
  1292. int reg;
  1293. u32 val;
  1294. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1295. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1296. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1297. reg = PCH_ADPA;
  1298. val = I915_READ(reg);
  1299. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1300. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1301. pipe_name(pipe));
  1302. reg = PCH_LVDS;
  1303. val = I915_READ(reg);
  1304. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1305. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1306. pipe_name(pipe));
  1307. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1308. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1309. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1310. }
  1311. static void intel_init_dpio(struct drm_device *dev)
  1312. {
  1313. struct drm_i915_private *dev_priv = dev->dev_private;
  1314. if (!IS_VALLEYVIEW(dev))
  1315. return;
  1316. /*
  1317. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1318. * CHV x1 PHY (DP/HDMI D)
  1319. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1320. */
  1321. if (IS_CHERRYVIEW(dev)) {
  1322. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1323. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1324. } else {
  1325. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1326. }
  1327. }
  1328. static void intel_reset_dpio(struct drm_device *dev)
  1329. {
  1330. struct drm_i915_private *dev_priv = dev->dev_private;
  1331. if (IS_CHERRYVIEW(dev)) {
  1332. enum dpio_phy phy;
  1333. u32 val;
  1334. for (phy = DPIO_PHY0; phy < I915_NUM_PHYS_VLV; phy++) {
  1335. /* Poll for phypwrgood signal */
  1336. if (wait_for(I915_READ(DISPLAY_PHY_STATUS) &
  1337. PHY_POWERGOOD(phy), 1))
  1338. DRM_ERROR("Display PHY %d is not power up\n", phy);
  1339. /*
  1340. * Deassert common lane reset for PHY.
  1341. *
  1342. * This should only be done on init and resume from S3
  1343. * with both PLLs disabled, or we risk losing DPIO and
  1344. * PLL synchronization.
  1345. */
  1346. val = I915_READ(DISPLAY_PHY_CONTROL);
  1347. I915_WRITE(DISPLAY_PHY_CONTROL,
  1348. PHY_COM_LANE_RESET_DEASSERT(phy, val));
  1349. }
  1350. }
  1351. }
  1352. static void vlv_enable_pll(struct intel_crtc *crtc)
  1353. {
  1354. struct drm_device *dev = crtc->base.dev;
  1355. struct drm_i915_private *dev_priv = dev->dev_private;
  1356. int reg = DPLL(crtc->pipe);
  1357. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1358. assert_pipe_disabled(dev_priv, crtc->pipe);
  1359. /* No really, not for ILK+ */
  1360. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1361. /* PLL is protected by panel, make sure we can write it */
  1362. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1363. assert_panel_unlocked(dev_priv, crtc->pipe);
  1364. I915_WRITE(reg, dpll);
  1365. POSTING_READ(reg);
  1366. udelay(150);
  1367. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1368. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1369. I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
  1370. POSTING_READ(DPLL_MD(crtc->pipe));
  1371. /* We do this three times for luck */
  1372. I915_WRITE(reg, dpll);
  1373. POSTING_READ(reg);
  1374. udelay(150); /* wait for warmup */
  1375. I915_WRITE(reg, dpll);
  1376. POSTING_READ(reg);
  1377. udelay(150); /* wait for warmup */
  1378. I915_WRITE(reg, dpll);
  1379. POSTING_READ(reg);
  1380. udelay(150); /* wait for warmup */
  1381. }
  1382. static void chv_enable_pll(struct intel_crtc *crtc)
  1383. {
  1384. struct drm_device *dev = crtc->base.dev;
  1385. struct drm_i915_private *dev_priv = dev->dev_private;
  1386. int pipe = crtc->pipe;
  1387. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1388. u32 tmp;
  1389. assert_pipe_disabled(dev_priv, crtc->pipe);
  1390. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1391. mutex_lock(&dev_priv->dpio_lock);
  1392. /* Enable back the 10bit clock to display controller */
  1393. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1394. tmp |= DPIO_DCLKP_EN;
  1395. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1396. /*
  1397. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1398. */
  1399. udelay(1);
  1400. /* Enable PLL */
  1401. I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
  1402. /* Check PLL is locked */
  1403. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1404. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1405. /* not sure when this should be written */
  1406. I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
  1407. POSTING_READ(DPLL_MD(pipe));
  1408. mutex_unlock(&dev_priv->dpio_lock);
  1409. }
  1410. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1411. {
  1412. struct drm_device *dev = crtc->base.dev;
  1413. struct drm_i915_private *dev_priv = dev->dev_private;
  1414. int reg = DPLL(crtc->pipe);
  1415. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1416. assert_pipe_disabled(dev_priv, crtc->pipe);
  1417. /* No really, not for ILK+ */
  1418. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1419. /* PLL is protected by panel, make sure we can write it */
  1420. if (IS_MOBILE(dev) && !IS_I830(dev))
  1421. assert_panel_unlocked(dev_priv, crtc->pipe);
  1422. I915_WRITE(reg, dpll);
  1423. /* Wait for the clocks to stabilize. */
  1424. POSTING_READ(reg);
  1425. udelay(150);
  1426. if (INTEL_INFO(dev)->gen >= 4) {
  1427. I915_WRITE(DPLL_MD(crtc->pipe),
  1428. crtc->config.dpll_hw_state.dpll_md);
  1429. } else {
  1430. /* The pixel multiplier can only be updated once the
  1431. * DPLL is enabled and the clocks are stable.
  1432. *
  1433. * So write it again.
  1434. */
  1435. I915_WRITE(reg, dpll);
  1436. }
  1437. /* We do this three times for luck */
  1438. I915_WRITE(reg, dpll);
  1439. POSTING_READ(reg);
  1440. udelay(150); /* wait for warmup */
  1441. I915_WRITE(reg, dpll);
  1442. POSTING_READ(reg);
  1443. udelay(150); /* wait for warmup */
  1444. I915_WRITE(reg, dpll);
  1445. POSTING_READ(reg);
  1446. udelay(150); /* wait for warmup */
  1447. }
  1448. /**
  1449. * i9xx_disable_pll - disable a PLL
  1450. * @dev_priv: i915 private structure
  1451. * @pipe: pipe PLL to disable
  1452. *
  1453. * Disable the PLL for @pipe, making sure the pipe is off first.
  1454. *
  1455. * Note! This is for pre-ILK only.
  1456. */
  1457. static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1458. {
  1459. /* Don't disable pipe A or pipe A PLLs if needed */
  1460. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1461. return;
  1462. /* Make sure the pipe isn't still relying on us */
  1463. assert_pipe_disabled(dev_priv, pipe);
  1464. I915_WRITE(DPLL(pipe), 0);
  1465. POSTING_READ(DPLL(pipe));
  1466. }
  1467. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1468. {
  1469. u32 val = 0;
  1470. /* Make sure the pipe isn't still relying on us */
  1471. assert_pipe_disabled(dev_priv, pipe);
  1472. /*
  1473. * Leave integrated clock source and reference clock enabled for pipe B.
  1474. * The latter is needed for VGA hotplug / manual detection.
  1475. */
  1476. if (pipe == PIPE_B)
  1477. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1478. I915_WRITE(DPLL(pipe), val);
  1479. POSTING_READ(DPLL(pipe));
  1480. }
  1481. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1482. {
  1483. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1484. u32 val;
  1485. /* Make sure the pipe isn't still relying on us */
  1486. assert_pipe_disabled(dev_priv, pipe);
  1487. /* Set PLL en = 0 */
  1488. val = DPLL_SSC_REF_CLOCK_CHV;
  1489. if (pipe != PIPE_A)
  1490. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1491. I915_WRITE(DPLL(pipe), val);
  1492. POSTING_READ(DPLL(pipe));
  1493. mutex_lock(&dev_priv->dpio_lock);
  1494. /* Disable 10bit clock to display controller */
  1495. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1496. val &= ~DPIO_DCLKP_EN;
  1497. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1498. /* disable left/right clock distribution */
  1499. if (pipe != PIPE_B) {
  1500. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1501. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1502. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1503. } else {
  1504. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1505. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1506. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1507. }
  1508. mutex_unlock(&dev_priv->dpio_lock);
  1509. }
  1510. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1511. struct intel_digital_port *dport)
  1512. {
  1513. u32 port_mask;
  1514. int dpll_reg;
  1515. switch (dport->port) {
  1516. case PORT_B:
  1517. port_mask = DPLL_PORTB_READY_MASK;
  1518. dpll_reg = DPLL(0);
  1519. break;
  1520. case PORT_C:
  1521. port_mask = DPLL_PORTC_READY_MASK;
  1522. dpll_reg = DPLL(0);
  1523. break;
  1524. case PORT_D:
  1525. port_mask = DPLL_PORTD_READY_MASK;
  1526. dpll_reg = DPIO_PHY_STATUS;
  1527. break;
  1528. default:
  1529. BUG();
  1530. }
  1531. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1532. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1533. port_name(dport->port), I915_READ(dpll_reg));
  1534. }
  1535. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1536. {
  1537. struct drm_device *dev = crtc->base.dev;
  1538. struct drm_i915_private *dev_priv = dev->dev_private;
  1539. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1540. if (WARN_ON(pll == NULL))
  1541. return;
  1542. WARN_ON(!pll->refcount);
  1543. if (pll->active == 0) {
  1544. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1545. WARN_ON(pll->on);
  1546. assert_shared_dpll_disabled(dev_priv, pll);
  1547. pll->mode_set(dev_priv, pll);
  1548. }
  1549. }
  1550. /**
  1551. * intel_enable_shared_dpll - enable PCH PLL
  1552. * @dev_priv: i915 private structure
  1553. * @pipe: pipe PLL to enable
  1554. *
  1555. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1556. * drives the transcoder clock.
  1557. */
  1558. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1559. {
  1560. struct drm_device *dev = crtc->base.dev;
  1561. struct drm_i915_private *dev_priv = dev->dev_private;
  1562. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1563. if (WARN_ON(pll == NULL))
  1564. return;
  1565. if (WARN_ON(pll->refcount == 0))
  1566. return;
  1567. DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
  1568. pll->name, pll->active, pll->on,
  1569. crtc->base.base.id);
  1570. if (pll->active++) {
  1571. WARN_ON(!pll->on);
  1572. assert_shared_dpll_enabled(dev_priv, pll);
  1573. return;
  1574. }
  1575. WARN_ON(pll->on);
  1576. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1577. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1578. pll->enable(dev_priv, pll);
  1579. pll->on = true;
  1580. }
  1581. void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1582. {
  1583. struct drm_device *dev = crtc->base.dev;
  1584. struct drm_i915_private *dev_priv = dev->dev_private;
  1585. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1586. /* PCH only available on ILK+ */
  1587. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1588. if (WARN_ON(pll == NULL))
  1589. return;
  1590. if (WARN_ON(pll->refcount == 0))
  1591. return;
  1592. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1593. pll->name, pll->active, pll->on,
  1594. crtc->base.base.id);
  1595. if (WARN_ON(pll->active == 0)) {
  1596. assert_shared_dpll_disabled(dev_priv, pll);
  1597. return;
  1598. }
  1599. assert_shared_dpll_enabled(dev_priv, pll);
  1600. WARN_ON(!pll->on);
  1601. if (--pll->active)
  1602. return;
  1603. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1604. pll->disable(dev_priv, pll);
  1605. pll->on = false;
  1606. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1607. }
  1608. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1609. enum pipe pipe)
  1610. {
  1611. struct drm_device *dev = dev_priv->dev;
  1612. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1613. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1614. uint32_t reg, val, pipeconf_val;
  1615. /* PCH only available on ILK+ */
  1616. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1617. /* Make sure PCH DPLL is enabled */
  1618. assert_shared_dpll_enabled(dev_priv,
  1619. intel_crtc_to_shared_dpll(intel_crtc));
  1620. /* FDI must be feeding us bits for PCH ports */
  1621. assert_fdi_tx_enabled(dev_priv, pipe);
  1622. assert_fdi_rx_enabled(dev_priv, pipe);
  1623. if (HAS_PCH_CPT(dev)) {
  1624. /* Workaround: Set the timing override bit before enabling the
  1625. * pch transcoder. */
  1626. reg = TRANS_CHICKEN2(pipe);
  1627. val = I915_READ(reg);
  1628. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1629. I915_WRITE(reg, val);
  1630. }
  1631. reg = PCH_TRANSCONF(pipe);
  1632. val = I915_READ(reg);
  1633. pipeconf_val = I915_READ(PIPECONF(pipe));
  1634. if (HAS_PCH_IBX(dev_priv->dev)) {
  1635. /*
  1636. * make the BPC in transcoder be consistent with
  1637. * that in pipeconf reg.
  1638. */
  1639. val &= ~PIPECONF_BPC_MASK;
  1640. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1641. }
  1642. val &= ~TRANS_INTERLACE_MASK;
  1643. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1644. if (HAS_PCH_IBX(dev_priv->dev) &&
  1645. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1646. val |= TRANS_LEGACY_INTERLACED_ILK;
  1647. else
  1648. val |= TRANS_INTERLACED;
  1649. else
  1650. val |= TRANS_PROGRESSIVE;
  1651. I915_WRITE(reg, val | TRANS_ENABLE);
  1652. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1653. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1654. }
  1655. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1656. enum transcoder cpu_transcoder)
  1657. {
  1658. u32 val, pipeconf_val;
  1659. /* PCH only available on ILK+ */
  1660. BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
  1661. /* FDI must be feeding us bits for PCH ports */
  1662. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1663. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1664. /* Workaround: set timing override bit. */
  1665. val = I915_READ(_TRANSA_CHICKEN2);
  1666. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1667. I915_WRITE(_TRANSA_CHICKEN2, val);
  1668. val = TRANS_ENABLE;
  1669. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1670. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1671. PIPECONF_INTERLACED_ILK)
  1672. val |= TRANS_INTERLACED;
  1673. else
  1674. val |= TRANS_PROGRESSIVE;
  1675. I915_WRITE(LPT_TRANSCONF, val);
  1676. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1677. DRM_ERROR("Failed to enable PCH transcoder\n");
  1678. }
  1679. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1680. enum pipe pipe)
  1681. {
  1682. struct drm_device *dev = dev_priv->dev;
  1683. uint32_t reg, val;
  1684. /* FDI relies on the transcoder */
  1685. assert_fdi_tx_disabled(dev_priv, pipe);
  1686. assert_fdi_rx_disabled(dev_priv, pipe);
  1687. /* Ports must be off as well */
  1688. assert_pch_ports_disabled(dev_priv, pipe);
  1689. reg = PCH_TRANSCONF(pipe);
  1690. val = I915_READ(reg);
  1691. val &= ~TRANS_ENABLE;
  1692. I915_WRITE(reg, val);
  1693. /* wait for PCH transcoder off, transcoder state */
  1694. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1695. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1696. if (!HAS_PCH_IBX(dev)) {
  1697. /* Workaround: Clear the timing override chicken bit again. */
  1698. reg = TRANS_CHICKEN2(pipe);
  1699. val = I915_READ(reg);
  1700. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1701. I915_WRITE(reg, val);
  1702. }
  1703. }
  1704. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1705. {
  1706. u32 val;
  1707. val = I915_READ(LPT_TRANSCONF);
  1708. val &= ~TRANS_ENABLE;
  1709. I915_WRITE(LPT_TRANSCONF, val);
  1710. /* wait for PCH transcoder off, transcoder state */
  1711. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1712. DRM_ERROR("Failed to disable PCH transcoder\n");
  1713. /* Workaround: clear timing override bit. */
  1714. val = I915_READ(_TRANSA_CHICKEN2);
  1715. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1716. I915_WRITE(_TRANSA_CHICKEN2, val);
  1717. }
  1718. /**
  1719. * intel_enable_pipe - enable a pipe, asserting requirements
  1720. * @crtc: crtc responsible for the pipe
  1721. *
  1722. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1723. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1724. */
  1725. static void intel_enable_pipe(struct intel_crtc *crtc)
  1726. {
  1727. struct drm_device *dev = crtc->base.dev;
  1728. struct drm_i915_private *dev_priv = dev->dev_private;
  1729. enum pipe pipe = crtc->pipe;
  1730. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1731. pipe);
  1732. enum pipe pch_transcoder;
  1733. int reg;
  1734. u32 val;
  1735. assert_planes_disabled(dev_priv, pipe);
  1736. assert_cursor_disabled(dev_priv, pipe);
  1737. assert_sprites_disabled(dev_priv, pipe);
  1738. if (HAS_PCH_LPT(dev_priv->dev))
  1739. pch_transcoder = TRANSCODER_A;
  1740. else
  1741. pch_transcoder = pipe;
  1742. /*
  1743. * A pipe without a PLL won't actually be able to drive bits from
  1744. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1745. * need the check.
  1746. */
  1747. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1748. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
  1749. assert_dsi_pll_enabled(dev_priv);
  1750. else
  1751. assert_pll_enabled(dev_priv, pipe);
  1752. else {
  1753. if (crtc->config.has_pch_encoder) {
  1754. /* if driving the PCH, we need FDI enabled */
  1755. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1756. assert_fdi_tx_pll_enabled(dev_priv,
  1757. (enum pipe) cpu_transcoder);
  1758. }
  1759. /* FIXME: assert CPU port conditions for SNB+ */
  1760. }
  1761. reg = PIPECONF(cpu_transcoder);
  1762. val = I915_READ(reg);
  1763. if (val & PIPECONF_ENABLE) {
  1764. WARN_ON(!(pipe == PIPE_A &&
  1765. dev_priv->quirks & QUIRK_PIPEA_FORCE));
  1766. return;
  1767. }
  1768. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1769. POSTING_READ(reg);
  1770. }
  1771. /**
  1772. * intel_disable_pipe - disable a pipe, asserting requirements
  1773. * @dev_priv: i915 private structure
  1774. * @pipe: pipe to disable
  1775. *
  1776. * Disable @pipe, making sure that various hardware specific requirements
  1777. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1778. *
  1779. * @pipe should be %PIPE_A or %PIPE_B.
  1780. *
  1781. * Will wait until the pipe has shut down before returning.
  1782. */
  1783. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1784. enum pipe pipe)
  1785. {
  1786. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1787. pipe);
  1788. int reg;
  1789. u32 val;
  1790. /*
  1791. * Make sure planes won't keep trying to pump pixels to us,
  1792. * or we might hang the display.
  1793. */
  1794. assert_planes_disabled(dev_priv, pipe);
  1795. assert_cursor_disabled(dev_priv, pipe);
  1796. assert_sprites_disabled(dev_priv, pipe);
  1797. /* Don't disable pipe A or pipe A PLLs if needed */
  1798. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1799. return;
  1800. reg = PIPECONF(cpu_transcoder);
  1801. val = I915_READ(reg);
  1802. if ((val & PIPECONF_ENABLE) == 0)
  1803. return;
  1804. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1805. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1806. }
  1807. /*
  1808. * Plane regs are double buffered, going from enabled->disabled needs a
  1809. * trigger in order to latch. The display address reg provides this.
  1810. */
  1811. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1812. enum plane plane)
  1813. {
  1814. struct drm_device *dev = dev_priv->dev;
  1815. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1816. I915_WRITE(reg, I915_READ(reg));
  1817. POSTING_READ(reg);
  1818. }
  1819. /**
  1820. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1821. * @dev_priv: i915 private structure
  1822. * @plane: plane to enable
  1823. * @pipe: pipe being fed
  1824. *
  1825. * Enable @plane on @pipe, making sure that @pipe is running first.
  1826. */
  1827. static void intel_enable_primary_hw_plane(struct drm_i915_private *dev_priv,
  1828. enum plane plane, enum pipe pipe)
  1829. {
  1830. struct drm_device *dev = dev_priv->dev;
  1831. struct intel_crtc *intel_crtc =
  1832. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1833. int reg;
  1834. u32 val;
  1835. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1836. assert_pipe_enabled(dev_priv, pipe);
  1837. if (intel_crtc->primary_enabled)
  1838. return;
  1839. intel_crtc->primary_enabled = true;
  1840. reg = DSPCNTR(plane);
  1841. val = I915_READ(reg);
  1842. WARN_ON(val & DISPLAY_PLANE_ENABLE);
  1843. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1844. intel_flush_primary_plane(dev_priv, plane);
  1845. /*
  1846. * BDW signals flip done immediately if the plane
  1847. * is disabled, even if the plane enable is already
  1848. * armed to occur at the next vblank :(
  1849. */
  1850. if (IS_BROADWELL(dev))
  1851. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1852. }
  1853. /**
  1854. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1855. * @dev_priv: i915 private structure
  1856. * @plane: plane to disable
  1857. * @pipe: pipe consuming the data
  1858. *
  1859. * Disable @plane; should be an independent operation.
  1860. */
  1861. static void intel_disable_primary_hw_plane(struct drm_i915_private *dev_priv,
  1862. enum plane plane, enum pipe pipe)
  1863. {
  1864. struct intel_crtc *intel_crtc =
  1865. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1866. int reg;
  1867. u32 val;
  1868. if (!intel_crtc->primary_enabled)
  1869. return;
  1870. intel_crtc->primary_enabled = false;
  1871. reg = DSPCNTR(plane);
  1872. val = I915_READ(reg);
  1873. WARN_ON((val & DISPLAY_PLANE_ENABLE) == 0);
  1874. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1875. intel_flush_primary_plane(dev_priv, plane);
  1876. }
  1877. static bool need_vtd_wa(struct drm_device *dev)
  1878. {
  1879. #ifdef CONFIG_INTEL_IOMMU
  1880. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1881. return true;
  1882. #endif
  1883. return false;
  1884. }
  1885. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1886. {
  1887. int tile_height;
  1888. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1889. return ALIGN(height, tile_height);
  1890. }
  1891. int
  1892. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1893. struct drm_i915_gem_object *obj,
  1894. struct intel_engine_cs *pipelined)
  1895. {
  1896. struct drm_i915_private *dev_priv = dev->dev_private;
  1897. u32 alignment;
  1898. int ret;
  1899. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1900. switch (obj->tiling_mode) {
  1901. case I915_TILING_NONE:
  1902. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1903. alignment = 128 * 1024;
  1904. else if (INTEL_INFO(dev)->gen >= 4)
  1905. alignment = 4 * 1024;
  1906. else
  1907. alignment = 64 * 1024;
  1908. break;
  1909. case I915_TILING_X:
  1910. /* pin() will align the object as required by fence */
  1911. alignment = 0;
  1912. break;
  1913. case I915_TILING_Y:
  1914. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1915. return -EINVAL;
  1916. default:
  1917. BUG();
  1918. }
  1919. /* Note that the w/a also requires 64 PTE of padding following the
  1920. * bo. We currently fill all unused PTE with the shadow page and so
  1921. * we should always have valid PTE following the scanout preventing
  1922. * the VT-d warning.
  1923. */
  1924. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1925. alignment = 256 * 1024;
  1926. /*
  1927. * Global gtt pte registers are special registers which actually forward
  1928. * writes to a chunk of system memory. Which means that there is no risk
  1929. * that the register values disappear as soon as we call
  1930. * intel_runtime_pm_put(), so it is correct to wrap only the
  1931. * pin/unpin/fence and not more.
  1932. */
  1933. intel_runtime_pm_get(dev_priv);
  1934. dev_priv->mm.interruptible = false;
  1935. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1936. if (ret)
  1937. goto err_interruptible;
  1938. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1939. * fence, whereas 965+ only requires a fence if using
  1940. * framebuffer compression. For simplicity, we always install
  1941. * a fence as the cost is not that onerous.
  1942. */
  1943. ret = i915_gem_object_get_fence(obj);
  1944. if (ret)
  1945. goto err_unpin;
  1946. i915_gem_object_pin_fence(obj);
  1947. dev_priv->mm.interruptible = true;
  1948. intel_runtime_pm_put(dev_priv);
  1949. return 0;
  1950. err_unpin:
  1951. i915_gem_object_unpin_from_display_plane(obj);
  1952. err_interruptible:
  1953. dev_priv->mm.interruptible = true;
  1954. intel_runtime_pm_put(dev_priv);
  1955. return ret;
  1956. }
  1957. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1958. {
  1959. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1960. i915_gem_object_unpin_fence(obj);
  1961. i915_gem_object_unpin_from_display_plane(obj);
  1962. }
  1963. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1964. * is assumed to be a power-of-two. */
  1965. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1966. unsigned int tiling_mode,
  1967. unsigned int cpp,
  1968. unsigned int pitch)
  1969. {
  1970. if (tiling_mode != I915_TILING_NONE) {
  1971. unsigned int tile_rows, tiles;
  1972. tile_rows = *y / 8;
  1973. *y %= 8;
  1974. tiles = *x / (512/cpp);
  1975. *x %= 512/cpp;
  1976. return tile_rows * pitch * 8 + tiles * 4096;
  1977. } else {
  1978. unsigned int offset;
  1979. offset = *y * pitch + *x * cpp;
  1980. *y = 0;
  1981. *x = (offset & 4095) / cpp;
  1982. return offset & -4096;
  1983. }
  1984. }
  1985. int intel_format_to_fourcc(int format)
  1986. {
  1987. switch (format) {
  1988. case DISPPLANE_8BPP:
  1989. return DRM_FORMAT_C8;
  1990. case DISPPLANE_BGRX555:
  1991. return DRM_FORMAT_XRGB1555;
  1992. case DISPPLANE_BGRX565:
  1993. return DRM_FORMAT_RGB565;
  1994. default:
  1995. case DISPPLANE_BGRX888:
  1996. return DRM_FORMAT_XRGB8888;
  1997. case DISPPLANE_RGBX888:
  1998. return DRM_FORMAT_XBGR8888;
  1999. case DISPPLANE_BGRX101010:
  2000. return DRM_FORMAT_XRGB2101010;
  2001. case DISPPLANE_RGBX101010:
  2002. return DRM_FORMAT_XBGR2101010;
  2003. }
  2004. }
  2005. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  2006. struct intel_plane_config *plane_config)
  2007. {
  2008. struct drm_device *dev = crtc->base.dev;
  2009. struct drm_i915_gem_object *obj = NULL;
  2010. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2011. u32 base = plane_config->base;
  2012. if (plane_config->size == 0)
  2013. return false;
  2014. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  2015. plane_config->size);
  2016. if (!obj)
  2017. return false;
  2018. if (plane_config->tiled) {
  2019. obj->tiling_mode = I915_TILING_X;
  2020. obj->stride = crtc->base.primary->fb->pitches[0];
  2021. }
  2022. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  2023. mode_cmd.width = crtc->base.primary->fb->width;
  2024. mode_cmd.height = crtc->base.primary->fb->height;
  2025. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  2026. mutex_lock(&dev->struct_mutex);
  2027. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  2028. &mode_cmd, obj)) {
  2029. DRM_DEBUG_KMS("intel fb init failed\n");
  2030. goto out_unref_obj;
  2031. }
  2032. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2033. mutex_unlock(&dev->struct_mutex);
  2034. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2035. return true;
  2036. out_unref_obj:
  2037. drm_gem_object_unreference(&obj->base);
  2038. mutex_unlock(&dev->struct_mutex);
  2039. return false;
  2040. }
  2041. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2042. struct intel_plane_config *plane_config)
  2043. {
  2044. struct drm_device *dev = intel_crtc->base.dev;
  2045. struct drm_crtc *c;
  2046. struct intel_crtc *i;
  2047. struct drm_i915_gem_object *obj;
  2048. if (!intel_crtc->base.primary->fb)
  2049. return;
  2050. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2051. return;
  2052. kfree(intel_crtc->base.primary->fb);
  2053. intel_crtc->base.primary->fb = NULL;
  2054. /*
  2055. * Failed to alloc the obj, check to see if we should share
  2056. * an fb with another CRTC instead
  2057. */
  2058. for_each_crtc(dev, c) {
  2059. i = to_intel_crtc(c);
  2060. if (c == &intel_crtc->base)
  2061. continue;
  2062. if (!i->active)
  2063. continue;
  2064. obj = intel_fb_obj(c->primary->fb);
  2065. if (obj == NULL)
  2066. continue;
  2067. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2068. drm_framebuffer_reference(c->primary->fb);
  2069. intel_crtc->base.primary->fb = c->primary->fb;
  2070. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2071. break;
  2072. }
  2073. }
  2074. }
  2075. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2076. struct drm_framebuffer *fb,
  2077. int x, int y)
  2078. {
  2079. struct drm_device *dev = crtc->dev;
  2080. struct drm_i915_private *dev_priv = dev->dev_private;
  2081. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2082. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2083. int plane = intel_crtc->plane;
  2084. unsigned long linear_offset;
  2085. u32 dspcntr;
  2086. u32 reg;
  2087. reg = DSPCNTR(plane);
  2088. dspcntr = I915_READ(reg);
  2089. /* Mask out pixel format bits in case we change it */
  2090. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  2091. switch (fb->pixel_format) {
  2092. case DRM_FORMAT_C8:
  2093. dspcntr |= DISPPLANE_8BPP;
  2094. break;
  2095. case DRM_FORMAT_XRGB1555:
  2096. case DRM_FORMAT_ARGB1555:
  2097. dspcntr |= DISPPLANE_BGRX555;
  2098. break;
  2099. case DRM_FORMAT_RGB565:
  2100. dspcntr |= DISPPLANE_BGRX565;
  2101. break;
  2102. case DRM_FORMAT_XRGB8888:
  2103. case DRM_FORMAT_ARGB8888:
  2104. dspcntr |= DISPPLANE_BGRX888;
  2105. break;
  2106. case DRM_FORMAT_XBGR8888:
  2107. case DRM_FORMAT_ABGR8888:
  2108. dspcntr |= DISPPLANE_RGBX888;
  2109. break;
  2110. case DRM_FORMAT_XRGB2101010:
  2111. case DRM_FORMAT_ARGB2101010:
  2112. dspcntr |= DISPPLANE_BGRX101010;
  2113. break;
  2114. case DRM_FORMAT_XBGR2101010:
  2115. case DRM_FORMAT_ABGR2101010:
  2116. dspcntr |= DISPPLANE_RGBX101010;
  2117. break;
  2118. default:
  2119. BUG();
  2120. }
  2121. if (INTEL_INFO(dev)->gen >= 4) {
  2122. if (obj->tiling_mode != I915_TILING_NONE)
  2123. dspcntr |= DISPPLANE_TILED;
  2124. else
  2125. dspcntr &= ~DISPPLANE_TILED;
  2126. }
  2127. if (IS_G4X(dev))
  2128. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2129. I915_WRITE(reg, dspcntr);
  2130. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  2131. if (INTEL_INFO(dev)->gen >= 4) {
  2132. intel_crtc->dspaddr_offset =
  2133. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2134. fb->bits_per_pixel / 8,
  2135. fb->pitches[0]);
  2136. linear_offset -= intel_crtc->dspaddr_offset;
  2137. } else {
  2138. intel_crtc->dspaddr_offset = linear_offset;
  2139. }
  2140. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2141. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2142. fb->pitches[0]);
  2143. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2144. if (INTEL_INFO(dev)->gen >= 4) {
  2145. I915_WRITE(DSPSURF(plane),
  2146. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2147. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2148. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2149. } else
  2150. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2151. POSTING_READ(reg);
  2152. }
  2153. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2154. struct drm_framebuffer *fb,
  2155. int x, int y)
  2156. {
  2157. struct drm_device *dev = crtc->dev;
  2158. struct drm_i915_private *dev_priv = dev->dev_private;
  2159. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2160. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2161. int plane = intel_crtc->plane;
  2162. unsigned long linear_offset;
  2163. u32 dspcntr;
  2164. u32 reg;
  2165. reg = DSPCNTR(plane);
  2166. dspcntr = I915_READ(reg);
  2167. /* Mask out pixel format bits in case we change it */
  2168. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  2169. switch (fb->pixel_format) {
  2170. case DRM_FORMAT_C8:
  2171. dspcntr |= DISPPLANE_8BPP;
  2172. break;
  2173. case DRM_FORMAT_RGB565:
  2174. dspcntr |= DISPPLANE_BGRX565;
  2175. break;
  2176. case DRM_FORMAT_XRGB8888:
  2177. case DRM_FORMAT_ARGB8888:
  2178. dspcntr |= DISPPLANE_BGRX888;
  2179. break;
  2180. case DRM_FORMAT_XBGR8888:
  2181. case DRM_FORMAT_ABGR8888:
  2182. dspcntr |= DISPPLANE_RGBX888;
  2183. break;
  2184. case DRM_FORMAT_XRGB2101010:
  2185. case DRM_FORMAT_ARGB2101010:
  2186. dspcntr |= DISPPLANE_BGRX101010;
  2187. break;
  2188. case DRM_FORMAT_XBGR2101010:
  2189. case DRM_FORMAT_ABGR2101010:
  2190. dspcntr |= DISPPLANE_RGBX101010;
  2191. break;
  2192. default:
  2193. BUG();
  2194. }
  2195. if (obj->tiling_mode != I915_TILING_NONE)
  2196. dspcntr |= DISPPLANE_TILED;
  2197. else
  2198. dspcntr &= ~DISPPLANE_TILED;
  2199. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2200. dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
  2201. else
  2202. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2203. I915_WRITE(reg, dspcntr);
  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. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2211. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2212. fb->pitches[0]);
  2213. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2214. I915_WRITE(DSPSURF(plane),
  2215. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2216. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2217. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2218. } else {
  2219. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2220. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2221. }
  2222. POSTING_READ(reg);
  2223. }
  2224. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2225. static int
  2226. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2227. int x, int y, enum mode_set_atomic state)
  2228. {
  2229. struct drm_device *dev = crtc->dev;
  2230. struct drm_i915_private *dev_priv = dev->dev_private;
  2231. if (dev_priv->display.disable_fbc)
  2232. dev_priv->display.disable_fbc(dev);
  2233. intel_increase_pllclock(dev, to_intel_crtc(crtc)->pipe);
  2234. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2235. return 0;
  2236. }
  2237. void intel_display_handle_reset(struct drm_device *dev)
  2238. {
  2239. struct drm_i915_private *dev_priv = dev->dev_private;
  2240. struct drm_crtc *crtc;
  2241. /*
  2242. * Flips in the rings have been nuked by the reset,
  2243. * so complete all pending flips so that user space
  2244. * will get its events and not get stuck.
  2245. *
  2246. * Also update the base address of all primary
  2247. * planes to the the last fb to make sure we're
  2248. * showing the correct fb after a reset.
  2249. *
  2250. * Need to make two loops over the crtcs so that we
  2251. * don't try to grab a crtc mutex before the
  2252. * pending_flip_queue really got woken up.
  2253. */
  2254. for_each_crtc(dev, crtc) {
  2255. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2256. enum plane plane = intel_crtc->plane;
  2257. intel_prepare_page_flip(dev, plane);
  2258. intel_finish_page_flip_plane(dev, plane);
  2259. }
  2260. for_each_crtc(dev, crtc) {
  2261. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2262. drm_modeset_lock(&crtc->mutex, NULL);
  2263. /*
  2264. * FIXME: Once we have proper support for primary planes (and
  2265. * disabling them without disabling the entire crtc) allow again
  2266. * a NULL crtc->primary->fb.
  2267. */
  2268. if (intel_crtc->active && crtc->primary->fb)
  2269. dev_priv->display.update_primary_plane(crtc,
  2270. crtc->primary->fb,
  2271. crtc->x,
  2272. crtc->y);
  2273. drm_modeset_unlock(&crtc->mutex);
  2274. }
  2275. }
  2276. static int
  2277. intel_finish_fb(struct drm_framebuffer *old_fb)
  2278. {
  2279. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2280. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2281. bool was_interruptible = dev_priv->mm.interruptible;
  2282. int ret;
  2283. /* Big Hammer, we also need to ensure that any pending
  2284. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2285. * current scanout is retired before unpinning the old
  2286. * framebuffer.
  2287. *
  2288. * This should only fail upon a hung GPU, in which case we
  2289. * can safely continue.
  2290. */
  2291. dev_priv->mm.interruptible = false;
  2292. ret = i915_gem_object_finish_gpu(obj);
  2293. dev_priv->mm.interruptible = was_interruptible;
  2294. return ret;
  2295. }
  2296. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2297. {
  2298. struct drm_device *dev = crtc->dev;
  2299. struct drm_i915_private *dev_priv = dev->dev_private;
  2300. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2301. unsigned long flags;
  2302. bool pending;
  2303. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2304. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2305. return false;
  2306. spin_lock_irqsave(&dev->event_lock, flags);
  2307. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2308. spin_unlock_irqrestore(&dev->event_lock, flags);
  2309. return pending;
  2310. }
  2311. static int
  2312. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  2313. struct drm_framebuffer *fb)
  2314. {
  2315. struct drm_device *dev = crtc->dev;
  2316. struct drm_i915_private *dev_priv = dev->dev_private;
  2317. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2318. enum pipe pipe = intel_crtc->pipe;
  2319. struct drm_framebuffer *old_fb = crtc->primary->fb;
  2320. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2321. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
  2322. int ret;
  2323. if (intel_crtc_has_pending_flip(crtc)) {
  2324. DRM_ERROR("pipe is still busy with an old pageflip\n");
  2325. return -EBUSY;
  2326. }
  2327. /* no fb bound */
  2328. if (!fb) {
  2329. DRM_ERROR("No FB bound\n");
  2330. return 0;
  2331. }
  2332. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  2333. DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
  2334. plane_name(intel_crtc->plane),
  2335. INTEL_INFO(dev)->num_pipes);
  2336. return -EINVAL;
  2337. }
  2338. mutex_lock(&dev->struct_mutex);
  2339. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  2340. if (ret == 0)
  2341. i915_gem_track_fb(old_obj, obj,
  2342. INTEL_FRONTBUFFER_PRIMARY(pipe));
  2343. mutex_unlock(&dev->struct_mutex);
  2344. if (ret != 0) {
  2345. DRM_ERROR("pin & fence failed\n");
  2346. return ret;
  2347. }
  2348. /*
  2349. * Update pipe size and adjust fitter if needed: the reason for this is
  2350. * that in compute_mode_changes we check the native mode (not the pfit
  2351. * mode) to see if we can flip rather than do a full mode set. In the
  2352. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2353. * pfit state, we'll end up with a big fb scanned out into the wrong
  2354. * sized surface.
  2355. *
  2356. * To fix this properly, we need to hoist the checks up into
  2357. * compute_mode_changes (or above), check the actual pfit state and
  2358. * whether the platform allows pfit disable with pipe active, and only
  2359. * then update the pipesrc and pfit state, even on the flip path.
  2360. */
  2361. if (i915.fastboot) {
  2362. const struct drm_display_mode *adjusted_mode =
  2363. &intel_crtc->config.adjusted_mode;
  2364. I915_WRITE(PIPESRC(intel_crtc->pipe),
  2365. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2366. (adjusted_mode->crtc_vdisplay - 1));
  2367. if (!intel_crtc->config.pch_pfit.enabled &&
  2368. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2369. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2370. I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
  2371. I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
  2372. I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
  2373. }
  2374. intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2375. intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2376. }
  2377. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2378. if (intel_crtc->active)
  2379. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  2380. crtc->primary->fb = fb;
  2381. crtc->x = x;
  2382. crtc->y = y;
  2383. if (old_fb) {
  2384. if (intel_crtc->active && old_fb != fb)
  2385. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2386. mutex_lock(&dev->struct_mutex);
  2387. intel_unpin_fb_obj(old_obj);
  2388. mutex_unlock(&dev->struct_mutex);
  2389. }
  2390. mutex_lock(&dev->struct_mutex);
  2391. intel_update_fbc(dev);
  2392. mutex_unlock(&dev->struct_mutex);
  2393. return 0;
  2394. }
  2395. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2396. {
  2397. struct drm_device *dev = crtc->dev;
  2398. struct drm_i915_private *dev_priv = dev->dev_private;
  2399. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2400. int pipe = intel_crtc->pipe;
  2401. u32 reg, temp;
  2402. /* enable normal train */
  2403. reg = FDI_TX_CTL(pipe);
  2404. temp = I915_READ(reg);
  2405. if (IS_IVYBRIDGE(dev)) {
  2406. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2407. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2408. } else {
  2409. temp &= ~FDI_LINK_TRAIN_NONE;
  2410. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2411. }
  2412. I915_WRITE(reg, temp);
  2413. reg = FDI_RX_CTL(pipe);
  2414. temp = I915_READ(reg);
  2415. if (HAS_PCH_CPT(dev)) {
  2416. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2417. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2418. } else {
  2419. temp &= ~FDI_LINK_TRAIN_NONE;
  2420. temp |= FDI_LINK_TRAIN_NONE;
  2421. }
  2422. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2423. /* wait one idle pattern time */
  2424. POSTING_READ(reg);
  2425. udelay(1000);
  2426. /* IVB wants error correction enabled */
  2427. if (IS_IVYBRIDGE(dev))
  2428. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2429. FDI_FE_ERRC_ENABLE);
  2430. }
  2431. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2432. {
  2433. return crtc->base.enabled && crtc->active &&
  2434. crtc->config.has_pch_encoder;
  2435. }
  2436. static void ivb_modeset_global_resources(struct drm_device *dev)
  2437. {
  2438. struct drm_i915_private *dev_priv = dev->dev_private;
  2439. struct intel_crtc *pipe_B_crtc =
  2440. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2441. struct intel_crtc *pipe_C_crtc =
  2442. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2443. uint32_t temp;
  2444. /*
  2445. * When everything is off disable fdi C so that we could enable fdi B
  2446. * with all lanes. Note that we don't care about enabled pipes without
  2447. * an enabled pch encoder.
  2448. */
  2449. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2450. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2451. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2452. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2453. temp = I915_READ(SOUTH_CHICKEN1);
  2454. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2455. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2456. I915_WRITE(SOUTH_CHICKEN1, temp);
  2457. }
  2458. }
  2459. /* The FDI link training functions for ILK/Ibexpeak. */
  2460. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2461. {
  2462. struct drm_device *dev = crtc->dev;
  2463. struct drm_i915_private *dev_priv = dev->dev_private;
  2464. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2465. int pipe = intel_crtc->pipe;
  2466. u32 reg, temp, tries;
  2467. /* FDI needs bits from pipe first */
  2468. assert_pipe_enabled(dev_priv, pipe);
  2469. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2470. for train result */
  2471. reg = FDI_RX_IMR(pipe);
  2472. temp = I915_READ(reg);
  2473. temp &= ~FDI_RX_SYMBOL_LOCK;
  2474. temp &= ~FDI_RX_BIT_LOCK;
  2475. I915_WRITE(reg, temp);
  2476. I915_READ(reg);
  2477. udelay(150);
  2478. /* enable CPU FDI TX and PCH FDI RX */
  2479. reg = FDI_TX_CTL(pipe);
  2480. temp = I915_READ(reg);
  2481. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2482. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2483. temp &= ~FDI_LINK_TRAIN_NONE;
  2484. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2485. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2486. reg = FDI_RX_CTL(pipe);
  2487. temp = I915_READ(reg);
  2488. temp &= ~FDI_LINK_TRAIN_NONE;
  2489. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2490. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2491. POSTING_READ(reg);
  2492. udelay(150);
  2493. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2494. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2495. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2496. FDI_RX_PHASE_SYNC_POINTER_EN);
  2497. reg = FDI_RX_IIR(pipe);
  2498. for (tries = 0; tries < 5; tries++) {
  2499. temp = I915_READ(reg);
  2500. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2501. if ((temp & FDI_RX_BIT_LOCK)) {
  2502. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2503. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2504. break;
  2505. }
  2506. }
  2507. if (tries == 5)
  2508. DRM_ERROR("FDI train 1 fail!\n");
  2509. /* Train 2 */
  2510. reg = FDI_TX_CTL(pipe);
  2511. temp = I915_READ(reg);
  2512. temp &= ~FDI_LINK_TRAIN_NONE;
  2513. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2514. I915_WRITE(reg, temp);
  2515. reg = FDI_RX_CTL(pipe);
  2516. temp = I915_READ(reg);
  2517. temp &= ~FDI_LINK_TRAIN_NONE;
  2518. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2519. I915_WRITE(reg, temp);
  2520. POSTING_READ(reg);
  2521. udelay(150);
  2522. reg = FDI_RX_IIR(pipe);
  2523. for (tries = 0; tries < 5; tries++) {
  2524. temp = I915_READ(reg);
  2525. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2526. if (temp & FDI_RX_SYMBOL_LOCK) {
  2527. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2528. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2529. break;
  2530. }
  2531. }
  2532. if (tries == 5)
  2533. DRM_ERROR("FDI train 2 fail!\n");
  2534. DRM_DEBUG_KMS("FDI train done\n");
  2535. }
  2536. static const int snb_b_fdi_train_param[] = {
  2537. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2538. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2539. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2540. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2541. };
  2542. /* The FDI link training functions for SNB/Cougarpoint. */
  2543. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2544. {
  2545. struct drm_device *dev = crtc->dev;
  2546. struct drm_i915_private *dev_priv = dev->dev_private;
  2547. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2548. int pipe = intel_crtc->pipe;
  2549. u32 reg, temp, i, retry;
  2550. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2551. for train result */
  2552. reg = FDI_RX_IMR(pipe);
  2553. temp = I915_READ(reg);
  2554. temp &= ~FDI_RX_SYMBOL_LOCK;
  2555. temp &= ~FDI_RX_BIT_LOCK;
  2556. I915_WRITE(reg, temp);
  2557. POSTING_READ(reg);
  2558. udelay(150);
  2559. /* enable CPU FDI TX and PCH FDI RX */
  2560. reg = FDI_TX_CTL(pipe);
  2561. temp = I915_READ(reg);
  2562. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2563. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2564. temp &= ~FDI_LINK_TRAIN_NONE;
  2565. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2566. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2567. /* SNB-B */
  2568. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2569. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2570. I915_WRITE(FDI_RX_MISC(pipe),
  2571. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2572. reg = FDI_RX_CTL(pipe);
  2573. temp = I915_READ(reg);
  2574. if (HAS_PCH_CPT(dev)) {
  2575. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2576. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2577. } else {
  2578. temp &= ~FDI_LINK_TRAIN_NONE;
  2579. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2580. }
  2581. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2582. POSTING_READ(reg);
  2583. udelay(150);
  2584. for (i = 0; i < 4; i++) {
  2585. reg = FDI_TX_CTL(pipe);
  2586. temp = I915_READ(reg);
  2587. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2588. temp |= snb_b_fdi_train_param[i];
  2589. I915_WRITE(reg, temp);
  2590. POSTING_READ(reg);
  2591. udelay(500);
  2592. for (retry = 0; retry < 5; retry++) {
  2593. reg = FDI_RX_IIR(pipe);
  2594. temp = I915_READ(reg);
  2595. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2596. if (temp & FDI_RX_BIT_LOCK) {
  2597. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2598. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2599. break;
  2600. }
  2601. udelay(50);
  2602. }
  2603. if (retry < 5)
  2604. break;
  2605. }
  2606. if (i == 4)
  2607. DRM_ERROR("FDI train 1 fail!\n");
  2608. /* Train 2 */
  2609. reg = FDI_TX_CTL(pipe);
  2610. temp = I915_READ(reg);
  2611. temp &= ~FDI_LINK_TRAIN_NONE;
  2612. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2613. if (IS_GEN6(dev)) {
  2614. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2615. /* SNB-B */
  2616. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2617. }
  2618. I915_WRITE(reg, temp);
  2619. reg = FDI_RX_CTL(pipe);
  2620. temp = I915_READ(reg);
  2621. if (HAS_PCH_CPT(dev)) {
  2622. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2623. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2624. } else {
  2625. temp &= ~FDI_LINK_TRAIN_NONE;
  2626. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2627. }
  2628. I915_WRITE(reg, temp);
  2629. POSTING_READ(reg);
  2630. udelay(150);
  2631. for (i = 0; i < 4; i++) {
  2632. reg = FDI_TX_CTL(pipe);
  2633. temp = I915_READ(reg);
  2634. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2635. temp |= snb_b_fdi_train_param[i];
  2636. I915_WRITE(reg, temp);
  2637. POSTING_READ(reg);
  2638. udelay(500);
  2639. for (retry = 0; retry < 5; retry++) {
  2640. reg = FDI_RX_IIR(pipe);
  2641. temp = I915_READ(reg);
  2642. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2643. if (temp & FDI_RX_SYMBOL_LOCK) {
  2644. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2645. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2646. break;
  2647. }
  2648. udelay(50);
  2649. }
  2650. if (retry < 5)
  2651. break;
  2652. }
  2653. if (i == 4)
  2654. DRM_ERROR("FDI train 2 fail!\n");
  2655. DRM_DEBUG_KMS("FDI train done.\n");
  2656. }
  2657. /* Manual link training for Ivy Bridge A0 parts */
  2658. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2659. {
  2660. struct drm_device *dev = crtc->dev;
  2661. struct drm_i915_private *dev_priv = dev->dev_private;
  2662. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2663. int pipe = intel_crtc->pipe;
  2664. u32 reg, temp, i, j;
  2665. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2666. for train result */
  2667. reg = FDI_RX_IMR(pipe);
  2668. temp = I915_READ(reg);
  2669. temp &= ~FDI_RX_SYMBOL_LOCK;
  2670. temp &= ~FDI_RX_BIT_LOCK;
  2671. I915_WRITE(reg, temp);
  2672. POSTING_READ(reg);
  2673. udelay(150);
  2674. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2675. I915_READ(FDI_RX_IIR(pipe)));
  2676. /* Try each vswing and preemphasis setting twice before moving on */
  2677. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2678. /* disable first in case we need to retry */
  2679. reg = FDI_TX_CTL(pipe);
  2680. temp = I915_READ(reg);
  2681. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2682. temp &= ~FDI_TX_ENABLE;
  2683. I915_WRITE(reg, temp);
  2684. reg = FDI_RX_CTL(pipe);
  2685. temp = I915_READ(reg);
  2686. temp &= ~FDI_LINK_TRAIN_AUTO;
  2687. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2688. temp &= ~FDI_RX_ENABLE;
  2689. I915_WRITE(reg, temp);
  2690. /* enable CPU FDI TX and PCH FDI RX */
  2691. reg = FDI_TX_CTL(pipe);
  2692. temp = I915_READ(reg);
  2693. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2694. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2695. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2696. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2697. temp |= snb_b_fdi_train_param[j/2];
  2698. temp |= FDI_COMPOSITE_SYNC;
  2699. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2700. I915_WRITE(FDI_RX_MISC(pipe),
  2701. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2702. reg = FDI_RX_CTL(pipe);
  2703. temp = I915_READ(reg);
  2704. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2705. temp |= FDI_COMPOSITE_SYNC;
  2706. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2707. POSTING_READ(reg);
  2708. udelay(1); /* should be 0.5us */
  2709. for (i = 0; i < 4; i++) {
  2710. reg = FDI_RX_IIR(pipe);
  2711. temp = I915_READ(reg);
  2712. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2713. if (temp & FDI_RX_BIT_LOCK ||
  2714. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2715. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2716. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2717. i);
  2718. break;
  2719. }
  2720. udelay(1); /* should be 0.5us */
  2721. }
  2722. if (i == 4) {
  2723. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2724. continue;
  2725. }
  2726. /* Train 2 */
  2727. reg = FDI_TX_CTL(pipe);
  2728. temp = I915_READ(reg);
  2729. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2730. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2731. I915_WRITE(reg, temp);
  2732. reg = FDI_RX_CTL(pipe);
  2733. temp = I915_READ(reg);
  2734. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2735. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2736. I915_WRITE(reg, temp);
  2737. POSTING_READ(reg);
  2738. udelay(2); /* should be 1.5us */
  2739. for (i = 0; i < 4; i++) {
  2740. reg = FDI_RX_IIR(pipe);
  2741. temp = I915_READ(reg);
  2742. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2743. if (temp & FDI_RX_SYMBOL_LOCK ||
  2744. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2745. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2746. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2747. i);
  2748. goto train_done;
  2749. }
  2750. udelay(2); /* should be 1.5us */
  2751. }
  2752. if (i == 4)
  2753. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2754. }
  2755. train_done:
  2756. DRM_DEBUG_KMS("FDI train done.\n");
  2757. }
  2758. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2759. {
  2760. struct drm_device *dev = intel_crtc->base.dev;
  2761. struct drm_i915_private *dev_priv = dev->dev_private;
  2762. int pipe = intel_crtc->pipe;
  2763. u32 reg, temp;
  2764. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2765. reg = FDI_RX_CTL(pipe);
  2766. temp = I915_READ(reg);
  2767. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2768. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2769. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2770. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2771. POSTING_READ(reg);
  2772. udelay(200);
  2773. /* Switch from Rawclk to PCDclk */
  2774. temp = I915_READ(reg);
  2775. I915_WRITE(reg, temp | FDI_PCDCLK);
  2776. POSTING_READ(reg);
  2777. udelay(200);
  2778. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2779. reg = FDI_TX_CTL(pipe);
  2780. temp = I915_READ(reg);
  2781. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2782. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2783. POSTING_READ(reg);
  2784. udelay(100);
  2785. }
  2786. }
  2787. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2788. {
  2789. struct drm_device *dev = intel_crtc->base.dev;
  2790. struct drm_i915_private *dev_priv = dev->dev_private;
  2791. int pipe = intel_crtc->pipe;
  2792. u32 reg, temp;
  2793. /* Switch from PCDclk to Rawclk */
  2794. reg = FDI_RX_CTL(pipe);
  2795. temp = I915_READ(reg);
  2796. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2797. /* Disable CPU FDI TX PLL */
  2798. reg = FDI_TX_CTL(pipe);
  2799. temp = I915_READ(reg);
  2800. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2801. POSTING_READ(reg);
  2802. udelay(100);
  2803. reg = FDI_RX_CTL(pipe);
  2804. temp = I915_READ(reg);
  2805. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2806. /* Wait for the clocks to turn off. */
  2807. POSTING_READ(reg);
  2808. udelay(100);
  2809. }
  2810. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2811. {
  2812. struct drm_device *dev = crtc->dev;
  2813. struct drm_i915_private *dev_priv = dev->dev_private;
  2814. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2815. int pipe = intel_crtc->pipe;
  2816. u32 reg, temp;
  2817. /* disable CPU FDI tx and PCH FDI rx */
  2818. reg = FDI_TX_CTL(pipe);
  2819. temp = I915_READ(reg);
  2820. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2821. POSTING_READ(reg);
  2822. reg = FDI_RX_CTL(pipe);
  2823. temp = I915_READ(reg);
  2824. temp &= ~(0x7 << 16);
  2825. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2826. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2827. POSTING_READ(reg);
  2828. udelay(100);
  2829. /* Ironlake workaround, disable clock pointer after downing FDI */
  2830. if (HAS_PCH_IBX(dev))
  2831. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2832. /* still set train pattern 1 */
  2833. reg = FDI_TX_CTL(pipe);
  2834. temp = I915_READ(reg);
  2835. temp &= ~FDI_LINK_TRAIN_NONE;
  2836. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2837. I915_WRITE(reg, temp);
  2838. reg = FDI_RX_CTL(pipe);
  2839. temp = I915_READ(reg);
  2840. if (HAS_PCH_CPT(dev)) {
  2841. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2842. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2843. } else {
  2844. temp &= ~FDI_LINK_TRAIN_NONE;
  2845. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2846. }
  2847. /* BPC in FDI rx is consistent with that in PIPECONF */
  2848. temp &= ~(0x07 << 16);
  2849. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2850. I915_WRITE(reg, temp);
  2851. POSTING_READ(reg);
  2852. udelay(100);
  2853. }
  2854. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  2855. {
  2856. struct intel_crtc *crtc;
  2857. /* Note that we don't need to be called with mode_config.lock here
  2858. * as our list of CRTC objects is static for the lifetime of the
  2859. * device and so cannot disappear as we iterate. Similarly, we can
  2860. * happily treat the predicates as racy, atomic checks as userspace
  2861. * cannot claim and pin a new fb without at least acquring the
  2862. * struct_mutex and so serialising with us.
  2863. */
  2864. for_each_intel_crtc(dev, crtc) {
  2865. if (atomic_read(&crtc->unpin_work_count) == 0)
  2866. continue;
  2867. if (crtc->unpin_work)
  2868. intel_wait_for_vblank(dev, crtc->pipe);
  2869. return true;
  2870. }
  2871. return false;
  2872. }
  2873. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2874. {
  2875. struct drm_device *dev = crtc->dev;
  2876. struct drm_i915_private *dev_priv = dev->dev_private;
  2877. if (crtc->primary->fb == NULL)
  2878. return;
  2879. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  2880. WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  2881. !intel_crtc_has_pending_flip(crtc),
  2882. 60*HZ) == 0);
  2883. mutex_lock(&dev->struct_mutex);
  2884. intel_finish_fb(crtc->primary->fb);
  2885. mutex_unlock(&dev->struct_mutex);
  2886. }
  2887. /* Program iCLKIP clock to the desired frequency */
  2888. static void lpt_program_iclkip(struct drm_crtc *crtc)
  2889. {
  2890. struct drm_device *dev = crtc->dev;
  2891. struct drm_i915_private *dev_priv = dev->dev_private;
  2892. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  2893. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  2894. u32 temp;
  2895. mutex_lock(&dev_priv->dpio_lock);
  2896. /* It is necessary to ungate the pixclk gate prior to programming
  2897. * the divisors, and gate it back when it is done.
  2898. */
  2899. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  2900. /* Disable SSCCTL */
  2901. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  2902. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  2903. SBI_SSCCTL_DISABLE,
  2904. SBI_ICLK);
  2905. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  2906. if (clock == 20000) {
  2907. auxdiv = 1;
  2908. divsel = 0x41;
  2909. phaseinc = 0x20;
  2910. } else {
  2911. /* The iCLK virtual clock root frequency is in MHz,
  2912. * but the adjusted_mode->crtc_clock in in KHz. To get the
  2913. * divisors, it is necessary to divide one by another, so we
  2914. * convert the virtual clock precision to KHz here for higher
  2915. * precision.
  2916. */
  2917. u32 iclk_virtual_root_freq = 172800 * 1000;
  2918. u32 iclk_pi_range = 64;
  2919. u32 desired_divisor, msb_divisor_value, pi_value;
  2920. desired_divisor = (iclk_virtual_root_freq / clock);
  2921. msb_divisor_value = desired_divisor / iclk_pi_range;
  2922. pi_value = desired_divisor % iclk_pi_range;
  2923. auxdiv = 0;
  2924. divsel = msb_divisor_value - 2;
  2925. phaseinc = pi_value;
  2926. }
  2927. /* This should not happen with any sane values */
  2928. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  2929. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  2930. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  2931. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  2932. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  2933. clock,
  2934. auxdiv,
  2935. divsel,
  2936. phasedir,
  2937. phaseinc);
  2938. /* Program SSCDIVINTPHASE6 */
  2939. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  2940. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  2941. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  2942. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  2943. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  2944. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  2945. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  2946. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  2947. /* Program SSCAUXDIV */
  2948. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  2949. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  2950. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  2951. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  2952. /* Enable modulator and associated divider */
  2953. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  2954. temp &= ~SBI_SSCCTL_DISABLE;
  2955. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  2956. /* Wait for initialization time */
  2957. udelay(24);
  2958. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  2959. mutex_unlock(&dev_priv->dpio_lock);
  2960. }
  2961. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  2962. enum pipe pch_transcoder)
  2963. {
  2964. struct drm_device *dev = crtc->base.dev;
  2965. struct drm_i915_private *dev_priv = dev->dev_private;
  2966. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  2967. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  2968. I915_READ(HTOTAL(cpu_transcoder)));
  2969. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  2970. I915_READ(HBLANK(cpu_transcoder)));
  2971. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  2972. I915_READ(HSYNC(cpu_transcoder)));
  2973. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  2974. I915_READ(VTOTAL(cpu_transcoder)));
  2975. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  2976. I915_READ(VBLANK(cpu_transcoder)));
  2977. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  2978. I915_READ(VSYNC(cpu_transcoder)));
  2979. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  2980. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  2981. }
  2982. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  2983. {
  2984. struct drm_i915_private *dev_priv = dev->dev_private;
  2985. uint32_t temp;
  2986. temp = I915_READ(SOUTH_CHICKEN1);
  2987. if (temp & FDI_BC_BIFURCATION_SELECT)
  2988. return;
  2989. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2990. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2991. temp |= FDI_BC_BIFURCATION_SELECT;
  2992. DRM_DEBUG_KMS("enabling fdi C rx\n");
  2993. I915_WRITE(SOUTH_CHICKEN1, temp);
  2994. POSTING_READ(SOUTH_CHICKEN1);
  2995. }
  2996. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  2997. {
  2998. struct drm_device *dev = intel_crtc->base.dev;
  2999. struct drm_i915_private *dev_priv = dev->dev_private;
  3000. switch (intel_crtc->pipe) {
  3001. case PIPE_A:
  3002. break;
  3003. case PIPE_B:
  3004. if (intel_crtc->config.fdi_lanes > 2)
  3005. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  3006. else
  3007. cpt_enable_fdi_bc_bifurcation(dev);
  3008. break;
  3009. case PIPE_C:
  3010. cpt_enable_fdi_bc_bifurcation(dev);
  3011. break;
  3012. default:
  3013. BUG();
  3014. }
  3015. }
  3016. /*
  3017. * Enable PCH resources required for PCH ports:
  3018. * - PCH PLLs
  3019. * - FDI training & RX/TX
  3020. * - update transcoder timings
  3021. * - DP transcoding bits
  3022. * - transcoder
  3023. */
  3024. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3025. {
  3026. struct drm_device *dev = crtc->dev;
  3027. struct drm_i915_private *dev_priv = dev->dev_private;
  3028. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3029. int pipe = intel_crtc->pipe;
  3030. u32 reg, temp;
  3031. assert_pch_transcoder_disabled(dev_priv, pipe);
  3032. if (IS_IVYBRIDGE(dev))
  3033. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3034. /* Write the TU size bits before fdi link training, so that error
  3035. * detection works. */
  3036. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3037. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3038. /* For PCH output, training FDI link */
  3039. dev_priv->display.fdi_link_train(crtc);
  3040. /* We need to program the right clock selection before writing the pixel
  3041. * mutliplier into the DPLL. */
  3042. if (HAS_PCH_CPT(dev)) {
  3043. u32 sel;
  3044. temp = I915_READ(PCH_DPLL_SEL);
  3045. temp |= TRANS_DPLL_ENABLE(pipe);
  3046. sel = TRANS_DPLLB_SEL(pipe);
  3047. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  3048. temp |= sel;
  3049. else
  3050. temp &= ~sel;
  3051. I915_WRITE(PCH_DPLL_SEL, temp);
  3052. }
  3053. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3054. * transcoder, and we actually should do this to not upset any PCH
  3055. * transcoder that already use the clock when we share it.
  3056. *
  3057. * Note that enable_shared_dpll tries to do the right thing, but
  3058. * get_shared_dpll unconditionally resets the pll - we need that to have
  3059. * the right LVDS enable sequence. */
  3060. intel_enable_shared_dpll(intel_crtc);
  3061. /* set transcoder timing, panel must allow it */
  3062. assert_panel_unlocked(dev_priv, pipe);
  3063. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3064. intel_fdi_normal_train(crtc);
  3065. /* For PCH DP, enable TRANS_DP_CTL */
  3066. if (HAS_PCH_CPT(dev) &&
  3067. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  3068. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  3069. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3070. reg = TRANS_DP_CTL(pipe);
  3071. temp = I915_READ(reg);
  3072. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3073. TRANS_DP_SYNC_MASK |
  3074. TRANS_DP_BPC_MASK);
  3075. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3076. TRANS_DP_ENH_FRAMING);
  3077. temp |= bpc << 9; /* same format but at 11:9 */
  3078. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3079. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3080. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3081. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3082. switch (intel_trans_dp_port_sel(crtc)) {
  3083. case PCH_DP_B:
  3084. temp |= TRANS_DP_PORT_SEL_B;
  3085. break;
  3086. case PCH_DP_C:
  3087. temp |= TRANS_DP_PORT_SEL_C;
  3088. break;
  3089. case PCH_DP_D:
  3090. temp |= TRANS_DP_PORT_SEL_D;
  3091. break;
  3092. default:
  3093. BUG();
  3094. }
  3095. I915_WRITE(reg, temp);
  3096. }
  3097. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3098. }
  3099. static void lpt_pch_enable(struct drm_crtc *crtc)
  3100. {
  3101. struct drm_device *dev = crtc->dev;
  3102. struct drm_i915_private *dev_priv = dev->dev_private;
  3103. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3104. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3105. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3106. lpt_program_iclkip(crtc);
  3107. /* Set transcoder timing. */
  3108. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3109. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3110. }
  3111. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3112. {
  3113. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3114. if (pll == NULL)
  3115. return;
  3116. if (pll->refcount == 0) {
  3117. WARN(1, "bad %s refcount\n", pll->name);
  3118. return;
  3119. }
  3120. if (--pll->refcount == 0) {
  3121. WARN_ON(pll->on);
  3122. WARN_ON(pll->active);
  3123. }
  3124. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3125. }
  3126. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3127. {
  3128. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3129. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3130. enum intel_dpll_id i;
  3131. if (pll) {
  3132. DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
  3133. crtc->base.base.id, pll->name);
  3134. intel_put_shared_dpll(crtc);
  3135. }
  3136. if (HAS_PCH_IBX(dev_priv->dev)) {
  3137. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3138. i = (enum intel_dpll_id) crtc->pipe;
  3139. pll = &dev_priv->shared_dplls[i];
  3140. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3141. crtc->base.base.id, pll->name);
  3142. WARN_ON(pll->refcount);
  3143. goto found;
  3144. }
  3145. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3146. pll = &dev_priv->shared_dplls[i];
  3147. /* Only want to check enabled timings first */
  3148. if (pll->refcount == 0)
  3149. continue;
  3150. if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
  3151. sizeof(pll->hw_state)) == 0) {
  3152. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
  3153. crtc->base.base.id,
  3154. pll->name, pll->refcount, pll->active);
  3155. goto found;
  3156. }
  3157. }
  3158. /* Ok no matching timings, maybe there's a free one? */
  3159. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3160. pll = &dev_priv->shared_dplls[i];
  3161. if (pll->refcount == 0) {
  3162. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3163. crtc->base.base.id, pll->name);
  3164. goto found;
  3165. }
  3166. }
  3167. return NULL;
  3168. found:
  3169. if (pll->refcount == 0)
  3170. pll->hw_state = crtc->config.dpll_hw_state;
  3171. crtc->config.shared_dpll = i;
  3172. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3173. pipe_name(crtc->pipe));
  3174. pll->refcount++;
  3175. return pll;
  3176. }
  3177. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3178. {
  3179. struct drm_i915_private *dev_priv = dev->dev_private;
  3180. int dslreg = PIPEDSL(pipe);
  3181. u32 temp;
  3182. temp = I915_READ(dslreg);
  3183. udelay(500);
  3184. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3185. if (wait_for(I915_READ(dslreg) != temp, 5))
  3186. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3187. }
  3188. }
  3189. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3190. {
  3191. struct drm_device *dev = crtc->base.dev;
  3192. struct drm_i915_private *dev_priv = dev->dev_private;
  3193. int pipe = crtc->pipe;
  3194. if (crtc->config.pch_pfit.enabled) {
  3195. /* Force use of hard-coded filter coefficients
  3196. * as some pre-programmed values are broken,
  3197. * e.g. x201.
  3198. */
  3199. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3200. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3201. PF_PIPE_SEL_IVB(pipe));
  3202. else
  3203. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3204. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3205. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3206. }
  3207. }
  3208. static void intel_enable_planes(struct drm_crtc *crtc)
  3209. {
  3210. struct drm_device *dev = crtc->dev;
  3211. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3212. struct drm_plane *plane;
  3213. struct intel_plane *intel_plane;
  3214. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3215. intel_plane = to_intel_plane(plane);
  3216. if (intel_plane->pipe == pipe)
  3217. intel_plane_restore(&intel_plane->base);
  3218. }
  3219. }
  3220. static void intel_disable_planes(struct drm_crtc *crtc)
  3221. {
  3222. struct drm_device *dev = crtc->dev;
  3223. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3224. struct drm_plane *plane;
  3225. struct intel_plane *intel_plane;
  3226. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3227. intel_plane = to_intel_plane(plane);
  3228. if (intel_plane->pipe == pipe)
  3229. intel_plane_disable(&intel_plane->base);
  3230. }
  3231. }
  3232. void hsw_enable_ips(struct intel_crtc *crtc)
  3233. {
  3234. struct drm_device *dev = crtc->base.dev;
  3235. struct drm_i915_private *dev_priv = dev->dev_private;
  3236. if (!crtc->config.ips_enabled)
  3237. return;
  3238. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3239. intel_wait_for_vblank(dev, crtc->pipe);
  3240. assert_plane_enabled(dev_priv, crtc->plane);
  3241. if (IS_BROADWELL(dev)) {
  3242. mutex_lock(&dev_priv->rps.hw_lock);
  3243. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3244. mutex_unlock(&dev_priv->rps.hw_lock);
  3245. /* Quoting Art Runyan: "its not safe to expect any particular
  3246. * value in IPS_CTL bit 31 after enabling IPS through the
  3247. * mailbox." Moreover, the mailbox may return a bogus state,
  3248. * so we need to just enable it and continue on.
  3249. */
  3250. } else {
  3251. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3252. /* The bit only becomes 1 in the next vblank, so this wait here
  3253. * is essentially intel_wait_for_vblank. If we don't have this
  3254. * and don't wait for vblanks until the end of crtc_enable, then
  3255. * the HW state readout code will complain that the expected
  3256. * IPS_CTL value is not the one we read. */
  3257. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3258. DRM_ERROR("Timed out waiting for IPS enable\n");
  3259. }
  3260. }
  3261. void hsw_disable_ips(struct intel_crtc *crtc)
  3262. {
  3263. struct drm_device *dev = crtc->base.dev;
  3264. struct drm_i915_private *dev_priv = dev->dev_private;
  3265. if (!crtc->config.ips_enabled)
  3266. return;
  3267. assert_plane_enabled(dev_priv, crtc->plane);
  3268. if (IS_BROADWELL(dev)) {
  3269. mutex_lock(&dev_priv->rps.hw_lock);
  3270. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3271. mutex_unlock(&dev_priv->rps.hw_lock);
  3272. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3273. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3274. DRM_ERROR("Timed out waiting for IPS disable\n");
  3275. } else {
  3276. I915_WRITE(IPS_CTL, 0);
  3277. POSTING_READ(IPS_CTL);
  3278. }
  3279. /* We need to wait for a vblank before we can disable the plane. */
  3280. intel_wait_for_vblank(dev, crtc->pipe);
  3281. }
  3282. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3283. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3284. {
  3285. struct drm_device *dev = crtc->dev;
  3286. struct drm_i915_private *dev_priv = dev->dev_private;
  3287. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3288. enum pipe pipe = intel_crtc->pipe;
  3289. int palreg = PALETTE(pipe);
  3290. int i;
  3291. bool reenable_ips = false;
  3292. /* The clocks have to be on to load the palette. */
  3293. if (!crtc->enabled || !intel_crtc->active)
  3294. return;
  3295. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3296. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  3297. assert_dsi_pll_enabled(dev_priv);
  3298. else
  3299. assert_pll_enabled(dev_priv, pipe);
  3300. }
  3301. /* use legacy palette for Ironlake */
  3302. if (!HAS_GMCH_DISPLAY(dev))
  3303. palreg = LGC_PALETTE(pipe);
  3304. /* Workaround : Do not read or write the pipe palette/gamma data while
  3305. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3306. */
  3307. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3308. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3309. GAMMA_MODE_MODE_SPLIT)) {
  3310. hsw_disable_ips(intel_crtc);
  3311. reenable_ips = true;
  3312. }
  3313. for (i = 0; i < 256; i++) {
  3314. I915_WRITE(palreg + 4 * i,
  3315. (intel_crtc->lut_r[i] << 16) |
  3316. (intel_crtc->lut_g[i] << 8) |
  3317. intel_crtc->lut_b[i]);
  3318. }
  3319. if (reenable_ips)
  3320. hsw_enable_ips(intel_crtc);
  3321. }
  3322. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3323. {
  3324. if (!enable && intel_crtc->overlay) {
  3325. struct drm_device *dev = intel_crtc->base.dev;
  3326. struct drm_i915_private *dev_priv = dev->dev_private;
  3327. mutex_lock(&dev->struct_mutex);
  3328. dev_priv->mm.interruptible = false;
  3329. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3330. dev_priv->mm.interruptible = true;
  3331. mutex_unlock(&dev->struct_mutex);
  3332. }
  3333. /* Let userspace switch the overlay on again. In most cases userspace
  3334. * has to recompute where to put it anyway.
  3335. */
  3336. }
  3337. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3338. {
  3339. struct drm_device *dev = crtc->dev;
  3340. struct drm_i915_private *dev_priv = dev->dev_private;
  3341. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3342. int pipe = intel_crtc->pipe;
  3343. int plane = intel_crtc->plane;
  3344. drm_vblank_on(dev, pipe);
  3345. intel_enable_primary_hw_plane(dev_priv, plane, pipe);
  3346. intel_enable_planes(crtc);
  3347. intel_crtc_update_cursor(crtc, true);
  3348. intel_crtc_dpms_overlay(intel_crtc, true);
  3349. hsw_enable_ips(intel_crtc);
  3350. mutex_lock(&dev->struct_mutex);
  3351. intel_update_fbc(dev);
  3352. mutex_unlock(&dev->struct_mutex);
  3353. /*
  3354. * FIXME: Once we grow proper nuclear flip support out of this we need
  3355. * to compute the mask of flip planes precisely. For the time being
  3356. * consider this a flip from a NULL plane.
  3357. */
  3358. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3359. }
  3360. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3361. {
  3362. struct drm_device *dev = crtc->dev;
  3363. struct drm_i915_private *dev_priv = dev->dev_private;
  3364. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3365. int pipe = intel_crtc->pipe;
  3366. int plane = intel_crtc->plane;
  3367. intel_crtc_wait_for_pending_flips(crtc);
  3368. if (dev_priv->fbc.plane == plane)
  3369. intel_disable_fbc(dev);
  3370. hsw_disable_ips(intel_crtc);
  3371. intel_crtc_dpms_overlay(intel_crtc, false);
  3372. intel_crtc_update_cursor(crtc, false);
  3373. intel_disable_planes(crtc);
  3374. intel_disable_primary_hw_plane(dev_priv, plane, pipe);
  3375. /*
  3376. * FIXME: Once we grow proper nuclear flip support out of this we need
  3377. * to compute the mask of flip planes precisely. For the time being
  3378. * consider this a flip to a NULL plane.
  3379. */
  3380. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3381. drm_vblank_off(dev, pipe);
  3382. }
  3383. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3384. {
  3385. struct drm_device *dev = crtc->dev;
  3386. struct drm_i915_private *dev_priv = dev->dev_private;
  3387. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3388. struct intel_encoder *encoder;
  3389. int pipe = intel_crtc->pipe;
  3390. enum plane plane = intel_crtc->plane;
  3391. WARN_ON(!crtc->enabled);
  3392. if (intel_crtc->active)
  3393. return;
  3394. if (intel_crtc->config.has_pch_encoder)
  3395. intel_prepare_shared_dpll(intel_crtc);
  3396. if (intel_crtc->config.has_dp_encoder)
  3397. intel_dp_set_m_n(intel_crtc);
  3398. intel_set_pipe_timings(intel_crtc);
  3399. if (intel_crtc->config.has_pch_encoder) {
  3400. intel_cpu_transcoder_set_m_n(intel_crtc,
  3401. &intel_crtc->config.fdi_m_n);
  3402. }
  3403. ironlake_set_pipeconf(crtc);
  3404. /* Set up the display plane register */
  3405. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
  3406. POSTING_READ(DSPCNTR(plane));
  3407. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3408. crtc->x, crtc->y);
  3409. intel_crtc->active = true;
  3410. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3411. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3412. for_each_encoder_on_crtc(dev, crtc, encoder)
  3413. if (encoder->pre_enable)
  3414. encoder->pre_enable(encoder);
  3415. if (intel_crtc->config.has_pch_encoder) {
  3416. /* Note: FDI PLL enabling _must_ be done before we enable the
  3417. * cpu pipes, hence this is separate from all the other fdi/pch
  3418. * enabling. */
  3419. ironlake_fdi_pll_enable(intel_crtc);
  3420. } else {
  3421. assert_fdi_tx_disabled(dev_priv, pipe);
  3422. assert_fdi_rx_disabled(dev_priv, pipe);
  3423. }
  3424. ironlake_pfit_enable(intel_crtc);
  3425. /*
  3426. * On ILK+ LUT must be loaded before the pipe is running but with
  3427. * clocks enabled
  3428. */
  3429. intel_crtc_load_lut(crtc);
  3430. intel_update_watermarks(crtc);
  3431. intel_enable_pipe(intel_crtc);
  3432. if (intel_crtc->config.has_pch_encoder)
  3433. ironlake_pch_enable(crtc);
  3434. for_each_encoder_on_crtc(dev, crtc, encoder)
  3435. encoder->enable(encoder);
  3436. if (HAS_PCH_CPT(dev))
  3437. cpt_verify_modeset(dev, intel_crtc->pipe);
  3438. intel_crtc_enable_planes(crtc);
  3439. }
  3440. /* IPS only exists on ULT machines and is tied to pipe A. */
  3441. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3442. {
  3443. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3444. }
  3445. /*
  3446. * This implements the workaround described in the "notes" section of the mode
  3447. * set sequence documentation. When going from no pipes or single pipe to
  3448. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3449. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3450. */
  3451. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3452. {
  3453. struct drm_device *dev = crtc->base.dev;
  3454. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3455. /* We want to get the other_active_crtc only if there's only 1 other
  3456. * active crtc. */
  3457. for_each_intel_crtc(dev, crtc_it) {
  3458. if (!crtc_it->active || crtc_it == crtc)
  3459. continue;
  3460. if (other_active_crtc)
  3461. return;
  3462. other_active_crtc = crtc_it;
  3463. }
  3464. if (!other_active_crtc)
  3465. return;
  3466. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3467. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3468. }
  3469. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3470. {
  3471. struct drm_device *dev = crtc->dev;
  3472. struct drm_i915_private *dev_priv = dev->dev_private;
  3473. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3474. struct intel_encoder *encoder;
  3475. int pipe = intel_crtc->pipe;
  3476. enum plane plane = intel_crtc->plane;
  3477. WARN_ON(!crtc->enabled);
  3478. if (intel_crtc->active)
  3479. return;
  3480. if (intel_crtc_to_shared_dpll(intel_crtc))
  3481. intel_enable_shared_dpll(intel_crtc);
  3482. if (intel_crtc->config.has_dp_encoder)
  3483. intel_dp_set_m_n(intel_crtc);
  3484. intel_set_pipe_timings(intel_crtc);
  3485. if (intel_crtc->config.has_pch_encoder) {
  3486. intel_cpu_transcoder_set_m_n(intel_crtc,
  3487. &intel_crtc->config.fdi_m_n);
  3488. }
  3489. haswell_set_pipeconf(crtc);
  3490. intel_set_pipe_csc(crtc);
  3491. /* Set up the display plane register */
  3492. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
  3493. POSTING_READ(DSPCNTR(plane));
  3494. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3495. crtc->x, crtc->y);
  3496. intel_crtc->active = true;
  3497. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3498. for_each_encoder_on_crtc(dev, crtc, encoder)
  3499. if (encoder->pre_enable)
  3500. encoder->pre_enable(encoder);
  3501. if (intel_crtc->config.has_pch_encoder) {
  3502. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3503. dev_priv->display.fdi_link_train(crtc);
  3504. }
  3505. intel_ddi_enable_pipe_clock(intel_crtc);
  3506. ironlake_pfit_enable(intel_crtc);
  3507. /*
  3508. * On ILK+ LUT must be loaded before the pipe is running but with
  3509. * clocks enabled
  3510. */
  3511. intel_crtc_load_lut(crtc);
  3512. intel_ddi_set_pipe_settings(crtc);
  3513. intel_ddi_enable_transcoder_func(crtc);
  3514. intel_update_watermarks(crtc);
  3515. intel_enable_pipe(intel_crtc);
  3516. if (intel_crtc->config.has_pch_encoder)
  3517. lpt_pch_enable(crtc);
  3518. if (intel_crtc->config.dp_encoder_is_mst)
  3519. intel_ddi_set_vc_payload_alloc(crtc, true);
  3520. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3521. encoder->enable(encoder);
  3522. intel_opregion_notify_encoder(encoder, true);
  3523. }
  3524. /* If we change the relative order between pipe/planes enabling, we need
  3525. * to change the workaround. */
  3526. haswell_mode_set_planes_workaround(intel_crtc);
  3527. intel_crtc_enable_planes(crtc);
  3528. }
  3529. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3530. {
  3531. struct drm_device *dev = crtc->base.dev;
  3532. struct drm_i915_private *dev_priv = dev->dev_private;
  3533. int pipe = crtc->pipe;
  3534. /* To avoid upsetting the power well on haswell only disable the pfit if
  3535. * it's in use. The hw state code will make sure we get this right. */
  3536. if (crtc->config.pch_pfit.enabled) {
  3537. I915_WRITE(PF_CTL(pipe), 0);
  3538. I915_WRITE(PF_WIN_POS(pipe), 0);
  3539. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3540. }
  3541. }
  3542. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3543. {
  3544. struct drm_device *dev = crtc->dev;
  3545. struct drm_i915_private *dev_priv = dev->dev_private;
  3546. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3547. struct intel_encoder *encoder;
  3548. int pipe = intel_crtc->pipe;
  3549. u32 reg, temp;
  3550. if (!intel_crtc->active)
  3551. return;
  3552. intel_crtc_disable_planes(crtc);
  3553. for_each_encoder_on_crtc(dev, crtc, encoder)
  3554. encoder->disable(encoder);
  3555. if (intel_crtc->config.has_pch_encoder)
  3556. intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
  3557. intel_disable_pipe(dev_priv, pipe);
  3558. ironlake_pfit_disable(intel_crtc);
  3559. for_each_encoder_on_crtc(dev, crtc, encoder)
  3560. if (encoder->post_disable)
  3561. encoder->post_disable(encoder);
  3562. if (intel_crtc->config.has_pch_encoder) {
  3563. ironlake_fdi_disable(crtc);
  3564. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3565. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3566. if (HAS_PCH_CPT(dev)) {
  3567. /* disable TRANS_DP_CTL */
  3568. reg = TRANS_DP_CTL(pipe);
  3569. temp = I915_READ(reg);
  3570. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3571. TRANS_DP_PORT_SEL_MASK);
  3572. temp |= TRANS_DP_PORT_SEL_NONE;
  3573. I915_WRITE(reg, temp);
  3574. /* disable DPLL_SEL */
  3575. temp = I915_READ(PCH_DPLL_SEL);
  3576. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3577. I915_WRITE(PCH_DPLL_SEL, temp);
  3578. }
  3579. /* disable PCH DPLL */
  3580. intel_disable_shared_dpll(intel_crtc);
  3581. ironlake_fdi_pll_disable(intel_crtc);
  3582. }
  3583. intel_crtc->active = false;
  3584. intel_update_watermarks(crtc);
  3585. mutex_lock(&dev->struct_mutex);
  3586. intel_update_fbc(dev);
  3587. mutex_unlock(&dev->struct_mutex);
  3588. }
  3589. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3590. {
  3591. struct drm_device *dev = crtc->dev;
  3592. struct drm_i915_private *dev_priv = dev->dev_private;
  3593. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3594. struct intel_encoder *encoder;
  3595. int pipe = intel_crtc->pipe;
  3596. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3597. if (!intel_crtc->active)
  3598. return;
  3599. intel_crtc_disable_planes(crtc);
  3600. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3601. intel_opregion_notify_encoder(encoder, false);
  3602. encoder->disable(encoder);
  3603. }
  3604. if (intel_crtc->config.has_pch_encoder)
  3605. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
  3606. intel_disable_pipe(dev_priv, pipe);
  3607. if (intel_crtc->config.dp_encoder_is_mst)
  3608. intel_ddi_set_vc_payload_alloc(crtc, false);
  3609. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3610. ironlake_pfit_disable(intel_crtc);
  3611. intel_ddi_disable_pipe_clock(intel_crtc);
  3612. if (intel_crtc->config.has_pch_encoder) {
  3613. lpt_disable_pch_transcoder(dev_priv);
  3614. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3615. intel_ddi_fdi_disable(crtc);
  3616. }
  3617. for_each_encoder_on_crtc(dev, crtc, encoder)
  3618. if (encoder->post_disable)
  3619. encoder->post_disable(encoder);
  3620. intel_crtc->active = false;
  3621. intel_update_watermarks(crtc);
  3622. mutex_lock(&dev->struct_mutex);
  3623. intel_update_fbc(dev);
  3624. mutex_unlock(&dev->struct_mutex);
  3625. if (intel_crtc_to_shared_dpll(intel_crtc))
  3626. intel_disable_shared_dpll(intel_crtc);
  3627. }
  3628. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3629. {
  3630. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3631. intel_put_shared_dpll(intel_crtc);
  3632. }
  3633. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3634. {
  3635. struct drm_device *dev = crtc->base.dev;
  3636. struct drm_i915_private *dev_priv = dev->dev_private;
  3637. struct intel_crtc_config *pipe_config = &crtc->config;
  3638. if (!crtc->config.gmch_pfit.control)
  3639. return;
  3640. /*
  3641. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3642. * according to register description and PRM.
  3643. */
  3644. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3645. assert_pipe_disabled(dev_priv, crtc->pipe);
  3646. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3647. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3648. /* Border color in case we don't scale up to the full screen. Black by
  3649. * default, change to something else for debugging. */
  3650. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3651. }
  3652. static enum intel_display_power_domain port_to_power_domain(enum port port)
  3653. {
  3654. switch (port) {
  3655. case PORT_A:
  3656. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3657. case PORT_B:
  3658. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3659. case PORT_C:
  3660. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3661. case PORT_D:
  3662. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3663. default:
  3664. WARN_ON_ONCE(1);
  3665. return POWER_DOMAIN_PORT_OTHER;
  3666. }
  3667. }
  3668. #define for_each_power_domain(domain, mask) \
  3669. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3670. if ((1 << (domain)) & (mask))
  3671. enum intel_display_power_domain
  3672. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3673. {
  3674. struct drm_device *dev = intel_encoder->base.dev;
  3675. struct intel_digital_port *intel_dig_port;
  3676. switch (intel_encoder->type) {
  3677. case INTEL_OUTPUT_UNKNOWN:
  3678. /* Only DDI platforms should ever use this output type */
  3679. WARN_ON_ONCE(!HAS_DDI(dev));
  3680. case INTEL_OUTPUT_DISPLAYPORT:
  3681. case INTEL_OUTPUT_HDMI:
  3682. case INTEL_OUTPUT_EDP:
  3683. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3684. return port_to_power_domain(intel_dig_port->port);
  3685. case INTEL_OUTPUT_DP_MST:
  3686. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  3687. return port_to_power_domain(intel_dig_port->port);
  3688. case INTEL_OUTPUT_ANALOG:
  3689. return POWER_DOMAIN_PORT_CRT;
  3690. case INTEL_OUTPUT_DSI:
  3691. return POWER_DOMAIN_PORT_DSI;
  3692. default:
  3693. return POWER_DOMAIN_PORT_OTHER;
  3694. }
  3695. }
  3696. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3697. {
  3698. struct drm_device *dev = crtc->dev;
  3699. struct intel_encoder *intel_encoder;
  3700. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3701. enum pipe pipe = intel_crtc->pipe;
  3702. unsigned long mask;
  3703. enum transcoder transcoder;
  3704. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3705. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3706. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3707. if (intel_crtc->config.pch_pfit.enabled ||
  3708. intel_crtc->config.pch_pfit.force_thru)
  3709. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3710. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3711. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3712. return mask;
  3713. }
  3714. void intel_display_set_init_power(struct drm_i915_private *dev_priv,
  3715. bool enable)
  3716. {
  3717. if (dev_priv->power_domains.init_power_on == enable)
  3718. return;
  3719. if (enable)
  3720. intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
  3721. else
  3722. intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
  3723. dev_priv->power_domains.init_power_on = enable;
  3724. }
  3725. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3726. {
  3727. struct drm_i915_private *dev_priv = dev->dev_private;
  3728. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3729. struct intel_crtc *crtc;
  3730. /*
  3731. * First get all needed power domains, then put all unneeded, to avoid
  3732. * any unnecessary toggling of the power wells.
  3733. */
  3734. for_each_intel_crtc(dev, crtc) {
  3735. enum intel_display_power_domain domain;
  3736. if (!crtc->base.enabled)
  3737. continue;
  3738. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3739. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3740. intel_display_power_get(dev_priv, domain);
  3741. }
  3742. for_each_intel_crtc(dev, crtc) {
  3743. enum intel_display_power_domain domain;
  3744. for_each_power_domain(domain, crtc->enabled_power_domains)
  3745. intel_display_power_put(dev_priv, domain);
  3746. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  3747. }
  3748. intel_display_set_init_power(dev_priv, false);
  3749. }
  3750. /* returns HPLL frequency in kHz */
  3751. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  3752. {
  3753. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  3754. /* Obtain SKU information */
  3755. mutex_lock(&dev_priv->dpio_lock);
  3756. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  3757. CCK_FUSE_HPLL_FREQ_MASK;
  3758. mutex_unlock(&dev_priv->dpio_lock);
  3759. return vco_freq[hpll_freq] * 1000;
  3760. }
  3761. static void vlv_update_cdclk(struct drm_device *dev)
  3762. {
  3763. struct drm_i915_private *dev_priv = dev->dev_private;
  3764. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  3765. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz",
  3766. dev_priv->vlv_cdclk_freq);
  3767. /*
  3768. * Program the gmbus_freq based on the cdclk frequency.
  3769. * BSpec erroneously claims we should aim for 4MHz, but
  3770. * in fact 1MHz is the correct frequency.
  3771. */
  3772. I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
  3773. }
  3774. /* Adjust CDclk dividers to allow high res or save power if possible */
  3775. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  3776. {
  3777. struct drm_i915_private *dev_priv = dev->dev_private;
  3778. u32 val, cmd;
  3779. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  3780. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  3781. cmd = 2;
  3782. else if (cdclk == 266667)
  3783. cmd = 1;
  3784. else
  3785. cmd = 0;
  3786. mutex_lock(&dev_priv->rps.hw_lock);
  3787. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3788. val &= ~DSPFREQGUAR_MASK;
  3789. val |= (cmd << DSPFREQGUAR_SHIFT);
  3790. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3791. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3792. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  3793. 50)) {
  3794. DRM_ERROR("timed out waiting for CDclk change\n");
  3795. }
  3796. mutex_unlock(&dev_priv->rps.hw_lock);
  3797. if (cdclk == 400000) {
  3798. u32 divider, vco;
  3799. vco = valleyview_get_vco(dev_priv);
  3800. divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
  3801. mutex_lock(&dev_priv->dpio_lock);
  3802. /* adjust cdclk divider */
  3803. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3804. val &= ~DISPLAY_FREQUENCY_VALUES;
  3805. val |= divider;
  3806. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  3807. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  3808. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  3809. 50))
  3810. DRM_ERROR("timed out waiting for CDclk change\n");
  3811. mutex_unlock(&dev_priv->dpio_lock);
  3812. }
  3813. mutex_lock(&dev_priv->dpio_lock);
  3814. /* adjust self-refresh exit latency value */
  3815. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  3816. val &= ~0x7f;
  3817. /*
  3818. * For high bandwidth configs, we set a higher latency in the bunit
  3819. * so that the core display fetch happens in time to avoid underruns.
  3820. */
  3821. if (cdclk == 400000)
  3822. val |= 4500 / 250; /* 4.5 usec */
  3823. else
  3824. val |= 3000 / 250; /* 3.0 usec */
  3825. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  3826. mutex_unlock(&dev_priv->dpio_lock);
  3827. vlv_update_cdclk(dev);
  3828. }
  3829. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  3830. int max_pixclk)
  3831. {
  3832. int vco = valleyview_get_vco(dev_priv);
  3833. int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
  3834. /*
  3835. * Really only a few cases to deal with, as only 4 CDclks are supported:
  3836. * 200MHz
  3837. * 267MHz
  3838. * 320/333MHz (depends on HPLL freq)
  3839. * 400MHz
  3840. * So we check to see whether we're above 90% of the lower bin and
  3841. * adjust if needed.
  3842. *
  3843. * We seem to get an unstable or solid color picture at 200MHz.
  3844. * Not sure what's wrong. For now use 200MHz only when all pipes
  3845. * are off.
  3846. */
  3847. if (max_pixclk > freq_320*9/10)
  3848. return 400000;
  3849. else if (max_pixclk > 266667*9/10)
  3850. return freq_320;
  3851. else if (max_pixclk > 0)
  3852. return 266667;
  3853. else
  3854. return 200000;
  3855. }
  3856. /* compute the max pixel clock for new configuration */
  3857. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  3858. {
  3859. struct drm_device *dev = dev_priv->dev;
  3860. struct intel_crtc *intel_crtc;
  3861. int max_pixclk = 0;
  3862. for_each_intel_crtc(dev, intel_crtc) {
  3863. if (intel_crtc->new_enabled)
  3864. max_pixclk = max(max_pixclk,
  3865. intel_crtc->new_config->adjusted_mode.crtc_clock);
  3866. }
  3867. return max_pixclk;
  3868. }
  3869. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  3870. unsigned *prepare_pipes)
  3871. {
  3872. struct drm_i915_private *dev_priv = dev->dev_private;
  3873. struct intel_crtc *intel_crtc;
  3874. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3875. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  3876. dev_priv->vlv_cdclk_freq)
  3877. return;
  3878. /* disable/enable all currently active pipes while we change cdclk */
  3879. for_each_intel_crtc(dev, intel_crtc)
  3880. if (intel_crtc->base.enabled)
  3881. *prepare_pipes |= (1 << intel_crtc->pipe);
  3882. }
  3883. static void valleyview_modeset_global_resources(struct drm_device *dev)
  3884. {
  3885. struct drm_i915_private *dev_priv = dev->dev_private;
  3886. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3887. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  3888. if (req_cdclk != dev_priv->vlv_cdclk_freq)
  3889. valleyview_set_cdclk(dev, req_cdclk);
  3890. modeset_update_crtc_power_domains(dev);
  3891. }
  3892. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  3893. {
  3894. struct drm_device *dev = crtc->dev;
  3895. struct drm_i915_private *dev_priv = dev->dev_private;
  3896. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3897. struct intel_encoder *encoder;
  3898. int pipe = intel_crtc->pipe;
  3899. int plane = intel_crtc->plane;
  3900. bool is_dsi;
  3901. u32 dspcntr;
  3902. WARN_ON(!crtc->enabled);
  3903. if (intel_crtc->active)
  3904. return;
  3905. is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
  3906. if (!is_dsi && !IS_CHERRYVIEW(dev))
  3907. vlv_prepare_pll(intel_crtc);
  3908. /* Set up the display plane register */
  3909. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3910. if (intel_crtc->config.has_dp_encoder)
  3911. intel_dp_set_m_n(intel_crtc);
  3912. intel_set_pipe_timings(intel_crtc);
  3913. /* pipesrc and dspsize control the size that is scaled from,
  3914. * which should always be the user's requested size.
  3915. */
  3916. I915_WRITE(DSPSIZE(plane),
  3917. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  3918. (intel_crtc->config.pipe_src_w - 1));
  3919. I915_WRITE(DSPPOS(plane), 0);
  3920. i9xx_set_pipeconf(intel_crtc);
  3921. I915_WRITE(DSPCNTR(plane), dspcntr);
  3922. POSTING_READ(DSPCNTR(plane));
  3923. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3924. crtc->x, crtc->y);
  3925. intel_crtc->active = true;
  3926. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3927. for_each_encoder_on_crtc(dev, crtc, encoder)
  3928. if (encoder->pre_pll_enable)
  3929. encoder->pre_pll_enable(encoder);
  3930. if (!is_dsi) {
  3931. if (IS_CHERRYVIEW(dev))
  3932. chv_enable_pll(intel_crtc);
  3933. else
  3934. vlv_enable_pll(intel_crtc);
  3935. }
  3936. for_each_encoder_on_crtc(dev, crtc, encoder)
  3937. if (encoder->pre_enable)
  3938. encoder->pre_enable(encoder);
  3939. i9xx_pfit_enable(intel_crtc);
  3940. intel_crtc_load_lut(crtc);
  3941. intel_update_watermarks(crtc);
  3942. intel_enable_pipe(intel_crtc);
  3943. for_each_encoder_on_crtc(dev, crtc, encoder)
  3944. encoder->enable(encoder);
  3945. intel_crtc_enable_planes(crtc);
  3946. /* Underruns don't raise interrupts, so check manually. */
  3947. i9xx_check_fifo_underruns(dev);
  3948. }
  3949. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  3950. {
  3951. struct drm_device *dev = crtc->base.dev;
  3952. struct drm_i915_private *dev_priv = dev->dev_private;
  3953. I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
  3954. I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
  3955. }
  3956. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  3957. {
  3958. struct drm_device *dev = crtc->dev;
  3959. struct drm_i915_private *dev_priv = dev->dev_private;
  3960. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3961. struct intel_encoder *encoder;
  3962. int pipe = intel_crtc->pipe;
  3963. int plane = intel_crtc->plane;
  3964. u32 dspcntr;
  3965. WARN_ON(!crtc->enabled);
  3966. if (intel_crtc->active)
  3967. return;
  3968. i9xx_set_pll_dividers(intel_crtc);
  3969. /* Set up the display plane register */
  3970. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3971. if (pipe == 0)
  3972. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3973. else
  3974. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3975. if (intel_crtc->config.has_dp_encoder)
  3976. intel_dp_set_m_n(intel_crtc);
  3977. intel_set_pipe_timings(intel_crtc);
  3978. /* pipesrc and dspsize control the size that is scaled from,
  3979. * which should always be the user's requested size.
  3980. */
  3981. I915_WRITE(DSPSIZE(plane),
  3982. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  3983. (intel_crtc->config.pipe_src_w - 1));
  3984. I915_WRITE(DSPPOS(plane), 0);
  3985. i9xx_set_pipeconf(intel_crtc);
  3986. I915_WRITE(DSPCNTR(plane), dspcntr);
  3987. POSTING_READ(DSPCNTR(plane));
  3988. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3989. crtc->x, crtc->y);
  3990. intel_crtc->active = true;
  3991. if (!IS_GEN2(dev))
  3992. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3993. for_each_encoder_on_crtc(dev, crtc, encoder)
  3994. if (encoder->pre_enable)
  3995. encoder->pre_enable(encoder);
  3996. i9xx_enable_pll(intel_crtc);
  3997. i9xx_pfit_enable(intel_crtc);
  3998. intel_crtc_load_lut(crtc);
  3999. intel_update_watermarks(crtc);
  4000. intel_enable_pipe(intel_crtc);
  4001. for_each_encoder_on_crtc(dev, crtc, encoder)
  4002. encoder->enable(encoder);
  4003. intel_crtc_enable_planes(crtc);
  4004. /*
  4005. * Gen2 reports pipe underruns whenever all planes are disabled.
  4006. * So don't enable underrun reporting before at least some planes
  4007. * are enabled.
  4008. * FIXME: Need to fix the logic to work when we turn off all planes
  4009. * but leave the pipe running.
  4010. */
  4011. if (IS_GEN2(dev))
  4012. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  4013. /* Underruns don't raise interrupts, so check manually. */
  4014. i9xx_check_fifo_underruns(dev);
  4015. }
  4016. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4017. {
  4018. struct drm_device *dev = crtc->base.dev;
  4019. struct drm_i915_private *dev_priv = dev->dev_private;
  4020. if (!crtc->config.gmch_pfit.control)
  4021. return;
  4022. assert_pipe_disabled(dev_priv, crtc->pipe);
  4023. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4024. I915_READ(PFIT_CONTROL));
  4025. I915_WRITE(PFIT_CONTROL, 0);
  4026. }
  4027. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4028. {
  4029. struct drm_device *dev = crtc->dev;
  4030. struct drm_i915_private *dev_priv = dev->dev_private;
  4031. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4032. struct intel_encoder *encoder;
  4033. int pipe = intel_crtc->pipe;
  4034. if (!intel_crtc->active)
  4035. return;
  4036. /*
  4037. * Gen2 reports pipe underruns whenever all planes are disabled.
  4038. * So diasble underrun reporting before all the planes get disabled.
  4039. * FIXME: Need to fix the logic to work when we turn off all planes
  4040. * but leave the pipe running.
  4041. */
  4042. if (IS_GEN2(dev))
  4043. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  4044. /*
  4045. * Vblank time updates from the shadow to live plane control register
  4046. * are blocked if the memory self-refresh mode is active at that
  4047. * moment. So to make sure the plane gets truly disabled, disable
  4048. * first the self-refresh mode. The self-refresh enable bit in turn
  4049. * will be checked/applied by the HW only at the next frame start
  4050. * event which is after the vblank start event, so we need to have a
  4051. * wait-for-vblank between disabling the plane and the pipe.
  4052. */
  4053. intel_set_memory_cxsr(dev_priv, false);
  4054. intel_crtc_disable_planes(crtc);
  4055. for_each_encoder_on_crtc(dev, crtc, encoder)
  4056. encoder->disable(encoder);
  4057. /*
  4058. * On gen2 planes are double buffered but the pipe isn't, so we must
  4059. * wait for planes to fully turn off before disabling the pipe.
  4060. * We also need to wait on all gmch platforms because of the
  4061. * self-refresh mode constraint explained above.
  4062. */
  4063. intel_wait_for_vblank(dev, pipe);
  4064. intel_disable_pipe(dev_priv, pipe);
  4065. i9xx_pfit_disable(intel_crtc);
  4066. for_each_encoder_on_crtc(dev, crtc, encoder)
  4067. if (encoder->post_disable)
  4068. encoder->post_disable(encoder);
  4069. if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
  4070. if (IS_CHERRYVIEW(dev))
  4071. chv_disable_pll(dev_priv, pipe);
  4072. else if (IS_VALLEYVIEW(dev))
  4073. vlv_disable_pll(dev_priv, pipe);
  4074. else
  4075. i9xx_disable_pll(dev_priv, pipe);
  4076. }
  4077. if (!IS_GEN2(dev))
  4078. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  4079. intel_crtc->active = false;
  4080. intel_update_watermarks(crtc);
  4081. mutex_lock(&dev->struct_mutex);
  4082. intel_update_fbc(dev);
  4083. mutex_unlock(&dev->struct_mutex);
  4084. }
  4085. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4086. {
  4087. }
  4088. static void intel_crtc_update_sarea(struct drm_crtc *crtc,
  4089. bool enabled)
  4090. {
  4091. struct drm_device *dev = crtc->dev;
  4092. struct drm_i915_master_private *master_priv;
  4093. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4094. int pipe = intel_crtc->pipe;
  4095. if (!dev->primary->master)
  4096. return;
  4097. master_priv = dev->primary->master->driver_priv;
  4098. if (!master_priv->sarea_priv)
  4099. return;
  4100. switch (pipe) {
  4101. case 0:
  4102. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  4103. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  4104. break;
  4105. case 1:
  4106. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  4107. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  4108. break;
  4109. default:
  4110. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  4111. break;
  4112. }
  4113. }
  4114. /* Master function to enable/disable CRTC and corresponding power wells */
  4115. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4116. {
  4117. struct drm_device *dev = crtc->dev;
  4118. struct drm_i915_private *dev_priv = dev->dev_private;
  4119. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4120. enum intel_display_power_domain domain;
  4121. unsigned long domains;
  4122. if (enable) {
  4123. if (!intel_crtc->active) {
  4124. domains = get_crtc_power_domains(crtc);
  4125. for_each_power_domain(domain, domains)
  4126. intel_display_power_get(dev_priv, domain);
  4127. intel_crtc->enabled_power_domains = domains;
  4128. dev_priv->display.crtc_enable(crtc);
  4129. }
  4130. } else {
  4131. if (intel_crtc->active) {
  4132. dev_priv->display.crtc_disable(crtc);
  4133. domains = intel_crtc->enabled_power_domains;
  4134. for_each_power_domain(domain, domains)
  4135. intel_display_power_put(dev_priv, domain);
  4136. intel_crtc->enabled_power_domains = 0;
  4137. }
  4138. }
  4139. }
  4140. /**
  4141. * Sets the power management mode of the pipe and plane.
  4142. */
  4143. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4144. {
  4145. struct drm_device *dev = crtc->dev;
  4146. struct intel_encoder *intel_encoder;
  4147. bool enable = false;
  4148. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4149. enable |= intel_encoder->connectors_active;
  4150. intel_crtc_control(crtc, enable);
  4151. intel_crtc_update_sarea(crtc, enable);
  4152. }
  4153. static void intel_crtc_disable(struct drm_crtc *crtc)
  4154. {
  4155. struct drm_device *dev = crtc->dev;
  4156. struct drm_connector *connector;
  4157. struct drm_i915_private *dev_priv = dev->dev_private;
  4158. struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
  4159. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  4160. /* crtc should still be enabled when we disable it. */
  4161. WARN_ON(!crtc->enabled);
  4162. dev_priv->display.crtc_disable(crtc);
  4163. intel_crtc_update_sarea(crtc, false);
  4164. dev_priv->display.off(crtc);
  4165. if (crtc->primary->fb) {
  4166. mutex_lock(&dev->struct_mutex);
  4167. intel_unpin_fb_obj(old_obj);
  4168. i915_gem_track_fb(old_obj, NULL,
  4169. INTEL_FRONTBUFFER_PRIMARY(pipe));
  4170. mutex_unlock(&dev->struct_mutex);
  4171. crtc->primary->fb = NULL;
  4172. }
  4173. /* Update computed state. */
  4174. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4175. if (!connector->encoder || !connector->encoder->crtc)
  4176. continue;
  4177. if (connector->encoder->crtc != crtc)
  4178. continue;
  4179. connector->dpms = DRM_MODE_DPMS_OFF;
  4180. to_intel_encoder(connector->encoder)->connectors_active = false;
  4181. }
  4182. }
  4183. void intel_encoder_destroy(struct drm_encoder *encoder)
  4184. {
  4185. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4186. drm_encoder_cleanup(encoder);
  4187. kfree(intel_encoder);
  4188. }
  4189. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4190. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4191. * state of the entire output pipe. */
  4192. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4193. {
  4194. if (mode == DRM_MODE_DPMS_ON) {
  4195. encoder->connectors_active = true;
  4196. intel_crtc_update_dpms(encoder->base.crtc);
  4197. } else {
  4198. encoder->connectors_active = false;
  4199. intel_crtc_update_dpms(encoder->base.crtc);
  4200. }
  4201. }
  4202. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4203. * internal consistency). */
  4204. static void intel_connector_check_state(struct intel_connector *connector)
  4205. {
  4206. if (connector->get_hw_state(connector)) {
  4207. struct intel_encoder *encoder = connector->encoder;
  4208. struct drm_crtc *crtc;
  4209. bool encoder_enabled;
  4210. enum pipe pipe;
  4211. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4212. connector->base.base.id,
  4213. connector->base.name);
  4214. /* there is no real hw state for MST connectors */
  4215. if (connector->mst_port)
  4216. return;
  4217. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4218. "wrong connector dpms state\n");
  4219. WARN(connector->base.encoder != &encoder->base,
  4220. "active connector not linked to encoder\n");
  4221. if (encoder) {
  4222. WARN(!encoder->connectors_active,
  4223. "encoder->connectors_active not set\n");
  4224. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4225. WARN(!encoder_enabled, "encoder not enabled\n");
  4226. if (WARN_ON(!encoder->base.crtc))
  4227. return;
  4228. crtc = encoder->base.crtc;
  4229. WARN(!crtc->enabled, "crtc not enabled\n");
  4230. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4231. WARN(pipe != to_intel_crtc(crtc)->pipe,
  4232. "encoder active on the wrong pipe\n");
  4233. }
  4234. }
  4235. }
  4236. /* Even simpler default implementation, if there's really no special case to
  4237. * consider. */
  4238. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4239. {
  4240. /* All the simple cases only support two dpms states. */
  4241. if (mode != DRM_MODE_DPMS_ON)
  4242. mode = DRM_MODE_DPMS_OFF;
  4243. if (mode == connector->dpms)
  4244. return;
  4245. connector->dpms = mode;
  4246. /* Only need to change hw state when actually enabled */
  4247. if (connector->encoder)
  4248. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4249. intel_modeset_check_state(connector->dev);
  4250. }
  4251. /* Simple connector->get_hw_state implementation for encoders that support only
  4252. * one connector and no cloning and hence the encoder state determines the state
  4253. * of the connector. */
  4254. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4255. {
  4256. enum pipe pipe = 0;
  4257. struct intel_encoder *encoder = connector->encoder;
  4258. return encoder->get_hw_state(encoder, &pipe);
  4259. }
  4260. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4261. struct intel_crtc_config *pipe_config)
  4262. {
  4263. struct drm_i915_private *dev_priv = dev->dev_private;
  4264. struct intel_crtc *pipe_B_crtc =
  4265. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4266. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4267. pipe_name(pipe), pipe_config->fdi_lanes);
  4268. if (pipe_config->fdi_lanes > 4) {
  4269. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4270. pipe_name(pipe), pipe_config->fdi_lanes);
  4271. return false;
  4272. }
  4273. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4274. if (pipe_config->fdi_lanes > 2) {
  4275. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4276. pipe_config->fdi_lanes);
  4277. return false;
  4278. } else {
  4279. return true;
  4280. }
  4281. }
  4282. if (INTEL_INFO(dev)->num_pipes == 2)
  4283. return true;
  4284. /* Ivybridge 3 pipe is really complicated */
  4285. switch (pipe) {
  4286. case PIPE_A:
  4287. return true;
  4288. case PIPE_B:
  4289. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4290. pipe_config->fdi_lanes > 2) {
  4291. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4292. pipe_name(pipe), pipe_config->fdi_lanes);
  4293. return false;
  4294. }
  4295. return true;
  4296. case PIPE_C:
  4297. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4298. pipe_B_crtc->config.fdi_lanes <= 2) {
  4299. if (pipe_config->fdi_lanes > 2) {
  4300. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4301. pipe_name(pipe), pipe_config->fdi_lanes);
  4302. return false;
  4303. }
  4304. } else {
  4305. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4306. return false;
  4307. }
  4308. return true;
  4309. default:
  4310. BUG();
  4311. }
  4312. }
  4313. #define RETRY 1
  4314. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4315. struct intel_crtc_config *pipe_config)
  4316. {
  4317. struct drm_device *dev = intel_crtc->base.dev;
  4318. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4319. int lane, link_bw, fdi_dotclock;
  4320. bool setup_ok, needs_recompute = false;
  4321. retry:
  4322. /* FDI is a binary signal running at ~2.7GHz, encoding
  4323. * each output octet as 10 bits. The actual frequency
  4324. * is stored as a divider into a 100MHz clock, and the
  4325. * mode pixel clock is stored in units of 1KHz.
  4326. * Hence the bw of each lane in terms of the mode signal
  4327. * is:
  4328. */
  4329. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4330. fdi_dotclock = adjusted_mode->crtc_clock;
  4331. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4332. pipe_config->pipe_bpp);
  4333. pipe_config->fdi_lanes = lane;
  4334. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4335. link_bw, &pipe_config->fdi_m_n);
  4336. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4337. intel_crtc->pipe, pipe_config);
  4338. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4339. pipe_config->pipe_bpp -= 2*3;
  4340. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4341. pipe_config->pipe_bpp);
  4342. needs_recompute = true;
  4343. pipe_config->bw_constrained = true;
  4344. goto retry;
  4345. }
  4346. if (needs_recompute)
  4347. return RETRY;
  4348. return setup_ok ? 0 : -EINVAL;
  4349. }
  4350. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4351. struct intel_crtc_config *pipe_config)
  4352. {
  4353. pipe_config->ips_enabled = i915.enable_ips &&
  4354. hsw_crtc_supports_ips(crtc) &&
  4355. pipe_config->pipe_bpp <= 24;
  4356. }
  4357. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4358. struct intel_crtc_config *pipe_config)
  4359. {
  4360. struct drm_device *dev = crtc->base.dev;
  4361. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4362. /* FIXME should check pixel clock limits on all platforms */
  4363. if (INTEL_INFO(dev)->gen < 4) {
  4364. struct drm_i915_private *dev_priv = dev->dev_private;
  4365. int clock_limit =
  4366. dev_priv->display.get_display_clock_speed(dev);
  4367. /*
  4368. * Enable pixel doubling when the dot clock
  4369. * is > 90% of the (display) core speed.
  4370. *
  4371. * GDG double wide on either pipe,
  4372. * otherwise pipe A only.
  4373. */
  4374. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4375. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4376. clock_limit *= 2;
  4377. pipe_config->double_wide = true;
  4378. }
  4379. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4380. return -EINVAL;
  4381. }
  4382. /*
  4383. * Pipe horizontal size must be even in:
  4384. * - DVO ganged mode
  4385. * - LVDS dual channel mode
  4386. * - Double wide pipe
  4387. */
  4388. if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4389. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4390. pipe_config->pipe_src_w &= ~1;
  4391. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4392. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4393. */
  4394. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4395. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4396. return -EINVAL;
  4397. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4398. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4399. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4400. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4401. * for lvds. */
  4402. pipe_config->pipe_bpp = 8*3;
  4403. }
  4404. if (HAS_IPS(dev))
  4405. hsw_compute_ips_config(crtc, pipe_config);
  4406. /*
  4407. * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
  4408. * old clock survives for now.
  4409. */
  4410. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
  4411. pipe_config->shared_dpll = crtc->config.shared_dpll;
  4412. if (pipe_config->has_pch_encoder)
  4413. return ironlake_fdi_compute_config(crtc, pipe_config);
  4414. return 0;
  4415. }
  4416. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4417. {
  4418. struct drm_i915_private *dev_priv = dev->dev_private;
  4419. int vco = valleyview_get_vco(dev_priv);
  4420. u32 val;
  4421. int divider;
  4422. mutex_lock(&dev_priv->dpio_lock);
  4423. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4424. mutex_unlock(&dev_priv->dpio_lock);
  4425. divider = val & DISPLAY_FREQUENCY_VALUES;
  4426. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4427. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4428. "cdclk change in progress\n");
  4429. return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
  4430. }
  4431. static int i945_get_display_clock_speed(struct drm_device *dev)
  4432. {
  4433. return 400000;
  4434. }
  4435. static int i915_get_display_clock_speed(struct drm_device *dev)
  4436. {
  4437. return 333000;
  4438. }
  4439. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4440. {
  4441. return 200000;
  4442. }
  4443. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4444. {
  4445. u16 gcfgc = 0;
  4446. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4447. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4448. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4449. return 267000;
  4450. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4451. return 333000;
  4452. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4453. return 444000;
  4454. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4455. return 200000;
  4456. default:
  4457. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4458. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4459. return 133000;
  4460. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4461. return 167000;
  4462. }
  4463. }
  4464. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4465. {
  4466. u16 gcfgc = 0;
  4467. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4468. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4469. return 133000;
  4470. else {
  4471. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4472. case GC_DISPLAY_CLOCK_333_MHZ:
  4473. return 333000;
  4474. default:
  4475. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4476. return 190000;
  4477. }
  4478. }
  4479. }
  4480. static int i865_get_display_clock_speed(struct drm_device *dev)
  4481. {
  4482. return 266000;
  4483. }
  4484. static int i855_get_display_clock_speed(struct drm_device *dev)
  4485. {
  4486. u16 hpllcc = 0;
  4487. /* Assume that the hardware is in the high speed state. This
  4488. * should be the default.
  4489. */
  4490. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4491. case GC_CLOCK_133_200:
  4492. case GC_CLOCK_100_200:
  4493. return 200000;
  4494. case GC_CLOCK_166_250:
  4495. return 250000;
  4496. case GC_CLOCK_100_133:
  4497. return 133000;
  4498. }
  4499. /* Shouldn't happen */
  4500. return 0;
  4501. }
  4502. static int i830_get_display_clock_speed(struct drm_device *dev)
  4503. {
  4504. return 133000;
  4505. }
  4506. static void
  4507. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4508. {
  4509. while (*num > DATA_LINK_M_N_MASK ||
  4510. *den > DATA_LINK_M_N_MASK) {
  4511. *num >>= 1;
  4512. *den >>= 1;
  4513. }
  4514. }
  4515. static void compute_m_n(unsigned int m, unsigned int n,
  4516. uint32_t *ret_m, uint32_t *ret_n)
  4517. {
  4518. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4519. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4520. intel_reduce_m_n_ratio(ret_m, ret_n);
  4521. }
  4522. void
  4523. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4524. int pixel_clock, int link_clock,
  4525. struct intel_link_m_n *m_n)
  4526. {
  4527. m_n->tu = 64;
  4528. compute_m_n(bits_per_pixel * pixel_clock,
  4529. link_clock * nlanes * 8,
  4530. &m_n->gmch_m, &m_n->gmch_n);
  4531. compute_m_n(pixel_clock, link_clock,
  4532. &m_n->link_m, &m_n->link_n);
  4533. }
  4534. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4535. {
  4536. if (i915.panel_use_ssc >= 0)
  4537. return i915.panel_use_ssc != 0;
  4538. return dev_priv->vbt.lvds_use_ssc
  4539. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4540. }
  4541. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  4542. {
  4543. struct drm_device *dev = crtc->dev;
  4544. struct drm_i915_private *dev_priv = dev->dev_private;
  4545. int refclk;
  4546. if (IS_VALLEYVIEW(dev)) {
  4547. refclk = 100000;
  4548. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4549. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4550. refclk = dev_priv->vbt.lvds_ssc_freq;
  4551. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4552. } else if (!IS_GEN2(dev)) {
  4553. refclk = 96000;
  4554. } else {
  4555. refclk = 48000;
  4556. }
  4557. return refclk;
  4558. }
  4559. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4560. {
  4561. return (1 << dpll->n) << 16 | dpll->m2;
  4562. }
  4563. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4564. {
  4565. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4566. }
  4567. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4568. intel_clock_t *reduced_clock)
  4569. {
  4570. struct drm_device *dev = crtc->base.dev;
  4571. u32 fp, fp2 = 0;
  4572. if (IS_PINEVIEW(dev)) {
  4573. fp = pnv_dpll_compute_fp(&crtc->config.dpll);
  4574. if (reduced_clock)
  4575. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4576. } else {
  4577. fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
  4578. if (reduced_clock)
  4579. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4580. }
  4581. crtc->config.dpll_hw_state.fp0 = fp;
  4582. crtc->lowfreq_avail = false;
  4583. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4584. reduced_clock && i915.powersave) {
  4585. crtc->config.dpll_hw_state.fp1 = fp2;
  4586. crtc->lowfreq_avail = true;
  4587. } else {
  4588. crtc->config.dpll_hw_state.fp1 = fp;
  4589. }
  4590. }
  4591. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4592. pipe)
  4593. {
  4594. u32 reg_val;
  4595. /*
  4596. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4597. * and set it to a reasonable value instead.
  4598. */
  4599. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4600. reg_val &= 0xffffff00;
  4601. reg_val |= 0x00000030;
  4602. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4603. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4604. reg_val &= 0x8cffffff;
  4605. reg_val = 0x8c000000;
  4606. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4607. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4608. reg_val &= 0xffffff00;
  4609. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4610. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4611. reg_val &= 0x00ffffff;
  4612. reg_val |= 0xb0000000;
  4613. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4614. }
  4615. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4616. struct intel_link_m_n *m_n)
  4617. {
  4618. struct drm_device *dev = crtc->base.dev;
  4619. struct drm_i915_private *dev_priv = dev->dev_private;
  4620. int pipe = crtc->pipe;
  4621. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4622. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4623. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4624. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4625. }
  4626. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4627. struct intel_link_m_n *m_n)
  4628. {
  4629. struct drm_device *dev = crtc->base.dev;
  4630. struct drm_i915_private *dev_priv = dev->dev_private;
  4631. int pipe = crtc->pipe;
  4632. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4633. if (INTEL_INFO(dev)->gen >= 5) {
  4634. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4635. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4636. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4637. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4638. } else {
  4639. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4640. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4641. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4642. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4643. }
  4644. }
  4645. static void intel_dp_set_m_n(struct intel_crtc *crtc)
  4646. {
  4647. if (crtc->config.has_pch_encoder)
  4648. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4649. else
  4650. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4651. }
  4652. static void vlv_update_pll(struct intel_crtc *crtc)
  4653. {
  4654. u32 dpll, dpll_md;
  4655. /*
  4656. * Enable DPIO clock input. We should never disable the reference
  4657. * clock for pipe B, since VGA hotplug / manual detection depends
  4658. * on it.
  4659. */
  4660. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4661. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4662. /* We should never disable this, set it here for state tracking */
  4663. if (crtc->pipe == PIPE_B)
  4664. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4665. dpll |= DPLL_VCO_ENABLE;
  4666. crtc->config.dpll_hw_state.dpll = dpll;
  4667. dpll_md = (crtc->config.pixel_multiplier - 1)
  4668. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4669. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4670. }
  4671. static void vlv_prepare_pll(struct intel_crtc *crtc)
  4672. {
  4673. struct drm_device *dev = crtc->base.dev;
  4674. struct drm_i915_private *dev_priv = dev->dev_private;
  4675. int pipe = crtc->pipe;
  4676. u32 mdiv;
  4677. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4678. u32 coreclk, reg_val;
  4679. mutex_lock(&dev_priv->dpio_lock);
  4680. bestn = crtc->config.dpll.n;
  4681. bestm1 = crtc->config.dpll.m1;
  4682. bestm2 = crtc->config.dpll.m2;
  4683. bestp1 = crtc->config.dpll.p1;
  4684. bestp2 = crtc->config.dpll.p2;
  4685. /* See eDP HDMI DPIO driver vbios notes doc */
  4686. /* PLL B needs special handling */
  4687. if (pipe == PIPE_B)
  4688. vlv_pllb_recal_opamp(dev_priv, pipe);
  4689. /* Set up Tx target for periodic Rcomp update */
  4690. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4691. /* Disable target IRef on PLL */
  4692. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4693. reg_val &= 0x00ffffff;
  4694. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4695. /* Disable fast lock */
  4696. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4697. /* Set idtafcrecal before PLL is enabled */
  4698. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4699. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4700. mdiv |= ((bestn << DPIO_N_SHIFT));
  4701. mdiv |= (1 << DPIO_K_SHIFT);
  4702. /*
  4703. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4704. * but we don't support that).
  4705. * Note: don't use the DAC post divider as it seems unstable.
  4706. */
  4707. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4708. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4709. mdiv |= DPIO_ENABLE_CALIBRATION;
  4710. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4711. /* Set HBR and RBR LPF coefficients */
  4712. if (crtc->config.port_clock == 162000 ||
  4713. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
  4714. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
  4715. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4716. 0x009f0003);
  4717. else
  4718. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4719. 0x00d0000f);
  4720. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
  4721. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
  4722. /* Use SSC source */
  4723. if (pipe == PIPE_A)
  4724. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4725. 0x0df40000);
  4726. else
  4727. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4728. 0x0df70000);
  4729. } else { /* HDMI or VGA */
  4730. /* Use bend source */
  4731. if (pipe == PIPE_A)
  4732. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4733. 0x0df70000);
  4734. else
  4735. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4736. 0x0df40000);
  4737. }
  4738. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  4739. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  4740. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
  4741. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
  4742. coreclk |= 0x01000000;
  4743. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  4744. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  4745. mutex_unlock(&dev_priv->dpio_lock);
  4746. }
  4747. static void chv_update_pll(struct intel_crtc *crtc)
  4748. {
  4749. struct drm_device *dev = crtc->base.dev;
  4750. struct drm_i915_private *dev_priv = dev->dev_private;
  4751. int pipe = crtc->pipe;
  4752. int dpll_reg = DPLL(crtc->pipe);
  4753. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  4754. u32 loopfilter, intcoeff;
  4755. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  4756. int refclk;
  4757. crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  4758. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  4759. DPLL_VCO_ENABLE;
  4760. if (pipe != PIPE_A)
  4761. crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4762. crtc->config.dpll_hw_state.dpll_md =
  4763. (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4764. bestn = crtc->config.dpll.n;
  4765. bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
  4766. bestm1 = crtc->config.dpll.m1;
  4767. bestm2 = crtc->config.dpll.m2 >> 22;
  4768. bestp1 = crtc->config.dpll.p1;
  4769. bestp2 = crtc->config.dpll.p2;
  4770. /*
  4771. * Enable Refclk and SSC
  4772. */
  4773. I915_WRITE(dpll_reg,
  4774. crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  4775. mutex_lock(&dev_priv->dpio_lock);
  4776. /* p1 and p2 divider */
  4777. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  4778. 5 << DPIO_CHV_S1_DIV_SHIFT |
  4779. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  4780. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  4781. 1 << DPIO_CHV_K_DIV_SHIFT);
  4782. /* Feedback post-divider - m2 */
  4783. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  4784. /* Feedback refclk divider - n and m1 */
  4785. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  4786. DPIO_CHV_M1_DIV_BY_2 |
  4787. 1 << DPIO_CHV_N_DIV_SHIFT);
  4788. /* M2 fraction division */
  4789. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  4790. /* M2 fraction division enable */
  4791. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  4792. DPIO_CHV_FRAC_DIV_EN |
  4793. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  4794. /* Loop filter */
  4795. refclk = i9xx_get_refclk(&crtc->base, 0);
  4796. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  4797. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  4798. if (refclk == 100000)
  4799. intcoeff = 11;
  4800. else if (refclk == 38400)
  4801. intcoeff = 10;
  4802. else
  4803. intcoeff = 9;
  4804. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  4805. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  4806. /* AFC Recal */
  4807. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  4808. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  4809. DPIO_AFC_RECAL);
  4810. mutex_unlock(&dev_priv->dpio_lock);
  4811. }
  4812. static void i9xx_update_pll(struct intel_crtc *crtc,
  4813. intel_clock_t *reduced_clock,
  4814. int num_connectors)
  4815. {
  4816. struct drm_device *dev = crtc->base.dev;
  4817. struct drm_i915_private *dev_priv = dev->dev_private;
  4818. u32 dpll;
  4819. bool is_sdvo;
  4820. struct dpll *clock = &crtc->config.dpll;
  4821. i9xx_update_pll_dividers(crtc, reduced_clock);
  4822. is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
  4823. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  4824. dpll = DPLL_VGA_MODE_DIS;
  4825. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
  4826. dpll |= DPLLB_MODE_LVDS;
  4827. else
  4828. dpll |= DPLLB_MODE_DAC_SERIAL;
  4829. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  4830. dpll |= (crtc->config.pixel_multiplier - 1)
  4831. << SDVO_MULTIPLIER_SHIFT_HIRES;
  4832. }
  4833. if (is_sdvo)
  4834. dpll |= DPLL_SDVO_HIGH_SPEED;
  4835. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
  4836. dpll |= DPLL_SDVO_HIGH_SPEED;
  4837. /* compute bitmask from p1 value */
  4838. if (IS_PINEVIEW(dev))
  4839. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4840. else {
  4841. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4842. if (IS_G4X(dev) && reduced_clock)
  4843. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4844. }
  4845. switch (clock->p2) {
  4846. case 5:
  4847. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4848. break;
  4849. case 7:
  4850. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4851. break;
  4852. case 10:
  4853. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4854. break;
  4855. case 14:
  4856. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4857. break;
  4858. }
  4859. if (INTEL_INFO(dev)->gen >= 4)
  4860. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4861. if (crtc->config.sdvo_tv_clock)
  4862. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4863. else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4864. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4865. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4866. else
  4867. dpll |= PLL_REF_INPUT_DREFCLK;
  4868. dpll |= DPLL_VCO_ENABLE;
  4869. crtc->config.dpll_hw_state.dpll = dpll;
  4870. if (INTEL_INFO(dev)->gen >= 4) {
  4871. u32 dpll_md = (crtc->config.pixel_multiplier - 1)
  4872. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4873. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4874. }
  4875. }
  4876. static void i8xx_update_pll(struct intel_crtc *crtc,
  4877. intel_clock_t *reduced_clock,
  4878. int num_connectors)
  4879. {
  4880. struct drm_device *dev = crtc->base.dev;
  4881. struct drm_i915_private *dev_priv = dev->dev_private;
  4882. u32 dpll;
  4883. struct dpll *clock = &crtc->config.dpll;
  4884. i9xx_update_pll_dividers(crtc, reduced_clock);
  4885. dpll = DPLL_VGA_MODE_DIS;
  4886. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
  4887. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4888. } else {
  4889. if (clock->p1 == 2)
  4890. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4891. else
  4892. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4893. if (clock->p2 == 4)
  4894. dpll |= PLL_P2_DIVIDE_BY_4;
  4895. }
  4896. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
  4897. dpll |= DPLL_DVO_2X_MODE;
  4898. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4899. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4900. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4901. else
  4902. dpll |= PLL_REF_INPUT_DREFCLK;
  4903. dpll |= DPLL_VCO_ENABLE;
  4904. crtc->config.dpll_hw_state.dpll = dpll;
  4905. }
  4906. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  4907. {
  4908. struct drm_device *dev = intel_crtc->base.dev;
  4909. struct drm_i915_private *dev_priv = dev->dev_private;
  4910. enum pipe pipe = intel_crtc->pipe;
  4911. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  4912. struct drm_display_mode *adjusted_mode =
  4913. &intel_crtc->config.adjusted_mode;
  4914. uint32_t crtc_vtotal, crtc_vblank_end;
  4915. int vsyncshift = 0;
  4916. /* We need to be careful not to changed the adjusted mode, for otherwise
  4917. * the hw state checker will get angry at the mismatch. */
  4918. crtc_vtotal = adjusted_mode->crtc_vtotal;
  4919. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  4920. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4921. /* the chip adds 2 halflines automatically */
  4922. crtc_vtotal -= 1;
  4923. crtc_vblank_end -= 1;
  4924. if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  4925. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  4926. else
  4927. vsyncshift = adjusted_mode->crtc_hsync_start -
  4928. adjusted_mode->crtc_htotal / 2;
  4929. if (vsyncshift < 0)
  4930. vsyncshift += adjusted_mode->crtc_htotal;
  4931. }
  4932. if (INTEL_INFO(dev)->gen > 3)
  4933. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  4934. I915_WRITE(HTOTAL(cpu_transcoder),
  4935. (adjusted_mode->crtc_hdisplay - 1) |
  4936. ((adjusted_mode->crtc_htotal - 1) << 16));
  4937. I915_WRITE(HBLANK(cpu_transcoder),
  4938. (adjusted_mode->crtc_hblank_start - 1) |
  4939. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4940. I915_WRITE(HSYNC(cpu_transcoder),
  4941. (adjusted_mode->crtc_hsync_start - 1) |
  4942. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4943. I915_WRITE(VTOTAL(cpu_transcoder),
  4944. (adjusted_mode->crtc_vdisplay - 1) |
  4945. ((crtc_vtotal - 1) << 16));
  4946. I915_WRITE(VBLANK(cpu_transcoder),
  4947. (adjusted_mode->crtc_vblank_start - 1) |
  4948. ((crtc_vblank_end - 1) << 16));
  4949. I915_WRITE(VSYNC(cpu_transcoder),
  4950. (adjusted_mode->crtc_vsync_start - 1) |
  4951. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4952. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  4953. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  4954. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  4955. * bits. */
  4956. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  4957. (pipe == PIPE_B || pipe == PIPE_C))
  4958. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  4959. /* pipesrc controls the size that is scaled from, which should
  4960. * always be the user's requested size.
  4961. */
  4962. I915_WRITE(PIPESRC(pipe),
  4963. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  4964. (intel_crtc->config.pipe_src_h - 1));
  4965. }
  4966. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  4967. struct intel_crtc_config *pipe_config)
  4968. {
  4969. struct drm_device *dev = crtc->base.dev;
  4970. struct drm_i915_private *dev_priv = dev->dev_private;
  4971. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  4972. uint32_t tmp;
  4973. tmp = I915_READ(HTOTAL(cpu_transcoder));
  4974. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  4975. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  4976. tmp = I915_READ(HBLANK(cpu_transcoder));
  4977. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  4978. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  4979. tmp = I915_READ(HSYNC(cpu_transcoder));
  4980. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  4981. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  4982. tmp = I915_READ(VTOTAL(cpu_transcoder));
  4983. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  4984. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  4985. tmp = I915_READ(VBLANK(cpu_transcoder));
  4986. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  4987. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  4988. tmp = I915_READ(VSYNC(cpu_transcoder));
  4989. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  4990. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  4991. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  4992. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  4993. pipe_config->adjusted_mode.crtc_vtotal += 1;
  4994. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  4995. }
  4996. tmp = I915_READ(PIPESRC(crtc->pipe));
  4997. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  4998. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  4999. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  5000. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  5001. }
  5002. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5003. struct intel_crtc_config *pipe_config)
  5004. {
  5005. mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  5006. mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
  5007. mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  5008. mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  5009. mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  5010. mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  5011. mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  5012. mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  5013. mode->flags = pipe_config->adjusted_mode.flags;
  5014. mode->clock = pipe_config->adjusted_mode.crtc_clock;
  5015. mode->flags |= pipe_config->adjusted_mode.flags;
  5016. }
  5017. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5018. {
  5019. struct drm_device *dev = intel_crtc->base.dev;
  5020. struct drm_i915_private *dev_priv = dev->dev_private;
  5021. uint32_t pipeconf;
  5022. pipeconf = 0;
  5023. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  5024. I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
  5025. pipeconf |= PIPECONF_ENABLE;
  5026. if (intel_crtc->config.double_wide)
  5027. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5028. /* only g4x and later have fancy bpc/dither controls */
  5029. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5030. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5031. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  5032. pipeconf |= PIPECONF_DITHER_EN |
  5033. PIPECONF_DITHER_TYPE_SP;
  5034. switch (intel_crtc->config.pipe_bpp) {
  5035. case 18:
  5036. pipeconf |= PIPECONF_6BPC;
  5037. break;
  5038. case 24:
  5039. pipeconf |= PIPECONF_8BPC;
  5040. break;
  5041. case 30:
  5042. pipeconf |= PIPECONF_10BPC;
  5043. break;
  5044. default:
  5045. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5046. BUG();
  5047. }
  5048. }
  5049. if (HAS_PIPE_CXSR(dev)) {
  5050. if (intel_crtc->lowfreq_avail) {
  5051. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5052. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5053. } else {
  5054. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5055. }
  5056. }
  5057. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5058. if (INTEL_INFO(dev)->gen < 4 ||
  5059. intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  5060. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5061. else
  5062. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5063. } else
  5064. pipeconf |= PIPECONF_PROGRESSIVE;
  5065. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  5066. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5067. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5068. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5069. }
  5070. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  5071. int x, int y,
  5072. struct drm_framebuffer *fb)
  5073. {
  5074. struct drm_device *dev = crtc->dev;
  5075. struct drm_i915_private *dev_priv = dev->dev_private;
  5076. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5077. int refclk, num_connectors = 0;
  5078. intel_clock_t clock, reduced_clock;
  5079. bool ok, has_reduced_clock = false;
  5080. bool is_lvds = false, is_dsi = false;
  5081. struct intel_encoder *encoder;
  5082. const intel_limit_t *limit;
  5083. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5084. switch (encoder->type) {
  5085. case INTEL_OUTPUT_LVDS:
  5086. is_lvds = true;
  5087. break;
  5088. case INTEL_OUTPUT_DSI:
  5089. is_dsi = true;
  5090. break;
  5091. }
  5092. num_connectors++;
  5093. }
  5094. if (is_dsi)
  5095. return 0;
  5096. if (!intel_crtc->config.clock_set) {
  5097. refclk = i9xx_get_refclk(crtc, num_connectors);
  5098. /*
  5099. * Returns a set of divisors for the desired target clock with
  5100. * the given refclk, or FALSE. The returned values represent
  5101. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5102. * 2) / p1 / p2.
  5103. */
  5104. limit = intel_limit(crtc, refclk);
  5105. ok = dev_priv->display.find_dpll(limit, crtc,
  5106. intel_crtc->config.port_clock,
  5107. refclk, NULL, &clock);
  5108. if (!ok) {
  5109. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5110. return -EINVAL;
  5111. }
  5112. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5113. /*
  5114. * Ensure we match the reduced clock's P to the target
  5115. * clock. If the clocks don't match, we can't switch
  5116. * the display clock by using the FP0/FP1. In such case
  5117. * we will disable the LVDS downclock feature.
  5118. */
  5119. has_reduced_clock =
  5120. dev_priv->display.find_dpll(limit, crtc,
  5121. dev_priv->lvds_downclock,
  5122. refclk, &clock,
  5123. &reduced_clock);
  5124. }
  5125. /* Compat-code for transition, will disappear. */
  5126. intel_crtc->config.dpll.n = clock.n;
  5127. intel_crtc->config.dpll.m1 = clock.m1;
  5128. intel_crtc->config.dpll.m2 = clock.m2;
  5129. intel_crtc->config.dpll.p1 = clock.p1;
  5130. intel_crtc->config.dpll.p2 = clock.p2;
  5131. }
  5132. if (IS_GEN2(dev)) {
  5133. i8xx_update_pll(intel_crtc,
  5134. has_reduced_clock ? &reduced_clock : NULL,
  5135. num_connectors);
  5136. } else if (IS_CHERRYVIEW(dev)) {
  5137. chv_update_pll(intel_crtc);
  5138. } else if (IS_VALLEYVIEW(dev)) {
  5139. vlv_update_pll(intel_crtc);
  5140. } else {
  5141. i9xx_update_pll(intel_crtc,
  5142. has_reduced_clock ? &reduced_clock : NULL,
  5143. num_connectors);
  5144. }
  5145. return 0;
  5146. }
  5147. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5148. struct intel_crtc_config *pipe_config)
  5149. {
  5150. struct drm_device *dev = crtc->base.dev;
  5151. struct drm_i915_private *dev_priv = dev->dev_private;
  5152. uint32_t tmp;
  5153. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5154. return;
  5155. tmp = I915_READ(PFIT_CONTROL);
  5156. if (!(tmp & PFIT_ENABLE))
  5157. return;
  5158. /* Check whether the pfit is attached to our pipe. */
  5159. if (INTEL_INFO(dev)->gen < 4) {
  5160. if (crtc->pipe != PIPE_B)
  5161. return;
  5162. } else {
  5163. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5164. return;
  5165. }
  5166. pipe_config->gmch_pfit.control = tmp;
  5167. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5168. if (INTEL_INFO(dev)->gen < 5)
  5169. pipe_config->gmch_pfit.lvds_border_bits =
  5170. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5171. }
  5172. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5173. struct intel_crtc_config *pipe_config)
  5174. {
  5175. struct drm_device *dev = crtc->base.dev;
  5176. struct drm_i915_private *dev_priv = dev->dev_private;
  5177. int pipe = pipe_config->cpu_transcoder;
  5178. intel_clock_t clock;
  5179. u32 mdiv;
  5180. int refclk = 100000;
  5181. /* In case of MIPI DPLL will not even be used */
  5182. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5183. return;
  5184. mutex_lock(&dev_priv->dpio_lock);
  5185. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5186. mutex_unlock(&dev_priv->dpio_lock);
  5187. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5188. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5189. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5190. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5191. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5192. vlv_clock(refclk, &clock);
  5193. /* clock.dot is the fast clock */
  5194. pipe_config->port_clock = clock.dot / 5;
  5195. }
  5196. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5197. struct intel_plane_config *plane_config)
  5198. {
  5199. struct drm_device *dev = crtc->base.dev;
  5200. struct drm_i915_private *dev_priv = dev->dev_private;
  5201. u32 val, base, offset;
  5202. int pipe = crtc->pipe, plane = crtc->plane;
  5203. int fourcc, pixel_format;
  5204. int aligned_height;
  5205. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5206. if (!crtc->base.primary->fb) {
  5207. DRM_DEBUG_KMS("failed to alloc fb\n");
  5208. return;
  5209. }
  5210. val = I915_READ(DSPCNTR(plane));
  5211. if (INTEL_INFO(dev)->gen >= 4)
  5212. if (val & DISPPLANE_TILED)
  5213. plane_config->tiled = true;
  5214. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5215. fourcc = intel_format_to_fourcc(pixel_format);
  5216. crtc->base.primary->fb->pixel_format = fourcc;
  5217. crtc->base.primary->fb->bits_per_pixel =
  5218. drm_format_plane_cpp(fourcc, 0) * 8;
  5219. if (INTEL_INFO(dev)->gen >= 4) {
  5220. if (plane_config->tiled)
  5221. offset = I915_READ(DSPTILEOFF(plane));
  5222. else
  5223. offset = I915_READ(DSPLINOFF(plane));
  5224. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5225. } else {
  5226. base = I915_READ(DSPADDR(plane));
  5227. }
  5228. plane_config->base = base;
  5229. val = I915_READ(PIPESRC(pipe));
  5230. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5231. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5232. val = I915_READ(DSPSTRIDE(pipe));
  5233. crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
  5234. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5235. plane_config->tiled);
  5236. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  5237. aligned_height);
  5238. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5239. pipe, plane, crtc->base.primary->fb->width,
  5240. crtc->base.primary->fb->height,
  5241. crtc->base.primary->fb->bits_per_pixel, base,
  5242. crtc->base.primary->fb->pitches[0],
  5243. plane_config->size);
  5244. }
  5245. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5246. struct intel_crtc_config *pipe_config)
  5247. {
  5248. struct drm_device *dev = crtc->base.dev;
  5249. struct drm_i915_private *dev_priv = dev->dev_private;
  5250. int pipe = pipe_config->cpu_transcoder;
  5251. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5252. intel_clock_t clock;
  5253. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5254. int refclk = 100000;
  5255. mutex_lock(&dev_priv->dpio_lock);
  5256. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5257. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5258. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5259. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5260. mutex_unlock(&dev_priv->dpio_lock);
  5261. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5262. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5263. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5264. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5265. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5266. chv_clock(refclk, &clock);
  5267. /* clock.dot is the fast clock */
  5268. pipe_config->port_clock = clock.dot / 5;
  5269. }
  5270. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5271. struct intel_crtc_config *pipe_config)
  5272. {
  5273. struct drm_device *dev = crtc->base.dev;
  5274. struct drm_i915_private *dev_priv = dev->dev_private;
  5275. uint32_t tmp;
  5276. if (!intel_display_power_enabled(dev_priv,
  5277. POWER_DOMAIN_PIPE(crtc->pipe)))
  5278. return false;
  5279. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5280. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5281. tmp = I915_READ(PIPECONF(crtc->pipe));
  5282. if (!(tmp & PIPECONF_ENABLE))
  5283. return false;
  5284. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5285. switch (tmp & PIPECONF_BPC_MASK) {
  5286. case PIPECONF_6BPC:
  5287. pipe_config->pipe_bpp = 18;
  5288. break;
  5289. case PIPECONF_8BPC:
  5290. pipe_config->pipe_bpp = 24;
  5291. break;
  5292. case PIPECONF_10BPC:
  5293. pipe_config->pipe_bpp = 30;
  5294. break;
  5295. default:
  5296. break;
  5297. }
  5298. }
  5299. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5300. pipe_config->limited_color_range = true;
  5301. if (INTEL_INFO(dev)->gen < 4)
  5302. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5303. intel_get_pipe_timings(crtc, pipe_config);
  5304. i9xx_get_pfit_config(crtc, pipe_config);
  5305. if (INTEL_INFO(dev)->gen >= 4) {
  5306. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5307. pipe_config->pixel_multiplier =
  5308. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5309. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5310. pipe_config->dpll_hw_state.dpll_md = tmp;
  5311. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5312. tmp = I915_READ(DPLL(crtc->pipe));
  5313. pipe_config->pixel_multiplier =
  5314. ((tmp & SDVO_MULTIPLIER_MASK)
  5315. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5316. } else {
  5317. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5318. * port and will be fixed up in the encoder->get_config
  5319. * function. */
  5320. pipe_config->pixel_multiplier = 1;
  5321. }
  5322. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5323. if (!IS_VALLEYVIEW(dev)) {
  5324. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5325. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5326. } else {
  5327. /* Mask out read-only status bits. */
  5328. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5329. DPLL_PORTC_READY_MASK |
  5330. DPLL_PORTB_READY_MASK);
  5331. }
  5332. if (IS_CHERRYVIEW(dev))
  5333. chv_crtc_clock_get(crtc, pipe_config);
  5334. else if (IS_VALLEYVIEW(dev))
  5335. vlv_crtc_clock_get(crtc, pipe_config);
  5336. else
  5337. i9xx_crtc_clock_get(crtc, pipe_config);
  5338. return true;
  5339. }
  5340. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5341. {
  5342. struct drm_i915_private *dev_priv = dev->dev_private;
  5343. struct drm_mode_config *mode_config = &dev->mode_config;
  5344. struct intel_encoder *encoder;
  5345. u32 val, final;
  5346. bool has_lvds = false;
  5347. bool has_cpu_edp = false;
  5348. bool has_panel = false;
  5349. bool has_ck505 = false;
  5350. bool can_ssc = false;
  5351. /* We need to take the global config into account */
  5352. list_for_each_entry(encoder, &mode_config->encoder_list,
  5353. base.head) {
  5354. switch (encoder->type) {
  5355. case INTEL_OUTPUT_LVDS:
  5356. has_panel = true;
  5357. has_lvds = true;
  5358. break;
  5359. case INTEL_OUTPUT_EDP:
  5360. has_panel = true;
  5361. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5362. has_cpu_edp = true;
  5363. break;
  5364. }
  5365. }
  5366. if (HAS_PCH_IBX(dev)) {
  5367. has_ck505 = dev_priv->vbt.display_clock_mode;
  5368. can_ssc = has_ck505;
  5369. } else {
  5370. has_ck505 = false;
  5371. can_ssc = true;
  5372. }
  5373. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5374. has_panel, has_lvds, has_ck505);
  5375. /* Ironlake: try to setup display ref clock before DPLL
  5376. * enabling. This is only under driver's control after
  5377. * PCH B stepping, previous chipset stepping should be
  5378. * ignoring this setting.
  5379. */
  5380. val = I915_READ(PCH_DREF_CONTROL);
  5381. /* As we must carefully and slowly disable/enable each source in turn,
  5382. * compute the final state we want first and check if we need to
  5383. * make any changes at all.
  5384. */
  5385. final = val;
  5386. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5387. if (has_ck505)
  5388. final |= DREF_NONSPREAD_CK505_ENABLE;
  5389. else
  5390. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5391. final &= ~DREF_SSC_SOURCE_MASK;
  5392. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5393. final &= ~DREF_SSC1_ENABLE;
  5394. if (has_panel) {
  5395. final |= DREF_SSC_SOURCE_ENABLE;
  5396. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5397. final |= DREF_SSC1_ENABLE;
  5398. if (has_cpu_edp) {
  5399. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5400. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5401. else
  5402. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5403. } else
  5404. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5405. } else {
  5406. final |= DREF_SSC_SOURCE_DISABLE;
  5407. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5408. }
  5409. if (final == val)
  5410. return;
  5411. /* Always enable nonspread source */
  5412. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5413. if (has_ck505)
  5414. val |= DREF_NONSPREAD_CK505_ENABLE;
  5415. else
  5416. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5417. if (has_panel) {
  5418. val &= ~DREF_SSC_SOURCE_MASK;
  5419. val |= DREF_SSC_SOURCE_ENABLE;
  5420. /* SSC must be turned on before enabling the CPU output */
  5421. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5422. DRM_DEBUG_KMS("Using SSC on panel\n");
  5423. val |= DREF_SSC1_ENABLE;
  5424. } else
  5425. val &= ~DREF_SSC1_ENABLE;
  5426. /* Get SSC going before enabling the outputs */
  5427. I915_WRITE(PCH_DREF_CONTROL, val);
  5428. POSTING_READ(PCH_DREF_CONTROL);
  5429. udelay(200);
  5430. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5431. /* Enable CPU source on CPU attached eDP */
  5432. if (has_cpu_edp) {
  5433. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5434. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5435. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5436. } else
  5437. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5438. } else
  5439. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5440. I915_WRITE(PCH_DREF_CONTROL, val);
  5441. POSTING_READ(PCH_DREF_CONTROL);
  5442. udelay(200);
  5443. } else {
  5444. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5445. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5446. /* Turn off CPU output */
  5447. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5448. I915_WRITE(PCH_DREF_CONTROL, val);
  5449. POSTING_READ(PCH_DREF_CONTROL);
  5450. udelay(200);
  5451. /* Turn off the SSC source */
  5452. val &= ~DREF_SSC_SOURCE_MASK;
  5453. val |= DREF_SSC_SOURCE_DISABLE;
  5454. /* Turn off SSC1 */
  5455. val &= ~DREF_SSC1_ENABLE;
  5456. I915_WRITE(PCH_DREF_CONTROL, val);
  5457. POSTING_READ(PCH_DREF_CONTROL);
  5458. udelay(200);
  5459. }
  5460. BUG_ON(val != final);
  5461. }
  5462. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5463. {
  5464. uint32_t tmp;
  5465. tmp = I915_READ(SOUTH_CHICKEN2);
  5466. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5467. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5468. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5469. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5470. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5471. tmp = I915_READ(SOUTH_CHICKEN2);
  5472. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5473. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5474. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5475. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5476. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5477. }
  5478. /* WaMPhyProgramming:hsw */
  5479. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5480. {
  5481. uint32_t tmp;
  5482. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5483. tmp &= ~(0xFF << 24);
  5484. tmp |= (0x12 << 24);
  5485. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5486. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5487. tmp |= (1 << 11);
  5488. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5489. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5490. tmp |= (1 << 11);
  5491. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5492. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5493. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5494. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5495. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5496. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5497. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5498. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5499. tmp &= ~(7 << 13);
  5500. tmp |= (5 << 13);
  5501. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5502. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5503. tmp &= ~(7 << 13);
  5504. tmp |= (5 << 13);
  5505. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5506. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5507. tmp &= ~0xFF;
  5508. tmp |= 0x1C;
  5509. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5510. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5511. tmp &= ~0xFF;
  5512. tmp |= 0x1C;
  5513. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5514. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5515. tmp &= ~(0xFF << 16);
  5516. tmp |= (0x1C << 16);
  5517. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5518. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5519. tmp &= ~(0xFF << 16);
  5520. tmp |= (0x1C << 16);
  5521. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5522. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5523. tmp |= (1 << 27);
  5524. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5525. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5526. tmp |= (1 << 27);
  5527. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5528. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5529. tmp &= ~(0xF << 28);
  5530. tmp |= (4 << 28);
  5531. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5532. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5533. tmp &= ~(0xF << 28);
  5534. tmp |= (4 << 28);
  5535. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5536. }
  5537. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5538. * Programming" based on the parameters passed:
  5539. * - Sequence to enable CLKOUT_DP
  5540. * - Sequence to enable CLKOUT_DP without spread
  5541. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5542. */
  5543. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5544. bool with_fdi)
  5545. {
  5546. struct drm_i915_private *dev_priv = dev->dev_private;
  5547. uint32_t reg, tmp;
  5548. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5549. with_spread = true;
  5550. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5551. with_fdi, "LP PCH doesn't have FDI\n"))
  5552. with_fdi = false;
  5553. mutex_lock(&dev_priv->dpio_lock);
  5554. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5555. tmp &= ~SBI_SSCCTL_DISABLE;
  5556. tmp |= SBI_SSCCTL_PATHALT;
  5557. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5558. udelay(24);
  5559. if (with_spread) {
  5560. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5561. tmp &= ~SBI_SSCCTL_PATHALT;
  5562. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5563. if (with_fdi) {
  5564. lpt_reset_fdi_mphy(dev_priv);
  5565. lpt_program_fdi_mphy(dev_priv);
  5566. }
  5567. }
  5568. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5569. SBI_GEN0 : SBI_DBUFF0;
  5570. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5571. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5572. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5573. mutex_unlock(&dev_priv->dpio_lock);
  5574. }
  5575. /* Sequence to disable CLKOUT_DP */
  5576. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5577. {
  5578. struct drm_i915_private *dev_priv = dev->dev_private;
  5579. uint32_t reg, tmp;
  5580. mutex_lock(&dev_priv->dpio_lock);
  5581. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5582. SBI_GEN0 : SBI_DBUFF0;
  5583. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5584. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5585. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5586. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5587. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5588. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5589. tmp |= SBI_SSCCTL_PATHALT;
  5590. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5591. udelay(32);
  5592. }
  5593. tmp |= SBI_SSCCTL_DISABLE;
  5594. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5595. }
  5596. mutex_unlock(&dev_priv->dpio_lock);
  5597. }
  5598. static void lpt_init_pch_refclk(struct drm_device *dev)
  5599. {
  5600. struct drm_mode_config *mode_config = &dev->mode_config;
  5601. struct intel_encoder *encoder;
  5602. bool has_vga = false;
  5603. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  5604. switch (encoder->type) {
  5605. case INTEL_OUTPUT_ANALOG:
  5606. has_vga = true;
  5607. break;
  5608. }
  5609. }
  5610. if (has_vga)
  5611. lpt_enable_clkout_dp(dev, true, true);
  5612. else
  5613. lpt_disable_clkout_dp(dev);
  5614. }
  5615. /*
  5616. * Initialize reference clocks when the driver loads
  5617. */
  5618. void intel_init_pch_refclk(struct drm_device *dev)
  5619. {
  5620. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5621. ironlake_init_pch_refclk(dev);
  5622. else if (HAS_PCH_LPT(dev))
  5623. lpt_init_pch_refclk(dev);
  5624. }
  5625. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5626. {
  5627. struct drm_device *dev = crtc->dev;
  5628. struct drm_i915_private *dev_priv = dev->dev_private;
  5629. struct intel_encoder *encoder;
  5630. int num_connectors = 0;
  5631. bool is_lvds = false;
  5632. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5633. switch (encoder->type) {
  5634. case INTEL_OUTPUT_LVDS:
  5635. is_lvds = true;
  5636. break;
  5637. }
  5638. num_connectors++;
  5639. }
  5640. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5641. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5642. dev_priv->vbt.lvds_ssc_freq);
  5643. return dev_priv->vbt.lvds_ssc_freq;
  5644. }
  5645. return 120000;
  5646. }
  5647. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5648. {
  5649. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5650. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5651. int pipe = intel_crtc->pipe;
  5652. uint32_t val;
  5653. val = 0;
  5654. switch (intel_crtc->config.pipe_bpp) {
  5655. case 18:
  5656. val |= PIPECONF_6BPC;
  5657. break;
  5658. case 24:
  5659. val |= PIPECONF_8BPC;
  5660. break;
  5661. case 30:
  5662. val |= PIPECONF_10BPC;
  5663. break;
  5664. case 36:
  5665. val |= PIPECONF_12BPC;
  5666. break;
  5667. default:
  5668. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5669. BUG();
  5670. }
  5671. if (intel_crtc->config.dither)
  5672. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5673. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5674. val |= PIPECONF_INTERLACED_ILK;
  5675. else
  5676. val |= PIPECONF_PROGRESSIVE;
  5677. if (intel_crtc->config.limited_color_range)
  5678. val |= PIPECONF_COLOR_RANGE_SELECT;
  5679. I915_WRITE(PIPECONF(pipe), val);
  5680. POSTING_READ(PIPECONF(pipe));
  5681. }
  5682. /*
  5683. * Set up the pipe CSC unit.
  5684. *
  5685. * Currently only full range RGB to limited range RGB conversion
  5686. * is supported, but eventually this should handle various
  5687. * RGB<->YCbCr scenarios as well.
  5688. */
  5689. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  5690. {
  5691. struct drm_device *dev = crtc->dev;
  5692. struct drm_i915_private *dev_priv = dev->dev_private;
  5693. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5694. int pipe = intel_crtc->pipe;
  5695. uint16_t coeff = 0x7800; /* 1.0 */
  5696. /*
  5697. * TODO: Check what kind of values actually come out of the pipe
  5698. * with these coeff/postoff values and adjust to get the best
  5699. * accuracy. Perhaps we even need to take the bpc value into
  5700. * consideration.
  5701. */
  5702. if (intel_crtc->config.limited_color_range)
  5703. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  5704. /*
  5705. * GY/GU and RY/RU should be the other way around according
  5706. * to BSpec, but reality doesn't agree. Just set them up in
  5707. * a way that results in the correct picture.
  5708. */
  5709. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  5710. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  5711. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  5712. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  5713. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  5714. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  5715. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  5716. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  5717. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  5718. if (INTEL_INFO(dev)->gen > 6) {
  5719. uint16_t postoff = 0;
  5720. if (intel_crtc->config.limited_color_range)
  5721. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  5722. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  5723. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  5724. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  5725. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  5726. } else {
  5727. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  5728. if (intel_crtc->config.limited_color_range)
  5729. mode |= CSC_BLACK_SCREEN_OFFSET;
  5730. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  5731. }
  5732. }
  5733. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  5734. {
  5735. struct drm_device *dev = crtc->dev;
  5736. struct drm_i915_private *dev_priv = dev->dev_private;
  5737. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5738. enum pipe pipe = intel_crtc->pipe;
  5739. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5740. uint32_t val;
  5741. val = 0;
  5742. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  5743. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5744. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5745. val |= PIPECONF_INTERLACED_ILK;
  5746. else
  5747. val |= PIPECONF_PROGRESSIVE;
  5748. I915_WRITE(PIPECONF(cpu_transcoder), val);
  5749. POSTING_READ(PIPECONF(cpu_transcoder));
  5750. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  5751. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  5752. if (IS_BROADWELL(dev)) {
  5753. val = 0;
  5754. switch (intel_crtc->config.pipe_bpp) {
  5755. case 18:
  5756. val |= PIPEMISC_DITHER_6_BPC;
  5757. break;
  5758. case 24:
  5759. val |= PIPEMISC_DITHER_8_BPC;
  5760. break;
  5761. case 30:
  5762. val |= PIPEMISC_DITHER_10_BPC;
  5763. break;
  5764. case 36:
  5765. val |= PIPEMISC_DITHER_12_BPC;
  5766. break;
  5767. default:
  5768. /* Case prevented by pipe_config_set_bpp. */
  5769. BUG();
  5770. }
  5771. if (intel_crtc->config.dither)
  5772. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  5773. I915_WRITE(PIPEMISC(pipe), val);
  5774. }
  5775. }
  5776. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  5777. intel_clock_t *clock,
  5778. bool *has_reduced_clock,
  5779. intel_clock_t *reduced_clock)
  5780. {
  5781. struct drm_device *dev = crtc->dev;
  5782. struct drm_i915_private *dev_priv = dev->dev_private;
  5783. struct intel_encoder *intel_encoder;
  5784. int refclk;
  5785. const intel_limit_t *limit;
  5786. bool ret, is_lvds = false;
  5787. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5788. switch (intel_encoder->type) {
  5789. case INTEL_OUTPUT_LVDS:
  5790. is_lvds = true;
  5791. break;
  5792. }
  5793. }
  5794. refclk = ironlake_get_refclk(crtc);
  5795. /*
  5796. * Returns a set of divisors for the desired target clock with the given
  5797. * refclk, or FALSE. The returned values represent the clock equation:
  5798. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  5799. */
  5800. limit = intel_limit(crtc, refclk);
  5801. ret = dev_priv->display.find_dpll(limit, crtc,
  5802. to_intel_crtc(crtc)->config.port_clock,
  5803. refclk, NULL, clock);
  5804. if (!ret)
  5805. return false;
  5806. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5807. /*
  5808. * Ensure we match the reduced clock's P to the target clock.
  5809. * If the clocks don't match, we can't switch the display clock
  5810. * by using the FP0/FP1. In such case we will disable the LVDS
  5811. * downclock feature.
  5812. */
  5813. *has_reduced_clock =
  5814. dev_priv->display.find_dpll(limit, crtc,
  5815. dev_priv->lvds_downclock,
  5816. refclk, clock,
  5817. reduced_clock);
  5818. }
  5819. return true;
  5820. }
  5821. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  5822. {
  5823. /*
  5824. * Account for spread spectrum to avoid
  5825. * oversubscribing the link. Max center spread
  5826. * is 2.5%; use 5% for safety's sake.
  5827. */
  5828. u32 bps = target_clock * bpp * 21 / 20;
  5829. return DIV_ROUND_UP(bps, link_bw * 8);
  5830. }
  5831. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  5832. {
  5833. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  5834. }
  5835. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  5836. u32 *fp,
  5837. intel_clock_t *reduced_clock, u32 *fp2)
  5838. {
  5839. struct drm_crtc *crtc = &intel_crtc->base;
  5840. struct drm_device *dev = crtc->dev;
  5841. struct drm_i915_private *dev_priv = dev->dev_private;
  5842. struct intel_encoder *intel_encoder;
  5843. uint32_t dpll;
  5844. int factor, num_connectors = 0;
  5845. bool is_lvds = false, is_sdvo = false;
  5846. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5847. switch (intel_encoder->type) {
  5848. case INTEL_OUTPUT_LVDS:
  5849. is_lvds = true;
  5850. break;
  5851. case INTEL_OUTPUT_SDVO:
  5852. case INTEL_OUTPUT_HDMI:
  5853. is_sdvo = true;
  5854. break;
  5855. }
  5856. num_connectors++;
  5857. }
  5858. /* Enable autotuning of the PLL clock (if permissible) */
  5859. factor = 21;
  5860. if (is_lvds) {
  5861. if ((intel_panel_use_ssc(dev_priv) &&
  5862. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  5863. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  5864. factor = 25;
  5865. } else if (intel_crtc->config.sdvo_tv_clock)
  5866. factor = 20;
  5867. if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
  5868. *fp |= FP_CB_TUNE;
  5869. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  5870. *fp2 |= FP_CB_TUNE;
  5871. dpll = 0;
  5872. if (is_lvds)
  5873. dpll |= DPLLB_MODE_LVDS;
  5874. else
  5875. dpll |= DPLLB_MODE_DAC_SERIAL;
  5876. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  5877. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  5878. if (is_sdvo)
  5879. dpll |= DPLL_SDVO_HIGH_SPEED;
  5880. if (intel_crtc->config.has_dp_encoder)
  5881. dpll |= DPLL_SDVO_HIGH_SPEED;
  5882. /* compute bitmask from p1 value */
  5883. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5884. /* also FPA1 */
  5885. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5886. switch (intel_crtc->config.dpll.p2) {
  5887. case 5:
  5888. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5889. break;
  5890. case 7:
  5891. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5892. break;
  5893. case 10:
  5894. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5895. break;
  5896. case 14:
  5897. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5898. break;
  5899. }
  5900. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5901. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5902. else
  5903. dpll |= PLL_REF_INPUT_DREFCLK;
  5904. return dpll | DPLL_VCO_ENABLE;
  5905. }
  5906. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  5907. int x, int y,
  5908. struct drm_framebuffer *fb)
  5909. {
  5910. struct drm_device *dev = crtc->dev;
  5911. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5912. int num_connectors = 0;
  5913. intel_clock_t clock, reduced_clock;
  5914. u32 dpll = 0, fp = 0, fp2 = 0;
  5915. bool ok, has_reduced_clock = false;
  5916. bool is_lvds = false;
  5917. struct intel_encoder *encoder;
  5918. struct intel_shared_dpll *pll;
  5919. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5920. switch (encoder->type) {
  5921. case INTEL_OUTPUT_LVDS:
  5922. is_lvds = true;
  5923. break;
  5924. }
  5925. num_connectors++;
  5926. }
  5927. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  5928. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  5929. ok = ironlake_compute_clocks(crtc, &clock,
  5930. &has_reduced_clock, &reduced_clock);
  5931. if (!ok && !intel_crtc->config.clock_set) {
  5932. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5933. return -EINVAL;
  5934. }
  5935. /* Compat-code for transition, will disappear. */
  5936. if (!intel_crtc->config.clock_set) {
  5937. intel_crtc->config.dpll.n = clock.n;
  5938. intel_crtc->config.dpll.m1 = clock.m1;
  5939. intel_crtc->config.dpll.m2 = clock.m2;
  5940. intel_crtc->config.dpll.p1 = clock.p1;
  5941. intel_crtc->config.dpll.p2 = clock.p2;
  5942. }
  5943. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  5944. if (intel_crtc->config.has_pch_encoder) {
  5945. fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
  5946. if (has_reduced_clock)
  5947. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  5948. dpll = ironlake_compute_dpll(intel_crtc,
  5949. &fp, &reduced_clock,
  5950. has_reduced_clock ? &fp2 : NULL);
  5951. intel_crtc->config.dpll_hw_state.dpll = dpll;
  5952. intel_crtc->config.dpll_hw_state.fp0 = fp;
  5953. if (has_reduced_clock)
  5954. intel_crtc->config.dpll_hw_state.fp1 = fp2;
  5955. else
  5956. intel_crtc->config.dpll_hw_state.fp1 = fp;
  5957. pll = intel_get_shared_dpll(intel_crtc);
  5958. if (pll == NULL) {
  5959. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  5960. pipe_name(intel_crtc->pipe));
  5961. return -EINVAL;
  5962. }
  5963. } else
  5964. intel_put_shared_dpll(intel_crtc);
  5965. if (is_lvds && has_reduced_clock && i915.powersave)
  5966. intel_crtc->lowfreq_avail = true;
  5967. else
  5968. intel_crtc->lowfreq_avail = false;
  5969. return 0;
  5970. }
  5971. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  5972. struct intel_link_m_n *m_n)
  5973. {
  5974. struct drm_device *dev = crtc->base.dev;
  5975. struct drm_i915_private *dev_priv = dev->dev_private;
  5976. enum pipe pipe = crtc->pipe;
  5977. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  5978. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  5979. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  5980. & ~TU_SIZE_MASK;
  5981. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  5982. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  5983. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5984. }
  5985. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  5986. enum transcoder transcoder,
  5987. struct intel_link_m_n *m_n)
  5988. {
  5989. struct drm_device *dev = crtc->base.dev;
  5990. struct drm_i915_private *dev_priv = dev->dev_private;
  5991. enum pipe pipe = crtc->pipe;
  5992. if (INTEL_INFO(dev)->gen >= 5) {
  5993. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  5994. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  5995. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  5996. & ~TU_SIZE_MASK;
  5997. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  5998. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  5999. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6000. } else {
  6001. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6002. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6003. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6004. & ~TU_SIZE_MASK;
  6005. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6006. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6007. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6008. }
  6009. }
  6010. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6011. struct intel_crtc_config *pipe_config)
  6012. {
  6013. if (crtc->config.has_pch_encoder)
  6014. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6015. else
  6016. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6017. &pipe_config->dp_m_n);
  6018. }
  6019. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6020. struct intel_crtc_config *pipe_config)
  6021. {
  6022. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6023. &pipe_config->fdi_m_n);
  6024. }
  6025. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6026. struct intel_crtc_config *pipe_config)
  6027. {
  6028. struct drm_device *dev = crtc->base.dev;
  6029. struct drm_i915_private *dev_priv = dev->dev_private;
  6030. uint32_t tmp;
  6031. tmp = I915_READ(PF_CTL(crtc->pipe));
  6032. if (tmp & PF_ENABLE) {
  6033. pipe_config->pch_pfit.enabled = true;
  6034. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6035. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6036. /* We currently do not free assignements of panel fitters on
  6037. * ivb/hsw (since we don't use the higher upscaling modes which
  6038. * differentiates them) so just WARN about this case for now. */
  6039. if (IS_GEN7(dev)) {
  6040. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6041. PF_PIPE_SEL_IVB(crtc->pipe));
  6042. }
  6043. }
  6044. }
  6045. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  6046. struct intel_plane_config *plane_config)
  6047. {
  6048. struct drm_device *dev = crtc->base.dev;
  6049. struct drm_i915_private *dev_priv = dev->dev_private;
  6050. u32 val, base, offset;
  6051. int pipe = crtc->pipe, plane = crtc->plane;
  6052. int fourcc, pixel_format;
  6053. int aligned_height;
  6054. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  6055. if (!crtc->base.primary->fb) {
  6056. DRM_DEBUG_KMS("failed to alloc fb\n");
  6057. return;
  6058. }
  6059. val = I915_READ(DSPCNTR(plane));
  6060. if (INTEL_INFO(dev)->gen >= 4)
  6061. if (val & DISPPLANE_TILED)
  6062. plane_config->tiled = true;
  6063. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6064. fourcc = intel_format_to_fourcc(pixel_format);
  6065. crtc->base.primary->fb->pixel_format = fourcc;
  6066. crtc->base.primary->fb->bits_per_pixel =
  6067. drm_format_plane_cpp(fourcc, 0) * 8;
  6068. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6069. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6070. offset = I915_READ(DSPOFFSET(plane));
  6071. } else {
  6072. if (plane_config->tiled)
  6073. offset = I915_READ(DSPTILEOFF(plane));
  6074. else
  6075. offset = I915_READ(DSPLINOFF(plane));
  6076. }
  6077. plane_config->base = base;
  6078. val = I915_READ(PIPESRC(pipe));
  6079. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  6080. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  6081. val = I915_READ(DSPSTRIDE(pipe));
  6082. crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
  6083. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  6084. plane_config->tiled);
  6085. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  6086. aligned_height);
  6087. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6088. pipe, plane, crtc->base.primary->fb->width,
  6089. crtc->base.primary->fb->height,
  6090. crtc->base.primary->fb->bits_per_pixel, base,
  6091. crtc->base.primary->fb->pitches[0],
  6092. plane_config->size);
  6093. }
  6094. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6095. struct intel_crtc_config *pipe_config)
  6096. {
  6097. struct drm_device *dev = crtc->base.dev;
  6098. struct drm_i915_private *dev_priv = dev->dev_private;
  6099. uint32_t tmp;
  6100. if (!intel_display_power_enabled(dev_priv,
  6101. POWER_DOMAIN_PIPE(crtc->pipe)))
  6102. return false;
  6103. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6104. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6105. tmp = I915_READ(PIPECONF(crtc->pipe));
  6106. if (!(tmp & PIPECONF_ENABLE))
  6107. return false;
  6108. switch (tmp & PIPECONF_BPC_MASK) {
  6109. case PIPECONF_6BPC:
  6110. pipe_config->pipe_bpp = 18;
  6111. break;
  6112. case PIPECONF_8BPC:
  6113. pipe_config->pipe_bpp = 24;
  6114. break;
  6115. case PIPECONF_10BPC:
  6116. pipe_config->pipe_bpp = 30;
  6117. break;
  6118. case PIPECONF_12BPC:
  6119. pipe_config->pipe_bpp = 36;
  6120. break;
  6121. default:
  6122. break;
  6123. }
  6124. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6125. pipe_config->limited_color_range = true;
  6126. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6127. struct intel_shared_dpll *pll;
  6128. pipe_config->has_pch_encoder = true;
  6129. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6130. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6131. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6132. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6133. if (HAS_PCH_IBX(dev_priv->dev)) {
  6134. pipe_config->shared_dpll =
  6135. (enum intel_dpll_id) crtc->pipe;
  6136. } else {
  6137. tmp = I915_READ(PCH_DPLL_SEL);
  6138. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6139. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6140. else
  6141. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6142. }
  6143. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6144. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6145. &pipe_config->dpll_hw_state));
  6146. tmp = pipe_config->dpll_hw_state.dpll;
  6147. pipe_config->pixel_multiplier =
  6148. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6149. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6150. ironlake_pch_clock_get(crtc, pipe_config);
  6151. } else {
  6152. pipe_config->pixel_multiplier = 1;
  6153. }
  6154. intel_get_pipe_timings(crtc, pipe_config);
  6155. ironlake_get_pfit_config(crtc, pipe_config);
  6156. return true;
  6157. }
  6158. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6159. {
  6160. struct drm_device *dev = dev_priv->dev;
  6161. struct intel_crtc *crtc;
  6162. for_each_intel_crtc(dev, crtc)
  6163. WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6164. pipe_name(crtc->pipe));
  6165. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6166. WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6167. WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6168. WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6169. WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6170. WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6171. "CPU PWM1 enabled\n");
  6172. if (IS_HASWELL(dev))
  6173. WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6174. "CPU PWM2 enabled\n");
  6175. WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6176. "PCH PWM1 enabled\n");
  6177. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6178. "Utility pin enabled\n");
  6179. WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6180. /*
  6181. * In theory we can still leave IRQs enabled, as long as only the HPD
  6182. * interrupts remain enabled. We used to check for that, but since it's
  6183. * gen-specific and since we only disable LCPLL after we fully disable
  6184. * the interrupts, the check below should be enough.
  6185. */
  6186. WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6187. }
  6188. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6189. {
  6190. struct drm_device *dev = dev_priv->dev;
  6191. if (IS_HASWELL(dev))
  6192. return I915_READ(D_COMP_HSW);
  6193. else
  6194. return I915_READ(D_COMP_BDW);
  6195. }
  6196. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6197. {
  6198. struct drm_device *dev = dev_priv->dev;
  6199. if (IS_HASWELL(dev)) {
  6200. mutex_lock(&dev_priv->rps.hw_lock);
  6201. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6202. val))
  6203. DRM_ERROR("Failed to write to D_COMP\n");
  6204. mutex_unlock(&dev_priv->rps.hw_lock);
  6205. } else {
  6206. I915_WRITE(D_COMP_BDW, val);
  6207. POSTING_READ(D_COMP_BDW);
  6208. }
  6209. }
  6210. /*
  6211. * This function implements pieces of two sequences from BSpec:
  6212. * - Sequence for display software to disable LCPLL
  6213. * - Sequence for display software to allow package C8+
  6214. * The steps implemented here are just the steps that actually touch the LCPLL
  6215. * register. Callers should take care of disabling all the display engine
  6216. * functions, doing the mode unset, fixing interrupts, etc.
  6217. */
  6218. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6219. bool switch_to_fclk, bool allow_power_down)
  6220. {
  6221. uint32_t val;
  6222. assert_can_disable_lcpll(dev_priv);
  6223. val = I915_READ(LCPLL_CTL);
  6224. if (switch_to_fclk) {
  6225. val |= LCPLL_CD_SOURCE_FCLK;
  6226. I915_WRITE(LCPLL_CTL, val);
  6227. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6228. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6229. DRM_ERROR("Switching to FCLK failed\n");
  6230. val = I915_READ(LCPLL_CTL);
  6231. }
  6232. val |= LCPLL_PLL_DISABLE;
  6233. I915_WRITE(LCPLL_CTL, val);
  6234. POSTING_READ(LCPLL_CTL);
  6235. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6236. DRM_ERROR("LCPLL still locked\n");
  6237. val = hsw_read_dcomp(dev_priv);
  6238. val |= D_COMP_COMP_DISABLE;
  6239. hsw_write_dcomp(dev_priv, val);
  6240. ndelay(100);
  6241. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6242. 1))
  6243. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6244. if (allow_power_down) {
  6245. val = I915_READ(LCPLL_CTL);
  6246. val |= LCPLL_POWER_DOWN_ALLOW;
  6247. I915_WRITE(LCPLL_CTL, val);
  6248. POSTING_READ(LCPLL_CTL);
  6249. }
  6250. }
  6251. /*
  6252. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6253. * source.
  6254. */
  6255. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6256. {
  6257. uint32_t val;
  6258. unsigned long irqflags;
  6259. val = I915_READ(LCPLL_CTL);
  6260. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6261. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6262. return;
  6263. /*
  6264. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6265. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6266. *
  6267. * The other problem is that hsw_restore_lcpll() is called as part of
  6268. * the runtime PM resume sequence, so we can't just call
  6269. * gen6_gt_force_wake_get() because that function calls
  6270. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6271. * while we are on the resume sequence. So to solve this problem we have
  6272. * to call special forcewake code that doesn't touch runtime PM and
  6273. * doesn't enable the forcewake delayed work.
  6274. */
  6275. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6276. if (dev_priv->uncore.forcewake_count++ == 0)
  6277. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6278. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6279. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6280. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6281. I915_WRITE(LCPLL_CTL, val);
  6282. POSTING_READ(LCPLL_CTL);
  6283. }
  6284. val = hsw_read_dcomp(dev_priv);
  6285. val |= D_COMP_COMP_FORCE;
  6286. val &= ~D_COMP_COMP_DISABLE;
  6287. hsw_write_dcomp(dev_priv, val);
  6288. val = I915_READ(LCPLL_CTL);
  6289. val &= ~LCPLL_PLL_DISABLE;
  6290. I915_WRITE(LCPLL_CTL, val);
  6291. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6292. DRM_ERROR("LCPLL not locked yet\n");
  6293. if (val & LCPLL_CD_SOURCE_FCLK) {
  6294. val = I915_READ(LCPLL_CTL);
  6295. val &= ~LCPLL_CD_SOURCE_FCLK;
  6296. I915_WRITE(LCPLL_CTL, val);
  6297. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6298. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6299. DRM_ERROR("Switching back to LCPLL failed\n");
  6300. }
  6301. /* See the big comment above. */
  6302. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6303. if (--dev_priv->uncore.forcewake_count == 0)
  6304. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6305. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6306. }
  6307. /*
  6308. * Package states C8 and deeper are really deep PC states that can only be
  6309. * reached when all the devices on the system allow it, so even if the graphics
  6310. * device allows PC8+, it doesn't mean the system will actually get to these
  6311. * states. Our driver only allows PC8+ when going into runtime PM.
  6312. *
  6313. * The requirements for PC8+ are that all the outputs are disabled, the power
  6314. * well is disabled and most interrupts are disabled, and these are also
  6315. * requirements for runtime PM. When these conditions are met, we manually do
  6316. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6317. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6318. * hang the machine.
  6319. *
  6320. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6321. * the state of some registers, so when we come back from PC8+ we need to
  6322. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6323. * need to take care of the registers kept by RC6. Notice that this happens even
  6324. * if we don't put the device in PCI D3 state (which is what currently happens
  6325. * because of the runtime PM support).
  6326. *
  6327. * For more, read "Display Sequences for Package C8" on the hardware
  6328. * documentation.
  6329. */
  6330. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6331. {
  6332. struct drm_device *dev = dev_priv->dev;
  6333. uint32_t val;
  6334. DRM_DEBUG_KMS("Enabling package C8+\n");
  6335. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6336. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6337. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6338. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6339. }
  6340. lpt_disable_clkout_dp(dev);
  6341. hsw_disable_lcpll(dev_priv, true, true);
  6342. }
  6343. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6344. {
  6345. struct drm_device *dev = dev_priv->dev;
  6346. uint32_t val;
  6347. DRM_DEBUG_KMS("Disabling package C8+\n");
  6348. hsw_restore_lcpll(dev_priv);
  6349. lpt_init_pch_refclk(dev);
  6350. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6351. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6352. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6353. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6354. }
  6355. intel_prepare_ddi(dev);
  6356. }
  6357. static void snb_modeset_global_resources(struct drm_device *dev)
  6358. {
  6359. modeset_update_crtc_power_domains(dev);
  6360. }
  6361. static void haswell_modeset_global_resources(struct drm_device *dev)
  6362. {
  6363. modeset_update_crtc_power_domains(dev);
  6364. }
  6365. static int haswell_crtc_mode_set(struct drm_crtc *crtc,
  6366. int x, int y,
  6367. struct drm_framebuffer *fb)
  6368. {
  6369. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6370. if (!intel_ddi_pll_select(intel_crtc))
  6371. return -EINVAL;
  6372. intel_crtc->lowfreq_avail = false;
  6373. return 0;
  6374. }
  6375. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6376. struct intel_crtc_config *pipe_config)
  6377. {
  6378. struct drm_device *dev = crtc->base.dev;
  6379. struct drm_i915_private *dev_priv = dev->dev_private;
  6380. struct intel_shared_dpll *pll;
  6381. enum port port;
  6382. uint32_t tmp;
  6383. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6384. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  6385. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6386. switch (pipe_config->ddi_pll_sel) {
  6387. case PORT_CLK_SEL_WRPLL1:
  6388. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6389. break;
  6390. case PORT_CLK_SEL_WRPLL2:
  6391. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6392. break;
  6393. }
  6394. if (pipe_config->shared_dpll >= 0) {
  6395. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6396. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6397. &pipe_config->dpll_hw_state));
  6398. }
  6399. /*
  6400. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6401. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6402. * the PCH transcoder is on.
  6403. */
  6404. if ((port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6405. pipe_config->has_pch_encoder = true;
  6406. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6407. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6408. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6409. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6410. }
  6411. }
  6412. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6413. struct intel_crtc_config *pipe_config)
  6414. {
  6415. struct drm_device *dev = crtc->base.dev;
  6416. struct drm_i915_private *dev_priv = dev->dev_private;
  6417. enum intel_display_power_domain pfit_domain;
  6418. uint32_t tmp;
  6419. if (!intel_display_power_enabled(dev_priv,
  6420. POWER_DOMAIN_PIPE(crtc->pipe)))
  6421. return false;
  6422. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6423. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6424. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6425. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6426. enum pipe trans_edp_pipe;
  6427. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6428. default:
  6429. WARN(1, "unknown pipe linked to edp transcoder\n");
  6430. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6431. case TRANS_DDI_EDP_INPUT_A_ON:
  6432. trans_edp_pipe = PIPE_A;
  6433. break;
  6434. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6435. trans_edp_pipe = PIPE_B;
  6436. break;
  6437. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6438. trans_edp_pipe = PIPE_C;
  6439. break;
  6440. }
  6441. if (trans_edp_pipe == crtc->pipe)
  6442. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6443. }
  6444. if (!intel_display_power_enabled(dev_priv,
  6445. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6446. return false;
  6447. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6448. if (!(tmp & PIPECONF_ENABLE))
  6449. return false;
  6450. haswell_get_ddi_port_state(crtc, pipe_config);
  6451. intel_get_pipe_timings(crtc, pipe_config);
  6452. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6453. if (intel_display_power_enabled(dev_priv, pfit_domain))
  6454. ironlake_get_pfit_config(crtc, pipe_config);
  6455. if (IS_HASWELL(dev))
  6456. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6457. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6458. pipe_config->pixel_multiplier = 1;
  6459. return true;
  6460. }
  6461. static struct {
  6462. int clock;
  6463. u32 config;
  6464. } hdmi_audio_clock[] = {
  6465. { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  6466. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  6467. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  6468. { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  6469. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  6470. { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  6471. { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  6472. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  6473. { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  6474. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  6475. };
  6476. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  6477. static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
  6478. {
  6479. int i;
  6480. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  6481. if (mode->clock == hdmi_audio_clock[i].clock)
  6482. break;
  6483. }
  6484. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  6485. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
  6486. i = 1;
  6487. }
  6488. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  6489. hdmi_audio_clock[i].clock,
  6490. hdmi_audio_clock[i].config);
  6491. return hdmi_audio_clock[i].config;
  6492. }
  6493. static bool intel_eld_uptodate(struct drm_connector *connector,
  6494. int reg_eldv, uint32_t bits_eldv,
  6495. int reg_elda, uint32_t bits_elda,
  6496. int reg_edid)
  6497. {
  6498. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6499. uint8_t *eld = connector->eld;
  6500. uint32_t i;
  6501. i = I915_READ(reg_eldv);
  6502. i &= bits_eldv;
  6503. if (!eld[0])
  6504. return !i;
  6505. if (!i)
  6506. return false;
  6507. i = I915_READ(reg_elda);
  6508. i &= ~bits_elda;
  6509. I915_WRITE(reg_elda, i);
  6510. for (i = 0; i < eld[2]; i++)
  6511. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  6512. return false;
  6513. return true;
  6514. }
  6515. static void g4x_write_eld(struct drm_connector *connector,
  6516. struct drm_crtc *crtc,
  6517. struct drm_display_mode *mode)
  6518. {
  6519. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6520. uint8_t *eld = connector->eld;
  6521. uint32_t eldv;
  6522. uint32_t len;
  6523. uint32_t i;
  6524. i = I915_READ(G4X_AUD_VID_DID);
  6525. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  6526. eldv = G4X_ELDV_DEVCL_DEVBLC;
  6527. else
  6528. eldv = G4X_ELDV_DEVCTG;
  6529. if (intel_eld_uptodate(connector,
  6530. G4X_AUD_CNTL_ST, eldv,
  6531. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  6532. G4X_HDMIW_HDMIEDID))
  6533. return;
  6534. i = I915_READ(G4X_AUD_CNTL_ST);
  6535. i &= ~(eldv | G4X_ELD_ADDR);
  6536. len = (i >> 9) & 0x1f; /* ELD buffer size */
  6537. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6538. if (!eld[0])
  6539. return;
  6540. len = min_t(uint8_t, eld[2], len);
  6541. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6542. for (i = 0; i < len; i++)
  6543. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  6544. i = I915_READ(G4X_AUD_CNTL_ST);
  6545. i |= eldv;
  6546. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6547. }
  6548. static void haswell_write_eld(struct drm_connector *connector,
  6549. struct drm_crtc *crtc,
  6550. struct drm_display_mode *mode)
  6551. {
  6552. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6553. uint8_t *eld = connector->eld;
  6554. uint32_t eldv;
  6555. uint32_t i;
  6556. int len;
  6557. int pipe = to_intel_crtc(crtc)->pipe;
  6558. int tmp;
  6559. int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
  6560. int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
  6561. int aud_config = HSW_AUD_CFG(pipe);
  6562. int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
  6563. /* Audio output enable */
  6564. DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
  6565. tmp = I915_READ(aud_cntrl_st2);
  6566. tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
  6567. I915_WRITE(aud_cntrl_st2, tmp);
  6568. POSTING_READ(aud_cntrl_st2);
  6569. assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  6570. /* Set ELD valid state */
  6571. tmp = I915_READ(aud_cntrl_st2);
  6572. DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
  6573. tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
  6574. I915_WRITE(aud_cntrl_st2, tmp);
  6575. tmp = I915_READ(aud_cntrl_st2);
  6576. DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
  6577. /* Enable HDMI mode */
  6578. tmp = I915_READ(aud_config);
  6579. DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
  6580. /* clear N_programing_enable and N_value_index */
  6581. tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
  6582. I915_WRITE(aud_config, tmp);
  6583. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6584. eldv = AUDIO_ELD_VALID_A << (pipe * 4);
  6585. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6586. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6587. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6588. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6589. } else {
  6590. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6591. }
  6592. if (intel_eld_uptodate(connector,
  6593. aud_cntrl_st2, eldv,
  6594. aud_cntl_st, IBX_ELD_ADDRESS,
  6595. hdmiw_hdmiedid))
  6596. return;
  6597. i = I915_READ(aud_cntrl_st2);
  6598. i &= ~eldv;
  6599. I915_WRITE(aud_cntrl_st2, i);
  6600. if (!eld[0])
  6601. return;
  6602. i = I915_READ(aud_cntl_st);
  6603. i &= ~IBX_ELD_ADDRESS;
  6604. I915_WRITE(aud_cntl_st, i);
  6605. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  6606. DRM_DEBUG_DRIVER("port num:%d\n", i);
  6607. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6608. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6609. for (i = 0; i < len; i++)
  6610. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6611. i = I915_READ(aud_cntrl_st2);
  6612. i |= eldv;
  6613. I915_WRITE(aud_cntrl_st2, i);
  6614. }
  6615. static void ironlake_write_eld(struct drm_connector *connector,
  6616. struct drm_crtc *crtc,
  6617. struct drm_display_mode *mode)
  6618. {
  6619. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6620. uint8_t *eld = connector->eld;
  6621. uint32_t eldv;
  6622. uint32_t i;
  6623. int len;
  6624. int hdmiw_hdmiedid;
  6625. int aud_config;
  6626. int aud_cntl_st;
  6627. int aud_cntrl_st2;
  6628. int pipe = to_intel_crtc(crtc)->pipe;
  6629. if (HAS_PCH_IBX(connector->dev)) {
  6630. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  6631. aud_config = IBX_AUD_CFG(pipe);
  6632. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  6633. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  6634. } else if (IS_VALLEYVIEW(connector->dev)) {
  6635. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  6636. aud_config = VLV_AUD_CFG(pipe);
  6637. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  6638. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  6639. } else {
  6640. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  6641. aud_config = CPT_AUD_CFG(pipe);
  6642. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  6643. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  6644. }
  6645. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6646. if (IS_VALLEYVIEW(connector->dev)) {
  6647. struct intel_encoder *intel_encoder;
  6648. struct intel_digital_port *intel_dig_port;
  6649. intel_encoder = intel_attached_encoder(connector);
  6650. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  6651. i = intel_dig_port->port;
  6652. } else {
  6653. i = I915_READ(aud_cntl_st);
  6654. i = (i >> 29) & DIP_PORT_SEL_MASK;
  6655. /* DIP_Port_Select, 0x1 = PortB */
  6656. }
  6657. if (!i) {
  6658. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  6659. /* operate blindly on all ports */
  6660. eldv = IBX_ELD_VALIDB;
  6661. eldv |= IBX_ELD_VALIDB << 4;
  6662. eldv |= IBX_ELD_VALIDB << 8;
  6663. } else {
  6664. DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
  6665. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  6666. }
  6667. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6668. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6669. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6670. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6671. } else {
  6672. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6673. }
  6674. if (intel_eld_uptodate(connector,
  6675. aud_cntrl_st2, eldv,
  6676. aud_cntl_st, IBX_ELD_ADDRESS,
  6677. hdmiw_hdmiedid))
  6678. return;
  6679. i = I915_READ(aud_cntrl_st2);
  6680. i &= ~eldv;
  6681. I915_WRITE(aud_cntrl_st2, i);
  6682. if (!eld[0])
  6683. return;
  6684. i = I915_READ(aud_cntl_st);
  6685. i &= ~IBX_ELD_ADDRESS;
  6686. I915_WRITE(aud_cntl_st, i);
  6687. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6688. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6689. for (i = 0; i < len; i++)
  6690. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6691. i = I915_READ(aud_cntrl_st2);
  6692. i |= eldv;
  6693. I915_WRITE(aud_cntrl_st2, i);
  6694. }
  6695. void intel_write_eld(struct drm_encoder *encoder,
  6696. struct drm_display_mode *mode)
  6697. {
  6698. struct drm_crtc *crtc = encoder->crtc;
  6699. struct drm_connector *connector;
  6700. struct drm_device *dev = encoder->dev;
  6701. struct drm_i915_private *dev_priv = dev->dev_private;
  6702. connector = drm_select_eld(encoder, mode);
  6703. if (!connector)
  6704. return;
  6705. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6706. connector->base.id,
  6707. connector->name,
  6708. connector->encoder->base.id,
  6709. connector->encoder->name);
  6710. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  6711. if (dev_priv->display.write_eld)
  6712. dev_priv->display.write_eld(connector, crtc, mode);
  6713. }
  6714. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6715. {
  6716. struct drm_device *dev = crtc->dev;
  6717. struct drm_i915_private *dev_priv = dev->dev_private;
  6718. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6719. uint32_t cntl;
  6720. if (base != intel_crtc->cursor_base) {
  6721. /* On these chipsets we can only modify the base whilst
  6722. * the cursor is disabled.
  6723. */
  6724. if (intel_crtc->cursor_cntl) {
  6725. I915_WRITE(_CURACNTR, 0);
  6726. POSTING_READ(_CURACNTR);
  6727. intel_crtc->cursor_cntl = 0;
  6728. }
  6729. I915_WRITE(_CURABASE, base);
  6730. POSTING_READ(_CURABASE);
  6731. }
  6732. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  6733. cntl = 0;
  6734. if (base)
  6735. cntl = (CURSOR_ENABLE |
  6736. CURSOR_GAMMA_ENABLE |
  6737. CURSOR_FORMAT_ARGB);
  6738. if (intel_crtc->cursor_cntl != cntl) {
  6739. I915_WRITE(_CURACNTR, cntl);
  6740. POSTING_READ(_CURACNTR);
  6741. intel_crtc->cursor_cntl = cntl;
  6742. }
  6743. }
  6744. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6745. {
  6746. struct drm_device *dev = crtc->dev;
  6747. struct drm_i915_private *dev_priv = dev->dev_private;
  6748. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6749. int pipe = intel_crtc->pipe;
  6750. uint32_t cntl;
  6751. cntl = 0;
  6752. if (base) {
  6753. cntl = MCURSOR_GAMMA_ENABLE;
  6754. switch (intel_crtc->cursor_width) {
  6755. case 64:
  6756. cntl |= CURSOR_MODE_64_ARGB_AX;
  6757. break;
  6758. case 128:
  6759. cntl |= CURSOR_MODE_128_ARGB_AX;
  6760. break;
  6761. case 256:
  6762. cntl |= CURSOR_MODE_256_ARGB_AX;
  6763. break;
  6764. default:
  6765. WARN_ON(1);
  6766. return;
  6767. }
  6768. cntl |= pipe << 28; /* Connect to correct pipe */
  6769. }
  6770. if (intel_crtc->cursor_cntl != cntl) {
  6771. I915_WRITE(CURCNTR(pipe), cntl);
  6772. POSTING_READ(CURCNTR(pipe));
  6773. intel_crtc->cursor_cntl = cntl;
  6774. }
  6775. /* and commit changes on next vblank */
  6776. I915_WRITE(CURBASE(pipe), base);
  6777. POSTING_READ(CURBASE(pipe));
  6778. }
  6779. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  6780. {
  6781. struct drm_device *dev = crtc->dev;
  6782. struct drm_i915_private *dev_priv = dev->dev_private;
  6783. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6784. int pipe = intel_crtc->pipe;
  6785. uint32_t cntl;
  6786. cntl = 0;
  6787. if (base) {
  6788. cntl = MCURSOR_GAMMA_ENABLE;
  6789. switch (intel_crtc->cursor_width) {
  6790. case 64:
  6791. cntl |= CURSOR_MODE_64_ARGB_AX;
  6792. break;
  6793. case 128:
  6794. cntl |= CURSOR_MODE_128_ARGB_AX;
  6795. break;
  6796. case 256:
  6797. cntl |= CURSOR_MODE_256_ARGB_AX;
  6798. break;
  6799. default:
  6800. WARN_ON(1);
  6801. return;
  6802. }
  6803. }
  6804. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6805. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6806. if (intel_crtc->cursor_cntl != cntl) {
  6807. I915_WRITE(CURCNTR(pipe), cntl);
  6808. POSTING_READ(CURCNTR(pipe));
  6809. intel_crtc->cursor_cntl = cntl;
  6810. }
  6811. /* and commit changes on next vblank */
  6812. I915_WRITE(CURBASE(pipe), base);
  6813. POSTING_READ(CURBASE(pipe));
  6814. }
  6815. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6816. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6817. bool on)
  6818. {
  6819. struct drm_device *dev = crtc->dev;
  6820. struct drm_i915_private *dev_priv = dev->dev_private;
  6821. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6822. int pipe = intel_crtc->pipe;
  6823. int x = crtc->cursor_x;
  6824. int y = crtc->cursor_y;
  6825. u32 base = 0, pos = 0;
  6826. if (on)
  6827. base = intel_crtc->cursor_addr;
  6828. if (x >= intel_crtc->config.pipe_src_w)
  6829. base = 0;
  6830. if (y >= intel_crtc->config.pipe_src_h)
  6831. base = 0;
  6832. if (x < 0) {
  6833. if (x + intel_crtc->cursor_width <= 0)
  6834. base = 0;
  6835. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  6836. x = -x;
  6837. }
  6838. pos |= x << CURSOR_X_SHIFT;
  6839. if (y < 0) {
  6840. if (y + intel_crtc->cursor_height <= 0)
  6841. base = 0;
  6842. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  6843. y = -y;
  6844. }
  6845. pos |= y << CURSOR_Y_SHIFT;
  6846. if (base == 0 && intel_crtc->cursor_base == 0)
  6847. return;
  6848. I915_WRITE(CURPOS(pipe), pos);
  6849. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev))
  6850. ivb_update_cursor(crtc, base);
  6851. else if (IS_845G(dev) || IS_I865G(dev))
  6852. i845_update_cursor(crtc, base);
  6853. else
  6854. i9xx_update_cursor(crtc, base);
  6855. intel_crtc->cursor_base = base;
  6856. }
  6857. /*
  6858. * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
  6859. *
  6860. * Note that the object's reference will be consumed if the update fails. If
  6861. * the update succeeds, the reference of the old object (if any) will be
  6862. * consumed.
  6863. */
  6864. static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
  6865. struct drm_i915_gem_object *obj,
  6866. uint32_t width, uint32_t height)
  6867. {
  6868. struct drm_device *dev = crtc->dev;
  6869. struct drm_i915_private *dev_priv = dev->dev_private;
  6870. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6871. enum pipe pipe = intel_crtc->pipe;
  6872. unsigned old_width;
  6873. uint32_t addr;
  6874. int ret;
  6875. /* if we want to turn off the cursor ignore width and height */
  6876. if (!obj) {
  6877. DRM_DEBUG_KMS("cursor off\n");
  6878. addr = 0;
  6879. obj = NULL;
  6880. mutex_lock(&dev->struct_mutex);
  6881. goto finish;
  6882. }
  6883. /* Check for which cursor types we support */
  6884. if (!((width == 64 && height == 64) ||
  6885. (width == 128 && height == 128 && !IS_GEN2(dev)) ||
  6886. (width == 256 && height == 256 && !IS_GEN2(dev)))) {
  6887. DRM_DEBUG("Cursor dimension not supported\n");
  6888. return -EINVAL;
  6889. }
  6890. if (obj->base.size < width * height * 4) {
  6891. DRM_DEBUG_KMS("buffer is too small\n");
  6892. ret = -ENOMEM;
  6893. goto fail;
  6894. }
  6895. /* we only need to pin inside GTT if cursor is non-phy */
  6896. mutex_lock(&dev->struct_mutex);
  6897. if (!INTEL_INFO(dev)->cursor_needs_physical) {
  6898. unsigned alignment;
  6899. if (obj->tiling_mode) {
  6900. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  6901. ret = -EINVAL;
  6902. goto fail_locked;
  6903. }
  6904. /*
  6905. * Global gtt pte registers are special registers which actually
  6906. * forward writes to a chunk of system memory. Which means that
  6907. * there is no risk that the register values disappear as soon
  6908. * as we call intel_runtime_pm_put(), so it is correct to wrap
  6909. * only the pin/unpin/fence and not more.
  6910. */
  6911. intel_runtime_pm_get(dev_priv);
  6912. /* Note that the w/a also requires 2 PTE of padding following
  6913. * the bo. We currently fill all unused PTE with the shadow
  6914. * page and so we should always have valid PTE following the
  6915. * cursor preventing the VT-d warning.
  6916. */
  6917. alignment = 0;
  6918. if (need_vtd_wa(dev))
  6919. alignment = 64*1024;
  6920. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  6921. if (ret) {
  6922. DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
  6923. intel_runtime_pm_put(dev_priv);
  6924. goto fail_locked;
  6925. }
  6926. ret = i915_gem_object_put_fence(obj);
  6927. if (ret) {
  6928. DRM_DEBUG_KMS("failed to release fence for cursor");
  6929. intel_runtime_pm_put(dev_priv);
  6930. goto fail_unpin;
  6931. }
  6932. addr = i915_gem_obj_ggtt_offset(obj);
  6933. intel_runtime_pm_put(dev_priv);
  6934. } else {
  6935. int align = IS_I830(dev) ? 16 * 1024 : 256;
  6936. ret = i915_gem_object_attach_phys(obj, align);
  6937. if (ret) {
  6938. DRM_DEBUG_KMS("failed to attach phys object\n");
  6939. goto fail_locked;
  6940. }
  6941. addr = obj->phys_handle->busaddr;
  6942. }
  6943. if (IS_GEN2(dev))
  6944. I915_WRITE(CURSIZE, (height << 12) | width);
  6945. finish:
  6946. if (intel_crtc->cursor_bo) {
  6947. if (!INTEL_INFO(dev)->cursor_needs_physical)
  6948. i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
  6949. }
  6950. i915_gem_track_fb(intel_crtc->cursor_bo, obj,
  6951. INTEL_FRONTBUFFER_CURSOR(pipe));
  6952. mutex_unlock(&dev->struct_mutex);
  6953. old_width = intel_crtc->cursor_width;
  6954. intel_crtc->cursor_addr = addr;
  6955. intel_crtc->cursor_bo = obj;
  6956. intel_crtc->cursor_width = width;
  6957. intel_crtc->cursor_height = height;
  6958. if (intel_crtc->active) {
  6959. if (old_width != width)
  6960. intel_update_watermarks(crtc);
  6961. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6962. }
  6963. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
  6964. return 0;
  6965. fail_unpin:
  6966. i915_gem_object_unpin_from_display_plane(obj);
  6967. fail_locked:
  6968. mutex_unlock(&dev->struct_mutex);
  6969. fail:
  6970. drm_gem_object_unreference_unlocked(&obj->base);
  6971. return ret;
  6972. }
  6973. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  6974. u16 *blue, uint32_t start, uint32_t size)
  6975. {
  6976. int end = (start + size > 256) ? 256 : start + size, i;
  6977. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6978. for (i = start; i < end; i++) {
  6979. intel_crtc->lut_r[i] = red[i] >> 8;
  6980. intel_crtc->lut_g[i] = green[i] >> 8;
  6981. intel_crtc->lut_b[i] = blue[i] >> 8;
  6982. }
  6983. intel_crtc_load_lut(crtc);
  6984. }
  6985. /* VESA 640x480x72Hz mode to set on the pipe */
  6986. static struct drm_display_mode load_detect_mode = {
  6987. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  6988. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  6989. };
  6990. struct drm_framebuffer *
  6991. __intel_framebuffer_create(struct drm_device *dev,
  6992. struct drm_mode_fb_cmd2 *mode_cmd,
  6993. struct drm_i915_gem_object *obj)
  6994. {
  6995. struct intel_framebuffer *intel_fb;
  6996. int ret;
  6997. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6998. if (!intel_fb) {
  6999. drm_gem_object_unreference_unlocked(&obj->base);
  7000. return ERR_PTR(-ENOMEM);
  7001. }
  7002. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7003. if (ret)
  7004. goto err;
  7005. return &intel_fb->base;
  7006. err:
  7007. drm_gem_object_unreference_unlocked(&obj->base);
  7008. kfree(intel_fb);
  7009. return ERR_PTR(ret);
  7010. }
  7011. static struct drm_framebuffer *
  7012. intel_framebuffer_create(struct drm_device *dev,
  7013. struct drm_mode_fb_cmd2 *mode_cmd,
  7014. struct drm_i915_gem_object *obj)
  7015. {
  7016. struct drm_framebuffer *fb;
  7017. int ret;
  7018. ret = i915_mutex_lock_interruptible(dev);
  7019. if (ret)
  7020. return ERR_PTR(ret);
  7021. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7022. mutex_unlock(&dev->struct_mutex);
  7023. return fb;
  7024. }
  7025. static u32
  7026. intel_framebuffer_pitch_for_width(int width, int bpp)
  7027. {
  7028. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7029. return ALIGN(pitch, 64);
  7030. }
  7031. static u32
  7032. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7033. {
  7034. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7035. return PAGE_ALIGN(pitch * mode->vdisplay);
  7036. }
  7037. static struct drm_framebuffer *
  7038. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7039. struct drm_display_mode *mode,
  7040. int depth, int bpp)
  7041. {
  7042. struct drm_i915_gem_object *obj;
  7043. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7044. obj = i915_gem_alloc_object(dev,
  7045. intel_framebuffer_size_for_mode(mode, bpp));
  7046. if (obj == NULL)
  7047. return ERR_PTR(-ENOMEM);
  7048. mode_cmd.width = mode->hdisplay;
  7049. mode_cmd.height = mode->vdisplay;
  7050. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7051. bpp);
  7052. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7053. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7054. }
  7055. static struct drm_framebuffer *
  7056. mode_fits_in_fbdev(struct drm_device *dev,
  7057. struct drm_display_mode *mode)
  7058. {
  7059. #ifdef CONFIG_DRM_I915_FBDEV
  7060. struct drm_i915_private *dev_priv = dev->dev_private;
  7061. struct drm_i915_gem_object *obj;
  7062. struct drm_framebuffer *fb;
  7063. if (!dev_priv->fbdev)
  7064. return NULL;
  7065. if (!dev_priv->fbdev->fb)
  7066. return NULL;
  7067. obj = dev_priv->fbdev->fb->obj;
  7068. BUG_ON(!obj);
  7069. fb = &dev_priv->fbdev->fb->base;
  7070. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7071. fb->bits_per_pixel))
  7072. return NULL;
  7073. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7074. return NULL;
  7075. return fb;
  7076. #else
  7077. return NULL;
  7078. #endif
  7079. }
  7080. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7081. struct drm_display_mode *mode,
  7082. struct intel_load_detect_pipe *old,
  7083. struct drm_modeset_acquire_ctx *ctx)
  7084. {
  7085. struct intel_crtc *intel_crtc;
  7086. struct intel_encoder *intel_encoder =
  7087. intel_attached_encoder(connector);
  7088. struct drm_crtc *possible_crtc;
  7089. struct drm_encoder *encoder = &intel_encoder->base;
  7090. struct drm_crtc *crtc = NULL;
  7091. struct drm_device *dev = encoder->dev;
  7092. struct drm_framebuffer *fb;
  7093. struct drm_mode_config *config = &dev->mode_config;
  7094. int ret, i = -1;
  7095. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7096. connector->base.id, connector->name,
  7097. encoder->base.id, encoder->name);
  7098. retry:
  7099. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7100. if (ret)
  7101. goto fail_unlock;
  7102. /*
  7103. * Algorithm gets a little messy:
  7104. *
  7105. * - if the connector already has an assigned crtc, use it (but make
  7106. * sure it's on first)
  7107. *
  7108. * - try to find the first unused crtc that can drive this connector,
  7109. * and use that if we find one
  7110. */
  7111. /* See if we already have a CRTC for this connector */
  7112. if (encoder->crtc) {
  7113. crtc = encoder->crtc;
  7114. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7115. if (ret)
  7116. goto fail_unlock;
  7117. old->dpms_mode = connector->dpms;
  7118. old->load_detect_temp = false;
  7119. /* Make sure the crtc and connector are running */
  7120. if (connector->dpms != DRM_MODE_DPMS_ON)
  7121. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7122. return true;
  7123. }
  7124. /* Find an unused one (if possible) */
  7125. for_each_crtc(dev, possible_crtc) {
  7126. i++;
  7127. if (!(encoder->possible_crtcs & (1 << i)))
  7128. continue;
  7129. if (possible_crtc->enabled)
  7130. continue;
  7131. /* This can occur when applying the pipe A quirk on resume. */
  7132. if (to_intel_crtc(possible_crtc)->new_enabled)
  7133. continue;
  7134. crtc = possible_crtc;
  7135. break;
  7136. }
  7137. /*
  7138. * If we didn't find an unused CRTC, don't use any.
  7139. */
  7140. if (!crtc) {
  7141. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7142. goto fail_unlock;
  7143. }
  7144. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7145. if (ret)
  7146. goto fail_unlock;
  7147. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7148. to_intel_connector(connector)->new_encoder = intel_encoder;
  7149. intel_crtc = to_intel_crtc(crtc);
  7150. intel_crtc->new_enabled = true;
  7151. intel_crtc->new_config = &intel_crtc->config;
  7152. old->dpms_mode = connector->dpms;
  7153. old->load_detect_temp = true;
  7154. old->release_fb = NULL;
  7155. if (!mode)
  7156. mode = &load_detect_mode;
  7157. /* We need a framebuffer large enough to accommodate all accesses
  7158. * that the plane may generate whilst we perform load detection.
  7159. * We can not rely on the fbcon either being present (we get called
  7160. * during its initialisation to detect all boot displays, or it may
  7161. * not even exist) or that it is large enough to satisfy the
  7162. * requested mode.
  7163. */
  7164. fb = mode_fits_in_fbdev(dev, mode);
  7165. if (fb == NULL) {
  7166. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7167. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7168. old->release_fb = fb;
  7169. } else
  7170. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7171. if (IS_ERR(fb)) {
  7172. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7173. goto fail;
  7174. }
  7175. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7176. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7177. if (old->release_fb)
  7178. old->release_fb->funcs->destroy(old->release_fb);
  7179. goto fail;
  7180. }
  7181. /* let the connector get through one full cycle before testing */
  7182. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7183. return true;
  7184. fail:
  7185. intel_crtc->new_enabled = crtc->enabled;
  7186. if (intel_crtc->new_enabled)
  7187. intel_crtc->new_config = &intel_crtc->config;
  7188. else
  7189. intel_crtc->new_config = NULL;
  7190. fail_unlock:
  7191. if (ret == -EDEADLK) {
  7192. drm_modeset_backoff(ctx);
  7193. goto retry;
  7194. }
  7195. return false;
  7196. }
  7197. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7198. struct intel_load_detect_pipe *old)
  7199. {
  7200. struct intel_encoder *intel_encoder =
  7201. intel_attached_encoder(connector);
  7202. struct drm_encoder *encoder = &intel_encoder->base;
  7203. struct drm_crtc *crtc = encoder->crtc;
  7204. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7205. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7206. connector->base.id, connector->name,
  7207. encoder->base.id, encoder->name);
  7208. if (old->load_detect_temp) {
  7209. to_intel_connector(connector)->new_encoder = NULL;
  7210. intel_encoder->new_crtc = NULL;
  7211. intel_crtc->new_enabled = false;
  7212. intel_crtc->new_config = NULL;
  7213. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7214. if (old->release_fb) {
  7215. drm_framebuffer_unregister_private(old->release_fb);
  7216. drm_framebuffer_unreference(old->release_fb);
  7217. }
  7218. return;
  7219. }
  7220. /* Switch crtc and encoder back off if necessary */
  7221. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7222. connector->funcs->dpms(connector, old->dpms_mode);
  7223. }
  7224. static int i9xx_pll_refclk(struct drm_device *dev,
  7225. const struct intel_crtc_config *pipe_config)
  7226. {
  7227. struct drm_i915_private *dev_priv = dev->dev_private;
  7228. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7229. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7230. return dev_priv->vbt.lvds_ssc_freq;
  7231. else if (HAS_PCH_SPLIT(dev))
  7232. return 120000;
  7233. else if (!IS_GEN2(dev))
  7234. return 96000;
  7235. else
  7236. return 48000;
  7237. }
  7238. /* Returns the clock of the currently programmed mode of the given pipe. */
  7239. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7240. struct intel_crtc_config *pipe_config)
  7241. {
  7242. struct drm_device *dev = crtc->base.dev;
  7243. struct drm_i915_private *dev_priv = dev->dev_private;
  7244. int pipe = pipe_config->cpu_transcoder;
  7245. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7246. u32 fp;
  7247. intel_clock_t clock;
  7248. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7249. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7250. fp = pipe_config->dpll_hw_state.fp0;
  7251. else
  7252. fp = pipe_config->dpll_hw_state.fp1;
  7253. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7254. if (IS_PINEVIEW(dev)) {
  7255. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7256. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7257. } else {
  7258. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7259. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7260. }
  7261. if (!IS_GEN2(dev)) {
  7262. if (IS_PINEVIEW(dev))
  7263. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7264. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7265. else
  7266. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7267. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7268. switch (dpll & DPLL_MODE_MASK) {
  7269. case DPLLB_MODE_DAC_SERIAL:
  7270. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7271. 5 : 10;
  7272. break;
  7273. case DPLLB_MODE_LVDS:
  7274. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7275. 7 : 14;
  7276. break;
  7277. default:
  7278. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7279. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7280. return;
  7281. }
  7282. if (IS_PINEVIEW(dev))
  7283. pineview_clock(refclk, &clock);
  7284. else
  7285. i9xx_clock(refclk, &clock);
  7286. } else {
  7287. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7288. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7289. if (is_lvds) {
  7290. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7291. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7292. if (lvds & LVDS_CLKB_POWER_UP)
  7293. clock.p2 = 7;
  7294. else
  7295. clock.p2 = 14;
  7296. } else {
  7297. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7298. clock.p1 = 2;
  7299. else {
  7300. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7301. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7302. }
  7303. if (dpll & PLL_P2_DIVIDE_BY_4)
  7304. clock.p2 = 4;
  7305. else
  7306. clock.p2 = 2;
  7307. }
  7308. i9xx_clock(refclk, &clock);
  7309. }
  7310. /*
  7311. * This value includes pixel_multiplier. We will use
  7312. * port_clock to compute adjusted_mode.crtc_clock in the
  7313. * encoder's get_config() function.
  7314. */
  7315. pipe_config->port_clock = clock.dot;
  7316. }
  7317. int intel_dotclock_calculate(int link_freq,
  7318. const struct intel_link_m_n *m_n)
  7319. {
  7320. /*
  7321. * The calculation for the data clock is:
  7322. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7323. * But we want to avoid losing precison if possible, so:
  7324. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7325. *
  7326. * and the link clock is simpler:
  7327. * link_clock = (m * link_clock) / n
  7328. */
  7329. if (!m_n->link_n)
  7330. return 0;
  7331. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7332. }
  7333. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7334. struct intel_crtc_config *pipe_config)
  7335. {
  7336. struct drm_device *dev = crtc->base.dev;
  7337. /* read out port_clock from the DPLL */
  7338. i9xx_crtc_clock_get(crtc, pipe_config);
  7339. /*
  7340. * This value does not include pixel_multiplier.
  7341. * We will check that port_clock and adjusted_mode.crtc_clock
  7342. * agree once we know their relationship in the encoder's
  7343. * get_config() function.
  7344. */
  7345. pipe_config->adjusted_mode.crtc_clock =
  7346. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7347. &pipe_config->fdi_m_n);
  7348. }
  7349. /** Returns the currently programmed mode of the given pipe. */
  7350. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7351. struct drm_crtc *crtc)
  7352. {
  7353. struct drm_i915_private *dev_priv = dev->dev_private;
  7354. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7355. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7356. struct drm_display_mode *mode;
  7357. struct intel_crtc_config pipe_config;
  7358. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7359. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7360. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7361. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7362. enum pipe pipe = intel_crtc->pipe;
  7363. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7364. if (!mode)
  7365. return NULL;
  7366. /*
  7367. * Construct a pipe_config sufficient for getting the clock info
  7368. * back out of crtc_clock_get.
  7369. *
  7370. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7371. * to use a real value here instead.
  7372. */
  7373. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7374. pipe_config.pixel_multiplier = 1;
  7375. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7376. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7377. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7378. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7379. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7380. mode->hdisplay = (htot & 0xffff) + 1;
  7381. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7382. mode->hsync_start = (hsync & 0xffff) + 1;
  7383. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7384. mode->vdisplay = (vtot & 0xffff) + 1;
  7385. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7386. mode->vsync_start = (vsync & 0xffff) + 1;
  7387. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7388. drm_mode_set_name(mode);
  7389. return mode;
  7390. }
  7391. static void intel_increase_pllclock(struct drm_device *dev,
  7392. enum pipe pipe)
  7393. {
  7394. struct drm_i915_private *dev_priv = dev->dev_private;
  7395. int dpll_reg = DPLL(pipe);
  7396. int dpll;
  7397. if (!HAS_GMCH_DISPLAY(dev))
  7398. return;
  7399. if (!dev_priv->lvds_downclock_avail)
  7400. return;
  7401. dpll = I915_READ(dpll_reg);
  7402. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  7403. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  7404. assert_panel_unlocked(dev_priv, pipe);
  7405. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  7406. I915_WRITE(dpll_reg, dpll);
  7407. intel_wait_for_vblank(dev, pipe);
  7408. dpll = I915_READ(dpll_reg);
  7409. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  7410. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  7411. }
  7412. }
  7413. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7414. {
  7415. struct drm_device *dev = crtc->dev;
  7416. struct drm_i915_private *dev_priv = dev->dev_private;
  7417. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7418. if (!HAS_GMCH_DISPLAY(dev))
  7419. return;
  7420. if (!dev_priv->lvds_downclock_avail)
  7421. return;
  7422. /*
  7423. * Since this is called by a timer, we should never get here in
  7424. * the manual case.
  7425. */
  7426. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7427. int pipe = intel_crtc->pipe;
  7428. int dpll_reg = DPLL(pipe);
  7429. int dpll;
  7430. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7431. assert_panel_unlocked(dev_priv, pipe);
  7432. dpll = I915_READ(dpll_reg);
  7433. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7434. I915_WRITE(dpll_reg, dpll);
  7435. intel_wait_for_vblank(dev, pipe);
  7436. dpll = I915_READ(dpll_reg);
  7437. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7438. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7439. }
  7440. }
  7441. void intel_mark_busy(struct drm_device *dev)
  7442. {
  7443. struct drm_i915_private *dev_priv = dev->dev_private;
  7444. if (dev_priv->mm.busy)
  7445. return;
  7446. intel_runtime_pm_get(dev_priv);
  7447. i915_update_gfx_val(dev_priv);
  7448. dev_priv->mm.busy = true;
  7449. }
  7450. void intel_mark_idle(struct drm_device *dev)
  7451. {
  7452. struct drm_i915_private *dev_priv = dev->dev_private;
  7453. struct drm_crtc *crtc;
  7454. if (!dev_priv->mm.busy)
  7455. return;
  7456. dev_priv->mm.busy = false;
  7457. if (!i915.powersave)
  7458. goto out;
  7459. for_each_crtc(dev, crtc) {
  7460. if (!crtc->primary->fb)
  7461. continue;
  7462. intel_decrease_pllclock(crtc);
  7463. }
  7464. if (INTEL_INFO(dev)->gen >= 6)
  7465. gen6_rps_idle(dev->dev_private);
  7466. out:
  7467. intel_runtime_pm_put(dev_priv);
  7468. }
  7469. /**
  7470. * intel_mark_fb_busy - mark given planes as busy
  7471. * @dev: DRM device
  7472. * @frontbuffer_bits: bits for the affected planes
  7473. * @ring: optional ring for asynchronous commands
  7474. *
  7475. * This function gets called every time the screen contents change. It can be
  7476. * used to keep e.g. the update rate at the nominal refresh rate with DRRS.
  7477. */
  7478. static void intel_mark_fb_busy(struct drm_device *dev,
  7479. unsigned frontbuffer_bits,
  7480. struct intel_engine_cs *ring)
  7481. {
  7482. enum pipe pipe;
  7483. if (!i915.powersave)
  7484. return;
  7485. for_each_pipe(pipe) {
  7486. if (!(frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe)))
  7487. continue;
  7488. intel_increase_pllclock(dev, pipe);
  7489. if (ring && intel_fbc_enabled(dev))
  7490. ring->fbc_dirty = true;
  7491. }
  7492. }
  7493. /**
  7494. * intel_fb_obj_invalidate - invalidate frontbuffer object
  7495. * @obj: GEM object to invalidate
  7496. * @ring: set for asynchronous rendering
  7497. *
  7498. * This function gets called every time rendering on the given object starts and
  7499. * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must
  7500. * be invalidated. If @ring is non-NULL any subsequent invalidation will be delayed
  7501. * until the rendering completes or a flip on this frontbuffer plane is
  7502. * scheduled.
  7503. */
  7504. void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj,
  7505. struct intel_engine_cs *ring)
  7506. {
  7507. struct drm_device *dev = obj->base.dev;
  7508. struct drm_i915_private *dev_priv = dev->dev_private;
  7509. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  7510. if (!obj->frontbuffer_bits)
  7511. return;
  7512. if (ring) {
  7513. mutex_lock(&dev_priv->fb_tracking.lock);
  7514. dev_priv->fb_tracking.busy_bits
  7515. |= obj->frontbuffer_bits;
  7516. dev_priv->fb_tracking.flip_bits
  7517. &= ~obj->frontbuffer_bits;
  7518. mutex_unlock(&dev_priv->fb_tracking.lock);
  7519. }
  7520. intel_mark_fb_busy(dev, obj->frontbuffer_bits, ring);
  7521. intel_edp_psr_invalidate(dev, obj->frontbuffer_bits);
  7522. }
  7523. /**
  7524. * intel_frontbuffer_flush - flush frontbuffer
  7525. * @dev: DRM device
  7526. * @frontbuffer_bits: frontbuffer plane tracking bits
  7527. *
  7528. * This function gets called every time rendering on the given planes has
  7529. * completed and frontbuffer caching can be started again. Flushes will get
  7530. * delayed if they're blocked by some oustanding asynchronous rendering.
  7531. *
  7532. * Can be called without any locks held.
  7533. */
  7534. void intel_frontbuffer_flush(struct drm_device *dev,
  7535. unsigned frontbuffer_bits)
  7536. {
  7537. struct drm_i915_private *dev_priv = dev->dev_private;
  7538. /* Delay flushing when rings are still busy.*/
  7539. mutex_lock(&dev_priv->fb_tracking.lock);
  7540. frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits;
  7541. mutex_unlock(&dev_priv->fb_tracking.lock);
  7542. intel_mark_fb_busy(dev, frontbuffer_bits, NULL);
  7543. intel_edp_psr_flush(dev, frontbuffer_bits);
  7544. }
  7545. /**
  7546. * intel_fb_obj_flush - flush frontbuffer object
  7547. * @obj: GEM object to flush
  7548. * @retire: set when retiring asynchronous rendering
  7549. *
  7550. * This function gets called every time rendering on the given object has
  7551. * completed and frontbuffer caching can be started again. If @retire is true
  7552. * then any delayed flushes will be unblocked.
  7553. */
  7554. void intel_fb_obj_flush(struct drm_i915_gem_object *obj,
  7555. bool retire)
  7556. {
  7557. struct drm_device *dev = obj->base.dev;
  7558. struct drm_i915_private *dev_priv = dev->dev_private;
  7559. unsigned frontbuffer_bits;
  7560. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  7561. if (!obj->frontbuffer_bits)
  7562. return;
  7563. frontbuffer_bits = obj->frontbuffer_bits;
  7564. if (retire) {
  7565. mutex_lock(&dev_priv->fb_tracking.lock);
  7566. /* Filter out new bits since rendering started. */
  7567. frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
  7568. dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
  7569. mutex_unlock(&dev_priv->fb_tracking.lock);
  7570. }
  7571. intel_frontbuffer_flush(dev, frontbuffer_bits);
  7572. }
  7573. /**
  7574. * intel_frontbuffer_flip_prepare - prepare asnychronous frontbuffer flip
  7575. * @dev: DRM device
  7576. * @frontbuffer_bits: frontbuffer plane tracking bits
  7577. *
  7578. * This function gets called after scheduling a flip on @obj. The actual
  7579. * frontbuffer flushing will be delayed until completion is signalled with
  7580. * intel_frontbuffer_flip_complete. If an invalidate happens in between this
  7581. * flush will be cancelled.
  7582. *
  7583. * Can be called without any locks held.
  7584. */
  7585. void intel_frontbuffer_flip_prepare(struct drm_device *dev,
  7586. unsigned frontbuffer_bits)
  7587. {
  7588. struct drm_i915_private *dev_priv = dev->dev_private;
  7589. mutex_lock(&dev_priv->fb_tracking.lock);
  7590. dev_priv->fb_tracking.flip_bits
  7591. |= frontbuffer_bits;
  7592. mutex_unlock(&dev_priv->fb_tracking.lock);
  7593. }
  7594. /**
  7595. * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flush
  7596. * @dev: DRM device
  7597. * @frontbuffer_bits: frontbuffer plane tracking bits
  7598. *
  7599. * This function gets called after the flip has been latched and will complete
  7600. * on the next vblank. It will execute the fush if it hasn't been cancalled yet.
  7601. *
  7602. * Can be called without any locks held.
  7603. */
  7604. void intel_frontbuffer_flip_complete(struct drm_device *dev,
  7605. unsigned frontbuffer_bits)
  7606. {
  7607. struct drm_i915_private *dev_priv = dev->dev_private;
  7608. mutex_lock(&dev_priv->fb_tracking.lock);
  7609. /* Mask any cancelled flips. */
  7610. frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
  7611. dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
  7612. mutex_unlock(&dev_priv->fb_tracking.lock);
  7613. intel_frontbuffer_flush(dev, frontbuffer_bits);
  7614. }
  7615. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7616. {
  7617. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7618. struct drm_device *dev = crtc->dev;
  7619. struct intel_unpin_work *work;
  7620. unsigned long flags;
  7621. spin_lock_irqsave(&dev->event_lock, flags);
  7622. work = intel_crtc->unpin_work;
  7623. intel_crtc->unpin_work = NULL;
  7624. spin_unlock_irqrestore(&dev->event_lock, flags);
  7625. if (work) {
  7626. cancel_work_sync(&work->work);
  7627. kfree(work);
  7628. }
  7629. drm_crtc_cleanup(crtc);
  7630. kfree(intel_crtc);
  7631. }
  7632. static void intel_unpin_work_fn(struct work_struct *__work)
  7633. {
  7634. struct intel_unpin_work *work =
  7635. container_of(__work, struct intel_unpin_work, work);
  7636. struct drm_device *dev = work->crtc->dev;
  7637. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7638. mutex_lock(&dev->struct_mutex);
  7639. intel_unpin_fb_obj(work->old_fb_obj);
  7640. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7641. drm_gem_object_unreference(&work->old_fb_obj->base);
  7642. intel_update_fbc(dev);
  7643. mutex_unlock(&dev->struct_mutex);
  7644. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7645. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7646. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7647. kfree(work);
  7648. }
  7649. static void do_intel_finish_page_flip(struct drm_device *dev,
  7650. struct drm_crtc *crtc)
  7651. {
  7652. struct drm_i915_private *dev_priv = dev->dev_private;
  7653. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7654. struct intel_unpin_work *work;
  7655. unsigned long flags;
  7656. /* Ignore early vblank irqs */
  7657. if (intel_crtc == NULL)
  7658. return;
  7659. spin_lock_irqsave(&dev->event_lock, flags);
  7660. work = intel_crtc->unpin_work;
  7661. /* Ensure we don't miss a work->pending update ... */
  7662. smp_rmb();
  7663. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7664. spin_unlock_irqrestore(&dev->event_lock, flags);
  7665. return;
  7666. }
  7667. /* and that the unpin work is consistent wrt ->pending. */
  7668. smp_rmb();
  7669. intel_crtc->unpin_work = NULL;
  7670. if (work->event)
  7671. drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
  7672. drm_crtc_vblank_put(crtc);
  7673. spin_unlock_irqrestore(&dev->event_lock, flags);
  7674. wake_up_all(&dev_priv->pending_flip_queue);
  7675. queue_work(dev_priv->wq, &work->work);
  7676. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  7677. }
  7678. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7679. {
  7680. struct drm_i915_private *dev_priv = dev->dev_private;
  7681. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7682. do_intel_finish_page_flip(dev, crtc);
  7683. }
  7684. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7685. {
  7686. struct drm_i915_private *dev_priv = dev->dev_private;
  7687. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7688. do_intel_finish_page_flip(dev, crtc);
  7689. }
  7690. /* Is 'a' after or equal to 'b'? */
  7691. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7692. {
  7693. return !((a - b) & 0x80000000);
  7694. }
  7695. static bool page_flip_finished(struct intel_crtc *crtc)
  7696. {
  7697. struct drm_device *dev = crtc->base.dev;
  7698. struct drm_i915_private *dev_priv = dev->dev_private;
  7699. /*
  7700. * The relevant registers doen't exist on pre-ctg.
  7701. * As the flip done interrupt doesn't trigger for mmio
  7702. * flips on gmch platforms, a flip count check isn't
  7703. * really needed there. But since ctg has the registers,
  7704. * include it in the check anyway.
  7705. */
  7706. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7707. return true;
  7708. /*
  7709. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7710. * used the same base address. In that case the mmio flip might
  7711. * have completed, but the CS hasn't even executed the flip yet.
  7712. *
  7713. * A flip count check isn't enough as the CS might have updated
  7714. * the base address just after start of vblank, but before we
  7715. * managed to process the interrupt. This means we'd complete the
  7716. * CS flip too soon.
  7717. *
  7718. * Combining both checks should get us a good enough result. It may
  7719. * still happen that the CS flip has been executed, but has not
  7720. * yet actually completed. But in case the base address is the same
  7721. * anyway, we don't really care.
  7722. */
  7723. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7724. crtc->unpin_work->gtt_offset &&
  7725. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7726. crtc->unpin_work->flip_count);
  7727. }
  7728. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7729. {
  7730. struct drm_i915_private *dev_priv = dev->dev_private;
  7731. struct intel_crtc *intel_crtc =
  7732. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7733. unsigned long flags;
  7734. /* NB: An MMIO update of the plane base pointer will also
  7735. * generate a page-flip completion irq, i.e. every modeset
  7736. * is also accompanied by a spurious intel_prepare_page_flip().
  7737. */
  7738. spin_lock_irqsave(&dev->event_lock, flags);
  7739. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7740. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7741. spin_unlock_irqrestore(&dev->event_lock, flags);
  7742. }
  7743. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7744. {
  7745. /* Ensure that the work item is consistent when activating it ... */
  7746. smp_wmb();
  7747. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7748. /* and that it is marked active as soon as the irq could fire. */
  7749. smp_wmb();
  7750. }
  7751. static int intel_gen2_queue_flip(struct drm_device *dev,
  7752. struct drm_crtc *crtc,
  7753. struct drm_framebuffer *fb,
  7754. struct drm_i915_gem_object *obj,
  7755. struct intel_engine_cs *ring,
  7756. uint32_t flags)
  7757. {
  7758. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7759. u32 flip_mask;
  7760. int ret;
  7761. ret = intel_ring_begin(ring, 6);
  7762. if (ret)
  7763. return ret;
  7764. /* Can't queue multiple flips, so wait for the previous
  7765. * one to finish before executing the next.
  7766. */
  7767. if (intel_crtc->plane)
  7768. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7769. else
  7770. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7771. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7772. intel_ring_emit(ring, MI_NOOP);
  7773. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7774. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7775. intel_ring_emit(ring, fb->pitches[0]);
  7776. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7777. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7778. intel_mark_page_flip_active(intel_crtc);
  7779. __intel_ring_advance(ring);
  7780. return 0;
  7781. }
  7782. static int intel_gen3_queue_flip(struct drm_device *dev,
  7783. struct drm_crtc *crtc,
  7784. struct drm_framebuffer *fb,
  7785. struct drm_i915_gem_object *obj,
  7786. struct intel_engine_cs *ring,
  7787. uint32_t flags)
  7788. {
  7789. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7790. u32 flip_mask;
  7791. int ret;
  7792. ret = intel_ring_begin(ring, 6);
  7793. if (ret)
  7794. return ret;
  7795. if (intel_crtc->plane)
  7796. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7797. else
  7798. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7799. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7800. intel_ring_emit(ring, MI_NOOP);
  7801. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7802. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7803. intel_ring_emit(ring, fb->pitches[0]);
  7804. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7805. intel_ring_emit(ring, MI_NOOP);
  7806. intel_mark_page_flip_active(intel_crtc);
  7807. __intel_ring_advance(ring);
  7808. return 0;
  7809. }
  7810. static int intel_gen4_queue_flip(struct drm_device *dev,
  7811. struct drm_crtc *crtc,
  7812. struct drm_framebuffer *fb,
  7813. struct drm_i915_gem_object *obj,
  7814. struct intel_engine_cs *ring,
  7815. uint32_t flags)
  7816. {
  7817. struct drm_i915_private *dev_priv = dev->dev_private;
  7818. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7819. uint32_t pf, pipesrc;
  7820. int ret;
  7821. ret = intel_ring_begin(ring, 4);
  7822. if (ret)
  7823. return ret;
  7824. /* i965+ uses the linear or tiled offsets from the
  7825. * Display Registers (which do not change across a page-flip)
  7826. * so we need only reprogram the base address.
  7827. */
  7828. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7829. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7830. intel_ring_emit(ring, fb->pitches[0]);
  7831. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7832. obj->tiling_mode);
  7833. /* XXX Enabling the panel-fitter across page-flip is so far
  7834. * untested on non-native modes, so ignore it for now.
  7835. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7836. */
  7837. pf = 0;
  7838. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7839. intel_ring_emit(ring, pf | pipesrc);
  7840. intel_mark_page_flip_active(intel_crtc);
  7841. __intel_ring_advance(ring);
  7842. return 0;
  7843. }
  7844. static int intel_gen6_queue_flip(struct drm_device *dev,
  7845. struct drm_crtc *crtc,
  7846. struct drm_framebuffer *fb,
  7847. struct drm_i915_gem_object *obj,
  7848. struct intel_engine_cs *ring,
  7849. uint32_t flags)
  7850. {
  7851. struct drm_i915_private *dev_priv = dev->dev_private;
  7852. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7853. uint32_t pf, pipesrc;
  7854. int ret;
  7855. ret = intel_ring_begin(ring, 4);
  7856. if (ret)
  7857. return ret;
  7858. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7859. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7860. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7861. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7862. /* Contrary to the suggestions in the documentation,
  7863. * "Enable Panel Fitter" does not seem to be required when page
  7864. * flipping with a non-native mode, and worse causes a normal
  7865. * modeset to fail.
  7866. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7867. */
  7868. pf = 0;
  7869. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7870. intel_ring_emit(ring, pf | pipesrc);
  7871. intel_mark_page_flip_active(intel_crtc);
  7872. __intel_ring_advance(ring);
  7873. return 0;
  7874. }
  7875. static int intel_gen7_queue_flip(struct drm_device *dev,
  7876. struct drm_crtc *crtc,
  7877. struct drm_framebuffer *fb,
  7878. struct drm_i915_gem_object *obj,
  7879. struct intel_engine_cs *ring,
  7880. uint32_t flags)
  7881. {
  7882. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7883. uint32_t plane_bit = 0;
  7884. int len, ret;
  7885. switch (intel_crtc->plane) {
  7886. case PLANE_A:
  7887. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7888. break;
  7889. case PLANE_B:
  7890. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7891. break;
  7892. case PLANE_C:
  7893. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7894. break;
  7895. default:
  7896. WARN_ONCE(1, "unknown plane in flip command\n");
  7897. return -ENODEV;
  7898. }
  7899. len = 4;
  7900. if (ring->id == RCS) {
  7901. len += 6;
  7902. /*
  7903. * On Gen 8, SRM is now taking an extra dword to accommodate
  7904. * 48bits addresses, and we need a NOOP for the batch size to
  7905. * stay even.
  7906. */
  7907. if (IS_GEN8(dev))
  7908. len += 2;
  7909. }
  7910. /*
  7911. * BSpec MI_DISPLAY_FLIP for IVB:
  7912. * "The full packet must be contained within the same cache line."
  7913. *
  7914. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7915. * cacheline, if we ever start emitting more commands before
  7916. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7917. * then do the cacheline alignment, and finally emit the
  7918. * MI_DISPLAY_FLIP.
  7919. */
  7920. ret = intel_ring_cacheline_align(ring);
  7921. if (ret)
  7922. return ret;
  7923. ret = intel_ring_begin(ring, len);
  7924. if (ret)
  7925. return ret;
  7926. /* Unmask the flip-done completion message. Note that the bspec says that
  7927. * we should do this for both the BCS and RCS, and that we must not unmask
  7928. * more than one flip event at any time (or ensure that one flip message
  7929. * can be sent by waiting for flip-done prior to queueing new flips).
  7930. * Experimentation says that BCS works despite DERRMR masking all
  7931. * flip-done completion events and that unmasking all planes at once
  7932. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7933. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7934. */
  7935. if (ring->id == RCS) {
  7936. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7937. intel_ring_emit(ring, DERRMR);
  7938. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7939. DERRMR_PIPEB_PRI_FLIP_DONE |
  7940. DERRMR_PIPEC_PRI_FLIP_DONE));
  7941. if (IS_GEN8(dev))
  7942. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7943. MI_SRM_LRM_GLOBAL_GTT);
  7944. else
  7945. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7946. MI_SRM_LRM_GLOBAL_GTT);
  7947. intel_ring_emit(ring, DERRMR);
  7948. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7949. if (IS_GEN8(dev)) {
  7950. intel_ring_emit(ring, 0);
  7951. intel_ring_emit(ring, MI_NOOP);
  7952. }
  7953. }
  7954. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7955. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7956. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7957. intel_ring_emit(ring, (MI_NOOP));
  7958. intel_mark_page_flip_active(intel_crtc);
  7959. __intel_ring_advance(ring);
  7960. return 0;
  7961. }
  7962. static bool use_mmio_flip(struct intel_engine_cs *ring,
  7963. struct drm_i915_gem_object *obj)
  7964. {
  7965. /*
  7966. * This is not being used for older platforms, because
  7967. * non-availability of flip done interrupt forces us to use
  7968. * CS flips. Older platforms derive flip done using some clever
  7969. * tricks involving the flip_pending status bits and vblank irqs.
  7970. * So using MMIO flips there would disrupt this mechanism.
  7971. */
  7972. if (ring == NULL)
  7973. return true;
  7974. if (INTEL_INFO(ring->dev)->gen < 5)
  7975. return false;
  7976. if (i915.use_mmio_flip < 0)
  7977. return false;
  7978. else if (i915.use_mmio_flip > 0)
  7979. return true;
  7980. else
  7981. return ring != obj->ring;
  7982. }
  7983. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  7984. {
  7985. struct drm_device *dev = intel_crtc->base.dev;
  7986. struct drm_i915_private *dev_priv = dev->dev_private;
  7987. struct intel_framebuffer *intel_fb =
  7988. to_intel_framebuffer(intel_crtc->base.primary->fb);
  7989. struct drm_i915_gem_object *obj = intel_fb->obj;
  7990. u32 dspcntr;
  7991. u32 reg;
  7992. intel_mark_page_flip_active(intel_crtc);
  7993. reg = DSPCNTR(intel_crtc->plane);
  7994. dspcntr = I915_READ(reg);
  7995. if (INTEL_INFO(dev)->gen >= 4) {
  7996. if (obj->tiling_mode != I915_TILING_NONE)
  7997. dspcntr |= DISPPLANE_TILED;
  7998. else
  7999. dspcntr &= ~DISPPLANE_TILED;
  8000. }
  8001. I915_WRITE(reg, dspcntr);
  8002. I915_WRITE(DSPSURF(intel_crtc->plane),
  8003. intel_crtc->unpin_work->gtt_offset);
  8004. POSTING_READ(DSPSURF(intel_crtc->plane));
  8005. }
  8006. static int intel_postpone_flip(struct drm_i915_gem_object *obj)
  8007. {
  8008. struct intel_engine_cs *ring;
  8009. int ret;
  8010. lockdep_assert_held(&obj->base.dev->struct_mutex);
  8011. if (!obj->last_write_seqno)
  8012. return 0;
  8013. ring = obj->ring;
  8014. if (i915_seqno_passed(ring->get_seqno(ring, true),
  8015. obj->last_write_seqno))
  8016. return 0;
  8017. ret = i915_gem_check_olr(ring, obj->last_write_seqno);
  8018. if (ret)
  8019. return ret;
  8020. if (WARN_ON(!ring->irq_get(ring)))
  8021. return 0;
  8022. return 1;
  8023. }
  8024. void intel_notify_mmio_flip(struct intel_engine_cs *ring)
  8025. {
  8026. struct drm_i915_private *dev_priv = to_i915(ring->dev);
  8027. struct intel_crtc *intel_crtc;
  8028. unsigned long irq_flags;
  8029. u32 seqno;
  8030. seqno = ring->get_seqno(ring, false);
  8031. spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
  8032. for_each_intel_crtc(ring->dev, intel_crtc) {
  8033. struct intel_mmio_flip *mmio_flip;
  8034. mmio_flip = &intel_crtc->mmio_flip;
  8035. if (mmio_flip->seqno == 0)
  8036. continue;
  8037. if (ring->id != mmio_flip->ring_id)
  8038. continue;
  8039. if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
  8040. intel_do_mmio_flip(intel_crtc);
  8041. mmio_flip->seqno = 0;
  8042. ring->irq_put(ring);
  8043. }
  8044. }
  8045. spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
  8046. }
  8047. static int intel_queue_mmio_flip(struct drm_device *dev,
  8048. struct drm_crtc *crtc,
  8049. struct drm_framebuffer *fb,
  8050. struct drm_i915_gem_object *obj,
  8051. struct intel_engine_cs *ring,
  8052. uint32_t flags)
  8053. {
  8054. struct drm_i915_private *dev_priv = dev->dev_private;
  8055. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8056. unsigned long irq_flags;
  8057. int ret;
  8058. if (WARN_ON(intel_crtc->mmio_flip.seqno))
  8059. return -EBUSY;
  8060. ret = intel_postpone_flip(obj);
  8061. if (ret < 0)
  8062. return ret;
  8063. if (ret == 0) {
  8064. intel_do_mmio_flip(intel_crtc);
  8065. return 0;
  8066. }
  8067. spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
  8068. intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
  8069. intel_crtc->mmio_flip.ring_id = obj->ring->id;
  8070. spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
  8071. /*
  8072. * Double check to catch cases where irq fired before
  8073. * mmio flip data was ready
  8074. */
  8075. intel_notify_mmio_flip(obj->ring);
  8076. return 0;
  8077. }
  8078. static int intel_default_queue_flip(struct drm_device *dev,
  8079. struct drm_crtc *crtc,
  8080. struct drm_framebuffer *fb,
  8081. struct drm_i915_gem_object *obj,
  8082. struct intel_engine_cs *ring,
  8083. uint32_t flags)
  8084. {
  8085. return -ENODEV;
  8086. }
  8087. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8088. struct drm_framebuffer *fb,
  8089. struct drm_pending_vblank_event *event,
  8090. uint32_t page_flip_flags)
  8091. {
  8092. struct drm_device *dev = crtc->dev;
  8093. struct drm_i915_private *dev_priv = dev->dev_private;
  8094. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8095. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8096. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8097. enum pipe pipe = intel_crtc->pipe;
  8098. struct intel_unpin_work *work;
  8099. struct intel_engine_cs *ring;
  8100. unsigned long flags;
  8101. int ret;
  8102. /*
  8103. * drm_mode_page_flip_ioctl() should already catch this, but double
  8104. * check to be safe. In the future we may enable pageflipping from
  8105. * a disabled primary plane.
  8106. */
  8107. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8108. return -EBUSY;
  8109. /* Can't change pixel format via MI display flips. */
  8110. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8111. return -EINVAL;
  8112. /*
  8113. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8114. * Note that pitch changes could also affect these register.
  8115. */
  8116. if (INTEL_INFO(dev)->gen > 3 &&
  8117. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8118. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8119. return -EINVAL;
  8120. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8121. goto out_hang;
  8122. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8123. if (work == NULL)
  8124. return -ENOMEM;
  8125. work->event = event;
  8126. work->crtc = crtc;
  8127. work->old_fb_obj = intel_fb_obj(old_fb);
  8128. INIT_WORK(&work->work, intel_unpin_work_fn);
  8129. ret = drm_crtc_vblank_get(crtc);
  8130. if (ret)
  8131. goto free_work;
  8132. /* We borrow the event spin lock for protecting unpin_work */
  8133. spin_lock_irqsave(&dev->event_lock, flags);
  8134. if (intel_crtc->unpin_work) {
  8135. spin_unlock_irqrestore(&dev->event_lock, flags);
  8136. kfree(work);
  8137. drm_crtc_vblank_put(crtc);
  8138. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8139. return -EBUSY;
  8140. }
  8141. intel_crtc->unpin_work = work;
  8142. spin_unlock_irqrestore(&dev->event_lock, flags);
  8143. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8144. flush_workqueue(dev_priv->wq);
  8145. ret = i915_mutex_lock_interruptible(dev);
  8146. if (ret)
  8147. goto cleanup;
  8148. /* Reference the objects for the scheduled work. */
  8149. drm_gem_object_reference(&work->old_fb_obj->base);
  8150. drm_gem_object_reference(&obj->base);
  8151. crtc->primary->fb = fb;
  8152. work->pending_flip_obj = obj;
  8153. work->enable_stall_check = true;
  8154. atomic_inc(&intel_crtc->unpin_work_count);
  8155. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8156. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8157. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8158. if (IS_VALLEYVIEW(dev)) {
  8159. ring = &dev_priv->ring[BCS];
  8160. if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
  8161. /* vlv: DISPLAY_FLIP fails to change tiling */
  8162. ring = NULL;
  8163. } else if (IS_IVYBRIDGE(dev)) {
  8164. ring = &dev_priv->ring[BCS];
  8165. } else if (INTEL_INFO(dev)->gen >= 7) {
  8166. ring = obj->ring;
  8167. if (ring == NULL || ring->id != RCS)
  8168. ring = &dev_priv->ring[BCS];
  8169. } else {
  8170. ring = &dev_priv->ring[RCS];
  8171. }
  8172. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  8173. if (ret)
  8174. goto cleanup_pending;
  8175. work->gtt_offset =
  8176. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8177. if (use_mmio_flip(ring, obj))
  8178. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8179. page_flip_flags);
  8180. else
  8181. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8182. page_flip_flags);
  8183. if (ret)
  8184. goto cleanup_unpin;
  8185. i915_gem_track_fb(work->old_fb_obj, obj,
  8186. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8187. intel_disable_fbc(dev);
  8188. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8189. mutex_unlock(&dev->struct_mutex);
  8190. trace_i915_flip_request(intel_crtc->plane, obj);
  8191. return 0;
  8192. cleanup_unpin:
  8193. intel_unpin_fb_obj(obj);
  8194. cleanup_pending:
  8195. atomic_dec(&intel_crtc->unpin_work_count);
  8196. crtc->primary->fb = old_fb;
  8197. drm_gem_object_unreference(&work->old_fb_obj->base);
  8198. drm_gem_object_unreference(&obj->base);
  8199. mutex_unlock(&dev->struct_mutex);
  8200. cleanup:
  8201. spin_lock_irqsave(&dev->event_lock, flags);
  8202. intel_crtc->unpin_work = NULL;
  8203. spin_unlock_irqrestore(&dev->event_lock, flags);
  8204. drm_crtc_vblank_put(crtc);
  8205. free_work:
  8206. kfree(work);
  8207. if (ret == -EIO) {
  8208. out_hang:
  8209. intel_crtc_wait_for_pending_flips(crtc);
  8210. ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
  8211. if (ret == 0 && event)
  8212. drm_send_vblank_event(dev, pipe, event);
  8213. }
  8214. return ret;
  8215. }
  8216. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8217. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8218. .load_lut = intel_crtc_load_lut,
  8219. };
  8220. /**
  8221. * intel_modeset_update_staged_output_state
  8222. *
  8223. * Updates the staged output configuration state, e.g. after we've read out the
  8224. * current hw state.
  8225. */
  8226. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8227. {
  8228. struct intel_crtc *crtc;
  8229. struct intel_encoder *encoder;
  8230. struct intel_connector *connector;
  8231. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8232. base.head) {
  8233. connector->new_encoder =
  8234. to_intel_encoder(connector->base.encoder);
  8235. }
  8236. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8237. base.head) {
  8238. encoder->new_crtc =
  8239. to_intel_crtc(encoder->base.crtc);
  8240. }
  8241. for_each_intel_crtc(dev, crtc) {
  8242. crtc->new_enabled = crtc->base.enabled;
  8243. if (crtc->new_enabled)
  8244. crtc->new_config = &crtc->config;
  8245. else
  8246. crtc->new_config = NULL;
  8247. }
  8248. }
  8249. /**
  8250. * intel_modeset_commit_output_state
  8251. *
  8252. * This function copies the stage display pipe configuration to the real one.
  8253. */
  8254. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8255. {
  8256. struct intel_crtc *crtc;
  8257. struct intel_encoder *encoder;
  8258. struct intel_connector *connector;
  8259. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8260. base.head) {
  8261. connector->base.encoder = &connector->new_encoder->base;
  8262. }
  8263. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8264. base.head) {
  8265. encoder->base.crtc = &encoder->new_crtc->base;
  8266. }
  8267. for_each_intel_crtc(dev, crtc) {
  8268. crtc->base.enabled = crtc->new_enabled;
  8269. }
  8270. }
  8271. static void
  8272. connected_sink_compute_bpp(struct intel_connector *connector,
  8273. struct intel_crtc_config *pipe_config)
  8274. {
  8275. int bpp = pipe_config->pipe_bpp;
  8276. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8277. connector->base.base.id,
  8278. connector->base.name);
  8279. /* Don't use an invalid EDID bpc value */
  8280. if (connector->base.display_info.bpc &&
  8281. connector->base.display_info.bpc * 3 < bpp) {
  8282. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8283. bpp, connector->base.display_info.bpc*3);
  8284. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8285. }
  8286. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8287. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8288. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8289. bpp);
  8290. pipe_config->pipe_bpp = 24;
  8291. }
  8292. }
  8293. static int
  8294. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8295. struct drm_framebuffer *fb,
  8296. struct intel_crtc_config *pipe_config)
  8297. {
  8298. struct drm_device *dev = crtc->base.dev;
  8299. struct intel_connector *connector;
  8300. int bpp;
  8301. switch (fb->pixel_format) {
  8302. case DRM_FORMAT_C8:
  8303. bpp = 8*3; /* since we go through a colormap */
  8304. break;
  8305. case DRM_FORMAT_XRGB1555:
  8306. case DRM_FORMAT_ARGB1555:
  8307. /* checked in intel_framebuffer_init already */
  8308. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8309. return -EINVAL;
  8310. case DRM_FORMAT_RGB565:
  8311. bpp = 6*3; /* min is 18bpp */
  8312. break;
  8313. case DRM_FORMAT_XBGR8888:
  8314. case DRM_FORMAT_ABGR8888:
  8315. /* checked in intel_framebuffer_init already */
  8316. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8317. return -EINVAL;
  8318. case DRM_FORMAT_XRGB8888:
  8319. case DRM_FORMAT_ARGB8888:
  8320. bpp = 8*3;
  8321. break;
  8322. case DRM_FORMAT_XRGB2101010:
  8323. case DRM_FORMAT_ARGB2101010:
  8324. case DRM_FORMAT_XBGR2101010:
  8325. case DRM_FORMAT_ABGR2101010:
  8326. /* checked in intel_framebuffer_init already */
  8327. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8328. return -EINVAL;
  8329. bpp = 10*3;
  8330. break;
  8331. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8332. default:
  8333. DRM_DEBUG_KMS("unsupported depth\n");
  8334. return -EINVAL;
  8335. }
  8336. pipe_config->pipe_bpp = bpp;
  8337. /* Clamp display bpp to EDID value */
  8338. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8339. base.head) {
  8340. if (!connector->new_encoder ||
  8341. connector->new_encoder->new_crtc != crtc)
  8342. continue;
  8343. connected_sink_compute_bpp(connector, pipe_config);
  8344. }
  8345. return bpp;
  8346. }
  8347. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8348. {
  8349. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8350. "type: 0x%x flags: 0x%x\n",
  8351. mode->crtc_clock,
  8352. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8353. mode->crtc_hsync_end, mode->crtc_htotal,
  8354. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8355. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8356. }
  8357. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8358. struct intel_crtc_config *pipe_config,
  8359. const char *context)
  8360. {
  8361. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8362. context, pipe_name(crtc->pipe));
  8363. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8364. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8365. pipe_config->pipe_bpp, pipe_config->dither);
  8366. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8367. pipe_config->has_pch_encoder,
  8368. pipe_config->fdi_lanes,
  8369. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8370. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8371. pipe_config->fdi_m_n.tu);
  8372. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8373. pipe_config->has_dp_encoder,
  8374. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8375. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8376. pipe_config->dp_m_n.tu);
  8377. DRM_DEBUG_KMS("requested mode:\n");
  8378. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  8379. DRM_DEBUG_KMS("adjusted mode:\n");
  8380. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  8381. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  8382. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8383. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8384. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8385. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8386. pipe_config->gmch_pfit.control,
  8387. pipe_config->gmch_pfit.pgm_ratios,
  8388. pipe_config->gmch_pfit.lvds_border_bits);
  8389. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8390. pipe_config->pch_pfit.pos,
  8391. pipe_config->pch_pfit.size,
  8392. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8393. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8394. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8395. }
  8396. static bool encoders_cloneable(const struct intel_encoder *a,
  8397. const struct intel_encoder *b)
  8398. {
  8399. /* masks could be asymmetric, so check both ways */
  8400. return a == b || (a->cloneable & (1 << b->type) &&
  8401. b->cloneable & (1 << a->type));
  8402. }
  8403. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8404. struct intel_encoder *encoder)
  8405. {
  8406. struct drm_device *dev = crtc->base.dev;
  8407. struct intel_encoder *source_encoder;
  8408. list_for_each_entry(source_encoder,
  8409. &dev->mode_config.encoder_list, base.head) {
  8410. if (source_encoder->new_crtc != crtc)
  8411. continue;
  8412. if (!encoders_cloneable(encoder, source_encoder))
  8413. return false;
  8414. }
  8415. return true;
  8416. }
  8417. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8418. {
  8419. struct drm_device *dev = crtc->base.dev;
  8420. struct intel_encoder *encoder;
  8421. list_for_each_entry(encoder,
  8422. &dev->mode_config.encoder_list, base.head) {
  8423. if (encoder->new_crtc != crtc)
  8424. continue;
  8425. if (!check_single_encoder_cloning(crtc, encoder))
  8426. return false;
  8427. }
  8428. return true;
  8429. }
  8430. static struct intel_crtc_config *
  8431. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8432. struct drm_framebuffer *fb,
  8433. struct drm_display_mode *mode)
  8434. {
  8435. struct drm_device *dev = crtc->dev;
  8436. struct intel_encoder *encoder;
  8437. struct intel_crtc_config *pipe_config;
  8438. int plane_bpp, ret = -EINVAL;
  8439. bool retry = true;
  8440. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8441. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8442. return ERR_PTR(-EINVAL);
  8443. }
  8444. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8445. if (!pipe_config)
  8446. return ERR_PTR(-ENOMEM);
  8447. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  8448. drm_mode_copy(&pipe_config->requested_mode, mode);
  8449. pipe_config->cpu_transcoder =
  8450. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8451. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8452. /*
  8453. * Sanitize sync polarity flags based on requested ones. If neither
  8454. * positive or negative polarity is requested, treat this as meaning
  8455. * negative polarity.
  8456. */
  8457. if (!(pipe_config->adjusted_mode.flags &
  8458. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8459. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8460. if (!(pipe_config->adjusted_mode.flags &
  8461. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8462. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8463. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8464. * plane pixel format and any sink constraints into account. Returns the
  8465. * source plane bpp so that dithering can be selected on mismatches
  8466. * after encoders and crtc also have had their say. */
  8467. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8468. fb, pipe_config);
  8469. if (plane_bpp < 0)
  8470. goto fail;
  8471. /*
  8472. * Determine the real pipe dimensions. Note that stereo modes can
  8473. * increase the actual pipe size due to the frame doubling and
  8474. * insertion of additional space for blanks between the frame. This
  8475. * is stored in the crtc timings. We use the requested mode to do this
  8476. * computation to clearly distinguish it from the adjusted mode, which
  8477. * can be changed by the connectors in the below retry loop.
  8478. */
  8479. drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
  8480. pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
  8481. pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
  8482. encoder_retry:
  8483. /* Ensure the port clock defaults are reset when retrying. */
  8484. pipe_config->port_clock = 0;
  8485. pipe_config->pixel_multiplier = 1;
  8486. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8487. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  8488. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8489. * adjust it according to limitations or connector properties, and also
  8490. * a chance to reject the mode entirely.
  8491. */
  8492. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8493. base.head) {
  8494. if (&encoder->new_crtc->base != crtc)
  8495. continue;
  8496. if (!(encoder->compute_config(encoder, pipe_config))) {
  8497. DRM_DEBUG_KMS("Encoder config failure\n");
  8498. goto fail;
  8499. }
  8500. }
  8501. /* Set default port clock if not overwritten by the encoder. Needs to be
  8502. * done afterwards in case the encoder adjusts the mode. */
  8503. if (!pipe_config->port_clock)
  8504. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  8505. * pipe_config->pixel_multiplier;
  8506. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8507. if (ret < 0) {
  8508. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8509. goto fail;
  8510. }
  8511. if (ret == RETRY) {
  8512. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8513. ret = -EINVAL;
  8514. goto fail;
  8515. }
  8516. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8517. retry = false;
  8518. goto encoder_retry;
  8519. }
  8520. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8521. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8522. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8523. return pipe_config;
  8524. fail:
  8525. kfree(pipe_config);
  8526. return ERR_PTR(ret);
  8527. }
  8528. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8529. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8530. static void
  8531. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8532. unsigned *prepare_pipes, unsigned *disable_pipes)
  8533. {
  8534. struct intel_crtc *intel_crtc;
  8535. struct drm_device *dev = crtc->dev;
  8536. struct intel_encoder *encoder;
  8537. struct intel_connector *connector;
  8538. struct drm_crtc *tmp_crtc;
  8539. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8540. /* Check which crtcs have changed outputs connected to them, these need
  8541. * to be part of the prepare_pipes mask. We don't (yet) support global
  8542. * modeset across multiple crtcs, so modeset_pipes will only have one
  8543. * bit set at most. */
  8544. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8545. base.head) {
  8546. if (connector->base.encoder == &connector->new_encoder->base)
  8547. continue;
  8548. if (connector->base.encoder) {
  8549. tmp_crtc = connector->base.encoder->crtc;
  8550. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8551. }
  8552. if (connector->new_encoder)
  8553. *prepare_pipes |=
  8554. 1 << connector->new_encoder->new_crtc->pipe;
  8555. }
  8556. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8557. base.head) {
  8558. if (encoder->base.crtc == &encoder->new_crtc->base)
  8559. continue;
  8560. if (encoder->base.crtc) {
  8561. tmp_crtc = encoder->base.crtc;
  8562. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8563. }
  8564. if (encoder->new_crtc)
  8565. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8566. }
  8567. /* Check for pipes that will be enabled/disabled ... */
  8568. for_each_intel_crtc(dev, intel_crtc) {
  8569. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8570. continue;
  8571. if (!intel_crtc->new_enabled)
  8572. *disable_pipes |= 1 << intel_crtc->pipe;
  8573. else
  8574. *prepare_pipes |= 1 << intel_crtc->pipe;
  8575. }
  8576. /* set_mode is also used to update properties on life display pipes. */
  8577. intel_crtc = to_intel_crtc(crtc);
  8578. if (intel_crtc->new_enabled)
  8579. *prepare_pipes |= 1 << intel_crtc->pipe;
  8580. /*
  8581. * For simplicity do a full modeset on any pipe where the output routing
  8582. * changed. We could be more clever, but that would require us to be
  8583. * more careful with calling the relevant encoder->mode_set functions.
  8584. */
  8585. if (*prepare_pipes)
  8586. *modeset_pipes = *prepare_pipes;
  8587. /* ... and mask these out. */
  8588. *modeset_pipes &= ~(*disable_pipes);
  8589. *prepare_pipes &= ~(*disable_pipes);
  8590. /*
  8591. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8592. * obies this rule, but the modeset restore mode of
  8593. * intel_modeset_setup_hw_state does not.
  8594. */
  8595. *modeset_pipes &= 1 << intel_crtc->pipe;
  8596. *prepare_pipes &= 1 << intel_crtc->pipe;
  8597. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8598. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8599. }
  8600. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8601. {
  8602. struct drm_encoder *encoder;
  8603. struct drm_device *dev = crtc->dev;
  8604. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8605. if (encoder->crtc == crtc)
  8606. return true;
  8607. return false;
  8608. }
  8609. static void
  8610. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8611. {
  8612. struct intel_encoder *intel_encoder;
  8613. struct intel_crtc *intel_crtc;
  8614. struct drm_connector *connector;
  8615. list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
  8616. base.head) {
  8617. if (!intel_encoder->base.crtc)
  8618. continue;
  8619. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8620. if (prepare_pipes & (1 << intel_crtc->pipe))
  8621. intel_encoder->connectors_active = false;
  8622. }
  8623. intel_modeset_commit_output_state(dev);
  8624. /* Double check state. */
  8625. for_each_intel_crtc(dev, intel_crtc) {
  8626. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8627. WARN_ON(intel_crtc->new_config &&
  8628. intel_crtc->new_config != &intel_crtc->config);
  8629. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8630. }
  8631. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8632. if (!connector->encoder || !connector->encoder->crtc)
  8633. continue;
  8634. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8635. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8636. struct drm_property *dpms_property =
  8637. dev->mode_config.dpms_property;
  8638. connector->dpms = DRM_MODE_DPMS_ON;
  8639. drm_object_property_set_value(&connector->base,
  8640. dpms_property,
  8641. DRM_MODE_DPMS_ON);
  8642. intel_encoder = to_intel_encoder(connector->encoder);
  8643. intel_encoder->connectors_active = true;
  8644. }
  8645. }
  8646. }
  8647. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8648. {
  8649. int diff;
  8650. if (clock1 == clock2)
  8651. return true;
  8652. if (!clock1 || !clock2)
  8653. return false;
  8654. diff = abs(clock1 - clock2);
  8655. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8656. return true;
  8657. return false;
  8658. }
  8659. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8660. list_for_each_entry((intel_crtc), \
  8661. &(dev)->mode_config.crtc_list, \
  8662. base.head) \
  8663. if (mask & (1 <<(intel_crtc)->pipe))
  8664. static bool
  8665. intel_pipe_config_compare(struct drm_device *dev,
  8666. struct intel_crtc_config *current_config,
  8667. struct intel_crtc_config *pipe_config)
  8668. {
  8669. #define PIPE_CONF_CHECK_X(name) \
  8670. if (current_config->name != pipe_config->name) { \
  8671. DRM_ERROR("mismatch in " #name " " \
  8672. "(expected 0x%08x, found 0x%08x)\n", \
  8673. current_config->name, \
  8674. pipe_config->name); \
  8675. return false; \
  8676. }
  8677. #define PIPE_CONF_CHECK_I(name) \
  8678. if (current_config->name != pipe_config->name) { \
  8679. DRM_ERROR("mismatch in " #name " " \
  8680. "(expected %i, found %i)\n", \
  8681. current_config->name, \
  8682. pipe_config->name); \
  8683. return false; \
  8684. }
  8685. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8686. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8687. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8688. "(expected %i, found %i)\n", \
  8689. current_config->name & (mask), \
  8690. pipe_config->name & (mask)); \
  8691. return false; \
  8692. }
  8693. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8694. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8695. DRM_ERROR("mismatch in " #name " " \
  8696. "(expected %i, found %i)\n", \
  8697. current_config->name, \
  8698. pipe_config->name); \
  8699. return false; \
  8700. }
  8701. #define PIPE_CONF_QUIRK(quirk) \
  8702. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8703. PIPE_CONF_CHECK_I(cpu_transcoder);
  8704. PIPE_CONF_CHECK_I(has_pch_encoder);
  8705. PIPE_CONF_CHECK_I(fdi_lanes);
  8706. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8707. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8708. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8709. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8710. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8711. PIPE_CONF_CHECK_I(has_dp_encoder);
  8712. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8713. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8714. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8715. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8716. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8717. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  8718. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  8719. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  8720. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  8721. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  8722. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  8723. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  8724. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  8725. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  8726. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  8727. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  8728. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  8729. PIPE_CONF_CHECK_I(pixel_multiplier);
  8730. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8731. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8732. IS_VALLEYVIEW(dev))
  8733. PIPE_CONF_CHECK_I(limited_color_range);
  8734. PIPE_CONF_CHECK_I(has_audio);
  8735. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8736. DRM_MODE_FLAG_INTERLACE);
  8737. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8738. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8739. DRM_MODE_FLAG_PHSYNC);
  8740. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8741. DRM_MODE_FLAG_NHSYNC);
  8742. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8743. DRM_MODE_FLAG_PVSYNC);
  8744. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8745. DRM_MODE_FLAG_NVSYNC);
  8746. }
  8747. PIPE_CONF_CHECK_I(pipe_src_w);
  8748. PIPE_CONF_CHECK_I(pipe_src_h);
  8749. /*
  8750. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8751. * screen. Since we don't yet re-compute the pipe config when moving
  8752. * just the lvds port away to another pipe the sw tracking won't match.
  8753. *
  8754. * Proper atomic modesets with recomputed global state will fix this.
  8755. * Until then just don't check gmch state for inherited modes.
  8756. */
  8757. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8758. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8759. /* pfit ratios are autocomputed by the hw on gen4+ */
  8760. if (INTEL_INFO(dev)->gen < 4)
  8761. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8762. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8763. }
  8764. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8765. if (current_config->pch_pfit.enabled) {
  8766. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8767. PIPE_CONF_CHECK_I(pch_pfit.size);
  8768. }
  8769. /* BDW+ don't expose a synchronous way to read the state */
  8770. if (IS_HASWELL(dev))
  8771. PIPE_CONF_CHECK_I(ips_enabled);
  8772. PIPE_CONF_CHECK_I(double_wide);
  8773. PIPE_CONF_CHECK_X(ddi_pll_sel);
  8774. PIPE_CONF_CHECK_I(shared_dpll);
  8775. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8776. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8777. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8778. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8779. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  8780. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8781. PIPE_CONF_CHECK_I(pipe_bpp);
  8782. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  8783. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8784. #undef PIPE_CONF_CHECK_X
  8785. #undef PIPE_CONF_CHECK_I
  8786. #undef PIPE_CONF_CHECK_FLAGS
  8787. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8788. #undef PIPE_CONF_QUIRK
  8789. return true;
  8790. }
  8791. static void
  8792. check_connector_state(struct drm_device *dev)
  8793. {
  8794. struct intel_connector *connector;
  8795. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8796. base.head) {
  8797. /* This also checks the encoder/connector hw state with the
  8798. * ->get_hw_state callbacks. */
  8799. intel_connector_check_state(connector);
  8800. WARN(&connector->new_encoder->base != connector->base.encoder,
  8801. "connector's staged encoder doesn't match current encoder\n");
  8802. }
  8803. }
  8804. static void
  8805. check_encoder_state(struct drm_device *dev)
  8806. {
  8807. struct intel_encoder *encoder;
  8808. struct intel_connector *connector;
  8809. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8810. base.head) {
  8811. bool enabled = false;
  8812. bool active = false;
  8813. enum pipe pipe, tracked_pipe;
  8814. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8815. encoder->base.base.id,
  8816. encoder->base.name);
  8817. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  8818. "encoder's stage crtc doesn't match current crtc\n");
  8819. WARN(encoder->connectors_active && !encoder->base.crtc,
  8820. "encoder's active_connectors set, but no crtc\n");
  8821. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8822. base.head) {
  8823. if (connector->base.encoder != &encoder->base)
  8824. continue;
  8825. enabled = true;
  8826. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  8827. active = true;
  8828. }
  8829. /*
  8830. * for MST connectors if we unplug the connector is gone
  8831. * away but the encoder is still connected to a crtc
  8832. * until a modeset happens in response to the hotplug.
  8833. */
  8834. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  8835. continue;
  8836. WARN(!!encoder->base.crtc != enabled,
  8837. "encoder's enabled state mismatch "
  8838. "(expected %i, found %i)\n",
  8839. !!encoder->base.crtc, enabled);
  8840. WARN(active && !encoder->base.crtc,
  8841. "active encoder with no crtc\n");
  8842. WARN(encoder->connectors_active != active,
  8843. "encoder's computed active state doesn't match tracked active state "
  8844. "(expected %i, found %i)\n", active, encoder->connectors_active);
  8845. active = encoder->get_hw_state(encoder, &pipe);
  8846. WARN(active != encoder->connectors_active,
  8847. "encoder's hw state doesn't match sw tracking "
  8848. "(expected %i, found %i)\n",
  8849. encoder->connectors_active, active);
  8850. if (!encoder->base.crtc)
  8851. continue;
  8852. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  8853. WARN(active && pipe != tracked_pipe,
  8854. "active encoder's pipe doesn't match"
  8855. "(expected %i, found %i)\n",
  8856. tracked_pipe, pipe);
  8857. }
  8858. }
  8859. static void
  8860. check_crtc_state(struct drm_device *dev)
  8861. {
  8862. struct drm_i915_private *dev_priv = dev->dev_private;
  8863. struct intel_crtc *crtc;
  8864. struct intel_encoder *encoder;
  8865. struct intel_crtc_config pipe_config;
  8866. for_each_intel_crtc(dev, crtc) {
  8867. bool enabled = false;
  8868. bool active = false;
  8869. memset(&pipe_config, 0, sizeof(pipe_config));
  8870. DRM_DEBUG_KMS("[CRTC:%d]\n",
  8871. crtc->base.base.id);
  8872. WARN(crtc->active && !crtc->base.enabled,
  8873. "active crtc, but not enabled in sw tracking\n");
  8874. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8875. base.head) {
  8876. if (encoder->base.crtc != &crtc->base)
  8877. continue;
  8878. enabled = true;
  8879. if (encoder->connectors_active)
  8880. active = true;
  8881. }
  8882. WARN(active != crtc->active,
  8883. "crtc's computed active state doesn't match tracked active state "
  8884. "(expected %i, found %i)\n", active, crtc->active);
  8885. WARN(enabled != crtc->base.enabled,
  8886. "crtc's computed enabled state doesn't match tracked enabled state "
  8887. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  8888. active = dev_priv->display.get_pipe_config(crtc,
  8889. &pipe_config);
  8890. /* hw state is inconsistent with the pipe A quirk */
  8891. if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  8892. active = crtc->active;
  8893. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8894. base.head) {
  8895. enum pipe pipe;
  8896. if (encoder->base.crtc != &crtc->base)
  8897. continue;
  8898. if (encoder->get_hw_state(encoder, &pipe))
  8899. encoder->get_config(encoder, &pipe_config);
  8900. }
  8901. WARN(crtc->active != active,
  8902. "crtc active state doesn't match with hw state "
  8903. "(expected %i, found %i)\n", crtc->active, active);
  8904. if (active &&
  8905. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  8906. WARN(1, "pipe state doesn't match!\n");
  8907. intel_dump_pipe_config(crtc, &pipe_config,
  8908. "[hw state]");
  8909. intel_dump_pipe_config(crtc, &crtc->config,
  8910. "[sw state]");
  8911. }
  8912. }
  8913. }
  8914. static void
  8915. check_shared_dpll_state(struct drm_device *dev)
  8916. {
  8917. struct drm_i915_private *dev_priv = dev->dev_private;
  8918. struct intel_crtc *crtc;
  8919. struct intel_dpll_hw_state dpll_hw_state;
  8920. int i;
  8921. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  8922. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  8923. int enabled_crtcs = 0, active_crtcs = 0;
  8924. bool active;
  8925. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  8926. DRM_DEBUG_KMS("%s\n", pll->name);
  8927. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  8928. WARN(pll->active > pll->refcount,
  8929. "more active pll users than references: %i vs %i\n",
  8930. pll->active, pll->refcount);
  8931. WARN(pll->active && !pll->on,
  8932. "pll in active use but not on in sw tracking\n");
  8933. WARN(pll->on && !pll->active,
  8934. "pll in on but not on in use in sw tracking\n");
  8935. WARN(pll->on != active,
  8936. "pll on state mismatch (expected %i, found %i)\n",
  8937. pll->on, active);
  8938. for_each_intel_crtc(dev, crtc) {
  8939. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  8940. enabled_crtcs++;
  8941. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  8942. active_crtcs++;
  8943. }
  8944. WARN(pll->active != active_crtcs,
  8945. "pll active crtcs mismatch (expected %i, found %i)\n",
  8946. pll->active, active_crtcs);
  8947. WARN(pll->refcount != enabled_crtcs,
  8948. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  8949. pll->refcount, enabled_crtcs);
  8950. WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
  8951. sizeof(dpll_hw_state)),
  8952. "pll hw state mismatch\n");
  8953. }
  8954. }
  8955. void
  8956. intel_modeset_check_state(struct drm_device *dev)
  8957. {
  8958. check_connector_state(dev);
  8959. check_encoder_state(dev);
  8960. check_crtc_state(dev);
  8961. check_shared_dpll_state(dev);
  8962. }
  8963. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  8964. int dotclock)
  8965. {
  8966. /*
  8967. * FDI already provided one idea for the dotclock.
  8968. * Yell if the encoder disagrees.
  8969. */
  8970. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  8971. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  8972. pipe_config->adjusted_mode.crtc_clock, dotclock);
  8973. }
  8974. static void update_scanline_offset(struct intel_crtc *crtc)
  8975. {
  8976. struct drm_device *dev = crtc->base.dev;
  8977. /*
  8978. * The scanline counter increments at the leading edge of hsync.
  8979. *
  8980. * On most platforms it starts counting from vtotal-1 on the
  8981. * first active line. That means the scanline counter value is
  8982. * always one less than what we would expect. Ie. just after
  8983. * start of vblank, which also occurs at start of hsync (on the
  8984. * last active line), the scanline counter will read vblank_start-1.
  8985. *
  8986. * On gen2 the scanline counter starts counting from 1 instead
  8987. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  8988. * to keep the value positive), instead of adding one.
  8989. *
  8990. * On HSW+ the behaviour of the scanline counter depends on the output
  8991. * type. For DP ports it behaves like most other platforms, but on HDMI
  8992. * there's an extra 1 line difference. So we need to add two instead of
  8993. * one to the value.
  8994. */
  8995. if (IS_GEN2(dev)) {
  8996. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  8997. int vtotal;
  8998. vtotal = mode->crtc_vtotal;
  8999. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9000. vtotal /= 2;
  9001. crtc->scanline_offset = vtotal - 1;
  9002. } else if (HAS_DDI(dev) &&
  9003. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
  9004. crtc->scanline_offset = 2;
  9005. } else
  9006. crtc->scanline_offset = 1;
  9007. }
  9008. static int __intel_set_mode(struct drm_crtc *crtc,
  9009. struct drm_display_mode *mode,
  9010. int x, int y, struct drm_framebuffer *fb)
  9011. {
  9012. struct drm_device *dev = crtc->dev;
  9013. struct drm_i915_private *dev_priv = dev->dev_private;
  9014. struct drm_display_mode *saved_mode;
  9015. struct intel_crtc_config *pipe_config = NULL;
  9016. struct intel_crtc *intel_crtc;
  9017. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  9018. int ret = 0;
  9019. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9020. if (!saved_mode)
  9021. return -ENOMEM;
  9022. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  9023. &prepare_pipes, &disable_pipes);
  9024. *saved_mode = crtc->mode;
  9025. /* Hack: Because we don't (yet) support global modeset on multiple
  9026. * crtcs, we don't keep track of the new mode for more than one crtc.
  9027. * Hence simply check whether any bit is set in modeset_pipes in all the
  9028. * pieces of code that are not yet converted to deal with mutliple crtcs
  9029. * changing their mode at the same time. */
  9030. if (modeset_pipes) {
  9031. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9032. if (IS_ERR(pipe_config)) {
  9033. ret = PTR_ERR(pipe_config);
  9034. pipe_config = NULL;
  9035. goto out;
  9036. }
  9037. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9038. "[modeset]");
  9039. to_intel_crtc(crtc)->new_config = pipe_config;
  9040. }
  9041. /*
  9042. * See if the config requires any additional preparation, e.g.
  9043. * to adjust global state with pipes off. We need to do this
  9044. * here so we can get the modeset_pipe updated config for the new
  9045. * mode set on this crtc. For other crtcs we need to use the
  9046. * adjusted_mode bits in the crtc directly.
  9047. */
  9048. if (IS_VALLEYVIEW(dev)) {
  9049. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9050. /* may have added more to prepare_pipes than we should */
  9051. prepare_pipes &= ~disable_pipes;
  9052. }
  9053. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9054. intel_crtc_disable(&intel_crtc->base);
  9055. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9056. if (intel_crtc->base.enabled)
  9057. dev_priv->display.crtc_disable(&intel_crtc->base);
  9058. }
  9059. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9060. * to set it here already despite that we pass it down the callchain.
  9061. */
  9062. if (modeset_pipes) {
  9063. crtc->mode = *mode;
  9064. /* mode_set/enable/disable functions rely on a correct pipe
  9065. * config. */
  9066. to_intel_crtc(crtc)->config = *pipe_config;
  9067. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  9068. /*
  9069. * Calculate and store various constants which
  9070. * are later needed by vblank and swap-completion
  9071. * timestamping. They are derived from true hwmode.
  9072. */
  9073. drm_calc_timestamping_constants(crtc,
  9074. &pipe_config->adjusted_mode);
  9075. }
  9076. /* Only after disabling all output pipelines that will be changed can we
  9077. * update the the output configuration. */
  9078. intel_modeset_update_state(dev, prepare_pipes);
  9079. if (dev_priv->display.modeset_global_resources)
  9080. dev_priv->display.modeset_global_resources(dev);
  9081. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9082. * on the DPLL.
  9083. */
  9084. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9085. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9086. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
  9087. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9088. mutex_lock(&dev->struct_mutex);
  9089. ret = intel_pin_and_fence_fb_obj(dev,
  9090. obj,
  9091. NULL);
  9092. if (ret != 0) {
  9093. DRM_ERROR("pin & fence failed\n");
  9094. mutex_unlock(&dev->struct_mutex);
  9095. goto done;
  9096. }
  9097. if (old_fb)
  9098. intel_unpin_fb_obj(old_obj);
  9099. i915_gem_track_fb(old_obj, obj,
  9100. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9101. mutex_unlock(&dev->struct_mutex);
  9102. crtc->primary->fb = fb;
  9103. crtc->x = x;
  9104. crtc->y = y;
  9105. ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
  9106. x, y, fb);
  9107. if (ret)
  9108. goto done;
  9109. }
  9110. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9111. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9112. update_scanline_offset(intel_crtc);
  9113. dev_priv->display.crtc_enable(&intel_crtc->base);
  9114. }
  9115. /* FIXME: add subpixel order */
  9116. done:
  9117. if (ret && crtc->enabled)
  9118. crtc->mode = *saved_mode;
  9119. out:
  9120. kfree(pipe_config);
  9121. kfree(saved_mode);
  9122. return ret;
  9123. }
  9124. static int intel_set_mode(struct drm_crtc *crtc,
  9125. struct drm_display_mode *mode,
  9126. int x, int y, struct drm_framebuffer *fb)
  9127. {
  9128. int ret;
  9129. ret = __intel_set_mode(crtc, mode, x, y, fb);
  9130. if (ret == 0)
  9131. intel_modeset_check_state(crtc->dev);
  9132. return ret;
  9133. }
  9134. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9135. {
  9136. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9137. }
  9138. #undef for_each_intel_crtc_masked
  9139. static void intel_set_config_free(struct intel_set_config *config)
  9140. {
  9141. if (!config)
  9142. return;
  9143. kfree(config->save_connector_encoders);
  9144. kfree(config->save_encoder_crtcs);
  9145. kfree(config->save_crtc_enabled);
  9146. kfree(config);
  9147. }
  9148. static int intel_set_config_save_state(struct drm_device *dev,
  9149. struct intel_set_config *config)
  9150. {
  9151. struct drm_crtc *crtc;
  9152. struct drm_encoder *encoder;
  9153. struct drm_connector *connector;
  9154. int count;
  9155. config->save_crtc_enabled =
  9156. kcalloc(dev->mode_config.num_crtc,
  9157. sizeof(bool), GFP_KERNEL);
  9158. if (!config->save_crtc_enabled)
  9159. return -ENOMEM;
  9160. config->save_encoder_crtcs =
  9161. kcalloc(dev->mode_config.num_encoder,
  9162. sizeof(struct drm_crtc *), GFP_KERNEL);
  9163. if (!config->save_encoder_crtcs)
  9164. return -ENOMEM;
  9165. config->save_connector_encoders =
  9166. kcalloc(dev->mode_config.num_connector,
  9167. sizeof(struct drm_encoder *), GFP_KERNEL);
  9168. if (!config->save_connector_encoders)
  9169. return -ENOMEM;
  9170. /* Copy data. Note that driver private data is not affected.
  9171. * Should anything bad happen only the expected state is
  9172. * restored, not the drivers personal bookkeeping.
  9173. */
  9174. count = 0;
  9175. for_each_crtc(dev, crtc) {
  9176. config->save_crtc_enabled[count++] = crtc->enabled;
  9177. }
  9178. count = 0;
  9179. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9180. config->save_encoder_crtcs[count++] = encoder->crtc;
  9181. }
  9182. count = 0;
  9183. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9184. config->save_connector_encoders[count++] = connector->encoder;
  9185. }
  9186. return 0;
  9187. }
  9188. static void intel_set_config_restore_state(struct drm_device *dev,
  9189. struct intel_set_config *config)
  9190. {
  9191. struct intel_crtc *crtc;
  9192. struct intel_encoder *encoder;
  9193. struct intel_connector *connector;
  9194. int count;
  9195. count = 0;
  9196. for_each_intel_crtc(dev, crtc) {
  9197. crtc->new_enabled = config->save_crtc_enabled[count++];
  9198. if (crtc->new_enabled)
  9199. crtc->new_config = &crtc->config;
  9200. else
  9201. crtc->new_config = NULL;
  9202. }
  9203. count = 0;
  9204. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  9205. encoder->new_crtc =
  9206. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9207. }
  9208. count = 0;
  9209. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9210. connector->new_encoder =
  9211. to_intel_encoder(config->save_connector_encoders[count++]);
  9212. }
  9213. }
  9214. static bool
  9215. is_crtc_connector_off(struct drm_mode_set *set)
  9216. {
  9217. int i;
  9218. if (set->num_connectors == 0)
  9219. return false;
  9220. if (WARN_ON(set->connectors == NULL))
  9221. return false;
  9222. for (i = 0; i < set->num_connectors; i++)
  9223. if (set->connectors[i]->encoder &&
  9224. set->connectors[i]->encoder->crtc == set->crtc &&
  9225. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9226. return true;
  9227. return false;
  9228. }
  9229. static void
  9230. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9231. struct intel_set_config *config)
  9232. {
  9233. /* We should be able to check here if the fb has the same properties
  9234. * and then just flip_or_move it */
  9235. if (is_crtc_connector_off(set)) {
  9236. config->mode_changed = true;
  9237. } else if (set->crtc->primary->fb != set->fb) {
  9238. /*
  9239. * If we have no fb, we can only flip as long as the crtc is
  9240. * active, otherwise we need a full mode set. The crtc may
  9241. * be active if we've only disabled the primary plane, or
  9242. * in fastboot situations.
  9243. */
  9244. if (set->crtc->primary->fb == NULL) {
  9245. struct intel_crtc *intel_crtc =
  9246. to_intel_crtc(set->crtc);
  9247. if (intel_crtc->active) {
  9248. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9249. config->fb_changed = true;
  9250. } else {
  9251. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9252. config->mode_changed = true;
  9253. }
  9254. } else if (set->fb == NULL) {
  9255. config->mode_changed = true;
  9256. } else if (set->fb->pixel_format !=
  9257. set->crtc->primary->fb->pixel_format) {
  9258. config->mode_changed = true;
  9259. } else {
  9260. config->fb_changed = true;
  9261. }
  9262. }
  9263. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9264. config->fb_changed = true;
  9265. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9266. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9267. drm_mode_debug_printmodeline(&set->crtc->mode);
  9268. drm_mode_debug_printmodeline(set->mode);
  9269. config->mode_changed = true;
  9270. }
  9271. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9272. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9273. }
  9274. static int
  9275. intel_modeset_stage_output_state(struct drm_device *dev,
  9276. struct drm_mode_set *set,
  9277. struct intel_set_config *config)
  9278. {
  9279. struct intel_connector *connector;
  9280. struct intel_encoder *encoder;
  9281. struct intel_crtc *crtc;
  9282. int ro;
  9283. /* The upper layers ensure that we either disable a crtc or have a list
  9284. * of connectors. For paranoia, double-check this. */
  9285. WARN_ON(!set->fb && (set->num_connectors != 0));
  9286. WARN_ON(set->fb && (set->num_connectors == 0));
  9287. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9288. base.head) {
  9289. /* Otherwise traverse passed in connector list and get encoders
  9290. * for them. */
  9291. for (ro = 0; ro < set->num_connectors; ro++) {
  9292. if (set->connectors[ro] == &connector->base) {
  9293. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9294. break;
  9295. }
  9296. }
  9297. /* If we disable the crtc, disable all its connectors. Also, if
  9298. * the connector is on the changing crtc but not on the new
  9299. * connector list, disable it. */
  9300. if ((!set->fb || ro == set->num_connectors) &&
  9301. connector->base.encoder &&
  9302. connector->base.encoder->crtc == set->crtc) {
  9303. connector->new_encoder = NULL;
  9304. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9305. connector->base.base.id,
  9306. connector->base.name);
  9307. }
  9308. if (&connector->new_encoder->base != connector->base.encoder) {
  9309. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  9310. config->mode_changed = true;
  9311. }
  9312. }
  9313. /* connector->new_encoder is now updated for all connectors. */
  9314. /* Update crtc of enabled connectors. */
  9315. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9316. base.head) {
  9317. struct drm_crtc *new_crtc;
  9318. if (!connector->new_encoder)
  9319. continue;
  9320. new_crtc = connector->new_encoder->base.crtc;
  9321. for (ro = 0; ro < set->num_connectors; ro++) {
  9322. if (set->connectors[ro] == &connector->base)
  9323. new_crtc = set->crtc;
  9324. }
  9325. /* Make sure the new CRTC will work with the encoder */
  9326. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9327. new_crtc)) {
  9328. return -EINVAL;
  9329. }
  9330. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9331. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9332. connector->base.base.id,
  9333. connector->base.name,
  9334. new_crtc->base.id);
  9335. }
  9336. /* Check for any encoders that needs to be disabled. */
  9337. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  9338. base.head) {
  9339. int num_connectors = 0;
  9340. list_for_each_entry(connector,
  9341. &dev->mode_config.connector_list,
  9342. base.head) {
  9343. if (connector->new_encoder == encoder) {
  9344. WARN_ON(!connector->new_encoder->new_crtc);
  9345. num_connectors++;
  9346. }
  9347. }
  9348. if (num_connectors == 0)
  9349. encoder->new_crtc = NULL;
  9350. else if (num_connectors > 1)
  9351. return -EINVAL;
  9352. /* Only now check for crtc changes so we don't miss encoders
  9353. * that will be disabled. */
  9354. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9355. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  9356. config->mode_changed = true;
  9357. }
  9358. }
  9359. /* Now we've also updated encoder->new_crtc for all encoders. */
  9360. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9361. base.head) {
  9362. if (connector->new_encoder)
  9363. if (connector->new_encoder != connector->encoder)
  9364. connector->encoder = connector->new_encoder;
  9365. }
  9366. for_each_intel_crtc(dev, crtc) {
  9367. crtc->new_enabled = false;
  9368. list_for_each_entry(encoder,
  9369. &dev->mode_config.encoder_list,
  9370. base.head) {
  9371. if (encoder->new_crtc == crtc) {
  9372. crtc->new_enabled = true;
  9373. break;
  9374. }
  9375. }
  9376. if (crtc->new_enabled != crtc->base.enabled) {
  9377. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  9378. crtc->new_enabled ? "en" : "dis");
  9379. config->mode_changed = true;
  9380. }
  9381. if (crtc->new_enabled)
  9382. crtc->new_config = &crtc->config;
  9383. else
  9384. crtc->new_config = NULL;
  9385. }
  9386. return 0;
  9387. }
  9388. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9389. {
  9390. struct drm_device *dev = crtc->base.dev;
  9391. struct intel_encoder *encoder;
  9392. struct intel_connector *connector;
  9393. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9394. pipe_name(crtc->pipe));
  9395. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9396. if (connector->new_encoder &&
  9397. connector->new_encoder->new_crtc == crtc)
  9398. connector->new_encoder = NULL;
  9399. }
  9400. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  9401. if (encoder->new_crtc == crtc)
  9402. encoder->new_crtc = NULL;
  9403. }
  9404. crtc->new_enabled = false;
  9405. crtc->new_config = NULL;
  9406. }
  9407. static int intel_crtc_set_config(struct drm_mode_set *set)
  9408. {
  9409. struct drm_device *dev;
  9410. struct drm_mode_set save_set;
  9411. struct intel_set_config *config;
  9412. int ret;
  9413. BUG_ON(!set);
  9414. BUG_ON(!set->crtc);
  9415. BUG_ON(!set->crtc->helper_private);
  9416. /* Enforce sane interface api - has been abused by the fb helper. */
  9417. BUG_ON(!set->mode && set->fb);
  9418. BUG_ON(set->fb && set->num_connectors == 0);
  9419. if (set->fb) {
  9420. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9421. set->crtc->base.id, set->fb->base.id,
  9422. (int)set->num_connectors, set->x, set->y);
  9423. } else {
  9424. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9425. }
  9426. dev = set->crtc->dev;
  9427. ret = -ENOMEM;
  9428. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9429. if (!config)
  9430. goto out_config;
  9431. ret = intel_set_config_save_state(dev, config);
  9432. if (ret)
  9433. goto out_config;
  9434. save_set.crtc = set->crtc;
  9435. save_set.mode = &set->crtc->mode;
  9436. save_set.x = set->crtc->x;
  9437. save_set.y = set->crtc->y;
  9438. save_set.fb = set->crtc->primary->fb;
  9439. /* Compute whether we need a full modeset, only an fb base update or no
  9440. * change at all. In the future we might also check whether only the
  9441. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9442. * such cases. */
  9443. intel_set_config_compute_mode_changes(set, config);
  9444. ret = intel_modeset_stage_output_state(dev, set, config);
  9445. if (ret)
  9446. goto fail;
  9447. if (config->mode_changed) {
  9448. ret = intel_set_mode(set->crtc, set->mode,
  9449. set->x, set->y, set->fb);
  9450. } else if (config->fb_changed) {
  9451. struct drm_i915_private *dev_priv = dev->dev_private;
  9452. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9453. intel_crtc_wait_for_pending_flips(set->crtc);
  9454. ret = intel_pipe_set_base(set->crtc,
  9455. set->x, set->y, set->fb);
  9456. /*
  9457. * We need to make sure the primary plane is re-enabled if it
  9458. * has previously been turned off.
  9459. */
  9460. if (!intel_crtc->primary_enabled && ret == 0) {
  9461. WARN_ON(!intel_crtc->active);
  9462. intel_enable_primary_hw_plane(dev_priv, intel_crtc->plane,
  9463. intel_crtc->pipe);
  9464. }
  9465. /*
  9466. * In the fastboot case this may be our only check of the
  9467. * state after boot. It would be better to only do it on
  9468. * the first update, but we don't have a nice way of doing that
  9469. * (and really, set_config isn't used much for high freq page
  9470. * flipping, so increasing its cost here shouldn't be a big
  9471. * deal).
  9472. */
  9473. if (i915.fastboot && ret == 0)
  9474. intel_modeset_check_state(set->crtc->dev);
  9475. }
  9476. if (ret) {
  9477. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9478. set->crtc->base.id, ret);
  9479. fail:
  9480. intel_set_config_restore_state(dev, config);
  9481. /*
  9482. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9483. * force the pipe off to avoid oopsing in the modeset code
  9484. * due to fb==NULL. This should only happen during boot since
  9485. * we don't yet reconstruct the FB from the hardware state.
  9486. */
  9487. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9488. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9489. /* Try to restore the config */
  9490. if (config->mode_changed &&
  9491. intel_set_mode(save_set.crtc, save_set.mode,
  9492. save_set.x, save_set.y, save_set.fb))
  9493. DRM_ERROR("failed to restore config after modeset failure\n");
  9494. }
  9495. out_config:
  9496. intel_set_config_free(config);
  9497. return ret;
  9498. }
  9499. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9500. .gamma_set = intel_crtc_gamma_set,
  9501. .set_config = intel_crtc_set_config,
  9502. .destroy = intel_crtc_destroy,
  9503. .page_flip = intel_crtc_page_flip,
  9504. };
  9505. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9506. struct intel_shared_dpll *pll,
  9507. struct intel_dpll_hw_state *hw_state)
  9508. {
  9509. uint32_t val;
  9510. if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9511. return false;
  9512. val = I915_READ(PCH_DPLL(pll->id));
  9513. hw_state->dpll = val;
  9514. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9515. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9516. return val & DPLL_VCO_ENABLE;
  9517. }
  9518. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9519. struct intel_shared_dpll *pll)
  9520. {
  9521. I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
  9522. I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
  9523. }
  9524. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9525. struct intel_shared_dpll *pll)
  9526. {
  9527. /* PCH refclock must be enabled first */
  9528. ibx_assert_pch_refclk_enabled(dev_priv);
  9529. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9530. /* Wait for the clocks to stabilize. */
  9531. POSTING_READ(PCH_DPLL(pll->id));
  9532. udelay(150);
  9533. /* The pixel multiplier can only be updated once the
  9534. * DPLL is enabled and the clocks are stable.
  9535. *
  9536. * So write it again.
  9537. */
  9538. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9539. POSTING_READ(PCH_DPLL(pll->id));
  9540. udelay(200);
  9541. }
  9542. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9543. struct intel_shared_dpll *pll)
  9544. {
  9545. struct drm_device *dev = dev_priv->dev;
  9546. struct intel_crtc *crtc;
  9547. /* Make sure no transcoder isn't still depending on us. */
  9548. for_each_intel_crtc(dev, crtc) {
  9549. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9550. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9551. }
  9552. I915_WRITE(PCH_DPLL(pll->id), 0);
  9553. POSTING_READ(PCH_DPLL(pll->id));
  9554. udelay(200);
  9555. }
  9556. static char *ibx_pch_dpll_names[] = {
  9557. "PCH DPLL A",
  9558. "PCH DPLL B",
  9559. };
  9560. static void ibx_pch_dpll_init(struct drm_device *dev)
  9561. {
  9562. struct drm_i915_private *dev_priv = dev->dev_private;
  9563. int i;
  9564. dev_priv->num_shared_dpll = 2;
  9565. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9566. dev_priv->shared_dplls[i].id = i;
  9567. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9568. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9569. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9570. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9571. dev_priv->shared_dplls[i].get_hw_state =
  9572. ibx_pch_dpll_get_hw_state;
  9573. }
  9574. }
  9575. static void intel_shared_dpll_init(struct drm_device *dev)
  9576. {
  9577. struct drm_i915_private *dev_priv = dev->dev_private;
  9578. if (HAS_DDI(dev))
  9579. intel_ddi_pll_init(dev);
  9580. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9581. ibx_pch_dpll_init(dev);
  9582. else
  9583. dev_priv->num_shared_dpll = 0;
  9584. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9585. }
  9586. static int
  9587. intel_primary_plane_disable(struct drm_plane *plane)
  9588. {
  9589. struct drm_device *dev = plane->dev;
  9590. struct drm_i915_private *dev_priv = dev->dev_private;
  9591. struct intel_plane *intel_plane = to_intel_plane(plane);
  9592. struct intel_crtc *intel_crtc;
  9593. if (!plane->fb)
  9594. return 0;
  9595. BUG_ON(!plane->crtc);
  9596. intel_crtc = to_intel_crtc(plane->crtc);
  9597. /*
  9598. * Even though we checked plane->fb above, it's still possible that
  9599. * the primary plane has been implicitly disabled because the crtc
  9600. * coordinates given weren't visible, or because we detected
  9601. * that it was 100% covered by a sprite plane. Or, the CRTC may be
  9602. * off and we've set a fb, but haven't actually turned on the CRTC yet.
  9603. * In either case, we need to unpin the FB and let the fb pointer get
  9604. * updated, but otherwise we don't need to touch the hardware.
  9605. */
  9606. if (!intel_crtc->primary_enabled)
  9607. goto disable_unpin;
  9608. intel_crtc_wait_for_pending_flips(plane->crtc);
  9609. intel_disable_primary_hw_plane(dev_priv, intel_plane->plane,
  9610. intel_plane->pipe);
  9611. disable_unpin:
  9612. mutex_lock(&dev->struct_mutex);
  9613. i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
  9614. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9615. intel_unpin_fb_obj(intel_fb_obj(plane->fb));
  9616. mutex_unlock(&dev->struct_mutex);
  9617. plane->fb = NULL;
  9618. return 0;
  9619. }
  9620. static int
  9621. intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
  9622. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9623. unsigned int crtc_w, unsigned int crtc_h,
  9624. uint32_t src_x, uint32_t src_y,
  9625. uint32_t src_w, uint32_t src_h)
  9626. {
  9627. struct drm_device *dev = crtc->dev;
  9628. struct drm_i915_private *dev_priv = dev->dev_private;
  9629. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9630. struct intel_plane *intel_plane = to_intel_plane(plane);
  9631. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9632. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9633. struct drm_rect dest = {
  9634. /* integer pixels */
  9635. .x1 = crtc_x,
  9636. .y1 = crtc_y,
  9637. .x2 = crtc_x + crtc_w,
  9638. .y2 = crtc_y + crtc_h,
  9639. };
  9640. struct drm_rect src = {
  9641. /* 16.16 fixed point */
  9642. .x1 = src_x,
  9643. .y1 = src_y,
  9644. .x2 = src_x + src_w,
  9645. .y2 = src_y + src_h,
  9646. };
  9647. const struct drm_rect clip = {
  9648. /* integer pixels */
  9649. .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
  9650. .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
  9651. };
  9652. bool visible;
  9653. int ret;
  9654. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9655. &src, &dest, &clip,
  9656. DRM_PLANE_HELPER_NO_SCALING,
  9657. DRM_PLANE_HELPER_NO_SCALING,
  9658. false, true, &visible);
  9659. if (ret)
  9660. return ret;
  9661. /*
  9662. * If the CRTC isn't enabled, we're just pinning the framebuffer,
  9663. * updating the fb pointer, and returning without touching the
  9664. * hardware. This allows us to later do a drmModeSetCrtc with fb=-1 to
  9665. * turn on the display with all planes setup as desired.
  9666. */
  9667. if (!crtc->enabled) {
  9668. mutex_lock(&dev->struct_mutex);
  9669. /*
  9670. * If we already called setplane while the crtc was disabled,
  9671. * we may have an fb pinned; unpin it.
  9672. */
  9673. if (plane->fb)
  9674. intel_unpin_fb_obj(old_obj);
  9675. i915_gem_track_fb(old_obj, obj,
  9676. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9677. /* Pin and return without programming hardware */
  9678. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  9679. mutex_unlock(&dev->struct_mutex);
  9680. return ret;
  9681. }
  9682. intel_crtc_wait_for_pending_flips(crtc);
  9683. /*
  9684. * If clipping results in a non-visible primary plane, we'll disable
  9685. * the primary plane. Note that this is a bit different than what
  9686. * happens if userspace explicitly disables the plane by passing fb=0
  9687. * because plane->fb still gets set and pinned.
  9688. */
  9689. if (!visible) {
  9690. mutex_lock(&dev->struct_mutex);
  9691. /*
  9692. * Try to pin the new fb first so that we can bail out if we
  9693. * fail.
  9694. */
  9695. if (plane->fb != fb) {
  9696. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  9697. if (ret) {
  9698. mutex_unlock(&dev->struct_mutex);
  9699. return ret;
  9700. }
  9701. }
  9702. i915_gem_track_fb(old_obj, obj,
  9703. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9704. if (intel_crtc->primary_enabled)
  9705. intel_disable_primary_hw_plane(dev_priv,
  9706. intel_plane->plane,
  9707. intel_plane->pipe);
  9708. if (plane->fb != fb)
  9709. if (plane->fb)
  9710. intel_unpin_fb_obj(old_obj);
  9711. mutex_unlock(&dev->struct_mutex);
  9712. return 0;
  9713. }
  9714. ret = intel_pipe_set_base(crtc, src.x1, src.y1, fb);
  9715. if (ret)
  9716. return ret;
  9717. if (!intel_crtc->primary_enabled)
  9718. intel_enable_primary_hw_plane(dev_priv, intel_crtc->plane,
  9719. intel_crtc->pipe);
  9720. return 0;
  9721. }
  9722. /* Common destruction function for both primary and cursor planes */
  9723. static void intel_plane_destroy(struct drm_plane *plane)
  9724. {
  9725. struct intel_plane *intel_plane = to_intel_plane(plane);
  9726. drm_plane_cleanup(plane);
  9727. kfree(intel_plane);
  9728. }
  9729. static const struct drm_plane_funcs intel_primary_plane_funcs = {
  9730. .update_plane = intel_primary_plane_setplane,
  9731. .disable_plane = intel_primary_plane_disable,
  9732. .destroy = intel_plane_destroy,
  9733. };
  9734. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  9735. int pipe)
  9736. {
  9737. struct intel_plane *primary;
  9738. const uint32_t *intel_primary_formats;
  9739. int num_formats;
  9740. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  9741. if (primary == NULL)
  9742. return NULL;
  9743. primary->can_scale = false;
  9744. primary->max_downscale = 1;
  9745. primary->pipe = pipe;
  9746. primary->plane = pipe;
  9747. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  9748. primary->plane = !pipe;
  9749. if (INTEL_INFO(dev)->gen <= 3) {
  9750. intel_primary_formats = intel_primary_formats_gen2;
  9751. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  9752. } else {
  9753. intel_primary_formats = intel_primary_formats_gen4;
  9754. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  9755. }
  9756. drm_universal_plane_init(dev, &primary->base, 0,
  9757. &intel_primary_plane_funcs,
  9758. intel_primary_formats, num_formats,
  9759. DRM_PLANE_TYPE_PRIMARY);
  9760. return &primary->base;
  9761. }
  9762. static int
  9763. intel_cursor_plane_disable(struct drm_plane *plane)
  9764. {
  9765. if (!plane->fb)
  9766. return 0;
  9767. BUG_ON(!plane->crtc);
  9768. return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
  9769. }
  9770. static int
  9771. intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
  9772. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9773. unsigned int crtc_w, unsigned int crtc_h,
  9774. uint32_t src_x, uint32_t src_y,
  9775. uint32_t src_w, uint32_t src_h)
  9776. {
  9777. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9778. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  9779. struct drm_i915_gem_object *obj = intel_fb->obj;
  9780. struct drm_rect dest = {
  9781. /* integer pixels */
  9782. .x1 = crtc_x,
  9783. .y1 = crtc_y,
  9784. .x2 = crtc_x + crtc_w,
  9785. .y2 = crtc_y + crtc_h,
  9786. };
  9787. struct drm_rect src = {
  9788. /* 16.16 fixed point */
  9789. .x1 = src_x,
  9790. .y1 = src_y,
  9791. .x2 = src_x + src_w,
  9792. .y2 = src_y + src_h,
  9793. };
  9794. const struct drm_rect clip = {
  9795. /* integer pixels */
  9796. .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
  9797. .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
  9798. };
  9799. bool visible;
  9800. int ret;
  9801. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9802. &src, &dest, &clip,
  9803. DRM_PLANE_HELPER_NO_SCALING,
  9804. DRM_PLANE_HELPER_NO_SCALING,
  9805. true, true, &visible);
  9806. if (ret)
  9807. return ret;
  9808. crtc->cursor_x = crtc_x;
  9809. crtc->cursor_y = crtc_y;
  9810. if (fb != crtc->cursor->fb) {
  9811. return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
  9812. } else {
  9813. intel_crtc_update_cursor(crtc, visible);
  9814. return 0;
  9815. }
  9816. }
  9817. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  9818. .update_plane = intel_cursor_plane_update,
  9819. .disable_plane = intel_cursor_plane_disable,
  9820. .destroy = intel_plane_destroy,
  9821. };
  9822. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  9823. int pipe)
  9824. {
  9825. struct intel_plane *cursor;
  9826. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  9827. if (cursor == NULL)
  9828. return NULL;
  9829. cursor->can_scale = false;
  9830. cursor->max_downscale = 1;
  9831. cursor->pipe = pipe;
  9832. cursor->plane = pipe;
  9833. drm_universal_plane_init(dev, &cursor->base, 0,
  9834. &intel_cursor_plane_funcs,
  9835. intel_cursor_formats,
  9836. ARRAY_SIZE(intel_cursor_formats),
  9837. DRM_PLANE_TYPE_CURSOR);
  9838. return &cursor->base;
  9839. }
  9840. static void intel_crtc_init(struct drm_device *dev, int pipe)
  9841. {
  9842. struct drm_i915_private *dev_priv = dev->dev_private;
  9843. struct intel_crtc *intel_crtc;
  9844. struct drm_plane *primary = NULL;
  9845. struct drm_plane *cursor = NULL;
  9846. int i, ret;
  9847. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  9848. if (intel_crtc == NULL)
  9849. return;
  9850. primary = intel_primary_plane_create(dev, pipe);
  9851. if (!primary)
  9852. goto fail;
  9853. cursor = intel_cursor_plane_create(dev, pipe);
  9854. if (!cursor)
  9855. goto fail;
  9856. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  9857. cursor, &intel_crtc_funcs);
  9858. if (ret)
  9859. goto fail;
  9860. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  9861. for (i = 0; i < 256; i++) {
  9862. intel_crtc->lut_r[i] = i;
  9863. intel_crtc->lut_g[i] = i;
  9864. intel_crtc->lut_b[i] = i;
  9865. }
  9866. /*
  9867. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  9868. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  9869. */
  9870. intel_crtc->pipe = pipe;
  9871. intel_crtc->plane = pipe;
  9872. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  9873. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  9874. intel_crtc->plane = !pipe;
  9875. }
  9876. intel_crtc->cursor_base = ~0;
  9877. intel_crtc->cursor_cntl = ~0;
  9878. init_waitqueue_head(&intel_crtc->vbl_wait);
  9879. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  9880. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  9881. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  9882. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  9883. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  9884. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  9885. return;
  9886. fail:
  9887. if (primary)
  9888. drm_plane_cleanup(primary);
  9889. if (cursor)
  9890. drm_plane_cleanup(cursor);
  9891. kfree(intel_crtc);
  9892. }
  9893. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  9894. {
  9895. struct drm_encoder *encoder = connector->base.encoder;
  9896. struct drm_device *dev = connector->base.dev;
  9897. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  9898. if (!encoder)
  9899. return INVALID_PIPE;
  9900. return to_intel_crtc(encoder->crtc)->pipe;
  9901. }
  9902. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  9903. struct drm_file *file)
  9904. {
  9905. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  9906. struct drm_crtc *drmmode_crtc;
  9907. struct intel_crtc *crtc;
  9908. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  9909. return -ENODEV;
  9910. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  9911. if (!drmmode_crtc) {
  9912. DRM_ERROR("no such CRTC id\n");
  9913. return -ENOENT;
  9914. }
  9915. crtc = to_intel_crtc(drmmode_crtc);
  9916. pipe_from_crtc_id->pipe = crtc->pipe;
  9917. return 0;
  9918. }
  9919. static int intel_encoder_clones(struct intel_encoder *encoder)
  9920. {
  9921. struct drm_device *dev = encoder->base.dev;
  9922. struct intel_encoder *source_encoder;
  9923. int index_mask = 0;
  9924. int entry = 0;
  9925. list_for_each_entry(source_encoder,
  9926. &dev->mode_config.encoder_list, base.head) {
  9927. if (encoders_cloneable(encoder, source_encoder))
  9928. index_mask |= (1 << entry);
  9929. entry++;
  9930. }
  9931. return index_mask;
  9932. }
  9933. static bool has_edp_a(struct drm_device *dev)
  9934. {
  9935. struct drm_i915_private *dev_priv = dev->dev_private;
  9936. if (!IS_MOBILE(dev))
  9937. return false;
  9938. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  9939. return false;
  9940. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  9941. return false;
  9942. return true;
  9943. }
  9944. const char *intel_output_name(int output)
  9945. {
  9946. static const char *names[] = {
  9947. [INTEL_OUTPUT_UNUSED] = "Unused",
  9948. [INTEL_OUTPUT_ANALOG] = "Analog",
  9949. [INTEL_OUTPUT_DVO] = "DVO",
  9950. [INTEL_OUTPUT_SDVO] = "SDVO",
  9951. [INTEL_OUTPUT_LVDS] = "LVDS",
  9952. [INTEL_OUTPUT_TVOUT] = "TV",
  9953. [INTEL_OUTPUT_HDMI] = "HDMI",
  9954. [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
  9955. [INTEL_OUTPUT_EDP] = "eDP",
  9956. [INTEL_OUTPUT_DSI] = "DSI",
  9957. [INTEL_OUTPUT_UNKNOWN] = "Unknown",
  9958. };
  9959. if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
  9960. return "Invalid";
  9961. return names[output];
  9962. }
  9963. static bool intel_crt_present(struct drm_device *dev)
  9964. {
  9965. struct drm_i915_private *dev_priv = dev->dev_private;
  9966. if (IS_ULT(dev))
  9967. return false;
  9968. if (IS_CHERRYVIEW(dev))
  9969. return false;
  9970. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  9971. return false;
  9972. return true;
  9973. }
  9974. static void intel_setup_outputs(struct drm_device *dev)
  9975. {
  9976. struct drm_i915_private *dev_priv = dev->dev_private;
  9977. struct intel_encoder *encoder;
  9978. bool dpd_is_edp = false;
  9979. intel_lvds_init(dev);
  9980. if (intel_crt_present(dev))
  9981. intel_crt_init(dev);
  9982. if (HAS_DDI(dev)) {
  9983. int found;
  9984. /* Haswell uses DDI functions to detect digital outputs */
  9985. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  9986. /* DDI A only supports eDP */
  9987. if (found)
  9988. intel_ddi_init(dev, PORT_A);
  9989. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  9990. * register */
  9991. found = I915_READ(SFUSE_STRAP);
  9992. if (found & SFUSE_STRAP_DDIB_DETECTED)
  9993. intel_ddi_init(dev, PORT_B);
  9994. if (found & SFUSE_STRAP_DDIC_DETECTED)
  9995. intel_ddi_init(dev, PORT_C);
  9996. if (found & SFUSE_STRAP_DDID_DETECTED)
  9997. intel_ddi_init(dev, PORT_D);
  9998. } else if (HAS_PCH_SPLIT(dev)) {
  9999. int found;
  10000. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10001. if (has_edp_a(dev))
  10002. intel_dp_init(dev, DP_A, PORT_A);
  10003. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10004. /* PCH SDVOB multiplex with HDMIB */
  10005. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10006. if (!found)
  10007. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10008. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10009. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10010. }
  10011. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10012. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10013. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10014. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10015. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10016. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10017. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10018. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10019. } else if (IS_VALLEYVIEW(dev)) {
  10020. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
  10021. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10022. PORT_B);
  10023. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
  10024. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10025. }
  10026. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
  10027. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10028. PORT_C);
  10029. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
  10030. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10031. }
  10032. if (IS_CHERRYVIEW(dev)) {
  10033. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
  10034. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10035. PORT_D);
  10036. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10037. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10038. }
  10039. }
  10040. intel_dsi_init(dev);
  10041. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10042. bool found = false;
  10043. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10044. DRM_DEBUG_KMS("probing SDVOB\n");
  10045. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10046. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10047. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10048. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10049. }
  10050. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10051. intel_dp_init(dev, DP_B, PORT_B);
  10052. }
  10053. /* Before G4X SDVOC doesn't have its own detect register */
  10054. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10055. DRM_DEBUG_KMS("probing SDVOC\n");
  10056. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10057. }
  10058. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10059. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10060. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10061. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10062. }
  10063. if (SUPPORTS_INTEGRATED_DP(dev))
  10064. intel_dp_init(dev, DP_C, PORT_C);
  10065. }
  10066. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10067. (I915_READ(DP_D) & DP_DETECTED))
  10068. intel_dp_init(dev, DP_D, PORT_D);
  10069. } else if (IS_GEN2(dev))
  10070. intel_dvo_init(dev);
  10071. if (SUPPORTS_TV(dev))
  10072. intel_tv_init(dev);
  10073. intel_edp_psr_init(dev);
  10074. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  10075. encoder->base.possible_crtcs = encoder->crtc_mask;
  10076. encoder->base.possible_clones =
  10077. intel_encoder_clones(encoder);
  10078. }
  10079. intel_init_pch_refclk(dev);
  10080. drm_helper_move_panel_connectors_to_head(dev);
  10081. }
  10082. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10083. {
  10084. struct drm_device *dev = fb->dev;
  10085. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10086. drm_framebuffer_cleanup(fb);
  10087. mutex_lock(&dev->struct_mutex);
  10088. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10089. drm_gem_object_unreference(&intel_fb->obj->base);
  10090. mutex_unlock(&dev->struct_mutex);
  10091. kfree(intel_fb);
  10092. }
  10093. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10094. struct drm_file *file,
  10095. unsigned int *handle)
  10096. {
  10097. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10098. struct drm_i915_gem_object *obj = intel_fb->obj;
  10099. return drm_gem_handle_create(file, &obj->base, handle);
  10100. }
  10101. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10102. .destroy = intel_user_framebuffer_destroy,
  10103. .create_handle = intel_user_framebuffer_create_handle,
  10104. };
  10105. static int intel_framebuffer_init(struct drm_device *dev,
  10106. struct intel_framebuffer *intel_fb,
  10107. struct drm_mode_fb_cmd2 *mode_cmd,
  10108. struct drm_i915_gem_object *obj)
  10109. {
  10110. int aligned_height;
  10111. int pitch_limit;
  10112. int ret;
  10113. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10114. if (obj->tiling_mode == I915_TILING_Y) {
  10115. DRM_DEBUG("hardware does not support tiling Y\n");
  10116. return -EINVAL;
  10117. }
  10118. if (mode_cmd->pitches[0] & 63) {
  10119. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  10120. mode_cmd->pitches[0]);
  10121. return -EINVAL;
  10122. }
  10123. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10124. pitch_limit = 32*1024;
  10125. } else if (INTEL_INFO(dev)->gen >= 4) {
  10126. if (obj->tiling_mode)
  10127. pitch_limit = 16*1024;
  10128. else
  10129. pitch_limit = 32*1024;
  10130. } else if (INTEL_INFO(dev)->gen >= 3) {
  10131. if (obj->tiling_mode)
  10132. pitch_limit = 8*1024;
  10133. else
  10134. pitch_limit = 16*1024;
  10135. } else
  10136. /* XXX DSPC is limited to 4k tiled */
  10137. pitch_limit = 8*1024;
  10138. if (mode_cmd->pitches[0] > pitch_limit) {
  10139. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  10140. obj->tiling_mode ? "tiled" : "linear",
  10141. mode_cmd->pitches[0], pitch_limit);
  10142. return -EINVAL;
  10143. }
  10144. if (obj->tiling_mode != I915_TILING_NONE &&
  10145. mode_cmd->pitches[0] != obj->stride) {
  10146. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10147. mode_cmd->pitches[0], obj->stride);
  10148. return -EINVAL;
  10149. }
  10150. /* Reject formats not supported by any plane early. */
  10151. switch (mode_cmd->pixel_format) {
  10152. case DRM_FORMAT_C8:
  10153. case DRM_FORMAT_RGB565:
  10154. case DRM_FORMAT_XRGB8888:
  10155. case DRM_FORMAT_ARGB8888:
  10156. break;
  10157. case DRM_FORMAT_XRGB1555:
  10158. case DRM_FORMAT_ARGB1555:
  10159. if (INTEL_INFO(dev)->gen > 3) {
  10160. DRM_DEBUG("unsupported pixel format: %s\n",
  10161. drm_get_format_name(mode_cmd->pixel_format));
  10162. return -EINVAL;
  10163. }
  10164. break;
  10165. case DRM_FORMAT_XBGR8888:
  10166. case DRM_FORMAT_ABGR8888:
  10167. case DRM_FORMAT_XRGB2101010:
  10168. case DRM_FORMAT_ARGB2101010:
  10169. case DRM_FORMAT_XBGR2101010:
  10170. case DRM_FORMAT_ABGR2101010:
  10171. if (INTEL_INFO(dev)->gen < 4) {
  10172. DRM_DEBUG("unsupported pixel format: %s\n",
  10173. drm_get_format_name(mode_cmd->pixel_format));
  10174. return -EINVAL;
  10175. }
  10176. break;
  10177. case DRM_FORMAT_YUYV:
  10178. case DRM_FORMAT_UYVY:
  10179. case DRM_FORMAT_YVYU:
  10180. case DRM_FORMAT_VYUY:
  10181. if (INTEL_INFO(dev)->gen < 5) {
  10182. DRM_DEBUG("unsupported pixel format: %s\n",
  10183. drm_get_format_name(mode_cmd->pixel_format));
  10184. return -EINVAL;
  10185. }
  10186. break;
  10187. default:
  10188. DRM_DEBUG("unsupported pixel format: %s\n",
  10189. drm_get_format_name(mode_cmd->pixel_format));
  10190. return -EINVAL;
  10191. }
  10192. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10193. if (mode_cmd->offsets[0] != 0)
  10194. return -EINVAL;
  10195. aligned_height = intel_align_height(dev, mode_cmd->height,
  10196. obj->tiling_mode);
  10197. /* FIXME drm helper for size checks (especially planar formats)? */
  10198. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10199. return -EINVAL;
  10200. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10201. intel_fb->obj = obj;
  10202. intel_fb->obj->framebuffer_references++;
  10203. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10204. if (ret) {
  10205. DRM_ERROR("framebuffer init failed %d\n", ret);
  10206. return ret;
  10207. }
  10208. return 0;
  10209. }
  10210. static struct drm_framebuffer *
  10211. intel_user_framebuffer_create(struct drm_device *dev,
  10212. struct drm_file *filp,
  10213. struct drm_mode_fb_cmd2 *mode_cmd)
  10214. {
  10215. struct drm_i915_gem_object *obj;
  10216. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10217. mode_cmd->handles[0]));
  10218. if (&obj->base == NULL)
  10219. return ERR_PTR(-ENOENT);
  10220. return intel_framebuffer_create(dev, mode_cmd, obj);
  10221. }
  10222. #ifndef CONFIG_DRM_I915_FBDEV
  10223. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10224. {
  10225. }
  10226. #endif
  10227. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10228. .fb_create = intel_user_framebuffer_create,
  10229. .output_poll_changed = intel_fbdev_output_poll_changed,
  10230. };
  10231. /* Set up chip specific display functions */
  10232. static void intel_init_display(struct drm_device *dev)
  10233. {
  10234. struct drm_i915_private *dev_priv = dev->dev_private;
  10235. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10236. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10237. else if (IS_CHERRYVIEW(dev))
  10238. dev_priv->display.find_dpll = chv_find_best_dpll;
  10239. else if (IS_VALLEYVIEW(dev))
  10240. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10241. else if (IS_PINEVIEW(dev))
  10242. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10243. else
  10244. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10245. if (HAS_DDI(dev)) {
  10246. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10247. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10248. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  10249. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10250. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10251. dev_priv->display.off = ironlake_crtc_off;
  10252. dev_priv->display.update_primary_plane =
  10253. ironlake_update_primary_plane;
  10254. } else if (HAS_PCH_SPLIT(dev)) {
  10255. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10256. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10257. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  10258. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10259. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  10260. dev_priv->display.off = ironlake_crtc_off;
  10261. dev_priv->display.update_primary_plane =
  10262. ironlake_update_primary_plane;
  10263. } else if (IS_VALLEYVIEW(dev)) {
  10264. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10265. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10266. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  10267. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10268. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10269. dev_priv->display.off = i9xx_crtc_off;
  10270. dev_priv->display.update_primary_plane =
  10271. i9xx_update_primary_plane;
  10272. } else {
  10273. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10274. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10275. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  10276. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  10277. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10278. dev_priv->display.off = i9xx_crtc_off;
  10279. dev_priv->display.update_primary_plane =
  10280. i9xx_update_primary_plane;
  10281. }
  10282. /* Returns the core display clock speed */
  10283. if (IS_VALLEYVIEW(dev))
  10284. dev_priv->display.get_display_clock_speed =
  10285. valleyview_get_display_clock_speed;
  10286. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10287. dev_priv->display.get_display_clock_speed =
  10288. i945_get_display_clock_speed;
  10289. else if (IS_I915G(dev))
  10290. dev_priv->display.get_display_clock_speed =
  10291. i915_get_display_clock_speed;
  10292. else if (IS_I945GM(dev) || IS_845G(dev))
  10293. dev_priv->display.get_display_clock_speed =
  10294. i9xx_misc_get_display_clock_speed;
  10295. else if (IS_PINEVIEW(dev))
  10296. dev_priv->display.get_display_clock_speed =
  10297. pnv_get_display_clock_speed;
  10298. else if (IS_I915GM(dev))
  10299. dev_priv->display.get_display_clock_speed =
  10300. i915gm_get_display_clock_speed;
  10301. else if (IS_I865G(dev))
  10302. dev_priv->display.get_display_clock_speed =
  10303. i865_get_display_clock_speed;
  10304. else if (IS_I85X(dev))
  10305. dev_priv->display.get_display_clock_speed =
  10306. i855_get_display_clock_speed;
  10307. else /* 852, 830 */
  10308. dev_priv->display.get_display_clock_speed =
  10309. i830_get_display_clock_speed;
  10310. if (HAS_PCH_SPLIT(dev)) {
  10311. if (IS_GEN5(dev)) {
  10312. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10313. dev_priv->display.write_eld = ironlake_write_eld;
  10314. } else if (IS_GEN6(dev)) {
  10315. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10316. dev_priv->display.write_eld = ironlake_write_eld;
  10317. dev_priv->display.modeset_global_resources =
  10318. snb_modeset_global_resources;
  10319. } else if (IS_IVYBRIDGE(dev)) {
  10320. /* FIXME: detect B0+ stepping and use auto training */
  10321. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10322. dev_priv->display.write_eld = ironlake_write_eld;
  10323. dev_priv->display.modeset_global_resources =
  10324. ivb_modeset_global_resources;
  10325. } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
  10326. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10327. dev_priv->display.write_eld = haswell_write_eld;
  10328. dev_priv->display.modeset_global_resources =
  10329. haswell_modeset_global_resources;
  10330. }
  10331. } else if (IS_G4X(dev)) {
  10332. dev_priv->display.write_eld = g4x_write_eld;
  10333. } else if (IS_VALLEYVIEW(dev)) {
  10334. dev_priv->display.modeset_global_resources =
  10335. valleyview_modeset_global_resources;
  10336. dev_priv->display.write_eld = ironlake_write_eld;
  10337. }
  10338. /* Default just returns -ENODEV to indicate unsupported */
  10339. dev_priv->display.queue_flip = intel_default_queue_flip;
  10340. switch (INTEL_INFO(dev)->gen) {
  10341. case 2:
  10342. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10343. break;
  10344. case 3:
  10345. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  10346. break;
  10347. case 4:
  10348. case 5:
  10349. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  10350. break;
  10351. case 6:
  10352. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  10353. break;
  10354. case 7:
  10355. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  10356. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  10357. break;
  10358. }
  10359. intel_panel_init_backlight_funcs(dev);
  10360. }
  10361. /*
  10362. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  10363. * resume, or other times. This quirk makes sure that's the case for
  10364. * affected systems.
  10365. */
  10366. static void quirk_pipea_force(struct drm_device *dev)
  10367. {
  10368. struct drm_i915_private *dev_priv = dev->dev_private;
  10369. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  10370. DRM_INFO("applying pipe a force quirk\n");
  10371. }
  10372. /*
  10373. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  10374. */
  10375. static void quirk_ssc_force_disable(struct drm_device *dev)
  10376. {
  10377. struct drm_i915_private *dev_priv = dev->dev_private;
  10378. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  10379. DRM_INFO("applying lvds SSC disable quirk\n");
  10380. }
  10381. /*
  10382. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  10383. * brightness value
  10384. */
  10385. static void quirk_invert_brightness(struct drm_device *dev)
  10386. {
  10387. struct drm_i915_private *dev_priv = dev->dev_private;
  10388. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  10389. DRM_INFO("applying inverted panel brightness quirk\n");
  10390. }
  10391. /* Some VBT's incorrectly indicate no backlight is present */
  10392. static void quirk_backlight_present(struct drm_device *dev)
  10393. {
  10394. struct drm_i915_private *dev_priv = dev->dev_private;
  10395. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  10396. DRM_INFO("applying backlight present quirk\n");
  10397. }
  10398. struct intel_quirk {
  10399. int device;
  10400. int subsystem_vendor;
  10401. int subsystem_device;
  10402. void (*hook)(struct drm_device *dev);
  10403. };
  10404. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  10405. struct intel_dmi_quirk {
  10406. void (*hook)(struct drm_device *dev);
  10407. const struct dmi_system_id (*dmi_id_list)[];
  10408. };
  10409. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  10410. {
  10411. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  10412. return 1;
  10413. }
  10414. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  10415. {
  10416. .dmi_id_list = &(const struct dmi_system_id[]) {
  10417. {
  10418. .callback = intel_dmi_reverse_brightness,
  10419. .ident = "NCR Corporation",
  10420. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  10421. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  10422. },
  10423. },
  10424. { } /* terminating entry */
  10425. },
  10426. .hook = quirk_invert_brightness,
  10427. },
  10428. };
  10429. static struct intel_quirk intel_quirks[] = {
  10430. /* HP Mini needs pipe A force quirk (LP: #322104) */
  10431. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  10432. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  10433. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  10434. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  10435. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  10436. /* Lenovo U160 cannot use SSC on LVDS */
  10437. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  10438. /* Sony Vaio Y cannot use SSC on LVDS */
  10439. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  10440. /* Acer Aspire 5734Z must invert backlight brightness */
  10441. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  10442. /* Acer/eMachines G725 */
  10443. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  10444. /* Acer/eMachines e725 */
  10445. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  10446. /* Acer/Packard Bell NCL20 */
  10447. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  10448. /* Acer Aspire 4736Z */
  10449. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  10450. /* Acer Aspire 5336 */
  10451. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  10452. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  10453. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  10454. /* Acer C720 Chromebook (Core i3 4005U) */
  10455. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  10456. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  10457. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  10458. /* HP Chromebook 14 (Celeron 2955U) */
  10459. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  10460. };
  10461. static void intel_init_quirks(struct drm_device *dev)
  10462. {
  10463. struct pci_dev *d = dev->pdev;
  10464. int i;
  10465. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  10466. struct intel_quirk *q = &intel_quirks[i];
  10467. if (d->device == q->device &&
  10468. (d->subsystem_vendor == q->subsystem_vendor ||
  10469. q->subsystem_vendor == PCI_ANY_ID) &&
  10470. (d->subsystem_device == q->subsystem_device ||
  10471. q->subsystem_device == PCI_ANY_ID))
  10472. q->hook(dev);
  10473. }
  10474. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  10475. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  10476. intel_dmi_quirks[i].hook(dev);
  10477. }
  10478. }
  10479. /* Disable the VGA plane that we never use */
  10480. static void i915_disable_vga(struct drm_device *dev)
  10481. {
  10482. struct drm_i915_private *dev_priv = dev->dev_private;
  10483. u8 sr1;
  10484. u32 vga_reg = i915_vgacntrl_reg(dev);
  10485. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  10486. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  10487. outb(SR01, VGA_SR_INDEX);
  10488. sr1 = inb(VGA_SR_DATA);
  10489. outb(sr1 | 1<<5, VGA_SR_DATA);
  10490. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  10491. udelay(300);
  10492. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  10493. POSTING_READ(vga_reg);
  10494. }
  10495. void intel_modeset_init_hw(struct drm_device *dev)
  10496. {
  10497. intel_prepare_ddi(dev);
  10498. if (IS_VALLEYVIEW(dev))
  10499. vlv_update_cdclk(dev);
  10500. intel_init_clock_gating(dev);
  10501. intel_reset_dpio(dev);
  10502. intel_enable_gt_powersave(dev);
  10503. }
  10504. void intel_modeset_suspend_hw(struct drm_device *dev)
  10505. {
  10506. intel_suspend_hw(dev);
  10507. }
  10508. void intel_modeset_init(struct drm_device *dev)
  10509. {
  10510. struct drm_i915_private *dev_priv = dev->dev_private;
  10511. int sprite, ret;
  10512. enum pipe pipe;
  10513. struct intel_crtc *crtc;
  10514. drm_mode_config_init(dev);
  10515. dev->mode_config.min_width = 0;
  10516. dev->mode_config.min_height = 0;
  10517. dev->mode_config.preferred_depth = 24;
  10518. dev->mode_config.prefer_shadow = 1;
  10519. dev->mode_config.funcs = &intel_mode_funcs;
  10520. intel_init_quirks(dev);
  10521. intel_init_pm(dev);
  10522. if (INTEL_INFO(dev)->num_pipes == 0)
  10523. return;
  10524. intel_init_display(dev);
  10525. if (IS_GEN2(dev)) {
  10526. dev->mode_config.max_width = 2048;
  10527. dev->mode_config.max_height = 2048;
  10528. } else if (IS_GEN3(dev)) {
  10529. dev->mode_config.max_width = 4096;
  10530. dev->mode_config.max_height = 4096;
  10531. } else {
  10532. dev->mode_config.max_width = 8192;
  10533. dev->mode_config.max_height = 8192;
  10534. }
  10535. if (IS_GEN2(dev)) {
  10536. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  10537. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  10538. } else {
  10539. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  10540. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  10541. }
  10542. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  10543. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  10544. INTEL_INFO(dev)->num_pipes,
  10545. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  10546. for_each_pipe(pipe) {
  10547. intel_crtc_init(dev, pipe);
  10548. for_each_sprite(pipe, sprite) {
  10549. ret = intel_plane_init(dev, pipe, sprite);
  10550. if (ret)
  10551. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  10552. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  10553. }
  10554. }
  10555. intel_init_dpio(dev);
  10556. intel_reset_dpio(dev);
  10557. intel_shared_dpll_init(dev);
  10558. /* Just disable it once at startup */
  10559. i915_disable_vga(dev);
  10560. intel_setup_outputs(dev);
  10561. /* Just in case the BIOS is doing something questionable. */
  10562. intel_disable_fbc(dev);
  10563. drm_modeset_lock_all(dev);
  10564. intel_modeset_setup_hw_state(dev, false);
  10565. drm_modeset_unlock_all(dev);
  10566. for_each_intel_crtc(dev, crtc) {
  10567. if (!crtc->active)
  10568. continue;
  10569. /*
  10570. * Note that reserving the BIOS fb up front prevents us
  10571. * from stuffing other stolen allocations like the ring
  10572. * on top. This prevents some ugliness at boot time, and
  10573. * can even allow for smooth boot transitions if the BIOS
  10574. * fb is large enough for the active pipe configuration.
  10575. */
  10576. if (dev_priv->display.get_plane_config) {
  10577. dev_priv->display.get_plane_config(crtc,
  10578. &crtc->plane_config);
  10579. /*
  10580. * If the fb is shared between multiple heads, we'll
  10581. * just get the first one.
  10582. */
  10583. intel_find_plane_obj(crtc, &crtc->plane_config);
  10584. }
  10585. }
  10586. }
  10587. static void intel_enable_pipe_a(struct drm_device *dev)
  10588. {
  10589. struct intel_connector *connector;
  10590. struct drm_connector *crt = NULL;
  10591. struct intel_load_detect_pipe load_detect_temp;
  10592. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  10593. /* We can't just switch on the pipe A, we need to set things up with a
  10594. * proper mode and output configuration. As a gross hack, enable pipe A
  10595. * by enabling the load detect pipe once. */
  10596. list_for_each_entry(connector,
  10597. &dev->mode_config.connector_list,
  10598. base.head) {
  10599. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  10600. crt = &connector->base;
  10601. break;
  10602. }
  10603. }
  10604. if (!crt)
  10605. return;
  10606. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  10607. intel_release_load_detect_pipe(crt, &load_detect_temp);
  10608. }
  10609. static bool
  10610. intel_check_plane_mapping(struct intel_crtc *crtc)
  10611. {
  10612. struct drm_device *dev = crtc->base.dev;
  10613. struct drm_i915_private *dev_priv = dev->dev_private;
  10614. u32 reg, val;
  10615. if (INTEL_INFO(dev)->num_pipes == 1)
  10616. return true;
  10617. reg = DSPCNTR(!crtc->plane);
  10618. val = I915_READ(reg);
  10619. if ((val & DISPLAY_PLANE_ENABLE) &&
  10620. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  10621. return false;
  10622. return true;
  10623. }
  10624. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  10625. {
  10626. struct drm_device *dev = crtc->base.dev;
  10627. struct drm_i915_private *dev_priv = dev->dev_private;
  10628. u32 reg;
  10629. /* Clear any frame start delays used for debugging left by the BIOS */
  10630. reg = PIPECONF(crtc->config.cpu_transcoder);
  10631. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  10632. /* restore vblank interrupts to correct state */
  10633. if (crtc->active)
  10634. drm_vblank_on(dev, crtc->pipe);
  10635. else
  10636. drm_vblank_off(dev, crtc->pipe);
  10637. /* We need to sanitize the plane -> pipe mapping first because this will
  10638. * disable the crtc (and hence change the state) if it is wrong. Note
  10639. * that gen4+ has a fixed plane -> pipe mapping. */
  10640. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  10641. struct intel_connector *connector;
  10642. bool plane;
  10643. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  10644. crtc->base.base.id);
  10645. /* Pipe has the wrong plane attached and the plane is active.
  10646. * Temporarily change the plane mapping and disable everything
  10647. * ... */
  10648. plane = crtc->plane;
  10649. crtc->plane = !plane;
  10650. crtc->primary_enabled = true;
  10651. dev_priv->display.crtc_disable(&crtc->base);
  10652. crtc->plane = plane;
  10653. /* ... and break all links. */
  10654. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10655. base.head) {
  10656. if (connector->encoder->base.crtc != &crtc->base)
  10657. continue;
  10658. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10659. connector->base.encoder = NULL;
  10660. }
  10661. /* multiple connectors may have the same encoder:
  10662. * handle them and break crtc link separately */
  10663. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10664. base.head)
  10665. if (connector->encoder->base.crtc == &crtc->base) {
  10666. connector->encoder->base.crtc = NULL;
  10667. connector->encoder->connectors_active = false;
  10668. }
  10669. WARN_ON(crtc->active);
  10670. crtc->base.enabled = false;
  10671. }
  10672. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  10673. crtc->pipe == PIPE_A && !crtc->active) {
  10674. /* BIOS forgot to enable pipe A, this mostly happens after
  10675. * resume. Force-enable the pipe to fix this, the update_dpms
  10676. * call below we restore the pipe to the right state, but leave
  10677. * the required bits on. */
  10678. intel_enable_pipe_a(dev);
  10679. }
  10680. /* Adjust the state of the output pipe according to whether we
  10681. * have active connectors/encoders. */
  10682. intel_crtc_update_dpms(&crtc->base);
  10683. if (crtc->active != crtc->base.enabled) {
  10684. struct intel_encoder *encoder;
  10685. /* This can happen either due to bugs in the get_hw_state
  10686. * functions or because the pipe is force-enabled due to the
  10687. * pipe A quirk. */
  10688. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  10689. crtc->base.base.id,
  10690. crtc->base.enabled ? "enabled" : "disabled",
  10691. crtc->active ? "enabled" : "disabled");
  10692. crtc->base.enabled = crtc->active;
  10693. /* Because we only establish the connector -> encoder ->
  10694. * crtc links if something is active, this means the
  10695. * crtc is now deactivated. Break the links. connector
  10696. * -> encoder links are only establish when things are
  10697. * actually up, hence no need to break them. */
  10698. WARN_ON(crtc->active);
  10699. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  10700. WARN_ON(encoder->connectors_active);
  10701. encoder->base.crtc = NULL;
  10702. }
  10703. }
  10704. if (crtc->active || IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen < 5) {
  10705. /*
  10706. * We start out with underrun reporting disabled to avoid races.
  10707. * For correct bookkeeping mark this on active crtcs.
  10708. *
  10709. * Also on gmch platforms we dont have any hardware bits to
  10710. * disable the underrun reporting. Which means we need to start
  10711. * out with underrun reporting disabled also on inactive pipes,
  10712. * since otherwise we'll complain about the garbage we read when
  10713. * e.g. coming up after runtime pm.
  10714. *
  10715. * No protection against concurrent access is required - at
  10716. * worst a fifo underrun happens which also sets this to false.
  10717. */
  10718. crtc->cpu_fifo_underrun_disabled = true;
  10719. crtc->pch_fifo_underrun_disabled = true;
  10720. update_scanline_offset(crtc);
  10721. }
  10722. }
  10723. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  10724. {
  10725. struct intel_connector *connector;
  10726. struct drm_device *dev = encoder->base.dev;
  10727. /* We need to check both for a crtc link (meaning that the
  10728. * encoder is active and trying to read from a pipe) and the
  10729. * pipe itself being active. */
  10730. bool has_active_crtc = encoder->base.crtc &&
  10731. to_intel_crtc(encoder->base.crtc)->active;
  10732. if (encoder->connectors_active && !has_active_crtc) {
  10733. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  10734. encoder->base.base.id,
  10735. encoder->base.name);
  10736. /* Connector is active, but has no active pipe. This is
  10737. * fallout from our resume register restoring. Disable
  10738. * the encoder manually again. */
  10739. if (encoder->base.crtc) {
  10740. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  10741. encoder->base.base.id,
  10742. encoder->base.name);
  10743. encoder->disable(encoder);
  10744. if (encoder->post_disable)
  10745. encoder->post_disable(encoder);
  10746. }
  10747. encoder->base.crtc = NULL;
  10748. encoder->connectors_active = false;
  10749. /* Inconsistent output/port/pipe state happens presumably due to
  10750. * a bug in one of the get_hw_state functions. Or someplace else
  10751. * in our code, like the register restore mess on resume. Clamp
  10752. * things to off as a safer default. */
  10753. list_for_each_entry(connector,
  10754. &dev->mode_config.connector_list,
  10755. base.head) {
  10756. if (connector->encoder != encoder)
  10757. continue;
  10758. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10759. connector->base.encoder = NULL;
  10760. }
  10761. }
  10762. /* Enabled encoders without active connectors will be fixed in
  10763. * the crtc fixup. */
  10764. }
  10765. void i915_redisable_vga_power_on(struct drm_device *dev)
  10766. {
  10767. struct drm_i915_private *dev_priv = dev->dev_private;
  10768. u32 vga_reg = i915_vgacntrl_reg(dev);
  10769. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  10770. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  10771. i915_disable_vga(dev);
  10772. }
  10773. }
  10774. void i915_redisable_vga(struct drm_device *dev)
  10775. {
  10776. struct drm_i915_private *dev_priv = dev->dev_private;
  10777. /* This function can be called both from intel_modeset_setup_hw_state or
  10778. * at a very early point in our resume sequence, where the power well
  10779. * structures are not yet restored. Since this function is at a very
  10780. * paranoid "someone might have enabled VGA while we were not looking"
  10781. * level, just check if the power well is enabled instead of trying to
  10782. * follow the "don't touch the power well if we don't need it" policy
  10783. * the rest of the driver uses. */
  10784. if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
  10785. return;
  10786. i915_redisable_vga_power_on(dev);
  10787. }
  10788. static bool primary_get_hw_state(struct intel_crtc *crtc)
  10789. {
  10790. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  10791. if (!crtc->active)
  10792. return false;
  10793. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  10794. }
  10795. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  10796. {
  10797. struct drm_i915_private *dev_priv = dev->dev_private;
  10798. enum pipe pipe;
  10799. struct intel_crtc *crtc;
  10800. struct intel_encoder *encoder;
  10801. struct intel_connector *connector;
  10802. int i;
  10803. for_each_intel_crtc(dev, crtc) {
  10804. memset(&crtc->config, 0, sizeof(crtc->config));
  10805. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  10806. crtc->active = dev_priv->display.get_pipe_config(crtc,
  10807. &crtc->config);
  10808. crtc->base.enabled = crtc->active;
  10809. crtc->primary_enabled = primary_get_hw_state(crtc);
  10810. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  10811. crtc->base.base.id,
  10812. crtc->active ? "enabled" : "disabled");
  10813. }
  10814. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10815. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10816. pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
  10817. pll->active = 0;
  10818. for_each_intel_crtc(dev, crtc) {
  10819. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10820. pll->active++;
  10821. }
  10822. pll->refcount = pll->active;
  10823. DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
  10824. pll->name, pll->refcount, pll->on);
  10825. if (pll->refcount)
  10826. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  10827. }
  10828. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  10829. base.head) {
  10830. pipe = 0;
  10831. if (encoder->get_hw_state(encoder, &pipe)) {
  10832. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10833. encoder->base.crtc = &crtc->base;
  10834. encoder->get_config(encoder, &crtc->config);
  10835. } else {
  10836. encoder->base.crtc = NULL;
  10837. }
  10838. encoder->connectors_active = false;
  10839. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  10840. encoder->base.base.id,
  10841. encoder->base.name,
  10842. encoder->base.crtc ? "enabled" : "disabled",
  10843. pipe_name(pipe));
  10844. }
  10845. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10846. base.head) {
  10847. if (connector->get_hw_state(connector)) {
  10848. connector->base.dpms = DRM_MODE_DPMS_ON;
  10849. connector->encoder->connectors_active = true;
  10850. connector->base.encoder = &connector->encoder->base;
  10851. } else {
  10852. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10853. connector->base.encoder = NULL;
  10854. }
  10855. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  10856. connector->base.base.id,
  10857. connector->base.name,
  10858. connector->base.encoder ? "enabled" : "disabled");
  10859. }
  10860. }
  10861. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  10862. * and i915 state tracking structures. */
  10863. void intel_modeset_setup_hw_state(struct drm_device *dev,
  10864. bool force_restore)
  10865. {
  10866. struct drm_i915_private *dev_priv = dev->dev_private;
  10867. enum pipe pipe;
  10868. struct intel_crtc *crtc;
  10869. struct intel_encoder *encoder;
  10870. int i;
  10871. intel_modeset_readout_hw_state(dev);
  10872. /*
  10873. * Now that we have the config, copy it to each CRTC struct
  10874. * Note that this could go away if we move to using crtc_config
  10875. * checking everywhere.
  10876. */
  10877. for_each_intel_crtc(dev, crtc) {
  10878. if (crtc->active && i915.fastboot) {
  10879. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  10880. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  10881. crtc->base.base.id);
  10882. drm_mode_debug_printmodeline(&crtc->base.mode);
  10883. }
  10884. }
  10885. /* HW state is read out, now we need to sanitize this mess. */
  10886. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  10887. base.head) {
  10888. intel_sanitize_encoder(encoder);
  10889. }
  10890. for_each_pipe(pipe) {
  10891. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10892. intel_sanitize_crtc(crtc);
  10893. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  10894. }
  10895. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10896. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10897. if (!pll->on || pll->active)
  10898. continue;
  10899. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  10900. pll->disable(dev_priv, pll);
  10901. pll->on = false;
  10902. }
  10903. if (HAS_PCH_SPLIT(dev))
  10904. ilk_wm_get_hw_state(dev);
  10905. if (force_restore) {
  10906. i915_redisable_vga(dev);
  10907. /*
  10908. * We need to use raw interfaces for restoring state to avoid
  10909. * checking (bogus) intermediate states.
  10910. */
  10911. for_each_pipe(pipe) {
  10912. struct drm_crtc *crtc =
  10913. dev_priv->pipe_to_crtc_mapping[pipe];
  10914. __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  10915. crtc->primary->fb);
  10916. }
  10917. } else {
  10918. intel_modeset_update_staged_output_state(dev);
  10919. }
  10920. intel_modeset_check_state(dev);
  10921. }
  10922. void intel_modeset_gem_init(struct drm_device *dev)
  10923. {
  10924. struct drm_crtc *c;
  10925. struct drm_i915_gem_object *obj;
  10926. mutex_lock(&dev->struct_mutex);
  10927. intel_init_gt_powersave(dev);
  10928. mutex_unlock(&dev->struct_mutex);
  10929. intel_modeset_init_hw(dev);
  10930. intel_setup_overlay(dev);
  10931. /*
  10932. * Make sure any fbs we allocated at startup are properly
  10933. * pinned & fenced. When we do the allocation it's too early
  10934. * for this.
  10935. */
  10936. mutex_lock(&dev->struct_mutex);
  10937. for_each_crtc(dev, c) {
  10938. obj = intel_fb_obj(c->primary->fb);
  10939. if (obj == NULL)
  10940. continue;
  10941. if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
  10942. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  10943. to_intel_crtc(c)->pipe);
  10944. drm_framebuffer_unreference(c->primary->fb);
  10945. c->primary->fb = NULL;
  10946. }
  10947. }
  10948. mutex_unlock(&dev->struct_mutex);
  10949. }
  10950. void intel_connector_unregister(struct intel_connector *intel_connector)
  10951. {
  10952. struct drm_connector *connector = &intel_connector->base;
  10953. intel_panel_destroy_backlight(connector);
  10954. drm_connector_unregister(connector);
  10955. }
  10956. void intel_modeset_cleanup(struct drm_device *dev)
  10957. {
  10958. struct drm_i915_private *dev_priv = dev->dev_private;
  10959. struct drm_connector *connector;
  10960. /*
  10961. * Interrupts and polling as the first thing to avoid creating havoc.
  10962. * Too much stuff here (turning of rps, connectors, ...) would
  10963. * experience fancy races otherwise.
  10964. */
  10965. drm_irq_uninstall(dev);
  10966. intel_hpd_cancel_work(dev_priv);
  10967. dev_priv->pm._irqs_disabled = true;
  10968. /*
  10969. * Due to the hpd irq storm handling the hotplug work can re-arm the
  10970. * poll handlers. Hence disable polling after hpd handling is shut down.
  10971. */
  10972. drm_kms_helper_poll_fini(dev);
  10973. mutex_lock(&dev->struct_mutex);
  10974. intel_unregister_dsm_handler();
  10975. intel_disable_fbc(dev);
  10976. intel_disable_gt_powersave(dev);
  10977. ironlake_teardown_rc6(dev);
  10978. mutex_unlock(&dev->struct_mutex);
  10979. /* flush any delayed tasks or pending work */
  10980. flush_scheduled_work();
  10981. /* destroy the backlight and sysfs files before encoders/connectors */
  10982. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10983. struct intel_connector *intel_connector;
  10984. intel_connector = to_intel_connector(connector);
  10985. intel_connector->unregister(intel_connector);
  10986. }
  10987. drm_mode_config_cleanup(dev);
  10988. intel_cleanup_overlay(dev);
  10989. mutex_lock(&dev->struct_mutex);
  10990. intel_cleanup_gt_powersave(dev);
  10991. mutex_unlock(&dev->struct_mutex);
  10992. }
  10993. /*
  10994. * Return which encoder is currently attached for connector.
  10995. */
  10996. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  10997. {
  10998. return &intel_attached_encoder(connector)->base;
  10999. }
  11000. void intel_connector_attach_encoder(struct intel_connector *connector,
  11001. struct intel_encoder *encoder)
  11002. {
  11003. connector->encoder = encoder;
  11004. drm_mode_connector_attach_encoder(&connector->base,
  11005. &encoder->base);
  11006. }
  11007. /*
  11008. * set vga decode state - true == enable VGA decode
  11009. */
  11010. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11011. {
  11012. struct drm_i915_private *dev_priv = dev->dev_private;
  11013. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11014. u16 gmch_ctrl;
  11015. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11016. DRM_ERROR("failed to read control word\n");
  11017. return -EIO;
  11018. }
  11019. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11020. return 0;
  11021. if (state)
  11022. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11023. else
  11024. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11025. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11026. DRM_ERROR("failed to write control word\n");
  11027. return -EIO;
  11028. }
  11029. return 0;
  11030. }
  11031. struct intel_display_error_state {
  11032. u32 power_well_driver;
  11033. int num_transcoders;
  11034. struct intel_cursor_error_state {
  11035. u32 control;
  11036. u32 position;
  11037. u32 base;
  11038. u32 size;
  11039. } cursor[I915_MAX_PIPES];
  11040. struct intel_pipe_error_state {
  11041. bool power_domain_on;
  11042. u32 source;
  11043. u32 stat;
  11044. } pipe[I915_MAX_PIPES];
  11045. struct intel_plane_error_state {
  11046. u32 control;
  11047. u32 stride;
  11048. u32 size;
  11049. u32 pos;
  11050. u32 addr;
  11051. u32 surface;
  11052. u32 tile_offset;
  11053. } plane[I915_MAX_PIPES];
  11054. struct intel_transcoder_error_state {
  11055. bool power_domain_on;
  11056. enum transcoder cpu_transcoder;
  11057. u32 conf;
  11058. u32 htotal;
  11059. u32 hblank;
  11060. u32 hsync;
  11061. u32 vtotal;
  11062. u32 vblank;
  11063. u32 vsync;
  11064. } transcoder[4];
  11065. };
  11066. struct intel_display_error_state *
  11067. intel_display_capture_error_state(struct drm_device *dev)
  11068. {
  11069. struct drm_i915_private *dev_priv = dev->dev_private;
  11070. struct intel_display_error_state *error;
  11071. int transcoders[] = {
  11072. TRANSCODER_A,
  11073. TRANSCODER_B,
  11074. TRANSCODER_C,
  11075. TRANSCODER_EDP,
  11076. };
  11077. int i;
  11078. if (INTEL_INFO(dev)->num_pipes == 0)
  11079. return NULL;
  11080. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11081. if (error == NULL)
  11082. return NULL;
  11083. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11084. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11085. for_each_pipe(i) {
  11086. error->pipe[i].power_domain_on =
  11087. intel_display_power_enabled_unlocked(dev_priv,
  11088. POWER_DOMAIN_PIPE(i));
  11089. if (!error->pipe[i].power_domain_on)
  11090. continue;
  11091. error->cursor[i].control = I915_READ(CURCNTR(i));
  11092. error->cursor[i].position = I915_READ(CURPOS(i));
  11093. error->cursor[i].base = I915_READ(CURBASE(i));
  11094. error->plane[i].control = I915_READ(DSPCNTR(i));
  11095. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11096. if (INTEL_INFO(dev)->gen <= 3) {
  11097. error->plane[i].size = I915_READ(DSPSIZE(i));
  11098. error->plane[i].pos = I915_READ(DSPPOS(i));
  11099. }
  11100. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11101. error->plane[i].addr = I915_READ(DSPADDR(i));
  11102. if (INTEL_INFO(dev)->gen >= 4) {
  11103. error->plane[i].surface = I915_READ(DSPSURF(i));
  11104. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11105. }
  11106. error->pipe[i].source = I915_READ(PIPESRC(i));
  11107. if (HAS_GMCH_DISPLAY(dev))
  11108. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11109. }
  11110. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11111. if (HAS_DDI(dev_priv->dev))
  11112. error->num_transcoders++; /* Account for eDP. */
  11113. for (i = 0; i < error->num_transcoders; i++) {
  11114. enum transcoder cpu_transcoder = transcoders[i];
  11115. error->transcoder[i].power_domain_on =
  11116. intel_display_power_enabled_unlocked(dev_priv,
  11117. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11118. if (!error->transcoder[i].power_domain_on)
  11119. continue;
  11120. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11121. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11122. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11123. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11124. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11125. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11126. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11127. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11128. }
  11129. return error;
  11130. }
  11131. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11132. void
  11133. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11134. struct drm_device *dev,
  11135. struct intel_display_error_state *error)
  11136. {
  11137. int i;
  11138. if (!error)
  11139. return;
  11140. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11141. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11142. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11143. error->power_well_driver);
  11144. for_each_pipe(i) {
  11145. err_printf(m, "Pipe [%d]:\n", i);
  11146. err_printf(m, " Power: %s\n",
  11147. error->pipe[i].power_domain_on ? "on" : "off");
  11148. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11149. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11150. err_printf(m, "Plane [%d]:\n", i);
  11151. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11152. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11153. if (INTEL_INFO(dev)->gen <= 3) {
  11154. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11155. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11156. }
  11157. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11158. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11159. if (INTEL_INFO(dev)->gen >= 4) {
  11160. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11161. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11162. }
  11163. err_printf(m, "Cursor [%d]:\n", i);
  11164. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11165. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11166. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11167. }
  11168. for (i = 0; i < error->num_transcoders; i++) {
  11169. err_printf(m, "CPU transcoder: %c\n",
  11170. transcoder_name(error->transcoder[i].cpu_transcoder));
  11171. err_printf(m, " Power: %s\n",
  11172. error->transcoder[i].power_domain_on ? "on" : "off");
  11173. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11174. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11175. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11176. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11177. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11178. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11179. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11180. }
  11181. }