intel_display.c 374 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_dp_helper.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include <drm/drm_plane_helper.h>
  42. #include <drm/drm_rect.h>
  43. #include <linux/dma_remapping.h>
  44. /* Primary plane formats supported by all gen */
  45. #define COMMON_PRIMARY_FORMATS \
  46. DRM_FORMAT_C8, \
  47. DRM_FORMAT_RGB565, \
  48. DRM_FORMAT_XRGB8888, \
  49. DRM_FORMAT_ARGB8888
  50. /* Primary plane formats for gen <= 3 */
  51. static const uint32_t intel_primary_formats_gen2[] = {
  52. COMMON_PRIMARY_FORMATS,
  53. DRM_FORMAT_XRGB1555,
  54. DRM_FORMAT_ARGB1555,
  55. };
  56. /* Primary plane formats for gen >= 4 */
  57. static const uint32_t intel_primary_formats_gen4[] = {
  58. COMMON_PRIMARY_FORMATS, \
  59. DRM_FORMAT_XBGR8888,
  60. DRM_FORMAT_ABGR8888,
  61. DRM_FORMAT_XRGB2101010,
  62. DRM_FORMAT_ARGB2101010,
  63. DRM_FORMAT_XBGR2101010,
  64. DRM_FORMAT_ABGR2101010,
  65. };
  66. /* Cursor formats */
  67. static const uint32_t intel_cursor_formats[] = {
  68. DRM_FORMAT_ARGB8888,
  69. };
  70. #define DIV_ROUND_CLOSEST_ULL(ll, d) \
  71. ({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
  72. static void intel_increase_pllclock(struct drm_device *dev,
  73. enum pipe pipe);
  74. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  75. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  76. struct intel_crtc_config *pipe_config);
  77. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  78. struct intel_crtc_config *pipe_config);
  79. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  80. int x, int y, struct drm_framebuffer *old_fb);
  81. static int intel_framebuffer_init(struct drm_device *dev,
  82. struct intel_framebuffer *ifb,
  83. struct drm_mode_fb_cmd2 *mode_cmd,
  84. struct drm_i915_gem_object *obj);
  85. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  86. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  87. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  88. struct intel_link_m_n *m_n,
  89. struct intel_link_m_n *m2_n2);
  90. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  91. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  92. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  93. static void vlv_prepare_pll(struct intel_crtc *crtc);
  94. static void chv_prepare_pll(struct intel_crtc *crtc);
  95. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  96. {
  97. if (!connector->mst_port)
  98. return connector->encoder;
  99. else
  100. return &connector->mst_port->mst_encoders[pipe]->base;
  101. }
  102. typedef struct {
  103. int min, max;
  104. } intel_range_t;
  105. typedef struct {
  106. int dot_limit;
  107. int p2_slow, p2_fast;
  108. } intel_p2_t;
  109. typedef struct intel_limit intel_limit_t;
  110. struct intel_limit {
  111. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  112. intel_p2_t p2;
  113. };
  114. int
  115. intel_pch_rawclk(struct drm_device *dev)
  116. {
  117. struct drm_i915_private *dev_priv = dev->dev_private;
  118. WARN_ON(!HAS_PCH_SPLIT(dev));
  119. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  120. }
  121. static inline u32 /* units of 100MHz */
  122. intel_fdi_link_freq(struct drm_device *dev)
  123. {
  124. if (IS_GEN5(dev)) {
  125. struct drm_i915_private *dev_priv = dev->dev_private;
  126. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  127. } else
  128. return 27;
  129. }
  130. static const intel_limit_t intel_limits_i8xx_dac = {
  131. .dot = { .min = 25000, .max = 350000 },
  132. .vco = { .min = 908000, .max = 1512000 },
  133. .n = { .min = 2, .max = 16 },
  134. .m = { .min = 96, .max = 140 },
  135. .m1 = { .min = 18, .max = 26 },
  136. .m2 = { .min = 6, .max = 16 },
  137. .p = { .min = 4, .max = 128 },
  138. .p1 = { .min = 2, .max = 33 },
  139. .p2 = { .dot_limit = 165000,
  140. .p2_slow = 4, .p2_fast = 2 },
  141. };
  142. static const intel_limit_t intel_limits_i8xx_dvo = {
  143. .dot = { .min = 25000, .max = 350000 },
  144. .vco = { .min = 908000, .max = 1512000 },
  145. .n = { .min = 2, .max = 16 },
  146. .m = { .min = 96, .max = 140 },
  147. .m1 = { .min = 18, .max = 26 },
  148. .m2 = { .min = 6, .max = 16 },
  149. .p = { .min = 4, .max = 128 },
  150. .p1 = { .min = 2, .max = 33 },
  151. .p2 = { .dot_limit = 165000,
  152. .p2_slow = 4, .p2_fast = 4 },
  153. };
  154. static const intel_limit_t intel_limits_i8xx_lvds = {
  155. .dot = { .min = 25000, .max = 350000 },
  156. .vco = { .min = 908000, .max = 1512000 },
  157. .n = { .min = 2, .max = 16 },
  158. .m = { .min = 96, .max = 140 },
  159. .m1 = { .min = 18, .max = 26 },
  160. .m2 = { .min = 6, .max = 16 },
  161. .p = { .min = 4, .max = 128 },
  162. .p1 = { .min = 1, .max = 6 },
  163. .p2 = { .dot_limit = 165000,
  164. .p2_slow = 14, .p2_fast = 7 },
  165. };
  166. static const intel_limit_t intel_limits_i9xx_sdvo = {
  167. .dot = { .min = 20000, .max = 400000 },
  168. .vco = { .min = 1400000, .max = 2800000 },
  169. .n = { .min = 1, .max = 6 },
  170. .m = { .min = 70, .max = 120 },
  171. .m1 = { .min = 8, .max = 18 },
  172. .m2 = { .min = 3, .max = 7 },
  173. .p = { .min = 5, .max = 80 },
  174. .p1 = { .min = 1, .max = 8 },
  175. .p2 = { .dot_limit = 200000,
  176. .p2_slow = 10, .p2_fast = 5 },
  177. };
  178. static const intel_limit_t intel_limits_i9xx_lvds = {
  179. .dot = { .min = 20000, .max = 400000 },
  180. .vco = { .min = 1400000, .max = 2800000 },
  181. .n = { .min = 1, .max = 6 },
  182. .m = { .min = 70, .max = 120 },
  183. .m1 = { .min = 8, .max = 18 },
  184. .m2 = { .min = 3, .max = 7 },
  185. .p = { .min = 7, .max = 98 },
  186. .p1 = { .min = 1, .max = 8 },
  187. .p2 = { .dot_limit = 112000,
  188. .p2_slow = 14, .p2_fast = 7 },
  189. };
  190. static const intel_limit_t intel_limits_g4x_sdvo = {
  191. .dot = { .min = 25000, .max = 270000 },
  192. .vco = { .min = 1750000, .max = 3500000},
  193. .n = { .min = 1, .max = 4 },
  194. .m = { .min = 104, .max = 138 },
  195. .m1 = { .min = 17, .max = 23 },
  196. .m2 = { .min = 5, .max = 11 },
  197. .p = { .min = 10, .max = 30 },
  198. .p1 = { .min = 1, .max = 3},
  199. .p2 = { .dot_limit = 270000,
  200. .p2_slow = 10,
  201. .p2_fast = 10
  202. },
  203. };
  204. static const intel_limit_t intel_limits_g4x_hdmi = {
  205. .dot = { .min = 22000, .max = 400000 },
  206. .vco = { .min = 1750000, .max = 3500000},
  207. .n = { .min = 1, .max = 4 },
  208. .m = { .min = 104, .max = 138 },
  209. .m1 = { .min = 16, .max = 23 },
  210. .m2 = { .min = 5, .max = 11 },
  211. .p = { .min = 5, .max = 80 },
  212. .p1 = { .min = 1, .max = 8},
  213. .p2 = { .dot_limit = 165000,
  214. .p2_slow = 10, .p2_fast = 5 },
  215. };
  216. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  217. .dot = { .min = 20000, .max = 115000 },
  218. .vco = { .min = 1750000, .max = 3500000 },
  219. .n = { .min = 1, .max = 3 },
  220. .m = { .min = 104, .max = 138 },
  221. .m1 = { .min = 17, .max = 23 },
  222. .m2 = { .min = 5, .max = 11 },
  223. .p = { .min = 28, .max = 112 },
  224. .p1 = { .min = 2, .max = 8 },
  225. .p2 = { .dot_limit = 0,
  226. .p2_slow = 14, .p2_fast = 14
  227. },
  228. };
  229. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  230. .dot = { .min = 80000, .max = 224000 },
  231. .vco = { .min = 1750000, .max = 3500000 },
  232. .n = { .min = 1, .max = 3 },
  233. .m = { .min = 104, .max = 138 },
  234. .m1 = { .min = 17, .max = 23 },
  235. .m2 = { .min = 5, .max = 11 },
  236. .p = { .min = 14, .max = 42 },
  237. .p1 = { .min = 2, .max = 6 },
  238. .p2 = { .dot_limit = 0,
  239. .p2_slow = 7, .p2_fast = 7
  240. },
  241. };
  242. static const intel_limit_t intel_limits_pineview_sdvo = {
  243. .dot = { .min = 20000, .max = 400000},
  244. .vco = { .min = 1700000, .max = 3500000 },
  245. /* Pineview's Ncounter is a ring counter */
  246. .n = { .min = 3, .max = 6 },
  247. .m = { .min = 2, .max = 256 },
  248. /* Pineview only has one combined m divider, which we treat as m2. */
  249. .m1 = { .min = 0, .max = 0 },
  250. .m2 = { .min = 0, .max = 254 },
  251. .p = { .min = 5, .max = 80 },
  252. .p1 = { .min = 1, .max = 8 },
  253. .p2 = { .dot_limit = 200000,
  254. .p2_slow = 10, .p2_fast = 5 },
  255. };
  256. static const intel_limit_t intel_limits_pineview_lvds = {
  257. .dot = { .min = 20000, .max = 400000 },
  258. .vco = { .min = 1700000, .max = 3500000 },
  259. .n = { .min = 3, .max = 6 },
  260. .m = { .min = 2, .max = 256 },
  261. .m1 = { .min = 0, .max = 0 },
  262. .m2 = { .min = 0, .max = 254 },
  263. .p = { .min = 7, .max = 112 },
  264. .p1 = { .min = 1, .max = 8 },
  265. .p2 = { .dot_limit = 112000,
  266. .p2_slow = 14, .p2_fast = 14 },
  267. };
  268. /* Ironlake / Sandybridge
  269. *
  270. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  271. * the range value for them is (actual_value - 2).
  272. */
  273. static const intel_limit_t intel_limits_ironlake_dac = {
  274. .dot = { .min = 25000, .max = 350000 },
  275. .vco = { .min = 1760000, .max = 3510000 },
  276. .n = { .min = 1, .max = 5 },
  277. .m = { .min = 79, .max = 127 },
  278. .m1 = { .min = 12, .max = 22 },
  279. .m2 = { .min = 5, .max = 9 },
  280. .p = { .min = 5, .max = 80 },
  281. .p1 = { .min = 1, .max = 8 },
  282. .p2 = { .dot_limit = 225000,
  283. .p2_slow = 10, .p2_fast = 5 },
  284. };
  285. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  286. .dot = { .min = 25000, .max = 350000 },
  287. .vco = { .min = 1760000, .max = 3510000 },
  288. .n = { .min = 1, .max = 3 },
  289. .m = { .min = 79, .max = 118 },
  290. .m1 = { .min = 12, .max = 22 },
  291. .m2 = { .min = 5, .max = 9 },
  292. .p = { .min = 28, .max = 112 },
  293. .p1 = { .min = 2, .max = 8 },
  294. .p2 = { .dot_limit = 225000,
  295. .p2_slow = 14, .p2_fast = 14 },
  296. };
  297. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  298. .dot = { .min = 25000, .max = 350000 },
  299. .vco = { .min = 1760000, .max = 3510000 },
  300. .n = { .min = 1, .max = 3 },
  301. .m = { .min = 79, .max = 127 },
  302. .m1 = { .min = 12, .max = 22 },
  303. .m2 = { .min = 5, .max = 9 },
  304. .p = { .min = 14, .max = 56 },
  305. .p1 = { .min = 2, .max = 8 },
  306. .p2 = { .dot_limit = 225000,
  307. .p2_slow = 7, .p2_fast = 7 },
  308. };
  309. /* LVDS 100mhz refclk limits. */
  310. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  311. .dot = { .min = 25000, .max = 350000 },
  312. .vco = { .min = 1760000, .max = 3510000 },
  313. .n = { .min = 1, .max = 2 },
  314. .m = { .min = 79, .max = 126 },
  315. .m1 = { .min = 12, .max = 22 },
  316. .m2 = { .min = 5, .max = 9 },
  317. .p = { .min = 28, .max = 112 },
  318. .p1 = { .min = 2, .max = 8 },
  319. .p2 = { .dot_limit = 225000,
  320. .p2_slow = 14, .p2_fast = 14 },
  321. };
  322. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  323. .dot = { .min = 25000, .max = 350000 },
  324. .vco = { .min = 1760000, .max = 3510000 },
  325. .n = { .min = 1, .max = 3 },
  326. .m = { .min = 79, .max = 126 },
  327. .m1 = { .min = 12, .max = 22 },
  328. .m2 = { .min = 5, .max = 9 },
  329. .p = { .min = 14, .max = 42 },
  330. .p1 = { .min = 2, .max = 6 },
  331. .p2 = { .dot_limit = 225000,
  332. .p2_slow = 7, .p2_fast = 7 },
  333. };
  334. static const intel_limit_t intel_limits_vlv = {
  335. /*
  336. * These are the data rate limits (measured in fast clocks)
  337. * since those are the strictest limits we have. The fast
  338. * clock and actual rate limits are more relaxed, so checking
  339. * them would make no difference.
  340. */
  341. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  342. .vco = { .min = 4000000, .max = 6000000 },
  343. .n = { .min = 1, .max = 7 },
  344. .m1 = { .min = 2, .max = 3 },
  345. .m2 = { .min = 11, .max = 156 },
  346. .p1 = { .min = 2, .max = 3 },
  347. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  348. };
  349. static const intel_limit_t intel_limits_chv = {
  350. /*
  351. * These are the data rate limits (measured in fast clocks)
  352. * since those are the strictest limits we have. The fast
  353. * clock and actual rate limits are more relaxed, so checking
  354. * them would make no difference.
  355. */
  356. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  357. .vco = { .min = 4860000, .max = 6700000 },
  358. .n = { .min = 1, .max = 1 },
  359. .m1 = { .min = 2, .max = 2 },
  360. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  361. .p1 = { .min = 2, .max = 4 },
  362. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  363. };
  364. static void vlv_clock(int refclk, intel_clock_t *clock)
  365. {
  366. clock->m = clock->m1 * clock->m2;
  367. clock->p = clock->p1 * clock->p2;
  368. if (WARN_ON(clock->n == 0 || clock->p == 0))
  369. return;
  370. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  371. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  372. }
  373. /**
  374. * Returns whether any output on the specified pipe is of the specified type
  375. */
  376. static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  377. {
  378. struct drm_device *dev = crtc->dev;
  379. struct intel_encoder *encoder;
  380. for_each_encoder_on_crtc(dev, crtc, encoder)
  381. if (encoder->type == type)
  382. return true;
  383. return false;
  384. }
  385. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  386. int refclk)
  387. {
  388. struct drm_device *dev = crtc->dev;
  389. const intel_limit_t *limit;
  390. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  391. if (intel_is_dual_link_lvds(dev)) {
  392. if (refclk == 100000)
  393. limit = &intel_limits_ironlake_dual_lvds_100m;
  394. else
  395. limit = &intel_limits_ironlake_dual_lvds;
  396. } else {
  397. if (refclk == 100000)
  398. limit = &intel_limits_ironlake_single_lvds_100m;
  399. else
  400. limit = &intel_limits_ironlake_single_lvds;
  401. }
  402. } else
  403. limit = &intel_limits_ironlake_dac;
  404. return limit;
  405. }
  406. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  407. {
  408. struct drm_device *dev = crtc->dev;
  409. const intel_limit_t *limit;
  410. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  411. if (intel_is_dual_link_lvds(dev))
  412. limit = &intel_limits_g4x_dual_channel_lvds;
  413. else
  414. limit = &intel_limits_g4x_single_channel_lvds;
  415. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  416. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  417. limit = &intel_limits_g4x_hdmi;
  418. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  419. limit = &intel_limits_g4x_sdvo;
  420. } else /* The option is for other outputs */
  421. limit = &intel_limits_i9xx_sdvo;
  422. return limit;
  423. }
  424. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  425. {
  426. struct drm_device *dev = crtc->dev;
  427. const intel_limit_t *limit;
  428. if (HAS_PCH_SPLIT(dev))
  429. limit = intel_ironlake_limit(crtc, refclk);
  430. else if (IS_G4X(dev)) {
  431. limit = intel_g4x_limit(crtc);
  432. } else if (IS_PINEVIEW(dev)) {
  433. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  434. limit = &intel_limits_pineview_lvds;
  435. else
  436. limit = &intel_limits_pineview_sdvo;
  437. } else if (IS_CHERRYVIEW(dev)) {
  438. limit = &intel_limits_chv;
  439. } else if (IS_VALLEYVIEW(dev)) {
  440. limit = &intel_limits_vlv;
  441. } else if (!IS_GEN2(dev)) {
  442. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  443. limit = &intel_limits_i9xx_lvds;
  444. else
  445. limit = &intel_limits_i9xx_sdvo;
  446. } else {
  447. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  448. limit = &intel_limits_i8xx_lvds;
  449. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
  450. limit = &intel_limits_i8xx_dvo;
  451. else
  452. limit = &intel_limits_i8xx_dac;
  453. }
  454. return limit;
  455. }
  456. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  457. static void pineview_clock(int refclk, intel_clock_t *clock)
  458. {
  459. clock->m = clock->m2 + 2;
  460. clock->p = clock->p1 * clock->p2;
  461. if (WARN_ON(clock->n == 0 || clock->p == 0))
  462. return;
  463. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  464. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  465. }
  466. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  467. {
  468. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  469. }
  470. static void i9xx_clock(int refclk, intel_clock_t *clock)
  471. {
  472. clock->m = i9xx_dpll_compute_m(clock);
  473. clock->p = clock->p1 * clock->p2;
  474. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  475. return;
  476. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  477. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  478. }
  479. static void chv_clock(int refclk, intel_clock_t *clock)
  480. {
  481. clock->m = clock->m1 * clock->m2;
  482. clock->p = clock->p1 * clock->p2;
  483. if (WARN_ON(clock->n == 0 || clock->p == 0))
  484. return;
  485. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  486. clock->n << 22);
  487. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  488. }
  489. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  490. /**
  491. * Returns whether the given set of divisors are valid for a given refclk with
  492. * the given connectors.
  493. */
  494. static bool intel_PLL_is_valid(struct drm_device *dev,
  495. const intel_limit_t *limit,
  496. const intel_clock_t *clock)
  497. {
  498. if (clock->n < limit->n.min || limit->n.max < clock->n)
  499. INTELPllInvalid("n out of range\n");
  500. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  501. INTELPllInvalid("p1 out of range\n");
  502. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  503. INTELPllInvalid("m2 out of range\n");
  504. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  505. INTELPllInvalid("m1 out of range\n");
  506. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  507. if (clock->m1 <= clock->m2)
  508. INTELPllInvalid("m1 <= m2\n");
  509. if (!IS_VALLEYVIEW(dev)) {
  510. if (clock->p < limit->p.min || limit->p.max < clock->p)
  511. INTELPllInvalid("p out of range\n");
  512. if (clock->m < limit->m.min || limit->m.max < clock->m)
  513. INTELPllInvalid("m out of range\n");
  514. }
  515. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  516. INTELPllInvalid("vco out of range\n");
  517. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  518. * connector, etc., rather than just a single range.
  519. */
  520. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  521. INTELPllInvalid("dot out of range\n");
  522. return true;
  523. }
  524. static bool
  525. i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  526. int target, int refclk, intel_clock_t *match_clock,
  527. intel_clock_t *best_clock)
  528. {
  529. struct drm_device *dev = crtc->dev;
  530. intel_clock_t clock;
  531. int err = target;
  532. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  533. /*
  534. * For LVDS just rely on its current settings for dual-channel.
  535. * We haven't figured out how to reliably set up different
  536. * single/dual channel state, if we even can.
  537. */
  538. if (intel_is_dual_link_lvds(dev))
  539. clock.p2 = limit->p2.p2_fast;
  540. else
  541. clock.p2 = limit->p2.p2_slow;
  542. } else {
  543. if (target < limit->p2.dot_limit)
  544. clock.p2 = limit->p2.p2_slow;
  545. else
  546. clock.p2 = limit->p2.p2_fast;
  547. }
  548. memset(best_clock, 0, sizeof(*best_clock));
  549. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  550. clock.m1++) {
  551. for (clock.m2 = limit->m2.min;
  552. clock.m2 <= limit->m2.max; clock.m2++) {
  553. if (clock.m2 >= clock.m1)
  554. break;
  555. for (clock.n = limit->n.min;
  556. clock.n <= limit->n.max; clock.n++) {
  557. for (clock.p1 = limit->p1.min;
  558. clock.p1 <= limit->p1.max; clock.p1++) {
  559. int this_err;
  560. i9xx_clock(refclk, &clock);
  561. if (!intel_PLL_is_valid(dev, limit,
  562. &clock))
  563. continue;
  564. if (match_clock &&
  565. clock.p != match_clock->p)
  566. continue;
  567. this_err = abs(clock.dot - target);
  568. if (this_err < err) {
  569. *best_clock = clock;
  570. err = this_err;
  571. }
  572. }
  573. }
  574. }
  575. }
  576. return (err != target);
  577. }
  578. static bool
  579. pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  580. int target, int refclk, intel_clock_t *match_clock,
  581. intel_clock_t *best_clock)
  582. {
  583. struct drm_device *dev = crtc->dev;
  584. intel_clock_t clock;
  585. int err = target;
  586. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  587. /*
  588. * For LVDS just rely on its current settings for dual-channel.
  589. * We haven't figured out how to reliably set up different
  590. * single/dual channel state, if we even can.
  591. */
  592. if (intel_is_dual_link_lvds(dev))
  593. clock.p2 = limit->p2.p2_fast;
  594. else
  595. clock.p2 = limit->p2.p2_slow;
  596. } else {
  597. if (target < limit->p2.dot_limit)
  598. clock.p2 = limit->p2.p2_slow;
  599. else
  600. clock.p2 = limit->p2.p2_fast;
  601. }
  602. memset(best_clock, 0, sizeof(*best_clock));
  603. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  604. clock.m1++) {
  605. for (clock.m2 = limit->m2.min;
  606. clock.m2 <= limit->m2.max; clock.m2++) {
  607. for (clock.n = limit->n.min;
  608. clock.n <= limit->n.max; clock.n++) {
  609. for (clock.p1 = limit->p1.min;
  610. clock.p1 <= limit->p1.max; clock.p1++) {
  611. int this_err;
  612. pineview_clock(refclk, &clock);
  613. if (!intel_PLL_is_valid(dev, limit,
  614. &clock))
  615. continue;
  616. if (match_clock &&
  617. clock.p != match_clock->p)
  618. continue;
  619. this_err = abs(clock.dot - target);
  620. if (this_err < err) {
  621. *best_clock = clock;
  622. err = this_err;
  623. }
  624. }
  625. }
  626. }
  627. }
  628. return (err != target);
  629. }
  630. static bool
  631. g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  632. int target, int refclk, intel_clock_t *match_clock,
  633. intel_clock_t *best_clock)
  634. {
  635. struct drm_device *dev = crtc->dev;
  636. intel_clock_t clock;
  637. int max_n;
  638. bool found;
  639. /* approximately equals target * 0.00585 */
  640. int err_most = (target >> 8) + (target >> 9);
  641. found = false;
  642. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  643. if (intel_is_dual_link_lvds(dev))
  644. clock.p2 = limit->p2.p2_fast;
  645. else
  646. clock.p2 = limit->p2.p2_slow;
  647. } else {
  648. if (target < limit->p2.dot_limit)
  649. clock.p2 = limit->p2.p2_slow;
  650. else
  651. clock.p2 = limit->p2.p2_fast;
  652. }
  653. memset(best_clock, 0, sizeof(*best_clock));
  654. max_n = limit->n.max;
  655. /* based on hardware requirement, prefer smaller n to precision */
  656. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  657. /* based on hardware requirement, prefere larger m1,m2 */
  658. for (clock.m1 = limit->m1.max;
  659. clock.m1 >= limit->m1.min; clock.m1--) {
  660. for (clock.m2 = limit->m2.max;
  661. clock.m2 >= limit->m2.min; clock.m2--) {
  662. for (clock.p1 = limit->p1.max;
  663. clock.p1 >= limit->p1.min; clock.p1--) {
  664. int this_err;
  665. i9xx_clock(refclk, &clock);
  666. if (!intel_PLL_is_valid(dev, limit,
  667. &clock))
  668. continue;
  669. this_err = abs(clock.dot - target);
  670. if (this_err < err_most) {
  671. *best_clock = clock;
  672. err_most = this_err;
  673. max_n = clock.n;
  674. found = true;
  675. }
  676. }
  677. }
  678. }
  679. }
  680. return found;
  681. }
  682. static bool
  683. vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  684. int target, int refclk, intel_clock_t *match_clock,
  685. intel_clock_t *best_clock)
  686. {
  687. struct drm_device *dev = crtc->dev;
  688. intel_clock_t clock;
  689. unsigned int bestppm = 1000000;
  690. /* min update 19.2 MHz */
  691. int max_n = min(limit->n.max, refclk / 19200);
  692. bool found = false;
  693. target *= 5; /* fast clock */
  694. memset(best_clock, 0, sizeof(*best_clock));
  695. /* based on hardware requirement, prefer smaller n to precision */
  696. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  697. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  698. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  699. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  700. clock.p = clock.p1 * clock.p2;
  701. /* based on hardware requirement, prefer bigger m1,m2 values */
  702. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  703. unsigned int ppm, diff;
  704. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  705. refclk * clock.m1);
  706. vlv_clock(refclk, &clock);
  707. if (!intel_PLL_is_valid(dev, limit,
  708. &clock))
  709. continue;
  710. diff = abs(clock.dot - target);
  711. ppm = div_u64(1000000ULL * diff, target);
  712. if (ppm < 100 && clock.p > best_clock->p) {
  713. bestppm = 0;
  714. *best_clock = clock;
  715. found = true;
  716. }
  717. if (bestppm >= 10 && ppm < bestppm - 10) {
  718. bestppm = ppm;
  719. *best_clock = clock;
  720. found = true;
  721. }
  722. }
  723. }
  724. }
  725. }
  726. return found;
  727. }
  728. static bool
  729. chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  730. int target, int refclk, intel_clock_t *match_clock,
  731. intel_clock_t *best_clock)
  732. {
  733. struct drm_device *dev = crtc->dev;
  734. intel_clock_t clock;
  735. uint64_t m2;
  736. int found = false;
  737. memset(best_clock, 0, sizeof(*best_clock));
  738. /*
  739. * Based on hardware doc, the n always set to 1, and m1 always
  740. * set to 2. If requires to support 200Mhz refclk, we need to
  741. * revisit this because n may not 1 anymore.
  742. */
  743. clock.n = 1, clock.m1 = 2;
  744. target *= 5; /* fast clock */
  745. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  746. for (clock.p2 = limit->p2.p2_fast;
  747. clock.p2 >= limit->p2.p2_slow;
  748. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  749. clock.p = clock.p1 * clock.p2;
  750. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  751. clock.n) << 22, refclk * clock.m1);
  752. if (m2 > INT_MAX/clock.m1)
  753. continue;
  754. clock.m2 = m2;
  755. chv_clock(refclk, &clock);
  756. if (!intel_PLL_is_valid(dev, limit, &clock))
  757. continue;
  758. /* based on hardware requirement, prefer bigger p
  759. */
  760. if (clock.p > best_clock->p) {
  761. *best_clock = clock;
  762. found = true;
  763. }
  764. }
  765. }
  766. return found;
  767. }
  768. bool intel_crtc_active(struct drm_crtc *crtc)
  769. {
  770. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  771. /* Be paranoid as we can arrive here with only partial
  772. * state retrieved from the hardware during setup.
  773. *
  774. * We can ditch the adjusted_mode.crtc_clock check as soon
  775. * as Haswell has gained clock readout/fastboot support.
  776. *
  777. * We can ditch the crtc->primary->fb check as soon as we can
  778. * properly reconstruct framebuffers.
  779. */
  780. return intel_crtc->active && crtc->primary->fb &&
  781. intel_crtc->config.adjusted_mode.crtc_clock;
  782. }
  783. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  784. enum pipe pipe)
  785. {
  786. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  787. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  788. return intel_crtc->config.cpu_transcoder;
  789. }
  790. static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
  791. {
  792. struct drm_i915_private *dev_priv = dev->dev_private;
  793. u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
  794. frame = I915_READ(frame_reg);
  795. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  796. WARN(1, "vblank wait on pipe %c timed out\n",
  797. pipe_name(pipe));
  798. }
  799. /**
  800. * intel_wait_for_vblank - wait for vblank on a given pipe
  801. * @dev: drm device
  802. * @pipe: pipe to wait for
  803. *
  804. * Wait for vblank to occur on a given pipe. Needed for various bits of
  805. * mode setting code.
  806. */
  807. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  808. {
  809. struct drm_i915_private *dev_priv = dev->dev_private;
  810. int pipestat_reg = PIPESTAT(pipe);
  811. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  812. g4x_wait_for_vblank(dev, pipe);
  813. return;
  814. }
  815. /* Clear existing vblank status. Note this will clear any other
  816. * sticky status fields as well.
  817. *
  818. * This races with i915_driver_irq_handler() with the result
  819. * that either function could miss a vblank event. Here it is not
  820. * fatal, as we will either wait upon the next vblank interrupt or
  821. * timeout. Generally speaking intel_wait_for_vblank() is only
  822. * called during modeset at which time the GPU should be idle and
  823. * should *not* be performing page flips and thus not waiting on
  824. * vblanks...
  825. * Currently, the result of us stealing a vblank from the irq
  826. * handler is that a single frame will be skipped during swapbuffers.
  827. */
  828. I915_WRITE(pipestat_reg,
  829. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  830. /* Wait for vblank interrupt bit to set */
  831. if (wait_for(I915_READ(pipestat_reg) &
  832. PIPE_VBLANK_INTERRUPT_STATUS,
  833. 50))
  834. DRM_DEBUG_KMS("vblank wait on pipe %c timed out\n",
  835. pipe_name(pipe));
  836. }
  837. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  838. {
  839. struct drm_i915_private *dev_priv = dev->dev_private;
  840. u32 reg = PIPEDSL(pipe);
  841. u32 line1, line2;
  842. u32 line_mask;
  843. if (IS_GEN2(dev))
  844. line_mask = DSL_LINEMASK_GEN2;
  845. else
  846. line_mask = DSL_LINEMASK_GEN3;
  847. line1 = I915_READ(reg) & line_mask;
  848. mdelay(5);
  849. line2 = I915_READ(reg) & line_mask;
  850. return line1 == line2;
  851. }
  852. /*
  853. * intel_wait_for_pipe_off - wait for pipe to turn off
  854. * @crtc: crtc whose pipe to wait for
  855. *
  856. * After disabling a pipe, we can't wait for vblank in the usual way,
  857. * spinning on the vblank interrupt status bit, since we won't actually
  858. * see an interrupt when the pipe is disabled.
  859. *
  860. * On Gen4 and above:
  861. * wait for the pipe register state bit to turn off
  862. *
  863. * Otherwise:
  864. * wait for the display line value to settle (it usually
  865. * ends up stopping at the start of the next frame).
  866. *
  867. */
  868. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  869. {
  870. struct drm_device *dev = crtc->base.dev;
  871. struct drm_i915_private *dev_priv = dev->dev_private;
  872. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  873. enum pipe pipe = crtc->pipe;
  874. if (INTEL_INFO(dev)->gen >= 4) {
  875. int reg = PIPECONF(cpu_transcoder);
  876. /* Wait for the Pipe State to go off */
  877. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  878. 100))
  879. WARN(1, "pipe_off wait timed out\n");
  880. } else {
  881. /* Wait for the display line to settle */
  882. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  883. WARN(1, "pipe_off wait timed out\n");
  884. }
  885. }
  886. /*
  887. * ibx_digital_port_connected - is the specified port connected?
  888. * @dev_priv: i915 private structure
  889. * @port: the port to test
  890. *
  891. * Returns true if @port is connected, false otherwise.
  892. */
  893. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  894. struct intel_digital_port *port)
  895. {
  896. u32 bit;
  897. if (HAS_PCH_IBX(dev_priv->dev)) {
  898. switch (port->port) {
  899. case PORT_B:
  900. bit = SDE_PORTB_HOTPLUG;
  901. break;
  902. case PORT_C:
  903. bit = SDE_PORTC_HOTPLUG;
  904. break;
  905. case PORT_D:
  906. bit = SDE_PORTD_HOTPLUG;
  907. break;
  908. default:
  909. return true;
  910. }
  911. } else {
  912. switch (port->port) {
  913. case PORT_B:
  914. bit = SDE_PORTB_HOTPLUG_CPT;
  915. break;
  916. case PORT_C:
  917. bit = SDE_PORTC_HOTPLUG_CPT;
  918. break;
  919. case PORT_D:
  920. bit = SDE_PORTD_HOTPLUG_CPT;
  921. break;
  922. default:
  923. return true;
  924. }
  925. }
  926. return I915_READ(SDEISR) & bit;
  927. }
  928. static const char *state_string(bool enabled)
  929. {
  930. return enabled ? "on" : "off";
  931. }
  932. /* Only for pre-ILK configs */
  933. void assert_pll(struct drm_i915_private *dev_priv,
  934. enum pipe pipe, bool state)
  935. {
  936. int reg;
  937. u32 val;
  938. bool cur_state;
  939. reg = DPLL(pipe);
  940. val = I915_READ(reg);
  941. cur_state = !!(val & DPLL_VCO_ENABLE);
  942. WARN(cur_state != state,
  943. "PLL state assertion failure (expected %s, current %s)\n",
  944. state_string(state), state_string(cur_state));
  945. }
  946. /* XXX: the dsi pll is shared between MIPI DSI ports */
  947. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  948. {
  949. u32 val;
  950. bool cur_state;
  951. mutex_lock(&dev_priv->dpio_lock);
  952. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  953. mutex_unlock(&dev_priv->dpio_lock);
  954. cur_state = val & DSI_PLL_VCO_EN;
  955. WARN(cur_state != state,
  956. "DSI PLL state assertion failure (expected %s, current %s)\n",
  957. state_string(state), state_string(cur_state));
  958. }
  959. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  960. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  961. struct intel_shared_dpll *
  962. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  963. {
  964. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  965. if (crtc->config.shared_dpll < 0)
  966. return NULL;
  967. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  968. }
  969. /* For ILK+ */
  970. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  971. struct intel_shared_dpll *pll,
  972. bool state)
  973. {
  974. bool cur_state;
  975. struct intel_dpll_hw_state hw_state;
  976. if (WARN (!pll,
  977. "asserting DPLL %s with no DPLL\n", state_string(state)))
  978. return;
  979. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  980. WARN(cur_state != state,
  981. "%s assertion failure (expected %s, current %s)\n",
  982. pll->name, state_string(state), state_string(cur_state));
  983. }
  984. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  985. enum pipe pipe, bool state)
  986. {
  987. int reg;
  988. u32 val;
  989. bool cur_state;
  990. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  991. pipe);
  992. if (HAS_DDI(dev_priv->dev)) {
  993. /* DDI does not have a specific FDI_TX register */
  994. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  995. val = I915_READ(reg);
  996. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  997. } else {
  998. reg = FDI_TX_CTL(pipe);
  999. val = I915_READ(reg);
  1000. cur_state = !!(val & FDI_TX_ENABLE);
  1001. }
  1002. WARN(cur_state != state,
  1003. "FDI TX state assertion failure (expected %s, current %s)\n",
  1004. state_string(state), state_string(cur_state));
  1005. }
  1006. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1007. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1008. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1009. enum pipe pipe, bool state)
  1010. {
  1011. int reg;
  1012. u32 val;
  1013. bool cur_state;
  1014. reg = FDI_RX_CTL(pipe);
  1015. val = I915_READ(reg);
  1016. cur_state = !!(val & FDI_RX_ENABLE);
  1017. WARN(cur_state != state,
  1018. "FDI RX state assertion failure (expected %s, current %s)\n",
  1019. state_string(state), state_string(cur_state));
  1020. }
  1021. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1022. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1023. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1024. enum pipe pipe)
  1025. {
  1026. int reg;
  1027. u32 val;
  1028. /* ILK FDI PLL is always enabled */
  1029. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1030. return;
  1031. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1032. if (HAS_DDI(dev_priv->dev))
  1033. return;
  1034. reg = FDI_TX_CTL(pipe);
  1035. val = I915_READ(reg);
  1036. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1037. }
  1038. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1039. enum pipe pipe, bool state)
  1040. {
  1041. int reg;
  1042. u32 val;
  1043. bool cur_state;
  1044. reg = FDI_RX_CTL(pipe);
  1045. val = I915_READ(reg);
  1046. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1047. WARN(cur_state != state,
  1048. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1049. state_string(state), state_string(cur_state));
  1050. }
  1051. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1052. enum pipe pipe)
  1053. {
  1054. struct drm_device *dev = dev_priv->dev;
  1055. int pp_reg;
  1056. u32 val;
  1057. enum pipe panel_pipe = PIPE_A;
  1058. bool locked = true;
  1059. if (WARN_ON(HAS_DDI(dev)))
  1060. return;
  1061. if (HAS_PCH_SPLIT(dev)) {
  1062. u32 port_sel;
  1063. pp_reg = PCH_PP_CONTROL;
  1064. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1065. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1066. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1067. panel_pipe = PIPE_B;
  1068. /* XXX: else fix for eDP */
  1069. } else if (IS_VALLEYVIEW(dev)) {
  1070. /* presumably write lock depends on pipe, not port select */
  1071. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1072. panel_pipe = pipe;
  1073. } else {
  1074. pp_reg = PP_CONTROL;
  1075. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1076. panel_pipe = PIPE_B;
  1077. }
  1078. val = I915_READ(pp_reg);
  1079. if (!(val & PANEL_POWER_ON) ||
  1080. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1081. locked = false;
  1082. WARN(panel_pipe == pipe && locked,
  1083. "panel assertion failure, pipe %c regs locked\n",
  1084. pipe_name(pipe));
  1085. }
  1086. static void assert_cursor(struct drm_i915_private *dev_priv,
  1087. enum pipe pipe, bool state)
  1088. {
  1089. struct drm_device *dev = dev_priv->dev;
  1090. bool cur_state;
  1091. if (IS_845G(dev) || IS_I865G(dev))
  1092. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1093. else
  1094. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1095. WARN(cur_state != state,
  1096. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1097. pipe_name(pipe), state_string(state), state_string(cur_state));
  1098. }
  1099. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1100. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1101. void assert_pipe(struct drm_i915_private *dev_priv,
  1102. enum pipe pipe, bool state)
  1103. {
  1104. int reg;
  1105. u32 val;
  1106. bool cur_state;
  1107. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1108. pipe);
  1109. /* if we need the pipe quirk it must be always on */
  1110. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1111. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1112. state = true;
  1113. if (!intel_display_power_enabled(dev_priv,
  1114. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1115. cur_state = false;
  1116. } else {
  1117. reg = PIPECONF(cpu_transcoder);
  1118. val = I915_READ(reg);
  1119. cur_state = !!(val & PIPECONF_ENABLE);
  1120. }
  1121. WARN(cur_state != state,
  1122. "pipe %c assertion failure (expected %s, current %s)\n",
  1123. pipe_name(pipe), state_string(state), state_string(cur_state));
  1124. }
  1125. static void assert_plane(struct drm_i915_private *dev_priv,
  1126. enum plane plane, bool state)
  1127. {
  1128. int reg;
  1129. u32 val;
  1130. bool cur_state;
  1131. reg = DSPCNTR(plane);
  1132. val = I915_READ(reg);
  1133. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1134. WARN(cur_state != state,
  1135. "plane %c assertion failure (expected %s, current %s)\n",
  1136. plane_name(plane), state_string(state), state_string(cur_state));
  1137. }
  1138. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1139. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1140. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1141. enum pipe pipe)
  1142. {
  1143. struct drm_device *dev = dev_priv->dev;
  1144. int reg, i;
  1145. u32 val;
  1146. int cur_pipe;
  1147. /* Primary planes are fixed to pipes on gen4+ */
  1148. if (INTEL_INFO(dev)->gen >= 4) {
  1149. reg = DSPCNTR(pipe);
  1150. val = I915_READ(reg);
  1151. WARN(val & DISPLAY_PLANE_ENABLE,
  1152. "plane %c assertion failure, should be disabled but not\n",
  1153. plane_name(pipe));
  1154. return;
  1155. }
  1156. /* Need to check both planes against the pipe */
  1157. for_each_pipe(dev_priv, i) {
  1158. reg = DSPCNTR(i);
  1159. val = I915_READ(reg);
  1160. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1161. DISPPLANE_SEL_PIPE_SHIFT;
  1162. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1163. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1164. plane_name(i), pipe_name(pipe));
  1165. }
  1166. }
  1167. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1168. enum pipe pipe)
  1169. {
  1170. struct drm_device *dev = dev_priv->dev;
  1171. int reg, sprite;
  1172. u32 val;
  1173. if (IS_VALLEYVIEW(dev)) {
  1174. for_each_sprite(pipe, sprite) {
  1175. reg = SPCNTR(pipe, sprite);
  1176. val = I915_READ(reg);
  1177. WARN(val & SP_ENABLE,
  1178. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1179. sprite_name(pipe, sprite), pipe_name(pipe));
  1180. }
  1181. } else if (INTEL_INFO(dev)->gen >= 7) {
  1182. reg = SPRCTL(pipe);
  1183. val = I915_READ(reg);
  1184. WARN(val & SPRITE_ENABLE,
  1185. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1186. plane_name(pipe), pipe_name(pipe));
  1187. } else if (INTEL_INFO(dev)->gen >= 5) {
  1188. reg = DVSCNTR(pipe);
  1189. val = I915_READ(reg);
  1190. WARN(val & DVS_ENABLE,
  1191. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1192. plane_name(pipe), pipe_name(pipe));
  1193. }
  1194. }
  1195. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1196. {
  1197. u32 val;
  1198. bool enabled;
  1199. WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1200. val = I915_READ(PCH_DREF_CONTROL);
  1201. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1202. DREF_SUPERSPREAD_SOURCE_MASK));
  1203. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1204. }
  1205. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1206. enum pipe pipe)
  1207. {
  1208. int reg;
  1209. u32 val;
  1210. bool enabled;
  1211. reg = PCH_TRANSCONF(pipe);
  1212. val = I915_READ(reg);
  1213. enabled = !!(val & TRANS_ENABLE);
  1214. WARN(enabled,
  1215. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1216. pipe_name(pipe));
  1217. }
  1218. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1219. enum pipe pipe, u32 port_sel, u32 val)
  1220. {
  1221. if ((val & DP_PORT_EN) == 0)
  1222. return false;
  1223. if (HAS_PCH_CPT(dev_priv->dev)) {
  1224. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1225. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1226. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1227. return false;
  1228. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1229. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1230. return false;
  1231. } else {
  1232. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1233. return false;
  1234. }
  1235. return true;
  1236. }
  1237. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1238. enum pipe pipe, u32 val)
  1239. {
  1240. if ((val & SDVO_ENABLE) == 0)
  1241. return false;
  1242. if (HAS_PCH_CPT(dev_priv->dev)) {
  1243. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1244. return false;
  1245. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1246. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1247. return false;
  1248. } else {
  1249. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1250. return false;
  1251. }
  1252. return true;
  1253. }
  1254. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1255. enum pipe pipe, u32 val)
  1256. {
  1257. if ((val & LVDS_PORT_EN) == 0)
  1258. return false;
  1259. if (HAS_PCH_CPT(dev_priv->dev)) {
  1260. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1261. return false;
  1262. } else {
  1263. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1264. return false;
  1265. }
  1266. return true;
  1267. }
  1268. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1269. enum pipe pipe, u32 val)
  1270. {
  1271. if ((val & ADPA_DAC_ENABLE) == 0)
  1272. return false;
  1273. if (HAS_PCH_CPT(dev_priv->dev)) {
  1274. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1275. return false;
  1276. } else {
  1277. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1278. return false;
  1279. }
  1280. return true;
  1281. }
  1282. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1283. enum pipe pipe, int reg, u32 port_sel)
  1284. {
  1285. u32 val = I915_READ(reg);
  1286. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1287. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1288. reg, pipe_name(pipe));
  1289. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1290. && (val & DP_PIPEB_SELECT),
  1291. "IBX PCH dp port still using transcoder B\n");
  1292. }
  1293. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1294. enum pipe pipe, int reg)
  1295. {
  1296. u32 val = I915_READ(reg);
  1297. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1298. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1299. reg, pipe_name(pipe));
  1300. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1301. && (val & SDVO_PIPE_B_SELECT),
  1302. "IBX PCH hdmi port still using transcoder B\n");
  1303. }
  1304. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1305. enum pipe pipe)
  1306. {
  1307. int reg;
  1308. u32 val;
  1309. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1310. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1311. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1312. reg = PCH_ADPA;
  1313. val = I915_READ(reg);
  1314. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1315. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1316. pipe_name(pipe));
  1317. reg = PCH_LVDS;
  1318. val = I915_READ(reg);
  1319. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1320. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1321. pipe_name(pipe));
  1322. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1323. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1324. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1325. }
  1326. static void intel_init_dpio(struct drm_device *dev)
  1327. {
  1328. struct drm_i915_private *dev_priv = dev->dev_private;
  1329. if (!IS_VALLEYVIEW(dev))
  1330. return;
  1331. /*
  1332. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1333. * CHV x1 PHY (DP/HDMI D)
  1334. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1335. */
  1336. if (IS_CHERRYVIEW(dev)) {
  1337. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1338. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1339. } else {
  1340. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1341. }
  1342. }
  1343. static void vlv_enable_pll(struct intel_crtc *crtc)
  1344. {
  1345. struct drm_device *dev = crtc->base.dev;
  1346. struct drm_i915_private *dev_priv = dev->dev_private;
  1347. int reg = DPLL(crtc->pipe);
  1348. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1349. assert_pipe_disabled(dev_priv, crtc->pipe);
  1350. /* No really, not for ILK+ */
  1351. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1352. /* PLL is protected by panel, make sure we can write it */
  1353. if (IS_MOBILE(dev_priv->dev))
  1354. assert_panel_unlocked(dev_priv, crtc->pipe);
  1355. I915_WRITE(reg, dpll);
  1356. POSTING_READ(reg);
  1357. udelay(150);
  1358. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1359. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1360. I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
  1361. POSTING_READ(DPLL_MD(crtc->pipe));
  1362. /* We do this three times for luck */
  1363. I915_WRITE(reg, dpll);
  1364. POSTING_READ(reg);
  1365. udelay(150); /* wait for warmup */
  1366. I915_WRITE(reg, dpll);
  1367. POSTING_READ(reg);
  1368. udelay(150); /* wait for warmup */
  1369. I915_WRITE(reg, dpll);
  1370. POSTING_READ(reg);
  1371. udelay(150); /* wait for warmup */
  1372. }
  1373. static void chv_enable_pll(struct intel_crtc *crtc)
  1374. {
  1375. struct drm_device *dev = crtc->base.dev;
  1376. struct drm_i915_private *dev_priv = dev->dev_private;
  1377. int pipe = crtc->pipe;
  1378. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1379. u32 tmp;
  1380. assert_pipe_disabled(dev_priv, crtc->pipe);
  1381. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1382. mutex_lock(&dev_priv->dpio_lock);
  1383. /* Enable back the 10bit clock to display controller */
  1384. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1385. tmp |= DPIO_DCLKP_EN;
  1386. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1387. /*
  1388. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1389. */
  1390. udelay(1);
  1391. /* Enable PLL */
  1392. I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
  1393. /* Check PLL is locked */
  1394. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1395. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1396. /* not sure when this should be written */
  1397. I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
  1398. POSTING_READ(DPLL_MD(pipe));
  1399. mutex_unlock(&dev_priv->dpio_lock);
  1400. }
  1401. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1402. {
  1403. struct drm_device *dev = crtc->base.dev;
  1404. struct drm_i915_private *dev_priv = dev->dev_private;
  1405. int reg = DPLL(crtc->pipe);
  1406. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1407. assert_pipe_disabled(dev_priv, crtc->pipe);
  1408. /* No really, not for ILK+ */
  1409. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1410. /* PLL is protected by panel, make sure we can write it */
  1411. if (IS_MOBILE(dev) && !IS_I830(dev))
  1412. assert_panel_unlocked(dev_priv, crtc->pipe);
  1413. I915_WRITE(reg, dpll);
  1414. /* Wait for the clocks to stabilize. */
  1415. POSTING_READ(reg);
  1416. udelay(150);
  1417. if (INTEL_INFO(dev)->gen >= 4) {
  1418. I915_WRITE(DPLL_MD(crtc->pipe),
  1419. crtc->config.dpll_hw_state.dpll_md);
  1420. } else {
  1421. /* The pixel multiplier can only be updated once the
  1422. * DPLL is enabled and the clocks are stable.
  1423. *
  1424. * So write it again.
  1425. */
  1426. I915_WRITE(reg, dpll);
  1427. }
  1428. /* We do this three times for luck */
  1429. I915_WRITE(reg, dpll);
  1430. POSTING_READ(reg);
  1431. udelay(150); /* wait for warmup */
  1432. I915_WRITE(reg, dpll);
  1433. POSTING_READ(reg);
  1434. udelay(150); /* wait for warmup */
  1435. I915_WRITE(reg, dpll);
  1436. POSTING_READ(reg);
  1437. udelay(150); /* wait for warmup */
  1438. }
  1439. /**
  1440. * i9xx_disable_pll - disable a PLL
  1441. * @dev_priv: i915 private structure
  1442. * @pipe: pipe PLL to disable
  1443. *
  1444. * Disable the PLL for @pipe, making sure the pipe is off first.
  1445. *
  1446. * Note! This is for pre-ILK only.
  1447. */
  1448. static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1449. {
  1450. /* Don't disable pipe or pipe PLLs if needed */
  1451. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1452. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1453. return;
  1454. /* Make sure the pipe isn't still relying on us */
  1455. assert_pipe_disabled(dev_priv, pipe);
  1456. I915_WRITE(DPLL(pipe), 0);
  1457. POSTING_READ(DPLL(pipe));
  1458. }
  1459. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1460. {
  1461. u32 val = 0;
  1462. /* Make sure the pipe isn't still relying on us */
  1463. assert_pipe_disabled(dev_priv, pipe);
  1464. /*
  1465. * Leave integrated clock source and reference clock enabled for pipe B.
  1466. * The latter is needed for VGA hotplug / manual detection.
  1467. */
  1468. if (pipe == PIPE_B)
  1469. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1470. I915_WRITE(DPLL(pipe), val);
  1471. POSTING_READ(DPLL(pipe));
  1472. }
  1473. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1474. {
  1475. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1476. u32 val;
  1477. /* Make sure the pipe isn't still relying on us */
  1478. assert_pipe_disabled(dev_priv, pipe);
  1479. /* Set PLL en = 0 */
  1480. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1481. if (pipe != PIPE_A)
  1482. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1483. I915_WRITE(DPLL(pipe), val);
  1484. POSTING_READ(DPLL(pipe));
  1485. mutex_lock(&dev_priv->dpio_lock);
  1486. /* Disable 10bit clock to display controller */
  1487. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1488. val &= ~DPIO_DCLKP_EN;
  1489. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1490. /* disable left/right clock distribution */
  1491. if (pipe != PIPE_B) {
  1492. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1493. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1494. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1495. } else {
  1496. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1497. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1498. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1499. }
  1500. mutex_unlock(&dev_priv->dpio_lock);
  1501. }
  1502. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1503. struct intel_digital_port *dport)
  1504. {
  1505. u32 port_mask;
  1506. int dpll_reg;
  1507. switch (dport->port) {
  1508. case PORT_B:
  1509. port_mask = DPLL_PORTB_READY_MASK;
  1510. dpll_reg = DPLL(0);
  1511. break;
  1512. case PORT_C:
  1513. port_mask = DPLL_PORTC_READY_MASK;
  1514. dpll_reg = DPLL(0);
  1515. break;
  1516. case PORT_D:
  1517. port_mask = DPLL_PORTD_READY_MASK;
  1518. dpll_reg = DPIO_PHY_STATUS;
  1519. break;
  1520. default:
  1521. BUG();
  1522. }
  1523. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1524. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1525. port_name(dport->port), I915_READ(dpll_reg));
  1526. }
  1527. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1528. {
  1529. struct drm_device *dev = crtc->base.dev;
  1530. struct drm_i915_private *dev_priv = dev->dev_private;
  1531. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1532. if (WARN_ON(pll == NULL))
  1533. return;
  1534. WARN_ON(!pll->refcount);
  1535. if (pll->active == 0) {
  1536. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1537. WARN_ON(pll->on);
  1538. assert_shared_dpll_disabled(dev_priv, pll);
  1539. pll->mode_set(dev_priv, pll);
  1540. }
  1541. }
  1542. /**
  1543. * intel_enable_shared_dpll - enable PCH PLL
  1544. * @dev_priv: i915 private structure
  1545. * @pipe: pipe PLL to enable
  1546. *
  1547. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1548. * drives the transcoder clock.
  1549. */
  1550. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1551. {
  1552. struct drm_device *dev = crtc->base.dev;
  1553. struct drm_i915_private *dev_priv = dev->dev_private;
  1554. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1555. if (WARN_ON(pll == NULL))
  1556. return;
  1557. if (WARN_ON(pll->refcount == 0))
  1558. return;
  1559. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1560. pll->name, pll->active, pll->on,
  1561. crtc->base.base.id);
  1562. if (pll->active++) {
  1563. WARN_ON(!pll->on);
  1564. assert_shared_dpll_enabled(dev_priv, pll);
  1565. return;
  1566. }
  1567. WARN_ON(pll->on);
  1568. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1569. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1570. pll->enable(dev_priv, pll);
  1571. pll->on = true;
  1572. }
  1573. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1574. {
  1575. struct drm_device *dev = crtc->base.dev;
  1576. struct drm_i915_private *dev_priv = dev->dev_private;
  1577. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1578. /* PCH only available on ILK+ */
  1579. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1580. if (WARN_ON(pll == NULL))
  1581. return;
  1582. if (WARN_ON(pll->refcount == 0))
  1583. return;
  1584. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1585. pll->name, pll->active, pll->on,
  1586. crtc->base.base.id);
  1587. if (WARN_ON(pll->active == 0)) {
  1588. assert_shared_dpll_disabled(dev_priv, pll);
  1589. return;
  1590. }
  1591. assert_shared_dpll_enabled(dev_priv, pll);
  1592. WARN_ON(!pll->on);
  1593. if (--pll->active)
  1594. return;
  1595. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1596. pll->disable(dev_priv, pll);
  1597. pll->on = false;
  1598. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1599. }
  1600. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1601. enum pipe pipe)
  1602. {
  1603. struct drm_device *dev = dev_priv->dev;
  1604. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1605. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1606. uint32_t reg, val, pipeconf_val;
  1607. /* PCH only available on ILK+ */
  1608. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1609. /* Make sure PCH DPLL is enabled */
  1610. assert_shared_dpll_enabled(dev_priv,
  1611. intel_crtc_to_shared_dpll(intel_crtc));
  1612. /* FDI must be feeding us bits for PCH ports */
  1613. assert_fdi_tx_enabled(dev_priv, pipe);
  1614. assert_fdi_rx_enabled(dev_priv, pipe);
  1615. if (HAS_PCH_CPT(dev)) {
  1616. /* Workaround: Set the timing override bit before enabling the
  1617. * pch transcoder. */
  1618. reg = TRANS_CHICKEN2(pipe);
  1619. val = I915_READ(reg);
  1620. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1621. I915_WRITE(reg, val);
  1622. }
  1623. reg = PCH_TRANSCONF(pipe);
  1624. val = I915_READ(reg);
  1625. pipeconf_val = I915_READ(PIPECONF(pipe));
  1626. if (HAS_PCH_IBX(dev_priv->dev)) {
  1627. /*
  1628. * make the BPC in transcoder be consistent with
  1629. * that in pipeconf reg.
  1630. */
  1631. val &= ~PIPECONF_BPC_MASK;
  1632. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1633. }
  1634. val &= ~TRANS_INTERLACE_MASK;
  1635. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1636. if (HAS_PCH_IBX(dev_priv->dev) &&
  1637. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1638. val |= TRANS_LEGACY_INTERLACED_ILK;
  1639. else
  1640. val |= TRANS_INTERLACED;
  1641. else
  1642. val |= TRANS_PROGRESSIVE;
  1643. I915_WRITE(reg, val | TRANS_ENABLE);
  1644. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1645. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1646. }
  1647. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1648. enum transcoder cpu_transcoder)
  1649. {
  1650. u32 val, pipeconf_val;
  1651. /* PCH only available on ILK+ */
  1652. BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
  1653. /* FDI must be feeding us bits for PCH ports */
  1654. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1655. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1656. /* Workaround: set timing override bit. */
  1657. val = I915_READ(_TRANSA_CHICKEN2);
  1658. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1659. I915_WRITE(_TRANSA_CHICKEN2, val);
  1660. val = TRANS_ENABLE;
  1661. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1662. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1663. PIPECONF_INTERLACED_ILK)
  1664. val |= TRANS_INTERLACED;
  1665. else
  1666. val |= TRANS_PROGRESSIVE;
  1667. I915_WRITE(LPT_TRANSCONF, val);
  1668. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1669. DRM_ERROR("Failed to enable PCH transcoder\n");
  1670. }
  1671. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1672. enum pipe pipe)
  1673. {
  1674. struct drm_device *dev = dev_priv->dev;
  1675. uint32_t reg, val;
  1676. /* FDI relies on the transcoder */
  1677. assert_fdi_tx_disabled(dev_priv, pipe);
  1678. assert_fdi_rx_disabled(dev_priv, pipe);
  1679. /* Ports must be off as well */
  1680. assert_pch_ports_disabled(dev_priv, pipe);
  1681. reg = PCH_TRANSCONF(pipe);
  1682. val = I915_READ(reg);
  1683. val &= ~TRANS_ENABLE;
  1684. I915_WRITE(reg, val);
  1685. /* wait for PCH transcoder off, transcoder state */
  1686. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1687. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1688. if (!HAS_PCH_IBX(dev)) {
  1689. /* Workaround: Clear the timing override chicken bit again. */
  1690. reg = TRANS_CHICKEN2(pipe);
  1691. val = I915_READ(reg);
  1692. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1693. I915_WRITE(reg, val);
  1694. }
  1695. }
  1696. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1697. {
  1698. u32 val;
  1699. val = I915_READ(LPT_TRANSCONF);
  1700. val &= ~TRANS_ENABLE;
  1701. I915_WRITE(LPT_TRANSCONF, val);
  1702. /* wait for PCH transcoder off, transcoder state */
  1703. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1704. DRM_ERROR("Failed to disable PCH transcoder\n");
  1705. /* Workaround: clear timing override bit. */
  1706. val = I915_READ(_TRANSA_CHICKEN2);
  1707. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1708. I915_WRITE(_TRANSA_CHICKEN2, val);
  1709. }
  1710. /**
  1711. * intel_enable_pipe - enable a pipe, asserting requirements
  1712. * @crtc: crtc responsible for the pipe
  1713. *
  1714. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1715. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1716. */
  1717. static void intel_enable_pipe(struct intel_crtc *crtc)
  1718. {
  1719. struct drm_device *dev = crtc->base.dev;
  1720. struct drm_i915_private *dev_priv = dev->dev_private;
  1721. enum pipe pipe = crtc->pipe;
  1722. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1723. pipe);
  1724. enum pipe pch_transcoder;
  1725. int reg;
  1726. u32 val;
  1727. assert_planes_disabled(dev_priv, pipe);
  1728. assert_cursor_disabled(dev_priv, pipe);
  1729. assert_sprites_disabled(dev_priv, pipe);
  1730. if (HAS_PCH_LPT(dev_priv->dev))
  1731. pch_transcoder = TRANSCODER_A;
  1732. else
  1733. pch_transcoder = pipe;
  1734. /*
  1735. * A pipe without a PLL won't actually be able to drive bits from
  1736. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1737. * need the check.
  1738. */
  1739. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1740. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
  1741. assert_dsi_pll_enabled(dev_priv);
  1742. else
  1743. assert_pll_enabled(dev_priv, pipe);
  1744. else {
  1745. if (crtc->config.has_pch_encoder) {
  1746. /* if driving the PCH, we need FDI enabled */
  1747. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1748. assert_fdi_tx_pll_enabled(dev_priv,
  1749. (enum pipe) cpu_transcoder);
  1750. }
  1751. /* FIXME: assert CPU port conditions for SNB+ */
  1752. }
  1753. reg = PIPECONF(cpu_transcoder);
  1754. val = I915_READ(reg);
  1755. if (val & PIPECONF_ENABLE) {
  1756. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1757. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1758. return;
  1759. }
  1760. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1761. POSTING_READ(reg);
  1762. }
  1763. /**
  1764. * intel_disable_pipe - disable a pipe, asserting requirements
  1765. * @crtc: crtc whose pipes is to be disabled
  1766. *
  1767. * Disable the pipe of @crtc, making sure that various hardware
  1768. * specific requirements are met, if applicable, e.g. plane
  1769. * disabled, panel fitter off, etc.
  1770. *
  1771. * Will wait until the pipe has shut down before returning.
  1772. */
  1773. static void intel_disable_pipe(struct intel_crtc *crtc)
  1774. {
  1775. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1776. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  1777. enum pipe pipe = crtc->pipe;
  1778. int reg;
  1779. u32 val;
  1780. /*
  1781. * Make sure planes won't keep trying to pump pixels to us,
  1782. * or we might hang the display.
  1783. */
  1784. assert_planes_disabled(dev_priv, pipe);
  1785. assert_cursor_disabled(dev_priv, pipe);
  1786. assert_sprites_disabled(dev_priv, pipe);
  1787. reg = PIPECONF(cpu_transcoder);
  1788. val = I915_READ(reg);
  1789. if ((val & PIPECONF_ENABLE) == 0)
  1790. return;
  1791. /*
  1792. * Double wide has implications for planes
  1793. * so best keep it disabled when not needed.
  1794. */
  1795. if (crtc->config.double_wide)
  1796. val &= ~PIPECONF_DOUBLE_WIDE;
  1797. /* Don't disable pipe or pipe PLLs if needed */
  1798. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1799. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1800. val &= ~PIPECONF_ENABLE;
  1801. I915_WRITE(reg, val);
  1802. if ((val & PIPECONF_ENABLE) == 0)
  1803. intel_wait_for_pipe_off(crtc);
  1804. }
  1805. /*
  1806. * Plane regs are double buffered, going from enabled->disabled needs a
  1807. * trigger in order to latch. The display address reg provides this.
  1808. */
  1809. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1810. enum plane plane)
  1811. {
  1812. struct drm_device *dev = dev_priv->dev;
  1813. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1814. I915_WRITE(reg, I915_READ(reg));
  1815. POSTING_READ(reg);
  1816. }
  1817. /**
  1818. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1819. * @plane: plane to be enabled
  1820. * @crtc: crtc for the plane
  1821. *
  1822. * Enable @plane on @crtc, making sure that the pipe is running first.
  1823. */
  1824. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1825. struct drm_crtc *crtc)
  1826. {
  1827. struct drm_device *dev = plane->dev;
  1828. struct drm_i915_private *dev_priv = dev->dev_private;
  1829. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1830. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1831. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1832. if (intel_crtc->primary_enabled)
  1833. return;
  1834. intel_crtc->primary_enabled = true;
  1835. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1836. crtc->x, crtc->y);
  1837. /*
  1838. * BDW signals flip done immediately if the plane
  1839. * is disabled, even if the plane enable is already
  1840. * armed to occur at the next vblank :(
  1841. */
  1842. if (IS_BROADWELL(dev))
  1843. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1844. }
  1845. /**
  1846. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1847. * @plane: plane to be disabled
  1848. * @crtc: crtc for the plane
  1849. *
  1850. * Disable @plane on @crtc, making sure that the pipe is running first.
  1851. */
  1852. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1853. struct drm_crtc *crtc)
  1854. {
  1855. struct drm_device *dev = plane->dev;
  1856. struct drm_i915_private *dev_priv = dev->dev_private;
  1857. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1858. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1859. if (!intel_crtc->primary_enabled)
  1860. return;
  1861. intel_crtc->primary_enabled = false;
  1862. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1863. crtc->x, crtc->y);
  1864. }
  1865. static bool need_vtd_wa(struct drm_device *dev)
  1866. {
  1867. #ifdef CONFIG_INTEL_IOMMU
  1868. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1869. return true;
  1870. #endif
  1871. return false;
  1872. }
  1873. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1874. {
  1875. int tile_height;
  1876. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1877. return ALIGN(height, tile_height);
  1878. }
  1879. int
  1880. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1881. struct drm_i915_gem_object *obj,
  1882. struct intel_engine_cs *pipelined)
  1883. {
  1884. struct drm_i915_private *dev_priv = dev->dev_private;
  1885. u32 alignment;
  1886. int ret;
  1887. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1888. switch (obj->tiling_mode) {
  1889. case I915_TILING_NONE:
  1890. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1891. alignment = 128 * 1024;
  1892. else if (INTEL_INFO(dev)->gen >= 4)
  1893. alignment = 4 * 1024;
  1894. else
  1895. alignment = 64 * 1024;
  1896. break;
  1897. case I915_TILING_X:
  1898. /* pin() will align the object as required by fence */
  1899. alignment = 0;
  1900. break;
  1901. case I915_TILING_Y:
  1902. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1903. return -EINVAL;
  1904. default:
  1905. BUG();
  1906. }
  1907. /* Note that the w/a also requires 64 PTE of padding following the
  1908. * bo. We currently fill all unused PTE with the shadow page and so
  1909. * we should always have valid PTE following the scanout preventing
  1910. * the VT-d warning.
  1911. */
  1912. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1913. alignment = 256 * 1024;
  1914. dev_priv->mm.interruptible = false;
  1915. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1916. if (ret)
  1917. goto err_interruptible;
  1918. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1919. * fence, whereas 965+ only requires a fence if using
  1920. * framebuffer compression. For simplicity, we always install
  1921. * a fence as the cost is not that onerous.
  1922. */
  1923. ret = i915_gem_object_get_fence(obj);
  1924. if (ret)
  1925. goto err_unpin;
  1926. i915_gem_object_pin_fence(obj);
  1927. dev_priv->mm.interruptible = true;
  1928. return 0;
  1929. err_unpin:
  1930. i915_gem_object_unpin_from_display_plane(obj);
  1931. err_interruptible:
  1932. dev_priv->mm.interruptible = true;
  1933. return ret;
  1934. }
  1935. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1936. {
  1937. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1938. i915_gem_object_unpin_fence(obj);
  1939. i915_gem_object_unpin_from_display_plane(obj);
  1940. }
  1941. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1942. * is assumed to be a power-of-two. */
  1943. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1944. unsigned int tiling_mode,
  1945. unsigned int cpp,
  1946. unsigned int pitch)
  1947. {
  1948. if (tiling_mode != I915_TILING_NONE) {
  1949. unsigned int tile_rows, tiles;
  1950. tile_rows = *y / 8;
  1951. *y %= 8;
  1952. tiles = *x / (512/cpp);
  1953. *x %= 512/cpp;
  1954. return tile_rows * pitch * 8 + tiles * 4096;
  1955. } else {
  1956. unsigned int offset;
  1957. offset = *y * pitch + *x * cpp;
  1958. *y = 0;
  1959. *x = (offset & 4095) / cpp;
  1960. return offset & -4096;
  1961. }
  1962. }
  1963. int intel_format_to_fourcc(int format)
  1964. {
  1965. switch (format) {
  1966. case DISPPLANE_8BPP:
  1967. return DRM_FORMAT_C8;
  1968. case DISPPLANE_BGRX555:
  1969. return DRM_FORMAT_XRGB1555;
  1970. case DISPPLANE_BGRX565:
  1971. return DRM_FORMAT_RGB565;
  1972. default:
  1973. case DISPPLANE_BGRX888:
  1974. return DRM_FORMAT_XRGB8888;
  1975. case DISPPLANE_RGBX888:
  1976. return DRM_FORMAT_XBGR8888;
  1977. case DISPPLANE_BGRX101010:
  1978. return DRM_FORMAT_XRGB2101010;
  1979. case DISPPLANE_RGBX101010:
  1980. return DRM_FORMAT_XBGR2101010;
  1981. }
  1982. }
  1983. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  1984. struct intel_plane_config *plane_config)
  1985. {
  1986. struct drm_device *dev = crtc->base.dev;
  1987. struct drm_i915_gem_object *obj = NULL;
  1988. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  1989. u32 base = plane_config->base;
  1990. if (plane_config->size == 0)
  1991. return false;
  1992. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  1993. plane_config->size);
  1994. if (!obj)
  1995. return false;
  1996. if (plane_config->tiled) {
  1997. obj->tiling_mode = I915_TILING_X;
  1998. obj->stride = crtc->base.primary->fb->pitches[0];
  1999. }
  2000. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  2001. mode_cmd.width = crtc->base.primary->fb->width;
  2002. mode_cmd.height = crtc->base.primary->fb->height;
  2003. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  2004. mutex_lock(&dev->struct_mutex);
  2005. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  2006. &mode_cmd, obj)) {
  2007. DRM_DEBUG_KMS("intel fb init failed\n");
  2008. goto out_unref_obj;
  2009. }
  2010. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2011. mutex_unlock(&dev->struct_mutex);
  2012. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2013. return true;
  2014. out_unref_obj:
  2015. drm_gem_object_unreference(&obj->base);
  2016. mutex_unlock(&dev->struct_mutex);
  2017. return false;
  2018. }
  2019. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2020. struct intel_plane_config *plane_config)
  2021. {
  2022. struct drm_device *dev = intel_crtc->base.dev;
  2023. struct drm_crtc *c;
  2024. struct intel_crtc *i;
  2025. struct drm_i915_gem_object *obj;
  2026. if (!intel_crtc->base.primary->fb)
  2027. return;
  2028. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2029. return;
  2030. kfree(intel_crtc->base.primary->fb);
  2031. intel_crtc->base.primary->fb = NULL;
  2032. /*
  2033. * Failed to alloc the obj, check to see if we should share
  2034. * an fb with another CRTC instead
  2035. */
  2036. for_each_crtc(dev, c) {
  2037. i = to_intel_crtc(c);
  2038. if (c == &intel_crtc->base)
  2039. continue;
  2040. if (!i->active)
  2041. continue;
  2042. obj = intel_fb_obj(c->primary->fb);
  2043. if (obj == NULL)
  2044. continue;
  2045. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2046. drm_framebuffer_reference(c->primary->fb);
  2047. intel_crtc->base.primary->fb = c->primary->fb;
  2048. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2049. break;
  2050. }
  2051. }
  2052. }
  2053. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2054. struct drm_framebuffer *fb,
  2055. int x, int y)
  2056. {
  2057. struct drm_device *dev = crtc->dev;
  2058. struct drm_i915_private *dev_priv = dev->dev_private;
  2059. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2060. struct drm_i915_gem_object *obj;
  2061. int plane = intel_crtc->plane;
  2062. unsigned long linear_offset;
  2063. u32 dspcntr;
  2064. u32 reg = DSPCNTR(plane);
  2065. int pixel_size;
  2066. if (!intel_crtc->primary_enabled) {
  2067. I915_WRITE(reg, 0);
  2068. if (INTEL_INFO(dev)->gen >= 4)
  2069. I915_WRITE(DSPSURF(plane), 0);
  2070. else
  2071. I915_WRITE(DSPADDR(plane), 0);
  2072. POSTING_READ(reg);
  2073. return;
  2074. }
  2075. obj = intel_fb_obj(fb);
  2076. if (WARN_ON(obj == NULL))
  2077. return;
  2078. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2079. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2080. dspcntr |= DISPLAY_PLANE_ENABLE;
  2081. if (INTEL_INFO(dev)->gen < 4) {
  2082. if (intel_crtc->pipe == PIPE_B)
  2083. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2084. /* pipesrc and dspsize control the size that is scaled from,
  2085. * which should always be the user's requested size.
  2086. */
  2087. I915_WRITE(DSPSIZE(plane),
  2088. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  2089. (intel_crtc->config.pipe_src_w - 1));
  2090. I915_WRITE(DSPPOS(plane), 0);
  2091. }
  2092. switch (fb->pixel_format) {
  2093. case DRM_FORMAT_C8:
  2094. dspcntr |= DISPPLANE_8BPP;
  2095. break;
  2096. case DRM_FORMAT_XRGB1555:
  2097. case DRM_FORMAT_ARGB1555:
  2098. dspcntr |= DISPPLANE_BGRX555;
  2099. break;
  2100. case DRM_FORMAT_RGB565:
  2101. dspcntr |= DISPPLANE_BGRX565;
  2102. break;
  2103. case DRM_FORMAT_XRGB8888:
  2104. case DRM_FORMAT_ARGB8888:
  2105. dspcntr |= DISPPLANE_BGRX888;
  2106. break;
  2107. case DRM_FORMAT_XBGR8888:
  2108. case DRM_FORMAT_ABGR8888:
  2109. dspcntr |= DISPPLANE_RGBX888;
  2110. break;
  2111. case DRM_FORMAT_XRGB2101010:
  2112. case DRM_FORMAT_ARGB2101010:
  2113. dspcntr |= DISPPLANE_BGRX101010;
  2114. break;
  2115. case DRM_FORMAT_XBGR2101010:
  2116. case DRM_FORMAT_ABGR2101010:
  2117. dspcntr |= DISPPLANE_RGBX101010;
  2118. break;
  2119. default:
  2120. BUG();
  2121. }
  2122. if (INTEL_INFO(dev)->gen >= 4 &&
  2123. obj->tiling_mode != I915_TILING_NONE)
  2124. dspcntr |= DISPPLANE_TILED;
  2125. if (IS_G4X(dev))
  2126. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2127. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2128. if (INTEL_INFO(dev)->gen >= 4) {
  2129. intel_crtc->dspaddr_offset =
  2130. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2131. pixel_size,
  2132. fb->pitches[0]);
  2133. linear_offset -= intel_crtc->dspaddr_offset;
  2134. } else {
  2135. intel_crtc->dspaddr_offset = linear_offset;
  2136. }
  2137. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2138. dspcntr |= DISPPLANE_ROTATE_180;
  2139. x += (intel_crtc->config.pipe_src_w - 1);
  2140. y += (intel_crtc->config.pipe_src_h - 1);
  2141. /* Finding the last pixel of the last line of the display
  2142. data and adding to linear_offset*/
  2143. linear_offset +=
  2144. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2145. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2146. }
  2147. I915_WRITE(reg, dspcntr);
  2148. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2149. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2150. fb->pitches[0]);
  2151. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2152. if (INTEL_INFO(dev)->gen >= 4) {
  2153. I915_WRITE(DSPSURF(plane),
  2154. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2155. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2156. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2157. } else
  2158. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2159. POSTING_READ(reg);
  2160. }
  2161. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2162. struct drm_framebuffer *fb,
  2163. int x, int y)
  2164. {
  2165. struct drm_device *dev = crtc->dev;
  2166. struct drm_i915_private *dev_priv = dev->dev_private;
  2167. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2168. struct drm_i915_gem_object *obj;
  2169. int plane = intel_crtc->plane;
  2170. unsigned long linear_offset;
  2171. u32 dspcntr;
  2172. u32 reg = DSPCNTR(plane);
  2173. int pixel_size;
  2174. if (!intel_crtc->primary_enabled) {
  2175. I915_WRITE(reg, 0);
  2176. I915_WRITE(DSPSURF(plane), 0);
  2177. POSTING_READ(reg);
  2178. return;
  2179. }
  2180. obj = intel_fb_obj(fb);
  2181. if (WARN_ON(obj == NULL))
  2182. return;
  2183. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2184. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2185. dspcntr |= DISPLAY_PLANE_ENABLE;
  2186. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2187. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2188. switch (fb->pixel_format) {
  2189. case DRM_FORMAT_C8:
  2190. dspcntr |= DISPPLANE_8BPP;
  2191. break;
  2192. case DRM_FORMAT_RGB565:
  2193. dspcntr |= DISPPLANE_BGRX565;
  2194. break;
  2195. case DRM_FORMAT_XRGB8888:
  2196. case DRM_FORMAT_ARGB8888:
  2197. dspcntr |= DISPPLANE_BGRX888;
  2198. break;
  2199. case DRM_FORMAT_XBGR8888:
  2200. case DRM_FORMAT_ABGR8888:
  2201. dspcntr |= DISPPLANE_RGBX888;
  2202. break;
  2203. case DRM_FORMAT_XRGB2101010:
  2204. case DRM_FORMAT_ARGB2101010:
  2205. dspcntr |= DISPPLANE_BGRX101010;
  2206. break;
  2207. case DRM_FORMAT_XBGR2101010:
  2208. case DRM_FORMAT_ABGR2101010:
  2209. dspcntr |= DISPPLANE_RGBX101010;
  2210. break;
  2211. default:
  2212. BUG();
  2213. }
  2214. if (obj->tiling_mode != I915_TILING_NONE)
  2215. dspcntr |= DISPPLANE_TILED;
  2216. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2217. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2218. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2219. intel_crtc->dspaddr_offset =
  2220. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2221. pixel_size,
  2222. fb->pitches[0]);
  2223. linear_offset -= intel_crtc->dspaddr_offset;
  2224. if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
  2225. dspcntr |= DISPPLANE_ROTATE_180;
  2226. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2227. x += (intel_crtc->config.pipe_src_w - 1);
  2228. y += (intel_crtc->config.pipe_src_h - 1);
  2229. /* Finding the last pixel of the last line of the display
  2230. data and adding to linear_offset*/
  2231. linear_offset +=
  2232. (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
  2233. (intel_crtc->config.pipe_src_w - 1) * pixel_size;
  2234. }
  2235. }
  2236. I915_WRITE(reg, dspcntr);
  2237. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2238. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2239. fb->pitches[0]);
  2240. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2241. I915_WRITE(DSPSURF(plane),
  2242. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2243. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2244. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2245. } else {
  2246. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2247. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2248. }
  2249. POSTING_READ(reg);
  2250. }
  2251. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2252. static int
  2253. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2254. int x, int y, enum mode_set_atomic state)
  2255. {
  2256. struct drm_device *dev = crtc->dev;
  2257. struct drm_i915_private *dev_priv = dev->dev_private;
  2258. if (dev_priv->display.disable_fbc)
  2259. dev_priv->display.disable_fbc(dev);
  2260. intel_increase_pllclock(dev, to_intel_crtc(crtc)->pipe);
  2261. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2262. return 0;
  2263. }
  2264. void intel_display_handle_reset(struct drm_device *dev)
  2265. {
  2266. struct drm_i915_private *dev_priv = dev->dev_private;
  2267. struct drm_crtc *crtc;
  2268. /*
  2269. * Flips in the rings have been nuked by the reset,
  2270. * so complete all pending flips so that user space
  2271. * will get its events and not get stuck.
  2272. *
  2273. * Also update the base address of all primary
  2274. * planes to the the last fb to make sure we're
  2275. * showing the correct fb after a reset.
  2276. *
  2277. * Need to make two loops over the crtcs so that we
  2278. * don't try to grab a crtc mutex before the
  2279. * pending_flip_queue really got woken up.
  2280. */
  2281. for_each_crtc(dev, crtc) {
  2282. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2283. enum plane plane = intel_crtc->plane;
  2284. intel_prepare_page_flip(dev, plane);
  2285. intel_finish_page_flip_plane(dev, plane);
  2286. }
  2287. for_each_crtc(dev, crtc) {
  2288. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2289. drm_modeset_lock(&crtc->mutex, NULL);
  2290. /*
  2291. * FIXME: Once we have proper support for primary planes (and
  2292. * disabling them without disabling the entire crtc) allow again
  2293. * a NULL crtc->primary->fb.
  2294. */
  2295. if (intel_crtc->active && crtc->primary->fb)
  2296. dev_priv->display.update_primary_plane(crtc,
  2297. crtc->primary->fb,
  2298. crtc->x,
  2299. crtc->y);
  2300. drm_modeset_unlock(&crtc->mutex);
  2301. }
  2302. }
  2303. static int
  2304. intel_finish_fb(struct drm_framebuffer *old_fb)
  2305. {
  2306. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2307. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2308. bool was_interruptible = dev_priv->mm.interruptible;
  2309. int ret;
  2310. /* Big Hammer, we also need to ensure that any pending
  2311. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2312. * current scanout is retired before unpinning the old
  2313. * framebuffer.
  2314. *
  2315. * This should only fail upon a hung GPU, in which case we
  2316. * can safely continue.
  2317. */
  2318. dev_priv->mm.interruptible = false;
  2319. ret = i915_gem_object_finish_gpu(obj);
  2320. dev_priv->mm.interruptible = was_interruptible;
  2321. return ret;
  2322. }
  2323. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2324. {
  2325. struct drm_device *dev = crtc->dev;
  2326. struct drm_i915_private *dev_priv = dev->dev_private;
  2327. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2328. unsigned long flags;
  2329. bool pending;
  2330. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2331. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2332. return false;
  2333. spin_lock_irqsave(&dev->event_lock, flags);
  2334. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2335. spin_unlock_irqrestore(&dev->event_lock, flags);
  2336. return pending;
  2337. }
  2338. static int
  2339. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  2340. struct drm_framebuffer *fb)
  2341. {
  2342. struct drm_device *dev = crtc->dev;
  2343. struct drm_i915_private *dev_priv = dev->dev_private;
  2344. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2345. enum pipe pipe = intel_crtc->pipe;
  2346. struct drm_framebuffer *old_fb = crtc->primary->fb;
  2347. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2348. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
  2349. int ret;
  2350. if (intel_crtc_has_pending_flip(crtc)) {
  2351. DRM_ERROR("pipe is still busy with an old pageflip\n");
  2352. return -EBUSY;
  2353. }
  2354. /* no fb bound */
  2355. if (!fb) {
  2356. DRM_ERROR("No FB bound\n");
  2357. return 0;
  2358. }
  2359. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  2360. DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
  2361. plane_name(intel_crtc->plane),
  2362. INTEL_INFO(dev)->num_pipes);
  2363. return -EINVAL;
  2364. }
  2365. mutex_lock(&dev->struct_mutex);
  2366. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  2367. if (ret == 0)
  2368. i915_gem_track_fb(old_obj, obj,
  2369. INTEL_FRONTBUFFER_PRIMARY(pipe));
  2370. mutex_unlock(&dev->struct_mutex);
  2371. if (ret != 0) {
  2372. DRM_ERROR("pin & fence failed\n");
  2373. return ret;
  2374. }
  2375. /*
  2376. * Update pipe size and adjust fitter if needed: the reason for this is
  2377. * that in compute_mode_changes we check the native mode (not the pfit
  2378. * mode) to see if we can flip rather than do a full mode set. In the
  2379. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2380. * pfit state, we'll end up with a big fb scanned out into the wrong
  2381. * sized surface.
  2382. *
  2383. * To fix this properly, we need to hoist the checks up into
  2384. * compute_mode_changes (or above), check the actual pfit state and
  2385. * whether the platform allows pfit disable with pipe active, and only
  2386. * then update the pipesrc and pfit state, even on the flip path.
  2387. */
  2388. if (i915.fastboot) {
  2389. const struct drm_display_mode *adjusted_mode =
  2390. &intel_crtc->config.adjusted_mode;
  2391. I915_WRITE(PIPESRC(intel_crtc->pipe),
  2392. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2393. (adjusted_mode->crtc_vdisplay - 1));
  2394. if (!intel_crtc->config.pch_pfit.enabled &&
  2395. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2396. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2397. I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
  2398. I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
  2399. I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
  2400. }
  2401. intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2402. intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2403. }
  2404. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2405. if (intel_crtc->active)
  2406. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  2407. crtc->primary->fb = fb;
  2408. crtc->x = x;
  2409. crtc->y = y;
  2410. if (old_fb) {
  2411. if (intel_crtc->active && old_fb != fb)
  2412. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2413. mutex_lock(&dev->struct_mutex);
  2414. intel_unpin_fb_obj(old_obj);
  2415. mutex_unlock(&dev->struct_mutex);
  2416. }
  2417. mutex_lock(&dev->struct_mutex);
  2418. intel_update_fbc(dev);
  2419. mutex_unlock(&dev->struct_mutex);
  2420. return 0;
  2421. }
  2422. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2423. {
  2424. struct drm_device *dev = crtc->dev;
  2425. struct drm_i915_private *dev_priv = dev->dev_private;
  2426. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2427. int pipe = intel_crtc->pipe;
  2428. u32 reg, temp;
  2429. /* enable normal train */
  2430. reg = FDI_TX_CTL(pipe);
  2431. temp = I915_READ(reg);
  2432. if (IS_IVYBRIDGE(dev)) {
  2433. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2434. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2435. } else {
  2436. temp &= ~FDI_LINK_TRAIN_NONE;
  2437. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2438. }
  2439. I915_WRITE(reg, temp);
  2440. reg = FDI_RX_CTL(pipe);
  2441. temp = I915_READ(reg);
  2442. if (HAS_PCH_CPT(dev)) {
  2443. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2444. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2445. } else {
  2446. temp &= ~FDI_LINK_TRAIN_NONE;
  2447. temp |= FDI_LINK_TRAIN_NONE;
  2448. }
  2449. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2450. /* wait one idle pattern time */
  2451. POSTING_READ(reg);
  2452. udelay(1000);
  2453. /* IVB wants error correction enabled */
  2454. if (IS_IVYBRIDGE(dev))
  2455. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2456. FDI_FE_ERRC_ENABLE);
  2457. }
  2458. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2459. {
  2460. return crtc->base.enabled && crtc->active &&
  2461. crtc->config.has_pch_encoder;
  2462. }
  2463. static void ivb_modeset_global_resources(struct drm_device *dev)
  2464. {
  2465. struct drm_i915_private *dev_priv = dev->dev_private;
  2466. struct intel_crtc *pipe_B_crtc =
  2467. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2468. struct intel_crtc *pipe_C_crtc =
  2469. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2470. uint32_t temp;
  2471. /*
  2472. * When everything is off disable fdi C so that we could enable fdi B
  2473. * with all lanes. Note that we don't care about enabled pipes without
  2474. * an enabled pch encoder.
  2475. */
  2476. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2477. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2478. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2479. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2480. temp = I915_READ(SOUTH_CHICKEN1);
  2481. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2482. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2483. I915_WRITE(SOUTH_CHICKEN1, temp);
  2484. }
  2485. }
  2486. /* The FDI link training functions for ILK/Ibexpeak. */
  2487. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2488. {
  2489. struct drm_device *dev = crtc->dev;
  2490. struct drm_i915_private *dev_priv = dev->dev_private;
  2491. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2492. int pipe = intel_crtc->pipe;
  2493. u32 reg, temp, tries;
  2494. /* FDI needs bits from pipe first */
  2495. assert_pipe_enabled(dev_priv, pipe);
  2496. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2497. for train result */
  2498. reg = FDI_RX_IMR(pipe);
  2499. temp = I915_READ(reg);
  2500. temp &= ~FDI_RX_SYMBOL_LOCK;
  2501. temp &= ~FDI_RX_BIT_LOCK;
  2502. I915_WRITE(reg, temp);
  2503. I915_READ(reg);
  2504. udelay(150);
  2505. /* enable CPU FDI TX and PCH FDI RX */
  2506. reg = FDI_TX_CTL(pipe);
  2507. temp = I915_READ(reg);
  2508. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2509. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2510. temp &= ~FDI_LINK_TRAIN_NONE;
  2511. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2512. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2513. reg = FDI_RX_CTL(pipe);
  2514. temp = I915_READ(reg);
  2515. temp &= ~FDI_LINK_TRAIN_NONE;
  2516. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2517. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2518. POSTING_READ(reg);
  2519. udelay(150);
  2520. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2521. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2522. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2523. FDI_RX_PHASE_SYNC_POINTER_EN);
  2524. reg = FDI_RX_IIR(pipe);
  2525. for (tries = 0; tries < 5; tries++) {
  2526. temp = I915_READ(reg);
  2527. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2528. if ((temp & FDI_RX_BIT_LOCK)) {
  2529. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2530. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2531. break;
  2532. }
  2533. }
  2534. if (tries == 5)
  2535. DRM_ERROR("FDI train 1 fail!\n");
  2536. /* Train 2 */
  2537. reg = FDI_TX_CTL(pipe);
  2538. temp = I915_READ(reg);
  2539. temp &= ~FDI_LINK_TRAIN_NONE;
  2540. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2541. I915_WRITE(reg, temp);
  2542. reg = FDI_RX_CTL(pipe);
  2543. temp = I915_READ(reg);
  2544. temp &= ~FDI_LINK_TRAIN_NONE;
  2545. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2546. I915_WRITE(reg, temp);
  2547. POSTING_READ(reg);
  2548. udelay(150);
  2549. reg = FDI_RX_IIR(pipe);
  2550. for (tries = 0; tries < 5; tries++) {
  2551. temp = I915_READ(reg);
  2552. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2553. if (temp & FDI_RX_SYMBOL_LOCK) {
  2554. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2555. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2556. break;
  2557. }
  2558. }
  2559. if (tries == 5)
  2560. DRM_ERROR("FDI train 2 fail!\n");
  2561. DRM_DEBUG_KMS("FDI train done\n");
  2562. }
  2563. static const int snb_b_fdi_train_param[] = {
  2564. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2565. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2566. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2567. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2568. };
  2569. /* The FDI link training functions for SNB/Cougarpoint. */
  2570. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2571. {
  2572. struct drm_device *dev = crtc->dev;
  2573. struct drm_i915_private *dev_priv = dev->dev_private;
  2574. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2575. int pipe = intel_crtc->pipe;
  2576. u32 reg, temp, i, retry;
  2577. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2578. for train result */
  2579. reg = FDI_RX_IMR(pipe);
  2580. temp = I915_READ(reg);
  2581. temp &= ~FDI_RX_SYMBOL_LOCK;
  2582. temp &= ~FDI_RX_BIT_LOCK;
  2583. I915_WRITE(reg, temp);
  2584. POSTING_READ(reg);
  2585. udelay(150);
  2586. /* enable CPU FDI TX and PCH FDI RX */
  2587. reg = FDI_TX_CTL(pipe);
  2588. temp = I915_READ(reg);
  2589. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2590. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2591. temp &= ~FDI_LINK_TRAIN_NONE;
  2592. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2593. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2594. /* SNB-B */
  2595. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2596. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2597. I915_WRITE(FDI_RX_MISC(pipe),
  2598. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2599. reg = FDI_RX_CTL(pipe);
  2600. temp = I915_READ(reg);
  2601. if (HAS_PCH_CPT(dev)) {
  2602. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2603. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2604. } else {
  2605. temp &= ~FDI_LINK_TRAIN_NONE;
  2606. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2607. }
  2608. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2609. POSTING_READ(reg);
  2610. udelay(150);
  2611. for (i = 0; i < 4; i++) {
  2612. reg = FDI_TX_CTL(pipe);
  2613. temp = I915_READ(reg);
  2614. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2615. temp |= snb_b_fdi_train_param[i];
  2616. I915_WRITE(reg, temp);
  2617. POSTING_READ(reg);
  2618. udelay(500);
  2619. for (retry = 0; retry < 5; retry++) {
  2620. reg = FDI_RX_IIR(pipe);
  2621. temp = I915_READ(reg);
  2622. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2623. if (temp & FDI_RX_BIT_LOCK) {
  2624. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2625. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2626. break;
  2627. }
  2628. udelay(50);
  2629. }
  2630. if (retry < 5)
  2631. break;
  2632. }
  2633. if (i == 4)
  2634. DRM_ERROR("FDI train 1 fail!\n");
  2635. /* Train 2 */
  2636. reg = FDI_TX_CTL(pipe);
  2637. temp = I915_READ(reg);
  2638. temp &= ~FDI_LINK_TRAIN_NONE;
  2639. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2640. if (IS_GEN6(dev)) {
  2641. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2642. /* SNB-B */
  2643. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2644. }
  2645. I915_WRITE(reg, temp);
  2646. reg = FDI_RX_CTL(pipe);
  2647. temp = I915_READ(reg);
  2648. if (HAS_PCH_CPT(dev)) {
  2649. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2650. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2651. } else {
  2652. temp &= ~FDI_LINK_TRAIN_NONE;
  2653. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2654. }
  2655. I915_WRITE(reg, temp);
  2656. POSTING_READ(reg);
  2657. udelay(150);
  2658. for (i = 0; i < 4; i++) {
  2659. reg = FDI_TX_CTL(pipe);
  2660. temp = I915_READ(reg);
  2661. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2662. temp |= snb_b_fdi_train_param[i];
  2663. I915_WRITE(reg, temp);
  2664. POSTING_READ(reg);
  2665. udelay(500);
  2666. for (retry = 0; retry < 5; retry++) {
  2667. reg = FDI_RX_IIR(pipe);
  2668. temp = I915_READ(reg);
  2669. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2670. if (temp & FDI_RX_SYMBOL_LOCK) {
  2671. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2672. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2673. break;
  2674. }
  2675. udelay(50);
  2676. }
  2677. if (retry < 5)
  2678. break;
  2679. }
  2680. if (i == 4)
  2681. DRM_ERROR("FDI train 2 fail!\n");
  2682. DRM_DEBUG_KMS("FDI train done.\n");
  2683. }
  2684. /* Manual link training for Ivy Bridge A0 parts */
  2685. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2686. {
  2687. struct drm_device *dev = crtc->dev;
  2688. struct drm_i915_private *dev_priv = dev->dev_private;
  2689. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2690. int pipe = intel_crtc->pipe;
  2691. u32 reg, temp, i, j;
  2692. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2693. for train result */
  2694. reg = FDI_RX_IMR(pipe);
  2695. temp = I915_READ(reg);
  2696. temp &= ~FDI_RX_SYMBOL_LOCK;
  2697. temp &= ~FDI_RX_BIT_LOCK;
  2698. I915_WRITE(reg, temp);
  2699. POSTING_READ(reg);
  2700. udelay(150);
  2701. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2702. I915_READ(FDI_RX_IIR(pipe)));
  2703. /* Try each vswing and preemphasis setting twice before moving on */
  2704. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2705. /* disable first in case we need to retry */
  2706. reg = FDI_TX_CTL(pipe);
  2707. temp = I915_READ(reg);
  2708. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2709. temp &= ~FDI_TX_ENABLE;
  2710. I915_WRITE(reg, temp);
  2711. reg = FDI_RX_CTL(pipe);
  2712. temp = I915_READ(reg);
  2713. temp &= ~FDI_LINK_TRAIN_AUTO;
  2714. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2715. temp &= ~FDI_RX_ENABLE;
  2716. I915_WRITE(reg, temp);
  2717. /* enable CPU FDI TX and PCH FDI RX */
  2718. reg = FDI_TX_CTL(pipe);
  2719. temp = I915_READ(reg);
  2720. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2721. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2722. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2723. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2724. temp |= snb_b_fdi_train_param[j/2];
  2725. temp |= FDI_COMPOSITE_SYNC;
  2726. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2727. I915_WRITE(FDI_RX_MISC(pipe),
  2728. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2729. reg = FDI_RX_CTL(pipe);
  2730. temp = I915_READ(reg);
  2731. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2732. temp |= FDI_COMPOSITE_SYNC;
  2733. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2734. POSTING_READ(reg);
  2735. udelay(1); /* should be 0.5us */
  2736. for (i = 0; i < 4; i++) {
  2737. reg = FDI_RX_IIR(pipe);
  2738. temp = I915_READ(reg);
  2739. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2740. if (temp & FDI_RX_BIT_LOCK ||
  2741. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2742. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2743. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2744. i);
  2745. break;
  2746. }
  2747. udelay(1); /* should be 0.5us */
  2748. }
  2749. if (i == 4) {
  2750. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2751. continue;
  2752. }
  2753. /* Train 2 */
  2754. reg = FDI_TX_CTL(pipe);
  2755. temp = I915_READ(reg);
  2756. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2757. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2758. I915_WRITE(reg, temp);
  2759. reg = FDI_RX_CTL(pipe);
  2760. temp = I915_READ(reg);
  2761. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2762. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2763. I915_WRITE(reg, temp);
  2764. POSTING_READ(reg);
  2765. udelay(2); /* should be 1.5us */
  2766. for (i = 0; i < 4; i++) {
  2767. reg = FDI_RX_IIR(pipe);
  2768. temp = I915_READ(reg);
  2769. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2770. if (temp & FDI_RX_SYMBOL_LOCK ||
  2771. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2772. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2773. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2774. i);
  2775. goto train_done;
  2776. }
  2777. udelay(2); /* should be 1.5us */
  2778. }
  2779. if (i == 4)
  2780. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2781. }
  2782. train_done:
  2783. DRM_DEBUG_KMS("FDI train done.\n");
  2784. }
  2785. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2786. {
  2787. struct drm_device *dev = intel_crtc->base.dev;
  2788. struct drm_i915_private *dev_priv = dev->dev_private;
  2789. int pipe = intel_crtc->pipe;
  2790. u32 reg, temp;
  2791. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2792. reg = FDI_RX_CTL(pipe);
  2793. temp = I915_READ(reg);
  2794. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2795. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2796. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2797. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2798. POSTING_READ(reg);
  2799. udelay(200);
  2800. /* Switch from Rawclk to PCDclk */
  2801. temp = I915_READ(reg);
  2802. I915_WRITE(reg, temp | FDI_PCDCLK);
  2803. POSTING_READ(reg);
  2804. udelay(200);
  2805. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2806. reg = FDI_TX_CTL(pipe);
  2807. temp = I915_READ(reg);
  2808. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2809. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2810. POSTING_READ(reg);
  2811. udelay(100);
  2812. }
  2813. }
  2814. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2815. {
  2816. struct drm_device *dev = intel_crtc->base.dev;
  2817. struct drm_i915_private *dev_priv = dev->dev_private;
  2818. int pipe = intel_crtc->pipe;
  2819. u32 reg, temp;
  2820. /* Switch from PCDclk to Rawclk */
  2821. reg = FDI_RX_CTL(pipe);
  2822. temp = I915_READ(reg);
  2823. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2824. /* Disable CPU FDI TX PLL */
  2825. reg = FDI_TX_CTL(pipe);
  2826. temp = I915_READ(reg);
  2827. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2828. POSTING_READ(reg);
  2829. udelay(100);
  2830. reg = FDI_RX_CTL(pipe);
  2831. temp = I915_READ(reg);
  2832. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2833. /* Wait for the clocks to turn off. */
  2834. POSTING_READ(reg);
  2835. udelay(100);
  2836. }
  2837. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2838. {
  2839. struct drm_device *dev = crtc->dev;
  2840. struct drm_i915_private *dev_priv = dev->dev_private;
  2841. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2842. int pipe = intel_crtc->pipe;
  2843. u32 reg, temp;
  2844. /* disable CPU FDI tx and PCH FDI rx */
  2845. reg = FDI_TX_CTL(pipe);
  2846. temp = I915_READ(reg);
  2847. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2848. POSTING_READ(reg);
  2849. reg = FDI_RX_CTL(pipe);
  2850. temp = I915_READ(reg);
  2851. temp &= ~(0x7 << 16);
  2852. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2853. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2854. POSTING_READ(reg);
  2855. udelay(100);
  2856. /* Ironlake workaround, disable clock pointer after downing FDI */
  2857. if (HAS_PCH_IBX(dev))
  2858. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2859. /* still set train pattern 1 */
  2860. reg = FDI_TX_CTL(pipe);
  2861. temp = I915_READ(reg);
  2862. temp &= ~FDI_LINK_TRAIN_NONE;
  2863. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2864. I915_WRITE(reg, temp);
  2865. reg = FDI_RX_CTL(pipe);
  2866. temp = I915_READ(reg);
  2867. if (HAS_PCH_CPT(dev)) {
  2868. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2869. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2870. } else {
  2871. temp &= ~FDI_LINK_TRAIN_NONE;
  2872. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2873. }
  2874. /* BPC in FDI rx is consistent with that in PIPECONF */
  2875. temp &= ~(0x07 << 16);
  2876. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2877. I915_WRITE(reg, temp);
  2878. POSTING_READ(reg);
  2879. udelay(100);
  2880. }
  2881. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  2882. {
  2883. struct intel_crtc *crtc;
  2884. /* Note that we don't need to be called with mode_config.lock here
  2885. * as our list of CRTC objects is static for the lifetime of the
  2886. * device and so cannot disappear as we iterate. Similarly, we can
  2887. * happily treat the predicates as racy, atomic checks as userspace
  2888. * cannot claim and pin a new fb without at least acquring the
  2889. * struct_mutex and so serialising with us.
  2890. */
  2891. for_each_intel_crtc(dev, crtc) {
  2892. if (atomic_read(&crtc->unpin_work_count) == 0)
  2893. continue;
  2894. if (crtc->unpin_work)
  2895. intel_wait_for_vblank(dev, crtc->pipe);
  2896. return true;
  2897. }
  2898. return false;
  2899. }
  2900. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2901. {
  2902. struct drm_device *dev = crtc->dev;
  2903. struct drm_i915_private *dev_priv = dev->dev_private;
  2904. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  2905. WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  2906. !intel_crtc_has_pending_flip(crtc),
  2907. 60*HZ) == 0);
  2908. if (crtc->primary->fb) {
  2909. mutex_lock(&dev->struct_mutex);
  2910. intel_finish_fb(crtc->primary->fb);
  2911. mutex_unlock(&dev->struct_mutex);
  2912. }
  2913. }
  2914. /* Program iCLKIP clock to the desired frequency */
  2915. static void lpt_program_iclkip(struct drm_crtc *crtc)
  2916. {
  2917. struct drm_device *dev = crtc->dev;
  2918. struct drm_i915_private *dev_priv = dev->dev_private;
  2919. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  2920. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  2921. u32 temp;
  2922. mutex_lock(&dev_priv->dpio_lock);
  2923. /* It is necessary to ungate the pixclk gate prior to programming
  2924. * the divisors, and gate it back when it is done.
  2925. */
  2926. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  2927. /* Disable SSCCTL */
  2928. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  2929. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  2930. SBI_SSCCTL_DISABLE,
  2931. SBI_ICLK);
  2932. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  2933. if (clock == 20000) {
  2934. auxdiv = 1;
  2935. divsel = 0x41;
  2936. phaseinc = 0x20;
  2937. } else {
  2938. /* The iCLK virtual clock root frequency is in MHz,
  2939. * but the adjusted_mode->crtc_clock in in KHz. To get the
  2940. * divisors, it is necessary to divide one by another, so we
  2941. * convert the virtual clock precision to KHz here for higher
  2942. * precision.
  2943. */
  2944. u32 iclk_virtual_root_freq = 172800 * 1000;
  2945. u32 iclk_pi_range = 64;
  2946. u32 desired_divisor, msb_divisor_value, pi_value;
  2947. desired_divisor = (iclk_virtual_root_freq / clock);
  2948. msb_divisor_value = desired_divisor / iclk_pi_range;
  2949. pi_value = desired_divisor % iclk_pi_range;
  2950. auxdiv = 0;
  2951. divsel = msb_divisor_value - 2;
  2952. phaseinc = pi_value;
  2953. }
  2954. /* This should not happen with any sane values */
  2955. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  2956. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  2957. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  2958. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  2959. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  2960. clock,
  2961. auxdiv,
  2962. divsel,
  2963. phasedir,
  2964. phaseinc);
  2965. /* Program SSCDIVINTPHASE6 */
  2966. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  2967. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  2968. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  2969. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  2970. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  2971. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  2972. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  2973. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  2974. /* Program SSCAUXDIV */
  2975. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  2976. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  2977. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  2978. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  2979. /* Enable modulator and associated divider */
  2980. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  2981. temp &= ~SBI_SSCCTL_DISABLE;
  2982. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  2983. /* Wait for initialization time */
  2984. udelay(24);
  2985. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  2986. mutex_unlock(&dev_priv->dpio_lock);
  2987. }
  2988. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  2989. enum pipe pch_transcoder)
  2990. {
  2991. struct drm_device *dev = crtc->base.dev;
  2992. struct drm_i915_private *dev_priv = dev->dev_private;
  2993. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  2994. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  2995. I915_READ(HTOTAL(cpu_transcoder)));
  2996. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  2997. I915_READ(HBLANK(cpu_transcoder)));
  2998. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  2999. I915_READ(HSYNC(cpu_transcoder)));
  3000. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3001. I915_READ(VTOTAL(cpu_transcoder)));
  3002. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3003. I915_READ(VBLANK(cpu_transcoder)));
  3004. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3005. I915_READ(VSYNC(cpu_transcoder)));
  3006. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3007. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3008. }
  3009. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  3010. {
  3011. struct drm_i915_private *dev_priv = dev->dev_private;
  3012. uint32_t temp;
  3013. temp = I915_READ(SOUTH_CHICKEN1);
  3014. if (temp & FDI_BC_BIFURCATION_SELECT)
  3015. return;
  3016. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3017. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3018. temp |= FDI_BC_BIFURCATION_SELECT;
  3019. DRM_DEBUG_KMS("enabling fdi C rx\n");
  3020. I915_WRITE(SOUTH_CHICKEN1, temp);
  3021. POSTING_READ(SOUTH_CHICKEN1);
  3022. }
  3023. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3024. {
  3025. struct drm_device *dev = intel_crtc->base.dev;
  3026. struct drm_i915_private *dev_priv = dev->dev_private;
  3027. switch (intel_crtc->pipe) {
  3028. case PIPE_A:
  3029. break;
  3030. case PIPE_B:
  3031. if (intel_crtc->config.fdi_lanes > 2)
  3032. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  3033. else
  3034. cpt_enable_fdi_bc_bifurcation(dev);
  3035. break;
  3036. case PIPE_C:
  3037. cpt_enable_fdi_bc_bifurcation(dev);
  3038. break;
  3039. default:
  3040. BUG();
  3041. }
  3042. }
  3043. /*
  3044. * Enable PCH resources required for PCH ports:
  3045. * - PCH PLLs
  3046. * - FDI training & RX/TX
  3047. * - update transcoder timings
  3048. * - DP transcoding bits
  3049. * - transcoder
  3050. */
  3051. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3052. {
  3053. struct drm_device *dev = crtc->dev;
  3054. struct drm_i915_private *dev_priv = dev->dev_private;
  3055. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3056. int pipe = intel_crtc->pipe;
  3057. u32 reg, temp;
  3058. assert_pch_transcoder_disabled(dev_priv, pipe);
  3059. if (IS_IVYBRIDGE(dev))
  3060. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3061. /* Write the TU size bits before fdi link training, so that error
  3062. * detection works. */
  3063. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3064. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3065. /* For PCH output, training FDI link */
  3066. dev_priv->display.fdi_link_train(crtc);
  3067. /* We need to program the right clock selection before writing the pixel
  3068. * mutliplier into the DPLL. */
  3069. if (HAS_PCH_CPT(dev)) {
  3070. u32 sel;
  3071. temp = I915_READ(PCH_DPLL_SEL);
  3072. temp |= TRANS_DPLL_ENABLE(pipe);
  3073. sel = TRANS_DPLLB_SEL(pipe);
  3074. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  3075. temp |= sel;
  3076. else
  3077. temp &= ~sel;
  3078. I915_WRITE(PCH_DPLL_SEL, temp);
  3079. }
  3080. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3081. * transcoder, and we actually should do this to not upset any PCH
  3082. * transcoder that already use the clock when we share it.
  3083. *
  3084. * Note that enable_shared_dpll tries to do the right thing, but
  3085. * get_shared_dpll unconditionally resets the pll - we need that to have
  3086. * the right LVDS enable sequence. */
  3087. intel_enable_shared_dpll(intel_crtc);
  3088. /* set transcoder timing, panel must allow it */
  3089. assert_panel_unlocked(dev_priv, pipe);
  3090. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3091. intel_fdi_normal_train(crtc);
  3092. /* For PCH DP, enable TRANS_DP_CTL */
  3093. if (HAS_PCH_CPT(dev) &&
  3094. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  3095. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  3096. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3097. reg = TRANS_DP_CTL(pipe);
  3098. temp = I915_READ(reg);
  3099. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3100. TRANS_DP_SYNC_MASK |
  3101. TRANS_DP_BPC_MASK);
  3102. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3103. TRANS_DP_ENH_FRAMING);
  3104. temp |= bpc << 9; /* same format but at 11:9 */
  3105. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3106. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3107. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3108. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3109. switch (intel_trans_dp_port_sel(crtc)) {
  3110. case PCH_DP_B:
  3111. temp |= TRANS_DP_PORT_SEL_B;
  3112. break;
  3113. case PCH_DP_C:
  3114. temp |= TRANS_DP_PORT_SEL_C;
  3115. break;
  3116. case PCH_DP_D:
  3117. temp |= TRANS_DP_PORT_SEL_D;
  3118. break;
  3119. default:
  3120. BUG();
  3121. }
  3122. I915_WRITE(reg, temp);
  3123. }
  3124. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3125. }
  3126. static void lpt_pch_enable(struct drm_crtc *crtc)
  3127. {
  3128. struct drm_device *dev = crtc->dev;
  3129. struct drm_i915_private *dev_priv = dev->dev_private;
  3130. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3131. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3132. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3133. lpt_program_iclkip(crtc);
  3134. /* Set transcoder timing. */
  3135. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3136. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3137. }
  3138. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3139. {
  3140. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3141. if (pll == NULL)
  3142. return;
  3143. if (pll->refcount == 0) {
  3144. WARN(1, "bad %s refcount\n", pll->name);
  3145. return;
  3146. }
  3147. if (--pll->refcount == 0) {
  3148. WARN_ON(pll->on);
  3149. WARN_ON(pll->active);
  3150. }
  3151. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3152. }
  3153. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3154. {
  3155. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3156. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3157. enum intel_dpll_id i;
  3158. if (pll) {
  3159. DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
  3160. crtc->base.base.id, pll->name);
  3161. intel_put_shared_dpll(crtc);
  3162. }
  3163. if (HAS_PCH_IBX(dev_priv->dev)) {
  3164. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3165. i = (enum intel_dpll_id) crtc->pipe;
  3166. pll = &dev_priv->shared_dplls[i];
  3167. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3168. crtc->base.base.id, pll->name);
  3169. WARN_ON(pll->refcount);
  3170. goto found;
  3171. }
  3172. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3173. pll = &dev_priv->shared_dplls[i];
  3174. /* Only want to check enabled timings first */
  3175. if (pll->refcount == 0)
  3176. continue;
  3177. if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
  3178. sizeof(pll->hw_state)) == 0) {
  3179. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
  3180. crtc->base.base.id,
  3181. pll->name, pll->refcount, pll->active);
  3182. goto found;
  3183. }
  3184. }
  3185. /* Ok no matching timings, maybe there's a free one? */
  3186. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3187. pll = &dev_priv->shared_dplls[i];
  3188. if (pll->refcount == 0) {
  3189. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3190. crtc->base.base.id, pll->name);
  3191. goto found;
  3192. }
  3193. }
  3194. return NULL;
  3195. found:
  3196. if (pll->refcount == 0)
  3197. pll->hw_state = crtc->config.dpll_hw_state;
  3198. crtc->config.shared_dpll = i;
  3199. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3200. pipe_name(crtc->pipe));
  3201. pll->refcount++;
  3202. return pll;
  3203. }
  3204. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3205. {
  3206. struct drm_i915_private *dev_priv = dev->dev_private;
  3207. int dslreg = PIPEDSL(pipe);
  3208. u32 temp;
  3209. temp = I915_READ(dslreg);
  3210. udelay(500);
  3211. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3212. if (wait_for(I915_READ(dslreg) != temp, 5))
  3213. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3214. }
  3215. }
  3216. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3217. {
  3218. struct drm_device *dev = crtc->base.dev;
  3219. struct drm_i915_private *dev_priv = dev->dev_private;
  3220. int pipe = crtc->pipe;
  3221. if (crtc->config.pch_pfit.enabled) {
  3222. /* Force use of hard-coded filter coefficients
  3223. * as some pre-programmed values are broken,
  3224. * e.g. x201.
  3225. */
  3226. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3227. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3228. PF_PIPE_SEL_IVB(pipe));
  3229. else
  3230. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3231. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3232. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3233. }
  3234. }
  3235. static void intel_enable_planes(struct drm_crtc *crtc)
  3236. {
  3237. struct drm_device *dev = crtc->dev;
  3238. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3239. struct drm_plane *plane;
  3240. struct intel_plane *intel_plane;
  3241. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3242. intel_plane = to_intel_plane(plane);
  3243. if (intel_plane->pipe == pipe)
  3244. intel_plane_restore(&intel_plane->base);
  3245. }
  3246. }
  3247. static void intel_disable_planes(struct drm_crtc *crtc)
  3248. {
  3249. struct drm_device *dev = crtc->dev;
  3250. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3251. struct drm_plane *plane;
  3252. struct intel_plane *intel_plane;
  3253. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3254. intel_plane = to_intel_plane(plane);
  3255. if (intel_plane->pipe == pipe)
  3256. intel_plane_disable(&intel_plane->base);
  3257. }
  3258. }
  3259. void hsw_enable_ips(struct intel_crtc *crtc)
  3260. {
  3261. struct drm_device *dev = crtc->base.dev;
  3262. struct drm_i915_private *dev_priv = dev->dev_private;
  3263. if (!crtc->config.ips_enabled)
  3264. return;
  3265. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3266. intel_wait_for_vblank(dev, crtc->pipe);
  3267. assert_plane_enabled(dev_priv, crtc->plane);
  3268. if (IS_BROADWELL(dev)) {
  3269. mutex_lock(&dev_priv->rps.hw_lock);
  3270. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3271. mutex_unlock(&dev_priv->rps.hw_lock);
  3272. /* Quoting Art Runyan: "its not safe to expect any particular
  3273. * value in IPS_CTL bit 31 after enabling IPS through the
  3274. * mailbox." Moreover, the mailbox may return a bogus state,
  3275. * so we need to just enable it and continue on.
  3276. */
  3277. } else {
  3278. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3279. /* The bit only becomes 1 in the next vblank, so this wait here
  3280. * is essentially intel_wait_for_vblank. If we don't have this
  3281. * and don't wait for vblanks until the end of crtc_enable, then
  3282. * the HW state readout code will complain that the expected
  3283. * IPS_CTL value is not the one we read. */
  3284. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3285. DRM_ERROR("Timed out waiting for IPS enable\n");
  3286. }
  3287. }
  3288. void hsw_disable_ips(struct intel_crtc *crtc)
  3289. {
  3290. struct drm_device *dev = crtc->base.dev;
  3291. struct drm_i915_private *dev_priv = dev->dev_private;
  3292. if (!crtc->config.ips_enabled)
  3293. return;
  3294. assert_plane_enabled(dev_priv, crtc->plane);
  3295. if (IS_BROADWELL(dev)) {
  3296. mutex_lock(&dev_priv->rps.hw_lock);
  3297. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3298. mutex_unlock(&dev_priv->rps.hw_lock);
  3299. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3300. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3301. DRM_ERROR("Timed out waiting for IPS disable\n");
  3302. } else {
  3303. I915_WRITE(IPS_CTL, 0);
  3304. POSTING_READ(IPS_CTL);
  3305. }
  3306. /* We need to wait for a vblank before we can disable the plane. */
  3307. intel_wait_for_vblank(dev, crtc->pipe);
  3308. }
  3309. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3310. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3311. {
  3312. struct drm_device *dev = crtc->dev;
  3313. struct drm_i915_private *dev_priv = dev->dev_private;
  3314. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3315. enum pipe pipe = intel_crtc->pipe;
  3316. int palreg = PALETTE(pipe);
  3317. int i;
  3318. bool reenable_ips = false;
  3319. /* The clocks have to be on to load the palette. */
  3320. if (!crtc->enabled || !intel_crtc->active)
  3321. return;
  3322. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3323. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  3324. assert_dsi_pll_enabled(dev_priv);
  3325. else
  3326. assert_pll_enabled(dev_priv, pipe);
  3327. }
  3328. /* use legacy palette for Ironlake */
  3329. if (!HAS_GMCH_DISPLAY(dev))
  3330. palreg = LGC_PALETTE(pipe);
  3331. /* Workaround : Do not read or write the pipe palette/gamma data while
  3332. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3333. */
  3334. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3335. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3336. GAMMA_MODE_MODE_SPLIT)) {
  3337. hsw_disable_ips(intel_crtc);
  3338. reenable_ips = true;
  3339. }
  3340. for (i = 0; i < 256; i++) {
  3341. I915_WRITE(palreg + 4 * i,
  3342. (intel_crtc->lut_r[i] << 16) |
  3343. (intel_crtc->lut_g[i] << 8) |
  3344. intel_crtc->lut_b[i]);
  3345. }
  3346. if (reenable_ips)
  3347. hsw_enable_ips(intel_crtc);
  3348. }
  3349. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3350. {
  3351. if (!enable && intel_crtc->overlay) {
  3352. struct drm_device *dev = intel_crtc->base.dev;
  3353. struct drm_i915_private *dev_priv = dev->dev_private;
  3354. mutex_lock(&dev->struct_mutex);
  3355. dev_priv->mm.interruptible = false;
  3356. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3357. dev_priv->mm.interruptible = true;
  3358. mutex_unlock(&dev->struct_mutex);
  3359. }
  3360. /* Let userspace switch the overlay on again. In most cases userspace
  3361. * has to recompute where to put it anyway.
  3362. */
  3363. }
  3364. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3365. {
  3366. struct drm_device *dev = crtc->dev;
  3367. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3368. int pipe = intel_crtc->pipe;
  3369. drm_vblank_on(dev, pipe);
  3370. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3371. intel_enable_planes(crtc);
  3372. intel_crtc_update_cursor(crtc, true);
  3373. intel_crtc_dpms_overlay(intel_crtc, true);
  3374. hsw_enable_ips(intel_crtc);
  3375. mutex_lock(&dev->struct_mutex);
  3376. intel_update_fbc(dev);
  3377. mutex_unlock(&dev->struct_mutex);
  3378. /*
  3379. * FIXME: Once we grow proper nuclear flip support out of this we need
  3380. * to compute the mask of flip planes precisely. For the time being
  3381. * consider this a flip from a NULL plane.
  3382. */
  3383. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3384. }
  3385. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3386. {
  3387. struct drm_device *dev = crtc->dev;
  3388. struct drm_i915_private *dev_priv = dev->dev_private;
  3389. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3390. int pipe = intel_crtc->pipe;
  3391. int plane = intel_crtc->plane;
  3392. intel_crtc_wait_for_pending_flips(crtc);
  3393. if (dev_priv->fbc.plane == plane)
  3394. intel_disable_fbc(dev);
  3395. hsw_disable_ips(intel_crtc);
  3396. intel_crtc_dpms_overlay(intel_crtc, false);
  3397. intel_crtc_update_cursor(crtc, false);
  3398. intel_disable_planes(crtc);
  3399. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3400. /*
  3401. * FIXME: Once we grow proper nuclear flip support out of this we need
  3402. * to compute the mask of flip planes precisely. For the time being
  3403. * consider this a flip to a NULL plane.
  3404. */
  3405. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3406. drm_vblank_off(dev, pipe);
  3407. }
  3408. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3409. {
  3410. struct drm_device *dev = crtc->dev;
  3411. struct drm_i915_private *dev_priv = dev->dev_private;
  3412. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3413. struct intel_encoder *encoder;
  3414. int pipe = intel_crtc->pipe;
  3415. WARN_ON(!crtc->enabled);
  3416. if (intel_crtc->active)
  3417. return;
  3418. if (intel_crtc->config.has_pch_encoder)
  3419. intel_prepare_shared_dpll(intel_crtc);
  3420. if (intel_crtc->config.has_dp_encoder)
  3421. intel_dp_set_m_n(intel_crtc);
  3422. intel_set_pipe_timings(intel_crtc);
  3423. if (intel_crtc->config.has_pch_encoder) {
  3424. intel_cpu_transcoder_set_m_n(intel_crtc,
  3425. &intel_crtc->config.fdi_m_n, NULL);
  3426. }
  3427. ironlake_set_pipeconf(crtc);
  3428. intel_crtc->active = true;
  3429. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3430. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3431. for_each_encoder_on_crtc(dev, crtc, encoder)
  3432. if (encoder->pre_enable)
  3433. encoder->pre_enable(encoder);
  3434. if (intel_crtc->config.has_pch_encoder) {
  3435. /* Note: FDI PLL enabling _must_ be done before we enable the
  3436. * cpu pipes, hence this is separate from all the other fdi/pch
  3437. * enabling. */
  3438. ironlake_fdi_pll_enable(intel_crtc);
  3439. } else {
  3440. assert_fdi_tx_disabled(dev_priv, pipe);
  3441. assert_fdi_rx_disabled(dev_priv, pipe);
  3442. }
  3443. ironlake_pfit_enable(intel_crtc);
  3444. /*
  3445. * On ILK+ LUT must be loaded before the pipe is running but with
  3446. * clocks enabled
  3447. */
  3448. intel_crtc_load_lut(crtc);
  3449. intel_update_watermarks(crtc);
  3450. intel_enable_pipe(intel_crtc);
  3451. if (intel_crtc->config.has_pch_encoder)
  3452. ironlake_pch_enable(crtc);
  3453. for_each_encoder_on_crtc(dev, crtc, encoder)
  3454. encoder->enable(encoder);
  3455. if (HAS_PCH_CPT(dev))
  3456. cpt_verify_modeset(dev, intel_crtc->pipe);
  3457. intel_crtc_enable_planes(crtc);
  3458. }
  3459. /* IPS only exists on ULT machines and is tied to pipe A. */
  3460. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3461. {
  3462. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3463. }
  3464. /*
  3465. * This implements the workaround described in the "notes" section of the mode
  3466. * set sequence documentation. When going from no pipes or single pipe to
  3467. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3468. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3469. */
  3470. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3471. {
  3472. struct drm_device *dev = crtc->base.dev;
  3473. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3474. /* We want to get the other_active_crtc only if there's only 1 other
  3475. * active crtc. */
  3476. for_each_intel_crtc(dev, crtc_it) {
  3477. if (!crtc_it->active || crtc_it == crtc)
  3478. continue;
  3479. if (other_active_crtc)
  3480. return;
  3481. other_active_crtc = crtc_it;
  3482. }
  3483. if (!other_active_crtc)
  3484. return;
  3485. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3486. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3487. }
  3488. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3489. {
  3490. struct drm_device *dev = crtc->dev;
  3491. struct drm_i915_private *dev_priv = dev->dev_private;
  3492. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3493. struct intel_encoder *encoder;
  3494. int pipe = intel_crtc->pipe;
  3495. WARN_ON(!crtc->enabled);
  3496. if (intel_crtc->active)
  3497. return;
  3498. if (intel_crtc_to_shared_dpll(intel_crtc))
  3499. intel_enable_shared_dpll(intel_crtc);
  3500. if (intel_crtc->config.has_dp_encoder)
  3501. intel_dp_set_m_n(intel_crtc);
  3502. intel_set_pipe_timings(intel_crtc);
  3503. if (intel_crtc->config.has_pch_encoder) {
  3504. intel_cpu_transcoder_set_m_n(intel_crtc,
  3505. &intel_crtc->config.fdi_m_n, NULL);
  3506. }
  3507. haswell_set_pipeconf(crtc);
  3508. intel_set_pipe_csc(crtc);
  3509. intel_crtc->active = true;
  3510. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3511. for_each_encoder_on_crtc(dev, crtc, encoder)
  3512. if (encoder->pre_enable)
  3513. encoder->pre_enable(encoder);
  3514. if (intel_crtc->config.has_pch_encoder) {
  3515. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3516. dev_priv->display.fdi_link_train(crtc);
  3517. }
  3518. intel_ddi_enable_pipe_clock(intel_crtc);
  3519. ironlake_pfit_enable(intel_crtc);
  3520. /*
  3521. * On ILK+ LUT must be loaded before the pipe is running but with
  3522. * clocks enabled
  3523. */
  3524. intel_crtc_load_lut(crtc);
  3525. intel_ddi_set_pipe_settings(crtc);
  3526. intel_ddi_enable_transcoder_func(crtc);
  3527. intel_update_watermarks(crtc);
  3528. intel_enable_pipe(intel_crtc);
  3529. if (intel_crtc->config.has_pch_encoder)
  3530. lpt_pch_enable(crtc);
  3531. if (intel_crtc->config.dp_encoder_is_mst)
  3532. intel_ddi_set_vc_payload_alloc(crtc, true);
  3533. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3534. encoder->enable(encoder);
  3535. intel_opregion_notify_encoder(encoder, true);
  3536. }
  3537. /* If we change the relative order between pipe/planes enabling, we need
  3538. * to change the workaround. */
  3539. haswell_mode_set_planes_workaround(intel_crtc);
  3540. intel_crtc_enable_planes(crtc);
  3541. }
  3542. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3543. {
  3544. struct drm_device *dev = crtc->base.dev;
  3545. struct drm_i915_private *dev_priv = dev->dev_private;
  3546. int pipe = crtc->pipe;
  3547. /* To avoid upsetting the power well on haswell only disable the pfit if
  3548. * it's in use. The hw state code will make sure we get this right. */
  3549. if (crtc->config.pch_pfit.enabled) {
  3550. I915_WRITE(PF_CTL(pipe), 0);
  3551. I915_WRITE(PF_WIN_POS(pipe), 0);
  3552. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3553. }
  3554. }
  3555. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3556. {
  3557. struct drm_device *dev = crtc->dev;
  3558. struct drm_i915_private *dev_priv = dev->dev_private;
  3559. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3560. struct intel_encoder *encoder;
  3561. int pipe = intel_crtc->pipe;
  3562. u32 reg, temp;
  3563. if (!intel_crtc->active)
  3564. return;
  3565. intel_crtc_disable_planes(crtc);
  3566. for_each_encoder_on_crtc(dev, crtc, encoder)
  3567. encoder->disable(encoder);
  3568. if (intel_crtc->config.has_pch_encoder)
  3569. intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
  3570. intel_disable_pipe(intel_crtc);
  3571. if (intel_crtc->config.dp_encoder_is_mst)
  3572. intel_ddi_set_vc_payload_alloc(crtc, false);
  3573. ironlake_pfit_disable(intel_crtc);
  3574. for_each_encoder_on_crtc(dev, crtc, encoder)
  3575. if (encoder->post_disable)
  3576. encoder->post_disable(encoder);
  3577. if (intel_crtc->config.has_pch_encoder) {
  3578. ironlake_fdi_disable(crtc);
  3579. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3580. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3581. if (HAS_PCH_CPT(dev)) {
  3582. /* disable TRANS_DP_CTL */
  3583. reg = TRANS_DP_CTL(pipe);
  3584. temp = I915_READ(reg);
  3585. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3586. TRANS_DP_PORT_SEL_MASK);
  3587. temp |= TRANS_DP_PORT_SEL_NONE;
  3588. I915_WRITE(reg, temp);
  3589. /* disable DPLL_SEL */
  3590. temp = I915_READ(PCH_DPLL_SEL);
  3591. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3592. I915_WRITE(PCH_DPLL_SEL, temp);
  3593. }
  3594. /* disable PCH DPLL */
  3595. intel_disable_shared_dpll(intel_crtc);
  3596. ironlake_fdi_pll_disable(intel_crtc);
  3597. }
  3598. intel_crtc->active = false;
  3599. intel_update_watermarks(crtc);
  3600. mutex_lock(&dev->struct_mutex);
  3601. intel_update_fbc(dev);
  3602. mutex_unlock(&dev->struct_mutex);
  3603. }
  3604. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3605. {
  3606. struct drm_device *dev = crtc->dev;
  3607. struct drm_i915_private *dev_priv = dev->dev_private;
  3608. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3609. struct intel_encoder *encoder;
  3610. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3611. if (!intel_crtc->active)
  3612. return;
  3613. intel_crtc_disable_planes(crtc);
  3614. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3615. intel_opregion_notify_encoder(encoder, false);
  3616. encoder->disable(encoder);
  3617. }
  3618. if (intel_crtc->config.has_pch_encoder)
  3619. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
  3620. intel_disable_pipe(intel_crtc);
  3621. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3622. ironlake_pfit_disable(intel_crtc);
  3623. intel_ddi_disable_pipe_clock(intel_crtc);
  3624. if (intel_crtc->config.has_pch_encoder) {
  3625. lpt_disable_pch_transcoder(dev_priv);
  3626. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3627. intel_ddi_fdi_disable(crtc);
  3628. }
  3629. for_each_encoder_on_crtc(dev, crtc, encoder)
  3630. if (encoder->post_disable)
  3631. encoder->post_disable(encoder);
  3632. intel_crtc->active = false;
  3633. intel_update_watermarks(crtc);
  3634. mutex_lock(&dev->struct_mutex);
  3635. intel_update_fbc(dev);
  3636. mutex_unlock(&dev->struct_mutex);
  3637. if (intel_crtc_to_shared_dpll(intel_crtc))
  3638. intel_disable_shared_dpll(intel_crtc);
  3639. }
  3640. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3641. {
  3642. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3643. intel_put_shared_dpll(intel_crtc);
  3644. }
  3645. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3646. {
  3647. struct drm_device *dev = crtc->base.dev;
  3648. struct drm_i915_private *dev_priv = dev->dev_private;
  3649. struct intel_crtc_config *pipe_config = &crtc->config;
  3650. if (!crtc->config.gmch_pfit.control)
  3651. return;
  3652. /*
  3653. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3654. * according to register description and PRM.
  3655. */
  3656. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3657. assert_pipe_disabled(dev_priv, crtc->pipe);
  3658. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3659. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3660. /* Border color in case we don't scale up to the full screen. Black by
  3661. * default, change to something else for debugging. */
  3662. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3663. }
  3664. static enum intel_display_power_domain port_to_power_domain(enum port port)
  3665. {
  3666. switch (port) {
  3667. case PORT_A:
  3668. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3669. case PORT_B:
  3670. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3671. case PORT_C:
  3672. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3673. case PORT_D:
  3674. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3675. default:
  3676. WARN_ON_ONCE(1);
  3677. return POWER_DOMAIN_PORT_OTHER;
  3678. }
  3679. }
  3680. #define for_each_power_domain(domain, mask) \
  3681. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3682. if ((1 << (domain)) & (mask))
  3683. enum intel_display_power_domain
  3684. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3685. {
  3686. struct drm_device *dev = intel_encoder->base.dev;
  3687. struct intel_digital_port *intel_dig_port;
  3688. switch (intel_encoder->type) {
  3689. case INTEL_OUTPUT_UNKNOWN:
  3690. /* Only DDI platforms should ever use this output type */
  3691. WARN_ON_ONCE(!HAS_DDI(dev));
  3692. case INTEL_OUTPUT_DISPLAYPORT:
  3693. case INTEL_OUTPUT_HDMI:
  3694. case INTEL_OUTPUT_EDP:
  3695. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3696. return port_to_power_domain(intel_dig_port->port);
  3697. case INTEL_OUTPUT_DP_MST:
  3698. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  3699. return port_to_power_domain(intel_dig_port->port);
  3700. case INTEL_OUTPUT_ANALOG:
  3701. return POWER_DOMAIN_PORT_CRT;
  3702. case INTEL_OUTPUT_DSI:
  3703. return POWER_DOMAIN_PORT_DSI;
  3704. default:
  3705. return POWER_DOMAIN_PORT_OTHER;
  3706. }
  3707. }
  3708. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3709. {
  3710. struct drm_device *dev = crtc->dev;
  3711. struct intel_encoder *intel_encoder;
  3712. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3713. enum pipe pipe = intel_crtc->pipe;
  3714. unsigned long mask;
  3715. enum transcoder transcoder;
  3716. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3717. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3718. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3719. if (intel_crtc->config.pch_pfit.enabled ||
  3720. intel_crtc->config.pch_pfit.force_thru)
  3721. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3722. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3723. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3724. return mask;
  3725. }
  3726. void intel_display_set_init_power(struct drm_i915_private *dev_priv,
  3727. bool enable)
  3728. {
  3729. if (dev_priv->power_domains.init_power_on == enable)
  3730. return;
  3731. if (enable)
  3732. intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
  3733. else
  3734. intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
  3735. dev_priv->power_domains.init_power_on = enable;
  3736. }
  3737. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3738. {
  3739. struct drm_i915_private *dev_priv = dev->dev_private;
  3740. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3741. struct intel_crtc *crtc;
  3742. /*
  3743. * First get all needed power domains, then put all unneeded, to avoid
  3744. * any unnecessary toggling of the power wells.
  3745. */
  3746. for_each_intel_crtc(dev, crtc) {
  3747. enum intel_display_power_domain domain;
  3748. if (!crtc->base.enabled)
  3749. continue;
  3750. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3751. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3752. intel_display_power_get(dev_priv, domain);
  3753. }
  3754. for_each_intel_crtc(dev, crtc) {
  3755. enum intel_display_power_domain domain;
  3756. for_each_power_domain(domain, crtc->enabled_power_domains)
  3757. intel_display_power_put(dev_priv, domain);
  3758. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  3759. }
  3760. intel_display_set_init_power(dev_priv, false);
  3761. }
  3762. /* returns HPLL frequency in kHz */
  3763. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  3764. {
  3765. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  3766. /* Obtain SKU information */
  3767. mutex_lock(&dev_priv->dpio_lock);
  3768. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  3769. CCK_FUSE_HPLL_FREQ_MASK;
  3770. mutex_unlock(&dev_priv->dpio_lock);
  3771. return vco_freq[hpll_freq] * 1000;
  3772. }
  3773. static void vlv_update_cdclk(struct drm_device *dev)
  3774. {
  3775. struct drm_i915_private *dev_priv = dev->dev_private;
  3776. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  3777. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz",
  3778. dev_priv->vlv_cdclk_freq);
  3779. /*
  3780. * Program the gmbus_freq based on the cdclk frequency.
  3781. * BSpec erroneously claims we should aim for 4MHz, but
  3782. * in fact 1MHz is the correct frequency.
  3783. */
  3784. I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
  3785. }
  3786. /* Adjust CDclk dividers to allow high res or save power if possible */
  3787. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  3788. {
  3789. struct drm_i915_private *dev_priv = dev->dev_private;
  3790. u32 val, cmd;
  3791. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  3792. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  3793. cmd = 2;
  3794. else if (cdclk == 266667)
  3795. cmd = 1;
  3796. else
  3797. cmd = 0;
  3798. mutex_lock(&dev_priv->rps.hw_lock);
  3799. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3800. val &= ~DSPFREQGUAR_MASK;
  3801. val |= (cmd << DSPFREQGUAR_SHIFT);
  3802. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3803. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3804. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  3805. 50)) {
  3806. DRM_ERROR("timed out waiting for CDclk change\n");
  3807. }
  3808. mutex_unlock(&dev_priv->rps.hw_lock);
  3809. if (cdclk == 400000) {
  3810. u32 divider, vco;
  3811. vco = valleyview_get_vco(dev_priv);
  3812. divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
  3813. mutex_lock(&dev_priv->dpio_lock);
  3814. /* adjust cdclk divider */
  3815. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3816. val &= ~DISPLAY_FREQUENCY_VALUES;
  3817. val |= divider;
  3818. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  3819. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  3820. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  3821. 50))
  3822. DRM_ERROR("timed out waiting for CDclk change\n");
  3823. mutex_unlock(&dev_priv->dpio_lock);
  3824. }
  3825. mutex_lock(&dev_priv->dpio_lock);
  3826. /* adjust self-refresh exit latency value */
  3827. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  3828. val &= ~0x7f;
  3829. /*
  3830. * For high bandwidth configs, we set a higher latency in the bunit
  3831. * so that the core display fetch happens in time to avoid underruns.
  3832. */
  3833. if (cdclk == 400000)
  3834. val |= 4500 / 250; /* 4.5 usec */
  3835. else
  3836. val |= 3000 / 250; /* 3.0 usec */
  3837. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  3838. mutex_unlock(&dev_priv->dpio_lock);
  3839. vlv_update_cdclk(dev);
  3840. }
  3841. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  3842. {
  3843. struct drm_i915_private *dev_priv = dev->dev_private;
  3844. u32 val, cmd;
  3845. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  3846. switch (cdclk) {
  3847. case 400000:
  3848. cmd = 3;
  3849. break;
  3850. case 333333:
  3851. case 320000:
  3852. cmd = 2;
  3853. break;
  3854. case 266667:
  3855. cmd = 1;
  3856. break;
  3857. case 200000:
  3858. cmd = 0;
  3859. break;
  3860. default:
  3861. WARN_ON(1);
  3862. return;
  3863. }
  3864. mutex_lock(&dev_priv->rps.hw_lock);
  3865. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3866. val &= ~DSPFREQGUAR_MASK_CHV;
  3867. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  3868. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3869. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3870. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  3871. 50)) {
  3872. DRM_ERROR("timed out waiting for CDclk change\n");
  3873. }
  3874. mutex_unlock(&dev_priv->rps.hw_lock);
  3875. vlv_update_cdclk(dev);
  3876. }
  3877. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  3878. int max_pixclk)
  3879. {
  3880. int vco = valleyview_get_vco(dev_priv);
  3881. int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
  3882. /* FIXME: Punit isn't quite ready yet */
  3883. if (IS_CHERRYVIEW(dev_priv->dev))
  3884. return 400000;
  3885. /*
  3886. * Really only a few cases to deal with, as only 4 CDclks are supported:
  3887. * 200MHz
  3888. * 267MHz
  3889. * 320/333MHz (depends on HPLL freq)
  3890. * 400MHz
  3891. * So we check to see whether we're above 90% of the lower bin and
  3892. * adjust if needed.
  3893. *
  3894. * We seem to get an unstable or solid color picture at 200MHz.
  3895. * Not sure what's wrong. For now use 200MHz only when all pipes
  3896. * are off.
  3897. */
  3898. if (max_pixclk > freq_320*9/10)
  3899. return 400000;
  3900. else if (max_pixclk > 266667*9/10)
  3901. return freq_320;
  3902. else if (max_pixclk > 0)
  3903. return 266667;
  3904. else
  3905. return 200000;
  3906. }
  3907. /* compute the max pixel clock for new configuration */
  3908. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  3909. {
  3910. struct drm_device *dev = dev_priv->dev;
  3911. struct intel_crtc *intel_crtc;
  3912. int max_pixclk = 0;
  3913. for_each_intel_crtc(dev, intel_crtc) {
  3914. if (intel_crtc->new_enabled)
  3915. max_pixclk = max(max_pixclk,
  3916. intel_crtc->new_config->adjusted_mode.crtc_clock);
  3917. }
  3918. return max_pixclk;
  3919. }
  3920. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  3921. unsigned *prepare_pipes)
  3922. {
  3923. struct drm_i915_private *dev_priv = dev->dev_private;
  3924. struct intel_crtc *intel_crtc;
  3925. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3926. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  3927. dev_priv->vlv_cdclk_freq)
  3928. return;
  3929. /* disable/enable all currently active pipes while we change cdclk */
  3930. for_each_intel_crtc(dev, intel_crtc)
  3931. if (intel_crtc->base.enabled)
  3932. *prepare_pipes |= (1 << intel_crtc->pipe);
  3933. }
  3934. static void valleyview_modeset_global_resources(struct drm_device *dev)
  3935. {
  3936. struct drm_i915_private *dev_priv = dev->dev_private;
  3937. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3938. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  3939. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  3940. if (IS_CHERRYVIEW(dev))
  3941. cherryview_set_cdclk(dev, req_cdclk);
  3942. else
  3943. valleyview_set_cdclk(dev, req_cdclk);
  3944. }
  3945. modeset_update_crtc_power_domains(dev);
  3946. }
  3947. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  3948. {
  3949. struct drm_device *dev = crtc->dev;
  3950. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3951. struct intel_encoder *encoder;
  3952. int pipe = intel_crtc->pipe;
  3953. bool is_dsi;
  3954. WARN_ON(!crtc->enabled);
  3955. if (intel_crtc->active)
  3956. return;
  3957. is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
  3958. if (!is_dsi) {
  3959. if (IS_CHERRYVIEW(dev))
  3960. chv_prepare_pll(intel_crtc);
  3961. else
  3962. vlv_prepare_pll(intel_crtc);
  3963. }
  3964. if (intel_crtc->config.has_dp_encoder)
  3965. intel_dp_set_m_n(intel_crtc);
  3966. intel_set_pipe_timings(intel_crtc);
  3967. i9xx_set_pipeconf(intel_crtc);
  3968. intel_crtc->active = true;
  3969. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3970. for_each_encoder_on_crtc(dev, crtc, encoder)
  3971. if (encoder->pre_pll_enable)
  3972. encoder->pre_pll_enable(encoder);
  3973. if (!is_dsi) {
  3974. if (IS_CHERRYVIEW(dev))
  3975. chv_enable_pll(intel_crtc);
  3976. else
  3977. vlv_enable_pll(intel_crtc);
  3978. }
  3979. for_each_encoder_on_crtc(dev, crtc, encoder)
  3980. if (encoder->pre_enable)
  3981. encoder->pre_enable(encoder);
  3982. i9xx_pfit_enable(intel_crtc);
  3983. intel_crtc_load_lut(crtc);
  3984. intel_update_watermarks(crtc);
  3985. intel_enable_pipe(intel_crtc);
  3986. for_each_encoder_on_crtc(dev, crtc, encoder)
  3987. encoder->enable(encoder);
  3988. intel_crtc_enable_planes(crtc);
  3989. /* Underruns don't raise interrupts, so check manually. */
  3990. i9xx_check_fifo_underruns(dev);
  3991. }
  3992. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  3993. {
  3994. struct drm_device *dev = crtc->base.dev;
  3995. struct drm_i915_private *dev_priv = dev->dev_private;
  3996. I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
  3997. I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
  3998. }
  3999. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4000. {
  4001. struct drm_device *dev = crtc->dev;
  4002. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4003. struct intel_encoder *encoder;
  4004. int pipe = intel_crtc->pipe;
  4005. WARN_ON(!crtc->enabled);
  4006. if (intel_crtc->active)
  4007. return;
  4008. i9xx_set_pll_dividers(intel_crtc);
  4009. if (intel_crtc->config.has_dp_encoder)
  4010. intel_dp_set_m_n(intel_crtc);
  4011. intel_set_pipe_timings(intel_crtc);
  4012. i9xx_set_pipeconf(intel_crtc);
  4013. intel_crtc->active = true;
  4014. if (!IS_GEN2(dev))
  4015. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  4016. for_each_encoder_on_crtc(dev, crtc, encoder)
  4017. if (encoder->pre_enable)
  4018. encoder->pre_enable(encoder);
  4019. i9xx_enable_pll(intel_crtc);
  4020. i9xx_pfit_enable(intel_crtc);
  4021. intel_crtc_load_lut(crtc);
  4022. intel_update_watermarks(crtc);
  4023. intel_enable_pipe(intel_crtc);
  4024. for_each_encoder_on_crtc(dev, crtc, encoder)
  4025. encoder->enable(encoder);
  4026. intel_crtc_enable_planes(crtc);
  4027. /*
  4028. * Gen2 reports pipe underruns whenever all planes are disabled.
  4029. * So don't enable underrun reporting before at least some planes
  4030. * are enabled.
  4031. * FIXME: Need to fix the logic to work when we turn off all planes
  4032. * but leave the pipe running.
  4033. */
  4034. if (IS_GEN2(dev))
  4035. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  4036. /* Underruns don't raise interrupts, so check manually. */
  4037. i9xx_check_fifo_underruns(dev);
  4038. }
  4039. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4040. {
  4041. struct drm_device *dev = crtc->base.dev;
  4042. struct drm_i915_private *dev_priv = dev->dev_private;
  4043. if (!crtc->config.gmch_pfit.control)
  4044. return;
  4045. assert_pipe_disabled(dev_priv, crtc->pipe);
  4046. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4047. I915_READ(PFIT_CONTROL));
  4048. I915_WRITE(PFIT_CONTROL, 0);
  4049. }
  4050. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4051. {
  4052. struct drm_device *dev = crtc->dev;
  4053. struct drm_i915_private *dev_priv = dev->dev_private;
  4054. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4055. struct intel_encoder *encoder;
  4056. int pipe = intel_crtc->pipe;
  4057. if (!intel_crtc->active)
  4058. return;
  4059. /*
  4060. * Gen2 reports pipe underruns whenever all planes are disabled.
  4061. * So diasble underrun reporting before all the planes get disabled.
  4062. * FIXME: Need to fix the logic to work when we turn off all planes
  4063. * but leave the pipe running.
  4064. */
  4065. if (IS_GEN2(dev))
  4066. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  4067. /*
  4068. * Vblank time updates from the shadow to live plane control register
  4069. * are blocked if the memory self-refresh mode is active at that
  4070. * moment. So to make sure the plane gets truly disabled, disable
  4071. * first the self-refresh mode. The self-refresh enable bit in turn
  4072. * will be checked/applied by the HW only at the next frame start
  4073. * event which is after the vblank start event, so we need to have a
  4074. * wait-for-vblank between disabling the plane and the pipe.
  4075. */
  4076. intel_set_memory_cxsr(dev_priv, false);
  4077. intel_crtc_disable_planes(crtc);
  4078. for_each_encoder_on_crtc(dev, crtc, encoder)
  4079. encoder->disable(encoder);
  4080. /*
  4081. * On gen2 planes are double buffered but the pipe isn't, so we must
  4082. * wait for planes to fully turn off before disabling the pipe.
  4083. * We also need to wait on all gmch platforms because of the
  4084. * self-refresh mode constraint explained above.
  4085. */
  4086. intel_wait_for_vblank(dev, pipe);
  4087. intel_disable_pipe(intel_crtc);
  4088. i9xx_pfit_disable(intel_crtc);
  4089. for_each_encoder_on_crtc(dev, crtc, encoder)
  4090. if (encoder->post_disable)
  4091. encoder->post_disable(encoder);
  4092. if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
  4093. if (IS_CHERRYVIEW(dev))
  4094. chv_disable_pll(dev_priv, pipe);
  4095. else if (IS_VALLEYVIEW(dev))
  4096. vlv_disable_pll(dev_priv, pipe);
  4097. else
  4098. i9xx_disable_pll(dev_priv, pipe);
  4099. }
  4100. if (!IS_GEN2(dev))
  4101. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  4102. intel_crtc->active = false;
  4103. intel_update_watermarks(crtc);
  4104. mutex_lock(&dev->struct_mutex);
  4105. intel_update_fbc(dev);
  4106. mutex_unlock(&dev->struct_mutex);
  4107. }
  4108. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4109. {
  4110. }
  4111. static void intel_crtc_update_sarea(struct drm_crtc *crtc,
  4112. bool enabled)
  4113. {
  4114. struct drm_device *dev = crtc->dev;
  4115. struct drm_i915_master_private *master_priv;
  4116. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4117. int pipe = intel_crtc->pipe;
  4118. if (!dev->primary->master)
  4119. return;
  4120. master_priv = dev->primary->master->driver_priv;
  4121. if (!master_priv->sarea_priv)
  4122. return;
  4123. switch (pipe) {
  4124. case 0:
  4125. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  4126. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  4127. break;
  4128. case 1:
  4129. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  4130. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  4131. break;
  4132. default:
  4133. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  4134. break;
  4135. }
  4136. }
  4137. /* Master function to enable/disable CRTC and corresponding power wells */
  4138. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4139. {
  4140. struct drm_device *dev = crtc->dev;
  4141. struct drm_i915_private *dev_priv = dev->dev_private;
  4142. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4143. enum intel_display_power_domain domain;
  4144. unsigned long domains;
  4145. if (enable) {
  4146. if (!intel_crtc->active) {
  4147. domains = get_crtc_power_domains(crtc);
  4148. for_each_power_domain(domain, domains)
  4149. intel_display_power_get(dev_priv, domain);
  4150. intel_crtc->enabled_power_domains = domains;
  4151. dev_priv->display.crtc_enable(crtc);
  4152. }
  4153. } else {
  4154. if (intel_crtc->active) {
  4155. dev_priv->display.crtc_disable(crtc);
  4156. domains = intel_crtc->enabled_power_domains;
  4157. for_each_power_domain(domain, domains)
  4158. intel_display_power_put(dev_priv, domain);
  4159. intel_crtc->enabled_power_domains = 0;
  4160. }
  4161. }
  4162. }
  4163. /**
  4164. * Sets the power management mode of the pipe and plane.
  4165. */
  4166. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4167. {
  4168. struct drm_device *dev = crtc->dev;
  4169. struct intel_encoder *intel_encoder;
  4170. bool enable = false;
  4171. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4172. enable |= intel_encoder->connectors_active;
  4173. intel_crtc_control(crtc, enable);
  4174. intel_crtc_update_sarea(crtc, enable);
  4175. }
  4176. static void intel_crtc_disable(struct drm_crtc *crtc)
  4177. {
  4178. struct drm_device *dev = crtc->dev;
  4179. struct drm_connector *connector;
  4180. struct drm_i915_private *dev_priv = dev->dev_private;
  4181. struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
  4182. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  4183. /* crtc should still be enabled when we disable it. */
  4184. WARN_ON(!crtc->enabled);
  4185. dev_priv->display.crtc_disable(crtc);
  4186. intel_crtc_update_sarea(crtc, false);
  4187. dev_priv->display.off(crtc);
  4188. if (crtc->primary->fb) {
  4189. mutex_lock(&dev->struct_mutex);
  4190. intel_unpin_fb_obj(old_obj);
  4191. i915_gem_track_fb(old_obj, NULL,
  4192. INTEL_FRONTBUFFER_PRIMARY(pipe));
  4193. mutex_unlock(&dev->struct_mutex);
  4194. crtc->primary->fb = NULL;
  4195. }
  4196. /* Update computed state. */
  4197. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4198. if (!connector->encoder || !connector->encoder->crtc)
  4199. continue;
  4200. if (connector->encoder->crtc != crtc)
  4201. continue;
  4202. connector->dpms = DRM_MODE_DPMS_OFF;
  4203. to_intel_encoder(connector->encoder)->connectors_active = false;
  4204. }
  4205. }
  4206. void intel_encoder_destroy(struct drm_encoder *encoder)
  4207. {
  4208. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4209. drm_encoder_cleanup(encoder);
  4210. kfree(intel_encoder);
  4211. }
  4212. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4213. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4214. * state of the entire output pipe. */
  4215. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4216. {
  4217. if (mode == DRM_MODE_DPMS_ON) {
  4218. encoder->connectors_active = true;
  4219. intel_crtc_update_dpms(encoder->base.crtc);
  4220. } else {
  4221. encoder->connectors_active = false;
  4222. intel_crtc_update_dpms(encoder->base.crtc);
  4223. }
  4224. }
  4225. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4226. * internal consistency). */
  4227. static void intel_connector_check_state(struct intel_connector *connector)
  4228. {
  4229. if (connector->get_hw_state(connector)) {
  4230. struct intel_encoder *encoder = connector->encoder;
  4231. struct drm_crtc *crtc;
  4232. bool encoder_enabled;
  4233. enum pipe pipe;
  4234. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4235. connector->base.base.id,
  4236. connector->base.name);
  4237. /* there is no real hw state for MST connectors */
  4238. if (connector->mst_port)
  4239. return;
  4240. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4241. "wrong connector dpms state\n");
  4242. WARN(connector->base.encoder != &encoder->base,
  4243. "active connector not linked to encoder\n");
  4244. if (encoder) {
  4245. WARN(!encoder->connectors_active,
  4246. "encoder->connectors_active not set\n");
  4247. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4248. WARN(!encoder_enabled, "encoder not enabled\n");
  4249. if (WARN_ON(!encoder->base.crtc))
  4250. return;
  4251. crtc = encoder->base.crtc;
  4252. WARN(!crtc->enabled, "crtc not enabled\n");
  4253. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4254. WARN(pipe != to_intel_crtc(crtc)->pipe,
  4255. "encoder active on the wrong pipe\n");
  4256. }
  4257. }
  4258. }
  4259. /* Even simpler default implementation, if there's really no special case to
  4260. * consider. */
  4261. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4262. {
  4263. /* All the simple cases only support two dpms states. */
  4264. if (mode != DRM_MODE_DPMS_ON)
  4265. mode = DRM_MODE_DPMS_OFF;
  4266. if (mode == connector->dpms)
  4267. return;
  4268. connector->dpms = mode;
  4269. /* Only need to change hw state when actually enabled */
  4270. if (connector->encoder)
  4271. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4272. intel_modeset_check_state(connector->dev);
  4273. }
  4274. /* Simple connector->get_hw_state implementation for encoders that support only
  4275. * one connector and no cloning and hence the encoder state determines the state
  4276. * of the connector. */
  4277. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4278. {
  4279. enum pipe pipe = 0;
  4280. struct intel_encoder *encoder = connector->encoder;
  4281. return encoder->get_hw_state(encoder, &pipe);
  4282. }
  4283. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4284. struct intel_crtc_config *pipe_config)
  4285. {
  4286. struct drm_i915_private *dev_priv = dev->dev_private;
  4287. struct intel_crtc *pipe_B_crtc =
  4288. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4289. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4290. pipe_name(pipe), pipe_config->fdi_lanes);
  4291. if (pipe_config->fdi_lanes > 4) {
  4292. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4293. pipe_name(pipe), pipe_config->fdi_lanes);
  4294. return false;
  4295. }
  4296. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4297. if (pipe_config->fdi_lanes > 2) {
  4298. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4299. pipe_config->fdi_lanes);
  4300. return false;
  4301. } else {
  4302. return true;
  4303. }
  4304. }
  4305. if (INTEL_INFO(dev)->num_pipes == 2)
  4306. return true;
  4307. /* Ivybridge 3 pipe is really complicated */
  4308. switch (pipe) {
  4309. case PIPE_A:
  4310. return true;
  4311. case PIPE_B:
  4312. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4313. pipe_config->fdi_lanes > 2) {
  4314. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4315. pipe_name(pipe), pipe_config->fdi_lanes);
  4316. return false;
  4317. }
  4318. return true;
  4319. case PIPE_C:
  4320. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4321. pipe_B_crtc->config.fdi_lanes <= 2) {
  4322. if (pipe_config->fdi_lanes > 2) {
  4323. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4324. pipe_name(pipe), pipe_config->fdi_lanes);
  4325. return false;
  4326. }
  4327. } else {
  4328. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4329. return false;
  4330. }
  4331. return true;
  4332. default:
  4333. BUG();
  4334. }
  4335. }
  4336. #define RETRY 1
  4337. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4338. struct intel_crtc_config *pipe_config)
  4339. {
  4340. struct drm_device *dev = intel_crtc->base.dev;
  4341. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4342. int lane, link_bw, fdi_dotclock;
  4343. bool setup_ok, needs_recompute = false;
  4344. retry:
  4345. /* FDI is a binary signal running at ~2.7GHz, encoding
  4346. * each output octet as 10 bits. The actual frequency
  4347. * is stored as a divider into a 100MHz clock, and the
  4348. * mode pixel clock is stored in units of 1KHz.
  4349. * Hence the bw of each lane in terms of the mode signal
  4350. * is:
  4351. */
  4352. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4353. fdi_dotclock = adjusted_mode->crtc_clock;
  4354. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4355. pipe_config->pipe_bpp);
  4356. pipe_config->fdi_lanes = lane;
  4357. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4358. link_bw, &pipe_config->fdi_m_n);
  4359. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4360. intel_crtc->pipe, pipe_config);
  4361. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4362. pipe_config->pipe_bpp -= 2*3;
  4363. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4364. pipe_config->pipe_bpp);
  4365. needs_recompute = true;
  4366. pipe_config->bw_constrained = true;
  4367. goto retry;
  4368. }
  4369. if (needs_recompute)
  4370. return RETRY;
  4371. return setup_ok ? 0 : -EINVAL;
  4372. }
  4373. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4374. struct intel_crtc_config *pipe_config)
  4375. {
  4376. pipe_config->ips_enabled = i915.enable_ips &&
  4377. hsw_crtc_supports_ips(crtc) &&
  4378. pipe_config->pipe_bpp <= 24;
  4379. }
  4380. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4381. struct intel_crtc_config *pipe_config)
  4382. {
  4383. struct drm_device *dev = crtc->base.dev;
  4384. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4385. /* FIXME should check pixel clock limits on all platforms */
  4386. if (INTEL_INFO(dev)->gen < 4) {
  4387. struct drm_i915_private *dev_priv = dev->dev_private;
  4388. int clock_limit =
  4389. dev_priv->display.get_display_clock_speed(dev);
  4390. /*
  4391. * Enable pixel doubling when the dot clock
  4392. * is > 90% of the (display) core speed.
  4393. *
  4394. * GDG double wide on either pipe,
  4395. * otherwise pipe A only.
  4396. */
  4397. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4398. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4399. clock_limit *= 2;
  4400. pipe_config->double_wide = true;
  4401. }
  4402. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4403. return -EINVAL;
  4404. }
  4405. /*
  4406. * Pipe horizontal size must be even in:
  4407. * - DVO ganged mode
  4408. * - LVDS dual channel mode
  4409. * - Double wide pipe
  4410. */
  4411. if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4412. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4413. pipe_config->pipe_src_w &= ~1;
  4414. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4415. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4416. */
  4417. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4418. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4419. return -EINVAL;
  4420. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4421. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4422. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4423. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4424. * for lvds. */
  4425. pipe_config->pipe_bpp = 8*3;
  4426. }
  4427. if (HAS_IPS(dev))
  4428. hsw_compute_ips_config(crtc, pipe_config);
  4429. /*
  4430. * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
  4431. * old clock survives for now.
  4432. */
  4433. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
  4434. pipe_config->shared_dpll = crtc->config.shared_dpll;
  4435. if (pipe_config->has_pch_encoder)
  4436. return ironlake_fdi_compute_config(crtc, pipe_config);
  4437. return 0;
  4438. }
  4439. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4440. {
  4441. struct drm_i915_private *dev_priv = dev->dev_private;
  4442. int vco = valleyview_get_vco(dev_priv);
  4443. u32 val;
  4444. int divider;
  4445. /* FIXME: Punit isn't quite ready yet */
  4446. if (IS_CHERRYVIEW(dev))
  4447. return 400000;
  4448. mutex_lock(&dev_priv->dpio_lock);
  4449. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4450. mutex_unlock(&dev_priv->dpio_lock);
  4451. divider = val & DISPLAY_FREQUENCY_VALUES;
  4452. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4453. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4454. "cdclk change in progress\n");
  4455. return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
  4456. }
  4457. static int i945_get_display_clock_speed(struct drm_device *dev)
  4458. {
  4459. return 400000;
  4460. }
  4461. static int i915_get_display_clock_speed(struct drm_device *dev)
  4462. {
  4463. return 333000;
  4464. }
  4465. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4466. {
  4467. return 200000;
  4468. }
  4469. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4470. {
  4471. u16 gcfgc = 0;
  4472. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4473. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4474. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4475. return 267000;
  4476. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4477. return 333000;
  4478. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4479. return 444000;
  4480. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4481. return 200000;
  4482. default:
  4483. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4484. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4485. return 133000;
  4486. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4487. return 167000;
  4488. }
  4489. }
  4490. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4491. {
  4492. u16 gcfgc = 0;
  4493. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4494. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4495. return 133000;
  4496. else {
  4497. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4498. case GC_DISPLAY_CLOCK_333_MHZ:
  4499. return 333000;
  4500. default:
  4501. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4502. return 190000;
  4503. }
  4504. }
  4505. }
  4506. static int i865_get_display_clock_speed(struct drm_device *dev)
  4507. {
  4508. return 266000;
  4509. }
  4510. static int i855_get_display_clock_speed(struct drm_device *dev)
  4511. {
  4512. u16 hpllcc = 0;
  4513. /* Assume that the hardware is in the high speed state. This
  4514. * should be the default.
  4515. */
  4516. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4517. case GC_CLOCK_133_200:
  4518. case GC_CLOCK_100_200:
  4519. return 200000;
  4520. case GC_CLOCK_166_250:
  4521. return 250000;
  4522. case GC_CLOCK_100_133:
  4523. return 133000;
  4524. }
  4525. /* Shouldn't happen */
  4526. return 0;
  4527. }
  4528. static int i830_get_display_clock_speed(struct drm_device *dev)
  4529. {
  4530. return 133000;
  4531. }
  4532. static void
  4533. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4534. {
  4535. while (*num > DATA_LINK_M_N_MASK ||
  4536. *den > DATA_LINK_M_N_MASK) {
  4537. *num >>= 1;
  4538. *den >>= 1;
  4539. }
  4540. }
  4541. static void compute_m_n(unsigned int m, unsigned int n,
  4542. uint32_t *ret_m, uint32_t *ret_n)
  4543. {
  4544. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4545. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4546. intel_reduce_m_n_ratio(ret_m, ret_n);
  4547. }
  4548. void
  4549. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4550. int pixel_clock, int link_clock,
  4551. struct intel_link_m_n *m_n)
  4552. {
  4553. m_n->tu = 64;
  4554. compute_m_n(bits_per_pixel * pixel_clock,
  4555. link_clock * nlanes * 8,
  4556. &m_n->gmch_m, &m_n->gmch_n);
  4557. compute_m_n(pixel_clock, link_clock,
  4558. &m_n->link_m, &m_n->link_n);
  4559. }
  4560. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4561. {
  4562. if (i915.panel_use_ssc >= 0)
  4563. return i915.panel_use_ssc != 0;
  4564. return dev_priv->vbt.lvds_use_ssc
  4565. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4566. }
  4567. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  4568. {
  4569. struct drm_device *dev = crtc->dev;
  4570. struct drm_i915_private *dev_priv = dev->dev_private;
  4571. int refclk;
  4572. if (IS_VALLEYVIEW(dev)) {
  4573. refclk = 100000;
  4574. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4575. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4576. refclk = dev_priv->vbt.lvds_ssc_freq;
  4577. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4578. } else if (!IS_GEN2(dev)) {
  4579. refclk = 96000;
  4580. } else {
  4581. refclk = 48000;
  4582. }
  4583. return refclk;
  4584. }
  4585. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4586. {
  4587. return (1 << dpll->n) << 16 | dpll->m2;
  4588. }
  4589. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4590. {
  4591. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4592. }
  4593. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4594. intel_clock_t *reduced_clock)
  4595. {
  4596. struct drm_device *dev = crtc->base.dev;
  4597. u32 fp, fp2 = 0;
  4598. if (IS_PINEVIEW(dev)) {
  4599. fp = pnv_dpll_compute_fp(&crtc->config.dpll);
  4600. if (reduced_clock)
  4601. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4602. } else {
  4603. fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
  4604. if (reduced_clock)
  4605. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4606. }
  4607. crtc->config.dpll_hw_state.fp0 = fp;
  4608. crtc->lowfreq_avail = false;
  4609. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4610. reduced_clock && i915.powersave) {
  4611. crtc->config.dpll_hw_state.fp1 = fp2;
  4612. crtc->lowfreq_avail = true;
  4613. } else {
  4614. crtc->config.dpll_hw_state.fp1 = fp;
  4615. }
  4616. }
  4617. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4618. pipe)
  4619. {
  4620. u32 reg_val;
  4621. /*
  4622. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4623. * and set it to a reasonable value instead.
  4624. */
  4625. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4626. reg_val &= 0xffffff00;
  4627. reg_val |= 0x00000030;
  4628. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4629. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4630. reg_val &= 0x8cffffff;
  4631. reg_val = 0x8c000000;
  4632. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4633. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4634. reg_val &= 0xffffff00;
  4635. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4636. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4637. reg_val &= 0x00ffffff;
  4638. reg_val |= 0xb0000000;
  4639. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4640. }
  4641. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4642. struct intel_link_m_n *m_n)
  4643. {
  4644. struct drm_device *dev = crtc->base.dev;
  4645. struct drm_i915_private *dev_priv = dev->dev_private;
  4646. int pipe = crtc->pipe;
  4647. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4648. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4649. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4650. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4651. }
  4652. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4653. struct intel_link_m_n *m_n,
  4654. struct intel_link_m_n *m2_n2)
  4655. {
  4656. struct drm_device *dev = crtc->base.dev;
  4657. struct drm_i915_private *dev_priv = dev->dev_private;
  4658. int pipe = crtc->pipe;
  4659. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4660. if (INTEL_INFO(dev)->gen >= 5) {
  4661. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4662. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4663. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4664. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4665. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  4666. * for gen < 8) and if DRRS is supported (to make sure the
  4667. * registers are not unnecessarily accessed).
  4668. */
  4669. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  4670. crtc->config.has_drrs) {
  4671. I915_WRITE(PIPE_DATA_M2(transcoder),
  4672. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  4673. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  4674. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  4675. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  4676. }
  4677. } else {
  4678. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4679. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4680. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4681. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4682. }
  4683. }
  4684. void intel_dp_set_m_n(struct intel_crtc *crtc)
  4685. {
  4686. if (crtc->config.has_pch_encoder)
  4687. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4688. else
  4689. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
  4690. &crtc->config.dp_m2_n2);
  4691. }
  4692. static void vlv_update_pll(struct intel_crtc *crtc)
  4693. {
  4694. u32 dpll, dpll_md;
  4695. /*
  4696. * Enable DPIO clock input. We should never disable the reference
  4697. * clock for pipe B, since VGA hotplug / manual detection depends
  4698. * on it.
  4699. */
  4700. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4701. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4702. /* We should never disable this, set it here for state tracking */
  4703. if (crtc->pipe == PIPE_B)
  4704. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4705. dpll |= DPLL_VCO_ENABLE;
  4706. crtc->config.dpll_hw_state.dpll = dpll;
  4707. dpll_md = (crtc->config.pixel_multiplier - 1)
  4708. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4709. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4710. }
  4711. static void vlv_prepare_pll(struct intel_crtc *crtc)
  4712. {
  4713. struct drm_device *dev = crtc->base.dev;
  4714. struct drm_i915_private *dev_priv = dev->dev_private;
  4715. int pipe = crtc->pipe;
  4716. u32 mdiv;
  4717. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4718. u32 coreclk, reg_val;
  4719. mutex_lock(&dev_priv->dpio_lock);
  4720. bestn = crtc->config.dpll.n;
  4721. bestm1 = crtc->config.dpll.m1;
  4722. bestm2 = crtc->config.dpll.m2;
  4723. bestp1 = crtc->config.dpll.p1;
  4724. bestp2 = crtc->config.dpll.p2;
  4725. /* See eDP HDMI DPIO driver vbios notes doc */
  4726. /* PLL B needs special handling */
  4727. if (pipe == PIPE_B)
  4728. vlv_pllb_recal_opamp(dev_priv, pipe);
  4729. /* Set up Tx target for periodic Rcomp update */
  4730. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4731. /* Disable target IRef on PLL */
  4732. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4733. reg_val &= 0x00ffffff;
  4734. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4735. /* Disable fast lock */
  4736. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4737. /* Set idtafcrecal before PLL is enabled */
  4738. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4739. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4740. mdiv |= ((bestn << DPIO_N_SHIFT));
  4741. mdiv |= (1 << DPIO_K_SHIFT);
  4742. /*
  4743. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4744. * but we don't support that).
  4745. * Note: don't use the DAC post divider as it seems unstable.
  4746. */
  4747. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4748. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4749. mdiv |= DPIO_ENABLE_CALIBRATION;
  4750. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4751. /* Set HBR and RBR LPF coefficients */
  4752. if (crtc->config.port_clock == 162000 ||
  4753. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
  4754. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
  4755. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4756. 0x009f0003);
  4757. else
  4758. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4759. 0x00d0000f);
  4760. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
  4761. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
  4762. /* Use SSC source */
  4763. if (pipe == PIPE_A)
  4764. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4765. 0x0df40000);
  4766. else
  4767. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4768. 0x0df70000);
  4769. } else { /* HDMI or VGA */
  4770. /* Use bend source */
  4771. if (pipe == PIPE_A)
  4772. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4773. 0x0df70000);
  4774. else
  4775. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4776. 0x0df40000);
  4777. }
  4778. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  4779. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  4780. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
  4781. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
  4782. coreclk |= 0x01000000;
  4783. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  4784. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  4785. mutex_unlock(&dev_priv->dpio_lock);
  4786. }
  4787. static void chv_update_pll(struct intel_crtc *crtc)
  4788. {
  4789. crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  4790. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  4791. DPLL_VCO_ENABLE;
  4792. if (crtc->pipe != PIPE_A)
  4793. crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4794. crtc->config.dpll_hw_state.dpll_md =
  4795. (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4796. }
  4797. static void chv_prepare_pll(struct intel_crtc *crtc)
  4798. {
  4799. struct drm_device *dev = crtc->base.dev;
  4800. struct drm_i915_private *dev_priv = dev->dev_private;
  4801. int pipe = crtc->pipe;
  4802. int dpll_reg = DPLL(crtc->pipe);
  4803. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  4804. u32 loopfilter, intcoeff;
  4805. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  4806. int refclk;
  4807. bestn = crtc->config.dpll.n;
  4808. bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
  4809. bestm1 = crtc->config.dpll.m1;
  4810. bestm2 = crtc->config.dpll.m2 >> 22;
  4811. bestp1 = crtc->config.dpll.p1;
  4812. bestp2 = crtc->config.dpll.p2;
  4813. /*
  4814. * Enable Refclk and SSC
  4815. */
  4816. I915_WRITE(dpll_reg,
  4817. crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  4818. mutex_lock(&dev_priv->dpio_lock);
  4819. /* p1 and p2 divider */
  4820. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  4821. 5 << DPIO_CHV_S1_DIV_SHIFT |
  4822. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  4823. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  4824. 1 << DPIO_CHV_K_DIV_SHIFT);
  4825. /* Feedback post-divider - m2 */
  4826. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  4827. /* Feedback refclk divider - n and m1 */
  4828. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  4829. DPIO_CHV_M1_DIV_BY_2 |
  4830. 1 << DPIO_CHV_N_DIV_SHIFT);
  4831. /* M2 fraction division */
  4832. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  4833. /* M2 fraction division enable */
  4834. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  4835. DPIO_CHV_FRAC_DIV_EN |
  4836. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  4837. /* Loop filter */
  4838. refclk = i9xx_get_refclk(&crtc->base, 0);
  4839. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  4840. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  4841. if (refclk == 100000)
  4842. intcoeff = 11;
  4843. else if (refclk == 38400)
  4844. intcoeff = 10;
  4845. else
  4846. intcoeff = 9;
  4847. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  4848. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  4849. /* AFC Recal */
  4850. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  4851. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  4852. DPIO_AFC_RECAL);
  4853. mutex_unlock(&dev_priv->dpio_lock);
  4854. }
  4855. static void i9xx_update_pll(struct intel_crtc *crtc,
  4856. intel_clock_t *reduced_clock,
  4857. int num_connectors)
  4858. {
  4859. struct drm_device *dev = crtc->base.dev;
  4860. struct drm_i915_private *dev_priv = dev->dev_private;
  4861. u32 dpll;
  4862. bool is_sdvo;
  4863. struct dpll *clock = &crtc->config.dpll;
  4864. i9xx_update_pll_dividers(crtc, reduced_clock);
  4865. is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
  4866. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  4867. dpll = DPLL_VGA_MODE_DIS;
  4868. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
  4869. dpll |= DPLLB_MODE_LVDS;
  4870. else
  4871. dpll |= DPLLB_MODE_DAC_SERIAL;
  4872. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  4873. dpll |= (crtc->config.pixel_multiplier - 1)
  4874. << SDVO_MULTIPLIER_SHIFT_HIRES;
  4875. }
  4876. if (is_sdvo)
  4877. dpll |= DPLL_SDVO_HIGH_SPEED;
  4878. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
  4879. dpll |= DPLL_SDVO_HIGH_SPEED;
  4880. /* compute bitmask from p1 value */
  4881. if (IS_PINEVIEW(dev))
  4882. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4883. else {
  4884. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4885. if (IS_G4X(dev) && reduced_clock)
  4886. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4887. }
  4888. switch (clock->p2) {
  4889. case 5:
  4890. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4891. break;
  4892. case 7:
  4893. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4894. break;
  4895. case 10:
  4896. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4897. break;
  4898. case 14:
  4899. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4900. break;
  4901. }
  4902. if (INTEL_INFO(dev)->gen >= 4)
  4903. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4904. if (crtc->config.sdvo_tv_clock)
  4905. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4906. else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4907. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4908. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4909. else
  4910. dpll |= PLL_REF_INPUT_DREFCLK;
  4911. dpll |= DPLL_VCO_ENABLE;
  4912. crtc->config.dpll_hw_state.dpll = dpll;
  4913. if (INTEL_INFO(dev)->gen >= 4) {
  4914. u32 dpll_md = (crtc->config.pixel_multiplier - 1)
  4915. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4916. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4917. }
  4918. }
  4919. static void i8xx_update_pll(struct intel_crtc *crtc,
  4920. intel_clock_t *reduced_clock,
  4921. int num_connectors)
  4922. {
  4923. struct drm_device *dev = crtc->base.dev;
  4924. struct drm_i915_private *dev_priv = dev->dev_private;
  4925. u32 dpll;
  4926. struct dpll *clock = &crtc->config.dpll;
  4927. i9xx_update_pll_dividers(crtc, reduced_clock);
  4928. dpll = DPLL_VGA_MODE_DIS;
  4929. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
  4930. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4931. } else {
  4932. if (clock->p1 == 2)
  4933. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4934. else
  4935. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4936. if (clock->p2 == 4)
  4937. dpll |= PLL_P2_DIVIDE_BY_4;
  4938. }
  4939. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
  4940. dpll |= DPLL_DVO_2X_MODE;
  4941. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4942. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4943. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4944. else
  4945. dpll |= PLL_REF_INPUT_DREFCLK;
  4946. dpll |= DPLL_VCO_ENABLE;
  4947. crtc->config.dpll_hw_state.dpll = dpll;
  4948. }
  4949. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  4950. {
  4951. struct drm_device *dev = intel_crtc->base.dev;
  4952. struct drm_i915_private *dev_priv = dev->dev_private;
  4953. enum pipe pipe = intel_crtc->pipe;
  4954. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  4955. struct drm_display_mode *adjusted_mode =
  4956. &intel_crtc->config.adjusted_mode;
  4957. uint32_t crtc_vtotal, crtc_vblank_end;
  4958. int vsyncshift = 0;
  4959. /* We need to be careful not to changed the adjusted mode, for otherwise
  4960. * the hw state checker will get angry at the mismatch. */
  4961. crtc_vtotal = adjusted_mode->crtc_vtotal;
  4962. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  4963. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4964. /* the chip adds 2 halflines automatically */
  4965. crtc_vtotal -= 1;
  4966. crtc_vblank_end -= 1;
  4967. if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  4968. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  4969. else
  4970. vsyncshift = adjusted_mode->crtc_hsync_start -
  4971. adjusted_mode->crtc_htotal / 2;
  4972. if (vsyncshift < 0)
  4973. vsyncshift += adjusted_mode->crtc_htotal;
  4974. }
  4975. if (INTEL_INFO(dev)->gen > 3)
  4976. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  4977. I915_WRITE(HTOTAL(cpu_transcoder),
  4978. (adjusted_mode->crtc_hdisplay - 1) |
  4979. ((adjusted_mode->crtc_htotal - 1) << 16));
  4980. I915_WRITE(HBLANK(cpu_transcoder),
  4981. (adjusted_mode->crtc_hblank_start - 1) |
  4982. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4983. I915_WRITE(HSYNC(cpu_transcoder),
  4984. (adjusted_mode->crtc_hsync_start - 1) |
  4985. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4986. I915_WRITE(VTOTAL(cpu_transcoder),
  4987. (adjusted_mode->crtc_vdisplay - 1) |
  4988. ((crtc_vtotal - 1) << 16));
  4989. I915_WRITE(VBLANK(cpu_transcoder),
  4990. (adjusted_mode->crtc_vblank_start - 1) |
  4991. ((crtc_vblank_end - 1) << 16));
  4992. I915_WRITE(VSYNC(cpu_transcoder),
  4993. (adjusted_mode->crtc_vsync_start - 1) |
  4994. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4995. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  4996. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  4997. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  4998. * bits. */
  4999. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5000. (pipe == PIPE_B || pipe == PIPE_C))
  5001. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5002. /* pipesrc controls the size that is scaled from, which should
  5003. * always be the user's requested size.
  5004. */
  5005. I915_WRITE(PIPESRC(pipe),
  5006. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  5007. (intel_crtc->config.pipe_src_h - 1));
  5008. }
  5009. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5010. struct intel_crtc_config *pipe_config)
  5011. {
  5012. struct drm_device *dev = crtc->base.dev;
  5013. struct drm_i915_private *dev_priv = dev->dev_private;
  5014. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5015. uint32_t tmp;
  5016. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5017. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5018. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5019. tmp = I915_READ(HBLANK(cpu_transcoder));
  5020. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5021. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5022. tmp = I915_READ(HSYNC(cpu_transcoder));
  5023. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5024. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5025. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5026. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5027. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5028. tmp = I915_READ(VBLANK(cpu_transcoder));
  5029. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5030. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5031. tmp = I915_READ(VSYNC(cpu_transcoder));
  5032. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5033. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5034. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5035. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5036. pipe_config->adjusted_mode.crtc_vtotal += 1;
  5037. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  5038. }
  5039. tmp = I915_READ(PIPESRC(crtc->pipe));
  5040. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5041. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5042. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  5043. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  5044. }
  5045. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5046. struct intel_crtc_config *pipe_config)
  5047. {
  5048. mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  5049. mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
  5050. mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  5051. mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  5052. mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  5053. mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  5054. mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  5055. mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  5056. mode->flags = pipe_config->adjusted_mode.flags;
  5057. mode->clock = pipe_config->adjusted_mode.crtc_clock;
  5058. mode->flags |= pipe_config->adjusted_mode.flags;
  5059. }
  5060. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5061. {
  5062. struct drm_device *dev = intel_crtc->base.dev;
  5063. struct drm_i915_private *dev_priv = dev->dev_private;
  5064. uint32_t pipeconf;
  5065. pipeconf = 0;
  5066. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5067. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5068. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5069. if (intel_crtc->config.double_wide)
  5070. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5071. /* only g4x and later have fancy bpc/dither controls */
  5072. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5073. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5074. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  5075. pipeconf |= PIPECONF_DITHER_EN |
  5076. PIPECONF_DITHER_TYPE_SP;
  5077. switch (intel_crtc->config.pipe_bpp) {
  5078. case 18:
  5079. pipeconf |= PIPECONF_6BPC;
  5080. break;
  5081. case 24:
  5082. pipeconf |= PIPECONF_8BPC;
  5083. break;
  5084. case 30:
  5085. pipeconf |= PIPECONF_10BPC;
  5086. break;
  5087. default:
  5088. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5089. BUG();
  5090. }
  5091. }
  5092. if (HAS_PIPE_CXSR(dev)) {
  5093. if (intel_crtc->lowfreq_avail) {
  5094. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5095. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5096. } else {
  5097. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5098. }
  5099. }
  5100. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5101. if (INTEL_INFO(dev)->gen < 4 ||
  5102. intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  5103. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5104. else
  5105. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5106. } else
  5107. pipeconf |= PIPECONF_PROGRESSIVE;
  5108. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  5109. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5110. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5111. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5112. }
  5113. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  5114. int x, int y,
  5115. struct drm_framebuffer *fb)
  5116. {
  5117. struct drm_device *dev = crtc->dev;
  5118. struct drm_i915_private *dev_priv = dev->dev_private;
  5119. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5120. int refclk, num_connectors = 0;
  5121. intel_clock_t clock, reduced_clock;
  5122. bool ok, has_reduced_clock = false;
  5123. bool is_lvds = false, is_dsi = false;
  5124. struct intel_encoder *encoder;
  5125. const intel_limit_t *limit;
  5126. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5127. switch (encoder->type) {
  5128. case INTEL_OUTPUT_LVDS:
  5129. is_lvds = true;
  5130. break;
  5131. case INTEL_OUTPUT_DSI:
  5132. is_dsi = true;
  5133. break;
  5134. }
  5135. num_connectors++;
  5136. }
  5137. if (is_dsi)
  5138. return 0;
  5139. if (!intel_crtc->config.clock_set) {
  5140. refclk = i9xx_get_refclk(crtc, num_connectors);
  5141. /*
  5142. * Returns a set of divisors for the desired target clock with
  5143. * the given refclk, or FALSE. The returned values represent
  5144. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5145. * 2) / p1 / p2.
  5146. */
  5147. limit = intel_limit(crtc, refclk);
  5148. ok = dev_priv->display.find_dpll(limit, crtc,
  5149. intel_crtc->config.port_clock,
  5150. refclk, NULL, &clock);
  5151. if (!ok) {
  5152. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5153. return -EINVAL;
  5154. }
  5155. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5156. /*
  5157. * Ensure we match the reduced clock's P to the target
  5158. * clock. If the clocks don't match, we can't switch
  5159. * the display clock by using the FP0/FP1. In such case
  5160. * we will disable the LVDS downclock feature.
  5161. */
  5162. has_reduced_clock =
  5163. dev_priv->display.find_dpll(limit, crtc,
  5164. dev_priv->lvds_downclock,
  5165. refclk, &clock,
  5166. &reduced_clock);
  5167. }
  5168. /* Compat-code for transition, will disappear. */
  5169. intel_crtc->config.dpll.n = clock.n;
  5170. intel_crtc->config.dpll.m1 = clock.m1;
  5171. intel_crtc->config.dpll.m2 = clock.m2;
  5172. intel_crtc->config.dpll.p1 = clock.p1;
  5173. intel_crtc->config.dpll.p2 = clock.p2;
  5174. }
  5175. if (IS_GEN2(dev)) {
  5176. i8xx_update_pll(intel_crtc,
  5177. has_reduced_clock ? &reduced_clock : NULL,
  5178. num_connectors);
  5179. } else if (IS_CHERRYVIEW(dev)) {
  5180. chv_update_pll(intel_crtc);
  5181. } else if (IS_VALLEYVIEW(dev)) {
  5182. vlv_update_pll(intel_crtc);
  5183. } else {
  5184. i9xx_update_pll(intel_crtc,
  5185. has_reduced_clock ? &reduced_clock : NULL,
  5186. num_connectors);
  5187. }
  5188. return 0;
  5189. }
  5190. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5191. struct intel_crtc_config *pipe_config)
  5192. {
  5193. struct drm_device *dev = crtc->base.dev;
  5194. struct drm_i915_private *dev_priv = dev->dev_private;
  5195. uint32_t tmp;
  5196. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5197. return;
  5198. tmp = I915_READ(PFIT_CONTROL);
  5199. if (!(tmp & PFIT_ENABLE))
  5200. return;
  5201. /* Check whether the pfit is attached to our pipe. */
  5202. if (INTEL_INFO(dev)->gen < 4) {
  5203. if (crtc->pipe != PIPE_B)
  5204. return;
  5205. } else {
  5206. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5207. return;
  5208. }
  5209. pipe_config->gmch_pfit.control = tmp;
  5210. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5211. if (INTEL_INFO(dev)->gen < 5)
  5212. pipe_config->gmch_pfit.lvds_border_bits =
  5213. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5214. }
  5215. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5216. struct intel_crtc_config *pipe_config)
  5217. {
  5218. struct drm_device *dev = crtc->base.dev;
  5219. struct drm_i915_private *dev_priv = dev->dev_private;
  5220. int pipe = pipe_config->cpu_transcoder;
  5221. intel_clock_t clock;
  5222. u32 mdiv;
  5223. int refclk = 100000;
  5224. /* In case of MIPI DPLL will not even be used */
  5225. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5226. return;
  5227. mutex_lock(&dev_priv->dpio_lock);
  5228. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5229. mutex_unlock(&dev_priv->dpio_lock);
  5230. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5231. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5232. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5233. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5234. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5235. vlv_clock(refclk, &clock);
  5236. /* clock.dot is the fast clock */
  5237. pipe_config->port_clock = clock.dot / 5;
  5238. }
  5239. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5240. struct intel_plane_config *plane_config)
  5241. {
  5242. struct drm_device *dev = crtc->base.dev;
  5243. struct drm_i915_private *dev_priv = dev->dev_private;
  5244. u32 val, base, offset;
  5245. int pipe = crtc->pipe, plane = crtc->plane;
  5246. int fourcc, pixel_format;
  5247. int aligned_height;
  5248. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5249. if (!crtc->base.primary->fb) {
  5250. DRM_DEBUG_KMS("failed to alloc fb\n");
  5251. return;
  5252. }
  5253. val = I915_READ(DSPCNTR(plane));
  5254. if (INTEL_INFO(dev)->gen >= 4)
  5255. if (val & DISPPLANE_TILED)
  5256. plane_config->tiled = true;
  5257. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5258. fourcc = intel_format_to_fourcc(pixel_format);
  5259. crtc->base.primary->fb->pixel_format = fourcc;
  5260. crtc->base.primary->fb->bits_per_pixel =
  5261. drm_format_plane_cpp(fourcc, 0) * 8;
  5262. if (INTEL_INFO(dev)->gen >= 4) {
  5263. if (plane_config->tiled)
  5264. offset = I915_READ(DSPTILEOFF(plane));
  5265. else
  5266. offset = I915_READ(DSPLINOFF(plane));
  5267. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5268. } else {
  5269. base = I915_READ(DSPADDR(plane));
  5270. }
  5271. plane_config->base = base;
  5272. val = I915_READ(PIPESRC(pipe));
  5273. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5274. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5275. val = I915_READ(DSPSTRIDE(pipe));
  5276. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  5277. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5278. plane_config->tiled);
  5279. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  5280. aligned_height);
  5281. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5282. pipe, plane, crtc->base.primary->fb->width,
  5283. crtc->base.primary->fb->height,
  5284. crtc->base.primary->fb->bits_per_pixel, base,
  5285. crtc->base.primary->fb->pitches[0],
  5286. plane_config->size);
  5287. }
  5288. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5289. struct intel_crtc_config *pipe_config)
  5290. {
  5291. struct drm_device *dev = crtc->base.dev;
  5292. struct drm_i915_private *dev_priv = dev->dev_private;
  5293. int pipe = pipe_config->cpu_transcoder;
  5294. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5295. intel_clock_t clock;
  5296. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5297. int refclk = 100000;
  5298. mutex_lock(&dev_priv->dpio_lock);
  5299. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5300. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5301. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5302. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5303. mutex_unlock(&dev_priv->dpio_lock);
  5304. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5305. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5306. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5307. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5308. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5309. chv_clock(refclk, &clock);
  5310. /* clock.dot is the fast clock */
  5311. pipe_config->port_clock = clock.dot / 5;
  5312. }
  5313. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5314. struct intel_crtc_config *pipe_config)
  5315. {
  5316. struct drm_device *dev = crtc->base.dev;
  5317. struct drm_i915_private *dev_priv = dev->dev_private;
  5318. uint32_t tmp;
  5319. if (!intel_display_power_enabled(dev_priv,
  5320. POWER_DOMAIN_PIPE(crtc->pipe)))
  5321. return false;
  5322. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5323. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5324. tmp = I915_READ(PIPECONF(crtc->pipe));
  5325. if (!(tmp & PIPECONF_ENABLE))
  5326. return false;
  5327. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5328. switch (tmp & PIPECONF_BPC_MASK) {
  5329. case PIPECONF_6BPC:
  5330. pipe_config->pipe_bpp = 18;
  5331. break;
  5332. case PIPECONF_8BPC:
  5333. pipe_config->pipe_bpp = 24;
  5334. break;
  5335. case PIPECONF_10BPC:
  5336. pipe_config->pipe_bpp = 30;
  5337. break;
  5338. default:
  5339. break;
  5340. }
  5341. }
  5342. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5343. pipe_config->limited_color_range = true;
  5344. if (INTEL_INFO(dev)->gen < 4)
  5345. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5346. intel_get_pipe_timings(crtc, pipe_config);
  5347. i9xx_get_pfit_config(crtc, pipe_config);
  5348. if (INTEL_INFO(dev)->gen >= 4) {
  5349. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5350. pipe_config->pixel_multiplier =
  5351. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5352. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5353. pipe_config->dpll_hw_state.dpll_md = tmp;
  5354. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5355. tmp = I915_READ(DPLL(crtc->pipe));
  5356. pipe_config->pixel_multiplier =
  5357. ((tmp & SDVO_MULTIPLIER_MASK)
  5358. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5359. } else {
  5360. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5361. * port and will be fixed up in the encoder->get_config
  5362. * function. */
  5363. pipe_config->pixel_multiplier = 1;
  5364. }
  5365. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5366. if (!IS_VALLEYVIEW(dev)) {
  5367. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5368. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5369. } else {
  5370. /* Mask out read-only status bits. */
  5371. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5372. DPLL_PORTC_READY_MASK |
  5373. DPLL_PORTB_READY_MASK);
  5374. }
  5375. if (IS_CHERRYVIEW(dev))
  5376. chv_crtc_clock_get(crtc, pipe_config);
  5377. else if (IS_VALLEYVIEW(dev))
  5378. vlv_crtc_clock_get(crtc, pipe_config);
  5379. else
  5380. i9xx_crtc_clock_get(crtc, pipe_config);
  5381. return true;
  5382. }
  5383. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5384. {
  5385. struct drm_i915_private *dev_priv = dev->dev_private;
  5386. struct intel_encoder *encoder;
  5387. u32 val, final;
  5388. bool has_lvds = false;
  5389. bool has_cpu_edp = false;
  5390. bool has_panel = false;
  5391. bool has_ck505 = false;
  5392. bool can_ssc = false;
  5393. /* We need to take the global config into account */
  5394. for_each_intel_encoder(dev, encoder) {
  5395. switch (encoder->type) {
  5396. case INTEL_OUTPUT_LVDS:
  5397. has_panel = true;
  5398. has_lvds = true;
  5399. break;
  5400. case INTEL_OUTPUT_EDP:
  5401. has_panel = true;
  5402. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5403. has_cpu_edp = true;
  5404. break;
  5405. }
  5406. }
  5407. if (HAS_PCH_IBX(dev)) {
  5408. has_ck505 = dev_priv->vbt.display_clock_mode;
  5409. can_ssc = has_ck505;
  5410. } else {
  5411. has_ck505 = false;
  5412. can_ssc = true;
  5413. }
  5414. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5415. has_panel, has_lvds, has_ck505);
  5416. /* Ironlake: try to setup display ref clock before DPLL
  5417. * enabling. This is only under driver's control after
  5418. * PCH B stepping, previous chipset stepping should be
  5419. * ignoring this setting.
  5420. */
  5421. val = I915_READ(PCH_DREF_CONTROL);
  5422. /* As we must carefully and slowly disable/enable each source in turn,
  5423. * compute the final state we want first and check if we need to
  5424. * make any changes at all.
  5425. */
  5426. final = val;
  5427. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5428. if (has_ck505)
  5429. final |= DREF_NONSPREAD_CK505_ENABLE;
  5430. else
  5431. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5432. final &= ~DREF_SSC_SOURCE_MASK;
  5433. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5434. final &= ~DREF_SSC1_ENABLE;
  5435. if (has_panel) {
  5436. final |= DREF_SSC_SOURCE_ENABLE;
  5437. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5438. final |= DREF_SSC1_ENABLE;
  5439. if (has_cpu_edp) {
  5440. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5441. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5442. else
  5443. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5444. } else
  5445. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5446. } else {
  5447. final |= DREF_SSC_SOURCE_DISABLE;
  5448. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5449. }
  5450. if (final == val)
  5451. return;
  5452. /* Always enable nonspread source */
  5453. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5454. if (has_ck505)
  5455. val |= DREF_NONSPREAD_CK505_ENABLE;
  5456. else
  5457. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5458. if (has_panel) {
  5459. val &= ~DREF_SSC_SOURCE_MASK;
  5460. val |= DREF_SSC_SOURCE_ENABLE;
  5461. /* SSC must be turned on before enabling the CPU output */
  5462. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5463. DRM_DEBUG_KMS("Using SSC on panel\n");
  5464. val |= DREF_SSC1_ENABLE;
  5465. } else
  5466. val &= ~DREF_SSC1_ENABLE;
  5467. /* Get SSC going before enabling the outputs */
  5468. I915_WRITE(PCH_DREF_CONTROL, val);
  5469. POSTING_READ(PCH_DREF_CONTROL);
  5470. udelay(200);
  5471. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5472. /* Enable CPU source on CPU attached eDP */
  5473. if (has_cpu_edp) {
  5474. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5475. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5476. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5477. } else
  5478. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5479. } else
  5480. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5481. I915_WRITE(PCH_DREF_CONTROL, val);
  5482. POSTING_READ(PCH_DREF_CONTROL);
  5483. udelay(200);
  5484. } else {
  5485. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5486. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5487. /* Turn off CPU output */
  5488. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5489. I915_WRITE(PCH_DREF_CONTROL, val);
  5490. POSTING_READ(PCH_DREF_CONTROL);
  5491. udelay(200);
  5492. /* Turn off the SSC source */
  5493. val &= ~DREF_SSC_SOURCE_MASK;
  5494. val |= DREF_SSC_SOURCE_DISABLE;
  5495. /* Turn off SSC1 */
  5496. val &= ~DREF_SSC1_ENABLE;
  5497. I915_WRITE(PCH_DREF_CONTROL, val);
  5498. POSTING_READ(PCH_DREF_CONTROL);
  5499. udelay(200);
  5500. }
  5501. BUG_ON(val != final);
  5502. }
  5503. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5504. {
  5505. uint32_t tmp;
  5506. tmp = I915_READ(SOUTH_CHICKEN2);
  5507. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5508. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5509. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5510. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5511. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5512. tmp = I915_READ(SOUTH_CHICKEN2);
  5513. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5514. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5515. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5516. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5517. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5518. }
  5519. /* WaMPhyProgramming:hsw */
  5520. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5521. {
  5522. uint32_t tmp;
  5523. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5524. tmp &= ~(0xFF << 24);
  5525. tmp |= (0x12 << 24);
  5526. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5527. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5528. tmp |= (1 << 11);
  5529. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5530. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5531. tmp |= (1 << 11);
  5532. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5533. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5534. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5535. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5536. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5537. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5538. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5539. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5540. tmp &= ~(7 << 13);
  5541. tmp |= (5 << 13);
  5542. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5543. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5544. tmp &= ~(7 << 13);
  5545. tmp |= (5 << 13);
  5546. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5547. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5548. tmp &= ~0xFF;
  5549. tmp |= 0x1C;
  5550. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5551. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5552. tmp &= ~0xFF;
  5553. tmp |= 0x1C;
  5554. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5555. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5556. tmp &= ~(0xFF << 16);
  5557. tmp |= (0x1C << 16);
  5558. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5559. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5560. tmp &= ~(0xFF << 16);
  5561. tmp |= (0x1C << 16);
  5562. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5563. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5564. tmp |= (1 << 27);
  5565. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5566. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5567. tmp |= (1 << 27);
  5568. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5569. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5570. tmp &= ~(0xF << 28);
  5571. tmp |= (4 << 28);
  5572. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5573. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5574. tmp &= ~(0xF << 28);
  5575. tmp |= (4 << 28);
  5576. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5577. }
  5578. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5579. * Programming" based on the parameters passed:
  5580. * - Sequence to enable CLKOUT_DP
  5581. * - Sequence to enable CLKOUT_DP without spread
  5582. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5583. */
  5584. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5585. bool with_fdi)
  5586. {
  5587. struct drm_i915_private *dev_priv = dev->dev_private;
  5588. uint32_t reg, tmp;
  5589. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5590. with_spread = true;
  5591. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5592. with_fdi, "LP PCH doesn't have FDI\n"))
  5593. with_fdi = false;
  5594. mutex_lock(&dev_priv->dpio_lock);
  5595. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5596. tmp &= ~SBI_SSCCTL_DISABLE;
  5597. tmp |= SBI_SSCCTL_PATHALT;
  5598. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5599. udelay(24);
  5600. if (with_spread) {
  5601. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5602. tmp &= ~SBI_SSCCTL_PATHALT;
  5603. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5604. if (with_fdi) {
  5605. lpt_reset_fdi_mphy(dev_priv);
  5606. lpt_program_fdi_mphy(dev_priv);
  5607. }
  5608. }
  5609. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5610. SBI_GEN0 : SBI_DBUFF0;
  5611. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5612. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5613. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5614. mutex_unlock(&dev_priv->dpio_lock);
  5615. }
  5616. /* Sequence to disable CLKOUT_DP */
  5617. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5618. {
  5619. struct drm_i915_private *dev_priv = dev->dev_private;
  5620. uint32_t reg, tmp;
  5621. mutex_lock(&dev_priv->dpio_lock);
  5622. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5623. SBI_GEN0 : SBI_DBUFF0;
  5624. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5625. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5626. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5627. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5628. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5629. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5630. tmp |= SBI_SSCCTL_PATHALT;
  5631. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5632. udelay(32);
  5633. }
  5634. tmp |= SBI_SSCCTL_DISABLE;
  5635. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5636. }
  5637. mutex_unlock(&dev_priv->dpio_lock);
  5638. }
  5639. static void lpt_init_pch_refclk(struct drm_device *dev)
  5640. {
  5641. struct intel_encoder *encoder;
  5642. bool has_vga = false;
  5643. for_each_intel_encoder(dev, encoder) {
  5644. switch (encoder->type) {
  5645. case INTEL_OUTPUT_ANALOG:
  5646. has_vga = true;
  5647. break;
  5648. }
  5649. }
  5650. if (has_vga)
  5651. lpt_enable_clkout_dp(dev, true, true);
  5652. else
  5653. lpt_disable_clkout_dp(dev);
  5654. }
  5655. /*
  5656. * Initialize reference clocks when the driver loads
  5657. */
  5658. void intel_init_pch_refclk(struct drm_device *dev)
  5659. {
  5660. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5661. ironlake_init_pch_refclk(dev);
  5662. else if (HAS_PCH_LPT(dev))
  5663. lpt_init_pch_refclk(dev);
  5664. }
  5665. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5666. {
  5667. struct drm_device *dev = crtc->dev;
  5668. struct drm_i915_private *dev_priv = dev->dev_private;
  5669. struct intel_encoder *encoder;
  5670. int num_connectors = 0;
  5671. bool is_lvds = false;
  5672. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5673. switch (encoder->type) {
  5674. case INTEL_OUTPUT_LVDS:
  5675. is_lvds = true;
  5676. break;
  5677. }
  5678. num_connectors++;
  5679. }
  5680. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5681. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5682. dev_priv->vbt.lvds_ssc_freq);
  5683. return dev_priv->vbt.lvds_ssc_freq;
  5684. }
  5685. return 120000;
  5686. }
  5687. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5688. {
  5689. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5690. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5691. int pipe = intel_crtc->pipe;
  5692. uint32_t val;
  5693. val = 0;
  5694. switch (intel_crtc->config.pipe_bpp) {
  5695. case 18:
  5696. val |= PIPECONF_6BPC;
  5697. break;
  5698. case 24:
  5699. val |= PIPECONF_8BPC;
  5700. break;
  5701. case 30:
  5702. val |= PIPECONF_10BPC;
  5703. break;
  5704. case 36:
  5705. val |= PIPECONF_12BPC;
  5706. break;
  5707. default:
  5708. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5709. BUG();
  5710. }
  5711. if (intel_crtc->config.dither)
  5712. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5713. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5714. val |= PIPECONF_INTERLACED_ILK;
  5715. else
  5716. val |= PIPECONF_PROGRESSIVE;
  5717. if (intel_crtc->config.limited_color_range)
  5718. val |= PIPECONF_COLOR_RANGE_SELECT;
  5719. I915_WRITE(PIPECONF(pipe), val);
  5720. POSTING_READ(PIPECONF(pipe));
  5721. }
  5722. /*
  5723. * Set up the pipe CSC unit.
  5724. *
  5725. * Currently only full range RGB to limited range RGB conversion
  5726. * is supported, but eventually this should handle various
  5727. * RGB<->YCbCr scenarios as well.
  5728. */
  5729. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  5730. {
  5731. struct drm_device *dev = crtc->dev;
  5732. struct drm_i915_private *dev_priv = dev->dev_private;
  5733. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5734. int pipe = intel_crtc->pipe;
  5735. uint16_t coeff = 0x7800; /* 1.0 */
  5736. /*
  5737. * TODO: Check what kind of values actually come out of the pipe
  5738. * with these coeff/postoff values and adjust to get the best
  5739. * accuracy. Perhaps we even need to take the bpc value into
  5740. * consideration.
  5741. */
  5742. if (intel_crtc->config.limited_color_range)
  5743. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  5744. /*
  5745. * GY/GU and RY/RU should be the other way around according
  5746. * to BSpec, but reality doesn't agree. Just set them up in
  5747. * a way that results in the correct picture.
  5748. */
  5749. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  5750. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  5751. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  5752. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  5753. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  5754. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  5755. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  5756. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  5757. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  5758. if (INTEL_INFO(dev)->gen > 6) {
  5759. uint16_t postoff = 0;
  5760. if (intel_crtc->config.limited_color_range)
  5761. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  5762. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  5763. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  5764. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  5765. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  5766. } else {
  5767. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  5768. if (intel_crtc->config.limited_color_range)
  5769. mode |= CSC_BLACK_SCREEN_OFFSET;
  5770. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  5771. }
  5772. }
  5773. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  5774. {
  5775. struct drm_device *dev = crtc->dev;
  5776. struct drm_i915_private *dev_priv = dev->dev_private;
  5777. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5778. enum pipe pipe = intel_crtc->pipe;
  5779. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5780. uint32_t val;
  5781. val = 0;
  5782. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  5783. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5784. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5785. val |= PIPECONF_INTERLACED_ILK;
  5786. else
  5787. val |= PIPECONF_PROGRESSIVE;
  5788. I915_WRITE(PIPECONF(cpu_transcoder), val);
  5789. POSTING_READ(PIPECONF(cpu_transcoder));
  5790. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  5791. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  5792. if (IS_BROADWELL(dev)) {
  5793. val = 0;
  5794. switch (intel_crtc->config.pipe_bpp) {
  5795. case 18:
  5796. val |= PIPEMISC_DITHER_6_BPC;
  5797. break;
  5798. case 24:
  5799. val |= PIPEMISC_DITHER_8_BPC;
  5800. break;
  5801. case 30:
  5802. val |= PIPEMISC_DITHER_10_BPC;
  5803. break;
  5804. case 36:
  5805. val |= PIPEMISC_DITHER_12_BPC;
  5806. break;
  5807. default:
  5808. /* Case prevented by pipe_config_set_bpp. */
  5809. BUG();
  5810. }
  5811. if (intel_crtc->config.dither)
  5812. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  5813. I915_WRITE(PIPEMISC(pipe), val);
  5814. }
  5815. }
  5816. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  5817. intel_clock_t *clock,
  5818. bool *has_reduced_clock,
  5819. intel_clock_t *reduced_clock)
  5820. {
  5821. struct drm_device *dev = crtc->dev;
  5822. struct drm_i915_private *dev_priv = dev->dev_private;
  5823. struct intel_encoder *intel_encoder;
  5824. int refclk;
  5825. const intel_limit_t *limit;
  5826. bool ret, is_lvds = false;
  5827. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5828. switch (intel_encoder->type) {
  5829. case INTEL_OUTPUT_LVDS:
  5830. is_lvds = true;
  5831. break;
  5832. }
  5833. }
  5834. refclk = ironlake_get_refclk(crtc);
  5835. /*
  5836. * Returns a set of divisors for the desired target clock with the given
  5837. * refclk, or FALSE. The returned values represent the clock equation:
  5838. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  5839. */
  5840. limit = intel_limit(crtc, refclk);
  5841. ret = dev_priv->display.find_dpll(limit, crtc,
  5842. to_intel_crtc(crtc)->config.port_clock,
  5843. refclk, NULL, clock);
  5844. if (!ret)
  5845. return false;
  5846. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5847. /*
  5848. * Ensure we match the reduced clock's P to the target clock.
  5849. * If the clocks don't match, we can't switch the display clock
  5850. * by using the FP0/FP1. In such case we will disable the LVDS
  5851. * downclock feature.
  5852. */
  5853. *has_reduced_clock =
  5854. dev_priv->display.find_dpll(limit, crtc,
  5855. dev_priv->lvds_downclock,
  5856. refclk, clock,
  5857. reduced_clock);
  5858. }
  5859. return true;
  5860. }
  5861. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  5862. {
  5863. /*
  5864. * Account for spread spectrum to avoid
  5865. * oversubscribing the link. Max center spread
  5866. * is 2.5%; use 5% for safety's sake.
  5867. */
  5868. u32 bps = target_clock * bpp * 21 / 20;
  5869. return DIV_ROUND_UP(bps, link_bw * 8);
  5870. }
  5871. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  5872. {
  5873. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  5874. }
  5875. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  5876. u32 *fp,
  5877. intel_clock_t *reduced_clock, u32 *fp2)
  5878. {
  5879. struct drm_crtc *crtc = &intel_crtc->base;
  5880. struct drm_device *dev = crtc->dev;
  5881. struct drm_i915_private *dev_priv = dev->dev_private;
  5882. struct intel_encoder *intel_encoder;
  5883. uint32_t dpll;
  5884. int factor, num_connectors = 0;
  5885. bool is_lvds = false, is_sdvo = false;
  5886. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5887. switch (intel_encoder->type) {
  5888. case INTEL_OUTPUT_LVDS:
  5889. is_lvds = true;
  5890. break;
  5891. case INTEL_OUTPUT_SDVO:
  5892. case INTEL_OUTPUT_HDMI:
  5893. is_sdvo = true;
  5894. break;
  5895. }
  5896. num_connectors++;
  5897. }
  5898. /* Enable autotuning of the PLL clock (if permissible) */
  5899. factor = 21;
  5900. if (is_lvds) {
  5901. if ((intel_panel_use_ssc(dev_priv) &&
  5902. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  5903. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  5904. factor = 25;
  5905. } else if (intel_crtc->config.sdvo_tv_clock)
  5906. factor = 20;
  5907. if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
  5908. *fp |= FP_CB_TUNE;
  5909. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  5910. *fp2 |= FP_CB_TUNE;
  5911. dpll = 0;
  5912. if (is_lvds)
  5913. dpll |= DPLLB_MODE_LVDS;
  5914. else
  5915. dpll |= DPLLB_MODE_DAC_SERIAL;
  5916. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  5917. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  5918. if (is_sdvo)
  5919. dpll |= DPLL_SDVO_HIGH_SPEED;
  5920. if (intel_crtc->config.has_dp_encoder)
  5921. dpll |= DPLL_SDVO_HIGH_SPEED;
  5922. /* compute bitmask from p1 value */
  5923. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5924. /* also FPA1 */
  5925. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5926. switch (intel_crtc->config.dpll.p2) {
  5927. case 5:
  5928. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5929. break;
  5930. case 7:
  5931. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5932. break;
  5933. case 10:
  5934. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5935. break;
  5936. case 14:
  5937. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5938. break;
  5939. }
  5940. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5941. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5942. else
  5943. dpll |= PLL_REF_INPUT_DREFCLK;
  5944. return dpll | DPLL_VCO_ENABLE;
  5945. }
  5946. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  5947. int x, int y,
  5948. struct drm_framebuffer *fb)
  5949. {
  5950. struct drm_device *dev = crtc->dev;
  5951. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5952. int num_connectors = 0;
  5953. intel_clock_t clock, reduced_clock;
  5954. u32 dpll = 0, fp = 0, fp2 = 0;
  5955. bool ok, has_reduced_clock = false;
  5956. bool is_lvds = false;
  5957. struct intel_encoder *encoder;
  5958. struct intel_shared_dpll *pll;
  5959. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5960. switch (encoder->type) {
  5961. case INTEL_OUTPUT_LVDS:
  5962. is_lvds = true;
  5963. break;
  5964. }
  5965. num_connectors++;
  5966. }
  5967. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  5968. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  5969. ok = ironlake_compute_clocks(crtc, &clock,
  5970. &has_reduced_clock, &reduced_clock);
  5971. if (!ok && !intel_crtc->config.clock_set) {
  5972. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5973. return -EINVAL;
  5974. }
  5975. /* Compat-code for transition, will disappear. */
  5976. if (!intel_crtc->config.clock_set) {
  5977. intel_crtc->config.dpll.n = clock.n;
  5978. intel_crtc->config.dpll.m1 = clock.m1;
  5979. intel_crtc->config.dpll.m2 = clock.m2;
  5980. intel_crtc->config.dpll.p1 = clock.p1;
  5981. intel_crtc->config.dpll.p2 = clock.p2;
  5982. }
  5983. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  5984. if (intel_crtc->config.has_pch_encoder) {
  5985. fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
  5986. if (has_reduced_clock)
  5987. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  5988. dpll = ironlake_compute_dpll(intel_crtc,
  5989. &fp, &reduced_clock,
  5990. has_reduced_clock ? &fp2 : NULL);
  5991. intel_crtc->config.dpll_hw_state.dpll = dpll;
  5992. intel_crtc->config.dpll_hw_state.fp0 = fp;
  5993. if (has_reduced_clock)
  5994. intel_crtc->config.dpll_hw_state.fp1 = fp2;
  5995. else
  5996. intel_crtc->config.dpll_hw_state.fp1 = fp;
  5997. pll = intel_get_shared_dpll(intel_crtc);
  5998. if (pll == NULL) {
  5999. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6000. pipe_name(intel_crtc->pipe));
  6001. return -EINVAL;
  6002. }
  6003. } else
  6004. intel_put_shared_dpll(intel_crtc);
  6005. if (is_lvds && has_reduced_clock && i915.powersave)
  6006. intel_crtc->lowfreq_avail = true;
  6007. else
  6008. intel_crtc->lowfreq_avail = false;
  6009. return 0;
  6010. }
  6011. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6012. struct intel_link_m_n *m_n)
  6013. {
  6014. struct drm_device *dev = crtc->base.dev;
  6015. struct drm_i915_private *dev_priv = dev->dev_private;
  6016. enum pipe pipe = crtc->pipe;
  6017. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6018. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6019. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6020. & ~TU_SIZE_MASK;
  6021. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6022. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6023. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6024. }
  6025. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6026. enum transcoder transcoder,
  6027. struct intel_link_m_n *m_n,
  6028. struct intel_link_m_n *m2_n2)
  6029. {
  6030. struct drm_device *dev = crtc->base.dev;
  6031. struct drm_i915_private *dev_priv = dev->dev_private;
  6032. enum pipe pipe = crtc->pipe;
  6033. if (INTEL_INFO(dev)->gen >= 5) {
  6034. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6035. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6036. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6037. & ~TU_SIZE_MASK;
  6038. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6039. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6040. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6041. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6042. * gen < 8) and if DRRS is supported (to make sure the
  6043. * registers are not unnecessarily read).
  6044. */
  6045. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6046. crtc->config.has_drrs) {
  6047. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6048. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6049. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6050. & ~TU_SIZE_MASK;
  6051. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6052. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6053. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6054. }
  6055. } else {
  6056. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6057. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6058. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6059. & ~TU_SIZE_MASK;
  6060. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6061. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6062. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6063. }
  6064. }
  6065. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6066. struct intel_crtc_config *pipe_config)
  6067. {
  6068. if (crtc->config.has_pch_encoder)
  6069. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6070. else
  6071. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6072. &pipe_config->dp_m_n,
  6073. &pipe_config->dp_m2_n2);
  6074. }
  6075. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6076. struct intel_crtc_config *pipe_config)
  6077. {
  6078. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6079. &pipe_config->fdi_m_n, NULL);
  6080. }
  6081. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6082. struct intel_crtc_config *pipe_config)
  6083. {
  6084. struct drm_device *dev = crtc->base.dev;
  6085. struct drm_i915_private *dev_priv = dev->dev_private;
  6086. uint32_t tmp;
  6087. tmp = I915_READ(PF_CTL(crtc->pipe));
  6088. if (tmp & PF_ENABLE) {
  6089. pipe_config->pch_pfit.enabled = true;
  6090. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6091. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6092. /* We currently do not free assignements of panel fitters on
  6093. * ivb/hsw (since we don't use the higher upscaling modes which
  6094. * differentiates them) so just WARN about this case for now. */
  6095. if (IS_GEN7(dev)) {
  6096. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6097. PF_PIPE_SEL_IVB(crtc->pipe));
  6098. }
  6099. }
  6100. }
  6101. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  6102. struct intel_plane_config *plane_config)
  6103. {
  6104. struct drm_device *dev = crtc->base.dev;
  6105. struct drm_i915_private *dev_priv = dev->dev_private;
  6106. u32 val, base, offset;
  6107. int pipe = crtc->pipe, plane = crtc->plane;
  6108. int fourcc, pixel_format;
  6109. int aligned_height;
  6110. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  6111. if (!crtc->base.primary->fb) {
  6112. DRM_DEBUG_KMS("failed to alloc fb\n");
  6113. return;
  6114. }
  6115. val = I915_READ(DSPCNTR(plane));
  6116. if (INTEL_INFO(dev)->gen >= 4)
  6117. if (val & DISPPLANE_TILED)
  6118. plane_config->tiled = true;
  6119. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6120. fourcc = intel_format_to_fourcc(pixel_format);
  6121. crtc->base.primary->fb->pixel_format = fourcc;
  6122. crtc->base.primary->fb->bits_per_pixel =
  6123. drm_format_plane_cpp(fourcc, 0) * 8;
  6124. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6125. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6126. offset = I915_READ(DSPOFFSET(plane));
  6127. } else {
  6128. if (plane_config->tiled)
  6129. offset = I915_READ(DSPTILEOFF(plane));
  6130. else
  6131. offset = I915_READ(DSPLINOFF(plane));
  6132. }
  6133. plane_config->base = base;
  6134. val = I915_READ(PIPESRC(pipe));
  6135. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  6136. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  6137. val = I915_READ(DSPSTRIDE(pipe));
  6138. crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
  6139. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  6140. plane_config->tiled);
  6141. plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
  6142. aligned_height);
  6143. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6144. pipe, plane, crtc->base.primary->fb->width,
  6145. crtc->base.primary->fb->height,
  6146. crtc->base.primary->fb->bits_per_pixel, base,
  6147. crtc->base.primary->fb->pitches[0],
  6148. plane_config->size);
  6149. }
  6150. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6151. struct intel_crtc_config *pipe_config)
  6152. {
  6153. struct drm_device *dev = crtc->base.dev;
  6154. struct drm_i915_private *dev_priv = dev->dev_private;
  6155. uint32_t tmp;
  6156. if (!intel_display_power_enabled(dev_priv,
  6157. POWER_DOMAIN_PIPE(crtc->pipe)))
  6158. return false;
  6159. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6160. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6161. tmp = I915_READ(PIPECONF(crtc->pipe));
  6162. if (!(tmp & PIPECONF_ENABLE))
  6163. return false;
  6164. switch (tmp & PIPECONF_BPC_MASK) {
  6165. case PIPECONF_6BPC:
  6166. pipe_config->pipe_bpp = 18;
  6167. break;
  6168. case PIPECONF_8BPC:
  6169. pipe_config->pipe_bpp = 24;
  6170. break;
  6171. case PIPECONF_10BPC:
  6172. pipe_config->pipe_bpp = 30;
  6173. break;
  6174. case PIPECONF_12BPC:
  6175. pipe_config->pipe_bpp = 36;
  6176. break;
  6177. default:
  6178. break;
  6179. }
  6180. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6181. pipe_config->limited_color_range = true;
  6182. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6183. struct intel_shared_dpll *pll;
  6184. pipe_config->has_pch_encoder = true;
  6185. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6186. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6187. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6188. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6189. if (HAS_PCH_IBX(dev_priv->dev)) {
  6190. pipe_config->shared_dpll =
  6191. (enum intel_dpll_id) crtc->pipe;
  6192. } else {
  6193. tmp = I915_READ(PCH_DPLL_SEL);
  6194. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6195. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6196. else
  6197. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6198. }
  6199. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6200. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6201. &pipe_config->dpll_hw_state));
  6202. tmp = pipe_config->dpll_hw_state.dpll;
  6203. pipe_config->pixel_multiplier =
  6204. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6205. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6206. ironlake_pch_clock_get(crtc, pipe_config);
  6207. } else {
  6208. pipe_config->pixel_multiplier = 1;
  6209. }
  6210. intel_get_pipe_timings(crtc, pipe_config);
  6211. ironlake_get_pfit_config(crtc, pipe_config);
  6212. return true;
  6213. }
  6214. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6215. {
  6216. struct drm_device *dev = dev_priv->dev;
  6217. struct intel_crtc *crtc;
  6218. for_each_intel_crtc(dev, crtc)
  6219. WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6220. pipe_name(crtc->pipe));
  6221. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6222. WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6223. WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6224. WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6225. WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6226. WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6227. "CPU PWM1 enabled\n");
  6228. if (IS_HASWELL(dev))
  6229. WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6230. "CPU PWM2 enabled\n");
  6231. WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6232. "PCH PWM1 enabled\n");
  6233. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6234. "Utility pin enabled\n");
  6235. WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6236. /*
  6237. * In theory we can still leave IRQs enabled, as long as only the HPD
  6238. * interrupts remain enabled. We used to check for that, but since it's
  6239. * gen-specific and since we only disable LCPLL after we fully disable
  6240. * the interrupts, the check below should be enough.
  6241. */
  6242. WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6243. }
  6244. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6245. {
  6246. struct drm_device *dev = dev_priv->dev;
  6247. if (IS_HASWELL(dev))
  6248. return I915_READ(D_COMP_HSW);
  6249. else
  6250. return I915_READ(D_COMP_BDW);
  6251. }
  6252. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6253. {
  6254. struct drm_device *dev = dev_priv->dev;
  6255. if (IS_HASWELL(dev)) {
  6256. mutex_lock(&dev_priv->rps.hw_lock);
  6257. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6258. val))
  6259. DRM_ERROR("Failed to write to D_COMP\n");
  6260. mutex_unlock(&dev_priv->rps.hw_lock);
  6261. } else {
  6262. I915_WRITE(D_COMP_BDW, val);
  6263. POSTING_READ(D_COMP_BDW);
  6264. }
  6265. }
  6266. /*
  6267. * This function implements pieces of two sequences from BSpec:
  6268. * - Sequence for display software to disable LCPLL
  6269. * - Sequence for display software to allow package C8+
  6270. * The steps implemented here are just the steps that actually touch the LCPLL
  6271. * register. Callers should take care of disabling all the display engine
  6272. * functions, doing the mode unset, fixing interrupts, etc.
  6273. */
  6274. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6275. bool switch_to_fclk, bool allow_power_down)
  6276. {
  6277. uint32_t val;
  6278. assert_can_disable_lcpll(dev_priv);
  6279. val = I915_READ(LCPLL_CTL);
  6280. if (switch_to_fclk) {
  6281. val |= LCPLL_CD_SOURCE_FCLK;
  6282. I915_WRITE(LCPLL_CTL, val);
  6283. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6284. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6285. DRM_ERROR("Switching to FCLK failed\n");
  6286. val = I915_READ(LCPLL_CTL);
  6287. }
  6288. val |= LCPLL_PLL_DISABLE;
  6289. I915_WRITE(LCPLL_CTL, val);
  6290. POSTING_READ(LCPLL_CTL);
  6291. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6292. DRM_ERROR("LCPLL still locked\n");
  6293. val = hsw_read_dcomp(dev_priv);
  6294. val |= D_COMP_COMP_DISABLE;
  6295. hsw_write_dcomp(dev_priv, val);
  6296. ndelay(100);
  6297. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6298. 1))
  6299. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6300. if (allow_power_down) {
  6301. val = I915_READ(LCPLL_CTL);
  6302. val |= LCPLL_POWER_DOWN_ALLOW;
  6303. I915_WRITE(LCPLL_CTL, val);
  6304. POSTING_READ(LCPLL_CTL);
  6305. }
  6306. }
  6307. /*
  6308. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6309. * source.
  6310. */
  6311. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6312. {
  6313. uint32_t val;
  6314. unsigned long irqflags;
  6315. val = I915_READ(LCPLL_CTL);
  6316. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6317. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6318. return;
  6319. /*
  6320. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6321. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6322. *
  6323. * The other problem is that hsw_restore_lcpll() is called as part of
  6324. * the runtime PM resume sequence, so we can't just call
  6325. * gen6_gt_force_wake_get() because that function calls
  6326. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6327. * while we are on the resume sequence. So to solve this problem we have
  6328. * to call special forcewake code that doesn't touch runtime PM and
  6329. * doesn't enable the forcewake delayed work.
  6330. */
  6331. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6332. if (dev_priv->uncore.forcewake_count++ == 0)
  6333. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6334. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6335. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6336. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6337. I915_WRITE(LCPLL_CTL, val);
  6338. POSTING_READ(LCPLL_CTL);
  6339. }
  6340. val = hsw_read_dcomp(dev_priv);
  6341. val |= D_COMP_COMP_FORCE;
  6342. val &= ~D_COMP_COMP_DISABLE;
  6343. hsw_write_dcomp(dev_priv, val);
  6344. val = I915_READ(LCPLL_CTL);
  6345. val &= ~LCPLL_PLL_DISABLE;
  6346. I915_WRITE(LCPLL_CTL, val);
  6347. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6348. DRM_ERROR("LCPLL not locked yet\n");
  6349. if (val & LCPLL_CD_SOURCE_FCLK) {
  6350. val = I915_READ(LCPLL_CTL);
  6351. val &= ~LCPLL_CD_SOURCE_FCLK;
  6352. I915_WRITE(LCPLL_CTL, val);
  6353. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6354. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6355. DRM_ERROR("Switching back to LCPLL failed\n");
  6356. }
  6357. /* See the big comment above. */
  6358. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6359. if (--dev_priv->uncore.forcewake_count == 0)
  6360. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6361. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6362. }
  6363. /*
  6364. * Package states C8 and deeper are really deep PC states that can only be
  6365. * reached when all the devices on the system allow it, so even if the graphics
  6366. * device allows PC8+, it doesn't mean the system will actually get to these
  6367. * states. Our driver only allows PC8+ when going into runtime PM.
  6368. *
  6369. * The requirements for PC8+ are that all the outputs are disabled, the power
  6370. * well is disabled and most interrupts are disabled, and these are also
  6371. * requirements for runtime PM. When these conditions are met, we manually do
  6372. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6373. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6374. * hang the machine.
  6375. *
  6376. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6377. * the state of some registers, so when we come back from PC8+ we need to
  6378. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6379. * need to take care of the registers kept by RC6. Notice that this happens even
  6380. * if we don't put the device in PCI D3 state (which is what currently happens
  6381. * because of the runtime PM support).
  6382. *
  6383. * For more, read "Display Sequences for Package C8" on the hardware
  6384. * documentation.
  6385. */
  6386. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6387. {
  6388. struct drm_device *dev = dev_priv->dev;
  6389. uint32_t val;
  6390. DRM_DEBUG_KMS("Enabling package C8+\n");
  6391. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6392. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6393. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6394. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6395. }
  6396. lpt_disable_clkout_dp(dev);
  6397. hsw_disable_lcpll(dev_priv, true, true);
  6398. }
  6399. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6400. {
  6401. struct drm_device *dev = dev_priv->dev;
  6402. uint32_t val;
  6403. DRM_DEBUG_KMS("Disabling package C8+\n");
  6404. hsw_restore_lcpll(dev_priv);
  6405. lpt_init_pch_refclk(dev);
  6406. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6407. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6408. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6409. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6410. }
  6411. intel_prepare_ddi(dev);
  6412. }
  6413. static void snb_modeset_global_resources(struct drm_device *dev)
  6414. {
  6415. modeset_update_crtc_power_domains(dev);
  6416. }
  6417. static void haswell_modeset_global_resources(struct drm_device *dev)
  6418. {
  6419. modeset_update_crtc_power_domains(dev);
  6420. }
  6421. static int haswell_crtc_mode_set(struct drm_crtc *crtc,
  6422. int x, int y,
  6423. struct drm_framebuffer *fb)
  6424. {
  6425. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6426. if (!intel_ddi_pll_select(intel_crtc))
  6427. return -EINVAL;
  6428. intel_crtc->lowfreq_avail = false;
  6429. return 0;
  6430. }
  6431. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  6432. enum port port,
  6433. struct intel_crtc_config *pipe_config)
  6434. {
  6435. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6436. switch (pipe_config->ddi_pll_sel) {
  6437. case PORT_CLK_SEL_WRPLL1:
  6438. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6439. break;
  6440. case PORT_CLK_SEL_WRPLL2:
  6441. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6442. break;
  6443. }
  6444. }
  6445. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6446. struct intel_crtc_config *pipe_config)
  6447. {
  6448. struct drm_device *dev = crtc->base.dev;
  6449. struct drm_i915_private *dev_priv = dev->dev_private;
  6450. struct intel_shared_dpll *pll;
  6451. enum port port;
  6452. uint32_t tmp;
  6453. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6454. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  6455. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  6456. if (pipe_config->shared_dpll >= 0) {
  6457. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6458. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6459. &pipe_config->dpll_hw_state));
  6460. }
  6461. /*
  6462. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6463. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6464. * the PCH transcoder is on.
  6465. */
  6466. if ((port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6467. pipe_config->has_pch_encoder = true;
  6468. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6469. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6470. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6471. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6472. }
  6473. }
  6474. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6475. struct intel_crtc_config *pipe_config)
  6476. {
  6477. struct drm_device *dev = crtc->base.dev;
  6478. struct drm_i915_private *dev_priv = dev->dev_private;
  6479. enum intel_display_power_domain pfit_domain;
  6480. uint32_t tmp;
  6481. if (!intel_display_power_enabled(dev_priv,
  6482. POWER_DOMAIN_PIPE(crtc->pipe)))
  6483. return false;
  6484. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6485. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6486. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6487. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6488. enum pipe trans_edp_pipe;
  6489. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6490. default:
  6491. WARN(1, "unknown pipe linked to edp transcoder\n");
  6492. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6493. case TRANS_DDI_EDP_INPUT_A_ON:
  6494. trans_edp_pipe = PIPE_A;
  6495. break;
  6496. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6497. trans_edp_pipe = PIPE_B;
  6498. break;
  6499. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6500. trans_edp_pipe = PIPE_C;
  6501. break;
  6502. }
  6503. if (trans_edp_pipe == crtc->pipe)
  6504. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6505. }
  6506. if (!intel_display_power_enabled(dev_priv,
  6507. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6508. return false;
  6509. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6510. if (!(tmp & PIPECONF_ENABLE))
  6511. return false;
  6512. haswell_get_ddi_port_state(crtc, pipe_config);
  6513. intel_get_pipe_timings(crtc, pipe_config);
  6514. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6515. if (intel_display_power_enabled(dev_priv, pfit_domain))
  6516. ironlake_get_pfit_config(crtc, pipe_config);
  6517. if (IS_HASWELL(dev))
  6518. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6519. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6520. pipe_config->pixel_multiplier = 1;
  6521. return true;
  6522. }
  6523. static struct {
  6524. int clock;
  6525. u32 config;
  6526. } hdmi_audio_clock[] = {
  6527. { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  6528. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  6529. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  6530. { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  6531. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  6532. { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  6533. { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  6534. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  6535. { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  6536. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  6537. };
  6538. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  6539. static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
  6540. {
  6541. int i;
  6542. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  6543. if (mode->clock == hdmi_audio_clock[i].clock)
  6544. break;
  6545. }
  6546. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  6547. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
  6548. i = 1;
  6549. }
  6550. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  6551. hdmi_audio_clock[i].clock,
  6552. hdmi_audio_clock[i].config);
  6553. return hdmi_audio_clock[i].config;
  6554. }
  6555. static bool intel_eld_uptodate(struct drm_connector *connector,
  6556. int reg_eldv, uint32_t bits_eldv,
  6557. int reg_elda, uint32_t bits_elda,
  6558. int reg_edid)
  6559. {
  6560. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6561. uint8_t *eld = connector->eld;
  6562. uint32_t i;
  6563. i = I915_READ(reg_eldv);
  6564. i &= bits_eldv;
  6565. if (!eld[0])
  6566. return !i;
  6567. if (!i)
  6568. return false;
  6569. i = I915_READ(reg_elda);
  6570. i &= ~bits_elda;
  6571. I915_WRITE(reg_elda, i);
  6572. for (i = 0; i < eld[2]; i++)
  6573. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  6574. return false;
  6575. return true;
  6576. }
  6577. static void g4x_write_eld(struct drm_connector *connector,
  6578. struct drm_crtc *crtc,
  6579. struct drm_display_mode *mode)
  6580. {
  6581. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6582. uint8_t *eld = connector->eld;
  6583. uint32_t eldv;
  6584. uint32_t len;
  6585. uint32_t i;
  6586. i = I915_READ(G4X_AUD_VID_DID);
  6587. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  6588. eldv = G4X_ELDV_DEVCL_DEVBLC;
  6589. else
  6590. eldv = G4X_ELDV_DEVCTG;
  6591. if (intel_eld_uptodate(connector,
  6592. G4X_AUD_CNTL_ST, eldv,
  6593. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  6594. G4X_HDMIW_HDMIEDID))
  6595. return;
  6596. i = I915_READ(G4X_AUD_CNTL_ST);
  6597. i &= ~(eldv | G4X_ELD_ADDR);
  6598. len = (i >> 9) & 0x1f; /* ELD buffer size */
  6599. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6600. if (!eld[0])
  6601. return;
  6602. len = min_t(uint8_t, eld[2], len);
  6603. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6604. for (i = 0; i < len; i++)
  6605. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  6606. i = I915_READ(G4X_AUD_CNTL_ST);
  6607. i |= eldv;
  6608. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6609. }
  6610. static void haswell_write_eld(struct drm_connector *connector,
  6611. struct drm_crtc *crtc,
  6612. struct drm_display_mode *mode)
  6613. {
  6614. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6615. uint8_t *eld = connector->eld;
  6616. uint32_t eldv;
  6617. uint32_t i;
  6618. int len;
  6619. int pipe = to_intel_crtc(crtc)->pipe;
  6620. int tmp;
  6621. int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
  6622. int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
  6623. int aud_config = HSW_AUD_CFG(pipe);
  6624. int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
  6625. /* Audio output enable */
  6626. DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
  6627. tmp = I915_READ(aud_cntrl_st2);
  6628. tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
  6629. I915_WRITE(aud_cntrl_st2, tmp);
  6630. POSTING_READ(aud_cntrl_st2);
  6631. assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  6632. /* Set ELD valid state */
  6633. tmp = I915_READ(aud_cntrl_st2);
  6634. DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
  6635. tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
  6636. I915_WRITE(aud_cntrl_st2, tmp);
  6637. tmp = I915_READ(aud_cntrl_st2);
  6638. DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
  6639. /* Enable HDMI mode */
  6640. tmp = I915_READ(aud_config);
  6641. DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
  6642. /* clear N_programing_enable and N_value_index */
  6643. tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
  6644. I915_WRITE(aud_config, tmp);
  6645. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6646. eldv = AUDIO_ELD_VALID_A << (pipe * 4);
  6647. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6648. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6649. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6650. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6651. } else {
  6652. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6653. }
  6654. if (intel_eld_uptodate(connector,
  6655. aud_cntrl_st2, eldv,
  6656. aud_cntl_st, IBX_ELD_ADDRESS,
  6657. hdmiw_hdmiedid))
  6658. return;
  6659. i = I915_READ(aud_cntrl_st2);
  6660. i &= ~eldv;
  6661. I915_WRITE(aud_cntrl_st2, i);
  6662. if (!eld[0])
  6663. return;
  6664. i = I915_READ(aud_cntl_st);
  6665. i &= ~IBX_ELD_ADDRESS;
  6666. I915_WRITE(aud_cntl_st, i);
  6667. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  6668. DRM_DEBUG_DRIVER("port num:%d\n", i);
  6669. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6670. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6671. for (i = 0; i < len; i++)
  6672. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6673. i = I915_READ(aud_cntrl_st2);
  6674. i |= eldv;
  6675. I915_WRITE(aud_cntrl_st2, i);
  6676. }
  6677. static void ironlake_write_eld(struct drm_connector *connector,
  6678. struct drm_crtc *crtc,
  6679. struct drm_display_mode *mode)
  6680. {
  6681. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6682. uint8_t *eld = connector->eld;
  6683. uint32_t eldv;
  6684. uint32_t i;
  6685. int len;
  6686. int hdmiw_hdmiedid;
  6687. int aud_config;
  6688. int aud_cntl_st;
  6689. int aud_cntrl_st2;
  6690. int pipe = to_intel_crtc(crtc)->pipe;
  6691. if (HAS_PCH_IBX(connector->dev)) {
  6692. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  6693. aud_config = IBX_AUD_CFG(pipe);
  6694. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  6695. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  6696. } else if (IS_VALLEYVIEW(connector->dev)) {
  6697. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  6698. aud_config = VLV_AUD_CFG(pipe);
  6699. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  6700. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  6701. } else {
  6702. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  6703. aud_config = CPT_AUD_CFG(pipe);
  6704. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  6705. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  6706. }
  6707. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6708. if (IS_VALLEYVIEW(connector->dev)) {
  6709. struct intel_encoder *intel_encoder;
  6710. struct intel_digital_port *intel_dig_port;
  6711. intel_encoder = intel_attached_encoder(connector);
  6712. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  6713. i = intel_dig_port->port;
  6714. } else {
  6715. i = I915_READ(aud_cntl_st);
  6716. i = (i >> 29) & DIP_PORT_SEL_MASK;
  6717. /* DIP_Port_Select, 0x1 = PortB */
  6718. }
  6719. if (!i) {
  6720. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  6721. /* operate blindly on all ports */
  6722. eldv = IBX_ELD_VALIDB;
  6723. eldv |= IBX_ELD_VALIDB << 4;
  6724. eldv |= IBX_ELD_VALIDB << 8;
  6725. } else {
  6726. DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
  6727. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  6728. }
  6729. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6730. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6731. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6732. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6733. } else {
  6734. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6735. }
  6736. if (intel_eld_uptodate(connector,
  6737. aud_cntrl_st2, eldv,
  6738. aud_cntl_st, IBX_ELD_ADDRESS,
  6739. hdmiw_hdmiedid))
  6740. return;
  6741. i = I915_READ(aud_cntrl_st2);
  6742. i &= ~eldv;
  6743. I915_WRITE(aud_cntrl_st2, i);
  6744. if (!eld[0])
  6745. return;
  6746. i = I915_READ(aud_cntl_st);
  6747. i &= ~IBX_ELD_ADDRESS;
  6748. I915_WRITE(aud_cntl_st, i);
  6749. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6750. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6751. for (i = 0; i < len; i++)
  6752. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6753. i = I915_READ(aud_cntrl_st2);
  6754. i |= eldv;
  6755. I915_WRITE(aud_cntrl_st2, i);
  6756. }
  6757. void intel_write_eld(struct drm_encoder *encoder,
  6758. struct drm_display_mode *mode)
  6759. {
  6760. struct drm_crtc *crtc = encoder->crtc;
  6761. struct drm_connector *connector;
  6762. struct drm_device *dev = encoder->dev;
  6763. struct drm_i915_private *dev_priv = dev->dev_private;
  6764. connector = drm_select_eld(encoder, mode);
  6765. if (!connector)
  6766. return;
  6767. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6768. connector->base.id,
  6769. connector->name,
  6770. connector->encoder->base.id,
  6771. connector->encoder->name);
  6772. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  6773. if (dev_priv->display.write_eld)
  6774. dev_priv->display.write_eld(connector, crtc, mode);
  6775. }
  6776. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6777. {
  6778. struct drm_device *dev = crtc->dev;
  6779. struct drm_i915_private *dev_priv = dev->dev_private;
  6780. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6781. uint32_t cntl = 0, size = 0;
  6782. if (base) {
  6783. unsigned int width = intel_crtc->cursor_width;
  6784. unsigned int height = intel_crtc->cursor_height;
  6785. unsigned int stride = roundup_pow_of_two(width) * 4;
  6786. switch (stride) {
  6787. default:
  6788. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  6789. width, stride);
  6790. stride = 256;
  6791. /* fallthrough */
  6792. case 256:
  6793. case 512:
  6794. case 1024:
  6795. case 2048:
  6796. break;
  6797. }
  6798. cntl |= CURSOR_ENABLE |
  6799. CURSOR_GAMMA_ENABLE |
  6800. CURSOR_FORMAT_ARGB |
  6801. CURSOR_STRIDE(stride);
  6802. size = (height << 12) | width;
  6803. }
  6804. if (intel_crtc->cursor_cntl != 0 &&
  6805. (intel_crtc->cursor_base != base ||
  6806. intel_crtc->cursor_size != size ||
  6807. intel_crtc->cursor_cntl != cntl)) {
  6808. /* On these chipsets we can only modify the base/size/stride
  6809. * whilst the cursor is disabled.
  6810. */
  6811. I915_WRITE(_CURACNTR, 0);
  6812. POSTING_READ(_CURACNTR);
  6813. intel_crtc->cursor_cntl = 0;
  6814. }
  6815. if (intel_crtc->cursor_base != base)
  6816. I915_WRITE(_CURABASE, base);
  6817. if (intel_crtc->cursor_size != size) {
  6818. I915_WRITE(CURSIZE, size);
  6819. intel_crtc->cursor_size = size;
  6820. }
  6821. if (intel_crtc->cursor_cntl != cntl) {
  6822. I915_WRITE(_CURACNTR, cntl);
  6823. POSTING_READ(_CURACNTR);
  6824. intel_crtc->cursor_cntl = cntl;
  6825. }
  6826. }
  6827. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6828. {
  6829. struct drm_device *dev = crtc->dev;
  6830. struct drm_i915_private *dev_priv = dev->dev_private;
  6831. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6832. int pipe = intel_crtc->pipe;
  6833. uint32_t cntl;
  6834. cntl = 0;
  6835. if (base) {
  6836. cntl = MCURSOR_GAMMA_ENABLE;
  6837. switch (intel_crtc->cursor_width) {
  6838. case 64:
  6839. cntl |= CURSOR_MODE_64_ARGB_AX;
  6840. break;
  6841. case 128:
  6842. cntl |= CURSOR_MODE_128_ARGB_AX;
  6843. break;
  6844. case 256:
  6845. cntl |= CURSOR_MODE_256_ARGB_AX;
  6846. break;
  6847. default:
  6848. WARN_ON(1);
  6849. return;
  6850. }
  6851. cntl |= pipe << 28; /* Connect to correct pipe */
  6852. }
  6853. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6854. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6855. if (intel_crtc->cursor_cntl != cntl) {
  6856. I915_WRITE(CURCNTR(pipe), cntl);
  6857. POSTING_READ(CURCNTR(pipe));
  6858. intel_crtc->cursor_cntl = cntl;
  6859. }
  6860. /* and commit changes on next vblank */
  6861. I915_WRITE(CURBASE(pipe), base);
  6862. POSTING_READ(CURBASE(pipe));
  6863. }
  6864. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6865. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6866. bool on)
  6867. {
  6868. struct drm_device *dev = crtc->dev;
  6869. struct drm_i915_private *dev_priv = dev->dev_private;
  6870. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6871. int pipe = intel_crtc->pipe;
  6872. int x = crtc->cursor_x;
  6873. int y = crtc->cursor_y;
  6874. u32 base = 0, pos = 0;
  6875. if (on)
  6876. base = intel_crtc->cursor_addr;
  6877. if (x >= intel_crtc->config.pipe_src_w)
  6878. base = 0;
  6879. if (y >= intel_crtc->config.pipe_src_h)
  6880. base = 0;
  6881. if (x < 0) {
  6882. if (x + intel_crtc->cursor_width <= 0)
  6883. base = 0;
  6884. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  6885. x = -x;
  6886. }
  6887. pos |= x << CURSOR_X_SHIFT;
  6888. if (y < 0) {
  6889. if (y + intel_crtc->cursor_height <= 0)
  6890. base = 0;
  6891. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  6892. y = -y;
  6893. }
  6894. pos |= y << CURSOR_Y_SHIFT;
  6895. if (base == 0 && intel_crtc->cursor_base == 0)
  6896. return;
  6897. I915_WRITE(CURPOS(pipe), pos);
  6898. if (IS_845G(dev) || IS_I865G(dev))
  6899. i845_update_cursor(crtc, base);
  6900. else
  6901. i9xx_update_cursor(crtc, base);
  6902. intel_crtc->cursor_base = base;
  6903. }
  6904. static bool cursor_size_ok(struct drm_device *dev,
  6905. uint32_t width, uint32_t height)
  6906. {
  6907. if (width == 0 || height == 0)
  6908. return false;
  6909. /*
  6910. * 845g/865g are special in that they are only limited by
  6911. * the width of their cursors, the height is arbitrary up to
  6912. * the precision of the register. Everything else requires
  6913. * square cursors, limited to a few power-of-two sizes.
  6914. */
  6915. if (IS_845G(dev) || IS_I865G(dev)) {
  6916. if ((width & 63) != 0)
  6917. return false;
  6918. if (width > (IS_845G(dev) ? 64 : 512))
  6919. return false;
  6920. if (height > 1023)
  6921. return false;
  6922. } else {
  6923. switch (width | height) {
  6924. case 256:
  6925. case 128:
  6926. if (IS_GEN2(dev))
  6927. return false;
  6928. case 64:
  6929. break;
  6930. default:
  6931. return false;
  6932. }
  6933. }
  6934. return true;
  6935. }
  6936. /*
  6937. * intel_crtc_cursor_set_obj - Set cursor to specified GEM object
  6938. *
  6939. * Note that the object's reference will be consumed if the update fails. If
  6940. * the update succeeds, the reference of the old object (if any) will be
  6941. * consumed.
  6942. */
  6943. static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
  6944. struct drm_i915_gem_object *obj,
  6945. uint32_t width, uint32_t height)
  6946. {
  6947. struct drm_device *dev = crtc->dev;
  6948. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6949. enum pipe pipe = intel_crtc->pipe;
  6950. unsigned old_width, stride;
  6951. uint32_t addr;
  6952. int ret;
  6953. /* if we want to turn off the cursor ignore width and height */
  6954. if (!obj) {
  6955. DRM_DEBUG_KMS("cursor off\n");
  6956. addr = 0;
  6957. mutex_lock(&dev->struct_mutex);
  6958. goto finish;
  6959. }
  6960. /* Check for which cursor types we support */
  6961. if (!cursor_size_ok(dev, width, height)) {
  6962. DRM_DEBUG("Cursor dimension not supported\n");
  6963. return -EINVAL;
  6964. }
  6965. stride = roundup_pow_of_two(width) * 4;
  6966. if (obj->base.size < stride * height) {
  6967. DRM_DEBUG_KMS("buffer is too small\n");
  6968. ret = -ENOMEM;
  6969. goto fail;
  6970. }
  6971. /* we only need to pin inside GTT if cursor is non-phy */
  6972. mutex_lock(&dev->struct_mutex);
  6973. if (!INTEL_INFO(dev)->cursor_needs_physical) {
  6974. unsigned alignment;
  6975. if (obj->tiling_mode) {
  6976. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  6977. ret = -EINVAL;
  6978. goto fail_locked;
  6979. }
  6980. /* Note that the w/a also requires 2 PTE of padding following
  6981. * the bo. We currently fill all unused PTE with the shadow
  6982. * page and so we should always have valid PTE following the
  6983. * cursor preventing the VT-d warning.
  6984. */
  6985. alignment = 0;
  6986. if (need_vtd_wa(dev))
  6987. alignment = 64*1024;
  6988. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  6989. if (ret) {
  6990. DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
  6991. goto fail_locked;
  6992. }
  6993. ret = i915_gem_object_put_fence(obj);
  6994. if (ret) {
  6995. DRM_DEBUG_KMS("failed to release fence for cursor");
  6996. goto fail_unpin;
  6997. }
  6998. addr = i915_gem_obj_ggtt_offset(obj);
  6999. } else {
  7000. int align = IS_I830(dev) ? 16 * 1024 : 256;
  7001. ret = i915_gem_object_attach_phys(obj, align);
  7002. if (ret) {
  7003. DRM_DEBUG_KMS("failed to attach phys object\n");
  7004. goto fail_locked;
  7005. }
  7006. addr = obj->phys_handle->busaddr;
  7007. }
  7008. finish:
  7009. if (intel_crtc->cursor_bo) {
  7010. if (!INTEL_INFO(dev)->cursor_needs_physical)
  7011. i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
  7012. }
  7013. i915_gem_track_fb(intel_crtc->cursor_bo, obj,
  7014. INTEL_FRONTBUFFER_CURSOR(pipe));
  7015. mutex_unlock(&dev->struct_mutex);
  7016. old_width = intel_crtc->cursor_width;
  7017. intel_crtc->cursor_addr = addr;
  7018. intel_crtc->cursor_bo = obj;
  7019. intel_crtc->cursor_width = width;
  7020. intel_crtc->cursor_height = height;
  7021. if (intel_crtc->active) {
  7022. if (old_width != width)
  7023. intel_update_watermarks(crtc);
  7024. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  7025. }
  7026. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
  7027. return 0;
  7028. fail_unpin:
  7029. i915_gem_object_unpin_from_display_plane(obj);
  7030. fail_locked:
  7031. mutex_unlock(&dev->struct_mutex);
  7032. fail:
  7033. drm_gem_object_unreference_unlocked(&obj->base);
  7034. return ret;
  7035. }
  7036. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  7037. u16 *blue, uint32_t start, uint32_t size)
  7038. {
  7039. int end = (start + size > 256) ? 256 : start + size, i;
  7040. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7041. for (i = start; i < end; i++) {
  7042. intel_crtc->lut_r[i] = red[i] >> 8;
  7043. intel_crtc->lut_g[i] = green[i] >> 8;
  7044. intel_crtc->lut_b[i] = blue[i] >> 8;
  7045. }
  7046. intel_crtc_load_lut(crtc);
  7047. }
  7048. /* VESA 640x480x72Hz mode to set on the pipe */
  7049. static struct drm_display_mode load_detect_mode = {
  7050. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7051. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7052. };
  7053. struct drm_framebuffer *
  7054. __intel_framebuffer_create(struct drm_device *dev,
  7055. struct drm_mode_fb_cmd2 *mode_cmd,
  7056. struct drm_i915_gem_object *obj)
  7057. {
  7058. struct intel_framebuffer *intel_fb;
  7059. int ret;
  7060. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7061. if (!intel_fb) {
  7062. drm_gem_object_unreference_unlocked(&obj->base);
  7063. return ERR_PTR(-ENOMEM);
  7064. }
  7065. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7066. if (ret)
  7067. goto err;
  7068. return &intel_fb->base;
  7069. err:
  7070. drm_gem_object_unreference_unlocked(&obj->base);
  7071. kfree(intel_fb);
  7072. return ERR_PTR(ret);
  7073. }
  7074. static struct drm_framebuffer *
  7075. intel_framebuffer_create(struct drm_device *dev,
  7076. struct drm_mode_fb_cmd2 *mode_cmd,
  7077. struct drm_i915_gem_object *obj)
  7078. {
  7079. struct drm_framebuffer *fb;
  7080. int ret;
  7081. ret = i915_mutex_lock_interruptible(dev);
  7082. if (ret)
  7083. return ERR_PTR(ret);
  7084. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7085. mutex_unlock(&dev->struct_mutex);
  7086. return fb;
  7087. }
  7088. static u32
  7089. intel_framebuffer_pitch_for_width(int width, int bpp)
  7090. {
  7091. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7092. return ALIGN(pitch, 64);
  7093. }
  7094. static u32
  7095. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7096. {
  7097. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7098. return PAGE_ALIGN(pitch * mode->vdisplay);
  7099. }
  7100. static struct drm_framebuffer *
  7101. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7102. struct drm_display_mode *mode,
  7103. int depth, int bpp)
  7104. {
  7105. struct drm_i915_gem_object *obj;
  7106. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7107. obj = i915_gem_alloc_object(dev,
  7108. intel_framebuffer_size_for_mode(mode, bpp));
  7109. if (obj == NULL)
  7110. return ERR_PTR(-ENOMEM);
  7111. mode_cmd.width = mode->hdisplay;
  7112. mode_cmd.height = mode->vdisplay;
  7113. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7114. bpp);
  7115. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7116. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7117. }
  7118. static struct drm_framebuffer *
  7119. mode_fits_in_fbdev(struct drm_device *dev,
  7120. struct drm_display_mode *mode)
  7121. {
  7122. #ifdef CONFIG_DRM_I915_FBDEV
  7123. struct drm_i915_private *dev_priv = dev->dev_private;
  7124. struct drm_i915_gem_object *obj;
  7125. struct drm_framebuffer *fb;
  7126. if (!dev_priv->fbdev)
  7127. return NULL;
  7128. if (!dev_priv->fbdev->fb)
  7129. return NULL;
  7130. obj = dev_priv->fbdev->fb->obj;
  7131. BUG_ON(!obj);
  7132. fb = &dev_priv->fbdev->fb->base;
  7133. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7134. fb->bits_per_pixel))
  7135. return NULL;
  7136. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7137. return NULL;
  7138. return fb;
  7139. #else
  7140. return NULL;
  7141. #endif
  7142. }
  7143. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7144. struct drm_display_mode *mode,
  7145. struct intel_load_detect_pipe *old,
  7146. struct drm_modeset_acquire_ctx *ctx)
  7147. {
  7148. struct intel_crtc *intel_crtc;
  7149. struct intel_encoder *intel_encoder =
  7150. intel_attached_encoder(connector);
  7151. struct drm_crtc *possible_crtc;
  7152. struct drm_encoder *encoder = &intel_encoder->base;
  7153. struct drm_crtc *crtc = NULL;
  7154. struct drm_device *dev = encoder->dev;
  7155. struct drm_framebuffer *fb;
  7156. struct drm_mode_config *config = &dev->mode_config;
  7157. int ret, i = -1;
  7158. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7159. connector->base.id, connector->name,
  7160. encoder->base.id, encoder->name);
  7161. retry:
  7162. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7163. if (ret)
  7164. goto fail_unlock;
  7165. /*
  7166. * Algorithm gets a little messy:
  7167. *
  7168. * - if the connector already has an assigned crtc, use it (but make
  7169. * sure it's on first)
  7170. *
  7171. * - try to find the first unused crtc that can drive this connector,
  7172. * and use that if we find one
  7173. */
  7174. /* See if we already have a CRTC for this connector */
  7175. if (encoder->crtc) {
  7176. crtc = encoder->crtc;
  7177. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7178. if (ret)
  7179. goto fail_unlock;
  7180. old->dpms_mode = connector->dpms;
  7181. old->load_detect_temp = false;
  7182. /* Make sure the crtc and connector are running */
  7183. if (connector->dpms != DRM_MODE_DPMS_ON)
  7184. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7185. return true;
  7186. }
  7187. /* Find an unused one (if possible) */
  7188. for_each_crtc(dev, possible_crtc) {
  7189. i++;
  7190. if (!(encoder->possible_crtcs & (1 << i)))
  7191. continue;
  7192. if (possible_crtc->enabled)
  7193. continue;
  7194. /* This can occur when applying the pipe A quirk on resume. */
  7195. if (to_intel_crtc(possible_crtc)->new_enabled)
  7196. continue;
  7197. crtc = possible_crtc;
  7198. break;
  7199. }
  7200. /*
  7201. * If we didn't find an unused CRTC, don't use any.
  7202. */
  7203. if (!crtc) {
  7204. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7205. goto fail_unlock;
  7206. }
  7207. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7208. if (ret)
  7209. goto fail_unlock;
  7210. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7211. to_intel_connector(connector)->new_encoder = intel_encoder;
  7212. intel_crtc = to_intel_crtc(crtc);
  7213. intel_crtc->new_enabled = true;
  7214. intel_crtc->new_config = &intel_crtc->config;
  7215. old->dpms_mode = connector->dpms;
  7216. old->load_detect_temp = true;
  7217. old->release_fb = NULL;
  7218. if (!mode)
  7219. mode = &load_detect_mode;
  7220. /* We need a framebuffer large enough to accommodate all accesses
  7221. * that the plane may generate whilst we perform load detection.
  7222. * We can not rely on the fbcon either being present (we get called
  7223. * during its initialisation to detect all boot displays, or it may
  7224. * not even exist) or that it is large enough to satisfy the
  7225. * requested mode.
  7226. */
  7227. fb = mode_fits_in_fbdev(dev, mode);
  7228. if (fb == NULL) {
  7229. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7230. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7231. old->release_fb = fb;
  7232. } else
  7233. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7234. if (IS_ERR(fb)) {
  7235. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7236. goto fail;
  7237. }
  7238. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7239. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7240. if (old->release_fb)
  7241. old->release_fb->funcs->destroy(old->release_fb);
  7242. goto fail;
  7243. }
  7244. /* let the connector get through one full cycle before testing */
  7245. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7246. return true;
  7247. fail:
  7248. intel_crtc->new_enabled = crtc->enabled;
  7249. if (intel_crtc->new_enabled)
  7250. intel_crtc->new_config = &intel_crtc->config;
  7251. else
  7252. intel_crtc->new_config = NULL;
  7253. fail_unlock:
  7254. if (ret == -EDEADLK) {
  7255. drm_modeset_backoff(ctx);
  7256. goto retry;
  7257. }
  7258. return false;
  7259. }
  7260. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7261. struct intel_load_detect_pipe *old)
  7262. {
  7263. struct intel_encoder *intel_encoder =
  7264. intel_attached_encoder(connector);
  7265. struct drm_encoder *encoder = &intel_encoder->base;
  7266. struct drm_crtc *crtc = encoder->crtc;
  7267. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7268. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7269. connector->base.id, connector->name,
  7270. encoder->base.id, encoder->name);
  7271. if (old->load_detect_temp) {
  7272. to_intel_connector(connector)->new_encoder = NULL;
  7273. intel_encoder->new_crtc = NULL;
  7274. intel_crtc->new_enabled = false;
  7275. intel_crtc->new_config = NULL;
  7276. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7277. if (old->release_fb) {
  7278. drm_framebuffer_unregister_private(old->release_fb);
  7279. drm_framebuffer_unreference(old->release_fb);
  7280. }
  7281. return;
  7282. }
  7283. /* Switch crtc and encoder back off if necessary */
  7284. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7285. connector->funcs->dpms(connector, old->dpms_mode);
  7286. }
  7287. static int i9xx_pll_refclk(struct drm_device *dev,
  7288. const struct intel_crtc_config *pipe_config)
  7289. {
  7290. struct drm_i915_private *dev_priv = dev->dev_private;
  7291. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7292. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7293. return dev_priv->vbt.lvds_ssc_freq;
  7294. else if (HAS_PCH_SPLIT(dev))
  7295. return 120000;
  7296. else if (!IS_GEN2(dev))
  7297. return 96000;
  7298. else
  7299. return 48000;
  7300. }
  7301. /* Returns the clock of the currently programmed mode of the given pipe. */
  7302. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7303. struct intel_crtc_config *pipe_config)
  7304. {
  7305. struct drm_device *dev = crtc->base.dev;
  7306. struct drm_i915_private *dev_priv = dev->dev_private;
  7307. int pipe = pipe_config->cpu_transcoder;
  7308. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7309. u32 fp;
  7310. intel_clock_t clock;
  7311. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7312. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7313. fp = pipe_config->dpll_hw_state.fp0;
  7314. else
  7315. fp = pipe_config->dpll_hw_state.fp1;
  7316. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7317. if (IS_PINEVIEW(dev)) {
  7318. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7319. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7320. } else {
  7321. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7322. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7323. }
  7324. if (!IS_GEN2(dev)) {
  7325. if (IS_PINEVIEW(dev))
  7326. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7327. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7328. else
  7329. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7330. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7331. switch (dpll & DPLL_MODE_MASK) {
  7332. case DPLLB_MODE_DAC_SERIAL:
  7333. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7334. 5 : 10;
  7335. break;
  7336. case DPLLB_MODE_LVDS:
  7337. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7338. 7 : 14;
  7339. break;
  7340. default:
  7341. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7342. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7343. return;
  7344. }
  7345. if (IS_PINEVIEW(dev))
  7346. pineview_clock(refclk, &clock);
  7347. else
  7348. i9xx_clock(refclk, &clock);
  7349. } else {
  7350. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7351. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7352. if (is_lvds) {
  7353. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7354. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7355. if (lvds & LVDS_CLKB_POWER_UP)
  7356. clock.p2 = 7;
  7357. else
  7358. clock.p2 = 14;
  7359. } else {
  7360. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7361. clock.p1 = 2;
  7362. else {
  7363. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7364. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7365. }
  7366. if (dpll & PLL_P2_DIVIDE_BY_4)
  7367. clock.p2 = 4;
  7368. else
  7369. clock.p2 = 2;
  7370. }
  7371. i9xx_clock(refclk, &clock);
  7372. }
  7373. /*
  7374. * This value includes pixel_multiplier. We will use
  7375. * port_clock to compute adjusted_mode.crtc_clock in the
  7376. * encoder's get_config() function.
  7377. */
  7378. pipe_config->port_clock = clock.dot;
  7379. }
  7380. int intel_dotclock_calculate(int link_freq,
  7381. const struct intel_link_m_n *m_n)
  7382. {
  7383. /*
  7384. * The calculation for the data clock is:
  7385. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7386. * But we want to avoid losing precison if possible, so:
  7387. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7388. *
  7389. * and the link clock is simpler:
  7390. * link_clock = (m * link_clock) / n
  7391. */
  7392. if (!m_n->link_n)
  7393. return 0;
  7394. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7395. }
  7396. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7397. struct intel_crtc_config *pipe_config)
  7398. {
  7399. struct drm_device *dev = crtc->base.dev;
  7400. /* read out port_clock from the DPLL */
  7401. i9xx_crtc_clock_get(crtc, pipe_config);
  7402. /*
  7403. * This value does not include pixel_multiplier.
  7404. * We will check that port_clock and adjusted_mode.crtc_clock
  7405. * agree once we know their relationship in the encoder's
  7406. * get_config() function.
  7407. */
  7408. pipe_config->adjusted_mode.crtc_clock =
  7409. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7410. &pipe_config->fdi_m_n);
  7411. }
  7412. /** Returns the currently programmed mode of the given pipe. */
  7413. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7414. struct drm_crtc *crtc)
  7415. {
  7416. struct drm_i915_private *dev_priv = dev->dev_private;
  7417. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7418. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7419. struct drm_display_mode *mode;
  7420. struct intel_crtc_config pipe_config;
  7421. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7422. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7423. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7424. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7425. enum pipe pipe = intel_crtc->pipe;
  7426. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7427. if (!mode)
  7428. return NULL;
  7429. /*
  7430. * Construct a pipe_config sufficient for getting the clock info
  7431. * back out of crtc_clock_get.
  7432. *
  7433. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7434. * to use a real value here instead.
  7435. */
  7436. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7437. pipe_config.pixel_multiplier = 1;
  7438. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7439. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7440. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7441. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7442. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7443. mode->hdisplay = (htot & 0xffff) + 1;
  7444. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7445. mode->hsync_start = (hsync & 0xffff) + 1;
  7446. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7447. mode->vdisplay = (vtot & 0xffff) + 1;
  7448. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7449. mode->vsync_start = (vsync & 0xffff) + 1;
  7450. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7451. drm_mode_set_name(mode);
  7452. return mode;
  7453. }
  7454. static void intel_increase_pllclock(struct drm_device *dev,
  7455. enum pipe pipe)
  7456. {
  7457. struct drm_i915_private *dev_priv = dev->dev_private;
  7458. int dpll_reg = DPLL(pipe);
  7459. int dpll;
  7460. if (!HAS_GMCH_DISPLAY(dev))
  7461. return;
  7462. if (!dev_priv->lvds_downclock_avail)
  7463. return;
  7464. dpll = I915_READ(dpll_reg);
  7465. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  7466. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  7467. assert_panel_unlocked(dev_priv, pipe);
  7468. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  7469. I915_WRITE(dpll_reg, dpll);
  7470. intel_wait_for_vblank(dev, pipe);
  7471. dpll = I915_READ(dpll_reg);
  7472. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  7473. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  7474. }
  7475. }
  7476. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7477. {
  7478. struct drm_device *dev = crtc->dev;
  7479. struct drm_i915_private *dev_priv = dev->dev_private;
  7480. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7481. if (!HAS_GMCH_DISPLAY(dev))
  7482. return;
  7483. if (!dev_priv->lvds_downclock_avail)
  7484. return;
  7485. /*
  7486. * Since this is called by a timer, we should never get here in
  7487. * the manual case.
  7488. */
  7489. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7490. int pipe = intel_crtc->pipe;
  7491. int dpll_reg = DPLL(pipe);
  7492. int dpll;
  7493. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7494. assert_panel_unlocked(dev_priv, pipe);
  7495. dpll = I915_READ(dpll_reg);
  7496. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7497. I915_WRITE(dpll_reg, dpll);
  7498. intel_wait_for_vblank(dev, pipe);
  7499. dpll = I915_READ(dpll_reg);
  7500. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7501. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7502. }
  7503. }
  7504. void intel_mark_busy(struct drm_device *dev)
  7505. {
  7506. struct drm_i915_private *dev_priv = dev->dev_private;
  7507. if (dev_priv->mm.busy)
  7508. return;
  7509. intel_runtime_pm_get(dev_priv);
  7510. i915_update_gfx_val(dev_priv);
  7511. dev_priv->mm.busy = true;
  7512. }
  7513. void intel_mark_idle(struct drm_device *dev)
  7514. {
  7515. struct drm_i915_private *dev_priv = dev->dev_private;
  7516. struct drm_crtc *crtc;
  7517. if (!dev_priv->mm.busy)
  7518. return;
  7519. dev_priv->mm.busy = false;
  7520. if (!i915.powersave)
  7521. goto out;
  7522. for_each_crtc(dev, crtc) {
  7523. if (!crtc->primary->fb)
  7524. continue;
  7525. intel_decrease_pllclock(crtc);
  7526. }
  7527. if (INTEL_INFO(dev)->gen >= 6)
  7528. gen6_rps_idle(dev->dev_private);
  7529. out:
  7530. intel_runtime_pm_put(dev_priv);
  7531. }
  7532. /**
  7533. * intel_mark_fb_busy - mark given planes as busy
  7534. * @dev: DRM device
  7535. * @frontbuffer_bits: bits for the affected planes
  7536. * @ring: optional ring for asynchronous commands
  7537. *
  7538. * This function gets called every time the screen contents change. It can be
  7539. * used to keep e.g. the update rate at the nominal refresh rate with DRRS.
  7540. */
  7541. static void intel_mark_fb_busy(struct drm_device *dev,
  7542. unsigned frontbuffer_bits,
  7543. struct intel_engine_cs *ring)
  7544. {
  7545. struct drm_i915_private *dev_priv = dev->dev_private;
  7546. enum pipe pipe;
  7547. if (!i915.powersave)
  7548. return;
  7549. for_each_pipe(dev_priv, pipe) {
  7550. if (!(frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe)))
  7551. continue;
  7552. intel_increase_pllclock(dev, pipe);
  7553. if (ring && intel_fbc_enabled(dev))
  7554. ring->fbc_dirty = true;
  7555. }
  7556. }
  7557. /**
  7558. * intel_fb_obj_invalidate - invalidate frontbuffer object
  7559. * @obj: GEM object to invalidate
  7560. * @ring: set for asynchronous rendering
  7561. *
  7562. * This function gets called every time rendering on the given object starts and
  7563. * frontbuffer caching (fbc, low refresh rate for DRRS, panel self refresh) must
  7564. * be invalidated. If @ring is non-NULL any subsequent invalidation will be delayed
  7565. * until the rendering completes or a flip on this frontbuffer plane is
  7566. * scheduled.
  7567. */
  7568. void intel_fb_obj_invalidate(struct drm_i915_gem_object *obj,
  7569. struct intel_engine_cs *ring)
  7570. {
  7571. struct drm_device *dev = obj->base.dev;
  7572. struct drm_i915_private *dev_priv = dev->dev_private;
  7573. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  7574. if (!obj->frontbuffer_bits)
  7575. return;
  7576. if (ring) {
  7577. mutex_lock(&dev_priv->fb_tracking.lock);
  7578. dev_priv->fb_tracking.busy_bits
  7579. |= obj->frontbuffer_bits;
  7580. dev_priv->fb_tracking.flip_bits
  7581. &= ~obj->frontbuffer_bits;
  7582. mutex_unlock(&dev_priv->fb_tracking.lock);
  7583. }
  7584. intel_mark_fb_busy(dev, obj->frontbuffer_bits, ring);
  7585. intel_edp_psr_invalidate(dev, obj->frontbuffer_bits);
  7586. }
  7587. /**
  7588. * intel_frontbuffer_flush - flush frontbuffer
  7589. * @dev: DRM device
  7590. * @frontbuffer_bits: frontbuffer plane tracking bits
  7591. *
  7592. * This function gets called every time rendering on the given planes has
  7593. * completed and frontbuffer caching can be started again. Flushes will get
  7594. * delayed if they're blocked by some oustanding asynchronous rendering.
  7595. *
  7596. * Can be called without any locks held.
  7597. */
  7598. void intel_frontbuffer_flush(struct drm_device *dev,
  7599. unsigned frontbuffer_bits)
  7600. {
  7601. struct drm_i915_private *dev_priv = dev->dev_private;
  7602. /* Delay flushing when rings are still busy.*/
  7603. mutex_lock(&dev_priv->fb_tracking.lock);
  7604. frontbuffer_bits &= ~dev_priv->fb_tracking.busy_bits;
  7605. mutex_unlock(&dev_priv->fb_tracking.lock);
  7606. intel_mark_fb_busy(dev, frontbuffer_bits, NULL);
  7607. intel_edp_psr_flush(dev, frontbuffer_bits);
  7608. if (IS_GEN8(dev))
  7609. gen8_fbc_sw_flush(dev, FBC_REND_CACHE_CLEAN);
  7610. }
  7611. /**
  7612. * intel_fb_obj_flush - flush frontbuffer object
  7613. * @obj: GEM object to flush
  7614. * @retire: set when retiring asynchronous rendering
  7615. *
  7616. * This function gets called every time rendering on the given object has
  7617. * completed and frontbuffer caching can be started again. If @retire is true
  7618. * then any delayed flushes will be unblocked.
  7619. */
  7620. void intel_fb_obj_flush(struct drm_i915_gem_object *obj,
  7621. bool retire)
  7622. {
  7623. struct drm_device *dev = obj->base.dev;
  7624. struct drm_i915_private *dev_priv = dev->dev_private;
  7625. unsigned frontbuffer_bits;
  7626. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  7627. if (!obj->frontbuffer_bits)
  7628. return;
  7629. frontbuffer_bits = obj->frontbuffer_bits;
  7630. if (retire) {
  7631. mutex_lock(&dev_priv->fb_tracking.lock);
  7632. /* Filter out new bits since rendering started. */
  7633. frontbuffer_bits &= dev_priv->fb_tracking.busy_bits;
  7634. dev_priv->fb_tracking.busy_bits &= ~frontbuffer_bits;
  7635. mutex_unlock(&dev_priv->fb_tracking.lock);
  7636. }
  7637. intel_frontbuffer_flush(dev, frontbuffer_bits);
  7638. }
  7639. /**
  7640. * intel_frontbuffer_flip_prepare - prepare asnychronous frontbuffer flip
  7641. * @dev: DRM device
  7642. * @frontbuffer_bits: frontbuffer plane tracking bits
  7643. *
  7644. * This function gets called after scheduling a flip on @obj. The actual
  7645. * frontbuffer flushing will be delayed until completion is signalled with
  7646. * intel_frontbuffer_flip_complete. If an invalidate happens in between this
  7647. * flush will be cancelled.
  7648. *
  7649. * Can be called without any locks held.
  7650. */
  7651. void intel_frontbuffer_flip_prepare(struct drm_device *dev,
  7652. unsigned frontbuffer_bits)
  7653. {
  7654. struct drm_i915_private *dev_priv = dev->dev_private;
  7655. mutex_lock(&dev_priv->fb_tracking.lock);
  7656. dev_priv->fb_tracking.flip_bits
  7657. |= frontbuffer_bits;
  7658. mutex_unlock(&dev_priv->fb_tracking.lock);
  7659. }
  7660. /**
  7661. * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flush
  7662. * @dev: DRM device
  7663. * @frontbuffer_bits: frontbuffer plane tracking bits
  7664. *
  7665. * This function gets called after the flip has been latched and will complete
  7666. * on the next vblank. It will execute the fush if it hasn't been cancalled yet.
  7667. *
  7668. * Can be called without any locks held.
  7669. */
  7670. void intel_frontbuffer_flip_complete(struct drm_device *dev,
  7671. unsigned frontbuffer_bits)
  7672. {
  7673. struct drm_i915_private *dev_priv = dev->dev_private;
  7674. mutex_lock(&dev_priv->fb_tracking.lock);
  7675. /* Mask any cancelled flips. */
  7676. frontbuffer_bits &= dev_priv->fb_tracking.flip_bits;
  7677. dev_priv->fb_tracking.flip_bits &= ~frontbuffer_bits;
  7678. mutex_unlock(&dev_priv->fb_tracking.lock);
  7679. intel_frontbuffer_flush(dev, frontbuffer_bits);
  7680. }
  7681. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7682. {
  7683. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7684. struct drm_device *dev = crtc->dev;
  7685. struct intel_unpin_work *work;
  7686. unsigned long flags;
  7687. spin_lock_irqsave(&dev->event_lock, flags);
  7688. work = intel_crtc->unpin_work;
  7689. intel_crtc->unpin_work = NULL;
  7690. spin_unlock_irqrestore(&dev->event_lock, flags);
  7691. if (work) {
  7692. cancel_work_sync(&work->work);
  7693. kfree(work);
  7694. }
  7695. drm_crtc_cleanup(crtc);
  7696. kfree(intel_crtc);
  7697. }
  7698. static void intel_unpin_work_fn(struct work_struct *__work)
  7699. {
  7700. struct intel_unpin_work *work =
  7701. container_of(__work, struct intel_unpin_work, work);
  7702. struct drm_device *dev = work->crtc->dev;
  7703. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7704. mutex_lock(&dev->struct_mutex);
  7705. intel_unpin_fb_obj(work->old_fb_obj);
  7706. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7707. drm_gem_object_unreference(&work->old_fb_obj->base);
  7708. intel_update_fbc(dev);
  7709. mutex_unlock(&dev->struct_mutex);
  7710. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7711. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7712. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7713. kfree(work);
  7714. }
  7715. static void do_intel_finish_page_flip(struct drm_device *dev,
  7716. struct drm_crtc *crtc)
  7717. {
  7718. struct drm_i915_private *dev_priv = dev->dev_private;
  7719. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7720. struct intel_unpin_work *work;
  7721. unsigned long flags;
  7722. /* Ignore early vblank irqs */
  7723. if (intel_crtc == NULL)
  7724. return;
  7725. spin_lock_irqsave(&dev->event_lock, flags);
  7726. work = intel_crtc->unpin_work;
  7727. /* Ensure we don't miss a work->pending update ... */
  7728. smp_rmb();
  7729. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7730. spin_unlock_irqrestore(&dev->event_lock, flags);
  7731. return;
  7732. }
  7733. /* and that the unpin work is consistent wrt ->pending. */
  7734. smp_rmb();
  7735. intel_crtc->unpin_work = NULL;
  7736. if (work->event)
  7737. drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
  7738. drm_crtc_vblank_put(crtc);
  7739. spin_unlock_irqrestore(&dev->event_lock, flags);
  7740. wake_up_all(&dev_priv->pending_flip_queue);
  7741. queue_work(dev_priv->wq, &work->work);
  7742. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  7743. }
  7744. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7745. {
  7746. struct drm_i915_private *dev_priv = dev->dev_private;
  7747. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7748. do_intel_finish_page_flip(dev, crtc);
  7749. }
  7750. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7751. {
  7752. struct drm_i915_private *dev_priv = dev->dev_private;
  7753. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7754. do_intel_finish_page_flip(dev, crtc);
  7755. }
  7756. /* Is 'a' after or equal to 'b'? */
  7757. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7758. {
  7759. return !((a - b) & 0x80000000);
  7760. }
  7761. static bool page_flip_finished(struct intel_crtc *crtc)
  7762. {
  7763. struct drm_device *dev = crtc->base.dev;
  7764. struct drm_i915_private *dev_priv = dev->dev_private;
  7765. /*
  7766. * The relevant registers doen't exist on pre-ctg.
  7767. * As the flip done interrupt doesn't trigger for mmio
  7768. * flips on gmch platforms, a flip count check isn't
  7769. * really needed there. But since ctg has the registers,
  7770. * include it in the check anyway.
  7771. */
  7772. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7773. return true;
  7774. /*
  7775. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7776. * used the same base address. In that case the mmio flip might
  7777. * have completed, but the CS hasn't even executed the flip yet.
  7778. *
  7779. * A flip count check isn't enough as the CS might have updated
  7780. * the base address just after start of vblank, but before we
  7781. * managed to process the interrupt. This means we'd complete the
  7782. * CS flip too soon.
  7783. *
  7784. * Combining both checks should get us a good enough result. It may
  7785. * still happen that the CS flip has been executed, but has not
  7786. * yet actually completed. But in case the base address is the same
  7787. * anyway, we don't really care.
  7788. */
  7789. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7790. crtc->unpin_work->gtt_offset &&
  7791. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7792. crtc->unpin_work->flip_count);
  7793. }
  7794. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7795. {
  7796. struct drm_i915_private *dev_priv = dev->dev_private;
  7797. struct intel_crtc *intel_crtc =
  7798. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7799. unsigned long flags;
  7800. /* NB: An MMIO update of the plane base pointer will also
  7801. * generate a page-flip completion irq, i.e. every modeset
  7802. * is also accompanied by a spurious intel_prepare_page_flip().
  7803. */
  7804. spin_lock_irqsave(&dev->event_lock, flags);
  7805. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7806. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7807. spin_unlock_irqrestore(&dev->event_lock, flags);
  7808. }
  7809. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7810. {
  7811. /* Ensure that the work item is consistent when activating it ... */
  7812. smp_wmb();
  7813. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7814. /* and that it is marked active as soon as the irq could fire. */
  7815. smp_wmb();
  7816. }
  7817. static int intel_gen2_queue_flip(struct drm_device *dev,
  7818. struct drm_crtc *crtc,
  7819. struct drm_framebuffer *fb,
  7820. struct drm_i915_gem_object *obj,
  7821. struct intel_engine_cs *ring,
  7822. uint32_t flags)
  7823. {
  7824. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7825. u32 flip_mask;
  7826. int ret;
  7827. ret = intel_ring_begin(ring, 6);
  7828. if (ret)
  7829. return ret;
  7830. /* Can't queue multiple flips, so wait for the previous
  7831. * one to finish before executing the next.
  7832. */
  7833. if (intel_crtc->plane)
  7834. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7835. else
  7836. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7837. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7838. intel_ring_emit(ring, MI_NOOP);
  7839. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7840. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7841. intel_ring_emit(ring, fb->pitches[0]);
  7842. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7843. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7844. intel_mark_page_flip_active(intel_crtc);
  7845. __intel_ring_advance(ring);
  7846. return 0;
  7847. }
  7848. static int intel_gen3_queue_flip(struct drm_device *dev,
  7849. struct drm_crtc *crtc,
  7850. struct drm_framebuffer *fb,
  7851. struct drm_i915_gem_object *obj,
  7852. struct intel_engine_cs *ring,
  7853. uint32_t flags)
  7854. {
  7855. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7856. u32 flip_mask;
  7857. int ret;
  7858. ret = intel_ring_begin(ring, 6);
  7859. if (ret)
  7860. return ret;
  7861. if (intel_crtc->plane)
  7862. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7863. else
  7864. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7865. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7866. intel_ring_emit(ring, MI_NOOP);
  7867. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7868. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7869. intel_ring_emit(ring, fb->pitches[0]);
  7870. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7871. intel_ring_emit(ring, MI_NOOP);
  7872. intel_mark_page_flip_active(intel_crtc);
  7873. __intel_ring_advance(ring);
  7874. return 0;
  7875. }
  7876. static int intel_gen4_queue_flip(struct drm_device *dev,
  7877. struct drm_crtc *crtc,
  7878. struct drm_framebuffer *fb,
  7879. struct drm_i915_gem_object *obj,
  7880. struct intel_engine_cs *ring,
  7881. uint32_t flags)
  7882. {
  7883. struct drm_i915_private *dev_priv = dev->dev_private;
  7884. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7885. uint32_t pf, pipesrc;
  7886. int ret;
  7887. ret = intel_ring_begin(ring, 4);
  7888. if (ret)
  7889. return ret;
  7890. /* i965+ uses the linear or tiled offsets from the
  7891. * Display Registers (which do not change across a page-flip)
  7892. * so we need only reprogram the base address.
  7893. */
  7894. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7895. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7896. intel_ring_emit(ring, fb->pitches[0]);
  7897. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7898. obj->tiling_mode);
  7899. /* XXX Enabling the panel-fitter across page-flip is so far
  7900. * untested on non-native modes, so ignore it for now.
  7901. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7902. */
  7903. pf = 0;
  7904. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7905. intel_ring_emit(ring, pf | pipesrc);
  7906. intel_mark_page_flip_active(intel_crtc);
  7907. __intel_ring_advance(ring);
  7908. return 0;
  7909. }
  7910. static int intel_gen6_queue_flip(struct drm_device *dev,
  7911. struct drm_crtc *crtc,
  7912. struct drm_framebuffer *fb,
  7913. struct drm_i915_gem_object *obj,
  7914. struct intel_engine_cs *ring,
  7915. uint32_t flags)
  7916. {
  7917. struct drm_i915_private *dev_priv = dev->dev_private;
  7918. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7919. uint32_t pf, pipesrc;
  7920. int ret;
  7921. ret = intel_ring_begin(ring, 4);
  7922. if (ret)
  7923. return ret;
  7924. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7925. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7926. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7927. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7928. /* Contrary to the suggestions in the documentation,
  7929. * "Enable Panel Fitter" does not seem to be required when page
  7930. * flipping with a non-native mode, and worse causes a normal
  7931. * modeset to fail.
  7932. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7933. */
  7934. pf = 0;
  7935. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7936. intel_ring_emit(ring, pf | pipesrc);
  7937. intel_mark_page_flip_active(intel_crtc);
  7938. __intel_ring_advance(ring);
  7939. return 0;
  7940. }
  7941. static int intel_gen7_queue_flip(struct drm_device *dev,
  7942. struct drm_crtc *crtc,
  7943. struct drm_framebuffer *fb,
  7944. struct drm_i915_gem_object *obj,
  7945. struct intel_engine_cs *ring,
  7946. uint32_t flags)
  7947. {
  7948. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7949. uint32_t plane_bit = 0;
  7950. int len, ret;
  7951. switch (intel_crtc->plane) {
  7952. case PLANE_A:
  7953. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7954. break;
  7955. case PLANE_B:
  7956. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7957. break;
  7958. case PLANE_C:
  7959. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7960. break;
  7961. default:
  7962. WARN_ONCE(1, "unknown plane in flip command\n");
  7963. return -ENODEV;
  7964. }
  7965. len = 4;
  7966. if (ring->id == RCS) {
  7967. len += 6;
  7968. /*
  7969. * On Gen 8, SRM is now taking an extra dword to accommodate
  7970. * 48bits addresses, and we need a NOOP for the batch size to
  7971. * stay even.
  7972. */
  7973. if (IS_GEN8(dev))
  7974. len += 2;
  7975. }
  7976. /*
  7977. * BSpec MI_DISPLAY_FLIP for IVB:
  7978. * "The full packet must be contained within the same cache line."
  7979. *
  7980. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7981. * cacheline, if we ever start emitting more commands before
  7982. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7983. * then do the cacheline alignment, and finally emit the
  7984. * MI_DISPLAY_FLIP.
  7985. */
  7986. ret = intel_ring_cacheline_align(ring);
  7987. if (ret)
  7988. return ret;
  7989. ret = intel_ring_begin(ring, len);
  7990. if (ret)
  7991. return ret;
  7992. /* Unmask the flip-done completion message. Note that the bspec says that
  7993. * we should do this for both the BCS and RCS, and that we must not unmask
  7994. * more than one flip event at any time (or ensure that one flip message
  7995. * can be sent by waiting for flip-done prior to queueing new flips).
  7996. * Experimentation says that BCS works despite DERRMR masking all
  7997. * flip-done completion events and that unmasking all planes at once
  7998. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7999. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8000. */
  8001. if (ring->id == RCS) {
  8002. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  8003. intel_ring_emit(ring, DERRMR);
  8004. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8005. DERRMR_PIPEB_PRI_FLIP_DONE |
  8006. DERRMR_PIPEC_PRI_FLIP_DONE));
  8007. if (IS_GEN8(dev))
  8008. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  8009. MI_SRM_LRM_GLOBAL_GTT);
  8010. else
  8011. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  8012. MI_SRM_LRM_GLOBAL_GTT);
  8013. intel_ring_emit(ring, DERRMR);
  8014. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  8015. if (IS_GEN8(dev)) {
  8016. intel_ring_emit(ring, 0);
  8017. intel_ring_emit(ring, MI_NOOP);
  8018. }
  8019. }
  8020. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  8021. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  8022. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8023. intel_ring_emit(ring, (MI_NOOP));
  8024. intel_mark_page_flip_active(intel_crtc);
  8025. __intel_ring_advance(ring);
  8026. return 0;
  8027. }
  8028. static bool use_mmio_flip(struct intel_engine_cs *ring,
  8029. struct drm_i915_gem_object *obj)
  8030. {
  8031. /*
  8032. * This is not being used for older platforms, because
  8033. * non-availability of flip done interrupt forces us to use
  8034. * CS flips. Older platforms derive flip done using some clever
  8035. * tricks involving the flip_pending status bits and vblank irqs.
  8036. * So using MMIO flips there would disrupt this mechanism.
  8037. */
  8038. if (ring == NULL)
  8039. return true;
  8040. if (INTEL_INFO(ring->dev)->gen < 5)
  8041. return false;
  8042. if (i915.use_mmio_flip < 0)
  8043. return false;
  8044. else if (i915.use_mmio_flip > 0)
  8045. return true;
  8046. else if (i915.enable_execlists)
  8047. return true;
  8048. else
  8049. return ring != obj->ring;
  8050. }
  8051. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  8052. {
  8053. struct drm_device *dev = intel_crtc->base.dev;
  8054. struct drm_i915_private *dev_priv = dev->dev_private;
  8055. struct intel_framebuffer *intel_fb =
  8056. to_intel_framebuffer(intel_crtc->base.primary->fb);
  8057. struct drm_i915_gem_object *obj = intel_fb->obj;
  8058. u32 dspcntr;
  8059. u32 reg;
  8060. intel_mark_page_flip_active(intel_crtc);
  8061. reg = DSPCNTR(intel_crtc->plane);
  8062. dspcntr = I915_READ(reg);
  8063. if (INTEL_INFO(dev)->gen >= 4) {
  8064. if (obj->tiling_mode != I915_TILING_NONE)
  8065. dspcntr |= DISPPLANE_TILED;
  8066. else
  8067. dspcntr &= ~DISPPLANE_TILED;
  8068. }
  8069. I915_WRITE(reg, dspcntr);
  8070. I915_WRITE(DSPSURF(intel_crtc->plane),
  8071. intel_crtc->unpin_work->gtt_offset);
  8072. POSTING_READ(DSPSURF(intel_crtc->plane));
  8073. }
  8074. static int intel_postpone_flip(struct drm_i915_gem_object *obj)
  8075. {
  8076. struct intel_engine_cs *ring;
  8077. int ret;
  8078. lockdep_assert_held(&obj->base.dev->struct_mutex);
  8079. if (!obj->last_write_seqno)
  8080. return 0;
  8081. ring = obj->ring;
  8082. if (i915_seqno_passed(ring->get_seqno(ring, true),
  8083. obj->last_write_seqno))
  8084. return 0;
  8085. ret = i915_gem_check_olr(ring, obj->last_write_seqno);
  8086. if (ret)
  8087. return ret;
  8088. if (WARN_ON(!ring->irq_get(ring)))
  8089. return 0;
  8090. return 1;
  8091. }
  8092. void intel_notify_mmio_flip(struct intel_engine_cs *ring)
  8093. {
  8094. struct drm_i915_private *dev_priv = to_i915(ring->dev);
  8095. struct intel_crtc *intel_crtc;
  8096. unsigned long irq_flags;
  8097. u32 seqno;
  8098. seqno = ring->get_seqno(ring, false);
  8099. spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
  8100. for_each_intel_crtc(ring->dev, intel_crtc) {
  8101. struct intel_mmio_flip *mmio_flip;
  8102. mmio_flip = &intel_crtc->mmio_flip;
  8103. if (mmio_flip->seqno == 0)
  8104. continue;
  8105. if (ring->id != mmio_flip->ring_id)
  8106. continue;
  8107. if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
  8108. intel_do_mmio_flip(intel_crtc);
  8109. mmio_flip->seqno = 0;
  8110. ring->irq_put(ring);
  8111. }
  8112. }
  8113. spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
  8114. }
  8115. static int intel_queue_mmio_flip(struct drm_device *dev,
  8116. struct drm_crtc *crtc,
  8117. struct drm_framebuffer *fb,
  8118. struct drm_i915_gem_object *obj,
  8119. struct intel_engine_cs *ring,
  8120. uint32_t flags)
  8121. {
  8122. struct drm_i915_private *dev_priv = dev->dev_private;
  8123. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8124. unsigned long irq_flags;
  8125. int ret;
  8126. if (WARN_ON(intel_crtc->mmio_flip.seqno))
  8127. return -EBUSY;
  8128. ret = intel_postpone_flip(obj);
  8129. if (ret < 0)
  8130. return ret;
  8131. if (ret == 0) {
  8132. intel_do_mmio_flip(intel_crtc);
  8133. return 0;
  8134. }
  8135. spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
  8136. intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
  8137. intel_crtc->mmio_flip.ring_id = obj->ring->id;
  8138. spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
  8139. /*
  8140. * Double check to catch cases where irq fired before
  8141. * mmio flip data was ready
  8142. */
  8143. intel_notify_mmio_flip(obj->ring);
  8144. return 0;
  8145. }
  8146. static int intel_default_queue_flip(struct drm_device *dev,
  8147. struct drm_crtc *crtc,
  8148. struct drm_framebuffer *fb,
  8149. struct drm_i915_gem_object *obj,
  8150. struct intel_engine_cs *ring,
  8151. uint32_t flags)
  8152. {
  8153. return -ENODEV;
  8154. }
  8155. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8156. struct drm_framebuffer *fb,
  8157. struct drm_pending_vblank_event *event,
  8158. uint32_t page_flip_flags)
  8159. {
  8160. struct drm_device *dev = crtc->dev;
  8161. struct drm_i915_private *dev_priv = dev->dev_private;
  8162. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8163. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8164. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8165. enum pipe pipe = intel_crtc->pipe;
  8166. struct intel_unpin_work *work;
  8167. struct intel_engine_cs *ring;
  8168. unsigned long flags;
  8169. int ret;
  8170. //trigger software GT busyness calculation
  8171. gen8_flip_interrupt(dev);
  8172. /*
  8173. * drm_mode_page_flip_ioctl() should already catch this, but double
  8174. * check to be safe. In the future we may enable pageflipping from
  8175. * a disabled primary plane.
  8176. */
  8177. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8178. return -EBUSY;
  8179. /* Can't change pixel format via MI display flips. */
  8180. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8181. return -EINVAL;
  8182. /*
  8183. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8184. * Note that pitch changes could also affect these register.
  8185. */
  8186. if (INTEL_INFO(dev)->gen > 3 &&
  8187. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8188. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8189. return -EINVAL;
  8190. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8191. goto out_hang;
  8192. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8193. if (work == NULL)
  8194. return -ENOMEM;
  8195. work->event = event;
  8196. work->crtc = crtc;
  8197. work->old_fb_obj = intel_fb_obj(old_fb);
  8198. INIT_WORK(&work->work, intel_unpin_work_fn);
  8199. ret = drm_crtc_vblank_get(crtc);
  8200. if (ret)
  8201. goto free_work;
  8202. /* We borrow the event spin lock for protecting unpin_work */
  8203. spin_lock_irqsave(&dev->event_lock, flags);
  8204. if (intel_crtc->unpin_work) {
  8205. spin_unlock_irqrestore(&dev->event_lock, flags);
  8206. kfree(work);
  8207. drm_crtc_vblank_put(crtc);
  8208. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8209. return -EBUSY;
  8210. }
  8211. intel_crtc->unpin_work = work;
  8212. spin_unlock_irqrestore(&dev->event_lock, flags);
  8213. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8214. flush_workqueue(dev_priv->wq);
  8215. ret = i915_mutex_lock_interruptible(dev);
  8216. if (ret)
  8217. goto cleanup;
  8218. /* Reference the objects for the scheduled work. */
  8219. drm_gem_object_reference(&work->old_fb_obj->base);
  8220. drm_gem_object_reference(&obj->base);
  8221. crtc->primary->fb = fb;
  8222. work->pending_flip_obj = obj;
  8223. work->enable_stall_check = true;
  8224. atomic_inc(&intel_crtc->unpin_work_count);
  8225. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8226. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8227. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8228. if (IS_VALLEYVIEW(dev)) {
  8229. ring = &dev_priv->ring[BCS];
  8230. if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
  8231. /* vlv: DISPLAY_FLIP fails to change tiling */
  8232. ring = NULL;
  8233. } else if (IS_IVYBRIDGE(dev)) {
  8234. ring = &dev_priv->ring[BCS];
  8235. } else if (INTEL_INFO(dev)->gen >= 7) {
  8236. ring = obj->ring;
  8237. if (ring == NULL || ring->id != RCS)
  8238. ring = &dev_priv->ring[BCS];
  8239. } else {
  8240. ring = &dev_priv->ring[RCS];
  8241. }
  8242. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  8243. if (ret)
  8244. goto cleanup_pending;
  8245. work->gtt_offset =
  8246. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8247. if (use_mmio_flip(ring, obj))
  8248. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8249. page_flip_flags);
  8250. else
  8251. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8252. page_flip_flags);
  8253. if (ret)
  8254. goto cleanup_unpin;
  8255. i915_gem_track_fb(work->old_fb_obj, obj,
  8256. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8257. intel_disable_fbc(dev);
  8258. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8259. mutex_unlock(&dev->struct_mutex);
  8260. trace_i915_flip_request(intel_crtc->plane, obj);
  8261. return 0;
  8262. cleanup_unpin:
  8263. intel_unpin_fb_obj(obj);
  8264. cleanup_pending:
  8265. atomic_dec(&intel_crtc->unpin_work_count);
  8266. crtc->primary->fb = old_fb;
  8267. drm_gem_object_unreference(&work->old_fb_obj->base);
  8268. drm_gem_object_unreference(&obj->base);
  8269. mutex_unlock(&dev->struct_mutex);
  8270. cleanup:
  8271. spin_lock_irqsave(&dev->event_lock, flags);
  8272. intel_crtc->unpin_work = NULL;
  8273. spin_unlock_irqrestore(&dev->event_lock, flags);
  8274. drm_crtc_vblank_put(crtc);
  8275. free_work:
  8276. kfree(work);
  8277. if (ret == -EIO) {
  8278. out_hang:
  8279. intel_crtc_wait_for_pending_flips(crtc);
  8280. ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
  8281. if (ret == 0 && event)
  8282. drm_send_vblank_event(dev, pipe, event);
  8283. }
  8284. return ret;
  8285. }
  8286. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8287. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8288. .load_lut = intel_crtc_load_lut,
  8289. };
  8290. /**
  8291. * intel_modeset_update_staged_output_state
  8292. *
  8293. * Updates the staged output configuration state, e.g. after we've read out the
  8294. * current hw state.
  8295. */
  8296. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8297. {
  8298. struct intel_crtc *crtc;
  8299. struct intel_encoder *encoder;
  8300. struct intel_connector *connector;
  8301. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8302. base.head) {
  8303. connector->new_encoder =
  8304. to_intel_encoder(connector->base.encoder);
  8305. }
  8306. for_each_intel_encoder(dev, encoder) {
  8307. encoder->new_crtc =
  8308. to_intel_crtc(encoder->base.crtc);
  8309. }
  8310. for_each_intel_crtc(dev, crtc) {
  8311. crtc->new_enabled = crtc->base.enabled;
  8312. if (crtc->new_enabled)
  8313. crtc->new_config = &crtc->config;
  8314. else
  8315. crtc->new_config = NULL;
  8316. }
  8317. }
  8318. /**
  8319. * intel_modeset_commit_output_state
  8320. *
  8321. * This function copies the stage display pipe configuration to the real one.
  8322. */
  8323. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8324. {
  8325. struct intel_crtc *crtc;
  8326. struct intel_encoder *encoder;
  8327. struct intel_connector *connector;
  8328. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8329. base.head) {
  8330. connector->base.encoder = &connector->new_encoder->base;
  8331. }
  8332. for_each_intel_encoder(dev, encoder) {
  8333. encoder->base.crtc = &encoder->new_crtc->base;
  8334. }
  8335. for_each_intel_crtc(dev, crtc) {
  8336. crtc->base.enabled = crtc->new_enabled;
  8337. }
  8338. }
  8339. static void
  8340. connected_sink_compute_bpp(struct intel_connector *connector,
  8341. struct intel_crtc_config *pipe_config)
  8342. {
  8343. int bpp = pipe_config->pipe_bpp;
  8344. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8345. connector->base.base.id,
  8346. connector->base.name);
  8347. /* Don't use an invalid EDID bpc value */
  8348. if (connector->base.display_info.bpc &&
  8349. connector->base.display_info.bpc * 3 < bpp) {
  8350. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8351. bpp, connector->base.display_info.bpc*3);
  8352. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8353. }
  8354. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8355. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8356. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8357. bpp);
  8358. pipe_config->pipe_bpp = 24;
  8359. }
  8360. }
  8361. static int
  8362. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8363. struct drm_framebuffer *fb,
  8364. struct intel_crtc_config *pipe_config)
  8365. {
  8366. struct drm_device *dev = crtc->base.dev;
  8367. struct intel_connector *connector;
  8368. int bpp;
  8369. switch (fb->pixel_format) {
  8370. case DRM_FORMAT_C8:
  8371. bpp = 8*3; /* since we go through a colormap */
  8372. break;
  8373. case DRM_FORMAT_XRGB1555:
  8374. case DRM_FORMAT_ARGB1555:
  8375. /* checked in intel_framebuffer_init already */
  8376. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8377. return -EINVAL;
  8378. case DRM_FORMAT_RGB565:
  8379. bpp = 6*3; /* min is 18bpp */
  8380. break;
  8381. case DRM_FORMAT_XBGR8888:
  8382. case DRM_FORMAT_ABGR8888:
  8383. /* checked in intel_framebuffer_init already */
  8384. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8385. return -EINVAL;
  8386. case DRM_FORMAT_XRGB8888:
  8387. case DRM_FORMAT_ARGB8888:
  8388. bpp = 8*3;
  8389. break;
  8390. case DRM_FORMAT_XRGB2101010:
  8391. case DRM_FORMAT_ARGB2101010:
  8392. case DRM_FORMAT_XBGR2101010:
  8393. case DRM_FORMAT_ABGR2101010:
  8394. /* checked in intel_framebuffer_init already */
  8395. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8396. return -EINVAL;
  8397. bpp = 10*3;
  8398. break;
  8399. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8400. default:
  8401. DRM_DEBUG_KMS("unsupported depth\n");
  8402. return -EINVAL;
  8403. }
  8404. pipe_config->pipe_bpp = bpp;
  8405. /* Clamp display bpp to EDID value */
  8406. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8407. base.head) {
  8408. if (!connector->new_encoder ||
  8409. connector->new_encoder->new_crtc != crtc)
  8410. continue;
  8411. connected_sink_compute_bpp(connector, pipe_config);
  8412. }
  8413. return bpp;
  8414. }
  8415. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8416. {
  8417. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8418. "type: 0x%x flags: 0x%x\n",
  8419. mode->crtc_clock,
  8420. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8421. mode->crtc_hsync_end, mode->crtc_htotal,
  8422. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8423. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8424. }
  8425. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8426. struct intel_crtc_config *pipe_config,
  8427. const char *context)
  8428. {
  8429. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8430. context, pipe_name(crtc->pipe));
  8431. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8432. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8433. pipe_config->pipe_bpp, pipe_config->dither);
  8434. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8435. pipe_config->has_pch_encoder,
  8436. pipe_config->fdi_lanes,
  8437. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8438. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8439. pipe_config->fdi_m_n.tu);
  8440. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8441. pipe_config->has_dp_encoder,
  8442. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8443. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8444. pipe_config->dp_m_n.tu);
  8445. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8446. pipe_config->has_dp_encoder,
  8447. pipe_config->dp_m2_n2.gmch_m,
  8448. pipe_config->dp_m2_n2.gmch_n,
  8449. pipe_config->dp_m2_n2.link_m,
  8450. pipe_config->dp_m2_n2.link_n,
  8451. pipe_config->dp_m2_n2.tu);
  8452. DRM_DEBUG_KMS("requested mode:\n");
  8453. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  8454. DRM_DEBUG_KMS("adjusted mode:\n");
  8455. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  8456. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  8457. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8458. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8459. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8460. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8461. pipe_config->gmch_pfit.control,
  8462. pipe_config->gmch_pfit.pgm_ratios,
  8463. pipe_config->gmch_pfit.lvds_border_bits);
  8464. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8465. pipe_config->pch_pfit.pos,
  8466. pipe_config->pch_pfit.size,
  8467. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8468. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8469. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8470. }
  8471. static bool encoders_cloneable(const struct intel_encoder *a,
  8472. const struct intel_encoder *b)
  8473. {
  8474. /* masks could be asymmetric, so check both ways */
  8475. return a == b || (a->cloneable & (1 << b->type) &&
  8476. b->cloneable & (1 << a->type));
  8477. }
  8478. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8479. struct intel_encoder *encoder)
  8480. {
  8481. struct drm_device *dev = crtc->base.dev;
  8482. struct intel_encoder *source_encoder;
  8483. for_each_intel_encoder(dev, source_encoder) {
  8484. if (source_encoder->new_crtc != crtc)
  8485. continue;
  8486. if (!encoders_cloneable(encoder, source_encoder))
  8487. return false;
  8488. }
  8489. return true;
  8490. }
  8491. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8492. {
  8493. struct drm_device *dev = crtc->base.dev;
  8494. struct intel_encoder *encoder;
  8495. for_each_intel_encoder(dev, encoder) {
  8496. if (encoder->new_crtc != crtc)
  8497. continue;
  8498. if (!check_single_encoder_cloning(crtc, encoder))
  8499. return false;
  8500. }
  8501. return true;
  8502. }
  8503. static struct intel_crtc_config *
  8504. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8505. struct drm_framebuffer *fb,
  8506. struct drm_display_mode *mode)
  8507. {
  8508. struct drm_device *dev = crtc->dev;
  8509. struct intel_encoder *encoder;
  8510. struct intel_crtc_config *pipe_config;
  8511. int plane_bpp, ret = -EINVAL;
  8512. bool retry = true;
  8513. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8514. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8515. return ERR_PTR(-EINVAL);
  8516. }
  8517. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8518. if (!pipe_config)
  8519. return ERR_PTR(-ENOMEM);
  8520. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  8521. drm_mode_copy(&pipe_config->requested_mode, mode);
  8522. pipe_config->cpu_transcoder =
  8523. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8524. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8525. /*
  8526. * Sanitize sync polarity flags based on requested ones. If neither
  8527. * positive or negative polarity is requested, treat this as meaning
  8528. * negative polarity.
  8529. */
  8530. if (!(pipe_config->adjusted_mode.flags &
  8531. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8532. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8533. if (!(pipe_config->adjusted_mode.flags &
  8534. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8535. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8536. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8537. * plane pixel format and any sink constraints into account. Returns the
  8538. * source plane bpp so that dithering can be selected on mismatches
  8539. * after encoders and crtc also have had their say. */
  8540. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8541. fb, pipe_config);
  8542. if (plane_bpp < 0)
  8543. goto fail;
  8544. /*
  8545. * Determine the real pipe dimensions. Note that stereo modes can
  8546. * increase the actual pipe size due to the frame doubling and
  8547. * insertion of additional space for blanks between the frame. This
  8548. * is stored in the crtc timings. We use the requested mode to do this
  8549. * computation to clearly distinguish it from the adjusted mode, which
  8550. * can be changed by the connectors in the below retry loop.
  8551. */
  8552. drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
  8553. pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
  8554. pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
  8555. encoder_retry:
  8556. /* Ensure the port clock defaults are reset when retrying. */
  8557. pipe_config->port_clock = 0;
  8558. pipe_config->pixel_multiplier = 1;
  8559. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8560. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  8561. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8562. * adjust it according to limitations or connector properties, and also
  8563. * a chance to reject the mode entirely.
  8564. */
  8565. for_each_intel_encoder(dev, encoder) {
  8566. if (&encoder->new_crtc->base != crtc)
  8567. continue;
  8568. if (!(encoder->compute_config(encoder, pipe_config))) {
  8569. DRM_DEBUG_KMS("Encoder config failure\n");
  8570. goto fail;
  8571. }
  8572. }
  8573. /* Set default port clock if not overwritten by the encoder. Needs to be
  8574. * done afterwards in case the encoder adjusts the mode. */
  8575. if (!pipe_config->port_clock)
  8576. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  8577. * pipe_config->pixel_multiplier;
  8578. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8579. if (ret < 0) {
  8580. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8581. goto fail;
  8582. }
  8583. if (ret == RETRY) {
  8584. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8585. ret = -EINVAL;
  8586. goto fail;
  8587. }
  8588. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8589. retry = false;
  8590. goto encoder_retry;
  8591. }
  8592. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8593. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8594. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8595. return pipe_config;
  8596. fail:
  8597. kfree(pipe_config);
  8598. return ERR_PTR(ret);
  8599. }
  8600. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8601. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8602. static void
  8603. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8604. unsigned *prepare_pipes, unsigned *disable_pipes)
  8605. {
  8606. struct intel_crtc *intel_crtc;
  8607. struct drm_device *dev = crtc->dev;
  8608. struct intel_encoder *encoder;
  8609. struct intel_connector *connector;
  8610. struct drm_crtc *tmp_crtc;
  8611. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8612. /* Check which crtcs have changed outputs connected to them, these need
  8613. * to be part of the prepare_pipes mask. We don't (yet) support global
  8614. * modeset across multiple crtcs, so modeset_pipes will only have one
  8615. * bit set at most. */
  8616. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8617. base.head) {
  8618. if (connector->base.encoder == &connector->new_encoder->base)
  8619. continue;
  8620. if (connector->base.encoder) {
  8621. tmp_crtc = connector->base.encoder->crtc;
  8622. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8623. }
  8624. if (connector->new_encoder)
  8625. *prepare_pipes |=
  8626. 1 << connector->new_encoder->new_crtc->pipe;
  8627. }
  8628. for_each_intel_encoder(dev, encoder) {
  8629. if (encoder->base.crtc == &encoder->new_crtc->base)
  8630. continue;
  8631. if (encoder->base.crtc) {
  8632. tmp_crtc = encoder->base.crtc;
  8633. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8634. }
  8635. if (encoder->new_crtc)
  8636. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8637. }
  8638. /* Check for pipes that will be enabled/disabled ... */
  8639. for_each_intel_crtc(dev, intel_crtc) {
  8640. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8641. continue;
  8642. if (!intel_crtc->new_enabled)
  8643. *disable_pipes |= 1 << intel_crtc->pipe;
  8644. else
  8645. *prepare_pipes |= 1 << intel_crtc->pipe;
  8646. }
  8647. /* set_mode is also used to update properties on life display pipes. */
  8648. intel_crtc = to_intel_crtc(crtc);
  8649. if (intel_crtc->new_enabled)
  8650. *prepare_pipes |= 1 << intel_crtc->pipe;
  8651. /*
  8652. * For simplicity do a full modeset on any pipe where the output routing
  8653. * changed. We could be more clever, but that would require us to be
  8654. * more careful with calling the relevant encoder->mode_set functions.
  8655. */
  8656. if (*prepare_pipes)
  8657. *modeset_pipes = *prepare_pipes;
  8658. /* ... and mask these out. */
  8659. *modeset_pipes &= ~(*disable_pipes);
  8660. *prepare_pipes &= ~(*disable_pipes);
  8661. /*
  8662. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8663. * obies this rule, but the modeset restore mode of
  8664. * intel_modeset_setup_hw_state does not.
  8665. */
  8666. *modeset_pipes &= 1 << intel_crtc->pipe;
  8667. *prepare_pipes &= 1 << intel_crtc->pipe;
  8668. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8669. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8670. }
  8671. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8672. {
  8673. struct drm_encoder *encoder;
  8674. struct drm_device *dev = crtc->dev;
  8675. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8676. if (encoder->crtc == crtc)
  8677. return true;
  8678. return false;
  8679. }
  8680. static void
  8681. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8682. {
  8683. struct intel_encoder *intel_encoder;
  8684. struct intel_crtc *intel_crtc;
  8685. struct drm_connector *connector;
  8686. for_each_intel_encoder(dev, intel_encoder) {
  8687. if (!intel_encoder->base.crtc)
  8688. continue;
  8689. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8690. if (prepare_pipes & (1 << intel_crtc->pipe))
  8691. intel_encoder->connectors_active = false;
  8692. }
  8693. intel_modeset_commit_output_state(dev);
  8694. /* Double check state. */
  8695. for_each_intel_crtc(dev, intel_crtc) {
  8696. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8697. WARN_ON(intel_crtc->new_config &&
  8698. intel_crtc->new_config != &intel_crtc->config);
  8699. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8700. }
  8701. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8702. if (!connector->encoder || !connector->encoder->crtc)
  8703. continue;
  8704. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8705. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8706. struct drm_property *dpms_property =
  8707. dev->mode_config.dpms_property;
  8708. connector->dpms = DRM_MODE_DPMS_ON;
  8709. drm_object_property_set_value(&connector->base,
  8710. dpms_property,
  8711. DRM_MODE_DPMS_ON);
  8712. intel_encoder = to_intel_encoder(connector->encoder);
  8713. intel_encoder->connectors_active = true;
  8714. }
  8715. }
  8716. }
  8717. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8718. {
  8719. int diff;
  8720. if (clock1 == clock2)
  8721. return true;
  8722. if (!clock1 || !clock2)
  8723. return false;
  8724. diff = abs(clock1 - clock2);
  8725. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8726. return true;
  8727. return false;
  8728. }
  8729. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8730. list_for_each_entry((intel_crtc), \
  8731. &(dev)->mode_config.crtc_list, \
  8732. base.head) \
  8733. if (mask & (1 <<(intel_crtc)->pipe))
  8734. static bool
  8735. intel_pipe_config_compare(struct drm_device *dev,
  8736. struct intel_crtc_config *current_config,
  8737. struct intel_crtc_config *pipe_config)
  8738. {
  8739. #define PIPE_CONF_CHECK_X(name) \
  8740. if (current_config->name != pipe_config->name) { \
  8741. DRM_ERROR("mismatch in " #name " " \
  8742. "(expected 0x%08x, found 0x%08x)\n", \
  8743. current_config->name, \
  8744. pipe_config->name); \
  8745. return false; \
  8746. }
  8747. #define PIPE_CONF_CHECK_I(name) \
  8748. if (current_config->name != pipe_config->name) { \
  8749. DRM_ERROR("mismatch in " #name " " \
  8750. "(expected %i, found %i)\n", \
  8751. current_config->name, \
  8752. pipe_config->name); \
  8753. return false; \
  8754. }
  8755. /* This is required for BDW+ where there is only one set of registers for
  8756. * switching between high and low RR.
  8757. * This macro can be used whenever a comparison has to be made between one
  8758. * hw state and multiple sw state variables.
  8759. */
  8760. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  8761. if ((current_config->name != pipe_config->name) && \
  8762. (current_config->alt_name != pipe_config->name)) { \
  8763. DRM_ERROR("mismatch in " #name " " \
  8764. "(expected %i or %i, found %i)\n", \
  8765. current_config->name, \
  8766. current_config->alt_name, \
  8767. pipe_config->name); \
  8768. return false; \
  8769. }
  8770. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8771. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8772. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8773. "(expected %i, found %i)\n", \
  8774. current_config->name & (mask), \
  8775. pipe_config->name & (mask)); \
  8776. return false; \
  8777. }
  8778. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8779. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8780. DRM_ERROR("mismatch in " #name " " \
  8781. "(expected %i, found %i)\n", \
  8782. current_config->name, \
  8783. pipe_config->name); \
  8784. return false; \
  8785. }
  8786. #define PIPE_CONF_QUIRK(quirk) \
  8787. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8788. PIPE_CONF_CHECK_I(cpu_transcoder);
  8789. PIPE_CONF_CHECK_I(has_pch_encoder);
  8790. PIPE_CONF_CHECK_I(fdi_lanes);
  8791. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8792. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8793. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8794. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8795. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8796. PIPE_CONF_CHECK_I(has_dp_encoder);
  8797. if (INTEL_INFO(dev)->gen < 8) {
  8798. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8799. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8800. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8801. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8802. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8803. if (current_config->has_drrs) {
  8804. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  8805. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  8806. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  8807. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  8808. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  8809. }
  8810. } else {
  8811. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  8812. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  8813. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  8814. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  8815. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  8816. }
  8817. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  8818. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  8819. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  8820. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  8821. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  8822. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  8823. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  8824. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  8825. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  8826. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  8827. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  8828. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  8829. PIPE_CONF_CHECK_I(pixel_multiplier);
  8830. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8831. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8832. IS_VALLEYVIEW(dev))
  8833. PIPE_CONF_CHECK_I(limited_color_range);
  8834. PIPE_CONF_CHECK_I(has_audio);
  8835. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8836. DRM_MODE_FLAG_INTERLACE);
  8837. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8838. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8839. DRM_MODE_FLAG_PHSYNC);
  8840. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8841. DRM_MODE_FLAG_NHSYNC);
  8842. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8843. DRM_MODE_FLAG_PVSYNC);
  8844. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8845. DRM_MODE_FLAG_NVSYNC);
  8846. }
  8847. PIPE_CONF_CHECK_I(pipe_src_w);
  8848. PIPE_CONF_CHECK_I(pipe_src_h);
  8849. /*
  8850. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8851. * screen. Since we don't yet re-compute the pipe config when moving
  8852. * just the lvds port away to another pipe the sw tracking won't match.
  8853. *
  8854. * Proper atomic modesets with recomputed global state will fix this.
  8855. * Until then just don't check gmch state for inherited modes.
  8856. */
  8857. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8858. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8859. /* pfit ratios are autocomputed by the hw on gen4+ */
  8860. if (INTEL_INFO(dev)->gen < 4)
  8861. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8862. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8863. }
  8864. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8865. if (current_config->pch_pfit.enabled) {
  8866. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8867. PIPE_CONF_CHECK_I(pch_pfit.size);
  8868. }
  8869. /* BDW+ don't expose a synchronous way to read the state */
  8870. if (IS_HASWELL(dev))
  8871. PIPE_CONF_CHECK_I(ips_enabled);
  8872. PIPE_CONF_CHECK_I(double_wide);
  8873. PIPE_CONF_CHECK_X(ddi_pll_sel);
  8874. PIPE_CONF_CHECK_I(shared_dpll);
  8875. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8876. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8877. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8878. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8879. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  8880. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8881. PIPE_CONF_CHECK_I(pipe_bpp);
  8882. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  8883. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8884. #undef PIPE_CONF_CHECK_X
  8885. #undef PIPE_CONF_CHECK_I
  8886. #undef PIPE_CONF_CHECK_I_ALT
  8887. #undef PIPE_CONF_CHECK_FLAGS
  8888. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8889. #undef PIPE_CONF_QUIRK
  8890. return true;
  8891. }
  8892. static void
  8893. check_connector_state(struct drm_device *dev)
  8894. {
  8895. struct intel_connector *connector;
  8896. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8897. base.head) {
  8898. /* This also checks the encoder/connector hw state with the
  8899. * ->get_hw_state callbacks. */
  8900. intel_connector_check_state(connector);
  8901. WARN(&connector->new_encoder->base != connector->base.encoder,
  8902. "connector's staged encoder doesn't match current encoder\n");
  8903. }
  8904. }
  8905. static void
  8906. check_encoder_state(struct drm_device *dev)
  8907. {
  8908. struct intel_encoder *encoder;
  8909. struct intel_connector *connector;
  8910. for_each_intel_encoder(dev, encoder) {
  8911. bool enabled = false;
  8912. bool active = false;
  8913. enum pipe pipe, tracked_pipe;
  8914. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8915. encoder->base.base.id,
  8916. encoder->base.name);
  8917. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  8918. "encoder's stage crtc doesn't match current crtc\n");
  8919. WARN(encoder->connectors_active && !encoder->base.crtc,
  8920. "encoder's active_connectors set, but no crtc\n");
  8921. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8922. base.head) {
  8923. if (connector->base.encoder != &encoder->base)
  8924. continue;
  8925. enabled = true;
  8926. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  8927. active = true;
  8928. }
  8929. /*
  8930. * for MST connectors if we unplug the connector is gone
  8931. * away but the encoder is still connected to a crtc
  8932. * until a modeset happens in response to the hotplug.
  8933. */
  8934. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  8935. continue;
  8936. WARN(!!encoder->base.crtc != enabled,
  8937. "encoder's enabled state mismatch "
  8938. "(expected %i, found %i)\n",
  8939. !!encoder->base.crtc, enabled);
  8940. WARN(active && !encoder->base.crtc,
  8941. "active encoder with no crtc\n");
  8942. WARN(encoder->connectors_active != active,
  8943. "encoder's computed active state doesn't match tracked active state "
  8944. "(expected %i, found %i)\n", active, encoder->connectors_active);
  8945. active = encoder->get_hw_state(encoder, &pipe);
  8946. WARN(active != encoder->connectors_active,
  8947. "encoder's hw state doesn't match sw tracking "
  8948. "(expected %i, found %i)\n",
  8949. encoder->connectors_active, active);
  8950. if (!encoder->base.crtc)
  8951. continue;
  8952. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  8953. WARN(active && pipe != tracked_pipe,
  8954. "active encoder's pipe doesn't match"
  8955. "(expected %i, found %i)\n",
  8956. tracked_pipe, pipe);
  8957. }
  8958. }
  8959. static void
  8960. check_crtc_state(struct drm_device *dev)
  8961. {
  8962. struct drm_i915_private *dev_priv = dev->dev_private;
  8963. struct intel_crtc *crtc;
  8964. struct intel_encoder *encoder;
  8965. struct intel_crtc_config pipe_config;
  8966. for_each_intel_crtc(dev, crtc) {
  8967. bool enabled = false;
  8968. bool active = false;
  8969. memset(&pipe_config, 0, sizeof(pipe_config));
  8970. DRM_DEBUG_KMS("[CRTC:%d]\n",
  8971. crtc->base.base.id);
  8972. WARN(crtc->active && !crtc->base.enabled,
  8973. "active crtc, but not enabled in sw tracking\n");
  8974. for_each_intel_encoder(dev, encoder) {
  8975. if (encoder->base.crtc != &crtc->base)
  8976. continue;
  8977. enabled = true;
  8978. if (encoder->connectors_active)
  8979. active = true;
  8980. }
  8981. WARN(active != crtc->active,
  8982. "crtc's computed active state doesn't match tracked active state "
  8983. "(expected %i, found %i)\n", active, crtc->active);
  8984. WARN(enabled != crtc->base.enabled,
  8985. "crtc's computed enabled state doesn't match tracked enabled state "
  8986. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  8987. active = dev_priv->display.get_pipe_config(crtc,
  8988. &pipe_config);
  8989. /* hw state is inconsistent with the pipe quirk */
  8990. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  8991. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  8992. active = crtc->active;
  8993. for_each_intel_encoder(dev, encoder) {
  8994. enum pipe pipe;
  8995. if (encoder->base.crtc != &crtc->base)
  8996. continue;
  8997. if (encoder->get_hw_state(encoder, &pipe))
  8998. encoder->get_config(encoder, &pipe_config);
  8999. }
  9000. WARN(crtc->active != active,
  9001. "crtc active state doesn't match with hw state "
  9002. "(expected %i, found %i)\n", crtc->active, active);
  9003. if (active &&
  9004. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  9005. WARN(1, "pipe state doesn't match!\n");
  9006. intel_dump_pipe_config(crtc, &pipe_config,
  9007. "[hw state]");
  9008. intel_dump_pipe_config(crtc, &crtc->config,
  9009. "[sw state]");
  9010. }
  9011. }
  9012. }
  9013. static void
  9014. check_shared_dpll_state(struct drm_device *dev)
  9015. {
  9016. struct drm_i915_private *dev_priv = dev->dev_private;
  9017. struct intel_crtc *crtc;
  9018. struct intel_dpll_hw_state dpll_hw_state;
  9019. int i;
  9020. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9021. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9022. int enabled_crtcs = 0, active_crtcs = 0;
  9023. bool active;
  9024. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9025. DRM_DEBUG_KMS("%s\n", pll->name);
  9026. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9027. WARN(pll->active > pll->refcount,
  9028. "more active pll users than references: %i vs %i\n",
  9029. pll->active, pll->refcount);
  9030. WARN(pll->active && !pll->on,
  9031. "pll in active use but not on in sw tracking\n");
  9032. WARN(pll->on && !pll->active,
  9033. "pll in on but not on in use in sw tracking\n");
  9034. WARN(pll->on != active,
  9035. "pll on state mismatch (expected %i, found %i)\n",
  9036. pll->on, active);
  9037. for_each_intel_crtc(dev, crtc) {
  9038. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  9039. enabled_crtcs++;
  9040. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9041. active_crtcs++;
  9042. }
  9043. WARN(pll->active != active_crtcs,
  9044. "pll active crtcs mismatch (expected %i, found %i)\n",
  9045. pll->active, active_crtcs);
  9046. WARN(pll->refcount != enabled_crtcs,
  9047. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9048. pll->refcount, enabled_crtcs);
  9049. WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
  9050. sizeof(dpll_hw_state)),
  9051. "pll hw state mismatch\n");
  9052. }
  9053. }
  9054. void
  9055. intel_modeset_check_state(struct drm_device *dev)
  9056. {
  9057. check_connector_state(dev);
  9058. check_encoder_state(dev);
  9059. check_crtc_state(dev);
  9060. check_shared_dpll_state(dev);
  9061. }
  9062. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  9063. int dotclock)
  9064. {
  9065. /*
  9066. * FDI already provided one idea for the dotclock.
  9067. * Yell if the encoder disagrees.
  9068. */
  9069. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  9070. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9071. pipe_config->adjusted_mode.crtc_clock, dotclock);
  9072. }
  9073. static void update_scanline_offset(struct intel_crtc *crtc)
  9074. {
  9075. struct drm_device *dev = crtc->base.dev;
  9076. /*
  9077. * The scanline counter increments at the leading edge of hsync.
  9078. *
  9079. * On most platforms it starts counting from vtotal-1 on the
  9080. * first active line. That means the scanline counter value is
  9081. * always one less than what we would expect. Ie. just after
  9082. * start of vblank, which also occurs at start of hsync (on the
  9083. * last active line), the scanline counter will read vblank_start-1.
  9084. *
  9085. * On gen2 the scanline counter starts counting from 1 instead
  9086. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9087. * to keep the value positive), instead of adding one.
  9088. *
  9089. * On HSW+ the behaviour of the scanline counter depends on the output
  9090. * type. For DP ports it behaves like most other platforms, but on HDMI
  9091. * there's an extra 1 line difference. So we need to add two instead of
  9092. * one to the value.
  9093. */
  9094. if (IS_GEN2(dev)) {
  9095. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  9096. int vtotal;
  9097. vtotal = mode->crtc_vtotal;
  9098. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9099. vtotal /= 2;
  9100. crtc->scanline_offset = vtotal - 1;
  9101. } else if (HAS_DDI(dev) &&
  9102. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
  9103. crtc->scanline_offset = 2;
  9104. } else
  9105. crtc->scanline_offset = 1;
  9106. }
  9107. static int __intel_set_mode(struct drm_crtc *crtc,
  9108. struct drm_display_mode *mode,
  9109. int x, int y, struct drm_framebuffer *fb)
  9110. {
  9111. struct drm_device *dev = crtc->dev;
  9112. struct drm_i915_private *dev_priv = dev->dev_private;
  9113. struct drm_display_mode *saved_mode;
  9114. struct intel_crtc_config *pipe_config = NULL;
  9115. struct intel_crtc *intel_crtc;
  9116. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  9117. int ret = 0;
  9118. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9119. if (!saved_mode)
  9120. return -ENOMEM;
  9121. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  9122. &prepare_pipes, &disable_pipes);
  9123. *saved_mode = crtc->mode;
  9124. /* Hack: Because we don't (yet) support global modeset on multiple
  9125. * crtcs, we don't keep track of the new mode for more than one crtc.
  9126. * Hence simply check whether any bit is set in modeset_pipes in all the
  9127. * pieces of code that are not yet converted to deal with mutliple crtcs
  9128. * changing their mode at the same time. */
  9129. if (modeset_pipes) {
  9130. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9131. if (IS_ERR(pipe_config)) {
  9132. ret = PTR_ERR(pipe_config);
  9133. pipe_config = NULL;
  9134. goto out;
  9135. }
  9136. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9137. "[modeset]");
  9138. to_intel_crtc(crtc)->new_config = pipe_config;
  9139. }
  9140. /*
  9141. * See if the config requires any additional preparation, e.g.
  9142. * to adjust global state with pipes off. We need to do this
  9143. * here so we can get the modeset_pipe updated config for the new
  9144. * mode set on this crtc. For other crtcs we need to use the
  9145. * adjusted_mode bits in the crtc directly.
  9146. */
  9147. if (IS_VALLEYVIEW(dev)) {
  9148. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9149. /* may have added more to prepare_pipes than we should */
  9150. prepare_pipes &= ~disable_pipes;
  9151. }
  9152. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9153. intel_crtc_disable(&intel_crtc->base);
  9154. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9155. if (intel_crtc->base.enabled)
  9156. dev_priv->display.crtc_disable(&intel_crtc->base);
  9157. }
  9158. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9159. * to set it here already despite that we pass it down the callchain.
  9160. */
  9161. if (modeset_pipes) {
  9162. crtc->mode = *mode;
  9163. /* mode_set/enable/disable functions rely on a correct pipe
  9164. * config. */
  9165. to_intel_crtc(crtc)->config = *pipe_config;
  9166. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  9167. /*
  9168. * Calculate and store various constants which
  9169. * are later needed by vblank and swap-completion
  9170. * timestamping. They are derived from true hwmode.
  9171. */
  9172. drm_calc_timestamping_constants(crtc,
  9173. &pipe_config->adjusted_mode);
  9174. }
  9175. /* Only after disabling all output pipelines that will be changed can we
  9176. * update the the output configuration. */
  9177. intel_modeset_update_state(dev, prepare_pipes);
  9178. if (dev_priv->display.modeset_global_resources)
  9179. dev_priv->display.modeset_global_resources(dev);
  9180. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9181. * on the DPLL.
  9182. */
  9183. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9184. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9185. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
  9186. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9187. mutex_lock(&dev->struct_mutex);
  9188. ret = intel_pin_and_fence_fb_obj(dev,
  9189. obj,
  9190. NULL);
  9191. if (ret != 0) {
  9192. DRM_ERROR("pin & fence failed\n");
  9193. mutex_unlock(&dev->struct_mutex);
  9194. goto done;
  9195. }
  9196. if (old_fb)
  9197. intel_unpin_fb_obj(old_obj);
  9198. i915_gem_track_fb(old_obj, obj,
  9199. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9200. mutex_unlock(&dev->struct_mutex);
  9201. crtc->primary->fb = fb;
  9202. crtc->x = x;
  9203. crtc->y = y;
  9204. ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
  9205. x, y, fb);
  9206. if (ret)
  9207. goto done;
  9208. }
  9209. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9210. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9211. update_scanline_offset(intel_crtc);
  9212. dev_priv->display.crtc_enable(&intel_crtc->base);
  9213. }
  9214. /* FIXME: add subpixel order */
  9215. done:
  9216. if (ret && crtc->enabled)
  9217. crtc->mode = *saved_mode;
  9218. out:
  9219. kfree(pipe_config);
  9220. kfree(saved_mode);
  9221. return ret;
  9222. }
  9223. static int intel_set_mode(struct drm_crtc *crtc,
  9224. struct drm_display_mode *mode,
  9225. int x, int y, struct drm_framebuffer *fb)
  9226. {
  9227. int ret;
  9228. ret = __intel_set_mode(crtc, mode, x, y, fb);
  9229. if (ret == 0)
  9230. intel_modeset_check_state(crtc->dev);
  9231. return ret;
  9232. }
  9233. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9234. {
  9235. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9236. }
  9237. #undef for_each_intel_crtc_masked
  9238. static void intel_set_config_free(struct intel_set_config *config)
  9239. {
  9240. if (!config)
  9241. return;
  9242. kfree(config->save_connector_encoders);
  9243. kfree(config->save_encoder_crtcs);
  9244. kfree(config->save_crtc_enabled);
  9245. kfree(config);
  9246. }
  9247. static int intel_set_config_save_state(struct drm_device *dev,
  9248. struct intel_set_config *config)
  9249. {
  9250. struct drm_crtc *crtc;
  9251. struct drm_encoder *encoder;
  9252. struct drm_connector *connector;
  9253. int count;
  9254. config->save_crtc_enabled =
  9255. kcalloc(dev->mode_config.num_crtc,
  9256. sizeof(bool), GFP_KERNEL);
  9257. if (!config->save_crtc_enabled)
  9258. return -ENOMEM;
  9259. config->save_encoder_crtcs =
  9260. kcalloc(dev->mode_config.num_encoder,
  9261. sizeof(struct drm_crtc *), GFP_KERNEL);
  9262. if (!config->save_encoder_crtcs)
  9263. return -ENOMEM;
  9264. config->save_connector_encoders =
  9265. kcalloc(dev->mode_config.num_connector,
  9266. sizeof(struct drm_encoder *), GFP_KERNEL);
  9267. if (!config->save_connector_encoders)
  9268. return -ENOMEM;
  9269. /* Copy data. Note that driver private data is not affected.
  9270. * Should anything bad happen only the expected state is
  9271. * restored, not the drivers personal bookkeeping.
  9272. */
  9273. count = 0;
  9274. for_each_crtc(dev, crtc) {
  9275. config->save_crtc_enabled[count++] = crtc->enabled;
  9276. }
  9277. count = 0;
  9278. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9279. config->save_encoder_crtcs[count++] = encoder->crtc;
  9280. }
  9281. count = 0;
  9282. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9283. config->save_connector_encoders[count++] = connector->encoder;
  9284. }
  9285. return 0;
  9286. }
  9287. static void intel_set_config_restore_state(struct drm_device *dev,
  9288. struct intel_set_config *config)
  9289. {
  9290. struct intel_crtc *crtc;
  9291. struct intel_encoder *encoder;
  9292. struct intel_connector *connector;
  9293. int count;
  9294. count = 0;
  9295. for_each_intel_crtc(dev, crtc) {
  9296. crtc->new_enabled = config->save_crtc_enabled[count++];
  9297. if (crtc->new_enabled)
  9298. crtc->new_config = &crtc->config;
  9299. else
  9300. crtc->new_config = NULL;
  9301. }
  9302. count = 0;
  9303. for_each_intel_encoder(dev, encoder) {
  9304. encoder->new_crtc =
  9305. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9306. }
  9307. count = 0;
  9308. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9309. connector->new_encoder =
  9310. to_intel_encoder(config->save_connector_encoders[count++]);
  9311. }
  9312. }
  9313. static bool
  9314. is_crtc_connector_off(struct drm_mode_set *set)
  9315. {
  9316. int i;
  9317. if (set->num_connectors == 0)
  9318. return false;
  9319. if (WARN_ON(set->connectors == NULL))
  9320. return false;
  9321. for (i = 0; i < set->num_connectors; i++)
  9322. if (set->connectors[i]->encoder &&
  9323. set->connectors[i]->encoder->crtc == set->crtc &&
  9324. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9325. return true;
  9326. return false;
  9327. }
  9328. static void
  9329. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9330. struct intel_set_config *config)
  9331. {
  9332. /* We should be able to check here if the fb has the same properties
  9333. * and then just flip_or_move it */
  9334. if (is_crtc_connector_off(set)) {
  9335. config->mode_changed = true;
  9336. } else if (set->crtc->primary->fb != set->fb) {
  9337. /*
  9338. * If we have no fb, we can only flip as long as the crtc is
  9339. * active, otherwise we need a full mode set. The crtc may
  9340. * be active if we've only disabled the primary plane, or
  9341. * in fastboot situations.
  9342. */
  9343. if (set->crtc->primary->fb == NULL) {
  9344. struct intel_crtc *intel_crtc =
  9345. to_intel_crtc(set->crtc);
  9346. if (intel_crtc->active) {
  9347. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9348. config->fb_changed = true;
  9349. } else {
  9350. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9351. config->mode_changed = true;
  9352. }
  9353. } else if (set->fb == NULL) {
  9354. config->mode_changed = true;
  9355. } else if (set->fb->pixel_format !=
  9356. set->crtc->primary->fb->pixel_format) {
  9357. config->mode_changed = true;
  9358. } else {
  9359. config->fb_changed = true;
  9360. }
  9361. }
  9362. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9363. config->fb_changed = true;
  9364. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9365. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9366. drm_mode_debug_printmodeline(&set->crtc->mode);
  9367. drm_mode_debug_printmodeline(set->mode);
  9368. config->mode_changed = true;
  9369. }
  9370. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9371. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9372. }
  9373. static int
  9374. intel_modeset_stage_output_state(struct drm_device *dev,
  9375. struct drm_mode_set *set,
  9376. struct intel_set_config *config)
  9377. {
  9378. struct intel_connector *connector;
  9379. struct intel_encoder *encoder;
  9380. struct intel_crtc *crtc;
  9381. int ro;
  9382. /* The upper layers ensure that we either disable a crtc or have a list
  9383. * of connectors. For paranoia, double-check this. */
  9384. WARN_ON(!set->fb && (set->num_connectors != 0));
  9385. WARN_ON(set->fb && (set->num_connectors == 0));
  9386. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9387. base.head) {
  9388. /* Otherwise traverse passed in connector list and get encoders
  9389. * for them. */
  9390. for (ro = 0; ro < set->num_connectors; ro++) {
  9391. if (set->connectors[ro] == &connector->base) {
  9392. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9393. break;
  9394. }
  9395. }
  9396. /* If we disable the crtc, disable all its connectors. Also, if
  9397. * the connector is on the changing crtc but not on the new
  9398. * connector list, disable it. */
  9399. if ((!set->fb || ro == set->num_connectors) &&
  9400. connector->base.encoder &&
  9401. connector->base.encoder->crtc == set->crtc) {
  9402. connector->new_encoder = NULL;
  9403. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9404. connector->base.base.id,
  9405. connector->base.name);
  9406. }
  9407. if (&connector->new_encoder->base != connector->base.encoder) {
  9408. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  9409. config->mode_changed = true;
  9410. }
  9411. }
  9412. /* connector->new_encoder is now updated for all connectors. */
  9413. /* Update crtc of enabled connectors. */
  9414. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9415. base.head) {
  9416. struct drm_crtc *new_crtc;
  9417. if (!connector->new_encoder)
  9418. continue;
  9419. new_crtc = connector->new_encoder->base.crtc;
  9420. for (ro = 0; ro < set->num_connectors; ro++) {
  9421. if (set->connectors[ro] == &connector->base)
  9422. new_crtc = set->crtc;
  9423. }
  9424. /* Make sure the new CRTC will work with the encoder */
  9425. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9426. new_crtc)) {
  9427. return -EINVAL;
  9428. }
  9429. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9430. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9431. connector->base.base.id,
  9432. connector->base.name,
  9433. new_crtc->base.id);
  9434. }
  9435. /* Check for any encoders that needs to be disabled. */
  9436. for_each_intel_encoder(dev, encoder) {
  9437. int num_connectors = 0;
  9438. list_for_each_entry(connector,
  9439. &dev->mode_config.connector_list,
  9440. base.head) {
  9441. if (connector->new_encoder == encoder) {
  9442. WARN_ON(!connector->new_encoder->new_crtc);
  9443. num_connectors++;
  9444. }
  9445. }
  9446. if (num_connectors == 0)
  9447. encoder->new_crtc = NULL;
  9448. else if (num_connectors > 1)
  9449. return -EINVAL;
  9450. /* Only now check for crtc changes so we don't miss encoders
  9451. * that will be disabled. */
  9452. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9453. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  9454. config->mode_changed = true;
  9455. }
  9456. }
  9457. /* Now we've also updated encoder->new_crtc for all encoders. */
  9458. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9459. base.head) {
  9460. if (connector->new_encoder)
  9461. if (connector->new_encoder != connector->encoder)
  9462. connector->encoder = connector->new_encoder;
  9463. }
  9464. for_each_intel_crtc(dev, crtc) {
  9465. crtc->new_enabled = false;
  9466. for_each_intel_encoder(dev, encoder) {
  9467. if (encoder->new_crtc == crtc) {
  9468. crtc->new_enabled = true;
  9469. break;
  9470. }
  9471. }
  9472. if (crtc->new_enabled != crtc->base.enabled) {
  9473. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  9474. crtc->new_enabled ? "en" : "dis");
  9475. config->mode_changed = true;
  9476. }
  9477. if (crtc->new_enabled)
  9478. crtc->new_config = &crtc->config;
  9479. else
  9480. crtc->new_config = NULL;
  9481. }
  9482. return 0;
  9483. }
  9484. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9485. {
  9486. struct drm_device *dev = crtc->base.dev;
  9487. struct intel_encoder *encoder;
  9488. struct intel_connector *connector;
  9489. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9490. pipe_name(crtc->pipe));
  9491. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9492. if (connector->new_encoder &&
  9493. connector->new_encoder->new_crtc == crtc)
  9494. connector->new_encoder = NULL;
  9495. }
  9496. for_each_intel_encoder(dev, encoder) {
  9497. if (encoder->new_crtc == crtc)
  9498. encoder->new_crtc = NULL;
  9499. }
  9500. crtc->new_enabled = false;
  9501. crtc->new_config = NULL;
  9502. }
  9503. static int intel_crtc_set_config(struct drm_mode_set *set)
  9504. {
  9505. struct drm_device *dev;
  9506. struct drm_mode_set save_set;
  9507. struct intel_set_config *config;
  9508. int ret;
  9509. BUG_ON(!set);
  9510. BUG_ON(!set->crtc);
  9511. BUG_ON(!set->crtc->helper_private);
  9512. /* Enforce sane interface api - has been abused by the fb helper. */
  9513. BUG_ON(!set->mode && set->fb);
  9514. BUG_ON(set->fb && set->num_connectors == 0);
  9515. if (set->fb) {
  9516. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9517. set->crtc->base.id, set->fb->base.id,
  9518. (int)set->num_connectors, set->x, set->y);
  9519. } else {
  9520. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9521. }
  9522. dev = set->crtc->dev;
  9523. ret = -ENOMEM;
  9524. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9525. if (!config)
  9526. goto out_config;
  9527. ret = intel_set_config_save_state(dev, config);
  9528. if (ret)
  9529. goto out_config;
  9530. save_set.crtc = set->crtc;
  9531. save_set.mode = &set->crtc->mode;
  9532. save_set.x = set->crtc->x;
  9533. save_set.y = set->crtc->y;
  9534. save_set.fb = set->crtc->primary->fb;
  9535. /* Compute whether we need a full modeset, only an fb base update or no
  9536. * change at all. In the future we might also check whether only the
  9537. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9538. * such cases. */
  9539. intel_set_config_compute_mode_changes(set, config);
  9540. ret = intel_modeset_stage_output_state(dev, set, config);
  9541. if (ret)
  9542. goto fail;
  9543. if (config->mode_changed) {
  9544. ret = intel_set_mode(set->crtc, set->mode,
  9545. set->x, set->y, set->fb);
  9546. } else if (config->fb_changed) {
  9547. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9548. intel_crtc_wait_for_pending_flips(set->crtc);
  9549. ret = intel_pipe_set_base(set->crtc,
  9550. set->x, set->y, set->fb);
  9551. /*
  9552. * We need to make sure the primary plane is re-enabled if it
  9553. * has previously been turned off.
  9554. */
  9555. if (!intel_crtc->primary_enabled && ret == 0) {
  9556. WARN_ON(!intel_crtc->active);
  9557. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9558. }
  9559. /*
  9560. * In the fastboot case this may be our only check of the
  9561. * state after boot. It would be better to only do it on
  9562. * the first update, but we don't have a nice way of doing that
  9563. * (and really, set_config isn't used much for high freq page
  9564. * flipping, so increasing its cost here shouldn't be a big
  9565. * deal).
  9566. */
  9567. if (i915.fastboot && ret == 0)
  9568. intel_modeset_check_state(set->crtc->dev);
  9569. }
  9570. if (ret) {
  9571. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9572. set->crtc->base.id, ret);
  9573. fail:
  9574. intel_set_config_restore_state(dev, config);
  9575. /*
  9576. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9577. * force the pipe off to avoid oopsing in the modeset code
  9578. * due to fb==NULL. This should only happen during boot since
  9579. * we don't yet reconstruct the FB from the hardware state.
  9580. */
  9581. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9582. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9583. /* Try to restore the config */
  9584. if (config->mode_changed &&
  9585. intel_set_mode(save_set.crtc, save_set.mode,
  9586. save_set.x, save_set.y, save_set.fb))
  9587. DRM_ERROR("failed to restore config after modeset failure\n");
  9588. }
  9589. out_config:
  9590. intel_set_config_free(config);
  9591. return ret;
  9592. }
  9593. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9594. .gamma_set = intel_crtc_gamma_set,
  9595. .set_config = intel_crtc_set_config,
  9596. .destroy = intel_crtc_destroy,
  9597. .page_flip = intel_crtc_page_flip,
  9598. };
  9599. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9600. struct intel_shared_dpll *pll,
  9601. struct intel_dpll_hw_state *hw_state)
  9602. {
  9603. uint32_t val;
  9604. if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9605. return false;
  9606. val = I915_READ(PCH_DPLL(pll->id));
  9607. hw_state->dpll = val;
  9608. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9609. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9610. return val & DPLL_VCO_ENABLE;
  9611. }
  9612. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9613. struct intel_shared_dpll *pll)
  9614. {
  9615. I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
  9616. I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
  9617. }
  9618. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9619. struct intel_shared_dpll *pll)
  9620. {
  9621. /* PCH refclock must be enabled first */
  9622. ibx_assert_pch_refclk_enabled(dev_priv);
  9623. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9624. /* Wait for the clocks to stabilize. */
  9625. POSTING_READ(PCH_DPLL(pll->id));
  9626. udelay(150);
  9627. /* The pixel multiplier can only be updated once the
  9628. * DPLL is enabled and the clocks are stable.
  9629. *
  9630. * So write it again.
  9631. */
  9632. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9633. POSTING_READ(PCH_DPLL(pll->id));
  9634. udelay(200);
  9635. }
  9636. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9637. struct intel_shared_dpll *pll)
  9638. {
  9639. struct drm_device *dev = dev_priv->dev;
  9640. struct intel_crtc *crtc;
  9641. /* Make sure no transcoder isn't still depending on us. */
  9642. for_each_intel_crtc(dev, crtc) {
  9643. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9644. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9645. }
  9646. I915_WRITE(PCH_DPLL(pll->id), 0);
  9647. POSTING_READ(PCH_DPLL(pll->id));
  9648. udelay(200);
  9649. }
  9650. static char *ibx_pch_dpll_names[] = {
  9651. "PCH DPLL A",
  9652. "PCH DPLL B",
  9653. };
  9654. static void ibx_pch_dpll_init(struct drm_device *dev)
  9655. {
  9656. struct drm_i915_private *dev_priv = dev->dev_private;
  9657. int i;
  9658. dev_priv->num_shared_dpll = 2;
  9659. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9660. dev_priv->shared_dplls[i].id = i;
  9661. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9662. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9663. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9664. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9665. dev_priv->shared_dplls[i].get_hw_state =
  9666. ibx_pch_dpll_get_hw_state;
  9667. }
  9668. }
  9669. static void intel_shared_dpll_init(struct drm_device *dev)
  9670. {
  9671. struct drm_i915_private *dev_priv = dev->dev_private;
  9672. if (HAS_DDI(dev))
  9673. intel_ddi_pll_init(dev);
  9674. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9675. ibx_pch_dpll_init(dev);
  9676. else
  9677. dev_priv->num_shared_dpll = 0;
  9678. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9679. }
  9680. static int
  9681. intel_primary_plane_disable(struct drm_plane *plane)
  9682. {
  9683. struct drm_device *dev = plane->dev;
  9684. struct intel_crtc *intel_crtc;
  9685. if (!plane->fb)
  9686. return 0;
  9687. BUG_ON(!plane->crtc);
  9688. intel_crtc = to_intel_crtc(plane->crtc);
  9689. /*
  9690. * Even though we checked plane->fb above, it's still possible that
  9691. * the primary plane has been implicitly disabled because the crtc
  9692. * coordinates given weren't visible, or because we detected
  9693. * that it was 100% covered by a sprite plane. Or, the CRTC may be
  9694. * off and we've set a fb, but haven't actually turned on the CRTC yet.
  9695. * In either case, we need to unpin the FB and let the fb pointer get
  9696. * updated, but otherwise we don't need to touch the hardware.
  9697. */
  9698. if (!intel_crtc->primary_enabled)
  9699. goto disable_unpin;
  9700. intel_crtc_wait_for_pending_flips(plane->crtc);
  9701. intel_disable_primary_hw_plane(plane, plane->crtc);
  9702. disable_unpin:
  9703. mutex_lock(&dev->struct_mutex);
  9704. i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
  9705. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9706. intel_unpin_fb_obj(intel_fb_obj(plane->fb));
  9707. mutex_unlock(&dev->struct_mutex);
  9708. plane->fb = NULL;
  9709. return 0;
  9710. }
  9711. static int
  9712. intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
  9713. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9714. unsigned int crtc_w, unsigned int crtc_h,
  9715. uint32_t src_x, uint32_t src_y,
  9716. uint32_t src_w, uint32_t src_h)
  9717. {
  9718. struct drm_device *dev = crtc->dev;
  9719. struct drm_i915_private *dev_priv = dev->dev_private;
  9720. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9721. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9722. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9723. struct drm_rect dest = {
  9724. /* integer pixels */
  9725. .x1 = crtc_x,
  9726. .y1 = crtc_y,
  9727. .x2 = crtc_x + crtc_w,
  9728. .y2 = crtc_y + crtc_h,
  9729. };
  9730. struct drm_rect src = {
  9731. /* 16.16 fixed point */
  9732. .x1 = src_x,
  9733. .y1 = src_y,
  9734. .x2 = src_x + src_w,
  9735. .y2 = src_y + src_h,
  9736. };
  9737. const struct drm_rect clip = {
  9738. /* integer pixels */
  9739. .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
  9740. .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
  9741. };
  9742. const struct {
  9743. int crtc_x, crtc_y;
  9744. unsigned int crtc_w, crtc_h;
  9745. uint32_t src_x, src_y, src_w, src_h;
  9746. } orig = {
  9747. .crtc_x = crtc_x,
  9748. .crtc_y = crtc_y,
  9749. .crtc_w = crtc_w,
  9750. .crtc_h = crtc_h,
  9751. .src_x = src_x,
  9752. .src_y = src_y,
  9753. .src_w = src_w,
  9754. .src_h = src_h,
  9755. };
  9756. struct intel_plane *intel_plane = to_intel_plane(plane);
  9757. bool visible;
  9758. int ret;
  9759. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9760. &src, &dest, &clip,
  9761. DRM_PLANE_HELPER_NO_SCALING,
  9762. DRM_PLANE_HELPER_NO_SCALING,
  9763. false, true, &visible);
  9764. if (ret)
  9765. return ret;
  9766. /*
  9767. * If the CRTC isn't enabled, we're just pinning the framebuffer,
  9768. * updating the fb pointer, and returning without touching the
  9769. * hardware. This allows us to later do a drmModeSetCrtc with fb=-1 to
  9770. * turn on the display with all planes setup as desired.
  9771. */
  9772. if (!crtc->enabled) {
  9773. mutex_lock(&dev->struct_mutex);
  9774. /*
  9775. * If we already called setplane while the crtc was disabled,
  9776. * we may have an fb pinned; unpin it.
  9777. */
  9778. if (plane->fb)
  9779. intel_unpin_fb_obj(old_obj);
  9780. i915_gem_track_fb(old_obj, obj,
  9781. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9782. /* Pin and return without programming hardware */
  9783. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  9784. mutex_unlock(&dev->struct_mutex);
  9785. return ret;
  9786. }
  9787. intel_crtc_wait_for_pending_flips(crtc);
  9788. /*
  9789. * If clipping results in a non-visible primary plane, we'll disable
  9790. * the primary plane. Note that this is a bit different than what
  9791. * happens if userspace explicitly disables the plane by passing fb=0
  9792. * because plane->fb still gets set and pinned.
  9793. */
  9794. if (!visible) {
  9795. mutex_lock(&dev->struct_mutex);
  9796. /*
  9797. * Try to pin the new fb first so that we can bail out if we
  9798. * fail.
  9799. */
  9800. if (plane->fb != fb) {
  9801. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  9802. if (ret) {
  9803. mutex_unlock(&dev->struct_mutex);
  9804. return ret;
  9805. }
  9806. }
  9807. i915_gem_track_fb(old_obj, obj,
  9808. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
  9809. if (intel_crtc->primary_enabled)
  9810. intel_disable_primary_hw_plane(plane, crtc);
  9811. if (plane->fb != fb)
  9812. if (plane->fb)
  9813. intel_unpin_fb_obj(old_obj);
  9814. mutex_unlock(&dev->struct_mutex);
  9815. } else {
  9816. if (intel_crtc && intel_crtc->active &&
  9817. intel_crtc->primary_enabled) {
  9818. /*
  9819. * FBC does not work on some platforms for rotated
  9820. * planes, so disable it when rotation is not 0 and
  9821. * update it when rotation is set back to 0.
  9822. *
  9823. * FIXME: This is redundant with the fbc update done in
  9824. * the primary plane enable function except that that
  9825. * one is done too late. We eventually need to unify
  9826. * this.
  9827. */
  9828. if (INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9829. dev_priv->fbc.plane == intel_crtc->plane &&
  9830. intel_plane->rotation != BIT(DRM_ROTATE_0)) {
  9831. intel_disable_fbc(dev);
  9832. }
  9833. }
  9834. ret = intel_pipe_set_base(crtc, src.x1, src.y1, fb);
  9835. if (ret)
  9836. return ret;
  9837. if (!intel_crtc->primary_enabled)
  9838. intel_enable_primary_hw_plane(plane, crtc);
  9839. }
  9840. intel_plane->crtc_x = orig.crtc_x;
  9841. intel_plane->crtc_y = orig.crtc_y;
  9842. intel_plane->crtc_w = orig.crtc_w;
  9843. intel_plane->crtc_h = orig.crtc_h;
  9844. intel_plane->src_x = orig.src_x;
  9845. intel_plane->src_y = orig.src_y;
  9846. intel_plane->src_w = orig.src_w;
  9847. intel_plane->src_h = orig.src_h;
  9848. intel_plane->obj = obj;
  9849. return 0;
  9850. }
  9851. /* Common destruction function for both primary and cursor planes */
  9852. static void intel_plane_destroy(struct drm_plane *plane)
  9853. {
  9854. struct intel_plane *intel_plane = to_intel_plane(plane);
  9855. drm_plane_cleanup(plane);
  9856. kfree(intel_plane);
  9857. }
  9858. static const struct drm_plane_funcs intel_primary_plane_funcs = {
  9859. .update_plane = intel_primary_plane_setplane,
  9860. .disable_plane = intel_primary_plane_disable,
  9861. .destroy = intel_plane_destroy,
  9862. .set_property = intel_plane_set_property
  9863. };
  9864. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  9865. int pipe)
  9866. {
  9867. struct intel_plane *primary;
  9868. const uint32_t *intel_primary_formats;
  9869. int num_formats;
  9870. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  9871. if (primary == NULL)
  9872. return NULL;
  9873. primary->can_scale = false;
  9874. primary->max_downscale = 1;
  9875. primary->pipe = pipe;
  9876. primary->plane = pipe;
  9877. primary->rotation = BIT(DRM_ROTATE_0);
  9878. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  9879. primary->plane = !pipe;
  9880. if (INTEL_INFO(dev)->gen <= 3) {
  9881. intel_primary_formats = intel_primary_formats_gen2;
  9882. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  9883. } else {
  9884. intel_primary_formats = intel_primary_formats_gen4;
  9885. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  9886. }
  9887. drm_universal_plane_init(dev, &primary->base, 0,
  9888. &intel_primary_plane_funcs,
  9889. intel_primary_formats, num_formats,
  9890. DRM_PLANE_TYPE_PRIMARY);
  9891. if (INTEL_INFO(dev)->gen >= 4) {
  9892. if (!dev->mode_config.rotation_property)
  9893. dev->mode_config.rotation_property =
  9894. drm_mode_create_rotation_property(dev,
  9895. BIT(DRM_ROTATE_0) |
  9896. BIT(DRM_ROTATE_180));
  9897. if (dev->mode_config.rotation_property)
  9898. drm_object_attach_property(&primary->base.base,
  9899. dev->mode_config.rotation_property,
  9900. primary->rotation);
  9901. }
  9902. return &primary->base;
  9903. }
  9904. static int
  9905. intel_cursor_plane_disable(struct drm_plane *plane)
  9906. {
  9907. if (!plane->fb)
  9908. return 0;
  9909. BUG_ON(!plane->crtc);
  9910. return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
  9911. }
  9912. static int
  9913. intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
  9914. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  9915. unsigned int crtc_w, unsigned int crtc_h,
  9916. uint32_t src_x, uint32_t src_y,
  9917. uint32_t src_w, uint32_t src_h)
  9918. {
  9919. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9920. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  9921. struct drm_i915_gem_object *obj = intel_fb->obj;
  9922. struct drm_rect dest = {
  9923. /* integer pixels */
  9924. .x1 = crtc_x,
  9925. .y1 = crtc_y,
  9926. .x2 = crtc_x + crtc_w,
  9927. .y2 = crtc_y + crtc_h,
  9928. };
  9929. struct drm_rect src = {
  9930. /* 16.16 fixed point */
  9931. .x1 = src_x,
  9932. .y1 = src_y,
  9933. .x2 = src_x + src_w,
  9934. .y2 = src_y + src_h,
  9935. };
  9936. const struct drm_rect clip = {
  9937. /* integer pixels */
  9938. .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
  9939. .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
  9940. };
  9941. bool visible;
  9942. int ret;
  9943. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9944. &src, &dest, &clip,
  9945. DRM_PLANE_HELPER_NO_SCALING,
  9946. DRM_PLANE_HELPER_NO_SCALING,
  9947. true, true, &visible);
  9948. if (ret)
  9949. return ret;
  9950. crtc->cursor_x = crtc_x;
  9951. crtc->cursor_y = crtc_y;
  9952. if (fb != crtc->cursor->fb) {
  9953. return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
  9954. } else {
  9955. intel_crtc_update_cursor(crtc, visible);
  9956. intel_frontbuffer_flip(crtc->dev,
  9957. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
  9958. return 0;
  9959. }
  9960. }
  9961. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  9962. .update_plane = intel_cursor_plane_update,
  9963. .disable_plane = intel_cursor_plane_disable,
  9964. .destroy = intel_plane_destroy,
  9965. };
  9966. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  9967. int pipe)
  9968. {
  9969. struct intel_plane *cursor;
  9970. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  9971. if (cursor == NULL)
  9972. return NULL;
  9973. cursor->can_scale = false;
  9974. cursor->max_downscale = 1;
  9975. cursor->pipe = pipe;
  9976. cursor->plane = pipe;
  9977. drm_universal_plane_init(dev, &cursor->base, 0,
  9978. &intel_cursor_plane_funcs,
  9979. intel_cursor_formats,
  9980. ARRAY_SIZE(intel_cursor_formats),
  9981. DRM_PLANE_TYPE_CURSOR);
  9982. return &cursor->base;
  9983. }
  9984. static void intel_crtc_init(struct drm_device *dev, int pipe)
  9985. {
  9986. struct drm_i915_private *dev_priv = dev->dev_private;
  9987. struct intel_crtc *intel_crtc;
  9988. struct drm_plane *primary = NULL;
  9989. struct drm_plane *cursor = NULL;
  9990. int i, ret;
  9991. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  9992. if (intel_crtc == NULL)
  9993. return;
  9994. primary = intel_primary_plane_create(dev, pipe);
  9995. if (!primary)
  9996. goto fail;
  9997. cursor = intel_cursor_plane_create(dev, pipe);
  9998. if (!cursor)
  9999. goto fail;
  10000. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  10001. cursor, &intel_crtc_funcs);
  10002. if (ret)
  10003. goto fail;
  10004. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  10005. for (i = 0; i < 256; i++) {
  10006. intel_crtc->lut_r[i] = i;
  10007. intel_crtc->lut_g[i] = i;
  10008. intel_crtc->lut_b[i] = i;
  10009. }
  10010. /*
  10011. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  10012. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  10013. */
  10014. intel_crtc->pipe = pipe;
  10015. intel_crtc->plane = pipe;
  10016. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  10017. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  10018. intel_crtc->plane = !pipe;
  10019. }
  10020. intel_crtc->cursor_base = ~0;
  10021. intel_crtc->cursor_cntl = ~0;
  10022. intel_crtc->cursor_size = ~0;
  10023. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  10024. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  10025. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  10026. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  10027. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  10028. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  10029. return;
  10030. fail:
  10031. if (primary)
  10032. drm_plane_cleanup(primary);
  10033. if (cursor)
  10034. drm_plane_cleanup(cursor);
  10035. kfree(intel_crtc);
  10036. }
  10037. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  10038. {
  10039. struct drm_encoder *encoder = connector->base.encoder;
  10040. struct drm_device *dev = connector->base.dev;
  10041. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  10042. if (!encoder)
  10043. return INVALID_PIPE;
  10044. return to_intel_crtc(encoder->crtc)->pipe;
  10045. }
  10046. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  10047. struct drm_file *file)
  10048. {
  10049. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  10050. struct drm_crtc *drmmode_crtc;
  10051. struct intel_crtc *crtc;
  10052. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  10053. return -ENODEV;
  10054. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  10055. if (!drmmode_crtc) {
  10056. DRM_ERROR("no such CRTC id\n");
  10057. return -ENOENT;
  10058. }
  10059. crtc = to_intel_crtc(drmmode_crtc);
  10060. pipe_from_crtc_id->pipe = crtc->pipe;
  10061. return 0;
  10062. }
  10063. static int intel_encoder_clones(struct intel_encoder *encoder)
  10064. {
  10065. struct drm_device *dev = encoder->base.dev;
  10066. struct intel_encoder *source_encoder;
  10067. int index_mask = 0;
  10068. int entry = 0;
  10069. for_each_intel_encoder(dev, source_encoder) {
  10070. if (encoders_cloneable(encoder, source_encoder))
  10071. index_mask |= (1 << entry);
  10072. entry++;
  10073. }
  10074. return index_mask;
  10075. }
  10076. static bool has_edp_a(struct drm_device *dev)
  10077. {
  10078. struct drm_i915_private *dev_priv = dev->dev_private;
  10079. if (!IS_MOBILE(dev))
  10080. return false;
  10081. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  10082. return false;
  10083. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  10084. return false;
  10085. return true;
  10086. }
  10087. const char *intel_output_name(int output)
  10088. {
  10089. static const char *names[] = {
  10090. [INTEL_OUTPUT_UNUSED] = "Unused",
  10091. [INTEL_OUTPUT_ANALOG] = "Analog",
  10092. [INTEL_OUTPUT_DVO] = "DVO",
  10093. [INTEL_OUTPUT_SDVO] = "SDVO",
  10094. [INTEL_OUTPUT_LVDS] = "LVDS",
  10095. [INTEL_OUTPUT_TVOUT] = "TV",
  10096. [INTEL_OUTPUT_HDMI] = "HDMI",
  10097. [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
  10098. [INTEL_OUTPUT_EDP] = "eDP",
  10099. [INTEL_OUTPUT_DSI] = "DSI",
  10100. [INTEL_OUTPUT_UNKNOWN] = "Unknown",
  10101. };
  10102. if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
  10103. return "Invalid";
  10104. return names[output];
  10105. }
  10106. static bool intel_crt_present(struct drm_device *dev)
  10107. {
  10108. struct drm_i915_private *dev_priv = dev->dev_private;
  10109. if (IS_ULT(dev))
  10110. return false;
  10111. if (IS_CHERRYVIEW(dev))
  10112. return false;
  10113. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10114. return false;
  10115. return true;
  10116. }
  10117. static void intel_setup_outputs(struct drm_device *dev)
  10118. {
  10119. struct drm_i915_private *dev_priv = dev->dev_private;
  10120. struct intel_encoder *encoder;
  10121. bool dpd_is_edp = false;
  10122. intel_lvds_init(dev);
  10123. if (intel_crt_present(dev))
  10124. intel_crt_init(dev);
  10125. if (HAS_DDI(dev)) {
  10126. int found;
  10127. /* Haswell uses DDI functions to detect digital outputs */
  10128. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10129. /* DDI A only supports eDP */
  10130. if (found)
  10131. intel_ddi_init(dev, PORT_A);
  10132. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10133. * register */
  10134. found = I915_READ(SFUSE_STRAP);
  10135. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10136. intel_ddi_init(dev, PORT_B);
  10137. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10138. intel_ddi_init(dev, PORT_C);
  10139. if (found & SFUSE_STRAP_DDID_DETECTED)
  10140. intel_ddi_init(dev, PORT_D);
  10141. } else if (HAS_PCH_SPLIT(dev)) {
  10142. int found;
  10143. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10144. if (has_edp_a(dev))
  10145. intel_dp_init(dev, DP_A, PORT_A);
  10146. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10147. /* PCH SDVOB multiplex with HDMIB */
  10148. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10149. if (!found)
  10150. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10151. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10152. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10153. }
  10154. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10155. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10156. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10157. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10158. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10159. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10160. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10161. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10162. } else if (IS_VALLEYVIEW(dev)) {
  10163. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
  10164. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10165. PORT_B);
  10166. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
  10167. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10168. }
  10169. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
  10170. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10171. PORT_C);
  10172. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
  10173. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10174. }
  10175. if (IS_CHERRYVIEW(dev)) {
  10176. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
  10177. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10178. PORT_D);
  10179. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10180. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10181. }
  10182. }
  10183. intel_dsi_init(dev);
  10184. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10185. bool found = false;
  10186. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10187. DRM_DEBUG_KMS("probing SDVOB\n");
  10188. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10189. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10190. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10191. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10192. }
  10193. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10194. intel_dp_init(dev, DP_B, PORT_B);
  10195. }
  10196. /* Before G4X SDVOC doesn't have its own detect register */
  10197. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10198. DRM_DEBUG_KMS("probing SDVOC\n");
  10199. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10200. }
  10201. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10202. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10203. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10204. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10205. }
  10206. if (SUPPORTS_INTEGRATED_DP(dev))
  10207. intel_dp_init(dev, DP_C, PORT_C);
  10208. }
  10209. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10210. (I915_READ(DP_D) & DP_DETECTED))
  10211. intel_dp_init(dev, DP_D, PORT_D);
  10212. } else if (IS_GEN2(dev))
  10213. intel_dvo_init(dev);
  10214. if (SUPPORTS_TV(dev))
  10215. intel_tv_init(dev);
  10216. intel_edp_psr_init(dev);
  10217. for_each_intel_encoder(dev, encoder) {
  10218. encoder->base.possible_crtcs = encoder->crtc_mask;
  10219. encoder->base.possible_clones =
  10220. intel_encoder_clones(encoder);
  10221. }
  10222. intel_init_pch_refclk(dev);
  10223. drm_helper_move_panel_connectors_to_head(dev);
  10224. }
  10225. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10226. {
  10227. struct drm_device *dev = fb->dev;
  10228. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10229. drm_framebuffer_cleanup(fb);
  10230. mutex_lock(&dev->struct_mutex);
  10231. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10232. drm_gem_object_unreference(&intel_fb->obj->base);
  10233. mutex_unlock(&dev->struct_mutex);
  10234. kfree(intel_fb);
  10235. }
  10236. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10237. struct drm_file *file,
  10238. unsigned int *handle)
  10239. {
  10240. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10241. struct drm_i915_gem_object *obj = intel_fb->obj;
  10242. return drm_gem_handle_create(file, &obj->base, handle);
  10243. }
  10244. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10245. .destroy = intel_user_framebuffer_destroy,
  10246. .create_handle = intel_user_framebuffer_create_handle,
  10247. };
  10248. static int intel_framebuffer_init(struct drm_device *dev,
  10249. struct intel_framebuffer *intel_fb,
  10250. struct drm_mode_fb_cmd2 *mode_cmd,
  10251. struct drm_i915_gem_object *obj)
  10252. {
  10253. int aligned_height;
  10254. int pitch_limit;
  10255. int ret;
  10256. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10257. if (obj->tiling_mode == I915_TILING_Y) {
  10258. DRM_DEBUG("hardware does not support tiling Y\n");
  10259. return -EINVAL;
  10260. }
  10261. if (mode_cmd->pitches[0] & 63) {
  10262. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  10263. mode_cmd->pitches[0]);
  10264. return -EINVAL;
  10265. }
  10266. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10267. pitch_limit = 32*1024;
  10268. } else if (INTEL_INFO(dev)->gen >= 4) {
  10269. if (obj->tiling_mode)
  10270. pitch_limit = 16*1024;
  10271. else
  10272. pitch_limit = 32*1024;
  10273. } else if (INTEL_INFO(dev)->gen >= 3) {
  10274. if (obj->tiling_mode)
  10275. pitch_limit = 8*1024;
  10276. else
  10277. pitch_limit = 16*1024;
  10278. } else
  10279. /* XXX DSPC is limited to 4k tiled */
  10280. pitch_limit = 8*1024;
  10281. if (mode_cmd->pitches[0] > pitch_limit) {
  10282. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  10283. obj->tiling_mode ? "tiled" : "linear",
  10284. mode_cmd->pitches[0], pitch_limit);
  10285. return -EINVAL;
  10286. }
  10287. if (obj->tiling_mode != I915_TILING_NONE &&
  10288. mode_cmd->pitches[0] != obj->stride) {
  10289. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10290. mode_cmd->pitches[0], obj->stride);
  10291. return -EINVAL;
  10292. }
  10293. /* Reject formats not supported by any plane early. */
  10294. switch (mode_cmd->pixel_format) {
  10295. case DRM_FORMAT_C8:
  10296. case DRM_FORMAT_RGB565:
  10297. case DRM_FORMAT_XRGB8888:
  10298. case DRM_FORMAT_ARGB8888:
  10299. break;
  10300. case DRM_FORMAT_XRGB1555:
  10301. case DRM_FORMAT_ARGB1555:
  10302. if (INTEL_INFO(dev)->gen > 3) {
  10303. DRM_DEBUG("unsupported pixel format: %s\n",
  10304. drm_get_format_name(mode_cmd->pixel_format));
  10305. return -EINVAL;
  10306. }
  10307. break;
  10308. case DRM_FORMAT_XBGR8888:
  10309. case DRM_FORMAT_ABGR8888:
  10310. case DRM_FORMAT_XRGB2101010:
  10311. case DRM_FORMAT_ARGB2101010:
  10312. case DRM_FORMAT_XBGR2101010:
  10313. case DRM_FORMAT_ABGR2101010:
  10314. if (INTEL_INFO(dev)->gen < 4) {
  10315. DRM_DEBUG("unsupported pixel format: %s\n",
  10316. drm_get_format_name(mode_cmd->pixel_format));
  10317. return -EINVAL;
  10318. }
  10319. break;
  10320. case DRM_FORMAT_YUYV:
  10321. case DRM_FORMAT_UYVY:
  10322. case DRM_FORMAT_YVYU:
  10323. case DRM_FORMAT_VYUY:
  10324. if (INTEL_INFO(dev)->gen < 5) {
  10325. DRM_DEBUG("unsupported pixel format: %s\n",
  10326. drm_get_format_name(mode_cmd->pixel_format));
  10327. return -EINVAL;
  10328. }
  10329. break;
  10330. default:
  10331. DRM_DEBUG("unsupported pixel format: %s\n",
  10332. drm_get_format_name(mode_cmd->pixel_format));
  10333. return -EINVAL;
  10334. }
  10335. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10336. if (mode_cmd->offsets[0] != 0)
  10337. return -EINVAL;
  10338. aligned_height = intel_align_height(dev, mode_cmd->height,
  10339. obj->tiling_mode);
  10340. /* FIXME drm helper for size checks (especially planar formats)? */
  10341. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10342. return -EINVAL;
  10343. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10344. intel_fb->obj = obj;
  10345. intel_fb->obj->framebuffer_references++;
  10346. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10347. if (ret) {
  10348. DRM_ERROR("framebuffer init failed %d\n", ret);
  10349. return ret;
  10350. }
  10351. return 0;
  10352. }
  10353. static struct drm_framebuffer *
  10354. intel_user_framebuffer_create(struct drm_device *dev,
  10355. struct drm_file *filp,
  10356. struct drm_mode_fb_cmd2 *mode_cmd)
  10357. {
  10358. struct drm_i915_gem_object *obj;
  10359. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10360. mode_cmd->handles[0]));
  10361. if (&obj->base == NULL)
  10362. return ERR_PTR(-ENOENT);
  10363. return intel_framebuffer_create(dev, mode_cmd, obj);
  10364. }
  10365. #ifndef CONFIG_DRM_I915_FBDEV
  10366. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10367. {
  10368. }
  10369. #endif
  10370. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10371. .fb_create = intel_user_framebuffer_create,
  10372. .output_poll_changed = intel_fbdev_output_poll_changed,
  10373. };
  10374. /* Set up chip specific display functions */
  10375. static void intel_init_display(struct drm_device *dev)
  10376. {
  10377. struct drm_i915_private *dev_priv = dev->dev_private;
  10378. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10379. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10380. else if (IS_CHERRYVIEW(dev))
  10381. dev_priv->display.find_dpll = chv_find_best_dpll;
  10382. else if (IS_VALLEYVIEW(dev))
  10383. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10384. else if (IS_PINEVIEW(dev))
  10385. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10386. else
  10387. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10388. if (HAS_DDI(dev)) {
  10389. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10390. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10391. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  10392. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10393. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10394. dev_priv->display.off = ironlake_crtc_off;
  10395. dev_priv->display.update_primary_plane =
  10396. ironlake_update_primary_plane;
  10397. } else if (HAS_PCH_SPLIT(dev)) {
  10398. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10399. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  10400. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  10401. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10402. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  10403. dev_priv->display.off = ironlake_crtc_off;
  10404. dev_priv->display.update_primary_plane =
  10405. ironlake_update_primary_plane;
  10406. } else if (IS_VALLEYVIEW(dev)) {
  10407. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10408. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10409. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  10410. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10411. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10412. dev_priv->display.off = i9xx_crtc_off;
  10413. dev_priv->display.update_primary_plane =
  10414. i9xx_update_primary_plane;
  10415. } else {
  10416. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10417. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  10418. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  10419. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  10420. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10421. dev_priv->display.off = i9xx_crtc_off;
  10422. dev_priv->display.update_primary_plane =
  10423. i9xx_update_primary_plane;
  10424. }
  10425. /* Returns the core display clock speed */
  10426. if (IS_VALLEYVIEW(dev))
  10427. dev_priv->display.get_display_clock_speed =
  10428. valleyview_get_display_clock_speed;
  10429. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10430. dev_priv->display.get_display_clock_speed =
  10431. i945_get_display_clock_speed;
  10432. else if (IS_I915G(dev))
  10433. dev_priv->display.get_display_clock_speed =
  10434. i915_get_display_clock_speed;
  10435. else if (IS_I945GM(dev) || IS_845G(dev))
  10436. dev_priv->display.get_display_clock_speed =
  10437. i9xx_misc_get_display_clock_speed;
  10438. else if (IS_PINEVIEW(dev))
  10439. dev_priv->display.get_display_clock_speed =
  10440. pnv_get_display_clock_speed;
  10441. else if (IS_I915GM(dev))
  10442. dev_priv->display.get_display_clock_speed =
  10443. i915gm_get_display_clock_speed;
  10444. else if (IS_I865G(dev))
  10445. dev_priv->display.get_display_clock_speed =
  10446. i865_get_display_clock_speed;
  10447. else if (IS_I85X(dev))
  10448. dev_priv->display.get_display_clock_speed =
  10449. i855_get_display_clock_speed;
  10450. else /* 852, 830 */
  10451. dev_priv->display.get_display_clock_speed =
  10452. i830_get_display_clock_speed;
  10453. if (IS_G4X(dev)) {
  10454. dev_priv->display.write_eld = g4x_write_eld;
  10455. } else if (IS_GEN5(dev)) {
  10456. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10457. dev_priv->display.write_eld = ironlake_write_eld;
  10458. } else if (IS_GEN6(dev)) {
  10459. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10460. dev_priv->display.write_eld = ironlake_write_eld;
  10461. dev_priv->display.modeset_global_resources =
  10462. snb_modeset_global_resources;
  10463. } else if (IS_IVYBRIDGE(dev)) {
  10464. /* FIXME: detect B0+ stepping and use auto training */
  10465. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10466. dev_priv->display.write_eld = ironlake_write_eld;
  10467. dev_priv->display.modeset_global_resources =
  10468. ivb_modeset_global_resources;
  10469. } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
  10470. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10471. dev_priv->display.write_eld = haswell_write_eld;
  10472. dev_priv->display.modeset_global_resources =
  10473. haswell_modeset_global_resources;
  10474. } else if (IS_VALLEYVIEW(dev)) {
  10475. dev_priv->display.modeset_global_resources =
  10476. valleyview_modeset_global_resources;
  10477. dev_priv->display.write_eld = ironlake_write_eld;
  10478. }
  10479. /* Default just returns -ENODEV to indicate unsupported */
  10480. dev_priv->display.queue_flip = intel_default_queue_flip;
  10481. switch (INTEL_INFO(dev)->gen) {
  10482. case 2:
  10483. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10484. break;
  10485. case 3:
  10486. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  10487. break;
  10488. case 4:
  10489. case 5:
  10490. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  10491. break;
  10492. case 6:
  10493. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  10494. break;
  10495. case 7:
  10496. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  10497. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  10498. break;
  10499. }
  10500. intel_panel_init_backlight_funcs(dev);
  10501. }
  10502. /*
  10503. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  10504. * resume, or other times. This quirk makes sure that's the case for
  10505. * affected systems.
  10506. */
  10507. static void quirk_pipea_force(struct drm_device *dev)
  10508. {
  10509. struct drm_i915_private *dev_priv = dev->dev_private;
  10510. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  10511. DRM_INFO("applying pipe a force quirk\n");
  10512. }
  10513. static void quirk_pipeb_force(struct drm_device *dev)
  10514. {
  10515. struct drm_i915_private *dev_priv = dev->dev_private;
  10516. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  10517. DRM_INFO("applying pipe b force quirk\n");
  10518. }
  10519. /*
  10520. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  10521. */
  10522. static void quirk_ssc_force_disable(struct drm_device *dev)
  10523. {
  10524. struct drm_i915_private *dev_priv = dev->dev_private;
  10525. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  10526. DRM_INFO("applying lvds SSC disable quirk\n");
  10527. }
  10528. /*
  10529. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  10530. * brightness value
  10531. */
  10532. static void quirk_invert_brightness(struct drm_device *dev)
  10533. {
  10534. struct drm_i915_private *dev_priv = dev->dev_private;
  10535. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  10536. DRM_INFO("applying inverted panel brightness quirk\n");
  10537. }
  10538. /* Some VBT's incorrectly indicate no backlight is present */
  10539. static void quirk_backlight_present(struct drm_device *dev)
  10540. {
  10541. struct drm_i915_private *dev_priv = dev->dev_private;
  10542. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  10543. DRM_INFO("applying backlight present quirk\n");
  10544. }
  10545. struct intel_quirk {
  10546. int device;
  10547. int subsystem_vendor;
  10548. int subsystem_device;
  10549. void (*hook)(struct drm_device *dev);
  10550. };
  10551. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  10552. struct intel_dmi_quirk {
  10553. void (*hook)(struct drm_device *dev);
  10554. const struct dmi_system_id (*dmi_id_list)[];
  10555. };
  10556. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  10557. {
  10558. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  10559. return 1;
  10560. }
  10561. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  10562. {
  10563. .dmi_id_list = &(const struct dmi_system_id[]) {
  10564. {
  10565. .callback = intel_dmi_reverse_brightness,
  10566. .ident = "NCR Corporation",
  10567. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  10568. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  10569. },
  10570. },
  10571. { } /* terminating entry */
  10572. },
  10573. .hook = quirk_invert_brightness,
  10574. },
  10575. };
  10576. static struct intel_quirk intel_quirks[] = {
  10577. /* HP Mini needs pipe A force quirk (LP: #322104) */
  10578. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  10579. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  10580. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  10581. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  10582. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  10583. /* 830 needs to leave pipe A & dpll A up */
  10584. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  10585. /* 830 needs to leave pipe B & dpll B up */
  10586. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  10587. /* Lenovo U160 cannot use SSC on LVDS */
  10588. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  10589. /* Sony Vaio Y cannot use SSC on LVDS */
  10590. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  10591. /* Acer Aspire 5734Z must invert backlight brightness */
  10592. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  10593. /* Acer/eMachines G725 */
  10594. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  10595. /* Acer/eMachines e725 */
  10596. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  10597. /* Acer/Packard Bell NCL20 */
  10598. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  10599. /* Acer Aspire 4736Z */
  10600. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  10601. /* Acer Aspire 5336 */
  10602. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  10603. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  10604. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  10605. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  10606. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  10607. /* HP Chromebook 14 (Celeron 2955U) */
  10608. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  10609. };
  10610. static void intel_init_quirks(struct drm_device *dev)
  10611. {
  10612. struct pci_dev *d = dev->pdev;
  10613. int i;
  10614. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  10615. struct intel_quirk *q = &intel_quirks[i];
  10616. if (d->device == q->device &&
  10617. (d->subsystem_vendor == q->subsystem_vendor ||
  10618. q->subsystem_vendor == PCI_ANY_ID) &&
  10619. (d->subsystem_device == q->subsystem_device ||
  10620. q->subsystem_device == PCI_ANY_ID))
  10621. q->hook(dev);
  10622. }
  10623. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  10624. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  10625. intel_dmi_quirks[i].hook(dev);
  10626. }
  10627. }
  10628. /* Disable the VGA plane that we never use */
  10629. static void i915_disable_vga(struct drm_device *dev)
  10630. {
  10631. struct drm_i915_private *dev_priv = dev->dev_private;
  10632. u8 sr1;
  10633. u32 vga_reg = i915_vgacntrl_reg(dev);
  10634. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  10635. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  10636. outb(SR01, VGA_SR_INDEX);
  10637. sr1 = inb(VGA_SR_DATA);
  10638. outb(sr1 | 1<<5, VGA_SR_DATA);
  10639. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  10640. udelay(300);
  10641. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  10642. POSTING_READ(vga_reg);
  10643. }
  10644. void intel_modeset_init_hw(struct drm_device *dev)
  10645. {
  10646. intel_prepare_ddi(dev);
  10647. if (IS_VALLEYVIEW(dev))
  10648. vlv_update_cdclk(dev);
  10649. intel_init_clock_gating(dev);
  10650. intel_enable_gt_powersave(dev);
  10651. }
  10652. void intel_modeset_suspend_hw(struct drm_device *dev)
  10653. {
  10654. intel_suspend_hw(dev);
  10655. }
  10656. void intel_modeset_init(struct drm_device *dev)
  10657. {
  10658. struct drm_i915_private *dev_priv = dev->dev_private;
  10659. int sprite, ret;
  10660. enum pipe pipe;
  10661. struct intel_crtc *crtc;
  10662. drm_mode_config_init(dev);
  10663. dev->mode_config.min_width = 0;
  10664. dev->mode_config.min_height = 0;
  10665. dev->mode_config.preferred_depth = 24;
  10666. dev->mode_config.prefer_shadow = 1;
  10667. dev->mode_config.funcs = &intel_mode_funcs;
  10668. intel_init_quirks(dev);
  10669. intel_init_pm(dev);
  10670. if (INTEL_INFO(dev)->num_pipes == 0)
  10671. return;
  10672. intel_init_display(dev);
  10673. if (IS_GEN2(dev)) {
  10674. dev->mode_config.max_width = 2048;
  10675. dev->mode_config.max_height = 2048;
  10676. } else if (IS_GEN3(dev)) {
  10677. dev->mode_config.max_width = 4096;
  10678. dev->mode_config.max_height = 4096;
  10679. } else {
  10680. dev->mode_config.max_width = 8192;
  10681. dev->mode_config.max_height = 8192;
  10682. }
  10683. if (IS_845G(dev) || IS_I865G(dev)) {
  10684. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  10685. dev->mode_config.cursor_height = 1023;
  10686. } else if (IS_GEN2(dev)) {
  10687. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  10688. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  10689. } else {
  10690. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  10691. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  10692. }
  10693. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  10694. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  10695. INTEL_INFO(dev)->num_pipes,
  10696. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  10697. for_each_pipe(dev_priv, pipe) {
  10698. intel_crtc_init(dev, pipe);
  10699. for_each_sprite(pipe, sprite) {
  10700. ret = intel_plane_init(dev, pipe, sprite);
  10701. if (ret)
  10702. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  10703. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  10704. }
  10705. }
  10706. intel_init_dpio(dev);
  10707. intel_shared_dpll_init(dev);
  10708. /* Just disable it once at startup */
  10709. i915_disable_vga(dev);
  10710. intel_setup_outputs(dev);
  10711. /* Just in case the BIOS is doing something questionable. */
  10712. intel_disable_fbc(dev);
  10713. drm_modeset_lock_all(dev);
  10714. intel_modeset_setup_hw_state(dev, false);
  10715. drm_modeset_unlock_all(dev);
  10716. for_each_intel_crtc(dev, crtc) {
  10717. if (!crtc->active)
  10718. continue;
  10719. /*
  10720. * Note that reserving the BIOS fb up front prevents us
  10721. * from stuffing other stolen allocations like the ring
  10722. * on top. This prevents some ugliness at boot time, and
  10723. * can even allow for smooth boot transitions if the BIOS
  10724. * fb is large enough for the active pipe configuration.
  10725. */
  10726. if (dev_priv->display.get_plane_config) {
  10727. dev_priv->display.get_plane_config(crtc,
  10728. &crtc->plane_config);
  10729. /*
  10730. * If the fb is shared between multiple heads, we'll
  10731. * just get the first one.
  10732. */
  10733. intel_find_plane_obj(crtc, &crtc->plane_config);
  10734. }
  10735. }
  10736. }
  10737. static void intel_enable_pipe_a(struct drm_device *dev)
  10738. {
  10739. struct intel_connector *connector;
  10740. struct drm_connector *crt = NULL;
  10741. struct intel_load_detect_pipe load_detect_temp;
  10742. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  10743. /* We can't just switch on the pipe A, we need to set things up with a
  10744. * proper mode and output configuration. As a gross hack, enable pipe A
  10745. * by enabling the load detect pipe once. */
  10746. list_for_each_entry(connector,
  10747. &dev->mode_config.connector_list,
  10748. base.head) {
  10749. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  10750. crt = &connector->base;
  10751. break;
  10752. }
  10753. }
  10754. if (!crt)
  10755. return;
  10756. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  10757. intel_release_load_detect_pipe(crt, &load_detect_temp);
  10758. }
  10759. static bool
  10760. intel_check_plane_mapping(struct intel_crtc *crtc)
  10761. {
  10762. struct drm_device *dev = crtc->base.dev;
  10763. struct drm_i915_private *dev_priv = dev->dev_private;
  10764. u32 reg, val;
  10765. if (INTEL_INFO(dev)->num_pipes == 1)
  10766. return true;
  10767. reg = DSPCNTR(!crtc->plane);
  10768. val = I915_READ(reg);
  10769. if ((val & DISPLAY_PLANE_ENABLE) &&
  10770. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  10771. return false;
  10772. return true;
  10773. }
  10774. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  10775. {
  10776. struct drm_device *dev = crtc->base.dev;
  10777. struct drm_i915_private *dev_priv = dev->dev_private;
  10778. u32 reg;
  10779. /* Clear any frame start delays used for debugging left by the BIOS */
  10780. reg = PIPECONF(crtc->config.cpu_transcoder);
  10781. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  10782. /* restore vblank interrupts to correct state */
  10783. if (crtc->active)
  10784. drm_vblank_on(dev, crtc->pipe);
  10785. else
  10786. drm_vblank_off(dev, crtc->pipe);
  10787. /* We need to sanitize the plane -> pipe mapping first because this will
  10788. * disable the crtc (and hence change the state) if it is wrong. Note
  10789. * that gen4+ has a fixed plane -> pipe mapping. */
  10790. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  10791. struct intel_connector *connector;
  10792. bool plane;
  10793. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  10794. crtc->base.base.id);
  10795. /* Pipe has the wrong plane attached and the plane is active.
  10796. * Temporarily change the plane mapping and disable everything
  10797. * ... */
  10798. plane = crtc->plane;
  10799. crtc->plane = !plane;
  10800. crtc->primary_enabled = true;
  10801. dev_priv->display.crtc_disable(&crtc->base);
  10802. crtc->plane = plane;
  10803. /* ... and break all links. */
  10804. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10805. base.head) {
  10806. if (connector->encoder->base.crtc != &crtc->base)
  10807. continue;
  10808. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10809. connector->base.encoder = NULL;
  10810. }
  10811. /* multiple connectors may have the same encoder:
  10812. * handle them and break crtc link separately */
  10813. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10814. base.head)
  10815. if (connector->encoder->base.crtc == &crtc->base) {
  10816. connector->encoder->base.crtc = NULL;
  10817. connector->encoder->connectors_active = false;
  10818. }
  10819. WARN_ON(crtc->active);
  10820. crtc->base.enabled = false;
  10821. }
  10822. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  10823. crtc->pipe == PIPE_A && !crtc->active) {
  10824. /* BIOS forgot to enable pipe A, this mostly happens after
  10825. * resume. Force-enable the pipe to fix this, the update_dpms
  10826. * call below we restore the pipe to the right state, but leave
  10827. * the required bits on. */
  10828. intel_enable_pipe_a(dev);
  10829. }
  10830. /* Adjust the state of the output pipe according to whether we
  10831. * have active connectors/encoders. */
  10832. intel_crtc_update_dpms(&crtc->base);
  10833. if (crtc->active != crtc->base.enabled) {
  10834. struct intel_encoder *encoder;
  10835. /* This can happen either due to bugs in the get_hw_state
  10836. * functions or because the pipe is force-enabled due to the
  10837. * pipe A quirk. */
  10838. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  10839. crtc->base.base.id,
  10840. crtc->base.enabled ? "enabled" : "disabled",
  10841. crtc->active ? "enabled" : "disabled");
  10842. crtc->base.enabled = crtc->active;
  10843. /* Because we only establish the connector -> encoder ->
  10844. * crtc links if something is active, this means the
  10845. * crtc is now deactivated. Break the links. connector
  10846. * -> encoder links are only establish when things are
  10847. * actually up, hence no need to break them. */
  10848. WARN_ON(crtc->active);
  10849. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  10850. WARN_ON(encoder->connectors_active);
  10851. encoder->base.crtc = NULL;
  10852. }
  10853. }
  10854. if (crtc->active || IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen < 5) {
  10855. /*
  10856. * We start out with underrun reporting disabled to avoid races.
  10857. * For correct bookkeeping mark this on active crtcs.
  10858. *
  10859. * Also on gmch platforms we dont have any hardware bits to
  10860. * disable the underrun reporting. Which means we need to start
  10861. * out with underrun reporting disabled also on inactive pipes,
  10862. * since otherwise we'll complain about the garbage we read when
  10863. * e.g. coming up after runtime pm.
  10864. *
  10865. * No protection against concurrent access is required - at
  10866. * worst a fifo underrun happens which also sets this to false.
  10867. */
  10868. crtc->cpu_fifo_underrun_disabled = true;
  10869. crtc->pch_fifo_underrun_disabled = true;
  10870. update_scanline_offset(crtc);
  10871. }
  10872. }
  10873. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  10874. {
  10875. struct intel_connector *connector;
  10876. struct drm_device *dev = encoder->base.dev;
  10877. /* We need to check both for a crtc link (meaning that the
  10878. * encoder is active and trying to read from a pipe) and the
  10879. * pipe itself being active. */
  10880. bool has_active_crtc = encoder->base.crtc &&
  10881. to_intel_crtc(encoder->base.crtc)->active;
  10882. if (encoder->connectors_active && !has_active_crtc) {
  10883. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  10884. encoder->base.base.id,
  10885. encoder->base.name);
  10886. /* Connector is active, but has no active pipe. This is
  10887. * fallout from our resume register restoring. Disable
  10888. * the encoder manually again. */
  10889. if (encoder->base.crtc) {
  10890. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  10891. encoder->base.base.id,
  10892. encoder->base.name);
  10893. encoder->disable(encoder);
  10894. if (encoder->post_disable)
  10895. encoder->post_disable(encoder);
  10896. }
  10897. encoder->base.crtc = NULL;
  10898. encoder->connectors_active = false;
  10899. /* Inconsistent output/port/pipe state happens presumably due to
  10900. * a bug in one of the get_hw_state functions. Or someplace else
  10901. * in our code, like the register restore mess on resume. Clamp
  10902. * things to off as a safer default. */
  10903. list_for_each_entry(connector,
  10904. &dev->mode_config.connector_list,
  10905. base.head) {
  10906. if (connector->encoder != encoder)
  10907. continue;
  10908. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10909. connector->base.encoder = NULL;
  10910. }
  10911. }
  10912. /* Enabled encoders without active connectors will be fixed in
  10913. * the crtc fixup. */
  10914. }
  10915. void i915_redisable_vga_power_on(struct drm_device *dev)
  10916. {
  10917. struct drm_i915_private *dev_priv = dev->dev_private;
  10918. u32 vga_reg = i915_vgacntrl_reg(dev);
  10919. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  10920. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  10921. i915_disable_vga(dev);
  10922. }
  10923. }
  10924. void i915_redisable_vga(struct drm_device *dev)
  10925. {
  10926. struct drm_i915_private *dev_priv = dev->dev_private;
  10927. /* This function can be called both from intel_modeset_setup_hw_state or
  10928. * at a very early point in our resume sequence, where the power well
  10929. * structures are not yet restored. Since this function is at a very
  10930. * paranoid "someone might have enabled VGA while we were not looking"
  10931. * level, just check if the power well is enabled instead of trying to
  10932. * follow the "don't touch the power well if we don't need it" policy
  10933. * the rest of the driver uses. */
  10934. if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
  10935. return;
  10936. i915_redisable_vga_power_on(dev);
  10937. }
  10938. static bool primary_get_hw_state(struct intel_crtc *crtc)
  10939. {
  10940. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  10941. if (!crtc->active)
  10942. return false;
  10943. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  10944. }
  10945. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  10946. {
  10947. struct drm_i915_private *dev_priv = dev->dev_private;
  10948. enum pipe pipe;
  10949. struct intel_crtc *crtc;
  10950. struct intel_encoder *encoder;
  10951. struct intel_connector *connector;
  10952. int i;
  10953. for_each_intel_crtc(dev, crtc) {
  10954. memset(&crtc->config, 0, sizeof(crtc->config));
  10955. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  10956. crtc->active = dev_priv->display.get_pipe_config(crtc,
  10957. &crtc->config);
  10958. crtc->base.enabled = crtc->active;
  10959. crtc->primary_enabled = primary_get_hw_state(crtc);
  10960. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  10961. crtc->base.base.id,
  10962. crtc->active ? "enabled" : "disabled");
  10963. }
  10964. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10965. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10966. pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
  10967. pll->active = 0;
  10968. for_each_intel_crtc(dev, crtc) {
  10969. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10970. pll->active++;
  10971. }
  10972. pll->refcount = pll->active;
  10973. DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
  10974. pll->name, pll->refcount, pll->on);
  10975. if (pll->refcount)
  10976. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  10977. }
  10978. for_each_intel_encoder(dev, encoder) {
  10979. pipe = 0;
  10980. if (encoder->get_hw_state(encoder, &pipe)) {
  10981. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10982. encoder->base.crtc = &crtc->base;
  10983. encoder->get_config(encoder, &crtc->config);
  10984. } else {
  10985. encoder->base.crtc = NULL;
  10986. }
  10987. encoder->connectors_active = false;
  10988. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  10989. encoder->base.base.id,
  10990. encoder->base.name,
  10991. encoder->base.crtc ? "enabled" : "disabled",
  10992. pipe_name(pipe));
  10993. }
  10994. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10995. base.head) {
  10996. if (connector->get_hw_state(connector)) {
  10997. connector->base.dpms = DRM_MODE_DPMS_ON;
  10998. connector->encoder->connectors_active = true;
  10999. connector->base.encoder = &connector->encoder->base;
  11000. } else {
  11001. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11002. connector->base.encoder = NULL;
  11003. }
  11004. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  11005. connector->base.base.id,
  11006. connector->base.name,
  11007. connector->base.encoder ? "enabled" : "disabled");
  11008. }
  11009. }
  11010. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  11011. * and i915 state tracking structures. */
  11012. void intel_modeset_setup_hw_state(struct drm_device *dev,
  11013. bool force_restore)
  11014. {
  11015. struct drm_i915_private *dev_priv = dev->dev_private;
  11016. enum pipe pipe;
  11017. struct intel_crtc *crtc;
  11018. struct intel_encoder *encoder;
  11019. int i;
  11020. intel_modeset_readout_hw_state(dev);
  11021. /*
  11022. * Now that we have the config, copy it to each CRTC struct
  11023. * Note that this could go away if we move to using crtc_config
  11024. * checking everywhere.
  11025. */
  11026. for_each_intel_crtc(dev, crtc) {
  11027. if (crtc->active && i915.fastboot) {
  11028. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  11029. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11030. crtc->base.base.id);
  11031. drm_mode_debug_printmodeline(&crtc->base.mode);
  11032. }
  11033. }
  11034. /* HW state is read out, now we need to sanitize this mess. */
  11035. for_each_intel_encoder(dev, encoder) {
  11036. intel_sanitize_encoder(encoder);
  11037. }
  11038. for_each_pipe(dev_priv, pipe) {
  11039. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11040. intel_sanitize_crtc(crtc);
  11041. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  11042. }
  11043. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11044. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11045. if (!pll->on || pll->active)
  11046. continue;
  11047. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11048. pll->disable(dev_priv, pll);
  11049. pll->on = false;
  11050. }
  11051. if (HAS_PCH_SPLIT(dev))
  11052. ilk_wm_get_hw_state(dev);
  11053. if (force_restore) {
  11054. i915_redisable_vga(dev);
  11055. /*
  11056. * We need to use raw interfaces for restoring state to avoid
  11057. * checking (bogus) intermediate states.
  11058. */
  11059. for_each_pipe(dev_priv, pipe) {
  11060. struct drm_crtc *crtc =
  11061. dev_priv->pipe_to_crtc_mapping[pipe];
  11062. __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  11063. crtc->primary->fb);
  11064. }
  11065. } else {
  11066. intel_modeset_update_staged_output_state(dev);
  11067. }
  11068. intel_modeset_check_state(dev);
  11069. }
  11070. void intel_modeset_gem_init(struct drm_device *dev)
  11071. {
  11072. struct drm_crtc *c;
  11073. struct drm_i915_gem_object *obj;
  11074. mutex_lock(&dev->struct_mutex);
  11075. intel_init_gt_powersave(dev);
  11076. mutex_unlock(&dev->struct_mutex);
  11077. intel_modeset_init_hw(dev);
  11078. intel_setup_overlay(dev);
  11079. /*
  11080. * Make sure any fbs we allocated at startup are properly
  11081. * pinned & fenced. When we do the allocation it's too early
  11082. * for this.
  11083. */
  11084. mutex_lock(&dev->struct_mutex);
  11085. for_each_crtc(dev, c) {
  11086. obj = intel_fb_obj(c->primary->fb);
  11087. if (obj == NULL)
  11088. continue;
  11089. if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
  11090. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11091. to_intel_crtc(c)->pipe);
  11092. drm_framebuffer_unreference(c->primary->fb);
  11093. c->primary->fb = NULL;
  11094. }
  11095. }
  11096. mutex_unlock(&dev->struct_mutex);
  11097. }
  11098. void intel_connector_unregister(struct intel_connector *intel_connector)
  11099. {
  11100. struct drm_connector *connector = &intel_connector->base;
  11101. intel_panel_destroy_backlight(connector);
  11102. drm_connector_unregister(connector);
  11103. }
  11104. void intel_modeset_cleanup(struct drm_device *dev)
  11105. {
  11106. struct drm_i915_private *dev_priv = dev->dev_private;
  11107. struct drm_connector *connector;
  11108. /*
  11109. * Interrupts and polling as the first thing to avoid creating havoc.
  11110. * Too much stuff here (turning of rps, connectors, ...) would
  11111. * experience fancy races otherwise.
  11112. */
  11113. drm_irq_uninstall(dev);
  11114. intel_hpd_cancel_work(dev_priv);
  11115. dev_priv->pm._irqs_disabled = true;
  11116. /*
  11117. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11118. * poll handlers. Hence disable polling after hpd handling is shut down.
  11119. */
  11120. drm_kms_helper_poll_fini(dev);
  11121. mutex_lock(&dev->struct_mutex);
  11122. intel_unregister_dsm_handler();
  11123. intel_disable_fbc(dev);
  11124. intel_disable_gt_powersave(dev);
  11125. ironlake_teardown_rc6(dev);
  11126. mutex_unlock(&dev->struct_mutex);
  11127. /* flush any delayed tasks or pending work */
  11128. flush_scheduled_work();
  11129. /* destroy the backlight and sysfs files before encoders/connectors */
  11130. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11131. struct intel_connector *intel_connector;
  11132. intel_connector = to_intel_connector(connector);
  11133. intel_connector->unregister(intel_connector);
  11134. }
  11135. drm_mode_config_cleanup(dev);
  11136. intel_cleanup_overlay(dev);
  11137. mutex_lock(&dev->struct_mutex);
  11138. intel_cleanup_gt_powersave(dev);
  11139. mutex_unlock(&dev->struct_mutex);
  11140. }
  11141. /*
  11142. * Return which encoder is currently attached for connector.
  11143. */
  11144. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11145. {
  11146. return &intel_attached_encoder(connector)->base;
  11147. }
  11148. void intel_connector_attach_encoder(struct intel_connector *connector,
  11149. struct intel_encoder *encoder)
  11150. {
  11151. connector->encoder = encoder;
  11152. drm_mode_connector_attach_encoder(&connector->base,
  11153. &encoder->base);
  11154. }
  11155. /*
  11156. * set vga decode state - true == enable VGA decode
  11157. */
  11158. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11159. {
  11160. struct drm_i915_private *dev_priv = dev->dev_private;
  11161. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11162. u16 gmch_ctrl;
  11163. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11164. DRM_ERROR("failed to read control word\n");
  11165. return -EIO;
  11166. }
  11167. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11168. return 0;
  11169. if (state)
  11170. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11171. else
  11172. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11173. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11174. DRM_ERROR("failed to write control word\n");
  11175. return -EIO;
  11176. }
  11177. return 0;
  11178. }
  11179. struct intel_display_error_state {
  11180. u32 power_well_driver;
  11181. int num_transcoders;
  11182. struct intel_cursor_error_state {
  11183. u32 control;
  11184. u32 position;
  11185. u32 base;
  11186. u32 size;
  11187. } cursor[I915_MAX_PIPES];
  11188. struct intel_pipe_error_state {
  11189. bool power_domain_on;
  11190. u32 source;
  11191. u32 stat;
  11192. } pipe[I915_MAX_PIPES];
  11193. struct intel_plane_error_state {
  11194. u32 control;
  11195. u32 stride;
  11196. u32 size;
  11197. u32 pos;
  11198. u32 addr;
  11199. u32 surface;
  11200. u32 tile_offset;
  11201. } plane[I915_MAX_PIPES];
  11202. struct intel_transcoder_error_state {
  11203. bool power_domain_on;
  11204. enum transcoder cpu_transcoder;
  11205. u32 conf;
  11206. u32 htotal;
  11207. u32 hblank;
  11208. u32 hsync;
  11209. u32 vtotal;
  11210. u32 vblank;
  11211. u32 vsync;
  11212. } transcoder[4];
  11213. };
  11214. struct intel_display_error_state *
  11215. intel_display_capture_error_state(struct drm_device *dev)
  11216. {
  11217. struct drm_i915_private *dev_priv = dev->dev_private;
  11218. struct intel_display_error_state *error;
  11219. int transcoders[] = {
  11220. TRANSCODER_A,
  11221. TRANSCODER_B,
  11222. TRANSCODER_C,
  11223. TRANSCODER_EDP,
  11224. };
  11225. int i;
  11226. if (INTEL_INFO(dev)->num_pipes == 0)
  11227. return NULL;
  11228. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11229. if (error == NULL)
  11230. return NULL;
  11231. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11232. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11233. for_each_pipe(dev_priv, i) {
  11234. error->pipe[i].power_domain_on =
  11235. intel_display_power_enabled_unlocked(dev_priv,
  11236. POWER_DOMAIN_PIPE(i));
  11237. if (!error->pipe[i].power_domain_on)
  11238. continue;
  11239. error->cursor[i].control = I915_READ(CURCNTR(i));
  11240. error->cursor[i].position = I915_READ(CURPOS(i));
  11241. error->cursor[i].base = I915_READ(CURBASE(i));
  11242. error->plane[i].control = I915_READ(DSPCNTR(i));
  11243. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11244. if (INTEL_INFO(dev)->gen <= 3) {
  11245. error->plane[i].size = I915_READ(DSPSIZE(i));
  11246. error->plane[i].pos = I915_READ(DSPPOS(i));
  11247. }
  11248. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11249. error->plane[i].addr = I915_READ(DSPADDR(i));
  11250. if (INTEL_INFO(dev)->gen >= 4) {
  11251. error->plane[i].surface = I915_READ(DSPSURF(i));
  11252. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11253. }
  11254. error->pipe[i].source = I915_READ(PIPESRC(i));
  11255. if (HAS_GMCH_DISPLAY(dev))
  11256. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11257. }
  11258. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11259. if (HAS_DDI(dev_priv->dev))
  11260. error->num_transcoders++; /* Account for eDP. */
  11261. for (i = 0; i < error->num_transcoders; i++) {
  11262. enum transcoder cpu_transcoder = transcoders[i];
  11263. error->transcoder[i].power_domain_on =
  11264. intel_display_power_enabled_unlocked(dev_priv,
  11265. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11266. if (!error->transcoder[i].power_domain_on)
  11267. continue;
  11268. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11269. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11270. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11271. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11272. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11273. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11274. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11275. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11276. }
  11277. return error;
  11278. }
  11279. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11280. void
  11281. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11282. struct drm_device *dev,
  11283. struct intel_display_error_state *error)
  11284. {
  11285. struct drm_i915_private *dev_priv = dev->dev_private;
  11286. int i;
  11287. if (!error)
  11288. return;
  11289. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11290. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11291. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11292. error->power_well_driver);
  11293. for_each_pipe(dev_priv, i) {
  11294. err_printf(m, "Pipe [%d]:\n", i);
  11295. err_printf(m, " Power: %s\n",
  11296. error->pipe[i].power_domain_on ? "on" : "off");
  11297. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11298. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11299. err_printf(m, "Plane [%d]:\n", i);
  11300. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11301. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11302. if (INTEL_INFO(dev)->gen <= 3) {
  11303. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11304. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11305. }
  11306. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11307. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11308. if (INTEL_INFO(dev)->gen >= 4) {
  11309. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11310. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11311. }
  11312. err_printf(m, "Cursor [%d]:\n", i);
  11313. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11314. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11315. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11316. }
  11317. for (i = 0; i < error->num_transcoders; i++) {
  11318. err_printf(m, "CPU transcoder: %c\n",
  11319. transcoder_name(error->transcoder[i].cpu_transcoder));
  11320. err_printf(m, " Power: %s\n",
  11321. error->transcoder[i].power_domain_on ? "on" : "off");
  11322. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11323. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11324. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11325. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11326. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11327. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11328. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11329. }
  11330. }
  11331. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11332. {
  11333. struct intel_crtc *crtc;
  11334. for_each_intel_crtc(dev, crtc) {
  11335. struct intel_unpin_work *work;
  11336. unsigned long irqflags;
  11337. spin_lock_irqsave(&dev->event_lock, irqflags);
  11338. work = crtc->unpin_work;
  11339. if (work && work->event &&
  11340. work->event->base.file_priv == file) {
  11341. kfree(work->event);
  11342. work->event = NULL;
  11343. }
  11344. spin_unlock_irqrestore(&dev->event_lock, irqflags);
  11345. }
  11346. }