intel_display.c 387 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038
  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_atomic_helper.h>
  40. #include <drm/drm_dp_helper.h>
  41. #include <drm/drm_crtc_helper.h>
  42. #include <drm/drm_plane_helper.h>
  43. #include <drm/drm_rect.h>
  44. #include <linux/dma_remapping.h>
  45. /* Primary plane formats supported by all gen */
  46. #define COMMON_PRIMARY_FORMATS \
  47. DRM_FORMAT_C8, \
  48. DRM_FORMAT_RGB565, \
  49. DRM_FORMAT_XRGB8888, \
  50. DRM_FORMAT_ARGB8888
  51. /* Primary plane formats for gen <= 3 */
  52. static const uint32_t intel_primary_formats_gen2[] = {
  53. COMMON_PRIMARY_FORMATS,
  54. DRM_FORMAT_XRGB1555,
  55. DRM_FORMAT_ARGB1555,
  56. };
  57. /* Primary plane formats for gen >= 4 */
  58. static const uint32_t intel_primary_formats_gen4[] = {
  59. COMMON_PRIMARY_FORMATS, \
  60. DRM_FORMAT_XBGR8888,
  61. DRM_FORMAT_ABGR8888,
  62. DRM_FORMAT_XRGB2101010,
  63. DRM_FORMAT_ARGB2101010,
  64. DRM_FORMAT_XBGR2101010,
  65. DRM_FORMAT_ABGR2101010,
  66. };
  67. /* Cursor formats */
  68. static const uint32_t intel_cursor_formats[] = {
  69. DRM_FORMAT_ARGB8888,
  70. };
  71. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  72. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  73. struct intel_crtc_state *pipe_config);
  74. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  75. struct intel_crtc_state *pipe_config);
  76. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  77. int x, int y, struct drm_framebuffer *old_fb);
  78. static int intel_framebuffer_init(struct drm_device *dev,
  79. struct intel_framebuffer *ifb,
  80. struct drm_mode_fb_cmd2 *mode_cmd,
  81. struct drm_i915_gem_object *obj);
  82. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  83. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  84. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  85. struct intel_link_m_n *m_n,
  86. struct intel_link_m_n *m2_n2);
  87. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  88. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  89. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  90. static void vlv_prepare_pll(struct intel_crtc *crtc,
  91. const struct intel_crtc_state *pipe_config);
  92. static void chv_prepare_pll(struct intel_crtc *crtc,
  93. const struct intel_crtc_state *pipe_config);
  94. static void intel_begin_crtc_commit(struct drm_crtc *crtc);
  95. static void intel_finish_crtc_commit(struct drm_crtc *crtc);
  96. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  97. {
  98. if (!connector->mst_port)
  99. return connector->encoder;
  100. else
  101. return &connector->mst_port->mst_encoders[pipe]->base;
  102. }
  103. typedef struct {
  104. int min, max;
  105. } intel_range_t;
  106. typedef struct {
  107. int dot_limit;
  108. int p2_slow, p2_fast;
  109. } intel_p2_t;
  110. typedef struct intel_limit intel_limit_t;
  111. struct intel_limit {
  112. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  113. intel_p2_t p2;
  114. };
  115. int
  116. intel_pch_rawclk(struct drm_device *dev)
  117. {
  118. struct drm_i915_private *dev_priv = dev->dev_private;
  119. WARN_ON(!HAS_PCH_SPLIT(dev));
  120. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  121. }
  122. static inline u32 /* units of 100MHz */
  123. intel_fdi_link_freq(struct drm_device *dev)
  124. {
  125. if (IS_GEN5(dev)) {
  126. struct drm_i915_private *dev_priv = dev->dev_private;
  127. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  128. } else
  129. return 27;
  130. }
  131. static const intel_limit_t intel_limits_i8xx_dac = {
  132. .dot = { .min = 25000, .max = 350000 },
  133. .vco = { .min = 908000, .max = 1512000 },
  134. .n = { .min = 2, .max = 16 },
  135. .m = { .min = 96, .max = 140 },
  136. .m1 = { .min = 18, .max = 26 },
  137. .m2 = { .min = 6, .max = 16 },
  138. .p = { .min = 4, .max = 128 },
  139. .p1 = { .min = 2, .max = 33 },
  140. .p2 = { .dot_limit = 165000,
  141. .p2_slow = 4, .p2_fast = 2 },
  142. };
  143. static const intel_limit_t intel_limits_i8xx_dvo = {
  144. .dot = { .min = 25000, .max = 350000 },
  145. .vco = { .min = 908000, .max = 1512000 },
  146. .n = { .min = 2, .max = 16 },
  147. .m = { .min = 96, .max = 140 },
  148. .m1 = { .min = 18, .max = 26 },
  149. .m2 = { .min = 6, .max = 16 },
  150. .p = { .min = 4, .max = 128 },
  151. .p1 = { .min = 2, .max = 33 },
  152. .p2 = { .dot_limit = 165000,
  153. .p2_slow = 4, .p2_fast = 4 },
  154. };
  155. static const intel_limit_t intel_limits_i8xx_lvds = {
  156. .dot = { .min = 25000, .max = 350000 },
  157. .vco = { .min = 908000, .max = 1512000 },
  158. .n = { .min = 2, .max = 16 },
  159. .m = { .min = 96, .max = 140 },
  160. .m1 = { .min = 18, .max = 26 },
  161. .m2 = { .min = 6, .max = 16 },
  162. .p = { .min = 4, .max = 128 },
  163. .p1 = { .min = 1, .max = 6 },
  164. .p2 = { .dot_limit = 165000,
  165. .p2_slow = 14, .p2_fast = 7 },
  166. };
  167. static const intel_limit_t intel_limits_i9xx_sdvo = {
  168. .dot = { .min = 20000, .max = 400000 },
  169. .vco = { .min = 1400000, .max = 2800000 },
  170. .n = { .min = 1, .max = 6 },
  171. .m = { .min = 70, .max = 120 },
  172. .m1 = { .min = 8, .max = 18 },
  173. .m2 = { .min = 3, .max = 7 },
  174. .p = { .min = 5, .max = 80 },
  175. .p1 = { .min = 1, .max = 8 },
  176. .p2 = { .dot_limit = 200000,
  177. .p2_slow = 10, .p2_fast = 5 },
  178. };
  179. static const intel_limit_t intel_limits_i9xx_lvds = {
  180. .dot = { .min = 20000, .max = 400000 },
  181. .vco = { .min = 1400000, .max = 2800000 },
  182. .n = { .min = 1, .max = 6 },
  183. .m = { .min = 70, .max = 120 },
  184. .m1 = { .min = 8, .max = 18 },
  185. .m2 = { .min = 3, .max = 7 },
  186. .p = { .min = 7, .max = 98 },
  187. .p1 = { .min = 1, .max = 8 },
  188. .p2 = { .dot_limit = 112000,
  189. .p2_slow = 14, .p2_fast = 7 },
  190. };
  191. static const intel_limit_t intel_limits_g4x_sdvo = {
  192. .dot = { .min = 25000, .max = 270000 },
  193. .vco = { .min = 1750000, .max = 3500000},
  194. .n = { .min = 1, .max = 4 },
  195. .m = { .min = 104, .max = 138 },
  196. .m1 = { .min = 17, .max = 23 },
  197. .m2 = { .min = 5, .max = 11 },
  198. .p = { .min = 10, .max = 30 },
  199. .p1 = { .min = 1, .max = 3},
  200. .p2 = { .dot_limit = 270000,
  201. .p2_slow = 10,
  202. .p2_fast = 10
  203. },
  204. };
  205. static const intel_limit_t intel_limits_g4x_hdmi = {
  206. .dot = { .min = 22000, .max = 400000 },
  207. .vco = { .min = 1750000, .max = 3500000},
  208. .n = { .min = 1, .max = 4 },
  209. .m = { .min = 104, .max = 138 },
  210. .m1 = { .min = 16, .max = 23 },
  211. .m2 = { .min = 5, .max = 11 },
  212. .p = { .min = 5, .max = 80 },
  213. .p1 = { .min = 1, .max = 8},
  214. .p2 = { .dot_limit = 165000,
  215. .p2_slow = 10, .p2_fast = 5 },
  216. };
  217. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  218. .dot = { .min = 20000, .max = 115000 },
  219. .vco = { .min = 1750000, .max = 3500000 },
  220. .n = { .min = 1, .max = 3 },
  221. .m = { .min = 104, .max = 138 },
  222. .m1 = { .min = 17, .max = 23 },
  223. .m2 = { .min = 5, .max = 11 },
  224. .p = { .min = 28, .max = 112 },
  225. .p1 = { .min = 2, .max = 8 },
  226. .p2 = { .dot_limit = 0,
  227. .p2_slow = 14, .p2_fast = 14
  228. },
  229. };
  230. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  231. .dot = { .min = 80000, .max = 224000 },
  232. .vco = { .min = 1750000, .max = 3500000 },
  233. .n = { .min = 1, .max = 3 },
  234. .m = { .min = 104, .max = 138 },
  235. .m1 = { .min = 17, .max = 23 },
  236. .m2 = { .min = 5, .max = 11 },
  237. .p = { .min = 14, .max = 42 },
  238. .p1 = { .min = 2, .max = 6 },
  239. .p2 = { .dot_limit = 0,
  240. .p2_slow = 7, .p2_fast = 7
  241. },
  242. };
  243. static const intel_limit_t intel_limits_pineview_sdvo = {
  244. .dot = { .min = 20000, .max = 400000},
  245. .vco = { .min = 1700000, .max = 3500000 },
  246. /* Pineview's Ncounter is a ring counter */
  247. .n = { .min = 3, .max = 6 },
  248. .m = { .min = 2, .max = 256 },
  249. /* Pineview only has one combined m divider, which we treat as m2. */
  250. .m1 = { .min = 0, .max = 0 },
  251. .m2 = { .min = 0, .max = 254 },
  252. .p = { .min = 5, .max = 80 },
  253. .p1 = { .min = 1, .max = 8 },
  254. .p2 = { .dot_limit = 200000,
  255. .p2_slow = 10, .p2_fast = 5 },
  256. };
  257. static const intel_limit_t intel_limits_pineview_lvds = {
  258. .dot = { .min = 20000, .max = 400000 },
  259. .vco = { .min = 1700000, .max = 3500000 },
  260. .n = { .min = 3, .max = 6 },
  261. .m = { .min = 2, .max = 256 },
  262. .m1 = { .min = 0, .max = 0 },
  263. .m2 = { .min = 0, .max = 254 },
  264. .p = { .min = 7, .max = 112 },
  265. .p1 = { .min = 1, .max = 8 },
  266. .p2 = { .dot_limit = 112000,
  267. .p2_slow = 14, .p2_fast = 14 },
  268. };
  269. /* Ironlake / Sandybridge
  270. *
  271. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  272. * the range value for them is (actual_value - 2).
  273. */
  274. static const intel_limit_t intel_limits_ironlake_dac = {
  275. .dot = { .min = 25000, .max = 350000 },
  276. .vco = { .min = 1760000, .max = 3510000 },
  277. .n = { .min = 1, .max = 5 },
  278. .m = { .min = 79, .max = 127 },
  279. .m1 = { .min = 12, .max = 22 },
  280. .m2 = { .min = 5, .max = 9 },
  281. .p = { .min = 5, .max = 80 },
  282. .p1 = { .min = 1, .max = 8 },
  283. .p2 = { .dot_limit = 225000,
  284. .p2_slow = 10, .p2_fast = 5 },
  285. };
  286. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  287. .dot = { .min = 25000, .max = 350000 },
  288. .vco = { .min = 1760000, .max = 3510000 },
  289. .n = { .min = 1, .max = 3 },
  290. .m = { .min = 79, .max = 118 },
  291. .m1 = { .min = 12, .max = 22 },
  292. .m2 = { .min = 5, .max = 9 },
  293. .p = { .min = 28, .max = 112 },
  294. .p1 = { .min = 2, .max = 8 },
  295. .p2 = { .dot_limit = 225000,
  296. .p2_slow = 14, .p2_fast = 14 },
  297. };
  298. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  299. .dot = { .min = 25000, .max = 350000 },
  300. .vco = { .min = 1760000, .max = 3510000 },
  301. .n = { .min = 1, .max = 3 },
  302. .m = { .min = 79, .max = 127 },
  303. .m1 = { .min = 12, .max = 22 },
  304. .m2 = { .min = 5, .max = 9 },
  305. .p = { .min = 14, .max = 56 },
  306. .p1 = { .min = 2, .max = 8 },
  307. .p2 = { .dot_limit = 225000,
  308. .p2_slow = 7, .p2_fast = 7 },
  309. };
  310. /* LVDS 100mhz refclk limits. */
  311. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  312. .dot = { .min = 25000, .max = 350000 },
  313. .vco = { .min = 1760000, .max = 3510000 },
  314. .n = { .min = 1, .max = 2 },
  315. .m = { .min = 79, .max = 126 },
  316. .m1 = { .min = 12, .max = 22 },
  317. .m2 = { .min = 5, .max = 9 },
  318. .p = { .min = 28, .max = 112 },
  319. .p1 = { .min = 2, .max = 8 },
  320. .p2 = { .dot_limit = 225000,
  321. .p2_slow = 14, .p2_fast = 14 },
  322. };
  323. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  324. .dot = { .min = 25000, .max = 350000 },
  325. .vco = { .min = 1760000, .max = 3510000 },
  326. .n = { .min = 1, .max = 3 },
  327. .m = { .min = 79, .max = 126 },
  328. .m1 = { .min = 12, .max = 22 },
  329. .m2 = { .min = 5, .max = 9 },
  330. .p = { .min = 14, .max = 42 },
  331. .p1 = { .min = 2, .max = 6 },
  332. .p2 = { .dot_limit = 225000,
  333. .p2_slow = 7, .p2_fast = 7 },
  334. };
  335. static const intel_limit_t intel_limits_vlv = {
  336. /*
  337. * These are the data rate limits (measured in fast clocks)
  338. * since those are the strictest limits we have. The fast
  339. * clock and actual rate limits are more relaxed, so checking
  340. * them would make no difference.
  341. */
  342. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  343. .vco = { .min = 4000000, .max = 6000000 },
  344. .n = { .min = 1, .max = 7 },
  345. .m1 = { .min = 2, .max = 3 },
  346. .m2 = { .min = 11, .max = 156 },
  347. .p1 = { .min = 2, .max = 3 },
  348. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  349. };
  350. static const intel_limit_t intel_limits_chv = {
  351. /*
  352. * These are the data rate limits (measured in fast clocks)
  353. * since those are the strictest limits we have. The fast
  354. * clock and actual rate limits are more relaxed, so checking
  355. * them would make no difference.
  356. */
  357. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  358. .vco = { .min = 4860000, .max = 6700000 },
  359. .n = { .min = 1, .max = 1 },
  360. .m1 = { .min = 2, .max = 2 },
  361. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  362. .p1 = { .min = 2, .max = 4 },
  363. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  364. };
  365. static void vlv_clock(int refclk, intel_clock_t *clock)
  366. {
  367. clock->m = clock->m1 * clock->m2;
  368. clock->p = clock->p1 * clock->p2;
  369. if (WARN_ON(clock->n == 0 || clock->p == 0))
  370. return;
  371. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  372. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  373. }
  374. /**
  375. * Returns whether any output on the specified pipe is of the specified type
  376. */
  377. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  378. {
  379. struct drm_device *dev = crtc->base.dev;
  380. struct intel_encoder *encoder;
  381. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  382. if (encoder->type == type)
  383. return true;
  384. return false;
  385. }
  386. /**
  387. * Returns whether any output on the specified pipe will have the specified
  388. * type after a staged modeset is complete, i.e., the same as
  389. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  390. * encoder->crtc.
  391. */
  392. static bool intel_pipe_will_have_type(struct intel_crtc *crtc, int type)
  393. {
  394. struct drm_device *dev = crtc->base.dev;
  395. struct intel_encoder *encoder;
  396. for_each_intel_encoder(dev, encoder)
  397. if (encoder->new_crtc == crtc && encoder->type == type)
  398. return true;
  399. return false;
  400. }
  401. static const intel_limit_t *intel_ironlake_limit(struct intel_crtc *crtc,
  402. int refclk)
  403. {
  404. struct drm_device *dev = crtc->base.dev;
  405. const intel_limit_t *limit;
  406. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  407. if (intel_is_dual_link_lvds(dev)) {
  408. if (refclk == 100000)
  409. limit = &intel_limits_ironlake_dual_lvds_100m;
  410. else
  411. limit = &intel_limits_ironlake_dual_lvds;
  412. } else {
  413. if (refclk == 100000)
  414. limit = &intel_limits_ironlake_single_lvds_100m;
  415. else
  416. limit = &intel_limits_ironlake_single_lvds;
  417. }
  418. } else
  419. limit = &intel_limits_ironlake_dac;
  420. return limit;
  421. }
  422. static const intel_limit_t *intel_g4x_limit(struct intel_crtc *crtc)
  423. {
  424. struct drm_device *dev = crtc->base.dev;
  425. const intel_limit_t *limit;
  426. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  427. if (intel_is_dual_link_lvds(dev))
  428. limit = &intel_limits_g4x_dual_channel_lvds;
  429. else
  430. limit = &intel_limits_g4x_single_channel_lvds;
  431. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI) ||
  432. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_ANALOG)) {
  433. limit = &intel_limits_g4x_hdmi;
  434. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO)) {
  435. limit = &intel_limits_g4x_sdvo;
  436. } else /* The option is for other outputs */
  437. limit = &intel_limits_i9xx_sdvo;
  438. return limit;
  439. }
  440. static const intel_limit_t *intel_limit(struct intel_crtc *crtc, int refclk)
  441. {
  442. struct drm_device *dev = crtc->base.dev;
  443. const intel_limit_t *limit;
  444. if (HAS_PCH_SPLIT(dev))
  445. limit = intel_ironlake_limit(crtc, refclk);
  446. else if (IS_G4X(dev)) {
  447. limit = intel_g4x_limit(crtc);
  448. } else if (IS_PINEVIEW(dev)) {
  449. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  450. limit = &intel_limits_pineview_lvds;
  451. else
  452. limit = &intel_limits_pineview_sdvo;
  453. } else if (IS_CHERRYVIEW(dev)) {
  454. limit = &intel_limits_chv;
  455. } else if (IS_VALLEYVIEW(dev)) {
  456. limit = &intel_limits_vlv;
  457. } else if (!IS_GEN2(dev)) {
  458. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  459. limit = &intel_limits_i9xx_lvds;
  460. else
  461. limit = &intel_limits_i9xx_sdvo;
  462. } else {
  463. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  464. limit = &intel_limits_i8xx_lvds;
  465. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  466. limit = &intel_limits_i8xx_dvo;
  467. else
  468. limit = &intel_limits_i8xx_dac;
  469. }
  470. return limit;
  471. }
  472. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  473. static void pineview_clock(int refclk, intel_clock_t *clock)
  474. {
  475. clock->m = clock->m2 + 2;
  476. clock->p = clock->p1 * clock->p2;
  477. if (WARN_ON(clock->n == 0 || clock->p == 0))
  478. return;
  479. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  480. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  481. }
  482. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  483. {
  484. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  485. }
  486. static void i9xx_clock(int refclk, intel_clock_t *clock)
  487. {
  488. clock->m = i9xx_dpll_compute_m(clock);
  489. clock->p = clock->p1 * clock->p2;
  490. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  491. return;
  492. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  493. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  494. }
  495. static void chv_clock(int refclk, intel_clock_t *clock)
  496. {
  497. clock->m = clock->m1 * clock->m2;
  498. clock->p = clock->p1 * clock->p2;
  499. if (WARN_ON(clock->n == 0 || clock->p == 0))
  500. return;
  501. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  502. clock->n << 22);
  503. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  504. }
  505. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  506. /**
  507. * Returns whether the given set of divisors are valid for a given refclk with
  508. * the given connectors.
  509. */
  510. static bool intel_PLL_is_valid(struct drm_device *dev,
  511. const intel_limit_t *limit,
  512. const intel_clock_t *clock)
  513. {
  514. if (clock->n < limit->n.min || limit->n.max < clock->n)
  515. INTELPllInvalid("n out of range\n");
  516. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  517. INTELPllInvalid("p1 out of range\n");
  518. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  519. INTELPllInvalid("m2 out of range\n");
  520. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  521. INTELPllInvalid("m1 out of range\n");
  522. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  523. if (clock->m1 <= clock->m2)
  524. INTELPllInvalid("m1 <= m2\n");
  525. if (!IS_VALLEYVIEW(dev)) {
  526. if (clock->p < limit->p.min || limit->p.max < clock->p)
  527. INTELPllInvalid("p out of range\n");
  528. if (clock->m < limit->m.min || limit->m.max < clock->m)
  529. INTELPllInvalid("m out of range\n");
  530. }
  531. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  532. INTELPllInvalid("vco out of range\n");
  533. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  534. * connector, etc., rather than just a single range.
  535. */
  536. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  537. INTELPllInvalid("dot out of range\n");
  538. return true;
  539. }
  540. static bool
  541. i9xx_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  542. int target, int refclk, intel_clock_t *match_clock,
  543. intel_clock_t *best_clock)
  544. {
  545. struct drm_device *dev = crtc->base.dev;
  546. intel_clock_t clock;
  547. int err = target;
  548. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  549. /*
  550. * For LVDS just rely on its current settings for dual-channel.
  551. * We haven't figured out how to reliably set up different
  552. * single/dual channel state, if we even can.
  553. */
  554. if (intel_is_dual_link_lvds(dev))
  555. clock.p2 = limit->p2.p2_fast;
  556. else
  557. clock.p2 = limit->p2.p2_slow;
  558. } else {
  559. if (target < limit->p2.dot_limit)
  560. clock.p2 = limit->p2.p2_slow;
  561. else
  562. clock.p2 = limit->p2.p2_fast;
  563. }
  564. memset(best_clock, 0, sizeof(*best_clock));
  565. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  566. clock.m1++) {
  567. for (clock.m2 = limit->m2.min;
  568. clock.m2 <= limit->m2.max; clock.m2++) {
  569. if (clock.m2 >= clock.m1)
  570. break;
  571. for (clock.n = limit->n.min;
  572. clock.n <= limit->n.max; clock.n++) {
  573. for (clock.p1 = limit->p1.min;
  574. clock.p1 <= limit->p1.max; clock.p1++) {
  575. int this_err;
  576. i9xx_clock(refclk, &clock);
  577. if (!intel_PLL_is_valid(dev, limit,
  578. &clock))
  579. continue;
  580. if (match_clock &&
  581. clock.p != match_clock->p)
  582. continue;
  583. this_err = abs(clock.dot - target);
  584. if (this_err < err) {
  585. *best_clock = clock;
  586. err = this_err;
  587. }
  588. }
  589. }
  590. }
  591. }
  592. return (err != target);
  593. }
  594. static bool
  595. pnv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  596. int target, int refclk, intel_clock_t *match_clock,
  597. intel_clock_t *best_clock)
  598. {
  599. struct drm_device *dev = crtc->base.dev;
  600. intel_clock_t clock;
  601. int err = target;
  602. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  603. /*
  604. * For LVDS just rely on its current settings for dual-channel.
  605. * We haven't figured out how to reliably set up different
  606. * single/dual channel state, if we even can.
  607. */
  608. if (intel_is_dual_link_lvds(dev))
  609. clock.p2 = limit->p2.p2_fast;
  610. else
  611. clock.p2 = limit->p2.p2_slow;
  612. } else {
  613. if (target < limit->p2.dot_limit)
  614. clock.p2 = limit->p2.p2_slow;
  615. else
  616. clock.p2 = limit->p2.p2_fast;
  617. }
  618. memset(best_clock, 0, sizeof(*best_clock));
  619. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  620. clock.m1++) {
  621. for (clock.m2 = limit->m2.min;
  622. clock.m2 <= limit->m2.max; clock.m2++) {
  623. for (clock.n = limit->n.min;
  624. clock.n <= limit->n.max; clock.n++) {
  625. for (clock.p1 = limit->p1.min;
  626. clock.p1 <= limit->p1.max; clock.p1++) {
  627. int this_err;
  628. pineview_clock(refclk, &clock);
  629. if (!intel_PLL_is_valid(dev, limit,
  630. &clock))
  631. continue;
  632. if (match_clock &&
  633. clock.p != match_clock->p)
  634. continue;
  635. this_err = abs(clock.dot - target);
  636. if (this_err < err) {
  637. *best_clock = clock;
  638. err = this_err;
  639. }
  640. }
  641. }
  642. }
  643. }
  644. return (err != target);
  645. }
  646. static bool
  647. g4x_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  648. int target, int refclk, intel_clock_t *match_clock,
  649. intel_clock_t *best_clock)
  650. {
  651. struct drm_device *dev = crtc->base.dev;
  652. intel_clock_t clock;
  653. int max_n;
  654. bool found;
  655. /* approximately equals target * 0.00585 */
  656. int err_most = (target >> 8) + (target >> 9);
  657. found = false;
  658. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  659. if (intel_is_dual_link_lvds(dev))
  660. clock.p2 = limit->p2.p2_fast;
  661. else
  662. clock.p2 = limit->p2.p2_slow;
  663. } else {
  664. if (target < limit->p2.dot_limit)
  665. clock.p2 = limit->p2.p2_slow;
  666. else
  667. clock.p2 = limit->p2.p2_fast;
  668. }
  669. memset(best_clock, 0, sizeof(*best_clock));
  670. max_n = limit->n.max;
  671. /* based on hardware requirement, prefer smaller n to precision */
  672. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  673. /* based on hardware requirement, prefere larger m1,m2 */
  674. for (clock.m1 = limit->m1.max;
  675. clock.m1 >= limit->m1.min; clock.m1--) {
  676. for (clock.m2 = limit->m2.max;
  677. clock.m2 >= limit->m2.min; clock.m2--) {
  678. for (clock.p1 = limit->p1.max;
  679. clock.p1 >= limit->p1.min; clock.p1--) {
  680. int this_err;
  681. i9xx_clock(refclk, &clock);
  682. if (!intel_PLL_is_valid(dev, limit,
  683. &clock))
  684. continue;
  685. this_err = abs(clock.dot - target);
  686. if (this_err < err_most) {
  687. *best_clock = clock;
  688. err_most = this_err;
  689. max_n = clock.n;
  690. found = true;
  691. }
  692. }
  693. }
  694. }
  695. }
  696. return found;
  697. }
  698. static bool
  699. vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  700. int target, int refclk, intel_clock_t *match_clock,
  701. intel_clock_t *best_clock)
  702. {
  703. struct drm_device *dev = crtc->base.dev;
  704. intel_clock_t clock;
  705. unsigned int bestppm = 1000000;
  706. /* min update 19.2 MHz */
  707. int max_n = min(limit->n.max, refclk / 19200);
  708. bool found = false;
  709. target *= 5; /* fast clock */
  710. memset(best_clock, 0, sizeof(*best_clock));
  711. /* based on hardware requirement, prefer smaller n to precision */
  712. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  713. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  714. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  715. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  716. clock.p = clock.p1 * clock.p2;
  717. /* based on hardware requirement, prefer bigger m1,m2 values */
  718. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  719. unsigned int ppm, diff;
  720. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  721. refclk * clock.m1);
  722. vlv_clock(refclk, &clock);
  723. if (!intel_PLL_is_valid(dev, limit,
  724. &clock))
  725. continue;
  726. diff = abs(clock.dot - target);
  727. ppm = div_u64(1000000ULL * diff, target);
  728. if (ppm < 100 && clock.p > best_clock->p) {
  729. bestppm = 0;
  730. *best_clock = clock;
  731. found = true;
  732. }
  733. if (bestppm >= 10 && ppm < bestppm - 10) {
  734. bestppm = ppm;
  735. *best_clock = clock;
  736. found = true;
  737. }
  738. }
  739. }
  740. }
  741. }
  742. return found;
  743. }
  744. static bool
  745. chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
  746. int target, int refclk, intel_clock_t *match_clock,
  747. intel_clock_t *best_clock)
  748. {
  749. struct drm_device *dev = crtc->base.dev;
  750. intel_clock_t clock;
  751. uint64_t m2;
  752. int found = false;
  753. memset(best_clock, 0, sizeof(*best_clock));
  754. /*
  755. * Based on hardware doc, the n always set to 1, and m1 always
  756. * set to 2. If requires to support 200Mhz refclk, we need to
  757. * revisit this because n may not 1 anymore.
  758. */
  759. clock.n = 1, clock.m1 = 2;
  760. target *= 5; /* fast clock */
  761. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  762. for (clock.p2 = limit->p2.p2_fast;
  763. clock.p2 >= limit->p2.p2_slow;
  764. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  765. clock.p = clock.p1 * clock.p2;
  766. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  767. clock.n) << 22, refclk * clock.m1);
  768. if (m2 > INT_MAX/clock.m1)
  769. continue;
  770. clock.m2 = m2;
  771. chv_clock(refclk, &clock);
  772. if (!intel_PLL_is_valid(dev, limit, &clock))
  773. continue;
  774. /* based on hardware requirement, prefer bigger p
  775. */
  776. if (clock.p > best_clock->p) {
  777. *best_clock = clock;
  778. found = true;
  779. }
  780. }
  781. }
  782. return found;
  783. }
  784. bool intel_crtc_active(struct drm_crtc *crtc)
  785. {
  786. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  787. /* Be paranoid as we can arrive here with only partial
  788. * state retrieved from the hardware during setup.
  789. *
  790. * We can ditch the adjusted_mode.crtc_clock check as soon
  791. * as Haswell has gained clock readout/fastboot support.
  792. *
  793. * We can ditch the crtc->primary->fb check as soon as we can
  794. * properly reconstruct framebuffers.
  795. */
  796. return intel_crtc->active && crtc->primary->fb &&
  797. intel_crtc->config->base.adjusted_mode.crtc_clock;
  798. }
  799. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  800. enum pipe pipe)
  801. {
  802. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  803. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  804. return intel_crtc->config->cpu_transcoder;
  805. }
  806. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  807. {
  808. struct drm_i915_private *dev_priv = dev->dev_private;
  809. u32 reg = PIPEDSL(pipe);
  810. u32 line1, line2;
  811. u32 line_mask;
  812. if (IS_GEN2(dev))
  813. line_mask = DSL_LINEMASK_GEN2;
  814. else
  815. line_mask = DSL_LINEMASK_GEN3;
  816. line1 = I915_READ(reg) & line_mask;
  817. mdelay(5);
  818. line2 = I915_READ(reg) & line_mask;
  819. return line1 == line2;
  820. }
  821. /*
  822. * intel_wait_for_pipe_off - wait for pipe to turn off
  823. * @crtc: crtc whose pipe to wait for
  824. *
  825. * After disabling a pipe, we can't wait for vblank in the usual way,
  826. * spinning on the vblank interrupt status bit, since we won't actually
  827. * see an interrupt when the pipe is disabled.
  828. *
  829. * On Gen4 and above:
  830. * wait for the pipe register state bit to turn off
  831. *
  832. * Otherwise:
  833. * wait for the display line value to settle (it usually
  834. * ends up stopping at the start of the next frame).
  835. *
  836. */
  837. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  838. {
  839. struct drm_device *dev = crtc->base.dev;
  840. struct drm_i915_private *dev_priv = dev->dev_private;
  841. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  842. enum pipe pipe = crtc->pipe;
  843. if (INTEL_INFO(dev)->gen >= 4) {
  844. int reg = PIPECONF(cpu_transcoder);
  845. /* Wait for the Pipe State to go off */
  846. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  847. 100))
  848. WARN(1, "pipe_off wait timed out\n");
  849. } else {
  850. /* Wait for the display line to settle */
  851. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  852. WARN(1, "pipe_off wait timed out\n");
  853. }
  854. }
  855. /*
  856. * ibx_digital_port_connected - is the specified port connected?
  857. * @dev_priv: i915 private structure
  858. * @port: the port to test
  859. *
  860. * Returns true if @port is connected, false otherwise.
  861. */
  862. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  863. struct intel_digital_port *port)
  864. {
  865. u32 bit;
  866. if (HAS_PCH_IBX(dev_priv->dev)) {
  867. switch (port->port) {
  868. case PORT_B:
  869. bit = SDE_PORTB_HOTPLUG;
  870. break;
  871. case PORT_C:
  872. bit = SDE_PORTC_HOTPLUG;
  873. break;
  874. case PORT_D:
  875. bit = SDE_PORTD_HOTPLUG;
  876. break;
  877. default:
  878. return true;
  879. }
  880. } else {
  881. switch (port->port) {
  882. case PORT_B:
  883. bit = SDE_PORTB_HOTPLUG_CPT;
  884. break;
  885. case PORT_C:
  886. bit = SDE_PORTC_HOTPLUG_CPT;
  887. break;
  888. case PORT_D:
  889. bit = SDE_PORTD_HOTPLUG_CPT;
  890. break;
  891. default:
  892. return true;
  893. }
  894. }
  895. return I915_READ(SDEISR) & bit;
  896. }
  897. static const char *state_string(bool enabled)
  898. {
  899. return enabled ? "on" : "off";
  900. }
  901. /* Only for pre-ILK configs */
  902. void assert_pll(struct drm_i915_private *dev_priv,
  903. enum pipe pipe, bool state)
  904. {
  905. int reg;
  906. u32 val;
  907. bool cur_state;
  908. reg = DPLL(pipe);
  909. val = I915_READ(reg);
  910. cur_state = !!(val & DPLL_VCO_ENABLE);
  911. I915_STATE_WARN(cur_state != state,
  912. "PLL state assertion failure (expected %s, current %s)\n",
  913. state_string(state), state_string(cur_state));
  914. }
  915. /* XXX: the dsi pll is shared between MIPI DSI ports */
  916. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  917. {
  918. u32 val;
  919. bool cur_state;
  920. mutex_lock(&dev_priv->dpio_lock);
  921. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  922. mutex_unlock(&dev_priv->dpio_lock);
  923. cur_state = val & DSI_PLL_VCO_EN;
  924. I915_STATE_WARN(cur_state != state,
  925. "DSI PLL state assertion failure (expected %s, current %s)\n",
  926. state_string(state), state_string(cur_state));
  927. }
  928. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  929. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  930. struct intel_shared_dpll *
  931. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  932. {
  933. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  934. if (crtc->config->shared_dpll < 0)
  935. return NULL;
  936. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  937. }
  938. /* For ILK+ */
  939. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  940. struct intel_shared_dpll *pll,
  941. bool state)
  942. {
  943. bool cur_state;
  944. struct intel_dpll_hw_state hw_state;
  945. if (WARN (!pll,
  946. "asserting DPLL %s with no DPLL\n", state_string(state)))
  947. return;
  948. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  949. I915_STATE_WARN(cur_state != state,
  950. "%s assertion failure (expected %s, current %s)\n",
  951. pll->name, state_string(state), state_string(cur_state));
  952. }
  953. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  954. enum pipe pipe, bool state)
  955. {
  956. int reg;
  957. u32 val;
  958. bool cur_state;
  959. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  960. pipe);
  961. if (HAS_DDI(dev_priv->dev)) {
  962. /* DDI does not have a specific FDI_TX register */
  963. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  964. val = I915_READ(reg);
  965. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  966. } else {
  967. reg = FDI_TX_CTL(pipe);
  968. val = I915_READ(reg);
  969. cur_state = !!(val & FDI_TX_ENABLE);
  970. }
  971. I915_STATE_WARN(cur_state != state,
  972. "FDI TX state assertion failure (expected %s, current %s)\n",
  973. state_string(state), state_string(cur_state));
  974. }
  975. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  976. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  977. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  978. enum pipe pipe, bool state)
  979. {
  980. int reg;
  981. u32 val;
  982. bool cur_state;
  983. reg = FDI_RX_CTL(pipe);
  984. val = I915_READ(reg);
  985. cur_state = !!(val & FDI_RX_ENABLE);
  986. I915_STATE_WARN(cur_state != state,
  987. "FDI RX state assertion failure (expected %s, current %s)\n",
  988. state_string(state), state_string(cur_state));
  989. }
  990. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  991. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  992. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  993. enum pipe pipe)
  994. {
  995. int reg;
  996. u32 val;
  997. /* ILK FDI PLL is always enabled */
  998. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  999. return;
  1000. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1001. if (HAS_DDI(dev_priv->dev))
  1002. return;
  1003. reg = FDI_TX_CTL(pipe);
  1004. val = I915_READ(reg);
  1005. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1006. }
  1007. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1008. enum pipe pipe, bool state)
  1009. {
  1010. int reg;
  1011. u32 val;
  1012. bool cur_state;
  1013. reg = FDI_RX_CTL(pipe);
  1014. val = I915_READ(reg);
  1015. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1016. I915_STATE_WARN(cur_state != state,
  1017. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1018. state_string(state), state_string(cur_state));
  1019. }
  1020. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1021. enum pipe pipe)
  1022. {
  1023. struct drm_device *dev = dev_priv->dev;
  1024. int pp_reg;
  1025. u32 val;
  1026. enum pipe panel_pipe = PIPE_A;
  1027. bool locked = true;
  1028. if (WARN_ON(HAS_DDI(dev)))
  1029. return;
  1030. if (HAS_PCH_SPLIT(dev)) {
  1031. u32 port_sel;
  1032. pp_reg = PCH_PP_CONTROL;
  1033. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1034. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1035. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1036. panel_pipe = PIPE_B;
  1037. /* XXX: else fix for eDP */
  1038. } else if (IS_VALLEYVIEW(dev)) {
  1039. /* presumably write lock depends on pipe, not port select */
  1040. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1041. panel_pipe = pipe;
  1042. } else {
  1043. pp_reg = PP_CONTROL;
  1044. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1045. panel_pipe = PIPE_B;
  1046. }
  1047. val = I915_READ(pp_reg);
  1048. if (!(val & PANEL_POWER_ON) ||
  1049. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1050. locked = false;
  1051. I915_STATE_WARN(panel_pipe == pipe && locked,
  1052. "panel assertion failure, pipe %c regs locked\n",
  1053. pipe_name(pipe));
  1054. }
  1055. static void assert_cursor(struct drm_i915_private *dev_priv,
  1056. enum pipe pipe, bool state)
  1057. {
  1058. struct drm_device *dev = dev_priv->dev;
  1059. bool cur_state;
  1060. if (IS_845G(dev) || IS_I865G(dev))
  1061. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1062. else
  1063. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1064. I915_STATE_WARN(cur_state != state,
  1065. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1066. pipe_name(pipe), state_string(state), state_string(cur_state));
  1067. }
  1068. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1069. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1070. void assert_pipe(struct drm_i915_private *dev_priv,
  1071. enum pipe pipe, bool state)
  1072. {
  1073. int reg;
  1074. u32 val;
  1075. bool cur_state;
  1076. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1077. pipe);
  1078. /* if we need the pipe quirk it must be always on */
  1079. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1080. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1081. state = true;
  1082. if (!intel_display_power_is_enabled(dev_priv,
  1083. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1084. cur_state = false;
  1085. } else {
  1086. reg = PIPECONF(cpu_transcoder);
  1087. val = I915_READ(reg);
  1088. cur_state = !!(val & PIPECONF_ENABLE);
  1089. }
  1090. I915_STATE_WARN(cur_state != state,
  1091. "pipe %c assertion failure (expected %s, current %s)\n",
  1092. pipe_name(pipe), state_string(state), state_string(cur_state));
  1093. }
  1094. static void assert_plane(struct drm_i915_private *dev_priv,
  1095. enum plane plane, bool state)
  1096. {
  1097. int reg;
  1098. u32 val;
  1099. bool cur_state;
  1100. reg = DSPCNTR(plane);
  1101. val = I915_READ(reg);
  1102. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1103. I915_STATE_WARN(cur_state != state,
  1104. "plane %c assertion failure (expected %s, current %s)\n",
  1105. plane_name(plane), state_string(state), state_string(cur_state));
  1106. }
  1107. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1108. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1109. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1110. enum pipe pipe)
  1111. {
  1112. struct drm_device *dev = dev_priv->dev;
  1113. int reg, i;
  1114. u32 val;
  1115. int cur_pipe;
  1116. /* Primary planes are fixed to pipes on gen4+ */
  1117. if (INTEL_INFO(dev)->gen >= 4) {
  1118. reg = DSPCNTR(pipe);
  1119. val = I915_READ(reg);
  1120. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1121. "plane %c assertion failure, should be disabled but not\n",
  1122. plane_name(pipe));
  1123. return;
  1124. }
  1125. /* Need to check both planes against the pipe */
  1126. for_each_pipe(dev_priv, i) {
  1127. reg = DSPCNTR(i);
  1128. val = I915_READ(reg);
  1129. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1130. DISPPLANE_SEL_PIPE_SHIFT;
  1131. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1132. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1133. plane_name(i), pipe_name(pipe));
  1134. }
  1135. }
  1136. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1137. enum pipe pipe)
  1138. {
  1139. struct drm_device *dev = dev_priv->dev;
  1140. int reg, sprite;
  1141. u32 val;
  1142. if (INTEL_INFO(dev)->gen >= 9) {
  1143. for_each_sprite(pipe, sprite) {
  1144. val = I915_READ(PLANE_CTL(pipe, sprite));
  1145. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1146. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1147. sprite, pipe_name(pipe));
  1148. }
  1149. } else if (IS_VALLEYVIEW(dev)) {
  1150. for_each_sprite(pipe, sprite) {
  1151. reg = SPCNTR(pipe, sprite);
  1152. val = I915_READ(reg);
  1153. I915_STATE_WARN(val & SP_ENABLE,
  1154. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1155. sprite_name(pipe, sprite), pipe_name(pipe));
  1156. }
  1157. } else if (INTEL_INFO(dev)->gen >= 7) {
  1158. reg = SPRCTL(pipe);
  1159. val = I915_READ(reg);
  1160. I915_STATE_WARN(val & SPRITE_ENABLE,
  1161. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1162. plane_name(pipe), pipe_name(pipe));
  1163. } else if (INTEL_INFO(dev)->gen >= 5) {
  1164. reg = DVSCNTR(pipe);
  1165. val = I915_READ(reg);
  1166. I915_STATE_WARN(val & DVS_ENABLE,
  1167. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1168. plane_name(pipe), pipe_name(pipe));
  1169. }
  1170. }
  1171. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1172. {
  1173. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1174. drm_crtc_vblank_put(crtc);
  1175. }
  1176. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1177. {
  1178. u32 val;
  1179. bool enabled;
  1180. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1181. val = I915_READ(PCH_DREF_CONTROL);
  1182. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1183. DREF_SUPERSPREAD_SOURCE_MASK));
  1184. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1185. }
  1186. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1187. enum pipe pipe)
  1188. {
  1189. int reg;
  1190. u32 val;
  1191. bool enabled;
  1192. reg = PCH_TRANSCONF(pipe);
  1193. val = I915_READ(reg);
  1194. enabled = !!(val & TRANS_ENABLE);
  1195. I915_STATE_WARN(enabled,
  1196. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1197. pipe_name(pipe));
  1198. }
  1199. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1200. enum pipe pipe, u32 port_sel, u32 val)
  1201. {
  1202. if ((val & DP_PORT_EN) == 0)
  1203. return false;
  1204. if (HAS_PCH_CPT(dev_priv->dev)) {
  1205. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1206. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1207. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1208. return false;
  1209. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1210. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1211. return false;
  1212. } else {
  1213. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1214. return false;
  1215. }
  1216. return true;
  1217. }
  1218. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1219. enum pipe pipe, u32 val)
  1220. {
  1221. if ((val & SDVO_ENABLE) == 0)
  1222. return false;
  1223. if (HAS_PCH_CPT(dev_priv->dev)) {
  1224. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1225. return false;
  1226. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1227. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1228. return false;
  1229. } else {
  1230. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1231. return false;
  1232. }
  1233. return true;
  1234. }
  1235. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1236. enum pipe pipe, u32 val)
  1237. {
  1238. if ((val & LVDS_PORT_EN) == 0)
  1239. return false;
  1240. if (HAS_PCH_CPT(dev_priv->dev)) {
  1241. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1242. return false;
  1243. } else {
  1244. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1245. return false;
  1246. }
  1247. return true;
  1248. }
  1249. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1250. enum pipe pipe, u32 val)
  1251. {
  1252. if ((val & ADPA_DAC_ENABLE) == 0)
  1253. return false;
  1254. if (HAS_PCH_CPT(dev_priv->dev)) {
  1255. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1256. return false;
  1257. } else {
  1258. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1259. return false;
  1260. }
  1261. return true;
  1262. }
  1263. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1264. enum pipe pipe, int reg, u32 port_sel)
  1265. {
  1266. u32 val = I915_READ(reg);
  1267. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1268. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1269. reg, pipe_name(pipe));
  1270. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1271. && (val & DP_PIPEB_SELECT),
  1272. "IBX PCH dp port still using transcoder B\n");
  1273. }
  1274. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1275. enum pipe pipe, int reg)
  1276. {
  1277. u32 val = I915_READ(reg);
  1278. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1279. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1280. reg, pipe_name(pipe));
  1281. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1282. && (val & SDVO_PIPE_B_SELECT),
  1283. "IBX PCH hdmi port still using transcoder B\n");
  1284. }
  1285. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1286. enum pipe pipe)
  1287. {
  1288. int reg;
  1289. u32 val;
  1290. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1291. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1292. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1293. reg = PCH_ADPA;
  1294. val = I915_READ(reg);
  1295. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1296. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1297. pipe_name(pipe));
  1298. reg = PCH_LVDS;
  1299. val = I915_READ(reg);
  1300. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1301. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1302. pipe_name(pipe));
  1303. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1304. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1305. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1306. }
  1307. static void intel_init_dpio(struct drm_device *dev)
  1308. {
  1309. struct drm_i915_private *dev_priv = dev->dev_private;
  1310. if (!IS_VALLEYVIEW(dev))
  1311. return;
  1312. /*
  1313. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1314. * CHV x1 PHY (DP/HDMI D)
  1315. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1316. */
  1317. if (IS_CHERRYVIEW(dev)) {
  1318. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1319. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1320. } else {
  1321. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1322. }
  1323. }
  1324. static void vlv_enable_pll(struct intel_crtc *crtc,
  1325. const struct intel_crtc_state *pipe_config)
  1326. {
  1327. struct drm_device *dev = crtc->base.dev;
  1328. struct drm_i915_private *dev_priv = dev->dev_private;
  1329. int reg = DPLL(crtc->pipe);
  1330. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1331. assert_pipe_disabled(dev_priv, crtc->pipe);
  1332. /* No really, not for ILK+ */
  1333. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1334. /* PLL is protected by panel, make sure we can write it */
  1335. if (IS_MOBILE(dev_priv->dev))
  1336. assert_panel_unlocked(dev_priv, crtc->pipe);
  1337. I915_WRITE(reg, dpll);
  1338. POSTING_READ(reg);
  1339. udelay(150);
  1340. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1341. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1342. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1343. POSTING_READ(DPLL_MD(crtc->pipe));
  1344. /* We do this three times for luck */
  1345. I915_WRITE(reg, dpll);
  1346. POSTING_READ(reg);
  1347. udelay(150); /* wait for warmup */
  1348. I915_WRITE(reg, dpll);
  1349. POSTING_READ(reg);
  1350. udelay(150); /* wait for warmup */
  1351. I915_WRITE(reg, dpll);
  1352. POSTING_READ(reg);
  1353. udelay(150); /* wait for warmup */
  1354. }
  1355. static void chv_enable_pll(struct intel_crtc *crtc,
  1356. const struct intel_crtc_state *pipe_config)
  1357. {
  1358. struct drm_device *dev = crtc->base.dev;
  1359. struct drm_i915_private *dev_priv = dev->dev_private;
  1360. int pipe = crtc->pipe;
  1361. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1362. u32 tmp;
  1363. assert_pipe_disabled(dev_priv, crtc->pipe);
  1364. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1365. mutex_lock(&dev_priv->dpio_lock);
  1366. /* Enable back the 10bit clock to display controller */
  1367. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1368. tmp |= DPIO_DCLKP_EN;
  1369. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1370. /*
  1371. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1372. */
  1373. udelay(1);
  1374. /* Enable PLL */
  1375. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1376. /* Check PLL is locked */
  1377. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1378. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1379. /* not sure when this should be written */
  1380. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1381. POSTING_READ(DPLL_MD(pipe));
  1382. mutex_unlock(&dev_priv->dpio_lock);
  1383. }
  1384. static int intel_num_dvo_pipes(struct drm_device *dev)
  1385. {
  1386. struct intel_crtc *crtc;
  1387. int count = 0;
  1388. for_each_intel_crtc(dev, crtc)
  1389. count += crtc->active &&
  1390. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1391. return count;
  1392. }
  1393. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1394. {
  1395. struct drm_device *dev = crtc->base.dev;
  1396. struct drm_i915_private *dev_priv = dev->dev_private;
  1397. int reg = DPLL(crtc->pipe);
  1398. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1399. assert_pipe_disabled(dev_priv, crtc->pipe);
  1400. /* No really, not for ILK+ */
  1401. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1402. /* PLL is protected by panel, make sure we can write it */
  1403. if (IS_MOBILE(dev) && !IS_I830(dev))
  1404. assert_panel_unlocked(dev_priv, crtc->pipe);
  1405. /* Enable DVO 2x clock on both PLLs if necessary */
  1406. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1407. /*
  1408. * It appears to be important that we don't enable this
  1409. * for the current pipe before otherwise configuring the
  1410. * PLL. No idea how this should be handled if multiple
  1411. * DVO outputs are enabled simultaneosly.
  1412. */
  1413. dpll |= DPLL_DVO_2X_MODE;
  1414. I915_WRITE(DPLL(!crtc->pipe),
  1415. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1416. }
  1417. /* Wait for the clocks to stabilize. */
  1418. POSTING_READ(reg);
  1419. udelay(150);
  1420. if (INTEL_INFO(dev)->gen >= 4) {
  1421. I915_WRITE(DPLL_MD(crtc->pipe),
  1422. crtc->config->dpll_hw_state.dpll_md);
  1423. } else {
  1424. /* The pixel multiplier can only be updated once the
  1425. * DPLL is enabled and the clocks are stable.
  1426. *
  1427. * So write it again.
  1428. */
  1429. I915_WRITE(reg, dpll);
  1430. }
  1431. /* We do this three times for luck */
  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. I915_WRITE(reg, dpll);
  1439. POSTING_READ(reg);
  1440. udelay(150); /* wait for warmup */
  1441. }
  1442. /**
  1443. * i9xx_disable_pll - disable a PLL
  1444. * @dev_priv: i915 private structure
  1445. * @pipe: pipe PLL to disable
  1446. *
  1447. * Disable the PLL for @pipe, making sure the pipe is off first.
  1448. *
  1449. * Note! This is for pre-ILK only.
  1450. */
  1451. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1452. {
  1453. struct drm_device *dev = crtc->base.dev;
  1454. struct drm_i915_private *dev_priv = dev->dev_private;
  1455. enum pipe pipe = crtc->pipe;
  1456. /* Disable DVO 2x clock on both PLLs if necessary */
  1457. if (IS_I830(dev) &&
  1458. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1459. intel_num_dvo_pipes(dev) == 1) {
  1460. I915_WRITE(DPLL(PIPE_B),
  1461. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1462. I915_WRITE(DPLL(PIPE_A),
  1463. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1464. }
  1465. /* Don't disable pipe or pipe PLLs if needed */
  1466. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1467. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1468. return;
  1469. /* Make sure the pipe isn't still relying on us */
  1470. assert_pipe_disabled(dev_priv, pipe);
  1471. I915_WRITE(DPLL(pipe), 0);
  1472. POSTING_READ(DPLL(pipe));
  1473. }
  1474. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1475. {
  1476. u32 val = 0;
  1477. /* Make sure the pipe isn't still relying on us */
  1478. assert_pipe_disabled(dev_priv, pipe);
  1479. /*
  1480. * Leave integrated clock source and reference clock enabled for pipe B.
  1481. * The latter is needed for VGA hotplug / manual detection.
  1482. */
  1483. if (pipe == PIPE_B)
  1484. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1485. I915_WRITE(DPLL(pipe), val);
  1486. POSTING_READ(DPLL(pipe));
  1487. }
  1488. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1489. {
  1490. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1491. u32 val;
  1492. /* Make sure the pipe isn't still relying on us */
  1493. assert_pipe_disabled(dev_priv, pipe);
  1494. /* Set PLL en = 0 */
  1495. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1496. if (pipe != PIPE_A)
  1497. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1498. I915_WRITE(DPLL(pipe), val);
  1499. POSTING_READ(DPLL(pipe));
  1500. mutex_lock(&dev_priv->dpio_lock);
  1501. /* Disable 10bit clock to display controller */
  1502. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1503. val &= ~DPIO_DCLKP_EN;
  1504. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1505. /* disable left/right clock distribution */
  1506. if (pipe != PIPE_B) {
  1507. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1508. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1509. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1510. } else {
  1511. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1512. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1513. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1514. }
  1515. mutex_unlock(&dev_priv->dpio_lock);
  1516. }
  1517. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1518. struct intel_digital_port *dport)
  1519. {
  1520. u32 port_mask;
  1521. int dpll_reg;
  1522. switch (dport->port) {
  1523. case PORT_B:
  1524. port_mask = DPLL_PORTB_READY_MASK;
  1525. dpll_reg = DPLL(0);
  1526. break;
  1527. case PORT_C:
  1528. port_mask = DPLL_PORTC_READY_MASK;
  1529. dpll_reg = DPLL(0);
  1530. break;
  1531. case PORT_D:
  1532. port_mask = DPLL_PORTD_READY_MASK;
  1533. dpll_reg = DPIO_PHY_STATUS;
  1534. break;
  1535. default:
  1536. BUG();
  1537. }
  1538. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1539. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1540. port_name(dport->port), I915_READ(dpll_reg));
  1541. }
  1542. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1543. {
  1544. struct drm_device *dev = crtc->base.dev;
  1545. struct drm_i915_private *dev_priv = dev->dev_private;
  1546. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1547. if (WARN_ON(pll == NULL))
  1548. return;
  1549. WARN_ON(!pll->config.crtc_mask);
  1550. if (pll->active == 0) {
  1551. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1552. WARN_ON(pll->on);
  1553. assert_shared_dpll_disabled(dev_priv, pll);
  1554. pll->mode_set(dev_priv, pll);
  1555. }
  1556. }
  1557. /**
  1558. * intel_enable_shared_dpll - enable PCH PLL
  1559. * @dev_priv: i915 private structure
  1560. * @pipe: pipe PLL to enable
  1561. *
  1562. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1563. * drives the transcoder clock.
  1564. */
  1565. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1566. {
  1567. struct drm_device *dev = crtc->base.dev;
  1568. struct drm_i915_private *dev_priv = dev->dev_private;
  1569. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1570. if (WARN_ON(pll == NULL))
  1571. return;
  1572. if (WARN_ON(pll->config.crtc_mask == 0))
  1573. return;
  1574. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1575. pll->name, pll->active, pll->on,
  1576. crtc->base.base.id);
  1577. if (pll->active++) {
  1578. WARN_ON(!pll->on);
  1579. assert_shared_dpll_enabled(dev_priv, pll);
  1580. return;
  1581. }
  1582. WARN_ON(pll->on);
  1583. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1584. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1585. pll->enable(dev_priv, pll);
  1586. pll->on = true;
  1587. }
  1588. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1589. {
  1590. struct drm_device *dev = crtc->base.dev;
  1591. struct drm_i915_private *dev_priv = dev->dev_private;
  1592. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1593. /* PCH only available on ILK+ */
  1594. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1595. if (WARN_ON(pll == NULL))
  1596. return;
  1597. if (WARN_ON(pll->config.crtc_mask == 0))
  1598. return;
  1599. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1600. pll->name, pll->active, pll->on,
  1601. crtc->base.base.id);
  1602. if (WARN_ON(pll->active == 0)) {
  1603. assert_shared_dpll_disabled(dev_priv, pll);
  1604. return;
  1605. }
  1606. assert_shared_dpll_enabled(dev_priv, pll);
  1607. WARN_ON(!pll->on);
  1608. if (--pll->active)
  1609. return;
  1610. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1611. pll->disable(dev_priv, pll);
  1612. pll->on = false;
  1613. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1614. }
  1615. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1616. enum pipe pipe)
  1617. {
  1618. struct drm_device *dev = dev_priv->dev;
  1619. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1620. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1621. uint32_t reg, val, pipeconf_val;
  1622. /* PCH only available on ILK+ */
  1623. BUG_ON(!HAS_PCH_SPLIT(dev));
  1624. /* Make sure PCH DPLL is enabled */
  1625. assert_shared_dpll_enabled(dev_priv,
  1626. intel_crtc_to_shared_dpll(intel_crtc));
  1627. /* FDI must be feeding us bits for PCH ports */
  1628. assert_fdi_tx_enabled(dev_priv, pipe);
  1629. assert_fdi_rx_enabled(dev_priv, pipe);
  1630. if (HAS_PCH_CPT(dev)) {
  1631. /* Workaround: Set the timing override bit before enabling the
  1632. * pch transcoder. */
  1633. reg = TRANS_CHICKEN2(pipe);
  1634. val = I915_READ(reg);
  1635. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1636. I915_WRITE(reg, val);
  1637. }
  1638. reg = PCH_TRANSCONF(pipe);
  1639. val = I915_READ(reg);
  1640. pipeconf_val = I915_READ(PIPECONF(pipe));
  1641. if (HAS_PCH_IBX(dev_priv->dev)) {
  1642. /*
  1643. * make the BPC in transcoder be consistent with
  1644. * that in pipeconf reg.
  1645. */
  1646. val &= ~PIPECONF_BPC_MASK;
  1647. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1648. }
  1649. val &= ~TRANS_INTERLACE_MASK;
  1650. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1651. if (HAS_PCH_IBX(dev_priv->dev) &&
  1652. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1653. val |= TRANS_LEGACY_INTERLACED_ILK;
  1654. else
  1655. val |= TRANS_INTERLACED;
  1656. else
  1657. val |= TRANS_PROGRESSIVE;
  1658. I915_WRITE(reg, val | TRANS_ENABLE);
  1659. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1660. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1661. }
  1662. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1663. enum transcoder cpu_transcoder)
  1664. {
  1665. u32 val, pipeconf_val;
  1666. /* PCH only available on ILK+ */
  1667. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1668. /* FDI must be feeding us bits for PCH ports */
  1669. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1670. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1671. /* Workaround: set timing override bit. */
  1672. val = I915_READ(_TRANSA_CHICKEN2);
  1673. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1674. I915_WRITE(_TRANSA_CHICKEN2, val);
  1675. val = TRANS_ENABLE;
  1676. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1677. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1678. PIPECONF_INTERLACED_ILK)
  1679. val |= TRANS_INTERLACED;
  1680. else
  1681. val |= TRANS_PROGRESSIVE;
  1682. I915_WRITE(LPT_TRANSCONF, val);
  1683. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1684. DRM_ERROR("Failed to enable PCH transcoder\n");
  1685. }
  1686. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1687. enum pipe pipe)
  1688. {
  1689. struct drm_device *dev = dev_priv->dev;
  1690. uint32_t reg, val;
  1691. /* FDI relies on the transcoder */
  1692. assert_fdi_tx_disabled(dev_priv, pipe);
  1693. assert_fdi_rx_disabled(dev_priv, pipe);
  1694. /* Ports must be off as well */
  1695. assert_pch_ports_disabled(dev_priv, pipe);
  1696. reg = PCH_TRANSCONF(pipe);
  1697. val = I915_READ(reg);
  1698. val &= ~TRANS_ENABLE;
  1699. I915_WRITE(reg, val);
  1700. /* wait for PCH transcoder off, transcoder state */
  1701. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1702. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1703. if (!HAS_PCH_IBX(dev)) {
  1704. /* Workaround: Clear the timing override chicken bit again. */
  1705. reg = TRANS_CHICKEN2(pipe);
  1706. val = I915_READ(reg);
  1707. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1708. I915_WRITE(reg, val);
  1709. }
  1710. }
  1711. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1712. {
  1713. u32 val;
  1714. val = I915_READ(LPT_TRANSCONF);
  1715. val &= ~TRANS_ENABLE;
  1716. I915_WRITE(LPT_TRANSCONF, val);
  1717. /* wait for PCH transcoder off, transcoder state */
  1718. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1719. DRM_ERROR("Failed to disable PCH transcoder\n");
  1720. /* Workaround: clear timing override bit. */
  1721. val = I915_READ(_TRANSA_CHICKEN2);
  1722. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1723. I915_WRITE(_TRANSA_CHICKEN2, val);
  1724. }
  1725. /**
  1726. * intel_enable_pipe - enable a pipe, asserting requirements
  1727. * @crtc: crtc responsible for the pipe
  1728. *
  1729. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1730. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1731. */
  1732. static void intel_enable_pipe(struct intel_crtc *crtc)
  1733. {
  1734. struct drm_device *dev = crtc->base.dev;
  1735. struct drm_i915_private *dev_priv = dev->dev_private;
  1736. enum pipe pipe = crtc->pipe;
  1737. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1738. pipe);
  1739. enum pipe pch_transcoder;
  1740. int reg;
  1741. u32 val;
  1742. assert_planes_disabled(dev_priv, pipe);
  1743. assert_cursor_disabled(dev_priv, pipe);
  1744. assert_sprites_disabled(dev_priv, pipe);
  1745. if (HAS_PCH_LPT(dev_priv->dev))
  1746. pch_transcoder = TRANSCODER_A;
  1747. else
  1748. pch_transcoder = pipe;
  1749. /*
  1750. * A pipe without a PLL won't actually be able to drive bits from
  1751. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1752. * need the check.
  1753. */
  1754. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1755. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1756. assert_dsi_pll_enabled(dev_priv);
  1757. else
  1758. assert_pll_enabled(dev_priv, pipe);
  1759. else {
  1760. if (crtc->config->has_pch_encoder) {
  1761. /* if driving the PCH, we need FDI enabled */
  1762. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1763. assert_fdi_tx_pll_enabled(dev_priv,
  1764. (enum pipe) cpu_transcoder);
  1765. }
  1766. /* FIXME: assert CPU port conditions for SNB+ */
  1767. }
  1768. reg = PIPECONF(cpu_transcoder);
  1769. val = I915_READ(reg);
  1770. if (val & PIPECONF_ENABLE) {
  1771. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1772. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1773. return;
  1774. }
  1775. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1776. POSTING_READ(reg);
  1777. }
  1778. /**
  1779. * intel_disable_pipe - disable a pipe, asserting requirements
  1780. * @crtc: crtc whose pipes is to be disabled
  1781. *
  1782. * Disable the pipe of @crtc, making sure that various hardware
  1783. * specific requirements are met, if applicable, e.g. plane
  1784. * disabled, panel fitter off, etc.
  1785. *
  1786. * Will wait until the pipe has shut down before returning.
  1787. */
  1788. static void intel_disable_pipe(struct intel_crtc *crtc)
  1789. {
  1790. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1791. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1792. enum pipe pipe = crtc->pipe;
  1793. int reg;
  1794. u32 val;
  1795. /*
  1796. * Make sure planes won't keep trying to pump pixels to us,
  1797. * or we might hang the display.
  1798. */
  1799. assert_planes_disabled(dev_priv, pipe);
  1800. assert_cursor_disabled(dev_priv, pipe);
  1801. assert_sprites_disabled(dev_priv, pipe);
  1802. reg = PIPECONF(cpu_transcoder);
  1803. val = I915_READ(reg);
  1804. if ((val & PIPECONF_ENABLE) == 0)
  1805. return;
  1806. /*
  1807. * Double wide has implications for planes
  1808. * so best keep it disabled when not needed.
  1809. */
  1810. if (crtc->config->double_wide)
  1811. val &= ~PIPECONF_DOUBLE_WIDE;
  1812. /* Don't disable pipe or pipe PLLs if needed */
  1813. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1814. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1815. val &= ~PIPECONF_ENABLE;
  1816. I915_WRITE(reg, val);
  1817. if ((val & PIPECONF_ENABLE) == 0)
  1818. intel_wait_for_pipe_off(crtc);
  1819. }
  1820. /*
  1821. * Plane regs are double buffered, going from enabled->disabled needs a
  1822. * trigger in order to latch. The display address reg provides this.
  1823. */
  1824. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1825. enum plane plane)
  1826. {
  1827. struct drm_device *dev = dev_priv->dev;
  1828. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1829. I915_WRITE(reg, I915_READ(reg));
  1830. POSTING_READ(reg);
  1831. }
  1832. /**
  1833. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1834. * @plane: plane to be enabled
  1835. * @crtc: crtc for the plane
  1836. *
  1837. * Enable @plane on @crtc, making sure that the pipe is running first.
  1838. */
  1839. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1840. struct drm_crtc *crtc)
  1841. {
  1842. struct drm_device *dev = plane->dev;
  1843. struct drm_i915_private *dev_priv = dev->dev_private;
  1844. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1845. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1846. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1847. if (intel_crtc->primary_enabled)
  1848. return;
  1849. intel_crtc->primary_enabled = true;
  1850. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1851. crtc->x, crtc->y);
  1852. /*
  1853. * BDW signals flip done immediately if the plane
  1854. * is disabled, even if the plane enable is already
  1855. * armed to occur at the next vblank :(
  1856. */
  1857. if (IS_BROADWELL(dev))
  1858. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1859. }
  1860. /**
  1861. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1862. * @plane: plane to be disabled
  1863. * @crtc: crtc for the plane
  1864. *
  1865. * Disable @plane on @crtc, making sure that the pipe is running first.
  1866. */
  1867. static void intel_disable_primary_hw_plane(struct drm_plane *plane,
  1868. struct drm_crtc *crtc)
  1869. {
  1870. struct drm_device *dev = plane->dev;
  1871. struct drm_i915_private *dev_priv = dev->dev_private;
  1872. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1873. if (WARN_ON(!intel_crtc->active))
  1874. return;
  1875. if (!intel_crtc->primary_enabled)
  1876. return;
  1877. intel_crtc->primary_enabled = false;
  1878. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1879. crtc->x, crtc->y);
  1880. }
  1881. static bool need_vtd_wa(struct drm_device *dev)
  1882. {
  1883. #ifdef CONFIG_INTEL_IOMMU
  1884. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1885. return true;
  1886. #endif
  1887. return false;
  1888. }
  1889. int
  1890. intel_fb_align_height(struct drm_device *dev, int height, unsigned int tiling)
  1891. {
  1892. int tile_height;
  1893. tile_height = tiling ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1894. return ALIGN(height, tile_height);
  1895. }
  1896. int
  1897. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1898. struct drm_framebuffer *fb,
  1899. struct intel_engine_cs *pipelined)
  1900. {
  1901. struct drm_device *dev = fb->dev;
  1902. struct drm_i915_private *dev_priv = dev->dev_private;
  1903. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1904. u32 alignment;
  1905. int ret;
  1906. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1907. switch (obj->tiling_mode) {
  1908. case I915_TILING_NONE:
  1909. if (INTEL_INFO(dev)->gen >= 9)
  1910. alignment = 256 * 1024;
  1911. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1912. alignment = 128 * 1024;
  1913. else if (INTEL_INFO(dev)->gen >= 4)
  1914. alignment = 4 * 1024;
  1915. else
  1916. alignment = 64 * 1024;
  1917. break;
  1918. case I915_TILING_X:
  1919. if (INTEL_INFO(dev)->gen >= 9)
  1920. alignment = 256 * 1024;
  1921. else {
  1922. /* pin() will align the object as required by fence */
  1923. alignment = 0;
  1924. }
  1925. break;
  1926. case I915_TILING_Y:
  1927. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1928. return -EINVAL;
  1929. default:
  1930. BUG();
  1931. }
  1932. /* Note that the w/a also requires 64 PTE of padding following the
  1933. * bo. We currently fill all unused PTE with the shadow page and so
  1934. * we should always have valid PTE following the scanout preventing
  1935. * the VT-d warning.
  1936. */
  1937. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1938. alignment = 256 * 1024;
  1939. /*
  1940. * Global gtt pte registers are special registers which actually forward
  1941. * writes to a chunk of system memory. Which means that there is no risk
  1942. * that the register values disappear as soon as we call
  1943. * intel_runtime_pm_put(), so it is correct to wrap only the
  1944. * pin/unpin/fence and not more.
  1945. */
  1946. intel_runtime_pm_get(dev_priv);
  1947. dev_priv->mm.interruptible = false;
  1948. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1949. if (ret)
  1950. goto err_interruptible;
  1951. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1952. * fence, whereas 965+ only requires a fence if using
  1953. * framebuffer compression. For simplicity, we always install
  1954. * a fence as the cost is not that onerous.
  1955. */
  1956. ret = i915_gem_object_get_fence(obj);
  1957. if (ret)
  1958. goto err_unpin;
  1959. i915_gem_object_pin_fence(obj);
  1960. dev_priv->mm.interruptible = true;
  1961. intel_runtime_pm_put(dev_priv);
  1962. return 0;
  1963. err_unpin:
  1964. i915_gem_object_unpin_from_display_plane(obj);
  1965. err_interruptible:
  1966. dev_priv->mm.interruptible = true;
  1967. intel_runtime_pm_put(dev_priv);
  1968. return ret;
  1969. }
  1970. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1971. {
  1972. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1973. i915_gem_object_unpin_fence(obj);
  1974. i915_gem_object_unpin_from_display_plane(obj);
  1975. }
  1976. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1977. * is assumed to be a power-of-two. */
  1978. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1979. unsigned int tiling_mode,
  1980. unsigned int cpp,
  1981. unsigned int pitch)
  1982. {
  1983. if (tiling_mode != I915_TILING_NONE) {
  1984. unsigned int tile_rows, tiles;
  1985. tile_rows = *y / 8;
  1986. *y %= 8;
  1987. tiles = *x / (512/cpp);
  1988. *x %= 512/cpp;
  1989. return tile_rows * pitch * 8 + tiles * 4096;
  1990. } else {
  1991. unsigned int offset;
  1992. offset = *y * pitch + *x * cpp;
  1993. *y = 0;
  1994. *x = (offset & 4095) / cpp;
  1995. return offset & -4096;
  1996. }
  1997. }
  1998. static int i9xx_format_to_fourcc(int format)
  1999. {
  2000. switch (format) {
  2001. case DISPPLANE_8BPP:
  2002. return DRM_FORMAT_C8;
  2003. case DISPPLANE_BGRX555:
  2004. return DRM_FORMAT_XRGB1555;
  2005. case DISPPLANE_BGRX565:
  2006. return DRM_FORMAT_RGB565;
  2007. default:
  2008. case DISPPLANE_BGRX888:
  2009. return DRM_FORMAT_XRGB8888;
  2010. case DISPPLANE_RGBX888:
  2011. return DRM_FORMAT_XBGR8888;
  2012. case DISPPLANE_BGRX101010:
  2013. return DRM_FORMAT_XRGB2101010;
  2014. case DISPPLANE_RGBX101010:
  2015. return DRM_FORMAT_XBGR2101010;
  2016. }
  2017. }
  2018. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2019. {
  2020. switch (format) {
  2021. case PLANE_CTL_FORMAT_RGB_565:
  2022. return DRM_FORMAT_RGB565;
  2023. default:
  2024. case PLANE_CTL_FORMAT_XRGB_8888:
  2025. if (rgb_order) {
  2026. if (alpha)
  2027. return DRM_FORMAT_ABGR8888;
  2028. else
  2029. return DRM_FORMAT_XBGR8888;
  2030. } else {
  2031. if (alpha)
  2032. return DRM_FORMAT_ARGB8888;
  2033. else
  2034. return DRM_FORMAT_XRGB8888;
  2035. }
  2036. case PLANE_CTL_FORMAT_XRGB_2101010:
  2037. if (rgb_order)
  2038. return DRM_FORMAT_XBGR2101010;
  2039. else
  2040. return DRM_FORMAT_XRGB2101010;
  2041. }
  2042. }
  2043. static bool
  2044. intel_alloc_plane_obj(struct intel_crtc *crtc,
  2045. struct intel_initial_plane_config *plane_config)
  2046. {
  2047. struct drm_device *dev = crtc->base.dev;
  2048. struct drm_i915_gem_object *obj = NULL;
  2049. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2050. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2051. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2052. PAGE_SIZE);
  2053. size_aligned -= base_aligned;
  2054. if (plane_config->size == 0)
  2055. return false;
  2056. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2057. base_aligned,
  2058. base_aligned,
  2059. size_aligned);
  2060. if (!obj)
  2061. return false;
  2062. obj->tiling_mode = plane_config->tiling;
  2063. if (obj->tiling_mode == I915_TILING_X)
  2064. obj->stride = crtc->base.primary->fb->pitches[0];
  2065. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  2066. mode_cmd.width = crtc->base.primary->fb->width;
  2067. mode_cmd.height = crtc->base.primary->fb->height;
  2068. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  2069. mutex_lock(&dev->struct_mutex);
  2070. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  2071. &mode_cmd, obj)) {
  2072. DRM_DEBUG_KMS("intel fb init failed\n");
  2073. goto out_unref_obj;
  2074. }
  2075. obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
  2076. mutex_unlock(&dev->struct_mutex);
  2077. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  2078. return true;
  2079. out_unref_obj:
  2080. drm_gem_object_unreference(&obj->base);
  2081. mutex_unlock(&dev->struct_mutex);
  2082. return false;
  2083. }
  2084. static void
  2085. intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2086. struct intel_initial_plane_config *plane_config)
  2087. {
  2088. struct drm_device *dev = intel_crtc->base.dev;
  2089. struct drm_i915_private *dev_priv = dev->dev_private;
  2090. struct drm_crtc *c;
  2091. struct intel_crtc *i;
  2092. struct drm_i915_gem_object *obj;
  2093. if (!intel_crtc->base.primary->fb)
  2094. return;
  2095. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2096. return;
  2097. kfree(intel_crtc->base.primary->fb);
  2098. intel_crtc->base.primary->fb = NULL;
  2099. /*
  2100. * Failed to alloc the obj, check to see if we should share
  2101. * an fb with another CRTC instead
  2102. */
  2103. for_each_crtc(dev, c) {
  2104. i = to_intel_crtc(c);
  2105. if (c == &intel_crtc->base)
  2106. continue;
  2107. if (!i->active)
  2108. continue;
  2109. obj = intel_fb_obj(c->primary->fb);
  2110. if (obj == NULL)
  2111. continue;
  2112. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2113. if (obj->tiling_mode != I915_TILING_NONE)
  2114. dev_priv->preserve_bios_swizzle = true;
  2115. drm_framebuffer_reference(c->primary->fb);
  2116. intel_crtc->base.primary->fb = c->primary->fb;
  2117. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2118. break;
  2119. }
  2120. }
  2121. }
  2122. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2123. struct drm_framebuffer *fb,
  2124. int x, int y)
  2125. {
  2126. struct drm_device *dev = crtc->dev;
  2127. struct drm_i915_private *dev_priv = dev->dev_private;
  2128. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2129. struct drm_i915_gem_object *obj;
  2130. int plane = intel_crtc->plane;
  2131. unsigned long linear_offset;
  2132. u32 dspcntr;
  2133. u32 reg = DSPCNTR(plane);
  2134. int pixel_size;
  2135. if (!intel_crtc->primary_enabled) {
  2136. I915_WRITE(reg, 0);
  2137. if (INTEL_INFO(dev)->gen >= 4)
  2138. I915_WRITE(DSPSURF(plane), 0);
  2139. else
  2140. I915_WRITE(DSPADDR(plane), 0);
  2141. POSTING_READ(reg);
  2142. return;
  2143. }
  2144. obj = intel_fb_obj(fb);
  2145. if (WARN_ON(obj == NULL))
  2146. return;
  2147. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2148. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2149. dspcntr |= DISPLAY_PLANE_ENABLE;
  2150. if (INTEL_INFO(dev)->gen < 4) {
  2151. if (intel_crtc->pipe == PIPE_B)
  2152. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2153. /* pipesrc and dspsize control the size that is scaled from,
  2154. * which should always be the user's requested size.
  2155. */
  2156. I915_WRITE(DSPSIZE(plane),
  2157. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2158. (intel_crtc->config->pipe_src_w - 1));
  2159. I915_WRITE(DSPPOS(plane), 0);
  2160. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2161. I915_WRITE(PRIMSIZE(plane),
  2162. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2163. (intel_crtc->config->pipe_src_w - 1));
  2164. I915_WRITE(PRIMPOS(plane), 0);
  2165. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2166. }
  2167. switch (fb->pixel_format) {
  2168. case DRM_FORMAT_C8:
  2169. dspcntr |= DISPPLANE_8BPP;
  2170. break;
  2171. case DRM_FORMAT_XRGB1555:
  2172. case DRM_FORMAT_ARGB1555:
  2173. dspcntr |= DISPPLANE_BGRX555;
  2174. break;
  2175. case DRM_FORMAT_RGB565:
  2176. dspcntr |= DISPPLANE_BGRX565;
  2177. break;
  2178. case DRM_FORMAT_XRGB8888:
  2179. case DRM_FORMAT_ARGB8888:
  2180. dspcntr |= DISPPLANE_BGRX888;
  2181. break;
  2182. case DRM_FORMAT_XBGR8888:
  2183. case DRM_FORMAT_ABGR8888:
  2184. dspcntr |= DISPPLANE_RGBX888;
  2185. break;
  2186. case DRM_FORMAT_XRGB2101010:
  2187. case DRM_FORMAT_ARGB2101010:
  2188. dspcntr |= DISPPLANE_BGRX101010;
  2189. break;
  2190. case DRM_FORMAT_XBGR2101010:
  2191. case DRM_FORMAT_ABGR2101010:
  2192. dspcntr |= DISPPLANE_RGBX101010;
  2193. break;
  2194. default:
  2195. BUG();
  2196. }
  2197. if (INTEL_INFO(dev)->gen >= 4 &&
  2198. obj->tiling_mode != I915_TILING_NONE)
  2199. dspcntr |= DISPPLANE_TILED;
  2200. if (IS_G4X(dev))
  2201. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2202. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2203. if (INTEL_INFO(dev)->gen >= 4) {
  2204. intel_crtc->dspaddr_offset =
  2205. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2206. pixel_size,
  2207. fb->pitches[0]);
  2208. linear_offset -= intel_crtc->dspaddr_offset;
  2209. } else {
  2210. intel_crtc->dspaddr_offset = linear_offset;
  2211. }
  2212. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2213. dspcntr |= DISPPLANE_ROTATE_180;
  2214. x += (intel_crtc->config->pipe_src_w - 1);
  2215. y += (intel_crtc->config->pipe_src_h - 1);
  2216. /* Finding the last pixel of the last line of the display
  2217. data and adding to linear_offset*/
  2218. linear_offset +=
  2219. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2220. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2221. }
  2222. I915_WRITE(reg, dspcntr);
  2223. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2224. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2225. fb->pitches[0]);
  2226. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2227. if (INTEL_INFO(dev)->gen >= 4) {
  2228. I915_WRITE(DSPSURF(plane),
  2229. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2230. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2231. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2232. } else
  2233. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2234. POSTING_READ(reg);
  2235. }
  2236. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2237. struct drm_framebuffer *fb,
  2238. int x, int y)
  2239. {
  2240. struct drm_device *dev = crtc->dev;
  2241. struct drm_i915_private *dev_priv = dev->dev_private;
  2242. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2243. struct drm_i915_gem_object *obj;
  2244. int plane = intel_crtc->plane;
  2245. unsigned long linear_offset;
  2246. u32 dspcntr;
  2247. u32 reg = DSPCNTR(plane);
  2248. int pixel_size;
  2249. if (!intel_crtc->primary_enabled) {
  2250. I915_WRITE(reg, 0);
  2251. I915_WRITE(DSPSURF(plane), 0);
  2252. POSTING_READ(reg);
  2253. return;
  2254. }
  2255. obj = intel_fb_obj(fb);
  2256. if (WARN_ON(obj == NULL))
  2257. return;
  2258. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2259. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2260. dspcntr |= DISPLAY_PLANE_ENABLE;
  2261. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2262. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2263. switch (fb->pixel_format) {
  2264. case DRM_FORMAT_C8:
  2265. dspcntr |= DISPPLANE_8BPP;
  2266. break;
  2267. case DRM_FORMAT_RGB565:
  2268. dspcntr |= DISPPLANE_BGRX565;
  2269. break;
  2270. case DRM_FORMAT_XRGB8888:
  2271. case DRM_FORMAT_ARGB8888:
  2272. dspcntr |= DISPPLANE_BGRX888;
  2273. break;
  2274. case DRM_FORMAT_XBGR8888:
  2275. case DRM_FORMAT_ABGR8888:
  2276. dspcntr |= DISPPLANE_RGBX888;
  2277. break;
  2278. case DRM_FORMAT_XRGB2101010:
  2279. case DRM_FORMAT_ARGB2101010:
  2280. dspcntr |= DISPPLANE_BGRX101010;
  2281. break;
  2282. case DRM_FORMAT_XBGR2101010:
  2283. case DRM_FORMAT_ABGR2101010:
  2284. dspcntr |= DISPPLANE_RGBX101010;
  2285. break;
  2286. default:
  2287. BUG();
  2288. }
  2289. if (obj->tiling_mode != I915_TILING_NONE)
  2290. dspcntr |= DISPPLANE_TILED;
  2291. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2292. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2293. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2294. intel_crtc->dspaddr_offset =
  2295. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2296. pixel_size,
  2297. fb->pitches[0]);
  2298. linear_offset -= intel_crtc->dspaddr_offset;
  2299. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2300. dspcntr |= DISPPLANE_ROTATE_180;
  2301. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2302. x += (intel_crtc->config->pipe_src_w - 1);
  2303. y += (intel_crtc->config->pipe_src_h - 1);
  2304. /* Finding the last pixel of the last line of the display
  2305. data and adding to linear_offset*/
  2306. linear_offset +=
  2307. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2308. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2309. }
  2310. }
  2311. I915_WRITE(reg, dspcntr);
  2312. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2313. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2314. fb->pitches[0]);
  2315. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2316. I915_WRITE(DSPSURF(plane),
  2317. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2318. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2319. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2320. } else {
  2321. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2322. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2323. }
  2324. POSTING_READ(reg);
  2325. }
  2326. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2327. struct drm_framebuffer *fb,
  2328. int x, int y)
  2329. {
  2330. struct drm_device *dev = crtc->dev;
  2331. struct drm_i915_private *dev_priv = dev->dev_private;
  2332. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2333. struct intel_framebuffer *intel_fb;
  2334. struct drm_i915_gem_object *obj;
  2335. int pipe = intel_crtc->pipe;
  2336. u32 plane_ctl, stride;
  2337. if (!intel_crtc->primary_enabled) {
  2338. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2339. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2340. POSTING_READ(PLANE_CTL(pipe, 0));
  2341. return;
  2342. }
  2343. plane_ctl = PLANE_CTL_ENABLE |
  2344. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2345. PLANE_CTL_PIPE_CSC_ENABLE;
  2346. switch (fb->pixel_format) {
  2347. case DRM_FORMAT_RGB565:
  2348. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  2349. break;
  2350. case DRM_FORMAT_XRGB8888:
  2351. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2352. break;
  2353. case DRM_FORMAT_ARGB8888:
  2354. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2355. plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2356. break;
  2357. case DRM_FORMAT_XBGR8888:
  2358. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2359. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2360. break;
  2361. case DRM_FORMAT_ABGR8888:
  2362. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2363. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  2364. plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2365. break;
  2366. case DRM_FORMAT_XRGB2101010:
  2367. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2368. break;
  2369. case DRM_FORMAT_XBGR2101010:
  2370. plane_ctl |= PLANE_CTL_ORDER_RGBX;
  2371. plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
  2372. break;
  2373. default:
  2374. BUG();
  2375. }
  2376. intel_fb = to_intel_framebuffer(fb);
  2377. obj = intel_fb->obj;
  2378. /*
  2379. * The stride is either expressed as a multiple of 64 bytes chunks for
  2380. * linear buffers or in number of tiles for tiled buffers.
  2381. */
  2382. switch (obj->tiling_mode) {
  2383. case I915_TILING_NONE:
  2384. stride = fb->pitches[0] >> 6;
  2385. break;
  2386. case I915_TILING_X:
  2387. plane_ctl |= PLANE_CTL_TILED_X;
  2388. stride = fb->pitches[0] >> 9;
  2389. break;
  2390. default:
  2391. BUG();
  2392. }
  2393. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2394. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180))
  2395. plane_ctl |= PLANE_CTL_ROTATE_180;
  2396. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2397. DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
  2398. i915_gem_obj_ggtt_offset(obj),
  2399. x, y, fb->width, fb->height,
  2400. fb->pitches[0]);
  2401. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2402. I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
  2403. I915_WRITE(PLANE_SIZE(pipe, 0),
  2404. (intel_crtc->config->pipe_src_h - 1) << 16 |
  2405. (intel_crtc->config->pipe_src_w - 1));
  2406. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2407. I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
  2408. POSTING_READ(PLANE_SURF(pipe, 0));
  2409. }
  2410. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2411. static int
  2412. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2413. int x, int y, enum mode_set_atomic state)
  2414. {
  2415. struct drm_device *dev = crtc->dev;
  2416. struct drm_i915_private *dev_priv = dev->dev_private;
  2417. if (dev_priv->display.disable_fbc)
  2418. dev_priv->display.disable_fbc(dev);
  2419. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2420. return 0;
  2421. }
  2422. static void intel_complete_page_flips(struct drm_device *dev)
  2423. {
  2424. struct drm_crtc *crtc;
  2425. for_each_crtc(dev, crtc) {
  2426. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2427. enum plane plane = intel_crtc->plane;
  2428. intel_prepare_page_flip(dev, plane);
  2429. intel_finish_page_flip_plane(dev, plane);
  2430. }
  2431. }
  2432. static void intel_update_primary_planes(struct drm_device *dev)
  2433. {
  2434. struct drm_i915_private *dev_priv = dev->dev_private;
  2435. struct drm_crtc *crtc;
  2436. for_each_crtc(dev, crtc) {
  2437. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2438. drm_modeset_lock(&crtc->mutex, NULL);
  2439. /*
  2440. * FIXME: Once we have proper support for primary planes (and
  2441. * disabling them without disabling the entire crtc) allow again
  2442. * a NULL crtc->primary->fb.
  2443. */
  2444. if (intel_crtc->active && crtc->primary->fb)
  2445. dev_priv->display.update_primary_plane(crtc,
  2446. crtc->primary->fb,
  2447. crtc->x,
  2448. crtc->y);
  2449. drm_modeset_unlock(&crtc->mutex);
  2450. }
  2451. }
  2452. void intel_prepare_reset(struct drm_device *dev)
  2453. {
  2454. struct drm_i915_private *dev_priv = to_i915(dev);
  2455. struct intel_crtc *crtc;
  2456. /* no reset support for gen2 */
  2457. if (IS_GEN2(dev))
  2458. return;
  2459. /* reset doesn't touch the display */
  2460. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2461. return;
  2462. drm_modeset_lock_all(dev);
  2463. /*
  2464. * Disabling the crtcs gracefully seems nicer. Also the
  2465. * g33 docs say we should at least disable all the planes.
  2466. */
  2467. for_each_intel_crtc(dev, crtc) {
  2468. if (crtc->active)
  2469. dev_priv->display.crtc_disable(&crtc->base);
  2470. }
  2471. }
  2472. void intel_finish_reset(struct drm_device *dev)
  2473. {
  2474. struct drm_i915_private *dev_priv = to_i915(dev);
  2475. /*
  2476. * Flips in the rings will be nuked by the reset,
  2477. * so complete all pending flips so that user space
  2478. * will get its events and not get stuck.
  2479. */
  2480. intel_complete_page_flips(dev);
  2481. /* no reset support for gen2 */
  2482. if (IS_GEN2(dev))
  2483. return;
  2484. /* reset doesn't touch the display */
  2485. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2486. /*
  2487. * Flips in the rings have been nuked by the reset,
  2488. * so update the base address of all primary
  2489. * planes to the the last fb to make sure we're
  2490. * showing the correct fb after a reset.
  2491. */
  2492. intel_update_primary_planes(dev);
  2493. return;
  2494. }
  2495. /*
  2496. * The display has been reset as well,
  2497. * so need a full re-initialization.
  2498. */
  2499. intel_runtime_pm_disable_interrupts(dev_priv);
  2500. intel_runtime_pm_enable_interrupts(dev_priv);
  2501. intel_modeset_init_hw(dev);
  2502. spin_lock_irq(&dev_priv->irq_lock);
  2503. if (dev_priv->display.hpd_irq_setup)
  2504. dev_priv->display.hpd_irq_setup(dev);
  2505. spin_unlock_irq(&dev_priv->irq_lock);
  2506. intel_modeset_setup_hw_state(dev, true);
  2507. intel_hpd_init(dev_priv);
  2508. drm_modeset_unlock_all(dev);
  2509. }
  2510. static int
  2511. intel_finish_fb(struct drm_framebuffer *old_fb)
  2512. {
  2513. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2514. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2515. bool was_interruptible = dev_priv->mm.interruptible;
  2516. int ret;
  2517. /* Big Hammer, we also need to ensure that any pending
  2518. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2519. * current scanout is retired before unpinning the old
  2520. * framebuffer.
  2521. *
  2522. * This should only fail upon a hung GPU, in which case we
  2523. * can safely continue.
  2524. */
  2525. dev_priv->mm.interruptible = false;
  2526. ret = i915_gem_object_finish_gpu(obj);
  2527. dev_priv->mm.interruptible = was_interruptible;
  2528. return ret;
  2529. }
  2530. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2531. {
  2532. struct drm_device *dev = crtc->dev;
  2533. struct drm_i915_private *dev_priv = dev->dev_private;
  2534. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2535. bool pending;
  2536. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2537. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2538. return false;
  2539. spin_lock_irq(&dev->event_lock);
  2540. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2541. spin_unlock_irq(&dev->event_lock);
  2542. return pending;
  2543. }
  2544. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2545. {
  2546. struct drm_device *dev = crtc->base.dev;
  2547. struct drm_i915_private *dev_priv = dev->dev_private;
  2548. const struct drm_display_mode *adjusted_mode;
  2549. if (!i915.fastboot)
  2550. return;
  2551. /*
  2552. * Update pipe size and adjust fitter if needed: the reason for this is
  2553. * that in compute_mode_changes we check the native mode (not the pfit
  2554. * mode) to see if we can flip rather than do a full mode set. In the
  2555. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2556. * pfit state, we'll end up with a big fb scanned out into the wrong
  2557. * sized surface.
  2558. *
  2559. * To fix this properly, we need to hoist the checks up into
  2560. * compute_mode_changes (or above), check the actual pfit state and
  2561. * whether the platform allows pfit disable with pipe active, and only
  2562. * then update the pipesrc and pfit state, even on the flip path.
  2563. */
  2564. adjusted_mode = &crtc->config->base.adjusted_mode;
  2565. I915_WRITE(PIPESRC(crtc->pipe),
  2566. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2567. (adjusted_mode->crtc_vdisplay - 1));
  2568. if (!crtc->config->pch_pfit.enabled &&
  2569. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2570. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2571. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2572. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2573. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2574. }
  2575. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2576. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2577. }
  2578. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2579. {
  2580. struct drm_device *dev = crtc->dev;
  2581. struct drm_i915_private *dev_priv = dev->dev_private;
  2582. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2583. int pipe = intel_crtc->pipe;
  2584. u32 reg, temp;
  2585. /* enable normal train */
  2586. reg = FDI_TX_CTL(pipe);
  2587. temp = I915_READ(reg);
  2588. if (IS_IVYBRIDGE(dev)) {
  2589. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2590. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2591. } else {
  2592. temp &= ~FDI_LINK_TRAIN_NONE;
  2593. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2594. }
  2595. I915_WRITE(reg, temp);
  2596. reg = FDI_RX_CTL(pipe);
  2597. temp = I915_READ(reg);
  2598. if (HAS_PCH_CPT(dev)) {
  2599. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2600. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2601. } else {
  2602. temp &= ~FDI_LINK_TRAIN_NONE;
  2603. temp |= FDI_LINK_TRAIN_NONE;
  2604. }
  2605. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2606. /* wait one idle pattern time */
  2607. POSTING_READ(reg);
  2608. udelay(1000);
  2609. /* IVB wants error correction enabled */
  2610. if (IS_IVYBRIDGE(dev))
  2611. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2612. FDI_FE_ERRC_ENABLE);
  2613. }
  2614. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2615. {
  2616. return crtc->base.enabled && crtc->active &&
  2617. crtc->config->has_pch_encoder;
  2618. }
  2619. static void ivb_modeset_global_resources(struct drm_device *dev)
  2620. {
  2621. struct drm_i915_private *dev_priv = dev->dev_private;
  2622. struct intel_crtc *pipe_B_crtc =
  2623. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2624. struct intel_crtc *pipe_C_crtc =
  2625. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2626. uint32_t temp;
  2627. /*
  2628. * When everything is off disable fdi C so that we could enable fdi B
  2629. * with all lanes. Note that we don't care about enabled pipes without
  2630. * an enabled pch encoder.
  2631. */
  2632. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2633. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2634. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2635. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2636. temp = I915_READ(SOUTH_CHICKEN1);
  2637. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2638. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2639. I915_WRITE(SOUTH_CHICKEN1, temp);
  2640. }
  2641. }
  2642. /* The FDI link training functions for ILK/Ibexpeak. */
  2643. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2644. {
  2645. struct drm_device *dev = crtc->dev;
  2646. struct drm_i915_private *dev_priv = dev->dev_private;
  2647. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2648. int pipe = intel_crtc->pipe;
  2649. u32 reg, temp, tries;
  2650. /* FDI needs bits from pipe first */
  2651. assert_pipe_enabled(dev_priv, pipe);
  2652. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2653. for train result */
  2654. reg = FDI_RX_IMR(pipe);
  2655. temp = I915_READ(reg);
  2656. temp &= ~FDI_RX_SYMBOL_LOCK;
  2657. temp &= ~FDI_RX_BIT_LOCK;
  2658. I915_WRITE(reg, temp);
  2659. I915_READ(reg);
  2660. udelay(150);
  2661. /* enable CPU FDI TX and PCH FDI RX */
  2662. reg = FDI_TX_CTL(pipe);
  2663. temp = I915_READ(reg);
  2664. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2665. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2666. temp &= ~FDI_LINK_TRAIN_NONE;
  2667. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2668. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2669. reg = FDI_RX_CTL(pipe);
  2670. temp = I915_READ(reg);
  2671. temp &= ~FDI_LINK_TRAIN_NONE;
  2672. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2673. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2674. POSTING_READ(reg);
  2675. udelay(150);
  2676. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2677. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2678. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2679. FDI_RX_PHASE_SYNC_POINTER_EN);
  2680. reg = FDI_RX_IIR(pipe);
  2681. for (tries = 0; tries < 5; tries++) {
  2682. temp = I915_READ(reg);
  2683. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2684. if ((temp & FDI_RX_BIT_LOCK)) {
  2685. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2686. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2687. break;
  2688. }
  2689. }
  2690. if (tries == 5)
  2691. DRM_ERROR("FDI train 1 fail!\n");
  2692. /* Train 2 */
  2693. reg = FDI_TX_CTL(pipe);
  2694. temp = I915_READ(reg);
  2695. temp &= ~FDI_LINK_TRAIN_NONE;
  2696. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2697. I915_WRITE(reg, temp);
  2698. reg = FDI_RX_CTL(pipe);
  2699. temp = I915_READ(reg);
  2700. temp &= ~FDI_LINK_TRAIN_NONE;
  2701. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2702. I915_WRITE(reg, temp);
  2703. POSTING_READ(reg);
  2704. udelay(150);
  2705. reg = FDI_RX_IIR(pipe);
  2706. for (tries = 0; tries < 5; tries++) {
  2707. temp = I915_READ(reg);
  2708. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2709. if (temp & FDI_RX_SYMBOL_LOCK) {
  2710. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2711. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2712. break;
  2713. }
  2714. }
  2715. if (tries == 5)
  2716. DRM_ERROR("FDI train 2 fail!\n");
  2717. DRM_DEBUG_KMS("FDI train done\n");
  2718. }
  2719. static const int snb_b_fdi_train_param[] = {
  2720. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2721. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2722. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2723. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2724. };
  2725. /* The FDI link training functions for SNB/Cougarpoint. */
  2726. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2727. {
  2728. struct drm_device *dev = crtc->dev;
  2729. struct drm_i915_private *dev_priv = dev->dev_private;
  2730. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2731. int pipe = intel_crtc->pipe;
  2732. u32 reg, temp, i, retry;
  2733. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2734. for train result */
  2735. reg = FDI_RX_IMR(pipe);
  2736. temp = I915_READ(reg);
  2737. temp &= ~FDI_RX_SYMBOL_LOCK;
  2738. temp &= ~FDI_RX_BIT_LOCK;
  2739. I915_WRITE(reg, temp);
  2740. POSTING_READ(reg);
  2741. udelay(150);
  2742. /* enable CPU FDI TX and PCH FDI RX */
  2743. reg = FDI_TX_CTL(pipe);
  2744. temp = I915_READ(reg);
  2745. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2746. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2747. temp &= ~FDI_LINK_TRAIN_NONE;
  2748. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2749. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2750. /* SNB-B */
  2751. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2752. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2753. I915_WRITE(FDI_RX_MISC(pipe),
  2754. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2755. reg = FDI_RX_CTL(pipe);
  2756. temp = I915_READ(reg);
  2757. if (HAS_PCH_CPT(dev)) {
  2758. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2759. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2760. } else {
  2761. temp &= ~FDI_LINK_TRAIN_NONE;
  2762. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2763. }
  2764. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2765. POSTING_READ(reg);
  2766. udelay(150);
  2767. for (i = 0; i < 4; i++) {
  2768. reg = FDI_TX_CTL(pipe);
  2769. temp = I915_READ(reg);
  2770. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2771. temp |= snb_b_fdi_train_param[i];
  2772. I915_WRITE(reg, temp);
  2773. POSTING_READ(reg);
  2774. udelay(500);
  2775. for (retry = 0; retry < 5; retry++) {
  2776. reg = FDI_RX_IIR(pipe);
  2777. temp = I915_READ(reg);
  2778. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2779. if (temp & FDI_RX_BIT_LOCK) {
  2780. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2781. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2782. break;
  2783. }
  2784. udelay(50);
  2785. }
  2786. if (retry < 5)
  2787. break;
  2788. }
  2789. if (i == 4)
  2790. DRM_ERROR("FDI train 1 fail!\n");
  2791. /* Train 2 */
  2792. reg = FDI_TX_CTL(pipe);
  2793. temp = I915_READ(reg);
  2794. temp &= ~FDI_LINK_TRAIN_NONE;
  2795. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2796. if (IS_GEN6(dev)) {
  2797. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2798. /* SNB-B */
  2799. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2800. }
  2801. I915_WRITE(reg, temp);
  2802. reg = FDI_RX_CTL(pipe);
  2803. temp = I915_READ(reg);
  2804. if (HAS_PCH_CPT(dev)) {
  2805. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2806. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2807. } else {
  2808. temp &= ~FDI_LINK_TRAIN_NONE;
  2809. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2810. }
  2811. I915_WRITE(reg, temp);
  2812. POSTING_READ(reg);
  2813. udelay(150);
  2814. for (i = 0; i < 4; i++) {
  2815. reg = FDI_TX_CTL(pipe);
  2816. temp = I915_READ(reg);
  2817. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2818. temp |= snb_b_fdi_train_param[i];
  2819. I915_WRITE(reg, temp);
  2820. POSTING_READ(reg);
  2821. udelay(500);
  2822. for (retry = 0; retry < 5; retry++) {
  2823. reg = FDI_RX_IIR(pipe);
  2824. temp = I915_READ(reg);
  2825. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2826. if (temp & FDI_RX_SYMBOL_LOCK) {
  2827. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2828. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2829. break;
  2830. }
  2831. udelay(50);
  2832. }
  2833. if (retry < 5)
  2834. break;
  2835. }
  2836. if (i == 4)
  2837. DRM_ERROR("FDI train 2 fail!\n");
  2838. DRM_DEBUG_KMS("FDI train done.\n");
  2839. }
  2840. /* Manual link training for Ivy Bridge A0 parts */
  2841. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2842. {
  2843. struct drm_device *dev = crtc->dev;
  2844. struct drm_i915_private *dev_priv = dev->dev_private;
  2845. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2846. int pipe = intel_crtc->pipe;
  2847. u32 reg, temp, i, j;
  2848. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2849. for train result */
  2850. reg = FDI_RX_IMR(pipe);
  2851. temp = I915_READ(reg);
  2852. temp &= ~FDI_RX_SYMBOL_LOCK;
  2853. temp &= ~FDI_RX_BIT_LOCK;
  2854. I915_WRITE(reg, temp);
  2855. POSTING_READ(reg);
  2856. udelay(150);
  2857. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2858. I915_READ(FDI_RX_IIR(pipe)));
  2859. /* Try each vswing and preemphasis setting twice before moving on */
  2860. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2861. /* disable first in case we need to retry */
  2862. reg = FDI_TX_CTL(pipe);
  2863. temp = I915_READ(reg);
  2864. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2865. temp &= ~FDI_TX_ENABLE;
  2866. I915_WRITE(reg, temp);
  2867. reg = FDI_RX_CTL(pipe);
  2868. temp = I915_READ(reg);
  2869. temp &= ~FDI_LINK_TRAIN_AUTO;
  2870. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2871. temp &= ~FDI_RX_ENABLE;
  2872. I915_WRITE(reg, temp);
  2873. /* enable CPU FDI TX and PCH FDI RX */
  2874. reg = FDI_TX_CTL(pipe);
  2875. temp = I915_READ(reg);
  2876. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2877. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2878. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2879. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2880. temp |= snb_b_fdi_train_param[j/2];
  2881. temp |= FDI_COMPOSITE_SYNC;
  2882. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2883. I915_WRITE(FDI_RX_MISC(pipe),
  2884. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2885. reg = FDI_RX_CTL(pipe);
  2886. temp = I915_READ(reg);
  2887. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2888. temp |= FDI_COMPOSITE_SYNC;
  2889. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2890. POSTING_READ(reg);
  2891. udelay(1); /* should be 0.5us */
  2892. for (i = 0; i < 4; i++) {
  2893. reg = FDI_RX_IIR(pipe);
  2894. temp = I915_READ(reg);
  2895. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2896. if (temp & FDI_RX_BIT_LOCK ||
  2897. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2898. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2899. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2900. i);
  2901. break;
  2902. }
  2903. udelay(1); /* should be 0.5us */
  2904. }
  2905. if (i == 4) {
  2906. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2907. continue;
  2908. }
  2909. /* Train 2 */
  2910. reg = FDI_TX_CTL(pipe);
  2911. temp = I915_READ(reg);
  2912. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2913. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2914. I915_WRITE(reg, temp);
  2915. reg = FDI_RX_CTL(pipe);
  2916. temp = I915_READ(reg);
  2917. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2918. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2919. I915_WRITE(reg, temp);
  2920. POSTING_READ(reg);
  2921. udelay(2); /* should be 1.5us */
  2922. for (i = 0; i < 4; i++) {
  2923. reg = FDI_RX_IIR(pipe);
  2924. temp = I915_READ(reg);
  2925. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2926. if (temp & FDI_RX_SYMBOL_LOCK ||
  2927. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2928. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2929. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2930. i);
  2931. goto train_done;
  2932. }
  2933. udelay(2); /* should be 1.5us */
  2934. }
  2935. if (i == 4)
  2936. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2937. }
  2938. train_done:
  2939. DRM_DEBUG_KMS("FDI train done.\n");
  2940. }
  2941. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2942. {
  2943. struct drm_device *dev = intel_crtc->base.dev;
  2944. struct drm_i915_private *dev_priv = dev->dev_private;
  2945. int pipe = intel_crtc->pipe;
  2946. u32 reg, temp;
  2947. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2948. reg = FDI_RX_CTL(pipe);
  2949. temp = I915_READ(reg);
  2950. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2951. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2952. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2953. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2954. POSTING_READ(reg);
  2955. udelay(200);
  2956. /* Switch from Rawclk to PCDclk */
  2957. temp = I915_READ(reg);
  2958. I915_WRITE(reg, temp | FDI_PCDCLK);
  2959. POSTING_READ(reg);
  2960. udelay(200);
  2961. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2962. reg = FDI_TX_CTL(pipe);
  2963. temp = I915_READ(reg);
  2964. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2965. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2966. POSTING_READ(reg);
  2967. udelay(100);
  2968. }
  2969. }
  2970. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2971. {
  2972. struct drm_device *dev = intel_crtc->base.dev;
  2973. struct drm_i915_private *dev_priv = dev->dev_private;
  2974. int pipe = intel_crtc->pipe;
  2975. u32 reg, temp;
  2976. /* Switch from PCDclk to Rawclk */
  2977. reg = FDI_RX_CTL(pipe);
  2978. temp = I915_READ(reg);
  2979. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2980. /* Disable CPU FDI TX PLL */
  2981. reg = FDI_TX_CTL(pipe);
  2982. temp = I915_READ(reg);
  2983. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2984. POSTING_READ(reg);
  2985. udelay(100);
  2986. reg = FDI_RX_CTL(pipe);
  2987. temp = I915_READ(reg);
  2988. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2989. /* Wait for the clocks to turn off. */
  2990. POSTING_READ(reg);
  2991. udelay(100);
  2992. }
  2993. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2994. {
  2995. struct drm_device *dev = crtc->dev;
  2996. struct drm_i915_private *dev_priv = dev->dev_private;
  2997. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2998. int pipe = intel_crtc->pipe;
  2999. u32 reg, temp;
  3000. /* disable CPU FDI tx and PCH FDI rx */
  3001. reg = FDI_TX_CTL(pipe);
  3002. temp = I915_READ(reg);
  3003. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3004. POSTING_READ(reg);
  3005. reg = FDI_RX_CTL(pipe);
  3006. temp = I915_READ(reg);
  3007. temp &= ~(0x7 << 16);
  3008. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3009. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3010. POSTING_READ(reg);
  3011. udelay(100);
  3012. /* Ironlake workaround, disable clock pointer after downing FDI */
  3013. if (HAS_PCH_IBX(dev))
  3014. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3015. /* still set train pattern 1 */
  3016. reg = FDI_TX_CTL(pipe);
  3017. temp = I915_READ(reg);
  3018. temp &= ~FDI_LINK_TRAIN_NONE;
  3019. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3020. I915_WRITE(reg, temp);
  3021. reg = FDI_RX_CTL(pipe);
  3022. temp = I915_READ(reg);
  3023. if (HAS_PCH_CPT(dev)) {
  3024. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3025. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3026. } else {
  3027. temp &= ~FDI_LINK_TRAIN_NONE;
  3028. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3029. }
  3030. /* BPC in FDI rx is consistent with that in PIPECONF */
  3031. temp &= ~(0x07 << 16);
  3032. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3033. I915_WRITE(reg, temp);
  3034. POSTING_READ(reg);
  3035. udelay(100);
  3036. }
  3037. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3038. {
  3039. struct intel_crtc *crtc;
  3040. /* Note that we don't need to be called with mode_config.lock here
  3041. * as our list of CRTC objects is static for the lifetime of the
  3042. * device and so cannot disappear as we iterate. Similarly, we can
  3043. * happily treat the predicates as racy, atomic checks as userspace
  3044. * cannot claim and pin a new fb without at least acquring the
  3045. * struct_mutex and so serialising with us.
  3046. */
  3047. for_each_intel_crtc(dev, crtc) {
  3048. if (atomic_read(&crtc->unpin_work_count) == 0)
  3049. continue;
  3050. if (crtc->unpin_work)
  3051. intel_wait_for_vblank(dev, crtc->pipe);
  3052. return true;
  3053. }
  3054. return false;
  3055. }
  3056. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3057. {
  3058. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3059. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3060. /* ensure that the unpin work is consistent wrt ->pending. */
  3061. smp_rmb();
  3062. intel_crtc->unpin_work = NULL;
  3063. if (work->event)
  3064. drm_send_vblank_event(intel_crtc->base.dev,
  3065. intel_crtc->pipe,
  3066. work->event);
  3067. drm_crtc_vblank_put(&intel_crtc->base);
  3068. wake_up_all(&dev_priv->pending_flip_queue);
  3069. queue_work(dev_priv->wq, &work->work);
  3070. trace_i915_flip_complete(intel_crtc->plane,
  3071. work->pending_flip_obj);
  3072. }
  3073. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3074. {
  3075. struct drm_device *dev = crtc->dev;
  3076. struct drm_i915_private *dev_priv = dev->dev_private;
  3077. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3078. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3079. !intel_crtc_has_pending_flip(crtc),
  3080. 60*HZ) == 0)) {
  3081. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3082. spin_lock_irq(&dev->event_lock);
  3083. if (intel_crtc->unpin_work) {
  3084. WARN_ONCE(1, "Removing stuck page flip\n");
  3085. page_flip_completed(intel_crtc);
  3086. }
  3087. spin_unlock_irq(&dev->event_lock);
  3088. }
  3089. if (crtc->primary->fb) {
  3090. mutex_lock(&dev->struct_mutex);
  3091. intel_finish_fb(crtc->primary->fb);
  3092. mutex_unlock(&dev->struct_mutex);
  3093. }
  3094. }
  3095. /* Program iCLKIP clock to the desired frequency */
  3096. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3097. {
  3098. struct drm_device *dev = crtc->dev;
  3099. struct drm_i915_private *dev_priv = dev->dev_private;
  3100. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3101. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3102. u32 temp;
  3103. mutex_lock(&dev_priv->dpio_lock);
  3104. /* It is necessary to ungate the pixclk gate prior to programming
  3105. * the divisors, and gate it back when it is done.
  3106. */
  3107. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3108. /* Disable SSCCTL */
  3109. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3110. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3111. SBI_SSCCTL_DISABLE,
  3112. SBI_ICLK);
  3113. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3114. if (clock == 20000) {
  3115. auxdiv = 1;
  3116. divsel = 0x41;
  3117. phaseinc = 0x20;
  3118. } else {
  3119. /* The iCLK virtual clock root frequency is in MHz,
  3120. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3121. * divisors, it is necessary to divide one by another, so we
  3122. * convert the virtual clock precision to KHz here for higher
  3123. * precision.
  3124. */
  3125. u32 iclk_virtual_root_freq = 172800 * 1000;
  3126. u32 iclk_pi_range = 64;
  3127. u32 desired_divisor, msb_divisor_value, pi_value;
  3128. desired_divisor = (iclk_virtual_root_freq / clock);
  3129. msb_divisor_value = desired_divisor / iclk_pi_range;
  3130. pi_value = desired_divisor % iclk_pi_range;
  3131. auxdiv = 0;
  3132. divsel = msb_divisor_value - 2;
  3133. phaseinc = pi_value;
  3134. }
  3135. /* This should not happen with any sane values */
  3136. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3137. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3138. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3139. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3140. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3141. clock,
  3142. auxdiv,
  3143. divsel,
  3144. phasedir,
  3145. phaseinc);
  3146. /* Program SSCDIVINTPHASE6 */
  3147. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3148. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3149. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3150. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3151. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3152. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3153. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3154. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3155. /* Program SSCAUXDIV */
  3156. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3157. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3158. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3159. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3160. /* Enable modulator and associated divider */
  3161. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3162. temp &= ~SBI_SSCCTL_DISABLE;
  3163. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3164. /* Wait for initialization time */
  3165. udelay(24);
  3166. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3167. mutex_unlock(&dev_priv->dpio_lock);
  3168. }
  3169. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3170. enum pipe pch_transcoder)
  3171. {
  3172. struct drm_device *dev = crtc->base.dev;
  3173. struct drm_i915_private *dev_priv = dev->dev_private;
  3174. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3175. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3176. I915_READ(HTOTAL(cpu_transcoder)));
  3177. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3178. I915_READ(HBLANK(cpu_transcoder)));
  3179. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3180. I915_READ(HSYNC(cpu_transcoder)));
  3181. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3182. I915_READ(VTOTAL(cpu_transcoder)));
  3183. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3184. I915_READ(VBLANK(cpu_transcoder)));
  3185. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3186. I915_READ(VSYNC(cpu_transcoder)));
  3187. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3188. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3189. }
  3190. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  3191. {
  3192. struct drm_i915_private *dev_priv = dev->dev_private;
  3193. uint32_t temp;
  3194. temp = I915_READ(SOUTH_CHICKEN1);
  3195. if (temp & FDI_BC_BIFURCATION_SELECT)
  3196. return;
  3197. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3198. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3199. temp |= FDI_BC_BIFURCATION_SELECT;
  3200. DRM_DEBUG_KMS("enabling fdi C rx\n");
  3201. I915_WRITE(SOUTH_CHICKEN1, temp);
  3202. POSTING_READ(SOUTH_CHICKEN1);
  3203. }
  3204. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3205. {
  3206. struct drm_device *dev = intel_crtc->base.dev;
  3207. struct drm_i915_private *dev_priv = dev->dev_private;
  3208. switch (intel_crtc->pipe) {
  3209. case PIPE_A:
  3210. break;
  3211. case PIPE_B:
  3212. if (intel_crtc->config->fdi_lanes > 2)
  3213. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  3214. else
  3215. cpt_enable_fdi_bc_bifurcation(dev);
  3216. break;
  3217. case PIPE_C:
  3218. cpt_enable_fdi_bc_bifurcation(dev);
  3219. break;
  3220. default:
  3221. BUG();
  3222. }
  3223. }
  3224. /*
  3225. * Enable PCH resources required for PCH ports:
  3226. * - PCH PLLs
  3227. * - FDI training & RX/TX
  3228. * - update transcoder timings
  3229. * - DP transcoding bits
  3230. * - transcoder
  3231. */
  3232. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3233. {
  3234. struct drm_device *dev = crtc->dev;
  3235. struct drm_i915_private *dev_priv = dev->dev_private;
  3236. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3237. int pipe = intel_crtc->pipe;
  3238. u32 reg, temp;
  3239. assert_pch_transcoder_disabled(dev_priv, pipe);
  3240. if (IS_IVYBRIDGE(dev))
  3241. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3242. /* Write the TU size bits before fdi link training, so that error
  3243. * detection works. */
  3244. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3245. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3246. /* For PCH output, training FDI link */
  3247. dev_priv->display.fdi_link_train(crtc);
  3248. /* We need to program the right clock selection before writing the pixel
  3249. * mutliplier into the DPLL. */
  3250. if (HAS_PCH_CPT(dev)) {
  3251. u32 sel;
  3252. temp = I915_READ(PCH_DPLL_SEL);
  3253. temp |= TRANS_DPLL_ENABLE(pipe);
  3254. sel = TRANS_DPLLB_SEL(pipe);
  3255. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3256. temp |= sel;
  3257. else
  3258. temp &= ~sel;
  3259. I915_WRITE(PCH_DPLL_SEL, temp);
  3260. }
  3261. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3262. * transcoder, and we actually should do this to not upset any PCH
  3263. * transcoder that already use the clock when we share it.
  3264. *
  3265. * Note that enable_shared_dpll tries to do the right thing, but
  3266. * get_shared_dpll unconditionally resets the pll - we need that to have
  3267. * the right LVDS enable sequence. */
  3268. intel_enable_shared_dpll(intel_crtc);
  3269. /* set transcoder timing, panel must allow it */
  3270. assert_panel_unlocked(dev_priv, pipe);
  3271. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3272. intel_fdi_normal_train(crtc);
  3273. /* For PCH DP, enable TRANS_DP_CTL */
  3274. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3275. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3276. reg = TRANS_DP_CTL(pipe);
  3277. temp = I915_READ(reg);
  3278. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3279. TRANS_DP_SYNC_MASK |
  3280. TRANS_DP_BPC_MASK);
  3281. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3282. TRANS_DP_ENH_FRAMING);
  3283. temp |= bpc << 9; /* same format but at 11:9 */
  3284. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3285. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3286. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3287. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3288. switch (intel_trans_dp_port_sel(crtc)) {
  3289. case PCH_DP_B:
  3290. temp |= TRANS_DP_PORT_SEL_B;
  3291. break;
  3292. case PCH_DP_C:
  3293. temp |= TRANS_DP_PORT_SEL_C;
  3294. break;
  3295. case PCH_DP_D:
  3296. temp |= TRANS_DP_PORT_SEL_D;
  3297. break;
  3298. default:
  3299. BUG();
  3300. }
  3301. I915_WRITE(reg, temp);
  3302. }
  3303. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3304. }
  3305. static void lpt_pch_enable(struct drm_crtc *crtc)
  3306. {
  3307. struct drm_device *dev = crtc->dev;
  3308. struct drm_i915_private *dev_priv = dev->dev_private;
  3309. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3310. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3311. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3312. lpt_program_iclkip(crtc);
  3313. /* Set transcoder timing. */
  3314. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3315. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3316. }
  3317. void intel_put_shared_dpll(struct intel_crtc *crtc)
  3318. {
  3319. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3320. if (pll == NULL)
  3321. return;
  3322. if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
  3323. WARN(1, "bad %s crtc mask\n", pll->name);
  3324. return;
  3325. }
  3326. pll->config.crtc_mask &= ~(1 << crtc->pipe);
  3327. if (pll->config.crtc_mask == 0) {
  3328. WARN_ON(pll->on);
  3329. WARN_ON(pll->active);
  3330. }
  3331. crtc->config->shared_dpll = DPLL_ID_PRIVATE;
  3332. }
  3333. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3334. struct intel_crtc_state *crtc_state)
  3335. {
  3336. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3337. struct intel_shared_dpll *pll;
  3338. enum intel_dpll_id i;
  3339. if (HAS_PCH_IBX(dev_priv->dev)) {
  3340. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3341. i = (enum intel_dpll_id) crtc->pipe;
  3342. pll = &dev_priv->shared_dplls[i];
  3343. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3344. crtc->base.base.id, pll->name);
  3345. WARN_ON(pll->new_config->crtc_mask);
  3346. goto found;
  3347. }
  3348. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3349. pll = &dev_priv->shared_dplls[i];
  3350. /* Only want to check enabled timings first */
  3351. if (pll->new_config->crtc_mask == 0)
  3352. continue;
  3353. if (memcmp(&crtc_state->dpll_hw_state,
  3354. &pll->new_config->hw_state,
  3355. sizeof(pll->new_config->hw_state)) == 0) {
  3356. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3357. crtc->base.base.id, pll->name,
  3358. pll->new_config->crtc_mask,
  3359. pll->active);
  3360. goto found;
  3361. }
  3362. }
  3363. /* Ok no matching timings, maybe there's a free one? */
  3364. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3365. pll = &dev_priv->shared_dplls[i];
  3366. if (pll->new_config->crtc_mask == 0) {
  3367. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3368. crtc->base.base.id, pll->name);
  3369. goto found;
  3370. }
  3371. }
  3372. return NULL;
  3373. found:
  3374. if (pll->new_config->crtc_mask == 0)
  3375. pll->new_config->hw_state = crtc_state->dpll_hw_state;
  3376. crtc_state->shared_dpll = i;
  3377. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3378. pipe_name(crtc->pipe));
  3379. pll->new_config->crtc_mask |= 1 << crtc->pipe;
  3380. return pll;
  3381. }
  3382. /**
  3383. * intel_shared_dpll_start_config - start a new PLL staged config
  3384. * @dev_priv: DRM device
  3385. * @clear_pipes: mask of pipes that will have their PLLs freed
  3386. *
  3387. * Starts a new PLL staged config, copying the current config but
  3388. * releasing the references of pipes specified in clear_pipes.
  3389. */
  3390. static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
  3391. unsigned clear_pipes)
  3392. {
  3393. struct intel_shared_dpll *pll;
  3394. enum intel_dpll_id i;
  3395. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3396. pll = &dev_priv->shared_dplls[i];
  3397. pll->new_config = kmemdup(&pll->config, sizeof pll->config,
  3398. GFP_KERNEL);
  3399. if (!pll->new_config)
  3400. goto cleanup;
  3401. pll->new_config->crtc_mask &= ~clear_pipes;
  3402. }
  3403. return 0;
  3404. cleanup:
  3405. while (--i >= 0) {
  3406. pll = &dev_priv->shared_dplls[i];
  3407. kfree(pll->new_config);
  3408. pll->new_config = NULL;
  3409. }
  3410. return -ENOMEM;
  3411. }
  3412. static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
  3413. {
  3414. struct intel_shared_dpll *pll;
  3415. enum intel_dpll_id i;
  3416. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3417. pll = &dev_priv->shared_dplls[i];
  3418. WARN_ON(pll->new_config == &pll->config);
  3419. pll->config = *pll->new_config;
  3420. kfree(pll->new_config);
  3421. pll->new_config = NULL;
  3422. }
  3423. }
  3424. static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
  3425. {
  3426. struct intel_shared_dpll *pll;
  3427. enum intel_dpll_id i;
  3428. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3429. pll = &dev_priv->shared_dplls[i];
  3430. WARN_ON(pll->new_config == &pll->config);
  3431. kfree(pll->new_config);
  3432. pll->new_config = NULL;
  3433. }
  3434. }
  3435. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3436. {
  3437. struct drm_i915_private *dev_priv = dev->dev_private;
  3438. int dslreg = PIPEDSL(pipe);
  3439. u32 temp;
  3440. temp = I915_READ(dslreg);
  3441. udelay(500);
  3442. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3443. if (wait_for(I915_READ(dslreg) != temp, 5))
  3444. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3445. }
  3446. }
  3447. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3448. {
  3449. struct drm_device *dev = crtc->base.dev;
  3450. struct drm_i915_private *dev_priv = dev->dev_private;
  3451. int pipe = crtc->pipe;
  3452. if (crtc->config->pch_pfit.enabled) {
  3453. I915_WRITE(PS_CTL(pipe), PS_ENABLE);
  3454. I915_WRITE(PS_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3455. I915_WRITE(PS_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3456. }
  3457. }
  3458. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3459. {
  3460. struct drm_device *dev = crtc->base.dev;
  3461. struct drm_i915_private *dev_priv = dev->dev_private;
  3462. int pipe = crtc->pipe;
  3463. if (crtc->config->pch_pfit.enabled) {
  3464. /* Force use of hard-coded filter coefficients
  3465. * as some pre-programmed values are broken,
  3466. * e.g. x201.
  3467. */
  3468. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3469. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3470. PF_PIPE_SEL_IVB(pipe));
  3471. else
  3472. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3473. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3474. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3475. }
  3476. }
  3477. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3478. {
  3479. struct drm_device *dev = crtc->dev;
  3480. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3481. struct drm_plane *plane;
  3482. struct intel_plane *intel_plane;
  3483. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3484. intel_plane = to_intel_plane(plane);
  3485. if (intel_plane->pipe == pipe)
  3486. intel_plane_restore(&intel_plane->base);
  3487. }
  3488. }
  3489. static void intel_disable_sprite_planes(struct drm_crtc *crtc)
  3490. {
  3491. struct drm_device *dev = crtc->dev;
  3492. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3493. struct drm_plane *plane;
  3494. struct intel_plane *intel_plane;
  3495. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3496. intel_plane = to_intel_plane(plane);
  3497. if (intel_plane->pipe == pipe)
  3498. plane->funcs->disable_plane(plane);
  3499. }
  3500. }
  3501. void hsw_enable_ips(struct intel_crtc *crtc)
  3502. {
  3503. struct drm_device *dev = crtc->base.dev;
  3504. struct drm_i915_private *dev_priv = dev->dev_private;
  3505. if (!crtc->config->ips_enabled)
  3506. return;
  3507. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3508. intel_wait_for_vblank(dev, crtc->pipe);
  3509. assert_plane_enabled(dev_priv, crtc->plane);
  3510. if (IS_BROADWELL(dev)) {
  3511. mutex_lock(&dev_priv->rps.hw_lock);
  3512. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3513. mutex_unlock(&dev_priv->rps.hw_lock);
  3514. /* Quoting Art Runyan: "its not safe to expect any particular
  3515. * value in IPS_CTL bit 31 after enabling IPS through the
  3516. * mailbox." Moreover, the mailbox may return a bogus state,
  3517. * so we need to just enable it and continue on.
  3518. */
  3519. } else {
  3520. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3521. /* The bit only becomes 1 in the next vblank, so this wait here
  3522. * is essentially intel_wait_for_vblank. If we don't have this
  3523. * and don't wait for vblanks until the end of crtc_enable, then
  3524. * the HW state readout code will complain that the expected
  3525. * IPS_CTL value is not the one we read. */
  3526. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3527. DRM_ERROR("Timed out waiting for IPS enable\n");
  3528. }
  3529. }
  3530. void hsw_disable_ips(struct intel_crtc *crtc)
  3531. {
  3532. struct drm_device *dev = crtc->base.dev;
  3533. struct drm_i915_private *dev_priv = dev->dev_private;
  3534. if (!crtc->config->ips_enabled)
  3535. return;
  3536. assert_plane_enabled(dev_priv, crtc->plane);
  3537. if (IS_BROADWELL(dev)) {
  3538. mutex_lock(&dev_priv->rps.hw_lock);
  3539. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3540. mutex_unlock(&dev_priv->rps.hw_lock);
  3541. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3542. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3543. DRM_ERROR("Timed out waiting for IPS disable\n");
  3544. } else {
  3545. I915_WRITE(IPS_CTL, 0);
  3546. POSTING_READ(IPS_CTL);
  3547. }
  3548. /* We need to wait for a vblank before we can disable the plane. */
  3549. intel_wait_for_vblank(dev, crtc->pipe);
  3550. }
  3551. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3552. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3553. {
  3554. struct drm_device *dev = crtc->dev;
  3555. struct drm_i915_private *dev_priv = dev->dev_private;
  3556. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3557. enum pipe pipe = intel_crtc->pipe;
  3558. int palreg = PALETTE(pipe);
  3559. int i;
  3560. bool reenable_ips = false;
  3561. /* The clocks have to be on to load the palette. */
  3562. if (!crtc->enabled || !intel_crtc->active)
  3563. return;
  3564. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3565. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3566. assert_dsi_pll_enabled(dev_priv);
  3567. else
  3568. assert_pll_enabled(dev_priv, pipe);
  3569. }
  3570. /* use legacy palette for Ironlake */
  3571. if (!HAS_GMCH_DISPLAY(dev))
  3572. palreg = LGC_PALETTE(pipe);
  3573. /* Workaround : Do not read or write the pipe palette/gamma data while
  3574. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3575. */
  3576. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3577. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3578. GAMMA_MODE_MODE_SPLIT)) {
  3579. hsw_disable_ips(intel_crtc);
  3580. reenable_ips = true;
  3581. }
  3582. for (i = 0; i < 256; i++) {
  3583. I915_WRITE(palreg + 4 * i,
  3584. (intel_crtc->lut_r[i] << 16) |
  3585. (intel_crtc->lut_g[i] << 8) |
  3586. intel_crtc->lut_b[i]);
  3587. }
  3588. if (reenable_ips)
  3589. hsw_enable_ips(intel_crtc);
  3590. }
  3591. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3592. {
  3593. if (!enable && intel_crtc->overlay) {
  3594. struct drm_device *dev = intel_crtc->base.dev;
  3595. struct drm_i915_private *dev_priv = dev->dev_private;
  3596. mutex_lock(&dev->struct_mutex);
  3597. dev_priv->mm.interruptible = false;
  3598. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3599. dev_priv->mm.interruptible = true;
  3600. mutex_unlock(&dev->struct_mutex);
  3601. }
  3602. /* Let userspace switch the overlay on again. In most cases userspace
  3603. * has to recompute where to put it anyway.
  3604. */
  3605. }
  3606. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3607. {
  3608. struct drm_device *dev = crtc->dev;
  3609. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3610. int pipe = intel_crtc->pipe;
  3611. intel_enable_primary_hw_plane(crtc->primary, crtc);
  3612. intel_enable_sprite_planes(crtc);
  3613. intel_crtc_update_cursor(crtc, true);
  3614. intel_crtc_dpms_overlay(intel_crtc, true);
  3615. hsw_enable_ips(intel_crtc);
  3616. mutex_lock(&dev->struct_mutex);
  3617. intel_fbc_update(dev);
  3618. mutex_unlock(&dev->struct_mutex);
  3619. /*
  3620. * FIXME: Once we grow proper nuclear flip support out of this we need
  3621. * to compute the mask of flip planes precisely. For the time being
  3622. * consider this a flip from a NULL plane.
  3623. */
  3624. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3625. }
  3626. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3627. {
  3628. struct drm_device *dev = crtc->dev;
  3629. struct drm_i915_private *dev_priv = dev->dev_private;
  3630. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3631. int pipe = intel_crtc->pipe;
  3632. int plane = intel_crtc->plane;
  3633. intel_crtc_wait_for_pending_flips(crtc);
  3634. if (dev_priv->fbc.plane == plane)
  3635. intel_fbc_disable(dev);
  3636. hsw_disable_ips(intel_crtc);
  3637. intel_crtc_dpms_overlay(intel_crtc, false);
  3638. intel_crtc_update_cursor(crtc, false);
  3639. intel_disable_sprite_planes(crtc);
  3640. intel_disable_primary_hw_plane(crtc->primary, crtc);
  3641. /*
  3642. * FIXME: Once we grow proper nuclear flip support out of this we need
  3643. * to compute the mask of flip planes precisely. For the time being
  3644. * consider this a flip to a NULL plane.
  3645. */
  3646. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3647. }
  3648. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3649. {
  3650. struct drm_device *dev = crtc->dev;
  3651. struct drm_i915_private *dev_priv = dev->dev_private;
  3652. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3653. struct intel_encoder *encoder;
  3654. int pipe = intel_crtc->pipe;
  3655. WARN_ON(!crtc->enabled);
  3656. if (intel_crtc->active)
  3657. return;
  3658. if (intel_crtc->config->has_pch_encoder)
  3659. intel_prepare_shared_dpll(intel_crtc);
  3660. if (intel_crtc->config->has_dp_encoder)
  3661. intel_dp_set_m_n(intel_crtc);
  3662. intel_set_pipe_timings(intel_crtc);
  3663. if (intel_crtc->config->has_pch_encoder) {
  3664. intel_cpu_transcoder_set_m_n(intel_crtc,
  3665. &intel_crtc->config->fdi_m_n, NULL);
  3666. }
  3667. ironlake_set_pipeconf(crtc);
  3668. intel_crtc->active = true;
  3669. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3670. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  3671. for_each_encoder_on_crtc(dev, crtc, encoder)
  3672. if (encoder->pre_enable)
  3673. encoder->pre_enable(encoder);
  3674. if (intel_crtc->config->has_pch_encoder) {
  3675. /* Note: FDI PLL enabling _must_ be done before we enable the
  3676. * cpu pipes, hence this is separate from all the other fdi/pch
  3677. * enabling. */
  3678. ironlake_fdi_pll_enable(intel_crtc);
  3679. } else {
  3680. assert_fdi_tx_disabled(dev_priv, pipe);
  3681. assert_fdi_rx_disabled(dev_priv, pipe);
  3682. }
  3683. ironlake_pfit_enable(intel_crtc);
  3684. /*
  3685. * On ILK+ LUT must be loaded before the pipe is running but with
  3686. * clocks enabled
  3687. */
  3688. intel_crtc_load_lut(crtc);
  3689. intel_update_watermarks(crtc);
  3690. intel_enable_pipe(intel_crtc);
  3691. if (intel_crtc->config->has_pch_encoder)
  3692. ironlake_pch_enable(crtc);
  3693. assert_vblank_disabled(crtc);
  3694. drm_crtc_vblank_on(crtc);
  3695. for_each_encoder_on_crtc(dev, crtc, encoder)
  3696. encoder->enable(encoder);
  3697. if (HAS_PCH_CPT(dev))
  3698. cpt_verify_modeset(dev, intel_crtc->pipe);
  3699. intel_crtc_enable_planes(crtc);
  3700. }
  3701. /* IPS only exists on ULT machines and is tied to pipe A. */
  3702. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3703. {
  3704. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3705. }
  3706. /*
  3707. * This implements the workaround described in the "notes" section of the mode
  3708. * set sequence documentation. When going from no pipes or single pipe to
  3709. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3710. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3711. */
  3712. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3713. {
  3714. struct drm_device *dev = crtc->base.dev;
  3715. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3716. /* We want to get the other_active_crtc only if there's only 1 other
  3717. * active crtc. */
  3718. for_each_intel_crtc(dev, crtc_it) {
  3719. if (!crtc_it->active || crtc_it == crtc)
  3720. continue;
  3721. if (other_active_crtc)
  3722. return;
  3723. other_active_crtc = crtc_it;
  3724. }
  3725. if (!other_active_crtc)
  3726. return;
  3727. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3728. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3729. }
  3730. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3731. {
  3732. struct drm_device *dev = crtc->dev;
  3733. struct drm_i915_private *dev_priv = dev->dev_private;
  3734. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3735. struct intel_encoder *encoder;
  3736. int pipe = intel_crtc->pipe;
  3737. WARN_ON(!crtc->enabled);
  3738. if (intel_crtc->active)
  3739. return;
  3740. if (intel_crtc_to_shared_dpll(intel_crtc))
  3741. intel_enable_shared_dpll(intel_crtc);
  3742. if (intel_crtc->config->has_dp_encoder)
  3743. intel_dp_set_m_n(intel_crtc);
  3744. intel_set_pipe_timings(intel_crtc);
  3745. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  3746. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  3747. intel_crtc->config->pixel_multiplier - 1);
  3748. }
  3749. if (intel_crtc->config->has_pch_encoder) {
  3750. intel_cpu_transcoder_set_m_n(intel_crtc,
  3751. &intel_crtc->config->fdi_m_n, NULL);
  3752. }
  3753. haswell_set_pipeconf(crtc);
  3754. intel_set_pipe_csc(crtc);
  3755. intel_crtc->active = true;
  3756. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3757. for_each_encoder_on_crtc(dev, crtc, encoder)
  3758. if (encoder->pre_enable)
  3759. encoder->pre_enable(encoder);
  3760. if (intel_crtc->config->has_pch_encoder) {
  3761. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3762. true);
  3763. dev_priv->display.fdi_link_train(crtc);
  3764. }
  3765. intel_ddi_enable_pipe_clock(intel_crtc);
  3766. if (IS_SKYLAKE(dev))
  3767. skylake_pfit_enable(intel_crtc);
  3768. else
  3769. ironlake_pfit_enable(intel_crtc);
  3770. /*
  3771. * On ILK+ LUT must be loaded before the pipe is running but with
  3772. * clocks enabled
  3773. */
  3774. intel_crtc_load_lut(crtc);
  3775. intel_ddi_set_pipe_settings(crtc);
  3776. intel_ddi_enable_transcoder_func(crtc);
  3777. intel_update_watermarks(crtc);
  3778. intel_enable_pipe(intel_crtc);
  3779. if (intel_crtc->config->has_pch_encoder)
  3780. lpt_pch_enable(crtc);
  3781. if (intel_crtc->config->dp_encoder_is_mst)
  3782. intel_ddi_set_vc_payload_alloc(crtc, true);
  3783. assert_vblank_disabled(crtc);
  3784. drm_crtc_vblank_on(crtc);
  3785. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3786. encoder->enable(encoder);
  3787. intel_opregion_notify_encoder(encoder, true);
  3788. }
  3789. /* If we change the relative order between pipe/planes enabling, we need
  3790. * to change the workaround. */
  3791. haswell_mode_set_planes_workaround(intel_crtc);
  3792. intel_crtc_enable_planes(crtc);
  3793. }
  3794. static void skylake_pfit_disable(struct intel_crtc *crtc)
  3795. {
  3796. struct drm_device *dev = crtc->base.dev;
  3797. struct drm_i915_private *dev_priv = dev->dev_private;
  3798. int pipe = crtc->pipe;
  3799. /* To avoid upsetting the power well on haswell only disable the pfit if
  3800. * it's in use. The hw state code will make sure we get this right. */
  3801. if (crtc->config->pch_pfit.enabled) {
  3802. I915_WRITE(PS_CTL(pipe), 0);
  3803. I915_WRITE(PS_WIN_POS(pipe), 0);
  3804. I915_WRITE(PS_WIN_SZ(pipe), 0);
  3805. }
  3806. }
  3807. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3808. {
  3809. struct drm_device *dev = crtc->base.dev;
  3810. struct drm_i915_private *dev_priv = dev->dev_private;
  3811. int pipe = crtc->pipe;
  3812. /* To avoid upsetting the power well on haswell only disable the pfit if
  3813. * it's in use. The hw state code will make sure we get this right. */
  3814. if (crtc->config->pch_pfit.enabled) {
  3815. I915_WRITE(PF_CTL(pipe), 0);
  3816. I915_WRITE(PF_WIN_POS(pipe), 0);
  3817. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3818. }
  3819. }
  3820. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3821. {
  3822. struct drm_device *dev = crtc->dev;
  3823. struct drm_i915_private *dev_priv = dev->dev_private;
  3824. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3825. struct intel_encoder *encoder;
  3826. int pipe = intel_crtc->pipe;
  3827. u32 reg, temp;
  3828. if (!intel_crtc->active)
  3829. return;
  3830. intel_crtc_disable_planes(crtc);
  3831. for_each_encoder_on_crtc(dev, crtc, encoder)
  3832. encoder->disable(encoder);
  3833. drm_crtc_vblank_off(crtc);
  3834. assert_vblank_disabled(crtc);
  3835. if (intel_crtc->config->has_pch_encoder)
  3836. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  3837. intel_disable_pipe(intel_crtc);
  3838. ironlake_pfit_disable(intel_crtc);
  3839. for_each_encoder_on_crtc(dev, crtc, encoder)
  3840. if (encoder->post_disable)
  3841. encoder->post_disable(encoder);
  3842. if (intel_crtc->config->has_pch_encoder) {
  3843. ironlake_fdi_disable(crtc);
  3844. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3845. if (HAS_PCH_CPT(dev)) {
  3846. /* disable TRANS_DP_CTL */
  3847. reg = TRANS_DP_CTL(pipe);
  3848. temp = I915_READ(reg);
  3849. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3850. TRANS_DP_PORT_SEL_MASK);
  3851. temp |= TRANS_DP_PORT_SEL_NONE;
  3852. I915_WRITE(reg, temp);
  3853. /* disable DPLL_SEL */
  3854. temp = I915_READ(PCH_DPLL_SEL);
  3855. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3856. I915_WRITE(PCH_DPLL_SEL, temp);
  3857. }
  3858. /* disable PCH DPLL */
  3859. intel_disable_shared_dpll(intel_crtc);
  3860. ironlake_fdi_pll_disable(intel_crtc);
  3861. }
  3862. intel_crtc->active = false;
  3863. intel_update_watermarks(crtc);
  3864. mutex_lock(&dev->struct_mutex);
  3865. intel_fbc_update(dev);
  3866. mutex_unlock(&dev->struct_mutex);
  3867. }
  3868. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3869. {
  3870. struct drm_device *dev = crtc->dev;
  3871. struct drm_i915_private *dev_priv = dev->dev_private;
  3872. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3873. struct intel_encoder *encoder;
  3874. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3875. if (!intel_crtc->active)
  3876. return;
  3877. intel_crtc_disable_planes(crtc);
  3878. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3879. intel_opregion_notify_encoder(encoder, false);
  3880. encoder->disable(encoder);
  3881. }
  3882. drm_crtc_vblank_off(crtc);
  3883. assert_vblank_disabled(crtc);
  3884. if (intel_crtc->config->has_pch_encoder)
  3885. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  3886. false);
  3887. intel_disable_pipe(intel_crtc);
  3888. if (intel_crtc->config->dp_encoder_is_mst)
  3889. intel_ddi_set_vc_payload_alloc(crtc, false);
  3890. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3891. if (IS_SKYLAKE(dev))
  3892. skylake_pfit_disable(intel_crtc);
  3893. else
  3894. ironlake_pfit_disable(intel_crtc);
  3895. intel_ddi_disable_pipe_clock(intel_crtc);
  3896. if (intel_crtc->config->has_pch_encoder) {
  3897. lpt_disable_pch_transcoder(dev_priv);
  3898. intel_ddi_fdi_disable(crtc);
  3899. }
  3900. for_each_encoder_on_crtc(dev, crtc, encoder)
  3901. if (encoder->post_disable)
  3902. encoder->post_disable(encoder);
  3903. intel_crtc->active = false;
  3904. intel_update_watermarks(crtc);
  3905. mutex_lock(&dev->struct_mutex);
  3906. intel_fbc_update(dev);
  3907. mutex_unlock(&dev->struct_mutex);
  3908. if (intel_crtc_to_shared_dpll(intel_crtc))
  3909. intel_disable_shared_dpll(intel_crtc);
  3910. }
  3911. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3912. {
  3913. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3914. intel_put_shared_dpll(intel_crtc);
  3915. }
  3916. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3917. {
  3918. struct drm_device *dev = crtc->base.dev;
  3919. struct drm_i915_private *dev_priv = dev->dev_private;
  3920. struct intel_crtc_state *pipe_config = crtc->config;
  3921. if (!pipe_config->gmch_pfit.control)
  3922. return;
  3923. /*
  3924. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3925. * according to register description and PRM.
  3926. */
  3927. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3928. assert_pipe_disabled(dev_priv, crtc->pipe);
  3929. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3930. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3931. /* Border color in case we don't scale up to the full screen. Black by
  3932. * default, change to something else for debugging. */
  3933. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3934. }
  3935. static enum intel_display_power_domain port_to_power_domain(enum port port)
  3936. {
  3937. switch (port) {
  3938. case PORT_A:
  3939. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3940. case PORT_B:
  3941. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3942. case PORT_C:
  3943. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3944. case PORT_D:
  3945. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3946. default:
  3947. WARN_ON_ONCE(1);
  3948. return POWER_DOMAIN_PORT_OTHER;
  3949. }
  3950. }
  3951. #define for_each_power_domain(domain, mask) \
  3952. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3953. if ((1 << (domain)) & (mask))
  3954. enum intel_display_power_domain
  3955. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3956. {
  3957. struct drm_device *dev = intel_encoder->base.dev;
  3958. struct intel_digital_port *intel_dig_port;
  3959. switch (intel_encoder->type) {
  3960. case INTEL_OUTPUT_UNKNOWN:
  3961. /* Only DDI platforms should ever use this output type */
  3962. WARN_ON_ONCE(!HAS_DDI(dev));
  3963. case INTEL_OUTPUT_DISPLAYPORT:
  3964. case INTEL_OUTPUT_HDMI:
  3965. case INTEL_OUTPUT_EDP:
  3966. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3967. return port_to_power_domain(intel_dig_port->port);
  3968. case INTEL_OUTPUT_DP_MST:
  3969. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  3970. return port_to_power_domain(intel_dig_port->port);
  3971. case INTEL_OUTPUT_ANALOG:
  3972. return POWER_DOMAIN_PORT_CRT;
  3973. case INTEL_OUTPUT_DSI:
  3974. return POWER_DOMAIN_PORT_DSI;
  3975. default:
  3976. return POWER_DOMAIN_PORT_OTHER;
  3977. }
  3978. }
  3979. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3980. {
  3981. struct drm_device *dev = crtc->dev;
  3982. struct intel_encoder *intel_encoder;
  3983. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3984. enum pipe pipe = intel_crtc->pipe;
  3985. unsigned long mask;
  3986. enum transcoder transcoder;
  3987. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3988. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3989. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3990. if (intel_crtc->config->pch_pfit.enabled ||
  3991. intel_crtc->config->pch_pfit.force_thru)
  3992. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3993. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3994. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3995. return mask;
  3996. }
  3997. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3998. {
  3999. struct drm_i915_private *dev_priv = dev->dev_private;
  4000. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4001. struct intel_crtc *crtc;
  4002. /*
  4003. * First get all needed power domains, then put all unneeded, to avoid
  4004. * any unnecessary toggling of the power wells.
  4005. */
  4006. for_each_intel_crtc(dev, crtc) {
  4007. enum intel_display_power_domain domain;
  4008. if (!crtc->base.enabled)
  4009. continue;
  4010. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4011. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4012. intel_display_power_get(dev_priv, domain);
  4013. }
  4014. if (dev_priv->display.modeset_global_resources)
  4015. dev_priv->display.modeset_global_resources(dev);
  4016. for_each_intel_crtc(dev, crtc) {
  4017. enum intel_display_power_domain domain;
  4018. for_each_power_domain(domain, crtc->enabled_power_domains)
  4019. intel_display_power_put(dev_priv, domain);
  4020. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4021. }
  4022. intel_display_set_init_power(dev_priv, false);
  4023. }
  4024. /* returns HPLL frequency in kHz */
  4025. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4026. {
  4027. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4028. /* Obtain SKU information */
  4029. mutex_lock(&dev_priv->dpio_lock);
  4030. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4031. CCK_FUSE_HPLL_FREQ_MASK;
  4032. mutex_unlock(&dev_priv->dpio_lock);
  4033. return vco_freq[hpll_freq] * 1000;
  4034. }
  4035. static void vlv_update_cdclk(struct drm_device *dev)
  4036. {
  4037. struct drm_i915_private *dev_priv = dev->dev_private;
  4038. dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4039. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4040. dev_priv->vlv_cdclk_freq);
  4041. /*
  4042. * Program the gmbus_freq based on the cdclk frequency.
  4043. * BSpec erroneously claims we should aim for 4MHz, but
  4044. * in fact 1MHz is the correct frequency.
  4045. */
  4046. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->vlv_cdclk_freq, 1000));
  4047. }
  4048. /* Adjust CDclk dividers to allow high res or save power if possible */
  4049. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4050. {
  4051. struct drm_i915_private *dev_priv = dev->dev_private;
  4052. u32 val, cmd;
  4053. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4054. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4055. cmd = 2;
  4056. else if (cdclk == 266667)
  4057. cmd = 1;
  4058. else
  4059. cmd = 0;
  4060. mutex_lock(&dev_priv->rps.hw_lock);
  4061. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4062. val &= ~DSPFREQGUAR_MASK;
  4063. val |= (cmd << DSPFREQGUAR_SHIFT);
  4064. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4065. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4066. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4067. 50)) {
  4068. DRM_ERROR("timed out waiting for CDclk change\n");
  4069. }
  4070. mutex_unlock(&dev_priv->rps.hw_lock);
  4071. if (cdclk == 400000) {
  4072. u32 divider;
  4073. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4074. mutex_lock(&dev_priv->dpio_lock);
  4075. /* adjust cdclk divider */
  4076. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4077. val &= ~DISPLAY_FREQUENCY_VALUES;
  4078. val |= divider;
  4079. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4080. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4081. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4082. 50))
  4083. DRM_ERROR("timed out waiting for CDclk change\n");
  4084. mutex_unlock(&dev_priv->dpio_lock);
  4085. }
  4086. mutex_lock(&dev_priv->dpio_lock);
  4087. /* adjust self-refresh exit latency value */
  4088. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4089. val &= ~0x7f;
  4090. /*
  4091. * For high bandwidth configs, we set a higher latency in the bunit
  4092. * so that the core display fetch happens in time to avoid underruns.
  4093. */
  4094. if (cdclk == 400000)
  4095. val |= 4500 / 250; /* 4.5 usec */
  4096. else
  4097. val |= 3000 / 250; /* 3.0 usec */
  4098. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4099. mutex_unlock(&dev_priv->dpio_lock);
  4100. vlv_update_cdclk(dev);
  4101. }
  4102. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4103. {
  4104. struct drm_i915_private *dev_priv = dev->dev_private;
  4105. u32 val, cmd;
  4106. WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
  4107. switch (cdclk) {
  4108. case 400000:
  4109. cmd = 3;
  4110. break;
  4111. case 333333:
  4112. case 320000:
  4113. cmd = 2;
  4114. break;
  4115. case 266667:
  4116. cmd = 1;
  4117. break;
  4118. case 200000:
  4119. cmd = 0;
  4120. break;
  4121. default:
  4122. MISSING_CASE(cdclk);
  4123. return;
  4124. }
  4125. mutex_lock(&dev_priv->rps.hw_lock);
  4126. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4127. val &= ~DSPFREQGUAR_MASK_CHV;
  4128. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4129. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4130. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4131. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4132. 50)) {
  4133. DRM_ERROR("timed out waiting for CDclk change\n");
  4134. }
  4135. mutex_unlock(&dev_priv->rps.hw_lock);
  4136. vlv_update_cdclk(dev);
  4137. }
  4138. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4139. int max_pixclk)
  4140. {
  4141. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4142. /* FIXME: Punit isn't quite ready yet */
  4143. if (IS_CHERRYVIEW(dev_priv->dev))
  4144. return 400000;
  4145. /*
  4146. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4147. * 200MHz
  4148. * 267MHz
  4149. * 320/333MHz (depends on HPLL freq)
  4150. * 400MHz
  4151. * So we check to see whether we're above 90% of the lower bin and
  4152. * adjust if needed.
  4153. *
  4154. * We seem to get an unstable or solid color picture at 200MHz.
  4155. * Not sure what's wrong. For now use 200MHz only when all pipes
  4156. * are off.
  4157. */
  4158. if (max_pixclk > freq_320*9/10)
  4159. return 400000;
  4160. else if (max_pixclk > 266667*9/10)
  4161. return freq_320;
  4162. else if (max_pixclk > 0)
  4163. return 266667;
  4164. else
  4165. return 200000;
  4166. }
  4167. /* compute the max pixel clock for new configuration */
  4168. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  4169. {
  4170. struct drm_device *dev = dev_priv->dev;
  4171. struct intel_crtc *intel_crtc;
  4172. int max_pixclk = 0;
  4173. for_each_intel_crtc(dev, intel_crtc) {
  4174. if (intel_crtc->new_enabled)
  4175. max_pixclk = max(max_pixclk,
  4176. intel_crtc->new_config->base.adjusted_mode.crtc_clock);
  4177. }
  4178. return max_pixclk;
  4179. }
  4180. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  4181. unsigned *prepare_pipes)
  4182. {
  4183. struct drm_i915_private *dev_priv = dev->dev_private;
  4184. struct intel_crtc *intel_crtc;
  4185. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4186. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  4187. dev_priv->vlv_cdclk_freq)
  4188. return;
  4189. /* disable/enable all currently active pipes while we change cdclk */
  4190. for_each_intel_crtc(dev, intel_crtc)
  4191. if (intel_crtc->base.enabled)
  4192. *prepare_pipes |= (1 << intel_crtc->pipe);
  4193. }
  4194. static void valleyview_modeset_global_resources(struct drm_device *dev)
  4195. {
  4196. struct drm_i915_private *dev_priv = dev->dev_private;
  4197. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  4198. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  4199. if (req_cdclk != dev_priv->vlv_cdclk_freq) {
  4200. /*
  4201. * FIXME: We can end up here with all power domains off, yet
  4202. * with a CDCLK frequency other than the minimum. To account
  4203. * for this take the PIPE-A power domain, which covers the HW
  4204. * blocks needed for the following programming. This can be
  4205. * removed once it's guaranteed that we get here either with
  4206. * the minimum CDCLK set, or the required power domains
  4207. * enabled.
  4208. */
  4209. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  4210. if (IS_CHERRYVIEW(dev))
  4211. cherryview_set_cdclk(dev, req_cdclk);
  4212. else
  4213. valleyview_set_cdclk(dev, req_cdclk);
  4214. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  4215. }
  4216. }
  4217. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  4218. {
  4219. struct drm_device *dev = crtc->dev;
  4220. struct drm_i915_private *dev_priv = to_i915(dev);
  4221. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4222. struct intel_encoder *encoder;
  4223. int pipe = intel_crtc->pipe;
  4224. bool is_dsi;
  4225. WARN_ON(!crtc->enabled);
  4226. if (intel_crtc->active)
  4227. return;
  4228. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4229. if (!is_dsi) {
  4230. if (IS_CHERRYVIEW(dev))
  4231. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4232. else
  4233. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4234. }
  4235. if (intel_crtc->config->has_dp_encoder)
  4236. intel_dp_set_m_n(intel_crtc);
  4237. intel_set_pipe_timings(intel_crtc);
  4238. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  4239. struct drm_i915_private *dev_priv = dev->dev_private;
  4240. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4241. I915_WRITE(CHV_CANVAS(pipe), 0);
  4242. }
  4243. i9xx_set_pipeconf(intel_crtc);
  4244. intel_crtc->active = true;
  4245. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4246. for_each_encoder_on_crtc(dev, crtc, encoder)
  4247. if (encoder->pre_pll_enable)
  4248. encoder->pre_pll_enable(encoder);
  4249. if (!is_dsi) {
  4250. if (IS_CHERRYVIEW(dev))
  4251. chv_enable_pll(intel_crtc, intel_crtc->config);
  4252. else
  4253. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4254. }
  4255. for_each_encoder_on_crtc(dev, crtc, encoder)
  4256. if (encoder->pre_enable)
  4257. encoder->pre_enable(encoder);
  4258. i9xx_pfit_enable(intel_crtc);
  4259. intel_crtc_load_lut(crtc);
  4260. intel_update_watermarks(crtc);
  4261. intel_enable_pipe(intel_crtc);
  4262. assert_vblank_disabled(crtc);
  4263. drm_crtc_vblank_on(crtc);
  4264. for_each_encoder_on_crtc(dev, crtc, encoder)
  4265. encoder->enable(encoder);
  4266. intel_crtc_enable_planes(crtc);
  4267. /* Underruns don't raise interrupts, so check manually. */
  4268. i9xx_check_fifo_underruns(dev_priv);
  4269. }
  4270. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4271. {
  4272. struct drm_device *dev = crtc->base.dev;
  4273. struct drm_i915_private *dev_priv = dev->dev_private;
  4274. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4275. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4276. }
  4277. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  4278. {
  4279. struct drm_device *dev = crtc->dev;
  4280. struct drm_i915_private *dev_priv = to_i915(dev);
  4281. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4282. struct intel_encoder *encoder;
  4283. int pipe = intel_crtc->pipe;
  4284. WARN_ON(!crtc->enabled);
  4285. if (intel_crtc->active)
  4286. return;
  4287. i9xx_set_pll_dividers(intel_crtc);
  4288. if (intel_crtc->config->has_dp_encoder)
  4289. intel_dp_set_m_n(intel_crtc);
  4290. intel_set_pipe_timings(intel_crtc);
  4291. i9xx_set_pipeconf(intel_crtc);
  4292. intel_crtc->active = true;
  4293. if (!IS_GEN2(dev))
  4294. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4295. for_each_encoder_on_crtc(dev, crtc, encoder)
  4296. if (encoder->pre_enable)
  4297. encoder->pre_enable(encoder);
  4298. i9xx_enable_pll(intel_crtc);
  4299. i9xx_pfit_enable(intel_crtc);
  4300. intel_crtc_load_lut(crtc);
  4301. intel_update_watermarks(crtc);
  4302. intel_enable_pipe(intel_crtc);
  4303. assert_vblank_disabled(crtc);
  4304. drm_crtc_vblank_on(crtc);
  4305. for_each_encoder_on_crtc(dev, crtc, encoder)
  4306. encoder->enable(encoder);
  4307. intel_crtc_enable_planes(crtc);
  4308. /*
  4309. * Gen2 reports pipe underruns whenever all planes are disabled.
  4310. * So don't enable underrun reporting before at least some planes
  4311. * are enabled.
  4312. * FIXME: Need to fix the logic to work when we turn off all planes
  4313. * but leave the pipe running.
  4314. */
  4315. if (IS_GEN2(dev))
  4316. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4317. /* Underruns don't raise interrupts, so check manually. */
  4318. i9xx_check_fifo_underruns(dev_priv);
  4319. }
  4320. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4321. {
  4322. struct drm_device *dev = crtc->base.dev;
  4323. struct drm_i915_private *dev_priv = dev->dev_private;
  4324. if (!crtc->config->gmch_pfit.control)
  4325. return;
  4326. assert_pipe_disabled(dev_priv, crtc->pipe);
  4327. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4328. I915_READ(PFIT_CONTROL));
  4329. I915_WRITE(PFIT_CONTROL, 0);
  4330. }
  4331. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  4332. {
  4333. struct drm_device *dev = crtc->dev;
  4334. struct drm_i915_private *dev_priv = dev->dev_private;
  4335. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4336. struct intel_encoder *encoder;
  4337. int pipe = intel_crtc->pipe;
  4338. if (!intel_crtc->active)
  4339. return;
  4340. /*
  4341. * Gen2 reports pipe underruns whenever all planes are disabled.
  4342. * So diasble underrun reporting before all the planes get disabled.
  4343. * FIXME: Need to fix the logic to work when we turn off all planes
  4344. * but leave the pipe running.
  4345. */
  4346. if (IS_GEN2(dev))
  4347. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4348. /*
  4349. * Vblank time updates from the shadow to live plane control register
  4350. * are blocked if the memory self-refresh mode is active at that
  4351. * moment. So to make sure the plane gets truly disabled, disable
  4352. * first the self-refresh mode. The self-refresh enable bit in turn
  4353. * will be checked/applied by the HW only at the next frame start
  4354. * event which is after the vblank start event, so we need to have a
  4355. * wait-for-vblank between disabling the plane and the pipe.
  4356. */
  4357. intel_set_memory_cxsr(dev_priv, false);
  4358. intel_crtc_disable_planes(crtc);
  4359. /*
  4360. * On gen2 planes are double buffered but the pipe isn't, so we must
  4361. * wait for planes to fully turn off before disabling the pipe.
  4362. * We also need to wait on all gmch platforms because of the
  4363. * self-refresh mode constraint explained above.
  4364. */
  4365. intel_wait_for_vblank(dev, pipe);
  4366. for_each_encoder_on_crtc(dev, crtc, encoder)
  4367. encoder->disable(encoder);
  4368. drm_crtc_vblank_off(crtc);
  4369. assert_vblank_disabled(crtc);
  4370. intel_disable_pipe(intel_crtc);
  4371. i9xx_pfit_disable(intel_crtc);
  4372. for_each_encoder_on_crtc(dev, crtc, encoder)
  4373. if (encoder->post_disable)
  4374. encoder->post_disable(encoder);
  4375. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  4376. if (IS_CHERRYVIEW(dev))
  4377. chv_disable_pll(dev_priv, pipe);
  4378. else if (IS_VALLEYVIEW(dev))
  4379. vlv_disable_pll(dev_priv, pipe);
  4380. else
  4381. i9xx_disable_pll(intel_crtc);
  4382. }
  4383. if (!IS_GEN2(dev))
  4384. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4385. intel_crtc->active = false;
  4386. intel_update_watermarks(crtc);
  4387. mutex_lock(&dev->struct_mutex);
  4388. intel_fbc_update(dev);
  4389. mutex_unlock(&dev->struct_mutex);
  4390. }
  4391. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4392. {
  4393. }
  4394. /* Master function to enable/disable CRTC and corresponding power wells */
  4395. void intel_crtc_control(struct drm_crtc *crtc, bool enable)
  4396. {
  4397. struct drm_device *dev = crtc->dev;
  4398. struct drm_i915_private *dev_priv = dev->dev_private;
  4399. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4400. enum intel_display_power_domain domain;
  4401. unsigned long domains;
  4402. if (enable) {
  4403. if (!intel_crtc->active) {
  4404. domains = get_crtc_power_domains(crtc);
  4405. for_each_power_domain(domain, domains)
  4406. intel_display_power_get(dev_priv, domain);
  4407. intel_crtc->enabled_power_domains = domains;
  4408. dev_priv->display.crtc_enable(crtc);
  4409. }
  4410. } else {
  4411. if (intel_crtc->active) {
  4412. dev_priv->display.crtc_disable(crtc);
  4413. domains = intel_crtc->enabled_power_domains;
  4414. for_each_power_domain(domain, domains)
  4415. intel_display_power_put(dev_priv, domain);
  4416. intel_crtc->enabled_power_domains = 0;
  4417. }
  4418. }
  4419. }
  4420. /**
  4421. * Sets the power management mode of the pipe and plane.
  4422. */
  4423. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4424. {
  4425. struct drm_device *dev = crtc->dev;
  4426. struct intel_encoder *intel_encoder;
  4427. bool enable = false;
  4428. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4429. enable |= intel_encoder->connectors_active;
  4430. intel_crtc_control(crtc, enable);
  4431. }
  4432. static void intel_crtc_disable(struct drm_crtc *crtc)
  4433. {
  4434. struct drm_device *dev = crtc->dev;
  4435. struct drm_connector *connector;
  4436. struct drm_i915_private *dev_priv = dev->dev_private;
  4437. /* crtc should still be enabled when we disable it. */
  4438. WARN_ON(!crtc->enabled);
  4439. dev_priv->display.crtc_disable(crtc);
  4440. dev_priv->display.off(crtc);
  4441. crtc->primary->funcs->disable_plane(crtc->primary);
  4442. /* Update computed state. */
  4443. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4444. if (!connector->encoder || !connector->encoder->crtc)
  4445. continue;
  4446. if (connector->encoder->crtc != crtc)
  4447. continue;
  4448. connector->dpms = DRM_MODE_DPMS_OFF;
  4449. to_intel_encoder(connector->encoder)->connectors_active = false;
  4450. }
  4451. }
  4452. void intel_encoder_destroy(struct drm_encoder *encoder)
  4453. {
  4454. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4455. drm_encoder_cleanup(encoder);
  4456. kfree(intel_encoder);
  4457. }
  4458. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4459. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4460. * state of the entire output pipe. */
  4461. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4462. {
  4463. if (mode == DRM_MODE_DPMS_ON) {
  4464. encoder->connectors_active = true;
  4465. intel_crtc_update_dpms(encoder->base.crtc);
  4466. } else {
  4467. encoder->connectors_active = false;
  4468. intel_crtc_update_dpms(encoder->base.crtc);
  4469. }
  4470. }
  4471. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4472. * internal consistency). */
  4473. static void intel_connector_check_state(struct intel_connector *connector)
  4474. {
  4475. if (connector->get_hw_state(connector)) {
  4476. struct intel_encoder *encoder = connector->encoder;
  4477. struct drm_crtc *crtc;
  4478. bool encoder_enabled;
  4479. enum pipe pipe;
  4480. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4481. connector->base.base.id,
  4482. connector->base.name);
  4483. /* there is no real hw state for MST connectors */
  4484. if (connector->mst_port)
  4485. return;
  4486. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4487. "wrong connector dpms state\n");
  4488. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  4489. "active connector not linked to encoder\n");
  4490. if (encoder) {
  4491. I915_STATE_WARN(!encoder->connectors_active,
  4492. "encoder->connectors_active not set\n");
  4493. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4494. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  4495. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  4496. return;
  4497. crtc = encoder->base.crtc;
  4498. I915_STATE_WARN(!crtc->enabled, "crtc not enabled\n");
  4499. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4500. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  4501. "encoder active on the wrong pipe\n");
  4502. }
  4503. }
  4504. }
  4505. /* Even simpler default implementation, if there's really no special case to
  4506. * consider. */
  4507. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4508. {
  4509. /* All the simple cases only support two dpms states. */
  4510. if (mode != DRM_MODE_DPMS_ON)
  4511. mode = DRM_MODE_DPMS_OFF;
  4512. if (mode == connector->dpms)
  4513. return;
  4514. connector->dpms = mode;
  4515. /* Only need to change hw state when actually enabled */
  4516. if (connector->encoder)
  4517. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4518. intel_modeset_check_state(connector->dev);
  4519. }
  4520. /* Simple connector->get_hw_state implementation for encoders that support only
  4521. * one connector and no cloning and hence the encoder state determines the state
  4522. * of the connector. */
  4523. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4524. {
  4525. enum pipe pipe = 0;
  4526. struct intel_encoder *encoder = connector->encoder;
  4527. return encoder->get_hw_state(encoder, &pipe);
  4528. }
  4529. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4530. struct intel_crtc_state *pipe_config)
  4531. {
  4532. struct drm_i915_private *dev_priv = dev->dev_private;
  4533. struct intel_crtc *pipe_B_crtc =
  4534. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4535. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4536. pipe_name(pipe), pipe_config->fdi_lanes);
  4537. if (pipe_config->fdi_lanes > 4) {
  4538. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4539. pipe_name(pipe), pipe_config->fdi_lanes);
  4540. return false;
  4541. }
  4542. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4543. if (pipe_config->fdi_lanes > 2) {
  4544. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4545. pipe_config->fdi_lanes);
  4546. return false;
  4547. } else {
  4548. return true;
  4549. }
  4550. }
  4551. if (INTEL_INFO(dev)->num_pipes == 2)
  4552. return true;
  4553. /* Ivybridge 3 pipe is really complicated */
  4554. switch (pipe) {
  4555. case PIPE_A:
  4556. return true;
  4557. case PIPE_B:
  4558. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4559. pipe_config->fdi_lanes > 2) {
  4560. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4561. pipe_name(pipe), pipe_config->fdi_lanes);
  4562. return false;
  4563. }
  4564. return true;
  4565. case PIPE_C:
  4566. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4567. pipe_B_crtc->config->fdi_lanes <= 2) {
  4568. if (pipe_config->fdi_lanes > 2) {
  4569. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4570. pipe_name(pipe), pipe_config->fdi_lanes);
  4571. return false;
  4572. }
  4573. } else {
  4574. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4575. return false;
  4576. }
  4577. return true;
  4578. default:
  4579. BUG();
  4580. }
  4581. }
  4582. #define RETRY 1
  4583. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4584. struct intel_crtc_state *pipe_config)
  4585. {
  4586. struct drm_device *dev = intel_crtc->base.dev;
  4587. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4588. int lane, link_bw, fdi_dotclock;
  4589. bool setup_ok, needs_recompute = false;
  4590. retry:
  4591. /* FDI is a binary signal running at ~2.7GHz, encoding
  4592. * each output octet as 10 bits. The actual frequency
  4593. * is stored as a divider into a 100MHz clock, and the
  4594. * mode pixel clock is stored in units of 1KHz.
  4595. * Hence the bw of each lane in terms of the mode signal
  4596. * is:
  4597. */
  4598. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4599. fdi_dotclock = adjusted_mode->crtc_clock;
  4600. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4601. pipe_config->pipe_bpp);
  4602. pipe_config->fdi_lanes = lane;
  4603. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4604. link_bw, &pipe_config->fdi_m_n);
  4605. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4606. intel_crtc->pipe, pipe_config);
  4607. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4608. pipe_config->pipe_bpp -= 2*3;
  4609. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4610. pipe_config->pipe_bpp);
  4611. needs_recompute = true;
  4612. pipe_config->bw_constrained = true;
  4613. goto retry;
  4614. }
  4615. if (needs_recompute)
  4616. return RETRY;
  4617. return setup_ok ? 0 : -EINVAL;
  4618. }
  4619. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4620. struct intel_crtc_state *pipe_config)
  4621. {
  4622. pipe_config->ips_enabled = i915.enable_ips &&
  4623. hsw_crtc_supports_ips(crtc) &&
  4624. pipe_config->pipe_bpp <= 24;
  4625. }
  4626. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4627. struct intel_crtc_state *pipe_config)
  4628. {
  4629. struct drm_device *dev = crtc->base.dev;
  4630. struct drm_i915_private *dev_priv = dev->dev_private;
  4631. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  4632. /* FIXME should check pixel clock limits on all platforms */
  4633. if (INTEL_INFO(dev)->gen < 4) {
  4634. int clock_limit =
  4635. dev_priv->display.get_display_clock_speed(dev);
  4636. /*
  4637. * Enable pixel doubling when the dot clock
  4638. * is > 90% of the (display) core speed.
  4639. *
  4640. * GDG double wide on either pipe,
  4641. * otherwise pipe A only.
  4642. */
  4643. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4644. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4645. clock_limit *= 2;
  4646. pipe_config->double_wide = true;
  4647. }
  4648. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4649. return -EINVAL;
  4650. }
  4651. /*
  4652. * Pipe horizontal size must be even in:
  4653. * - DVO ganged mode
  4654. * - LVDS dual channel mode
  4655. * - Double wide pipe
  4656. */
  4657. if ((intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4658. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4659. pipe_config->pipe_src_w &= ~1;
  4660. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4661. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4662. */
  4663. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4664. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4665. return -EINVAL;
  4666. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4667. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4668. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4669. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4670. * for lvds. */
  4671. pipe_config->pipe_bpp = 8*3;
  4672. }
  4673. if (HAS_IPS(dev))
  4674. hsw_compute_ips_config(crtc, pipe_config);
  4675. if (pipe_config->has_pch_encoder)
  4676. return ironlake_fdi_compute_config(crtc, pipe_config);
  4677. return 0;
  4678. }
  4679. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4680. {
  4681. struct drm_i915_private *dev_priv = dev->dev_private;
  4682. u32 val;
  4683. int divider;
  4684. /* FIXME: Punit isn't quite ready yet */
  4685. if (IS_CHERRYVIEW(dev))
  4686. return 400000;
  4687. if (dev_priv->hpll_freq == 0)
  4688. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  4689. mutex_lock(&dev_priv->dpio_lock);
  4690. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4691. mutex_unlock(&dev_priv->dpio_lock);
  4692. divider = val & DISPLAY_FREQUENCY_VALUES;
  4693. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  4694. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4695. "cdclk change in progress\n");
  4696. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  4697. }
  4698. static int i945_get_display_clock_speed(struct drm_device *dev)
  4699. {
  4700. return 400000;
  4701. }
  4702. static int i915_get_display_clock_speed(struct drm_device *dev)
  4703. {
  4704. return 333000;
  4705. }
  4706. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4707. {
  4708. return 200000;
  4709. }
  4710. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4711. {
  4712. u16 gcfgc = 0;
  4713. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4714. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4715. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4716. return 267000;
  4717. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4718. return 333000;
  4719. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4720. return 444000;
  4721. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4722. return 200000;
  4723. default:
  4724. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4725. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4726. return 133000;
  4727. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4728. return 167000;
  4729. }
  4730. }
  4731. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4732. {
  4733. u16 gcfgc = 0;
  4734. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4735. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4736. return 133000;
  4737. else {
  4738. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4739. case GC_DISPLAY_CLOCK_333_MHZ:
  4740. return 333000;
  4741. default:
  4742. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4743. return 190000;
  4744. }
  4745. }
  4746. }
  4747. static int i865_get_display_clock_speed(struct drm_device *dev)
  4748. {
  4749. return 266000;
  4750. }
  4751. static int i855_get_display_clock_speed(struct drm_device *dev)
  4752. {
  4753. u16 hpllcc = 0;
  4754. /* Assume that the hardware is in the high speed state. This
  4755. * should be the default.
  4756. */
  4757. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4758. case GC_CLOCK_133_200:
  4759. case GC_CLOCK_100_200:
  4760. return 200000;
  4761. case GC_CLOCK_166_250:
  4762. return 250000;
  4763. case GC_CLOCK_100_133:
  4764. return 133000;
  4765. }
  4766. /* Shouldn't happen */
  4767. return 0;
  4768. }
  4769. static int i830_get_display_clock_speed(struct drm_device *dev)
  4770. {
  4771. return 133000;
  4772. }
  4773. static void
  4774. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4775. {
  4776. while (*num > DATA_LINK_M_N_MASK ||
  4777. *den > DATA_LINK_M_N_MASK) {
  4778. *num >>= 1;
  4779. *den >>= 1;
  4780. }
  4781. }
  4782. static void compute_m_n(unsigned int m, unsigned int n,
  4783. uint32_t *ret_m, uint32_t *ret_n)
  4784. {
  4785. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4786. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4787. intel_reduce_m_n_ratio(ret_m, ret_n);
  4788. }
  4789. void
  4790. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4791. int pixel_clock, int link_clock,
  4792. struct intel_link_m_n *m_n)
  4793. {
  4794. m_n->tu = 64;
  4795. compute_m_n(bits_per_pixel * pixel_clock,
  4796. link_clock * nlanes * 8,
  4797. &m_n->gmch_m, &m_n->gmch_n);
  4798. compute_m_n(pixel_clock, link_clock,
  4799. &m_n->link_m, &m_n->link_n);
  4800. }
  4801. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4802. {
  4803. if (i915.panel_use_ssc >= 0)
  4804. return i915.panel_use_ssc != 0;
  4805. return dev_priv->vbt.lvds_use_ssc
  4806. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4807. }
  4808. static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
  4809. {
  4810. struct drm_device *dev = crtc->base.dev;
  4811. struct drm_i915_private *dev_priv = dev->dev_private;
  4812. int refclk;
  4813. if (IS_VALLEYVIEW(dev)) {
  4814. refclk = 100000;
  4815. } else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4816. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4817. refclk = dev_priv->vbt.lvds_ssc_freq;
  4818. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4819. } else if (!IS_GEN2(dev)) {
  4820. refclk = 96000;
  4821. } else {
  4822. refclk = 48000;
  4823. }
  4824. return refclk;
  4825. }
  4826. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4827. {
  4828. return (1 << dpll->n) << 16 | dpll->m2;
  4829. }
  4830. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4831. {
  4832. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4833. }
  4834. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4835. struct intel_crtc_state *crtc_state,
  4836. intel_clock_t *reduced_clock)
  4837. {
  4838. struct drm_device *dev = crtc->base.dev;
  4839. u32 fp, fp2 = 0;
  4840. if (IS_PINEVIEW(dev)) {
  4841. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  4842. if (reduced_clock)
  4843. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4844. } else {
  4845. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  4846. if (reduced_clock)
  4847. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4848. }
  4849. crtc_state->dpll_hw_state.fp0 = fp;
  4850. crtc->lowfreq_avail = false;
  4851. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  4852. reduced_clock && i915.powersave) {
  4853. crtc_state->dpll_hw_state.fp1 = fp2;
  4854. crtc->lowfreq_avail = true;
  4855. } else {
  4856. crtc_state->dpll_hw_state.fp1 = fp;
  4857. }
  4858. }
  4859. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4860. pipe)
  4861. {
  4862. u32 reg_val;
  4863. /*
  4864. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4865. * and set it to a reasonable value instead.
  4866. */
  4867. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4868. reg_val &= 0xffffff00;
  4869. reg_val |= 0x00000030;
  4870. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4871. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4872. reg_val &= 0x8cffffff;
  4873. reg_val = 0x8c000000;
  4874. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4875. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4876. reg_val &= 0xffffff00;
  4877. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4878. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4879. reg_val &= 0x00ffffff;
  4880. reg_val |= 0xb0000000;
  4881. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4882. }
  4883. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4884. struct intel_link_m_n *m_n)
  4885. {
  4886. struct drm_device *dev = crtc->base.dev;
  4887. struct drm_i915_private *dev_priv = dev->dev_private;
  4888. int pipe = crtc->pipe;
  4889. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4890. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4891. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4892. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4893. }
  4894. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4895. struct intel_link_m_n *m_n,
  4896. struct intel_link_m_n *m2_n2)
  4897. {
  4898. struct drm_device *dev = crtc->base.dev;
  4899. struct drm_i915_private *dev_priv = dev->dev_private;
  4900. int pipe = crtc->pipe;
  4901. enum transcoder transcoder = crtc->config->cpu_transcoder;
  4902. if (INTEL_INFO(dev)->gen >= 5) {
  4903. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4904. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4905. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4906. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4907. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  4908. * for gen < 8) and if DRRS is supported (to make sure the
  4909. * registers are not unnecessarily accessed).
  4910. */
  4911. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  4912. crtc->config->has_drrs) {
  4913. I915_WRITE(PIPE_DATA_M2(transcoder),
  4914. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  4915. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  4916. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  4917. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  4918. }
  4919. } else {
  4920. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4921. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4922. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4923. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4924. }
  4925. }
  4926. void intel_dp_set_m_n(struct intel_crtc *crtc)
  4927. {
  4928. if (crtc->config->has_pch_encoder)
  4929. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  4930. else
  4931. intel_cpu_transcoder_set_m_n(crtc, &crtc->config->dp_m_n,
  4932. &crtc->config->dp_m2_n2);
  4933. }
  4934. static void vlv_update_pll(struct intel_crtc *crtc,
  4935. struct intel_crtc_state *pipe_config)
  4936. {
  4937. u32 dpll, dpll_md;
  4938. /*
  4939. * Enable DPIO clock input. We should never disable the reference
  4940. * clock for pipe B, since VGA hotplug / manual detection depends
  4941. * on it.
  4942. */
  4943. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4944. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4945. /* We should never disable this, set it here for state tracking */
  4946. if (crtc->pipe == PIPE_B)
  4947. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4948. dpll |= DPLL_VCO_ENABLE;
  4949. pipe_config->dpll_hw_state.dpll = dpll;
  4950. dpll_md = (pipe_config->pixel_multiplier - 1)
  4951. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4952. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  4953. }
  4954. static void vlv_prepare_pll(struct intel_crtc *crtc,
  4955. const struct intel_crtc_state *pipe_config)
  4956. {
  4957. struct drm_device *dev = crtc->base.dev;
  4958. struct drm_i915_private *dev_priv = dev->dev_private;
  4959. int pipe = crtc->pipe;
  4960. u32 mdiv;
  4961. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4962. u32 coreclk, reg_val;
  4963. mutex_lock(&dev_priv->dpio_lock);
  4964. bestn = pipe_config->dpll.n;
  4965. bestm1 = pipe_config->dpll.m1;
  4966. bestm2 = pipe_config->dpll.m2;
  4967. bestp1 = pipe_config->dpll.p1;
  4968. bestp2 = pipe_config->dpll.p2;
  4969. /* See eDP HDMI DPIO driver vbios notes doc */
  4970. /* PLL B needs special handling */
  4971. if (pipe == PIPE_B)
  4972. vlv_pllb_recal_opamp(dev_priv, pipe);
  4973. /* Set up Tx target for periodic Rcomp update */
  4974. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4975. /* Disable target IRef on PLL */
  4976. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4977. reg_val &= 0x00ffffff;
  4978. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4979. /* Disable fast lock */
  4980. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4981. /* Set idtafcrecal before PLL is enabled */
  4982. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4983. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4984. mdiv |= ((bestn << DPIO_N_SHIFT));
  4985. mdiv |= (1 << DPIO_K_SHIFT);
  4986. /*
  4987. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4988. * but we don't support that).
  4989. * Note: don't use the DAC post divider as it seems unstable.
  4990. */
  4991. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4992. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4993. mdiv |= DPIO_ENABLE_CALIBRATION;
  4994. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4995. /* Set HBR and RBR LPF coefficients */
  4996. if (pipe_config->port_clock == 162000 ||
  4997. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  4998. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  4999. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5000. 0x009f0003);
  5001. else
  5002. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5003. 0x00d0000f);
  5004. if (pipe_config->has_dp_encoder) {
  5005. /* Use SSC source */
  5006. if (pipe == PIPE_A)
  5007. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5008. 0x0df40000);
  5009. else
  5010. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5011. 0x0df70000);
  5012. } else { /* HDMI or VGA */
  5013. /* Use bend source */
  5014. if (pipe == PIPE_A)
  5015. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5016. 0x0df70000);
  5017. else
  5018. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5019. 0x0df40000);
  5020. }
  5021. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5022. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5023. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  5024. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  5025. coreclk |= 0x01000000;
  5026. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5027. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5028. mutex_unlock(&dev_priv->dpio_lock);
  5029. }
  5030. static void chv_update_pll(struct intel_crtc *crtc,
  5031. struct intel_crtc_state *pipe_config)
  5032. {
  5033. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  5034. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  5035. DPLL_VCO_ENABLE;
  5036. if (crtc->pipe != PIPE_A)
  5037. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5038. pipe_config->dpll_hw_state.dpll_md =
  5039. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5040. }
  5041. static void chv_prepare_pll(struct intel_crtc *crtc,
  5042. const struct intel_crtc_state *pipe_config)
  5043. {
  5044. struct drm_device *dev = crtc->base.dev;
  5045. struct drm_i915_private *dev_priv = dev->dev_private;
  5046. int pipe = crtc->pipe;
  5047. int dpll_reg = DPLL(crtc->pipe);
  5048. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5049. u32 loopfilter, intcoeff;
  5050. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5051. int refclk;
  5052. bestn = pipe_config->dpll.n;
  5053. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5054. bestm1 = pipe_config->dpll.m1;
  5055. bestm2 = pipe_config->dpll.m2 >> 22;
  5056. bestp1 = pipe_config->dpll.p1;
  5057. bestp2 = pipe_config->dpll.p2;
  5058. /*
  5059. * Enable Refclk and SSC
  5060. */
  5061. I915_WRITE(dpll_reg,
  5062. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5063. mutex_lock(&dev_priv->dpio_lock);
  5064. /* p1 and p2 divider */
  5065. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5066. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5067. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5068. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5069. 1 << DPIO_CHV_K_DIV_SHIFT);
  5070. /* Feedback post-divider - m2 */
  5071. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5072. /* Feedback refclk divider - n and m1 */
  5073. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5074. DPIO_CHV_M1_DIV_BY_2 |
  5075. 1 << DPIO_CHV_N_DIV_SHIFT);
  5076. /* M2 fraction division */
  5077. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5078. /* M2 fraction division enable */
  5079. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  5080. DPIO_CHV_FRAC_DIV_EN |
  5081. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  5082. /* Loop filter */
  5083. refclk = i9xx_get_refclk(crtc, 0);
  5084. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  5085. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  5086. if (refclk == 100000)
  5087. intcoeff = 11;
  5088. else if (refclk == 38400)
  5089. intcoeff = 10;
  5090. else
  5091. intcoeff = 9;
  5092. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  5093. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5094. /* AFC Recal */
  5095. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5096. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5097. DPIO_AFC_RECAL);
  5098. mutex_unlock(&dev_priv->dpio_lock);
  5099. }
  5100. /**
  5101. * vlv_force_pll_on - forcibly enable just the PLL
  5102. * @dev_priv: i915 private structure
  5103. * @pipe: pipe PLL to enable
  5104. * @dpll: PLL configuration
  5105. *
  5106. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5107. * in cases where we need the PLL enabled even when @pipe is not going to
  5108. * be enabled.
  5109. */
  5110. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  5111. const struct dpll *dpll)
  5112. {
  5113. struct intel_crtc *crtc =
  5114. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  5115. struct intel_crtc_state pipe_config = {
  5116. .pixel_multiplier = 1,
  5117. .dpll = *dpll,
  5118. };
  5119. if (IS_CHERRYVIEW(dev)) {
  5120. chv_update_pll(crtc, &pipe_config);
  5121. chv_prepare_pll(crtc, &pipe_config);
  5122. chv_enable_pll(crtc, &pipe_config);
  5123. } else {
  5124. vlv_update_pll(crtc, &pipe_config);
  5125. vlv_prepare_pll(crtc, &pipe_config);
  5126. vlv_enable_pll(crtc, &pipe_config);
  5127. }
  5128. }
  5129. /**
  5130. * vlv_force_pll_off - forcibly disable just the PLL
  5131. * @dev_priv: i915 private structure
  5132. * @pipe: pipe PLL to disable
  5133. *
  5134. * Disable the PLL for @pipe. To be used in cases where we need
  5135. * the PLL enabled even when @pipe is not going to be enabled.
  5136. */
  5137. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  5138. {
  5139. if (IS_CHERRYVIEW(dev))
  5140. chv_disable_pll(to_i915(dev), pipe);
  5141. else
  5142. vlv_disable_pll(to_i915(dev), pipe);
  5143. }
  5144. static void i9xx_update_pll(struct intel_crtc *crtc,
  5145. struct intel_crtc_state *crtc_state,
  5146. intel_clock_t *reduced_clock,
  5147. int num_connectors)
  5148. {
  5149. struct drm_device *dev = crtc->base.dev;
  5150. struct drm_i915_private *dev_priv = dev->dev_private;
  5151. u32 dpll;
  5152. bool is_sdvo;
  5153. struct dpll *clock = &crtc_state->dpll;
  5154. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5155. is_sdvo = intel_pipe_will_have_type(crtc, INTEL_OUTPUT_SDVO) ||
  5156. intel_pipe_will_have_type(crtc, INTEL_OUTPUT_HDMI);
  5157. dpll = DPLL_VGA_MODE_DIS;
  5158. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS))
  5159. dpll |= DPLLB_MODE_LVDS;
  5160. else
  5161. dpll |= DPLLB_MODE_DAC_SERIAL;
  5162. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5163. dpll |= (crtc_state->pixel_multiplier - 1)
  5164. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5165. }
  5166. if (is_sdvo)
  5167. dpll |= DPLL_SDVO_HIGH_SPEED;
  5168. if (crtc_state->has_dp_encoder)
  5169. dpll |= DPLL_SDVO_HIGH_SPEED;
  5170. /* compute bitmask from p1 value */
  5171. if (IS_PINEVIEW(dev))
  5172. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5173. else {
  5174. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5175. if (IS_G4X(dev) && reduced_clock)
  5176. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5177. }
  5178. switch (clock->p2) {
  5179. case 5:
  5180. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5181. break;
  5182. case 7:
  5183. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5184. break;
  5185. case 10:
  5186. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5187. break;
  5188. case 14:
  5189. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5190. break;
  5191. }
  5192. if (INTEL_INFO(dev)->gen >= 4)
  5193. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5194. if (crtc_state->sdvo_tv_clock)
  5195. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5196. else if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5197. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5198. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5199. else
  5200. dpll |= PLL_REF_INPUT_DREFCLK;
  5201. dpll |= DPLL_VCO_ENABLE;
  5202. crtc_state->dpll_hw_state.dpll = dpll;
  5203. if (INTEL_INFO(dev)->gen >= 4) {
  5204. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5205. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5206. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5207. }
  5208. }
  5209. static void i8xx_update_pll(struct intel_crtc *crtc,
  5210. struct intel_crtc_state *crtc_state,
  5211. intel_clock_t *reduced_clock,
  5212. int num_connectors)
  5213. {
  5214. struct drm_device *dev = crtc->base.dev;
  5215. struct drm_i915_private *dev_priv = dev->dev_private;
  5216. u32 dpll;
  5217. struct dpll *clock = &crtc_state->dpll;
  5218. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5219. dpll = DPLL_VGA_MODE_DIS;
  5220. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS)) {
  5221. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5222. } else {
  5223. if (clock->p1 == 2)
  5224. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5225. else
  5226. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5227. if (clock->p2 == 4)
  5228. dpll |= PLL_P2_DIVIDE_BY_4;
  5229. }
  5230. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc, INTEL_OUTPUT_DVO))
  5231. dpll |= DPLL_DVO_2X_MODE;
  5232. if (intel_pipe_will_have_type(crtc, INTEL_OUTPUT_LVDS) &&
  5233. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5234. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5235. else
  5236. dpll |= PLL_REF_INPUT_DREFCLK;
  5237. dpll |= DPLL_VCO_ENABLE;
  5238. crtc_state->dpll_hw_state.dpll = dpll;
  5239. }
  5240. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5241. {
  5242. struct drm_device *dev = intel_crtc->base.dev;
  5243. struct drm_i915_private *dev_priv = dev->dev_private;
  5244. enum pipe pipe = intel_crtc->pipe;
  5245. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5246. struct drm_display_mode *adjusted_mode =
  5247. &intel_crtc->config->base.adjusted_mode;
  5248. uint32_t crtc_vtotal, crtc_vblank_end;
  5249. int vsyncshift = 0;
  5250. /* We need to be careful not to changed the adjusted mode, for otherwise
  5251. * the hw state checker will get angry at the mismatch. */
  5252. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5253. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5254. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5255. /* the chip adds 2 halflines automatically */
  5256. crtc_vtotal -= 1;
  5257. crtc_vblank_end -= 1;
  5258. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5259. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5260. else
  5261. vsyncshift = adjusted_mode->crtc_hsync_start -
  5262. adjusted_mode->crtc_htotal / 2;
  5263. if (vsyncshift < 0)
  5264. vsyncshift += adjusted_mode->crtc_htotal;
  5265. }
  5266. if (INTEL_INFO(dev)->gen > 3)
  5267. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5268. I915_WRITE(HTOTAL(cpu_transcoder),
  5269. (adjusted_mode->crtc_hdisplay - 1) |
  5270. ((adjusted_mode->crtc_htotal - 1) << 16));
  5271. I915_WRITE(HBLANK(cpu_transcoder),
  5272. (adjusted_mode->crtc_hblank_start - 1) |
  5273. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5274. I915_WRITE(HSYNC(cpu_transcoder),
  5275. (adjusted_mode->crtc_hsync_start - 1) |
  5276. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5277. I915_WRITE(VTOTAL(cpu_transcoder),
  5278. (adjusted_mode->crtc_vdisplay - 1) |
  5279. ((crtc_vtotal - 1) << 16));
  5280. I915_WRITE(VBLANK(cpu_transcoder),
  5281. (adjusted_mode->crtc_vblank_start - 1) |
  5282. ((crtc_vblank_end - 1) << 16));
  5283. I915_WRITE(VSYNC(cpu_transcoder),
  5284. (adjusted_mode->crtc_vsync_start - 1) |
  5285. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5286. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5287. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5288. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5289. * bits. */
  5290. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  5291. (pipe == PIPE_B || pipe == PIPE_C))
  5292. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5293. /* pipesrc controls the size that is scaled from, which should
  5294. * always be the user's requested size.
  5295. */
  5296. I915_WRITE(PIPESRC(pipe),
  5297. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5298. (intel_crtc->config->pipe_src_h - 1));
  5299. }
  5300. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5301. struct intel_crtc_state *pipe_config)
  5302. {
  5303. struct drm_device *dev = crtc->base.dev;
  5304. struct drm_i915_private *dev_priv = dev->dev_private;
  5305. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5306. uint32_t tmp;
  5307. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5308. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5309. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5310. tmp = I915_READ(HBLANK(cpu_transcoder));
  5311. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5312. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5313. tmp = I915_READ(HSYNC(cpu_transcoder));
  5314. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5315. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5316. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5317. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5318. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5319. tmp = I915_READ(VBLANK(cpu_transcoder));
  5320. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5321. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5322. tmp = I915_READ(VSYNC(cpu_transcoder));
  5323. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5324. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5325. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5326. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5327. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5328. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5329. }
  5330. tmp = I915_READ(PIPESRC(crtc->pipe));
  5331. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5332. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5333. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5334. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5335. }
  5336. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5337. struct intel_crtc_state *pipe_config)
  5338. {
  5339. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5340. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5341. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5342. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5343. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5344. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5345. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5346. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5347. mode->flags = pipe_config->base.adjusted_mode.flags;
  5348. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5349. mode->flags |= pipe_config->base.adjusted_mode.flags;
  5350. }
  5351. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5352. {
  5353. struct drm_device *dev = intel_crtc->base.dev;
  5354. struct drm_i915_private *dev_priv = dev->dev_private;
  5355. uint32_t pipeconf;
  5356. pipeconf = 0;
  5357. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5358. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5359. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5360. if (intel_crtc->config->double_wide)
  5361. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5362. /* only g4x and later have fancy bpc/dither controls */
  5363. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5364. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5365. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  5366. pipeconf |= PIPECONF_DITHER_EN |
  5367. PIPECONF_DITHER_TYPE_SP;
  5368. switch (intel_crtc->config->pipe_bpp) {
  5369. case 18:
  5370. pipeconf |= PIPECONF_6BPC;
  5371. break;
  5372. case 24:
  5373. pipeconf |= PIPECONF_8BPC;
  5374. break;
  5375. case 30:
  5376. pipeconf |= PIPECONF_10BPC;
  5377. break;
  5378. default:
  5379. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5380. BUG();
  5381. }
  5382. }
  5383. if (HAS_PIPE_CXSR(dev)) {
  5384. if (intel_crtc->lowfreq_avail) {
  5385. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5386. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5387. } else {
  5388. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5389. }
  5390. }
  5391. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5392. if (INTEL_INFO(dev)->gen < 4 ||
  5393. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  5394. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5395. else
  5396. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5397. } else
  5398. pipeconf |= PIPECONF_PROGRESSIVE;
  5399. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  5400. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5401. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5402. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5403. }
  5404. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  5405. struct intel_crtc_state *crtc_state)
  5406. {
  5407. struct drm_device *dev = crtc->base.dev;
  5408. struct drm_i915_private *dev_priv = dev->dev_private;
  5409. int refclk, num_connectors = 0;
  5410. intel_clock_t clock, reduced_clock;
  5411. bool ok, has_reduced_clock = false;
  5412. bool is_lvds = false, is_dsi = false;
  5413. struct intel_encoder *encoder;
  5414. const intel_limit_t *limit;
  5415. for_each_intel_encoder(dev, encoder) {
  5416. if (encoder->new_crtc != crtc)
  5417. continue;
  5418. switch (encoder->type) {
  5419. case INTEL_OUTPUT_LVDS:
  5420. is_lvds = true;
  5421. break;
  5422. case INTEL_OUTPUT_DSI:
  5423. is_dsi = true;
  5424. break;
  5425. default:
  5426. break;
  5427. }
  5428. num_connectors++;
  5429. }
  5430. if (is_dsi)
  5431. return 0;
  5432. if (!crtc_state->clock_set) {
  5433. refclk = i9xx_get_refclk(crtc, num_connectors);
  5434. /*
  5435. * Returns a set of divisors for the desired target clock with
  5436. * the given refclk, or FALSE. The returned values represent
  5437. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5438. * 2) / p1 / p2.
  5439. */
  5440. limit = intel_limit(crtc, refclk);
  5441. ok = dev_priv->display.find_dpll(limit, crtc,
  5442. crtc_state->port_clock,
  5443. refclk, NULL, &clock);
  5444. if (!ok) {
  5445. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5446. return -EINVAL;
  5447. }
  5448. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5449. /*
  5450. * Ensure we match the reduced clock's P to the target
  5451. * clock. If the clocks don't match, we can't switch
  5452. * the display clock by using the FP0/FP1. In such case
  5453. * we will disable the LVDS downclock feature.
  5454. */
  5455. has_reduced_clock =
  5456. dev_priv->display.find_dpll(limit, crtc,
  5457. dev_priv->lvds_downclock,
  5458. refclk, &clock,
  5459. &reduced_clock);
  5460. }
  5461. /* Compat-code for transition, will disappear. */
  5462. crtc_state->dpll.n = clock.n;
  5463. crtc_state->dpll.m1 = clock.m1;
  5464. crtc_state->dpll.m2 = clock.m2;
  5465. crtc_state->dpll.p1 = clock.p1;
  5466. crtc_state->dpll.p2 = clock.p2;
  5467. }
  5468. if (IS_GEN2(dev)) {
  5469. i8xx_update_pll(crtc, crtc_state,
  5470. has_reduced_clock ? &reduced_clock : NULL,
  5471. num_connectors);
  5472. } else if (IS_CHERRYVIEW(dev)) {
  5473. chv_update_pll(crtc, crtc_state);
  5474. } else if (IS_VALLEYVIEW(dev)) {
  5475. vlv_update_pll(crtc, crtc_state);
  5476. } else {
  5477. i9xx_update_pll(crtc, crtc_state,
  5478. has_reduced_clock ? &reduced_clock : NULL,
  5479. num_connectors);
  5480. }
  5481. return 0;
  5482. }
  5483. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5484. struct intel_crtc_state *pipe_config)
  5485. {
  5486. struct drm_device *dev = crtc->base.dev;
  5487. struct drm_i915_private *dev_priv = dev->dev_private;
  5488. uint32_t tmp;
  5489. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5490. return;
  5491. tmp = I915_READ(PFIT_CONTROL);
  5492. if (!(tmp & PFIT_ENABLE))
  5493. return;
  5494. /* Check whether the pfit is attached to our pipe. */
  5495. if (INTEL_INFO(dev)->gen < 4) {
  5496. if (crtc->pipe != PIPE_B)
  5497. return;
  5498. } else {
  5499. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5500. return;
  5501. }
  5502. pipe_config->gmch_pfit.control = tmp;
  5503. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5504. if (INTEL_INFO(dev)->gen < 5)
  5505. pipe_config->gmch_pfit.lvds_border_bits =
  5506. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5507. }
  5508. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5509. struct intel_crtc_state *pipe_config)
  5510. {
  5511. struct drm_device *dev = crtc->base.dev;
  5512. struct drm_i915_private *dev_priv = dev->dev_private;
  5513. int pipe = pipe_config->cpu_transcoder;
  5514. intel_clock_t clock;
  5515. u32 mdiv;
  5516. int refclk = 100000;
  5517. /* In case of MIPI DPLL will not even be used */
  5518. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  5519. return;
  5520. mutex_lock(&dev_priv->dpio_lock);
  5521. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5522. mutex_unlock(&dev_priv->dpio_lock);
  5523. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5524. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5525. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5526. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5527. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5528. vlv_clock(refclk, &clock);
  5529. /* clock.dot is the fast clock */
  5530. pipe_config->port_clock = clock.dot / 5;
  5531. }
  5532. static void
  5533. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  5534. struct intel_initial_plane_config *plane_config)
  5535. {
  5536. struct drm_device *dev = crtc->base.dev;
  5537. struct drm_i915_private *dev_priv = dev->dev_private;
  5538. u32 val, base, offset;
  5539. int pipe = crtc->pipe, plane = crtc->plane;
  5540. int fourcc, pixel_format;
  5541. int aligned_height;
  5542. struct drm_framebuffer *fb;
  5543. struct intel_framebuffer *intel_fb;
  5544. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5545. if (!intel_fb) {
  5546. DRM_DEBUG_KMS("failed to alloc fb\n");
  5547. return;
  5548. }
  5549. fb = &intel_fb->base;
  5550. val = I915_READ(DSPCNTR(plane));
  5551. if (INTEL_INFO(dev)->gen >= 4)
  5552. if (val & DISPPLANE_TILED)
  5553. plane_config->tiling = I915_TILING_X;
  5554. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5555. fourcc = i9xx_format_to_fourcc(pixel_format);
  5556. fb->pixel_format = fourcc;
  5557. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  5558. if (INTEL_INFO(dev)->gen >= 4) {
  5559. if (plane_config->tiling)
  5560. offset = I915_READ(DSPTILEOFF(plane));
  5561. else
  5562. offset = I915_READ(DSPLINOFF(plane));
  5563. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5564. } else {
  5565. base = I915_READ(DSPADDR(plane));
  5566. }
  5567. plane_config->base = base;
  5568. val = I915_READ(PIPESRC(pipe));
  5569. fb->width = ((val >> 16) & 0xfff) + 1;
  5570. fb->height = ((val >> 0) & 0xfff) + 1;
  5571. val = I915_READ(DSPSTRIDE(pipe));
  5572. fb->pitches[0] = val & 0xffffffc0;
  5573. aligned_height = intel_fb_align_height(dev, fb->height,
  5574. plane_config->tiling);
  5575. plane_config->size = fb->pitches[0] * aligned_height;
  5576. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5577. pipe_name(pipe), plane, fb->width, fb->height,
  5578. fb->bits_per_pixel, base, fb->pitches[0],
  5579. plane_config->size);
  5580. crtc->base.primary->fb = fb;
  5581. }
  5582. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5583. struct intel_crtc_state *pipe_config)
  5584. {
  5585. struct drm_device *dev = crtc->base.dev;
  5586. struct drm_i915_private *dev_priv = dev->dev_private;
  5587. int pipe = pipe_config->cpu_transcoder;
  5588. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5589. intel_clock_t clock;
  5590. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5591. int refclk = 100000;
  5592. mutex_lock(&dev_priv->dpio_lock);
  5593. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5594. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5595. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5596. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5597. mutex_unlock(&dev_priv->dpio_lock);
  5598. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5599. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5600. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5601. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5602. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5603. chv_clock(refclk, &clock);
  5604. /* clock.dot is the fast clock */
  5605. pipe_config->port_clock = clock.dot / 5;
  5606. }
  5607. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5608. struct intel_crtc_state *pipe_config)
  5609. {
  5610. struct drm_device *dev = crtc->base.dev;
  5611. struct drm_i915_private *dev_priv = dev->dev_private;
  5612. uint32_t tmp;
  5613. if (!intel_display_power_is_enabled(dev_priv,
  5614. POWER_DOMAIN_PIPE(crtc->pipe)))
  5615. return false;
  5616. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5617. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5618. tmp = I915_READ(PIPECONF(crtc->pipe));
  5619. if (!(tmp & PIPECONF_ENABLE))
  5620. return false;
  5621. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5622. switch (tmp & PIPECONF_BPC_MASK) {
  5623. case PIPECONF_6BPC:
  5624. pipe_config->pipe_bpp = 18;
  5625. break;
  5626. case PIPECONF_8BPC:
  5627. pipe_config->pipe_bpp = 24;
  5628. break;
  5629. case PIPECONF_10BPC:
  5630. pipe_config->pipe_bpp = 30;
  5631. break;
  5632. default:
  5633. break;
  5634. }
  5635. }
  5636. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5637. pipe_config->limited_color_range = true;
  5638. if (INTEL_INFO(dev)->gen < 4)
  5639. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5640. intel_get_pipe_timings(crtc, pipe_config);
  5641. i9xx_get_pfit_config(crtc, pipe_config);
  5642. if (INTEL_INFO(dev)->gen >= 4) {
  5643. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5644. pipe_config->pixel_multiplier =
  5645. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5646. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5647. pipe_config->dpll_hw_state.dpll_md = tmp;
  5648. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5649. tmp = I915_READ(DPLL(crtc->pipe));
  5650. pipe_config->pixel_multiplier =
  5651. ((tmp & SDVO_MULTIPLIER_MASK)
  5652. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5653. } else {
  5654. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5655. * port and will be fixed up in the encoder->get_config
  5656. * function. */
  5657. pipe_config->pixel_multiplier = 1;
  5658. }
  5659. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5660. if (!IS_VALLEYVIEW(dev)) {
  5661. /*
  5662. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  5663. * on 830. Filter it out here so that we don't
  5664. * report errors due to that.
  5665. */
  5666. if (IS_I830(dev))
  5667. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  5668. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5669. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5670. } else {
  5671. /* Mask out read-only status bits. */
  5672. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5673. DPLL_PORTC_READY_MASK |
  5674. DPLL_PORTB_READY_MASK);
  5675. }
  5676. if (IS_CHERRYVIEW(dev))
  5677. chv_crtc_clock_get(crtc, pipe_config);
  5678. else if (IS_VALLEYVIEW(dev))
  5679. vlv_crtc_clock_get(crtc, pipe_config);
  5680. else
  5681. i9xx_crtc_clock_get(crtc, pipe_config);
  5682. return true;
  5683. }
  5684. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5685. {
  5686. struct drm_i915_private *dev_priv = dev->dev_private;
  5687. struct intel_encoder *encoder;
  5688. u32 val, final;
  5689. bool has_lvds = false;
  5690. bool has_cpu_edp = false;
  5691. bool has_panel = false;
  5692. bool has_ck505 = false;
  5693. bool can_ssc = false;
  5694. /* We need to take the global config into account */
  5695. for_each_intel_encoder(dev, encoder) {
  5696. switch (encoder->type) {
  5697. case INTEL_OUTPUT_LVDS:
  5698. has_panel = true;
  5699. has_lvds = true;
  5700. break;
  5701. case INTEL_OUTPUT_EDP:
  5702. has_panel = true;
  5703. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5704. has_cpu_edp = true;
  5705. break;
  5706. default:
  5707. break;
  5708. }
  5709. }
  5710. if (HAS_PCH_IBX(dev)) {
  5711. has_ck505 = dev_priv->vbt.display_clock_mode;
  5712. can_ssc = has_ck505;
  5713. } else {
  5714. has_ck505 = false;
  5715. can_ssc = true;
  5716. }
  5717. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5718. has_panel, has_lvds, has_ck505);
  5719. /* Ironlake: try to setup display ref clock before DPLL
  5720. * enabling. This is only under driver's control after
  5721. * PCH B stepping, previous chipset stepping should be
  5722. * ignoring this setting.
  5723. */
  5724. val = I915_READ(PCH_DREF_CONTROL);
  5725. /* As we must carefully and slowly disable/enable each source in turn,
  5726. * compute the final state we want first and check if we need to
  5727. * make any changes at all.
  5728. */
  5729. final = val;
  5730. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5731. if (has_ck505)
  5732. final |= DREF_NONSPREAD_CK505_ENABLE;
  5733. else
  5734. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5735. final &= ~DREF_SSC_SOURCE_MASK;
  5736. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5737. final &= ~DREF_SSC1_ENABLE;
  5738. if (has_panel) {
  5739. final |= DREF_SSC_SOURCE_ENABLE;
  5740. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5741. final |= DREF_SSC1_ENABLE;
  5742. if (has_cpu_edp) {
  5743. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5744. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5745. else
  5746. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5747. } else
  5748. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5749. } else {
  5750. final |= DREF_SSC_SOURCE_DISABLE;
  5751. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5752. }
  5753. if (final == val)
  5754. return;
  5755. /* Always enable nonspread source */
  5756. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5757. if (has_ck505)
  5758. val |= DREF_NONSPREAD_CK505_ENABLE;
  5759. else
  5760. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5761. if (has_panel) {
  5762. val &= ~DREF_SSC_SOURCE_MASK;
  5763. val |= DREF_SSC_SOURCE_ENABLE;
  5764. /* SSC must be turned on before enabling the CPU output */
  5765. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5766. DRM_DEBUG_KMS("Using SSC on panel\n");
  5767. val |= DREF_SSC1_ENABLE;
  5768. } else
  5769. val &= ~DREF_SSC1_ENABLE;
  5770. /* Get SSC going before enabling the outputs */
  5771. I915_WRITE(PCH_DREF_CONTROL, val);
  5772. POSTING_READ(PCH_DREF_CONTROL);
  5773. udelay(200);
  5774. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5775. /* Enable CPU source on CPU attached eDP */
  5776. if (has_cpu_edp) {
  5777. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5778. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5779. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5780. } else
  5781. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5782. } else
  5783. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5784. I915_WRITE(PCH_DREF_CONTROL, val);
  5785. POSTING_READ(PCH_DREF_CONTROL);
  5786. udelay(200);
  5787. } else {
  5788. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5789. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5790. /* Turn off CPU output */
  5791. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5792. I915_WRITE(PCH_DREF_CONTROL, val);
  5793. POSTING_READ(PCH_DREF_CONTROL);
  5794. udelay(200);
  5795. /* Turn off the SSC source */
  5796. val &= ~DREF_SSC_SOURCE_MASK;
  5797. val |= DREF_SSC_SOURCE_DISABLE;
  5798. /* Turn off SSC1 */
  5799. val &= ~DREF_SSC1_ENABLE;
  5800. I915_WRITE(PCH_DREF_CONTROL, val);
  5801. POSTING_READ(PCH_DREF_CONTROL);
  5802. udelay(200);
  5803. }
  5804. BUG_ON(val != final);
  5805. }
  5806. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5807. {
  5808. uint32_t tmp;
  5809. tmp = I915_READ(SOUTH_CHICKEN2);
  5810. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5811. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5812. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5813. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5814. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5815. tmp = I915_READ(SOUTH_CHICKEN2);
  5816. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5817. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5818. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5819. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5820. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5821. }
  5822. /* WaMPhyProgramming:hsw */
  5823. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5824. {
  5825. uint32_t tmp;
  5826. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5827. tmp &= ~(0xFF << 24);
  5828. tmp |= (0x12 << 24);
  5829. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5830. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5831. tmp |= (1 << 11);
  5832. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5833. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5834. tmp |= (1 << 11);
  5835. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5836. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5837. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5838. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5839. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5840. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5841. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5842. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5843. tmp &= ~(7 << 13);
  5844. tmp |= (5 << 13);
  5845. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5846. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5847. tmp &= ~(7 << 13);
  5848. tmp |= (5 << 13);
  5849. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5850. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5851. tmp &= ~0xFF;
  5852. tmp |= 0x1C;
  5853. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5854. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5855. tmp &= ~0xFF;
  5856. tmp |= 0x1C;
  5857. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5858. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5859. tmp &= ~(0xFF << 16);
  5860. tmp |= (0x1C << 16);
  5861. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5862. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5863. tmp &= ~(0xFF << 16);
  5864. tmp |= (0x1C << 16);
  5865. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5866. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5867. tmp |= (1 << 27);
  5868. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5869. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5870. tmp |= (1 << 27);
  5871. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5872. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5873. tmp &= ~(0xF << 28);
  5874. tmp |= (4 << 28);
  5875. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5876. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5877. tmp &= ~(0xF << 28);
  5878. tmp |= (4 << 28);
  5879. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5880. }
  5881. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5882. * Programming" based on the parameters passed:
  5883. * - Sequence to enable CLKOUT_DP
  5884. * - Sequence to enable CLKOUT_DP without spread
  5885. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5886. */
  5887. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5888. bool with_fdi)
  5889. {
  5890. struct drm_i915_private *dev_priv = dev->dev_private;
  5891. uint32_t reg, tmp;
  5892. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5893. with_spread = true;
  5894. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5895. with_fdi, "LP PCH doesn't have FDI\n"))
  5896. with_fdi = false;
  5897. mutex_lock(&dev_priv->dpio_lock);
  5898. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5899. tmp &= ~SBI_SSCCTL_DISABLE;
  5900. tmp |= SBI_SSCCTL_PATHALT;
  5901. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5902. udelay(24);
  5903. if (with_spread) {
  5904. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5905. tmp &= ~SBI_SSCCTL_PATHALT;
  5906. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5907. if (with_fdi) {
  5908. lpt_reset_fdi_mphy(dev_priv);
  5909. lpt_program_fdi_mphy(dev_priv);
  5910. }
  5911. }
  5912. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5913. SBI_GEN0 : SBI_DBUFF0;
  5914. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5915. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5916. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5917. mutex_unlock(&dev_priv->dpio_lock);
  5918. }
  5919. /* Sequence to disable CLKOUT_DP */
  5920. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5921. {
  5922. struct drm_i915_private *dev_priv = dev->dev_private;
  5923. uint32_t reg, tmp;
  5924. mutex_lock(&dev_priv->dpio_lock);
  5925. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5926. SBI_GEN0 : SBI_DBUFF0;
  5927. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5928. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5929. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5930. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5931. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5932. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5933. tmp |= SBI_SSCCTL_PATHALT;
  5934. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5935. udelay(32);
  5936. }
  5937. tmp |= SBI_SSCCTL_DISABLE;
  5938. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5939. }
  5940. mutex_unlock(&dev_priv->dpio_lock);
  5941. }
  5942. static void lpt_init_pch_refclk(struct drm_device *dev)
  5943. {
  5944. struct intel_encoder *encoder;
  5945. bool has_vga = false;
  5946. for_each_intel_encoder(dev, encoder) {
  5947. switch (encoder->type) {
  5948. case INTEL_OUTPUT_ANALOG:
  5949. has_vga = true;
  5950. break;
  5951. default:
  5952. break;
  5953. }
  5954. }
  5955. if (has_vga)
  5956. lpt_enable_clkout_dp(dev, true, true);
  5957. else
  5958. lpt_disable_clkout_dp(dev);
  5959. }
  5960. /*
  5961. * Initialize reference clocks when the driver loads
  5962. */
  5963. void intel_init_pch_refclk(struct drm_device *dev)
  5964. {
  5965. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5966. ironlake_init_pch_refclk(dev);
  5967. else if (HAS_PCH_LPT(dev))
  5968. lpt_init_pch_refclk(dev);
  5969. }
  5970. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5971. {
  5972. struct drm_device *dev = crtc->dev;
  5973. struct drm_i915_private *dev_priv = dev->dev_private;
  5974. struct intel_encoder *encoder;
  5975. int num_connectors = 0;
  5976. bool is_lvds = false;
  5977. for_each_intel_encoder(dev, encoder) {
  5978. if (encoder->new_crtc != to_intel_crtc(crtc))
  5979. continue;
  5980. switch (encoder->type) {
  5981. case INTEL_OUTPUT_LVDS:
  5982. is_lvds = true;
  5983. break;
  5984. default:
  5985. break;
  5986. }
  5987. num_connectors++;
  5988. }
  5989. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5990. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5991. dev_priv->vbt.lvds_ssc_freq);
  5992. return dev_priv->vbt.lvds_ssc_freq;
  5993. }
  5994. return 120000;
  5995. }
  5996. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5997. {
  5998. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5999. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6000. int pipe = intel_crtc->pipe;
  6001. uint32_t val;
  6002. val = 0;
  6003. switch (intel_crtc->config->pipe_bpp) {
  6004. case 18:
  6005. val |= PIPECONF_6BPC;
  6006. break;
  6007. case 24:
  6008. val |= PIPECONF_8BPC;
  6009. break;
  6010. case 30:
  6011. val |= PIPECONF_10BPC;
  6012. break;
  6013. case 36:
  6014. val |= PIPECONF_12BPC;
  6015. break;
  6016. default:
  6017. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6018. BUG();
  6019. }
  6020. if (intel_crtc->config->dither)
  6021. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6022. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6023. val |= PIPECONF_INTERLACED_ILK;
  6024. else
  6025. val |= PIPECONF_PROGRESSIVE;
  6026. if (intel_crtc->config->limited_color_range)
  6027. val |= PIPECONF_COLOR_RANGE_SELECT;
  6028. I915_WRITE(PIPECONF(pipe), val);
  6029. POSTING_READ(PIPECONF(pipe));
  6030. }
  6031. /*
  6032. * Set up the pipe CSC unit.
  6033. *
  6034. * Currently only full range RGB to limited range RGB conversion
  6035. * is supported, but eventually this should handle various
  6036. * RGB<->YCbCr scenarios as well.
  6037. */
  6038. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  6039. {
  6040. struct drm_device *dev = crtc->dev;
  6041. struct drm_i915_private *dev_priv = dev->dev_private;
  6042. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6043. int pipe = intel_crtc->pipe;
  6044. uint16_t coeff = 0x7800; /* 1.0 */
  6045. /*
  6046. * TODO: Check what kind of values actually come out of the pipe
  6047. * with these coeff/postoff values and adjust to get the best
  6048. * accuracy. Perhaps we even need to take the bpc value into
  6049. * consideration.
  6050. */
  6051. if (intel_crtc->config->limited_color_range)
  6052. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  6053. /*
  6054. * GY/GU and RY/RU should be the other way around according
  6055. * to BSpec, but reality doesn't agree. Just set them up in
  6056. * a way that results in the correct picture.
  6057. */
  6058. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  6059. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  6060. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  6061. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  6062. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  6063. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  6064. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  6065. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  6066. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  6067. if (INTEL_INFO(dev)->gen > 6) {
  6068. uint16_t postoff = 0;
  6069. if (intel_crtc->config->limited_color_range)
  6070. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  6071. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  6072. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  6073. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  6074. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  6075. } else {
  6076. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  6077. if (intel_crtc->config->limited_color_range)
  6078. mode |= CSC_BLACK_SCREEN_OFFSET;
  6079. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  6080. }
  6081. }
  6082. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6083. {
  6084. struct drm_device *dev = crtc->dev;
  6085. struct drm_i915_private *dev_priv = dev->dev_private;
  6086. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6087. enum pipe pipe = intel_crtc->pipe;
  6088. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6089. uint32_t val;
  6090. val = 0;
  6091. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  6092. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6093. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6094. val |= PIPECONF_INTERLACED_ILK;
  6095. else
  6096. val |= PIPECONF_PROGRESSIVE;
  6097. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6098. POSTING_READ(PIPECONF(cpu_transcoder));
  6099. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  6100. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  6101. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  6102. val = 0;
  6103. switch (intel_crtc->config->pipe_bpp) {
  6104. case 18:
  6105. val |= PIPEMISC_DITHER_6_BPC;
  6106. break;
  6107. case 24:
  6108. val |= PIPEMISC_DITHER_8_BPC;
  6109. break;
  6110. case 30:
  6111. val |= PIPEMISC_DITHER_10_BPC;
  6112. break;
  6113. case 36:
  6114. val |= PIPEMISC_DITHER_12_BPC;
  6115. break;
  6116. default:
  6117. /* Case prevented by pipe_config_set_bpp. */
  6118. BUG();
  6119. }
  6120. if (intel_crtc->config->dither)
  6121. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6122. I915_WRITE(PIPEMISC(pipe), val);
  6123. }
  6124. }
  6125. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  6126. struct intel_crtc_state *crtc_state,
  6127. intel_clock_t *clock,
  6128. bool *has_reduced_clock,
  6129. intel_clock_t *reduced_clock)
  6130. {
  6131. struct drm_device *dev = crtc->dev;
  6132. struct drm_i915_private *dev_priv = dev->dev_private;
  6133. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6134. int refclk;
  6135. const intel_limit_t *limit;
  6136. bool ret, is_lvds = false;
  6137. is_lvds = intel_pipe_will_have_type(intel_crtc, INTEL_OUTPUT_LVDS);
  6138. refclk = ironlake_get_refclk(crtc);
  6139. /*
  6140. * Returns a set of divisors for the desired target clock with the given
  6141. * refclk, or FALSE. The returned values represent the clock equation:
  6142. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  6143. */
  6144. limit = intel_limit(intel_crtc, refclk);
  6145. ret = dev_priv->display.find_dpll(limit, intel_crtc,
  6146. crtc_state->port_clock,
  6147. refclk, NULL, clock);
  6148. if (!ret)
  6149. return false;
  6150. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6151. /*
  6152. * Ensure we match the reduced clock's P to the target clock.
  6153. * If the clocks don't match, we can't switch the display clock
  6154. * by using the FP0/FP1. In such case we will disable the LVDS
  6155. * downclock feature.
  6156. */
  6157. *has_reduced_clock =
  6158. dev_priv->display.find_dpll(limit, intel_crtc,
  6159. dev_priv->lvds_downclock,
  6160. refclk, clock,
  6161. reduced_clock);
  6162. }
  6163. return true;
  6164. }
  6165. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6166. {
  6167. /*
  6168. * Account for spread spectrum to avoid
  6169. * oversubscribing the link. Max center spread
  6170. * is 2.5%; use 5% for safety's sake.
  6171. */
  6172. u32 bps = target_clock * bpp * 21 / 20;
  6173. return DIV_ROUND_UP(bps, link_bw * 8);
  6174. }
  6175. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6176. {
  6177. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6178. }
  6179. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6180. struct intel_crtc_state *crtc_state,
  6181. u32 *fp,
  6182. intel_clock_t *reduced_clock, u32 *fp2)
  6183. {
  6184. struct drm_crtc *crtc = &intel_crtc->base;
  6185. struct drm_device *dev = crtc->dev;
  6186. struct drm_i915_private *dev_priv = dev->dev_private;
  6187. struct intel_encoder *intel_encoder;
  6188. uint32_t dpll;
  6189. int factor, num_connectors = 0;
  6190. bool is_lvds = false, is_sdvo = false;
  6191. for_each_intel_encoder(dev, intel_encoder) {
  6192. if (intel_encoder->new_crtc != to_intel_crtc(crtc))
  6193. continue;
  6194. switch (intel_encoder->type) {
  6195. case INTEL_OUTPUT_LVDS:
  6196. is_lvds = true;
  6197. break;
  6198. case INTEL_OUTPUT_SDVO:
  6199. case INTEL_OUTPUT_HDMI:
  6200. is_sdvo = true;
  6201. break;
  6202. default:
  6203. break;
  6204. }
  6205. num_connectors++;
  6206. }
  6207. /* Enable autotuning of the PLL clock (if permissible) */
  6208. factor = 21;
  6209. if (is_lvds) {
  6210. if ((intel_panel_use_ssc(dev_priv) &&
  6211. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6212. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  6213. factor = 25;
  6214. } else if (crtc_state->sdvo_tv_clock)
  6215. factor = 20;
  6216. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6217. *fp |= FP_CB_TUNE;
  6218. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  6219. *fp2 |= FP_CB_TUNE;
  6220. dpll = 0;
  6221. if (is_lvds)
  6222. dpll |= DPLLB_MODE_LVDS;
  6223. else
  6224. dpll |= DPLLB_MODE_DAC_SERIAL;
  6225. dpll |= (crtc_state->pixel_multiplier - 1)
  6226. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6227. if (is_sdvo)
  6228. dpll |= DPLL_SDVO_HIGH_SPEED;
  6229. if (crtc_state->has_dp_encoder)
  6230. dpll |= DPLL_SDVO_HIGH_SPEED;
  6231. /* compute bitmask from p1 value */
  6232. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6233. /* also FPA1 */
  6234. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6235. switch (crtc_state->dpll.p2) {
  6236. case 5:
  6237. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6238. break;
  6239. case 7:
  6240. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6241. break;
  6242. case 10:
  6243. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6244. break;
  6245. case 14:
  6246. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6247. break;
  6248. }
  6249. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6250. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6251. else
  6252. dpll |= PLL_REF_INPUT_DREFCLK;
  6253. return dpll | DPLL_VCO_ENABLE;
  6254. }
  6255. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6256. struct intel_crtc_state *crtc_state)
  6257. {
  6258. struct drm_device *dev = crtc->base.dev;
  6259. intel_clock_t clock, reduced_clock;
  6260. u32 dpll = 0, fp = 0, fp2 = 0;
  6261. bool ok, has_reduced_clock = false;
  6262. bool is_lvds = false;
  6263. struct intel_shared_dpll *pll;
  6264. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  6265. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  6266. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  6267. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  6268. &has_reduced_clock, &reduced_clock);
  6269. if (!ok && !crtc_state->clock_set) {
  6270. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6271. return -EINVAL;
  6272. }
  6273. /* Compat-code for transition, will disappear. */
  6274. if (!crtc_state->clock_set) {
  6275. crtc_state->dpll.n = clock.n;
  6276. crtc_state->dpll.m1 = clock.m1;
  6277. crtc_state->dpll.m2 = clock.m2;
  6278. crtc_state->dpll.p1 = clock.p1;
  6279. crtc_state->dpll.p2 = clock.p2;
  6280. }
  6281. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6282. if (crtc_state->has_pch_encoder) {
  6283. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6284. if (has_reduced_clock)
  6285. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  6286. dpll = ironlake_compute_dpll(crtc, crtc_state,
  6287. &fp, &reduced_clock,
  6288. has_reduced_clock ? &fp2 : NULL);
  6289. crtc_state->dpll_hw_state.dpll = dpll;
  6290. crtc_state->dpll_hw_state.fp0 = fp;
  6291. if (has_reduced_clock)
  6292. crtc_state->dpll_hw_state.fp1 = fp2;
  6293. else
  6294. crtc_state->dpll_hw_state.fp1 = fp;
  6295. pll = intel_get_shared_dpll(crtc, crtc_state);
  6296. if (pll == NULL) {
  6297. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6298. pipe_name(crtc->pipe));
  6299. return -EINVAL;
  6300. }
  6301. }
  6302. if (is_lvds && has_reduced_clock && i915.powersave)
  6303. crtc->lowfreq_avail = true;
  6304. else
  6305. crtc->lowfreq_avail = false;
  6306. return 0;
  6307. }
  6308. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6309. struct intel_link_m_n *m_n)
  6310. {
  6311. struct drm_device *dev = crtc->base.dev;
  6312. struct drm_i915_private *dev_priv = dev->dev_private;
  6313. enum pipe pipe = crtc->pipe;
  6314. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6315. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6316. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6317. & ~TU_SIZE_MASK;
  6318. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6319. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6320. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6321. }
  6322. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6323. enum transcoder transcoder,
  6324. struct intel_link_m_n *m_n,
  6325. struct intel_link_m_n *m2_n2)
  6326. {
  6327. struct drm_device *dev = crtc->base.dev;
  6328. struct drm_i915_private *dev_priv = dev->dev_private;
  6329. enum pipe pipe = crtc->pipe;
  6330. if (INTEL_INFO(dev)->gen >= 5) {
  6331. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6332. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6333. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6334. & ~TU_SIZE_MASK;
  6335. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6336. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6337. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6338. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6339. * gen < 8) and if DRRS is supported (to make sure the
  6340. * registers are not unnecessarily read).
  6341. */
  6342. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  6343. crtc->config->has_drrs) {
  6344. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6345. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6346. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6347. & ~TU_SIZE_MASK;
  6348. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6349. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6350. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6351. }
  6352. } else {
  6353. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6354. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6355. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6356. & ~TU_SIZE_MASK;
  6357. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6358. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6359. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6360. }
  6361. }
  6362. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6363. struct intel_crtc_state *pipe_config)
  6364. {
  6365. if (pipe_config->has_pch_encoder)
  6366. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6367. else
  6368. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6369. &pipe_config->dp_m_n,
  6370. &pipe_config->dp_m2_n2);
  6371. }
  6372. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6373. struct intel_crtc_state *pipe_config)
  6374. {
  6375. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6376. &pipe_config->fdi_m_n, NULL);
  6377. }
  6378. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6379. struct intel_crtc_state *pipe_config)
  6380. {
  6381. struct drm_device *dev = crtc->base.dev;
  6382. struct drm_i915_private *dev_priv = dev->dev_private;
  6383. uint32_t tmp;
  6384. tmp = I915_READ(PS_CTL(crtc->pipe));
  6385. if (tmp & PS_ENABLE) {
  6386. pipe_config->pch_pfit.enabled = true;
  6387. pipe_config->pch_pfit.pos = I915_READ(PS_WIN_POS(crtc->pipe));
  6388. pipe_config->pch_pfit.size = I915_READ(PS_WIN_SZ(crtc->pipe));
  6389. }
  6390. }
  6391. static void
  6392. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  6393. struct intel_initial_plane_config *plane_config)
  6394. {
  6395. struct drm_device *dev = crtc->base.dev;
  6396. struct drm_i915_private *dev_priv = dev->dev_private;
  6397. u32 val, base, offset, stride_mult;
  6398. int pipe = crtc->pipe;
  6399. int fourcc, pixel_format;
  6400. int aligned_height;
  6401. struct drm_framebuffer *fb;
  6402. struct intel_framebuffer *intel_fb;
  6403. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6404. if (!intel_fb) {
  6405. DRM_DEBUG_KMS("failed to alloc fb\n");
  6406. return;
  6407. }
  6408. fb = &intel_fb->base;
  6409. val = I915_READ(PLANE_CTL(pipe, 0));
  6410. if (val & PLANE_CTL_TILED_MASK)
  6411. plane_config->tiling = I915_TILING_X;
  6412. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  6413. fourcc = skl_format_to_fourcc(pixel_format,
  6414. val & PLANE_CTL_ORDER_RGBX,
  6415. val & PLANE_CTL_ALPHA_MASK);
  6416. fb->pixel_format = fourcc;
  6417. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6418. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  6419. plane_config->base = base;
  6420. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  6421. val = I915_READ(PLANE_SIZE(pipe, 0));
  6422. fb->height = ((val >> 16) & 0xfff) + 1;
  6423. fb->width = ((val >> 0) & 0x1fff) + 1;
  6424. val = I915_READ(PLANE_STRIDE(pipe, 0));
  6425. switch (plane_config->tiling) {
  6426. case I915_TILING_NONE:
  6427. stride_mult = 64;
  6428. break;
  6429. case I915_TILING_X:
  6430. stride_mult = 512;
  6431. break;
  6432. default:
  6433. MISSING_CASE(plane_config->tiling);
  6434. goto error;
  6435. }
  6436. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  6437. aligned_height = intel_fb_align_height(dev, fb->height,
  6438. plane_config->tiling);
  6439. plane_config->size = fb->pitches[0] * aligned_height;
  6440. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6441. pipe_name(pipe), fb->width, fb->height,
  6442. fb->bits_per_pixel, base, fb->pitches[0],
  6443. plane_config->size);
  6444. crtc->base.primary->fb = fb;
  6445. return;
  6446. error:
  6447. kfree(fb);
  6448. }
  6449. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  6450. struct intel_crtc_state *pipe_config)
  6451. {
  6452. struct drm_device *dev = crtc->base.dev;
  6453. struct drm_i915_private *dev_priv = dev->dev_private;
  6454. uint32_t tmp;
  6455. tmp = I915_READ(PF_CTL(crtc->pipe));
  6456. if (tmp & PF_ENABLE) {
  6457. pipe_config->pch_pfit.enabled = true;
  6458. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  6459. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  6460. /* We currently do not free assignements of panel fitters on
  6461. * ivb/hsw (since we don't use the higher upscaling modes which
  6462. * differentiates them) so just WARN about this case for now. */
  6463. if (IS_GEN7(dev)) {
  6464. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  6465. PF_PIPE_SEL_IVB(crtc->pipe));
  6466. }
  6467. }
  6468. }
  6469. static void
  6470. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  6471. struct intel_initial_plane_config *plane_config)
  6472. {
  6473. struct drm_device *dev = crtc->base.dev;
  6474. struct drm_i915_private *dev_priv = dev->dev_private;
  6475. u32 val, base, offset;
  6476. int pipe = crtc->pipe;
  6477. int fourcc, pixel_format;
  6478. int aligned_height;
  6479. struct drm_framebuffer *fb;
  6480. struct intel_framebuffer *intel_fb;
  6481. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6482. if (!intel_fb) {
  6483. DRM_DEBUG_KMS("failed to alloc fb\n");
  6484. return;
  6485. }
  6486. fb = &intel_fb->base;
  6487. val = I915_READ(DSPCNTR(pipe));
  6488. if (INTEL_INFO(dev)->gen >= 4)
  6489. if (val & DISPPLANE_TILED)
  6490. plane_config->tiling = I915_TILING_X;
  6491. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6492. fourcc = i9xx_format_to_fourcc(pixel_format);
  6493. fb->pixel_format = fourcc;
  6494. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6495. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  6496. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6497. offset = I915_READ(DSPOFFSET(pipe));
  6498. } else {
  6499. if (plane_config->tiling)
  6500. offset = I915_READ(DSPTILEOFF(pipe));
  6501. else
  6502. offset = I915_READ(DSPLINOFF(pipe));
  6503. }
  6504. plane_config->base = base;
  6505. val = I915_READ(PIPESRC(pipe));
  6506. fb->width = ((val >> 16) & 0xfff) + 1;
  6507. fb->height = ((val >> 0) & 0xfff) + 1;
  6508. val = I915_READ(DSPSTRIDE(pipe));
  6509. fb->pitches[0] = val & 0xffffffc0;
  6510. aligned_height = intel_fb_align_height(dev, fb->height,
  6511. plane_config->tiling);
  6512. plane_config->size = fb->pitches[0] * aligned_height;
  6513. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6514. pipe_name(pipe), fb->width, fb->height,
  6515. fb->bits_per_pixel, base, fb->pitches[0],
  6516. plane_config->size);
  6517. crtc->base.primary->fb = fb;
  6518. }
  6519. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  6520. struct intel_crtc_state *pipe_config)
  6521. {
  6522. struct drm_device *dev = crtc->base.dev;
  6523. struct drm_i915_private *dev_priv = dev->dev_private;
  6524. uint32_t tmp;
  6525. if (!intel_display_power_is_enabled(dev_priv,
  6526. POWER_DOMAIN_PIPE(crtc->pipe)))
  6527. return false;
  6528. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6529. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6530. tmp = I915_READ(PIPECONF(crtc->pipe));
  6531. if (!(tmp & PIPECONF_ENABLE))
  6532. return false;
  6533. switch (tmp & PIPECONF_BPC_MASK) {
  6534. case PIPECONF_6BPC:
  6535. pipe_config->pipe_bpp = 18;
  6536. break;
  6537. case PIPECONF_8BPC:
  6538. pipe_config->pipe_bpp = 24;
  6539. break;
  6540. case PIPECONF_10BPC:
  6541. pipe_config->pipe_bpp = 30;
  6542. break;
  6543. case PIPECONF_12BPC:
  6544. pipe_config->pipe_bpp = 36;
  6545. break;
  6546. default:
  6547. break;
  6548. }
  6549. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6550. pipe_config->limited_color_range = true;
  6551. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6552. struct intel_shared_dpll *pll;
  6553. pipe_config->has_pch_encoder = true;
  6554. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6555. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6556. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6557. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6558. if (HAS_PCH_IBX(dev_priv->dev)) {
  6559. pipe_config->shared_dpll =
  6560. (enum intel_dpll_id) crtc->pipe;
  6561. } else {
  6562. tmp = I915_READ(PCH_DPLL_SEL);
  6563. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6564. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6565. else
  6566. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6567. }
  6568. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6569. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6570. &pipe_config->dpll_hw_state));
  6571. tmp = pipe_config->dpll_hw_state.dpll;
  6572. pipe_config->pixel_multiplier =
  6573. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6574. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6575. ironlake_pch_clock_get(crtc, pipe_config);
  6576. } else {
  6577. pipe_config->pixel_multiplier = 1;
  6578. }
  6579. intel_get_pipe_timings(crtc, pipe_config);
  6580. ironlake_get_pfit_config(crtc, pipe_config);
  6581. return true;
  6582. }
  6583. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6584. {
  6585. struct drm_device *dev = dev_priv->dev;
  6586. struct intel_crtc *crtc;
  6587. for_each_intel_crtc(dev, crtc)
  6588. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6589. pipe_name(crtc->pipe));
  6590. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6591. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  6592. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  6593. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  6594. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6595. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6596. "CPU PWM1 enabled\n");
  6597. if (IS_HASWELL(dev))
  6598. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6599. "CPU PWM2 enabled\n");
  6600. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6601. "PCH PWM1 enabled\n");
  6602. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6603. "Utility pin enabled\n");
  6604. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6605. /*
  6606. * In theory we can still leave IRQs enabled, as long as only the HPD
  6607. * interrupts remain enabled. We used to check for that, but since it's
  6608. * gen-specific and since we only disable LCPLL after we fully disable
  6609. * the interrupts, the check below should be enough.
  6610. */
  6611. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  6612. }
  6613. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  6614. {
  6615. struct drm_device *dev = dev_priv->dev;
  6616. if (IS_HASWELL(dev))
  6617. return I915_READ(D_COMP_HSW);
  6618. else
  6619. return I915_READ(D_COMP_BDW);
  6620. }
  6621. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6622. {
  6623. struct drm_device *dev = dev_priv->dev;
  6624. if (IS_HASWELL(dev)) {
  6625. mutex_lock(&dev_priv->rps.hw_lock);
  6626. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6627. val))
  6628. DRM_ERROR("Failed to write to D_COMP\n");
  6629. mutex_unlock(&dev_priv->rps.hw_lock);
  6630. } else {
  6631. I915_WRITE(D_COMP_BDW, val);
  6632. POSTING_READ(D_COMP_BDW);
  6633. }
  6634. }
  6635. /*
  6636. * This function implements pieces of two sequences from BSpec:
  6637. * - Sequence for display software to disable LCPLL
  6638. * - Sequence for display software to allow package C8+
  6639. * The steps implemented here are just the steps that actually touch the LCPLL
  6640. * register. Callers should take care of disabling all the display engine
  6641. * functions, doing the mode unset, fixing interrupts, etc.
  6642. */
  6643. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6644. bool switch_to_fclk, bool allow_power_down)
  6645. {
  6646. uint32_t val;
  6647. assert_can_disable_lcpll(dev_priv);
  6648. val = I915_READ(LCPLL_CTL);
  6649. if (switch_to_fclk) {
  6650. val |= LCPLL_CD_SOURCE_FCLK;
  6651. I915_WRITE(LCPLL_CTL, val);
  6652. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6653. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6654. DRM_ERROR("Switching to FCLK failed\n");
  6655. val = I915_READ(LCPLL_CTL);
  6656. }
  6657. val |= LCPLL_PLL_DISABLE;
  6658. I915_WRITE(LCPLL_CTL, val);
  6659. POSTING_READ(LCPLL_CTL);
  6660. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6661. DRM_ERROR("LCPLL still locked\n");
  6662. val = hsw_read_dcomp(dev_priv);
  6663. val |= D_COMP_COMP_DISABLE;
  6664. hsw_write_dcomp(dev_priv, val);
  6665. ndelay(100);
  6666. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  6667. 1))
  6668. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6669. if (allow_power_down) {
  6670. val = I915_READ(LCPLL_CTL);
  6671. val |= LCPLL_POWER_DOWN_ALLOW;
  6672. I915_WRITE(LCPLL_CTL, val);
  6673. POSTING_READ(LCPLL_CTL);
  6674. }
  6675. }
  6676. /*
  6677. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6678. * source.
  6679. */
  6680. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6681. {
  6682. uint32_t val;
  6683. val = I915_READ(LCPLL_CTL);
  6684. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6685. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6686. return;
  6687. /*
  6688. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6689. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6690. */
  6691. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  6692. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6693. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6694. I915_WRITE(LCPLL_CTL, val);
  6695. POSTING_READ(LCPLL_CTL);
  6696. }
  6697. val = hsw_read_dcomp(dev_priv);
  6698. val |= D_COMP_COMP_FORCE;
  6699. val &= ~D_COMP_COMP_DISABLE;
  6700. hsw_write_dcomp(dev_priv, val);
  6701. val = I915_READ(LCPLL_CTL);
  6702. val &= ~LCPLL_PLL_DISABLE;
  6703. I915_WRITE(LCPLL_CTL, val);
  6704. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6705. DRM_ERROR("LCPLL not locked yet\n");
  6706. if (val & LCPLL_CD_SOURCE_FCLK) {
  6707. val = I915_READ(LCPLL_CTL);
  6708. val &= ~LCPLL_CD_SOURCE_FCLK;
  6709. I915_WRITE(LCPLL_CTL, val);
  6710. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6711. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6712. DRM_ERROR("Switching back to LCPLL failed\n");
  6713. }
  6714. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  6715. }
  6716. /*
  6717. * Package states C8 and deeper are really deep PC states that can only be
  6718. * reached when all the devices on the system allow it, so even if the graphics
  6719. * device allows PC8+, it doesn't mean the system will actually get to these
  6720. * states. Our driver only allows PC8+ when going into runtime PM.
  6721. *
  6722. * The requirements for PC8+ are that all the outputs are disabled, the power
  6723. * well is disabled and most interrupts are disabled, and these are also
  6724. * requirements for runtime PM. When these conditions are met, we manually do
  6725. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6726. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6727. * hang the machine.
  6728. *
  6729. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6730. * the state of some registers, so when we come back from PC8+ we need to
  6731. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6732. * need to take care of the registers kept by RC6. Notice that this happens even
  6733. * if we don't put the device in PCI D3 state (which is what currently happens
  6734. * because of the runtime PM support).
  6735. *
  6736. * For more, read "Display Sequences for Package C8" on the hardware
  6737. * documentation.
  6738. */
  6739. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6740. {
  6741. struct drm_device *dev = dev_priv->dev;
  6742. uint32_t val;
  6743. DRM_DEBUG_KMS("Enabling package C8+\n");
  6744. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6745. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6746. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6747. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6748. }
  6749. lpt_disable_clkout_dp(dev);
  6750. hsw_disable_lcpll(dev_priv, true, true);
  6751. }
  6752. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6753. {
  6754. struct drm_device *dev = dev_priv->dev;
  6755. uint32_t val;
  6756. DRM_DEBUG_KMS("Disabling package C8+\n");
  6757. hsw_restore_lcpll(dev_priv);
  6758. lpt_init_pch_refclk(dev);
  6759. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6760. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6761. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6762. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6763. }
  6764. intel_prepare_ddi(dev);
  6765. }
  6766. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  6767. struct intel_crtc_state *crtc_state)
  6768. {
  6769. if (!intel_ddi_pll_select(crtc, crtc_state))
  6770. return -EINVAL;
  6771. crtc->lowfreq_avail = false;
  6772. return 0;
  6773. }
  6774. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  6775. enum port port,
  6776. struct intel_crtc_state *pipe_config)
  6777. {
  6778. u32 temp, dpll_ctl1;
  6779. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  6780. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  6781. switch (pipe_config->ddi_pll_sel) {
  6782. case SKL_DPLL0:
  6783. /*
  6784. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  6785. * of the shared DPLL framework and thus needs to be read out
  6786. * separately
  6787. */
  6788. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  6789. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  6790. break;
  6791. case SKL_DPLL1:
  6792. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  6793. break;
  6794. case SKL_DPLL2:
  6795. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  6796. break;
  6797. case SKL_DPLL3:
  6798. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  6799. break;
  6800. }
  6801. }
  6802. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  6803. enum port port,
  6804. struct intel_crtc_state *pipe_config)
  6805. {
  6806. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  6807. switch (pipe_config->ddi_pll_sel) {
  6808. case PORT_CLK_SEL_WRPLL1:
  6809. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  6810. break;
  6811. case PORT_CLK_SEL_WRPLL2:
  6812. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  6813. break;
  6814. }
  6815. }
  6816. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  6817. struct intel_crtc_state *pipe_config)
  6818. {
  6819. struct drm_device *dev = crtc->base.dev;
  6820. struct drm_i915_private *dev_priv = dev->dev_private;
  6821. struct intel_shared_dpll *pll;
  6822. enum port port;
  6823. uint32_t tmp;
  6824. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6825. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  6826. if (IS_SKYLAKE(dev))
  6827. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  6828. else
  6829. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  6830. if (pipe_config->shared_dpll >= 0) {
  6831. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6832. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6833. &pipe_config->dpll_hw_state));
  6834. }
  6835. /*
  6836. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6837. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6838. * the PCH transcoder is on.
  6839. */
  6840. if (INTEL_INFO(dev)->gen < 9 &&
  6841. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6842. pipe_config->has_pch_encoder = true;
  6843. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6844. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6845. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6846. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6847. }
  6848. }
  6849. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6850. struct intel_crtc_state *pipe_config)
  6851. {
  6852. struct drm_device *dev = crtc->base.dev;
  6853. struct drm_i915_private *dev_priv = dev->dev_private;
  6854. enum intel_display_power_domain pfit_domain;
  6855. uint32_t tmp;
  6856. if (!intel_display_power_is_enabled(dev_priv,
  6857. POWER_DOMAIN_PIPE(crtc->pipe)))
  6858. return false;
  6859. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6860. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6861. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6862. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6863. enum pipe trans_edp_pipe;
  6864. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6865. default:
  6866. WARN(1, "unknown pipe linked to edp transcoder\n");
  6867. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6868. case TRANS_DDI_EDP_INPUT_A_ON:
  6869. trans_edp_pipe = PIPE_A;
  6870. break;
  6871. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6872. trans_edp_pipe = PIPE_B;
  6873. break;
  6874. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6875. trans_edp_pipe = PIPE_C;
  6876. break;
  6877. }
  6878. if (trans_edp_pipe == crtc->pipe)
  6879. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6880. }
  6881. if (!intel_display_power_is_enabled(dev_priv,
  6882. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6883. return false;
  6884. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6885. if (!(tmp & PIPECONF_ENABLE))
  6886. return false;
  6887. haswell_get_ddi_port_state(crtc, pipe_config);
  6888. intel_get_pipe_timings(crtc, pipe_config);
  6889. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6890. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  6891. if (IS_SKYLAKE(dev))
  6892. skylake_get_pfit_config(crtc, pipe_config);
  6893. else
  6894. ironlake_get_pfit_config(crtc, pipe_config);
  6895. }
  6896. if (IS_HASWELL(dev))
  6897. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6898. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6899. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  6900. pipe_config->pixel_multiplier =
  6901. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  6902. } else {
  6903. pipe_config->pixel_multiplier = 1;
  6904. }
  6905. return true;
  6906. }
  6907. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6908. {
  6909. struct drm_device *dev = crtc->dev;
  6910. struct drm_i915_private *dev_priv = dev->dev_private;
  6911. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6912. uint32_t cntl = 0, size = 0;
  6913. if (base) {
  6914. unsigned int width = intel_crtc->cursor_width;
  6915. unsigned int height = intel_crtc->cursor_height;
  6916. unsigned int stride = roundup_pow_of_two(width) * 4;
  6917. switch (stride) {
  6918. default:
  6919. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  6920. width, stride);
  6921. stride = 256;
  6922. /* fallthrough */
  6923. case 256:
  6924. case 512:
  6925. case 1024:
  6926. case 2048:
  6927. break;
  6928. }
  6929. cntl |= CURSOR_ENABLE |
  6930. CURSOR_GAMMA_ENABLE |
  6931. CURSOR_FORMAT_ARGB |
  6932. CURSOR_STRIDE(stride);
  6933. size = (height << 12) | width;
  6934. }
  6935. if (intel_crtc->cursor_cntl != 0 &&
  6936. (intel_crtc->cursor_base != base ||
  6937. intel_crtc->cursor_size != size ||
  6938. intel_crtc->cursor_cntl != cntl)) {
  6939. /* On these chipsets we can only modify the base/size/stride
  6940. * whilst the cursor is disabled.
  6941. */
  6942. I915_WRITE(_CURACNTR, 0);
  6943. POSTING_READ(_CURACNTR);
  6944. intel_crtc->cursor_cntl = 0;
  6945. }
  6946. if (intel_crtc->cursor_base != base) {
  6947. I915_WRITE(_CURABASE, base);
  6948. intel_crtc->cursor_base = base;
  6949. }
  6950. if (intel_crtc->cursor_size != size) {
  6951. I915_WRITE(CURSIZE, size);
  6952. intel_crtc->cursor_size = size;
  6953. }
  6954. if (intel_crtc->cursor_cntl != cntl) {
  6955. I915_WRITE(_CURACNTR, cntl);
  6956. POSTING_READ(_CURACNTR);
  6957. intel_crtc->cursor_cntl = cntl;
  6958. }
  6959. }
  6960. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6961. {
  6962. struct drm_device *dev = crtc->dev;
  6963. struct drm_i915_private *dev_priv = dev->dev_private;
  6964. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6965. int pipe = intel_crtc->pipe;
  6966. uint32_t cntl;
  6967. cntl = 0;
  6968. if (base) {
  6969. cntl = MCURSOR_GAMMA_ENABLE;
  6970. switch (intel_crtc->cursor_width) {
  6971. case 64:
  6972. cntl |= CURSOR_MODE_64_ARGB_AX;
  6973. break;
  6974. case 128:
  6975. cntl |= CURSOR_MODE_128_ARGB_AX;
  6976. break;
  6977. case 256:
  6978. cntl |= CURSOR_MODE_256_ARGB_AX;
  6979. break;
  6980. default:
  6981. MISSING_CASE(intel_crtc->cursor_width);
  6982. return;
  6983. }
  6984. cntl |= pipe << 28; /* Connect to correct pipe */
  6985. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6986. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6987. }
  6988. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  6989. cntl |= CURSOR_ROTATE_180;
  6990. if (intel_crtc->cursor_cntl != cntl) {
  6991. I915_WRITE(CURCNTR(pipe), cntl);
  6992. POSTING_READ(CURCNTR(pipe));
  6993. intel_crtc->cursor_cntl = cntl;
  6994. }
  6995. /* and commit changes on next vblank */
  6996. I915_WRITE(CURBASE(pipe), base);
  6997. POSTING_READ(CURBASE(pipe));
  6998. intel_crtc->cursor_base = base;
  6999. }
  7000. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  7001. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  7002. bool on)
  7003. {
  7004. struct drm_device *dev = crtc->dev;
  7005. struct drm_i915_private *dev_priv = dev->dev_private;
  7006. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7007. int pipe = intel_crtc->pipe;
  7008. int x = crtc->cursor_x;
  7009. int y = crtc->cursor_y;
  7010. u32 base = 0, pos = 0;
  7011. if (on)
  7012. base = intel_crtc->cursor_addr;
  7013. if (x >= intel_crtc->config->pipe_src_w)
  7014. base = 0;
  7015. if (y >= intel_crtc->config->pipe_src_h)
  7016. base = 0;
  7017. if (x < 0) {
  7018. if (x + intel_crtc->cursor_width <= 0)
  7019. base = 0;
  7020. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7021. x = -x;
  7022. }
  7023. pos |= x << CURSOR_X_SHIFT;
  7024. if (y < 0) {
  7025. if (y + intel_crtc->cursor_height <= 0)
  7026. base = 0;
  7027. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7028. y = -y;
  7029. }
  7030. pos |= y << CURSOR_Y_SHIFT;
  7031. if (base == 0 && intel_crtc->cursor_base == 0)
  7032. return;
  7033. I915_WRITE(CURPOS(pipe), pos);
  7034. /* ILK+ do this automagically */
  7035. if (HAS_GMCH_DISPLAY(dev) &&
  7036. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  7037. base += (intel_crtc->cursor_height *
  7038. intel_crtc->cursor_width - 1) * 4;
  7039. }
  7040. if (IS_845G(dev) || IS_I865G(dev))
  7041. i845_update_cursor(crtc, base);
  7042. else
  7043. i9xx_update_cursor(crtc, base);
  7044. }
  7045. static bool cursor_size_ok(struct drm_device *dev,
  7046. uint32_t width, uint32_t height)
  7047. {
  7048. if (width == 0 || height == 0)
  7049. return false;
  7050. /*
  7051. * 845g/865g are special in that they are only limited by
  7052. * the width of their cursors, the height is arbitrary up to
  7053. * the precision of the register. Everything else requires
  7054. * square cursors, limited to a few power-of-two sizes.
  7055. */
  7056. if (IS_845G(dev) || IS_I865G(dev)) {
  7057. if ((width & 63) != 0)
  7058. return false;
  7059. if (width > (IS_845G(dev) ? 64 : 512))
  7060. return false;
  7061. if (height > 1023)
  7062. return false;
  7063. } else {
  7064. switch (width | height) {
  7065. case 256:
  7066. case 128:
  7067. if (IS_GEN2(dev))
  7068. return false;
  7069. case 64:
  7070. break;
  7071. default:
  7072. return false;
  7073. }
  7074. }
  7075. return true;
  7076. }
  7077. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  7078. u16 *blue, uint32_t start, uint32_t size)
  7079. {
  7080. int end = (start + size > 256) ? 256 : start + size, i;
  7081. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7082. for (i = start; i < end; i++) {
  7083. intel_crtc->lut_r[i] = red[i] >> 8;
  7084. intel_crtc->lut_g[i] = green[i] >> 8;
  7085. intel_crtc->lut_b[i] = blue[i] >> 8;
  7086. }
  7087. intel_crtc_load_lut(crtc);
  7088. }
  7089. /* VESA 640x480x72Hz mode to set on the pipe */
  7090. static struct drm_display_mode load_detect_mode = {
  7091. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7092. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7093. };
  7094. struct drm_framebuffer *
  7095. __intel_framebuffer_create(struct drm_device *dev,
  7096. struct drm_mode_fb_cmd2 *mode_cmd,
  7097. struct drm_i915_gem_object *obj)
  7098. {
  7099. struct intel_framebuffer *intel_fb;
  7100. int ret;
  7101. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7102. if (!intel_fb) {
  7103. drm_gem_object_unreference(&obj->base);
  7104. return ERR_PTR(-ENOMEM);
  7105. }
  7106. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  7107. if (ret)
  7108. goto err;
  7109. return &intel_fb->base;
  7110. err:
  7111. drm_gem_object_unreference(&obj->base);
  7112. kfree(intel_fb);
  7113. return ERR_PTR(ret);
  7114. }
  7115. static struct drm_framebuffer *
  7116. intel_framebuffer_create(struct drm_device *dev,
  7117. struct drm_mode_fb_cmd2 *mode_cmd,
  7118. struct drm_i915_gem_object *obj)
  7119. {
  7120. struct drm_framebuffer *fb;
  7121. int ret;
  7122. ret = i915_mutex_lock_interruptible(dev);
  7123. if (ret)
  7124. return ERR_PTR(ret);
  7125. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  7126. mutex_unlock(&dev->struct_mutex);
  7127. return fb;
  7128. }
  7129. static u32
  7130. intel_framebuffer_pitch_for_width(int width, int bpp)
  7131. {
  7132. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7133. return ALIGN(pitch, 64);
  7134. }
  7135. static u32
  7136. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7137. {
  7138. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7139. return PAGE_ALIGN(pitch * mode->vdisplay);
  7140. }
  7141. static struct drm_framebuffer *
  7142. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7143. struct drm_display_mode *mode,
  7144. int depth, int bpp)
  7145. {
  7146. struct drm_i915_gem_object *obj;
  7147. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7148. obj = i915_gem_alloc_object(dev,
  7149. intel_framebuffer_size_for_mode(mode, bpp));
  7150. if (obj == NULL)
  7151. return ERR_PTR(-ENOMEM);
  7152. mode_cmd.width = mode->hdisplay;
  7153. mode_cmd.height = mode->vdisplay;
  7154. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7155. bpp);
  7156. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7157. return intel_framebuffer_create(dev, &mode_cmd, obj);
  7158. }
  7159. static struct drm_framebuffer *
  7160. mode_fits_in_fbdev(struct drm_device *dev,
  7161. struct drm_display_mode *mode)
  7162. {
  7163. #ifdef CONFIG_DRM_I915_FBDEV
  7164. struct drm_i915_private *dev_priv = dev->dev_private;
  7165. struct drm_i915_gem_object *obj;
  7166. struct drm_framebuffer *fb;
  7167. if (!dev_priv->fbdev)
  7168. return NULL;
  7169. if (!dev_priv->fbdev->fb)
  7170. return NULL;
  7171. obj = dev_priv->fbdev->fb->obj;
  7172. BUG_ON(!obj);
  7173. fb = &dev_priv->fbdev->fb->base;
  7174. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7175. fb->bits_per_pixel))
  7176. return NULL;
  7177. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7178. return NULL;
  7179. return fb;
  7180. #else
  7181. return NULL;
  7182. #endif
  7183. }
  7184. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7185. struct drm_display_mode *mode,
  7186. struct intel_load_detect_pipe *old,
  7187. struct drm_modeset_acquire_ctx *ctx)
  7188. {
  7189. struct intel_crtc *intel_crtc;
  7190. struct intel_encoder *intel_encoder =
  7191. intel_attached_encoder(connector);
  7192. struct drm_crtc *possible_crtc;
  7193. struct drm_encoder *encoder = &intel_encoder->base;
  7194. struct drm_crtc *crtc = NULL;
  7195. struct drm_device *dev = encoder->dev;
  7196. struct drm_framebuffer *fb;
  7197. struct drm_mode_config *config = &dev->mode_config;
  7198. int ret, i = -1;
  7199. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7200. connector->base.id, connector->name,
  7201. encoder->base.id, encoder->name);
  7202. retry:
  7203. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7204. if (ret)
  7205. goto fail_unlock;
  7206. /*
  7207. * Algorithm gets a little messy:
  7208. *
  7209. * - if the connector already has an assigned crtc, use it (but make
  7210. * sure it's on first)
  7211. *
  7212. * - try to find the first unused crtc that can drive this connector,
  7213. * and use that if we find one
  7214. */
  7215. /* See if we already have a CRTC for this connector */
  7216. if (encoder->crtc) {
  7217. crtc = encoder->crtc;
  7218. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7219. if (ret)
  7220. goto fail_unlock;
  7221. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7222. if (ret)
  7223. goto fail_unlock;
  7224. old->dpms_mode = connector->dpms;
  7225. old->load_detect_temp = false;
  7226. /* Make sure the crtc and connector are running */
  7227. if (connector->dpms != DRM_MODE_DPMS_ON)
  7228. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  7229. return true;
  7230. }
  7231. /* Find an unused one (if possible) */
  7232. for_each_crtc(dev, possible_crtc) {
  7233. i++;
  7234. if (!(encoder->possible_crtcs & (1 << i)))
  7235. continue;
  7236. if (possible_crtc->enabled)
  7237. continue;
  7238. /* This can occur when applying the pipe A quirk on resume. */
  7239. if (to_intel_crtc(possible_crtc)->new_enabled)
  7240. continue;
  7241. crtc = possible_crtc;
  7242. break;
  7243. }
  7244. /*
  7245. * If we didn't find an unused CRTC, don't use any.
  7246. */
  7247. if (!crtc) {
  7248. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7249. goto fail_unlock;
  7250. }
  7251. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7252. if (ret)
  7253. goto fail_unlock;
  7254. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7255. if (ret)
  7256. goto fail_unlock;
  7257. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7258. to_intel_connector(connector)->new_encoder = intel_encoder;
  7259. intel_crtc = to_intel_crtc(crtc);
  7260. intel_crtc->new_enabled = true;
  7261. intel_crtc->new_config = intel_crtc->config;
  7262. old->dpms_mode = connector->dpms;
  7263. old->load_detect_temp = true;
  7264. old->release_fb = NULL;
  7265. if (!mode)
  7266. mode = &load_detect_mode;
  7267. /* We need a framebuffer large enough to accommodate all accesses
  7268. * that the plane may generate whilst we perform load detection.
  7269. * We can not rely on the fbcon either being present (we get called
  7270. * during its initialisation to detect all boot displays, or it may
  7271. * not even exist) or that it is large enough to satisfy the
  7272. * requested mode.
  7273. */
  7274. fb = mode_fits_in_fbdev(dev, mode);
  7275. if (fb == NULL) {
  7276. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7277. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7278. old->release_fb = fb;
  7279. } else
  7280. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7281. if (IS_ERR(fb)) {
  7282. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7283. goto fail;
  7284. }
  7285. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7286. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7287. if (old->release_fb)
  7288. old->release_fb->funcs->destroy(old->release_fb);
  7289. goto fail;
  7290. }
  7291. /* let the connector get through one full cycle before testing */
  7292. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7293. return true;
  7294. fail:
  7295. intel_crtc->new_enabled = crtc->enabled;
  7296. if (intel_crtc->new_enabled)
  7297. intel_crtc->new_config = intel_crtc->config;
  7298. else
  7299. intel_crtc->new_config = NULL;
  7300. fail_unlock:
  7301. if (ret == -EDEADLK) {
  7302. drm_modeset_backoff(ctx);
  7303. goto retry;
  7304. }
  7305. return false;
  7306. }
  7307. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7308. struct intel_load_detect_pipe *old)
  7309. {
  7310. struct intel_encoder *intel_encoder =
  7311. intel_attached_encoder(connector);
  7312. struct drm_encoder *encoder = &intel_encoder->base;
  7313. struct drm_crtc *crtc = encoder->crtc;
  7314. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7315. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7316. connector->base.id, connector->name,
  7317. encoder->base.id, encoder->name);
  7318. if (old->load_detect_temp) {
  7319. to_intel_connector(connector)->new_encoder = NULL;
  7320. intel_encoder->new_crtc = NULL;
  7321. intel_crtc->new_enabled = false;
  7322. intel_crtc->new_config = NULL;
  7323. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7324. if (old->release_fb) {
  7325. drm_framebuffer_unregister_private(old->release_fb);
  7326. drm_framebuffer_unreference(old->release_fb);
  7327. }
  7328. return;
  7329. }
  7330. /* Switch crtc and encoder back off if necessary */
  7331. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7332. connector->funcs->dpms(connector, old->dpms_mode);
  7333. }
  7334. static int i9xx_pll_refclk(struct drm_device *dev,
  7335. const struct intel_crtc_state *pipe_config)
  7336. {
  7337. struct drm_i915_private *dev_priv = dev->dev_private;
  7338. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7339. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7340. return dev_priv->vbt.lvds_ssc_freq;
  7341. else if (HAS_PCH_SPLIT(dev))
  7342. return 120000;
  7343. else if (!IS_GEN2(dev))
  7344. return 96000;
  7345. else
  7346. return 48000;
  7347. }
  7348. /* Returns the clock of the currently programmed mode of the given pipe. */
  7349. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7350. struct intel_crtc_state *pipe_config)
  7351. {
  7352. struct drm_device *dev = crtc->base.dev;
  7353. struct drm_i915_private *dev_priv = dev->dev_private;
  7354. int pipe = pipe_config->cpu_transcoder;
  7355. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7356. u32 fp;
  7357. intel_clock_t clock;
  7358. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7359. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7360. fp = pipe_config->dpll_hw_state.fp0;
  7361. else
  7362. fp = pipe_config->dpll_hw_state.fp1;
  7363. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7364. if (IS_PINEVIEW(dev)) {
  7365. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7366. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7367. } else {
  7368. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7369. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7370. }
  7371. if (!IS_GEN2(dev)) {
  7372. if (IS_PINEVIEW(dev))
  7373. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7374. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7375. else
  7376. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7377. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7378. switch (dpll & DPLL_MODE_MASK) {
  7379. case DPLLB_MODE_DAC_SERIAL:
  7380. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7381. 5 : 10;
  7382. break;
  7383. case DPLLB_MODE_LVDS:
  7384. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7385. 7 : 14;
  7386. break;
  7387. default:
  7388. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7389. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7390. return;
  7391. }
  7392. if (IS_PINEVIEW(dev))
  7393. pineview_clock(refclk, &clock);
  7394. else
  7395. i9xx_clock(refclk, &clock);
  7396. } else {
  7397. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7398. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7399. if (is_lvds) {
  7400. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7401. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7402. if (lvds & LVDS_CLKB_POWER_UP)
  7403. clock.p2 = 7;
  7404. else
  7405. clock.p2 = 14;
  7406. } else {
  7407. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7408. clock.p1 = 2;
  7409. else {
  7410. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7411. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7412. }
  7413. if (dpll & PLL_P2_DIVIDE_BY_4)
  7414. clock.p2 = 4;
  7415. else
  7416. clock.p2 = 2;
  7417. }
  7418. i9xx_clock(refclk, &clock);
  7419. }
  7420. /*
  7421. * This value includes pixel_multiplier. We will use
  7422. * port_clock to compute adjusted_mode.crtc_clock in the
  7423. * encoder's get_config() function.
  7424. */
  7425. pipe_config->port_clock = clock.dot;
  7426. }
  7427. int intel_dotclock_calculate(int link_freq,
  7428. const struct intel_link_m_n *m_n)
  7429. {
  7430. /*
  7431. * The calculation for the data clock is:
  7432. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7433. * But we want to avoid losing precison if possible, so:
  7434. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7435. *
  7436. * and the link clock is simpler:
  7437. * link_clock = (m * link_clock) / n
  7438. */
  7439. if (!m_n->link_n)
  7440. return 0;
  7441. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7442. }
  7443. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7444. struct intel_crtc_state *pipe_config)
  7445. {
  7446. struct drm_device *dev = crtc->base.dev;
  7447. /* read out port_clock from the DPLL */
  7448. i9xx_crtc_clock_get(crtc, pipe_config);
  7449. /*
  7450. * This value does not include pixel_multiplier.
  7451. * We will check that port_clock and adjusted_mode.crtc_clock
  7452. * agree once we know their relationship in the encoder's
  7453. * get_config() function.
  7454. */
  7455. pipe_config->base.adjusted_mode.crtc_clock =
  7456. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7457. &pipe_config->fdi_m_n);
  7458. }
  7459. /** Returns the currently programmed mode of the given pipe. */
  7460. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7461. struct drm_crtc *crtc)
  7462. {
  7463. struct drm_i915_private *dev_priv = dev->dev_private;
  7464. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7465. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7466. struct drm_display_mode *mode;
  7467. struct intel_crtc_state pipe_config;
  7468. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7469. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7470. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7471. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7472. enum pipe pipe = intel_crtc->pipe;
  7473. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7474. if (!mode)
  7475. return NULL;
  7476. /*
  7477. * Construct a pipe_config sufficient for getting the clock info
  7478. * back out of crtc_clock_get.
  7479. *
  7480. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7481. * to use a real value here instead.
  7482. */
  7483. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7484. pipe_config.pixel_multiplier = 1;
  7485. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7486. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7487. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7488. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7489. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7490. mode->hdisplay = (htot & 0xffff) + 1;
  7491. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7492. mode->hsync_start = (hsync & 0xffff) + 1;
  7493. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7494. mode->vdisplay = (vtot & 0xffff) + 1;
  7495. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7496. mode->vsync_start = (vsync & 0xffff) + 1;
  7497. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7498. drm_mode_set_name(mode);
  7499. return mode;
  7500. }
  7501. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7502. {
  7503. struct drm_device *dev = crtc->dev;
  7504. struct drm_i915_private *dev_priv = dev->dev_private;
  7505. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7506. if (!HAS_GMCH_DISPLAY(dev))
  7507. return;
  7508. if (!dev_priv->lvds_downclock_avail)
  7509. return;
  7510. /*
  7511. * Since this is called by a timer, we should never get here in
  7512. * the manual case.
  7513. */
  7514. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7515. int pipe = intel_crtc->pipe;
  7516. int dpll_reg = DPLL(pipe);
  7517. int dpll;
  7518. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7519. assert_panel_unlocked(dev_priv, pipe);
  7520. dpll = I915_READ(dpll_reg);
  7521. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7522. I915_WRITE(dpll_reg, dpll);
  7523. intel_wait_for_vblank(dev, pipe);
  7524. dpll = I915_READ(dpll_reg);
  7525. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7526. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7527. }
  7528. }
  7529. void intel_mark_busy(struct drm_device *dev)
  7530. {
  7531. struct drm_i915_private *dev_priv = dev->dev_private;
  7532. if (dev_priv->mm.busy)
  7533. return;
  7534. intel_runtime_pm_get(dev_priv);
  7535. i915_update_gfx_val(dev_priv);
  7536. dev_priv->mm.busy = true;
  7537. }
  7538. void intel_mark_idle(struct drm_device *dev)
  7539. {
  7540. struct drm_i915_private *dev_priv = dev->dev_private;
  7541. struct drm_crtc *crtc;
  7542. if (!dev_priv->mm.busy)
  7543. return;
  7544. dev_priv->mm.busy = false;
  7545. if (!i915.powersave)
  7546. goto out;
  7547. for_each_crtc(dev, crtc) {
  7548. if (!crtc->primary->fb)
  7549. continue;
  7550. intel_decrease_pllclock(crtc);
  7551. }
  7552. if (INTEL_INFO(dev)->gen >= 6)
  7553. gen6_rps_idle(dev->dev_private);
  7554. out:
  7555. intel_runtime_pm_put(dev_priv);
  7556. }
  7557. static void intel_crtc_set_state(struct intel_crtc *crtc,
  7558. struct intel_crtc_state *crtc_state)
  7559. {
  7560. kfree(crtc->config);
  7561. crtc->config = crtc_state;
  7562. crtc->base.state = &crtc_state->base;
  7563. }
  7564. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7565. {
  7566. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7567. struct drm_device *dev = crtc->dev;
  7568. struct intel_unpin_work *work;
  7569. spin_lock_irq(&dev->event_lock);
  7570. work = intel_crtc->unpin_work;
  7571. intel_crtc->unpin_work = NULL;
  7572. spin_unlock_irq(&dev->event_lock);
  7573. if (work) {
  7574. cancel_work_sync(&work->work);
  7575. kfree(work);
  7576. }
  7577. intel_crtc_set_state(intel_crtc, NULL);
  7578. drm_crtc_cleanup(crtc);
  7579. kfree(intel_crtc);
  7580. }
  7581. static void intel_unpin_work_fn(struct work_struct *__work)
  7582. {
  7583. struct intel_unpin_work *work =
  7584. container_of(__work, struct intel_unpin_work, work);
  7585. struct drm_device *dev = work->crtc->dev;
  7586. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  7587. mutex_lock(&dev->struct_mutex);
  7588. intel_unpin_fb_obj(work->old_fb_obj);
  7589. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7590. drm_gem_object_unreference(&work->old_fb_obj->base);
  7591. intel_fbc_update(dev);
  7592. if (work->flip_queued_req)
  7593. i915_gem_request_assign(&work->flip_queued_req, NULL);
  7594. mutex_unlock(&dev->struct_mutex);
  7595. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  7596. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7597. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7598. kfree(work);
  7599. }
  7600. static void do_intel_finish_page_flip(struct drm_device *dev,
  7601. struct drm_crtc *crtc)
  7602. {
  7603. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7604. struct intel_unpin_work *work;
  7605. unsigned long flags;
  7606. /* Ignore early vblank irqs */
  7607. if (intel_crtc == NULL)
  7608. return;
  7609. /*
  7610. * This is called both by irq handlers and the reset code (to complete
  7611. * lost pageflips) so needs the full irqsave spinlocks.
  7612. */
  7613. spin_lock_irqsave(&dev->event_lock, flags);
  7614. work = intel_crtc->unpin_work;
  7615. /* Ensure we don't miss a work->pending update ... */
  7616. smp_rmb();
  7617. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7618. spin_unlock_irqrestore(&dev->event_lock, flags);
  7619. return;
  7620. }
  7621. page_flip_completed(intel_crtc);
  7622. spin_unlock_irqrestore(&dev->event_lock, flags);
  7623. }
  7624. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7625. {
  7626. struct drm_i915_private *dev_priv = dev->dev_private;
  7627. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7628. do_intel_finish_page_flip(dev, crtc);
  7629. }
  7630. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7631. {
  7632. struct drm_i915_private *dev_priv = dev->dev_private;
  7633. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7634. do_intel_finish_page_flip(dev, crtc);
  7635. }
  7636. /* Is 'a' after or equal to 'b'? */
  7637. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7638. {
  7639. return !((a - b) & 0x80000000);
  7640. }
  7641. static bool page_flip_finished(struct intel_crtc *crtc)
  7642. {
  7643. struct drm_device *dev = crtc->base.dev;
  7644. struct drm_i915_private *dev_priv = dev->dev_private;
  7645. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  7646. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  7647. return true;
  7648. /*
  7649. * The relevant registers doen't exist on pre-ctg.
  7650. * As the flip done interrupt doesn't trigger for mmio
  7651. * flips on gmch platforms, a flip count check isn't
  7652. * really needed there. But since ctg has the registers,
  7653. * include it in the check anyway.
  7654. */
  7655. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7656. return true;
  7657. /*
  7658. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7659. * used the same base address. In that case the mmio flip might
  7660. * have completed, but the CS hasn't even executed the flip yet.
  7661. *
  7662. * A flip count check isn't enough as the CS might have updated
  7663. * the base address just after start of vblank, but before we
  7664. * managed to process the interrupt. This means we'd complete the
  7665. * CS flip too soon.
  7666. *
  7667. * Combining both checks should get us a good enough result. It may
  7668. * still happen that the CS flip has been executed, but has not
  7669. * yet actually completed. But in case the base address is the same
  7670. * anyway, we don't really care.
  7671. */
  7672. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7673. crtc->unpin_work->gtt_offset &&
  7674. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7675. crtc->unpin_work->flip_count);
  7676. }
  7677. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7678. {
  7679. struct drm_i915_private *dev_priv = dev->dev_private;
  7680. struct intel_crtc *intel_crtc =
  7681. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7682. unsigned long flags;
  7683. /*
  7684. * This is called both by irq handlers and the reset code (to complete
  7685. * lost pageflips) so needs the full irqsave spinlocks.
  7686. *
  7687. * NB: An MMIO update of the plane base pointer will also
  7688. * generate a page-flip completion irq, i.e. every modeset
  7689. * is also accompanied by a spurious intel_prepare_page_flip().
  7690. */
  7691. spin_lock_irqsave(&dev->event_lock, flags);
  7692. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7693. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7694. spin_unlock_irqrestore(&dev->event_lock, flags);
  7695. }
  7696. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7697. {
  7698. /* Ensure that the work item is consistent when activating it ... */
  7699. smp_wmb();
  7700. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7701. /* and that it is marked active as soon as the irq could fire. */
  7702. smp_wmb();
  7703. }
  7704. static int intel_gen2_queue_flip(struct drm_device *dev,
  7705. struct drm_crtc *crtc,
  7706. struct drm_framebuffer *fb,
  7707. struct drm_i915_gem_object *obj,
  7708. struct intel_engine_cs *ring,
  7709. uint32_t flags)
  7710. {
  7711. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7712. u32 flip_mask;
  7713. int ret;
  7714. ret = intel_ring_begin(ring, 6);
  7715. if (ret)
  7716. return ret;
  7717. /* Can't queue multiple flips, so wait for the previous
  7718. * one to finish before executing the next.
  7719. */
  7720. if (intel_crtc->plane)
  7721. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7722. else
  7723. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7724. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7725. intel_ring_emit(ring, MI_NOOP);
  7726. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7727. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7728. intel_ring_emit(ring, fb->pitches[0]);
  7729. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7730. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7731. intel_mark_page_flip_active(intel_crtc);
  7732. __intel_ring_advance(ring);
  7733. return 0;
  7734. }
  7735. static int intel_gen3_queue_flip(struct drm_device *dev,
  7736. struct drm_crtc *crtc,
  7737. struct drm_framebuffer *fb,
  7738. struct drm_i915_gem_object *obj,
  7739. struct intel_engine_cs *ring,
  7740. uint32_t flags)
  7741. {
  7742. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7743. u32 flip_mask;
  7744. int ret;
  7745. ret = intel_ring_begin(ring, 6);
  7746. if (ret)
  7747. return ret;
  7748. if (intel_crtc->plane)
  7749. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7750. else
  7751. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7752. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7753. intel_ring_emit(ring, MI_NOOP);
  7754. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7755. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7756. intel_ring_emit(ring, fb->pitches[0]);
  7757. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7758. intel_ring_emit(ring, MI_NOOP);
  7759. intel_mark_page_flip_active(intel_crtc);
  7760. __intel_ring_advance(ring);
  7761. return 0;
  7762. }
  7763. static int intel_gen4_queue_flip(struct drm_device *dev,
  7764. struct drm_crtc *crtc,
  7765. struct drm_framebuffer *fb,
  7766. struct drm_i915_gem_object *obj,
  7767. struct intel_engine_cs *ring,
  7768. uint32_t flags)
  7769. {
  7770. struct drm_i915_private *dev_priv = dev->dev_private;
  7771. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7772. uint32_t pf, pipesrc;
  7773. int ret;
  7774. ret = intel_ring_begin(ring, 4);
  7775. if (ret)
  7776. return ret;
  7777. /* i965+ uses the linear or tiled offsets from the
  7778. * Display Registers (which do not change across a page-flip)
  7779. * so we need only reprogram the base address.
  7780. */
  7781. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7782. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7783. intel_ring_emit(ring, fb->pitches[0]);
  7784. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7785. obj->tiling_mode);
  7786. /* XXX Enabling the panel-fitter across page-flip is so far
  7787. * untested on non-native modes, so ignore it for now.
  7788. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7789. */
  7790. pf = 0;
  7791. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7792. intel_ring_emit(ring, pf | pipesrc);
  7793. intel_mark_page_flip_active(intel_crtc);
  7794. __intel_ring_advance(ring);
  7795. return 0;
  7796. }
  7797. static int intel_gen6_queue_flip(struct drm_device *dev,
  7798. struct drm_crtc *crtc,
  7799. struct drm_framebuffer *fb,
  7800. struct drm_i915_gem_object *obj,
  7801. struct intel_engine_cs *ring,
  7802. uint32_t flags)
  7803. {
  7804. struct drm_i915_private *dev_priv = dev->dev_private;
  7805. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7806. uint32_t pf, pipesrc;
  7807. int ret;
  7808. ret = intel_ring_begin(ring, 4);
  7809. if (ret)
  7810. return ret;
  7811. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7812. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7813. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7814. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7815. /* Contrary to the suggestions in the documentation,
  7816. * "Enable Panel Fitter" does not seem to be required when page
  7817. * flipping with a non-native mode, and worse causes a normal
  7818. * modeset to fail.
  7819. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7820. */
  7821. pf = 0;
  7822. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7823. intel_ring_emit(ring, pf | pipesrc);
  7824. intel_mark_page_flip_active(intel_crtc);
  7825. __intel_ring_advance(ring);
  7826. return 0;
  7827. }
  7828. static int intel_gen7_queue_flip(struct drm_device *dev,
  7829. struct drm_crtc *crtc,
  7830. struct drm_framebuffer *fb,
  7831. struct drm_i915_gem_object *obj,
  7832. struct intel_engine_cs *ring,
  7833. uint32_t flags)
  7834. {
  7835. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7836. uint32_t plane_bit = 0;
  7837. int len, ret;
  7838. switch (intel_crtc->plane) {
  7839. case PLANE_A:
  7840. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7841. break;
  7842. case PLANE_B:
  7843. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7844. break;
  7845. case PLANE_C:
  7846. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7847. break;
  7848. default:
  7849. WARN_ONCE(1, "unknown plane in flip command\n");
  7850. return -ENODEV;
  7851. }
  7852. len = 4;
  7853. if (ring->id == RCS) {
  7854. len += 6;
  7855. /*
  7856. * On Gen 8, SRM is now taking an extra dword to accommodate
  7857. * 48bits addresses, and we need a NOOP for the batch size to
  7858. * stay even.
  7859. */
  7860. if (IS_GEN8(dev))
  7861. len += 2;
  7862. }
  7863. /*
  7864. * BSpec MI_DISPLAY_FLIP for IVB:
  7865. * "The full packet must be contained within the same cache line."
  7866. *
  7867. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7868. * cacheline, if we ever start emitting more commands before
  7869. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7870. * then do the cacheline alignment, and finally emit the
  7871. * MI_DISPLAY_FLIP.
  7872. */
  7873. ret = intel_ring_cacheline_align(ring);
  7874. if (ret)
  7875. return ret;
  7876. ret = intel_ring_begin(ring, len);
  7877. if (ret)
  7878. return ret;
  7879. /* Unmask the flip-done completion message. Note that the bspec says that
  7880. * we should do this for both the BCS and RCS, and that we must not unmask
  7881. * more than one flip event at any time (or ensure that one flip message
  7882. * can be sent by waiting for flip-done prior to queueing new flips).
  7883. * Experimentation says that BCS works despite DERRMR masking all
  7884. * flip-done completion events and that unmasking all planes at once
  7885. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7886. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7887. */
  7888. if (ring->id == RCS) {
  7889. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7890. intel_ring_emit(ring, DERRMR);
  7891. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7892. DERRMR_PIPEB_PRI_FLIP_DONE |
  7893. DERRMR_PIPEC_PRI_FLIP_DONE));
  7894. if (IS_GEN8(dev))
  7895. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7896. MI_SRM_LRM_GLOBAL_GTT);
  7897. else
  7898. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7899. MI_SRM_LRM_GLOBAL_GTT);
  7900. intel_ring_emit(ring, DERRMR);
  7901. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7902. if (IS_GEN8(dev)) {
  7903. intel_ring_emit(ring, 0);
  7904. intel_ring_emit(ring, MI_NOOP);
  7905. }
  7906. }
  7907. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7908. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7909. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7910. intel_ring_emit(ring, (MI_NOOP));
  7911. intel_mark_page_flip_active(intel_crtc);
  7912. __intel_ring_advance(ring);
  7913. return 0;
  7914. }
  7915. static bool use_mmio_flip(struct intel_engine_cs *ring,
  7916. struct drm_i915_gem_object *obj)
  7917. {
  7918. /*
  7919. * This is not being used for older platforms, because
  7920. * non-availability of flip done interrupt forces us to use
  7921. * CS flips. Older platforms derive flip done using some clever
  7922. * tricks involving the flip_pending status bits and vblank irqs.
  7923. * So using MMIO flips there would disrupt this mechanism.
  7924. */
  7925. if (ring == NULL)
  7926. return true;
  7927. if (INTEL_INFO(ring->dev)->gen < 5)
  7928. return false;
  7929. if (i915.use_mmio_flip < 0)
  7930. return false;
  7931. else if (i915.use_mmio_flip > 0)
  7932. return true;
  7933. else if (i915.enable_execlists)
  7934. return true;
  7935. else
  7936. return ring != i915_gem_request_get_ring(obj->last_read_req);
  7937. }
  7938. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  7939. {
  7940. struct drm_device *dev = intel_crtc->base.dev;
  7941. struct drm_i915_private *dev_priv = dev->dev_private;
  7942. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  7943. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  7944. struct drm_i915_gem_object *obj = intel_fb->obj;
  7945. const enum pipe pipe = intel_crtc->pipe;
  7946. u32 ctl, stride;
  7947. ctl = I915_READ(PLANE_CTL(pipe, 0));
  7948. ctl &= ~PLANE_CTL_TILED_MASK;
  7949. if (obj->tiling_mode == I915_TILING_X)
  7950. ctl |= PLANE_CTL_TILED_X;
  7951. /*
  7952. * The stride is either expressed as a multiple of 64 bytes chunks for
  7953. * linear buffers or in number of tiles for tiled buffers.
  7954. */
  7955. stride = fb->pitches[0] >> 6;
  7956. if (obj->tiling_mode == I915_TILING_X)
  7957. stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
  7958. /*
  7959. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  7960. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  7961. */
  7962. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  7963. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  7964. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  7965. POSTING_READ(PLANE_SURF(pipe, 0));
  7966. }
  7967. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  7968. {
  7969. struct drm_device *dev = intel_crtc->base.dev;
  7970. struct drm_i915_private *dev_priv = dev->dev_private;
  7971. struct intel_framebuffer *intel_fb =
  7972. to_intel_framebuffer(intel_crtc->base.primary->fb);
  7973. struct drm_i915_gem_object *obj = intel_fb->obj;
  7974. u32 dspcntr;
  7975. u32 reg;
  7976. reg = DSPCNTR(intel_crtc->plane);
  7977. dspcntr = I915_READ(reg);
  7978. if (obj->tiling_mode != I915_TILING_NONE)
  7979. dspcntr |= DISPPLANE_TILED;
  7980. else
  7981. dspcntr &= ~DISPPLANE_TILED;
  7982. I915_WRITE(reg, dspcntr);
  7983. I915_WRITE(DSPSURF(intel_crtc->plane),
  7984. intel_crtc->unpin_work->gtt_offset);
  7985. POSTING_READ(DSPSURF(intel_crtc->plane));
  7986. }
  7987. /*
  7988. * XXX: This is the temporary way to update the plane registers until we get
  7989. * around to using the usual plane update functions for MMIO flips
  7990. */
  7991. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  7992. {
  7993. struct drm_device *dev = intel_crtc->base.dev;
  7994. bool atomic_update;
  7995. u32 start_vbl_count;
  7996. intel_mark_page_flip_active(intel_crtc);
  7997. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  7998. if (INTEL_INFO(dev)->gen >= 9)
  7999. skl_do_mmio_flip(intel_crtc);
  8000. else
  8001. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  8002. ilk_do_mmio_flip(intel_crtc);
  8003. if (atomic_update)
  8004. intel_pipe_update_end(intel_crtc, start_vbl_count);
  8005. }
  8006. static void intel_mmio_flip_work_func(struct work_struct *work)
  8007. {
  8008. struct intel_crtc *crtc =
  8009. container_of(work, struct intel_crtc, mmio_flip.work);
  8010. struct intel_mmio_flip *mmio_flip;
  8011. mmio_flip = &crtc->mmio_flip;
  8012. if (mmio_flip->req)
  8013. WARN_ON(__i915_wait_request(mmio_flip->req,
  8014. crtc->reset_counter,
  8015. false, NULL, NULL) != 0);
  8016. intel_do_mmio_flip(crtc);
  8017. if (mmio_flip->req) {
  8018. mutex_lock(&crtc->base.dev->struct_mutex);
  8019. i915_gem_request_assign(&mmio_flip->req, NULL);
  8020. mutex_unlock(&crtc->base.dev->struct_mutex);
  8021. }
  8022. }
  8023. static int intel_queue_mmio_flip(struct drm_device *dev,
  8024. struct drm_crtc *crtc,
  8025. struct drm_framebuffer *fb,
  8026. struct drm_i915_gem_object *obj,
  8027. struct intel_engine_cs *ring,
  8028. uint32_t flags)
  8029. {
  8030. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8031. i915_gem_request_assign(&intel_crtc->mmio_flip.req,
  8032. obj->last_write_req);
  8033. schedule_work(&intel_crtc->mmio_flip.work);
  8034. return 0;
  8035. }
  8036. static int intel_gen9_queue_flip(struct drm_device *dev,
  8037. struct drm_crtc *crtc,
  8038. struct drm_framebuffer *fb,
  8039. struct drm_i915_gem_object *obj,
  8040. struct intel_engine_cs *ring,
  8041. uint32_t flags)
  8042. {
  8043. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8044. uint32_t plane = 0, stride;
  8045. int ret;
  8046. switch(intel_crtc->pipe) {
  8047. case PIPE_A:
  8048. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_A;
  8049. break;
  8050. case PIPE_B:
  8051. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_B;
  8052. break;
  8053. case PIPE_C:
  8054. plane = MI_DISPLAY_FLIP_SKL_PLANE_1_C;
  8055. break;
  8056. default:
  8057. WARN_ONCE(1, "unknown plane in flip command\n");
  8058. return -ENODEV;
  8059. }
  8060. switch (obj->tiling_mode) {
  8061. case I915_TILING_NONE:
  8062. stride = fb->pitches[0] >> 6;
  8063. break;
  8064. case I915_TILING_X:
  8065. stride = fb->pitches[0] >> 9;
  8066. break;
  8067. default:
  8068. WARN_ONCE(1, "unknown tiling in flip command\n");
  8069. return -ENODEV;
  8070. }
  8071. ret = intel_ring_begin(ring, 10);
  8072. if (ret)
  8073. return ret;
  8074. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  8075. intel_ring_emit(ring, DERRMR);
  8076. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8077. DERRMR_PIPEB_PRI_FLIP_DONE |
  8078. DERRMR_PIPEC_PRI_FLIP_DONE));
  8079. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  8080. MI_SRM_LRM_GLOBAL_GTT);
  8081. intel_ring_emit(ring, DERRMR);
  8082. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  8083. intel_ring_emit(ring, 0);
  8084. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane);
  8085. intel_ring_emit(ring, stride << 6 | obj->tiling_mode);
  8086. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  8087. intel_mark_page_flip_active(intel_crtc);
  8088. __intel_ring_advance(ring);
  8089. return 0;
  8090. }
  8091. static int intel_default_queue_flip(struct drm_device *dev,
  8092. struct drm_crtc *crtc,
  8093. struct drm_framebuffer *fb,
  8094. struct drm_i915_gem_object *obj,
  8095. struct intel_engine_cs *ring,
  8096. uint32_t flags)
  8097. {
  8098. return -ENODEV;
  8099. }
  8100. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  8101. struct drm_crtc *crtc)
  8102. {
  8103. struct drm_i915_private *dev_priv = dev->dev_private;
  8104. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8105. struct intel_unpin_work *work = intel_crtc->unpin_work;
  8106. u32 addr;
  8107. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  8108. return true;
  8109. if (!work->enable_stall_check)
  8110. return false;
  8111. if (work->flip_ready_vblank == 0) {
  8112. if (work->flip_queued_req &&
  8113. !i915_gem_request_completed(work->flip_queued_req, true))
  8114. return false;
  8115. work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8116. }
  8117. if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
  8118. return false;
  8119. /* Potential stall - if we see that the flip has happened,
  8120. * assume a missed interrupt. */
  8121. if (INTEL_INFO(dev)->gen >= 4)
  8122. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8123. else
  8124. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8125. /* There is a potential issue here with a false positive after a flip
  8126. * to the same address. We could address this by checking for a
  8127. * non-incrementing frame counter.
  8128. */
  8129. return addr == work->gtt_offset;
  8130. }
  8131. void intel_check_page_flip(struct drm_device *dev, int pipe)
  8132. {
  8133. struct drm_i915_private *dev_priv = dev->dev_private;
  8134. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  8135. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8136. WARN_ON(!in_irq());
  8137. if (crtc == NULL)
  8138. return;
  8139. spin_lock(&dev->event_lock);
  8140. if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
  8141. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  8142. intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  8143. page_flip_completed(intel_crtc);
  8144. }
  8145. spin_unlock(&dev->event_lock);
  8146. }
  8147. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8148. struct drm_framebuffer *fb,
  8149. struct drm_pending_vblank_event *event,
  8150. uint32_t page_flip_flags)
  8151. {
  8152. struct drm_device *dev = crtc->dev;
  8153. struct drm_i915_private *dev_priv = dev->dev_private;
  8154. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8155. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8156. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8157. struct drm_plane *primary = crtc->primary;
  8158. enum pipe pipe = intel_crtc->pipe;
  8159. struct intel_unpin_work *work;
  8160. struct intel_engine_cs *ring;
  8161. int ret;
  8162. /*
  8163. * drm_mode_page_flip_ioctl() should already catch this, but double
  8164. * check to be safe. In the future we may enable pageflipping from
  8165. * a disabled primary plane.
  8166. */
  8167. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8168. return -EBUSY;
  8169. /* Can't change pixel format via MI display flips. */
  8170. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  8171. return -EINVAL;
  8172. /*
  8173. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8174. * Note that pitch changes could also affect these register.
  8175. */
  8176. if (INTEL_INFO(dev)->gen > 3 &&
  8177. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8178. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8179. return -EINVAL;
  8180. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8181. goto out_hang;
  8182. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8183. if (work == NULL)
  8184. return -ENOMEM;
  8185. work->event = event;
  8186. work->crtc = crtc;
  8187. work->old_fb_obj = intel_fb_obj(old_fb);
  8188. INIT_WORK(&work->work, intel_unpin_work_fn);
  8189. ret = drm_crtc_vblank_get(crtc);
  8190. if (ret)
  8191. goto free_work;
  8192. /* We borrow the event spin lock for protecting unpin_work */
  8193. spin_lock_irq(&dev->event_lock);
  8194. if (intel_crtc->unpin_work) {
  8195. /* Before declaring the flip queue wedged, check if
  8196. * the hardware completed the operation behind our backs.
  8197. */
  8198. if (__intel_pageflip_stall_check(dev, crtc)) {
  8199. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8200. page_flip_completed(intel_crtc);
  8201. } else {
  8202. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8203. spin_unlock_irq(&dev->event_lock);
  8204. drm_crtc_vblank_put(crtc);
  8205. kfree(work);
  8206. return -EBUSY;
  8207. }
  8208. }
  8209. intel_crtc->unpin_work = work;
  8210. spin_unlock_irq(&dev->event_lock);
  8211. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8212. flush_workqueue(dev_priv->wq);
  8213. ret = i915_mutex_lock_interruptible(dev);
  8214. if (ret)
  8215. goto cleanup;
  8216. /* Reference the objects for the scheduled work. */
  8217. drm_gem_object_reference(&work->old_fb_obj->base);
  8218. drm_gem_object_reference(&obj->base);
  8219. crtc->primary->fb = fb;
  8220. work->pending_flip_obj = obj;
  8221. atomic_inc(&intel_crtc->unpin_work_count);
  8222. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  8223. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  8224. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  8225. if (IS_VALLEYVIEW(dev)) {
  8226. ring = &dev_priv->ring[BCS];
  8227. if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
  8228. /* vlv: DISPLAY_FLIP fails to change tiling */
  8229. ring = NULL;
  8230. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  8231. ring = &dev_priv->ring[BCS];
  8232. } else if (INTEL_INFO(dev)->gen >= 7) {
  8233. ring = i915_gem_request_get_ring(obj->last_read_req);
  8234. if (ring == NULL || ring->id != RCS)
  8235. ring = &dev_priv->ring[BCS];
  8236. } else {
  8237. ring = &dev_priv->ring[RCS];
  8238. }
  8239. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb, ring);
  8240. if (ret)
  8241. goto cleanup_pending;
  8242. work->gtt_offset =
  8243. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  8244. if (use_mmio_flip(ring, obj)) {
  8245. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  8246. page_flip_flags);
  8247. if (ret)
  8248. goto cleanup_unpin;
  8249. i915_gem_request_assign(&work->flip_queued_req,
  8250. obj->last_write_req);
  8251. } else {
  8252. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  8253. page_flip_flags);
  8254. if (ret)
  8255. goto cleanup_unpin;
  8256. i915_gem_request_assign(&work->flip_queued_req,
  8257. intel_ring_get_request(ring));
  8258. }
  8259. work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
  8260. work->enable_stall_check = true;
  8261. i915_gem_track_fb(work->old_fb_obj, obj,
  8262. INTEL_FRONTBUFFER_PRIMARY(pipe));
  8263. intel_fbc_disable(dev);
  8264. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  8265. mutex_unlock(&dev->struct_mutex);
  8266. trace_i915_flip_request(intel_crtc->plane, obj);
  8267. return 0;
  8268. cleanup_unpin:
  8269. intel_unpin_fb_obj(obj);
  8270. cleanup_pending:
  8271. atomic_dec(&intel_crtc->unpin_work_count);
  8272. crtc->primary->fb = old_fb;
  8273. drm_gem_object_unreference(&work->old_fb_obj->base);
  8274. drm_gem_object_unreference(&obj->base);
  8275. mutex_unlock(&dev->struct_mutex);
  8276. cleanup:
  8277. spin_lock_irq(&dev->event_lock);
  8278. intel_crtc->unpin_work = NULL;
  8279. spin_unlock_irq(&dev->event_lock);
  8280. drm_crtc_vblank_put(crtc);
  8281. free_work:
  8282. kfree(work);
  8283. if (ret == -EIO) {
  8284. out_hang:
  8285. ret = intel_plane_restore(primary);
  8286. if (ret == 0 && event) {
  8287. spin_lock_irq(&dev->event_lock);
  8288. drm_send_vblank_event(dev, pipe, event);
  8289. spin_unlock_irq(&dev->event_lock);
  8290. }
  8291. }
  8292. return ret;
  8293. }
  8294. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  8295. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  8296. .load_lut = intel_crtc_load_lut,
  8297. .atomic_begin = intel_begin_crtc_commit,
  8298. .atomic_flush = intel_finish_crtc_commit,
  8299. };
  8300. /**
  8301. * intel_modeset_update_staged_output_state
  8302. *
  8303. * Updates the staged output configuration state, e.g. after we've read out the
  8304. * current hw state.
  8305. */
  8306. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  8307. {
  8308. struct intel_crtc *crtc;
  8309. struct intel_encoder *encoder;
  8310. struct intel_connector *connector;
  8311. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8312. base.head) {
  8313. connector->new_encoder =
  8314. to_intel_encoder(connector->base.encoder);
  8315. }
  8316. for_each_intel_encoder(dev, encoder) {
  8317. encoder->new_crtc =
  8318. to_intel_crtc(encoder->base.crtc);
  8319. }
  8320. for_each_intel_crtc(dev, crtc) {
  8321. crtc->new_enabled = crtc->base.enabled;
  8322. if (crtc->new_enabled)
  8323. crtc->new_config = crtc->config;
  8324. else
  8325. crtc->new_config = NULL;
  8326. }
  8327. }
  8328. /**
  8329. * intel_modeset_commit_output_state
  8330. *
  8331. * This function copies the stage display pipe configuration to the real one.
  8332. */
  8333. static void intel_modeset_commit_output_state(struct drm_device *dev)
  8334. {
  8335. struct intel_crtc *crtc;
  8336. struct intel_encoder *encoder;
  8337. struct intel_connector *connector;
  8338. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8339. base.head) {
  8340. connector->base.encoder = &connector->new_encoder->base;
  8341. }
  8342. for_each_intel_encoder(dev, encoder) {
  8343. encoder->base.crtc = &encoder->new_crtc->base;
  8344. }
  8345. for_each_intel_crtc(dev, crtc) {
  8346. crtc->base.enabled = crtc->new_enabled;
  8347. }
  8348. }
  8349. static void
  8350. connected_sink_compute_bpp(struct intel_connector *connector,
  8351. struct intel_crtc_state *pipe_config)
  8352. {
  8353. int bpp = pipe_config->pipe_bpp;
  8354. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8355. connector->base.base.id,
  8356. connector->base.name);
  8357. /* Don't use an invalid EDID bpc value */
  8358. if (connector->base.display_info.bpc &&
  8359. connector->base.display_info.bpc * 3 < bpp) {
  8360. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8361. bpp, connector->base.display_info.bpc*3);
  8362. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  8363. }
  8364. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8365. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  8366. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8367. bpp);
  8368. pipe_config->pipe_bpp = 24;
  8369. }
  8370. }
  8371. static int
  8372. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8373. struct drm_framebuffer *fb,
  8374. struct intel_crtc_state *pipe_config)
  8375. {
  8376. struct drm_device *dev = crtc->base.dev;
  8377. struct intel_connector *connector;
  8378. int bpp;
  8379. switch (fb->pixel_format) {
  8380. case DRM_FORMAT_C8:
  8381. bpp = 8*3; /* since we go through a colormap */
  8382. break;
  8383. case DRM_FORMAT_XRGB1555:
  8384. case DRM_FORMAT_ARGB1555:
  8385. /* checked in intel_framebuffer_init already */
  8386. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  8387. return -EINVAL;
  8388. case DRM_FORMAT_RGB565:
  8389. bpp = 6*3; /* min is 18bpp */
  8390. break;
  8391. case DRM_FORMAT_XBGR8888:
  8392. case DRM_FORMAT_ABGR8888:
  8393. /* checked in intel_framebuffer_init already */
  8394. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8395. return -EINVAL;
  8396. case DRM_FORMAT_XRGB8888:
  8397. case DRM_FORMAT_ARGB8888:
  8398. bpp = 8*3;
  8399. break;
  8400. case DRM_FORMAT_XRGB2101010:
  8401. case DRM_FORMAT_ARGB2101010:
  8402. case DRM_FORMAT_XBGR2101010:
  8403. case DRM_FORMAT_ABGR2101010:
  8404. /* checked in intel_framebuffer_init already */
  8405. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  8406. return -EINVAL;
  8407. bpp = 10*3;
  8408. break;
  8409. /* TODO: gen4+ supports 16 bpc floating point, too. */
  8410. default:
  8411. DRM_DEBUG_KMS("unsupported depth\n");
  8412. return -EINVAL;
  8413. }
  8414. pipe_config->pipe_bpp = bpp;
  8415. /* Clamp display bpp to EDID value */
  8416. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8417. base.head) {
  8418. if (!connector->new_encoder ||
  8419. connector->new_encoder->new_crtc != crtc)
  8420. continue;
  8421. connected_sink_compute_bpp(connector, pipe_config);
  8422. }
  8423. return bpp;
  8424. }
  8425. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  8426. {
  8427. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  8428. "type: 0x%x flags: 0x%x\n",
  8429. mode->crtc_clock,
  8430. mode->crtc_hdisplay, mode->crtc_hsync_start,
  8431. mode->crtc_hsync_end, mode->crtc_htotal,
  8432. mode->crtc_vdisplay, mode->crtc_vsync_start,
  8433. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  8434. }
  8435. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  8436. struct intel_crtc_state *pipe_config,
  8437. const char *context)
  8438. {
  8439. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  8440. context, pipe_name(crtc->pipe));
  8441. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  8442. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  8443. pipe_config->pipe_bpp, pipe_config->dither);
  8444. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8445. pipe_config->has_pch_encoder,
  8446. pipe_config->fdi_lanes,
  8447. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  8448. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  8449. pipe_config->fdi_m_n.tu);
  8450. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  8451. pipe_config->has_dp_encoder,
  8452. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  8453. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  8454. pipe_config->dp_m_n.tu);
  8455. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  8456. pipe_config->has_dp_encoder,
  8457. pipe_config->dp_m2_n2.gmch_m,
  8458. pipe_config->dp_m2_n2.gmch_n,
  8459. pipe_config->dp_m2_n2.link_m,
  8460. pipe_config->dp_m2_n2.link_n,
  8461. pipe_config->dp_m2_n2.tu);
  8462. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  8463. pipe_config->has_audio,
  8464. pipe_config->has_infoframe);
  8465. DRM_DEBUG_KMS("requested mode:\n");
  8466. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  8467. DRM_DEBUG_KMS("adjusted mode:\n");
  8468. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  8469. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  8470. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  8471. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  8472. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  8473. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  8474. pipe_config->gmch_pfit.control,
  8475. pipe_config->gmch_pfit.pgm_ratios,
  8476. pipe_config->gmch_pfit.lvds_border_bits);
  8477. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  8478. pipe_config->pch_pfit.pos,
  8479. pipe_config->pch_pfit.size,
  8480. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8481. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8482. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8483. }
  8484. static bool encoders_cloneable(const struct intel_encoder *a,
  8485. const struct intel_encoder *b)
  8486. {
  8487. /* masks could be asymmetric, so check both ways */
  8488. return a == b || (a->cloneable & (1 << b->type) &&
  8489. b->cloneable & (1 << a->type));
  8490. }
  8491. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8492. struct intel_encoder *encoder)
  8493. {
  8494. struct drm_device *dev = crtc->base.dev;
  8495. struct intel_encoder *source_encoder;
  8496. for_each_intel_encoder(dev, source_encoder) {
  8497. if (source_encoder->new_crtc != crtc)
  8498. continue;
  8499. if (!encoders_cloneable(encoder, source_encoder))
  8500. return false;
  8501. }
  8502. return true;
  8503. }
  8504. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8505. {
  8506. struct drm_device *dev = crtc->base.dev;
  8507. struct intel_encoder *encoder;
  8508. for_each_intel_encoder(dev, encoder) {
  8509. if (encoder->new_crtc != crtc)
  8510. continue;
  8511. if (!check_single_encoder_cloning(crtc, encoder))
  8512. return false;
  8513. }
  8514. return true;
  8515. }
  8516. static bool check_digital_port_conflicts(struct drm_device *dev)
  8517. {
  8518. struct intel_connector *connector;
  8519. unsigned int used_ports = 0;
  8520. /*
  8521. * Walk the connector list instead of the encoder
  8522. * list to detect the problem on ddi platforms
  8523. * where there's just one encoder per digital port.
  8524. */
  8525. list_for_each_entry(connector,
  8526. &dev->mode_config.connector_list, base.head) {
  8527. struct intel_encoder *encoder = connector->new_encoder;
  8528. if (!encoder)
  8529. continue;
  8530. WARN_ON(!encoder->new_crtc);
  8531. switch (encoder->type) {
  8532. unsigned int port_mask;
  8533. case INTEL_OUTPUT_UNKNOWN:
  8534. if (WARN_ON(!HAS_DDI(dev)))
  8535. break;
  8536. case INTEL_OUTPUT_DISPLAYPORT:
  8537. case INTEL_OUTPUT_HDMI:
  8538. case INTEL_OUTPUT_EDP:
  8539. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  8540. /* the same port mustn't appear more than once */
  8541. if (used_ports & port_mask)
  8542. return false;
  8543. used_ports |= port_mask;
  8544. default:
  8545. break;
  8546. }
  8547. }
  8548. return true;
  8549. }
  8550. static struct intel_crtc_state *
  8551. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8552. struct drm_framebuffer *fb,
  8553. struct drm_display_mode *mode)
  8554. {
  8555. struct drm_device *dev = crtc->dev;
  8556. struct intel_encoder *encoder;
  8557. struct intel_crtc_state *pipe_config;
  8558. int plane_bpp, ret = -EINVAL;
  8559. bool retry = true;
  8560. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8561. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8562. return ERR_PTR(-EINVAL);
  8563. }
  8564. if (!check_digital_port_conflicts(dev)) {
  8565. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  8566. return ERR_PTR(-EINVAL);
  8567. }
  8568. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8569. if (!pipe_config)
  8570. return ERR_PTR(-ENOMEM);
  8571. drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
  8572. drm_mode_copy(&pipe_config->base.mode, mode);
  8573. pipe_config->cpu_transcoder =
  8574. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8575. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8576. /*
  8577. * Sanitize sync polarity flags based on requested ones. If neither
  8578. * positive or negative polarity is requested, treat this as meaning
  8579. * negative polarity.
  8580. */
  8581. if (!(pipe_config->base.adjusted_mode.flags &
  8582. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8583. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8584. if (!(pipe_config->base.adjusted_mode.flags &
  8585. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8586. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8587. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8588. * plane pixel format and any sink constraints into account. Returns the
  8589. * source plane bpp so that dithering can be selected on mismatches
  8590. * after encoders and crtc also have had their say. */
  8591. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8592. fb, pipe_config);
  8593. if (plane_bpp < 0)
  8594. goto fail;
  8595. /*
  8596. * Determine the real pipe dimensions. Note that stereo modes can
  8597. * increase the actual pipe size due to the frame doubling and
  8598. * insertion of additional space for blanks between the frame. This
  8599. * is stored in the crtc timings. We use the requested mode to do this
  8600. * computation to clearly distinguish it from the adjusted mode, which
  8601. * can be changed by the connectors in the below retry loop.
  8602. */
  8603. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  8604. &pipe_config->pipe_src_w,
  8605. &pipe_config->pipe_src_h);
  8606. encoder_retry:
  8607. /* Ensure the port clock defaults are reset when retrying. */
  8608. pipe_config->port_clock = 0;
  8609. pipe_config->pixel_multiplier = 1;
  8610. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8611. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  8612. CRTC_STEREO_DOUBLE);
  8613. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8614. * adjust it according to limitations or connector properties, and also
  8615. * a chance to reject the mode entirely.
  8616. */
  8617. for_each_intel_encoder(dev, encoder) {
  8618. if (&encoder->new_crtc->base != crtc)
  8619. continue;
  8620. if (!(encoder->compute_config(encoder, pipe_config))) {
  8621. DRM_DEBUG_KMS("Encoder config failure\n");
  8622. goto fail;
  8623. }
  8624. }
  8625. /* Set default port clock if not overwritten by the encoder. Needs to be
  8626. * done afterwards in case the encoder adjusts the mode. */
  8627. if (!pipe_config->port_clock)
  8628. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  8629. * pipe_config->pixel_multiplier;
  8630. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8631. if (ret < 0) {
  8632. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8633. goto fail;
  8634. }
  8635. if (ret == RETRY) {
  8636. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8637. ret = -EINVAL;
  8638. goto fail;
  8639. }
  8640. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8641. retry = false;
  8642. goto encoder_retry;
  8643. }
  8644. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8645. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8646. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8647. return pipe_config;
  8648. fail:
  8649. kfree(pipe_config);
  8650. return ERR_PTR(ret);
  8651. }
  8652. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8653. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8654. static void
  8655. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8656. unsigned *prepare_pipes, unsigned *disable_pipes)
  8657. {
  8658. struct intel_crtc *intel_crtc;
  8659. struct drm_device *dev = crtc->dev;
  8660. struct intel_encoder *encoder;
  8661. struct intel_connector *connector;
  8662. struct drm_crtc *tmp_crtc;
  8663. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8664. /* Check which crtcs have changed outputs connected to them, these need
  8665. * to be part of the prepare_pipes mask. We don't (yet) support global
  8666. * modeset across multiple crtcs, so modeset_pipes will only have one
  8667. * bit set at most. */
  8668. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8669. base.head) {
  8670. if (connector->base.encoder == &connector->new_encoder->base)
  8671. continue;
  8672. if (connector->base.encoder) {
  8673. tmp_crtc = connector->base.encoder->crtc;
  8674. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8675. }
  8676. if (connector->new_encoder)
  8677. *prepare_pipes |=
  8678. 1 << connector->new_encoder->new_crtc->pipe;
  8679. }
  8680. for_each_intel_encoder(dev, encoder) {
  8681. if (encoder->base.crtc == &encoder->new_crtc->base)
  8682. continue;
  8683. if (encoder->base.crtc) {
  8684. tmp_crtc = encoder->base.crtc;
  8685. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8686. }
  8687. if (encoder->new_crtc)
  8688. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8689. }
  8690. /* Check for pipes that will be enabled/disabled ... */
  8691. for_each_intel_crtc(dev, intel_crtc) {
  8692. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8693. continue;
  8694. if (!intel_crtc->new_enabled)
  8695. *disable_pipes |= 1 << intel_crtc->pipe;
  8696. else
  8697. *prepare_pipes |= 1 << intel_crtc->pipe;
  8698. }
  8699. /* set_mode is also used to update properties on life display pipes. */
  8700. intel_crtc = to_intel_crtc(crtc);
  8701. if (intel_crtc->new_enabled)
  8702. *prepare_pipes |= 1 << intel_crtc->pipe;
  8703. /*
  8704. * For simplicity do a full modeset on any pipe where the output routing
  8705. * changed. We could be more clever, but that would require us to be
  8706. * more careful with calling the relevant encoder->mode_set functions.
  8707. */
  8708. if (*prepare_pipes)
  8709. *modeset_pipes = *prepare_pipes;
  8710. /* ... and mask these out. */
  8711. *modeset_pipes &= ~(*disable_pipes);
  8712. *prepare_pipes &= ~(*disable_pipes);
  8713. /*
  8714. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8715. * obies this rule, but the modeset restore mode of
  8716. * intel_modeset_setup_hw_state does not.
  8717. */
  8718. *modeset_pipes &= 1 << intel_crtc->pipe;
  8719. *prepare_pipes &= 1 << intel_crtc->pipe;
  8720. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8721. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8722. }
  8723. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8724. {
  8725. struct drm_encoder *encoder;
  8726. struct drm_device *dev = crtc->dev;
  8727. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8728. if (encoder->crtc == crtc)
  8729. return true;
  8730. return false;
  8731. }
  8732. static void
  8733. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8734. {
  8735. struct drm_i915_private *dev_priv = dev->dev_private;
  8736. struct intel_encoder *intel_encoder;
  8737. struct intel_crtc *intel_crtc;
  8738. struct drm_connector *connector;
  8739. intel_shared_dpll_commit(dev_priv);
  8740. for_each_intel_encoder(dev, intel_encoder) {
  8741. if (!intel_encoder->base.crtc)
  8742. continue;
  8743. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8744. if (prepare_pipes & (1 << intel_crtc->pipe))
  8745. intel_encoder->connectors_active = false;
  8746. }
  8747. intel_modeset_commit_output_state(dev);
  8748. /* Double check state. */
  8749. for_each_intel_crtc(dev, intel_crtc) {
  8750. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8751. WARN_ON(intel_crtc->new_config &&
  8752. intel_crtc->new_config != intel_crtc->config);
  8753. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8754. }
  8755. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8756. if (!connector->encoder || !connector->encoder->crtc)
  8757. continue;
  8758. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8759. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8760. struct drm_property *dpms_property =
  8761. dev->mode_config.dpms_property;
  8762. connector->dpms = DRM_MODE_DPMS_ON;
  8763. drm_object_property_set_value(&connector->base,
  8764. dpms_property,
  8765. DRM_MODE_DPMS_ON);
  8766. intel_encoder = to_intel_encoder(connector->encoder);
  8767. intel_encoder->connectors_active = true;
  8768. }
  8769. }
  8770. }
  8771. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8772. {
  8773. int diff;
  8774. if (clock1 == clock2)
  8775. return true;
  8776. if (!clock1 || !clock2)
  8777. return false;
  8778. diff = abs(clock1 - clock2);
  8779. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8780. return true;
  8781. return false;
  8782. }
  8783. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8784. list_for_each_entry((intel_crtc), \
  8785. &(dev)->mode_config.crtc_list, \
  8786. base.head) \
  8787. if (mask & (1 <<(intel_crtc)->pipe))
  8788. static bool
  8789. intel_pipe_config_compare(struct drm_device *dev,
  8790. struct intel_crtc_state *current_config,
  8791. struct intel_crtc_state *pipe_config)
  8792. {
  8793. #define PIPE_CONF_CHECK_X(name) \
  8794. if (current_config->name != pipe_config->name) { \
  8795. DRM_ERROR("mismatch in " #name " " \
  8796. "(expected 0x%08x, found 0x%08x)\n", \
  8797. current_config->name, \
  8798. pipe_config->name); \
  8799. return false; \
  8800. }
  8801. #define PIPE_CONF_CHECK_I(name) \
  8802. if (current_config->name != pipe_config->name) { \
  8803. DRM_ERROR("mismatch in " #name " " \
  8804. "(expected %i, found %i)\n", \
  8805. current_config->name, \
  8806. pipe_config->name); \
  8807. return false; \
  8808. }
  8809. /* This is required for BDW+ where there is only one set of registers for
  8810. * switching between high and low RR.
  8811. * This macro can be used whenever a comparison has to be made between one
  8812. * hw state and multiple sw state variables.
  8813. */
  8814. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  8815. if ((current_config->name != pipe_config->name) && \
  8816. (current_config->alt_name != pipe_config->name)) { \
  8817. DRM_ERROR("mismatch in " #name " " \
  8818. "(expected %i or %i, found %i)\n", \
  8819. current_config->name, \
  8820. current_config->alt_name, \
  8821. pipe_config->name); \
  8822. return false; \
  8823. }
  8824. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8825. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8826. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8827. "(expected %i, found %i)\n", \
  8828. current_config->name & (mask), \
  8829. pipe_config->name & (mask)); \
  8830. return false; \
  8831. }
  8832. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8833. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8834. DRM_ERROR("mismatch in " #name " " \
  8835. "(expected %i, found %i)\n", \
  8836. current_config->name, \
  8837. pipe_config->name); \
  8838. return false; \
  8839. }
  8840. #define PIPE_CONF_QUIRK(quirk) \
  8841. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8842. PIPE_CONF_CHECK_I(cpu_transcoder);
  8843. PIPE_CONF_CHECK_I(has_pch_encoder);
  8844. PIPE_CONF_CHECK_I(fdi_lanes);
  8845. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8846. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8847. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8848. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8849. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8850. PIPE_CONF_CHECK_I(has_dp_encoder);
  8851. if (INTEL_INFO(dev)->gen < 8) {
  8852. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8853. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8854. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8855. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8856. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8857. if (current_config->has_drrs) {
  8858. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  8859. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  8860. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  8861. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  8862. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  8863. }
  8864. } else {
  8865. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  8866. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  8867. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  8868. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  8869. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  8870. }
  8871. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  8872. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  8873. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  8874. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  8875. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  8876. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  8877. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  8878. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  8879. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  8880. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  8881. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  8882. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  8883. PIPE_CONF_CHECK_I(pixel_multiplier);
  8884. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8885. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8886. IS_VALLEYVIEW(dev))
  8887. PIPE_CONF_CHECK_I(limited_color_range);
  8888. PIPE_CONF_CHECK_I(has_infoframe);
  8889. PIPE_CONF_CHECK_I(has_audio);
  8890. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8891. DRM_MODE_FLAG_INTERLACE);
  8892. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8893. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8894. DRM_MODE_FLAG_PHSYNC);
  8895. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8896. DRM_MODE_FLAG_NHSYNC);
  8897. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8898. DRM_MODE_FLAG_PVSYNC);
  8899. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  8900. DRM_MODE_FLAG_NVSYNC);
  8901. }
  8902. PIPE_CONF_CHECK_I(pipe_src_w);
  8903. PIPE_CONF_CHECK_I(pipe_src_h);
  8904. /*
  8905. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8906. * screen. Since we don't yet re-compute the pipe config when moving
  8907. * just the lvds port away to another pipe the sw tracking won't match.
  8908. *
  8909. * Proper atomic modesets with recomputed global state will fix this.
  8910. * Until then just don't check gmch state for inherited modes.
  8911. */
  8912. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8913. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8914. /* pfit ratios are autocomputed by the hw on gen4+ */
  8915. if (INTEL_INFO(dev)->gen < 4)
  8916. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8917. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8918. }
  8919. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8920. if (current_config->pch_pfit.enabled) {
  8921. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8922. PIPE_CONF_CHECK_I(pch_pfit.size);
  8923. }
  8924. /* BDW+ don't expose a synchronous way to read the state */
  8925. if (IS_HASWELL(dev))
  8926. PIPE_CONF_CHECK_I(ips_enabled);
  8927. PIPE_CONF_CHECK_I(double_wide);
  8928. PIPE_CONF_CHECK_X(ddi_pll_sel);
  8929. PIPE_CONF_CHECK_I(shared_dpll);
  8930. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8931. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8932. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8933. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8934. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  8935. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  8936. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  8937. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  8938. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8939. PIPE_CONF_CHECK_I(pipe_bpp);
  8940. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  8941. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8942. #undef PIPE_CONF_CHECK_X
  8943. #undef PIPE_CONF_CHECK_I
  8944. #undef PIPE_CONF_CHECK_I_ALT
  8945. #undef PIPE_CONF_CHECK_FLAGS
  8946. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8947. #undef PIPE_CONF_QUIRK
  8948. return true;
  8949. }
  8950. static void check_wm_state(struct drm_device *dev)
  8951. {
  8952. struct drm_i915_private *dev_priv = dev->dev_private;
  8953. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  8954. struct intel_crtc *intel_crtc;
  8955. int plane;
  8956. if (INTEL_INFO(dev)->gen < 9)
  8957. return;
  8958. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  8959. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  8960. for_each_intel_crtc(dev, intel_crtc) {
  8961. struct skl_ddb_entry *hw_entry, *sw_entry;
  8962. const enum pipe pipe = intel_crtc->pipe;
  8963. if (!intel_crtc->active)
  8964. continue;
  8965. /* planes */
  8966. for_each_plane(pipe, plane) {
  8967. hw_entry = &hw_ddb.plane[pipe][plane];
  8968. sw_entry = &sw_ddb->plane[pipe][plane];
  8969. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  8970. continue;
  8971. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  8972. "(expected (%u,%u), found (%u,%u))\n",
  8973. pipe_name(pipe), plane + 1,
  8974. sw_entry->start, sw_entry->end,
  8975. hw_entry->start, hw_entry->end);
  8976. }
  8977. /* cursor */
  8978. hw_entry = &hw_ddb.cursor[pipe];
  8979. sw_entry = &sw_ddb->cursor[pipe];
  8980. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  8981. continue;
  8982. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  8983. "(expected (%u,%u), found (%u,%u))\n",
  8984. pipe_name(pipe),
  8985. sw_entry->start, sw_entry->end,
  8986. hw_entry->start, hw_entry->end);
  8987. }
  8988. }
  8989. static void
  8990. check_connector_state(struct drm_device *dev)
  8991. {
  8992. struct intel_connector *connector;
  8993. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8994. base.head) {
  8995. /* This also checks the encoder/connector hw state with the
  8996. * ->get_hw_state callbacks. */
  8997. intel_connector_check_state(connector);
  8998. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  8999. "connector's staged encoder doesn't match current encoder\n");
  9000. }
  9001. }
  9002. static void
  9003. check_encoder_state(struct drm_device *dev)
  9004. {
  9005. struct intel_encoder *encoder;
  9006. struct intel_connector *connector;
  9007. for_each_intel_encoder(dev, encoder) {
  9008. bool enabled = false;
  9009. bool active = false;
  9010. enum pipe pipe, tracked_pipe;
  9011. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9012. encoder->base.base.id,
  9013. encoder->base.name);
  9014. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  9015. "encoder's stage crtc doesn't match current crtc\n");
  9016. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  9017. "encoder's active_connectors set, but no crtc\n");
  9018. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9019. base.head) {
  9020. if (connector->base.encoder != &encoder->base)
  9021. continue;
  9022. enabled = true;
  9023. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  9024. active = true;
  9025. }
  9026. /*
  9027. * for MST connectors if we unplug the connector is gone
  9028. * away but the encoder is still connected to a crtc
  9029. * until a modeset happens in response to the hotplug.
  9030. */
  9031. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  9032. continue;
  9033. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9034. "encoder's enabled state mismatch "
  9035. "(expected %i, found %i)\n",
  9036. !!encoder->base.crtc, enabled);
  9037. I915_STATE_WARN(active && !encoder->base.crtc,
  9038. "active encoder with no crtc\n");
  9039. I915_STATE_WARN(encoder->connectors_active != active,
  9040. "encoder's computed active state doesn't match tracked active state "
  9041. "(expected %i, found %i)\n", active, encoder->connectors_active);
  9042. active = encoder->get_hw_state(encoder, &pipe);
  9043. I915_STATE_WARN(active != encoder->connectors_active,
  9044. "encoder's hw state doesn't match sw tracking "
  9045. "(expected %i, found %i)\n",
  9046. encoder->connectors_active, active);
  9047. if (!encoder->base.crtc)
  9048. continue;
  9049. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  9050. I915_STATE_WARN(active && pipe != tracked_pipe,
  9051. "active encoder's pipe doesn't match"
  9052. "(expected %i, found %i)\n",
  9053. tracked_pipe, pipe);
  9054. }
  9055. }
  9056. static void
  9057. check_crtc_state(struct drm_device *dev)
  9058. {
  9059. struct drm_i915_private *dev_priv = dev->dev_private;
  9060. struct intel_crtc *crtc;
  9061. struct intel_encoder *encoder;
  9062. struct intel_crtc_state pipe_config;
  9063. for_each_intel_crtc(dev, crtc) {
  9064. bool enabled = false;
  9065. bool active = false;
  9066. memset(&pipe_config, 0, sizeof(pipe_config));
  9067. DRM_DEBUG_KMS("[CRTC:%d]\n",
  9068. crtc->base.base.id);
  9069. I915_STATE_WARN(crtc->active && !crtc->base.enabled,
  9070. "active crtc, but not enabled in sw tracking\n");
  9071. for_each_intel_encoder(dev, encoder) {
  9072. if (encoder->base.crtc != &crtc->base)
  9073. continue;
  9074. enabled = true;
  9075. if (encoder->connectors_active)
  9076. active = true;
  9077. }
  9078. I915_STATE_WARN(active != crtc->active,
  9079. "crtc's computed active state doesn't match tracked active state "
  9080. "(expected %i, found %i)\n", active, crtc->active);
  9081. I915_STATE_WARN(enabled != crtc->base.enabled,
  9082. "crtc's computed enabled state doesn't match tracked enabled state "
  9083. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  9084. active = dev_priv->display.get_pipe_config(crtc,
  9085. &pipe_config);
  9086. /* hw state is inconsistent with the pipe quirk */
  9087. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  9088. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  9089. active = crtc->active;
  9090. for_each_intel_encoder(dev, encoder) {
  9091. enum pipe pipe;
  9092. if (encoder->base.crtc != &crtc->base)
  9093. continue;
  9094. if (encoder->get_hw_state(encoder, &pipe))
  9095. encoder->get_config(encoder, &pipe_config);
  9096. }
  9097. I915_STATE_WARN(crtc->active != active,
  9098. "crtc active state doesn't match with hw state "
  9099. "(expected %i, found %i)\n", crtc->active, active);
  9100. if (active &&
  9101. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  9102. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9103. intel_dump_pipe_config(crtc, &pipe_config,
  9104. "[hw state]");
  9105. intel_dump_pipe_config(crtc, crtc->config,
  9106. "[sw state]");
  9107. }
  9108. }
  9109. }
  9110. static void
  9111. check_shared_dpll_state(struct drm_device *dev)
  9112. {
  9113. struct drm_i915_private *dev_priv = dev->dev_private;
  9114. struct intel_crtc *crtc;
  9115. struct intel_dpll_hw_state dpll_hw_state;
  9116. int i;
  9117. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9118. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  9119. int enabled_crtcs = 0, active_crtcs = 0;
  9120. bool active;
  9121. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9122. DRM_DEBUG_KMS("%s\n", pll->name);
  9123. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9124. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  9125. "more active pll users than references: %i vs %i\n",
  9126. pll->active, hweight32(pll->config.crtc_mask));
  9127. I915_STATE_WARN(pll->active && !pll->on,
  9128. "pll in active use but not on in sw tracking\n");
  9129. I915_STATE_WARN(pll->on && !pll->active,
  9130. "pll in on but not on in use in sw tracking\n");
  9131. I915_STATE_WARN(pll->on != active,
  9132. "pll on state mismatch (expected %i, found %i)\n",
  9133. pll->on, active);
  9134. for_each_intel_crtc(dev, crtc) {
  9135. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  9136. enabled_crtcs++;
  9137. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  9138. active_crtcs++;
  9139. }
  9140. I915_STATE_WARN(pll->active != active_crtcs,
  9141. "pll active crtcs mismatch (expected %i, found %i)\n",
  9142. pll->active, active_crtcs);
  9143. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  9144. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  9145. hweight32(pll->config.crtc_mask), enabled_crtcs);
  9146. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  9147. sizeof(dpll_hw_state)),
  9148. "pll hw state mismatch\n");
  9149. }
  9150. }
  9151. void
  9152. intel_modeset_check_state(struct drm_device *dev)
  9153. {
  9154. check_wm_state(dev);
  9155. check_connector_state(dev);
  9156. check_encoder_state(dev);
  9157. check_crtc_state(dev);
  9158. check_shared_dpll_state(dev);
  9159. }
  9160. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  9161. int dotclock)
  9162. {
  9163. /*
  9164. * FDI already provided one idea for the dotclock.
  9165. * Yell if the encoder disagrees.
  9166. */
  9167. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  9168. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9169. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  9170. }
  9171. static void update_scanline_offset(struct intel_crtc *crtc)
  9172. {
  9173. struct drm_device *dev = crtc->base.dev;
  9174. /*
  9175. * The scanline counter increments at the leading edge of hsync.
  9176. *
  9177. * On most platforms it starts counting from vtotal-1 on the
  9178. * first active line. That means the scanline counter value is
  9179. * always one less than what we would expect. Ie. just after
  9180. * start of vblank, which also occurs at start of hsync (on the
  9181. * last active line), the scanline counter will read vblank_start-1.
  9182. *
  9183. * On gen2 the scanline counter starts counting from 1 instead
  9184. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  9185. * to keep the value positive), instead of adding one.
  9186. *
  9187. * On HSW+ the behaviour of the scanline counter depends on the output
  9188. * type. For DP ports it behaves like most other platforms, but on HDMI
  9189. * there's an extra 1 line difference. So we need to add two instead of
  9190. * one to the value.
  9191. */
  9192. if (IS_GEN2(dev)) {
  9193. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  9194. int vtotal;
  9195. vtotal = mode->crtc_vtotal;
  9196. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  9197. vtotal /= 2;
  9198. crtc->scanline_offset = vtotal - 1;
  9199. } else if (HAS_DDI(dev) &&
  9200. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  9201. crtc->scanline_offset = 2;
  9202. } else
  9203. crtc->scanline_offset = 1;
  9204. }
  9205. static struct intel_crtc_state *
  9206. intel_modeset_compute_config(struct drm_crtc *crtc,
  9207. struct drm_display_mode *mode,
  9208. struct drm_framebuffer *fb,
  9209. unsigned *modeset_pipes,
  9210. unsigned *prepare_pipes,
  9211. unsigned *disable_pipes)
  9212. {
  9213. struct intel_crtc_state *pipe_config = NULL;
  9214. intel_modeset_affected_pipes(crtc, modeset_pipes,
  9215. prepare_pipes, disable_pipes);
  9216. if ((*modeset_pipes) == 0)
  9217. goto out;
  9218. /*
  9219. * Note this needs changes when we start tracking multiple modes
  9220. * and crtcs. At that point we'll need to compute the whole config
  9221. * (i.e. one pipe_config for each crtc) rather than just the one
  9222. * for this crtc.
  9223. */
  9224. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  9225. if (IS_ERR(pipe_config)) {
  9226. goto out;
  9227. }
  9228. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  9229. "[modeset]");
  9230. out:
  9231. return pipe_config;
  9232. }
  9233. static int __intel_set_mode_setup_plls(struct drm_device *dev,
  9234. unsigned modeset_pipes,
  9235. unsigned disable_pipes)
  9236. {
  9237. struct drm_i915_private *dev_priv = to_i915(dev);
  9238. unsigned clear_pipes = modeset_pipes | disable_pipes;
  9239. struct intel_crtc *intel_crtc;
  9240. int ret = 0;
  9241. if (!dev_priv->display.crtc_compute_clock)
  9242. return 0;
  9243. ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
  9244. if (ret)
  9245. goto done;
  9246. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9247. struct intel_crtc_state *state = intel_crtc->new_config;
  9248. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9249. state);
  9250. if (ret) {
  9251. intel_shared_dpll_abort_config(dev_priv);
  9252. goto done;
  9253. }
  9254. }
  9255. done:
  9256. return ret;
  9257. }
  9258. static int __intel_set_mode(struct drm_crtc *crtc,
  9259. struct drm_display_mode *mode,
  9260. int x, int y, struct drm_framebuffer *fb,
  9261. struct intel_crtc_state *pipe_config,
  9262. unsigned modeset_pipes,
  9263. unsigned prepare_pipes,
  9264. unsigned disable_pipes)
  9265. {
  9266. struct drm_device *dev = crtc->dev;
  9267. struct drm_i915_private *dev_priv = dev->dev_private;
  9268. struct drm_display_mode *saved_mode;
  9269. struct intel_crtc *intel_crtc;
  9270. int ret = 0;
  9271. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  9272. if (!saved_mode)
  9273. return -ENOMEM;
  9274. *saved_mode = crtc->mode;
  9275. if (modeset_pipes)
  9276. to_intel_crtc(crtc)->new_config = pipe_config;
  9277. /*
  9278. * See if the config requires any additional preparation, e.g.
  9279. * to adjust global state with pipes off. We need to do this
  9280. * here so we can get the modeset_pipe updated config for the new
  9281. * mode set on this crtc. For other crtcs we need to use the
  9282. * adjusted_mode bits in the crtc directly.
  9283. */
  9284. if (IS_VALLEYVIEW(dev)) {
  9285. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  9286. /* may have added more to prepare_pipes than we should */
  9287. prepare_pipes &= ~disable_pipes;
  9288. }
  9289. ret = __intel_set_mode_setup_plls(dev, modeset_pipes, disable_pipes);
  9290. if (ret)
  9291. goto done;
  9292. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  9293. intel_crtc_disable(&intel_crtc->base);
  9294. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9295. if (intel_crtc->base.enabled)
  9296. dev_priv->display.crtc_disable(&intel_crtc->base);
  9297. }
  9298. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  9299. * to set it here already despite that we pass it down the callchain.
  9300. *
  9301. * Note we'll need to fix this up when we start tracking multiple
  9302. * pipes; here we assume a single modeset_pipe and only track the
  9303. * single crtc and mode.
  9304. */
  9305. if (modeset_pipes) {
  9306. crtc->mode = *mode;
  9307. /* mode_set/enable/disable functions rely on a correct pipe
  9308. * config. */
  9309. intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
  9310. /*
  9311. * Calculate and store various constants which
  9312. * are later needed by vblank and swap-completion
  9313. * timestamping. They are derived from true hwmode.
  9314. */
  9315. drm_calc_timestamping_constants(crtc,
  9316. &pipe_config->base.adjusted_mode);
  9317. }
  9318. /* Only after disabling all output pipelines that will be changed can we
  9319. * update the the output configuration. */
  9320. intel_modeset_update_state(dev, prepare_pipes);
  9321. modeset_update_crtc_power_domains(dev);
  9322. /* Set up the DPLL and any encoders state that needs to adjust or depend
  9323. * on the DPLL.
  9324. */
  9325. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  9326. struct drm_plane *primary = intel_crtc->base.primary;
  9327. int vdisplay, hdisplay;
  9328. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9329. ret = primary->funcs->update_plane(primary, &intel_crtc->base,
  9330. fb, 0, 0,
  9331. hdisplay, vdisplay,
  9332. x << 16, y << 16,
  9333. hdisplay << 16, vdisplay << 16);
  9334. }
  9335. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  9336. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  9337. update_scanline_offset(intel_crtc);
  9338. dev_priv->display.crtc_enable(&intel_crtc->base);
  9339. }
  9340. /* FIXME: add subpixel order */
  9341. done:
  9342. if (ret && crtc->enabled)
  9343. crtc->mode = *saved_mode;
  9344. kfree(saved_mode);
  9345. return ret;
  9346. }
  9347. static int intel_set_mode_pipes(struct drm_crtc *crtc,
  9348. struct drm_display_mode *mode,
  9349. int x, int y, struct drm_framebuffer *fb,
  9350. struct intel_crtc_state *pipe_config,
  9351. unsigned modeset_pipes,
  9352. unsigned prepare_pipes,
  9353. unsigned disable_pipes)
  9354. {
  9355. int ret;
  9356. ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
  9357. prepare_pipes, disable_pipes);
  9358. if (ret == 0)
  9359. intel_modeset_check_state(crtc->dev);
  9360. return ret;
  9361. }
  9362. static int intel_set_mode(struct drm_crtc *crtc,
  9363. struct drm_display_mode *mode,
  9364. int x, int y, struct drm_framebuffer *fb)
  9365. {
  9366. struct intel_crtc_state *pipe_config;
  9367. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9368. pipe_config = intel_modeset_compute_config(crtc, mode, fb,
  9369. &modeset_pipes,
  9370. &prepare_pipes,
  9371. &disable_pipes);
  9372. if (IS_ERR(pipe_config))
  9373. return PTR_ERR(pipe_config);
  9374. return intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
  9375. modeset_pipes, prepare_pipes,
  9376. disable_pipes);
  9377. }
  9378. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  9379. {
  9380. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  9381. }
  9382. #undef for_each_intel_crtc_masked
  9383. static void intel_set_config_free(struct intel_set_config *config)
  9384. {
  9385. if (!config)
  9386. return;
  9387. kfree(config->save_connector_encoders);
  9388. kfree(config->save_encoder_crtcs);
  9389. kfree(config->save_crtc_enabled);
  9390. kfree(config);
  9391. }
  9392. static int intel_set_config_save_state(struct drm_device *dev,
  9393. struct intel_set_config *config)
  9394. {
  9395. struct drm_crtc *crtc;
  9396. struct drm_encoder *encoder;
  9397. struct drm_connector *connector;
  9398. int count;
  9399. config->save_crtc_enabled =
  9400. kcalloc(dev->mode_config.num_crtc,
  9401. sizeof(bool), GFP_KERNEL);
  9402. if (!config->save_crtc_enabled)
  9403. return -ENOMEM;
  9404. config->save_encoder_crtcs =
  9405. kcalloc(dev->mode_config.num_encoder,
  9406. sizeof(struct drm_crtc *), GFP_KERNEL);
  9407. if (!config->save_encoder_crtcs)
  9408. return -ENOMEM;
  9409. config->save_connector_encoders =
  9410. kcalloc(dev->mode_config.num_connector,
  9411. sizeof(struct drm_encoder *), GFP_KERNEL);
  9412. if (!config->save_connector_encoders)
  9413. return -ENOMEM;
  9414. /* Copy data. Note that driver private data is not affected.
  9415. * Should anything bad happen only the expected state is
  9416. * restored, not the drivers personal bookkeeping.
  9417. */
  9418. count = 0;
  9419. for_each_crtc(dev, crtc) {
  9420. config->save_crtc_enabled[count++] = crtc->enabled;
  9421. }
  9422. count = 0;
  9423. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  9424. config->save_encoder_crtcs[count++] = encoder->crtc;
  9425. }
  9426. count = 0;
  9427. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  9428. config->save_connector_encoders[count++] = connector->encoder;
  9429. }
  9430. return 0;
  9431. }
  9432. static void intel_set_config_restore_state(struct drm_device *dev,
  9433. struct intel_set_config *config)
  9434. {
  9435. struct intel_crtc *crtc;
  9436. struct intel_encoder *encoder;
  9437. struct intel_connector *connector;
  9438. int count;
  9439. count = 0;
  9440. for_each_intel_crtc(dev, crtc) {
  9441. crtc->new_enabled = config->save_crtc_enabled[count++];
  9442. if (crtc->new_enabled)
  9443. crtc->new_config = crtc->config;
  9444. else
  9445. crtc->new_config = NULL;
  9446. }
  9447. count = 0;
  9448. for_each_intel_encoder(dev, encoder) {
  9449. encoder->new_crtc =
  9450. to_intel_crtc(config->save_encoder_crtcs[count++]);
  9451. }
  9452. count = 0;
  9453. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9454. connector->new_encoder =
  9455. to_intel_encoder(config->save_connector_encoders[count++]);
  9456. }
  9457. }
  9458. static bool
  9459. is_crtc_connector_off(struct drm_mode_set *set)
  9460. {
  9461. int i;
  9462. if (set->num_connectors == 0)
  9463. return false;
  9464. if (WARN_ON(set->connectors == NULL))
  9465. return false;
  9466. for (i = 0; i < set->num_connectors; i++)
  9467. if (set->connectors[i]->encoder &&
  9468. set->connectors[i]->encoder->crtc == set->crtc &&
  9469. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  9470. return true;
  9471. return false;
  9472. }
  9473. static void
  9474. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  9475. struct intel_set_config *config)
  9476. {
  9477. /* We should be able to check here if the fb has the same properties
  9478. * and then just flip_or_move it */
  9479. if (is_crtc_connector_off(set)) {
  9480. config->mode_changed = true;
  9481. } else if (set->crtc->primary->fb != set->fb) {
  9482. /*
  9483. * If we have no fb, we can only flip as long as the crtc is
  9484. * active, otherwise we need a full mode set. The crtc may
  9485. * be active if we've only disabled the primary plane, or
  9486. * in fastboot situations.
  9487. */
  9488. if (set->crtc->primary->fb == NULL) {
  9489. struct intel_crtc *intel_crtc =
  9490. to_intel_crtc(set->crtc);
  9491. if (intel_crtc->active) {
  9492. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  9493. config->fb_changed = true;
  9494. } else {
  9495. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  9496. config->mode_changed = true;
  9497. }
  9498. } else if (set->fb == NULL) {
  9499. config->mode_changed = true;
  9500. } else if (set->fb->pixel_format !=
  9501. set->crtc->primary->fb->pixel_format) {
  9502. config->mode_changed = true;
  9503. } else {
  9504. config->fb_changed = true;
  9505. }
  9506. }
  9507. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  9508. config->fb_changed = true;
  9509. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  9510. DRM_DEBUG_KMS("modes are different, full mode set\n");
  9511. drm_mode_debug_printmodeline(&set->crtc->mode);
  9512. drm_mode_debug_printmodeline(set->mode);
  9513. config->mode_changed = true;
  9514. }
  9515. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  9516. set->crtc->base.id, config->mode_changed, config->fb_changed);
  9517. }
  9518. static int
  9519. intel_modeset_stage_output_state(struct drm_device *dev,
  9520. struct drm_mode_set *set,
  9521. struct intel_set_config *config)
  9522. {
  9523. struct intel_connector *connector;
  9524. struct intel_encoder *encoder;
  9525. struct intel_crtc *crtc;
  9526. int ro;
  9527. /* The upper layers ensure that we either disable a crtc or have a list
  9528. * of connectors. For paranoia, double-check this. */
  9529. WARN_ON(!set->fb && (set->num_connectors != 0));
  9530. WARN_ON(set->fb && (set->num_connectors == 0));
  9531. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9532. base.head) {
  9533. /* Otherwise traverse passed in connector list and get encoders
  9534. * for them. */
  9535. for (ro = 0; ro < set->num_connectors; ro++) {
  9536. if (set->connectors[ro] == &connector->base) {
  9537. connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
  9538. break;
  9539. }
  9540. }
  9541. /* If we disable the crtc, disable all its connectors. Also, if
  9542. * the connector is on the changing crtc but not on the new
  9543. * connector list, disable it. */
  9544. if ((!set->fb || ro == set->num_connectors) &&
  9545. connector->base.encoder &&
  9546. connector->base.encoder->crtc == set->crtc) {
  9547. connector->new_encoder = NULL;
  9548. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  9549. connector->base.base.id,
  9550. connector->base.name);
  9551. }
  9552. if (&connector->new_encoder->base != connector->base.encoder) {
  9553. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  9554. config->mode_changed = true;
  9555. }
  9556. }
  9557. /* connector->new_encoder is now updated for all connectors. */
  9558. /* Update crtc of enabled connectors. */
  9559. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9560. base.head) {
  9561. struct drm_crtc *new_crtc;
  9562. if (!connector->new_encoder)
  9563. continue;
  9564. new_crtc = connector->new_encoder->base.crtc;
  9565. for (ro = 0; ro < set->num_connectors; ro++) {
  9566. if (set->connectors[ro] == &connector->base)
  9567. new_crtc = set->crtc;
  9568. }
  9569. /* Make sure the new CRTC will work with the encoder */
  9570. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  9571. new_crtc)) {
  9572. return -EINVAL;
  9573. }
  9574. connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
  9575. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  9576. connector->base.base.id,
  9577. connector->base.name,
  9578. new_crtc->base.id);
  9579. }
  9580. /* Check for any encoders that needs to be disabled. */
  9581. for_each_intel_encoder(dev, encoder) {
  9582. int num_connectors = 0;
  9583. list_for_each_entry(connector,
  9584. &dev->mode_config.connector_list,
  9585. base.head) {
  9586. if (connector->new_encoder == encoder) {
  9587. WARN_ON(!connector->new_encoder->new_crtc);
  9588. num_connectors++;
  9589. }
  9590. }
  9591. if (num_connectors == 0)
  9592. encoder->new_crtc = NULL;
  9593. else if (num_connectors > 1)
  9594. return -EINVAL;
  9595. /* Only now check for crtc changes so we don't miss encoders
  9596. * that will be disabled. */
  9597. if (&encoder->new_crtc->base != encoder->base.crtc) {
  9598. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  9599. config->mode_changed = true;
  9600. }
  9601. }
  9602. /* Now we've also updated encoder->new_crtc for all encoders. */
  9603. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9604. base.head) {
  9605. if (connector->new_encoder)
  9606. if (connector->new_encoder != connector->encoder)
  9607. connector->encoder = connector->new_encoder;
  9608. }
  9609. for_each_intel_crtc(dev, crtc) {
  9610. crtc->new_enabled = false;
  9611. for_each_intel_encoder(dev, encoder) {
  9612. if (encoder->new_crtc == crtc) {
  9613. crtc->new_enabled = true;
  9614. break;
  9615. }
  9616. }
  9617. if (crtc->new_enabled != crtc->base.enabled) {
  9618. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  9619. crtc->new_enabled ? "en" : "dis");
  9620. config->mode_changed = true;
  9621. }
  9622. if (crtc->new_enabled)
  9623. crtc->new_config = crtc->config;
  9624. else
  9625. crtc->new_config = NULL;
  9626. }
  9627. return 0;
  9628. }
  9629. static void disable_crtc_nofb(struct intel_crtc *crtc)
  9630. {
  9631. struct drm_device *dev = crtc->base.dev;
  9632. struct intel_encoder *encoder;
  9633. struct intel_connector *connector;
  9634. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  9635. pipe_name(crtc->pipe));
  9636. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  9637. if (connector->new_encoder &&
  9638. connector->new_encoder->new_crtc == crtc)
  9639. connector->new_encoder = NULL;
  9640. }
  9641. for_each_intel_encoder(dev, encoder) {
  9642. if (encoder->new_crtc == crtc)
  9643. encoder->new_crtc = NULL;
  9644. }
  9645. crtc->new_enabled = false;
  9646. crtc->new_config = NULL;
  9647. }
  9648. static int intel_crtc_set_config(struct drm_mode_set *set)
  9649. {
  9650. struct drm_device *dev;
  9651. struct drm_mode_set save_set;
  9652. struct intel_set_config *config;
  9653. struct intel_crtc_state *pipe_config;
  9654. unsigned modeset_pipes, prepare_pipes, disable_pipes;
  9655. int ret;
  9656. BUG_ON(!set);
  9657. BUG_ON(!set->crtc);
  9658. BUG_ON(!set->crtc->helper_private);
  9659. /* Enforce sane interface api - has been abused by the fb helper. */
  9660. BUG_ON(!set->mode && set->fb);
  9661. BUG_ON(set->fb && set->num_connectors == 0);
  9662. if (set->fb) {
  9663. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9664. set->crtc->base.id, set->fb->base.id,
  9665. (int)set->num_connectors, set->x, set->y);
  9666. } else {
  9667. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9668. }
  9669. dev = set->crtc->dev;
  9670. ret = -ENOMEM;
  9671. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9672. if (!config)
  9673. goto out_config;
  9674. ret = intel_set_config_save_state(dev, config);
  9675. if (ret)
  9676. goto out_config;
  9677. save_set.crtc = set->crtc;
  9678. save_set.mode = &set->crtc->mode;
  9679. save_set.x = set->crtc->x;
  9680. save_set.y = set->crtc->y;
  9681. save_set.fb = set->crtc->primary->fb;
  9682. /* Compute whether we need a full modeset, only an fb base update or no
  9683. * change at all. In the future we might also check whether only the
  9684. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9685. * such cases. */
  9686. intel_set_config_compute_mode_changes(set, config);
  9687. ret = intel_modeset_stage_output_state(dev, set, config);
  9688. if (ret)
  9689. goto fail;
  9690. pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
  9691. set->fb,
  9692. &modeset_pipes,
  9693. &prepare_pipes,
  9694. &disable_pipes);
  9695. if (IS_ERR(pipe_config)) {
  9696. ret = PTR_ERR(pipe_config);
  9697. goto fail;
  9698. } else if (pipe_config) {
  9699. if (pipe_config->has_audio !=
  9700. to_intel_crtc(set->crtc)->config->has_audio)
  9701. config->mode_changed = true;
  9702. /*
  9703. * Note we have an issue here with infoframes: current code
  9704. * only updates them on the full mode set path per hw
  9705. * requirements. So here we should be checking for any
  9706. * required changes and forcing a mode set.
  9707. */
  9708. }
  9709. /* set_mode will free it in the mode_changed case */
  9710. if (!config->mode_changed)
  9711. kfree(pipe_config);
  9712. intel_update_pipe_size(to_intel_crtc(set->crtc));
  9713. if (config->mode_changed) {
  9714. ret = intel_set_mode_pipes(set->crtc, set->mode,
  9715. set->x, set->y, set->fb, pipe_config,
  9716. modeset_pipes, prepare_pipes,
  9717. disable_pipes);
  9718. } else if (config->fb_changed) {
  9719. struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
  9720. struct drm_plane *primary = set->crtc->primary;
  9721. int vdisplay, hdisplay;
  9722. drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
  9723. ret = primary->funcs->update_plane(primary, set->crtc, set->fb,
  9724. 0, 0, hdisplay, vdisplay,
  9725. set->x << 16, set->y << 16,
  9726. hdisplay << 16, vdisplay << 16);
  9727. /*
  9728. * We need to make sure the primary plane is re-enabled if it
  9729. * has previously been turned off.
  9730. */
  9731. if (!intel_crtc->primary_enabled && ret == 0) {
  9732. WARN_ON(!intel_crtc->active);
  9733. intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
  9734. }
  9735. /*
  9736. * In the fastboot case this may be our only check of the
  9737. * state after boot. It would be better to only do it on
  9738. * the first update, but we don't have a nice way of doing that
  9739. * (and really, set_config isn't used much for high freq page
  9740. * flipping, so increasing its cost here shouldn't be a big
  9741. * deal).
  9742. */
  9743. if (i915.fastboot && ret == 0)
  9744. intel_modeset_check_state(set->crtc->dev);
  9745. }
  9746. if (ret) {
  9747. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9748. set->crtc->base.id, ret);
  9749. fail:
  9750. intel_set_config_restore_state(dev, config);
  9751. /*
  9752. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9753. * force the pipe off to avoid oopsing in the modeset code
  9754. * due to fb==NULL. This should only happen during boot since
  9755. * we don't yet reconstruct the FB from the hardware state.
  9756. */
  9757. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9758. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9759. /* Try to restore the config */
  9760. if (config->mode_changed &&
  9761. intel_set_mode(save_set.crtc, save_set.mode,
  9762. save_set.x, save_set.y, save_set.fb))
  9763. DRM_ERROR("failed to restore config after modeset failure\n");
  9764. }
  9765. out_config:
  9766. intel_set_config_free(config);
  9767. return ret;
  9768. }
  9769. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9770. .gamma_set = intel_crtc_gamma_set,
  9771. .set_config = intel_crtc_set_config,
  9772. .destroy = intel_crtc_destroy,
  9773. .page_flip = intel_crtc_page_flip,
  9774. .atomic_duplicate_state = intel_crtc_duplicate_state,
  9775. .atomic_destroy_state = intel_crtc_destroy_state,
  9776. };
  9777. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9778. struct intel_shared_dpll *pll,
  9779. struct intel_dpll_hw_state *hw_state)
  9780. {
  9781. uint32_t val;
  9782. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  9783. return false;
  9784. val = I915_READ(PCH_DPLL(pll->id));
  9785. hw_state->dpll = val;
  9786. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9787. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9788. return val & DPLL_VCO_ENABLE;
  9789. }
  9790. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9791. struct intel_shared_dpll *pll)
  9792. {
  9793. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  9794. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  9795. }
  9796. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9797. struct intel_shared_dpll *pll)
  9798. {
  9799. /* PCH refclock must be enabled first */
  9800. ibx_assert_pch_refclk_enabled(dev_priv);
  9801. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9802. /* Wait for the clocks to stabilize. */
  9803. POSTING_READ(PCH_DPLL(pll->id));
  9804. udelay(150);
  9805. /* The pixel multiplier can only be updated once the
  9806. * DPLL is enabled and the clocks are stable.
  9807. *
  9808. * So write it again.
  9809. */
  9810. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  9811. POSTING_READ(PCH_DPLL(pll->id));
  9812. udelay(200);
  9813. }
  9814. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9815. struct intel_shared_dpll *pll)
  9816. {
  9817. struct drm_device *dev = dev_priv->dev;
  9818. struct intel_crtc *crtc;
  9819. /* Make sure no transcoder isn't still depending on us. */
  9820. for_each_intel_crtc(dev, crtc) {
  9821. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9822. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9823. }
  9824. I915_WRITE(PCH_DPLL(pll->id), 0);
  9825. POSTING_READ(PCH_DPLL(pll->id));
  9826. udelay(200);
  9827. }
  9828. static char *ibx_pch_dpll_names[] = {
  9829. "PCH DPLL A",
  9830. "PCH DPLL B",
  9831. };
  9832. static void ibx_pch_dpll_init(struct drm_device *dev)
  9833. {
  9834. struct drm_i915_private *dev_priv = dev->dev_private;
  9835. int i;
  9836. dev_priv->num_shared_dpll = 2;
  9837. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9838. dev_priv->shared_dplls[i].id = i;
  9839. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9840. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9841. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9842. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9843. dev_priv->shared_dplls[i].get_hw_state =
  9844. ibx_pch_dpll_get_hw_state;
  9845. }
  9846. }
  9847. static void intel_shared_dpll_init(struct drm_device *dev)
  9848. {
  9849. struct drm_i915_private *dev_priv = dev->dev_private;
  9850. if (HAS_DDI(dev))
  9851. intel_ddi_pll_init(dev);
  9852. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9853. ibx_pch_dpll_init(dev);
  9854. else
  9855. dev_priv->num_shared_dpll = 0;
  9856. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9857. }
  9858. /**
  9859. * intel_prepare_plane_fb - Prepare fb for usage on plane
  9860. * @plane: drm plane to prepare for
  9861. * @fb: framebuffer to prepare for presentation
  9862. *
  9863. * Prepares a framebuffer for usage on a display plane. Generally this
  9864. * involves pinning the underlying object and updating the frontbuffer tracking
  9865. * bits. Some older platforms need special physical address handling for
  9866. * cursor planes.
  9867. *
  9868. * Returns 0 on success, negative error code on failure.
  9869. */
  9870. int
  9871. intel_prepare_plane_fb(struct drm_plane *plane,
  9872. struct drm_framebuffer *fb)
  9873. {
  9874. struct drm_device *dev = plane->dev;
  9875. struct intel_plane *intel_plane = to_intel_plane(plane);
  9876. enum pipe pipe = intel_plane->pipe;
  9877. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9878. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  9879. unsigned frontbuffer_bits = 0;
  9880. int ret = 0;
  9881. if (!obj)
  9882. return 0;
  9883. switch (plane->type) {
  9884. case DRM_PLANE_TYPE_PRIMARY:
  9885. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  9886. break;
  9887. case DRM_PLANE_TYPE_CURSOR:
  9888. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  9889. break;
  9890. case DRM_PLANE_TYPE_OVERLAY:
  9891. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  9892. break;
  9893. }
  9894. mutex_lock(&dev->struct_mutex);
  9895. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  9896. INTEL_INFO(dev)->cursor_needs_physical) {
  9897. int align = IS_I830(dev) ? 16 * 1024 : 256;
  9898. ret = i915_gem_object_attach_phys(obj, align);
  9899. if (ret)
  9900. DRM_DEBUG_KMS("failed to attach phys object\n");
  9901. } else {
  9902. ret = intel_pin_and_fence_fb_obj(plane, fb, NULL);
  9903. }
  9904. if (ret == 0)
  9905. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  9906. mutex_unlock(&dev->struct_mutex);
  9907. return ret;
  9908. }
  9909. /**
  9910. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  9911. * @plane: drm plane to clean up for
  9912. * @fb: old framebuffer that was on plane
  9913. *
  9914. * Cleans up a framebuffer that has just been removed from a plane.
  9915. */
  9916. void
  9917. intel_cleanup_plane_fb(struct drm_plane *plane,
  9918. struct drm_framebuffer *fb)
  9919. {
  9920. struct drm_device *dev = plane->dev;
  9921. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9922. if (WARN_ON(!obj))
  9923. return;
  9924. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  9925. !INTEL_INFO(dev)->cursor_needs_physical) {
  9926. mutex_lock(&dev->struct_mutex);
  9927. intel_unpin_fb_obj(obj);
  9928. mutex_unlock(&dev->struct_mutex);
  9929. }
  9930. }
  9931. static int
  9932. intel_check_primary_plane(struct drm_plane *plane,
  9933. struct intel_plane_state *state)
  9934. {
  9935. struct drm_device *dev = plane->dev;
  9936. struct drm_i915_private *dev_priv = dev->dev_private;
  9937. struct drm_crtc *crtc = state->base.crtc;
  9938. struct intel_crtc *intel_crtc;
  9939. struct drm_framebuffer *fb = state->base.fb;
  9940. struct drm_rect *dest = &state->dst;
  9941. struct drm_rect *src = &state->src;
  9942. const struct drm_rect *clip = &state->clip;
  9943. int ret;
  9944. crtc = crtc ? crtc : plane->crtc;
  9945. intel_crtc = to_intel_crtc(crtc);
  9946. ret = drm_plane_helper_check_update(plane, crtc, fb,
  9947. src, dest, clip,
  9948. DRM_PLANE_HELPER_NO_SCALING,
  9949. DRM_PLANE_HELPER_NO_SCALING,
  9950. false, true, &state->visible);
  9951. if (ret)
  9952. return ret;
  9953. if (intel_crtc->active) {
  9954. intel_crtc->atomic.wait_for_flips = true;
  9955. /*
  9956. * FBC does not work on some platforms for rotated
  9957. * planes, so disable it when rotation is not 0 and
  9958. * update it when rotation is set back to 0.
  9959. *
  9960. * FIXME: This is redundant with the fbc update done in
  9961. * the primary plane enable function except that that
  9962. * one is done too late. We eventually need to unify
  9963. * this.
  9964. */
  9965. if (intel_crtc->primary_enabled &&
  9966. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9967. dev_priv->fbc.plane == intel_crtc->plane &&
  9968. state->base.rotation != BIT(DRM_ROTATE_0)) {
  9969. intel_crtc->atomic.disable_fbc = true;
  9970. }
  9971. if (state->visible) {
  9972. /*
  9973. * BDW signals flip done immediately if the plane
  9974. * is disabled, even if the plane enable is already
  9975. * armed to occur at the next vblank :(
  9976. */
  9977. if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
  9978. intel_crtc->atomic.wait_vblank = true;
  9979. }
  9980. intel_crtc->atomic.fb_bits |=
  9981. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  9982. intel_crtc->atomic.update_fbc = true;
  9983. }
  9984. return 0;
  9985. }
  9986. static void
  9987. intel_commit_primary_plane(struct drm_plane *plane,
  9988. struct intel_plane_state *state)
  9989. {
  9990. struct drm_crtc *crtc = state->base.crtc;
  9991. struct drm_framebuffer *fb = state->base.fb;
  9992. struct drm_device *dev = plane->dev;
  9993. struct drm_i915_private *dev_priv = dev->dev_private;
  9994. struct intel_crtc *intel_crtc;
  9995. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9996. struct intel_plane *intel_plane = to_intel_plane(plane);
  9997. struct drm_rect *src = &state->src;
  9998. crtc = crtc ? crtc : plane->crtc;
  9999. intel_crtc = to_intel_crtc(crtc);
  10000. plane->fb = fb;
  10001. crtc->x = src->x1 >> 16;
  10002. crtc->y = src->y1 >> 16;
  10003. intel_plane->obj = obj;
  10004. if (intel_crtc->active) {
  10005. if (state->visible) {
  10006. /* FIXME: kill this fastboot hack */
  10007. intel_update_pipe_size(intel_crtc);
  10008. intel_crtc->primary_enabled = true;
  10009. dev_priv->display.update_primary_plane(crtc, plane->fb,
  10010. crtc->x, crtc->y);
  10011. } else {
  10012. /*
  10013. * If clipping results in a non-visible primary plane,
  10014. * we'll disable the primary plane. Note that this is
  10015. * a bit different than what happens if userspace
  10016. * explicitly disables the plane by passing fb=0
  10017. * because plane->fb still gets set and pinned.
  10018. */
  10019. intel_disable_primary_hw_plane(plane, crtc);
  10020. }
  10021. }
  10022. }
  10023. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  10024. {
  10025. struct drm_device *dev = crtc->dev;
  10026. struct drm_i915_private *dev_priv = dev->dev_private;
  10027. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10028. struct intel_plane *intel_plane;
  10029. struct drm_plane *p;
  10030. unsigned fb_bits = 0;
  10031. /* Track fb's for any planes being disabled */
  10032. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  10033. intel_plane = to_intel_plane(p);
  10034. if (intel_crtc->atomic.disabled_planes &
  10035. (1 << drm_plane_index(p))) {
  10036. switch (p->type) {
  10037. case DRM_PLANE_TYPE_PRIMARY:
  10038. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  10039. break;
  10040. case DRM_PLANE_TYPE_CURSOR:
  10041. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  10042. break;
  10043. case DRM_PLANE_TYPE_OVERLAY:
  10044. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  10045. break;
  10046. }
  10047. mutex_lock(&dev->struct_mutex);
  10048. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  10049. mutex_unlock(&dev->struct_mutex);
  10050. }
  10051. }
  10052. if (intel_crtc->atomic.wait_for_flips)
  10053. intel_crtc_wait_for_pending_flips(crtc);
  10054. if (intel_crtc->atomic.disable_fbc)
  10055. intel_fbc_disable(dev);
  10056. if (intel_crtc->atomic.pre_disable_primary)
  10057. intel_pre_disable_primary(crtc);
  10058. if (intel_crtc->atomic.update_wm)
  10059. intel_update_watermarks(crtc);
  10060. intel_runtime_pm_get(dev_priv);
  10061. /* Perform vblank evasion around commit operation */
  10062. if (intel_crtc->active)
  10063. intel_crtc->atomic.evade =
  10064. intel_pipe_update_start(intel_crtc,
  10065. &intel_crtc->atomic.start_vbl_count);
  10066. }
  10067. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  10068. {
  10069. struct drm_device *dev = crtc->dev;
  10070. struct drm_i915_private *dev_priv = dev->dev_private;
  10071. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10072. struct drm_plane *p;
  10073. if (intel_crtc->atomic.evade)
  10074. intel_pipe_update_end(intel_crtc,
  10075. intel_crtc->atomic.start_vbl_count);
  10076. intel_runtime_pm_put(dev_priv);
  10077. if (intel_crtc->atomic.wait_vblank)
  10078. intel_wait_for_vblank(dev, intel_crtc->pipe);
  10079. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  10080. if (intel_crtc->atomic.update_fbc) {
  10081. mutex_lock(&dev->struct_mutex);
  10082. intel_fbc_update(dev);
  10083. mutex_unlock(&dev->struct_mutex);
  10084. }
  10085. if (intel_crtc->atomic.post_enable_primary)
  10086. intel_post_enable_primary(crtc);
  10087. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  10088. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  10089. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  10090. false, false);
  10091. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  10092. }
  10093. /**
  10094. * intel_plane_destroy - destroy a plane
  10095. * @plane: plane to destroy
  10096. *
  10097. * Common destruction function for all types of planes (primary, cursor,
  10098. * sprite).
  10099. */
  10100. void intel_plane_destroy(struct drm_plane *plane)
  10101. {
  10102. struct intel_plane *intel_plane = to_intel_plane(plane);
  10103. drm_plane_cleanup(plane);
  10104. kfree(intel_plane);
  10105. }
  10106. const struct drm_plane_funcs intel_plane_funcs = {
  10107. .update_plane = drm_plane_helper_update,
  10108. .disable_plane = drm_plane_helper_disable,
  10109. .destroy = intel_plane_destroy,
  10110. .set_property = drm_atomic_helper_plane_set_property,
  10111. .atomic_get_property = intel_plane_atomic_get_property,
  10112. .atomic_set_property = intel_plane_atomic_set_property,
  10113. .atomic_duplicate_state = intel_plane_duplicate_state,
  10114. .atomic_destroy_state = intel_plane_destroy_state,
  10115. };
  10116. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  10117. int pipe)
  10118. {
  10119. struct intel_plane *primary;
  10120. struct intel_plane_state *state;
  10121. const uint32_t *intel_primary_formats;
  10122. int num_formats;
  10123. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  10124. if (primary == NULL)
  10125. return NULL;
  10126. state = intel_create_plane_state(&primary->base);
  10127. if (!state) {
  10128. kfree(primary);
  10129. return NULL;
  10130. }
  10131. primary->base.state = &state->base;
  10132. primary->can_scale = false;
  10133. primary->max_downscale = 1;
  10134. primary->pipe = pipe;
  10135. primary->plane = pipe;
  10136. primary->check_plane = intel_check_primary_plane;
  10137. primary->commit_plane = intel_commit_primary_plane;
  10138. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  10139. primary->plane = !pipe;
  10140. if (INTEL_INFO(dev)->gen <= 3) {
  10141. intel_primary_formats = intel_primary_formats_gen2;
  10142. num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
  10143. } else {
  10144. intel_primary_formats = intel_primary_formats_gen4;
  10145. num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
  10146. }
  10147. drm_universal_plane_init(dev, &primary->base, 0,
  10148. &intel_plane_funcs,
  10149. intel_primary_formats, num_formats,
  10150. DRM_PLANE_TYPE_PRIMARY);
  10151. if (INTEL_INFO(dev)->gen >= 4) {
  10152. if (!dev->mode_config.rotation_property)
  10153. dev->mode_config.rotation_property =
  10154. drm_mode_create_rotation_property(dev,
  10155. BIT(DRM_ROTATE_0) |
  10156. BIT(DRM_ROTATE_180));
  10157. if (dev->mode_config.rotation_property)
  10158. drm_object_attach_property(&primary->base.base,
  10159. dev->mode_config.rotation_property,
  10160. state->base.rotation);
  10161. }
  10162. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  10163. return &primary->base;
  10164. }
  10165. static int
  10166. intel_check_cursor_plane(struct drm_plane *plane,
  10167. struct intel_plane_state *state)
  10168. {
  10169. struct drm_crtc *crtc = state->base.crtc;
  10170. struct drm_device *dev = plane->dev;
  10171. struct drm_framebuffer *fb = state->base.fb;
  10172. struct drm_rect *dest = &state->dst;
  10173. struct drm_rect *src = &state->src;
  10174. const struct drm_rect *clip = &state->clip;
  10175. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10176. struct intel_crtc *intel_crtc;
  10177. unsigned stride;
  10178. int ret;
  10179. crtc = crtc ? crtc : plane->crtc;
  10180. intel_crtc = to_intel_crtc(crtc);
  10181. ret = drm_plane_helper_check_update(plane, crtc, fb,
  10182. src, dest, clip,
  10183. DRM_PLANE_HELPER_NO_SCALING,
  10184. DRM_PLANE_HELPER_NO_SCALING,
  10185. true, true, &state->visible);
  10186. if (ret)
  10187. return ret;
  10188. /* if we want to turn off the cursor ignore width and height */
  10189. if (!obj)
  10190. goto finish;
  10191. /* Check for which cursor types we support */
  10192. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  10193. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  10194. state->base.crtc_w, state->base.crtc_h);
  10195. return -EINVAL;
  10196. }
  10197. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  10198. if (obj->base.size < stride * state->base.crtc_h) {
  10199. DRM_DEBUG_KMS("buffer is too small\n");
  10200. return -ENOMEM;
  10201. }
  10202. if (fb == crtc->cursor->fb)
  10203. return 0;
  10204. /* we only need to pin inside GTT if cursor is non-phy */
  10205. mutex_lock(&dev->struct_mutex);
  10206. if (!INTEL_INFO(dev)->cursor_needs_physical && obj->tiling_mode) {
  10207. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  10208. ret = -EINVAL;
  10209. }
  10210. mutex_unlock(&dev->struct_mutex);
  10211. finish:
  10212. if (intel_crtc->active) {
  10213. if (intel_crtc->cursor_width != state->base.crtc_w)
  10214. intel_crtc->atomic.update_wm = true;
  10215. intel_crtc->atomic.fb_bits |=
  10216. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  10217. }
  10218. return ret;
  10219. }
  10220. static void
  10221. intel_commit_cursor_plane(struct drm_plane *plane,
  10222. struct intel_plane_state *state)
  10223. {
  10224. struct drm_crtc *crtc = state->base.crtc;
  10225. struct drm_device *dev = plane->dev;
  10226. struct intel_crtc *intel_crtc;
  10227. struct intel_plane *intel_plane = to_intel_plane(plane);
  10228. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  10229. uint32_t addr;
  10230. crtc = crtc ? crtc : plane->crtc;
  10231. intel_crtc = to_intel_crtc(crtc);
  10232. plane->fb = state->base.fb;
  10233. crtc->cursor_x = state->base.crtc_x;
  10234. crtc->cursor_y = state->base.crtc_y;
  10235. intel_plane->obj = obj;
  10236. if (intel_crtc->cursor_bo == obj)
  10237. goto update;
  10238. if (!obj)
  10239. addr = 0;
  10240. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  10241. addr = i915_gem_obj_ggtt_offset(obj);
  10242. else
  10243. addr = obj->phys_handle->busaddr;
  10244. intel_crtc->cursor_addr = addr;
  10245. intel_crtc->cursor_bo = obj;
  10246. update:
  10247. intel_crtc->cursor_width = state->base.crtc_w;
  10248. intel_crtc->cursor_height = state->base.crtc_h;
  10249. if (intel_crtc->active)
  10250. intel_crtc_update_cursor(crtc, state->visible);
  10251. }
  10252. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  10253. int pipe)
  10254. {
  10255. struct intel_plane *cursor;
  10256. struct intel_plane_state *state;
  10257. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  10258. if (cursor == NULL)
  10259. return NULL;
  10260. state = intel_create_plane_state(&cursor->base);
  10261. if (!state) {
  10262. kfree(cursor);
  10263. return NULL;
  10264. }
  10265. cursor->base.state = &state->base;
  10266. cursor->can_scale = false;
  10267. cursor->max_downscale = 1;
  10268. cursor->pipe = pipe;
  10269. cursor->plane = pipe;
  10270. cursor->check_plane = intel_check_cursor_plane;
  10271. cursor->commit_plane = intel_commit_cursor_plane;
  10272. drm_universal_plane_init(dev, &cursor->base, 0,
  10273. &intel_plane_funcs,
  10274. intel_cursor_formats,
  10275. ARRAY_SIZE(intel_cursor_formats),
  10276. DRM_PLANE_TYPE_CURSOR);
  10277. if (INTEL_INFO(dev)->gen >= 4) {
  10278. if (!dev->mode_config.rotation_property)
  10279. dev->mode_config.rotation_property =
  10280. drm_mode_create_rotation_property(dev,
  10281. BIT(DRM_ROTATE_0) |
  10282. BIT(DRM_ROTATE_180));
  10283. if (dev->mode_config.rotation_property)
  10284. drm_object_attach_property(&cursor->base.base,
  10285. dev->mode_config.rotation_property,
  10286. state->base.rotation);
  10287. }
  10288. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  10289. return &cursor->base;
  10290. }
  10291. static void intel_crtc_init(struct drm_device *dev, int pipe)
  10292. {
  10293. struct drm_i915_private *dev_priv = dev->dev_private;
  10294. struct intel_crtc *intel_crtc;
  10295. struct intel_crtc_state *crtc_state = NULL;
  10296. struct drm_plane *primary = NULL;
  10297. struct drm_plane *cursor = NULL;
  10298. int i, ret;
  10299. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  10300. if (intel_crtc == NULL)
  10301. return;
  10302. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  10303. if (!crtc_state)
  10304. goto fail;
  10305. intel_crtc_set_state(intel_crtc, crtc_state);
  10306. primary = intel_primary_plane_create(dev, pipe);
  10307. if (!primary)
  10308. goto fail;
  10309. cursor = intel_cursor_plane_create(dev, pipe);
  10310. if (!cursor)
  10311. goto fail;
  10312. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  10313. cursor, &intel_crtc_funcs);
  10314. if (ret)
  10315. goto fail;
  10316. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  10317. for (i = 0; i < 256; i++) {
  10318. intel_crtc->lut_r[i] = i;
  10319. intel_crtc->lut_g[i] = i;
  10320. intel_crtc->lut_b[i] = i;
  10321. }
  10322. /*
  10323. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  10324. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  10325. */
  10326. intel_crtc->pipe = pipe;
  10327. intel_crtc->plane = pipe;
  10328. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  10329. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  10330. intel_crtc->plane = !pipe;
  10331. }
  10332. intel_crtc->cursor_base = ~0;
  10333. intel_crtc->cursor_cntl = ~0;
  10334. intel_crtc->cursor_size = ~0;
  10335. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  10336. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  10337. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  10338. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  10339. INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
  10340. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  10341. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  10342. return;
  10343. fail:
  10344. if (primary)
  10345. drm_plane_cleanup(primary);
  10346. if (cursor)
  10347. drm_plane_cleanup(cursor);
  10348. kfree(crtc_state);
  10349. kfree(intel_crtc);
  10350. }
  10351. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  10352. {
  10353. struct drm_encoder *encoder = connector->base.encoder;
  10354. struct drm_device *dev = connector->base.dev;
  10355. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  10356. if (!encoder || WARN_ON(!encoder->crtc))
  10357. return INVALID_PIPE;
  10358. return to_intel_crtc(encoder->crtc)->pipe;
  10359. }
  10360. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  10361. struct drm_file *file)
  10362. {
  10363. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  10364. struct drm_crtc *drmmode_crtc;
  10365. struct intel_crtc *crtc;
  10366. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  10367. return -ENODEV;
  10368. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  10369. if (!drmmode_crtc) {
  10370. DRM_ERROR("no such CRTC id\n");
  10371. return -ENOENT;
  10372. }
  10373. crtc = to_intel_crtc(drmmode_crtc);
  10374. pipe_from_crtc_id->pipe = crtc->pipe;
  10375. return 0;
  10376. }
  10377. static int intel_encoder_clones(struct intel_encoder *encoder)
  10378. {
  10379. struct drm_device *dev = encoder->base.dev;
  10380. struct intel_encoder *source_encoder;
  10381. int index_mask = 0;
  10382. int entry = 0;
  10383. for_each_intel_encoder(dev, source_encoder) {
  10384. if (encoders_cloneable(encoder, source_encoder))
  10385. index_mask |= (1 << entry);
  10386. entry++;
  10387. }
  10388. return index_mask;
  10389. }
  10390. static bool has_edp_a(struct drm_device *dev)
  10391. {
  10392. struct drm_i915_private *dev_priv = dev->dev_private;
  10393. if (!IS_MOBILE(dev))
  10394. return false;
  10395. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  10396. return false;
  10397. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  10398. return false;
  10399. return true;
  10400. }
  10401. static bool intel_crt_present(struct drm_device *dev)
  10402. {
  10403. struct drm_i915_private *dev_priv = dev->dev_private;
  10404. if (INTEL_INFO(dev)->gen >= 9)
  10405. return false;
  10406. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  10407. return false;
  10408. if (IS_CHERRYVIEW(dev))
  10409. return false;
  10410. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  10411. return false;
  10412. return true;
  10413. }
  10414. static void intel_setup_outputs(struct drm_device *dev)
  10415. {
  10416. struct drm_i915_private *dev_priv = dev->dev_private;
  10417. struct intel_encoder *encoder;
  10418. struct drm_connector *connector;
  10419. bool dpd_is_edp = false;
  10420. intel_lvds_init(dev);
  10421. if (intel_crt_present(dev))
  10422. intel_crt_init(dev);
  10423. if (HAS_DDI(dev)) {
  10424. int found;
  10425. /* Haswell uses DDI functions to detect digital outputs */
  10426. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  10427. /* DDI A only supports eDP */
  10428. if (found)
  10429. intel_ddi_init(dev, PORT_A);
  10430. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  10431. * register */
  10432. found = I915_READ(SFUSE_STRAP);
  10433. if (found & SFUSE_STRAP_DDIB_DETECTED)
  10434. intel_ddi_init(dev, PORT_B);
  10435. if (found & SFUSE_STRAP_DDIC_DETECTED)
  10436. intel_ddi_init(dev, PORT_C);
  10437. if (found & SFUSE_STRAP_DDID_DETECTED)
  10438. intel_ddi_init(dev, PORT_D);
  10439. } else if (HAS_PCH_SPLIT(dev)) {
  10440. int found;
  10441. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  10442. if (has_edp_a(dev))
  10443. intel_dp_init(dev, DP_A, PORT_A);
  10444. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  10445. /* PCH SDVOB multiplex with HDMIB */
  10446. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  10447. if (!found)
  10448. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  10449. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  10450. intel_dp_init(dev, PCH_DP_B, PORT_B);
  10451. }
  10452. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  10453. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  10454. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  10455. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  10456. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  10457. intel_dp_init(dev, PCH_DP_C, PORT_C);
  10458. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  10459. intel_dp_init(dev, PCH_DP_D, PORT_D);
  10460. } else if (IS_VALLEYVIEW(dev)) {
  10461. /*
  10462. * The DP_DETECTED bit is the latched state of the DDC
  10463. * SDA pin at boot. However since eDP doesn't require DDC
  10464. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  10465. * eDP ports may have been muxed to an alternate function.
  10466. * Thus we can't rely on the DP_DETECTED bit alone to detect
  10467. * eDP ports. Consult the VBT as well as DP_DETECTED to
  10468. * detect eDP ports.
  10469. */
  10470. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  10471. !intel_dp_is_edp(dev, PORT_B))
  10472. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  10473. PORT_B);
  10474. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  10475. intel_dp_is_edp(dev, PORT_B))
  10476. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  10477. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  10478. !intel_dp_is_edp(dev, PORT_C))
  10479. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  10480. PORT_C);
  10481. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  10482. intel_dp_is_edp(dev, PORT_C))
  10483. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  10484. if (IS_CHERRYVIEW(dev)) {
  10485. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  10486. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  10487. PORT_D);
  10488. /* eDP not supported on port D, so don't check VBT */
  10489. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  10490. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  10491. }
  10492. intel_dsi_init(dev);
  10493. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  10494. bool found = false;
  10495. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10496. DRM_DEBUG_KMS("probing SDVOB\n");
  10497. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  10498. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  10499. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  10500. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  10501. }
  10502. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  10503. intel_dp_init(dev, DP_B, PORT_B);
  10504. }
  10505. /* Before G4X SDVOC doesn't have its own detect register */
  10506. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  10507. DRM_DEBUG_KMS("probing SDVOC\n");
  10508. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  10509. }
  10510. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  10511. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  10512. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  10513. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  10514. }
  10515. if (SUPPORTS_INTEGRATED_DP(dev))
  10516. intel_dp_init(dev, DP_C, PORT_C);
  10517. }
  10518. if (SUPPORTS_INTEGRATED_DP(dev) &&
  10519. (I915_READ(DP_D) & DP_DETECTED))
  10520. intel_dp_init(dev, DP_D, PORT_D);
  10521. } else if (IS_GEN2(dev))
  10522. intel_dvo_init(dev);
  10523. if (SUPPORTS_TV(dev))
  10524. intel_tv_init(dev);
  10525. /*
  10526. * FIXME: We don't have full atomic support yet, but we want to be
  10527. * able to enable/test plane updates via the atomic interface in the
  10528. * meantime. However as soon as we flip DRIVER_ATOMIC on, the DRM core
  10529. * will take some atomic codepaths to lookup properties during
  10530. * drmModeGetConnector() that unconditionally dereference
  10531. * connector->state.
  10532. *
  10533. * We create a dummy connector state here for each connector to ensure
  10534. * the DRM core doesn't try to dereference a NULL connector->state.
  10535. * The actual connector properties will never be updated or contain
  10536. * useful information, but since we're doing this specifically for
  10537. * testing/debug of the plane operations (and only when a specific
  10538. * kernel module option is given), that shouldn't really matter.
  10539. *
  10540. * Once atomic support for crtc's + connectors lands, this loop should
  10541. * be removed since we'll be setting up real connector state, which
  10542. * will contain Intel-specific properties.
  10543. */
  10544. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  10545. list_for_each_entry(connector,
  10546. &dev->mode_config.connector_list,
  10547. head) {
  10548. if (!WARN_ON(connector->state)) {
  10549. connector->state =
  10550. kzalloc(sizeof(*connector->state),
  10551. GFP_KERNEL);
  10552. }
  10553. }
  10554. }
  10555. intel_psr_init(dev);
  10556. for_each_intel_encoder(dev, encoder) {
  10557. encoder->base.possible_crtcs = encoder->crtc_mask;
  10558. encoder->base.possible_clones =
  10559. intel_encoder_clones(encoder);
  10560. }
  10561. intel_init_pch_refclk(dev);
  10562. drm_helper_move_panel_connectors_to_head(dev);
  10563. }
  10564. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  10565. {
  10566. struct drm_device *dev = fb->dev;
  10567. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10568. drm_framebuffer_cleanup(fb);
  10569. mutex_lock(&dev->struct_mutex);
  10570. WARN_ON(!intel_fb->obj->framebuffer_references--);
  10571. drm_gem_object_unreference(&intel_fb->obj->base);
  10572. mutex_unlock(&dev->struct_mutex);
  10573. kfree(intel_fb);
  10574. }
  10575. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  10576. struct drm_file *file,
  10577. unsigned int *handle)
  10578. {
  10579. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  10580. struct drm_i915_gem_object *obj = intel_fb->obj;
  10581. return drm_gem_handle_create(file, &obj->base, handle);
  10582. }
  10583. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  10584. .destroy = intel_user_framebuffer_destroy,
  10585. .create_handle = intel_user_framebuffer_create_handle,
  10586. };
  10587. static int intel_framebuffer_init(struct drm_device *dev,
  10588. struct intel_framebuffer *intel_fb,
  10589. struct drm_mode_fb_cmd2 *mode_cmd,
  10590. struct drm_i915_gem_object *obj)
  10591. {
  10592. int aligned_height;
  10593. int pitch_limit;
  10594. int ret;
  10595. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  10596. if (obj->tiling_mode == I915_TILING_Y) {
  10597. DRM_DEBUG("hardware does not support tiling Y\n");
  10598. return -EINVAL;
  10599. }
  10600. if (mode_cmd->pitches[0] & 63) {
  10601. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  10602. mode_cmd->pitches[0]);
  10603. return -EINVAL;
  10604. }
  10605. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  10606. pitch_limit = 32*1024;
  10607. } else if (INTEL_INFO(dev)->gen >= 4) {
  10608. if (obj->tiling_mode)
  10609. pitch_limit = 16*1024;
  10610. else
  10611. pitch_limit = 32*1024;
  10612. } else if (INTEL_INFO(dev)->gen >= 3) {
  10613. if (obj->tiling_mode)
  10614. pitch_limit = 8*1024;
  10615. else
  10616. pitch_limit = 16*1024;
  10617. } else
  10618. /* XXX DSPC is limited to 4k tiled */
  10619. pitch_limit = 8*1024;
  10620. if (mode_cmd->pitches[0] > pitch_limit) {
  10621. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  10622. obj->tiling_mode ? "tiled" : "linear",
  10623. mode_cmd->pitches[0], pitch_limit);
  10624. return -EINVAL;
  10625. }
  10626. if (obj->tiling_mode != I915_TILING_NONE &&
  10627. mode_cmd->pitches[0] != obj->stride) {
  10628. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  10629. mode_cmd->pitches[0], obj->stride);
  10630. return -EINVAL;
  10631. }
  10632. /* Reject formats not supported by any plane early. */
  10633. switch (mode_cmd->pixel_format) {
  10634. case DRM_FORMAT_C8:
  10635. case DRM_FORMAT_RGB565:
  10636. case DRM_FORMAT_XRGB8888:
  10637. case DRM_FORMAT_ARGB8888:
  10638. break;
  10639. case DRM_FORMAT_XRGB1555:
  10640. case DRM_FORMAT_ARGB1555:
  10641. if (INTEL_INFO(dev)->gen > 3) {
  10642. DRM_DEBUG("unsupported pixel format: %s\n",
  10643. drm_get_format_name(mode_cmd->pixel_format));
  10644. return -EINVAL;
  10645. }
  10646. break;
  10647. case DRM_FORMAT_XBGR8888:
  10648. case DRM_FORMAT_ABGR8888:
  10649. case DRM_FORMAT_XRGB2101010:
  10650. case DRM_FORMAT_ARGB2101010:
  10651. case DRM_FORMAT_XBGR2101010:
  10652. case DRM_FORMAT_ABGR2101010:
  10653. if (INTEL_INFO(dev)->gen < 4) {
  10654. DRM_DEBUG("unsupported pixel format: %s\n",
  10655. drm_get_format_name(mode_cmd->pixel_format));
  10656. return -EINVAL;
  10657. }
  10658. break;
  10659. case DRM_FORMAT_YUYV:
  10660. case DRM_FORMAT_UYVY:
  10661. case DRM_FORMAT_YVYU:
  10662. case DRM_FORMAT_VYUY:
  10663. if (INTEL_INFO(dev)->gen < 5) {
  10664. DRM_DEBUG("unsupported pixel format: %s\n",
  10665. drm_get_format_name(mode_cmd->pixel_format));
  10666. return -EINVAL;
  10667. }
  10668. break;
  10669. default:
  10670. DRM_DEBUG("unsupported pixel format: %s\n",
  10671. drm_get_format_name(mode_cmd->pixel_format));
  10672. return -EINVAL;
  10673. }
  10674. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  10675. if (mode_cmd->offsets[0] != 0)
  10676. return -EINVAL;
  10677. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  10678. obj->tiling_mode);
  10679. /* FIXME drm helper for size checks (especially planar formats)? */
  10680. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  10681. return -EINVAL;
  10682. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  10683. intel_fb->obj = obj;
  10684. intel_fb->obj->framebuffer_references++;
  10685. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  10686. if (ret) {
  10687. DRM_ERROR("framebuffer init failed %d\n", ret);
  10688. return ret;
  10689. }
  10690. return 0;
  10691. }
  10692. static struct drm_framebuffer *
  10693. intel_user_framebuffer_create(struct drm_device *dev,
  10694. struct drm_file *filp,
  10695. struct drm_mode_fb_cmd2 *mode_cmd)
  10696. {
  10697. struct drm_i915_gem_object *obj;
  10698. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  10699. mode_cmd->handles[0]));
  10700. if (&obj->base == NULL)
  10701. return ERR_PTR(-ENOENT);
  10702. return intel_framebuffer_create(dev, mode_cmd, obj);
  10703. }
  10704. #ifndef CONFIG_DRM_I915_FBDEV
  10705. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  10706. {
  10707. }
  10708. #endif
  10709. static const struct drm_mode_config_funcs intel_mode_funcs = {
  10710. .fb_create = intel_user_framebuffer_create,
  10711. .output_poll_changed = intel_fbdev_output_poll_changed,
  10712. .atomic_check = intel_atomic_check,
  10713. .atomic_commit = intel_atomic_commit,
  10714. };
  10715. /* Set up chip specific display functions */
  10716. static void intel_init_display(struct drm_device *dev)
  10717. {
  10718. struct drm_i915_private *dev_priv = dev->dev_private;
  10719. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  10720. dev_priv->display.find_dpll = g4x_find_best_dpll;
  10721. else if (IS_CHERRYVIEW(dev))
  10722. dev_priv->display.find_dpll = chv_find_best_dpll;
  10723. else if (IS_VALLEYVIEW(dev))
  10724. dev_priv->display.find_dpll = vlv_find_best_dpll;
  10725. else if (IS_PINEVIEW(dev))
  10726. dev_priv->display.find_dpll = pnv_find_best_dpll;
  10727. else
  10728. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  10729. if (INTEL_INFO(dev)->gen >= 9) {
  10730. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10731. dev_priv->display.get_initial_plane_config =
  10732. skylake_get_initial_plane_config;
  10733. dev_priv->display.crtc_compute_clock =
  10734. haswell_crtc_compute_clock;
  10735. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10736. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10737. dev_priv->display.off = ironlake_crtc_off;
  10738. dev_priv->display.update_primary_plane =
  10739. skylake_update_primary_plane;
  10740. } else if (HAS_DDI(dev)) {
  10741. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  10742. dev_priv->display.get_initial_plane_config =
  10743. ironlake_get_initial_plane_config;
  10744. dev_priv->display.crtc_compute_clock =
  10745. haswell_crtc_compute_clock;
  10746. dev_priv->display.crtc_enable = haswell_crtc_enable;
  10747. dev_priv->display.crtc_disable = haswell_crtc_disable;
  10748. dev_priv->display.off = ironlake_crtc_off;
  10749. dev_priv->display.update_primary_plane =
  10750. ironlake_update_primary_plane;
  10751. } else if (HAS_PCH_SPLIT(dev)) {
  10752. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  10753. dev_priv->display.get_initial_plane_config =
  10754. ironlake_get_initial_plane_config;
  10755. dev_priv->display.crtc_compute_clock =
  10756. ironlake_crtc_compute_clock;
  10757. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  10758. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  10759. dev_priv->display.off = ironlake_crtc_off;
  10760. dev_priv->display.update_primary_plane =
  10761. ironlake_update_primary_plane;
  10762. } else if (IS_VALLEYVIEW(dev)) {
  10763. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10764. dev_priv->display.get_initial_plane_config =
  10765. i9xx_get_initial_plane_config;
  10766. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10767. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  10768. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10769. dev_priv->display.off = i9xx_crtc_off;
  10770. dev_priv->display.update_primary_plane =
  10771. i9xx_update_primary_plane;
  10772. } else {
  10773. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  10774. dev_priv->display.get_initial_plane_config =
  10775. i9xx_get_initial_plane_config;
  10776. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  10777. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  10778. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  10779. dev_priv->display.off = i9xx_crtc_off;
  10780. dev_priv->display.update_primary_plane =
  10781. i9xx_update_primary_plane;
  10782. }
  10783. /* Returns the core display clock speed */
  10784. if (IS_VALLEYVIEW(dev))
  10785. dev_priv->display.get_display_clock_speed =
  10786. valleyview_get_display_clock_speed;
  10787. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  10788. dev_priv->display.get_display_clock_speed =
  10789. i945_get_display_clock_speed;
  10790. else if (IS_I915G(dev))
  10791. dev_priv->display.get_display_clock_speed =
  10792. i915_get_display_clock_speed;
  10793. else if (IS_I945GM(dev) || IS_845G(dev))
  10794. dev_priv->display.get_display_clock_speed =
  10795. i9xx_misc_get_display_clock_speed;
  10796. else if (IS_PINEVIEW(dev))
  10797. dev_priv->display.get_display_clock_speed =
  10798. pnv_get_display_clock_speed;
  10799. else if (IS_I915GM(dev))
  10800. dev_priv->display.get_display_clock_speed =
  10801. i915gm_get_display_clock_speed;
  10802. else if (IS_I865G(dev))
  10803. dev_priv->display.get_display_clock_speed =
  10804. i865_get_display_clock_speed;
  10805. else if (IS_I85X(dev))
  10806. dev_priv->display.get_display_clock_speed =
  10807. i855_get_display_clock_speed;
  10808. else /* 852, 830 */
  10809. dev_priv->display.get_display_clock_speed =
  10810. i830_get_display_clock_speed;
  10811. if (IS_GEN5(dev)) {
  10812. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  10813. } else if (IS_GEN6(dev)) {
  10814. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  10815. } else if (IS_IVYBRIDGE(dev)) {
  10816. /* FIXME: detect B0+ stepping and use auto training */
  10817. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  10818. dev_priv->display.modeset_global_resources =
  10819. ivb_modeset_global_resources;
  10820. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  10821. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  10822. } else if (IS_VALLEYVIEW(dev)) {
  10823. dev_priv->display.modeset_global_resources =
  10824. valleyview_modeset_global_resources;
  10825. }
  10826. /* Default just returns -ENODEV to indicate unsupported */
  10827. dev_priv->display.queue_flip = intel_default_queue_flip;
  10828. switch (INTEL_INFO(dev)->gen) {
  10829. case 2:
  10830. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  10831. break;
  10832. case 3:
  10833. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  10834. break;
  10835. case 4:
  10836. case 5:
  10837. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  10838. break;
  10839. case 6:
  10840. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  10841. break;
  10842. case 7:
  10843. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  10844. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  10845. break;
  10846. case 9:
  10847. dev_priv->display.queue_flip = intel_gen9_queue_flip;
  10848. break;
  10849. }
  10850. intel_panel_init_backlight_funcs(dev);
  10851. mutex_init(&dev_priv->pps_mutex);
  10852. }
  10853. /*
  10854. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  10855. * resume, or other times. This quirk makes sure that's the case for
  10856. * affected systems.
  10857. */
  10858. static void quirk_pipea_force(struct drm_device *dev)
  10859. {
  10860. struct drm_i915_private *dev_priv = dev->dev_private;
  10861. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  10862. DRM_INFO("applying pipe a force quirk\n");
  10863. }
  10864. static void quirk_pipeb_force(struct drm_device *dev)
  10865. {
  10866. struct drm_i915_private *dev_priv = dev->dev_private;
  10867. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  10868. DRM_INFO("applying pipe b force quirk\n");
  10869. }
  10870. /*
  10871. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  10872. */
  10873. static void quirk_ssc_force_disable(struct drm_device *dev)
  10874. {
  10875. struct drm_i915_private *dev_priv = dev->dev_private;
  10876. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  10877. DRM_INFO("applying lvds SSC disable quirk\n");
  10878. }
  10879. /*
  10880. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  10881. * brightness value
  10882. */
  10883. static void quirk_invert_brightness(struct drm_device *dev)
  10884. {
  10885. struct drm_i915_private *dev_priv = dev->dev_private;
  10886. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  10887. DRM_INFO("applying inverted panel brightness quirk\n");
  10888. }
  10889. /* Some VBT's incorrectly indicate no backlight is present */
  10890. static void quirk_backlight_present(struct drm_device *dev)
  10891. {
  10892. struct drm_i915_private *dev_priv = dev->dev_private;
  10893. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  10894. DRM_INFO("applying backlight present quirk\n");
  10895. }
  10896. struct intel_quirk {
  10897. int device;
  10898. int subsystem_vendor;
  10899. int subsystem_device;
  10900. void (*hook)(struct drm_device *dev);
  10901. };
  10902. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  10903. struct intel_dmi_quirk {
  10904. void (*hook)(struct drm_device *dev);
  10905. const struct dmi_system_id (*dmi_id_list)[];
  10906. };
  10907. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  10908. {
  10909. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  10910. return 1;
  10911. }
  10912. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  10913. {
  10914. .dmi_id_list = &(const struct dmi_system_id[]) {
  10915. {
  10916. .callback = intel_dmi_reverse_brightness,
  10917. .ident = "NCR Corporation",
  10918. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  10919. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  10920. },
  10921. },
  10922. { } /* terminating entry */
  10923. },
  10924. .hook = quirk_invert_brightness,
  10925. },
  10926. };
  10927. static struct intel_quirk intel_quirks[] = {
  10928. /* HP Mini needs pipe A force quirk (LP: #322104) */
  10929. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  10930. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  10931. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  10932. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  10933. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  10934. /* 830 needs to leave pipe A & dpll A up */
  10935. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  10936. /* 830 needs to leave pipe B & dpll B up */
  10937. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  10938. /* Lenovo U160 cannot use SSC on LVDS */
  10939. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  10940. /* Sony Vaio Y cannot use SSC on LVDS */
  10941. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  10942. /* Acer Aspire 5734Z must invert backlight brightness */
  10943. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  10944. /* Acer/eMachines G725 */
  10945. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  10946. /* Acer/eMachines e725 */
  10947. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  10948. /* Acer/Packard Bell NCL20 */
  10949. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  10950. /* Acer Aspire 4736Z */
  10951. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  10952. /* Acer Aspire 5336 */
  10953. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  10954. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  10955. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  10956. /* Acer C720 Chromebook (Core i3 4005U) */
  10957. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  10958. /* Apple Macbook 2,1 (Core 2 T7400) */
  10959. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  10960. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  10961. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  10962. /* HP Chromebook 14 (Celeron 2955U) */
  10963. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  10964. /* Dell Chromebook 11 */
  10965. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  10966. };
  10967. static void intel_init_quirks(struct drm_device *dev)
  10968. {
  10969. struct pci_dev *d = dev->pdev;
  10970. int i;
  10971. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  10972. struct intel_quirk *q = &intel_quirks[i];
  10973. if (d->device == q->device &&
  10974. (d->subsystem_vendor == q->subsystem_vendor ||
  10975. q->subsystem_vendor == PCI_ANY_ID) &&
  10976. (d->subsystem_device == q->subsystem_device ||
  10977. q->subsystem_device == PCI_ANY_ID))
  10978. q->hook(dev);
  10979. }
  10980. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  10981. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  10982. intel_dmi_quirks[i].hook(dev);
  10983. }
  10984. }
  10985. /* Disable the VGA plane that we never use */
  10986. static void i915_disable_vga(struct drm_device *dev)
  10987. {
  10988. struct drm_i915_private *dev_priv = dev->dev_private;
  10989. u8 sr1;
  10990. u32 vga_reg = i915_vgacntrl_reg(dev);
  10991. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  10992. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  10993. outb(SR01, VGA_SR_INDEX);
  10994. sr1 = inb(VGA_SR_DATA);
  10995. outb(sr1 | 1<<5, VGA_SR_DATA);
  10996. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  10997. udelay(300);
  10998. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  10999. POSTING_READ(vga_reg);
  11000. }
  11001. void intel_modeset_init_hw(struct drm_device *dev)
  11002. {
  11003. intel_prepare_ddi(dev);
  11004. if (IS_VALLEYVIEW(dev))
  11005. vlv_update_cdclk(dev);
  11006. intel_init_clock_gating(dev);
  11007. intel_enable_gt_powersave(dev);
  11008. }
  11009. void intel_modeset_init(struct drm_device *dev)
  11010. {
  11011. struct drm_i915_private *dev_priv = dev->dev_private;
  11012. int sprite, ret;
  11013. enum pipe pipe;
  11014. struct intel_crtc *crtc;
  11015. drm_mode_config_init(dev);
  11016. dev->mode_config.min_width = 0;
  11017. dev->mode_config.min_height = 0;
  11018. dev->mode_config.preferred_depth = 24;
  11019. dev->mode_config.prefer_shadow = 1;
  11020. dev->mode_config.funcs = &intel_mode_funcs;
  11021. intel_init_quirks(dev);
  11022. intel_init_pm(dev);
  11023. if (INTEL_INFO(dev)->num_pipes == 0)
  11024. return;
  11025. intel_init_display(dev);
  11026. intel_init_audio(dev);
  11027. if (IS_GEN2(dev)) {
  11028. dev->mode_config.max_width = 2048;
  11029. dev->mode_config.max_height = 2048;
  11030. } else if (IS_GEN3(dev)) {
  11031. dev->mode_config.max_width = 4096;
  11032. dev->mode_config.max_height = 4096;
  11033. } else {
  11034. dev->mode_config.max_width = 8192;
  11035. dev->mode_config.max_height = 8192;
  11036. }
  11037. if (IS_845G(dev) || IS_I865G(dev)) {
  11038. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  11039. dev->mode_config.cursor_height = 1023;
  11040. } else if (IS_GEN2(dev)) {
  11041. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  11042. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  11043. } else {
  11044. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  11045. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  11046. }
  11047. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  11048. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  11049. INTEL_INFO(dev)->num_pipes,
  11050. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  11051. for_each_pipe(dev_priv, pipe) {
  11052. intel_crtc_init(dev, pipe);
  11053. for_each_sprite(pipe, sprite) {
  11054. ret = intel_plane_init(dev, pipe, sprite);
  11055. if (ret)
  11056. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  11057. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  11058. }
  11059. }
  11060. intel_init_dpio(dev);
  11061. intel_shared_dpll_init(dev);
  11062. /* Just disable it once at startup */
  11063. i915_disable_vga(dev);
  11064. intel_setup_outputs(dev);
  11065. /* Just in case the BIOS is doing something questionable. */
  11066. intel_fbc_disable(dev);
  11067. drm_modeset_lock_all(dev);
  11068. intel_modeset_setup_hw_state(dev, false);
  11069. drm_modeset_unlock_all(dev);
  11070. for_each_intel_crtc(dev, crtc) {
  11071. if (!crtc->active)
  11072. continue;
  11073. /*
  11074. * Note that reserving the BIOS fb up front prevents us
  11075. * from stuffing other stolen allocations like the ring
  11076. * on top. This prevents some ugliness at boot time, and
  11077. * can even allow for smooth boot transitions if the BIOS
  11078. * fb is large enough for the active pipe configuration.
  11079. */
  11080. if (dev_priv->display.get_initial_plane_config) {
  11081. dev_priv->display.get_initial_plane_config(crtc,
  11082. &crtc->plane_config);
  11083. /*
  11084. * If the fb is shared between multiple heads, we'll
  11085. * just get the first one.
  11086. */
  11087. intel_find_plane_obj(crtc, &crtc->plane_config);
  11088. }
  11089. }
  11090. }
  11091. static void intel_enable_pipe_a(struct drm_device *dev)
  11092. {
  11093. struct intel_connector *connector;
  11094. struct drm_connector *crt = NULL;
  11095. struct intel_load_detect_pipe load_detect_temp;
  11096. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  11097. /* We can't just switch on the pipe A, we need to set things up with a
  11098. * proper mode and output configuration. As a gross hack, enable pipe A
  11099. * by enabling the load detect pipe once. */
  11100. list_for_each_entry(connector,
  11101. &dev->mode_config.connector_list,
  11102. base.head) {
  11103. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  11104. crt = &connector->base;
  11105. break;
  11106. }
  11107. }
  11108. if (!crt)
  11109. return;
  11110. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  11111. intel_release_load_detect_pipe(crt, &load_detect_temp);
  11112. }
  11113. static bool
  11114. intel_check_plane_mapping(struct intel_crtc *crtc)
  11115. {
  11116. struct drm_device *dev = crtc->base.dev;
  11117. struct drm_i915_private *dev_priv = dev->dev_private;
  11118. u32 reg, val;
  11119. if (INTEL_INFO(dev)->num_pipes == 1)
  11120. return true;
  11121. reg = DSPCNTR(!crtc->plane);
  11122. val = I915_READ(reg);
  11123. if ((val & DISPLAY_PLANE_ENABLE) &&
  11124. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  11125. return false;
  11126. return true;
  11127. }
  11128. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  11129. {
  11130. struct drm_device *dev = crtc->base.dev;
  11131. struct drm_i915_private *dev_priv = dev->dev_private;
  11132. u32 reg;
  11133. /* Clear any frame start delays used for debugging left by the BIOS */
  11134. reg = PIPECONF(crtc->config->cpu_transcoder);
  11135. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  11136. /* restore vblank interrupts to correct state */
  11137. if (crtc->active) {
  11138. update_scanline_offset(crtc);
  11139. drm_vblank_on(dev, crtc->pipe);
  11140. } else
  11141. drm_vblank_off(dev, crtc->pipe);
  11142. /* We need to sanitize the plane -> pipe mapping first because this will
  11143. * disable the crtc (and hence change the state) if it is wrong. Note
  11144. * that gen4+ has a fixed plane -> pipe mapping. */
  11145. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  11146. struct intel_connector *connector;
  11147. bool plane;
  11148. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  11149. crtc->base.base.id);
  11150. /* Pipe has the wrong plane attached and the plane is active.
  11151. * Temporarily change the plane mapping and disable everything
  11152. * ... */
  11153. plane = crtc->plane;
  11154. crtc->plane = !plane;
  11155. crtc->primary_enabled = true;
  11156. dev_priv->display.crtc_disable(&crtc->base);
  11157. crtc->plane = plane;
  11158. /* ... and break all links. */
  11159. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11160. base.head) {
  11161. if (connector->encoder->base.crtc != &crtc->base)
  11162. continue;
  11163. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11164. connector->base.encoder = NULL;
  11165. }
  11166. /* multiple connectors may have the same encoder:
  11167. * handle them and break crtc link separately */
  11168. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11169. base.head)
  11170. if (connector->encoder->base.crtc == &crtc->base) {
  11171. connector->encoder->base.crtc = NULL;
  11172. connector->encoder->connectors_active = false;
  11173. }
  11174. WARN_ON(crtc->active);
  11175. crtc->base.enabled = false;
  11176. }
  11177. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  11178. crtc->pipe == PIPE_A && !crtc->active) {
  11179. /* BIOS forgot to enable pipe A, this mostly happens after
  11180. * resume. Force-enable the pipe to fix this, the update_dpms
  11181. * call below we restore the pipe to the right state, but leave
  11182. * the required bits on. */
  11183. intel_enable_pipe_a(dev);
  11184. }
  11185. /* Adjust the state of the output pipe according to whether we
  11186. * have active connectors/encoders. */
  11187. intel_crtc_update_dpms(&crtc->base);
  11188. if (crtc->active != crtc->base.enabled) {
  11189. struct intel_encoder *encoder;
  11190. /* This can happen either due to bugs in the get_hw_state
  11191. * functions or because the pipe is force-enabled due to the
  11192. * pipe A quirk. */
  11193. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  11194. crtc->base.base.id,
  11195. crtc->base.enabled ? "enabled" : "disabled",
  11196. crtc->active ? "enabled" : "disabled");
  11197. crtc->base.enabled = crtc->active;
  11198. /* Because we only establish the connector -> encoder ->
  11199. * crtc links if something is active, this means the
  11200. * crtc is now deactivated. Break the links. connector
  11201. * -> encoder links are only establish when things are
  11202. * actually up, hence no need to break them. */
  11203. WARN_ON(crtc->active);
  11204. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  11205. WARN_ON(encoder->connectors_active);
  11206. encoder->base.crtc = NULL;
  11207. }
  11208. }
  11209. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  11210. /*
  11211. * We start out with underrun reporting disabled to avoid races.
  11212. * For correct bookkeeping mark this on active crtcs.
  11213. *
  11214. * Also on gmch platforms we dont have any hardware bits to
  11215. * disable the underrun reporting. Which means we need to start
  11216. * out with underrun reporting disabled also on inactive pipes,
  11217. * since otherwise we'll complain about the garbage we read when
  11218. * e.g. coming up after runtime pm.
  11219. *
  11220. * No protection against concurrent access is required - at
  11221. * worst a fifo underrun happens which also sets this to false.
  11222. */
  11223. crtc->cpu_fifo_underrun_disabled = true;
  11224. crtc->pch_fifo_underrun_disabled = true;
  11225. }
  11226. }
  11227. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  11228. {
  11229. struct intel_connector *connector;
  11230. struct drm_device *dev = encoder->base.dev;
  11231. /* We need to check both for a crtc link (meaning that the
  11232. * encoder is active and trying to read from a pipe) and the
  11233. * pipe itself being active. */
  11234. bool has_active_crtc = encoder->base.crtc &&
  11235. to_intel_crtc(encoder->base.crtc)->active;
  11236. if (encoder->connectors_active && !has_active_crtc) {
  11237. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  11238. encoder->base.base.id,
  11239. encoder->base.name);
  11240. /* Connector is active, but has no active pipe. This is
  11241. * fallout from our resume register restoring. Disable
  11242. * the encoder manually again. */
  11243. if (encoder->base.crtc) {
  11244. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  11245. encoder->base.base.id,
  11246. encoder->base.name);
  11247. encoder->disable(encoder);
  11248. if (encoder->post_disable)
  11249. encoder->post_disable(encoder);
  11250. }
  11251. encoder->base.crtc = NULL;
  11252. encoder->connectors_active = false;
  11253. /* Inconsistent output/port/pipe state happens presumably due to
  11254. * a bug in one of the get_hw_state functions. Or someplace else
  11255. * in our code, like the register restore mess on resume. Clamp
  11256. * things to off as a safer default. */
  11257. list_for_each_entry(connector,
  11258. &dev->mode_config.connector_list,
  11259. base.head) {
  11260. if (connector->encoder != encoder)
  11261. continue;
  11262. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11263. connector->base.encoder = NULL;
  11264. }
  11265. }
  11266. /* Enabled encoders without active connectors will be fixed in
  11267. * the crtc fixup. */
  11268. }
  11269. void i915_redisable_vga_power_on(struct drm_device *dev)
  11270. {
  11271. struct drm_i915_private *dev_priv = dev->dev_private;
  11272. u32 vga_reg = i915_vgacntrl_reg(dev);
  11273. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  11274. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  11275. i915_disable_vga(dev);
  11276. }
  11277. }
  11278. void i915_redisable_vga(struct drm_device *dev)
  11279. {
  11280. struct drm_i915_private *dev_priv = dev->dev_private;
  11281. /* This function can be called both from intel_modeset_setup_hw_state or
  11282. * at a very early point in our resume sequence, where the power well
  11283. * structures are not yet restored. Since this function is at a very
  11284. * paranoid "someone might have enabled VGA while we were not looking"
  11285. * level, just check if the power well is enabled instead of trying to
  11286. * follow the "don't touch the power well if we don't need it" policy
  11287. * the rest of the driver uses. */
  11288. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  11289. return;
  11290. i915_redisable_vga_power_on(dev);
  11291. }
  11292. static bool primary_get_hw_state(struct intel_crtc *crtc)
  11293. {
  11294. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  11295. if (!crtc->active)
  11296. return false;
  11297. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  11298. }
  11299. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  11300. {
  11301. struct drm_i915_private *dev_priv = dev->dev_private;
  11302. enum pipe pipe;
  11303. struct intel_crtc *crtc;
  11304. struct intel_encoder *encoder;
  11305. struct intel_connector *connector;
  11306. int i;
  11307. for_each_intel_crtc(dev, crtc) {
  11308. memset(crtc->config, 0, sizeof(*crtc->config));
  11309. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  11310. crtc->active = dev_priv->display.get_pipe_config(crtc,
  11311. crtc->config);
  11312. crtc->base.enabled = crtc->active;
  11313. crtc->primary_enabled = primary_get_hw_state(crtc);
  11314. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  11315. crtc->base.base.id,
  11316. crtc->active ? "enabled" : "disabled");
  11317. }
  11318. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11319. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11320. pll->on = pll->get_hw_state(dev_priv, pll,
  11321. &pll->config.hw_state);
  11322. pll->active = 0;
  11323. pll->config.crtc_mask = 0;
  11324. for_each_intel_crtc(dev, crtc) {
  11325. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  11326. pll->active++;
  11327. pll->config.crtc_mask |= 1 << crtc->pipe;
  11328. }
  11329. }
  11330. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  11331. pll->name, pll->config.crtc_mask, pll->on);
  11332. if (pll->config.crtc_mask)
  11333. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  11334. }
  11335. for_each_intel_encoder(dev, encoder) {
  11336. pipe = 0;
  11337. if (encoder->get_hw_state(encoder, &pipe)) {
  11338. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11339. encoder->base.crtc = &crtc->base;
  11340. encoder->get_config(encoder, crtc->config);
  11341. } else {
  11342. encoder->base.crtc = NULL;
  11343. }
  11344. encoder->connectors_active = false;
  11345. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  11346. encoder->base.base.id,
  11347. encoder->base.name,
  11348. encoder->base.crtc ? "enabled" : "disabled",
  11349. pipe_name(pipe));
  11350. }
  11351. list_for_each_entry(connector, &dev->mode_config.connector_list,
  11352. base.head) {
  11353. if (connector->get_hw_state(connector)) {
  11354. connector->base.dpms = DRM_MODE_DPMS_ON;
  11355. connector->encoder->connectors_active = true;
  11356. connector->base.encoder = &connector->encoder->base;
  11357. } else {
  11358. connector->base.dpms = DRM_MODE_DPMS_OFF;
  11359. connector->base.encoder = NULL;
  11360. }
  11361. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  11362. connector->base.base.id,
  11363. connector->base.name,
  11364. connector->base.encoder ? "enabled" : "disabled");
  11365. }
  11366. }
  11367. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  11368. * and i915 state tracking structures. */
  11369. void intel_modeset_setup_hw_state(struct drm_device *dev,
  11370. bool force_restore)
  11371. {
  11372. struct drm_i915_private *dev_priv = dev->dev_private;
  11373. enum pipe pipe;
  11374. struct intel_crtc *crtc;
  11375. struct intel_encoder *encoder;
  11376. int i;
  11377. intel_modeset_readout_hw_state(dev);
  11378. /*
  11379. * Now that we have the config, copy it to each CRTC struct
  11380. * Note that this could go away if we move to using crtc_config
  11381. * checking everywhere.
  11382. */
  11383. for_each_intel_crtc(dev, crtc) {
  11384. if (crtc->active && i915.fastboot) {
  11385. intel_mode_from_pipe_config(&crtc->base.mode,
  11386. crtc->config);
  11387. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  11388. crtc->base.base.id);
  11389. drm_mode_debug_printmodeline(&crtc->base.mode);
  11390. }
  11391. }
  11392. /* HW state is read out, now we need to sanitize this mess. */
  11393. for_each_intel_encoder(dev, encoder) {
  11394. intel_sanitize_encoder(encoder);
  11395. }
  11396. for_each_pipe(dev_priv, pipe) {
  11397. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  11398. intel_sanitize_crtc(crtc);
  11399. intel_dump_pipe_config(crtc, crtc->config,
  11400. "[setup_hw_state]");
  11401. }
  11402. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11403. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  11404. if (!pll->on || pll->active)
  11405. continue;
  11406. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  11407. pll->disable(dev_priv, pll);
  11408. pll->on = false;
  11409. }
  11410. if (IS_GEN9(dev))
  11411. skl_wm_get_hw_state(dev);
  11412. else if (HAS_PCH_SPLIT(dev))
  11413. ilk_wm_get_hw_state(dev);
  11414. if (force_restore) {
  11415. i915_redisable_vga(dev);
  11416. /*
  11417. * We need to use raw interfaces for restoring state to avoid
  11418. * checking (bogus) intermediate states.
  11419. */
  11420. for_each_pipe(dev_priv, pipe) {
  11421. struct drm_crtc *crtc =
  11422. dev_priv->pipe_to_crtc_mapping[pipe];
  11423. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  11424. crtc->primary->fb);
  11425. }
  11426. } else {
  11427. intel_modeset_update_staged_output_state(dev);
  11428. }
  11429. intel_modeset_check_state(dev);
  11430. }
  11431. void intel_modeset_gem_init(struct drm_device *dev)
  11432. {
  11433. struct drm_i915_private *dev_priv = dev->dev_private;
  11434. struct drm_crtc *c;
  11435. struct drm_i915_gem_object *obj;
  11436. mutex_lock(&dev->struct_mutex);
  11437. intel_init_gt_powersave(dev);
  11438. mutex_unlock(&dev->struct_mutex);
  11439. /*
  11440. * There may be no VBT; and if the BIOS enabled SSC we can
  11441. * just keep using it to avoid unnecessary flicker. Whereas if the
  11442. * BIOS isn't using it, don't assume it will work even if the VBT
  11443. * indicates as much.
  11444. */
  11445. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11446. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  11447. DREF_SSC1_ENABLE);
  11448. intel_modeset_init_hw(dev);
  11449. intel_setup_overlay(dev);
  11450. /*
  11451. * Make sure any fbs we allocated at startup are properly
  11452. * pinned & fenced. When we do the allocation it's too early
  11453. * for this.
  11454. */
  11455. mutex_lock(&dev->struct_mutex);
  11456. for_each_crtc(dev, c) {
  11457. obj = intel_fb_obj(c->primary->fb);
  11458. if (obj == NULL)
  11459. continue;
  11460. if (intel_pin_and_fence_fb_obj(c->primary,
  11461. c->primary->fb,
  11462. NULL)) {
  11463. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  11464. to_intel_crtc(c)->pipe);
  11465. drm_framebuffer_unreference(c->primary->fb);
  11466. c->primary->fb = NULL;
  11467. }
  11468. }
  11469. mutex_unlock(&dev->struct_mutex);
  11470. intel_backlight_register(dev);
  11471. }
  11472. void intel_connector_unregister(struct intel_connector *intel_connector)
  11473. {
  11474. struct drm_connector *connector = &intel_connector->base;
  11475. intel_panel_destroy_backlight(connector);
  11476. drm_connector_unregister(connector);
  11477. }
  11478. void intel_modeset_cleanup(struct drm_device *dev)
  11479. {
  11480. struct drm_i915_private *dev_priv = dev->dev_private;
  11481. struct drm_connector *connector;
  11482. intel_disable_gt_powersave(dev);
  11483. intel_backlight_unregister(dev);
  11484. /*
  11485. * Interrupts and polling as the first thing to avoid creating havoc.
  11486. * Too much stuff here (turning of connectors, ...) would
  11487. * experience fancy races otherwise.
  11488. */
  11489. intel_irq_uninstall(dev_priv);
  11490. /*
  11491. * Due to the hpd irq storm handling the hotplug work can re-arm the
  11492. * poll handlers. Hence disable polling after hpd handling is shut down.
  11493. */
  11494. drm_kms_helper_poll_fini(dev);
  11495. mutex_lock(&dev->struct_mutex);
  11496. intel_unregister_dsm_handler();
  11497. intel_fbc_disable(dev);
  11498. ironlake_teardown_rc6(dev);
  11499. mutex_unlock(&dev->struct_mutex);
  11500. /* flush any delayed tasks or pending work */
  11501. flush_scheduled_work();
  11502. /* destroy the backlight and sysfs files before encoders/connectors */
  11503. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  11504. struct intel_connector *intel_connector;
  11505. intel_connector = to_intel_connector(connector);
  11506. intel_connector->unregister(intel_connector);
  11507. }
  11508. drm_mode_config_cleanup(dev);
  11509. intel_cleanup_overlay(dev);
  11510. mutex_lock(&dev->struct_mutex);
  11511. intel_cleanup_gt_powersave(dev);
  11512. mutex_unlock(&dev->struct_mutex);
  11513. }
  11514. /*
  11515. * Return which encoder is currently attached for connector.
  11516. */
  11517. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  11518. {
  11519. return &intel_attached_encoder(connector)->base;
  11520. }
  11521. void intel_connector_attach_encoder(struct intel_connector *connector,
  11522. struct intel_encoder *encoder)
  11523. {
  11524. connector->encoder = encoder;
  11525. drm_mode_connector_attach_encoder(&connector->base,
  11526. &encoder->base);
  11527. }
  11528. /*
  11529. * set vga decode state - true == enable VGA decode
  11530. */
  11531. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  11532. {
  11533. struct drm_i915_private *dev_priv = dev->dev_private;
  11534. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  11535. u16 gmch_ctrl;
  11536. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  11537. DRM_ERROR("failed to read control word\n");
  11538. return -EIO;
  11539. }
  11540. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  11541. return 0;
  11542. if (state)
  11543. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  11544. else
  11545. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  11546. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  11547. DRM_ERROR("failed to write control word\n");
  11548. return -EIO;
  11549. }
  11550. return 0;
  11551. }
  11552. struct intel_display_error_state {
  11553. u32 power_well_driver;
  11554. int num_transcoders;
  11555. struct intel_cursor_error_state {
  11556. u32 control;
  11557. u32 position;
  11558. u32 base;
  11559. u32 size;
  11560. } cursor[I915_MAX_PIPES];
  11561. struct intel_pipe_error_state {
  11562. bool power_domain_on;
  11563. u32 source;
  11564. u32 stat;
  11565. } pipe[I915_MAX_PIPES];
  11566. struct intel_plane_error_state {
  11567. u32 control;
  11568. u32 stride;
  11569. u32 size;
  11570. u32 pos;
  11571. u32 addr;
  11572. u32 surface;
  11573. u32 tile_offset;
  11574. } plane[I915_MAX_PIPES];
  11575. struct intel_transcoder_error_state {
  11576. bool power_domain_on;
  11577. enum transcoder cpu_transcoder;
  11578. u32 conf;
  11579. u32 htotal;
  11580. u32 hblank;
  11581. u32 hsync;
  11582. u32 vtotal;
  11583. u32 vblank;
  11584. u32 vsync;
  11585. } transcoder[4];
  11586. };
  11587. struct intel_display_error_state *
  11588. intel_display_capture_error_state(struct drm_device *dev)
  11589. {
  11590. struct drm_i915_private *dev_priv = dev->dev_private;
  11591. struct intel_display_error_state *error;
  11592. int transcoders[] = {
  11593. TRANSCODER_A,
  11594. TRANSCODER_B,
  11595. TRANSCODER_C,
  11596. TRANSCODER_EDP,
  11597. };
  11598. int i;
  11599. if (INTEL_INFO(dev)->num_pipes == 0)
  11600. return NULL;
  11601. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  11602. if (error == NULL)
  11603. return NULL;
  11604. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11605. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  11606. for_each_pipe(dev_priv, i) {
  11607. error->pipe[i].power_domain_on =
  11608. __intel_display_power_is_enabled(dev_priv,
  11609. POWER_DOMAIN_PIPE(i));
  11610. if (!error->pipe[i].power_domain_on)
  11611. continue;
  11612. error->cursor[i].control = I915_READ(CURCNTR(i));
  11613. error->cursor[i].position = I915_READ(CURPOS(i));
  11614. error->cursor[i].base = I915_READ(CURBASE(i));
  11615. error->plane[i].control = I915_READ(DSPCNTR(i));
  11616. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  11617. if (INTEL_INFO(dev)->gen <= 3) {
  11618. error->plane[i].size = I915_READ(DSPSIZE(i));
  11619. error->plane[i].pos = I915_READ(DSPPOS(i));
  11620. }
  11621. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11622. error->plane[i].addr = I915_READ(DSPADDR(i));
  11623. if (INTEL_INFO(dev)->gen >= 4) {
  11624. error->plane[i].surface = I915_READ(DSPSURF(i));
  11625. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  11626. }
  11627. error->pipe[i].source = I915_READ(PIPESRC(i));
  11628. if (HAS_GMCH_DISPLAY(dev))
  11629. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  11630. }
  11631. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  11632. if (HAS_DDI(dev_priv->dev))
  11633. error->num_transcoders++; /* Account for eDP. */
  11634. for (i = 0; i < error->num_transcoders; i++) {
  11635. enum transcoder cpu_transcoder = transcoders[i];
  11636. error->transcoder[i].power_domain_on =
  11637. __intel_display_power_is_enabled(dev_priv,
  11638. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  11639. if (!error->transcoder[i].power_domain_on)
  11640. continue;
  11641. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  11642. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  11643. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  11644. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  11645. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  11646. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  11647. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  11648. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  11649. }
  11650. return error;
  11651. }
  11652. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  11653. void
  11654. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  11655. struct drm_device *dev,
  11656. struct intel_display_error_state *error)
  11657. {
  11658. struct drm_i915_private *dev_priv = dev->dev_private;
  11659. int i;
  11660. if (!error)
  11661. return;
  11662. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  11663. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  11664. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  11665. error->power_well_driver);
  11666. for_each_pipe(dev_priv, i) {
  11667. err_printf(m, "Pipe [%d]:\n", i);
  11668. err_printf(m, " Power: %s\n",
  11669. error->pipe[i].power_domain_on ? "on" : "off");
  11670. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  11671. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  11672. err_printf(m, "Plane [%d]:\n", i);
  11673. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  11674. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  11675. if (INTEL_INFO(dev)->gen <= 3) {
  11676. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  11677. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  11678. }
  11679. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  11680. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  11681. if (INTEL_INFO(dev)->gen >= 4) {
  11682. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  11683. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  11684. }
  11685. err_printf(m, "Cursor [%d]:\n", i);
  11686. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  11687. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  11688. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  11689. }
  11690. for (i = 0; i < error->num_transcoders; i++) {
  11691. err_printf(m, "CPU transcoder: %c\n",
  11692. transcoder_name(error->transcoder[i].cpu_transcoder));
  11693. err_printf(m, " Power: %s\n",
  11694. error->transcoder[i].power_domain_on ? "on" : "off");
  11695. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  11696. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  11697. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  11698. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  11699. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  11700. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  11701. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  11702. }
  11703. }
  11704. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  11705. {
  11706. struct intel_crtc *crtc;
  11707. for_each_intel_crtc(dev, crtc) {
  11708. struct intel_unpin_work *work;
  11709. spin_lock_irq(&dev->event_lock);
  11710. work = crtc->unpin_work;
  11711. if (work && work->event &&
  11712. work->event->base.file_priv == file) {
  11713. kfree(work->event);
  11714. work->event = NULL;
  11715. }
  11716. spin_unlock_irq(&dev->event_lock);
  11717. }
  11718. }