intel_display.c 377 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681
  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. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  71. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  72. struct intel_crtc_config *pipe_config);
  73. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  74. struct intel_crtc_config *pipe_config);
  75. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  76. int x, int y, struct drm_framebuffer *old_fb);
  77. static int intel_framebuffer_init(struct drm_device *dev,
  78. struct intel_framebuffer *ifb,
  79. struct drm_mode_fb_cmd2 *mode_cmd,
  80. struct drm_i915_gem_object *obj);
  81. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  82. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  83. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  84. struct intel_link_m_n *m_n,
  85. struct intel_link_m_n *m2_n2);
  86. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  87. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  88. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  89. static void vlv_prepare_pll(struct intel_crtc *crtc);
  90. static void chv_prepare_pll(struct intel_crtc *crtc);
  91. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  92. {
  93. if (!connector->mst_port)
  94. return connector->encoder;
  95. else
  96. return &connector->mst_port->mst_encoders[pipe]->base;
  97. }
  98. typedef struct {
  99. int min, max;
  100. } intel_range_t;
  101. typedef struct {
  102. int dot_limit;
  103. int p2_slow, p2_fast;
  104. } intel_p2_t;
  105. typedef struct intel_limit intel_limit_t;
  106. struct intel_limit {
  107. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  108. intel_p2_t p2;
  109. };
  110. int
  111. intel_pch_rawclk(struct drm_device *dev)
  112. {
  113. struct drm_i915_private *dev_priv = dev->dev_private;
  114. WARN_ON(!HAS_PCH_SPLIT(dev));
  115. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  116. }
  117. static inline u32 /* units of 100MHz */
  118. intel_fdi_link_freq(struct drm_device *dev)
  119. {
  120. if (IS_GEN5(dev)) {
  121. struct drm_i915_private *dev_priv = dev->dev_private;
  122. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  123. } else
  124. return 27;
  125. }
  126. static const intel_limit_t intel_limits_i8xx_dac = {
  127. .dot = { .min = 25000, .max = 350000 },
  128. .vco = { .min = 908000, .max = 1512000 },
  129. .n = { .min = 2, .max = 16 },
  130. .m = { .min = 96, .max = 140 },
  131. .m1 = { .min = 18, .max = 26 },
  132. .m2 = { .min = 6, .max = 16 },
  133. .p = { .min = 4, .max = 128 },
  134. .p1 = { .min = 2, .max = 33 },
  135. .p2 = { .dot_limit = 165000,
  136. .p2_slow = 4, .p2_fast = 2 },
  137. };
  138. static const intel_limit_t intel_limits_i8xx_dvo = {
  139. .dot = { .min = 25000, .max = 350000 },
  140. .vco = { .min = 908000, .max = 1512000 },
  141. .n = { .min = 2, .max = 16 },
  142. .m = { .min = 96, .max = 140 },
  143. .m1 = { .min = 18, .max = 26 },
  144. .m2 = { .min = 6, .max = 16 },
  145. .p = { .min = 4, .max = 128 },
  146. .p1 = { .min = 2, .max = 33 },
  147. .p2 = { .dot_limit = 165000,
  148. .p2_slow = 4, .p2_fast = 4 },
  149. };
  150. static const intel_limit_t intel_limits_i8xx_lvds = {
  151. .dot = { .min = 25000, .max = 350000 },
  152. .vco = { .min = 908000, .max = 1512000 },
  153. .n = { .min = 2, .max = 16 },
  154. .m = { .min = 96, .max = 140 },
  155. .m1 = { .min = 18, .max = 26 },
  156. .m2 = { .min = 6, .max = 16 },
  157. .p = { .min = 4, .max = 128 },
  158. .p1 = { .min = 1, .max = 6 },
  159. .p2 = { .dot_limit = 165000,
  160. .p2_slow = 14, .p2_fast = 7 },
  161. };
  162. static const intel_limit_t intel_limits_i9xx_sdvo = {
  163. .dot = { .min = 20000, .max = 400000 },
  164. .vco = { .min = 1400000, .max = 2800000 },
  165. .n = { .min = 1, .max = 6 },
  166. .m = { .min = 70, .max = 120 },
  167. .m1 = { .min = 8, .max = 18 },
  168. .m2 = { .min = 3, .max = 7 },
  169. .p = { .min = 5, .max = 80 },
  170. .p1 = { .min = 1, .max = 8 },
  171. .p2 = { .dot_limit = 200000,
  172. .p2_slow = 10, .p2_fast = 5 },
  173. };
  174. static const intel_limit_t intel_limits_i9xx_lvds = {
  175. .dot = { .min = 20000, .max = 400000 },
  176. .vco = { .min = 1400000, .max = 2800000 },
  177. .n = { .min = 1, .max = 6 },
  178. .m = { .min = 70, .max = 120 },
  179. .m1 = { .min = 8, .max = 18 },
  180. .m2 = { .min = 3, .max = 7 },
  181. .p = { .min = 7, .max = 98 },
  182. .p1 = { .min = 1, .max = 8 },
  183. .p2 = { .dot_limit = 112000,
  184. .p2_slow = 14, .p2_fast = 7 },
  185. };
  186. static const intel_limit_t intel_limits_g4x_sdvo = {
  187. .dot = { .min = 25000, .max = 270000 },
  188. .vco = { .min = 1750000, .max = 3500000},
  189. .n = { .min = 1, .max = 4 },
  190. .m = { .min = 104, .max = 138 },
  191. .m1 = { .min = 17, .max = 23 },
  192. .m2 = { .min = 5, .max = 11 },
  193. .p = { .min = 10, .max = 30 },
  194. .p1 = { .min = 1, .max = 3},
  195. .p2 = { .dot_limit = 270000,
  196. .p2_slow = 10,
  197. .p2_fast = 10
  198. },
  199. };
  200. static const intel_limit_t intel_limits_g4x_hdmi = {
  201. .dot = { .min = 22000, .max = 400000 },
  202. .vco = { .min = 1750000, .max = 3500000},
  203. .n = { .min = 1, .max = 4 },
  204. .m = { .min = 104, .max = 138 },
  205. .m1 = { .min = 16, .max = 23 },
  206. .m2 = { .min = 5, .max = 11 },
  207. .p = { .min = 5, .max = 80 },
  208. .p1 = { .min = 1, .max = 8},
  209. .p2 = { .dot_limit = 165000,
  210. .p2_slow = 10, .p2_fast = 5 },
  211. };
  212. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  213. .dot = { .min = 20000, .max = 115000 },
  214. .vco = { .min = 1750000, .max = 3500000 },
  215. .n = { .min = 1, .max = 3 },
  216. .m = { .min = 104, .max = 138 },
  217. .m1 = { .min = 17, .max = 23 },
  218. .m2 = { .min = 5, .max = 11 },
  219. .p = { .min = 28, .max = 112 },
  220. .p1 = { .min = 2, .max = 8 },
  221. .p2 = { .dot_limit = 0,
  222. .p2_slow = 14, .p2_fast = 14
  223. },
  224. };
  225. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  226. .dot = { .min = 80000, .max = 224000 },
  227. .vco = { .min = 1750000, .max = 3500000 },
  228. .n = { .min = 1, .max = 3 },
  229. .m = { .min = 104, .max = 138 },
  230. .m1 = { .min = 17, .max = 23 },
  231. .m2 = { .min = 5, .max = 11 },
  232. .p = { .min = 14, .max = 42 },
  233. .p1 = { .min = 2, .max = 6 },
  234. .p2 = { .dot_limit = 0,
  235. .p2_slow = 7, .p2_fast = 7
  236. },
  237. };
  238. static const intel_limit_t intel_limits_pineview_sdvo = {
  239. .dot = { .min = 20000, .max = 400000},
  240. .vco = { .min = 1700000, .max = 3500000 },
  241. /* Pineview's Ncounter is a ring counter */
  242. .n = { .min = 3, .max = 6 },
  243. .m = { .min = 2, .max = 256 },
  244. /* Pineview only has one combined m divider, which we treat as m2. */
  245. .m1 = { .min = 0, .max = 0 },
  246. .m2 = { .min = 0, .max = 254 },
  247. .p = { .min = 5, .max = 80 },
  248. .p1 = { .min = 1, .max = 8 },
  249. .p2 = { .dot_limit = 200000,
  250. .p2_slow = 10, .p2_fast = 5 },
  251. };
  252. static const intel_limit_t intel_limits_pineview_lvds = {
  253. .dot = { .min = 20000, .max = 400000 },
  254. .vco = { .min = 1700000, .max = 3500000 },
  255. .n = { .min = 3, .max = 6 },
  256. .m = { .min = 2, .max = 256 },
  257. .m1 = { .min = 0, .max = 0 },
  258. .m2 = { .min = 0, .max = 254 },
  259. .p = { .min = 7, .max = 112 },
  260. .p1 = { .min = 1, .max = 8 },
  261. .p2 = { .dot_limit = 112000,
  262. .p2_slow = 14, .p2_fast = 14 },
  263. };
  264. /* Ironlake / Sandybridge
  265. *
  266. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  267. * the range value for them is (actual_value - 2).
  268. */
  269. static const intel_limit_t intel_limits_ironlake_dac = {
  270. .dot = { .min = 25000, .max = 350000 },
  271. .vco = { .min = 1760000, .max = 3510000 },
  272. .n = { .min = 1, .max = 5 },
  273. .m = { .min = 79, .max = 127 },
  274. .m1 = { .min = 12, .max = 22 },
  275. .m2 = { .min = 5, .max = 9 },
  276. .p = { .min = 5, .max = 80 },
  277. .p1 = { .min = 1, .max = 8 },
  278. .p2 = { .dot_limit = 225000,
  279. .p2_slow = 10, .p2_fast = 5 },
  280. };
  281. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  282. .dot = { .min = 25000, .max = 350000 },
  283. .vco = { .min = 1760000, .max = 3510000 },
  284. .n = { .min = 1, .max = 3 },
  285. .m = { .min = 79, .max = 118 },
  286. .m1 = { .min = 12, .max = 22 },
  287. .m2 = { .min = 5, .max = 9 },
  288. .p = { .min = 28, .max = 112 },
  289. .p1 = { .min = 2, .max = 8 },
  290. .p2 = { .dot_limit = 225000,
  291. .p2_slow = 14, .p2_fast = 14 },
  292. };
  293. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  294. .dot = { .min = 25000, .max = 350000 },
  295. .vco = { .min = 1760000, .max = 3510000 },
  296. .n = { .min = 1, .max = 3 },
  297. .m = { .min = 79, .max = 127 },
  298. .m1 = { .min = 12, .max = 22 },
  299. .m2 = { .min = 5, .max = 9 },
  300. .p = { .min = 14, .max = 56 },
  301. .p1 = { .min = 2, .max = 8 },
  302. .p2 = { .dot_limit = 225000,
  303. .p2_slow = 7, .p2_fast = 7 },
  304. };
  305. /* LVDS 100mhz refclk limits. */
  306. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  307. .dot = { .min = 25000, .max = 350000 },
  308. .vco = { .min = 1760000, .max = 3510000 },
  309. .n = { .min = 1, .max = 2 },
  310. .m = { .min = 79, .max = 126 },
  311. .m1 = { .min = 12, .max = 22 },
  312. .m2 = { .min = 5, .max = 9 },
  313. .p = { .min = 28, .max = 112 },
  314. .p1 = { .min = 2, .max = 8 },
  315. .p2 = { .dot_limit = 225000,
  316. .p2_slow = 14, .p2_fast = 14 },
  317. };
  318. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  319. .dot = { .min = 25000, .max = 350000 },
  320. .vco = { .min = 1760000, .max = 3510000 },
  321. .n = { .min = 1, .max = 3 },
  322. .m = { .min = 79, .max = 126 },
  323. .m1 = { .min = 12, .max = 22 },
  324. .m2 = { .min = 5, .max = 9 },
  325. .p = { .min = 14, .max = 42 },
  326. .p1 = { .min = 2, .max = 6 },
  327. .p2 = { .dot_limit = 225000,
  328. .p2_slow = 7, .p2_fast = 7 },
  329. };
  330. static const intel_limit_t intel_limits_vlv = {
  331. /*
  332. * These are the data rate limits (measured in fast clocks)
  333. * since those are the strictest limits we have. The fast
  334. * clock and actual rate limits are more relaxed, so checking
  335. * them would make no difference.
  336. */
  337. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  338. .vco = { .min = 4000000, .max = 6000000 },
  339. .n = { .min = 1, .max = 7 },
  340. .m1 = { .min = 2, .max = 3 },
  341. .m2 = { .min = 11, .max = 156 },
  342. .p1 = { .min = 2, .max = 3 },
  343. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  344. };
  345. static const intel_limit_t intel_limits_chv = {
  346. /*
  347. * These are the data rate limits (measured in fast clocks)
  348. * since those are the strictest limits we have. The fast
  349. * clock and actual rate limits are more relaxed, so checking
  350. * them would make no difference.
  351. */
  352. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  353. .vco = { .min = 4860000, .max = 6700000 },
  354. .n = { .min = 1, .max = 1 },
  355. .m1 = { .min = 2, .max = 2 },
  356. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  357. .p1 = { .min = 2, .max = 4 },
  358. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  359. };
  360. static void vlv_clock(int refclk, intel_clock_t *clock)
  361. {
  362. clock->m = clock->m1 * clock->m2;
  363. clock->p = clock->p1 * clock->p2;
  364. if (WARN_ON(clock->n == 0 || clock->p == 0))
  365. return;
  366. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  367. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  368. }
  369. /**
  370. * Returns whether any output on the specified pipe is of the specified type
  371. */
  372. static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  373. {
  374. struct drm_device *dev = crtc->dev;
  375. struct intel_encoder *encoder;
  376. for_each_encoder_on_crtc(dev, crtc, encoder)
  377. if (encoder->type == type)
  378. return true;
  379. return false;
  380. }
  381. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  382. int refclk)
  383. {
  384. struct drm_device *dev = crtc->dev;
  385. const intel_limit_t *limit;
  386. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  387. if (intel_is_dual_link_lvds(dev)) {
  388. if (refclk == 100000)
  389. limit = &intel_limits_ironlake_dual_lvds_100m;
  390. else
  391. limit = &intel_limits_ironlake_dual_lvds;
  392. } else {
  393. if (refclk == 100000)
  394. limit = &intel_limits_ironlake_single_lvds_100m;
  395. else
  396. limit = &intel_limits_ironlake_single_lvds;
  397. }
  398. } else
  399. limit = &intel_limits_ironlake_dac;
  400. return limit;
  401. }
  402. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  403. {
  404. struct drm_device *dev = crtc->dev;
  405. const intel_limit_t *limit;
  406. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  407. if (intel_is_dual_link_lvds(dev))
  408. limit = &intel_limits_g4x_dual_channel_lvds;
  409. else
  410. limit = &intel_limits_g4x_single_channel_lvds;
  411. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  412. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  413. limit = &intel_limits_g4x_hdmi;
  414. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  415. limit = &intel_limits_g4x_sdvo;
  416. } else /* The option is for other outputs */
  417. limit = &intel_limits_i9xx_sdvo;
  418. return limit;
  419. }
  420. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  421. {
  422. struct drm_device *dev = crtc->dev;
  423. const intel_limit_t *limit;
  424. if (HAS_PCH_SPLIT(dev))
  425. limit = intel_ironlake_limit(crtc, refclk);
  426. else if (IS_G4X(dev)) {
  427. limit = intel_g4x_limit(crtc);
  428. } else if (IS_PINEVIEW(dev)) {
  429. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  430. limit = &intel_limits_pineview_lvds;
  431. else
  432. limit = &intel_limits_pineview_sdvo;
  433. } else if (IS_CHERRYVIEW(dev)) {
  434. limit = &intel_limits_chv;
  435. } else if (IS_VALLEYVIEW(dev)) {
  436. limit = &intel_limits_vlv;
  437. } else if (!IS_GEN2(dev)) {
  438. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  439. limit = &intel_limits_i9xx_lvds;
  440. else
  441. limit = &intel_limits_i9xx_sdvo;
  442. } else {
  443. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  444. limit = &intel_limits_i8xx_lvds;
  445. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
  446. limit = &intel_limits_i8xx_dvo;
  447. else
  448. limit = &intel_limits_i8xx_dac;
  449. }
  450. return limit;
  451. }
  452. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  453. static void pineview_clock(int refclk, intel_clock_t *clock)
  454. {
  455. clock->m = clock->m2 + 2;
  456. clock->p = clock->p1 * clock->p2;
  457. if (WARN_ON(clock->n == 0 || clock->p == 0))
  458. return;
  459. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  460. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  461. }
  462. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  463. {
  464. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  465. }
  466. static void i9xx_clock(int refclk, intel_clock_t *clock)
  467. {
  468. clock->m = i9xx_dpll_compute_m(clock);
  469. clock->p = clock->p1 * clock->p2;
  470. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  471. return;
  472. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  473. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  474. }
  475. static void chv_clock(int refclk, intel_clock_t *clock)
  476. {
  477. clock->m = clock->m1 * clock->m2;
  478. clock->p = clock->p1 * clock->p2;
  479. if (WARN_ON(clock->n == 0 || clock->p == 0))
  480. return;
  481. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  482. clock->n << 22);
  483. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  484. }
  485. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  486. /**
  487. * Returns whether the given set of divisors are valid for a given refclk with
  488. * the given connectors.
  489. */
  490. static bool intel_PLL_is_valid(struct drm_device *dev,
  491. const intel_limit_t *limit,
  492. const intel_clock_t *clock)
  493. {
  494. if (clock->n < limit->n.min || limit->n.max < clock->n)
  495. INTELPllInvalid("n out of range\n");
  496. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  497. INTELPllInvalid("p1 out of range\n");
  498. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  499. INTELPllInvalid("m2 out of range\n");
  500. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  501. INTELPllInvalid("m1 out of range\n");
  502. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  503. if (clock->m1 <= clock->m2)
  504. INTELPllInvalid("m1 <= m2\n");
  505. if (!IS_VALLEYVIEW(dev)) {
  506. if (clock->p < limit->p.min || limit->p.max < clock->p)
  507. INTELPllInvalid("p out of range\n");
  508. if (clock->m < limit->m.min || limit->m.max < clock->m)
  509. INTELPllInvalid("m out of range\n");
  510. }
  511. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  512. INTELPllInvalid("vco out of range\n");
  513. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  514. * connector, etc., rather than just a single range.
  515. */
  516. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  517. INTELPllInvalid("dot out of range\n");
  518. return true;
  519. }
  520. static bool
  521. i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  522. int target, int refclk, intel_clock_t *match_clock,
  523. intel_clock_t *best_clock)
  524. {
  525. struct drm_device *dev = crtc->dev;
  526. intel_clock_t clock;
  527. int err = target;
  528. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  529. /*
  530. * For LVDS just rely on its current settings for dual-channel.
  531. * We haven't figured out how to reliably set up different
  532. * single/dual channel state, if we even can.
  533. */
  534. if (intel_is_dual_link_lvds(dev))
  535. clock.p2 = limit->p2.p2_fast;
  536. else
  537. clock.p2 = limit->p2.p2_slow;
  538. } else {
  539. if (target < limit->p2.dot_limit)
  540. clock.p2 = limit->p2.p2_slow;
  541. else
  542. clock.p2 = limit->p2.p2_fast;
  543. }
  544. memset(best_clock, 0, sizeof(*best_clock));
  545. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  546. clock.m1++) {
  547. for (clock.m2 = limit->m2.min;
  548. clock.m2 <= limit->m2.max; clock.m2++) {
  549. if (clock.m2 >= clock.m1)
  550. break;
  551. for (clock.n = limit->n.min;
  552. clock.n <= limit->n.max; clock.n++) {
  553. for (clock.p1 = limit->p1.min;
  554. clock.p1 <= limit->p1.max; clock.p1++) {
  555. int this_err;
  556. i9xx_clock(refclk, &clock);
  557. if (!intel_PLL_is_valid(dev, limit,
  558. &clock))
  559. continue;
  560. if (match_clock &&
  561. clock.p != match_clock->p)
  562. continue;
  563. this_err = abs(clock.dot - target);
  564. if (this_err < err) {
  565. *best_clock = clock;
  566. err = this_err;
  567. }
  568. }
  569. }
  570. }
  571. }
  572. return (err != target);
  573. }
  574. static bool
  575. pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  576. int target, int refclk, intel_clock_t *match_clock,
  577. intel_clock_t *best_clock)
  578. {
  579. struct drm_device *dev = crtc->dev;
  580. intel_clock_t clock;
  581. int err = target;
  582. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  583. /*
  584. * For LVDS just rely on its current settings for dual-channel.
  585. * We haven't figured out how to reliably set up different
  586. * single/dual channel state, if we even can.
  587. */
  588. if (intel_is_dual_link_lvds(dev))
  589. clock.p2 = limit->p2.p2_fast;
  590. else
  591. clock.p2 = limit->p2.p2_slow;
  592. } else {
  593. if (target < limit->p2.dot_limit)
  594. clock.p2 = limit->p2.p2_slow;
  595. else
  596. clock.p2 = limit->p2.p2_fast;
  597. }
  598. memset(best_clock, 0, sizeof(*best_clock));
  599. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  600. clock.m1++) {
  601. for (clock.m2 = limit->m2.min;
  602. clock.m2 <= limit->m2.max; clock.m2++) {
  603. for (clock.n = limit->n.min;
  604. clock.n <= limit->n.max; clock.n++) {
  605. for (clock.p1 = limit->p1.min;
  606. clock.p1 <= limit->p1.max; clock.p1++) {
  607. int this_err;
  608. pineview_clock(refclk, &clock);
  609. if (!intel_PLL_is_valid(dev, limit,
  610. &clock))
  611. continue;
  612. if (match_clock &&
  613. clock.p != match_clock->p)
  614. continue;
  615. this_err = abs(clock.dot - target);
  616. if (this_err < err) {
  617. *best_clock = clock;
  618. err = this_err;
  619. }
  620. }
  621. }
  622. }
  623. }
  624. return (err != target);
  625. }
  626. static bool
  627. g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  628. int target, int refclk, intel_clock_t *match_clock,
  629. intel_clock_t *best_clock)
  630. {
  631. struct drm_device *dev = crtc->dev;
  632. intel_clock_t clock;
  633. int max_n;
  634. bool found;
  635. /* approximately equals target * 0.00585 */
  636. int err_most = (target >> 8) + (target >> 9);
  637. found = false;
  638. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  639. if (intel_is_dual_link_lvds(dev))
  640. clock.p2 = limit->p2.p2_fast;
  641. else
  642. clock.p2 = limit->p2.p2_slow;
  643. } else {
  644. if (target < limit->p2.dot_limit)
  645. clock.p2 = limit->p2.p2_slow;
  646. else
  647. clock.p2 = limit->p2.p2_fast;
  648. }
  649. memset(best_clock, 0, sizeof(*best_clock));
  650. max_n = limit->n.max;
  651. /* based on hardware requirement, prefer smaller n to precision */
  652. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  653. /* based on hardware requirement, prefere larger m1,m2 */
  654. for (clock.m1 = limit->m1.max;
  655. clock.m1 >= limit->m1.min; clock.m1--) {
  656. for (clock.m2 = limit->m2.max;
  657. clock.m2 >= limit->m2.min; clock.m2--) {
  658. for (clock.p1 = limit->p1.max;
  659. clock.p1 >= limit->p1.min; clock.p1--) {
  660. int this_err;
  661. i9xx_clock(refclk, &clock);
  662. if (!intel_PLL_is_valid(dev, limit,
  663. &clock))
  664. continue;
  665. this_err = abs(clock.dot - target);
  666. if (this_err < err_most) {
  667. *best_clock = clock;
  668. err_most = this_err;
  669. max_n = clock.n;
  670. found = true;
  671. }
  672. }
  673. }
  674. }
  675. }
  676. return found;
  677. }
  678. static bool
  679. vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  680. int target, int refclk, intel_clock_t *match_clock,
  681. intel_clock_t *best_clock)
  682. {
  683. struct drm_device *dev = crtc->dev;
  684. intel_clock_t clock;
  685. unsigned int bestppm = 1000000;
  686. /* min update 19.2 MHz */
  687. int max_n = min(limit->n.max, refclk / 19200);
  688. bool found = false;
  689. target *= 5; /* fast clock */
  690. memset(best_clock, 0, sizeof(*best_clock));
  691. /* based on hardware requirement, prefer smaller n to precision */
  692. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  693. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  694. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  695. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  696. clock.p = clock.p1 * clock.p2;
  697. /* based on hardware requirement, prefer bigger m1,m2 values */
  698. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  699. unsigned int ppm, diff;
  700. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  701. refclk * clock.m1);
  702. vlv_clock(refclk, &clock);
  703. if (!intel_PLL_is_valid(dev, limit,
  704. &clock))
  705. continue;
  706. diff = abs(clock.dot - target);
  707. ppm = div_u64(1000000ULL * diff, target);
  708. if (ppm < 100 && clock.p > best_clock->p) {
  709. bestppm = 0;
  710. *best_clock = clock;
  711. found = true;
  712. }
  713. if (bestppm >= 10 && ppm < bestppm - 10) {
  714. bestppm = ppm;
  715. *best_clock = clock;
  716. found = true;
  717. }
  718. }
  719. }
  720. }
  721. }
  722. return found;
  723. }
  724. static bool
  725. chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  726. int target, int refclk, intel_clock_t *match_clock,
  727. intel_clock_t *best_clock)
  728. {
  729. struct drm_device *dev = crtc->dev;
  730. intel_clock_t clock;
  731. uint64_t m2;
  732. int found = false;
  733. memset(best_clock, 0, sizeof(*best_clock));
  734. /*
  735. * Based on hardware doc, the n always set to 1, and m1 always
  736. * set to 2. If requires to support 200Mhz refclk, we need to
  737. * revisit this because n may not 1 anymore.
  738. */
  739. clock.n = 1, clock.m1 = 2;
  740. target *= 5; /* fast clock */
  741. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  742. for (clock.p2 = limit->p2.p2_fast;
  743. clock.p2 >= limit->p2.p2_slow;
  744. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  745. clock.p = clock.p1 * clock.p2;
  746. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  747. clock.n) << 22, refclk * clock.m1);
  748. if (m2 > INT_MAX/clock.m1)
  749. continue;
  750. clock.m2 = m2;
  751. chv_clock(refclk, &clock);
  752. if (!intel_PLL_is_valid(dev, limit, &clock))
  753. continue;
  754. /* based on hardware requirement, prefer bigger p
  755. */
  756. if (clock.p > best_clock->p) {
  757. *best_clock = clock;
  758. found = true;
  759. }
  760. }
  761. }
  762. return found;
  763. }
  764. bool intel_crtc_active(struct drm_crtc *crtc)
  765. {
  766. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  767. /* Be paranoid as we can arrive here with only partial
  768. * state retrieved from the hardware during setup.
  769. *
  770. * We can ditch the adjusted_mode.crtc_clock check as soon
  771. * as Haswell has gained clock readout/fastboot support.
  772. *
  773. * We can ditch the crtc->primary->fb check as soon as we can
  774. * properly reconstruct framebuffers.
  775. */
  776. return intel_crtc->active && crtc->primary->fb &&
  777. intel_crtc->config.adjusted_mode.crtc_clock;
  778. }
  779. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  780. enum pipe pipe)
  781. {
  782. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  783. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  784. return intel_crtc->config.cpu_transcoder;
  785. }
  786. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  787. {
  788. struct drm_i915_private *dev_priv = dev->dev_private;
  789. u32 reg = PIPEDSL(pipe);
  790. u32 line1, line2;
  791. u32 line_mask;
  792. if (IS_GEN2(dev))
  793. line_mask = DSL_LINEMASK_GEN2;
  794. else
  795. line_mask = DSL_LINEMASK_GEN3;
  796. line1 = I915_READ(reg) & line_mask;
  797. mdelay(5);
  798. line2 = I915_READ(reg) & line_mask;
  799. return line1 == line2;
  800. }
  801. /*
  802. * intel_wait_for_pipe_off - wait for pipe to turn off
  803. * @crtc: crtc whose pipe to wait for
  804. *
  805. * After disabling a pipe, we can't wait for vblank in the usual way,
  806. * spinning on the vblank interrupt status bit, since we won't actually
  807. * see an interrupt when the pipe is disabled.
  808. *
  809. * On Gen4 and above:
  810. * wait for the pipe register state bit to turn off
  811. *
  812. * Otherwise:
  813. * wait for the display line value to settle (it usually
  814. * ends up stopping at the start of the next frame).
  815. *
  816. */
  817. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  818. {
  819. struct drm_device *dev = crtc->base.dev;
  820. struct drm_i915_private *dev_priv = dev->dev_private;
  821. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  822. enum pipe pipe = crtc->pipe;
  823. if (INTEL_INFO(dev)->gen >= 4) {
  824. int reg = PIPECONF(cpu_transcoder);
  825. /* Wait for the Pipe State to go off */
  826. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  827. 100))
  828. WARN(1, "pipe_off wait timed out\n");
  829. } else {
  830. /* Wait for the display line to settle */
  831. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  832. WARN(1, "pipe_off wait timed out\n");
  833. }
  834. }
  835. /*
  836. * ibx_digital_port_connected - is the specified port connected?
  837. * @dev_priv: i915 private structure
  838. * @port: the port to test
  839. *
  840. * Returns true if @port is connected, false otherwise.
  841. */
  842. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  843. struct intel_digital_port *port)
  844. {
  845. u32 bit;
  846. if (HAS_PCH_IBX(dev_priv->dev)) {
  847. switch (port->port) {
  848. case PORT_B:
  849. bit = SDE_PORTB_HOTPLUG;
  850. break;
  851. case PORT_C:
  852. bit = SDE_PORTC_HOTPLUG;
  853. break;
  854. case PORT_D:
  855. bit = SDE_PORTD_HOTPLUG;
  856. break;
  857. default:
  858. return true;
  859. }
  860. } else {
  861. switch (port->port) {
  862. case PORT_B:
  863. bit = SDE_PORTB_HOTPLUG_CPT;
  864. break;
  865. case PORT_C:
  866. bit = SDE_PORTC_HOTPLUG_CPT;
  867. break;
  868. case PORT_D:
  869. bit = SDE_PORTD_HOTPLUG_CPT;
  870. break;
  871. default:
  872. return true;
  873. }
  874. }
  875. return I915_READ(SDEISR) & bit;
  876. }
  877. static const char *state_string(bool enabled)
  878. {
  879. return enabled ? "on" : "off";
  880. }
  881. /* Only for pre-ILK configs */
  882. void assert_pll(struct drm_i915_private *dev_priv,
  883. enum pipe pipe, bool state)
  884. {
  885. int reg;
  886. u32 val;
  887. bool cur_state;
  888. reg = DPLL(pipe);
  889. val = I915_READ(reg);
  890. cur_state = !!(val & DPLL_VCO_ENABLE);
  891. WARN(cur_state != state,
  892. "PLL state assertion failure (expected %s, current %s)\n",
  893. state_string(state), state_string(cur_state));
  894. }
  895. /* XXX: the dsi pll is shared between MIPI DSI ports */
  896. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  897. {
  898. u32 val;
  899. bool cur_state;
  900. mutex_lock(&dev_priv->dpio_lock);
  901. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  902. mutex_unlock(&dev_priv->dpio_lock);
  903. cur_state = val & DSI_PLL_VCO_EN;
  904. WARN(cur_state != state,
  905. "DSI PLL state assertion failure (expected %s, current %s)\n",
  906. state_string(state), state_string(cur_state));
  907. }
  908. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  909. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  910. struct intel_shared_dpll *
  911. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  912. {
  913. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  914. if (crtc->config.shared_dpll < 0)
  915. return NULL;
  916. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  917. }
  918. /* For ILK+ */
  919. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  920. struct intel_shared_dpll *pll,
  921. bool state)
  922. {
  923. bool cur_state;
  924. struct intel_dpll_hw_state hw_state;
  925. if (WARN (!pll,
  926. "asserting DPLL %s with no DPLL\n", state_string(state)))
  927. return;
  928. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  929. WARN(cur_state != state,
  930. "%s assertion failure (expected %s, current %s)\n",
  931. pll->name, state_string(state), state_string(cur_state));
  932. }
  933. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  934. enum pipe pipe, bool state)
  935. {
  936. int reg;
  937. u32 val;
  938. bool cur_state;
  939. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  940. pipe);
  941. if (HAS_DDI(dev_priv->dev)) {
  942. /* DDI does not have a specific FDI_TX register */
  943. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  944. val = I915_READ(reg);
  945. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  946. } else {
  947. reg = FDI_TX_CTL(pipe);
  948. val = I915_READ(reg);
  949. cur_state = !!(val & FDI_TX_ENABLE);
  950. }
  951. WARN(cur_state != state,
  952. "FDI TX state assertion failure (expected %s, current %s)\n",
  953. state_string(state), state_string(cur_state));
  954. }
  955. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  956. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  957. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  958. enum pipe pipe, bool state)
  959. {
  960. int reg;
  961. u32 val;
  962. bool cur_state;
  963. reg = FDI_RX_CTL(pipe);
  964. val = I915_READ(reg);
  965. cur_state = !!(val & FDI_RX_ENABLE);
  966. WARN(cur_state != state,
  967. "FDI RX state assertion failure (expected %s, current %s)\n",
  968. state_string(state), state_string(cur_state));
  969. }
  970. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  971. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  972. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  973. enum pipe pipe)
  974. {
  975. int reg;
  976. u32 val;
  977. /* ILK FDI PLL is always enabled */
  978. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  979. return;
  980. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  981. if (HAS_DDI(dev_priv->dev))
  982. return;
  983. reg = FDI_TX_CTL(pipe);
  984. val = I915_READ(reg);
  985. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  986. }
  987. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  988. enum pipe pipe, bool state)
  989. {
  990. int reg;
  991. u32 val;
  992. bool cur_state;
  993. reg = FDI_RX_CTL(pipe);
  994. val = I915_READ(reg);
  995. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  996. WARN(cur_state != state,
  997. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  998. state_string(state), state_string(cur_state));
  999. }
  1000. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1001. enum pipe pipe)
  1002. {
  1003. struct drm_device *dev = dev_priv->dev;
  1004. int pp_reg;
  1005. u32 val;
  1006. enum pipe panel_pipe = PIPE_A;
  1007. bool locked = true;
  1008. if (WARN_ON(HAS_DDI(dev)))
  1009. return;
  1010. if (HAS_PCH_SPLIT(dev)) {
  1011. u32 port_sel;
  1012. pp_reg = PCH_PP_CONTROL;
  1013. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1014. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1015. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1016. panel_pipe = PIPE_B;
  1017. /* XXX: else fix for eDP */
  1018. } else if (IS_VALLEYVIEW(dev)) {
  1019. /* presumably write lock depends on pipe, not port select */
  1020. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1021. panel_pipe = pipe;
  1022. } else {
  1023. pp_reg = PP_CONTROL;
  1024. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1025. panel_pipe = PIPE_B;
  1026. }
  1027. val = I915_READ(pp_reg);
  1028. if (!(val & PANEL_POWER_ON) ||
  1029. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1030. locked = false;
  1031. WARN(panel_pipe == pipe && locked,
  1032. "panel assertion failure, pipe %c regs locked\n",
  1033. pipe_name(pipe));
  1034. }
  1035. static void assert_cursor(struct drm_i915_private *dev_priv,
  1036. enum pipe pipe, bool state)
  1037. {
  1038. struct drm_device *dev = dev_priv->dev;
  1039. bool cur_state;
  1040. if (IS_845G(dev) || IS_I865G(dev))
  1041. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1042. else
  1043. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1044. WARN(cur_state != state,
  1045. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1046. pipe_name(pipe), state_string(state), state_string(cur_state));
  1047. }
  1048. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1049. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1050. void assert_pipe(struct drm_i915_private *dev_priv,
  1051. enum pipe pipe, bool state)
  1052. {
  1053. int reg;
  1054. u32 val;
  1055. bool cur_state;
  1056. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1057. pipe);
  1058. /* if we need the pipe quirk it must be always on */
  1059. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1060. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1061. state = true;
  1062. if (!intel_display_power_is_enabled(dev_priv,
  1063. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1064. cur_state = false;
  1065. } else {
  1066. reg = PIPECONF(cpu_transcoder);
  1067. val = I915_READ(reg);
  1068. cur_state = !!(val & PIPECONF_ENABLE);
  1069. }
  1070. WARN(cur_state != state,
  1071. "pipe %c assertion failure (expected %s, current %s)\n",
  1072. pipe_name(pipe), state_string(state), state_string(cur_state));
  1073. }
  1074. static void assert_plane(struct drm_i915_private *dev_priv,
  1075. enum plane plane, bool state)
  1076. {
  1077. int reg;
  1078. u32 val;
  1079. bool cur_state;
  1080. reg = DSPCNTR(plane);
  1081. val = I915_READ(reg);
  1082. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1083. WARN(cur_state != state,
  1084. "plane %c assertion failure (expected %s, current %s)\n",
  1085. plane_name(plane), state_string(state), state_string(cur_state));
  1086. }
  1087. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1088. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1089. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1090. enum pipe pipe)
  1091. {
  1092. struct drm_device *dev = dev_priv->dev;
  1093. int reg, i;
  1094. u32 val;
  1095. int cur_pipe;
  1096. /* Primary planes are fixed to pipes on gen4+ */
  1097. if (INTEL_INFO(dev)->gen >= 4) {
  1098. reg = DSPCNTR(pipe);
  1099. val = I915_READ(reg);
  1100. WARN(val & DISPLAY_PLANE_ENABLE,
  1101. "plane %c assertion failure, should be disabled but not\n",
  1102. plane_name(pipe));
  1103. return;
  1104. }
  1105. /* Need to check both planes against the pipe */
  1106. for_each_pipe(dev_priv, i) {
  1107. reg = DSPCNTR(i);
  1108. val = I915_READ(reg);
  1109. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1110. DISPPLANE_SEL_PIPE_SHIFT;
  1111. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1112. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1113. plane_name(i), pipe_name(pipe));
  1114. }
  1115. }
  1116. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1117. enum pipe pipe)
  1118. {
  1119. struct drm_device *dev = dev_priv->dev;
  1120. int reg, sprite;
  1121. u32 val;
  1122. if (INTEL_INFO(dev)->gen >= 9) {
  1123. for_each_sprite(pipe, sprite) {
  1124. val = I915_READ(PLANE_CTL(pipe, sprite));
  1125. WARN(val & PLANE_CTL_ENABLE,
  1126. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1127. sprite, pipe_name(pipe));
  1128. }
  1129. } else if (IS_VALLEYVIEW(dev)) {
  1130. for_each_sprite(pipe, sprite) {
  1131. reg = SPCNTR(pipe, sprite);
  1132. val = I915_READ(reg);
  1133. WARN(val & SP_ENABLE,
  1134. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1135. sprite_name(pipe, sprite), pipe_name(pipe));
  1136. }
  1137. } else if (INTEL_INFO(dev)->gen >= 7) {
  1138. reg = SPRCTL(pipe);
  1139. val = I915_READ(reg);
  1140. WARN(val & SPRITE_ENABLE,
  1141. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1142. plane_name(pipe), pipe_name(pipe));
  1143. } else if (INTEL_INFO(dev)->gen >= 5) {
  1144. reg = DVSCNTR(pipe);
  1145. val = I915_READ(reg);
  1146. WARN(val & DVS_ENABLE,
  1147. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1148. plane_name(pipe), pipe_name(pipe));
  1149. }
  1150. }
  1151. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1152. {
  1153. if (WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1154. drm_crtc_vblank_put(crtc);
  1155. }
  1156. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1157. {
  1158. u32 val;
  1159. bool enabled;
  1160. WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1161. val = I915_READ(PCH_DREF_CONTROL);
  1162. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1163. DREF_SUPERSPREAD_SOURCE_MASK));
  1164. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1165. }
  1166. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1167. enum pipe pipe)
  1168. {
  1169. int reg;
  1170. u32 val;
  1171. bool enabled;
  1172. reg = PCH_TRANSCONF(pipe);
  1173. val = I915_READ(reg);
  1174. enabled = !!(val & TRANS_ENABLE);
  1175. WARN(enabled,
  1176. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1177. pipe_name(pipe));
  1178. }
  1179. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1180. enum pipe pipe, u32 port_sel, u32 val)
  1181. {
  1182. if ((val & DP_PORT_EN) == 0)
  1183. return false;
  1184. if (HAS_PCH_CPT(dev_priv->dev)) {
  1185. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1186. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1187. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1188. return false;
  1189. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1190. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1191. return false;
  1192. } else {
  1193. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1194. return false;
  1195. }
  1196. return true;
  1197. }
  1198. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1199. enum pipe pipe, u32 val)
  1200. {
  1201. if ((val & SDVO_ENABLE) == 0)
  1202. return false;
  1203. if (HAS_PCH_CPT(dev_priv->dev)) {
  1204. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1205. return false;
  1206. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1207. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1208. return false;
  1209. } else {
  1210. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1211. return false;
  1212. }
  1213. return true;
  1214. }
  1215. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1216. enum pipe pipe, u32 val)
  1217. {
  1218. if ((val & LVDS_PORT_EN) == 0)
  1219. return false;
  1220. if (HAS_PCH_CPT(dev_priv->dev)) {
  1221. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1222. return false;
  1223. } else {
  1224. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1225. return false;
  1226. }
  1227. return true;
  1228. }
  1229. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1230. enum pipe pipe, u32 val)
  1231. {
  1232. if ((val & ADPA_DAC_ENABLE) == 0)
  1233. return false;
  1234. if (HAS_PCH_CPT(dev_priv->dev)) {
  1235. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1236. return false;
  1237. } else {
  1238. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1239. return false;
  1240. }
  1241. return true;
  1242. }
  1243. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1244. enum pipe pipe, int reg, u32 port_sel)
  1245. {
  1246. u32 val = I915_READ(reg);
  1247. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1248. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1249. reg, pipe_name(pipe));
  1250. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1251. && (val & DP_PIPEB_SELECT),
  1252. "IBX PCH dp port still using transcoder B\n");
  1253. }
  1254. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1255. enum pipe pipe, int reg)
  1256. {
  1257. u32 val = I915_READ(reg);
  1258. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1259. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1260. reg, pipe_name(pipe));
  1261. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1262. && (val & SDVO_PIPE_B_SELECT),
  1263. "IBX PCH hdmi port still using transcoder B\n");
  1264. }
  1265. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1266. enum pipe pipe)
  1267. {
  1268. int reg;
  1269. u32 val;
  1270. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1271. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1272. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1273. reg = PCH_ADPA;
  1274. val = I915_READ(reg);
  1275. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1276. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1277. pipe_name(pipe));
  1278. reg = PCH_LVDS;
  1279. val = I915_READ(reg);
  1280. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1281. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1282. pipe_name(pipe));
  1283. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1284. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1285. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1286. }
  1287. static void intel_init_dpio(struct drm_device *dev)
  1288. {
  1289. struct drm_i915_private *dev_priv = dev->dev_private;
  1290. if (!IS_VALLEYVIEW(dev))
  1291. return;
  1292. /*
  1293. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1294. * CHV x1 PHY (DP/HDMI D)
  1295. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1296. */
  1297. if (IS_CHERRYVIEW(dev)) {
  1298. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1299. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1300. } else {
  1301. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1302. }
  1303. }
  1304. static void vlv_enable_pll(struct intel_crtc *crtc)
  1305. {
  1306. struct drm_device *dev = crtc->base.dev;
  1307. struct drm_i915_private *dev_priv = dev->dev_private;
  1308. int reg = DPLL(crtc->pipe);
  1309. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1310. assert_pipe_disabled(dev_priv, crtc->pipe);
  1311. /* No really, not for ILK+ */
  1312. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1313. /* PLL is protected by panel, make sure we can write it */
  1314. if (IS_MOBILE(dev_priv->dev))
  1315. assert_panel_unlocked(dev_priv, crtc->pipe);
  1316. I915_WRITE(reg, dpll);
  1317. POSTING_READ(reg);
  1318. udelay(150);
  1319. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1320. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1321. I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
  1322. POSTING_READ(DPLL_MD(crtc->pipe));
  1323. /* We do this three times for luck */
  1324. I915_WRITE(reg, dpll);
  1325. POSTING_READ(reg);
  1326. udelay(150); /* wait for warmup */
  1327. I915_WRITE(reg, dpll);
  1328. POSTING_READ(reg);
  1329. udelay(150); /* wait for warmup */
  1330. I915_WRITE(reg, dpll);
  1331. POSTING_READ(reg);
  1332. udelay(150); /* wait for warmup */
  1333. }
  1334. static void chv_enable_pll(struct intel_crtc *crtc)
  1335. {
  1336. struct drm_device *dev = crtc->base.dev;
  1337. struct drm_i915_private *dev_priv = dev->dev_private;
  1338. int pipe = crtc->pipe;
  1339. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1340. u32 tmp;
  1341. assert_pipe_disabled(dev_priv, crtc->pipe);
  1342. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1343. mutex_lock(&dev_priv->dpio_lock);
  1344. /* Enable back the 10bit clock to display controller */
  1345. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1346. tmp |= DPIO_DCLKP_EN;
  1347. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1348. /*
  1349. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1350. */
  1351. udelay(1);
  1352. /* Enable PLL */
  1353. I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
  1354. /* Check PLL is locked */
  1355. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1356. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1357. /* not sure when this should be written */
  1358. I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
  1359. POSTING_READ(DPLL_MD(pipe));
  1360. mutex_unlock(&dev_priv->dpio_lock);
  1361. }
  1362. static int intel_num_dvo_pipes(struct drm_device *dev)
  1363. {
  1364. struct intel_crtc *crtc;
  1365. int count = 0;
  1366. for_each_intel_crtc(dev, crtc)
  1367. count += crtc->active &&
  1368. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO);
  1369. return count;
  1370. }
  1371. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1372. {
  1373. struct drm_device *dev = crtc->base.dev;
  1374. struct drm_i915_private *dev_priv = dev->dev_private;
  1375. int reg = DPLL(crtc->pipe);
  1376. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1377. assert_pipe_disabled(dev_priv, crtc->pipe);
  1378. /* No really, not for ILK+ */
  1379. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1380. /* PLL is protected by panel, make sure we can write it */
  1381. if (IS_MOBILE(dev) && !IS_I830(dev))
  1382. assert_panel_unlocked(dev_priv, crtc->pipe);
  1383. /* Enable DVO 2x clock on both PLLs if necessary */
  1384. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1385. /*
  1386. * It appears to be important that we don't enable this
  1387. * for the current pipe before otherwise configuring the
  1388. * PLL. No idea how this should be handled if multiple
  1389. * DVO outputs are enabled simultaneosly.
  1390. */
  1391. dpll |= DPLL_DVO_2X_MODE;
  1392. I915_WRITE(DPLL(!crtc->pipe),
  1393. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1394. }
  1395. /* Wait for the clocks to stabilize. */
  1396. POSTING_READ(reg);
  1397. udelay(150);
  1398. if (INTEL_INFO(dev)->gen >= 4) {
  1399. I915_WRITE(DPLL_MD(crtc->pipe),
  1400. crtc->config.dpll_hw_state.dpll_md);
  1401. } else {
  1402. /* The pixel multiplier can only be updated once the
  1403. * DPLL is enabled and the clocks are stable.
  1404. *
  1405. * So write it again.
  1406. */
  1407. I915_WRITE(reg, dpll);
  1408. }
  1409. /* We do this three times for luck */
  1410. I915_WRITE(reg, dpll);
  1411. POSTING_READ(reg);
  1412. udelay(150); /* wait for warmup */
  1413. I915_WRITE(reg, dpll);
  1414. POSTING_READ(reg);
  1415. udelay(150); /* wait for warmup */
  1416. I915_WRITE(reg, dpll);
  1417. POSTING_READ(reg);
  1418. udelay(150); /* wait for warmup */
  1419. }
  1420. /**
  1421. * i9xx_disable_pll - disable a PLL
  1422. * @dev_priv: i915 private structure
  1423. * @pipe: pipe PLL to disable
  1424. *
  1425. * Disable the PLL for @pipe, making sure the pipe is off first.
  1426. *
  1427. * Note! This is for pre-ILK only.
  1428. */
  1429. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1430. {
  1431. struct drm_device *dev = crtc->base.dev;
  1432. struct drm_i915_private *dev_priv = dev->dev_private;
  1433. enum pipe pipe = crtc->pipe;
  1434. /* Disable DVO 2x clock on both PLLs if necessary */
  1435. if (IS_I830(dev) &&
  1436. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO) &&
  1437. intel_num_dvo_pipes(dev) == 1) {
  1438. I915_WRITE(DPLL(PIPE_B),
  1439. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1440. I915_WRITE(DPLL(PIPE_A),
  1441. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1442. }
  1443. /* Don't disable pipe or pipe PLLs if needed */
  1444. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1445. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1446. return;
  1447. /* Make sure the pipe isn't still relying on us */
  1448. assert_pipe_disabled(dev_priv, pipe);
  1449. I915_WRITE(DPLL(pipe), 0);
  1450. POSTING_READ(DPLL(pipe));
  1451. }
  1452. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1453. {
  1454. u32 val = 0;
  1455. /* Make sure the pipe isn't still relying on us */
  1456. assert_pipe_disabled(dev_priv, pipe);
  1457. /*
  1458. * Leave integrated clock source and reference clock enabled for pipe B.
  1459. * The latter is needed for VGA hotplug / manual detection.
  1460. */
  1461. if (pipe == PIPE_B)
  1462. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1463. I915_WRITE(DPLL(pipe), val);
  1464. POSTING_READ(DPLL(pipe));
  1465. }
  1466. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1467. {
  1468. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1469. u32 val;
  1470. /* Make sure the pipe isn't still relying on us */
  1471. assert_pipe_disabled(dev_priv, pipe);
  1472. /* Set PLL en = 0 */
  1473. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1474. if (pipe != PIPE_A)
  1475. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1476. I915_WRITE(DPLL(pipe), val);
  1477. POSTING_READ(DPLL(pipe));
  1478. mutex_lock(&dev_priv->dpio_lock);
  1479. /* Disable 10bit clock to display controller */
  1480. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1481. val &= ~DPIO_DCLKP_EN;
  1482. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1483. /* disable left/right clock distribution */
  1484. if (pipe != PIPE_B) {
  1485. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1486. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1487. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1488. } else {
  1489. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1490. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1491. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1492. }
  1493. mutex_unlock(&dev_priv->dpio_lock);
  1494. }
  1495. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1496. struct intel_digital_port *dport)
  1497. {
  1498. u32 port_mask;
  1499. int dpll_reg;
  1500. switch (dport->port) {
  1501. case PORT_B:
  1502. port_mask = DPLL_PORTB_READY_MASK;
  1503. dpll_reg = DPLL(0);
  1504. break;
  1505. case PORT_C:
  1506. port_mask = DPLL_PORTC_READY_MASK;
  1507. dpll_reg = DPLL(0);
  1508. break;
  1509. case PORT_D:
  1510. port_mask = DPLL_PORTD_READY_MASK;
  1511. dpll_reg = DPIO_PHY_STATUS;
  1512. break;
  1513. default:
  1514. BUG();
  1515. }
  1516. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1517. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1518. port_name(dport->port), I915_READ(dpll_reg));
  1519. }
  1520. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1521. {
  1522. struct drm_device *dev = crtc->base.dev;
  1523. struct drm_i915_private *dev_priv = dev->dev_private;
  1524. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1525. if (WARN_ON(pll == NULL))
  1526. return;
  1527. WARN_ON(!pll->refcount);
  1528. if (pll->active == 0) {
  1529. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1530. WARN_ON(pll->on);
  1531. assert_shared_dpll_disabled(dev_priv, pll);
  1532. pll->mode_set(dev_priv, pll);
  1533. }
  1534. }
  1535. /**
  1536. * intel_enable_shared_dpll - enable PCH PLL
  1537. * @dev_priv: i915 private structure
  1538. * @pipe: pipe PLL to enable
  1539. *
  1540. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1541. * drives the transcoder clock.
  1542. */
  1543. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1544. {
  1545. struct drm_device *dev = crtc->base.dev;
  1546. struct drm_i915_private *dev_priv = dev->dev_private;
  1547. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1548. if (WARN_ON(pll == NULL))
  1549. return;
  1550. if (WARN_ON(pll->refcount == 0))
  1551. return;
  1552. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1553. pll->name, pll->active, pll->on,
  1554. crtc->base.base.id);
  1555. if (pll->active++) {
  1556. WARN_ON(!pll->on);
  1557. assert_shared_dpll_enabled(dev_priv, pll);
  1558. return;
  1559. }
  1560. WARN_ON(pll->on);
  1561. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1562. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1563. pll->enable(dev_priv, pll);
  1564. pll->on = true;
  1565. }
  1566. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1567. {
  1568. struct drm_device *dev = crtc->base.dev;
  1569. struct drm_i915_private *dev_priv = dev->dev_private;
  1570. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1571. /* PCH only available on ILK+ */
  1572. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1573. if (WARN_ON(pll == NULL))
  1574. return;
  1575. if (WARN_ON(pll->refcount == 0))
  1576. return;
  1577. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1578. pll->name, pll->active, pll->on,
  1579. crtc->base.base.id);
  1580. if (WARN_ON(pll->active == 0)) {
  1581. assert_shared_dpll_disabled(dev_priv, pll);
  1582. return;
  1583. }
  1584. assert_shared_dpll_enabled(dev_priv, pll);
  1585. WARN_ON(!pll->on);
  1586. if (--pll->active)
  1587. return;
  1588. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1589. pll->disable(dev_priv, pll);
  1590. pll->on = false;
  1591. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1592. }
  1593. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1594. enum pipe pipe)
  1595. {
  1596. struct drm_device *dev = dev_priv->dev;
  1597. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1598. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1599. uint32_t reg, val, pipeconf_val;
  1600. /* PCH only available on ILK+ */
  1601. BUG_ON(!HAS_PCH_SPLIT(dev));
  1602. /* Make sure PCH DPLL is enabled */
  1603. assert_shared_dpll_enabled(dev_priv,
  1604. intel_crtc_to_shared_dpll(intel_crtc));
  1605. /* FDI must be feeding us bits for PCH ports */
  1606. assert_fdi_tx_enabled(dev_priv, pipe);
  1607. assert_fdi_rx_enabled(dev_priv, pipe);
  1608. if (HAS_PCH_CPT(dev)) {
  1609. /* Workaround: Set the timing override bit before enabling the
  1610. * pch transcoder. */
  1611. reg = TRANS_CHICKEN2(pipe);
  1612. val = I915_READ(reg);
  1613. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1614. I915_WRITE(reg, val);
  1615. }
  1616. reg = PCH_TRANSCONF(pipe);
  1617. val = I915_READ(reg);
  1618. pipeconf_val = I915_READ(PIPECONF(pipe));
  1619. if (HAS_PCH_IBX(dev_priv->dev)) {
  1620. /*
  1621. * make the BPC in transcoder be consistent with
  1622. * that in pipeconf reg.
  1623. */
  1624. val &= ~PIPECONF_BPC_MASK;
  1625. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1626. }
  1627. val &= ~TRANS_INTERLACE_MASK;
  1628. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1629. if (HAS_PCH_IBX(dev_priv->dev) &&
  1630. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1631. val |= TRANS_LEGACY_INTERLACED_ILK;
  1632. else
  1633. val |= TRANS_INTERLACED;
  1634. else
  1635. val |= TRANS_PROGRESSIVE;
  1636. I915_WRITE(reg, val | TRANS_ENABLE);
  1637. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1638. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1639. }
  1640. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1641. enum transcoder cpu_transcoder)
  1642. {
  1643. u32 val, pipeconf_val;
  1644. /* PCH only available on ILK+ */
  1645. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1646. /* FDI must be feeding us bits for PCH ports */
  1647. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1648. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1649. /* Workaround: set timing override bit. */
  1650. val = I915_READ(_TRANSA_CHICKEN2);
  1651. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1652. I915_WRITE(_TRANSA_CHICKEN2, val);
  1653. val = TRANS_ENABLE;
  1654. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1655. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1656. PIPECONF_INTERLACED_ILK)
  1657. val |= TRANS_INTERLACED;
  1658. else
  1659. val |= TRANS_PROGRESSIVE;
  1660. I915_WRITE(LPT_TRANSCONF, val);
  1661. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1662. DRM_ERROR("Failed to enable PCH transcoder\n");
  1663. }
  1664. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1665. enum pipe pipe)
  1666. {
  1667. struct drm_device *dev = dev_priv->dev;
  1668. uint32_t reg, val;
  1669. /* FDI relies on the transcoder */
  1670. assert_fdi_tx_disabled(dev_priv, pipe);
  1671. assert_fdi_rx_disabled(dev_priv, pipe);
  1672. /* Ports must be off as well */
  1673. assert_pch_ports_disabled(dev_priv, pipe);
  1674. reg = PCH_TRANSCONF(pipe);
  1675. val = I915_READ(reg);
  1676. val &= ~TRANS_ENABLE;
  1677. I915_WRITE(reg, val);
  1678. /* wait for PCH transcoder off, transcoder state */
  1679. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1680. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1681. if (!HAS_PCH_IBX(dev)) {
  1682. /* Workaround: Clear the timing override chicken bit again. */
  1683. reg = TRANS_CHICKEN2(pipe);
  1684. val = I915_READ(reg);
  1685. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1686. I915_WRITE(reg, val);
  1687. }
  1688. }
  1689. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1690. {
  1691. u32 val;
  1692. val = I915_READ(LPT_TRANSCONF);
  1693. val &= ~TRANS_ENABLE;
  1694. I915_WRITE(LPT_TRANSCONF, val);
  1695. /* wait for PCH transcoder off, transcoder state */
  1696. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1697. DRM_ERROR("Failed to disable PCH transcoder\n");
  1698. /* Workaround: clear timing override bit. */
  1699. val = I915_READ(_TRANSA_CHICKEN2);
  1700. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1701. I915_WRITE(_TRANSA_CHICKEN2, val);
  1702. }
  1703. /**
  1704. * intel_enable_pipe - enable a pipe, asserting requirements
  1705. * @crtc: crtc responsible for the pipe
  1706. *
  1707. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1708. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1709. */
  1710. static void intel_enable_pipe(struct intel_crtc *crtc)
  1711. {
  1712. struct drm_device *dev = crtc->base.dev;
  1713. struct drm_i915_private *dev_priv = dev->dev_private;
  1714. enum pipe pipe = crtc->pipe;
  1715. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1716. pipe);
  1717. enum pipe pch_transcoder;
  1718. int reg;
  1719. u32 val;
  1720. assert_planes_disabled(dev_priv, pipe);
  1721. assert_cursor_disabled(dev_priv, pipe);
  1722. assert_sprites_disabled(dev_priv, pipe);
  1723. if (HAS_PCH_LPT(dev_priv->dev))
  1724. pch_transcoder = TRANSCODER_A;
  1725. else
  1726. pch_transcoder = pipe;
  1727. /*
  1728. * A pipe without a PLL won't actually be able to drive bits from
  1729. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1730. * need the check.
  1731. */
  1732. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1733. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
  1734. assert_dsi_pll_enabled(dev_priv);
  1735. else
  1736. assert_pll_enabled(dev_priv, pipe);
  1737. else {
  1738. if (crtc->config.has_pch_encoder) {
  1739. /* if driving the PCH, we need FDI enabled */
  1740. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1741. assert_fdi_tx_pll_enabled(dev_priv,
  1742. (enum pipe) cpu_transcoder);
  1743. }
  1744. /* FIXME: assert CPU port conditions for SNB+ */
  1745. }
  1746. reg = PIPECONF(cpu_transcoder);
  1747. val = I915_READ(reg);
  1748. if (val & PIPECONF_ENABLE) {
  1749. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1750. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1751. return;
  1752. }
  1753. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1754. POSTING_READ(reg);
  1755. }
  1756. /**
  1757. * intel_disable_pipe - disable a pipe, asserting requirements
  1758. * @crtc: crtc whose pipes is to be disabled
  1759. *
  1760. * Disable the pipe of @crtc, making sure that various hardware
  1761. * specific requirements are met, if applicable, e.g. plane
  1762. * disabled, panel fitter off, etc.
  1763. *
  1764. * Will wait until the pipe has shut down before returning.
  1765. */
  1766. static void intel_disable_pipe(struct intel_crtc *crtc)
  1767. {
  1768. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1769. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  1770. enum pipe pipe = crtc->pipe;
  1771. int reg;
  1772. u32 val;
  1773. /*
  1774. * Make sure planes won't keep trying to pump pixels to us,
  1775. * or we might hang the display.
  1776. */
  1777. assert_planes_disabled(dev_priv, pipe);
  1778. assert_cursor_disabled(dev_priv, pipe);
  1779. assert_sprites_disabled(dev_priv, pipe);
  1780. reg = PIPECONF(cpu_transcoder);
  1781. val = I915_READ(reg);
  1782. if ((val & PIPECONF_ENABLE) == 0)
  1783. return;
  1784. /*
  1785. * Double wide has implications for planes
  1786. * so best keep it disabled when not needed.
  1787. */
  1788. if (crtc->config.double_wide)
  1789. val &= ~PIPECONF_DOUBLE_WIDE;
  1790. /* Don't disable pipe or pipe PLLs if needed */
  1791. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1792. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1793. val &= ~PIPECONF_ENABLE;
  1794. I915_WRITE(reg, val);
  1795. if ((val & PIPECONF_ENABLE) == 0)
  1796. intel_wait_for_pipe_off(crtc);
  1797. }
  1798. /*
  1799. * Plane regs are double buffered, going from enabled->disabled needs a
  1800. * trigger in order to latch. The display address reg provides this.
  1801. */
  1802. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1803. enum plane plane)
  1804. {
  1805. struct drm_device *dev = dev_priv->dev;
  1806. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1807. I915_WRITE(reg, I915_READ(reg));
  1808. POSTING_READ(reg);
  1809. }
  1810. /**
  1811. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1812. * @plane: plane to be enabled
  1813. * @crtc: crtc for the plane
  1814. *
  1815. * Enable @plane on @crtc, making sure that the pipe is running first.
  1816. */
  1817. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1818. struct drm_crtc *crtc)
  1819. {
  1820. struct drm_device *dev = plane->dev;
  1821. struct drm_i915_private *dev_priv = dev->dev_private;
  1822. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1823. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1824. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1825. if (intel_crtc->primary_enabled)
  1826. return;
  1827. intel_crtc->primary_enabled = true;
  1828. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1829. crtc->x, crtc->y);
  1830. /*
  1831. * BDW signals flip done immediately if the plane
  1832. * is disabled, even if the plane enable is already
  1833. * armed to occur at the next vblank :(
  1834. */
  1835. if (IS_BROADWELL(dev))
  1836. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1837. }
  1838. /**
  1839. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1840. * @plane: plane to be disabled
  1841. * @crtc: crtc for the plane
  1842. *
  1843. * Disable @plane on @crtc, making sure that the pipe is running first.
  1844. */
  1845. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1846. struct drm_crtc *crtc)
  1847. {
  1848. struct drm_device *dev = plane->dev;
  1849. struct drm_i915_private *dev_priv = dev->dev_private;
  1850. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1851. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1852. if (!intel_crtc->primary_enabled)
  1853. return;
  1854. intel_crtc->primary_enabled = false;
  1855. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1856. crtc->x, crtc->y);
  1857. }
  1858. static bool need_vtd_wa(struct drm_device *dev)
  1859. {
  1860. #ifdef CONFIG_INTEL_IOMMU
  1861. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1862. return true;
  1863. #endif
  1864. return false;
  1865. }
  1866. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1867. {
  1868. int tile_height;
  1869. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1870. return ALIGN(height, tile_height);
  1871. }
  1872. int
  1873. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1874. struct drm_i915_gem_object *obj,
  1875. struct intel_engine_cs *pipelined)
  1876. {
  1877. struct drm_i915_private *dev_priv = dev->dev_private;
  1878. u32 alignment;
  1879. int ret;
  1880. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1881. switch (obj->tiling_mode) {
  1882. case I915_TILING_NONE:
  1883. if (INTEL_INFO(dev)->gen >= 9)
  1884. alignment = 256 * 1024;
  1885. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1886. alignment = 128 * 1024;
  1887. else if (INTEL_INFO(dev)->gen >= 4)
  1888. alignment = 4 * 1024;
  1889. else
  1890. alignment = 64 * 1024;
  1891. break;
  1892. case I915_TILING_X:
  1893. if (INTEL_INFO(dev)->gen >= 9)
  1894. alignment = 256 * 1024;
  1895. else {
  1896. /* pin() will align the object as required by fence */
  1897. alignment = 0;
  1898. }
  1899. break;
  1900. case I915_TILING_Y:
  1901. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1902. return -EINVAL;
  1903. default:
  1904. BUG();
  1905. }
  1906. /* Note that the w/a also requires 64 PTE of padding following the
  1907. * bo. We currently fill all unused PTE with the shadow page and so
  1908. * we should always have valid PTE following the scanout preventing
  1909. * the VT-d warning.
  1910. */
  1911. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1912. alignment = 256 * 1024;
  1913. /*
  1914. * Global gtt pte registers are special registers which actually forward
  1915. * writes to a chunk of system memory. Which means that there is no risk
  1916. * that the register values disappear as soon as we call
  1917. * intel_runtime_pm_put(), so it is correct to wrap only the
  1918. * pin/unpin/fence and not more.
  1919. */
  1920. intel_runtime_pm_get(dev_priv);
  1921. dev_priv->mm.interruptible = false;
  1922. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1923. if (ret)
  1924. goto err_interruptible;
  1925. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1926. * fence, whereas 965+ only requires a fence if using
  1927. * framebuffer compression. For simplicity, we always install
  1928. * a fence as the cost is not that onerous.
  1929. */
  1930. ret = i915_gem_object_get_fence(obj);
  1931. if (ret)
  1932. goto err_unpin;
  1933. i915_gem_object_pin_fence(obj);
  1934. dev_priv->mm.interruptible = true;
  1935. intel_runtime_pm_put(dev_priv);
  1936. return 0;
  1937. err_unpin:
  1938. i915_gem_object_unpin_from_display_plane(obj);
  1939. err_interruptible:
  1940. dev_priv->mm.interruptible = true;
  1941. intel_runtime_pm_put(dev_priv);
  1942. return ret;
  1943. }
  1944. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1945. {
  1946. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1947. i915_gem_object_unpin_fence(obj);
  1948. i915_gem_object_unpin_from_display_plane(obj);
  1949. }
  1950. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1951. * is assumed to be a power-of-two. */
  1952. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1953. unsigned int tiling_mode,
  1954. unsigned int cpp,
  1955. unsigned int pitch)
  1956. {
  1957. if (tiling_mode != I915_TILING_NONE) {
  1958. unsigned int tile_rows, tiles;
  1959. tile_rows = *y / 8;
  1960. *y %= 8;
  1961. tiles = *x / (512/cpp);
  1962. *x %= 512/cpp;
  1963. return tile_rows * pitch * 8 + tiles * 4096;
  1964. } else {
  1965. unsigned int offset;
  1966. offset = *y * pitch + *x * cpp;
  1967. *y = 0;
  1968. *x = (offset & 4095) / cpp;
  1969. return offset & -4096;
  1970. }
  1971. }
  1972. int intel_format_to_fourcc(int format)
  1973. {
  1974. switch (format) {
  1975. case DISPPLANE_8BPP:
  1976. return DRM_FORMAT_C8;
  1977. case DISPPLANE_BGRX555:
  1978. return DRM_FORMAT_XRGB1555;
  1979. case DISPPLANE_BGRX565:
  1980. return DRM_FORMAT_RGB565;
  1981. default:
  1982. case DISPPLANE_BGRX888:
  1983. return DRM_FORMAT_XRGB8888;
  1984. case DISPPLANE_RGBX888:
  1985. return DRM_FORMAT_XBGR8888;
  1986. case DISPPLANE_BGRX101010:
  1987. return DRM_FORMAT_XRGB2101010;
  1988. case DISPPLANE_RGBX101010:
  1989. return DRM_FORMAT_XBGR2101010;
  1990. }
  1991. }
  1992. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  1993. struct intel_plane_config *plane_config)
  1994. {
  1995. struct drm_device *dev = crtc->base.dev;
  1996. struct drm_i915_gem_object *obj = NULL;
  1997. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  1998. u32 base = plane_config->base;
  1999. if (plane_config->size == 0)
  2000. return false;
  2001. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  2002. plane_config->size);
  2003. if (!obj)
  2004. return false;
  2005. if (plane_config->tiled) {
  2006. obj->tiling_mode = I915_TILING_X;
  2007. obj->stride = crtc->base.primary->fb->pitches[0];
  2008. }
  2009. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  2010. mode_cmd.width = crtc->base.primary->fb->width;
  2011. mode_cmd.height = crtc->base.primary->fb->height;
  2012. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  2013. mutex_lock(&dev->struct_mutex);
  2014. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  2015. &mode_cmd, obj)) {
  2016. DRM_DEBUG_KMS("intel fb init failed\n");
  2017. goto out_unref_obj;
  2018. }
  2019. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2020. mutex_unlock(&dev->struct_mutex);
  2021. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2022. return true;
  2023. out_unref_obj:
  2024. drm_gem_object_unreference(&obj->base);
  2025. mutex_unlock(&dev->struct_mutex);
  2026. return false;
  2027. }
  2028. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2029. struct intel_plane_config *plane_config)
  2030. {
  2031. struct drm_device *dev = intel_crtc->base.dev;
  2032. struct drm_crtc *c;
  2033. struct intel_crtc *i;
  2034. struct drm_i915_gem_object *obj;
  2035. if (!intel_crtc->base.primary->fb)
  2036. return;
  2037. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2038. return;
  2039. kfree(intel_crtc->base.primary->fb);
  2040. intel_crtc->base.primary->fb = NULL;
  2041. /*
  2042. * Failed to alloc the obj, check to see if we should share
  2043. * an fb with another CRTC instead
  2044. */
  2045. for_each_crtc(dev, c) {
  2046. i = to_intel_crtc(c);
  2047. if (c == &intel_crtc->base)
  2048. continue;
  2049. if (!i->active)
  2050. continue;
  2051. obj = intel_fb_obj(c->primary->fb);
  2052. if (obj == NULL)
  2053. continue;
  2054. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2055. drm_framebuffer_reference(c->primary->fb);
  2056. intel_crtc->base.primary->fb = c->primary->fb;
  2057. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2058. break;
  2059. }
  2060. }
  2061. }
  2062. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2063. struct drm_framebuffer *fb,
  2064. int x, int y)
  2065. {
  2066. struct drm_device *dev = crtc->dev;
  2067. struct drm_i915_private *dev_priv = dev->dev_private;
  2068. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2069. struct drm_i915_gem_object *obj;
  2070. int plane = intel_crtc->plane;
  2071. unsigned long linear_offset;
  2072. u32 dspcntr;
  2073. u32 reg = DSPCNTR(plane);
  2074. int pixel_size;
  2075. if (!intel_crtc->primary_enabled) {
  2076. I915_WRITE(reg, 0);
  2077. if (INTEL_INFO(dev)->gen >= 4)
  2078. I915_WRITE(DSPSURF(plane), 0);
  2079. else
  2080. I915_WRITE(DSPADDR(plane), 0);
  2081. POSTING_READ(reg);
  2082. return;
  2083. }
  2084. obj = intel_fb_obj(fb);
  2085. if (WARN_ON(obj == NULL))
  2086. return;
  2087. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2088. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2089. dspcntr |= DISPLAY_PLANE_ENABLE;
  2090. if (INTEL_INFO(dev)->gen < 4) {
  2091. if (intel_crtc->pipe == PIPE_B)
  2092. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2093. /* pipesrc and dspsize control the size that is scaled from,
  2094. * which should always be the user's requested size.
  2095. */
  2096. I915_WRITE(DSPSIZE(plane),
  2097. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  2098. (intel_crtc->config.pipe_src_w - 1));
  2099. I915_WRITE(DSPPOS(plane), 0);
  2100. }
  2101. switch (fb->pixel_format) {
  2102. case DRM_FORMAT_C8:
  2103. dspcntr |= DISPPLANE_8BPP;
  2104. break;
  2105. case DRM_FORMAT_XRGB1555:
  2106. case DRM_FORMAT_ARGB1555:
  2107. dspcntr |= DISPPLANE_BGRX555;
  2108. break;
  2109. case DRM_FORMAT_RGB565:
  2110. dspcntr |= DISPPLANE_BGRX565;
  2111. break;
  2112. case DRM_FORMAT_XRGB8888:
  2113. case DRM_FORMAT_ARGB8888:
  2114. dspcntr |= DISPPLANE_BGRX888;
  2115. break;
  2116. case DRM_FORMAT_XBGR8888:
  2117. case DRM_FORMAT_ABGR8888:
  2118. dspcntr |= DISPPLANE_RGBX888;
  2119. break;
  2120. case DRM_FORMAT_XRGB2101010:
  2121. case DRM_FORMAT_ARGB2101010:
  2122. dspcntr |= DISPPLANE_BGRX101010;
  2123. break;
  2124. case DRM_FORMAT_XBGR2101010:
  2125. case DRM_FORMAT_ABGR2101010:
  2126. dspcntr |= DISPPLANE_RGBX101010;
  2127. break;
  2128. default:
  2129. BUG();
  2130. }
  2131. if (INTEL_INFO(dev)->gen >= 4 &&
  2132. obj->tiling_mode != I915_TILING_NONE)
  2133. dspcntr |= DISPPLANE_TILED;
  2134. if (IS_G4X(dev))
  2135. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2136. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2137. if (INTEL_INFO(dev)->gen >= 4) {
  2138. intel_crtc->dspaddr_offset =
  2139. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2140. pixel_size,
  2141. fb->pitches[0]);
  2142. linear_offset -= intel_crtc->dspaddr_offset;
  2143. } else {
  2144. intel_crtc->dspaddr_offset = linear_offset;
  2145. }
  2146. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2147. dspcntr |= DISPPLANE_ROTATE_180;
  2148. x += (intel_crtc->config.pipe_src_w - 1);
  2149. y += (intel_crtc->config.pipe_src_h - 1);
  2150. /* Finding the last pixel of the last line of the display
  2151. data and adding to linear_offset*/
  2152. linear_offset +=
  2153. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2154. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2155. }
  2156. I915_WRITE(reg, dspcntr);
  2157. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2158. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2159. fb->pitches[0]);
  2160. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2161. if (INTEL_INFO(dev)->gen >= 4) {
  2162. I915_WRITE(DSPSURF(plane),
  2163. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2164. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2165. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2166. } else
  2167. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2168. POSTING_READ(reg);
  2169. }
  2170. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2171. struct drm_framebuffer *fb,
  2172. int x, int y)
  2173. {
  2174. struct drm_device *dev = crtc->dev;
  2175. struct drm_i915_private *dev_priv = dev->dev_private;
  2176. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2177. struct drm_i915_gem_object *obj;
  2178. int plane = intel_crtc->plane;
  2179. unsigned long linear_offset;
  2180. u32 dspcntr;
  2181. u32 reg = DSPCNTR(plane);
  2182. int pixel_size;
  2183. if (!intel_crtc->primary_enabled) {
  2184. I915_WRITE(reg, 0);
  2185. I915_WRITE(DSPSURF(plane), 0);
  2186. POSTING_READ(reg);
  2187. return;
  2188. }
  2189. obj = intel_fb_obj(fb);
  2190. if (WARN_ON(obj == NULL))
  2191. return;
  2192. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2193. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2194. dspcntr |= DISPLAY_PLANE_ENABLE;
  2195. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2196. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2197. switch (fb->pixel_format) {
  2198. case DRM_FORMAT_C8:
  2199. dspcntr |= DISPPLANE_8BPP;
  2200. break;
  2201. case DRM_FORMAT_RGB565:
  2202. dspcntr |= DISPPLANE_BGRX565;
  2203. break;
  2204. case DRM_FORMAT_XRGB8888:
  2205. case DRM_FORMAT_ARGB8888:
  2206. dspcntr |= DISPPLANE_BGRX888;
  2207. break;
  2208. case DRM_FORMAT_XBGR8888:
  2209. case DRM_FORMAT_ABGR8888:
  2210. dspcntr |= DISPPLANE_RGBX888;
  2211. break;
  2212. case DRM_FORMAT_XRGB2101010:
  2213. case DRM_FORMAT_ARGB2101010:
  2214. dspcntr |= DISPPLANE_BGRX101010;
  2215. break;
  2216. case DRM_FORMAT_XBGR2101010:
  2217. case DRM_FORMAT_ABGR2101010:
  2218. dspcntr |= DISPPLANE_RGBX101010;
  2219. break;
  2220. default:
  2221. BUG();
  2222. }
  2223. if (obj->tiling_mode != I915_TILING_NONE)
  2224. dspcntr |= DISPPLANE_TILED;
  2225. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2226. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2227. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2228. intel_crtc->dspaddr_offset =
  2229. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2230. pixel_size,
  2231. fb->pitches[0]);
  2232. linear_offset -= intel_crtc->dspaddr_offset;
  2233. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2234. dspcntr |= DISPPLANE_ROTATE_180;
  2235. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2236. x += (intel_crtc->config.pipe_src_w - 1);
  2237. y += (intel_crtc->config.pipe_src_h - 1);
  2238. /* Finding the last pixel of the last line of the display
  2239. data and adding to linear_offset*/
  2240. linear_offset +=
  2241. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2242. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2243. }
  2244. }
  2245. I915_WRITE(reg, dspcntr);
  2246. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2247. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2248. fb->pitches[0]);
  2249. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2250. I915_WRITE(DSPSURF(plane),
  2251. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2252. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2253. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2254. } else {
  2255. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2256. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2257. }
  2258. POSTING_READ(reg);
  2259. }
  2260. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2261. struct drm_framebuffer *fb,
  2262. int x, int y)
  2263. {
  2264. struct drm_device *dev = crtc->dev;
  2265. struct drm_i915_private *dev_priv = dev->dev_private;
  2266. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2267. struct intel_framebuffer *intel_fb;
  2268. struct drm_i915_gem_object *obj;
  2269. int pipe = intel_crtc->pipe;
  2270. u32 plane_ctl, stride;
  2271. if (!intel_crtc->primary_enabled) {
  2272. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2273. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2274. POSTING_READ(PLANE_CTL(pipe, 0));
  2275. return;
  2276. }
  2277. plane_ctl = PLANE_CTL_ENABLE |
  2278. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2279. PLANE_CTL_PIPE_CSC_ENABLE;
  2280. switch (fb->pixel_format) {
  2281. case DRM_FORMAT_RGB565:
  2282. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  2283. break;
  2284. case DRM_FORMAT_XRGB8888:
  2285. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2286. break;
  2287. case DRM_FORMAT_XBGR8888:
  2288. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2289. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2290. break;
  2291. case DRM_FORMAT_XRGB2101010:
  2292. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2293. break;
  2294. case DRM_FORMAT_XBGR2101010:
  2295. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2296. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2297. break;
  2298. default:
  2299. BUG();
  2300. }
  2301. intel_fb = to_intel_framebuffer(fb);
  2302. obj = intel_fb->obj;
  2303. /*
  2304. * The stride is either expressed as a multiple of 64 bytes chunks for
  2305. * linear buffers or in number of tiles for tiled buffers.
  2306. */
  2307. switch (obj->tiling_mode) {
  2308. case I915_TILING_NONE:
  2309. stride = fb->pitches[0] >> 6;
  2310. break;
  2311. case I915_TILING_X:
  2312. plane_ctl |= PLANE_CTL_TILED_X;
  2313. stride = fb->pitches[0] >> 9;
  2314. break;
  2315. default:
  2316. BUG();
  2317. }
  2318. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2319. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2320. DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
  2321. i915_gem_obj_ggtt_offset(obj),
  2322. x, y, fb->width, fb->height,
  2323. fb->pitches[0]);
  2324. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2325. I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
  2326. I915_WRITE(PLANE_SIZE(pipe, 0),
  2327. (intel_crtc->config.pipe_src_h - 1) << 16 |
  2328. (intel_crtc->config.pipe_src_w - 1));
  2329. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2330. I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
  2331. POSTING_READ(PLANE_SURF(pipe, 0));
  2332. }
  2333. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2334. static int
  2335. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2336. int x, int y, enum mode_set_atomic state)
  2337. {
  2338. struct drm_device *dev = crtc->dev;
  2339. struct drm_i915_private *dev_priv = dev->dev_private;
  2340. if (dev_priv->display.disable_fbc)
  2341. dev_priv->display.disable_fbc(dev);
  2342. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2343. return 0;
  2344. }
  2345. void intel_display_handle_reset(struct drm_device *dev)
  2346. {
  2347. struct drm_i915_private *dev_priv = dev->dev_private;
  2348. struct drm_crtc *crtc;
  2349. /*
  2350. * Flips in the rings have been nuked by the reset,
  2351. * so complete all pending flips so that user space
  2352. * will get its events and not get stuck.
  2353. *
  2354. * Also update the base address of all primary
  2355. * planes to the the last fb to make sure we're
  2356. * showing the correct fb after a reset.
  2357. *
  2358. * Need to make two loops over the crtcs so that we
  2359. * don't try to grab a crtc mutex before the
  2360. * pending_flip_queue really got woken up.
  2361. */
  2362. for_each_crtc(dev, crtc) {
  2363. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2364. enum plane plane = intel_crtc->plane;
  2365. intel_prepare_page_flip(dev, plane);
  2366. intel_finish_page_flip_plane(dev, plane);
  2367. }
  2368. for_each_crtc(dev, crtc) {
  2369. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2370. drm_modeset_lock(&crtc->mutex, NULL);
  2371. /*
  2372. * FIXME: Once we have proper support for primary planes (and
  2373. * disabling them without disabling the entire crtc) allow again
  2374. * a NULL crtc->primary->fb.
  2375. */
  2376. if (intel_crtc->active && crtc->primary->fb)
  2377. dev_priv->display.update_primary_plane(crtc,
  2378. crtc->primary->fb,
  2379. crtc->x,
  2380. crtc->y);
  2381. drm_modeset_unlock(&crtc->mutex);
  2382. }
  2383. }
  2384. static int
  2385. intel_finish_fb(struct drm_framebuffer *old_fb)
  2386. {
  2387. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2388. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2389. bool was_interruptible = dev_priv->mm.interruptible;
  2390. int ret;
  2391. /* Big Hammer, we also need to ensure that any pending
  2392. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2393. * current scanout is retired before unpinning the old
  2394. * framebuffer.
  2395. *
  2396. * This should only fail upon a hung GPU, in which case we
  2397. * can safely continue.
  2398. */
  2399. dev_priv->mm.interruptible = false;
  2400. ret = i915_gem_object_finish_gpu(obj);
  2401. dev_priv->mm.interruptible = was_interruptible;
  2402. return ret;
  2403. }
  2404. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2405. {
  2406. struct drm_device *dev = crtc->dev;
  2407. struct drm_i915_private *dev_priv = dev->dev_private;
  2408. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2409. bool pending;
  2410. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2411. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2412. return false;
  2413. spin_lock_irq(&dev->event_lock);
  2414. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2415. spin_unlock_irq(&dev->event_lock);
  2416. return pending;
  2417. }
  2418. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2419. {
  2420. struct drm_device *dev = crtc->base.dev;
  2421. struct drm_i915_private *dev_priv = dev->dev_private;
  2422. const struct drm_display_mode *adjusted_mode;
  2423. if (!i915.fastboot)
  2424. return;
  2425. /*
  2426. * Update pipe size and adjust fitter if needed: the reason for this is
  2427. * that in compute_mode_changes we check the native mode (not the pfit
  2428. * mode) to see if we can flip rather than do a full mode set. In the
  2429. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2430. * pfit state, we'll end up with a big fb scanned out into the wrong
  2431. * sized surface.
  2432. *
  2433. * To fix this properly, we need to hoist the checks up into
  2434. * compute_mode_changes (or above), check the actual pfit state and
  2435. * whether the platform allows pfit disable with pipe active, and only
  2436. * then update the pipesrc and pfit state, even on the flip path.
  2437. */
  2438. adjusted_mode = &crtc->config.adjusted_mode;
  2439. I915_WRITE(PIPESRC(crtc->pipe),
  2440. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2441. (adjusted_mode->crtc_vdisplay - 1));
  2442. if (!crtc->config.pch_pfit.enabled &&
  2443. (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) ||
  2444. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))) {
  2445. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2446. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2447. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2448. }
  2449. crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2450. crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2451. }
  2452. static int
  2453. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  2454. struct drm_framebuffer *fb)
  2455. {
  2456. struct drm_device *dev = crtc->dev;
  2457. struct drm_i915_private *dev_priv = dev->dev_private;
  2458. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2459. enum pipe pipe = intel_crtc->pipe;
  2460. struct drm_framebuffer *old_fb = crtc->primary->fb;
  2461. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2462. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
  2463. int ret;
  2464. if (intel_crtc_has_pending_flip(crtc)) {
  2465. DRM_ERROR("pipe is still busy with an old pageflip\n");
  2466. return -EBUSY;
  2467. }
  2468. /* no fb bound */
  2469. if (!fb) {
  2470. DRM_ERROR("No FB bound\n");
  2471. return 0;
  2472. }
  2473. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  2474. DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
  2475. plane_name(intel_crtc->plane),
  2476. INTEL_INFO(dev)->num_pipes);
  2477. return -EINVAL;
  2478. }
  2479. mutex_lock(&dev->struct_mutex);
  2480. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  2481. if (ret == 0)
  2482. i915_gem_track_fb(old_obj, obj,
  2483. INTEL_FRONTBUFFER_PRIMARY(pipe));
  2484. mutex_unlock(&dev->struct_mutex);
  2485. if (ret != 0) {
  2486. DRM_ERROR("pin & fence failed\n");
  2487. return ret;
  2488. }
  2489. intel_update_pipe_size(intel_crtc);
  2490. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2491. if (intel_crtc->active)
  2492. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  2493. crtc->primary->fb = fb;
  2494. crtc->x = x;
  2495. crtc->y = y;
  2496. if (old_fb) {
  2497. if (intel_crtc->active && old_fb != fb)
  2498. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2499. mutex_lock(&dev->struct_mutex);
  2500. intel_unpin_fb_obj(old_obj);
  2501. mutex_unlock(&dev->struct_mutex);
  2502. }
  2503. mutex_lock(&dev->struct_mutex);
  2504. intel_update_fbc(dev);
  2505. mutex_unlock(&dev->struct_mutex);
  2506. return 0;
  2507. }
  2508. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2509. {
  2510. struct drm_device *dev = crtc->dev;
  2511. struct drm_i915_private *dev_priv = dev->dev_private;
  2512. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2513. int pipe = intel_crtc->pipe;
  2514. u32 reg, temp;
  2515. /* enable normal train */
  2516. reg = FDI_TX_CTL(pipe);
  2517. temp = I915_READ(reg);
  2518. if (IS_IVYBRIDGE(dev)) {
  2519. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2520. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2521. } else {
  2522. temp &= ~FDI_LINK_TRAIN_NONE;
  2523. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2524. }
  2525. I915_WRITE(reg, temp);
  2526. reg = FDI_RX_CTL(pipe);
  2527. temp = I915_READ(reg);
  2528. if (HAS_PCH_CPT(dev)) {
  2529. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2530. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2531. } else {
  2532. temp &= ~FDI_LINK_TRAIN_NONE;
  2533. temp |= FDI_LINK_TRAIN_NONE;
  2534. }
  2535. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2536. /* wait one idle pattern time */
  2537. POSTING_READ(reg);
  2538. udelay(1000);
  2539. /* IVB wants error correction enabled */
  2540. if (IS_IVYBRIDGE(dev))
  2541. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2542. FDI_FE_ERRC_ENABLE);
  2543. }
  2544. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2545. {
  2546. return crtc->base.enabled && crtc->active &&
  2547. crtc->config.has_pch_encoder;
  2548. }
  2549. static void ivb_modeset_global_resources(struct drm_device *dev)
  2550. {
  2551. struct drm_i915_private *dev_priv = dev->dev_private;
  2552. struct intel_crtc *pipe_B_crtc =
  2553. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2554. struct intel_crtc *pipe_C_crtc =
  2555. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2556. uint32_t temp;
  2557. /*
  2558. * When everything is off disable fdi C so that we could enable fdi B
  2559. * with all lanes. Note that we don't care about enabled pipes without
  2560. * an enabled pch encoder.
  2561. */
  2562. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2563. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2564. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2565. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2566. temp = I915_READ(SOUTH_CHICKEN1);
  2567. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2568. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2569. I915_WRITE(SOUTH_CHICKEN1, temp);
  2570. }
  2571. }
  2572. /* The FDI link training functions for ILK/Ibexpeak. */
  2573. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2574. {
  2575. struct drm_device *dev = crtc->dev;
  2576. struct drm_i915_private *dev_priv = dev->dev_private;
  2577. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2578. int pipe = intel_crtc->pipe;
  2579. u32 reg, temp, tries;
  2580. /* FDI needs bits from pipe first */
  2581. assert_pipe_enabled(dev_priv, pipe);
  2582. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2583. for train result */
  2584. reg = FDI_RX_IMR(pipe);
  2585. temp = I915_READ(reg);
  2586. temp &= ~FDI_RX_SYMBOL_LOCK;
  2587. temp &= ~FDI_RX_BIT_LOCK;
  2588. I915_WRITE(reg, temp);
  2589. I915_READ(reg);
  2590. udelay(150);
  2591. /* enable CPU FDI TX and PCH FDI RX */
  2592. reg = FDI_TX_CTL(pipe);
  2593. temp = I915_READ(reg);
  2594. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2595. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2596. temp &= ~FDI_LINK_TRAIN_NONE;
  2597. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2598. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2599. reg = FDI_RX_CTL(pipe);
  2600. temp = I915_READ(reg);
  2601. temp &= ~FDI_LINK_TRAIN_NONE;
  2602. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2603. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2604. POSTING_READ(reg);
  2605. udelay(150);
  2606. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2607. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2608. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2609. FDI_RX_PHASE_SYNC_POINTER_EN);
  2610. reg = FDI_RX_IIR(pipe);
  2611. for (tries = 0; tries < 5; tries++) {
  2612. temp = I915_READ(reg);
  2613. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2614. if ((temp & FDI_RX_BIT_LOCK)) {
  2615. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2616. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2617. break;
  2618. }
  2619. }
  2620. if (tries == 5)
  2621. DRM_ERROR("FDI train 1 fail!\n");
  2622. /* Train 2 */
  2623. reg = FDI_TX_CTL(pipe);
  2624. temp = I915_READ(reg);
  2625. temp &= ~FDI_LINK_TRAIN_NONE;
  2626. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2627. I915_WRITE(reg, temp);
  2628. reg = FDI_RX_CTL(pipe);
  2629. temp = I915_READ(reg);
  2630. temp &= ~FDI_LINK_TRAIN_NONE;
  2631. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2632. I915_WRITE(reg, temp);
  2633. POSTING_READ(reg);
  2634. udelay(150);
  2635. reg = FDI_RX_IIR(pipe);
  2636. for (tries = 0; tries < 5; tries++) {
  2637. temp = I915_READ(reg);
  2638. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2639. if (temp & FDI_RX_SYMBOL_LOCK) {
  2640. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2641. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2642. break;
  2643. }
  2644. }
  2645. if (tries == 5)
  2646. DRM_ERROR("FDI train 2 fail!\n");
  2647. DRM_DEBUG_KMS("FDI train done\n");
  2648. }
  2649. static const int snb_b_fdi_train_param[] = {
  2650. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2651. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2652. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2653. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2654. };
  2655. /* The FDI link training functions for SNB/Cougarpoint. */
  2656. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2657. {
  2658. struct drm_device *dev = crtc->dev;
  2659. struct drm_i915_private *dev_priv = dev->dev_private;
  2660. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2661. int pipe = intel_crtc->pipe;
  2662. u32 reg, temp, i, retry;
  2663. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2664. for train result */
  2665. reg = FDI_RX_IMR(pipe);
  2666. temp = I915_READ(reg);
  2667. temp &= ~FDI_RX_SYMBOL_LOCK;
  2668. temp &= ~FDI_RX_BIT_LOCK;
  2669. I915_WRITE(reg, temp);
  2670. POSTING_READ(reg);
  2671. udelay(150);
  2672. /* enable CPU FDI TX and PCH FDI RX */
  2673. reg = FDI_TX_CTL(pipe);
  2674. temp = I915_READ(reg);
  2675. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2676. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2677. temp &= ~FDI_LINK_TRAIN_NONE;
  2678. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2679. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2680. /* SNB-B */
  2681. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2682. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2683. I915_WRITE(FDI_RX_MISC(pipe),
  2684. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2685. reg = FDI_RX_CTL(pipe);
  2686. temp = I915_READ(reg);
  2687. if (HAS_PCH_CPT(dev)) {
  2688. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2689. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2690. } else {
  2691. temp &= ~FDI_LINK_TRAIN_NONE;
  2692. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2693. }
  2694. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2695. POSTING_READ(reg);
  2696. udelay(150);
  2697. for (i = 0; i < 4; i++) {
  2698. reg = FDI_TX_CTL(pipe);
  2699. temp = I915_READ(reg);
  2700. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2701. temp |= snb_b_fdi_train_param[i];
  2702. I915_WRITE(reg, temp);
  2703. POSTING_READ(reg);
  2704. udelay(500);
  2705. for (retry = 0; retry < 5; retry++) {
  2706. reg = FDI_RX_IIR(pipe);
  2707. temp = I915_READ(reg);
  2708. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2709. if (temp & FDI_RX_BIT_LOCK) {
  2710. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2711. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2712. break;
  2713. }
  2714. udelay(50);
  2715. }
  2716. if (retry < 5)
  2717. break;
  2718. }
  2719. if (i == 4)
  2720. DRM_ERROR("FDI train 1 fail!\n");
  2721. /* Train 2 */
  2722. reg = FDI_TX_CTL(pipe);
  2723. temp = I915_READ(reg);
  2724. temp &= ~FDI_LINK_TRAIN_NONE;
  2725. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2726. if (IS_GEN6(dev)) {
  2727. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2728. /* SNB-B */
  2729. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2730. }
  2731. I915_WRITE(reg, temp);
  2732. reg = FDI_RX_CTL(pipe);
  2733. temp = I915_READ(reg);
  2734. if (HAS_PCH_CPT(dev)) {
  2735. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2736. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2737. } else {
  2738. temp &= ~FDI_LINK_TRAIN_NONE;
  2739. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2740. }
  2741. I915_WRITE(reg, temp);
  2742. POSTING_READ(reg);
  2743. udelay(150);
  2744. for (i = 0; i < 4; i++) {
  2745. reg = FDI_TX_CTL(pipe);
  2746. temp = I915_READ(reg);
  2747. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2748. temp |= snb_b_fdi_train_param[i];
  2749. I915_WRITE(reg, temp);
  2750. POSTING_READ(reg);
  2751. udelay(500);
  2752. for (retry = 0; retry < 5; retry++) {
  2753. reg = FDI_RX_IIR(pipe);
  2754. temp = I915_READ(reg);
  2755. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2756. if (temp & FDI_RX_SYMBOL_LOCK) {
  2757. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2758. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2759. break;
  2760. }
  2761. udelay(50);
  2762. }
  2763. if (retry < 5)
  2764. break;
  2765. }
  2766. if (i == 4)
  2767. DRM_ERROR("FDI train 2 fail!\n");
  2768. DRM_DEBUG_KMS("FDI train done.\n");
  2769. }
  2770. /* Manual link training for Ivy Bridge A0 parts */
  2771. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2772. {
  2773. struct drm_device *dev = crtc->dev;
  2774. struct drm_i915_private *dev_priv = dev->dev_private;
  2775. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2776. int pipe = intel_crtc->pipe;
  2777. u32 reg, temp, i, j;
  2778. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2779. for train result */
  2780. reg = FDI_RX_IMR(pipe);
  2781. temp = I915_READ(reg);
  2782. temp &= ~FDI_RX_SYMBOL_LOCK;
  2783. temp &= ~FDI_RX_BIT_LOCK;
  2784. I915_WRITE(reg, temp);
  2785. POSTING_READ(reg);
  2786. udelay(150);
  2787. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2788. I915_READ(FDI_RX_IIR(pipe)));
  2789. /* Try each vswing and preemphasis setting twice before moving on */
  2790. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2791. /* disable first in case we need to retry */
  2792. reg = FDI_TX_CTL(pipe);
  2793. temp = I915_READ(reg);
  2794. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2795. temp &= ~FDI_TX_ENABLE;
  2796. I915_WRITE(reg, temp);
  2797. reg = FDI_RX_CTL(pipe);
  2798. temp = I915_READ(reg);
  2799. temp &= ~FDI_LINK_TRAIN_AUTO;
  2800. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2801. temp &= ~FDI_RX_ENABLE;
  2802. I915_WRITE(reg, temp);
  2803. /* enable CPU FDI TX and PCH FDI RX */
  2804. reg = FDI_TX_CTL(pipe);
  2805. temp = I915_READ(reg);
  2806. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2807. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2808. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2809. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2810. temp |= snb_b_fdi_train_param[j/2];
  2811. temp |= FDI_COMPOSITE_SYNC;
  2812. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2813. I915_WRITE(FDI_RX_MISC(pipe),
  2814. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2815. reg = FDI_RX_CTL(pipe);
  2816. temp = I915_READ(reg);
  2817. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2818. temp |= FDI_COMPOSITE_SYNC;
  2819. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2820. POSTING_READ(reg);
  2821. udelay(1); /* should be 0.5us */
  2822. for (i = 0; i < 4; i++) {
  2823. reg = FDI_RX_IIR(pipe);
  2824. temp = I915_READ(reg);
  2825. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2826. if (temp & FDI_RX_BIT_LOCK ||
  2827. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2828. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2829. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2830. i);
  2831. break;
  2832. }
  2833. udelay(1); /* should be 0.5us */
  2834. }
  2835. if (i == 4) {
  2836. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2837. continue;
  2838. }
  2839. /* Train 2 */
  2840. reg = FDI_TX_CTL(pipe);
  2841. temp = I915_READ(reg);
  2842. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2843. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2844. I915_WRITE(reg, temp);
  2845. reg = FDI_RX_CTL(pipe);
  2846. temp = I915_READ(reg);
  2847. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2848. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2849. I915_WRITE(reg, temp);
  2850. POSTING_READ(reg);
  2851. udelay(2); /* should be 1.5us */
  2852. for (i = 0; i < 4; i++) {
  2853. reg = FDI_RX_IIR(pipe);
  2854. temp = I915_READ(reg);
  2855. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2856. if (temp & FDI_RX_SYMBOL_LOCK ||
  2857. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2858. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2859. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2860. i);
  2861. goto train_done;
  2862. }
  2863. udelay(2); /* should be 1.5us */
  2864. }
  2865. if (i == 4)
  2866. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2867. }
  2868. train_done:
  2869. DRM_DEBUG_KMS("FDI train done.\n");
  2870. }
  2871. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2872. {
  2873. struct drm_device *dev = intel_crtc->base.dev;
  2874. struct drm_i915_private *dev_priv = dev->dev_private;
  2875. int pipe = intel_crtc->pipe;
  2876. u32 reg, temp;
  2877. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2878. reg = FDI_RX_CTL(pipe);
  2879. temp = I915_READ(reg);
  2880. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2881. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2882. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2883. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2884. POSTING_READ(reg);
  2885. udelay(200);
  2886. /* Switch from Rawclk to PCDclk */
  2887. temp = I915_READ(reg);
  2888. I915_WRITE(reg, temp | FDI_PCDCLK);
  2889. POSTING_READ(reg);
  2890. udelay(200);
  2891. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2892. reg = FDI_TX_CTL(pipe);
  2893. temp = I915_READ(reg);
  2894. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2895. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2896. POSTING_READ(reg);
  2897. udelay(100);
  2898. }
  2899. }
  2900. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2901. {
  2902. struct drm_device *dev = intel_crtc->base.dev;
  2903. struct drm_i915_private *dev_priv = dev->dev_private;
  2904. int pipe = intel_crtc->pipe;
  2905. u32 reg, temp;
  2906. /* Switch from PCDclk to Rawclk */
  2907. reg = FDI_RX_CTL(pipe);
  2908. temp = I915_READ(reg);
  2909. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2910. /* Disable CPU FDI TX PLL */
  2911. reg = FDI_TX_CTL(pipe);
  2912. temp = I915_READ(reg);
  2913. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2914. POSTING_READ(reg);
  2915. udelay(100);
  2916. reg = FDI_RX_CTL(pipe);
  2917. temp = I915_READ(reg);
  2918. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2919. /* Wait for the clocks to turn off. */
  2920. POSTING_READ(reg);
  2921. udelay(100);
  2922. }
  2923. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2924. {
  2925. struct drm_device *dev = crtc->dev;
  2926. struct drm_i915_private *dev_priv = dev->dev_private;
  2927. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2928. int pipe = intel_crtc->pipe;
  2929. u32 reg, temp;
  2930. /* disable CPU FDI tx and PCH FDI rx */
  2931. reg = FDI_TX_CTL(pipe);
  2932. temp = I915_READ(reg);
  2933. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2934. POSTING_READ(reg);
  2935. reg = FDI_RX_CTL(pipe);
  2936. temp = I915_READ(reg);
  2937. temp &= ~(0x7 << 16);
  2938. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2939. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2940. POSTING_READ(reg);
  2941. udelay(100);
  2942. /* Ironlake workaround, disable clock pointer after downing FDI */
  2943. if (HAS_PCH_IBX(dev))
  2944. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2945. /* still set train pattern 1 */
  2946. reg = FDI_TX_CTL(pipe);
  2947. temp = I915_READ(reg);
  2948. temp &= ~FDI_LINK_TRAIN_NONE;
  2949. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2950. I915_WRITE(reg, temp);
  2951. reg = FDI_RX_CTL(pipe);
  2952. temp = I915_READ(reg);
  2953. if (HAS_PCH_CPT(dev)) {
  2954. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2955. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2956. } else {
  2957. temp &= ~FDI_LINK_TRAIN_NONE;
  2958. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2959. }
  2960. /* BPC in FDI rx is consistent with that in PIPECONF */
  2961. temp &= ~(0x07 << 16);
  2962. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2963. I915_WRITE(reg, temp);
  2964. POSTING_READ(reg);
  2965. udelay(100);
  2966. }
  2967. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  2968. {
  2969. struct intel_crtc *crtc;
  2970. /* Note that we don't need to be called with mode_config.lock here
  2971. * as our list of CRTC objects is static for the lifetime of the
  2972. * device and so cannot disappear as we iterate. Similarly, we can
  2973. * happily treat the predicates as racy, atomic checks as userspace
  2974. * cannot claim and pin a new fb without at least acquring the
  2975. * struct_mutex and so serialising with us.
  2976. */
  2977. for_each_intel_crtc(dev, crtc) {
  2978. if (atomic_read(&crtc->unpin_work_count) == 0)
  2979. continue;
  2980. if (crtc->unpin_work)
  2981. intel_wait_for_vblank(dev, crtc->pipe);
  2982. return true;
  2983. }
  2984. return false;
  2985. }
  2986. static void page_flip_completed(struct intel_crtc *intel_crtc)
  2987. {
  2988. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  2989. struct intel_unpin_work *work = intel_crtc->unpin_work;
  2990. /* ensure that the unpin work is consistent wrt ->pending. */
  2991. smp_rmb();
  2992. intel_crtc->unpin_work = NULL;
  2993. if (work->event)
  2994. drm_send_vblank_event(intel_crtc->base.dev,
  2995. intel_crtc->pipe,
  2996. work->event);
  2997. drm_crtc_vblank_put(&intel_crtc->base);
  2998. wake_up_all(&dev_priv->pending_flip_queue);
  2999. queue_work(dev_priv->wq, &work->work);
  3000. trace_i915_flip_complete(intel_crtc->plane,
  3001. work->pending_flip_obj);
  3002. }
  3003. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3004. {
  3005. struct drm_device *dev = crtc->dev;
  3006. struct drm_i915_private *dev_priv = dev->dev_private;
  3007. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3008. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3009. !intel_crtc_has_pending_flip(crtc),
  3010. 60*HZ) == 0)) {
  3011. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3012. spin_lock_irq(&dev->event_lock);
  3013. if (intel_crtc->unpin_work) {
  3014. WARN_ONCE(1, "Removing stuck page flip\n");
  3015. page_flip_completed(intel_crtc);
  3016. }
  3017. spin_unlock_irq(&dev->event_lock);
  3018. }
  3019. if (crtc->primary->fb) {
  3020. mutex_lock(&dev->struct_mutex);
  3021. intel_finish_fb(crtc->primary->fb);
  3022. mutex_unlock(&dev->struct_mutex);
  3023. }
  3024. }
  3025. /* Program iCLKIP clock to the desired frequency */
  3026. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3027. {
  3028. struct drm_device *dev = crtc->dev;
  3029. struct drm_i915_private *dev_priv = dev->dev_private;
  3030. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  3031. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3032. u32 temp;
  3033. mutex_lock(&dev_priv->dpio_lock);
  3034. /* It is necessary to ungate the pixclk gate prior to programming
  3035. * the divisors, and gate it back when it is done.
  3036. */
  3037. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3038. /* Disable SSCCTL */
  3039. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3040. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3041. SBI_SSCCTL_DISABLE,
  3042. SBI_ICLK);
  3043. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3044. if (clock == 20000) {
  3045. auxdiv = 1;
  3046. divsel = 0x41;
  3047. phaseinc = 0x20;
  3048. } else {
  3049. /* The iCLK virtual clock root frequency is in MHz,
  3050. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3051. * divisors, it is necessary to divide one by another, so we
  3052. * convert the virtual clock precision to KHz here for higher
  3053. * precision.
  3054. */
  3055. u32 iclk_virtual_root_freq = 172800 * 1000;
  3056. u32 iclk_pi_range = 64;
  3057. u32 desired_divisor, msb_divisor_value, pi_value;
  3058. desired_divisor = (iclk_virtual_root_freq / clock);
  3059. msb_divisor_value = desired_divisor / iclk_pi_range;
  3060. pi_value = desired_divisor % iclk_pi_range;
  3061. auxdiv = 0;
  3062. divsel = msb_divisor_value - 2;
  3063. phaseinc = pi_value;
  3064. }
  3065. /* This should not happen with any sane values */
  3066. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3067. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3068. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3069. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3070. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3071. clock,
  3072. auxdiv,
  3073. divsel,
  3074. phasedir,
  3075. phaseinc);
  3076. /* Program SSCDIVINTPHASE6 */
  3077. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3078. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3079. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3080. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3081. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3082. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3083. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3084. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3085. /* Program SSCAUXDIV */
  3086. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3087. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3088. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3089. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3090. /* Enable modulator and associated divider */
  3091. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3092. temp &= ~SBI_SSCCTL_DISABLE;
  3093. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3094. /* Wait for initialization time */
  3095. udelay(24);
  3096. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3097. mutex_unlock(&dev_priv->dpio_lock);
  3098. }
  3099. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3100. enum pipe pch_transcoder)
  3101. {
  3102. struct drm_device *dev = crtc->base.dev;
  3103. struct drm_i915_private *dev_priv = dev->dev_private;
  3104. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  3105. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3106. I915_READ(HTOTAL(cpu_transcoder)));
  3107. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3108. I915_READ(HBLANK(cpu_transcoder)));
  3109. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3110. I915_READ(HSYNC(cpu_transcoder)));
  3111. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3112. I915_READ(VTOTAL(cpu_transcoder)));
  3113. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3114. I915_READ(VBLANK(cpu_transcoder)));
  3115. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3116. I915_READ(VSYNC(cpu_transcoder)));
  3117. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3118. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3119. }
  3120. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  3121. {
  3122. struct drm_i915_private *dev_priv = dev->dev_private;
  3123. uint32_t temp;
  3124. temp = I915_READ(SOUTH_CHICKEN1);
  3125. if (temp & FDI_BC_BIFURCATION_SELECT)
  3126. return;
  3127. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3128. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3129. temp |= FDI_BC_BIFURCATION_SELECT;
  3130. DRM_DEBUG_KMS("enabling fdi C rx\n");
  3131. I915_WRITE(SOUTH_CHICKEN1, temp);
  3132. POSTING_READ(SOUTH_CHICKEN1);
  3133. }
  3134. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3135. {
  3136. struct drm_device *dev = intel_crtc->base.dev;
  3137. struct drm_i915_private *dev_priv = dev->dev_private;
  3138. switch (intel_crtc->pipe) {
  3139. case PIPE_A:
  3140. break;
  3141. case PIPE_B:
  3142. if (intel_crtc->config.fdi_lanes > 2)
  3143. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  3144. else
  3145. cpt_enable_fdi_bc_bifurcation(dev);
  3146. break;
  3147. case PIPE_C:
  3148. cpt_enable_fdi_bc_bifurcation(dev);
  3149. break;
  3150. default:
  3151. BUG();
  3152. }
  3153. }
  3154. /*
  3155. * Enable PCH resources required for PCH ports:
  3156. * - PCH PLLs
  3157. * - FDI training & RX/TX
  3158. * - update transcoder timings
  3159. * - DP transcoding bits
  3160. * - transcoder
  3161. */
  3162. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3163. {
  3164. struct drm_device *dev = crtc->dev;
  3165. struct drm_i915_private *dev_priv = dev->dev_private;
  3166. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3167. int pipe = intel_crtc->pipe;
  3168. u32 reg, temp;
  3169. assert_pch_transcoder_disabled(dev_priv, pipe);
  3170. if (IS_IVYBRIDGE(dev))
  3171. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3172. /* Write the TU size bits before fdi link training, so that error
  3173. * detection works. */
  3174. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3175. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3176. /* For PCH output, training FDI link */
  3177. dev_priv->display.fdi_link_train(crtc);
  3178. /* We need to program the right clock selection before writing the pixel
  3179. * mutliplier into the DPLL. */
  3180. if (HAS_PCH_CPT(dev)) {
  3181. u32 sel;
  3182. temp = I915_READ(PCH_DPLL_SEL);
  3183. temp |= TRANS_DPLL_ENABLE(pipe);
  3184. sel = TRANS_DPLLB_SEL(pipe);
  3185. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  3186. temp |= sel;
  3187. else
  3188. temp &= ~sel;
  3189. I915_WRITE(PCH_DPLL_SEL, temp);
  3190. }
  3191. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3192. * transcoder, and we actually should do this to not upset any PCH
  3193. * transcoder that already use the clock when we share it.
  3194. *
  3195. * Note that enable_shared_dpll tries to do the right thing, but
  3196. * get_shared_dpll unconditionally resets the pll - we need that to have
  3197. * the right LVDS enable sequence. */
  3198. intel_enable_shared_dpll(intel_crtc);
  3199. /* set transcoder timing, panel must allow it */
  3200. assert_panel_unlocked(dev_priv, pipe);
  3201. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3202. intel_fdi_normal_train(crtc);
  3203. /* For PCH DP, enable TRANS_DP_CTL */
  3204. if (HAS_PCH_CPT(dev) &&
  3205. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  3206. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  3207. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3208. reg = TRANS_DP_CTL(pipe);
  3209. temp = I915_READ(reg);
  3210. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3211. TRANS_DP_SYNC_MASK |
  3212. TRANS_DP_BPC_MASK);
  3213. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3214. TRANS_DP_ENH_FRAMING);
  3215. temp |= bpc << 9; /* same format but at 11:9 */
  3216. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3217. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3218. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3219. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3220. switch (intel_trans_dp_port_sel(crtc)) {
  3221. case PCH_DP_B:
  3222. temp |= TRANS_DP_PORT_SEL_B;
  3223. break;
  3224. case PCH_DP_C:
  3225. temp |= TRANS_DP_PORT_SEL_C;
  3226. break;
  3227. case PCH_DP_D:
  3228. temp |= TRANS_DP_PORT_SEL_D;
  3229. break;
  3230. default:
  3231. BUG();
  3232. }
  3233. I915_WRITE(reg, temp);
  3234. }
  3235. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3236. }
  3237. static void lpt_pch_enable(struct drm_crtc *crtc)
  3238. {
  3239. struct drm_device *dev = crtc->dev;
  3240. struct drm_i915_private *dev_priv = dev->dev_private;
  3241. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3242. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3243. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3244. lpt_program_iclkip(crtc);
  3245. /* Set transcoder timing. */
  3246. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3247. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3248. }
  3249. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3250. {
  3251. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3252. if (pll == NULL)
  3253. return;
  3254. if (pll->refcount == 0) {
  3255. WARN(1, "bad %s refcount\n", pll->name);
  3256. return;
  3257. }
  3258. if (--pll->refcount == 0) {
  3259. WARN_ON(pll->on);
  3260. WARN_ON(pll->active);
  3261. }
  3262. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3263. }
  3264. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3265. {
  3266. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3267. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3268. enum intel_dpll_id i;
  3269. if (pll) {
  3270. DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
  3271. crtc->base.base.id, pll->name);
  3272. intel_put_shared_dpll(crtc);
  3273. }
  3274. if (HAS_PCH_IBX(dev_priv->dev)) {
  3275. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3276. i = (enum intel_dpll_id) crtc->pipe;
  3277. pll = &dev_priv->shared_dplls[i];
  3278. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3279. crtc->base.base.id, pll->name);
  3280. WARN_ON(pll->refcount);
  3281. goto found;
  3282. }
  3283. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3284. pll = &dev_priv->shared_dplls[i];
  3285. /* Only want to check enabled timings first */
  3286. if (pll->refcount == 0)
  3287. continue;
  3288. if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
  3289. sizeof(pll->hw_state)) == 0) {
  3290. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
  3291. crtc->base.base.id,
  3292. pll->name, pll->refcount, pll->active);
  3293. goto found;
  3294. }
  3295. }
  3296. /* Ok no matching timings, maybe there's a free one? */
  3297. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3298. pll = &dev_priv->shared_dplls[i];
  3299. if (pll->refcount == 0) {
  3300. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3301. crtc->base.base.id, pll->name);
  3302. goto found;
  3303. }
  3304. }
  3305. return NULL;
  3306. found:
  3307. if (pll->refcount == 0)
  3308. pll->hw_state = crtc->config.dpll_hw_state;
  3309. crtc->config.shared_dpll = i;
  3310. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3311. pipe_name(crtc->pipe));
  3312. pll->refcount++;
  3313. return pll;
  3314. }
  3315. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3316. {
  3317. struct drm_i915_private *dev_priv = dev->dev_private;
  3318. int dslreg = PIPEDSL(pipe);
  3319. u32 temp;
  3320. temp = I915_READ(dslreg);
  3321. udelay(500);
  3322. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3323. if (wait_for(I915_READ(dslreg) != temp, 5))
  3324. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3325. }
  3326. }
  3327. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3328. {
  3329. struct drm_device *dev = crtc->base.dev;
  3330. struct drm_i915_private *dev_priv = dev->dev_private;
  3331. int pipe = crtc->pipe;
  3332. if (crtc->config.pch_pfit.enabled) {
  3333. /* Force use of hard-coded filter coefficients
  3334. * as some pre-programmed values are broken,
  3335. * e.g. x201.
  3336. */
  3337. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3338. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3339. PF_PIPE_SEL_IVB(pipe));
  3340. else
  3341. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3342. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3343. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3344. }
  3345. }
  3346. static void intel_enable_planes(struct drm_crtc *crtc)
  3347. {
  3348. struct drm_device *dev = crtc->dev;
  3349. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3350. struct drm_plane *plane;
  3351. struct intel_plane *intel_plane;
  3352. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3353. intel_plane = to_intel_plane(plane);
  3354. if (intel_plane->pipe == pipe)
  3355. intel_plane_restore(&intel_plane->base);
  3356. }
  3357. }
  3358. static void intel_disable_planes(struct drm_crtc *crtc)
  3359. {
  3360. struct drm_device *dev = crtc->dev;
  3361. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3362. struct drm_plane *plane;
  3363. struct intel_plane *intel_plane;
  3364. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3365. intel_plane = to_intel_plane(plane);
  3366. if (intel_plane->pipe == pipe)
  3367. intel_plane_disable(&intel_plane->base);
  3368. }
  3369. }
  3370. void hsw_enable_ips(struct intel_crtc *crtc)
  3371. {
  3372. struct drm_device *dev = crtc->base.dev;
  3373. struct drm_i915_private *dev_priv = dev->dev_private;
  3374. if (!crtc->config.ips_enabled)
  3375. return;
  3376. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3377. intel_wait_for_vblank(dev, crtc->pipe);
  3378. assert_plane_enabled(dev_priv, crtc->plane);
  3379. if (IS_BROADWELL(dev)) {
  3380. mutex_lock(&dev_priv->rps.hw_lock);
  3381. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3382. mutex_unlock(&dev_priv->rps.hw_lock);
  3383. /* Quoting Art Runyan: "its not safe to expect any particular
  3384. * value in IPS_CTL bit 31 after enabling IPS through the
  3385. * mailbox." Moreover, the mailbox may return a bogus state,
  3386. * so we need to just enable it and continue on.
  3387. */
  3388. } else {
  3389. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3390. /* The bit only becomes 1 in the next vblank, so this wait here
  3391. * is essentially intel_wait_for_vblank. If we don't have this
  3392. * and don't wait for vblanks until the end of crtc_enable, then
  3393. * the HW state readout code will complain that the expected
  3394. * IPS_CTL value is not the one we read. */
  3395. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3396. DRM_ERROR("Timed out waiting for IPS enable\n");
  3397. }
  3398. }
  3399. void hsw_disable_ips(struct intel_crtc *crtc)
  3400. {
  3401. struct drm_device *dev = crtc->base.dev;
  3402. struct drm_i915_private *dev_priv = dev->dev_private;
  3403. if (!crtc->config.ips_enabled)
  3404. return;
  3405. assert_plane_enabled(dev_priv, crtc->plane);
  3406. if (IS_BROADWELL(dev)) {
  3407. mutex_lock(&dev_priv->rps.hw_lock);
  3408. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3409. mutex_unlock(&dev_priv->rps.hw_lock);
  3410. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3411. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3412. DRM_ERROR("Timed out waiting for IPS disable\n");
  3413. } else {
  3414. I915_WRITE(IPS_CTL, 0);
  3415. POSTING_READ(IPS_CTL);
  3416. }
  3417. /* We need to wait for a vblank before we can disable the plane. */
  3418. intel_wait_for_vblank(dev, crtc->pipe);
  3419. }
  3420. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3421. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3422. {
  3423. struct drm_device *dev = crtc->dev;
  3424. struct drm_i915_private *dev_priv = dev->dev_private;
  3425. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3426. enum pipe pipe = intel_crtc->pipe;
  3427. int palreg = PALETTE(pipe);
  3428. int i;
  3429. bool reenable_ips = false;
  3430. /* The clocks have to be on to load the palette. */
  3431. if (!crtc->enabled || !intel_crtc->active)
  3432. return;
  3433. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3434. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  3435. assert_dsi_pll_enabled(dev_priv);
  3436. else
  3437. assert_pll_enabled(dev_priv, pipe);
  3438. }
  3439. /* use legacy palette for Ironlake */
  3440. if (!HAS_GMCH_DISPLAY(dev))
  3441. palreg = LGC_PALETTE(pipe);
  3442. /* Workaround : Do not read or write the pipe palette/gamma data while
  3443. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3444. */
  3445. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3446. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3447. GAMMA_MODE_MODE_SPLIT)) {
  3448. hsw_disable_ips(intel_crtc);
  3449. reenable_ips = true;
  3450. }
  3451. for (i = 0; i < 256; i++) {
  3452. I915_WRITE(palreg + 4 * i,
  3453. (intel_crtc->lut_r[i] << 16) |
  3454. (intel_crtc->lut_g[i] << 8) |
  3455. intel_crtc->lut_b[i]);
  3456. }
  3457. if (reenable_ips)
  3458. hsw_enable_ips(intel_crtc);
  3459. }
  3460. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3461. {
  3462. if (!enable && intel_crtc->overlay) {
  3463. struct drm_device *dev = intel_crtc->base.dev;
  3464. struct drm_i915_private *dev_priv = dev->dev_private;
  3465. mutex_lock(&dev->struct_mutex);
  3466. dev_priv->mm.interruptible = false;
  3467. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3468. dev_priv->mm.interruptible = true;
  3469. mutex_unlock(&dev->struct_mutex);
  3470. }
  3471. /* Let userspace switch the overlay on again. In most cases userspace
  3472. * has to recompute where to put it anyway.
  3473. */
  3474. }
  3475. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3476. {
  3477. struct drm_device *dev = crtc->dev;
  3478. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3479. int pipe = intel_crtc->pipe;
  3480. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3481. intel_enable_planes(crtc);
  3482. intel_crtc_update_cursor(crtc, true);
  3483. intel_crtc_dpms_overlay(intel_crtc, true);
  3484. hsw_enable_ips(intel_crtc);
  3485. mutex_lock(&dev->struct_mutex);
  3486. intel_update_fbc(dev);
  3487. mutex_unlock(&dev->struct_mutex);
  3488. /*
  3489. * FIXME: Once we grow proper nuclear flip support out of this we need
  3490. * to compute the mask of flip planes precisely. For the time being
  3491. * consider this a flip from a NULL plane.
  3492. */
  3493. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3494. }
  3495. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3496. {
  3497. struct drm_device *dev = crtc->dev;
  3498. struct drm_i915_private *dev_priv = dev->dev_private;
  3499. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3500. int pipe = intel_crtc->pipe;
  3501. int plane = intel_crtc->plane;
  3502. intel_crtc_wait_for_pending_flips(crtc);
  3503. if (dev_priv->fbc.plane == plane)
  3504. intel_disable_fbc(dev);
  3505. hsw_disable_ips(intel_crtc);
  3506. intel_crtc_dpms_overlay(intel_crtc, false);
  3507. intel_crtc_update_cursor(crtc, false);
  3508. intel_disable_planes(crtc);
  3509. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3510. /*
  3511. * FIXME: Once we grow proper nuclear flip support out of this we need
  3512. * to compute the mask of flip planes precisely. For the time being
  3513. * consider this a flip to a NULL plane.
  3514. */
  3515. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3516. }
  3517. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3518. {
  3519. struct drm_device *dev = crtc->dev;
  3520. struct drm_i915_private *dev_priv = dev->dev_private;
  3521. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3522. struct intel_encoder *encoder;
  3523. int pipe = intel_crtc->pipe;
  3524. WARN_ON(!crtc->enabled);
  3525. if (intel_crtc->active)
  3526. return;
  3527. if (intel_crtc->config.has_pch_encoder)
  3528. intel_prepare_shared_dpll(intel_crtc);
  3529. if (intel_crtc->config.has_dp_encoder)
  3530. intel_dp_set_m_n(intel_crtc);
  3531. intel_set_pipe_timings(intel_crtc);
  3532. if (intel_crtc->config.has_pch_encoder) {
  3533. intel_cpu_transcoder_set_m_n(intel_crtc,
  3534. &intel_crtc->config.fdi_m_n, NULL);
  3535. }
  3536. ironlake_set_pipeconf(crtc);
  3537. intel_crtc->active = true;
  3538. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3539. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3540. for_each_encoder_on_crtc(dev, crtc, encoder)
  3541. if (encoder->pre_enable)
  3542. encoder->pre_enable(encoder);
  3543. if (intel_crtc->config.has_pch_encoder) {
  3544. /* Note: FDI PLL enabling _must_ be done before we enable the
  3545. * cpu pipes, hence this is separate from all the other fdi/pch
  3546. * enabling. */
  3547. ironlake_fdi_pll_enable(intel_crtc);
  3548. } else {
  3549. assert_fdi_tx_disabled(dev_priv, pipe);
  3550. assert_fdi_rx_disabled(dev_priv, pipe);
  3551. }
  3552. ironlake_pfit_enable(intel_crtc);
  3553. /*
  3554. * On ILK+ LUT must be loaded before the pipe is running but with
  3555. * clocks enabled
  3556. */
  3557. intel_crtc_load_lut(crtc);
  3558. intel_update_watermarks(crtc);
  3559. intel_enable_pipe(intel_crtc);
  3560. if (intel_crtc->config.has_pch_encoder)
  3561. ironlake_pch_enable(crtc);
  3562. for_each_encoder_on_crtc(dev, crtc, encoder)
  3563. encoder->enable(encoder);
  3564. if (HAS_PCH_CPT(dev))
  3565. cpt_verify_modeset(dev, intel_crtc->pipe);
  3566. assert_vblank_disabled(crtc);
  3567. drm_crtc_vblank_on(crtc);
  3568. intel_crtc_enable_planes(crtc);
  3569. }
  3570. /* IPS only exists on ULT machines and is tied to pipe A. */
  3571. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3572. {
  3573. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3574. }
  3575. /*
  3576. * This implements the workaround described in the "notes" section of the mode
  3577. * set sequence documentation. When going from no pipes or single pipe to
  3578. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3579. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3580. */
  3581. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3582. {
  3583. struct drm_device *dev = crtc->base.dev;
  3584. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3585. /* We want to get the other_active_crtc only if there's only 1 other
  3586. * active crtc. */
  3587. for_each_intel_crtc(dev, crtc_it) {
  3588. if (!crtc_it->active || crtc_it == crtc)
  3589. continue;
  3590. if (other_active_crtc)
  3591. return;
  3592. other_active_crtc = crtc_it;
  3593. }
  3594. if (!other_active_crtc)
  3595. return;
  3596. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3597. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3598. }
  3599. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3600. {
  3601. struct drm_device *dev = crtc->dev;
  3602. struct drm_i915_private *dev_priv = dev->dev_private;
  3603. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3604. struct intel_encoder *encoder;
  3605. int pipe = intel_crtc->pipe;
  3606. WARN_ON(!crtc->enabled);
  3607. if (intel_crtc->active)
  3608. return;
  3609. if (intel_crtc_to_shared_dpll(intel_crtc))
  3610. intel_enable_shared_dpll(intel_crtc);
  3611. if (intel_crtc->config.has_dp_encoder)
  3612. intel_dp_set_m_n(intel_crtc);
  3613. intel_set_pipe_timings(intel_crtc);
  3614. if (intel_crtc->config.cpu_transcoder != TRANSCODER_EDP) {
  3615. I915_WRITE(PIPE_MULT(intel_crtc->config.cpu_transcoder),
  3616. intel_crtc->config.pixel_multiplier - 1);
  3617. }
  3618. if (intel_crtc->config.has_pch_encoder) {
  3619. intel_cpu_transcoder_set_m_n(intel_crtc,
  3620. &intel_crtc->config.fdi_m_n, NULL);
  3621. }
  3622. haswell_set_pipeconf(crtc);
  3623. intel_set_pipe_csc(crtc);
  3624. intel_crtc->active = true;
  3625. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3626. for_each_encoder_on_crtc(dev, crtc, encoder)
  3627. if (encoder->pre_enable)
  3628. encoder->pre_enable(encoder);
  3629. if (intel_crtc->config.has_pch_encoder) {
  3630. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3631. true);
  3632. dev_priv->display.fdi_link_train(crtc);
  3633. }
  3634. intel_ddi_enable_pipe_clock(intel_crtc);
  3635. ironlake_pfit_enable(intel_crtc);
  3636. /*
  3637. * On ILK+ LUT must be loaded before the pipe is running but with
  3638. * clocks enabled
  3639. */
  3640. intel_crtc_load_lut(crtc);
  3641. intel_ddi_set_pipe_settings(crtc);
  3642. intel_ddi_enable_transcoder_func(crtc);
  3643. intel_update_watermarks(crtc);
  3644. intel_enable_pipe(intel_crtc);
  3645. if (intel_crtc->config.has_pch_encoder)
  3646. lpt_pch_enable(crtc);
  3647. if (intel_crtc->config.dp_encoder_is_mst)
  3648. intel_ddi_set_vc_payload_alloc(crtc, true);
  3649. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3650. encoder->enable(encoder);
  3651. intel_opregion_notify_encoder(encoder, true);
  3652. }
  3653. assert_vblank_disabled(crtc);
  3654. drm_crtc_vblank_on(crtc);
  3655. /* If we change the relative order between pipe/planes enabling, we need
  3656. * to change the workaround. */
  3657. haswell_mode_set_planes_workaround(intel_crtc);
  3658. intel_crtc_enable_planes(crtc);
  3659. }
  3660. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3661. {
  3662. struct drm_device *dev = crtc->base.dev;
  3663. struct drm_i915_private *dev_priv = dev->dev_private;
  3664. int pipe = crtc->pipe;
  3665. /* To avoid upsetting the power well on haswell only disable the pfit if
  3666. * it's in use. The hw state code will make sure we get this right. */
  3667. if (crtc->config.pch_pfit.enabled) {
  3668. I915_WRITE(PF_CTL(pipe), 0);
  3669. I915_WRITE(PF_WIN_POS(pipe), 0);
  3670. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3671. }
  3672. }
  3673. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3674. {
  3675. struct drm_device *dev = crtc->dev;
  3676. struct drm_i915_private *dev_priv = dev->dev_private;
  3677. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3678. struct intel_encoder *encoder;
  3679. int pipe = intel_crtc->pipe;
  3680. u32 reg, temp;
  3681. if (!intel_crtc->active)
  3682. return;
  3683. intel_crtc_disable_planes(crtc);
  3684. drm_crtc_vblank_off(crtc);
  3685. assert_vblank_disabled(crtc);
  3686. for_each_encoder_on_crtc(dev, crtc, encoder)
  3687. encoder->disable(encoder);
  3688. if (intel_crtc->config.has_pch_encoder)
  3689. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  3690. intel_disable_pipe(intel_crtc);
  3691. ironlake_pfit_disable(intel_crtc);
  3692. for_each_encoder_on_crtc(dev, crtc, encoder)
  3693. if (encoder->post_disable)
  3694. encoder->post_disable(encoder);
  3695. if (intel_crtc->config.has_pch_encoder) {
  3696. ironlake_fdi_disable(crtc);
  3697. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3698. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3699. if (HAS_PCH_CPT(dev)) {
  3700. /* disable TRANS_DP_CTL */
  3701. reg = TRANS_DP_CTL(pipe);
  3702. temp = I915_READ(reg);
  3703. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3704. TRANS_DP_PORT_SEL_MASK);
  3705. temp |= TRANS_DP_PORT_SEL_NONE;
  3706. I915_WRITE(reg, temp);
  3707. /* disable DPLL_SEL */
  3708. temp = I915_READ(PCH_DPLL_SEL);
  3709. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3710. I915_WRITE(PCH_DPLL_SEL, temp);
  3711. }
  3712. /* disable PCH DPLL */
  3713. intel_disable_shared_dpll(intel_crtc);
  3714. ironlake_fdi_pll_disable(intel_crtc);
  3715. }
  3716. intel_crtc->active = false;
  3717. intel_update_watermarks(crtc);
  3718. mutex_lock(&dev->struct_mutex);
  3719. intel_update_fbc(dev);
  3720. mutex_unlock(&dev->struct_mutex);
  3721. }
  3722. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3723. {
  3724. struct drm_device *dev = crtc->dev;
  3725. struct drm_i915_private *dev_priv = dev->dev_private;
  3726. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3727. struct intel_encoder *encoder;
  3728. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3729. if (!intel_crtc->active)
  3730. return;
  3731. intel_crtc_disable_planes(crtc);
  3732. drm_crtc_vblank_off(crtc);
  3733. assert_vblank_disabled(crtc);
  3734. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3735. intel_opregion_notify_encoder(encoder, false);
  3736. encoder->disable(encoder);
  3737. }
  3738. if (intel_crtc->config.has_pch_encoder)
  3739. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3740. false);
  3741. intel_disable_pipe(intel_crtc);
  3742. if (intel_crtc->config.dp_encoder_is_mst)
  3743. intel_ddi_set_vc_payload_alloc(crtc, false);
  3744. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3745. ironlake_pfit_disable(intel_crtc);
  3746. intel_ddi_disable_pipe_clock(intel_crtc);
  3747. if (intel_crtc->config.has_pch_encoder) {
  3748. lpt_disable_pch_transcoder(dev_priv);
  3749. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3750. true);
  3751. intel_ddi_fdi_disable(crtc);
  3752. }
  3753. for_each_encoder_on_crtc(dev, crtc, encoder)
  3754. if (encoder->post_disable)
  3755. encoder->post_disable(encoder);
  3756. intel_crtc->active = false;
  3757. intel_update_watermarks(crtc);
  3758. mutex_lock(&dev->struct_mutex);
  3759. intel_update_fbc(dev);
  3760. mutex_unlock(&dev->struct_mutex);
  3761. if (intel_crtc_to_shared_dpll(intel_crtc))
  3762. intel_disable_shared_dpll(intel_crtc);
  3763. }
  3764. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3765. {
  3766. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3767. intel_put_shared_dpll(intel_crtc);
  3768. }
  3769. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3770. {
  3771. struct drm_device *dev = crtc->base.dev;
  3772. struct drm_i915_private *dev_priv = dev->dev_private;
  3773. struct intel_crtc_config *pipe_config = &crtc->config;
  3774. if (!crtc->config.gmch_pfit.control)
  3775. return;
  3776. /*
  3777. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3778. * according to register description and PRM.
  3779. */
  3780. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3781. assert_pipe_disabled(dev_priv, crtc->pipe);
  3782. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3783. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3784. /* Border color in case we don't scale up to the full screen. Black by
  3785. * default, change to something else for debugging. */
  3786. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3787. }
  3788. static enum intel_display_power_domain port_to_power_domain(enum port port)
  3789. {
  3790. switch (port) {
  3791. case PORT_A:
  3792. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3793. case PORT_B:
  3794. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3795. case PORT_C:
  3796. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3797. case PORT_D:
  3798. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3799. default:
  3800. WARN_ON_ONCE(1);
  3801. return POWER_DOMAIN_PORT_OTHER;
  3802. }
  3803. }
  3804. #define for_each_power_domain(domain, mask) \
  3805. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3806. if ((1 << (domain)) & (mask))
  3807. enum intel_display_power_domain
  3808. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3809. {
  3810. struct drm_device *dev = intel_encoder->base.dev;
  3811. struct intel_digital_port *intel_dig_port;
  3812. switch (intel_encoder->type) {
  3813. case INTEL_OUTPUT_UNKNOWN:
  3814. /* Only DDI platforms should ever use this output type */
  3815. WARN_ON_ONCE(!HAS_DDI(dev));
  3816. case INTEL_OUTPUT_DISPLAYPORT:
  3817. case INTEL_OUTPUT_HDMI:
  3818. case INTEL_OUTPUT_EDP:
  3819. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3820. return port_to_power_domain(intel_dig_port->port);
  3821. case INTEL_OUTPUT_DP_MST:
  3822. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  3823. return port_to_power_domain(intel_dig_port->port);
  3824. case INTEL_OUTPUT_ANALOG:
  3825. return POWER_DOMAIN_PORT_CRT;
  3826. case INTEL_OUTPUT_DSI:
  3827. return POWER_DOMAIN_PORT_DSI;
  3828. default:
  3829. return POWER_DOMAIN_PORT_OTHER;
  3830. }
  3831. }
  3832. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3833. {
  3834. struct drm_device *dev = crtc->dev;
  3835. struct intel_encoder *intel_encoder;
  3836. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3837. enum pipe pipe = intel_crtc->pipe;
  3838. unsigned long mask;
  3839. enum transcoder transcoder;
  3840. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3841. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3842. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3843. if (intel_crtc->config.pch_pfit.enabled ||
  3844. intel_crtc->config.pch_pfit.force_thru)
  3845. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3846. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3847. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3848. return mask;
  3849. }
  3850. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3851. {
  3852. struct drm_i915_private *dev_priv = dev->dev_private;
  3853. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3854. struct intel_crtc *crtc;
  3855. /*
  3856. * First get all needed power domains, then put all unneeded, to avoid
  3857. * any unnecessary toggling of the power wells.
  3858. */
  3859. for_each_intel_crtc(dev, crtc) {
  3860. enum intel_display_power_domain domain;
  3861. if (!crtc->base.enabled)
  3862. continue;
  3863. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3864. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3865. intel_display_power_get(dev_priv, domain);
  3866. }
  3867. for_each_intel_crtc(dev, crtc) {
  3868. enum intel_display_power_domain domain;
  3869. for_each_power_domain(domain, crtc->enabled_power_domains)
  3870. intel_display_power_put(dev_priv, domain);
  3871. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  3872. }
  3873. intel_display_set_init_power(dev_priv, false);
  3874. }
  3875. /* returns HPLL frequency in kHz */
  3876. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  3877. {
  3878. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  3879. /* Obtain SKU information */
  3880. mutex_lock(&dev_priv->dpio_lock);
  3881. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  3882. CCK_FUSE_HPLL_FREQ_MASK;
  3883. mutex_unlock(&dev_priv->dpio_lock);
  3884. return vco_freq[hpll_freq] * 1000;
  3885. }
  3886. static void vlv_update_cdclk(struct drm_device *dev)
  3887. {
  3888. struct drm_i915_private *dev_priv = dev->dev_private;
  3889. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  3890. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz",
  3891. dev_priv->vlv_cdclk_freq);
  3892. /*
  3893. * Program the gmbus_freq based on the cdclk frequency.
  3894. * BSpec erroneously claims we should aim for 4MHz, but
  3895. * in fact 1MHz is the correct frequency.
  3896. */
  3897. I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
  3898. }
  3899. /* Adjust CDclk dividers to allow high res or save power if possible */
  3900. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  3901. {
  3902. struct drm_i915_private *dev_priv = dev->dev_private;
  3903. u32 val, cmd;
  3904. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  3905. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  3906. cmd = 2;
  3907. else if (cdclk == 266667)
  3908. cmd = 1;
  3909. else
  3910. cmd = 0;
  3911. mutex_lock(&dev_priv->rps.hw_lock);
  3912. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3913. val &= ~DSPFREQGUAR_MASK;
  3914. val |= (cmd << DSPFREQGUAR_SHIFT);
  3915. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3916. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3917. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  3918. 50)) {
  3919. DRM_ERROR("timed out waiting for CDclk change\n");
  3920. }
  3921. mutex_unlock(&dev_priv->rps.hw_lock);
  3922. if (cdclk == 400000) {
  3923. u32 divider, vco;
  3924. vco = valleyview_get_vco(dev_priv);
  3925. divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
  3926. mutex_lock(&dev_priv->dpio_lock);
  3927. /* adjust cdclk divider */
  3928. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3929. val &= ~DISPLAY_FREQUENCY_VALUES;
  3930. val |= divider;
  3931. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  3932. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  3933. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  3934. 50))
  3935. DRM_ERROR("timed out waiting for CDclk change\n");
  3936. mutex_unlock(&dev_priv->dpio_lock);
  3937. }
  3938. mutex_lock(&dev_priv->dpio_lock);
  3939. /* adjust self-refresh exit latency value */
  3940. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  3941. val &= ~0x7f;
  3942. /*
  3943. * For high bandwidth configs, we set a higher latency in the bunit
  3944. * so that the core display fetch happens in time to avoid underruns.
  3945. */
  3946. if (cdclk == 400000)
  3947. val |= 4500 / 250; /* 4.5 usec */
  3948. else
  3949. val |= 3000 / 250; /* 3.0 usec */
  3950. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  3951. mutex_unlock(&dev_priv->dpio_lock);
  3952. vlv_update_cdclk(dev);
  3953. }
  3954. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  3955. {
  3956. struct drm_i915_private *dev_priv = dev->dev_private;
  3957. u32 val, cmd;
  3958. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  3959. switch (cdclk) {
  3960. case 400000:
  3961. cmd = 3;
  3962. break;
  3963. case 333333:
  3964. case 320000:
  3965. cmd = 2;
  3966. break;
  3967. case 266667:
  3968. cmd = 1;
  3969. break;
  3970. case 200000:
  3971. cmd = 0;
  3972. break;
  3973. default:
  3974. WARN_ON(1);
  3975. return;
  3976. }
  3977. mutex_lock(&dev_priv->rps.hw_lock);
  3978. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3979. val &= ~DSPFREQGUAR_MASK_CHV;
  3980. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  3981. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3982. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3983. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  3984. 50)) {
  3985. DRM_ERROR("timed out waiting for CDclk change\n");
  3986. }
  3987. mutex_unlock(&dev_priv->rps.hw_lock);
  3988. vlv_update_cdclk(dev);
  3989. }
  3990. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  3991. int max_pixclk)
  3992. {
  3993. int vco = valleyview_get_vco(dev_priv);
  3994. int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
  3995. /* FIXME: Punit isn't quite ready yet */
  3996. if (IS_CHERRYVIEW(dev_priv->dev))
  3997. return 400000;
  3998. /*
  3999. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4000. * 200MHz
  4001. * 267MHz
  4002. * 320/333MHz (depends on HPLL freq)
  4003. * 400MHz
  4004. * So we check to see whether we're above 90% of the lower bin and
  4005. * adjust if needed.
  4006. *
  4007. * We seem to get an unstable or solid color picture at 200MHz.
  4008. * Not sure what's wrong. For now use 200MHz only when all pipes
  4009. * are off.
  4010. */
  4011. if (max_pixclk > freq_320*9/10)
  4012. return 400000;
  4013. else if (max_pixclk > 266667*9/10)
  4014. return freq_320;
  4015. else if (max_pixclk > 0)
  4016. return 266667;
  4017. else
  4018. return 200000;
  4019. }
  4020. /* compute the max pixel clock for new configuration */
  4021. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  4022. {
  4023. struct drm_device *dev = dev_priv->dev;
  4024. struct intel_crtc *intel_crtc;
  4025. int max_pixclk = 0;
  4026. for_each_intel_crtc(dev, intel_crtc) {
  4027. if (intel_crtc->new_enabled)
  4028. max_pixclk = max(max_pixclk,
  4029. intel_crtc->new_config->adjusted_mode.crtc_clock);
  4030. }
  4031. return max_pixclk;
  4032. }
  4033. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  4034. unsigned *prepare_pipes)
  4035. {
  4036. struct drm_i915_private *dev_priv = dev->dev_private;
  4037. struct intel_crtc *intel_crtc;
  4038. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4039. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  4040. dev_priv->vlv_cdclk_freq)
  4041. return;
  4042. /* disable/enable all currently active pipes while we change cdclk */
  4043. for_each_intel_crtc(dev, intel_crtc)
  4044. if (intel_crtc->base.enabled)
  4045. *prepare_pipes |= (1 << intel_crtc->pipe);
  4046. }
  4047. static void valleyview_modeset_global_resources(struct drm_device *dev)
  4048. {
  4049. struct drm_i915_private *dev_priv = dev->dev_private;
  4050. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4051. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4052. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  4053. if (IS_CHERRYVIEW(dev))
  4054. cherryview_set_cdclk(dev, req_cdclk);
  4055. else
  4056. valleyview_set_cdclk(dev, req_cdclk);
  4057. }
  4058. modeset_update_crtc_power_domains(dev);
  4059. }
  4060. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4061. {
  4062. struct drm_device *dev = crtc->dev;
  4063. struct drm_i915_private *dev_priv = to_i915(dev);
  4064. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4065. struct intel_encoder *encoder;
  4066. int pipe = intel_crtc->pipe;
  4067. bool is_dsi;
  4068. WARN_ON(!crtc->enabled);
  4069. if (intel_crtc->active)
  4070. return;
  4071. is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
  4072. if (!is_dsi) {
  4073. if (IS_CHERRYVIEW(dev))
  4074. chv_prepare_pll(intel_crtc);
  4075. else
  4076. vlv_prepare_pll(intel_crtc);
  4077. }
  4078. if (intel_crtc->config.has_dp_encoder)
  4079. intel_dp_set_m_n(intel_crtc);
  4080. intel_set_pipe_timings(intel_crtc);
  4081. i9xx_set_pipeconf(intel_crtc);
  4082. intel_crtc->active = true;
  4083. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4084. for_each_encoder_on_crtc(dev, crtc, encoder)
  4085. if (encoder->pre_pll_enable)
  4086. encoder->pre_pll_enable(encoder);
  4087. if (!is_dsi) {
  4088. if (IS_CHERRYVIEW(dev))
  4089. chv_enable_pll(intel_crtc);
  4090. else
  4091. vlv_enable_pll(intel_crtc);
  4092. }
  4093. for_each_encoder_on_crtc(dev, crtc, encoder)
  4094. if (encoder->pre_enable)
  4095. encoder->pre_enable(encoder);
  4096. i9xx_pfit_enable(intel_crtc);
  4097. intel_crtc_load_lut(crtc);
  4098. intel_update_watermarks(crtc);
  4099. intel_enable_pipe(intel_crtc);
  4100. for_each_encoder_on_crtc(dev, crtc, encoder)
  4101. encoder->enable(encoder);
  4102. assert_vblank_disabled(crtc);
  4103. drm_crtc_vblank_on(crtc);
  4104. intel_crtc_enable_planes(crtc);
  4105. /* Underruns don't raise interrupts, so check manually. */
  4106. i9xx_check_fifo_underruns(dev_priv);
  4107. }
  4108. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4109. {
  4110. struct drm_device *dev = crtc->base.dev;
  4111. struct drm_i915_private *dev_priv = dev->dev_private;
  4112. I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
  4113. I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
  4114. }
  4115. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4116. {
  4117. struct drm_device *dev = crtc->dev;
  4118. struct drm_i915_private *dev_priv = to_i915(dev);
  4119. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4120. struct intel_encoder *encoder;
  4121. int pipe = intel_crtc->pipe;
  4122. WARN_ON(!crtc->enabled);
  4123. if (intel_crtc->active)
  4124. return;
  4125. i9xx_set_pll_dividers(intel_crtc);
  4126. if (intel_crtc->config.has_dp_encoder)
  4127. intel_dp_set_m_n(intel_crtc);
  4128. intel_set_pipe_timings(intel_crtc);
  4129. i9xx_set_pipeconf(intel_crtc);
  4130. intel_crtc->active = true;
  4131. if (!IS_GEN2(dev))
  4132. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4133. for_each_encoder_on_crtc(dev, crtc, encoder)
  4134. if (encoder->pre_enable)
  4135. encoder->pre_enable(encoder);
  4136. i9xx_enable_pll(intel_crtc);
  4137. i9xx_pfit_enable(intel_crtc);
  4138. intel_crtc_load_lut(crtc);
  4139. intel_update_watermarks(crtc);
  4140. intel_enable_pipe(intel_crtc);
  4141. for_each_encoder_on_crtc(dev, crtc, encoder)
  4142. encoder->enable(encoder);
  4143. assert_vblank_disabled(crtc);
  4144. drm_crtc_vblank_on(crtc);
  4145. intel_crtc_enable_planes(crtc);
  4146. /*
  4147. * Gen2 reports pipe underruns whenever all planes are disabled.
  4148. * So don't enable underrun reporting before at least some planes
  4149. * are enabled.
  4150. * FIXME: Need to fix the logic to work when we turn off all planes
  4151. * but leave the pipe running.
  4152. */
  4153. if (IS_GEN2(dev))
  4154. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4155. /* Underruns don't raise interrupts, so check manually. */
  4156. i9xx_check_fifo_underruns(dev_priv);
  4157. }
  4158. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4159. {
  4160. struct drm_device *dev = crtc->base.dev;
  4161. struct drm_i915_private *dev_priv = dev->dev_private;
  4162. if (!crtc->config.gmch_pfit.control)
  4163. return;
  4164. assert_pipe_disabled(dev_priv, crtc->pipe);
  4165. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4166. I915_READ(PFIT_CONTROL));
  4167. I915_WRITE(PFIT_CONTROL, 0);
  4168. }
  4169. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4170. {
  4171. struct drm_device *dev = crtc->dev;
  4172. struct drm_i915_private *dev_priv = dev->dev_private;
  4173. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4174. struct intel_encoder *encoder;
  4175. int pipe = intel_crtc->pipe;
  4176. if (!intel_crtc->active)
  4177. return;
  4178. /*
  4179. * Gen2 reports pipe underruns whenever all planes are disabled.
  4180. * So diasble underrun reporting before all the planes get disabled.
  4181. * FIXME: Need to fix the logic to work when we turn off all planes
  4182. * but leave the pipe running.
  4183. */
  4184. if (IS_GEN2(dev))
  4185. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4186. /*
  4187. * Vblank time updates from the shadow to live plane control register
  4188. * are blocked if the memory self-refresh mode is active at that
  4189. * moment. So to make sure the plane gets truly disabled, disable
  4190. * first the self-refresh mode. The self-refresh enable bit in turn
  4191. * will be checked/applied by the HW only at the next frame start
  4192. * event which is after the vblank start event, so we need to have a
  4193. * wait-for-vblank between disabling the plane and the pipe.
  4194. */
  4195. intel_set_memory_cxsr(dev_priv, false);
  4196. intel_crtc_disable_planes(crtc);
  4197. /*
  4198. * On gen2 planes are double buffered but the pipe isn't, so we must
  4199. * wait for planes to fully turn off before disabling the pipe.
  4200. * We also need to wait on all gmch platforms because of the
  4201. * self-refresh mode constraint explained above.
  4202. */
  4203. intel_wait_for_vblank(dev, pipe);
  4204. drm_crtc_vblank_off(crtc);
  4205. assert_vblank_disabled(crtc);
  4206. for_each_encoder_on_crtc(dev, crtc, encoder)
  4207. encoder->disable(encoder);
  4208. intel_disable_pipe(intel_crtc);
  4209. i9xx_pfit_disable(intel_crtc);
  4210. for_each_encoder_on_crtc(dev, crtc, encoder)
  4211. if (encoder->post_disable)
  4212. encoder->post_disable(encoder);
  4213. if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
  4214. if (IS_CHERRYVIEW(dev))
  4215. chv_disable_pll(dev_priv, pipe);
  4216. else if (IS_VALLEYVIEW(dev))
  4217. vlv_disable_pll(dev_priv, pipe);
  4218. else
  4219. i9xx_disable_pll(intel_crtc);
  4220. }
  4221. if (!IS_GEN2(dev))
  4222. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4223. intel_crtc->active = false;
  4224. intel_update_watermarks(crtc);
  4225. mutex_lock(&dev->struct_mutex);
  4226. intel_update_fbc(dev);
  4227. mutex_unlock(&dev->struct_mutex);
  4228. }
  4229. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4230. {
  4231. }
  4232. static void intel_crtc_update_sarea(struct drm_crtc *crtc,
  4233. bool enabled)
  4234. {
  4235. struct drm_device *dev = crtc->dev;
  4236. struct drm_i915_master_private *master_priv;
  4237. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4238. int pipe = intel_crtc->pipe;
  4239. if (!dev->primary->master)
  4240. return;
  4241. master_priv = dev->primary->master->driver_priv;
  4242. if (!master_priv->sarea_priv)
  4243. return;
  4244. switch (pipe) {
  4245. case 0:
  4246. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  4247. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  4248. break;
  4249. case 1:
  4250. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  4251. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  4252. break;
  4253. default:
  4254. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  4255. break;
  4256. }
  4257. }
  4258. /* Master function to enable/disable CRTC and corresponding power wells */
  4259. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4260. {
  4261. struct drm_device *dev = crtc->dev;
  4262. struct drm_i915_private *dev_priv = dev->dev_private;
  4263. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4264. enum intel_display_power_domain domain;
  4265. unsigned long domains;
  4266. if (enable) {
  4267. if (!intel_crtc->active) {
  4268. domains = get_crtc_power_domains(crtc);
  4269. for_each_power_domain(domain, domains)
  4270. intel_display_power_get(dev_priv, domain);
  4271. intel_crtc->enabled_power_domains = domains;
  4272. dev_priv->display.crtc_enable(crtc);
  4273. }
  4274. } else {
  4275. if (intel_crtc->active) {
  4276. dev_priv->display.crtc_disable(crtc);
  4277. domains = intel_crtc->enabled_power_domains;
  4278. for_each_power_domain(domain, domains)
  4279. intel_display_power_put(dev_priv, domain);
  4280. intel_crtc->enabled_power_domains = 0;
  4281. }
  4282. }
  4283. }
  4284. /**
  4285. * Sets the power management mode of the pipe and plane.
  4286. */
  4287. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4288. {
  4289. struct drm_device *dev = crtc->dev;
  4290. struct intel_encoder *intel_encoder;
  4291. bool enable = false;
  4292. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4293. enable |= intel_encoder->connectors_active;
  4294. intel_crtc_control(crtc, enable);
  4295. intel_crtc_update_sarea(crtc, enable);
  4296. }
  4297. static void intel_crtc_disable(struct drm_crtc *crtc)
  4298. {
  4299. struct drm_device *dev = crtc->dev;
  4300. struct drm_connector *connector;
  4301. struct drm_i915_private *dev_priv = dev->dev_private;
  4302. struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
  4303. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  4304. /* crtc should still be enabled when we disable it. */
  4305. WARN_ON(!crtc->enabled);
  4306. dev_priv->display.crtc_disable(crtc);
  4307. intel_crtc_update_sarea(crtc, false);
  4308. dev_priv->display.off(crtc);
  4309. if (crtc->primary->fb) {
  4310. mutex_lock(&dev->struct_mutex);
  4311. intel_unpin_fb_obj(old_obj);
  4312. i915_gem_track_fb(old_obj, NULL,
  4313. INTEL_FRONTBUFFER_PRIMARY(pipe));
  4314. mutex_unlock(&dev->struct_mutex);
  4315. crtc->primary->fb = NULL;
  4316. }
  4317. /* Update computed state. */
  4318. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4319. if (!connector->encoder || !connector->encoder->crtc)
  4320. continue;
  4321. if (connector->encoder->crtc != crtc)
  4322. continue;
  4323. connector->dpms = DRM_MODE_DPMS_OFF;
  4324. to_intel_encoder(connector->encoder)->connectors_active = false;
  4325. }
  4326. }
  4327. void intel_encoder_destroy(struct drm_encoder *encoder)
  4328. {
  4329. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4330. drm_encoder_cleanup(encoder);
  4331. kfree(intel_encoder);
  4332. }
  4333. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4334. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4335. * state of the entire output pipe. */
  4336. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4337. {
  4338. if (mode == DRM_MODE_DPMS_ON) {
  4339. encoder->connectors_active = true;
  4340. intel_crtc_update_dpms(encoder->base.crtc);
  4341. } else {
  4342. encoder->connectors_active = false;
  4343. intel_crtc_update_dpms(encoder->base.crtc);
  4344. }
  4345. }
  4346. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4347. * internal consistency). */
  4348. static void intel_connector_check_state(struct intel_connector *connector)
  4349. {
  4350. if (connector->get_hw_state(connector)) {
  4351. struct intel_encoder *encoder = connector->encoder;
  4352. struct drm_crtc *crtc;
  4353. bool encoder_enabled;
  4354. enum pipe pipe;
  4355. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4356. connector->base.base.id,
  4357. connector->base.name);
  4358. /* there is no real hw state for MST connectors */
  4359. if (connector->mst_port)
  4360. return;
  4361. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4362. "wrong connector dpms state\n");
  4363. WARN(connector->base.encoder != &encoder->base,
  4364. "active connector not linked to encoder\n");
  4365. if (encoder) {
  4366. WARN(!encoder->connectors_active,
  4367. "encoder->connectors_active not set\n");
  4368. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4369. WARN(!encoder_enabled, "encoder not enabled\n");
  4370. if (WARN_ON(!encoder->base.crtc))
  4371. return;
  4372. crtc = encoder->base.crtc;
  4373. WARN(!crtc->enabled, "crtc not enabled\n");
  4374. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4375. WARN(pipe != to_intel_crtc(crtc)->pipe,
  4376. "encoder active on the wrong pipe\n");
  4377. }
  4378. }
  4379. }
  4380. /* Even simpler default implementation, if there's really no special case to
  4381. * consider. */
  4382. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4383. {
  4384. /* All the simple cases only support two dpms states. */
  4385. if (mode != DRM_MODE_DPMS_ON)
  4386. mode = DRM_MODE_DPMS_OFF;
  4387. if (mode == connector->dpms)
  4388. return;
  4389. connector->dpms = mode;
  4390. /* Only need to change hw state when actually enabled */
  4391. if (connector->encoder)
  4392. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4393. intel_modeset_check_state(connector->dev);
  4394. }
  4395. /* Simple connector->get_hw_state implementation for encoders that support only
  4396. * one connector and no cloning and hence the encoder state determines the state
  4397. * of the connector. */
  4398. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4399. {
  4400. enum pipe pipe = 0;
  4401. struct intel_encoder *encoder = connector->encoder;
  4402. return encoder->get_hw_state(encoder, &pipe);
  4403. }
  4404. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4405. struct intel_crtc_config *pipe_config)
  4406. {
  4407. struct drm_i915_private *dev_priv = dev->dev_private;
  4408. struct intel_crtc *pipe_B_crtc =
  4409. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4410. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4411. pipe_name(pipe), pipe_config->fdi_lanes);
  4412. if (pipe_config->fdi_lanes > 4) {
  4413. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4414. pipe_name(pipe), pipe_config->fdi_lanes);
  4415. return false;
  4416. }
  4417. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4418. if (pipe_config->fdi_lanes > 2) {
  4419. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4420. pipe_config->fdi_lanes);
  4421. return false;
  4422. } else {
  4423. return true;
  4424. }
  4425. }
  4426. if (INTEL_INFO(dev)->num_pipes == 2)
  4427. return true;
  4428. /* Ivybridge 3 pipe is really complicated */
  4429. switch (pipe) {
  4430. case PIPE_A:
  4431. return true;
  4432. case PIPE_B:
  4433. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4434. pipe_config->fdi_lanes > 2) {
  4435. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4436. pipe_name(pipe), pipe_config->fdi_lanes);
  4437. return false;
  4438. }
  4439. return true;
  4440. case PIPE_C:
  4441. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4442. pipe_B_crtc->config.fdi_lanes <= 2) {
  4443. if (pipe_config->fdi_lanes > 2) {
  4444. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4445. pipe_name(pipe), pipe_config->fdi_lanes);
  4446. return false;
  4447. }
  4448. } else {
  4449. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4450. return false;
  4451. }
  4452. return true;
  4453. default:
  4454. BUG();
  4455. }
  4456. }
  4457. #define RETRY 1
  4458. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4459. struct intel_crtc_config *pipe_config)
  4460. {
  4461. struct drm_device *dev = intel_crtc->base.dev;
  4462. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4463. int lane, link_bw, fdi_dotclock;
  4464. bool setup_ok, needs_recompute = false;
  4465. retry:
  4466. /* FDI is a binary signal running at ~2.7GHz, encoding
  4467. * each output octet as 10 bits. The actual frequency
  4468. * is stored as a divider into a 100MHz clock, and the
  4469. * mode pixel clock is stored in units of 1KHz.
  4470. * Hence the bw of each lane in terms of the mode signal
  4471. * is:
  4472. */
  4473. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4474. fdi_dotclock = adjusted_mode->crtc_clock;
  4475. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4476. pipe_config->pipe_bpp);
  4477. pipe_config->fdi_lanes = lane;
  4478. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4479. link_bw, &pipe_config->fdi_m_n);
  4480. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4481. intel_crtc->pipe, pipe_config);
  4482. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4483. pipe_config->pipe_bpp -= 2*3;
  4484. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4485. pipe_config->pipe_bpp);
  4486. needs_recompute = true;
  4487. pipe_config->bw_constrained = true;
  4488. goto retry;
  4489. }
  4490. if (needs_recompute)
  4491. return RETRY;
  4492. return setup_ok ? 0 : -EINVAL;
  4493. }
  4494. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4495. struct intel_crtc_config *pipe_config)
  4496. {
  4497. pipe_config->ips_enabled = i915.enable_ips &&
  4498. hsw_crtc_supports_ips(crtc) &&
  4499. pipe_config->pipe_bpp <= 24;
  4500. }
  4501. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4502. struct intel_crtc_config *pipe_config)
  4503. {
  4504. struct drm_device *dev = crtc->base.dev;
  4505. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4506. /* FIXME should check pixel clock limits on all platforms */
  4507. if (INTEL_INFO(dev)->gen < 4) {
  4508. struct drm_i915_private *dev_priv = dev->dev_private;
  4509. int clock_limit =
  4510. dev_priv->display.get_display_clock_speed(dev);
  4511. /*
  4512. * Enable pixel doubling when the dot clock
  4513. * is > 90% of the (display) core speed.
  4514. *
  4515. * GDG double wide on either pipe,
  4516. * otherwise pipe A only.
  4517. */
  4518. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4519. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4520. clock_limit *= 2;
  4521. pipe_config->double_wide = true;
  4522. }
  4523. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4524. return -EINVAL;
  4525. }
  4526. /*
  4527. * Pipe horizontal size must be even in:
  4528. * - DVO ganged mode
  4529. * - LVDS dual channel mode
  4530. * - Double wide pipe
  4531. */
  4532. if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4533. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4534. pipe_config->pipe_src_w &= ~1;
  4535. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4536. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4537. */
  4538. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4539. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4540. return -EINVAL;
  4541. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4542. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4543. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4544. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4545. * for lvds. */
  4546. pipe_config->pipe_bpp = 8*3;
  4547. }
  4548. if (HAS_IPS(dev))
  4549. hsw_compute_ips_config(crtc, pipe_config);
  4550. /*
  4551. * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
  4552. * old clock survives for now.
  4553. */
  4554. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
  4555. pipe_config->shared_dpll = crtc->config.shared_dpll;
  4556. if (pipe_config->has_pch_encoder)
  4557. return ironlake_fdi_compute_config(crtc, pipe_config);
  4558. return 0;
  4559. }
  4560. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4561. {
  4562. struct drm_i915_private *dev_priv = dev->dev_private;
  4563. int vco = valleyview_get_vco(dev_priv);
  4564. u32 val;
  4565. int divider;
  4566. /* FIXME: Punit isn't quite ready yet */
  4567. if (IS_CHERRYVIEW(dev))
  4568. return 400000;
  4569. mutex_lock(&dev_priv->dpio_lock);
  4570. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4571. mutex_unlock(&dev_priv->dpio_lock);
  4572. divider = val & DISPLAY_FREQUENCY_VALUES;
  4573. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4574. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4575. "cdclk change in progress\n");
  4576. return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
  4577. }
  4578. static int i945_get_display_clock_speed(struct drm_device *dev)
  4579. {
  4580. return 400000;
  4581. }
  4582. static int i915_get_display_clock_speed(struct drm_device *dev)
  4583. {
  4584. return 333000;
  4585. }
  4586. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4587. {
  4588. return 200000;
  4589. }
  4590. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4591. {
  4592. u16 gcfgc = 0;
  4593. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4594. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4595. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4596. return 267000;
  4597. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4598. return 333000;
  4599. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4600. return 444000;
  4601. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4602. return 200000;
  4603. default:
  4604. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4605. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4606. return 133000;
  4607. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4608. return 167000;
  4609. }
  4610. }
  4611. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4612. {
  4613. u16 gcfgc = 0;
  4614. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4615. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4616. return 133000;
  4617. else {
  4618. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4619. case GC_DISPLAY_CLOCK_333_MHZ:
  4620. return 333000;
  4621. default:
  4622. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4623. return 190000;
  4624. }
  4625. }
  4626. }
  4627. static int i865_get_display_clock_speed(struct drm_device *dev)
  4628. {
  4629. return 266000;
  4630. }
  4631. static int i855_get_display_clock_speed(struct drm_device *dev)
  4632. {
  4633. u16 hpllcc = 0;
  4634. /* Assume that the hardware is in the high speed state. This
  4635. * should be the default.
  4636. */
  4637. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4638. case GC_CLOCK_133_200:
  4639. case GC_CLOCK_100_200:
  4640. return 200000;
  4641. case GC_CLOCK_166_250:
  4642. return 250000;
  4643. case GC_CLOCK_100_133:
  4644. return 133000;
  4645. }
  4646. /* Shouldn't happen */
  4647. return 0;
  4648. }
  4649. static int i830_get_display_clock_speed(struct drm_device *dev)
  4650. {
  4651. return 133000;
  4652. }
  4653. static void
  4654. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4655. {
  4656. while (*num > DATA_LINK_M_N_MASK ||
  4657. *den > DATA_LINK_M_N_MASK) {
  4658. *num >>= 1;
  4659. *den >>= 1;
  4660. }
  4661. }
  4662. static void compute_m_n(unsigned int m, unsigned int n,
  4663. uint32_t *ret_m, uint32_t *ret_n)
  4664. {
  4665. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4666. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4667. intel_reduce_m_n_ratio(ret_m, ret_n);
  4668. }
  4669. void
  4670. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4671. int pixel_clock, int link_clock,
  4672. struct intel_link_m_n *m_n)
  4673. {
  4674. m_n->tu = 64;
  4675. compute_m_n(bits_per_pixel * pixel_clock,
  4676. link_clock * nlanes * 8,
  4677. &m_n->gmch_m, &m_n->gmch_n);
  4678. compute_m_n(pixel_clock, link_clock,
  4679. &m_n->link_m, &m_n->link_n);
  4680. }
  4681. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4682. {
  4683. if (i915.panel_use_ssc >= 0)
  4684. return i915.panel_use_ssc != 0;
  4685. return dev_priv->vbt.lvds_use_ssc
  4686. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4687. }
  4688. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  4689. {
  4690. struct drm_device *dev = crtc->dev;
  4691. struct drm_i915_private *dev_priv = dev->dev_private;
  4692. int refclk;
  4693. if (IS_VALLEYVIEW(dev)) {
  4694. refclk = 100000;
  4695. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4696. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4697. refclk = dev_priv->vbt.lvds_ssc_freq;
  4698. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4699. } else if (!IS_GEN2(dev)) {
  4700. refclk = 96000;
  4701. } else {
  4702. refclk = 48000;
  4703. }
  4704. return refclk;
  4705. }
  4706. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4707. {
  4708. return (1 << dpll->n) << 16 | dpll->m2;
  4709. }
  4710. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4711. {
  4712. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4713. }
  4714. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4715. intel_clock_t *reduced_clock)
  4716. {
  4717. struct drm_device *dev = crtc->base.dev;
  4718. u32 fp, fp2 = 0;
  4719. if (IS_PINEVIEW(dev)) {
  4720. fp = pnv_dpll_compute_fp(&crtc->config.dpll);
  4721. if (reduced_clock)
  4722. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4723. } else {
  4724. fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
  4725. if (reduced_clock)
  4726. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4727. }
  4728. crtc->config.dpll_hw_state.fp0 = fp;
  4729. crtc->lowfreq_avail = false;
  4730. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4731. reduced_clock && i915.powersave) {
  4732. crtc->config.dpll_hw_state.fp1 = fp2;
  4733. crtc->lowfreq_avail = true;
  4734. } else {
  4735. crtc->config.dpll_hw_state.fp1 = fp;
  4736. }
  4737. }
  4738. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4739. pipe)
  4740. {
  4741. u32 reg_val;
  4742. /*
  4743. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4744. * and set it to a reasonable value instead.
  4745. */
  4746. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4747. reg_val &= 0xffffff00;
  4748. reg_val |= 0x00000030;
  4749. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4750. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4751. reg_val &= 0x8cffffff;
  4752. reg_val = 0x8c000000;
  4753. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4754. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4755. reg_val &= 0xffffff00;
  4756. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4757. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4758. reg_val &= 0x00ffffff;
  4759. reg_val |= 0xb0000000;
  4760. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4761. }
  4762. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4763. struct intel_link_m_n *m_n)
  4764. {
  4765. struct drm_device *dev = crtc->base.dev;
  4766. struct drm_i915_private *dev_priv = dev->dev_private;
  4767. int pipe = crtc->pipe;
  4768. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4769. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4770. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4771. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4772. }
  4773. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4774. struct intel_link_m_n *m_n,
  4775. struct intel_link_m_n *m2_n2)
  4776. {
  4777. struct drm_device *dev = crtc->base.dev;
  4778. struct drm_i915_private *dev_priv = dev->dev_private;
  4779. int pipe = crtc->pipe;
  4780. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4781. if (INTEL_INFO(dev)->gen >= 5) {
  4782. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4783. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4784. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4785. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4786. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  4787. * for gen < 8) and if DRRS is supported (to make sure the
  4788. * registers are not unnecessarily accessed).
  4789. */
  4790. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  4791. crtc->config.has_drrs) {
  4792. I915_WRITE(PIPE_DATA_M2(transcoder),
  4793. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  4794. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  4795. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  4796. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  4797. }
  4798. } else {
  4799. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4800. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4801. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4802. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4803. }
  4804. }
  4805. void intel_dp_set_m_n(struct intel_crtc *crtc)
  4806. {
  4807. if (crtc->config.has_pch_encoder)
  4808. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4809. else
  4810. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
  4811. &crtc->config.dp_m2_n2);
  4812. }
  4813. static void vlv_update_pll(struct intel_crtc *crtc)
  4814. {
  4815. u32 dpll, dpll_md;
  4816. /*
  4817. * Enable DPIO clock input. We should never disable the reference
  4818. * clock for pipe B, since VGA hotplug / manual detection depends
  4819. * on it.
  4820. */
  4821. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4822. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4823. /* We should never disable this, set it here for state tracking */
  4824. if (crtc->pipe == PIPE_B)
  4825. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4826. dpll |= DPLL_VCO_ENABLE;
  4827. crtc->config.dpll_hw_state.dpll = dpll;
  4828. dpll_md = (crtc->config.pixel_multiplier - 1)
  4829. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4830. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4831. }
  4832. static void vlv_prepare_pll(struct intel_crtc *crtc)
  4833. {
  4834. struct drm_device *dev = crtc->base.dev;
  4835. struct drm_i915_private *dev_priv = dev->dev_private;
  4836. int pipe = crtc->pipe;
  4837. u32 mdiv;
  4838. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4839. u32 coreclk, reg_val;
  4840. mutex_lock(&dev_priv->dpio_lock);
  4841. bestn = crtc->config.dpll.n;
  4842. bestm1 = crtc->config.dpll.m1;
  4843. bestm2 = crtc->config.dpll.m2;
  4844. bestp1 = crtc->config.dpll.p1;
  4845. bestp2 = crtc->config.dpll.p2;
  4846. /* See eDP HDMI DPIO driver vbios notes doc */
  4847. /* PLL B needs special handling */
  4848. if (pipe == PIPE_B)
  4849. vlv_pllb_recal_opamp(dev_priv, pipe);
  4850. /* Set up Tx target for periodic Rcomp update */
  4851. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4852. /* Disable target IRef on PLL */
  4853. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4854. reg_val &= 0x00ffffff;
  4855. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4856. /* Disable fast lock */
  4857. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4858. /* Set idtafcrecal before PLL is enabled */
  4859. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4860. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4861. mdiv |= ((bestn << DPIO_N_SHIFT));
  4862. mdiv |= (1 << DPIO_K_SHIFT);
  4863. /*
  4864. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4865. * but we don't support that).
  4866. * Note: don't use the DAC post divider as it seems unstable.
  4867. */
  4868. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4869. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4870. mdiv |= DPIO_ENABLE_CALIBRATION;
  4871. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4872. /* Set HBR and RBR LPF coefficients */
  4873. if (crtc->config.port_clock == 162000 ||
  4874. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
  4875. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
  4876. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4877. 0x009f0003);
  4878. else
  4879. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4880. 0x00d0000f);
  4881. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
  4882. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
  4883. /* Use SSC source */
  4884. if (pipe == PIPE_A)
  4885. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4886. 0x0df40000);
  4887. else
  4888. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4889. 0x0df70000);
  4890. } else { /* HDMI or VGA */
  4891. /* Use bend source */
  4892. if (pipe == PIPE_A)
  4893. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4894. 0x0df70000);
  4895. else
  4896. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4897. 0x0df40000);
  4898. }
  4899. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  4900. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  4901. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
  4902. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
  4903. coreclk |= 0x01000000;
  4904. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  4905. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  4906. mutex_unlock(&dev_priv->dpio_lock);
  4907. }
  4908. static void chv_update_pll(struct intel_crtc *crtc)
  4909. {
  4910. crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  4911. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  4912. DPLL_VCO_ENABLE;
  4913. if (crtc->pipe != PIPE_A)
  4914. crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4915. crtc->config.dpll_hw_state.dpll_md =
  4916. (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4917. }
  4918. static void chv_prepare_pll(struct intel_crtc *crtc)
  4919. {
  4920. struct drm_device *dev = crtc->base.dev;
  4921. struct drm_i915_private *dev_priv = dev->dev_private;
  4922. int pipe = crtc->pipe;
  4923. int dpll_reg = DPLL(crtc->pipe);
  4924. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  4925. u32 loopfilter, intcoeff;
  4926. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  4927. int refclk;
  4928. bestn = crtc->config.dpll.n;
  4929. bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
  4930. bestm1 = crtc->config.dpll.m1;
  4931. bestm2 = crtc->config.dpll.m2 >> 22;
  4932. bestp1 = crtc->config.dpll.p1;
  4933. bestp2 = crtc->config.dpll.p2;
  4934. /*
  4935. * Enable Refclk and SSC
  4936. */
  4937. I915_WRITE(dpll_reg,
  4938. crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  4939. mutex_lock(&dev_priv->dpio_lock);
  4940. /* p1 and p2 divider */
  4941. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  4942. 5 << DPIO_CHV_S1_DIV_SHIFT |
  4943. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  4944. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  4945. 1 << DPIO_CHV_K_DIV_SHIFT);
  4946. /* Feedback post-divider - m2 */
  4947. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  4948. /* Feedback refclk divider - n and m1 */
  4949. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  4950. DPIO_CHV_M1_DIV_BY_2 |
  4951. 1 << DPIO_CHV_N_DIV_SHIFT);
  4952. /* M2 fraction division */
  4953. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  4954. /* M2 fraction division enable */
  4955. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  4956. DPIO_CHV_FRAC_DIV_EN |
  4957. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  4958. /* Loop filter */
  4959. refclk = i9xx_get_refclk(&crtc->base, 0);
  4960. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  4961. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  4962. if (refclk == 100000)
  4963. intcoeff = 11;
  4964. else if (refclk == 38400)
  4965. intcoeff = 10;
  4966. else
  4967. intcoeff = 9;
  4968. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  4969. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  4970. /* AFC Recal */
  4971. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  4972. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  4973. DPIO_AFC_RECAL);
  4974. mutex_unlock(&dev_priv->dpio_lock);
  4975. }
  4976. static void i9xx_update_pll(struct intel_crtc *crtc,
  4977. intel_clock_t *reduced_clock,
  4978. int num_connectors)
  4979. {
  4980. struct drm_device *dev = crtc->base.dev;
  4981. struct drm_i915_private *dev_priv = dev->dev_private;
  4982. u32 dpll;
  4983. bool is_sdvo;
  4984. struct dpll *clock = &crtc->config.dpll;
  4985. i9xx_update_pll_dividers(crtc, reduced_clock);
  4986. is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
  4987. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  4988. dpll = DPLL_VGA_MODE_DIS;
  4989. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
  4990. dpll |= DPLLB_MODE_LVDS;
  4991. else
  4992. dpll |= DPLLB_MODE_DAC_SERIAL;
  4993. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  4994. dpll |= (crtc->config.pixel_multiplier - 1)
  4995. << SDVO_MULTIPLIER_SHIFT_HIRES;
  4996. }
  4997. if (is_sdvo)
  4998. dpll |= DPLL_SDVO_HIGH_SPEED;
  4999. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
  5000. dpll |= DPLL_SDVO_HIGH_SPEED;
  5001. /* compute bitmask from p1 value */
  5002. if (IS_PINEVIEW(dev))
  5003. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5004. else {
  5005. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5006. if (IS_G4X(dev) && reduced_clock)
  5007. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5008. }
  5009. switch (clock->p2) {
  5010. case 5:
  5011. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5012. break;
  5013. case 7:
  5014. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5015. break;
  5016. case 10:
  5017. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5018. break;
  5019. case 14:
  5020. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5021. break;
  5022. }
  5023. if (INTEL_INFO(dev)->gen >= 4)
  5024. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5025. if (crtc->config.sdvo_tv_clock)
  5026. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5027. else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  5028. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5029. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5030. else
  5031. dpll |= PLL_REF_INPUT_DREFCLK;
  5032. dpll |= DPLL_VCO_ENABLE;
  5033. crtc->config.dpll_hw_state.dpll = dpll;
  5034. if (INTEL_INFO(dev)->gen >= 4) {
  5035. u32 dpll_md = (crtc->config.pixel_multiplier - 1)
  5036. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5037. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  5038. }
  5039. }
  5040. static void i8xx_update_pll(struct intel_crtc *crtc,
  5041. intel_clock_t *reduced_clock,
  5042. int num_connectors)
  5043. {
  5044. struct drm_device *dev = crtc->base.dev;
  5045. struct drm_i915_private *dev_priv = dev->dev_private;
  5046. u32 dpll;
  5047. struct dpll *clock = &crtc->config.dpll;
  5048. i9xx_update_pll_dividers(crtc, reduced_clock);
  5049. dpll = DPLL_VGA_MODE_DIS;
  5050. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
  5051. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5052. } else {
  5053. if (clock->p1 == 2)
  5054. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5055. else
  5056. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5057. if (clock->p2 == 4)
  5058. dpll |= PLL_P2_DIVIDE_BY_4;
  5059. }
  5060. if (!IS_I830(dev) && intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
  5061. dpll |= DPLL_DVO_2X_MODE;
  5062. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  5063. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5064. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5065. else
  5066. dpll |= PLL_REF_INPUT_DREFCLK;
  5067. dpll |= DPLL_VCO_ENABLE;
  5068. crtc->config.dpll_hw_state.dpll = dpll;
  5069. }
  5070. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5071. {
  5072. struct drm_device *dev = intel_crtc->base.dev;
  5073. struct drm_i915_private *dev_priv = dev->dev_private;
  5074. enum pipe pipe = intel_crtc->pipe;
  5075. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5076. struct drm_display_mode *adjusted_mode =
  5077. &intel_crtc->config.adjusted_mode;
  5078. uint32_t crtc_vtotal, crtc_vblank_end;
  5079. int vsyncshift = 0;
  5080. /* We need to be careful not to changed the adjusted mode, for otherwise
  5081. * the hw state checker will get angry at the mismatch. */
  5082. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5083. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5084. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5085. /* the chip adds 2 halflines automatically */
  5086. crtc_vtotal -= 1;
  5087. crtc_vblank_end -= 1;
  5088. if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  5089. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5090. else
  5091. vsyncshift = adjusted_mode->crtc_hsync_start -
  5092. adjusted_mode->crtc_htotal / 2;
  5093. if (vsyncshift < 0)
  5094. vsyncshift += adjusted_mode->crtc_htotal;
  5095. }
  5096. if (INTEL_INFO(dev)->gen > 3)
  5097. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5098. I915_WRITE(HTOTAL(cpu_transcoder),
  5099. (adjusted_mode->crtc_hdisplay - 1) |
  5100. ((adjusted_mode->crtc_htotal - 1) << 16));
  5101. I915_WRITE(HBLANK(cpu_transcoder),
  5102. (adjusted_mode->crtc_hblank_start - 1) |
  5103. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5104. I915_WRITE(HSYNC(cpu_transcoder),
  5105. (adjusted_mode->crtc_hsync_start - 1) |
  5106. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5107. I915_WRITE(VTOTAL(cpu_transcoder),
  5108. (adjusted_mode->crtc_vdisplay - 1) |
  5109. ((crtc_vtotal - 1) << 16));
  5110. I915_WRITE(VBLANK(cpu_transcoder),
  5111. (adjusted_mode->crtc_vblank_start - 1) |
  5112. ((crtc_vblank_end - 1) << 16));
  5113. I915_WRITE(VSYNC(cpu_transcoder),
  5114. (adjusted_mode->crtc_vsync_start - 1) |
  5115. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5116. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5117. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5118. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5119. * bits. */
  5120. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5121. (pipe == PIPE_B || pipe == PIPE_C))
  5122. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5123. /* pipesrc controls the size that is scaled from, which should
  5124. * always be the user's requested size.
  5125. */
  5126. I915_WRITE(PIPESRC(pipe),
  5127. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  5128. (intel_crtc->config.pipe_src_h - 1));
  5129. }
  5130. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5131. struct intel_crtc_config *pipe_config)
  5132. {
  5133. struct drm_device *dev = crtc->base.dev;
  5134. struct drm_i915_private *dev_priv = dev->dev_private;
  5135. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5136. uint32_t tmp;
  5137. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5138. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5139. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5140. tmp = I915_READ(HBLANK(cpu_transcoder));
  5141. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5142. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5143. tmp = I915_READ(HSYNC(cpu_transcoder));
  5144. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5145. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5146. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5147. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5148. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5149. tmp = I915_READ(VBLANK(cpu_transcoder));
  5150. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5151. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5152. tmp = I915_READ(VSYNC(cpu_transcoder));
  5153. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5154. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5155. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5156. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5157. pipe_config->adjusted_mode.crtc_vtotal += 1;
  5158. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  5159. }
  5160. tmp = I915_READ(PIPESRC(crtc->pipe));
  5161. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5162. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5163. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  5164. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  5165. }
  5166. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5167. struct intel_crtc_config *pipe_config)
  5168. {
  5169. mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  5170. mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
  5171. mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  5172. mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  5173. mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  5174. mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  5175. mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  5176. mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  5177. mode->flags = pipe_config->adjusted_mode.flags;
  5178. mode->clock = pipe_config->adjusted_mode.crtc_clock;
  5179. mode->flags |= pipe_config->adjusted_mode.flags;
  5180. }
  5181. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5182. {
  5183. struct drm_device *dev = intel_crtc->base.dev;
  5184. struct drm_i915_private *dev_priv = dev->dev_private;
  5185. uint32_t pipeconf;
  5186. pipeconf = 0;
  5187. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5188. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5189. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5190. if (intel_crtc->config.double_wide)
  5191. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5192. /* only g4x and later have fancy bpc/dither controls */
  5193. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5194. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5195. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  5196. pipeconf |= PIPECONF_DITHER_EN |
  5197. PIPECONF_DITHER_TYPE_SP;
  5198. switch (intel_crtc->config.pipe_bpp) {
  5199. case 18:
  5200. pipeconf |= PIPECONF_6BPC;
  5201. break;
  5202. case 24:
  5203. pipeconf |= PIPECONF_8BPC;
  5204. break;
  5205. case 30:
  5206. pipeconf |= PIPECONF_10BPC;
  5207. break;
  5208. default:
  5209. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5210. BUG();
  5211. }
  5212. }
  5213. if (HAS_PIPE_CXSR(dev)) {
  5214. if (intel_crtc->lowfreq_avail) {
  5215. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5216. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5217. } else {
  5218. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5219. }
  5220. }
  5221. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5222. if (INTEL_INFO(dev)->gen < 4 ||
  5223. intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  5224. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5225. else
  5226. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5227. } else
  5228. pipeconf |= PIPECONF_PROGRESSIVE;
  5229. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  5230. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5231. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5232. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5233. }
  5234. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  5235. int x, int y,
  5236. struct drm_framebuffer *fb)
  5237. {
  5238. struct drm_device *dev = crtc->dev;
  5239. struct drm_i915_private *dev_priv = dev->dev_private;
  5240. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5241. int refclk, num_connectors = 0;
  5242. intel_clock_t clock, reduced_clock;
  5243. bool ok, has_reduced_clock = false;
  5244. bool is_lvds = false, is_dsi = false;
  5245. struct intel_encoder *encoder;
  5246. const intel_limit_t *limit;
  5247. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5248. switch (encoder->type) {
  5249. case INTEL_OUTPUT_LVDS:
  5250. is_lvds = true;
  5251. break;
  5252. case INTEL_OUTPUT_DSI:
  5253. is_dsi = true;
  5254. break;
  5255. }
  5256. num_connectors++;
  5257. }
  5258. if (is_dsi)
  5259. return 0;
  5260. if (!intel_crtc->config.clock_set) {
  5261. refclk = i9xx_get_refclk(crtc, num_connectors);
  5262. /*
  5263. * Returns a set of divisors for the desired target clock with
  5264. * the given refclk, or FALSE. The returned values represent
  5265. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5266. * 2) / p1 / p2.
  5267. */
  5268. limit = intel_limit(crtc, refclk);
  5269. ok = dev_priv->display.find_dpll(limit, crtc,
  5270. intel_crtc->config.port_clock,
  5271. refclk, NULL, &clock);
  5272. if (!ok) {
  5273. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5274. return -EINVAL;
  5275. }
  5276. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5277. /*
  5278. * Ensure we match the reduced clock's P to the target
  5279. * clock. If the clocks don't match, we can't switch
  5280. * the display clock by using the FP0/FP1. In such case
  5281. * we will disable the LVDS downclock feature.
  5282. */
  5283. has_reduced_clock =
  5284. dev_priv->display.find_dpll(limit, crtc,
  5285. dev_priv->lvds_downclock,
  5286. refclk, &clock,
  5287. &reduced_clock);
  5288. }
  5289. /* Compat-code for transition, will disappear. */
  5290. intel_crtc->config.dpll.n = clock.n;
  5291. intel_crtc->config.dpll.m1 = clock.m1;
  5292. intel_crtc->config.dpll.m2 = clock.m2;
  5293. intel_crtc->config.dpll.p1 = clock.p1;
  5294. intel_crtc->config.dpll.p2 = clock.p2;
  5295. }
  5296. if (IS_GEN2(dev)) {
  5297. i8xx_update_pll(intel_crtc,
  5298. has_reduced_clock ? &reduced_clock : NULL,
  5299. num_connectors);
  5300. } else if (IS_CHERRYVIEW(dev)) {
  5301. chv_update_pll(intel_crtc);
  5302. } else if (IS_VALLEYVIEW(dev)) {
  5303. vlv_update_pll(intel_crtc);
  5304. } else {
  5305. i9xx_update_pll(intel_crtc,
  5306. has_reduced_clock ? &reduced_clock : NULL,
  5307. num_connectors);
  5308. }
  5309. return 0;
  5310. }
  5311. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5312. struct intel_crtc_config *pipe_config)
  5313. {
  5314. struct drm_device *dev = crtc->base.dev;
  5315. struct drm_i915_private *dev_priv = dev->dev_private;
  5316. uint32_t tmp;
  5317. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5318. return;
  5319. tmp = I915_READ(PFIT_CONTROL);
  5320. if (!(tmp & PFIT_ENABLE))
  5321. return;
  5322. /* Check whether the pfit is attached to our pipe. */
  5323. if (INTEL_INFO(dev)->gen < 4) {
  5324. if (crtc->pipe != PIPE_B)
  5325. return;
  5326. } else {
  5327. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5328. return;
  5329. }
  5330. pipe_config->gmch_pfit.control = tmp;
  5331. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5332. if (INTEL_INFO(dev)->gen < 5)
  5333. pipe_config->gmch_pfit.lvds_border_bits =
  5334. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5335. }
  5336. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5337. struct intel_crtc_config *pipe_config)
  5338. {
  5339. struct drm_device *dev = crtc->base.dev;
  5340. struct drm_i915_private *dev_priv = dev->dev_private;
  5341. int pipe = pipe_config->cpu_transcoder;
  5342. intel_clock_t clock;
  5343. u32 mdiv;
  5344. int refclk = 100000;
  5345. /* In case of MIPI DPLL will not even be used */
  5346. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5347. return;
  5348. mutex_lock(&dev_priv->dpio_lock);
  5349. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5350. mutex_unlock(&dev_priv->dpio_lock);
  5351. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5352. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5353. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5354. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5355. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5356. vlv_clock(refclk, &clock);
  5357. /* clock.dot is the fast clock */
  5358. pipe_config->port_clock = clock.dot / 5;
  5359. }
  5360. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5361. struct intel_plane_config *plane_config)
  5362. {
  5363. struct drm_device *dev = crtc->base.dev;
  5364. struct drm_i915_private *dev_priv = dev->dev_private;
  5365. u32 val, base, offset;
  5366. int pipe = crtc->pipe, plane = crtc->plane;
  5367. int fourcc, pixel_format;
  5368. int aligned_height;
  5369. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5370. if (!crtc->base.primary->fb) {
  5371. DRM_DEBUG_KMS("failed to alloc fb\n");
  5372. return;
  5373. }
  5374. val = I915_READ(DSPCNTR(plane));
  5375. if (INTEL_INFO(dev)->gen >= 4)
  5376. if (val & DISPPLANE_TILED)
  5377. plane_config->tiled = true;
  5378. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5379. fourcc = intel_format_to_fourcc(pixel_format);
  5380. crtc->base.primary->fb->pixel_format = fourcc;
  5381. crtc->base.primary->fb->bits_per_pixel =
  5382. drm_format_plane_cpp(fourcc, 0) * 8;
  5383. if (INTEL_INFO(dev)->gen >= 4) {
  5384. if (plane_config->tiled)
  5385. offset = I915_READ(DSPTILEOFF(plane));
  5386. else
  5387. offset = I915_READ(DSPLINOFF(plane));
  5388. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5389. } else {
  5390. base = I915_READ(DSPADDR(plane));
  5391. }
  5392. plane_config->base = base;
  5393. val = I915_READ(PIPESRC(pipe));
  5394. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5395. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5396. val = I915_READ(DSPSTRIDE(pipe));
  5397. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  5398. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5399. plane_config->tiled);
  5400. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  5401. aligned_height);
  5402. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5403. pipe, plane, crtc->base.primary->fb->width,
  5404. crtc->base.primary->fb->height,
  5405. crtc->base.primary->fb->bits_per_pixel, base,
  5406. crtc->base.primary->fb->pitches[0],
  5407. plane_config->size);
  5408. }
  5409. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5410. struct intel_crtc_config *pipe_config)
  5411. {
  5412. struct drm_device *dev = crtc->base.dev;
  5413. struct drm_i915_private *dev_priv = dev->dev_private;
  5414. int pipe = pipe_config->cpu_transcoder;
  5415. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5416. intel_clock_t clock;
  5417. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5418. int refclk = 100000;
  5419. mutex_lock(&dev_priv->dpio_lock);
  5420. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5421. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5422. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5423. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5424. mutex_unlock(&dev_priv->dpio_lock);
  5425. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5426. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5427. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5428. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5429. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5430. chv_clock(refclk, &clock);
  5431. /* clock.dot is the fast clock */
  5432. pipe_config->port_clock = clock.dot / 5;
  5433. }
  5434. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5435. struct intel_crtc_config *pipe_config)
  5436. {
  5437. struct drm_device *dev = crtc->base.dev;
  5438. struct drm_i915_private *dev_priv = dev->dev_private;
  5439. uint32_t tmp;
  5440. if (!intel_display_power_is_enabled(dev_priv,
  5441. POWER_DOMAIN_PIPE(crtc->pipe)))
  5442. return false;
  5443. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5444. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5445. tmp = I915_READ(PIPECONF(crtc->pipe));
  5446. if (!(tmp & PIPECONF_ENABLE))
  5447. return false;
  5448. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5449. switch (tmp & PIPECONF_BPC_MASK) {
  5450. case PIPECONF_6BPC:
  5451. pipe_config->pipe_bpp = 18;
  5452. break;
  5453. case PIPECONF_8BPC:
  5454. pipe_config->pipe_bpp = 24;
  5455. break;
  5456. case PIPECONF_10BPC:
  5457. pipe_config->pipe_bpp = 30;
  5458. break;
  5459. default:
  5460. break;
  5461. }
  5462. }
  5463. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5464. pipe_config->limited_color_range = true;
  5465. if (INTEL_INFO(dev)->gen < 4)
  5466. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5467. intel_get_pipe_timings(crtc, pipe_config);
  5468. i9xx_get_pfit_config(crtc, pipe_config);
  5469. if (INTEL_INFO(dev)->gen >= 4) {
  5470. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5471. pipe_config->pixel_multiplier =
  5472. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5473. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5474. pipe_config->dpll_hw_state.dpll_md = tmp;
  5475. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5476. tmp = I915_READ(DPLL(crtc->pipe));
  5477. pipe_config->pixel_multiplier =
  5478. ((tmp & SDVO_MULTIPLIER_MASK)
  5479. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5480. } else {
  5481. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5482. * port and will be fixed up in the encoder->get_config
  5483. * function. */
  5484. pipe_config->pixel_multiplier = 1;
  5485. }
  5486. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5487. if (!IS_VALLEYVIEW(dev)) {
  5488. /*
  5489. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  5490. * on 830. Filter it out here so that we don't
  5491. * report errors due to that.
  5492. */
  5493. if (IS_I830(dev))
  5494. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  5495. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5496. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5497. } else {
  5498. /* Mask out read-only status bits. */
  5499. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5500. DPLL_PORTC_READY_MASK |
  5501. DPLL_PORTB_READY_MASK);
  5502. }
  5503. if (IS_CHERRYVIEW(dev))
  5504. chv_crtc_clock_get(crtc, pipe_config);
  5505. else if (IS_VALLEYVIEW(dev))
  5506. vlv_crtc_clock_get(crtc, pipe_config);
  5507. else
  5508. i9xx_crtc_clock_get(crtc, pipe_config);
  5509. return true;
  5510. }
  5511. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5512. {
  5513. struct drm_i915_private *dev_priv = dev->dev_private;
  5514. struct intel_encoder *encoder;
  5515. u32 val, final;
  5516. bool has_lvds = false;
  5517. bool has_cpu_edp = false;
  5518. bool has_panel = false;
  5519. bool has_ck505 = false;
  5520. bool can_ssc = false;
  5521. /* We need to take the global config into account */
  5522. for_each_intel_encoder(dev, encoder) {
  5523. switch (encoder->type) {
  5524. case INTEL_OUTPUT_LVDS:
  5525. has_panel = true;
  5526. has_lvds = true;
  5527. break;
  5528. case INTEL_OUTPUT_EDP:
  5529. has_panel = true;
  5530. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5531. has_cpu_edp = true;
  5532. break;
  5533. }
  5534. }
  5535. if (HAS_PCH_IBX(dev)) {
  5536. has_ck505 = dev_priv->vbt.display_clock_mode;
  5537. can_ssc = has_ck505;
  5538. } else {
  5539. has_ck505 = false;
  5540. can_ssc = true;
  5541. }
  5542. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5543. has_panel, has_lvds, has_ck505);
  5544. /* Ironlake: try to setup display ref clock before DPLL
  5545. * enabling. This is only under driver's control after
  5546. * PCH B stepping, previous chipset stepping should be
  5547. * ignoring this setting.
  5548. */
  5549. val = I915_READ(PCH_DREF_CONTROL);
  5550. /* As we must carefully and slowly disable/enable each source in turn,
  5551. * compute the final state we want first and check if we need to
  5552. * make any changes at all.
  5553. */
  5554. final = val;
  5555. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5556. if (has_ck505)
  5557. final |= DREF_NONSPREAD_CK505_ENABLE;
  5558. else
  5559. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5560. final &= ~DREF_SSC_SOURCE_MASK;
  5561. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5562. final &= ~DREF_SSC1_ENABLE;
  5563. if (has_panel) {
  5564. final |= DREF_SSC_SOURCE_ENABLE;
  5565. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5566. final |= DREF_SSC1_ENABLE;
  5567. if (has_cpu_edp) {
  5568. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5569. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5570. else
  5571. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5572. } else
  5573. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5574. } else {
  5575. final |= DREF_SSC_SOURCE_DISABLE;
  5576. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5577. }
  5578. if (final == val)
  5579. return;
  5580. /* Always enable nonspread source */
  5581. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5582. if (has_ck505)
  5583. val |= DREF_NONSPREAD_CK505_ENABLE;
  5584. else
  5585. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5586. if (has_panel) {
  5587. val &= ~DREF_SSC_SOURCE_MASK;
  5588. val |= DREF_SSC_SOURCE_ENABLE;
  5589. /* SSC must be turned on before enabling the CPU output */
  5590. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5591. DRM_DEBUG_KMS("Using SSC on panel\n");
  5592. val |= DREF_SSC1_ENABLE;
  5593. } else
  5594. val &= ~DREF_SSC1_ENABLE;
  5595. /* Get SSC going before enabling the outputs */
  5596. I915_WRITE(PCH_DREF_CONTROL, val);
  5597. POSTING_READ(PCH_DREF_CONTROL);
  5598. udelay(200);
  5599. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5600. /* Enable CPU source on CPU attached eDP */
  5601. if (has_cpu_edp) {
  5602. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5603. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5604. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5605. } else
  5606. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5607. } else
  5608. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5609. I915_WRITE(PCH_DREF_CONTROL, val);
  5610. POSTING_READ(PCH_DREF_CONTROL);
  5611. udelay(200);
  5612. } else {
  5613. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5614. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5615. /* Turn off CPU output */
  5616. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5617. I915_WRITE(PCH_DREF_CONTROL, val);
  5618. POSTING_READ(PCH_DREF_CONTROL);
  5619. udelay(200);
  5620. /* Turn off the SSC source */
  5621. val &= ~DREF_SSC_SOURCE_MASK;
  5622. val |= DREF_SSC_SOURCE_DISABLE;
  5623. /* Turn off SSC1 */
  5624. val &= ~DREF_SSC1_ENABLE;
  5625. I915_WRITE(PCH_DREF_CONTROL, val);
  5626. POSTING_READ(PCH_DREF_CONTROL);
  5627. udelay(200);
  5628. }
  5629. BUG_ON(val != final);
  5630. }
  5631. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5632. {
  5633. uint32_t tmp;
  5634. tmp = I915_READ(SOUTH_CHICKEN2);
  5635. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5636. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5637. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5638. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5639. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5640. tmp = I915_READ(SOUTH_CHICKEN2);
  5641. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5642. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5643. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5644. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5645. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5646. }
  5647. /* WaMPhyProgramming:hsw */
  5648. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5649. {
  5650. uint32_t tmp;
  5651. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5652. tmp &= ~(0xFF << 24);
  5653. tmp |= (0x12 << 24);
  5654. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5655. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5656. tmp |= (1 << 11);
  5657. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5658. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5659. tmp |= (1 << 11);
  5660. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5661. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5662. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5663. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5664. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5665. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5666. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5667. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5668. tmp &= ~(7 << 13);
  5669. tmp |= (5 << 13);
  5670. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5671. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5672. tmp &= ~(7 << 13);
  5673. tmp |= (5 << 13);
  5674. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5675. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5676. tmp &= ~0xFF;
  5677. tmp |= 0x1C;
  5678. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5679. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5680. tmp &= ~0xFF;
  5681. tmp |= 0x1C;
  5682. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5683. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5684. tmp &= ~(0xFF << 16);
  5685. tmp |= (0x1C << 16);
  5686. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5687. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5688. tmp &= ~(0xFF << 16);
  5689. tmp |= (0x1C << 16);
  5690. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5691. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5692. tmp |= (1 << 27);
  5693. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5694. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5695. tmp |= (1 << 27);
  5696. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5697. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5698. tmp &= ~(0xF << 28);
  5699. tmp |= (4 << 28);
  5700. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5701. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5702. tmp &= ~(0xF << 28);
  5703. tmp |= (4 << 28);
  5704. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5705. }
  5706. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5707. * Programming" based on the parameters passed:
  5708. * - Sequence to enable CLKOUT_DP
  5709. * - Sequence to enable CLKOUT_DP without spread
  5710. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5711. */
  5712. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5713. bool with_fdi)
  5714. {
  5715. struct drm_i915_private *dev_priv = dev->dev_private;
  5716. uint32_t reg, tmp;
  5717. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5718. with_spread = true;
  5719. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5720. with_fdi, "LP PCH doesn't have FDI\n"))
  5721. with_fdi = false;
  5722. mutex_lock(&dev_priv->dpio_lock);
  5723. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5724. tmp &= ~SBI_SSCCTL_DISABLE;
  5725. tmp |= SBI_SSCCTL_PATHALT;
  5726. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5727. udelay(24);
  5728. if (with_spread) {
  5729. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5730. tmp &= ~SBI_SSCCTL_PATHALT;
  5731. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5732. if (with_fdi) {
  5733. lpt_reset_fdi_mphy(dev_priv);
  5734. lpt_program_fdi_mphy(dev_priv);
  5735. }
  5736. }
  5737. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5738. SBI_GEN0 : SBI_DBUFF0;
  5739. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5740. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5741. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5742. mutex_unlock(&dev_priv->dpio_lock);
  5743. }
  5744. /* Sequence to disable CLKOUT_DP */
  5745. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5746. {
  5747. struct drm_i915_private *dev_priv = dev->dev_private;
  5748. uint32_t reg, tmp;
  5749. mutex_lock(&dev_priv->dpio_lock);
  5750. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5751. SBI_GEN0 : SBI_DBUFF0;
  5752. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5753. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5754. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5755. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5756. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5757. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5758. tmp |= SBI_SSCCTL_PATHALT;
  5759. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5760. udelay(32);
  5761. }
  5762. tmp |= SBI_SSCCTL_DISABLE;
  5763. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5764. }
  5765. mutex_unlock(&dev_priv->dpio_lock);
  5766. }
  5767. static void lpt_init_pch_refclk(struct drm_device *dev)
  5768. {
  5769. struct intel_encoder *encoder;
  5770. bool has_vga = false;
  5771. for_each_intel_encoder(dev, encoder) {
  5772. switch (encoder->type) {
  5773. case INTEL_OUTPUT_ANALOG:
  5774. has_vga = true;
  5775. break;
  5776. }
  5777. }
  5778. if (has_vga)
  5779. lpt_enable_clkout_dp(dev, true, true);
  5780. else
  5781. lpt_disable_clkout_dp(dev);
  5782. }
  5783. /*
  5784. * Initialize reference clocks when the driver loads
  5785. */
  5786. void intel_init_pch_refclk(struct drm_device *dev)
  5787. {
  5788. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5789. ironlake_init_pch_refclk(dev);
  5790. else if (HAS_PCH_LPT(dev))
  5791. lpt_init_pch_refclk(dev);
  5792. }
  5793. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5794. {
  5795. struct drm_device *dev = crtc->dev;
  5796. struct drm_i915_private *dev_priv = dev->dev_private;
  5797. struct intel_encoder *encoder;
  5798. int num_connectors = 0;
  5799. bool is_lvds = false;
  5800. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5801. switch (encoder->type) {
  5802. case INTEL_OUTPUT_LVDS:
  5803. is_lvds = true;
  5804. break;
  5805. }
  5806. num_connectors++;
  5807. }
  5808. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5809. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5810. dev_priv->vbt.lvds_ssc_freq);
  5811. return dev_priv->vbt.lvds_ssc_freq;
  5812. }
  5813. return 120000;
  5814. }
  5815. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5816. {
  5817. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5818. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5819. int pipe = intel_crtc->pipe;
  5820. uint32_t val;
  5821. val = 0;
  5822. switch (intel_crtc->config.pipe_bpp) {
  5823. case 18:
  5824. val |= PIPECONF_6BPC;
  5825. break;
  5826. case 24:
  5827. val |= PIPECONF_8BPC;
  5828. break;
  5829. case 30:
  5830. val |= PIPECONF_10BPC;
  5831. break;
  5832. case 36:
  5833. val |= PIPECONF_12BPC;
  5834. break;
  5835. default:
  5836. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5837. BUG();
  5838. }
  5839. if (intel_crtc->config.dither)
  5840. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5841. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5842. val |= PIPECONF_INTERLACED_ILK;
  5843. else
  5844. val |= PIPECONF_PROGRESSIVE;
  5845. if (intel_crtc->config.limited_color_range)
  5846. val |= PIPECONF_COLOR_RANGE_SELECT;
  5847. I915_WRITE(PIPECONF(pipe), val);
  5848. POSTING_READ(PIPECONF(pipe));
  5849. }
  5850. /*
  5851. * Set up the pipe CSC unit.
  5852. *
  5853. * Currently only full range RGB to limited range RGB conversion
  5854. * is supported, but eventually this should handle various
  5855. * RGB<->YCbCr scenarios as well.
  5856. */
  5857. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  5858. {
  5859. struct drm_device *dev = crtc->dev;
  5860. struct drm_i915_private *dev_priv = dev->dev_private;
  5861. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5862. int pipe = intel_crtc->pipe;
  5863. uint16_t coeff = 0x7800; /* 1.0 */
  5864. /*
  5865. * TODO: Check what kind of values actually come out of the pipe
  5866. * with these coeff/postoff values and adjust to get the best
  5867. * accuracy. Perhaps we even need to take the bpc value into
  5868. * consideration.
  5869. */
  5870. if (intel_crtc->config.limited_color_range)
  5871. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  5872. /*
  5873. * GY/GU and RY/RU should be the other way around according
  5874. * to BSpec, but reality doesn't agree. Just set them up in
  5875. * a way that results in the correct picture.
  5876. */
  5877. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  5878. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  5879. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  5880. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  5881. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  5882. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  5883. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  5884. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  5885. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  5886. if (INTEL_INFO(dev)->gen > 6) {
  5887. uint16_t postoff = 0;
  5888. if (intel_crtc->config.limited_color_range)
  5889. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  5890. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  5891. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  5892. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  5893. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  5894. } else {
  5895. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  5896. if (intel_crtc->config.limited_color_range)
  5897. mode |= CSC_BLACK_SCREEN_OFFSET;
  5898. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  5899. }
  5900. }
  5901. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  5902. {
  5903. struct drm_device *dev = crtc->dev;
  5904. struct drm_i915_private *dev_priv = dev->dev_private;
  5905. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5906. enum pipe pipe = intel_crtc->pipe;
  5907. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5908. uint32_t val;
  5909. val = 0;
  5910. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  5911. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5912. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5913. val |= PIPECONF_INTERLACED_ILK;
  5914. else
  5915. val |= PIPECONF_PROGRESSIVE;
  5916. I915_WRITE(PIPECONF(cpu_transcoder), val);
  5917. POSTING_READ(PIPECONF(cpu_transcoder));
  5918. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  5919. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  5920. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  5921. val = 0;
  5922. switch (intel_crtc->config.pipe_bpp) {
  5923. case 18:
  5924. val |= PIPEMISC_DITHER_6_BPC;
  5925. break;
  5926. case 24:
  5927. val |= PIPEMISC_DITHER_8_BPC;
  5928. break;
  5929. case 30:
  5930. val |= PIPEMISC_DITHER_10_BPC;
  5931. break;
  5932. case 36:
  5933. val |= PIPEMISC_DITHER_12_BPC;
  5934. break;
  5935. default:
  5936. /* Case prevented by pipe_config_set_bpp. */
  5937. BUG();
  5938. }
  5939. if (intel_crtc->config.dither)
  5940. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  5941. I915_WRITE(PIPEMISC(pipe), val);
  5942. }
  5943. }
  5944. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  5945. intel_clock_t *clock,
  5946. bool *has_reduced_clock,
  5947. intel_clock_t *reduced_clock)
  5948. {
  5949. struct drm_device *dev = crtc->dev;
  5950. struct drm_i915_private *dev_priv = dev->dev_private;
  5951. struct intel_encoder *intel_encoder;
  5952. int refclk;
  5953. const intel_limit_t *limit;
  5954. bool ret, is_lvds = false;
  5955. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5956. switch (intel_encoder->type) {
  5957. case INTEL_OUTPUT_LVDS:
  5958. is_lvds = true;
  5959. break;
  5960. }
  5961. }
  5962. refclk = ironlake_get_refclk(crtc);
  5963. /*
  5964. * Returns a set of divisors for the desired target clock with the given
  5965. * refclk, or FALSE. The returned values represent the clock equation:
  5966. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  5967. */
  5968. limit = intel_limit(crtc, refclk);
  5969. ret = dev_priv->display.find_dpll(limit, crtc,
  5970. to_intel_crtc(crtc)->config.port_clock,
  5971. refclk, NULL, clock);
  5972. if (!ret)
  5973. return false;
  5974. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5975. /*
  5976. * Ensure we match the reduced clock's P to the target clock.
  5977. * If the clocks don't match, we can't switch the display clock
  5978. * by using the FP0/FP1. In such case we will disable the LVDS
  5979. * downclock feature.
  5980. */
  5981. *has_reduced_clock =
  5982. dev_priv->display.find_dpll(limit, crtc,
  5983. dev_priv->lvds_downclock,
  5984. refclk, clock,
  5985. reduced_clock);
  5986. }
  5987. return true;
  5988. }
  5989. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  5990. {
  5991. /*
  5992. * Account for spread spectrum to avoid
  5993. * oversubscribing the link. Max center spread
  5994. * is 2.5%; use 5% for safety's sake.
  5995. */
  5996. u32 bps = target_clock * bpp * 21 / 20;
  5997. return DIV_ROUND_UP(bps, link_bw * 8);
  5998. }
  5999. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6000. {
  6001. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6002. }
  6003. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6004. u32 *fp,
  6005. intel_clock_t *reduced_clock, u32 *fp2)
  6006. {
  6007. struct drm_crtc *crtc = &intel_crtc->base;
  6008. struct drm_device *dev = crtc->dev;
  6009. struct drm_i915_private *dev_priv = dev->dev_private;
  6010. struct intel_encoder *intel_encoder;
  6011. uint32_t dpll;
  6012. int factor, num_connectors = 0;
  6013. bool is_lvds = false, is_sdvo = false;
  6014. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  6015. switch (intel_encoder->type) {
  6016. case INTEL_OUTPUT_LVDS:
  6017. is_lvds = true;
  6018. break;
  6019. case INTEL_OUTPUT_SDVO:
  6020. case INTEL_OUTPUT_HDMI:
  6021. is_sdvo = true;
  6022. break;
  6023. }
  6024. num_connectors++;
  6025. }
  6026. /* Enable autotuning of the PLL clock (if permissible) */
  6027. factor = 21;
  6028. if (is_lvds) {
  6029. if ((intel_panel_use_ssc(dev_priv) &&
  6030. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6031. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  6032. factor = 25;
  6033. } else if (intel_crtc->config.sdvo_tv_clock)
  6034. factor = 20;
  6035. if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
  6036. *fp |= FP_CB_TUNE;
  6037. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  6038. *fp2 |= FP_CB_TUNE;
  6039. dpll = 0;
  6040. if (is_lvds)
  6041. dpll |= DPLLB_MODE_LVDS;
  6042. else
  6043. dpll |= DPLLB_MODE_DAC_SERIAL;
  6044. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  6045. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6046. if (is_sdvo)
  6047. dpll |= DPLL_SDVO_HIGH_SPEED;
  6048. if (intel_crtc->config.has_dp_encoder)
  6049. dpll |= DPLL_SDVO_HIGH_SPEED;
  6050. /* compute bitmask from p1 value */
  6051. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6052. /* also FPA1 */
  6053. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6054. switch (intel_crtc->config.dpll.p2) {
  6055. case 5:
  6056. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6057. break;
  6058. case 7:
  6059. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6060. break;
  6061. case 10:
  6062. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6063. break;
  6064. case 14:
  6065. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6066. break;
  6067. }
  6068. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6069. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6070. else
  6071. dpll |= PLL_REF_INPUT_DREFCLK;
  6072. return dpll | DPLL_VCO_ENABLE;
  6073. }
  6074. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  6075. int x, int y,
  6076. struct drm_framebuffer *fb)
  6077. {
  6078. struct drm_device *dev = crtc->dev;
  6079. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6080. int num_connectors = 0;
  6081. intel_clock_t clock, reduced_clock;
  6082. u32 dpll = 0, fp = 0, fp2 = 0;
  6083. bool ok, has_reduced_clock = false;
  6084. bool is_lvds = false;
  6085. struct intel_encoder *encoder;
  6086. struct intel_shared_dpll *pll;
  6087. for_each_encoder_on_crtc(dev, crtc, encoder) {
  6088. switch (encoder->type) {
  6089. case INTEL_OUTPUT_LVDS:
  6090. is_lvds = true;
  6091. break;
  6092. }
  6093. num_connectors++;
  6094. }
  6095. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  6096. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  6097. ok = ironlake_compute_clocks(crtc, &clock,
  6098. &has_reduced_clock, &reduced_clock);
  6099. if (!ok && !intel_crtc->config.clock_set) {
  6100. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6101. return -EINVAL;
  6102. }
  6103. /* Compat-code for transition, will disappear. */
  6104. if (!intel_crtc->config.clock_set) {
  6105. intel_crtc->config.dpll.n = clock.n;
  6106. intel_crtc->config.dpll.m1 = clock.m1;
  6107. intel_crtc->config.dpll.m2 = clock.m2;
  6108. intel_crtc->config.dpll.p1 = clock.p1;
  6109. intel_crtc->config.dpll.p2 = clock.p2;
  6110. }
  6111. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6112. if (intel_crtc->config.has_pch_encoder) {
  6113. fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
  6114. if (has_reduced_clock)
  6115. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  6116. dpll = ironlake_compute_dpll(intel_crtc,
  6117. &fp, &reduced_clock,
  6118. has_reduced_clock ? &fp2 : NULL);
  6119. intel_crtc->config.dpll_hw_state.dpll = dpll;
  6120. intel_crtc->config.dpll_hw_state.fp0 = fp;
  6121. if (has_reduced_clock)
  6122. intel_crtc->config.dpll_hw_state.fp1 = fp2;
  6123. else
  6124. intel_crtc->config.dpll_hw_state.fp1 = fp;
  6125. pll = intel_get_shared_dpll(intel_crtc);
  6126. if (pll == NULL) {
  6127. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6128. pipe_name(intel_crtc->pipe));
  6129. return -EINVAL;
  6130. }
  6131. } else
  6132. intel_put_shared_dpll(intel_crtc);
  6133. if (is_lvds && has_reduced_clock && i915.powersave)
  6134. intel_crtc->lowfreq_avail = true;
  6135. else
  6136. intel_crtc->lowfreq_avail = false;
  6137. return 0;
  6138. }
  6139. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6140. struct intel_link_m_n *m_n)
  6141. {
  6142. struct drm_device *dev = crtc->base.dev;
  6143. struct drm_i915_private *dev_priv = dev->dev_private;
  6144. enum pipe pipe = crtc->pipe;
  6145. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6146. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6147. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6148. & ~TU_SIZE_MASK;
  6149. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6150. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6151. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6152. }
  6153. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6154. enum transcoder transcoder,
  6155. struct intel_link_m_n *m_n,
  6156. struct intel_link_m_n *m2_n2)
  6157. {
  6158. struct drm_device *dev = crtc->base.dev;
  6159. struct drm_i915_private *dev_priv = dev->dev_private;
  6160. enum pipe pipe = crtc->pipe;
  6161. if (INTEL_INFO(dev)->gen >= 5) {
  6162. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6163. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6164. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6165. & ~TU_SIZE_MASK;
  6166. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6167. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6168. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6169. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6170. * gen < 8) and if DRRS is supported (to make sure the
  6171. * registers are not unnecessarily read).
  6172. */
  6173. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6174. crtc->config.has_drrs) {
  6175. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6176. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6177. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6178. & ~TU_SIZE_MASK;
  6179. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6180. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6181. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6182. }
  6183. } else {
  6184. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6185. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6186. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6187. & ~TU_SIZE_MASK;
  6188. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6189. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6190. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6191. }
  6192. }
  6193. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6194. struct intel_crtc_config *pipe_config)
  6195. {
  6196. if (crtc->config.has_pch_encoder)
  6197. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6198. else
  6199. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6200. &pipe_config->dp_m_n,
  6201. &pipe_config->dp_m2_n2);
  6202. }
  6203. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6204. struct intel_crtc_config *pipe_config)
  6205. {
  6206. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6207. &pipe_config->fdi_m_n, NULL);
  6208. }
  6209. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6210. struct intel_crtc_config *pipe_config)
  6211. {
  6212. struct drm_device *dev = crtc->base.dev;
  6213. struct drm_i915_private *dev_priv = dev->dev_private;
  6214. uint32_t tmp;
  6215. tmp = I915_READ(PF_CTL(crtc->pipe));
  6216. if (tmp & PF_ENABLE) {
  6217. pipe_config->pch_pfit.enabled = true;
  6218. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6219. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6220. /* We currently do not free assignements of panel fitters on
  6221. * ivb/hsw (since we don't use the higher upscaling modes which
  6222. * differentiates them) so just WARN about this case for now. */
  6223. if (IS_GEN7(dev)) {
  6224. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6225. PF_PIPE_SEL_IVB(crtc->pipe));
  6226. }
  6227. }
  6228. }
  6229. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  6230. struct intel_plane_config *plane_config)
  6231. {
  6232. struct drm_device *dev = crtc->base.dev;
  6233. struct drm_i915_private *dev_priv = dev->dev_private;
  6234. u32 val, base, offset;
  6235. int pipe = crtc->pipe, plane = crtc->plane;
  6236. int fourcc, pixel_format;
  6237. int aligned_height;
  6238. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  6239. if (!crtc->base.primary->fb) {
  6240. DRM_DEBUG_KMS("failed to alloc fb\n");
  6241. return;
  6242. }
  6243. val = I915_READ(DSPCNTR(plane));
  6244. if (INTEL_INFO(dev)->gen >= 4)
  6245. if (val & DISPPLANE_TILED)
  6246. plane_config->tiled = true;
  6247. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6248. fourcc = intel_format_to_fourcc(pixel_format);
  6249. crtc->base.primary->fb->pixel_format = fourcc;
  6250. crtc->base.primary->fb->bits_per_pixel =
  6251. drm_format_plane_cpp(fourcc, 0) * 8;
  6252. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6253. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6254. offset = I915_READ(DSPOFFSET(plane));
  6255. } else {
  6256. if (plane_config->tiled)
  6257. offset = I915_READ(DSPTILEOFF(plane));
  6258. else
  6259. offset = I915_READ(DSPLINOFF(plane));
  6260. }
  6261. plane_config->base = base;
  6262. val = I915_READ(PIPESRC(pipe));
  6263. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  6264. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  6265. val = I915_READ(DSPSTRIDE(pipe));
  6266. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  6267. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  6268. plane_config->tiled);
  6269. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  6270. aligned_height);
  6271. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6272. pipe, plane, crtc->base.primary->fb->width,
  6273. crtc->base.primary->fb->height,
  6274. crtc->base.primary->fb->bits_per_pixel, base,
  6275. crtc->base.primary->fb->pitches[0],
  6276. plane_config->size);
  6277. }
  6278. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6279. struct intel_crtc_config *pipe_config)
  6280. {
  6281. struct drm_device *dev = crtc->base.dev;
  6282. struct drm_i915_private *dev_priv = dev->dev_private;
  6283. uint32_t tmp;
  6284. if (!intel_display_power_is_enabled(dev_priv,
  6285. POWER_DOMAIN_PIPE(crtc->pipe)))
  6286. return false;
  6287. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6288. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6289. tmp = I915_READ(PIPECONF(crtc->pipe));
  6290. if (!(tmp & PIPECONF_ENABLE))
  6291. return false;
  6292. switch (tmp & PIPECONF_BPC_MASK) {
  6293. case PIPECONF_6BPC:
  6294. pipe_config->pipe_bpp = 18;
  6295. break;
  6296. case PIPECONF_8BPC:
  6297. pipe_config->pipe_bpp = 24;
  6298. break;
  6299. case PIPECONF_10BPC:
  6300. pipe_config->pipe_bpp = 30;
  6301. break;
  6302. case PIPECONF_12BPC:
  6303. pipe_config->pipe_bpp = 36;
  6304. break;
  6305. default:
  6306. break;
  6307. }
  6308. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6309. pipe_config->limited_color_range = true;
  6310. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6311. struct intel_shared_dpll *pll;
  6312. pipe_config->has_pch_encoder = true;
  6313. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6314. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6315. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6316. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6317. if (HAS_PCH_IBX(dev_priv->dev)) {
  6318. pipe_config->shared_dpll =
  6319. (enum intel_dpll_id) crtc->pipe;
  6320. } else {
  6321. tmp = I915_READ(PCH_DPLL_SEL);
  6322. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6323. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6324. else
  6325. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6326. }
  6327. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6328. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6329. &pipe_config->dpll_hw_state));
  6330. tmp = pipe_config->dpll_hw_state.dpll;
  6331. pipe_config->pixel_multiplier =
  6332. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6333. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6334. ironlake_pch_clock_get(crtc, pipe_config);
  6335. } else {
  6336. pipe_config->pixel_multiplier = 1;
  6337. }
  6338. intel_get_pipe_timings(crtc, pipe_config);
  6339. ironlake_get_pfit_config(crtc, pipe_config);
  6340. return true;
  6341. }
  6342. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6343. {
  6344. struct drm_device *dev = dev_priv->dev;
  6345. struct intel_crtc *crtc;
  6346. for_each_intel_crtc(dev, crtc)
  6347. WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6348. pipe_name(crtc->pipe));
  6349. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6350. WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6351. WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6352. WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6353. WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6354. WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6355. "CPU PWM1 enabled\n");
  6356. if (IS_HASWELL(dev))
  6357. WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6358. "CPU PWM2 enabled\n");
  6359. WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6360. "PCH PWM1 enabled\n");
  6361. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6362. "Utility pin enabled\n");
  6363. WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6364. /*
  6365. * In theory we can still leave IRQs enabled, as long as only the HPD
  6366. * interrupts remain enabled. We used to check for that, but since it's
  6367. * gen-specific and since we only disable LCPLL after we fully disable
  6368. * the interrupts, the check below should be enough.
  6369. */
  6370. WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6371. }
  6372. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6373. {
  6374. struct drm_device *dev = dev_priv->dev;
  6375. if (IS_HASWELL(dev))
  6376. return I915_READ(D_COMP_HSW);
  6377. else
  6378. return I915_READ(D_COMP_BDW);
  6379. }
  6380. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6381. {
  6382. struct drm_device *dev = dev_priv->dev;
  6383. if (IS_HASWELL(dev)) {
  6384. mutex_lock(&dev_priv->rps.hw_lock);
  6385. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6386. val))
  6387. DRM_ERROR("Failed to write to D_COMP\n");
  6388. mutex_unlock(&dev_priv->rps.hw_lock);
  6389. } else {
  6390. I915_WRITE(D_COMP_BDW, val);
  6391. POSTING_READ(D_COMP_BDW);
  6392. }
  6393. }
  6394. /*
  6395. * This function implements pieces of two sequences from BSpec:
  6396. * - Sequence for display software to disable LCPLL
  6397. * - Sequence for display software to allow package C8+
  6398. * The steps implemented here are just the steps that actually touch the LCPLL
  6399. * register. Callers should take care of disabling all the display engine
  6400. * functions, doing the mode unset, fixing interrupts, etc.
  6401. */
  6402. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6403. bool switch_to_fclk, bool allow_power_down)
  6404. {
  6405. uint32_t val;
  6406. assert_can_disable_lcpll(dev_priv);
  6407. val = I915_READ(LCPLL_CTL);
  6408. if (switch_to_fclk) {
  6409. val |= LCPLL_CD_SOURCE_FCLK;
  6410. I915_WRITE(LCPLL_CTL, val);
  6411. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6412. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6413. DRM_ERROR("Switching to FCLK failed\n");
  6414. val = I915_READ(LCPLL_CTL);
  6415. }
  6416. val |= LCPLL_PLL_DISABLE;
  6417. I915_WRITE(LCPLL_CTL, val);
  6418. POSTING_READ(LCPLL_CTL);
  6419. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6420. DRM_ERROR("LCPLL still locked\n");
  6421. val = hsw_read_dcomp(dev_priv);
  6422. val |= D_COMP_COMP_DISABLE;
  6423. hsw_write_dcomp(dev_priv, val);
  6424. ndelay(100);
  6425. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6426. 1))
  6427. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6428. if (allow_power_down) {
  6429. val = I915_READ(LCPLL_CTL);
  6430. val |= LCPLL_POWER_DOWN_ALLOW;
  6431. I915_WRITE(LCPLL_CTL, val);
  6432. POSTING_READ(LCPLL_CTL);
  6433. }
  6434. }
  6435. /*
  6436. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6437. * source.
  6438. */
  6439. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6440. {
  6441. uint32_t val;
  6442. val = I915_READ(LCPLL_CTL);
  6443. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6444. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6445. return;
  6446. /*
  6447. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6448. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6449. *
  6450. * The other problem is that hsw_restore_lcpll() is called as part of
  6451. * the runtime PM resume sequence, so we can't just call
  6452. * gen6_gt_force_wake_get() because that function calls
  6453. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6454. * while we are on the resume sequence. So to solve this problem we have
  6455. * to call special forcewake code that doesn't touch runtime PM and
  6456. * doesn't enable the forcewake delayed work.
  6457. */
  6458. spin_lock_irq(&dev_priv->uncore.lock);
  6459. if (dev_priv->uncore.forcewake_count++ == 0)
  6460. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6461. spin_unlock_irq(&dev_priv->uncore.lock);
  6462. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6463. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6464. I915_WRITE(LCPLL_CTL, val);
  6465. POSTING_READ(LCPLL_CTL);
  6466. }
  6467. val = hsw_read_dcomp(dev_priv);
  6468. val |= D_COMP_COMP_FORCE;
  6469. val &= ~D_COMP_COMP_DISABLE;
  6470. hsw_write_dcomp(dev_priv, val);
  6471. val = I915_READ(LCPLL_CTL);
  6472. val &= ~LCPLL_PLL_DISABLE;
  6473. I915_WRITE(LCPLL_CTL, val);
  6474. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6475. DRM_ERROR("LCPLL not locked yet\n");
  6476. if (val & LCPLL_CD_SOURCE_FCLK) {
  6477. val = I915_READ(LCPLL_CTL);
  6478. val &= ~LCPLL_CD_SOURCE_FCLK;
  6479. I915_WRITE(LCPLL_CTL, val);
  6480. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6481. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6482. DRM_ERROR("Switching back to LCPLL failed\n");
  6483. }
  6484. /* See the big comment above. */
  6485. spin_lock_irq(&dev_priv->uncore.lock);
  6486. if (--dev_priv->uncore.forcewake_count == 0)
  6487. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6488. spin_unlock_irq(&dev_priv->uncore.lock);
  6489. }
  6490. /*
  6491. * Package states C8 and deeper are really deep PC states that can only be
  6492. * reached when all the devices on the system allow it, so even if the graphics
  6493. * device allows PC8+, it doesn't mean the system will actually get to these
  6494. * states. Our driver only allows PC8+ when going into runtime PM.
  6495. *
  6496. * The requirements for PC8+ are that all the outputs are disabled, the power
  6497. * well is disabled and most interrupts are disabled, and these are also
  6498. * requirements for runtime PM. When these conditions are met, we manually do
  6499. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6500. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6501. * hang the machine.
  6502. *
  6503. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6504. * the state of some registers, so when we come back from PC8+ we need to
  6505. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6506. * need to take care of the registers kept by RC6. Notice that this happens even
  6507. * if we don't put the device in PCI D3 state (which is what currently happens
  6508. * because of the runtime PM support).
  6509. *
  6510. * For more, read "Display Sequences for Package C8" on the hardware
  6511. * documentation.
  6512. */
  6513. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6514. {
  6515. struct drm_device *dev = dev_priv->dev;
  6516. uint32_t val;
  6517. DRM_DEBUG_KMS("Enabling package C8+\n");
  6518. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6519. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6520. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6521. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6522. }
  6523. lpt_disable_clkout_dp(dev);
  6524. hsw_disable_lcpll(dev_priv, true, true);
  6525. }
  6526. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6527. {
  6528. struct drm_device *dev = dev_priv->dev;
  6529. uint32_t val;
  6530. DRM_DEBUG_KMS("Disabling package C8+\n");
  6531. hsw_restore_lcpll(dev_priv);
  6532. lpt_init_pch_refclk(dev);
  6533. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6534. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6535. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6536. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6537. }
  6538. intel_prepare_ddi(dev);
  6539. }
  6540. static void snb_modeset_global_resources(struct drm_device *dev)
  6541. {
  6542. modeset_update_crtc_power_domains(dev);
  6543. }
  6544. static void haswell_modeset_global_resources(struct drm_device *dev)
  6545. {
  6546. modeset_update_crtc_power_domains(dev);
  6547. }
  6548. static int haswell_crtc_mode_set(struct drm_crtc *crtc,
  6549. int x, int y,
  6550. struct drm_framebuffer *fb)
  6551. {
  6552. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6553. if (!intel_ddi_pll_select(intel_crtc))
  6554. return -EINVAL;
  6555. intel_crtc->lowfreq_avail = false;
  6556. return 0;
  6557. }
  6558. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  6559. enum port port,
  6560. struct intel_crtc_config *pipe_config)
  6561. {
  6562. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6563. switch (pipe_config->ddi_pll_sel) {
  6564. case PORT_CLK_SEL_WRPLL1:
  6565. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6566. break;
  6567. case PORT_CLK_SEL_WRPLL2:
  6568. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6569. break;
  6570. }
  6571. }
  6572. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6573. struct intel_crtc_config *pipe_config)
  6574. {
  6575. struct drm_device *dev = crtc->base.dev;
  6576. struct drm_i915_private *dev_priv = dev->dev_private;
  6577. struct intel_shared_dpll *pll;
  6578. enum port port;
  6579. uint32_t tmp;
  6580. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6581. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  6582. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  6583. if (pipe_config->shared_dpll >= 0) {
  6584. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6585. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6586. &pipe_config->dpll_hw_state));
  6587. }
  6588. /*
  6589. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6590. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6591. * the PCH transcoder is on.
  6592. */
  6593. if (INTEL_INFO(dev)->gen < 9 &&
  6594. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6595. pipe_config->has_pch_encoder = true;
  6596. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6597. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6598. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6599. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6600. }
  6601. }
  6602. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6603. struct intel_crtc_config *pipe_config)
  6604. {
  6605. struct drm_device *dev = crtc->base.dev;
  6606. struct drm_i915_private *dev_priv = dev->dev_private;
  6607. enum intel_display_power_domain pfit_domain;
  6608. uint32_t tmp;
  6609. if (!intel_display_power_is_enabled(dev_priv,
  6610. POWER_DOMAIN_PIPE(crtc->pipe)))
  6611. return false;
  6612. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6613. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6614. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6615. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6616. enum pipe trans_edp_pipe;
  6617. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6618. default:
  6619. WARN(1, "unknown pipe linked to edp transcoder\n");
  6620. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6621. case TRANS_DDI_EDP_INPUT_A_ON:
  6622. trans_edp_pipe = PIPE_A;
  6623. break;
  6624. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6625. trans_edp_pipe = PIPE_B;
  6626. break;
  6627. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6628. trans_edp_pipe = PIPE_C;
  6629. break;
  6630. }
  6631. if (trans_edp_pipe == crtc->pipe)
  6632. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6633. }
  6634. if (!intel_display_power_is_enabled(dev_priv,
  6635. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6636. return false;
  6637. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6638. if (!(tmp & PIPECONF_ENABLE))
  6639. return false;
  6640. haswell_get_ddi_port_state(crtc, pipe_config);
  6641. intel_get_pipe_timings(crtc, pipe_config);
  6642. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6643. if (intel_display_power_is_enabled(dev_priv, pfit_domain))
  6644. ironlake_get_pfit_config(crtc, pipe_config);
  6645. if (IS_HASWELL(dev))
  6646. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6647. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6648. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  6649. pipe_config->pixel_multiplier =
  6650. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  6651. } else {
  6652. pipe_config->pixel_multiplier = 1;
  6653. }
  6654. return true;
  6655. }
  6656. static struct {
  6657. int clock;
  6658. u32 config;
  6659. } hdmi_audio_clock[] = {
  6660. { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  6661. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  6662. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  6663. { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  6664. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  6665. { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  6666. { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  6667. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  6668. { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  6669. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  6670. };
  6671. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  6672. static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
  6673. {
  6674. int i;
  6675. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  6676. if (mode->clock == hdmi_audio_clock[i].clock)
  6677. break;
  6678. }
  6679. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  6680. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
  6681. i = 1;
  6682. }
  6683. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  6684. hdmi_audio_clock[i].clock,
  6685. hdmi_audio_clock[i].config);
  6686. return hdmi_audio_clock[i].config;
  6687. }
  6688. static bool intel_eld_uptodate(struct drm_connector *connector,
  6689. int reg_eldv, uint32_t bits_eldv,
  6690. int reg_elda, uint32_t bits_elda,
  6691. int reg_edid)
  6692. {
  6693. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6694. uint8_t *eld = connector->eld;
  6695. uint32_t i;
  6696. i = I915_READ(reg_eldv);
  6697. i &= bits_eldv;
  6698. if (!eld[0])
  6699. return !i;
  6700. if (!i)
  6701. return false;
  6702. i = I915_READ(reg_elda);
  6703. i &= ~bits_elda;
  6704. I915_WRITE(reg_elda, i);
  6705. for (i = 0; i < eld[2]; i++)
  6706. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  6707. return false;
  6708. return true;
  6709. }
  6710. static void g4x_write_eld(struct drm_connector *connector,
  6711. struct drm_crtc *crtc,
  6712. struct drm_display_mode *mode)
  6713. {
  6714. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6715. uint8_t *eld = connector->eld;
  6716. uint32_t eldv;
  6717. uint32_t len;
  6718. uint32_t i;
  6719. i = I915_READ(G4X_AUD_VID_DID);
  6720. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  6721. eldv = G4X_ELDV_DEVCL_DEVBLC;
  6722. else
  6723. eldv = G4X_ELDV_DEVCTG;
  6724. if (intel_eld_uptodate(connector,
  6725. G4X_AUD_CNTL_ST, eldv,
  6726. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  6727. G4X_HDMIW_HDMIEDID))
  6728. return;
  6729. i = I915_READ(G4X_AUD_CNTL_ST);
  6730. i &= ~(eldv | G4X_ELD_ADDR);
  6731. len = (i >> 9) & 0x1f; /* ELD buffer size */
  6732. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6733. if (!eld[0])
  6734. return;
  6735. len = min_t(uint8_t, eld[2], len);
  6736. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6737. for (i = 0; i < len; i++)
  6738. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  6739. i = I915_READ(G4X_AUD_CNTL_ST);
  6740. i |= eldv;
  6741. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6742. }
  6743. static void haswell_write_eld(struct drm_connector *connector,
  6744. struct drm_crtc *crtc,
  6745. struct drm_display_mode *mode)
  6746. {
  6747. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6748. uint8_t *eld = connector->eld;
  6749. uint32_t eldv;
  6750. uint32_t i;
  6751. int len;
  6752. int pipe = to_intel_crtc(crtc)->pipe;
  6753. int tmp;
  6754. int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
  6755. int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
  6756. int aud_config = HSW_AUD_CFG(pipe);
  6757. int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
  6758. /* Audio output enable */
  6759. DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
  6760. tmp = I915_READ(aud_cntrl_st2);
  6761. tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
  6762. I915_WRITE(aud_cntrl_st2, tmp);
  6763. POSTING_READ(aud_cntrl_st2);
  6764. assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  6765. /* Set ELD valid state */
  6766. tmp = I915_READ(aud_cntrl_st2);
  6767. DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
  6768. tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
  6769. I915_WRITE(aud_cntrl_st2, tmp);
  6770. tmp = I915_READ(aud_cntrl_st2);
  6771. DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
  6772. /* Enable HDMI mode */
  6773. tmp = I915_READ(aud_config);
  6774. DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
  6775. /* clear N_programing_enable and N_value_index */
  6776. tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
  6777. I915_WRITE(aud_config, tmp);
  6778. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6779. eldv = AUDIO_ELD_VALID_A << (pipe * 4);
  6780. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6781. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6782. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6783. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6784. } else {
  6785. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6786. }
  6787. if (intel_eld_uptodate(connector,
  6788. aud_cntrl_st2, eldv,
  6789. aud_cntl_st, IBX_ELD_ADDRESS,
  6790. hdmiw_hdmiedid))
  6791. return;
  6792. i = I915_READ(aud_cntrl_st2);
  6793. i &= ~eldv;
  6794. I915_WRITE(aud_cntrl_st2, i);
  6795. if (!eld[0])
  6796. return;
  6797. i = I915_READ(aud_cntl_st);
  6798. i &= ~IBX_ELD_ADDRESS;
  6799. I915_WRITE(aud_cntl_st, i);
  6800. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  6801. DRM_DEBUG_DRIVER("port num:%d\n", i);
  6802. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6803. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6804. for (i = 0; i < len; i++)
  6805. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6806. i = I915_READ(aud_cntrl_st2);
  6807. i |= eldv;
  6808. I915_WRITE(aud_cntrl_st2, i);
  6809. }
  6810. static void ironlake_write_eld(struct drm_connector *connector,
  6811. struct drm_crtc *crtc,
  6812. struct drm_display_mode *mode)
  6813. {
  6814. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6815. uint8_t *eld = connector->eld;
  6816. uint32_t eldv;
  6817. uint32_t i;
  6818. int len;
  6819. int hdmiw_hdmiedid;
  6820. int aud_config;
  6821. int aud_cntl_st;
  6822. int aud_cntrl_st2;
  6823. int pipe = to_intel_crtc(crtc)->pipe;
  6824. if (HAS_PCH_IBX(connector->dev)) {
  6825. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  6826. aud_config = IBX_AUD_CFG(pipe);
  6827. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  6828. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  6829. } else if (IS_VALLEYVIEW(connector->dev)) {
  6830. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  6831. aud_config = VLV_AUD_CFG(pipe);
  6832. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  6833. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  6834. } else {
  6835. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  6836. aud_config = CPT_AUD_CFG(pipe);
  6837. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  6838. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  6839. }
  6840. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6841. if (IS_VALLEYVIEW(connector->dev)) {
  6842. struct intel_encoder *intel_encoder;
  6843. struct intel_digital_port *intel_dig_port;
  6844. intel_encoder = intel_attached_encoder(connector);
  6845. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  6846. i = intel_dig_port->port;
  6847. } else {
  6848. i = I915_READ(aud_cntl_st);
  6849. i = (i >> 29) & DIP_PORT_SEL_MASK;
  6850. /* DIP_Port_Select, 0x1 = PortB */
  6851. }
  6852. if (!i) {
  6853. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  6854. /* operate blindly on all ports */
  6855. eldv = IBX_ELD_VALIDB;
  6856. eldv |= IBX_ELD_VALIDB << 4;
  6857. eldv |= IBX_ELD_VALIDB << 8;
  6858. } else {
  6859. DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
  6860. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  6861. }
  6862. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6863. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6864. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6865. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6866. } else {
  6867. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6868. }
  6869. if (intel_eld_uptodate(connector,
  6870. aud_cntrl_st2, eldv,
  6871. aud_cntl_st, IBX_ELD_ADDRESS,
  6872. hdmiw_hdmiedid))
  6873. return;
  6874. i = I915_READ(aud_cntrl_st2);
  6875. i &= ~eldv;
  6876. I915_WRITE(aud_cntrl_st2, i);
  6877. if (!eld[0])
  6878. return;
  6879. i = I915_READ(aud_cntl_st);
  6880. i &= ~IBX_ELD_ADDRESS;
  6881. I915_WRITE(aud_cntl_st, i);
  6882. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6883. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6884. for (i = 0; i < len; i++)
  6885. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6886. i = I915_READ(aud_cntrl_st2);
  6887. i |= eldv;
  6888. I915_WRITE(aud_cntrl_st2, i);
  6889. }
  6890. void intel_write_eld(struct drm_encoder *encoder,
  6891. struct drm_display_mode *mode)
  6892. {
  6893. struct drm_crtc *crtc = encoder->crtc;
  6894. struct drm_connector *connector;
  6895. struct drm_device *dev = encoder->dev;
  6896. struct drm_i915_private *dev_priv = dev->dev_private;
  6897. connector = drm_select_eld(encoder, mode);
  6898. if (!connector)
  6899. return;
  6900. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6901. connector->base.id,
  6902. connector->name,
  6903. connector->encoder->base.id,
  6904. connector->encoder->name);
  6905. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  6906. if (dev_priv->display.write_eld)
  6907. dev_priv->display.write_eld(connector, crtc, mode);
  6908. }
  6909. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6910. {
  6911. struct drm_device *dev = crtc->dev;
  6912. struct drm_i915_private *dev_priv = dev->dev_private;
  6913. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6914. uint32_t cntl = 0, size = 0;
  6915. if (base) {
  6916. unsigned int width = intel_crtc->cursor_width;
  6917. unsigned int height = intel_crtc->cursor_height;
  6918. unsigned int stride = roundup_pow_of_two(width) * 4;
  6919. switch (stride) {
  6920. default:
  6921. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  6922. width, stride);
  6923. stride = 256;
  6924. /* fallthrough */
  6925. case 256:
  6926. case 512:
  6927. case 1024:
  6928. case 2048:
  6929. break;
  6930. }
  6931. cntl |= CURSOR_ENABLE |
  6932. CURSOR_GAMMA_ENABLE |
  6933. CURSOR_FORMAT_ARGB |
  6934. CURSOR_STRIDE(stride);
  6935. size = (height << 12) | width;
  6936. }
  6937. if (intel_crtc->cursor_cntl != 0 &&
  6938. (intel_crtc->cursor_base != base ||
  6939. intel_crtc->cursor_size != size ||
  6940. intel_crtc->cursor_cntl != cntl)) {
  6941. /* On these chipsets we can only modify the base/size/stride
  6942. * whilst the cursor is disabled.
  6943. */
  6944. I915_WRITE(_CURACNTR, 0);
  6945. POSTING_READ(_CURACNTR);
  6946. intel_crtc->cursor_cntl = 0;
  6947. }
  6948. if (intel_crtc->cursor_base != base) {
  6949. I915_WRITE(_CURABASE, base);
  6950. intel_crtc->cursor_base = base;
  6951. }
  6952. if (intel_crtc->cursor_size != size) {
  6953. I915_WRITE(CURSIZE, size);
  6954. intel_crtc->cursor_size = size;
  6955. }
  6956. if (intel_crtc->cursor_cntl != cntl) {
  6957. I915_WRITE(_CURACNTR, cntl);
  6958. POSTING_READ(_CURACNTR);
  6959. intel_crtc->cursor_cntl = cntl;
  6960. }
  6961. }
  6962. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6963. {
  6964. struct drm_device *dev = crtc->dev;
  6965. struct drm_i915_private *dev_priv = dev->dev_private;
  6966. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6967. int pipe = intel_crtc->pipe;
  6968. uint32_t cntl;
  6969. cntl = 0;
  6970. if (base) {
  6971. cntl = MCURSOR_GAMMA_ENABLE;
  6972. switch (intel_crtc->cursor_width) {
  6973. case 64:
  6974. cntl |= CURSOR_MODE_64_ARGB_AX;
  6975. break;
  6976. case 128:
  6977. cntl |= CURSOR_MODE_128_ARGB_AX;
  6978. break;
  6979. case 256:
  6980. cntl |= CURSOR_MODE_256_ARGB_AX;
  6981. break;
  6982. default:
  6983. WARN_ON(1);
  6984. return;
  6985. }
  6986. cntl |= pipe << 28; /* Connect to correct pipe */
  6987. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6988. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6989. }
  6990. if (intel_crtc->cursor_cntl != cntl) {
  6991. I915_WRITE(CURCNTR(pipe), cntl);
  6992. POSTING_READ(CURCNTR(pipe));
  6993. intel_crtc->cursor_cntl = cntl;
  6994. }
  6995. /* and commit changes on next vblank */
  6996. I915_WRITE(CURBASE(pipe), base);
  6997. POSTING_READ(CURBASE(pipe));
  6998. intel_crtc->cursor_base = base;
  6999. }
  7000. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  7001. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  7002. bool on)
  7003. {
  7004. struct drm_device *dev = crtc->dev;
  7005. struct drm_i915_private *dev_priv = dev->dev_private;
  7006. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7007. int pipe = intel_crtc->pipe;
  7008. int x = crtc->cursor_x;
  7009. int y = crtc->cursor_y;
  7010. u32 base = 0, pos = 0;
  7011. if (on)
  7012. base = intel_crtc->cursor_addr;
  7013. if (x >= intel_crtc->config.pipe_src_w)
  7014. base = 0;
  7015. if (y >= intel_crtc->config.pipe_src_h)
  7016. base = 0;
  7017. if (x < 0) {
  7018. if (x + intel_crtc->cursor_width <= 0)
  7019. base = 0;
  7020. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7021. x = -x;
  7022. }
  7023. pos |= x << CURSOR_X_SHIFT;
  7024. if (y < 0) {
  7025. if (y + intel_crtc->cursor_height <= 0)
  7026. base = 0;
  7027. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7028. y = -y;
  7029. }
  7030. pos |= y << CURSOR_Y_SHIFT;
  7031. if (base == 0 && intel_crtc->cursor_base == 0)
  7032. return;
  7033. I915_WRITE(CURPOS(pipe), pos);
  7034. if (IS_845G(dev) || IS_I865G(dev))
  7035. i845_update_cursor(crtc, base);
  7036. else
  7037. i9xx_update_cursor(crtc, base);
  7038. }
  7039. static bool cursor_size_ok(struct drm_device *dev,
  7040. uint32_t width, uint32_t height)
  7041. {
  7042. if (width == 0 || height == 0)
  7043. return false;
  7044. /*
  7045. * 845g/865g are special in that they are only limited by
  7046. * the width of their cursors, the height is arbitrary up to
  7047. * the precision of the register. Everything else requires
  7048. * square cursors, limited to a few power-of-two sizes.
  7049. */
  7050. if (IS_845G(dev) || IS_I865G(dev)) {
  7051. if ((width & 63) != 0)
  7052. return false;
  7053. if (width > (IS_845G(dev) ? 64 : 512))
  7054. return false;
  7055. if (height > 1023)
  7056. return false;
  7057. } else {
  7058. switch (width | height) {
  7059. case 256:
  7060. case 128:
  7061. if (IS_GEN2(dev))
  7062. return false;
  7063. case 64:
  7064. break;
  7065. default:
  7066. return false;
  7067. }
  7068. }
  7069. return true;
  7070. }
  7071. /*
  7072. * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
  7073. *
  7074. * Note that the object's reference will be consumed if the update fails. If
  7075. * the update succeeds, the reference of the old object (if any) will be
  7076. * consumed.
  7077. */
  7078. static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
  7079. struct drm_i915_gem_object *obj,
  7080. uint32_t width, uint32_t height)
  7081. {
  7082. struct drm_device *dev = crtc->dev;
  7083. struct drm_i915_private *dev_priv = dev->dev_private;
  7084. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7085. enum pipe pipe = intel_crtc->pipe;
  7086. unsigned old_width, stride;
  7087. uint32_t addr;
  7088. int ret;
  7089. /* if we want to turn off the cursor ignore width and height */
  7090. if (!obj) {
  7091. DRM_DEBUG_KMS("cursor off\n");
  7092. addr = 0;
  7093. mutex_lock(&dev->struct_mutex);
  7094. goto finish;
  7095. }
  7096. /* Check for which cursor types we support */
  7097. if (!cursor_size_ok(dev, width, height)) {
  7098. DRM_DEBUG("Cursor dimension not supported\n");
  7099. return -EINVAL;
  7100. }
  7101. stride = roundup_pow_of_two(width) * 4;
  7102. if (obj->base.size < stride * height) {
  7103. DRM_DEBUG_KMS("buffer is too small\n");
  7104. ret = -ENOMEM;
  7105. goto fail;
  7106. }
  7107. /* we only need to pin inside GTT if cursor is non-phy */
  7108. mutex_lock(&dev->struct_mutex);
  7109. if (!INTEL_INFO(dev)->cursor_needs_physical) {
  7110. unsigned alignment;
  7111. if (obj->tiling_mode) {
  7112. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  7113. ret = -EINVAL;
  7114. goto fail_locked;
  7115. }
  7116. /*
  7117. * Global gtt pte registers are special registers which actually
  7118. * forward writes to a chunk of system memory. Which means that
  7119. * there is no risk that the register values disappear as soon
  7120. * as we call intel_runtime_pm_put(), so it is correct to wrap
  7121. * only the pin/unpin/fence and not more.
  7122. */
  7123. intel_runtime_pm_get(dev_priv);
  7124. /* Note that the w/a also requires 2 PTE of padding following
  7125. * the bo. We currently fill all unused PTE with the shadow
  7126. * page and so we should always have valid PTE following the
  7127. * cursor preventing the VT-d warning.
  7128. */
  7129. alignment = 0;
  7130. if (need_vtd_wa(dev))
  7131. alignment = 64*1024;
  7132. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  7133. if (ret) {
  7134. DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
  7135. intel_runtime_pm_put(dev_priv);
  7136. goto fail_locked;
  7137. }
  7138. ret = i915_gem_object_put_fence(obj);
  7139. if (ret) {
  7140. DRM_DEBUG_KMS("failed to release fence for cursor");
  7141. intel_runtime_pm_put(dev_priv);
  7142. goto fail_unpin;
  7143. }
  7144. addr = i915_gem_obj_ggtt_offset(obj);
  7145. intel_runtime_pm_put(dev_priv);
  7146. } else {
  7147. int align = IS_I830(dev) ? 16 * 1024 : 256;
  7148. ret = i915_gem_object_attach_phys(obj, align);
  7149. if (ret) {
  7150. DRM_DEBUG_KMS("failed to attach phys object\n");
  7151. goto fail_locked;
  7152. }
  7153. addr = obj->phys_handle->busaddr;
  7154. }
  7155. finish:
  7156. if (intel_crtc->cursor_bo) {
  7157. if (!INTEL_INFO(dev)->cursor_needs_physical)
  7158. i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
  7159. }
  7160. i915_gem_track_fb(intel_crtc->cursor_bo, obj,
  7161. INTEL_FRONTBUFFER_CURSOR(pipe));
  7162. mutex_unlock(&dev->struct_mutex);
  7163. old_width = intel_crtc->cursor_width;
  7164. intel_crtc->cursor_addr = addr;
  7165. intel_crtc->cursor_bo = obj;
  7166. intel_crtc->cursor_width = width;
  7167. intel_crtc->cursor_height = height;
  7168. if (intel_crtc->active) {
  7169. if (old_width != width)
  7170. intel_update_watermarks(crtc);
  7171. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  7172. }
  7173. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
  7174. return 0;
  7175. fail_unpin:
  7176. i915_gem_object_unpin_from_display_plane(obj);
  7177. fail_locked:
  7178. mutex_unlock(&dev->struct_mutex);
  7179. fail:
  7180. drm_gem_object_unreference_unlocked(&obj->base);
  7181. return ret;
  7182. }
  7183. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  7184. u16 *blue, uint32_t start, uint32_t size)
  7185. {
  7186. int end = (start + size > 256) ? 256 : start + size, i;
  7187. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7188. for (i = start; i < end; i++) {
  7189. intel_crtc->lut_r[i] = red[i] >> 8;
  7190. intel_crtc->lut_g[i] = green[i] >> 8;
  7191. intel_crtc->lut_b[i] = blue[i] >> 8;
  7192. }
  7193. intel_crtc_load_lut(crtc);
  7194. }
  7195. /* VESA 640x480x72Hz mode to set on the pipe */
  7196. static struct drm_display_mode load_detect_mode = {
  7197. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7198. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7199. };
  7200. struct drm_framebuffer *
  7201. __intel_framebuffer_create(struct drm_device *dev,
  7202. struct drm_mode_fb_cmd2 *mode_cmd,
  7203. struct drm_i915_gem_object *obj)
  7204. {
  7205. struct intel_framebuffer *intel_fb;
  7206. int ret;
  7207. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7208. if (!intel_fb) {
  7209. drm_gem_object_unreference_unlocked(&obj->base);
  7210. return ERR_PTR(-ENOMEM);
  7211. }
  7212. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7213. if (ret)
  7214. goto err;
  7215. return &intel_fb->base;
  7216. err:
  7217. drm_gem_object_unreference_unlocked(&obj->base);
  7218. kfree(intel_fb);
  7219. return ERR_PTR(ret);
  7220. }
  7221. static struct drm_framebuffer *
  7222. intel_framebuffer_create(struct drm_device *dev,
  7223. struct drm_mode_fb_cmd2 *mode_cmd,
  7224. struct drm_i915_gem_object *obj)
  7225. {
  7226. struct drm_framebuffer *fb;
  7227. int ret;
  7228. ret = i915_mutex_lock_interruptible(dev);
  7229. if (ret)
  7230. return ERR_PTR(ret);
  7231. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7232. mutex_unlock(&dev->struct_mutex);
  7233. return fb;
  7234. }
  7235. static u32
  7236. intel_framebuffer_pitch_for_width(int width, int bpp)
  7237. {
  7238. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7239. return ALIGN(pitch, 64);
  7240. }
  7241. static u32
  7242. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7243. {
  7244. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7245. return PAGE_ALIGN(pitch * mode->vdisplay);
  7246. }
  7247. static struct drm_framebuffer *
  7248. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7249. struct drm_display_mode *mode,
  7250. int depth, int bpp)
  7251. {
  7252. struct drm_i915_gem_object *obj;
  7253. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7254. obj = i915_gem_alloc_object(dev,
  7255. intel_framebuffer_size_for_mode(mode, bpp));
  7256. if (obj == NULL)
  7257. return ERR_PTR(-ENOMEM);
  7258. mode_cmd.width = mode->hdisplay;
  7259. mode_cmd.height = mode->vdisplay;
  7260. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7261. bpp);
  7262. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7263. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7264. }
  7265. static struct drm_framebuffer *
  7266. mode_fits_in_fbdev(struct drm_device *dev,
  7267. struct drm_display_mode *mode)
  7268. {
  7269. #ifdef CONFIG_DRM_I915_FBDEV
  7270. struct drm_i915_private *dev_priv = dev->dev_private;
  7271. struct drm_i915_gem_object *obj;
  7272. struct drm_framebuffer *fb;
  7273. if (!dev_priv->fbdev)
  7274. return NULL;
  7275. if (!dev_priv->fbdev->fb)
  7276. return NULL;
  7277. obj = dev_priv->fbdev->fb->obj;
  7278. BUG_ON(!obj);
  7279. fb = &dev_priv->fbdev->fb->base;
  7280. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7281. fb->bits_per_pixel))
  7282. return NULL;
  7283. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7284. return NULL;
  7285. return fb;
  7286. #else
  7287. return NULL;
  7288. #endif
  7289. }
  7290. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7291. struct drm_display_mode *mode,
  7292. struct intel_load_detect_pipe *old,
  7293. struct drm_modeset_acquire_ctx *ctx)
  7294. {
  7295. struct intel_crtc *intel_crtc;
  7296. struct intel_encoder *intel_encoder =
  7297. intel_attached_encoder(connector);
  7298. struct drm_crtc *possible_crtc;
  7299. struct drm_encoder *encoder = &intel_encoder->base;
  7300. struct drm_crtc *crtc = NULL;
  7301. struct drm_device *dev = encoder->dev;
  7302. struct drm_framebuffer *fb;
  7303. struct drm_mode_config *config = &dev->mode_config;
  7304. int ret, i = -1;
  7305. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7306. connector->base.id, connector->name,
  7307. encoder->base.id, encoder->name);
  7308. retry:
  7309. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7310. if (ret)
  7311. goto fail_unlock;
  7312. /*
  7313. * Algorithm gets a little messy:
  7314. *
  7315. * - if the connector already has an assigned crtc, use it (but make
  7316. * sure it's on first)
  7317. *
  7318. * - try to find the first unused crtc that can drive this connector,
  7319. * and use that if we find one
  7320. */
  7321. /* See if we already have a CRTC for this connector */
  7322. if (encoder->crtc) {
  7323. crtc = encoder->crtc;
  7324. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7325. if (ret)
  7326. goto fail_unlock;
  7327. old->dpms_mode = connector->dpms;
  7328. old->load_detect_temp = false;
  7329. /* Make sure the crtc and connector are running */
  7330. if (connector->dpms != DRM_MODE_DPMS_ON)
  7331. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7332. return true;
  7333. }
  7334. /* Find an unused one (if possible) */
  7335. for_each_crtc(dev, possible_crtc) {
  7336. i++;
  7337. if (!(encoder->possible_crtcs & (1 << i)))
  7338. continue;
  7339. if (possible_crtc->enabled)
  7340. continue;
  7341. /* This can occur when applying the pipe A quirk on resume. */
  7342. if (to_intel_crtc(possible_crtc)->new_enabled)
  7343. continue;
  7344. crtc = possible_crtc;
  7345. break;
  7346. }
  7347. /*
  7348. * If we didn't find an unused CRTC, don't use any.
  7349. */
  7350. if (!crtc) {
  7351. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7352. goto fail_unlock;
  7353. }
  7354. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7355. if (ret)
  7356. goto fail_unlock;
  7357. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7358. to_intel_connector(connector)->new_encoder = intel_encoder;
  7359. intel_crtc = to_intel_crtc(crtc);
  7360. intel_crtc->new_enabled = true;
  7361. intel_crtc->new_config = &intel_crtc->config;
  7362. old->dpms_mode = connector->dpms;
  7363. old->load_detect_temp = true;
  7364. old->release_fb = NULL;
  7365. if (!mode)
  7366. mode = &load_detect_mode;
  7367. /* We need a framebuffer large enough to accommodate all accesses
  7368. * that the plane may generate whilst we perform load detection.
  7369. * We can not rely on the fbcon either being present (we get called
  7370. * during its initialisation to detect all boot displays, or it may
  7371. * not even exist) or that it is large enough to satisfy the
  7372. * requested mode.
  7373. */
  7374. fb = mode_fits_in_fbdev(dev, mode);
  7375. if (fb == NULL) {
  7376. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7377. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7378. old->release_fb = fb;
  7379. } else
  7380. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7381. if (IS_ERR(fb)) {
  7382. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7383. goto fail;
  7384. }
  7385. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7386. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7387. if (old->release_fb)
  7388. old->release_fb->funcs->destroy(old->release_fb);
  7389. goto fail;
  7390. }
  7391. /* let the connector get through one full cycle before testing */
  7392. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7393. return true;
  7394. fail:
  7395. intel_crtc->new_enabled = crtc->enabled;
  7396. if (intel_crtc->new_enabled)
  7397. intel_crtc->new_config = &intel_crtc->config;
  7398. else
  7399. intel_crtc->new_config = NULL;
  7400. fail_unlock:
  7401. if (ret == -EDEADLK) {
  7402. drm_modeset_backoff(ctx);
  7403. goto retry;
  7404. }
  7405. return false;
  7406. }
  7407. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7408. struct intel_load_detect_pipe *old)
  7409. {
  7410. struct intel_encoder *intel_encoder =
  7411. intel_attached_encoder(connector);
  7412. struct drm_encoder *encoder = &intel_encoder->base;
  7413. struct drm_crtc *crtc = encoder->crtc;
  7414. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7415. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7416. connector->base.id, connector->name,
  7417. encoder->base.id, encoder->name);
  7418. if (old->load_detect_temp) {
  7419. to_intel_connector(connector)->new_encoder = NULL;
  7420. intel_encoder->new_crtc = NULL;
  7421. intel_crtc->new_enabled = false;
  7422. intel_crtc->new_config = NULL;
  7423. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7424. if (old->release_fb) {
  7425. drm_framebuffer_unregister_private(old->release_fb);
  7426. drm_framebuffer_unreference(old->release_fb);
  7427. }
  7428. return;
  7429. }
  7430. /* Switch crtc and encoder back off if necessary */
  7431. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7432. connector->funcs->dpms(connector, old->dpms_mode);
  7433. }
  7434. static int i9xx_pll_refclk(struct drm_device *dev,
  7435. const struct intel_crtc_config *pipe_config)
  7436. {
  7437. struct drm_i915_private *dev_priv = dev->dev_private;
  7438. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7439. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7440. return dev_priv->vbt.lvds_ssc_freq;
  7441. else if (HAS_PCH_SPLIT(dev))
  7442. return 120000;
  7443. else if (!IS_GEN2(dev))
  7444. return 96000;
  7445. else
  7446. return 48000;
  7447. }
  7448. /* Returns the clock of the currently programmed mode of the given pipe. */
  7449. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7450. struct intel_crtc_config *pipe_config)
  7451. {
  7452. struct drm_device *dev = crtc->base.dev;
  7453. struct drm_i915_private *dev_priv = dev->dev_private;
  7454. int pipe = pipe_config->cpu_transcoder;
  7455. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7456. u32 fp;
  7457. intel_clock_t clock;
  7458. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7459. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7460. fp = pipe_config->dpll_hw_state.fp0;
  7461. else
  7462. fp = pipe_config->dpll_hw_state.fp1;
  7463. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7464. if (IS_PINEVIEW(dev)) {
  7465. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7466. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7467. } else {
  7468. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7469. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7470. }
  7471. if (!IS_GEN2(dev)) {
  7472. if (IS_PINEVIEW(dev))
  7473. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7474. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7475. else
  7476. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7477. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7478. switch (dpll & DPLL_MODE_MASK) {
  7479. case DPLLB_MODE_DAC_SERIAL:
  7480. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7481. 5 : 10;
  7482. break;
  7483. case DPLLB_MODE_LVDS:
  7484. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7485. 7 : 14;
  7486. break;
  7487. default:
  7488. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7489. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7490. return;
  7491. }
  7492. if (IS_PINEVIEW(dev))
  7493. pineview_clock(refclk, &clock);
  7494. else
  7495. i9xx_clock(refclk, &clock);
  7496. } else {
  7497. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7498. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7499. if (is_lvds) {
  7500. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7501. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7502. if (lvds & LVDS_CLKB_POWER_UP)
  7503. clock.p2 = 7;
  7504. else
  7505. clock.p2 = 14;
  7506. } else {
  7507. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7508. clock.p1 = 2;
  7509. else {
  7510. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7511. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7512. }
  7513. if (dpll & PLL_P2_DIVIDE_BY_4)
  7514. clock.p2 = 4;
  7515. else
  7516. clock.p2 = 2;
  7517. }
  7518. i9xx_clock(refclk, &clock);
  7519. }
  7520. /*
  7521. * This value includes pixel_multiplier. We will use
  7522. * port_clock to compute adjusted_mode.crtc_clock in the
  7523. * encoder's get_config() function.
  7524. */
  7525. pipe_config->port_clock = clock.dot;
  7526. }
  7527. int intel_dotclock_calculate(int link_freq,
  7528. const struct intel_link_m_n *m_n)
  7529. {
  7530. /*
  7531. * The calculation for the data clock is:
  7532. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7533. * But we want to avoid losing precison if possible, so:
  7534. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7535. *
  7536. * and the link clock is simpler:
  7537. * link_clock = (m * link_clock) / n
  7538. */
  7539. if (!m_n->link_n)
  7540. return 0;
  7541. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7542. }
  7543. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7544. struct intel_crtc_config *pipe_config)
  7545. {
  7546. struct drm_device *dev = crtc->base.dev;
  7547. /* read out port_clock from the DPLL */
  7548. i9xx_crtc_clock_get(crtc, pipe_config);
  7549. /*
  7550. * This value does not include pixel_multiplier.
  7551. * We will check that port_clock and adjusted_mode.crtc_clock
  7552. * agree once we know their relationship in the encoder's
  7553. * get_config() function.
  7554. */
  7555. pipe_config->adjusted_mode.crtc_clock =
  7556. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7557. &pipe_config->fdi_m_n);
  7558. }
  7559. /** Returns the currently programmed mode of the given pipe. */
  7560. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7561. struct drm_crtc *crtc)
  7562. {
  7563. struct drm_i915_private *dev_priv = dev->dev_private;
  7564. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7565. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7566. struct drm_display_mode *mode;
  7567. struct intel_crtc_config pipe_config;
  7568. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7569. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7570. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7571. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7572. enum pipe pipe = intel_crtc->pipe;
  7573. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7574. if (!mode)
  7575. return NULL;
  7576. /*
  7577. * Construct a pipe_config sufficient for getting the clock info
  7578. * back out of crtc_clock_get.
  7579. *
  7580. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7581. * to use a real value here instead.
  7582. */
  7583. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7584. pipe_config.pixel_multiplier = 1;
  7585. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7586. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7587. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7588. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7589. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7590. mode->hdisplay = (htot & 0xffff) + 1;
  7591. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7592. mode->hsync_start = (hsync & 0xffff) + 1;
  7593. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7594. mode->vdisplay = (vtot & 0xffff) + 1;
  7595. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7596. mode->vsync_start = (vsync & 0xffff) + 1;
  7597. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7598. drm_mode_set_name(mode);
  7599. return mode;
  7600. }
  7601. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7602. {
  7603. struct drm_device *dev = crtc->dev;
  7604. struct drm_i915_private *dev_priv = dev->dev_private;
  7605. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7606. if (!HAS_GMCH_DISPLAY(dev))
  7607. return;
  7608. if (!dev_priv->lvds_downclock_avail)
  7609. return;
  7610. /*
  7611. * Since this is called by a timer, we should never get here in
  7612. * the manual case.
  7613. */
  7614. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7615. int pipe = intel_crtc->pipe;
  7616. int dpll_reg = DPLL(pipe);
  7617. int dpll;
  7618. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7619. assert_panel_unlocked(dev_priv, pipe);
  7620. dpll = I915_READ(dpll_reg);
  7621. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7622. I915_WRITE(dpll_reg, dpll);
  7623. intel_wait_for_vblank(dev, pipe);
  7624. dpll = I915_READ(dpll_reg);
  7625. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7626. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7627. }
  7628. }
  7629. void intel_mark_busy(struct drm_device *dev)
  7630. {
  7631. struct drm_i915_private *dev_priv = dev->dev_private;
  7632. if (dev_priv->mm.busy)
  7633. return;
  7634. intel_runtime_pm_get(dev_priv);
  7635. i915_update_gfx_val(dev_priv);
  7636. dev_priv->mm.busy = true;
  7637. }
  7638. void intel_mark_idle(struct drm_device *dev)
  7639. {
  7640. struct drm_i915_private *dev_priv = dev->dev_private;
  7641. struct drm_crtc *crtc;
  7642. if (!dev_priv->mm.busy)
  7643. return;
  7644. dev_priv->mm.busy = false;
  7645. if (!i915.powersave)
  7646. goto out;
  7647. for_each_crtc(dev, crtc) {
  7648. if (!crtc->primary->fb)
  7649. continue;
  7650. intel_decrease_pllclock(crtc);
  7651. }
  7652. if (INTEL_INFO(dev)->gen >= 6)
  7653. gen6_rps_idle(dev->dev_private);
  7654. out:
  7655. intel_runtime_pm_put(dev_priv);
  7656. }
  7657. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7658. {
  7659. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7660. struct drm_device *dev = crtc->dev;
  7661. struct intel_unpin_work *work;
  7662. spin_lock_irq(&dev->event_lock);
  7663. work = intel_crtc->unpin_work;
  7664. intel_crtc->unpin_work = NULL;
  7665. spin_unlock_irq(&dev->event_lock);
  7666. if (work) {
  7667. cancel_work_sync(&work->work);
  7668. kfree(work);
  7669. }
  7670. drm_crtc_cleanup(crtc);
  7671. kfree(intel_crtc);
  7672. }
  7673. static void intel_unpin_work_fn(struct work_struct *__work)
  7674. {
  7675. struct intel_unpin_work *work =
  7676. container_of(__work, struct intel_unpin_work, work);
  7677. struct drm_device *dev = work->crtc->dev;
  7678. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7679. mutex_lock(&dev->struct_mutex);
  7680. intel_unpin_fb_obj(work->old_fb_obj);
  7681. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7682. drm_gem_object_unreference(&work->old_fb_obj->base);
  7683. intel_update_fbc(dev);
  7684. mutex_unlock(&dev->struct_mutex);
  7685. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7686. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7687. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7688. kfree(work);
  7689. }
  7690. static void do_intel_finish_page_flip(struct drm_device *dev,
  7691. struct drm_crtc *crtc)
  7692. {
  7693. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7694. struct intel_unpin_work *work;
  7695. unsigned long flags;
  7696. /* Ignore early vblank irqs */
  7697. if (intel_crtc == NULL)
  7698. return;
  7699. /*
  7700. * This is called both by irq handlers and the reset code (to complete
  7701. * lost pageflips) so needs the full irqsave spinlocks.
  7702. */
  7703. spin_lock_irqsave(&dev->event_lock, flags);
  7704. work = intel_crtc->unpin_work;
  7705. /* Ensure we don't miss a work->pending update ... */
  7706. smp_rmb();
  7707. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7708. spin_unlock_irqrestore(&dev->event_lock, flags);
  7709. return;
  7710. }
  7711. page_flip_completed(intel_crtc);
  7712. spin_unlock_irqrestore(&dev->event_lock, flags);
  7713. }
  7714. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7715. {
  7716. struct drm_i915_private *dev_priv = dev->dev_private;
  7717. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7718. do_intel_finish_page_flip(dev, crtc);
  7719. }
  7720. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7721. {
  7722. struct drm_i915_private *dev_priv = dev->dev_private;
  7723. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7724. do_intel_finish_page_flip(dev, crtc);
  7725. }
  7726. /* Is 'a' after or equal to 'b'? */
  7727. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7728. {
  7729. return !((a - b) & 0x80000000);
  7730. }
  7731. static bool page_flip_finished(struct intel_crtc *crtc)
  7732. {
  7733. struct drm_device *dev = crtc->base.dev;
  7734. struct drm_i915_private *dev_priv = dev->dev_private;
  7735. /*
  7736. * The relevant registers doen't exist on pre-ctg.
  7737. * As the flip done interrupt doesn't trigger for mmio
  7738. * flips on gmch platforms, a flip count check isn't
  7739. * really needed there. But since ctg has the registers,
  7740. * include it in the check anyway.
  7741. */
  7742. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7743. return true;
  7744. /*
  7745. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7746. * used the same base address. In that case the mmio flip might
  7747. * have completed, but the CS hasn't even executed the flip yet.
  7748. *
  7749. * A flip count check isn't enough as the CS might have updated
  7750. * the base address just after start of vblank, but before we
  7751. * managed to process the interrupt. This means we'd complete the
  7752. * CS flip too soon.
  7753. *
  7754. * Combining both checks should get us a good enough result. It may
  7755. * still happen that the CS flip has been executed, but has not
  7756. * yet actually completed. But in case the base address is the same
  7757. * anyway, we don't really care.
  7758. */
  7759. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7760. crtc->unpin_work->gtt_offset &&
  7761. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7762. crtc->unpin_work->flip_count);
  7763. }
  7764. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7765. {
  7766. struct drm_i915_private *dev_priv = dev->dev_private;
  7767. struct intel_crtc *intel_crtc =
  7768. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7769. unsigned long flags;
  7770. /*
  7771. * This is called both by irq handlers and the reset code (to complete
  7772. * lost pageflips) so needs the full irqsave spinlocks.
  7773. *
  7774. * NB: An MMIO update of the plane base pointer will also
  7775. * generate a page-flip completion irq, i.e. every modeset
  7776. * is also accompanied by a spurious intel_prepare_page_flip().
  7777. */
  7778. spin_lock_irqsave(&dev->event_lock, flags);
  7779. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7780. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7781. spin_unlock_irqrestore(&dev->event_lock, flags);
  7782. }
  7783. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7784. {
  7785. /* Ensure that the work item is consistent when activating it ... */
  7786. smp_wmb();
  7787. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7788. /* and that it is marked active as soon as the irq could fire. */
  7789. smp_wmb();
  7790. }
  7791. static int intel_gen2_queue_flip(struct drm_device *dev,
  7792. struct drm_crtc *crtc,
  7793. struct drm_framebuffer *fb,
  7794. struct drm_i915_gem_object *obj,
  7795. struct intel_engine_cs *ring,
  7796. uint32_t flags)
  7797. {
  7798. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7799. u32 flip_mask;
  7800. int ret;
  7801. ret = intel_ring_begin(ring, 6);
  7802. if (ret)
  7803. return ret;
  7804. /* Can't queue multiple flips, so wait for the previous
  7805. * one to finish before executing the next.
  7806. */
  7807. if (intel_crtc->plane)
  7808. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7809. else
  7810. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7811. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7812. intel_ring_emit(ring, MI_NOOP);
  7813. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7814. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7815. intel_ring_emit(ring, fb->pitches[0]);
  7816. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7817. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7818. intel_mark_page_flip_active(intel_crtc);
  7819. __intel_ring_advance(ring);
  7820. return 0;
  7821. }
  7822. static int intel_gen3_queue_flip(struct drm_device *dev,
  7823. struct drm_crtc *crtc,
  7824. struct drm_framebuffer *fb,
  7825. struct drm_i915_gem_object *obj,
  7826. struct intel_engine_cs *ring,
  7827. uint32_t flags)
  7828. {
  7829. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7830. u32 flip_mask;
  7831. int ret;
  7832. ret = intel_ring_begin(ring, 6);
  7833. if (ret)
  7834. return ret;
  7835. if (intel_crtc->plane)
  7836. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7837. else
  7838. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7839. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7840. intel_ring_emit(ring, MI_NOOP);
  7841. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7842. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7843. intel_ring_emit(ring, fb->pitches[0]);
  7844. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7845. intel_ring_emit(ring, MI_NOOP);
  7846. intel_mark_page_flip_active(intel_crtc);
  7847. __intel_ring_advance(ring);
  7848. return 0;
  7849. }
  7850. static int intel_gen4_queue_flip(struct drm_device *dev,
  7851. struct drm_crtc *crtc,
  7852. struct drm_framebuffer *fb,
  7853. struct drm_i915_gem_object *obj,
  7854. struct intel_engine_cs *ring,
  7855. uint32_t flags)
  7856. {
  7857. struct drm_i915_private *dev_priv = dev->dev_private;
  7858. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7859. uint32_t pf, pipesrc;
  7860. int ret;
  7861. ret = intel_ring_begin(ring, 4);
  7862. if (ret)
  7863. return ret;
  7864. /* i965+ uses the linear or tiled offsets from the
  7865. * Display Registers (which do not change across a page-flip)
  7866. * so we need only reprogram the base address.
  7867. */
  7868. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7869. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7870. intel_ring_emit(ring, fb->pitches[0]);
  7871. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7872. obj->tiling_mode);
  7873. /* XXX Enabling the panel-fitter across page-flip is so far
  7874. * untested on non-native modes, so ignore it for now.
  7875. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7876. */
  7877. pf = 0;
  7878. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7879. intel_ring_emit(ring, pf | pipesrc);
  7880. intel_mark_page_flip_active(intel_crtc);
  7881. __intel_ring_advance(ring);
  7882. return 0;
  7883. }
  7884. static int intel_gen6_queue_flip(struct drm_device *dev,
  7885. struct drm_crtc *crtc,
  7886. struct drm_framebuffer *fb,
  7887. struct drm_i915_gem_object *obj,
  7888. struct intel_engine_cs *ring,
  7889. uint32_t flags)
  7890. {
  7891. struct drm_i915_private *dev_priv = dev->dev_private;
  7892. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7893. uint32_t pf, pipesrc;
  7894. int ret;
  7895. ret = intel_ring_begin(ring, 4);
  7896. if (ret)
  7897. return ret;
  7898. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7899. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7900. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7901. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7902. /* Contrary to the suggestions in the documentation,
  7903. * "Enable Panel Fitter" does not seem to be required when page
  7904. * flipping with a non-native mode, and worse causes a normal
  7905. * modeset to fail.
  7906. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7907. */
  7908. pf = 0;
  7909. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7910. intel_ring_emit(ring, pf | pipesrc);
  7911. intel_mark_page_flip_active(intel_crtc);
  7912. __intel_ring_advance(ring);
  7913. return 0;
  7914. }
  7915. static int intel_gen7_queue_flip(struct drm_device *dev,
  7916. struct drm_crtc *crtc,
  7917. struct drm_framebuffer *fb,
  7918. struct drm_i915_gem_object *obj,
  7919. struct intel_engine_cs *ring,
  7920. uint32_t flags)
  7921. {
  7922. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7923. uint32_t plane_bit = 0;
  7924. int len, ret;
  7925. switch (intel_crtc->plane) {
  7926. case PLANE_A:
  7927. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7928. break;
  7929. case PLANE_B:
  7930. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7931. break;
  7932. case PLANE_C:
  7933. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7934. break;
  7935. default:
  7936. WARN_ONCE(1, "unknown plane in flip command\n");
  7937. return -ENODEV;
  7938. }
  7939. len = 4;
  7940. if (ring->id == RCS) {
  7941. len += 6;
  7942. /*
  7943. * On Gen 8, SRM is now taking an extra dword to accommodate
  7944. * 48bits addresses, and we need a NOOP for the batch size to
  7945. * stay even.
  7946. */
  7947. if (IS_GEN8(dev))
  7948. len += 2;
  7949. }
  7950. /*
  7951. * BSpec MI_DISPLAY_FLIP for IVB:
  7952. * "The full packet must be contained within the same cache line."
  7953. *
  7954. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7955. * cacheline, if we ever start emitting more commands before
  7956. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7957. * then do the cacheline alignment, and finally emit the
  7958. * MI_DISPLAY_FLIP.
  7959. */
  7960. ret = intel_ring_cacheline_align(ring);
  7961. if (ret)
  7962. return ret;
  7963. ret = intel_ring_begin(ring, len);
  7964. if (ret)
  7965. return ret;
  7966. /* Unmask the flip-done completion message. Note that the bspec says that
  7967. * we should do this for both the BCS and RCS, and that we must not unmask
  7968. * more than one flip event at any time (or ensure that one flip message
  7969. * can be sent by waiting for flip-done prior to queueing new flips).
  7970. * Experimentation says that BCS works despite DERRMR masking all
  7971. * flip-done completion events and that unmasking all planes at once
  7972. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7973. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7974. */
  7975. if (ring->id == RCS) {
  7976. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7977. intel_ring_emit(ring, DERRMR);
  7978. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7979. DERRMR_PIPEB_PRI_FLIP_DONE |
  7980. DERRMR_PIPEC_PRI_FLIP_DONE));
  7981. if (IS_GEN8(dev))
  7982. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7983. MI_SRM_LRM_GLOBAL_GTT);
  7984. else
  7985. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7986. MI_SRM_LRM_GLOBAL_GTT);
  7987. intel_ring_emit(ring, DERRMR);
  7988. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7989. if (IS_GEN8(dev)) {
  7990. intel_ring_emit(ring, 0);
  7991. intel_ring_emit(ring, MI_NOOP);
  7992. }
  7993. }
  7994. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7995. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7996. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7997. intel_ring_emit(ring, (MI_NOOP));
  7998. intel_mark_page_flip_active(intel_crtc);
  7999. __intel_ring_advance(ring);
  8000. return 0;
  8001. }
  8002. static bool use_mmio_flip(struct intel_engine_cs *ring,
  8003. struct drm_i915_gem_object *obj)
  8004. {
  8005. /*
  8006. * This is not being used for older platforms, because
  8007. * non-availability of flip done interrupt forces us to use
  8008. * CS flips. Older platforms derive flip done using some clever
  8009. * tricks involving the flip_pending status bits and vblank irqs.
  8010. * So using MMIO flips there would disrupt this mechanism.
  8011. */
  8012. if (ring == NULL)
  8013. return true;
  8014. if (INTEL_INFO(ring->dev)->gen < 5)
  8015. return false;
  8016. if (i915.use_mmio_flip < 0)
  8017. return false;
  8018. else if (i915.use_mmio_flip > 0)
  8019. return true;
  8020. else if (i915.enable_execlists)
  8021. return true;
  8022. else
  8023. return ring != obj->ring;
  8024. }
  8025. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  8026. {
  8027. struct drm_device *dev = intel_crtc->base.dev;
  8028. struct drm_i915_private *dev_priv = dev->dev_private;
  8029. struct intel_framebuffer *intel_fb =
  8030. to_intel_framebuffer(intel_crtc->base.primary->fb);
  8031. struct drm_i915_gem_object *obj = intel_fb->obj;
  8032. u32 dspcntr;
  8033. u32 reg;
  8034. intel_mark_page_flip_active(intel_crtc);
  8035. reg = DSPCNTR(intel_crtc->plane);
  8036. dspcntr = I915_READ(reg);
  8037. if (INTEL_INFO(dev)->gen >= 4) {
  8038. if (obj->tiling_mode != I915_TILING_NONE)
  8039. dspcntr |= DISPPLANE_TILED;
  8040. else
  8041. dspcntr &= ~DISPPLANE_TILED;
  8042. }
  8043. I915_WRITE(reg, dspcntr);
  8044. I915_WRITE(DSPSURF(intel_crtc->plane),
  8045. intel_crtc->unpin_work->gtt_offset);
  8046. POSTING_READ(DSPSURF(intel_crtc->plane));
  8047. }
  8048. static int intel_postpone_flip(struct drm_i915_gem_object *obj)
  8049. {
  8050. struct intel_engine_cs *ring;
  8051. int ret;
  8052. lockdep_assert_held(&obj->base.dev->struct_mutex);
  8053. if (!obj->last_write_seqno)
  8054. return 0;
  8055. ring = obj->ring;
  8056. if (i915_seqno_passed(ring->get_seqno(ring, true),
  8057. obj->last_write_seqno))
  8058. return 0;
  8059. ret = i915_gem_check_olr(ring, obj->last_write_seqno);
  8060. if (ret)
  8061. return ret;
  8062. if (WARN_ON(!ring->irq_get(ring)))
  8063. return 0;
  8064. return 1;
  8065. }
  8066. void intel_notify_mmio_flip(struct intel_engine_cs *ring)
  8067. {
  8068. struct drm_i915_private *dev_priv = to_i915(ring->dev);
  8069. struct intel_crtc *intel_crtc;
  8070. unsigned long irq_flags;
  8071. u32 seqno;
  8072. seqno = ring->get_seqno(ring, false);
  8073. spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
  8074. for_each_intel_crtc(ring->dev, intel_crtc) {
  8075. struct intel_mmio_flip *mmio_flip;
  8076. mmio_flip = &intel_crtc->mmio_flip;
  8077. if (mmio_flip->seqno == 0)
  8078. continue;
  8079. if (ring->id != mmio_flip->ring_id)
  8080. continue;
  8081. if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
  8082. intel_do_mmio_flip(intel_crtc);
  8083. mmio_flip->seqno = 0;
  8084. ring->irq_put(ring);
  8085. }
  8086. }
  8087. spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
  8088. }
  8089. static int intel_queue_mmio_flip(struct drm_device *dev,
  8090. struct drm_crtc *crtc,
  8091. struct drm_framebuffer *fb,
  8092. struct drm_i915_gem_object *obj,
  8093. struct intel_engine_cs *ring,
  8094. uint32_t flags)
  8095. {
  8096. struct drm_i915_private *dev_priv = dev->dev_private;
  8097. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8098. int ret;
  8099. if (WARN_ON(intel_crtc->mmio_flip.seqno))
  8100. return -EBUSY;
  8101. ret = intel_postpone_flip(obj);
  8102. if (ret < 0)
  8103. return ret;
  8104. if (ret == 0) {
  8105. intel_do_mmio_flip(intel_crtc);
  8106. return 0;
  8107. }
  8108. spin_lock_irq(&dev_priv->mmio_flip_lock);
  8109. intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
  8110. intel_crtc->mmio_flip.ring_id = obj->ring->id;
  8111. spin_unlock_irq(&dev_priv->mmio_flip_lock);
  8112. /*
  8113. * Double check to catch cases where irq fired before
  8114. * mmio flip data was ready
  8115. */
  8116. intel_notify_mmio_flip(obj->ring);
  8117. return 0;
  8118. }
  8119. static int intel_default_queue_flip(struct drm_device *dev,
  8120. struct drm_crtc *crtc,
  8121. struct drm_framebuffer *fb,
  8122. struct drm_i915_gem_object *obj,
  8123. struct intel_engine_cs *ring,
  8124. uint32_t flags)
  8125. {
  8126. return -ENODEV;
  8127. }
  8128. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  8129. struct drm_crtc *crtc)
  8130. {
  8131. struct drm_i915_private *dev_priv = dev->dev_private;
  8132. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8133. struct intel_unpin_work *work = intel_crtc->unpin_work;
  8134. u32 addr;
  8135. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  8136. return true;
  8137. if (!work->enable_stall_check)
  8138. return false;
  8139. if (work->flip_ready_vblank == 0) {
  8140. if (work->flip_queued_ring &&
  8141. !i915_seqno_passed(work->flip_queued_ring->get_seqno(work->flip_queued_ring, true),
  8142. work->flip_queued_seqno))
  8143. return false;
  8144. work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8145. }
  8146. if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
  8147. return false;
  8148. /* Potential stall - if we see that the flip has happened,
  8149. * assume a missed interrupt. */
  8150. if (INTEL_INFO(dev)->gen >= 4)
  8151. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8152. else
  8153. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8154. /* There is a potential issue here with a false positive after a flip
  8155. * to the same address. We could address this by checking for a
  8156. * non-incrementing frame counter.
  8157. */
  8158. return addr == work->gtt_offset;
  8159. }
  8160. void intel_check_page_flip(struct drm_device *dev, int pipe)
  8161. {
  8162. struct drm_i915_private *dev_priv = dev->dev_private;
  8163. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8164. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8165. WARN_ON(!in_irq());
  8166. if (crtc == NULL)
  8167. return;
  8168. spin_lock(&dev->event_lock);
  8169. if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
  8170. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  8171. intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  8172. page_flip_completed(intel_crtc);
  8173. }
  8174. spin_unlock(&dev->event_lock);
  8175. }
  8176. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8177. struct drm_framebuffer *fb,
  8178. struct drm_pending_vblank_event *event,
  8179. uint32_t page_flip_flags)
  8180. {
  8181. struct drm_device *dev = crtc->dev;
  8182. struct drm_i915_private *dev_priv = dev->dev_private;
  8183. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8184. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8185. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8186. enum pipe pipe = intel_crtc->pipe;
  8187. struct intel_unpin_work *work;
  8188. struct intel_engine_cs *ring;
  8189. int ret;
  8190. /*
  8191. * drm_mode_page_flip_ioctl() should already catch this, but double
  8192. * check to be safe. In the future we may enable pageflipping from
  8193. * a disabled primary plane.
  8194. */
  8195. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8196. return -EBUSY;
  8197. /* Can't change pixel format via MI display flips. */
  8198. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8199. return -EINVAL;
  8200. /*
  8201. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8202. * Note that pitch changes could also affect these register.
  8203. */
  8204. if (INTEL_INFO(dev)->gen > 3 &&
  8205. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8206. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8207. return -EINVAL;
  8208. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8209. goto out_hang;
  8210. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8211. if (work == NULL)
  8212. return -ENOMEM;
  8213. work->event = event;
  8214. work->crtc = crtc;
  8215. work->old_fb_obj = intel_fb_obj(old_fb);
  8216. INIT_WORK(&work->work, intel_unpin_work_fn);
  8217. ret = drm_crtc_vblank_get(crtc);
  8218. if (ret)
  8219. goto free_work;
  8220. /* We borrow the event spin lock for protecting unpin_work */
  8221. spin_lock_irq(&dev->event_lock);
  8222. if (intel_crtc->unpin_work) {
  8223. /* Before declaring the flip queue wedged, check if
  8224. * the hardware completed the operation behind our backs.
  8225. */
  8226. if (__intel_pageflip_stall_check(dev, crtc)) {
  8227. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8228. page_flip_completed(intel_crtc);
  8229. } else {
  8230. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8231. spin_unlock_irq(&dev->event_lock);
  8232. drm_crtc_vblank_put(crtc);
  8233. kfree(work);
  8234. return -EBUSY;
  8235. }
  8236. }
  8237. intel_crtc->unpin_work = work;
  8238. spin_unlock_irq(&dev->event_lock);
  8239. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8240. flush_workqueue(dev_priv->wq);
  8241. ret = i915_mutex_lock_interruptible(dev);
  8242. if (ret)
  8243. goto cleanup;
  8244. /* Reference the objects for the scheduled work. */
  8245. drm_gem_object_reference(&work->old_fb_obj->base);
  8246. drm_gem_object_reference(&obj->base);
  8247. crtc->primary->fb = fb;
  8248. work->pending_flip_obj = obj;
  8249. atomic_inc(&intel_crtc->unpin_work_count);
  8250. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8251. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8252. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8253. if (IS_VALLEYVIEW(dev)) {
  8254. ring = &dev_priv->ring[BCS];
  8255. if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
  8256. /* vlv: DISPLAY_FLIP fails to change tiling */
  8257. ring = NULL;
  8258. } else if (IS_IVYBRIDGE(dev)) {
  8259. ring = &dev_priv->ring[BCS];
  8260. } else if (INTEL_INFO(dev)->gen >= 7) {
  8261. ring = obj->ring;
  8262. if (ring == NULL || ring->id != RCS)
  8263. ring = &dev_priv->ring[BCS];
  8264. } else {
  8265. ring = &dev_priv->ring[RCS];
  8266. }
  8267. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  8268. if (ret)
  8269. goto cleanup_pending;
  8270. work->gtt_offset =
  8271. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8272. if (use_mmio_flip(ring, obj)) {
  8273. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8274. page_flip_flags);
  8275. if (ret)
  8276. goto cleanup_unpin;
  8277. work->flip_queued_seqno = obj->last_write_seqno;
  8278. work->flip_queued_ring = obj->ring;
  8279. } else {
  8280. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8281. page_flip_flags);
  8282. if (ret)
  8283. goto cleanup_unpin;
  8284. work->flip_queued_seqno = intel_ring_get_seqno(ring);
  8285. work->flip_queued_ring = ring;
  8286. }
  8287. work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8288. work->enable_stall_check = true;
  8289. i915_gem_track_fb(work->old_fb_obj, obj,
  8290. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8291. intel_disable_fbc(dev);
  8292. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8293. mutex_unlock(&dev->struct_mutex);
  8294. trace_i915_flip_request(intel_crtc->plane, obj);
  8295. return 0;
  8296. cleanup_unpin:
  8297. intel_unpin_fb_obj(obj);
  8298. cleanup_pending:
  8299. atomic_dec(&intel_crtc->unpin_work_count);
  8300. crtc->primary->fb = old_fb;
  8301. drm_gem_object_unreference(&work->old_fb_obj->base);
  8302. drm_gem_object_unreference(&obj->base);
  8303. mutex_unlock(&dev->struct_mutex);
  8304. cleanup:
  8305. spin_lock_irq(&dev->event_lock);
  8306. intel_crtc->unpin_work = NULL;
  8307. spin_unlock_irq(&dev->event_lock);
  8308. drm_crtc_vblank_put(crtc);
  8309. free_work:
  8310. kfree(work);
  8311. if (ret == -EIO) {
  8312. out_hang:
  8313. intel_crtc_wait_for_pending_flips(crtc);
  8314. ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
  8315. if (ret == 0 && event) {
  8316. spin_lock_irq(&dev->event_lock);
  8317. drm_send_vblank_event(dev, pipe, event);
  8318. spin_unlock_irq(&dev->event_lock);
  8319. }
  8320. }
  8321. return ret;
  8322. }
  8323. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8324. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8325. .load_lut = intel_crtc_load_lut,
  8326. };
  8327. /**
  8328. * intel_modeset_update_staged_output_state
  8329. *
  8330. * Updates the staged output configuration state, e.g. after we've read out the
  8331. * current hw state.
  8332. */
  8333. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8334. {
  8335. struct intel_crtc *crtc;
  8336. struct intel_encoder *encoder;
  8337. struct intel_connector *connector;
  8338. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8339. base.head) {
  8340. connector->new_encoder =
  8341. to_intel_encoder(connector->base.encoder);
  8342. }
  8343. for_each_intel_encoder(dev, encoder) {
  8344. encoder->new_crtc =
  8345. to_intel_crtc(encoder->base.crtc);
  8346. }
  8347. for_each_intel_crtc(dev, crtc) {
  8348. crtc->new_enabled = crtc->base.enabled;
  8349. if (crtc->new_enabled)
  8350. crtc->new_config = &crtc->config;
  8351. else
  8352. crtc->new_config = NULL;
  8353. }
  8354. }
  8355. /**
  8356. * intel_modeset_commit_output_state
  8357. *
  8358. * This function copies the stage display pipe configuration to the real one.
  8359. */
  8360. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8361. {
  8362. struct intel_crtc *crtc;
  8363. struct intel_encoder *encoder;
  8364. struct intel_connector *connector;
  8365. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8366. base.head) {
  8367. connector->base.encoder = &connector->new_encoder->base;
  8368. }
  8369. for_each_intel_encoder(dev, encoder) {
  8370. encoder->base.crtc = &encoder->new_crtc->base;
  8371. }
  8372. for_each_intel_crtc(dev, crtc) {
  8373. crtc->base.enabled = crtc->new_enabled;
  8374. }
  8375. }
  8376. static void
  8377. connected_sink_compute_bpp(struct intel_connector *connector,
  8378. struct intel_crtc_config *pipe_config)
  8379. {
  8380. int bpp = pipe_config->pipe_bpp;
  8381. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8382. connector->base.base.id,
  8383. connector->base.name);
  8384. /* Don't use an invalid EDID bpc value */
  8385. if (connector->base.display_info.bpc &&
  8386. connector->base.display_info.bpc * 3 < bpp) {
  8387. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8388. bpp, connector->base.display_info.bpc*3);
  8389. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8390. }
  8391. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8392. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8393. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8394. bpp);
  8395. pipe_config->pipe_bpp = 24;
  8396. }
  8397. }
  8398. static int
  8399. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8400. struct drm_framebuffer *fb,
  8401. struct intel_crtc_config *pipe_config)
  8402. {
  8403. struct drm_device *dev = crtc->base.dev;
  8404. struct intel_connector *connector;
  8405. int bpp;
  8406. switch (fb->pixel_format) {
  8407. case DRM_FORMAT_C8:
  8408. bpp = 8*3; /* since we go through a colormap */
  8409. break;
  8410. case DRM_FORMAT_XRGB1555:
  8411. case DRM_FORMAT_ARGB1555:
  8412. /* checked in intel_framebuffer_init already */
  8413. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8414. return -EINVAL;
  8415. case DRM_FORMAT_RGB565:
  8416. bpp = 6*3; /* min is 18bpp */
  8417. break;
  8418. case DRM_FORMAT_XBGR8888:
  8419. case DRM_FORMAT_ABGR8888:
  8420. /* checked in intel_framebuffer_init already */
  8421. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8422. return -EINVAL;
  8423. case DRM_FORMAT_XRGB8888:
  8424. case DRM_FORMAT_ARGB8888:
  8425. bpp = 8*3;
  8426. break;
  8427. case DRM_FORMAT_XRGB2101010:
  8428. case DRM_FORMAT_ARGB2101010:
  8429. case DRM_FORMAT_XBGR2101010:
  8430. case DRM_FORMAT_ABGR2101010:
  8431. /* checked in intel_framebuffer_init already */
  8432. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8433. return -EINVAL;
  8434. bpp = 10*3;
  8435. break;
  8436. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8437. default:
  8438. DRM_DEBUG_KMS("unsupported depth\n");
  8439. return -EINVAL;
  8440. }
  8441. pipe_config->pipe_bpp = bpp;
  8442. /* Clamp display bpp to EDID value */
  8443. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8444. base.head) {
  8445. if (!connector->new_encoder ||
  8446. connector->new_encoder->new_crtc != crtc)
  8447. continue;
  8448. connected_sink_compute_bpp(connector, pipe_config);
  8449. }
  8450. return bpp;
  8451. }
  8452. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8453. {
  8454. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8455. "type: 0x%x flags: 0x%x\n",
  8456. mode->crtc_clock,
  8457. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8458. mode->crtc_hsync_end, mode->crtc_htotal,
  8459. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8460. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8461. }
  8462. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8463. struct intel_crtc_config *pipe_config,
  8464. const char *context)
  8465. {
  8466. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8467. context, pipe_name(crtc->pipe));
  8468. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8469. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8470. pipe_config->pipe_bpp, pipe_config->dither);
  8471. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8472. pipe_config->has_pch_encoder,
  8473. pipe_config->fdi_lanes,
  8474. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8475. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8476. pipe_config->fdi_m_n.tu);
  8477. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8478. pipe_config->has_dp_encoder,
  8479. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8480. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8481. pipe_config->dp_m_n.tu);
  8482. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8483. pipe_config->has_dp_encoder,
  8484. pipe_config->dp_m2_n2.gmch_m,
  8485. pipe_config->dp_m2_n2.gmch_n,
  8486. pipe_config->dp_m2_n2.link_m,
  8487. pipe_config->dp_m2_n2.link_n,
  8488. pipe_config->dp_m2_n2.tu);
  8489. DRM_DEBUG_KMS("requested mode:\n");
  8490. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  8491. DRM_DEBUG_KMS("adjusted mode:\n");
  8492. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  8493. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  8494. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8495. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8496. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8497. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8498. pipe_config->gmch_pfit.control,
  8499. pipe_config->gmch_pfit.pgm_ratios,
  8500. pipe_config->gmch_pfit.lvds_border_bits);
  8501. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8502. pipe_config->pch_pfit.pos,
  8503. pipe_config->pch_pfit.size,
  8504. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8505. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8506. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8507. }
  8508. static bool encoders_cloneable(const struct intel_encoder *a,
  8509. const struct intel_encoder *b)
  8510. {
  8511. /* masks could be asymmetric, so check both ways */
  8512. return a == b || (a->cloneable & (1 << b->type) &&
  8513. b->cloneable & (1 << a->type));
  8514. }
  8515. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8516. struct intel_encoder *encoder)
  8517. {
  8518. struct drm_device *dev = crtc->base.dev;
  8519. struct intel_encoder *source_encoder;
  8520. for_each_intel_encoder(dev, source_encoder) {
  8521. if (source_encoder->new_crtc != crtc)
  8522. continue;
  8523. if (!encoders_cloneable(encoder, source_encoder))
  8524. return false;
  8525. }
  8526. return true;
  8527. }
  8528. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8529. {
  8530. struct drm_device *dev = crtc->base.dev;
  8531. struct intel_encoder *encoder;
  8532. for_each_intel_encoder(dev, encoder) {
  8533. if (encoder->new_crtc != crtc)
  8534. continue;
  8535. if (!check_single_encoder_cloning(crtc, encoder))
  8536. return false;
  8537. }
  8538. return true;
  8539. }
  8540. static struct intel_crtc_config *
  8541. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8542. struct drm_framebuffer *fb,
  8543. struct drm_display_mode *mode)
  8544. {
  8545. struct drm_device *dev = crtc->dev;
  8546. struct intel_encoder *encoder;
  8547. struct intel_crtc_config *pipe_config;
  8548. int plane_bpp, ret = -EINVAL;
  8549. bool retry = true;
  8550. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8551. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8552. return ERR_PTR(-EINVAL);
  8553. }
  8554. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8555. if (!pipe_config)
  8556. return ERR_PTR(-ENOMEM);
  8557. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  8558. drm_mode_copy(&pipe_config->requested_mode, mode);
  8559. pipe_config->cpu_transcoder =
  8560. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8561. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8562. /*
  8563. * Sanitize sync polarity flags based on requested ones. If neither
  8564. * positive or negative polarity is requested, treat this as meaning
  8565. * negative polarity.
  8566. */
  8567. if (!(pipe_config->adjusted_mode.flags &
  8568. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8569. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8570. if (!(pipe_config->adjusted_mode.flags &
  8571. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8572. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8573. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8574. * plane pixel format and any sink constraints into account. Returns the
  8575. * source plane bpp so that dithering can be selected on mismatches
  8576. * after encoders and crtc also have had their say. */
  8577. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8578. fb, pipe_config);
  8579. if (plane_bpp < 0)
  8580. goto fail;
  8581. /*
  8582. * Determine the real pipe dimensions. Note that stereo modes can
  8583. * increase the actual pipe size due to the frame doubling and
  8584. * insertion of additional space for blanks between the frame. This
  8585. * is stored in the crtc timings. We use the requested mode to do this
  8586. * computation to clearly distinguish it from the adjusted mode, which
  8587. * can be changed by the connectors in the below retry loop.
  8588. */
  8589. drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
  8590. pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
  8591. pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
  8592. encoder_retry:
  8593. /* Ensure the port clock defaults are reset when retrying. */
  8594. pipe_config->port_clock = 0;
  8595. pipe_config->pixel_multiplier = 1;
  8596. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8597. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  8598. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8599. * adjust it according to limitations or connector properties, and also
  8600. * a chance to reject the mode entirely.
  8601. */
  8602. for_each_intel_encoder(dev, encoder) {
  8603. if (&encoder->new_crtc->base != crtc)
  8604. continue;
  8605. if (!(encoder->compute_config(encoder, pipe_config))) {
  8606. DRM_DEBUG_KMS("Encoder config failure\n");
  8607. goto fail;
  8608. }
  8609. }
  8610. /* Set default port clock if not overwritten by the encoder. Needs to be
  8611. * done afterwards in case the encoder adjusts the mode. */
  8612. if (!pipe_config->port_clock)
  8613. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  8614. * pipe_config->pixel_multiplier;
  8615. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8616. if (ret < 0) {
  8617. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8618. goto fail;
  8619. }
  8620. if (ret == RETRY) {
  8621. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8622. ret = -EINVAL;
  8623. goto fail;
  8624. }
  8625. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8626. retry = false;
  8627. goto encoder_retry;
  8628. }
  8629. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8630. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8631. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8632. return pipe_config;
  8633. fail:
  8634. kfree(pipe_config);
  8635. return ERR_PTR(ret);
  8636. }
  8637. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8638. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8639. static void
  8640. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8641. unsigned *prepare_pipes, unsigned *disable_pipes)
  8642. {
  8643. struct intel_crtc *intel_crtc;
  8644. struct drm_device *dev = crtc->dev;
  8645. struct intel_encoder *encoder;
  8646. struct intel_connector *connector;
  8647. struct drm_crtc *tmp_crtc;
  8648. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8649. /* Check which crtcs have changed outputs connected to them, these need
  8650. * to be part of the prepare_pipes mask. We don't (yet) support global
  8651. * modeset across multiple crtcs, so modeset_pipes will only have one
  8652. * bit set at most. */
  8653. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8654. base.head) {
  8655. if (connector->base.encoder == &connector->new_encoder->base)
  8656. continue;
  8657. if (connector->base.encoder) {
  8658. tmp_crtc = connector->base.encoder->crtc;
  8659. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8660. }
  8661. if (connector->new_encoder)
  8662. *prepare_pipes |=
  8663. 1 << connector->new_encoder->new_crtc->pipe;
  8664. }
  8665. for_each_intel_encoder(dev, encoder) {
  8666. if (encoder->base.crtc == &encoder->new_crtc->base)
  8667. continue;
  8668. if (encoder->base.crtc) {
  8669. tmp_crtc = encoder->base.crtc;
  8670. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8671. }
  8672. if (encoder->new_crtc)
  8673. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8674. }
  8675. /* Check for pipes that will be enabled/disabled ... */
  8676. for_each_intel_crtc(dev, intel_crtc) {
  8677. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8678. continue;
  8679. if (!intel_crtc->new_enabled)
  8680. *disable_pipes |= 1 << intel_crtc->pipe;
  8681. else
  8682. *prepare_pipes |= 1 << intel_crtc->pipe;
  8683. }
  8684. /* set_mode is also used to update properties on life display pipes. */
  8685. intel_crtc = to_intel_crtc(crtc);
  8686. if (intel_crtc->new_enabled)
  8687. *prepare_pipes |= 1 << intel_crtc->pipe;
  8688. /*
  8689. * For simplicity do a full modeset on any pipe where the output routing
  8690. * changed. We could be more clever, but that would require us to be
  8691. * more careful with calling the relevant encoder->mode_set functions.
  8692. */
  8693. if (*prepare_pipes)
  8694. *modeset_pipes = *prepare_pipes;
  8695. /* ... and mask these out. */
  8696. *modeset_pipes &= ~(*disable_pipes);
  8697. *prepare_pipes &= ~(*disable_pipes);
  8698. /*
  8699. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8700. * obies this rule, but the modeset restore mode of
  8701. * intel_modeset_setup_hw_state does not.
  8702. */
  8703. *modeset_pipes &= 1 << intel_crtc->pipe;
  8704. *prepare_pipes &= 1 << intel_crtc->pipe;
  8705. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8706. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8707. }
  8708. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8709. {
  8710. struct drm_encoder *encoder;
  8711. struct drm_device *dev = crtc->dev;
  8712. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8713. if (encoder->crtc == crtc)
  8714. return true;
  8715. return false;
  8716. }
  8717. static void
  8718. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8719. {
  8720. struct intel_encoder *intel_encoder;
  8721. struct intel_crtc *intel_crtc;
  8722. struct drm_connector *connector;
  8723. for_each_intel_encoder(dev, intel_encoder) {
  8724. if (!intel_encoder->base.crtc)
  8725. continue;
  8726. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8727. if (prepare_pipes & (1 << intel_crtc->pipe))
  8728. intel_encoder->connectors_active = false;
  8729. }
  8730. intel_modeset_commit_output_state(dev);
  8731. /* Double check state. */
  8732. for_each_intel_crtc(dev, intel_crtc) {
  8733. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8734. WARN_ON(intel_crtc->new_config &&
  8735. intel_crtc->new_config != &intel_crtc->config);
  8736. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8737. }
  8738. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8739. if (!connector->encoder || !connector->encoder->crtc)
  8740. continue;
  8741. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8742. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8743. struct drm_property *dpms_property =
  8744. dev->mode_config.dpms_property;
  8745. connector->dpms = DRM_MODE_DPMS_ON;
  8746. drm_object_property_set_value(&connector->base,
  8747. dpms_property,
  8748. DRM_MODE_DPMS_ON);
  8749. intel_encoder = to_intel_encoder(connector->encoder);
  8750. intel_encoder->connectors_active = true;
  8751. }
  8752. }
  8753. }
  8754. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8755. {
  8756. int diff;
  8757. if (clock1 == clock2)
  8758. return true;
  8759. if (!clock1 || !clock2)
  8760. return false;
  8761. diff = abs(clock1 - clock2);
  8762. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8763. return true;
  8764. return false;
  8765. }
  8766. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8767. list_for_each_entry((intel_crtc), \
  8768. &(dev)->mode_config.crtc_list, \
  8769. base.head) \
  8770. if (mask & (1 <<(intel_crtc)->pipe))
  8771. static bool
  8772. intel_pipe_config_compare(struct drm_device *dev,
  8773. struct intel_crtc_config *current_config,
  8774. struct intel_crtc_config *pipe_config)
  8775. {
  8776. #define PIPE_CONF_CHECK_X(name) \
  8777. if (current_config->name != pipe_config->name) { \
  8778. DRM_ERROR("mismatch in " #name " " \
  8779. "(expected 0x%08x, found 0x%08x)\n", \
  8780. current_config->name, \
  8781. pipe_config->name); \
  8782. return false; \
  8783. }
  8784. #define PIPE_CONF_CHECK_I(name) \
  8785. if (current_config->name != pipe_config->name) { \
  8786. DRM_ERROR("mismatch in " #name " " \
  8787. "(expected %i, found %i)\n", \
  8788. current_config->name, \
  8789. pipe_config->name); \
  8790. return false; \
  8791. }
  8792. /* This is required for BDW+ where there is only one set of registers for
  8793. * switching between high and low RR.
  8794. * This macro can be used whenever a comparison has to be made between one
  8795. * hw state and multiple sw state variables.
  8796. */
  8797. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  8798. if ((current_config->name != pipe_config->name) && \
  8799. (current_config->alt_name != pipe_config->name)) { \
  8800. DRM_ERROR("mismatch in " #name " " \
  8801. "(expected %i or %i, found %i)\n", \
  8802. current_config->name, \
  8803. current_config->alt_name, \
  8804. pipe_config->name); \
  8805. return false; \
  8806. }
  8807. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8808. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8809. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8810. "(expected %i, found %i)\n", \
  8811. current_config->name & (mask), \
  8812. pipe_config->name & (mask)); \
  8813. return false; \
  8814. }
  8815. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8816. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8817. DRM_ERROR("mismatch in " #name " " \
  8818. "(expected %i, found %i)\n", \
  8819. current_config->name, \
  8820. pipe_config->name); \
  8821. return false; \
  8822. }
  8823. #define PIPE_CONF_QUIRK(quirk) \
  8824. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8825. PIPE_CONF_CHECK_I(cpu_transcoder);
  8826. PIPE_CONF_CHECK_I(has_pch_encoder);
  8827. PIPE_CONF_CHECK_I(fdi_lanes);
  8828. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8829. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8830. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8831. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8832. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8833. PIPE_CONF_CHECK_I(has_dp_encoder);
  8834. if (INTEL_INFO(dev)->gen < 8) {
  8835. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8836. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8837. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8838. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8839. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8840. if (current_config->has_drrs) {
  8841. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  8842. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  8843. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  8844. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  8845. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  8846. }
  8847. } else {
  8848. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  8849. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  8850. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  8851. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  8852. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  8853. }
  8854. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  8855. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  8856. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  8857. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  8858. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  8859. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  8860. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  8861. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  8862. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  8863. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  8864. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  8865. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  8866. PIPE_CONF_CHECK_I(pixel_multiplier);
  8867. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8868. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8869. IS_VALLEYVIEW(dev))
  8870. PIPE_CONF_CHECK_I(limited_color_range);
  8871. PIPE_CONF_CHECK_I(has_audio);
  8872. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8873. DRM_MODE_FLAG_INTERLACE);
  8874. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8875. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8876. DRM_MODE_FLAG_PHSYNC);
  8877. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8878. DRM_MODE_FLAG_NHSYNC);
  8879. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8880. DRM_MODE_FLAG_PVSYNC);
  8881. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8882. DRM_MODE_FLAG_NVSYNC);
  8883. }
  8884. PIPE_CONF_CHECK_I(pipe_src_w);
  8885. PIPE_CONF_CHECK_I(pipe_src_h);
  8886. /*
  8887. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8888. * screen. Since we don't yet re-compute the pipe config when moving
  8889. * just the lvds port away to another pipe the sw tracking won't match.
  8890. *
  8891. * Proper atomic modesets with recomputed global state will fix this.
  8892. * Until then just don't check gmch state for inherited modes.
  8893. */
  8894. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8895. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8896. /* pfit ratios are autocomputed by the hw on gen4+ */
  8897. if (INTEL_INFO(dev)->gen < 4)
  8898. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8899. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8900. }
  8901. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8902. if (current_config->pch_pfit.enabled) {
  8903. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8904. PIPE_CONF_CHECK_I(pch_pfit.size);
  8905. }
  8906. /* BDW+ don't expose a synchronous way to read the state */
  8907. if (IS_HASWELL(dev))
  8908. PIPE_CONF_CHECK_I(ips_enabled);
  8909. PIPE_CONF_CHECK_I(double_wide);
  8910. PIPE_CONF_CHECK_X(ddi_pll_sel);
  8911. PIPE_CONF_CHECK_I(shared_dpll);
  8912. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8913. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8914. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8915. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8916. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  8917. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8918. PIPE_CONF_CHECK_I(pipe_bpp);
  8919. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  8920. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8921. #undef PIPE_CONF_CHECK_X
  8922. #undef PIPE_CONF_CHECK_I
  8923. #undef PIPE_CONF_CHECK_I_ALT
  8924. #undef PIPE_CONF_CHECK_FLAGS
  8925. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8926. #undef PIPE_CONF_QUIRK
  8927. return true;
  8928. }
  8929. static void
  8930. check_connector_state(struct drm_device *dev)
  8931. {
  8932. struct intel_connector *connector;
  8933. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8934. base.head) {
  8935. /* This also checks the encoder/connector hw state with the
  8936. * ->get_hw_state callbacks. */
  8937. intel_connector_check_state(connector);
  8938. WARN(&connector->new_encoder->base != connector->base.encoder,
  8939. "connector's staged encoder doesn't match current encoder\n");
  8940. }
  8941. }
  8942. static void
  8943. check_encoder_state(struct drm_device *dev)
  8944. {
  8945. struct intel_encoder *encoder;
  8946. struct intel_connector *connector;
  8947. for_each_intel_encoder(dev, encoder) {
  8948. bool enabled = false;
  8949. bool active = false;
  8950. enum pipe pipe, tracked_pipe;
  8951. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8952. encoder->base.base.id,
  8953. encoder->base.name);
  8954. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  8955. "encoder's stage crtc doesn't match current crtc\n");
  8956. WARN(encoder->connectors_active && !encoder->base.crtc,
  8957. "encoder's active_connectors set, but no crtc\n");
  8958. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8959. base.head) {
  8960. if (connector->base.encoder != &encoder->base)
  8961. continue;
  8962. enabled = true;
  8963. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  8964. active = true;
  8965. }
  8966. /*
  8967. * for MST connectors if we unplug the connector is gone
  8968. * away but the encoder is still connected to a crtc
  8969. * until a modeset happens in response to the hotplug.
  8970. */
  8971. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  8972. continue;
  8973. WARN(!!encoder->base.crtc != enabled,
  8974. "encoder's enabled state mismatch "
  8975. "(expected %i, found %i)\n",
  8976. !!encoder->base.crtc, enabled);
  8977. WARN(active && !encoder->base.crtc,
  8978. "active encoder with no crtc\n");
  8979. WARN(encoder->connectors_active != active,
  8980. "encoder's computed active state doesn't match tracked active state "
  8981. "(expected %i, found %i)\n", active, encoder->connectors_active);
  8982. active = encoder->get_hw_state(encoder, &pipe);
  8983. WARN(active != encoder->connectors_active,
  8984. "encoder's hw state doesn't match sw tracking "
  8985. "(expected %i, found %i)\n",
  8986. encoder->connectors_active, active);
  8987. if (!encoder->base.crtc)
  8988. continue;
  8989. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  8990. WARN(active && pipe != tracked_pipe,
  8991. "active encoder's pipe doesn't match"
  8992. "(expected %i, found %i)\n",
  8993. tracked_pipe, pipe);
  8994. }
  8995. }
  8996. static void
  8997. check_crtc_state(struct drm_device *dev)
  8998. {
  8999. struct drm_i915_private *dev_priv = dev->dev_private;
  9000. struct intel_crtc *crtc;
  9001. struct intel_encoder *encoder;
  9002. struct intel_crtc_config pipe_config;
  9003. for_each_intel_crtc(dev, crtc) {
  9004. bool enabled = false;
  9005. bool active = false;
  9006. memset(&pipe_config, 0, sizeof(pipe_config));
  9007. DRM_DEBUG_KMS("[CRTC:%d]\n",
  9008. crtc->base.base.id);
  9009. WARN(crtc->active && !crtc->base.enabled,
  9010. "active crtc, but not enabled in sw tracking\n");
  9011. for_each_intel_encoder(dev, encoder) {
  9012. if (encoder->base.crtc != &crtc->base)
  9013. continue;
  9014. enabled = true;
  9015. if (encoder->connectors_active)
  9016. active = true;
  9017. }
  9018. WARN(active != crtc->active,
  9019. "crtc's computed active state doesn't match tracked active state "
  9020. "(expected %i, found %i)\n", active, crtc->active);
  9021. WARN(enabled != crtc->base.enabled,
  9022. "crtc's computed enabled state doesn't match tracked enabled state "
  9023. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  9024. active = dev_priv->display.get_pipe_config(crtc,
  9025. &pipe_config);
  9026. /* hw state is inconsistent with the pipe quirk */
  9027. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  9028. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  9029. active = crtc->active;
  9030. for_each_intel_encoder(dev, encoder) {
  9031. enum pipe pipe;
  9032. if (encoder->base.crtc != &crtc->base)
  9033. continue;
  9034. if (encoder->get_hw_state(encoder, &pipe))
  9035. encoder->get_config(encoder, &pipe_config);
  9036. }
  9037. WARN(crtc->active != active,
  9038. "crtc active state doesn't match with hw state "
  9039. "(expected %i, found %i)\n", crtc->active, active);
  9040. if (active &&
  9041. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  9042. WARN(1, "pipe state doesn't match!\n");
  9043. intel_dump_pipe_config(crtc, &pipe_config,
  9044. "[hw state]");
  9045. intel_dump_pipe_config(crtc, &crtc->config,
  9046. "[sw state]");
  9047. }
  9048. }
  9049. }
  9050. static void
  9051. check_shared_dpll_state(struct drm_device *dev)
  9052. {
  9053. struct drm_i915_private *dev_priv = dev->dev_private;
  9054. struct intel_crtc *crtc;
  9055. struct intel_dpll_hw_state dpll_hw_state;
  9056. int i;
  9057. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9058. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9059. int enabled_crtcs = 0, active_crtcs = 0;
  9060. bool active;
  9061. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9062. DRM_DEBUG_KMS("%s\n", pll->name);
  9063. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9064. WARN(pll->active > pll->refcount,
  9065. "more active pll users than references: %i vs %i\n",
  9066. pll->active, pll->refcount);
  9067. WARN(pll->active && !pll->on,
  9068. "pll in active use but not on in sw tracking\n");
  9069. WARN(pll->on && !pll->active,
  9070. "pll in on but not on in use in sw tracking\n");
  9071. WARN(pll->on != active,
  9072. "pll on state mismatch (expected %i, found %i)\n",
  9073. pll->on, active);
  9074. for_each_intel_crtc(dev, crtc) {
  9075. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  9076. enabled_crtcs++;
  9077. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9078. active_crtcs++;
  9079. }
  9080. WARN(pll->active != active_crtcs,
  9081. "pll active crtcs mismatch (expected %i, found %i)\n",
  9082. pll->active, active_crtcs);
  9083. WARN(pll->refcount != enabled_crtcs,
  9084. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9085. pll->refcount, enabled_crtcs);
  9086. WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
  9087. sizeof(dpll_hw_state)),
  9088. "pll hw state mismatch\n");
  9089. }
  9090. }
  9091. void
  9092. intel_modeset_check_state(struct drm_device *dev)
  9093. {
  9094. check_connector_state(dev);
  9095. check_encoder_state(dev);
  9096. check_crtc_state(dev);
  9097. check_shared_dpll_state(dev);
  9098. }
  9099. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  9100. int dotclock)
  9101. {
  9102. /*
  9103. * FDI already provided one idea for the dotclock.
  9104. * Yell if the encoder disagrees.
  9105. */
  9106. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  9107. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9108. pipe_config->adjusted_mode.crtc_clock, dotclock);
  9109. }
  9110. static void update_scanline_offset(struct intel_crtc *crtc)
  9111. {
  9112. struct drm_device *dev = crtc->base.dev;
  9113. /*
  9114. * The scanline counter increments at the leading edge of hsync.
  9115. *
  9116. * On most platforms it starts counting from vtotal-1 on the
  9117. * first active line. That means the scanline counter value is
  9118. * always one less than what we would expect. Ie. just after
  9119. * start of vblank, which also occurs at start of hsync (on the
  9120. * last active line), the scanline counter will read vblank_start-1.
  9121. *
  9122. * On gen2 the scanline counter starts counting from 1 instead
  9123. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9124. * to keep the value positive), instead of adding one.
  9125. *
  9126. * On HSW+ the behaviour of the scanline counter depends on the output
  9127. * type. For DP ports it behaves like most other platforms, but on HDMI
  9128. * there's an extra 1 line difference. So we need to add two instead of
  9129. * one to the value.
  9130. */
  9131. if (IS_GEN2(dev)) {
  9132. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  9133. int vtotal;
  9134. vtotal = mode->crtc_vtotal;
  9135. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9136. vtotal /= 2;
  9137. crtc->scanline_offset = vtotal - 1;
  9138. } else if (HAS_DDI(dev) &&
  9139. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
  9140. crtc->scanline_offset = 2;
  9141. } else
  9142. crtc->scanline_offset = 1;
  9143. }
  9144. static int __intel_set_mode(struct drm_crtc *crtc,
  9145. struct drm_display_mode *mode,
  9146. int x, int y, struct drm_framebuffer *fb)
  9147. {
  9148. struct drm_device *dev = crtc->dev;
  9149. struct drm_i915_private *dev_priv = dev->dev_private;
  9150. struct drm_display_mode *saved_mode;
  9151. struct intel_crtc_config *pipe_config = NULL;
  9152. struct intel_crtc *intel_crtc;
  9153. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  9154. int ret = 0;
  9155. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9156. if (!saved_mode)
  9157. return -ENOMEM;
  9158. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  9159. &prepare_pipes, &disable_pipes);
  9160. *saved_mode = crtc->mode;
  9161. /* Hack: Because we don't (yet) support global modeset on multiple
  9162. * crtcs, we don't keep track of the new mode for more than one crtc.
  9163. * Hence simply check whether any bit is set in modeset_pipes in all the
  9164. * pieces of code that are not yet converted to deal with mutliple crtcs
  9165. * changing their mode at the same time. */
  9166. if (modeset_pipes) {
  9167. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9168. if (IS_ERR(pipe_config)) {
  9169. ret = PTR_ERR(pipe_config);
  9170. pipe_config = NULL;
  9171. goto out;
  9172. }
  9173. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9174. "[modeset]");
  9175. to_intel_crtc(crtc)->new_config = pipe_config;
  9176. }
  9177. /*
  9178. * See if the config requires any additional preparation, e.g.
  9179. * to adjust global state with pipes off. We need to do this
  9180. * here so we can get the modeset_pipe updated config for the new
  9181. * mode set on this crtc. For other crtcs we need to use the
  9182. * adjusted_mode bits in the crtc directly.
  9183. */
  9184. if (IS_VALLEYVIEW(dev)) {
  9185. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9186. /* may have added more to prepare_pipes than we should */
  9187. prepare_pipes &= ~disable_pipes;
  9188. }
  9189. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9190. intel_crtc_disable(&intel_crtc->base);
  9191. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9192. if (intel_crtc->base.enabled)
  9193. dev_priv->display.crtc_disable(&intel_crtc->base);
  9194. }
  9195. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9196. * to set it here already despite that we pass it down the callchain.
  9197. */
  9198. if (modeset_pipes) {
  9199. crtc->mode = *mode;
  9200. /* mode_set/enable/disable functions rely on a correct pipe
  9201. * config. */
  9202. to_intel_crtc(crtc)->config = *pipe_config;
  9203. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  9204. /*
  9205. * Calculate and store various constants which
  9206. * are later needed by vblank and swap-completion
  9207. * timestamping. They are derived from true hwmode.
  9208. */
  9209. drm_calc_timestamping_constants(crtc,
  9210. &pipe_config->adjusted_mode);
  9211. }
  9212. /* Only after disabling all output pipelines that will be changed can we
  9213. * update the the output configuration. */
  9214. intel_modeset_update_state(dev, prepare_pipes);
  9215. if (dev_priv->display.modeset_global_resources)
  9216. dev_priv->display.modeset_global_resources(dev);
  9217. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9218. * on the DPLL.
  9219. */
  9220. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9221. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9222. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
  9223. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9224. mutex_lock(&dev->struct_mutex);
  9225. ret = intel_pin_and_fence_fb_obj(dev,
  9226. obj,
  9227. NULL);
  9228. if (ret != 0) {
  9229. DRM_ERROR("pin & fence failed\n");
  9230. mutex_unlock(&dev->struct_mutex);
  9231. goto done;
  9232. }
  9233. if (old_fb)
  9234. intel_unpin_fb_obj(old_obj);
  9235. i915_gem_track_fb(old_obj, obj,
  9236. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9237. mutex_unlock(&dev->struct_mutex);
  9238. crtc->primary->fb = fb;
  9239. crtc->x = x;
  9240. crtc->y = y;
  9241. ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
  9242. x, y, fb);
  9243. if (ret)
  9244. goto done;
  9245. }
  9246. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9247. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9248. update_scanline_offset(intel_crtc);
  9249. dev_priv->display.crtc_enable(&intel_crtc->base);
  9250. }
  9251. /* FIXME: add subpixel order */
  9252. done:
  9253. if (ret && crtc->enabled)
  9254. crtc->mode = *saved_mode;
  9255. out:
  9256. kfree(pipe_config);
  9257. kfree(saved_mode);
  9258. return ret;
  9259. }
  9260. static int intel_set_mode(struct drm_crtc *crtc,
  9261. struct drm_display_mode *mode,
  9262. int x, int y, struct drm_framebuffer *fb)
  9263. {
  9264. int ret;
  9265. ret = __intel_set_mode(crtc, mode, x, y, fb);
  9266. if (ret == 0)
  9267. intel_modeset_check_state(crtc->dev);
  9268. return ret;
  9269. }
  9270. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9271. {
  9272. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9273. }
  9274. #undef for_each_intel_crtc_masked
  9275. static void intel_set_config_free(struct intel_set_config *config)
  9276. {
  9277. if (!config)
  9278. return;
  9279. kfree(config->save_connector_encoders);
  9280. kfree(config->save_encoder_crtcs);
  9281. kfree(config->save_crtc_enabled);
  9282. kfree(config);
  9283. }
  9284. static int intel_set_config_save_state(struct drm_device *dev,
  9285. struct intel_set_config *config)
  9286. {
  9287. struct drm_crtc *crtc;
  9288. struct drm_encoder *encoder;
  9289. struct drm_connector *connector;
  9290. int count;
  9291. config->save_crtc_enabled =
  9292. kcalloc(dev->mode_config.num_crtc,
  9293. sizeof(bool), GFP_KERNEL);
  9294. if (!config->save_crtc_enabled)
  9295. return -ENOMEM;
  9296. config->save_encoder_crtcs =
  9297. kcalloc(dev->mode_config.num_encoder,
  9298. sizeof(struct drm_crtc *), GFP_KERNEL);
  9299. if (!config->save_encoder_crtcs)
  9300. return -ENOMEM;
  9301. config->save_connector_encoders =
  9302. kcalloc(dev->mode_config.num_connector,
  9303. sizeof(struct drm_encoder *), GFP_KERNEL);
  9304. if (!config->save_connector_encoders)
  9305. return -ENOMEM;
  9306. /* Copy data. Note that driver private data is not affected.
  9307. * Should anything bad happen only the expected state is
  9308. * restored, not the drivers personal bookkeeping.
  9309. */
  9310. count = 0;
  9311. for_each_crtc(dev, crtc) {
  9312. config->save_crtc_enabled[count++] = crtc->enabled;
  9313. }
  9314. count = 0;
  9315. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9316. config->save_encoder_crtcs[count++] = encoder->crtc;
  9317. }
  9318. count = 0;
  9319. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9320. config->save_connector_encoders[count++] = connector->encoder;
  9321. }
  9322. return 0;
  9323. }
  9324. static void intel_set_config_restore_state(struct drm_device *dev,
  9325. struct intel_set_config *config)
  9326. {
  9327. struct intel_crtc *crtc;
  9328. struct intel_encoder *encoder;
  9329. struct intel_connector *connector;
  9330. int count;
  9331. count = 0;
  9332. for_each_intel_crtc(dev, crtc) {
  9333. crtc->new_enabled = config->save_crtc_enabled[count++];
  9334. if (crtc->new_enabled)
  9335. crtc->new_config = &crtc->config;
  9336. else
  9337. crtc->new_config = NULL;
  9338. }
  9339. count = 0;
  9340. for_each_intel_encoder(dev, encoder) {
  9341. encoder->new_crtc =
  9342. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9343. }
  9344. count = 0;
  9345. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9346. connector->new_encoder =
  9347. to_intel_encoder(config->save_connector_encoders[count++]);
  9348. }
  9349. }
  9350. static bool
  9351. is_crtc_connector_off(struct drm_mode_set *set)
  9352. {
  9353. int i;
  9354. if (set->num_connectors == 0)
  9355. return false;
  9356. if (WARN_ON(set->connectors == NULL))
  9357. return false;
  9358. for (i = 0; i < set->num_connectors; i++)
  9359. if (set->connectors[i]->encoder &&
  9360. set->connectors[i]->encoder->crtc == set->crtc &&
  9361. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9362. return true;
  9363. return false;
  9364. }
  9365. static void
  9366. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9367. struct intel_set_config *config)
  9368. {
  9369. /* We should be able to check here if the fb has the same properties
  9370. * and then just flip_or_move it */
  9371. if (is_crtc_connector_off(set)) {
  9372. config->mode_changed = true;
  9373. } else if (set->crtc->primary->fb != set->fb) {
  9374. /*
  9375. * If we have no fb, we can only flip as long as the crtc is
  9376. * active, otherwise we need a full mode set. The crtc may
  9377. * be active if we've only disabled the primary plane, or
  9378. * in fastboot situations.
  9379. */
  9380. if (set->crtc->primary->fb == NULL) {
  9381. struct intel_crtc *intel_crtc =
  9382. to_intel_crtc(set->crtc);
  9383. if (intel_crtc->active) {
  9384. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9385. config->fb_changed = true;
  9386. } else {
  9387. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9388. config->mode_changed = true;
  9389. }
  9390. } else if (set->fb == NULL) {
  9391. config->mode_changed = true;
  9392. } else if (set->fb->pixel_format !=
  9393. set->crtc->primary->fb->pixel_format) {
  9394. config->mode_changed = true;
  9395. } else {
  9396. config->fb_changed = true;
  9397. }
  9398. }
  9399. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9400. config->fb_changed = true;
  9401. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9402. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9403. drm_mode_debug_printmodeline(&set->crtc->mode);
  9404. drm_mode_debug_printmodeline(set->mode);
  9405. config->mode_changed = true;
  9406. }
  9407. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9408. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9409. }
  9410. static int
  9411. intel_modeset_stage_output_state(struct drm_device *dev,
  9412. struct drm_mode_set *set,
  9413. struct intel_set_config *config)
  9414. {
  9415. struct intel_connector *connector;
  9416. struct intel_encoder *encoder;
  9417. struct intel_crtc *crtc;
  9418. int ro;
  9419. /* The upper layers ensure that we either disable a crtc or have a list
  9420. * of connectors. For paranoia, double-check this. */
  9421. WARN_ON(!set->fb && (set->num_connectors != 0));
  9422. WARN_ON(set->fb && (set->num_connectors == 0));
  9423. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9424. base.head) {
  9425. /* Otherwise traverse passed in connector list and get encoders
  9426. * for them. */
  9427. for (ro = 0; ro < set->num_connectors; ro++) {
  9428. if (set->connectors[ro] == &connector->base) {
  9429. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9430. break;
  9431. }
  9432. }
  9433. /* If we disable the crtc, disable all its connectors. Also, if
  9434. * the connector is on the changing crtc but not on the new
  9435. * connector list, disable it. */
  9436. if ((!set->fb || ro == set->num_connectors) &&
  9437. connector->base.encoder &&
  9438. connector->base.encoder->crtc == set->crtc) {
  9439. connector->new_encoder = NULL;
  9440. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9441. connector->base.base.id,
  9442. connector->base.name);
  9443. }
  9444. if (&connector->new_encoder->base != connector->base.encoder) {
  9445. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  9446. config->mode_changed = true;
  9447. }
  9448. }
  9449. /* connector->new_encoder is now updated for all connectors. */
  9450. /* Update crtc of enabled connectors. */
  9451. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9452. base.head) {
  9453. struct drm_crtc *new_crtc;
  9454. if (!connector->new_encoder)
  9455. continue;
  9456. new_crtc = connector->new_encoder->base.crtc;
  9457. for (ro = 0; ro < set->num_connectors; ro++) {
  9458. if (set->connectors[ro] == &connector->base)
  9459. new_crtc = set->crtc;
  9460. }
  9461. /* Make sure the new CRTC will work with the encoder */
  9462. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9463. new_crtc)) {
  9464. return -EINVAL;
  9465. }
  9466. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9467. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9468. connector->base.base.id,
  9469. connector->base.name,
  9470. new_crtc->base.id);
  9471. }
  9472. /* Check for any encoders that needs to be disabled. */
  9473. for_each_intel_encoder(dev, encoder) {
  9474. int num_connectors = 0;
  9475. list_for_each_entry(connector,
  9476. &dev->mode_config.connector_list,
  9477. base.head) {
  9478. if (connector->new_encoder == encoder) {
  9479. WARN_ON(!connector->new_encoder->new_crtc);
  9480. num_connectors++;
  9481. }
  9482. }
  9483. if (num_connectors == 0)
  9484. encoder->new_crtc = NULL;
  9485. else if (num_connectors > 1)
  9486. return -EINVAL;
  9487. /* Only now check for crtc changes so we don't miss encoders
  9488. * that will be disabled. */
  9489. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9490. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  9491. config->mode_changed = true;
  9492. }
  9493. }
  9494. /* Now we've also updated encoder->new_crtc for all encoders. */
  9495. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9496. base.head) {
  9497. if (connector->new_encoder)
  9498. if (connector->new_encoder != connector->encoder)
  9499. connector->encoder = connector->new_encoder;
  9500. }
  9501. for_each_intel_crtc(dev, crtc) {
  9502. crtc->new_enabled = false;
  9503. for_each_intel_encoder(dev, encoder) {
  9504. if (encoder->new_crtc == crtc) {
  9505. crtc->new_enabled = true;
  9506. break;
  9507. }
  9508. }
  9509. if (crtc->new_enabled != crtc->base.enabled) {
  9510. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  9511. crtc->new_enabled ? "en" : "dis");
  9512. config->mode_changed = true;
  9513. }
  9514. if (crtc->new_enabled)
  9515. crtc->new_config = &crtc->config;
  9516. else
  9517. crtc->new_config = NULL;
  9518. }
  9519. return 0;
  9520. }
  9521. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9522. {
  9523. struct drm_device *dev = crtc->base.dev;
  9524. struct intel_encoder *encoder;
  9525. struct intel_connector *connector;
  9526. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9527. pipe_name(crtc->pipe));
  9528. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9529. if (connector->new_encoder &&
  9530. connector->new_encoder->new_crtc == crtc)
  9531. connector->new_encoder = NULL;
  9532. }
  9533. for_each_intel_encoder(dev, encoder) {
  9534. if (encoder->new_crtc == crtc)
  9535. encoder->new_crtc = NULL;
  9536. }
  9537. crtc->new_enabled = false;
  9538. crtc->new_config = NULL;
  9539. }
  9540. static int intel_crtc_set_config(struct drm_mode_set *set)
  9541. {
  9542. struct drm_device *dev;
  9543. struct drm_mode_set save_set;
  9544. struct intel_set_config *config;
  9545. int ret;
  9546. BUG_ON(!set);
  9547. BUG_ON(!set->crtc);
  9548. BUG_ON(!set->crtc->helper_private);
  9549. /* Enforce sane interface api - has been abused by the fb helper. */
  9550. BUG_ON(!set->mode && set->fb);
  9551. BUG_ON(set->fb && set->num_connectors == 0);
  9552. if (set->fb) {
  9553. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9554. set->crtc->base.id, set->fb->base.id,
  9555. (int)set->num_connectors, set->x, set->y);
  9556. } else {
  9557. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9558. }
  9559. dev = set->crtc->dev;
  9560. ret = -ENOMEM;
  9561. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9562. if (!config)
  9563. goto out_config;
  9564. ret = intel_set_config_save_state(dev, config);
  9565. if (ret)
  9566. goto out_config;
  9567. save_set.crtc = set->crtc;
  9568. save_set.mode = &set->crtc->mode;
  9569. save_set.x = set->crtc->x;
  9570. save_set.y = set->crtc->y;
  9571. save_set.fb = set->crtc->primary->fb;
  9572. /* Compute whether we need a full modeset, only an fb base update or no
  9573. * change at all. In the future we might also check whether only the
  9574. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9575. * such cases. */
  9576. intel_set_config_compute_mode_changes(set, config);
  9577. ret = intel_modeset_stage_output_state(dev, set, config);
  9578. if (ret)
  9579. goto fail;
  9580. if (config->mode_changed) {
  9581. ret = intel_set_mode(set->crtc, set->mode,
  9582. set->x, set->y, set->fb);
  9583. } else if (config->fb_changed) {
  9584. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9585. intel_crtc_wait_for_pending_flips(set->crtc);
  9586. ret = intel_pipe_set_base(set->crtc,
  9587. set->x, set->y, set->fb);
  9588. /*
  9589. * We need to make sure the primary plane is re-enabled if it
  9590. * has previously been turned off.
  9591. */
  9592. if (!intel_crtc->primary_enabled && ret == 0) {
  9593. WARN_ON(!intel_crtc->active);
  9594. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9595. }
  9596. /*
  9597. * In the fastboot case this may be our only check of the
  9598. * state after boot. It would be better to only do it on
  9599. * the first update, but we don't have a nice way of doing that
  9600. * (and really, set_config isn't used much for high freq page
  9601. * flipping, so increasing its cost here shouldn't be a big
  9602. * deal).
  9603. */
  9604. if (i915.fastboot && ret == 0)
  9605. intel_modeset_check_state(set->crtc->dev);
  9606. }
  9607. if (ret) {
  9608. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9609. set->crtc->base.id, ret);
  9610. fail:
  9611. intel_set_config_restore_state(dev, config);
  9612. /*
  9613. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9614. * force the pipe off to avoid oopsing in the modeset code
  9615. * due to fb==NULL. This should only happen during boot since
  9616. * we don't yet reconstruct the FB from the hardware state.
  9617. */
  9618. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9619. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9620. /* Try to restore the config */
  9621. if (config->mode_changed &&
  9622. intel_set_mode(save_set.crtc, save_set.mode,
  9623. save_set.x, save_set.y, save_set.fb))
  9624. DRM_ERROR("failed to restore config after modeset failure\n");
  9625. }
  9626. out_config:
  9627. intel_set_config_free(config);
  9628. return ret;
  9629. }
  9630. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9631. .gamma_set = intel_crtc_gamma_set,
  9632. .set_config = intel_crtc_set_config,
  9633. .destroy = intel_crtc_destroy,
  9634. .page_flip = intel_crtc_page_flip,
  9635. };
  9636. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9637. struct intel_shared_dpll *pll,
  9638. struct intel_dpll_hw_state *hw_state)
  9639. {
  9640. uint32_t val;
  9641. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9642. return false;
  9643. val = I915_READ(PCH_DPLL(pll->id));
  9644. hw_state->dpll = val;
  9645. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9646. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9647. return val & DPLL_VCO_ENABLE;
  9648. }
  9649. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9650. struct intel_shared_dpll *pll)
  9651. {
  9652. I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
  9653. I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
  9654. }
  9655. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9656. struct intel_shared_dpll *pll)
  9657. {
  9658. /* PCH refclock must be enabled first */
  9659. ibx_assert_pch_refclk_enabled(dev_priv);
  9660. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9661. /* Wait for the clocks to stabilize. */
  9662. POSTING_READ(PCH_DPLL(pll->id));
  9663. udelay(150);
  9664. /* The pixel multiplier can only be updated once the
  9665. * DPLL is enabled and the clocks are stable.
  9666. *
  9667. * So write it again.
  9668. */
  9669. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9670. POSTING_READ(PCH_DPLL(pll->id));
  9671. udelay(200);
  9672. }
  9673. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9674. struct intel_shared_dpll *pll)
  9675. {
  9676. struct drm_device *dev = dev_priv->dev;
  9677. struct intel_crtc *crtc;
  9678. /* Make sure no transcoder isn't still depending on us. */
  9679. for_each_intel_crtc(dev, crtc) {
  9680. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9681. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9682. }
  9683. I915_WRITE(PCH_DPLL(pll->id), 0);
  9684. POSTING_READ(PCH_DPLL(pll->id));
  9685. udelay(200);
  9686. }
  9687. static char *ibx_pch_dpll_names[] = {
  9688. "PCH DPLL A",
  9689. "PCH DPLL B",
  9690. };
  9691. static void ibx_pch_dpll_init(struct drm_device *dev)
  9692. {
  9693. struct drm_i915_private *dev_priv = dev->dev_private;
  9694. int i;
  9695. dev_priv->num_shared_dpll = 2;
  9696. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9697. dev_priv->shared_dplls[i].id = i;
  9698. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9699. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9700. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9701. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9702. dev_priv->shared_dplls[i].get_hw_state =
  9703. ibx_pch_dpll_get_hw_state;
  9704. }
  9705. }
  9706. static void intel_shared_dpll_init(struct drm_device *dev)
  9707. {
  9708. struct drm_i915_private *dev_priv = dev->dev_private;
  9709. if (HAS_DDI(dev))
  9710. intel_ddi_pll_init(dev);
  9711. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9712. ibx_pch_dpll_init(dev);
  9713. else
  9714. dev_priv->num_shared_dpll = 0;
  9715. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9716. }
  9717. static int
  9718. intel_primary_plane_disable(struct drm_plane *plane)
  9719. {
  9720. struct drm_device *dev = plane->dev;
  9721. struct intel_crtc *intel_crtc;
  9722. if (!plane->fb)
  9723. return 0;
  9724. BUG_ON(!plane->crtc);
  9725. intel_crtc = to_intel_crtc(plane->crtc);
  9726. /*
  9727. * Even though we checked plane->fb above, it's still possible that
  9728. * the primary plane has been implicitly disabled because the crtc
  9729. * coordinates given weren't visible, or because we detected
  9730. * that it was 100% covered by a sprite plane. Or, the CRTC may be
  9731. * off and we've set a fb, but haven't actually turned on the CRTC yet.
  9732. * In either case, we need to unpin the FB and let the fb pointer get
  9733. * updated, but otherwise we don't need to touch the hardware.
  9734. */
  9735. if (!intel_crtc->primary_enabled)
  9736. goto disable_unpin;
  9737. intel_crtc_wait_for_pending_flips(plane->crtc);
  9738. intel_disable_primary_hw_plane(plane, plane->crtc);
  9739. disable_unpin:
  9740. mutex_lock(&dev->struct_mutex);
  9741. i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
  9742. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9743. intel_unpin_fb_obj(intel_fb_obj(plane->fb));
  9744. mutex_unlock(&dev->struct_mutex);
  9745. plane->fb = NULL;
  9746. return 0;
  9747. }
  9748. static int
  9749. intel_check_primary_plane(struct drm_plane *plane,
  9750. struct intel_plane_state *state)
  9751. {
  9752. struct drm_crtc *crtc = state->crtc;
  9753. struct drm_framebuffer *fb = state->fb;
  9754. struct drm_rect *dest = &state->dst;
  9755. struct drm_rect *src = &state->src;
  9756. const struct drm_rect *clip = &state->clip;
  9757. return drm_plane_helper_check_update(plane, crtc, fb,
  9758. src, dest, clip,
  9759. DRM_PLANE_HELPER_NO_SCALING,
  9760. DRM_PLANE_HELPER_NO_SCALING,
  9761. false, true, &state->visible);
  9762. }
  9763. static int
  9764. intel_commit_primary_plane(struct drm_plane *plane,
  9765. struct intel_plane_state *state)
  9766. {
  9767. struct drm_crtc *crtc = state->crtc;
  9768. struct drm_framebuffer *fb = state->fb;
  9769. struct drm_device *dev = crtc->dev;
  9770. struct drm_i915_private *dev_priv = dev->dev_private;
  9771. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9772. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9773. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9774. struct intel_plane *intel_plane = to_intel_plane(plane);
  9775. struct drm_rect *src = &state->src;
  9776. int ret;
  9777. intel_crtc_wait_for_pending_flips(crtc);
  9778. /*
  9779. * If clipping results in a non-visible primary plane, we'll disable
  9780. * the primary plane. Note that this is a bit different than what
  9781. * happens if userspace explicitly disables the plane by passing fb=0
  9782. * because plane->fb still gets set and pinned.
  9783. */
  9784. if (!state->visible) {
  9785. mutex_lock(&dev->struct_mutex);
  9786. /*
  9787. * Try to pin the new fb first so that we can bail out if we
  9788. * fail.
  9789. */
  9790. if (plane->fb != fb) {
  9791. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  9792. if (ret) {
  9793. mutex_unlock(&dev->struct_mutex);
  9794. return ret;
  9795. }
  9796. }
  9797. i915_gem_track_fb(old_obj, obj,
  9798. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9799. if (intel_crtc->primary_enabled)
  9800. intel_disable_primary_hw_plane(plane, crtc);
  9801. if (plane->fb != fb)
  9802. if (plane->fb)
  9803. intel_unpin_fb_obj(old_obj);
  9804. mutex_unlock(&dev->struct_mutex);
  9805. } else {
  9806. if (intel_crtc && intel_crtc->active &&
  9807. intel_crtc->primary_enabled) {
  9808. /*
  9809. * FBC does not work on some platforms for rotated
  9810. * planes, so disable it when rotation is not 0 and
  9811. * update it when rotation is set back to 0.
  9812. *
  9813. * FIXME: This is redundant with the fbc update done in
  9814. * the primary plane enable function except that that
  9815. * one is done too late. We eventually need to unify
  9816. * this.
  9817. */
  9818. if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9819. dev_priv->fbc.plane == intel_crtc->plane &&
  9820. intel_plane->rotation != BIT(DRM_ROTATE_0)) {
  9821. intel_disable_fbc(dev);
  9822. }
  9823. }
  9824. ret = intel_pipe_set_base(crtc, src->x1, src->y1, fb);
  9825. if (ret)
  9826. return ret;
  9827. if (!intel_crtc->primary_enabled)
  9828. intel_enable_primary_hw_plane(plane, crtc);
  9829. }
  9830. intel_plane->crtc_x = state->orig_dst.x1;
  9831. intel_plane->crtc_y = state->orig_dst.y1;
  9832. intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
  9833. intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
  9834. intel_plane->src_x = state->orig_src.x1;
  9835. intel_plane->src_y = state->orig_src.y1;
  9836. intel_plane->src_w = drm_rect_width(&state->orig_src);
  9837. intel_plane->src_h = drm_rect_height(&state->orig_src);
  9838. intel_plane->obj = obj;
  9839. return 0;
  9840. }
  9841. static int
  9842. intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
  9843. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9844. unsigned int crtc_w, unsigned int crtc_h,
  9845. uint32_t src_x, uint32_t src_y,
  9846. uint32_t src_w, uint32_t src_h)
  9847. {
  9848. struct intel_plane_state state;
  9849. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9850. int ret;
  9851. state.crtc = crtc;
  9852. state.fb = fb;
  9853. /* sample coordinates in 16.16 fixed point */
  9854. state.src.x1 = src_x;
  9855. state.src.x2 = src_x + src_w;
  9856. state.src.y1 = src_y;
  9857. state.src.y2 = src_y + src_h;
  9858. /* integer pixels */
  9859. state.dst.x1 = crtc_x;
  9860. state.dst.x2 = crtc_x + crtc_w;
  9861. state.dst.y1 = crtc_y;
  9862. state.dst.y2 = crtc_y + crtc_h;
  9863. state.clip.x1 = 0;
  9864. state.clip.y1 = 0;
  9865. state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
  9866. state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
  9867. state.orig_src = state.src;
  9868. state.orig_dst = state.dst;
  9869. ret = intel_check_primary_plane(plane, &state);
  9870. if (ret)
  9871. return ret;
  9872. intel_commit_primary_plane(plane, &state);
  9873. return 0;
  9874. }
  9875. /* Common destruction function for both primary and cursor planes */
  9876. static void intel_plane_destroy(struct drm_plane *plane)
  9877. {
  9878. struct intel_plane *intel_plane = to_intel_plane(plane);
  9879. drm_plane_cleanup(plane);
  9880. kfree(intel_plane);
  9881. }
  9882. static const struct drm_plane_funcs intel_primary_plane_funcs = {
  9883. .update_plane = intel_primary_plane_setplane,
  9884. .disable_plane = intel_primary_plane_disable,
  9885. .destroy = intel_plane_destroy,
  9886. .set_property = intel_plane_set_property
  9887. };
  9888. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  9889. int pipe)
  9890. {
  9891. struct intel_plane *primary;
  9892. const uint32_t *intel_primary_formats;
  9893. int num_formats;
  9894. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  9895. if (primary == NULL)
  9896. return NULL;
  9897. primary->can_scale = false;
  9898. primary->max_downscale = 1;
  9899. primary->pipe = pipe;
  9900. primary->plane = pipe;
  9901. primary->rotation = BIT(DRM_ROTATE_0);
  9902. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  9903. primary->plane = !pipe;
  9904. if (INTEL_INFO(dev)->gen <= 3) {
  9905. intel_primary_formats = intel_primary_formats_gen2;
  9906. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  9907. } else {
  9908. intel_primary_formats = intel_primary_formats_gen4;
  9909. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  9910. }
  9911. drm_universal_plane_init(dev, &primary->base, 0,
  9912. &intel_primary_plane_funcs,
  9913. intel_primary_formats, num_formats,
  9914. DRM_PLANE_TYPE_PRIMARY);
  9915. if (INTEL_INFO(dev)->gen >= 4) {
  9916. if (!dev->mode_config.rotation_property)
  9917. dev->mode_config.rotation_property =
  9918. drm_mode_create_rotation_property(dev,
  9919. BIT(DRM_ROTATE_0) |
  9920. BIT(DRM_ROTATE_180));
  9921. if (dev->mode_config.rotation_property)
  9922. drm_object_attach_property(&primary->base.base,
  9923. dev->mode_config.rotation_property,
  9924. primary->rotation);
  9925. }
  9926. return &primary->base;
  9927. }
  9928. static int
  9929. intel_cursor_plane_disable(struct drm_plane *plane)
  9930. {
  9931. if (!plane->fb)
  9932. return 0;
  9933. BUG_ON(!plane->crtc);
  9934. return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
  9935. }
  9936. static int
  9937. intel_check_cursor_plane(struct drm_plane *plane,
  9938. struct intel_plane_state *state)
  9939. {
  9940. struct drm_crtc *crtc = state->crtc;
  9941. struct drm_framebuffer *fb = state->fb;
  9942. struct drm_rect *dest = &state->dst;
  9943. struct drm_rect *src = &state->src;
  9944. const struct drm_rect *clip = &state->clip;
  9945. return drm_plane_helper_check_update(plane, crtc, fb,
  9946. src, dest, clip,
  9947. DRM_PLANE_HELPER_NO_SCALING,
  9948. DRM_PLANE_HELPER_NO_SCALING,
  9949. true, true, &state->visible);
  9950. }
  9951. static int
  9952. intel_commit_cursor_plane(struct drm_plane *plane,
  9953. struct intel_plane_state *state)
  9954. {
  9955. struct drm_crtc *crtc = state->crtc;
  9956. struct drm_framebuffer *fb = state->fb;
  9957. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9958. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  9959. struct drm_i915_gem_object *obj = intel_fb->obj;
  9960. int crtc_w, crtc_h;
  9961. crtc->cursor_x = state->orig_dst.x1;
  9962. crtc->cursor_y = state->orig_dst.y1;
  9963. if (fb != crtc->cursor->fb) {
  9964. crtc_w = drm_rect_width(&state->orig_dst);
  9965. crtc_h = drm_rect_height(&state->orig_dst);
  9966. return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
  9967. } else {
  9968. intel_crtc_update_cursor(crtc, state->visible);
  9969. intel_frontbuffer_flip(crtc->dev,
  9970. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
  9971. return 0;
  9972. }
  9973. }
  9974. static int
  9975. intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
  9976. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9977. unsigned int crtc_w, unsigned int crtc_h,
  9978. uint32_t src_x, uint32_t src_y,
  9979. uint32_t src_w, uint32_t src_h)
  9980. {
  9981. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9982. struct intel_plane_state state;
  9983. int ret;
  9984. state.crtc = crtc;
  9985. state.fb = fb;
  9986. /* sample coordinates in 16.16 fixed point */
  9987. state.src.x1 = src_x;
  9988. state.src.x2 = src_x + src_w;
  9989. state.src.y1 = src_y;
  9990. state.src.y2 = src_y + src_h;
  9991. /* integer pixels */
  9992. state.dst.x1 = crtc_x;
  9993. state.dst.x2 = crtc_x + crtc_w;
  9994. state.dst.y1 = crtc_y;
  9995. state.dst.y2 = crtc_y + crtc_h;
  9996. state.clip.x1 = 0;
  9997. state.clip.y1 = 0;
  9998. state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
  9999. state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
  10000. state.orig_src = state.src;
  10001. state.orig_dst = state.dst;
  10002. ret = intel_check_cursor_plane(plane, &state);
  10003. if (ret)
  10004. return ret;
  10005. return intel_commit_cursor_plane(plane, &state);
  10006. }
  10007. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  10008. .update_plane = intel_cursor_plane_update,
  10009. .disable_plane = intel_cursor_plane_disable,
  10010. .destroy = intel_plane_destroy,
  10011. };
  10012. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  10013. int pipe)
  10014. {
  10015. struct intel_plane *cursor;
  10016. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  10017. if (cursor == NULL)
  10018. return NULL;
  10019. cursor->can_scale = false;
  10020. cursor->max_downscale = 1;
  10021. cursor->pipe = pipe;
  10022. cursor->plane = pipe;
  10023. drm_universal_plane_init(dev, &cursor->base, 0,
  10024. &intel_cursor_plane_funcs,
  10025. intel_cursor_formats,
  10026. ARRAY_SIZE(intel_cursor_formats),
  10027. DRM_PLANE_TYPE_CURSOR);
  10028. return &cursor->base;
  10029. }
  10030. static void intel_crtc_init(struct drm_device *dev, int pipe)
  10031. {
  10032. struct drm_i915_private *dev_priv = dev->dev_private;
  10033. struct intel_crtc *intel_crtc;
  10034. struct drm_plane *primary = NULL;
  10035. struct drm_plane *cursor = NULL;
  10036. int i, ret;
  10037. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  10038. if (intel_crtc == NULL)
  10039. return;
  10040. primary = intel_primary_plane_create(dev, pipe);
  10041. if (!primary)
  10042. goto fail;
  10043. cursor = intel_cursor_plane_create(dev, pipe);
  10044. if (!cursor)
  10045. goto fail;
  10046. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  10047. cursor, &intel_crtc_funcs);
  10048. if (ret)
  10049. goto fail;
  10050. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  10051. for (i = 0; i < 256; i++) {
  10052. intel_crtc->lut_r[i] = i;
  10053. intel_crtc->lut_g[i] = i;
  10054. intel_crtc->lut_b[i] = i;
  10055. }
  10056. /*
  10057. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  10058. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  10059. */
  10060. intel_crtc->pipe = pipe;
  10061. intel_crtc->plane = pipe;
  10062. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  10063. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  10064. intel_crtc->plane = !pipe;
  10065. }
  10066. intel_crtc->cursor_base = ~0;
  10067. intel_crtc->cursor_cntl = ~0;
  10068. intel_crtc->cursor_size = ~0;
  10069. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  10070. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  10071. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  10072. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  10073. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  10074. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  10075. return;
  10076. fail:
  10077. if (primary)
  10078. drm_plane_cleanup(primary);
  10079. if (cursor)
  10080. drm_plane_cleanup(cursor);
  10081. kfree(intel_crtc);
  10082. }
  10083. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  10084. {
  10085. struct drm_encoder *encoder = connector->base.encoder;
  10086. struct drm_device *dev = connector->base.dev;
  10087. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  10088. if (!encoder)
  10089. return INVALID_PIPE;
  10090. return to_intel_crtc(encoder->crtc)->pipe;
  10091. }
  10092. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  10093. struct drm_file *file)
  10094. {
  10095. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  10096. struct drm_crtc *drmmode_crtc;
  10097. struct intel_crtc *crtc;
  10098. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  10099. return -ENODEV;
  10100. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  10101. if (!drmmode_crtc) {
  10102. DRM_ERROR("no such CRTC id\n");
  10103. return -ENOENT;
  10104. }
  10105. crtc = to_intel_crtc(drmmode_crtc);
  10106. pipe_from_crtc_id->pipe = crtc->pipe;
  10107. return 0;
  10108. }
  10109. static int intel_encoder_clones(struct intel_encoder *encoder)
  10110. {
  10111. struct drm_device *dev = encoder->base.dev;
  10112. struct intel_encoder *source_encoder;
  10113. int index_mask = 0;
  10114. int entry = 0;
  10115. for_each_intel_encoder(dev, source_encoder) {
  10116. if (encoders_cloneable(encoder, source_encoder))
  10117. index_mask |= (1 << entry);
  10118. entry++;
  10119. }
  10120. return index_mask;
  10121. }
  10122. static bool has_edp_a(struct drm_device *dev)
  10123. {
  10124. struct drm_i915_private *dev_priv = dev->dev_private;
  10125. if (!IS_MOBILE(dev))
  10126. return false;
  10127. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  10128. return false;
  10129. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  10130. return false;
  10131. return true;
  10132. }
  10133. const char *intel_output_name(int output)
  10134. {
  10135. static const char *names[] = {
  10136. [INTEL_OUTPUT_UNUSED] = "Unused",
  10137. [INTEL_OUTPUT_ANALOG] = "Analog",
  10138. [INTEL_OUTPUT_DVO] = "DVO",
  10139. [INTEL_OUTPUT_SDVO] = "SDVO",
  10140. [INTEL_OUTPUT_LVDS] = "LVDS",
  10141. [INTEL_OUTPUT_TVOUT] = "TV",
  10142. [INTEL_OUTPUT_HDMI] = "HDMI",
  10143. [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
  10144. [INTEL_OUTPUT_EDP] = "eDP",
  10145. [INTEL_OUTPUT_DSI] = "DSI",
  10146. [INTEL_OUTPUT_UNKNOWN] = "Unknown",
  10147. };
  10148. if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
  10149. return "Invalid";
  10150. return names[output];
  10151. }
  10152. static bool intel_crt_present(struct drm_device *dev)
  10153. {
  10154. struct drm_i915_private *dev_priv = dev->dev_private;
  10155. if (INTEL_INFO(dev)->gen >= 9)
  10156. return false;
  10157. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  10158. return false;
  10159. if (IS_CHERRYVIEW(dev))
  10160. return false;
  10161. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10162. return false;
  10163. return true;
  10164. }
  10165. static void intel_setup_outputs(struct drm_device *dev)
  10166. {
  10167. struct drm_i915_private *dev_priv = dev->dev_private;
  10168. struct intel_encoder *encoder;
  10169. bool dpd_is_edp = false;
  10170. intel_lvds_init(dev);
  10171. if (intel_crt_present(dev))
  10172. intel_crt_init(dev);
  10173. if (HAS_DDI(dev)) {
  10174. int found;
  10175. /* Haswell uses DDI functions to detect digital outputs */
  10176. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10177. /* DDI A only supports eDP */
  10178. if (found)
  10179. intel_ddi_init(dev, PORT_A);
  10180. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10181. * register */
  10182. found = I915_READ(SFUSE_STRAP);
  10183. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10184. intel_ddi_init(dev, PORT_B);
  10185. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10186. intel_ddi_init(dev, PORT_C);
  10187. if (found & SFUSE_STRAP_DDID_DETECTED)
  10188. intel_ddi_init(dev, PORT_D);
  10189. } else if (HAS_PCH_SPLIT(dev)) {
  10190. int found;
  10191. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10192. if (has_edp_a(dev))
  10193. intel_dp_init(dev, DP_A, PORT_A);
  10194. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10195. /* PCH SDVOB multiplex with HDMIB */
  10196. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10197. if (!found)
  10198. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10199. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10200. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10201. }
  10202. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10203. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10204. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10205. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10206. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10207. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10208. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10209. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10210. } else if (IS_VALLEYVIEW(dev)) {
  10211. /*
  10212. * The DP_DETECTED bit is the latched state of the DDC
  10213. * SDA pin at boot. However since eDP doesn't require DDC
  10214. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  10215. * eDP ports may have been muxed to an alternate function.
  10216. * Thus we can't rely on the DP_DETECTED bit alone to detect
  10217. * eDP ports. Consult the VBT as well as DP_DETECTED to
  10218. * detect eDP ports.
  10219. */
  10220. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED)
  10221. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10222. PORT_B);
  10223. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  10224. intel_dp_is_edp(dev, PORT_B))
  10225. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10226. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED)
  10227. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10228. PORT_C);
  10229. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  10230. intel_dp_is_edp(dev, PORT_C))
  10231. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10232. if (IS_CHERRYVIEW(dev)) {
  10233. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  10234. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10235. PORT_D);
  10236. /* eDP not supported on port D, so don't check VBT */
  10237. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10238. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10239. }
  10240. intel_dsi_init(dev);
  10241. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10242. bool found = false;
  10243. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10244. DRM_DEBUG_KMS("probing SDVOB\n");
  10245. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10246. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10247. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10248. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10249. }
  10250. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10251. intel_dp_init(dev, DP_B, PORT_B);
  10252. }
  10253. /* Before G4X SDVOC doesn't have its own detect register */
  10254. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10255. DRM_DEBUG_KMS("probing SDVOC\n");
  10256. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10257. }
  10258. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10259. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10260. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10261. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10262. }
  10263. if (SUPPORTS_INTEGRATED_DP(dev))
  10264. intel_dp_init(dev, DP_C, PORT_C);
  10265. }
  10266. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10267. (I915_READ(DP_D) & DP_DETECTED))
  10268. intel_dp_init(dev, DP_D, PORT_D);
  10269. } else if (IS_GEN2(dev))
  10270. intel_dvo_init(dev);
  10271. if (SUPPORTS_TV(dev))
  10272. intel_tv_init(dev);
  10273. intel_edp_psr_init(dev);
  10274. for_each_intel_encoder(dev, encoder) {
  10275. encoder->base.possible_crtcs = encoder->crtc_mask;
  10276. encoder->base.possible_clones =
  10277. intel_encoder_clones(encoder);
  10278. }
  10279. intel_init_pch_refclk(dev);
  10280. drm_helper_move_panel_connectors_to_head(dev);
  10281. }
  10282. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10283. {
  10284. struct drm_device *dev = fb->dev;
  10285. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10286. drm_framebuffer_cleanup(fb);
  10287. mutex_lock(&dev->struct_mutex);
  10288. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10289. drm_gem_object_unreference(&intel_fb->obj->base);
  10290. mutex_unlock(&dev->struct_mutex);
  10291. kfree(intel_fb);
  10292. }
  10293. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10294. struct drm_file *file,
  10295. unsigned int *handle)
  10296. {
  10297. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10298. struct drm_i915_gem_object *obj = intel_fb->obj;
  10299. return drm_gem_handle_create(file, &obj->base, handle);
  10300. }
  10301. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10302. .destroy = intel_user_framebuffer_destroy,
  10303. .create_handle = intel_user_framebuffer_create_handle,
  10304. };
  10305. static int intel_framebuffer_init(struct drm_device *dev,
  10306. struct intel_framebuffer *intel_fb,
  10307. struct drm_mode_fb_cmd2 *mode_cmd,
  10308. struct drm_i915_gem_object *obj)
  10309. {
  10310. int aligned_height;
  10311. int pitch_limit;
  10312. int ret;
  10313. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10314. if (obj->tiling_mode == I915_TILING_Y) {
  10315. DRM_DEBUG("hardware does not support tiling Y\n");
  10316. return -EINVAL;
  10317. }
  10318. if (mode_cmd->pitches[0] & 63) {
  10319. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  10320. mode_cmd->pitches[0]);
  10321. return -EINVAL;
  10322. }
  10323. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10324. pitch_limit = 32*1024;
  10325. } else if (INTEL_INFO(dev)->gen >= 4) {
  10326. if (obj->tiling_mode)
  10327. pitch_limit = 16*1024;
  10328. else
  10329. pitch_limit = 32*1024;
  10330. } else if (INTEL_INFO(dev)->gen >= 3) {
  10331. if (obj->tiling_mode)
  10332. pitch_limit = 8*1024;
  10333. else
  10334. pitch_limit = 16*1024;
  10335. } else
  10336. /* XXX DSPC is limited to 4k tiled */
  10337. pitch_limit = 8*1024;
  10338. if (mode_cmd->pitches[0] > pitch_limit) {
  10339. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  10340. obj->tiling_mode ? "tiled" : "linear",
  10341. mode_cmd->pitches[0], pitch_limit);
  10342. return -EINVAL;
  10343. }
  10344. if (obj->tiling_mode != I915_TILING_NONE &&
  10345. mode_cmd->pitches[0] != obj->stride) {
  10346. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10347. mode_cmd->pitches[0], obj->stride);
  10348. return -EINVAL;
  10349. }
  10350. /* Reject formats not supported by any plane early. */
  10351. switch (mode_cmd->pixel_format) {
  10352. case DRM_FORMAT_C8:
  10353. case DRM_FORMAT_RGB565:
  10354. case DRM_FORMAT_XRGB8888:
  10355. case DRM_FORMAT_ARGB8888:
  10356. break;
  10357. case DRM_FORMAT_XRGB1555:
  10358. case DRM_FORMAT_ARGB1555:
  10359. if (INTEL_INFO(dev)->gen > 3) {
  10360. DRM_DEBUG("unsupported pixel format: %s\n",
  10361. drm_get_format_name(mode_cmd->pixel_format));
  10362. return -EINVAL;
  10363. }
  10364. break;
  10365. case DRM_FORMAT_XBGR8888:
  10366. case DRM_FORMAT_ABGR8888:
  10367. case DRM_FORMAT_XRGB2101010:
  10368. case DRM_FORMAT_ARGB2101010:
  10369. case DRM_FORMAT_XBGR2101010:
  10370. case DRM_FORMAT_ABGR2101010:
  10371. if (INTEL_INFO(dev)->gen < 4) {
  10372. DRM_DEBUG("unsupported pixel format: %s\n",
  10373. drm_get_format_name(mode_cmd->pixel_format));
  10374. return -EINVAL;
  10375. }
  10376. break;
  10377. case DRM_FORMAT_YUYV:
  10378. case DRM_FORMAT_UYVY:
  10379. case DRM_FORMAT_YVYU:
  10380. case DRM_FORMAT_VYUY:
  10381. if (INTEL_INFO(dev)->gen < 5) {
  10382. DRM_DEBUG("unsupported pixel format: %s\n",
  10383. drm_get_format_name(mode_cmd->pixel_format));
  10384. return -EINVAL;
  10385. }
  10386. break;
  10387. default:
  10388. DRM_DEBUG("unsupported pixel format: %s\n",
  10389. drm_get_format_name(mode_cmd->pixel_format));
  10390. return -EINVAL;
  10391. }
  10392. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10393. if (mode_cmd->offsets[0] != 0)
  10394. return -EINVAL;
  10395. aligned_height = intel_align_height(dev, mode_cmd->height,
  10396. obj->tiling_mode);
  10397. /* FIXME drm helper for size checks (especially planar formats)? */
  10398. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10399. return -EINVAL;
  10400. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10401. intel_fb->obj = obj;
  10402. intel_fb->obj->framebuffer_references++;
  10403. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10404. if (ret) {
  10405. DRM_ERROR("framebuffer init failed %d\n", ret);
  10406. return ret;
  10407. }
  10408. return 0;
  10409. }
  10410. static struct drm_framebuffer *
  10411. intel_user_framebuffer_create(struct drm_device *dev,
  10412. struct drm_file *filp,
  10413. struct drm_mode_fb_cmd2 *mode_cmd)
  10414. {
  10415. struct drm_i915_gem_object *obj;
  10416. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10417. mode_cmd->handles[0]));
  10418. if (&obj->base == NULL)
  10419. return ERR_PTR(-ENOENT);
  10420. return intel_framebuffer_create(dev, mode_cmd, obj);
  10421. }
  10422. #ifndef CONFIG_DRM_I915_FBDEV
  10423. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10424. {
  10425. }
  10426. #endif
  10427. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10428. .fb_create = intel_user_framebuffer_create,
  10429. .output_poll_changed = intel_fbdev_output_poll_changed,
  10430. };
  10431. /* Set up chip specific display functions */
  10432. static void intel_init_display(struct drm_device *dev)
  10433. {
  10434. struct drm_i915_private *dev_priv = dev->dev_private;
  10435. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10436. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10437. else if (IS_CHERRYVIEW(dev))
  10438. dev_priv->display.find_dpll = chv_find_best_dpll;
  10439. else if (IS_VALLEYVIEW(dev))
  10440. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10441. else if (IS_PINEVIEW(dev))
  10442. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10443. else
  10444. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10445. if (HAS_DDI(dev)) {
  10446. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10447. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10448. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  10449. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10450. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10451. dev_priv->display.off = ironlake_crtc_off;
  10452. if (INTEL_INFO(dev)->gen >= 9)
  10453. dev_priv->display.update_primary_plane =
  10454. skylake_update_primary_plane;
  10455. else
  10456. dev_priv->display.update_primary_plane =
  10457. ironlake_update_primary_plane;
  10458. } else if (HAS_PCH_SPLIT(dev)) {
  10459. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10460. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10461. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  10462. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10463. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  10464. dev_priv->display.off = ironlake_crtc_off;
  10465. dev_priv->display.update_primary_plane =
  10466. ironlake_update_primary_plane;
  10467. } else if (IS_VALLEYVIEW(dev)) {
  10468. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10469. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10470. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  10471. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10472. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10473. dev_priv->display.off = i9xx_crtc_off;
  10474. dev_priv->display.update_primary_plane =
  10475. i9xx_update_primary_plane;
  10476. } else {
  10477. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10478. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10479. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  10480. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  10481. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10482. dev_priv->display.off = i9xx_crtc_off;
  10483. dev_priv->display.update_primary_plane =
  10484. i9xx_update_primary_plane;
  10485. }
  10486. /* Returns the core display clock speed */
  10487. if (IS_VALLEYVIEW(dev))
  10488. dev_priv->display.get_display_clock_speed =
  10489. valleyview_get_display_clock_speed;
  10490. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10491. dev_priv->display.get_display_clock_speed =
  10492. i945_get_display_clock_speed;
  10493. else if (IS_I915G(dev))
  10494. dev_priv->display.get_display_clock_speed =
  10495. i915_get_display_clock_speed;
  10496. else if (IS_I945GM(dev) || IS_845G(dev))
  10497. dev_priv->display.get_display_clock_speed =
  10498. i9xx_misc_get_display_clock_speed;
  10499. else if (IS_PINEVIEW(dev))
  10500. dev_priv->display.get_display_clock_speed =
  10501. pnv_get_display_clock_speed;
  10502. else if (IS_I915GM(dev))
  10503. dev_priv->display.get_display_clock_speed =
  10504. i915gm_get_display_clock_speed;
  10505. else if (IS_I865G(dev))
  10506. dev_priv->display.get_display_clock_speed =
  10507. i865_get_display_clock_speed;
  10508. else if (IS_I85X(dev))
  10509. dev_priv->display.get_display_clock_speed =
  10510. i855_get_display_clock_speed;
  10511. else /* 852, 830 */
  10512. dev_priv->display.get_display_clock_speed =
  10513. i830_get_display_clock_speed;
  10514. if (IS_G4X(dev)) {
  10515. dev_priv->display.write_eld = g4x_write_eld;
  10516. } else if (IS_GEN5(dev)) {
  10517. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10518. dev_priv->display.write_eld = ironlake_write_eld;
  10519. } else if (IS_GEN6(dev)) {
  10520. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10521. dev_priv->display.write_eld = ironlake_write_eld;
  10522. dev_priv->display.modeset_global_resources =
  10523. snb_modeset_global_resources;
  10524. } else if (IS_IVYBRIDGE(dev)) {
  10525. /* FIXME: detect B0+ stepping and use auto training */
  10526. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10527. dev_priv->display.write_eld = ironlake_write_eld;
  10528. dev_priv->display.modeset_global_resources =
  10529. ivb_modeset_global_resources;
  10530. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  10531. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10532. dev_priv->display.write_eld = haswell_write_eld;
  10533. dev_priv->display.modeset_global_resources =
  10534. haswell_modeset_global_resources;
  10535. } else if (IS_VALLEYVIEW(dev)) {
  10536. dev_priv->display.modeset_global_resources =
  10537. valleyview_modeset_global_resources;
  10538. dev_priv->display.write_eld = ironlake_write_eld;
  10539. } else if (INTEL_INFO(dev)->gen >= 9) {
  10540. dev_priv->display.write_eld = haswell_write_eld;
  10541. dev_priv->display.modeset_global_resources =
  10542. haswell_modeset_global_resources;
  10543. }
  10544. /* Default just returns -ENODEV to indicate unsupported */
  10545. dev_priv->display.queue_flip = intel_default_queue_flip;
  10546. switch (INTEL_INFO(dev)->gen) {
  10547. case 2:
  10548. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10549. break;
  10550. case 3:
  10551. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  10552. break;
  10553. case 4:
  10554. case 5:
  10555. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  10556. break;
  10557. case 6:
  10558. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  10559. break;
  10560. case 7:
  10561. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  10562. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  10563. break;
  10564. }
  10565. intel_panel_init_backlight_funcs(dev);
  10566. mutex_init(&dev_priv->pps_mutex);
  10567. }
  10568. /*
  10569. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  10570. * resume, or other times. This quirk makes sure that's the case for
  10571. * affected systems.
  10572. */
  10573. static void quirk_pipea_force(struct drm_device *dev)
  10574. {
  10575. struct drm_i915_private *dev_priv = dev->dev_private;
  10576. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  10577. DRM_INFO("applying pipe a force quirk\n");
  10578. }
  10579. static void quirk_pipeb_force(struct drm_device *dev)
  10580. {
  10581. struct drm_i915_private *dev_priv = dev->dev_private;
  10582. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  10583. DRM_INFO("applying pipe b force quirk\n");
  10584. }
  10585. /*
  10586. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  10587. */
  10588. static void quirk_ssc_force_disable(struct drm_device *dev)
  10589. {
  10590. struct drm_i915_private *dev_priv = dev->dev_private;
  10591. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  10592. DRM_INFO("applying lvds SSC disable quirk\n");
  10593. }
  10594. /*
  10595. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  10596. * brightness value
  10597. */
  10598. static void quirk_invert_brightness(struct drm_device *dev)
  10599. {
  10600. struct drm_i915_private *dev_priv = dev->dev_private;
  10601. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  10602. DRM_INFO("applying inverted panel brightness quirk\n");
  10603. }
  10604. /* Some VBT's incorrectly indicate no backlight is present */
  10605. static void quirk_backlight_present(struct drm_device *dev)
  10606. {
  10607. struct drm_i915_private *dev_priv = dev->dev_private;
  10608. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  10609. DRM_INFO("applying backlight present quirk\n");
  10610. }
  10611. struct intel_quirk {
  10612. int device;
  10613. int subsystem_vendor;
  10614. int subsystem_device;
  10615. void (*hook)(struct drm_device *dev);
  10616. };
  10617. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  10618. struct intel_dmi_quirk {
  10619. void (*hook)(struct drm_device *dev);
  10620. const struct dmi_system_id (*dmi_id_list)[];
  10621. };
  10622. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  10623. {
  10624. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  10625. return 1;
  10626. }
  10627. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  10628. {
  10629. .dmi_id_list = &(const struct dmi_system_id[]) {
  10630. {
  10631. .callback = intel_dmi_reverse_brightness,
  10632. .ident = "NCR Corporation",
  10633. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  10634. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  10635. },
  10636. },
  10637. { } /* terminating entry */
  10638. },
  10639. .hook = quirk_invert_brightness,
  10640. },
  10641. };
  10642. static struct intel_quirk intel_quirks[] = {
  10643. /* HP Mini needs pipe A force quirk (LP: #322104) */
  10644. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  10645. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  10646. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  10647. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  10648. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  10649. /* 830 needs to leave pipe A & dpll A up */
  10650. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  10651. /* 830 needs to leave pipe B & dpll B up */
  10652. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  10653. /* Lenovo U160 cannot use SSC on LVDS */
  10654. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  10655. /* Sony Vaio Y cannot use SSC on LVDS */
  10656. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  10657. /* Acer Aspire 5734Z must invert backlight brightness */
  10658. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  10659. /* Acer/eMachines G725 */
  10660. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  10661. /* Acer/eMachines e725 */
  10662. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  10663. /* Acer/Packard Bell NCL20 */
  10664. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  10665. /* Acer Aspire 4736Z */
  10666. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  10667. /* Acer Aspire 5336 */
  10668. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  10669. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  10670. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  10671. /* Acer C720 Chromebook (Core i3 4005U) */
  10672. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  10673. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  10674. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  10675. /* HP Chromebook 14 (Celeron 2955U) */
  10676. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  10677. };
  10678. static void intel_init_quirks(struct drm_device *dev)
  10679. {
  10680. struct pci_dev *d = dev->pdev;
  10681. int i;
  10682. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  10683. struct intel_quirk *q = &intel_quirks[i];
  10684. if (d->device == q->device &&
  10685. (d->subsystem_vendor == q->subsystem_vendor ||
  10686. q->subsystem_vendor == PCI_ANY_ID) &&
  10687. (d->subsystem_device == q->subsystem_device ||
  10688. q->subsystem_device == PCI_ANY_ID))
  10689. q->hook(dev);
  10690. }
  10691. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  10692. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  10693. intel_dmi_quirks[i].hook(dev);
  10694. }
  10695. }
  10696. /* Disable the VGA plane that we never use */
  10697. static void i915_disable_vga(struct drm_device *dev)
  10698. {
  10699. struct drm_i915_private *dev_priv = dev->dev_private;
  10700. u8 sr1;
  10701. u32 vga_reg = i915_vgacntrl_reg(dev);
  10702. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  10703. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  10704. outb(SR01, VGA_SR_INDEX);
  10705. sr1 = inb(VGA_SR_DATA);
  10706. outb(sr1 | 1<<5, VGA_SR_DATA);
  10707. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  10708. udelay(300);
  10709. /*
  10710. * Fujitsu-Siemens Lifebook S6010 (830) has problems resuming
  10711. * from S3 without preserving (some of?) the other bits.
  10712. */
  10713. I915_WRITE(vga_reg, dev_priv->bios_vgacntr | VGA_DISP_DISABLE);
  10714. POSTING_READ(vga_reg);
  10715. }
  10716. void intel_modeset_init_hw(struct drm_device *dev)
  10717. {
  10718. intel_prepare_ddi(dev);
  10719. if (IS_VALLEYVIEW(dev))
  10720. vlv_update_cdclk(dev);
  10721. intel_init_clock_gating(dev);
  10722. intel_enable_gt_powersave(dev);
  10723. }
  10724. void intel_modeset_init(struct drm_device *dev)
  10725. {
  10726. struct drm_i915_private *dev_priv = dev->dev_private;
  10727. int sprite, ret;
  10728. enum pipe pipe;
  10729. struct intel_crtc *crtc;
  10730. drm_mode_config_init(dev);
  10731. dev->mode_config.min_width = 0;
  10732. dev->mode_config.min_height = 0;
  10733. dev->mode_config.preferred_depth = 24;
  10734. dev->mode_config.prefer_shadow = 1;
  10735. dev->mode_config.funcs = &intel_mode_funcs;
  10736. intel_init_quirks(dev);
  10737. intel_init_pm(dev);
  10738. if (INTEL_INFO(dev)->num_pipes == 0)
  10739. return;
  10740. intel_init_display(dev);
  10741. if (IS_GEN2(dev)) {
  10742. dev->mode_config.max_width = 2048;
  10743. dev->mode_config.max_height = 2048;
  10744. } else if (IS_GEN3(dev)) {
  10745. dev->mode_config.max_width = 4096;
  10746. dev->mode_config.max_height = 4096;
  10747. } else {
  10748. dev->mode_config.max_width = 8192;
  10749. dev->mode_config.max_height = 8192;
  10750. }
  10751. if (IS_845G(dev) || IS_I865G(dev)) {
  10752. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  10753. dev->mode_config.cursor_height = 1023;
  10754. } else if (IS_GEN2(dev)) {
  10755. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  10756. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  10757. } else {
  10758. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  10759. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  10760. }
  10761. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  10762. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  10763. INTEL_INFO(dev)->num_pipes,
  10764. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  10765. for_each_pipe(dev_priv, pipe) {
  10766. intel_crtc_init(dev, pipe);
  10767. for_each_sprite(pipe, sprite) {
  10768. ret = intel_plane_init(dev, pipe, sprite);
  10769. if (ret)
  10770. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  10771. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  10772. }
  10773. }
  10774. intel_init_dpio(dev);
  10775. intel_shared_dpll_init(dev);
  10776. /* save the BIOS value before clobbering it */
  10777. dev_priv->bios_vgacntr = I915_READ(i915_vgacntrl_reg(dev));
  10778. /* Just disable it once at startup */
  10779. i915_disable_vga(dev);
  10780. intel_setup_outputs(dev);
  10781. /* Just in case the BIOS is doing something questionable. */
  10782. intel_disable_fbc(dev);
  10783. drm_modeset_lock_all(dev);
  10784. intel_modeset_setup_hw_state(dev, false);
  10785. drm_modeset_unlock_all(dev);
  10786. for_each_intel_crtc(dev, crtc) {
  10787. if (!crtc->active)
  10788. continue;
  10789. /*
  10790. * Note that reserving the BIOS fb up front prevents us
  10791. * from stuffing other stolen allocations like the ring
  10792. * on top. This prevents some ugliness at boot time, and
  10793. * can even allow for smooth boot transitions if the BIOS
  10794. * fb is large enough for the active pipe configuration.
  10795. */
  10796. if (dev_priv->display.get_plane_config) {
  10797. dev_priv->display.get_plane_config(crtc,
  10798. &crtc->plane_config);
  10799. /*
  10800. * If the fb is shared between multiple heads, we'll
  10801. * just get the first one.
  10802. */
  10803. intel_find_plane_obj(crtc, &crtc->plane_config);
  10804. }
  10805. }
  10806. }
  10807. static void intel_enable_pipe_a(struct drm_device *dev)
  10808. {
  10809. struct intel_connector *connector;
  10810. struct drm_connector *crt = NULL;
  10811. struct intel_load_detect_pipe load_detect_temp;
  10812. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  10813. /* We can't just switch on the pipe A, we need to set things up with a
  10814. * proper mode and output configuration. As a gross hack, enable pipe A
  10815. * by enabling the load detect pipe once. */
  10816. list_for_each_entry(connector,
  10817. &dev->mode_config.connector_list,
  10818. base.head) {
  10819. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  10820. crt = &connector->base;
  10821. break;
  10822. }
  10823. }
  10824. if (!crt)
  10825. return;
  10826. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  10827. intel_release_load_detect_pipe(crt, &load_detect_temp);
  10828. }
  10829. static bool
  10830. intel_check_plane_mapping(struct intel_crtc *crtc)
  10831. {
  10832. struct drm_device *dev = crtc->base.dev;
  10833. struct drm_i915_private *dev_priv = dev->dev_private;
  10834. u32 reg, val;
  10835. if (INTEL_INFO(dev)->num_pipes == 1)
  10836. return true;
  10837. reg = DSPCNTR(!crtc->plane);
  10838. val = I915_READ(reg);
  10839. if ((val & DISPLAY_PLANE_ENABLE) &&
  10840. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  10841. return false;
  10842. return true;
  10843. }
  10844. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  10845. {
  10846. struct drm_device *dev = crtc->base.dev;
  10847. struct drm_i915_private *dev_priv = dev->dev_private;
  10848. u32 reg;
  10849. /* Clear any frame start delays used for debugging left by the BIOS */
  10850. reg = PIPECONF(crtc->config.cpu_transcoder);
  10851. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  10852. /* restore vblank interrupts to correct state */
  10853. if (crtc->active) {
  10854. update_scanline_offset(crtc);
  10855. drm_vblank_on(dev, crtc->pipe);
  10856. } else
  10857. drm_vblank_off(dev, crtc->pipe);
  10858. /* We need to sanitize the plane -> pipe mapping first because this will
  10859. * disable the crtc (and hence change the state) if it is wrong. Note
  10860. * that gen4+ has a fixed plane -> pipe mapping. */
  10861. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  10862. struct intel_connector *connector;
  10863. bool plane;
  10864. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  10865. crtc->base.base.id);
  10866. /* Pipe has the wrong plane attached and the plane is active.
  10867. * Temporarily change the plane mapping and disable everything
  10868. * ... */
  10869. plane = crtc->plane;
  10870. crtc->plane = !plane;
  10871. crtc->primary_enabled = true;
  10872. dev_priv->display.crtc_disable(&crtc->base);
  10873. crtc->plane = plane;
  10874. /* ... and break all links. */
  10875. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10876. base.head) {
  10877. if (connector->encoder->base.crtc != &crtc->base)
  10878. continue;
  10879. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10880. connector->base.encoder = NULL;
  10881. }
  10882. /* multiple connectors may have the same encoder:
  10883. * handle them and break crtc link separately */
  10884. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10885. base.head)
  10886. if (connector->encoder->base.crtc == &crtc->base) {
  10887. connector->encoder->base.crtc = NULL;
  10888. connector->encoder->connectors_active = false;
  10889. }
  10890. WARN_ON(crtc->active);
  10891. crtc->base.enabled = false;
  10892. }
  10893. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  10894. crtc->pipe == PIPE_A && !crtc->active) {
  10895. /* BIOS forgot to enable pipe A, this mostly happens after
  10896. * resume. Force-enable the pipe to fix this, the update_dpms
  10897. * call below we restore the pipe to the right state, but leave
  10898. * the required bits on. */
  10899. intel_enable_pipe_a(dev);
  10900. }
  10901. /* Adjust the state of the output pipe according to whether we
  10902. * have active connectors/encoders. */
  10903. intel_crtc_update_dpms(&crtc->base);
  10904. if (crtc->active != crtc->base.enabled) {
  10905. struct intel_encoder *encoder;
  10906. /* This can happen either due to bugs in the get_hw_state
  10907. * functions or because the pipe is force-enabled due to the
  10908. * pipe A quirk. */
  10909. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  10910. crtc->base.base.id,
  10911. crtc->base.enabled ? "enabled" : "disabled",
  10912. crtc->active ? "enabled" : "disabled");
  10913. crtc->base.enabled = crtc->active;
  10914. /* Because we only establish the connector -> encoder ->
  10915. * crtc links if something is active, this means the
  10916. * crtc is now deactivated. Break the links. connector
  10917. * -> encoder links are only establish when things are
  10918. * actually up, hence no need to break them. */
  10919. WARN_ON(crtc->active);
  10920. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  10921. WARN_ON(encoder->connectors_active);
  10922. encoder->base.crtc = NULL;
  10923. }
  10924. }
  10925. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  10926. /*
  10927. * We start out with underrun reporting disabled to avoid races.
  10928. * For correct bookkeeping mark this on active crtcs.
  10929. *
  10930. * Also on gmch platforms we dont have any hardware bits to
  10931. * disable the underrun reporting. Which means we need to start
  10932. * out with underrun reporting disabled also on inactive pipes,
  10933. * since otherwise we'll complain about the garbage we read when
  10934. * e.g. coming up after runtime pm.
  10935. *
  10936. * No protection against concurrent access is required - at
  10937. * worst a fifo underrun happens which also sets this to false.
  10938. */
  10939. crtc->cpu_fifo_underrun_disabled = true;
  10940. crtc->pch_fifo_underrun_disabled = true;
  10941. }
  10942. }
  10943. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  10944. {
  10945. struct intel_connector *connector;
  10946. struct drm_device *dev = encoder->base.dev;
  10947. /* We need to check both for a crtc link (meaning that the
  10948. * encoder is active and trying to read from a pipe) and the
  10949. * pipe itself being active. */
  10950. bool has_active_crtc = encoder->base.crtc &&
  10951. to_intel_crtc(encoder->base.crtc)->active;
  10952. if (encoder->connectors_active && !has_active_crtc) {
  10953. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  10954. encoder->base.base.id,
  10955. encoder->base.name);
  10956. /* Connector is active, but has no active pipe. This is
  10957. * fallout from our resume register restoring. Disable
  10958. * the encoder manually again. */
  10959. if (encoder->base.crtc) {
  10960. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  10961. encoder->base.base.id,
  10962. encoder->base.name);
  10963. encoder->disable(encoder);
  10964. if (encoder->post_disable)
  10965. encoder->post_disable(encoder);
  10966. }
  10967. encoder->base.crtc = NULL;
  10968. encoder->connectors_active = false;
  10969. /* Inconsistent output/port/pipe state happens presumably due to
  10970. * a bug in one of the get_hw_state functions. Or someplace else
  10971. * in our code, like the register restore mess on resume. Clamp
  10972. * things to off as a safer default. */
  10973. list_for_each_entry(connector,
  10974. &dev->mode_config.connector_list,
  10975. base.head) {
  10976. if (connector->encoder != encoder)
  10977. continue;
  10978. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10979. connector->base.encoder = NULL;
  10980. }
  10981. }
  10982. /* Enabled encoders without active connectors will be fixed in
  10983. * the crtc fixup. */
  10984. }
  10985. void i915_redisable_vga_power_on(struct drm_device *dev)
  10986. {
  10987. struct drm_i915_private *dev_priv = dev->dev_private;
  10988. u32 vga_reg = i915_vgacntrl_reg(dev);
  10989. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  10990. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  10991. i915_disable_vga(dev);
  10992. }
  10993. }
  10994. void i915_redisable_vga(struct drm_device *dev)
  10995. {
  10996. struct drm_i915_private *dev_priv = dev->dev_private;
  10997. /* This function can be called both from intel_modeset_setup_hw_state or
  10998. * at a very early point in our resume sequence, where the power well
  10999. * structures are not yet restored. Since this function is at a very
  11000. * paranoid "someone might have enabled VGA while we were not looking"
  11001. * level, just check if the power well is enabled instead of trying to
  11002. * follow the "don't touch the power well if we don't need it" policy
  11003. * the rest of the driver uses. */
  11004. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  11005. return;
  11006. i915_redisable_vga_power_on(dev);
  11007. }
  11008. static bool primary_get_hw_state(struct intel_crtc *crtc)
  11009. {
  11010. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  11011. if (!crtc->active)
  11012. return false;
  11013. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  11014. }
  11015. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  11016. {
  11017. struct drm_i915_private *dev_priv = dev->dev_private;
  11018. enum pipe pipe;
  11019. struct intel_crtc *crtc;
  11020. struct intel_encoder *encoder;
  11021. struct intel_connector *connector;
  11022. int i;
  11023. for_each_intel_crtc(dev, crtc) {
  11024. memset(&crtc->config, 0, sizeof(crtc->config));
  11025. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  11026. crtc->active = dev_priv->display.get_pipe_config(crtc,
  11027. &crtc->config);
  11028. crtc->base.enabled = crtc->active;
  11029. crtc->primary_enabled = primary_get_hw_state(crtc);
  11030. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  11031. crtc->base.base.id,
  11032. crtc->active ? "enabled" : "disabled");
  11033. }
  11034. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11035. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11036. pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
  11037. pll->active = 0;
  11038. for_each_intel_crtc(dev, crtc) {
  11039. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  11040. pll->active++;
  11041. }
  11042. pll->refcount = pll->active;
  11043. DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
  11044. pll->name, pll->refcount, pll->on);
  11045. if (pll->refcount)
  11046. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  11047. }
  11048. for_each_intel_encoder(dev, encoder) {
  11049. pipe = 0;
  11050. if (encoder->get_hw_state(encoder, &pipe)) {
  11051. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11052. encoder->base.crtc = &crtc->base;
  11053. encoder->get_config(encoder, &crtc->config);
  11054. } else {
  11055. encoder->base.crtc = NULL;
  11056. }
  11057. encoder->connectors_active = false;
  11058. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  11059. encoder->base.base.id,
  11060. encoder->base.name,
  11061. encoder->base.crtc ? "enabled" : "disabled",
  11062. pipe_name(pipe));
  11063. }
  11064. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11065. base.head) {
  11066. if (connector->get_hw_state(connector)) {
  11067. connector->base.dpms = DRM_MODE_DPMS_ON;
  11068. connector->encoder->connectors_active = true;
  11069. connector->base.encoder = &connector->encoder->base;
  11070. } else {
  11071. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11072. connector->base.encoder = NULL;
  11073. }
  11074. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  11075. connector->base.base.id,
  11076. connector->base.name,
  11077. connector->base.encoder ? "enabled" : "disabled");
  11078. }
  11079. }
  11080. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  11081. * and i915 state tracking structures. */
  11082. void intel_modeset_setup_hw_state(struct drm_device *dev,
  11083. bool force_restore)
  11084. {
  11085. struct drm_i915_private *dev_priv = dev->dev_private;
  11086. enum pipe pipe;
  11087. struct intel_crtc *crtc;
  11088. struct intel_encoder *encoder;
  11089. int i;
  11090. intel_modeset_readout_hw_state(dev);
  11091. /*
  11092. * Now that we have the config, copy it to each CRTC struct
  11093. * Note that this could go away if we move to using crtc_config
  11094. * checking everywhere.
  11095. */
  11096. for_each_intel_crtc(dev, crtc) {
  11097. if (crtc->active && i915.fastboot) {
  11098. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  11099. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11100. crtc->base.base.id);
  11101. drm_mode_debug_printmodeline(&crtc->base.mode);
  11102. }
  11103. }
  11104. /* HW state is read out, now we need to sanitize this mess. */
  11105. for_each_intel_encoder(dev, encoder) {
  11106. intel_sanitize_encoder(encoder);
  11107. }
  11108. for_each_pipe(dev_priv, pipe) {
  11109. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11110. intel_sanitize_crtc(crtc);
  11111. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  11112. }
  11113. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11114. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11115. if (!pll->on || pll->active)
  11116. continue;
  11117. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11118. pll->disable(dev_priv, pll);
  11119. pll->on = false;
  11120. }
  11121. if (HAS_PCH_SPLIT(dev))
  11122. ilk_wm_get_hw_state(dev);
  11123. if (force_restore) {
  11124. i915_redisable_vga(dev);
  11125. /*
  11126. * We need to use raw interfaces for restoring state to avoid
  11127. * checking (bogus) intermediate states.
  11128. */
  11129. for_each_pipe(dev_priv, pipe) {
  11130. struct drm_crtc *crtc =
  11131. dev_priv->pipe_to_crtc_mapping[pipe];
  11132. __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  11133. crtc->primary->fb);
  11134. }
  11135. } else {
  11136. intel_modeset_update_staged_output_state(dev);
  11137. }
  11138. intel_modeset_check_state(dev);
  11139. }
  11140. void intel_modeset_gem_init(struct drm_device *dev)
  11141. {
  11142. struct drm_crtc *c;
  11143. struct drm_i915_gem_object *obj;
  11144. mutex_lock(&dev->struct_mutex);
  11145. intel_init_gt_powersave(dev);
  11146. mutex_unlock(&dev->struct_mutex);
  11147. intel_modeset_init_hw(dev);
  11148. intel_setup_overlay(dev);
  11149. /*
  11150. * Make sure any fbs we allocated at startup are properly
  11151. * pinned & fenced. When we do the allocation it's too early
  11152. * for this.
  11153. */
  11154. mutex_lock(&dev->struct_mutex);
  11155. for_each_crtc(dev, c) {
  11156. obj = intel_fb_obj(c->primary->fb);
  11157. if (obj == NULL)
  11158. continue;
  11159. if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
  11160. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11161. to_intel_crtc(c)->pipe);
  11162. drm_framebuffer_unreference(c->primary->fb);
  11163. c->primary->fb = NULL;
  11164. }
  11165. }
  11166. mutex_unlock(&dev->struct_mutex);
  11167. }
  11168. void intel_connector_unregister(struct intel_connector *intel_connector)
  11169. {
  11170. struct drm_connector *connector = &intel_connector->base;
  11171. intel_panel_destroy_backlight(connector);
  11172. drm_connector_unregister(connector);
  11173. }
  11174. void intel_modeset_cleanup(struct drm_device *dev)
  11175. {
  11176. struct drm_i915_private *dev_priv = dev->dev_private;
  11177. struct drm_connector *connector;
  11178. /*
  11179. * Interrupts and polling as the first thing to avoid creating havoc.
  11180. * Too much stuff here (turning of rps, connectors, ...) would
  11181. * experience fancy races otherwise.
  11182. */
  11183. intel_irq_uninstall(dev_priv);
  11184. /*
  11185. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11186. * poll handlers. Hence disable polling after hpd handling is shut down.
  11187. */
  11188. drm_kms_helper_poll_fini(dev);
  11189. mutex_lock(&dev->struct_mutex);
  11190. intel_unregister_dsm_handler();
  11191. intel_disable_fbc(dev);
  11192. intel_disable_gt_powersave(dev);
  11193. ironlake_teardown_rc6(dev);
  11194. mutex_unlock(&dev->struct_mutex);
  11195. /* flush any delayed tasks or pending work */
  11196. flush_scheduled_work();
  11197. /* destroy the backlight and sysfs files before encoders/connectors */
  11198. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11199. struct intel_connector *intel_connector;
  11200. intel_connector = to_intel_connector(connector);
  11201. intel_connector->unregister(intel_connector);
  11202. }
  11203. drm_mode_config_cleanup(dev);
  11204. intel_cleanup_overlay(dev);
  11205. mutex_lock(&dev->struct_mutex);
  11206. intel_cleanup_gt_powersave(dev);
  11207. mutex_unlock(&dev->struct_mutex);
  11208. }
  11209. /*
  11210. * Return which encoder is currently attached for connector.
  11211. */
  11212. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11213. {
  11214. return &intel_attached_encoder(connector)->base;
  11215. }
  11216. void intel_connector_attach_encoder(struct intel_connector *connector,
  11217. struct intel_encoder *encoder)
  11218. {
  11219. connector->encoder = encoder;
  11220. drm_mode_connector_attach_encoder(&connector->base,
  11221. &encoder->base);
  11222. }
  11223. /*
  11224. * set vga decode state - true == enable VGA decode
  11225. */
  11226. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11227. {
  11228. struct drm_i915_private *dev_priv = dev->dev_private;
  11229. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11230. u16 gmch_ctrl;
  11231. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11232. DRM_ERROR("failed to read control word\n");
  11233. return -EIO;
  11234. }
  11235. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11236. return 0;
  11237. if (state)
  11238. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11239. else
  11240. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11241. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11242. DRM_ERROR("failed to write control word\n");
  11243. return -EIO;
  11244. }
  11245. return 0;
  11246. }
  11247. struct intel_display_error_state {
  11248. u32 power_well_driver;
  11249. int num_transcoders;
  11250. struct intel_cursor_error_state {
  11251. u32 control;
  11252. u32 position;
  11253. u32 base;
  11254. u32 size;
  11255. } cursor[I915_MAX_PIPES];
  11256. struct intel_pipe_error_state {
  11257. bool power_domain_on;
  11258. u32 source;
  11259. u32 stat;
  11260. } pipe[I915_MAX_PIPES];
  11261. struct intel_plane_error_state {
  11262. u32 control;
  11263. u32 stride;
  11264. u32 size;
  11265. u32 pos;
  11266. u32 addr;
  11267. u32 surface;
  11268. u32 tile_offset;
  11269. } plane[I915_MAX_PIPES];
  11270. struct intel_transcoder_error_state {
  11271. bool power_domain_on;
  11272. enum transcoder cpu_transcoder;
  11273. u32 conf;
  11274. u32 htotal;
  11275. u32 hblank;
  11276. u32 hsync;
  11277. u32 vtotal;
  11278. u32 vblank;
  11279. u32 vsync;
  11280. } transcoder[4];
  11281. };
  11282. struct intel_display_error_state *
  11283. intel_display_capture_error_state(struct drm_device *dev)
  11284. {
  11285. struct drm_i915_private *dev_priv = dev->dev_private;
  11286. struct intel_display_error_state *error;
  11287. int transcoders[] = {
  11288. TRANSCODER_A,
  11289. TRANSCODER_B,
  11290. TRANSCODER_C,
  11291. TRANSCODER_EDP,
  11292. };
  11293. int i;
  11294. if (INTEL_INFO(dev)->num_pipes == 0)
  11295. return NULL;
  11296. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11297. if (error == NULL)
  11298. return NULL;
  11299. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11300. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11301. for_each_pipe(dev_priv, i) {
  11302. error->pipe[i].power_domain_on =
  11303. __intel_display_power_is_enabled(dev_priv,
  11304. POWER_DOMAIN_PIPE(i));
  11305. if (!error->pipe[i].power_domain_on)
  11306. continue;
  11307. error->cursor[i].control = I915_READ(CURCNTR(i));
  11308. error->cursor[i].position = I915_READ(CURPOS(i));
  11309. error->cursor[i].base = I915_READ(CURBASE(i));
  11310. error->plane[i].control = I915_READ(DSPCNTR(i));
  11311. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11312. if (INTEL_INFO(dev)->gen <= 3) {
  11313. error->plane[i].size = I915_READ(DSPSIZE(i));
  11314. error->plane[i].pos = I915_READ(DSPPOS(i));
  11315. }
  11316. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11317. error->plane[i].addr = I915_READ(DSPADDR(i));
  11318. if (INTEL_INFO(dev)->gen >= 4) {
  11319. error->plane[i].surface = I915_READ(DSPSURF(i));
  11320. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11321. }
  11322. error->pipe[i].source = I915_READ(PIPESRC(i));
  11323. if (HAS_GMCH_DISPLAY(dev))
  11324. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11325. }
  11326. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11327. if (HAS_DDI(dev_priv->dev))
  11328. error->num_transcoders++; /* Account for eDP. */
  11329. for (i = 0; i < error->num_transcoders; i++) {
  11330. enum transcoder cpu_transcoder = transcoders[i];
  11331. error->transcoder[i].power_domain_on =
  11332. __intel_display_power_is_enabled(dev_priv,
  11333. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11334. if (!error->transcoder[i].power_domain_on)
  11335. continue;
  11336. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11337. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11338. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11339. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11340. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11341. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11342. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11343. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11344. }
  11345. return error;
  11346. }
  11347. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11348. void
  11349. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11350. struct drm_device *dev,
  11351. struct intel_display_error_state *error)
  11352. {
  11353. struct drm_i915_private *dev_priv = dev->dev_private;
  11354. int i;
  11355. if (!error)
  11356. return;
  11357. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11358. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11359. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11360. error->power_well_driver);
  11361. for_each_pipe(dev_priv, i) {
  11362. err_printf(m, "Pipe [%d]:\n", i);
  11363. err_printf(m, " Power: %s\n",
  11364. error->pipe[i].power_domain_on ? "on" : "off");
  11365. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11366. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11367. err_printf(m, "Plane [%d]:\n", i);
  11368. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11369. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11370. if (INTEL_INFO(dev)->gen <= 3) {
  11371. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11372. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11373. }
  11374. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11375. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11376. if (INTEL_INFO(dev)->gen >= 4) {
  11377. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11378. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11379. }
  11380. err_printf(m, "Cursor [%d]:\n", i);
  11381. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11382. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11383. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11384. }
  11385. for (i = 0; i < error->num_transcoders; i++) {
  11386. err_printf(m, "CPU transcoder: %c\n",
  11387. transcoder_name(error->transcoder[i].cpu_transcoder));
  11388. err_printf(m, " Power: %s\n",
  11389. error->transcoder[i].power_domain_on ? "on" : "off");
  11390. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11391. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11392. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11393. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11394. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11395. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11396. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11397. }
  11398. }
  11399. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11400. {
  11401. struct intel_crtc *crtc;
  11402. for_each_intel_crtc(dev, crtc) {
  11403. struct intel_unpin_work *work;
  11404. spin_lock_irq(&dev->event_lock);
  11405. work = crtc->unpin_work;
  11406. if (work && work->event &&
  11407. work->event->base.file_priv == file) {
  11408. kfree(work->event);
  11409. work->event = NULL;
  11410. }
  11411. spin_unlock_irq(&dev->event_lock);
  11412. }
  11413. }