intel_display.c 253 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_dp_helper.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include <linux/dma_remapping.h>
  42. bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
  43. static void intel_increase_pllclock(struct drm_crtc *crtc);
  44. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  45. typedef struct {
  46. /* given values */
  47. int n;
  48. int m1, m2;
  49. int p1, p2;
  50. /* derived values */
  51. int dot;
  52. int vco;
  53. int m;
  54. int p;
  55. } intel_clock_t;
  56. typedef struct {
  57. int min, max;
  58. } intel_range_t;
  59. typedef struct {
  60. int dot_limit;
  61. int p2_slow, p2_fast;
  62. } intel_p2_t;
  63. #define INTEL_P2_NUM 2
  64. typedef struct intel_limit intel_limit_t;
  65. struct intel_limit {
  66. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  67. intel_p2_t p2;
  68. /**
  69. * find_pll() - Find the best values for the PLL
  70. * @limit: limits for the PLL
  71. * @crtc: current CRTC
  72. * @target: target frequency in kHz
  73. * @refclk: reference clock frequency in kHz
  74. * @match_clock: if provided, @best_clock P divider must
  75. * match the P divider from @match_clock
  76. * used for LVDS downclocking
  77. * @best_clock: best PLL values found
  78. *
  79. * Returns true on success, false on failure.
  80. */
  81. bool (*find_pll)(const intel_limit_t *limit,
  82. struct drm_crtc *crtc,
  83. int target, int refclk,
  84. intel_clock_t *match_clock,
  85. intel_clock_t *best_clock);
  86. };
  87. /* FDI */
  88. #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
  89. int
  90. intel_pch_rawclk(struct drm_device *dev)
  91. {
  92. struct drm_i915_private *dev_priv = dev->dev_private;
  93. WARN_ON(!HAS_PCH_SPLIT(dev));
  94. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  95. }
  96. static bool
  97. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  98. int target, int refclk, intel_clock_t *match_clock,
  99. intel_clock_t *best_clock);
  100. static bool
  101. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  102. int target, int refclk, intel_clock_t *match_clock,
  103. intel_clock_t *best_clock);
  104. static bool
  105. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  106. int target, int refclk, intel_clock_t *match_clock,
  107. intel_clock_t *best_clock);
  108. static bool
  109. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  110. int target, int refclk, intel_clock_t *match_clock,
  111. intel_clock_t *best_clock);
  112. static bool
  113. intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
  114. int target, int refclk, intel_clock_t *match_clock,
  115. intel_clock_t *best_clock);
  116. static inline u32 /* units of 100MHz */
  117. intel_fdi_link_freq(struct drm_device *dev)
  118. {
  119. if (IS_GEN5(dev)) {
  120. struct drm_i915_private *dev_priv = dev->dev_private;
  121. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  122. } else
  123. return 27;
  124. }
  125. static const intel_limit_t intel_limits_i8xx_dvo = {
  126. .dot = { .min = 25000, .max = 350000 },
  127. .vco = { .min = 930000, .max = 1400000 },
  128. .n = { .min = 3, .max = 16 },
  129. .m = { .min = 96, .max = 140 },
  130. .m1 = { .min = 18, .max = 26 },
  131. .m2 = { .min = 6, .max = 16 },
  132. .p = { .min = 4, .max = 128 },
  133. .p1 = { .min = 2, .max = 33 },
  134. .p2 = { .dot_limit = 165000,
  135. .p2_slow = 4, .p2_fast = 2 },
  136. .find_pll = intel_find_best_PLL,
  137. };
  138. static const intel_limit_t intel_limits_i8xx_lvds = {
  139. .dot = { .min = 25000, .max = 350000 },
  140. .vco = { .min = 930000, .max = 1400000 },
  141. .n = { .min = 3, .max = 16 },
  142. .m = { .min = 96, .max = 140 },
  143. .m1 = { .min = 18, .max = 26 },
  144. .m2 = { .min = 6, .max = 16 },
  145. .p = { .min = 4, .max = 128 },
  146. .p1 = { .min = 1, .max = 6 },
  147. .p2 = { .dot_limit = 165000,
  148. .p2_slow = 14, .p2_fast = 7 },
  149. .find_pll = intel_find_best_PLL,
  150. };
  151. static const intel_limit_t intel_limits_i9xx_sdvo = {
  152. .dot = { .min = 20000, .max = 400000 },
  153. .vco = { .min = 1400000, .max = 2800000 },
  154. .n = { .min = 1, .max = 6 },
  155. .m = { .min = 70, .max = 120 },
  156. .m1 = { .min = 8, .max = 18 },
  157. .m2 = { .min = 3, .max = 7 },
  158. .p = { .min = 5, .max = 80 },
  159. .p1 = { .min = 1, .max = 8 },
  160. .p2 = { .dot_limit = 200000,
  161. .p2_slow = 10, .p2_fast = 5 },
  162. .find_pll = intel_find_best_PLL,
  163. };
  164. static const intel_limit_t intel_limits_i9xx_lvds = {
  165. .dot = { .min = 20000, .max = 400000 },
  166. .vco = { .min = 1400000, .max = 2800000 },
  167. .n = { .min = 1, .max = 6 },
  168. .m = { .min = 70, .max = 120 },
  169. .m1 = { .min = 8, .max = 18 },
  170. .m2 = { .min = 3, .max = 7 },
  171. .p = { .min = 7, .max = 98 },
  172. .p1 = { .min = 1, .max = 8 },
  173. .p2 = { .dot_limit = 112000,
  174. .p2_slow = 14, .p2_fast = 7 },
  175. .find_pll = intel_find_best_PLL,
  176. };
  177. static const intel_limit_t intel_limits_g4x_sdvo = {
  178. .dot = { .min = 25000, .max = 270000 },
  179. .vco = { .min = 1750000, .max = 3500000},
  180. .n = { .min = 1, .max = 4 },
  181. .m = { .min = 104, .max = 138 },
  182. .m1 = { .min = 17, .max = 23 },
  183. .m2 = { .min = 5, .max = 11 },
  184. .p = { .min = 10, .max = 30 },
  185. .p1 = { .min = 1, .max = 3},
  186. .p2 = { .dot_limit = 270000,
  187. .p2_slow = 10,
  188. .p2_fast = 10
  189. },
  190. .find_pll = intel_g4x_find_best_PLL,
  191. };
  192. static const intel_limit_t intel_limits_g4x_hdmi = {
  193. .dot = { .min = 22000, .max = 400000 },
  194. .vco = { .min = 1750000, .max = 3500000},
  195. .n = { .min = 1, .max = 4 },
  196. .m = { .min = 104, .max = 138 },
  197. .m1 = { .min = 16, .max = 23 },
  198. .m2 = { .min = 5, .max = 11 },
  199. .p = { .min = 5, .max = 80 },
  200. .p1 = { .min = 1, .max = 8},
  201. .p2 = { .dot_limit = 165000,
  202. .p2_slow = 10, .p2_fast = 5 },
  203. .find_pll = intel_g4x_find_best_PLL,
  204. };
  205. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  206. .dot = { .min = 20000, .max = 115000 },
  207. .vco = { .min = 1750000, .max = 3500000 },
  208. .n = { .min = 1, .max = 3 },
  209. .m = { .min = 104, .max = 138 },
  210. .m1 = { .min = 17, .max = 23 },
  211. .m2 = { .min = 5, .max = 11 },
  212. .p = { .min = 28, .max = 112 },
  213. .p1 = { .min = 2, .max = 8 },
  214. .p2 = { .dot_limit = 0,
  215. .p2_slow = 14, .p2_fast = 14
  216. },
  217. .find_pll = intel_g4x_find_best_PLL,
  218. };
  219. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  220. .dot = { .min = 80000, .max = 224000 },
  221. .vco = { .min = 1750000, .max = 3500000 },
  222. .n = { .min = 1, .max = 3 },
  223. .m = { .min = 104, .max = 138 },
  224. .m1 = { .min = 17, .max = 23 },
  225. .m2 = { .min = 5, .max = 11 },
  226. .p = { .min = 14, .max = 42 },
  227. .p1 = { .min = 2, .max = 6 },
  228. .p2 = { .dot_limit = 0,
  229. .p2_slow = 7, .p2_fast = 7
  230. },
  231. .find_pll = intel_g4x_find_best_PLL,
  232. };
  233. static const intel_limit_t intel_limits_g4x_display_port = {
  234. .dot = { .min = 161670, .max = 227000 },
  235. .vco = { .min = 1750000, .max = 3500000},
  236. .n = { .min = 1, .max = 2 },
  237. .m = { .min = 97, .max = 108 },
  238. .m1 = { .min = 0x10, .max = 0x12 },
  239. .m2 = { .min = 0x05, .max = 0x06 },
  240. .p = { .min = 10, .max = 20 },
  241. .p1 = { .min = 1, .max = 2},
  242. .p2 = { .dot_limit = 0,
  243. .p2_slow = 10, .p2_fast = 10 },
  244. .find_pll = intel_find_pll_g4x_dp,
  245. };
  246. static const intel_limit_t intel_limits_pineview_sdvo = {
  247. .dot = { .min = 20000, .max = 400000},
  248. .vco = { .min = 1700000, .max = 3500000 },
  249. /* Pineview's Ncounter is a ring counter */
  250. .n = { .min = 3, .max = 6 },
  251. .m = { .min = 2, .max = 256 },
  252. /* Pineview only has one combined m divider, which we treat as m2. */
  253. .m1 = { .min = 0, .max = 0 },
  254. .m2 = { .min = 0, .max = 254 },
  255. .p = { .min = 5, .max = 80 },
  256. .p1 = { .min = 1, .max = 8 },
  257. .p2 = { .dot_limit = 200000,
  258. .p2_slow = 10, .p2_fast = 5 },
  259. .find_pll = intel_find_best_PLL,
  260. };
  261. static const intel_limit_t intel_limits_pineview_lvds = {
  262. .dot = { .min = 20000, .max = 400000 },
  263. .vco = { .min = 1700000, .max = 3500000 },
  264. .n = { .min = 3, .max = 6 },
  265. .m = { .min = 2, .max = 256 },
  266. .m1 = { .min = 0, .max = 0 },
  267. .m2 = { .min = 0, .max = 254 },
  268. .p = { .min = 7, .max = 112 },
  269. .p1 = { .min = 1, .max = 8 },
  270. .p2 = { .dot_limit = 112000,
  271. .p2_slow = 14, .p2_fast = 14 },
  272. .find_pll = intel_find_best_PLL,
  273. };
  274. /* Ironlake / Sandybridge
  275. *
  276. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  277. * the range value for them is (actual_value - 2).
  278. */
  279. static const intel_limit_t intel_limits_ironlake_dac = {
  280. .dot = { .min = 25000, .max = 350000 },
  281. .vco = { .min = 1760000, .max = 3510000 },
  282. .n = { .min = 1, .max = 5 },
  283. .m = { .min = 79, .max = 127 },
  284. .m1 = { .min = 12, .max = 22 },
  285. .m2 = { .min = 5, .max = 9 },
  286. .p = { .min = 5, .max = 80 },
  287. .p1 = { .min = 1, .max = 8 },
  288. .p2 = { .dot_limit = 225000,
  289. .p2_slow = 10, .p2_fast = 5 },
  290. .find_pll = intel_g4x_find_best_PLL,
  291. };
  292. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  293. .dot = { .min = 25000, .max = 350000 },
  294. .vco = { .min = 1760000, .max = 3510000 },
  295. .n = { .min = 1, .max = 3 },
  296. .m = { .min = 79, .max = 118 },
  297. .m1 = { .min = 12, .max = 22 },
  298. .m2 = { .min = 5, .max = 9 },
  299. .p = { .min = 28, .max = 112 },
  300. .p1 = { .min = 2, .max = 8 },
  301. .p2 = { .dot_limit = 225000,
  302. .p2_slow = 14, .p2_fast = 14 },
  303. .find_pll = intel_g4x_find_best_PLL,
  304. };
  305. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  306. .dot = { .min = 25000, .max = 350000 },
  307. .vco = { .min = 1760000, .max = 3510000 },
  308. .n = { .min = 1, .max = 3 },
  309. .m = { .min = 79, .max = 127 },
  310. .m1 = { .min = 12, .max = 22 },
  311. .m2 = { .min = 5, .max = 9 },
  312. .p = { .min = 14, .max = 56 },
  313. .p1 = { .min = 2, .max = 8 },
  314. .p2 = { .dot_limit = 225000,
  315. .p2_slow = 7, .p2_fast = 7 },
  316. .find_pll = intel_g4x_find_best_PLL,
  317. };
  318. /* LVDS 100mhz refclk limits. */
  319. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  320. .dot = { .min = 25000, .max = 350000 },
  321. .vco = { .min = 1760000, .max = 3510000 },
  322. .n = { .min = 1, .max = 2 },
  323. .m = { .min = 79, .max = 126 },
  324. .m1 = { .min = 12, .max = 22 },
  325. .m2 = { .min = 5, .max = 9 },
  326. .p = { .min = 28, .max = 112 },
  327. .p1 = { .min = 2, .max = 8 },
  328. .p2 = { .dot_limit = 225000,
  329. .p2_slow = 14, .p2_fast = 14 },
  330. .find_pll = intel_g4x_find_best_PLL,
  331. };
  332. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  333. .dot = { .min = 25000, .max = 350000 },
  334. .vco = { .min = 1760000, .max = 3510000 },
  335. .n = { .min = 1, .max = 3 },
  336. .m = { .min = 79, .max = 126 },
  337. .m1 = { .min = 12, .max = 22 },
  338. .m2 = { .min = 5, .max = 9 },
  339. .p = { .min = 14, .max = 42 },
  340. .p1 = { .min = 2, .max = 6 },
  341. .p2 = { .dot_limit = 225000,
  342. .p2_slow = 7, .p2_fast = 7 },
  343. .find_pll = intel_g4x_find_best_PLL,
  344. };
  345. static const intel_limit_t intel_limits_ironlake_display_port = {
  346. .dot = { .min = 25000, .max = 350000 },
  347. .vco = { .min = 1760000, .max = 3510000},
  348. .n = { .min = 1, .max = 2 },
  349. .m = { .min = 81, .max = 90 },
  350. .m1 = { .min = 12, .max = 22 },
  351. .m2 = { .min = 5, .max = 9 },
  352. .p = { .min = 10, .max = 20 },
  353. .p1 = { .min = 1, .max = 2},
  354. .p2 = { .dot_limit = 0,
  355. .p2_slow = 10, .p2_fast = 10 },
  356. .find_pll = intel_find_pll_ironlake_dp,
  357. };
  358. static const intel_limit_t intel_limits_vlv_dac = {
  359. .dot = { .min = 25000, .max = 270000 },
  360. .vco = { .min = 4000000, .max = 6000000 },
  361. .n = { .min = 1, .max = 7 },
  362. .m = { .min = 22, .max = 450 }, /* guess */
  363. .m1 = { .min = 2, .max = 3 },
  364. .m2 = { .min = 11, .max = 156 },
  365. .p = { .min = 10, .max = 30 },
  366. .p1 = { .min = 2, .max = 3 },
  367. .p2 = { .dot_limit = 270000,
  368. .p2_slow = 2, .p2_fast = 20 },
  369. .find_pll = intel_vlv_find_best_pll,
  370. };
  371. static const intel_limit_t intel_limits_vlv_hdmi = {
  372. .dot = { .min = 20000, .max = 165000 },
  373. .vco = { .min = 4000000, .max = 5994000},
  374. .n = { .min = 1, .max = 7 },
  375. .m = { .min = 60, .max = 300 }, /* guess */
  376. .m1 = { .min = 2, .max = 3 },
  377. .m2 = { .min = 11, .max = 156 },
  378. .p = { .min = 10, .max = 30 },
  379. .p1 = { .min = 2, .max = 3 },
  380. .p2 = { .dot_limit = 270000,
  381. .p2_slow = 2, .p2_fast = 20 },
  382. .find_pll = intel_vlv_find_best_pll,
  383. };
  384. static const intel_limit_t intel_limits_vlv_dp = {
  385. .dot = { .min = 25000, .max = 270000 },
  386. .vco = { .min = 4000000, .max = 6000000 },
  387. .n = { .min = 1, .max = 7 },
  388. .m = { .min = 22, .max = 450 },
  389. .m1 = { .min = 2, .max = 3 },
  390. .m2 = { .min = 11, .max = 156 },
  391. .p = { .min = 10, .max = 30 },
  392. .p1 = { .min = 2, .max = 3 },
  393. .p2 = { .dot_limit = 270000,
  394. .p2_slow = 2, .p2_fast = 20 },
  395. .find_pll = intel_vlv_find_best_pll,
  396. };
  397. u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
  398. {
  399. WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
  400. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
  401. DRM_ERROR("DPIO idle wait timed out\n");
  402. return 0;
  403. }
  404. I915_WRITE(DPIO_REG, reg);
  405. I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
  406. DPIO_BYTE);
  407. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
  408. DRM_ERROR("DPIO read wait timed out\n");
  409. return 0;
  410. }
  411. return I915_READ(DPIO_DATA);
  412. }
  413. static void intel_dpio_write(struct drm_i915_private *dev_priv, int reg,
  414. u32 val)
  415. {
  416. WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
  417. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
  418. DRM_ERROR("DPIO idle wait timed out\n");
  419. return;
  420. }
  421. I915_WRITE(DPIO_DATA, val);
  422. I915_WRITE(DPIO_REG, reg);
  423. I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_WRITE | DPIO_PORTID |
  424. DPIO_BYTE);
  425. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100))
  426. DRM_ERROR("DPIO write wait timed out\n");
  427. }
  428. static void vlv_init_dpio(struct drm_device *dev)
  429. {
  430. struct drm_i915_private *dev_priv = dev->dev_private;
  431. /* Reset the DPIO config */
  432. I915_WRITE(DPIO_CTL, 0);
  433. POSTING_READ(DPIO_CTL);
  434. I915_WRITE(DPIO_CTL, 1);
  435. POSTING_READ(DPIO_CTL);
  436. }
  437. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  438. int refclk)
  439. {
  440. struct drm_device *dev = crtc->dev;
  441. const intel_limit_t *limit;
  442. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  443. if (intel_is_dual_link_lvds(dev)) {
  444. if (refclk == 100000)
  445. limit = &intel_limits_ironlake_dual_lvds_100m;
  446. else
  447. limit = &intel_limits_ironlake_dual_lvds;
  448. } else {
  449. if (refclk == 100000)
  450. limit = &intel_limits_ironlake_single_lvds_100m;
  451. else
  452. limit = &intel_limits_ironlake_single_lvds;
  453. }
  454. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  455. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  456. limit = &intel_limits_ironlake_display_port;
  457. else
  458. limit = &intel_limits_ironlake_dac;
  459. return limit;
  460. }
  461. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  462. {
  463. struct drm_device *dev = crtc->dev;
  464. const intel_limit_t *limit;
  465. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  466. if (intel_is_dual_link_lvds(dev))
  467. limit = &intel_limits_g4x_dual_channel_lvds;
  468. else
  469. limit = &intel_limits_g4x_single_channel_lvds;
  470. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  471. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  472. limit = &intel_limits_g4x_hdmi;
  473. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  474. limit = &intel_limits_g4x_sdvo;
  475. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  476. limit = &intel_limits_g4x_display_port;
  477. } else /* The option is for other outputs */
  478. limit = &intel_limits_i9xx_sdvo;
  479. return limit;
  480. }
  481. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  482. {
  483. struct drm_device *dev = crtc->dev;
  484. const intel_limit_t *limit;
  485. if (HAS_PCH_SPLIT(dev))
  486. limit = intel_ironlake_limit(crtc, refclk);
  487. else if (IS_G4X(dev)) {
  488. limit = intel_g4x_limit(crtc);
  489. } else if (IS_PINEVIEW(dev)) {
  490. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  491. limit = &intel_limits_pineview_lvds;
  492. else
  493. limit = &intel_limits_pineview_sdvo;
  494. } else if (IS_VALLEYVIEW(dev)) {
  495. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
  496. limit = &intel_limits_vlv_dac;
  497. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  498. limit = &intel_limits_vlv_hdmi;
  499. else
  500. limit = &intel_limits_vlv_dp;
  501. } else if (!IS_GEN2(dev)) {
  502. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  503. limit = &intel_limits_i9xx_lvds;
  504. else
  505. limit = &intel_limits_i9xx_sdvo;
  506. } else {
  507. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  508. limit = &intel_limits_i8xx_lvds;
  509. else
  510. limit = &intel_limits_i8xx_dvo;
  511. }
  512. return limit;
  513. }
  514. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  515. static void pineview_clock(int refclk, intel_clock_t *clock)
  516. {
  517. clock->m = clock->m2 + 2;
  518. clock->p = clock->p1 * clock->p2;
  519. clock->vco = refclk * clock->m / clock->n;
  520. clock->dot = clock->vco / clock->p;
  521. }
  522. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  523. {
  524. if (IS_PINEVIEW(dev)) {
  525. pineview_clock(refclk, clock);
  526. return;
  527. }
  528. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  529. clock->p = clock->p1 * clock->p2;
  530. clock->vco = refclk * clock->m / (clock->n + 2);
  531. clock->dot = clock->vco / clock->p;
  532. }
  533. /**
  534. * Returns whether any output on the specified pipe is of the specified type
  535. */
  536. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  537. {
  538. struct drm_device *dev = crtc->dev;
  539. struct intel_encoder *encoder;
  540. for_each_encoder_on_crtc(dev, crtc, encoder)
  541. if (encoder->type == type)
  542. return true;
  543. return false;
  544. }
  545. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  546. /**
  547. * Returns whether the given set of divisors are valid for a given refclk with
  548. * the given connectors.
  549. */
  550. static bool intel_PLL_is_valid(struct drm_device *dev,
  551. const intel_limit_t *limit,
  552. const intel_clock_t *clock)
  553. {
  554. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  555. INTELPllInvalid("p1 out of range\n");
  556. if (clock->p < limit->p.min || limit->p.max < clock->p)
  557. INTELPllInvalid("p out of range\n");
  558. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  559. INTELPllInvalid("m2 out of range\n");
  560. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  561. INTELPllInvalid("m1 out of range\n");
  562. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  563. INTELPllInvalid("m1 <= m2\n");
  564. if (clock->m < limit->m.min || limit->m.max < clock->m)
  565. INTELPllInvalid("m out of range\n");
  566. if (clock->n < limit->n.min || limit->n.max < clock->n)
  567. INTELPllInvalid("n out of range\n");
  568. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  569. INTELPllInvalid("vco out of range\n");
  570. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  571. * connector, etc., rather than just a single range.
  572. */
  573. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  574. INTELPllInvalid("dot out of range\n");
  575. return true;
  576. }
  577. static bool
  578. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  579. int target, int refclk, intel_clock_t *match_clock,
  580. intel_clock_t *best_clock)
  581. {
  582. struct drm_device *dev = crtc->dev;
  583. intel_clock_t clock;
  584. int err = target;
  585. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  586. /*
  587. * For LVDS just rely on its current settings for dual-channel.
  588. * We haven't figured out how to reliably set up different
  589. * single/dual channel state, if we even can.
  590. */
  591. if (intel_is_dual_link_lvds(dev))
  592. clock.p2 = limit->p2.p2_fast;
  593. else
  594. clock.p2 = limit->p2.p2_slow;
  595. } else {
  596. if (target < limit->p2.dot_limit)
  597. clock.p2 = limit->p2.p2_slow;
  598. else
  599. clock.p2 = limit->p2.p2_fast;
  600. }
  601. memset(best_clock, 0, sizeof(*best_clock));
  602. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  603. clock.m1++) {
  604. for (clock.m2 = limit->m2.min;
  605. clock.m2 <= limit->m2.max; clock.m2++) {
  606. /* m1 is always 0 in Pineview */
  607. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  608. break;
  609. for (clock.n = limit->n.min;
  610. clock.n <= limit->n.max; clock.n++) {
  611. for (clock.p1 = limit->p1.min;
  612. clock.p1 <= limit->p1.max; clock.p1++) {
  613. int this_err;
  614. intel_clock(dev, refclk, &clock);
  615. if (!intel_PLL_is_valid(dev, limit,
  616. &clock))
  617. continue;
  618. if (match_clock &&
  619. clock.p != match_clock->p)
  620. continue;
  621. this_err = abs(clock.dot - target);
  622. if (this_err < err) {
  623. *best_clock = clock;
  624. err = this_err;
  625. }
  626. }
  627. }
  628. }
  629. }
  630. return (err != target);
  631. }
  632. static bool
  633. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  634. int target, int refclk, intel_clock_t *match_clock,
  635. intel_clock_t *best_clock)
  636. {
  637. struct drm_device *dev = crtc->dev;
  638. intel_clock_t clock;
  639. int max_n;
  640. bool found;
  641. /* approximately equals target * 0.00585 */
  642. int err_most = (target >> 8) + (target >> 9);
  643. found = false;
  644. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  645. int lvds_reg;
  646. if (HAS_PCH_SPLIT(dev))
  647. lvds_reg = PCH_LVDS;
  648. else
  649. lvds_reg = LVDS;
  650. if (intel_is_dual_link_lvds(dev))
  651. clock.p2 = limit->p2.p2_fast;
  652. else
  653. clock.p2 = limit->p2.p2_slow;
  654. } else {
  655. if (target < limit->p2.dot_limit)
  656. clock.p2 = limit->p2.p2_slow;
  657. else
  658. clock.p2 = limit->p2.p2_fast;
  659. }
  660. memset(best_clock, 0, sizeof(*best_clock));
  661. max_n = limit->n.max;
  662. /* based on hardware requirement, prefer smaller n to precision */
  663. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  664. /* based on hardware requirement, prefere larger m1,m2 */
  665. for (clock.m1 = limit->m1.max;
  666. clock.m1 >= limit->m1.min; clock.m1--) {
  667. for (clock.m2 = limit->m2.max;
  668. clock.m2 >= limit->m2.min; clock.m2--) {
  669. for (clock.p1 = limit->p1.max;
  670. clock.p1 >= limit->p1.min; clock.p1--) {
  671. int this_err;
  672. intel_clock(dev, refclk, &clock);
  673. if (!intel_PLL_is_valid(dev, limit,
  674. &clock))
  675. continue;
  676. if (match_clock &&
  677. clock.p != match_clock->p)
  678. continue;
  679. this_err = abs(clock.dot - target);
  680. if (this_err < err_most) {
  681. *best_clock = clock;
  682. err_most = this_err;
  683. max_n = clock.n;
  684. found = true;
  685. }
  686. }
  687. }
  688. }
  689. }
  690. return found;
  691. }
  692. static bool
  693. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  694. int target, int refclk, intel_clock_t *match_clock,
  695. intel_clock_t *best_clock)
  696. {
  697. struct drm_device *dev = crtc->dev;
  698. intel_clock_t clock;
  699. if (target < 200000) {
  700. clock.n = 1;
  701. clock.p1 = 2;
  702. clock.p2 = 10;
  703. clock.m1 = 12;
  704. clock.m2 = 9;
  705. } else {
  706. clock.n = 2;
  707. clock.p1 = 1;
  708. clock.p2 = 10;
  709. clock.m1 = 14;
  710. clock.m2 = 8;
  711. }
  712. intel_clock(dev, refclk, &clock);
  713. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  714. return true;
  715. }
  716. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  717. static bool
  718. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  719. int target, int refclk, intel_clock_t *match_clock,
  720. intel_clock_t *best_clock)
  721. {
  722. intel_clock_t clock;
  723. if (target < 200000) {
  724. clock.p1 = 2;
  725. clock.p2 = 10;
  726. clock.n = 2;
  727. clock.m1 = 23;
  728. clock.m2 = 8;
  729. } else {
  730. clock.p1 = 1;
  731. clock.p2 = 10;
  732. clock.n = 1;
  733. clock.m1 = 14;
  734. clock.m2 = 2;
  735. }
  736. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  737. clock.p = (clock.p1 * clock.p2);
  738. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  739. clock.vco = 0;
  740. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  741. return true;
  742. }
  743. static bool
  744. intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
  745. int target, int refclk, intel_clock_t *match_clock,
  746. intel_clock_t *best_clock)
  747. {
  748. u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
  749. u32 m, n, fastclk;
  750. u32 updrate, minupdate, fracbits, p;
  751. unsigned long bestppm, ppm, absppm;
  752. int dotclk, flag;
  753. flag = 0;
  754. dotclk = target * 1000;
  755. bestppm = 1000000;
  756. ppm = absppm = 0;
  757. fastclk = dotclk / (2*100);
  758. updrate = 0;
  759. minupdate = 19200;
  760. fracbits = 1;
  761. n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
  762. bestm1 = bestm2 = bestp1 = bestp2 = 0;
  763. /* based on hardware requirement, prefer smaller n to precision */
  764. for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
  765. updrate = refclk / n;
  766. for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
  767. for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
  768. if (p2 > 10)
  769. p2 = p2 - 1;
  770. p = p1 * p2;
  771. /* based on hardware requirement, prefer bigger m1,m2 values */
  772. for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
  773. m2 = (((2*(fastclk * p * n / m1 )) +
  774. refclk) / (2*refclk));
  775. m = m1 * m2;
  776. vco = updrate * m;
  777. if (vco >= limit->vco.min && vco < limit->vco.max) {
  778. ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
  779. absppm = (ppm > 0) ? ppm : (-ppm);
  780. if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
  781. bestppm = 0;
  782. flag = 1;
  783. }
  784. if (absppm < bestppm - 10) {
  785. bestppm = absppm;
  786. flag = 1;
  787. }
  788. if (flag) {
  789. bestn = n;
  790. bestm1 = m1;
  791. bestm2 = m2;
  792. bestp1 = p1;
  793. bestp2 = p2;
  794. flag = 0;
  795. }
  796. }
  797. }
  798. }
  799. }
  800. }
  801. best_clock->n = bestn;
  802. best_clock->m1 = bestm1;
  803. best_clock->m2 = bestm2;
  804. best_clock->p1 = bestp1;
  805. best_clock->p2 = bestp2;
  806. return true;
  807. }
  808. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  809. enum pipe pipe)
  810. {
  811. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  812. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  813. return intel_crtc->cpu_transcoder;
  814. }
  815. static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
  816. {
  817. struct drm_i915_private *dev_priv = dev->dev_private;
  818. u32 frame, frame_reg = PIPEFRAME(pipe);
  819. frame = I915_READ(frame_reg);
  820. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  821. DRM_DEBUG_KMS("vblank wait timed out\n");
  822. }
  823. /**
  824. * intel_wait_for_vblank - wait for vblank on a given pipe
  825. * @dev: drm device
  826. * @pipe: pipe to wait for
  827. *
  828. * Wait for vblank to occur on a given pipe. Needed for various bits of
  829. * mode setting code.
  830. */
  831. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  832. {
  833. struct drm_i915_private *dev_priv = dev->dev_private;
  834. int pipestat_reg = PIPESTAT(pipe);
  835. if (INTEL_INFO(dev)->gen >= 5) {
  836. ironlake_wait_for_vblank(dev, pipe);
  837. return;
  838. }
  839. /* Clear existing vblank status. Note this will clear any other
  840. * sticky status fields as well.
  841. *
  842. * This races with i915_driver_irq_handler() with the result
  843. * that either function could miss a vblank event. Here it is not
  844. * fatal, as we will either wait upon the next vblank interrupt or
  845. * timeout. Generally speaking intel_wait_for_vblank() is only
  846. * called during modeset at which time the GPU should be idle and
  847. * should *not* be performing page flips and thus not waiting on
  848. * vblanks...
  849. * Currently, the result of us stealing a vblank from the irq
  850. * handler is that a single frame will be skipped during swapbuffers.
  851. */
  852. I915_WRITE(pipestat_reg,
  853. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  854. /* Wait for vblank interrupt bit to set */
  855. if (wait_for(I915_READ(pipestat_reg) &
  856. PIPE_VBLANK_INTERRUPT_STATUS,
  857. 50))
  858. DRM_DEBUG_KMS("vblank wait timed out\n");
  859. }
  860. /*
  861. * intel_wait_for_pipe_off - wait for pipe to turn off
  862. * @dev: drm device
  863. * @pipe: pipe to wait for
  864. *
  865. * After disabling a pipe, we can't wait for vblank in the usual way,
  866. * spinning on the vblank interrupt status bit, since we won't actually
  867. * see an interrupt when the pipe is disabled.
  868. *
  869. * On Gen4 and above:
  870. * wait for the pipe register state bit to turn off
  871. *
  872. * Otherwise:
  873. * wait for the display line value to settle (it usually
  874. * ends up stopping at the start of the next frame).
  875. *
  876. */
  877. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  878. {
  879. struct drm_i915_private *dev_priv = dev->dev_private;
  880. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  881. pipe);
  882. if (INTEL_INFO(dev)->gen >= 4) {
  883. int reg = PIPECONF(cpu_transcoder);
  884. /* Wait for the Pipe State to go off */
  885. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  886. 100))
  887. WARN(1, "pipe_off wait timed out\n");
  888. } else {
  889. u32 last_line, line_mask;
  890. int reg = PIPEDSL(pipe);
  891. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  892. if (IS_GEN2(dev))
  893. line_mask = DSL_LINEMASK_GEN2;
  894. else
  895. line_mask = DSL_LINEMASK_GEN3;
  896. /* Wait for the display line to settle */
  897. do {
  898. last_line = I915_READ(reg) & line_mask;
  899. mdelay(5);
  900. } while (((I915_READ(reg) & line_mask) != last_line) &&
  901. time_after(timeout, jiffies));
  902. if (time_after(jiffies, timeout))
  903. WARN(1, "pipe_off wait timed out\n");
  904. }
  905. }
  906. /*
  907. * ibx_digital_port_connected - is the specified port connected?
  908. * @dev_priv: i915 private structure
  909. * @port: the port to test
  910. *
  911. * Returns true if @port is connected, false otherwise.
  912. */
  913. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  914. struct intel_digital_port *port)
  915. {
  916. u32 bit;
  917. if (HAS_PCH_IBX(dev_priv->dev)) {
  918. switch(port->port) {
  919. case PORT_B:
  920. bit = SDE_PORTB_HOTPLUG;
  921. break;
  922. case PORT_C:
  923. bit = SDE_PORTC_HOTPLUG;
  924. break;
  925. case PORT_D:
  926. bit = SDE_PORTD_HOTPLUG;
  927. break;
  928. default:
  929. return true;
  930. }
  931. } else {
  932. switch(port->port) {
  933. case PORT_B:
  934. bit = SDE_PORTB_HOTPLUG_CPT;
  935. break;
  936. case PORT_C:
  937. bit = SDE_PORTC_HOTPLUG_CPT;
  938. break;
  939. case PORT_D:
  940. bit = SDE_PORTD_HOTPLUG_CPT;
  941. break;
  942. default:
  943. return true;
  944. }
  945. }
  946. return I915_READ(SDEISR) & bit;
  947. }
  948. static const char *state_string(bool enabled)
  949. {
  950. return enabled ? "on" : "off";
  951. }
  952. /* Only for pre-ILK configs */
  953. static void assert_pll(struct drm_i915_private *dev_priv,
  954. enum pipe pipe, bool state)
  955. {
  956. int reg;
  957. u32 val;
  958. bool cur_state;
  959. reg = DPLL(pipe);
  960. val = I915_READ(reg);
  961. cur_state = !!(val & DPLL_VCO_ENABLE);
  962. WARN(cur_state != state,
  963. "PLL state assertion failure (expected %s, current %s)\n",
  964. state_string(state), state_string(cur_state));
  965. }
  966. #define assert_pll_enabled(d, p) assert_pll(d, p, true)
  967. #define assert_pll_disabled(d, p) assert_pll(d, p, false)
  968. /* For ILK+ */
  969. static void assert_pch_pll(struct drm_i915_private *dev_priv,
  970. struct intel_pch_pll *pll,
  971. struct intel_crtc *crtc,
  972. bool state)
  973. {
  974. u32 val;
  975. bool cur_state;
  976. if (HAS_PCH_LPT(dev_priv->dev)) {
  977. DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
  978. return;
  979. }
  980. if (WARN (!pll,
  981. "asserting PCH PLL %s with no PLL\n", state_string(state)))
  982. return;
  983. val = I915_READ(pll->pll_reg);
  984. cur_state = !!(val & DPLL_VCO_ENABLE);
  985. WARN(cur_state != state,
  986. "PCH PLL state for reg %x assertion failure (expected %s, current %s), val=%08x\n",
  987. pll->pll_reg, state_string(state), state_string(cur_state), val);
  988. /* Make sure the selected PLL is correctly attached to the transcoder */
  989. if (crtc && HAS_PCH_CPT(dev_priv->dev)) {
  990. u32 pch_dpll;
  991. pch_dpll = I915_READ(PCH_DPLL_SEL);
  992. cur_state = pll->pll_reg == _PCH_DPLL_B;
  993. if (!WARN(((pch_dpll >> (4 * crtc->pipe)) & 1) != cur_state,
  994. "PLL[%d] not attached to this transcoder %d: %08x\n",
  995. cur_state, crtc->pipe, pch_dpll)) {
  996. cur_state = !!(val >> (4*crtc->pipe + 3));
  997. WARN(cur_state != state,
  998. "PLL[%d] not %s on this transcoder %d: %08x\n",
  999. pll->pll_reg == _PCH_DPLL_B,
  1000. state_string(state),
  1001. crtc->pipe,
  1002. val);
  1003. }
  1004. }
  1005. }
  1006. #define assert_pch_pll_enabled(d, p, c) assert_pch_pll(d, p, c, true)
  1007. #define assert_pch_pll_disabled(d, p, c) assert_pch_pll(d, p, c, false)
  1008. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1009. enum pipe pipe, bool state)
  1010. {
  1011. int reg;
  1012. u32 val;
  1013. bool cur_state;
  1014. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1015. pipe);
  1016. if (HAS_DDI(dev_priv->dev)) {
  1017. /* DDI does not have a specific FDI_TX register */
  1018. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1019. val = I915_READ(reg);
  1020. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1021. } else {
  1022. reg = FDI_TX_CTL(pipe);
  1023. val = I915_READ(reg);
  1024. cur_state = !!(val & FDI_TX_ENABLE);
  1025. }
  1026. WARN(cur_state != state,
  1027. "FDI TX state assertion failure (expected %s, current %s)\n",
  1028. state_string(state), state_string(cur_state));
  1029. }
  1030. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1031. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1032. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1033. enum pipe pipe, bool state)
  1034. {
  1035. int reg;
  1036. u32 val;
  1037. bool cur_state;
  1038. reg = FDI_RX_CTL(pipe);
  1039. val = I915_READ(reg);
  1040. cur_state = !!(val & FDI_RX_ENABLE);
  1041. WARN(cur_state != state,
  1042. "FDI RX state assertion failure (expected %s, current %s)\n",
  1043. state_string(state), state_string(cur_state));
  1044. }
  1045. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1046. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1047. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1048. enum pipe pipe)
  1049. {
  1050. int reg;
  1051. u32 val;
  1052. /* ILK FDI PLL is always enabled */
  1053. if (dev_priv->info->gen == 5)
  1054. return;
  1055. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1056. if (HAS_DDI(dev_priv->dev))
  1057. return;
  1058. reg = FDI_TX_CTL(pipe);
  1059. val = I915_READ(reg);
  1060. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1061. }
  1062. static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
  1063. enum pipe pipe)
  1064. {
  1065. int reg;
  1066. u32 val;
  1067. reg = FDI_RX_CTL(pipe);
  1068. val = I915_READ(reg);
  1069. WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
  1070. }
  1071. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1072. enum pipe pipe)
  1073. {
  1074. int pp_reg, lvds_reg;
  1075. u32 val;
  1076. enum pipe panel_pipe = PIPE_A;
  1077. bool locked = true;
  1078. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  1079. pp_reg = PCH_PP_CONTROL;
  1080. lvds_reg = PCH_LVDS;
  1081. } else {
  1082. pp_reg = PP_CONTROL;
  1083. lvds_reg = LVDS;
  1084. }
  1085. val = I915_READ(pp_reg);
  1086. if (!(val & PANEL_POWER_ON) ||
  1087. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  1088. locked = false;
  1089. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  1090. panel_pipe = PIPE_B;
  1091. WARN(panel_pipe == pipe && locked,
  1092. "panel assertion failure, pipe %c regs locked\n",
  1093. pipe_name(pipe));
  1094. }
  1095. void assert_pipe(struct drm_i915_private *dev_priv,
  1096. enum pipe pipe, bool state)
  1097. {
  1098. int reg;
  1099. u32 val;
  1100. bool cur_state;
  1101. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1102. pipe);
  1103. /* if we need the pipe A quirk it must be always on */
  1104. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  1105. state = true;
  1106. if (IS_HASWELL(dev_priv->dev) && cpu_transcoder != TRANSCODER_EDP &&
  1107. !(I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_ENABLE)) {
  1108. cur_state = false;
  1109. } else {
  1110. reg = PIPECONF(cpu_transcoder);
  1111. val = I915_READ(reg);
  1112. cur_state = !!(val & PIPECONF_ENABLE);
  1113. }
  1114. WARN(cur_state != state,
  1115. "pipe %c assertion failure (expected %s, current %s)\n",
  1116. pipe_name(pipe), state_string(state), state_string(cur_state));
  1117. }
  1118. static void assert_plane(struct drm_i915_private *dev_priv,
  1119. enum plane plane, bool state)
  1120. {
  1121. int reg;
  1122. u32 val;
  1123. bool cur_state;
  1124. reg = DSPCNTR(plane);
  1125. val = I915_READ(reg);
  1126. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1127. WARN(cur_state != state,
  1128. "plane %c assertion failure (expected %s, current %s)\n",
  1129. plane_name(plane), state_string(state), state_string(cur_state));
  1130. }
  1131. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1132. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1133. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1134. enum pipe pipe)
  1135. {
  1136. int reg, i;
  1137. u32 val;
  1138. int cur_pipe;
  1139. /* Planes are fixed to pipes on ILK+ */
  1140. if (HAS_PCH_SPLIT(dev_priv->dev) || IS_VALLEYVIEW(dev_priv->dev)) {
  1141. reg = DSPCNTR(pipe);
  1142. val = I915_READ(reg);
  1143. WARN((val & DISPLAY_PLANE_ENABLE),
  1144. "plane %c assertion failure, should be disabled but not\n",
  1145. plane_name(pipe));
  1146. return;
  1147. }
  1148. /* Need to check both planes against the pipe */
  1149. for (i = 0; i < 2; i++) {
  1150. reg = DSPCNTR(i);
  1151. val = I915_READ(reg);
  1152. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1153. DISPPLANE_SEL_PIPE_SHIFT;
  1154. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1155. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1156. plane_name(i), pipe_name(pipe));
  1157. }
  1158. }
  1159. static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1160. {
  1161. u32 val;
  1162. bool enabled;
  1163. if (HAS_PCH_LPT(dev_priv->dev)) {
  1164. DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
  1165. return;
  1166. }
  1167. val = I915_READ(PCH_DREF_CONTROL);
  1168. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1169. DREF_SUPERSPREAD_SOURCE_MASK));
  1170. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1171. }
  1172. static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
  1173. enum pipe pipe)
  1174. {
  1175. int reg;
  1176. u32 val;
  1177. bool enabled;
  1178. reg = TRANSCONF(pipe);
  1179. val = I915_READ(reg);
  1180. enabled = !!(val & TRANS_ENABLE);
  1181. WARN(enabled,
  1182. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1183. pipe_name(pipe));
  1184. }
  1185. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1186. enum pipe pipe, u32 port_sel, u32 val)
  1187. {
  1188. if ((val & DP_PORT_EN) == 0)
  1189. return false;
  1190. if (HAS_PCH_CPT(dev_priv->dev)) {
  1191. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1192. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1193. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1194. return false;
  1195. } else {
  1196. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1197. return false;
  1198. }
  1199. return true;
  1200. }
  1201. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1202. enum pipe pipe, u32 val)
  1203. {
  1204. if ((val & SDVO_ENABLE) == 0)
  1205. return false;
  1206. if (HAS_PCH_CPT(dev_priv->dev)) {
  1207. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1208. return false;
  1209. } else {
  1210. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1211. return false;
  1212. }
  1213. return true;
  1214. }
  1215. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1216. enum pipe pipe, u32 val)
  1217. {
  1218. if ((val & LVDS_PORT_EN) == 0)
  1219. return false;
  1220. if (HAS_PCH_CPT(dev_priv->dev)) {
  1221. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1222. return false;
  1223. } else {
  1224. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1225. return false;
  1226. }
  1227. return true;
  1228. }
  1229. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1230. enum pipe pipe, u32 val)
  1231. {
  1232. if ((val & ADPA_DAC_ENABLE) == 0)
  1233. return false;
  1234. if (HAS_PCH_CPT(dev_priv->dev)) {
  1235. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1236. return false;
  1237. } else {
  1238. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1239. return false;
  1240. }
  1241. return true;
  1242. }
  1243. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1244. enum pipe pipe, int reg, u32 port_sel)
  1245. {
  1246. u32 val = I915_READ(reg);
  1247. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1248. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1249. reg, pipe_name(pipe));
  1250. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1251. && (val & DP_PIPEB_SELECT),
  1252. "IBX PCH dp port still using transcoder B\n");
  1253. }
  1254. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1255. enum pipe pipe, int reg)
  1256. {
  1257. u32 val = I915_READ(reg);
  1258. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1259. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1260. reg, pipe_name(pipe));
  1261. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1262. && (val & SDVO_PIPE_B_SELECT),
  1263. "IBX PCH hdmi port still using transcoder B\n");
  1264. }
  1265. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1266. enum pipe pipe)
  1267. {
  1268. int reg;
  1269. u32 val;
  1270. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1271. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1272. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1273. reg = PCH_ADPA;
  1274. val = I915_READ(reg);
  1275. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1276. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1277. pipe_name(pipe));
  1278. reg = PCH_LVDS;
  1279. val = I915_READ(reg);
  1280. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1281. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1282. pipe_name(pipe));
  1283. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1284. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1285. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1286. }
  1287. /**
  1288. * intel_enable_pll - enable a PLL
  1289. * @dev_priv: i915 private structure
  1290. * @pipe: pipe PLL to enable
  1291. *
  1292. * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
  1293. * make sure the PLL reg is writable first though, since the panel write
  1294. * protect mechanism may be enabled.
  1295. *
  1296. * Note! This is for pre-ILK only.
  1297. *
  1298. * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
  1299. */
  1300. static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1301. {
  1302. int reg;
  1303. u32 val;
  1304. /* No really, not for ILK+ */
  1305. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
  1306. /* PLL is protected by panel, make sure we can write it */
  1307. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1308. assert_panel_unlocked(dev_priv, pipe);
  1309. reg = DPLL(pipe);
  1310. val = I915_READ(reg);
  1311. val |= DPLL_VCO_ENABLE;
  1312. /* We do this three times for luck */
  1313. I915_WRITE(reg, val);
  1314. POSTING_READ(reg);
  1315. udelay(150); /* wait for warmup */
  1316. I915_WRITE(reg, val);
  1317. POSTING_READ(reg);
  1318. udelay(150); /* wait for warmup */
  1319. I915_WRITE(reg, val);
  1320. POSTING_READ(reg);
  1321. udelay(150); /* wait for warmup */
  1322. }
  1323. /**
  1324. * intel_disable_pll - disable a PLL
  1325. * @dev_priv: i915 private structure
  1326. * @pipe: pipe PLL to disable
  1327. *
  1328. * Disable the PLL for @pipe, making sure the pipe is off first.
  1329. *
  1330. * Note! This is for pre-ILK only.
  1331. */
  1332. static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1333. {
  1334. int reg;
  1335. u32 val;
  1336. /* Don't disable pipe A or pipe A PLLs if needed */
  1337. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1338. return;
  1339. /* Make sure the pipe isn't still relying on us */
  1340. assert_pipe_disabled(dev_priv, pipe);
  1341. reg = DPLL(pipe);
  1342. val = I915_READ(reg);
  1343. val &= ~DPLL_VCO_ENABLE;
  1344. I915_WRITE(reg, val);
  1345. POSTING_READ(reg);
  1346. }
  1347. /* SBI access */
  1348. static void
  1349. intel_sbi_write(struct drm_i915_private *dev_priv, u16 reg, u32 value,
  1350. enum intel_sbi_destination destination)
  1351. {
  1352. u32 tmp;
  1353. WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
  1354. if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
  1355. 100)) {
  1356. DRM_ERROR("timeout waiting for SBI to become ready\n");
  1357. return;
  1358. }
  1359. I915_WRITE(SBI_ADDR, (reg << 16));
  1360. I915_WRITE(SBI_DATA, value);
  1361. if (destination == SBI_ICLK)
  1362. tmp = SBI_CTL_DEST_ICLK | SBI_CTL_OP_CRWR;
  1363. else
  1364. tmp = SBI_CTL_DEST_MPHY | SBI_CTL_OP_IOWR;
  1365. I915_WRITE(SBI_CTL_STAT, SBI_BUSY | tmp);
  1366. if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
  1367. 100)) {
  1368. DRM_ERROR("timeout waiting for SBI to complete write transaction\n");
  1369. return;
  1370. }
  1371. }
  1372. static u32
  1373. intel_sbi_read(struct drm_i915_private *dev_priv, u16 reg,
  1374. enum intel_sbi_destination destination)
  1375. {
  1376. u32 value = 0;
  1377. WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
  1378. if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
  1379. 100)) {
  1380. DRM_ERROR("timeout waiting for SBI to become ready\n");
  1381. return 0;
  1382. }
  1383. I915_WRITE(SBI_ADDR, (reg << 16));
  1384. if (destination == SBI_ICLK)
  1385. value = SBI_CTL_DEST_ICLK | SBI_CTL_OP_CRRD;
  1386. else
  1387. value = SBI_CTL_DEST_MPHY | SBI_CTL_OP_IORD;
  1388. I915_WRITE(SBI_CTL_STAT, value | SBI_BUSY);
  1389. if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
  1390. 100)) {
  1391. DRM_ERROR("timeout waiting for SBI to complete read transaction\n");
  1392. return 0;
  1393. }
  1394. return I915_READ(SBI_DATA);
  1395. }
  1396. /**
  1397. * ironlake_enable_pch_pll - enable PCH PLL
  1398. * @dev_priv: i915 private structure
  1399. * @pipe: pipe PLL to enable
  1400. *
  1401. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1402. * drives the transcoder clock.
  1403. */
  1404. static void ironlake_enable_pch_pll(struct intel_crtc *intel_crtc)
  1405. {
  1406. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  1407. struct intel_pch_pll *pll;
  1408. int reg;
  1409. u32 val;
  1410. /* PCH PLLs only available on ILK, SNB and IVB */
  1411. BUG_ON(dev_priv->info->gen < 5);
  1412. pll = intel_crtc->pch_pll;
  1413. if (pll == NULL)
  1414. return;
  1415. if (WARN_ON(pll->refcount == 0))
  1416. return;
  1417. DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
  1418. pll->pll_reg, pll->active, pll->on,
  1419. intel_crtc->base.base.id);
  1420. /* PCH refclock must be enabled first */
  1421. assert_pch_refclk_enabled(dev_priv);
  1422. if (pll->active++ && pll->on) {
  1423. assert_pch_pll_enabled(dev_priv, pll, NULL);
  1424. return;
  1425. }
  1426. DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
  1427. reg = pll->pll_reg;
  1428. val = I915_READ(reg);
  1429. val |= DPLL_VCO_ENABLE;
  1430. I915_WRITE(reg, val);
  1431. POSTING_READ(reg);
  1432. udelay(200);
  1433. pll->on = true;
  1434. }
  1435. static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
  1436. {
  1437. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  1438. struct intel_pch_pll *pll = intel_crtc->pch_pll;
  1439. int reg;
  1440. u32 val;
  1441. /* PCH only available on ILK+ */
  1442. BUG_ON(dev_priv->info->gen < 5);
  1443. if (pll == NULL)
  1444. return;
  1445. if (WARN_ON(pll->refcount == 0))
  1446. return;
  1447. DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
  1448. pll->pll_reg, pll->active, pll->on,
  1449. intel_crtc->base.base.id);
  1450. if (WARN_ON(pll->active == 0)) {
  1451. assert_pch_pll_disabled(dev_priv, pll, NULL);
  1452. return;
  1453. }
  1454. if (--pll->active) {
  1455. assert_pch_pll_enabled(dev_priv, pll, NULL);
  1456. return;
  1457. }
  1458. DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
  1459. /* Make sure transcoder isn't still depending on us */
  1460. assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
  1461. reg = pll->pll_reg;
  1462. val = I915_READ(reg);
  1463. val &= ~DPLL_VCO_ENABLE;
  1464. I915_WRITE(reg, val);
  1465. POSTING_READ(reg);
  1466. udelay(200);
  1467. pll->on = false;
  1468. }
  1469. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1470. enum pipe pipe)
  1471. {
  1472. struct drm_device *dev = dev_priv->dev;
  1473. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1474. uint32_t reg, val, pipeconf_val;
  1475. /* PCH only available on ILK+ */
  1476. BUG_ON(dev_priv->info->gen < 5);
  1477. /* Make sure PCH DPLL is enabled */
  1478. assert_pch_pll_enabled(dev_priv,
  1479. to_intel_crtc(crtc)->pch_pll,
  1480. to_intel_crtc(crtc));
  1481. /* FDI must be feeding us bits for PCH ports */
  1482. assert_fdi_tx_enabled(dev_priv, pipe);
  1483. assert_fdi_rx_enabled(dev_priv, pipe);
  1484. if (HAS_PCH_CPT(dev)) {
  1485. /* Workaround: Set the timing override bit before enabling the
  1486. * pch transcoder. */
  1487. reg = TRANS_CHICKEN2(pipe);
  1488. val = I915_READ(reg);
  1489. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1490. I915_WRITE(reg, val);
  1491. }
  1492. reg = TRANSCONF(pipe);
  1493. val = I915_READ(reg);
  1494. pipeconf_val = I915_READ(PIPECONF(pipe));
  1495. if (HAS_PCH_IBX(dev_priv->dev)) {
  1496. /*
  1497. * make the BPC in transcoder be consistent with
  1498. * that in pipeconf reg.
  1499. */
  1500. val &= ~PIPECONF_BPC_MASK;
  1501. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1502. }
  1503. val &= ~TRANS_INTERLACE_MASK;
  1504. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1505. if (HAS_PCH_IBX(dev_priv->dev) &&
  1506. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1507. val |= TRANS_LEGACY_INTERLACED_ILK;
  1508. else
  1509. val |= TRANS_INTERLACED;
  1510. else
  1511. val |= TRANS_PROGRESSIVE;
  1512. I915_WRITE(reg, val | TRANS_ENABLE);
  1513. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1514. DRM_ERROR("failed to enable transcoder %d\n", pipe);
  1515. }
  1516. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1517. enum transcoder cpu_transcoder)
  1518. {
  1519. u32 val, pipeconf_val;
  1520. /* PCH only available on ILK+ */
  1521. BUG_ON(dev_priv->info->gen < 5);
  1522. /* FDI must be feeding us bits for PCH ports */
  1523. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1524. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1525. /* Workaround: set timing override bit. */
  1526. val = I915_READ(_TRANSA_CHICKEN2);
  1527. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1528. I915_WRITE(_TRANSA_CHICKEN2, val);
  1529. val = TRANS_ENABLE;
  1530. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1531. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1532. PIPECONF_INTERLACED_ILK)
  1533. val |= TRANS_INTERLACED;
  1534. else
  1535. val |= TRANS_PROGRESSIVE;
  1536. I915_WRITE(TRANSCONF(TRANSCODER_A), val);
  1537. if (wait_for(I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE, 100))
  1538. DRM_ERROR("Failed to enable PCH transcoder\n");
  1539. }
  1540. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1541. enum pipe pipe)
  1542. {
  1543. struct drm_device *dev = dev_priv->dev;
  1544. uint32_t reg, val;
  1545. /* FDI relies on the transcoder */
  1546. assert_fdi_tx_disabled(dev_priv, pipe);
  1547. assert_fdi_rx_disabled(dev_priv, pipe);
  1548. /* Ports must be off as well */
  1549. assert_pch_ports_disabled(dev_priv, pipe);
  1550. reg = TRANSCONF(pipe);
  1551. val = I915_READ(reg);
  1552. val &= ~TRANS_ENABLE;
  1553. I915_WRITE(reg, val);
  1554. /* wait for PCH transcoder off, transcoder state */
  1555. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1556. DRM_ERROR("failed to disable transcoder %d\n", pipe);
  1557. if (!HAS_PCH_IBX(dev)) {
  1558. /* Workaround: Clear the timing override chicken bit again. */
  1559. reg = TRANS_CHICKEN2(pipe);
  1560. val = I915_READ(reg);
  1561. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1562. I915_WRITE(reg, val);
  1563. }
  1564. }
  1565. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1566. {
  1567. u32 val;
  1568. val = I915_READ(_TRANSACONF);
  1569. val &= ~TRANS_ENABLE;
  1570. I915_WRITE(_TRANSACONF, val);
  1571. /* wait for PCH transcoder off, transcoder state */
  1572. if (wait_for((I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE) == 0, 50))
  1573. DRM_ERROR("Failed to disable PCH transcoder\n");
  1574. /* Workaround: clear timing override bit. */
  1575. val = I915_READ(_TRANSA_CHICKEN2);
  1576. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1577. I915_WRITE(_TRANSA_CHICKEN2, val);
  1578. }
  1579. /**
  1580. * intel_enable_pipe - enable a pipe, asserting requirements
  1581. * @dev_priv: i915 private structure
  1582. * @pipe: pipe to enable
  1583. * @pch_port: on ILK+, is this pipe driving a PCH port or not
  1584. *
  1585. * Enable @pipe, making sure that various hardware specific requirements
  1586. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1587. *
  1588. * @pipe should be %PIPE_A or %PIPE_B.
  1589. *
  1590. * Will wait until the pipe is actually running (i.e. first vblank) before
  1591. * returning.
  1592. */
  1593. static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
  1594. bool pch_port)
  1595. {
  1596. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1597. pipe);
  1598. enum pipe pch_transcoder;
  1599. int reg;
  1600. u32 val;
  1601. if (HAS_PCH_LPT(dev_priv->dev))
  1602. pch_transcoder = TRANSCODER_A;
  1603. else
  1604. pch_transcoder = pipe;
  1605. /*
  1606. * A pipe without a PLL won't actually be able to drive bits from
  1607. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1608. * need the check.
  1609. */
  1610. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1611. assert_pll_enabled(dev_priv, pipe);
  1612. else {
  1613. if (pch_port) {
  1614. /* if driving the PCH, we need FDI enabled */
  1615. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1616. assert_fdi_tx_pll_enabled(dev_priv,
  1617. (enum pipe) cpu_transcoder);
  1618. }
  1619. /* FIXME: assert CPU port conditions for SNB+ */
  1620. }
  1621. reg = PIPECONF(cpu_transcoder);
  1622. val = I915_READ(reg);
  1623. if (val & PIPECONF_ENABLE)
  1624. return;
  1625. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1626. intel_wait_for_vblank(dev_priv->dev, pipe);
  1627. }
  1628. /**
  1629. * intel_disable_pipe - disable a pipe, asserting requirements
  1630. * @dev_priv: i915 private structure
  1631. * @pipe: pipe to disable
  1632. *
  1633. * Disable @pipe, making sure that various hardware specific requirements
  1634. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1635. *
  1636. * @pipe should be %PIPE_A or %PIPE_B.
  1637. *
  1638. * Will wait until the pipe has shut down before returning.
  1639. */
  1640. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1641. enum pipe pipe)
  1642. {
  1643. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1644. pipe);
  1645. int reg;
  1646. u32 val;
  1647. /*
  1648. * Make sure planes won't keep trying to pump pixels to us,
  1649. * or we might hang the display.
  1650. */
  1651. assert_planes_disabled(dev_priv, pipe);
  1652. /* Don't disable pipe A or pipe A PLLs if needed */
  1653. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1654. return;
  1655. reg = PIPECONF(cpu_transcoder);
  1656. val = I915_READ(reg);
  1657. if ((val & PIPECONF_ENABLE) == 0)
  1658. return;
  1659. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1660. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1661. }
  1662. /*
  1663. * Plane regs are double buffered, going from enabled->disabled needs a
  1664. * trigger in order to latch. The display address reg provides this.
  1665. */
  1666. void intel_flush_display_plane(struct drm_i915_private *dev_priv,
  1667. enum plane plane)
  1668. {
  1669. if (dev_priv->info->gen >= 4)
  1670. I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
  1671. else
  1672. I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
  1673. }
  1674. /**
  1675. * intel_enable_plane - enable a display plane on a given pipe
  1676. * @dev_priv: i915 private structure
  1677. * @plane: plane to enable
  1678. * @pipe: pipe being fed
  1679. *
  1680. * Enable @plane on @pipe, making sure that @pipe is running first.
  1681. */
  1682. static void intel_enable_plane(struct drm_i915_private *dev_priv,
  1683. enum plane plane, enum pipe pipe)
  1684. {
  1685. int reg;
  1686. u32 val;
  1687. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1688. assert_pipe_enabled(dev_priv, pipe);
  1689. reg = DSPCNTR(plane);
  1690. val = I915_READ(reg);
  1691. if (val & DISPLAY_PLANE_ENABLE)
  1692. return;
  1693. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1694. intel_flush_display_plane(dev_priv, plane);
  1695. intel_wait_for_vblank(dev_priv->dev, pipe);
  1696. }
  1697. /**
  1698. * intel_disable_plane - disable a display plane
  1699. * @dev_priv: i915 private structure
  1700. * @plane: plane to disable
  1701. * @pipe: pipe consuming the data
  1702. *
  1703. * Disable @plane; should be an independent operation.
  1704. */
  1705. static void intel_disable_plane(struct drm_i915_private *dev_priv,
  1706. enum plane plane, enum pipe pipe)
  1707. {
  1708. int reg;
  1709. u32 val;
  1710. reg = DSPCNTR(plane);
  1711. val = I915_READ(reg);
  1712. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  1713. return;
  1714. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1715. intel_flush_display_plane(dev_priv, plane);
  1716. intel_wait_for_vblank(dev_priv->dev, pipe);
  1717. }
  1718. static bool need_vtd_wa(struct drm_device *dev)
  1719. {
  1720. #ifdef CONFIG_INTEL_IOMMU
  1721. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1722. return true;
  1723. #endif
  1724. return false;
  1725. }
  1726. int
  1727. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1728. struct drm_i915_gem_object *obj,
  1729. struct intel_ring_buffer *pipelined)
  1730. {
  1731. struct drm_i915_private *dev_priv = dev->dev_private;
  1732. u32 alignment;
  1733. int ret;
  1734. switch (obj->tiling_mode) {
  1735. case I915_TILING_NONE:
  1736. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1737. alignment = 128 * 1024;
  1738. else if (INTEL_INFO(dev)->gen >= 4)
  1739. alignment = 4 * 1024;
  1740. else
  1741. alignment = 64 * 1024;
  1742. break;
  1743. case I915_TILING_X:
  1744. /* pin() will align the object as required by fence */
  1745. alignment = 0;
  1746. break;
  1747. case I915_TILING_Y:
  1748. /* FIXME: Is this true? */
  1749. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1750. return -EINVAL;
  1751. default:
  1752. BUG();
  1753. }
  1754. /* Note that the w/a also requires 64 PTE of padding following the
  1755. * bo. We currently fill all unused PTE with the shadow page and so
  1756. * we should always have valid PTE following the scanout preventing
  1757. * the VT-d warning.
  1758. */
  1759. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1760. alignment = 256 * 1024;
  1761. dev_priv->mm.interruptible = false;
  1762. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1763. if (ret)
  1764. goto err_interruptible;
  1765. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1766. * fence, whereas 965+ only requires a fence if using
  1767. * framebuffer compression. For simplicity, we always install
  1768. * a fence as the cost is not that onerous.
  1769. */
  1770. ret = i915_gem_object_get_fence(obj);
  1771. if (ret)
  1772. goto err_unpin;
  1773. i915_gem_object_pin_fence(obj);
  1774. dev_priv->mm.interruptible = true;
  1775. return 0;
  1776. err_unpin:
  1777. i915_gem_object_unpin(obj);
  1778. err_interruptible:
  1779. dev_priv->mm.interruptible = true;
  1780. return ret;
  1781. }
  1782. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1783. {
  1784. i915_gem_object_unpin_fence(obj);
  1785. i915_gem_object_unpin(obj);
  1786. }
  1787. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1788. * is assumed to be a power-of-two. */
  1789. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1790. unsigned int tiling_mode,
  1791. unsigned int cpp,
  1792. unsigned int pitch)
  1793. {
  1794. if (tiling_mode != I915_TILING_NONE) {
  1795. unsigned int tile_rows, tiles;
  1796. tile_rows = *y / 8;
  1797. *y %= 8;
  1798. tiles = *x / (512/cpp);
  1799. *x %= 512/cpp;
  1800. return tile_rows * pitch * 8 + tiles * 4096;
  1801. } else {
  1802. unsigned int offset;
  1803. offset = *y * pitch + *x * cpp;
  1804. *y = 0;
  1805. *x = (offset & 4095) / cpp;
  1806. return offset & -4096;
  1807. }
  1808. }
  1809. static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1810. int x, int y)
  1811. {
  1812. struct drm_device *dev = crtc->dev;
  1813. struct drm_i915_private *dev_priv = dev->dev_private;
  1814. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1815. struct intel_framebuffer *intel_fb;
  1816. struct drm_i915_gem_object *obj;
  1817. int plane = intel_crtc->plane;
  1818. unsigned long linear_offset;
  1819. u32 dspcntr;
  1820. u32 reg;
  1821. switch (plane) {
  1822. case 0:
  1823. case 1:
  1824. break;
  1825. default:
  1826. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1827. return -EINVAL;
  1828. }
  1829. intel_fb = to_intel_framebuffer(fb);
  1830. obj = intel_fb->obj;
  1831. reg = DSPCNTR(plane);
  1832. dspcntr = I915_READ(reg);
  1833. /* Mask out pixel format bits in case we change it */
  1834. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1835. switch (fb->pixel_format) {
  1836. case DRM_FORMAT_C8:
  1837. dspcntr |= DISPPLANE_8BPP;
  1838. break;
  1839. case DRM_FORMAT_XRGB1555:
  1840. case DRM_FORMAT_ARGB1555:
  1841. dspcntr |= DISPPLANE_BGRX555;
  1842. break;
  1843. case DRM_FORMAT_RGB565:
  1844. dspcntr |= DISPPLANE_BGRX565;
  1845. break;
  1846. case DRM_FORMAT_XRGB8888:
  1847. case DRM_FORMAT_ARGB8888:
  1848. dspcntr |= DISPPLANE_BGRX888;
  1849. break;
  1850. case DRM_FORMAT_XBGR8888:
  1851. case DRM_FORMAT_ABGR8888:
  1852. dspcntr |= DISPPLANE_RGBX888;
  1853. break;
  1854. case DRM_FORMAT_XRGB2101010:
  1855. case DRM_FORMAT_ARGB2101010:
  1856. dspcntr |= DISPPLANE_BGRX101010;
  1857. break;
  1858. case DRM_FORMAT_XBGR2101010:
  1859. case DRM_FORMAT_ABGR2101010:
  1860. dspcntr |= DISPPLANE_RGBX101010;
  1861. break;
  1862. default:
  1863. BUG();
  1864. }
  1865. if (INTEL_INFO(dev)->gen >= 4) {
  1866. if (obj->tiling_mode != I915_TILING_NONE)
  1867. dspcntr |= DISPPLANE_TILED;
  1868. else
  1869. dspcntr &= ~DISPPLANE_TILED;
  1870. }
  1871. I915_WRITE(reg, dspcntr);
  1872. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1873. if (INTEL_INFO(dev)->gen >= 4) {
  1874. intel_crtc->dspaddr_offset =
  1875. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  1876. fb->bits_per_pixel / 8,
  1877. fb->pitches[0]);
  1878. linear_offset -= intel_crtc->dspaddr_offset;
  1879. } else {
  1880. intel_crtc->dspaddr_offset = linear_offset;
  1881. }
  1882. DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
  1883. obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
  1884. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1885. if (INTEL_INFO(dev)->gen >= 4) {
  1886. I915_MODIFY_DISPBASE(DSPSURF(plane),
  1887. obj->gtt_offset + intel_crtc->dspaddr_offset);
  1888. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1889. I915_WRITE(DSPLINOFF(plane), linear_offset);
  1890. } else
  1891. I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
  1892. POSTING_READ(reg);
  1893. return 0;
  1894. }
  1895. static int ironlake_update_plane(struct drm_crtc *crtc,
  1896. struct drm_framebuffer *fb, int x, int y)
  1897. {
  1898. struct drm_device *dev = crtc->dev;
  1899. struct drm_i915_private *dev_priv = dev->dev_private;
  1900. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1901. struct intel_framebuffer *intel_fb;
  1902. struct drm_i915_gem_object *obj;
  1903. int plane = intel_crtc->plane;
  1904. unsigned long linear_offset;
  1905. u32 dspcntr;
  1906. u32 reg;
  1907. switch (plane) {
  1908. case 0:
  1909. case 1:
  1910. case 2:
  1911. break;
  1912. default:
  1913. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1914. return -EINVAL;
  1915. }
  1916. intel_fb = to_intel_framebuffer(fb);
  1917. obj = intel_fb->obj;
  1918. reg = DSPCNTR(plane);
  1919. dspcntr = I915_READ(reg);
  1920. /* Mask out pixel format bits in case we change it */
  1921. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1922. switch (fb->pixel_format) {
  1923. case DRM_FORMAT_C8:
  1924. dspcntr |= DISPPLANE_8BPP;
  1925. break;
  1926. case DRM_FORMAT_RGB565:
  1927. dspcntr |= DISPPLANE_BGRX565;
  1928. break;
  1929. case DRM_FORMAT_XRGB8888:
  1930. case DRM_FORMAT_ARGB8888:
  1931. dspcntr |= DISPPLANE_BGRX888;
  1932. break;
  1933. case DRM_FORMAT_XBGR8888:
  1934. case DRM_FORMAT_ABGR8888:
  1935. dspcntr |= DISPPLANE_RGBX888;
  1936. break;
  1937. case DRM_FORMAT_XRGB2101010:
  1938. case DRM_FORMAT_ARGB2101010:
  1939. dspcntr |= DISPPLANE_BGRX101010;
  1940. break;
  1941. case DRM_FORMAT_XBGR2101010:
  1942. case DRM_FORMAT_ABGR2101010:
  1943. dspcntr |= DISPPLANE_RGBX101010;
  1944. break;
  1945. default:
  1946. BUG();
  1947. }
  1948. if (obj->tiling_mode != I915_TILING_NONE)
  1949. dspcntr |= DISPPLANE_TILED;
  1950. else
  1951. dspcntr &= ~DISPPLANE_TILED;
  1952. /* must disable */
  1953. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1954. I915_WRITE(reg, dspcntr);
  1955. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1956. intel_crtc->dspaddr_offset =
  1957. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  1958. fb->bits_per_pixel / 8,
  1959. fb->pitches[0]);
  1960. linear_offset -= intel_crtc->dspaddr_offset;
  1961. DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
  1962. obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
  1963. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1964. I915_MODIFY_DISPBASE(DSPSURF(plane),
  1965. obj->gtt_offset + intel_crtc->dspaddr_offset);
  1966. if (IS_HASWELL(dev)) {
  1967. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  1968. } else {
  1969. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1970. I915_WRITE(DSPLINOFF(plane), linear_offset);
  1971. }
  1972. POSTING_READ(reg);
  1973. return 0;
  1974. }
  1975. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1976. static int
  1977. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1978. int x, int y, enum mode_set_atomic state)
  1979. {
  1980. struct drm_device *dev = crtc->dev;
  1981. struct drm_i915_private *dev_priv = dev->dev_private;
  1982. if (dev_priv->display.disable_fbc)
  1983. dev_priv->display.disable_fbc(dev);
  1984. intel_increase_pllclock(crtc);
  1985. return dev_priv->display.update_plane(crtc, fb, x, y);
  1986. }
  1987. void intel_display_handle_reset(struct drm_device *dev)
  1988. {
  1989. struct drm_i915_private *dev_priv = dev->dev_private;
  1990. struct drm_crtc *crtc;
  1991. /*
  1992. * Flips in the rings have been nuked by the reset,
  1993. * so complete all pending flips so that user space
  1994. * will get its events and not get stuck.
  1995. *
  1996. * Also update the base address of all primary
  1997. * planes to the the last fb to make sure we're
  1998. * showing the correct fb after a reset.
  1999. *
  2000. * Need to make two loops over the crtcs so that we
  2001. * don't try to grab a crtc mutex before the
  2002. * pending_flip_queue really got woken up.
  2003. */
  2004. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2005. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2006. enum plane plane = intel_crtc->plane;
  2007. intel_prepare_page_flip(dev, plane);
  2008. intel_finish_page_flip_plane(dev, plane);
  2009. }
  2010. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2011. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2012. mutex_lock(&crtc->mutex);
  2013. if (intel_crtc->active)
  2014. dev_priv->display.update_plane(crtc, crtc->fb,
  2015. crtc->x, crtc->y);
  2016. mutex_unlock(&crtc->mutex);
  2017. }
  2018. }
  2019. static int
  2020. intel_finish_fb(struct drm_framebuffer *old_fb)
  2021. {
  2022. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  2023. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2024. bool was_interruptible = dev_priv->mm.interruptible;
  2025. int ret;
  2026. /* Big Hammer, we also need to ensure that any pending
  2027. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2028. * current scanout is retired before unpinning the old
  2029. * framebuffer.
  2030. *
  2031. * This should only fail upon a hung GPU, in which case we
  2032. * can safely continue.
  2033. */
  2034. dev_priv->mm.interruptible = false;
  2035. ret = i915_gem_object_finish_gpu(obj);
  2036. dev_priv->mm.interruptible = was_interruptible;
  2037. return ret;
  2038. }
  2039. static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
  2040. {
  2041. struct drm_device *dev = crtc->dev;
  2042. struct drm_i915_master_private *master_priv;
  2043. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2044. if (!dev->primary->master)
  2045. return;
  2046. master_priv = dev->primary->master->driver_priv;
  2047. if (!master_priv->sarea_priv)
  2048. return;
  2049. switch (intel_crtc->pipe) {
  2050. case 0:
  2051. master_priv->sarea_priv->pipeA_x = x;
  2052. master_priv->sarea_priv->pipeA_y = y;
  2053. break;
  2054. case 1:
  2055. master_priv->sarea_priv->pipeB_x = x;
  2056. master_priv->sarea_priv->pipeB_y = y;
  2057. break;
  2058. default:
  2059. break;
  2060. }
  2061. }
  2062. static int
  2063. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  2064. struct drm_framebuffer *fb)
  2065. {
  2066. struct drm_device *dev = crtc->dev;
  2067. struct drm_i915_private *dev_priv = dev->dev_private;
  2068. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2069. struct drm_framebuffer *old_fb;
  2070. int ret;
  2071. /* no fb bound */
  2072. if (!fb) {
  2073. DRM_ERROR("No FB bound\n");
  2074. return 0;
  2075. }
  2076. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  2077. DRM_ERROR("no plane for crtc: plane %d, num_pipes %d\n",
  2078. intel_crtc->plane,
  2079. INTEL_INFO(dev)->num_pipes);
  2080. return -EINVAL;
  2081. }
  2082. mutex_lock(&dev->struct_mutex);
  2083. ret = intel_pin_and_fence_fb_obj(dev,
  2084. to_intel_framebuffer(fb)->obj,
  2085. NULL);
  2086. if (ret != 0) {
  2087. mutex_unlock(&dev->struct_mutex);
  2088. DRM_ERROR("pin & fence failed\n");
  2089. return ret;
  2090. }
  2091. ret = dev_priv->display.update_plane(crtc, fb, x, y);
  2092. if (ret) {
  2093. intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
  2094. mutex_unlock(&dev->struct_mutex);
  2095. DRM_ERROR("failed to update base address\n");
  2096. return ret;
  2097. }
  2098. old_fb = crtc->fb;
  2099. crtc->fb = fb;
  2100. crtc->x = x;
  2101. crtc->y = y;
  2102. if (old_fb) {
  2103. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2104. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  2105. }
  2106. intel_update_fbc(dev);
  2107. mutex_unlock(&dev->struct_mutex);
  2108. intel_crtc_update_sarea_pos(crtc, x, y);
  2109. return 0;
  2110. }
  2111. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2112. {
  2113. struct drm_device *dev = crtc->dev;
  2114. struct drm_i915_private *dev_priv = dev->dev_private;
  2115. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2116. int pipe = intel_crtc->pipe;
  2117. u32 reg, temp;
  2118. /* enable normal train */
  2119. reg = FDI_TX_CTL(pipe);
  2120. temp = I915_READ(reg);
  2121. if (IS_IVYBRIDGE(dev)) {
  2122. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2123. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2124. } else {
  2125. temp &= ~FDI_LINK_TRAIN_NONE;
  2126. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2127. }
  2128. I915_WRITE(reg, temp);
  2129. reg = FDI_RX_CTL(pipe);
  2130. temp = I915_READ(reg);
  2131. if (HAS_PCH_CPT(dev)) {
  2132. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2133. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2134. } else {
  2135. temp &= ~FDI_LINK_TRAIN_NONE;
  2136. temp |= FDI_LINK_TRAIN_NONE;
  2137. }
  2138. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2139. /* wait one idle pattern time */
  2140. POSTING_READ(reg);
  2141. udelay(1000);
  2142. /* IVB wants error correction enabled */
  2143. if (IS_IVYBRIDGE(dev))
  2144. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2145. FDI_FE_ERRC_ENABLE);
  2146. }
  2147. static void ivb_modeset_global_resources(struct drm_device *dev)
  2148. {
  2149. struct drm_i915_private *dev_priv = dev->dev_private;
  2150. struct intel_crtc *pipe_B_crtc =
  2151. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2152. struct intel_crtc *pipe_C_crtc =
  2153. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2154. uint32_t temp;
  2155. /* When everything is off disable fdi C so that we could enable fdi B
  2156. * with all lanes. XXX: This misses the case where a pipe is not using
  2157. * any pch resources and so doesn't need any fdi lanes. */
  2158. if (!pipe_B_crtc->base.enabled && !pipe_C_crtc->base.enabled) {
  2159. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2160. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2161. temp = I915_READ(SOUTH_CHICKEN1);
  2162. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2163. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2164. I915_WRITE(SOUTH_CHICKEN1, temp);
  2165. }
  2166. }
  2167. /* The FDI link training functions for ILK/Ibexpeak. */
  2168. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2169. {
  2170. struct drm_device *dev = crtc->dev;
  2171. struct drm_i915_private *dev_priv = dev->dev_private;
  2172. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2173. int pipe = intel_crtc->pipe;
  2174. int plane = intel_crtc->plane;
  2175. u32 reg, temp, tries;
  2176. /* FDI needs bits from pipe & plane first */
  2177. assert_pipe_enabled(dev_priv, pipe);
  2178. assert_plane_enabled(dev_priv, plane);
  2179. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2180. for train result */
  2181. reg = FDI_RX_IMR(pipe);
  2182. temp = I915_READ(reg);
  2183. temp &= ~FDI_RX_SYMBOL_LOCK;
  2184. temp &= ~FDI_RX_BIT_LOCK;
  2185. I915_WRITE(reg, temp);
  2186. I915_READ(reg);
  2187. udelay(150);
  2188. /* enable CPU FDI TX and PCH FDI RX */
  2189. reg = FDI_TX_CTL(pipe);
  2190. temp = I915_READ(reg);
  2191. temp &= ~(7 << 19);
  2192. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2193. temp &= ~FDI_LINK_TRAIN_NONE;
  2194. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2195. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2196. reg = FDI_RX_CTL(pipe);
  2197. temp = I915_READ(reg);
  2198. temp &= ~FDI_LINK_TRAIN_NONE;
  2199. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2200. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2201. POSTING_READ(reg);
  2202. udelay(150);
  2203. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2204. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2205. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2206. FDI_RX_PHASE_SYNC_POINTER_EN);
  2207. reg = FDI_RX_IIR(pipe);
  2208. for (tries = 0; tries < 5; tries++) {
  2209. temp = I915_READ(reg);
  2210. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2211. if ((temp & FDI_RX_BIT_LOCK)) {
  2212. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2213. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2214. break;
  2215. }
  2216. }
  2217. if (tries == 5)
  2218. DRM_ERROR("FDI train 1 fail!\n");
  2219. /* Train 2 */
  2220. reg = FDI_TX_CTL(pipe);
  2221. temp = I915_READ(reg);
  2222. temp &= ~FDI_LINK_TRAIN_NONE;
  2223. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2224. I915_WRITE(reg, temp);
  2225. reg = FDI_RX_CTL(pipe);
  2226. temp = I915_READ(reg);
  2227. temp &= ~FDI_LINK_TRAIN_NONE;
  2228. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2229. I915_WRITE(reg, temp);
  2230. POSTING_READ(reg);
  2231. udelay(150);
  2232. reg = FDI_RX_IIR(pipe);
  2233. for (tries = 0; tries < 5; tries++) {
  2234. temp = I915_READ(reg);
  2235. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2236. if (temp & FDI_RX_SYMBOL_LOCK) {
  2237. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2238. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2239. break;
  2240. }
  2241. }
  2242. if (tries == 5)
  2243. DRM_ERROR("FDI train 2 fail!\n");
  2244. DRM_DEBUG_KMS("FDI train done\n");
  2245. }
  2246. static const int snb_b_fdi_train_param[] = {
  2247. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2248. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2249. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2250. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2251. };
  2252. /* The FDI link training functions for SNB/Cougarpoint. */
  2253. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2254. {
  2255. struct drm_device *dev = crtc->dev;
  2256. struct drm_i915_private *dev_priv = dev->dev_private;
  2257. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2258. int pipe = intel_crtc->pipe;
  2259. u32 reg, temp, i, retry;
  2260. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2261. for train result */
  2262. reg = FDI_RX_IMR(pipe);
  2263. temp = I915_READ(reg);
  2264. temp &= ~FDI_RX_SYMBOL_LOCK;
  2265. temp &= ~FDI_RX_BIT_LOCK;
  2266. I915_WRITE(reg, temp);
  2267. POSTING_READ(reg);
  2268. udelay(150);
  2269. /* enable CPU FDI TX and PCH FDI RX */
  2270. reg = FDI_TX_CTL(pipe);
  2271. temp = I915_READ(reg);
  2272. temp &= ~(7 << 19);
  2273. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2274. temp &= ~FDI_LINK_TRAIN_NONE;
  2275. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2276. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2277. /* SNB-B */
  2278. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2279. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2280. I915_WRITE(FDI_RX_MISC(pipe),
  2281. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2282. reg = FDI_RX_CTL(pipe);
  2283. temp = I915_READ(reg);
  2284. if (HAS_PCH_CPT(dev)) {
  2285. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2286. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2287. } else {
  2288. temp &= ~FDI_LINK_TRAIN_NONE;
  2289. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2290. }
  2291. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2292. POSTING_READ(reg);
  2293. udelay(150);
  2294. for (i = 0; i < 4; i++) {
  2295. reg = FDI_TX_CTL(pipe);
  2296. temp = I915_READ(reg);
  2297. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2298. temp |= snb_b_fdi_train_param[i];
  2299. I915_WRITE(reg, temp);
  2300. POSTING_READ(reg);
  2301. udelay(500);
  2302. for (retry = 0; retry < 5; retry++) {
  2303. reg = FDI_RX_IIR(pipe);
  2304. temp = I915_READ(reg);
  2305. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2306. if (temp & FDI_RX_BIT_LOCK) {
  2307. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2308. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2309. break;
  2310. }
  2311. udelay(50);
  2312. }
  2313. if (retry < 5)
  2314. break;
  2315. }
  2316. if (i == 4)
  2317. DRM_ERROR("FDI train 1 fail!\n");
  2318. /* Train 2 */
  2319. reg = FDI_TX_CTL(pipe);
  2320. temp = I915_READ(reg);
  2321. temp &= ~FDI_LINK_TRAIN_NONE;
  2322. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2323. if (IS_GEN6(dev)) {
  2324. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2325. /* SNB-B */
  2326. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2327. }
  2328. I915_WRITE(reg, temp);
  2329. reg = FDI_RX_CTL(pipe);
  2330. temp = I915_READ(reg);
  2331. if (HAS_PCH_CPT(dev)) {
  2332. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2333. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2334. } else {
  2335. temp &= ~FDI_LINK_TRAIN_NONE;
  2336. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2337. }
  2338. I915_WRITE(reg, temp);
  2339. POSTING_READ(reg);
  2340. udelay(150);
  2341. for (i = 0; i < 4; i++) {
  2342. reg = FDI_TX_CTL(pipe);
  2343. temp = I915_READ(reg);
  2344. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2345. temp |= snb_b_fdi_train_param[i];
  2346. I915_WRITE(reg, temp);
  2347. POSTING_READ(reg);
  2348. udelay(500);
  2349. for (retry = 0; retry < 5; retry++) {
  2350. reg = FDI_RX_IIR(pipe);
  2351. temp = I915_READ(reg);
  2352. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2353. if (temp & FDI_RX_SYMBOL_LOCK) {
  2354. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2355. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2356. break;
  2357. }
  2358. udelay(50);
  2359. }
  2360. if (retry < 5)
  2361. break;
  2362. }
  2363. if (i == 4)
  2364. DRM_ERROR("FDI train 2 fail!\n");
  2365. DRM_DEBUG_KMS("FDI train done.\n");
  2366. }
  2367. /* Manual link training for Ivy Bridge A0 parts */
  2368. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2369. {
  2370. struct drm_device *dev = crtc->dev;
  2371. struct drm_i915_private *dev_priv = dev->dev_private;
  2372. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2373. int pipe = intel_crtc->pipe;
  2374. u32 reg, temp, i;
  2375. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2376. for train result */
  2377. reg = FDI_RX_IMR(pipe);
  2378. temp = I915_READ(reg);
  2379. temp &= ~FDI_RX_SYMBOL_LOCK;
  2380. temp &= ~FDI_RX_BIT_LOCK;
  2381. I915_WRITE(reg, temp);
  2382. POSTING_READ(reg);
  2383. udelay(150);
  2384. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2385. I915_READ(FDI_RX_IIR(pipe)));
  2386. /* enable CPU FDI TX and PCH FDI RX */
  2387. reg = FDI_TX_CTL(pipe);
  2388. temp = I915_READ(reg);
  2389. temp &= ~(7 << 19);
  2390. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2391. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2392. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2393. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2394. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2395. temp |= FDI_COMPOSITE_SYNC;
  2396. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2397. I915_WRITE(FDI_RX_MISC(pipe),
  2398. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2399. reg = FDI_RX_CTL(pipe);
  2400. temp = I915_READ(reg);
  2401. temp &= ~FDI_LINK_TRAIN_AUTO;
  2402. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2403. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2404. temp |= FDI_COMPOSITE_SYNC;
  2405. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2406. POSTING_READ(reg);
  2407. udelay(150);
  2408. for (i = 0; i < 4; i++) {
  2409. reg = FDI_TX_CTL(pipe);
  2410. temp = I915_READ(reg);
  2411. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2412. temp |= snb_b_fdi_train_param[i];
  2413. I915_WRITE(reg, temp);
  2414. POSTING_READ(reg);
  2415. udelay(500);
  2416. reg = FDI_RX_IIR(pipe);
  2417. temp = I915_READ(reg);
  2418. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2419. if (temp & FDI_RX_BIT_LOCK ||
  2420. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2421. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2422. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
  2423. break;
  2424. }
  2425. }
  2426. if (i == 4)
  2427. DRM_ERROR("FDI train 1 fail!\n");
  2428. /* Train 2 */
  2429. reg = FDI_TX_CTL(pipe);
  2430. temp = I915_READ(reg);
  2431. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2432. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2433. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2434. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2435. I915_WRITE(reg, temp);
  2436. reg = FDI_RX_CTL(pipe);
  2437. temp = I915_READ(reg);
  2438. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2439. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2440. I915_WRITE(reg, temp);
  2441. POSTING_READ(reg);
  2442. udelay(150);
  2443. for (i = 0; i < 4; i++) {
  2444. reg = FDI_TX_CTL(pipe);
  2445. temp = I915_READ(reg);
  2446. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2447. temp |= snb_b_fdi_train_param[i];
  2448. I915_WRITE(reg, temp);
  2449. POSTING_READ(reg);
  2450. udelay(500);
  2451. reg = FDI_RX_IIR(pipe);
  2452. temp = I915_READ(reg);
  2453. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2454. if (temp & FDI_RX_SYMBOL_LOCK) {
  2455. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2456. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
  2457. break;
  2458. }
  2459. }
  2460. if (i == 4)
  2461. DRM_ERROR("FDI train 2 fail!\n");
  2462. DRM_DEBUG_KMS("FDI train done.\n");
  2463. }
  2464. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2465. {
  2466. struct drm_device *dev = intel_crtc->base.dev;
  2467. struct drm_i915_private *dev_priv = dev->dev_private;
  2468. int pipe = intel_crtc->pipe;
  2469. u32 reg, temp;
  2470. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2471. reg = FDI_RX_CTL(pipe);
  2472. temp = I915_READ(reg);
  2473. temp &= ~((0x7 << 19) | (0x7 << 16));
  2474. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2475. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2476. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2477. POSTING_READ(reg);
  2478. udelay(200);
  2479. /* Switch from Rawclk to PCDclk */
  2480. temp = I915_READ(reg);
  2481. I915_WRITE(reg, temp | FDI_PCDCLK);
  2482. POSTING_READ(reg);
  2483. udelay(200);
  2484. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2485. reg = FDI_TX_CTL(pipe);
  2486. temp = I915_READ(reg);
  2487. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2488. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2489. POSTING_READ(reg);
  2490. udelay(100);
  2491. }
  2492. }
  2493. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2494. {
  2495. struct drm_device *dev = intel_crtc->base.dev;
  2496. struct drm_i915_private *dev_priv = dev->dev_private;
  2497. int pipe = intel_crtc->pipe;
  2498. u32 reg, temp;
  2499. /* Switch from PCDclk to Rawclk */
  2500. reg = FDI_RX_CTL(pipe);
  2501. temp = I915_READ(reg);
  2502. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2503. /* Disable CPU FDI TX PLL */
  2504. reg = FDI_TX_CTL(pipe);
  2505. temp = I915_READ(reg);
  2506. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2507. POSTING_READ(reg);
  2508. udelay(100);
  2509. reg = FDI_RX_CTL(pipe);
  2510. temp = I915_READ(reg);
  2511. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2512. /* Wait for the clocks to turn off. */
  2513. POSTING_READ(reg);
  2514. udelay(100);
  2515. }
  2516. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2517. {
  2518. struct drm_device *dev = crtc->dev;
  2519. struct drm_i915_private *dev_priv = dev->dev_private;
  2520. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2521. int pipe = intel_crtc->pipe;
  2522. u32 reg, temp;
  2523. /* disable CPU FDI tx and PCH FDI rx */
  2524. reg = FDI_TX_CTL(pipe);
  2525. temp = I915_READ(reg);
  2526. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2527. POSTING_READ(reg);
  2528. reg = FDI_RX_CTL(pipe);
  2529. temp = I915_READ(reg);
  2530. temp &= ~(0x7 << 16);
  2531. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2532. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2533. POSTING_READ(reg);
  2534. udelay(100);
  2535. /* Ironlake workaround, disable clock pointer after downing FDI */
  2536. if (HAS_PCH_IBX(dev)) {
  2537. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2538. }
  2539. /* still set train pattern 1 */
  2540. reg = FDI_TX_CTL(pipe);
  2541. temp = I915_READ(reg);
  2542. temp &= ~FDI_LINK_TRAIN_NONE;
  2543. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2544. I915_WRITE(reg, temp);
  2545. reg = FDI_RX_CTL(pipe);
  2546. temp = I915_READ(reg);
  2547. if (HAS_PCH_CPT(dev)) {
  2548. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2549. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2550. } else {
  2551. temp &= ~FDI_LINK_TRAIN_NONE;
  2552. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2553. }
  2554. /* BPC in FDI rx is consistent with that in PIPECONF */
  2555. temp &= ~(0x07 << 16);
  2556. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2557. I915_WRITE(reg, temp);
  2558. POSTING_READ(reg);
  2559. udelay(100);
  2560. }
  2561. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2562. {
  2563. struct drm_device *dev = crtc->dev;
  2564. struct drm_i915_private *dev_priv = dev->dev_private;
  2565. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2566. unsigned long flags;
  2567. bool pending;
  2568. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2569. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2570. return false;
  2571. spin_lock_irqsave(&dev->event_lock, flags);
  2572. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2573. spin_unlock_irqrestore(&dev->event_lock, flags);
  2574. return pending;
  2575. }
  2576. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2577. {
  2578. struct drm_device *dev = crtc->dev;
  2579. struct drm_i915_private *dev_priv = dev->dev_private;
  2580. if (crtc->fb == NULL)
  2581. return;
  2582. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  2583. wait_event(dev_priv->pending_flip_queue,
  2584. !intel_crtc_has_pending_flip(crtc));
  2585. mutex_lock(&dev->struct_mutex);
  2586. intel_finish_fb(crtc->fb);
  2587. mutex_unlock(&dev->struct_mutex);
  2588. }
  2589. static bool haswell_crtc_driving_pch(struct drm_crtc *crtc)
  2590. {
  2591. return intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG);
  2592. }
  2593. /* Program iCLKIP clock to the desired frequency */
  2594. static void lpt_program_iclkip(struct drm_crtc *crtc)
  2595. {
  2596. struct drm_device *dev = crtc->dev;
  2597. struct drm_i915_private *dev_priv = dev->dev_private;
  2598. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  2599. u32 temp;
  2600. mutex_lock(&dev_priv->dpio_lock);
  2601. /* It is necessary to ungate the pixclk gate prior to programming
  2602. * the divisors, and gate it back when it is done.
  2603. */
  2604. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  2605. /* Disable SSCCTL */
  2606. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  2607. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  2608. SBI_SSCCTL_DISABLE,
  2609. SBI_ICLK);
  2610. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  2611. if (crtc->mode.clock == 20000) {
  2612. auxdiv = 1;
  2613. divsel = 0x41;
  2614. phaseinc = 0x20;
  2615. } else {
  2616. /* The iCLK virtual clock root frequency is in MHz,
  2617. * but the crtc->mode.clock in in KHz. To get the divisors,
  2618. * it is necessary to divide one by another, so we
  2619. * convert the virtual clock precision to KHz here for higher
  2620. * precision.
  2621. */
  2622. u32 iclk_virtual_root_freq = 172800 * 1000;
  2623. u32 iclk_pi_range = 64;
  2624. u32 desired_divisor, msb_divisor_value, pi_value;
  2625. desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
  2626. msb_divisor_value = desired_divisor / iclk_pi_range;
  2627. pi_value = desired_divisor % iclk_pi_range;
  2628. auxdiv = 0;
  2629. divsel = msb_divisor_value - 2;
  2630. phaseinc = pi_value;
  2631. }
  2632. /* This should not happen with any sane values */
  2633. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  2634. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  2635. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  2636. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  2637. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  2638. crtc->mode.clock,
  2639. auxdiv,
  2640. divsel,
  2641. phasedir,
  2642. phaseinc);
  2643. /* Program SSCDIVINTPHASE6 */
  2644. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  2645. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  2646. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  2647. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  2648. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  2649. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  2650. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  2651. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  2652. /* Program SSCAUXDIV */
  2653. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  2654. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  2655. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  2656. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  2657. /* Enable modulator and associated divider */
  2658. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  2659. temp &= ~SBI_SSCCTL_DISABLE;
  2660. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  2661. /* Wait for initialization time */
  2662. udelay(24);
  2663. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  2664. mutex_unlock(&dev_priv->dpio_lock);
  2665. }
  2666. /*
  2667. * Enable PCH resources required for PCH ports:
  2668. * - PCH PLLs
  2669. * - FDI training & RX/TX
  2670. * - update transcoder timings
  2671. * - DP transcoding bits
  2672. * - transcoder
  2673. */
  2674. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2675. {
  2676. struct drm_device *dev = crtc->dev;
  2677. struct drm_i915_private *dev_priv = dev->dev_private;
  2678. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2679. int pipe = intel_crtc->pipe;
  2680. u32 reg, temp;
  2681. assert_transcoder_disabled(dev_priv, pipe);
  2682. /* Write the TU size bits before fdi link training, so that error
  2683. * detection works. */
  2684. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2685. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2686. /* For PCH output, training FDI link */
  2687. dev_priv->display.fdi_link_train(crtc);
  2688. /* XXX: pch pll's can be enabled any time before we enable the PCH
  2689. * transcoder, and we actually should do this to not upset any PCH
  2690. * transcoder that already use the clock when we share it.
  2691. *
  2692. * Note that enable_pch_pll tries to do the right thing, but get_pch_pll
  2693. * unconditionally resets the pll - we need that to have the right LVDS
  2694. * enable sequence. */
  2695. ironlake_enable_pch_pll(intel_crtc);
  2696. if (HAS_PCH_CPT(dev)) {
  2697. u32 sel;
  2698. temp = I915_READ(PCH_DPLL_SEL);
  2699. switch (pipe) {
  2700. default:
  2701. case 0:
  2702. temp |= TRANSA_DPLL_ENABLE;
  2703. sel = TRANSA_DPLLB_SEL;
  2704. break;
  2705. case 1:
  2706. temp |= TRANSB_DPLL_ENABLE;
  2707. sel = TRANSB_DPLLB_SEL;
  2708. break;
  2709. case 2:
  2710. temp |= TRANSC_DPLL_ENABLE;
  2711. sel = TRANSC_DPLLB_SEL;
  2712. break;
  2713. }
  2714. if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
  2715. temp |= sel;
  2716. else
  2717. temp &= ~sel;
  2718. I915_WRITE(PCH_DPLL_SEL, temp);
  2719. }
  2720. /* set transcoder timing, panel must allow it */
  2721. assert_panel_unlocked(dev_priv, pipe);
  2722. I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
  2723. I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
  2724. I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
  2725. I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
  2726. I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
  2727. I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
  2728. I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
  2729. intel_fdi_normal_train(crtc);
  2730. /* For PCH DP, enable TRANS_DP_CTL */
  2731. if (HAS_PCH_CPT(dev) &&
  2732. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  2733. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2734. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  2735. reg = TRANS_DP_CTL(pipe);
  2736. temp = I915_READ(reg);
  2737. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  2738. TRANS_DP_SYNC_MASK |
  2739. TRANS_DP_BPC_MASK);
  2740. temp |= (TRANS_DP_OUTPUT_ENABLE |
  2741. TRANS_DP_ENH_FRAMING);
  2742. temp |= bpc << 9; /* same format but at 11:9 */
  2743. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  2744. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  2745. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  2746. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  2747. switch (intel_trans_dp_port_sel(crtc)) {
  2748. case PCH_DP_B:
  2749. temp |= TRANS_DP_PORT_SEL_B;
  2750. break;
  2751. case PCH_DP_C:
  2752. temp |= TRANS_DP_PORT_SEL_C;
  2753. break;
  2754. case PCH_DP_D:
  2755. temp |= TRANS_DP_PORT_SEL_D;
  2756. break;
  2757. default:
  2758. BUG();
  2759. }
  2760. I915_WRITE(reg, temp);
  2761. }
  2762. ironlake_enable_pch_transcoder(dev_priv, pipe);
  2763. }
  2764. static void lpt_pch_enable(struct drm_crtc *crtc)
  2765. {
  2766. struct drm_device *dev = crtc->dev;
  2767. struct drm_i915_private *dev_priv = dev->dev_private;
  2768. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2769. enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
  2770. assert_transcoder_disabled(dev_priv, TRANSCODER_A);
  2771. lpt_program_iclkip(crtc);
  2772. /* Set transcoder timing. */
  2773. I915_WRITE(_TRANS_HTOTAL_A, I915_READ(HTOTAL(cpu_transcoder)));
  2774. I915_WRITE(_TRANS_HBLANK_A, I915_READ(HBLANK(cpu_transcoder)));
  2775. I915_WRITE(_TRANS_HSYNC_A, I915_READ(HSYNC(cpu_transcoder)));
  2776. I915_WRITE(_TRANS_VTOTAL_A, I915_READ(VTOTAL(cpu_transcoder)));
  2777. I915_WRITE(_TRANS_VBLANK_A, I915_READ(VBLANK(cpu_transcoder)));
  2778. I915_WRITE(_TRANS_VSYNC_A, I915_READ(VSYNC(cpu_transcoder)));
  2779. I915_WRITE(_TRANS_VSYNCSHIFT_A, I915_READ(VSYNCSHIFT(cpu_transcoder)));
  2780. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  2781. }
  2782. static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
  2783. {
  2784. struct intel_pch_pll *pll = intel_crtc->pch_pll;
  2785. if (pll == NULL)
  2786. return;
  2787. if (pll->refcount == 0) {
  2788. WARN(1, "bad PCH PLL refcount\n");
  2789. return;
  2790. }
  2791. --pll->refcount;
  2792. intel_crtc->pch_pll = NULL;
  2793. }
  2794. static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
  2795. {
  2796. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  2797. struct intel_pch_pll *pll;
  2798. int i;
  2799. pll = intel_crtc->pch_pll;
  2800. if (pll) {
  2801. DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
  2802. intel_crtc->base.base.id, pll->pll_reg);
  2803. goto prepare;
  2804. }
  2805. if (HAS_PCH_IBX(dev_priv->dev)) {
  2806. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  2807. i = intel_crtc->pipe;
  2808. pll = &dev_priv->pch_plls[i];
  2809. DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
  2810. intel_crtc->base.base.id, pll->pll_reg);
  2811. goto found;
  2812. }
  2813. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  2814. pll = &dev_priv->pch_plls[i];
  2815. /* Only want to check enabled timings first */
  2816. if (pll->refcount == 0)
  2817. continue;
  2818. if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
  2819. fp == I915_READ(pll->fp0_reg)) {
  2820. DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
  2821. intel_crtc->base.base.id,
  2822. pll->pll_reg, pll->refcount, pll->active);
  2823. goto found;
  2824. }
  2825. }
  2826. /* Ok no matching timings, maybe there's a free one? */
  2827. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  2828. pll = &dev_priv->pch_plls[i];
  2829. if (pll->refcount == 0) {
  2830. DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
  2831. intel_crtc->base.base.id, pll->pll_reg);
  2832. goto found;
  2833. }
  2834. }
  2835. return NULL;
  2836. found:
  2837. intel_crtc->pch_pll = pll;
  2838. pll->refcount++;
  2839. DRM_DEBUG_DRIVER("using pll %d for pipe %d\n", i, intel_crtc->pipe);
  2840. prepare: /* separate function? */
  2841. DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
  2842. /* Wait for the clocks to stabilize before rewriting the regs */
  2843. I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
  2844. POSTING_READ(pll->pll_reg);
  2845. udelay(150);
  2846. I915_WRITE(pll->fp0_reg, fp);
  2847. I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
  2848. pll->on = false;
  2849. return pll;
  2850. }
  2851. void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
  2852. {
  2853. struct drm_i915_private *dev_priv = dev->dev_private;
  2854. int dslreg = PIPEDSL(pipe);
  2855. u32 temp;
  2856. temp = I915_READ(dslreg);
  2857. udelay(500);
  2858. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  2859. if (wait_for(I915_READ(dslreg) != temp, 5))
  2860. DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
  2861. }
  2862. }
  2863. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  2864. {
  2865. struct drm_device *dev = crtc->dev;
  2866. struct drm_i915_private *dev_priv = dev->dev_private;
  2867. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2868. struct intel_encoder *encoder;
  2869. int pipe = intel_crtc->pipe;
  2870. int plane = intel_crtc->plane;
  2871. u32 temp;
  2872. WARN_ON(!crtc->enabled);
  2873. if (intel_crtc->active)
  2874. return;
  2875. intel_crtc->active = true;
  2876. intel_update_watermarks(dev);
  2877. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  2878. temp = I915_READ(PCH_LVDS);
  2879. if ((temp & LVDS_PORT_EN) == 0)
  2880. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  2881. }
  2882. if (intel_crtc->config.has_pch_encoder) {
  2883. /* Note: FDI PLL enabling _must_ be done before we enable the
  2884. * cpu pipes, hence this is separate from all the other fdi/pch
  2885. * enabling. */
  2886. ironlake_fdi_pll_enable(intel_crtc);
  2887. } else {
  2888. assert_fdi_tx_disabled(dev_priv, pipe);
  2889. assert_fdi_rx_disabled(dev_priv, pipe);
  2890. }
  2891. for_each_encoder_on_crtc(dev, crtc, encoder)
  2892. if (encoder->pre_enable)
  2893. encoder->pre_enable(encoder);
  2894. /* Enable panel fitting for LVDS */
  2895. if (dev_priv->pch_pf_size &&
  2896. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2897. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2898. /* Force use of hard-coded filter coefficients
  2899. * as some pre-programmed values are broken,
  2900. * e.g. x201.
  2901. */
  2902. if (IS_IVYBRIDGE(dev))
  2903. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  2904. PF_PIPE_SEL_IVB(pipe));
  2905. else
  2906. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  2907. I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
  2908. I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
  2909. }
  2910. /*
  2911. * On ILK+ LUT must be loaded before the pipe is running but with
  2912. * clocks enabled
  2913. */
  2914. intel_crtc_load_lut(crtc);
  2915. intel_enable_pipe(dev_priv, pipe,
  2916. intel_crtc->config.has_pch_encoder);
  2917. intel_enable_plane(dev_priv, plane, pipe);
  2918. if (intel_crtc->config.has_pch_encoder)
  2919. ironlake_pch_enable(crtc);
  2920. mutex_lock(&dev->struct_mutex);
  2921. intel_update_fbc(dev);
  2922. mutex_unlock(&dev->struct_mutex);
  2923. intel_crtc_update_cursor(crtc, true);
  2924. for_each_encoder_on_crtc(dev, crtc, encoder)
  2925. encoder->enable(encoder);
  2926. if (HAS_PCH_CPT(dev))
  2927. intel_cpt_verify_modeset(dev, intel_crtc->pipe);
  2928. /*
  2929. * There seems to be a race in PCH platform hw (at least on some
  2930. * outputs) where an enabled pipe still completes any pageflip right
  2931. * away (as if the pipe is off) instead of waiting for vblank. As soon
  2932. * as the first vblank happend, everything works as expected. Hence just
  2933. * wait for one vblank before returning to avoid strange things
  2934. * happening.
  2935. */
  2936. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2937. }
  2938. static void haswell_crtc_enable(struct drm_crtc *crtc)
  2939. {
  2940. struct drm_device *dev = crtc->dev;
  2941. struct drm_i915_private *dev_priv = dev->dev_private;
  2942. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2943. struct intel_encoder *encoder;
  2944. int pipe = intel_crtc->pipe;
  2945. int plane = intel_crtc->plane;
  2946. WARN_ON(!crtc->enabled);
  2947. if (intel_crtc->active)
  2948. return;
  2949. intel_crtc->active = true;
  2950. intel_update_watermarks(dev);
  2951. if (intel_crtc->config.has_pch_encoder)
  2952. dev_priv->display.fdi_link_train(crtc);
  2953. for_each_encoder_on_crtc(dev, crtc, encoder)
  2954. if (encoder->pre_enable)
  2955. encoder->pre_enable(encoder);
  2956. intel_ddi_enable_pipe_clock(intel_crtc);
  2957. /* Enable panel fitting for eDP */
  2958. if (dev_priv->pch_pf_size &&
  2959. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
  2960. /* Force use of hard-coded filter coefficients
  2961. * as some pre-programmed values are broken,
  2962. * e.g. x201.
  2963. */
  2964. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  2965. PF_PIPE_SEL_IVB(pipe));
  2966. I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
  2967. I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
  2968. }
  2969. /*
  2970. * On ILK+ LUT must be loaded before the pipe is running but with
  2971. * clocks enabled
  2972. */
  2973. intel_crtc_load_lut(crtc);
  2974. intel_ddi_set_pipe_settings(crtc);
  2975. intel_ddi_enable_transcoder_func(crtc);
  2976. intel_enable_pipe(dev_priv, pipe,
  2977. intel_crtc->config.has_pch_encoder);
  2978. intel_enable_plane(dev_priv, plane, pipe);
  2979. if (intel_crtc->config.has_pch_encoder)
  2980. lpt_pch_enable(crtc);
  2981. mutex_lock(&dev->struct_mutex);
  2982. intel_update_fbc(dev);
  2983. mutex_unlock(&dev->struct_mutex);
  2984. intel_crtc_update_cursor(crtc, true);
  2985. for_each_encoder_on_crtc(dev, crtc, encoder)
  2986. encoder->enable(encoder);
  2987. /*
  2988. * There seems to be a race in PCH platform hw (at least on some
  2989. * outputs) where an enabled pipe still completes any pageflip right
  2990. * away (as if the pipe is off) instead of waiting for vblank. As soon
  2991. * as the first vblank happend, everything works as expected. Hence just
  2992. * wait for one vblank before returning to avoid strange things
  2993. * happening.
  2994. */
  2995. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2996. }
  2997. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  2998. {
  2999. struct drm_device *dev = crtc->dev;
  3000. struct drm_i915_private *dev_priv = dev->dev_private;
  3001. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3002. struct intel_encoder *encoder;
  3003. int pipe = intel_crtc->pipe;
  3004. int plane = intel_crtc->plane;
  3005. u32 reg, temp;
  3006. if (!intel_crtc->active)
  3007. return;
  3008. for_each_encoder_on_crtc(dev, crtc, encoder)
  3009. encoder->disable(encoder);
  3010. intel_crtc_wait_for_pending_flips(crtc);
  3011. drm_vblank_off(dev, pipe);
  3012. intel_crtc_update_cursor(crtc, false);
  3013. intel_disable_plane(dev_priv, plane, pipe);
  3014. if (dev_priv->cfb_plane == plane)
  3015. intel_disable_fbc(dev);
  3016. intel_disable_pipe(dev_priv, pipe);
  3017. /* Disable PF */
  3018. I915_WRITE(PF_CTL(pipe), 0);
  3019. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3020. for_each_encoder_on_crtc(dev, crtc, encoder)
  3021. if (encoder->post_disable)
  3022. encoder->post_disable(encoder);
  3023. ironlake_fdi_disable(crtc);
  3024. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3025. if (HAS_PCH_CPT(dev)) {
  3026. /* disable TRANS_DP_CTL */
  3027. reg = TRANS_DP_CTL(pipe);
  3028. temp = I915_READ(reg);
  3029. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  3030. temp |= TRANS_DP_PORT_SEL_NONE;
  3031. I915_WRITE(reg, temp);
  3032. /* disable DPLL_SEL */
  3033. temp = I915_READ(PCH_DPLL_SEL);
  3034. switch (pipe) {
  3035. case 0:
  3036. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  3037. break;
  3038. case 1:
  3039. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  3040. break;
  3041. case 2:
  3042. /* C shares PLL A or B */
  3043. temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
  3044. break;
  3045. default:
  3046. BUG(); /* wtf */
  3047. }
  3048. I915_WRITE(PCH_DPLL_SEL, temp);
  3049. }
  3050. /* disable PCH DPLL */
  3051. intel_disable_pch_pll(intel_crtc);
  3052. ironlake_fdi_pll_disable(intel_crtc);
  3053. intel_crtc->active = false;
  3054. intel_update_watermarks(dev);
  3055. mutex_lock(&dev->struct_mutex);
  3056. intel_update_fbc(dev);
  3057. mutex_unlock(&dev->struct_mutex);
  3058. }
  3059. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3060. {
  3061. struct drm_device *dev = crtc->dev;
  3062. struct drm_i915_private *dev_priv = dev->dev_private;
  3063. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3064. struct intel_encoder *encoder;
  3065. int pipe = intel_crtc->pipe;
  3066. int plane = intel_crtc->plane;
  3067. enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
  3068. bool is_pch_port;
  3069. if (!intel_crtc->active)
  3070. return;
  3071. is_pch_port = haswell_crtc_driving_pch(crtc);
  3072. for_each_encoder_on_crtc(dev, crtc, encoder)
  3073. encoder->disable(encoder);
  3074. intel_crtc_wait_for_pending_flips(crtc);
  3075. drm_vblank_off(dev, pipe);
  3076. intel_crtc_update_cursor(crtc, false);
  3077. intel_disable_plane(dev_priv, plane, pipe);
  3078. if (dev_priv->cfb_plane == plane)
  3079. intel_disable_fbc(dev);
  3080. intel_disable_pipe(dev_priv, pipe);
  3081. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3082. /* Disable PF */
  3083. I915_WRITE(PF_CTL(pipe), 0);
  3084. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3085. intel_ddi_disable_pipe_clock(intel_crtc);
  3086. for_each_encoder_on_crtc(dev, crtc, encoder)
  3087. if (encoder->post_disable)
  3088. encoder->post_disable(encoder);
  3089. if (is_pch_port) {
  3090. lpt_disable_pch_transcoder(dev_priv);
  3091. intel_ddi_fdi_disable(crtc);
  3092. }
  3093. intel_crtc->active = false;
  3094. intel_update_watermarks(dev);
  3095. mutex_lock(&dev->struct_mutex);
  3096. intel_update_fbc(dev);
  3097. mutex_unlock(&dev->struct_mutex);
  3098. }
  3099. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3100. {
  3101. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3102. intel_put_pch_pll(intel_crtc);
  3103. }
  3104. static void haswell_crtc_off(struct drm_crtc *crtc)
  3105. {
  3106. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3107. /* Stop saying we're using TRANSCODER_EDP because some other CRTC might
  3108. * start using it. */
  3109. intel_crtc->cpu_transcoder = (enum transcoder) intel_crtc->pipe;
  3110. intel_ddi_put_crtc_pll(crtc);
  3111. }
  3112. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3113. {
  3114. if (!enable && intel_crtc->overlay) {
  3115. struct drm_device *dev = intel_crtc->base.dev;
  3116. struct drm_i915_private *dev_priv = dev->dev_private;
  3117. mutex_lock(&dev->struct_mutex);
  3118. dev_priv->mm.interruptible = false;
  3119. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3120. dev_priv->mm.interruptible = true;
  3121. mutex_unlock(&dev->struct_mutex);
  3122. }
  3123. /* Let userspace switch the overlay on again. In most cases userspace
  3124. * has to recompute where to put it anyway.
  3125. */
  3126. }
  3127. /**
  3128. * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
  3129. * cursor plane briefly if not already running after enabling the display
  3130. * plane.
  3131. * This workaround avoids occasional blank screens when self refresh is
  3132. * enabled.
  3133. */
  3134. static void
  3135. g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
  3136. {
  3137. u32 cntl = I915_READ(CURCNTR(pipe));
  3138. if ((cntl & CURSOR_MODE) == 0) {
  3139. u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
  3140. I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
  3141. I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
  3142. intel_wait_for_vblank(dev_priv->dev, pipe);
  3143. I915_WRITE(CURCNTR(pipe), cntl);
  3144. I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
  3145. I915_WRITE(FW_BLC_SELF, fw_bcl_self);
  3146. }
  3147. }
  3148. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  3149. {
  3150. struct drm_device *dev = crtc->dev;
  3151. struct drm_i915_private *dev_priv = dev->dev_private;
  3152. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3153. struct intel_encoder *encoder;
  3154. int pipe = intel_crtc->pipe;
  3155. int plane = intel_crtc->plane;
  3156. WARN_ON(!crtc->enabled);
  3157. if (intel_crtc->active)
  3158. return;
  3159. intel_crtc->active = true;
  3160. intel_update_watermarks(dev);
  3161. intel_enable_pll(dev_priv, pipe);
  3162. for_each_encoder_on_crtc(dev, crtc, encoder)
  3163. if (encoder->pre_enable)
  3164. encoder->pre_enable(encoder);
  3165. intel_enable_pipe(dev_priv, pipe, false);
  3166. intel_enable_plane(dev_priv, plane, pipe);
  3167. if (IS_G4X(dev))
  3168. g4x_fixup_plane(dev_priv, pipe);
  3169. intel_crtc_load_lut(crtc);
  3170. intel_update_fbc(dev);
  3171. /* Give the overlay scaler a chance to enable if it's on this pipe */
  3172. intel_crtc_dpms_overlay(intel_crtc, true);
  3173. intel_crtc_update_cursor(crtc, true);
  3174. for_each_encoder_on_crtc(dev, crtc, encoder)
  3175. encoder->enable(encoder);
  3176. }
  3177. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  3178. {
  3179. struct drm_device *dev = crtc->dev;
  3180. struct drm_i915_private *dev_priv = dev->dev_private;
  3181. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3182. struct intel_encoder *encoder;
  3183. int pipe = intel_crtc->pipe;
  3184. int plane = intel_crtc->plane;
  3185. u32 pctl;
  3186. if (!intel_crtc->active)
  3187. return;
  3188. for_each_encoder_on_crtc(dev, crtc, encoder)
  3189. encoder->disable(encoder);
  3190. /* Give the overlay scaler a chance to disable if it's on this pipe */
  3191. intel_crtc_wait_for_pending_flips(crtc);
  3192. drm_vblank_off(dev, pipe);
  3193. intel_crtc_dpms_overlay(intel_crtc, false);
  3194. intel_crtc_update_cursor(crtc, false);
  3195. if (dev_priv->cfb_plane == plane)
  3196. intel_disable_fbc(dev);
  3197. intel_disable_plane(dev_priv, plane, pipe);
  3198. intel_disable_pipe(dev_priv, pipe);
  3199. /* Disable pannel fitter if it is on this pipe. */
  3200. pctl = I915_READ(PFIT_CONTROL);
  3201. if ((pctl & PFIT_ENABLE) &&
  3202. ((pctl & PFIT_PIPE_MASK) >> PFIT_PIPE_SHIFT) == pipe)
  3203. I915_WRITE(PFIT_CONTROL, 0);
  3204. intel_disable_pll(dev_priv, pipe);
  3205. intel_crtc->active = false;
  3206. intel_update_fbc(dev);
  3207. intel_update_watermarks(dev);
  3208. }
  3209. static void i9xx_crtc_off(struct drm_crtc *crtc)
  3210. {
  3211. }
  3212. static void intel_crtc_update_sarea(struct drm_crtc *crtc,
  3213. bool enabled)
  3214. {
  3215. struct drm_device *dev = crtc->dev;
  3216. struct drm_i915_master_private *master_priv;
  3217. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3218. int pipe = intel_crtc->pipe;
  3219. if (!dev->primary->master)
  3220. return;
  3221. master_priv = dev->primary->master->driver_priv;
  3222. if (!master_priv->sarea_priv)
  3223. return;
  3224. switch (pipe) {
  3225. case 0:
  3226. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  3227. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  3228. break;
  3229. case 1:
  3230. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  3231. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  3232. break;
  3233. default:
  3234. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  3235. break;
  3236. }
  3237. }
  3238. /**
  3239. * Sets the power management mode of the pipe and plane.
  3240. */
  3241. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  3242. {
  3243. struct drm_device *dev = crtc->dev;
  3244. struct drm_i915_private *dev_priv = dev->dev_private;
  3245. struct intel_encoder *intel_encoder;
  3246. bool enable = false;
  3247. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3248. enable |= intel_encoder->connectors_active;
  3249. if (enable)
  3250. dev_priv->display.crtc_enable(crtc);
  3251. else
  3252. dev_priv->display.crtc_disable(crtc);
  3253. intel_crtc_update_sarea(crtc, enable);
  3254. }
  3255. static void intel_crtc_disable(struct drm_crtc *crtc)
  3256. {
  3257. struct drm_device *dev = crtc->dev;
  3258. struct drm_connector *connector;
  3259. struct drm_i915_private *dev_priv = dev->dev_private;
  3260. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3261. /* crtc should still be enabled when we disable it. */
  3262. WARN_ON(!crtc->enabled);
  3263. intel_crtc->eld_vld = false;
  3264. dev_priv->display.crtc_disable(crtc);
  3265. intel_crtc_update_sarea(crtc, false);
  3266. dev_priv->display.off(crtc);
  3267. assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
  3268. assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
  3269. if (crtc->fb) {
  3270. mutex_lock(&dev->struct_mutex);
  3271. intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
  3272. mutex_unlock(&dev->struct_mutex);
  3273. crtc->fb = NULL;
  3274. }
  3275. /* Update computed state. */
  3276. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  3277. if (!connector->encoder || !connector->encoder->crtc)
  3278. continue;
  3279. if (connector->encoder->crtc != crtc)
  3280. continue;
  3281. connector->dpms = DRM_MODE_DPMS_OFF;
  3282. to_intel_encoder(connector->encoder)->connectors_active = false;
  3283. }
  3284. }
  3285. void intel_modeset_disable(struct drm_device *dev)
  3286. {
  3287. struct drm_crtc *crtc;
  3288. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3289. if (crtc->enabled)
  3290. intel_crtc_disable(crtc);
  3291. }
  3292. }
  3293. void intel_encoder_destroy(struct drm_encoder *encoder)
  3294. {
  3295. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  3296. drm_encoder_cleanup(encoder);
  3297. kfree(intel_encoder);
  3298. }
  3299. /* Simple dpms helper for encodres with just one connector, no cloning and only
  3300. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  3301. * state of the entire output pipe. */
  3302. void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  3303. {
  3304. if (mode == DRM_MODE_DPMS_ON) {
  3305. encoder->connectors_active = true;
  3306. intel_crtc_update_dpms(encoder->base.crtc);
  3307. } else {
  3308. encoder->connectors_active = false;
  3309. intel_crtc_update_dpms(encoder->base.crtc);
  3310. }
  3311. }
  3312. /* Cross check the actual hw state with our own modeset state tracking (and it's
  3313. * internal consistency). */
  3314. static void intel_connector_check_state(struct intel_connector *connector)
  3315. {
  3316. if (connector->get_hw_state(connector)) {
  3317. struct intel_encoder *encoder = connector->encoder;
  3318. struct drm_crtc *crtc;
  3319. bool encoder_enabled;
  3320. enum pipe pipe;
  3321. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  3322. connector->base.base.id,
  3323. drm_get_connector_name(&connector->base));
  3324. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  3325. "wrong connector dpms state\n");
  3326. WARN(connector->base.encoder != &encoder->base,
  3327. "active connector not linked to encoder\n");
  3328. WARN(!encoder->connectors_active,
  3329. "encoder->connectors_active not set\n");
  3330. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  3331. WARN(!encoder_enabled, "encoder not enabled\n");
  3332. if (WARN_ON(!encoder->base.crtc))
  3333. return;
  3334. crtc = encoder->base.crtc;
  3335. WARN(!crtc->enabled, "crtc not enabled\n");
  3336. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  3337. WARN(pipe != to_intel_crtc(crtc)->pipe,
  3338. "encoder active on the wrong pipe\n");
  3339. }
  3340. }
  3341. /* Even simpler default implementation, if there's really no special case to
  3342. * consider. */
  3343. void intel_connector_dpms(struct drm_connector *connector, int mode)
  3344. {
  3345. struct intel_encoder *encoder = intel_attached_encoder(connector);
  3346. /* All the simple cases only support two dpms states. */
  3347. if (mode != DRM_MODE_DPMS_ON)
  3348. mode = DRM_MODE_DPMS_OFF;
  3349. if (mode == connector->dpms)
  3350. return;
  3351. connector->dpms = mode;
  3352. /* Only need to change hw state when actually enabled */
  3353. if (encoder->base.crtc)
  3354. intel_encoder_dpms(encoder, mode);
  3355. else
  3356. WARN_ON(encoder->connectors_active != false);
  3357. intel_modeset_check_state(connector->dev);
  3358. }
  3359. /* Simple connector->get_hw_state implementation for encoders that support only
  3360. * one connector and no cloning and hence the encoder state determines the state
  3361. * of the connector. */
  3362. bool intel_connector_get_hw_state(struct intel_connector *connector)
  3363. {
  3364. enum pipe pipe = 0;
  3365. struct intel_encoder *encoder = connector->encoder;
  3366. return encoder->get_hw_state(encoder, &pipe);
  3367. }
  3368. static bool intel_crtc_compute_config(struct drm_crtc *crtc,
  3369. struct intel_crtc_config *pipe_config)
  3370. {
  3371. struct drm_device *dev = crtc->dev;
  3372. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  3373. if (HAS_PCH_SPLIT(dev)) {
  3374. /* FDI link clock is fixed at 2.7G */
  3375. if (pipe_config->requested_mode.clock * 3
  3376. > IRONLAKE_FDI_FREQ * 4)
  3377. return false;
  3378. }
  3379. /* All interlaced capable intel hw wants timings in frames. Note though
  3380. * that intel_lvds_mode_fixup does some funny tricks with the crtc
  3381. * timings, so we need to be careful not to clobber these.*/
  3382. if (!pipe_config->timings_set)
  3383. drm_mode_set_crtcinfo(adjusted_mode, 0);
  3384. /* WaPruneModeWithIncorrectHsyncOffset: Cantiga+ cannot handle modes
  3385. * with a hsync front porch of 0.
  3386. */
  3387. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  3388. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  3389. return false;
  3390. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10) {
  3391. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  3392. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8) {
  3393. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  3394. * for lvds. */
  3395. pipe_config->pipe_bpp = 8*3;
  3396. }
  3397. return true;
  3398. }
  3399. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  3400. {
  3401. return 400000; /* FIXME */
  3402. }
  3403. static int i945_get_display_clock_speed(struct drm_device *dev)
  3404. {
  3405. return 400000;
  3406. }
  3407. static int i915_get_display_clock_speed(struct drm_device *dev)
  3408. {
  3409. return 333000;
  3410. }
  3411. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  3412. {
  3413. return 200000;
  3414. }
  3415. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  3416. {
  3417. u16 gcfgc = 0;
  3418. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  3419. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  3420. return 133000;
  3421. else {
  3422. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  3423. case GC_DISPLAY_CLOCK_333_MHZ:
  3424. return 333000;
  3425. default:
  3426. case GC_DISPLAY_CLOCK_190_200_MHZ:
  3427. return 190000;
  3428. }
  3429. }
  3430. }
  3431. static int i865_get_display_clock_speed(struct drm_device *dev)
  3432. {
  3433. return 266000;
  3434. }
  3435. static int i855_get_display_clock_speed(struct drm_device *dev)
  3436. {
  3437. u16 hpllcc = 0;
  3438. /* Assume that the hardware is in the high speed state. This
  3439. * should be the default.
  3440. */
  3441. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  3442. case GC_CLOCK_133_200:
  3443. case GC_CLOCK_100_200:
  3444. return 200000;
  3445. case GC_CLOCK_166_250:
  3446. return 250000;
  3447. case GC_CLOCK_100_133:
  3448. return 133000;
  3449. }
  3450. /* Shouldn't happen */
  3451. return 0;
  3452. }
  3453. static int i830_get_display_clock_speed(struct drm_device *dev)
  3454. {
  3455. return 133000;
  3456. }
  3457. static void
  3458. intel_reduce_ratio(uint32_t *num, uint32_t *den)
  3459. {
  3460. while (*num > 0xffffff || *den > 0xffffff) {
  3461. *num >>= 1;
  3462. *den >>= 1;
  3463. }
  3464. }
  3465. void
  3466. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  3467. int pixel_clock, int link_clock,
  3468. struct intel_link_m_n *m_n)
  3469. {
  3470. m_n->tu = 64;
  3471. m_n->gmch_m = bits_per_pixel * pixel_clock;
  3472. m_n->gmch_n = link_clock * nlanes * 8;
  3473. intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  3474. m_n->link_m = pixel_clock;
  3475. m_n->link_n = link_clock;
  3476. intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
  3477. }
  3478. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  3479. {
  3480. if (i915_panel_use_ssc >= 0)
  3481. return i915_panel_use_ssc != 0;
  3482. return dev_priv->lvds_use_ssc
  3483. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  3484. }
  3485. static int vlv_get_refclk(struct drm_crtc *crtc)
  3486. {
  3487. struct drm_device *dev = crtc->dev;
  3488. struct drm_i915_private *dev_priv = dev->dev_private;
  3489. int refclk = 27000; /* for DP & HDMI */
  3490. return 100000; /* only one validated so far */
  3491. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  3492. refclk = 96000;
  3493. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  3494. if (intel_panel_use_ssc(dev_priv))
  3495. refclk = 100000;
  3496. else
  3497. refclk = 96000;
  3498. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
  3499. refclk = 100000;
  3500. }
  3501. return refclk;
  3502. }
  3503. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  3504. {
  3505. struct drm_device *dev = crtc->dev;
  3506. struct drm_i915_private *dev_priv = dev->dev_private;
  3507. int refclk;
  3508. if (IS_VALLEYVIEW(dev)) {
  3509. refclk = vlv_get_refclk(crtc);
  3510. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3511. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  3512. refclk = dev_priv->lvds_ssc_freq * 1000;
  3513. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3514. refclk / 1000);
  3515. } else if (!IS_GEN2(dev)) {
  3516. refclk = 96000;
  3517. } else {
  3518. refclk = 48000;
  3519. }
  3520. return refclk;
  3521. }
  3522. static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
  3523. intel_clock_t *clock)
  3524. {
  3525. /* SDVO TV has fixed PLL values depend on its clock range,
  3526. this mirrors vbios setting. */
  3527. if (adjusted_mode->clock >= 100000
  3528. && adjusted_mode->clock < 140500) {
  3529. clock->p1 = 2;
  3530. clock->p2 = 10;
  3531. clock->n = 3;
  3532. clock->m1 = 16;
  3533. clock->m2 = 8;
  3534. } else if (adjusted_mode->clock >= 140500
  3535. && adjusted_mode->clock <= 200000) {
  3536. clock->p1 = 1;
  3537. clock->p2 = 10;
  3538. clock->n = 6;
  3539. clock->m1 = 12;
  3540. clock->m2 = 8;
  3541. }
  3542. }
  3543. static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
  3544. intel_clock_t *clock,
  3545. intel_clock_t *reduced_clock)
  3546. {
  3547. struct drm_device *dev = crtc->dev;
  3548. struct drm_i915_private *dev_priv = dev->dev_private;
  3549. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3550. int pipe = intel_crtc->pipe;
  3551. u32 fp, fp2 = 0;
  3552. if (IS_PINEVIEW(dev)) {
  3553. fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
  3554. if (reduced_clock)
  3555. fp2 = (1 << reduced_clock->n) << 16 |
  3556. reduced_clock->m1 << 8 | reduced_clock->m2;
  3557. } else {
  3558. fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
  3559. if (reduced_clock)
  3560. fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
  3561. reduced_clock->m2;
  3562. }
  3563. I915_WRITE(FP0(pipe), fp);
  3564. intel_crtc->lowfreq_avail = false;
  3565. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3566. reduced_clock && i915_powersave) {
  3567. I915_WRITE(FP1(pipe), fp2);
  3568. intel_crtc->lowfreq_avail = true;
  3569. } else {
  3570. I915_WRITE(FP1(pipe), fp);
  3571. }
  3572. }
  3573. static void vlv_update_pll(struct drm_crtc *crtc,
  3574. intel_clock_t *clock, intel_clock_t *reduced_clock,
  3575. int num_connectors)
  3576. {
  3577. struct drm_device *dev = crtc->dev;
  3578. struct drm_i915_private *dev_priv = dev->dev_private;
  3579. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3580. struct drm_display_mode *adjusted_mode =
  3581. &intel_crtc->config.adjusted_mode;
  3582. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  3583. int pipe = intel_crtc->pipe;
  3584. u32 dpll, mdiv, pdiv;
  3585. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  3586. bool is_sdvo;
  3587. u32 temp;
  3588. mutex_lock(&dev_priv->dpio_lock);
  3589. is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
  3590. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
  3591. dpll = DPLL_VGA_MODE_DIS;
  3592. dpll |= DPLL_EXT_BUFFER_ENABLE_VLV;
  3593. dpll |= DPLL_REFA_CLK_ENABLE_VLV;
  3594. dpll |= DPLL_INTEGRATED_CLOCK_VLV;
  3595. I915_WRITE(DPLL(pipe), dpll);
  3596. POSTING_READ(DPLL(pipe));
  3597. bestn = clock->n;
  3598. bestm1 = clock->m1;
  3599. bestm2 = clock->m2;
  3600. bestp1 = clock->p1;
  3601. bestp2 = clock->p2;
  3602. /*
  3603. * In Valleyview PLL and program lane counter registers are exposed
  3604. * through DPIO interface
  3605. */
  3606. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  3607. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  3608. mdiv |= ((bestn << DPIO_N_SHIFT));
  3609. mdiv |= (1 << DPIO_POST_DIV_SHIFT);
  3610. mdiv |= (1 << DPIO_K_SHIFT);
  3611. mdiv |= DPIO_ENABLE_CALIBRATION;
  3612. intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
  3613. intel_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), 0x01000000);
  3614. pdiv = (1 << DPIO_REFSEL_OVERRIDE) | (5 << DPIO_PLL_MODESEL_SHIFT) |
  3615. (3 << DPIO_BIAS_CURRENT_CTL_SHIFT) | (1<<20) |
  3616. (7 << DPIO_PLL_REFCLK_SEL_SHIFT) | (8 << DPIO_DRIVER_CTL_SHIFT) |
  3617. (5 << DPIO_CLK_BIAS_CTL_SHIFT);
  3618. intel_dpio_write(dev_priv, DPIO_REFSFR(pipe), pdiv);
  3619. intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe), 0x005f003b);
  3620. dpll |= DPLL_VCO_ENABLE;
  3621. I915_WRITE(DPLL(pipe), dpll);
  3622. POSTING_READ(DPLL(pipe));
  3623. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  3624. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  3625. intel_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x620);
  3626. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  3627. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3628. I915_WRITE(DPLL(pipe), dpll);
  3629. /* Wait for the clocks to stabilize. */
  3630. POSTING_READ(DPLL(pipe));
  3631. udelay(150);
  3632. temp = 0;
  3633. if (is_sdvo) {
  3634. temp = 0;
  3635. if (intel_crtc->config.pixel_multiplier > 1) {
  3636. temp = (intel_crtc->config.pixel_multiplier - 1)
  3637. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3638. }
  3639. }
  3640. I915_WRITE(DPLL_MD(pipe), temp);
  3641. POSTING_READ(DPLL_MD(pipe));
  3642. /* Now program lane control registers */
  3643. if(intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)
  3644. || intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  3645. {
  3646. temp = 0x1000C4;
  3647. if(pipe == 1)
  3648. temp |= (1 << 21);
  3649. intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL1, temp);
  3650. }
  3651. if(intel_pipe_has_type(crtc,INTEL_OUTPUT_EDP))
  3652. {
  3653. temp = 0x1000C4;
  3654. if(pipe == 1)
  3655. temp |= (1 << 21);
  3656. intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL2, temp);
  3657. }
  3658. mutex_unlock(&dev_priv->dpio_lock);
  3659. }
  3660. static void i9xx_update_pll(struct drm_crtc *crtc,
  3661. intel_clock_t *clock, intel_clock_t *reduced_clock,
  3662. int num_connectors)
  3663. {
  3664. struct drm_device *dev = crtc->dev;
  3665. struct drm_i915_private *dev_priv = dev->dev_private;
  3666. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3667. struct drm_display_mode *adjusted_mode =
  3668. &intel_crtc->config.adjusted_mode;
  3669. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  3670. struct intel_encoder *encoder;
  3671. int pipe = intel_crtc->pipe;
  3672. u32 dpll;
  3673. bool is_sdvo;
  3674. i9xx_update_pll_dividers(crtc, clock, reduced_clock);
  3675. is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
  3676. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
  3677. dpll = DPLL_VGA_MODE_DIS;
  3678. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  3679. dpll |= DPLLB_MODE_LVDS;
  3680. else
  3681. dpll |= DPLLB_MODE_DAC_SERIAL;
  3682. if (is_sdvo) {
  3683. if ((intel_crtc->config.pixel_multiplier > 1) &&
  3684. (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))) {
  3685. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  3686. << SDVO_MULTIPLIER_SHIFT_HIRES;
  3687. }
  3688. dpll |= DPLL_DVO_HIGH_SPEED;
  3689. }
  3690. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  3691. dpll |= DPLL_DVO_HIGH_SPEED;
  3692. /* compute bitmask from p1 value */
  3693. if (IS_PINEVIEW(dev))
  3694. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  3695. else {
  3696. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3697. if (IS_G4X(dev) && reduced_clock)
  3698. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3699. }
  3700. switch (clock->p2) {
  3701. case 5:
  3702. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3703. break;
  3704. case 7:
  3705. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3706. break;
  3707. case 10:
  3708. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3709. break;
  3710. case 14:
  3711. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3712. break;
  3713. }
  3714. if (INTEL_INFO(dev)->gen >= 4)
  3715. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  3716. if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  3717. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3718. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  3719. /* XXX: just matching BIOS for now */
  3720. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3721. dpll |= 3;
  3722. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3723. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3724. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3725. else
  3726. dpll |= PLL_REF_INPUT_DREFCLK;
  3727. dpll |= DPLL_VCO_ENABLE;
  3728. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  3729. POSTING_READ(DPLL(pipe));
  3730. udelay(150);
  3731. for_each_encoder_on_crtc(dev, crtc, encoder)
  3732. if (encoder->pre_pll_enable)
  3733. encoder->pre_pll_enable(encoder);
  3734. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  3735. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3736. I915_WRITE(DPLL(pipe), dpll);
  3737. /* Wait for the clocks to stabilize. */
  3738. POSTING_READ(DPLL(pipe));
  3739. udelay(150);
  3740. if (INTEL_INFO(dev)->gen >= 4) {
  3741. u32 temp = 0;
  3742. if (is_sdvo) {
  3743. temp = 0;
  3744. if (intel_crtc->config.pixel_multiplier > 1) {
  3745. temp = (intel_crtc->config.pixel_multiplier - 1)
  3746. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3747. }
  3748. }
  3749. I915_WRITE(DPLL_MD(pipe), temp);
  3750. } else {
  3751. /* The pixel multiplier can only be updated once the
  3752. * DPLL is enabled and the clocks are stable.
  3753. *
  3754. * So write it again.
  3755. */
  3756. I915_WRITE(DPLL(pipe), dpll);
  3757. }
  3758. }
  3759. static void i8xx_update_pll(struct drm_crtc *crtc,
  3760. struct drm_display_mode *adjusted_mode,
  3761. intel_clock_t *clock, intel_clock_t *reduced_clock,
  3762. int num_connectors)
  3763. {
  3764. struct drm_device *dev = crtc->dev;
  3765. struct drm_i915_private *dev_priv = dev->dev_private;
  3766. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3767. struct intel_encoder *encoder;
  3768. int pipe = intel_crtc->pipe;
  3769. u32 dpll;
  3770. i9xx_update_pll_dividers(crtc, clock, reduced_clock);
  3771. dpll = DPLL_VGA_MODE_DIS;
  3772. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  3773. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3774. } else {
  3775. if (clock->p1 == 2)
  3776. dpll |= PLL_P1_DIVIDE_BY_TWO;
  3777. else
  3778. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3779. if (clock->p2 == 4)
  3780. dpll |= PLL_P2_DIVIDE_BY_4;
  3781. }
  3782. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3783. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3784. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3785. else
  3786. dpll |= PLL_REF_INPUT_DREFCLK;
  3787. dpll |= DPLL_VCO_ENABLE;
  3788. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  3789. POSTING_READ(DPLL(pipe));
  3790. udelay(150);
  3791. for_each_encoder_on_crtc(dev, crtc, encoder)
  3792. if (encoder->pre_pll_enable)
  3793. encoder->pre_pll_enable(encoder);
  3794. I915_WRITE(DPLL(pipe), dpll);
  3795. /* Wait for the clocks to stabilize. */
  3796. POSTING_READ(DPLL(pipe));
  3797. udelay(150);
  3798. /* The pixel multiplier can only be updated once the
  3799. * DPLL is enabled and the clocks are stable.
  3800. *
  3801. * So write it again.
  3802. */
  3803. I915_WRITE(DPLL(pipe), dpll);
  3804. }
  3805. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc,
  3806. struct drm_display_mode *mode,
  3807. struct drm_display_mode *adjusted_mode)
  3808. {
  3809. struct drm_device *dev = intel_crtc->base.dev;
  3810. struct drm_i915_private *dev_priv = dev->dev_private;
  3811. enum pipe pipe = intel_crtc->pipe;
  3812. enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
  3813. uint32_t vsyncshift;
  3814. if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3815. /* the chip adds 2 halflines automatically */
  3816. adjusted_mode->crtc_vtotal -= 1;
  3817. adjusted_mode->crtc_vblank_end -= 1;
  3818. vsyncshift = adjusted_mode->crtc_hsync_start
  3819. - adjusted_mode->crtc_htotal / 2;
  3820. } else {
  3821. vsyncshift = 0;
  3822. }
  3823. if (INTEL_INFO(dev)->gen > 3)
  3824. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  3825. I915_WRITE(HTOTAL(cpu_transcoder),
  3826. (adjusted_mode->crtc_hdisplay - 1) |
  3827. ((adjusted_mode->crtc_htotal - 1) << 16));
  3828. I915_WRITE(HBLANK(cpu_transcoder),
  3829. (adjusted_mode->crtc_hblank_start - 1) |
  3830. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3831. I915_WRITE(HSYNC(cpu_transcoder),
  3832. (adjusted_mode->crtc_hsync_start - 1) |
  3833. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3834. I915_WRITE(VTOTAL(cpu_transcoder),
  3835. (adjusted_mode->crtc_vdisplay - 1) |
  3836. ((adjusted_mode->crtc_vtotal - 1) << 16));
  3837. I915_WRITE(VBLANK(cpu_transcoder),
  3838. (adjusted_mode->crtc_vblank_start - 1) |
  3839. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  3840. I915_WRITE(VSYNC(cpu_transcoder),
  3841. (adjusted_mode->crtc_vsync_start - 1) |
  3842. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3843. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  3844. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  3845. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  3846. * bits. */
  3847. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  3848. (pipe == PIPE_B || pipe == PIPE_C))
  3849. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  3850. /* pipesrc controls the size that is scaled from, which should
  3851. * always be the user's requested size.
  3852. */
  3853. I915_WRITE(PIPESRC(pipe),
  3854. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3855. }
  3856. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  3857. int x, int y,
  3858. struct drm_framebuffer *fb)
  3859. {
  3860. struct drm_device *dev = crtc->dev;
  3861. struct drm_i915_private *dev_priv = dev->dev_private;
  3862. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3863. struct drm_display_mode *adjusted_mode =
  3864. &intel_crtc->config.adjusted_mode;
  3865. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  3866. int pipe = intel_crtc->pipe;
  3867. int plane = intel_crtc->plane;
  3868. int refclk, num_connectors = 0;
  3869. intel_clock_t clock, reduced_clock;
  3870. u32 dspcntr, pipeconf;
  3871. bool ok, has_reduced_clock = false, is_sdvo = false;
  3872. bool is_lvds = false, is_tv = false, is_dp = false;
  3873. struct intel_encoder *encoder;
  3874. const intel_limit_t *limit;
  3875. int ret;
  3876. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3877. switch (encoder->type) {
  3878. case INTEL_OUTPUT_LVDS:
  3879. is_lvds = true;
  3880. break;
  3881. case INTEL_OUTPUT_SDVO:
  3882. case INTEL_OUTPUT_HDMI:
  3883. is_sdvo = true;
  3884. if (encoder->needs_tv_clock)
  3885. is_tv = true;
  3886. break;
  3887. case INTEL_OUTPUT_TVOUT:
  3888. is_tv = true;
  3889. break;
  3890. case INTEL_OUTPUT_DISPLAYPORT:
  3891. is_dp = true;
  3892. break;
  3893. }
  3894. num_connectors++;
  3895. }
  3896. refclk = i9xx_get_refclk(crtc, num_connectors);
  3897. /*
  3898. * Returns a set of divisors for the desired target clock with the given
  3899. * refclk, or FALSE. The returned values represent the clock equation:
  3900. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3901. */
  3902. limit = intel_limit(crtc, refclk);
  3903. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
  3904. &clock);
  3905. if (!ok) {
  3906. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3907. return -EINVAL;
  3908. }
  3909. /* Ensure that the cursor is valid for the new mode before changing... */
  3910. intel_crtc_update_cursor(crtc, true);
  3911. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3912. /*
  3913. * Ensure we match the reduced clock's P to the target clock.
  3914. * If the clocks don't match, we can't switch the display clock
  3915. * by using the FP0/FP1. In such case we will disable the LVDS
  3916. * downclock feature.
  3917. */
  3918. has_reduced_clock = limit->find_pll(limit, crtc,
  3919. dev_priv->lvds_downclock,
  3920. refclk,
  3921. &clock,
  3922. &reduced_clock);
  3923. }
  3924. if (is_sdvo && is_tv)
  3925. i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
  3926. if (IS_GEN2(dev))
  3927. i8xx_update_pll(crtc, adjusted_mode, &clock,
  3928. has_reduced_clock ? &reduced_clock : NULL,
  3929. num_connectors);
  3930. else if (IS_VALLEYVIEW(dev))
  3931. vlv_update_pll(crtc, &clock,
  3932. has_reduced_clock ? &reduced_clock : NULL,
  3933. num_connectors);
  3934. else
  3935. i9xx_update_pll(crtc, &clock,
  3936. has_reduced_clock ? &reduced_clock : NULL,
  3937. num_connectors);
  3938. /* setup pipeconf */
  3939. pipeconf = I915_READ(PIPECONF(pipe));
  3940. /* Set up the display plane register */
  3941. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3942. if (!IS_VALLEYVIEW(dev)) {
  3943. if (pipe == 0)
  3944. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3945. else
  3946. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3947. }
  3948. if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  3949. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  3950. * core speed.
  3951. *
  3952. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  3953. * pipe == 0 check?
  3954. */
  3955. if (mode->clock >
  3956. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  3957. pipeconf |= PIPECONF_DOUBLE_WIDE;
  3958. else
  3959. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  3960. }
  3961. /* default to 8bpc */
  3962. pipeconf &= ~(PIPECONF_BPC_MASK | PIPECONF_DITHER_EN);
  3963. if (is_dp) {
  3964. if (intel_crtc->config.dither) {
  3965. pipeconf |= PIPECONF_6BPC |
  3966. PIPECONF_DITHER_EN |
  3967. PIPECONF_DITHER_TYPE_SP;
  3968. }
  3969. }
  3970. if (IS_VALLEYVIEW(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
  3971. if (intel_crtc->config.dither) {
  3972. pipeconf |= PIPECONF_6BPC |
  3973. PIPECONF_ENABLE |
  3974. I965_PIPECONF_ACTIVE;
  3975. }
  3976. }
  3977. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  3978. drm_mode_debug_printmodeline(mode);
  3979. if (HAS_PIPE_CXSR(dev)) {
  3980. if (intel_crtc->lowfreq_avail) {
  3981. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  3982. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  3983. } else {
  3984. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  3985. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  3986. }
  3987. }
  3988. pipeconf &= ~PIPECONF_INTERLACE_MASK;
  3989. if (!IS_GEN2(dev) &&
  3990. adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  3991. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  3992. else
  3993. pipeconf |= PIPECONF_PROGRESSIVE;
  3994. intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
  3995. /* pipesrc and dspsize control the size that is scaled from,
  3996. * which should always be the user's requested size.
  3997. */
  3998. I915_WRITE(DSPSIZE(plane),
  3999. ((mode->vdisplay - 1) << 16) |
  4000. (mode->hdisplay - 1));
  4001. I915_WRITE(DSPPOS(plane), 0);
  4002. I915_WRITE(PIPECONF(pipe), pipeconf);
  4003. POSTING_READ(PIPECONF(pipe));
  4004. intel_enable_pipe(dev_priv, pipe, false);
  4005. intel_wait_for_vblank(dev, pipe);
  4006. I915_WRITE(DSPCNTR(plane), dspcntr);
  4007. POSTING_READ(DSPCNTR(plane));
  4008. ret = intel_pipe_set_base(crtc, x, y, fb);
  4009. intel_update_watermarks(dev);
  4010. return ret;
  4011. }
  4012. static void ironlake_init_pch_refclk(struct drm_device *dev)
  4013. {
  4014. struct drm_i915_private *dev_priv = dev->dev_private;
  4015. struct drm_mode_config *mode_config = &dev->mode_config;
  4016. struct intel_encoder *encoder;
  4017. u32 val, final;
  4018. bool has_lvds = false;
  4019. bool has_cpu_edp = false;
  4020. bool has_pch_edp = false;
  4021. bool has_panel = false;
  4022. bool has_ck505 = false;
  4023. bool can_ssc = false;
  4024. /* We need to take the global config into account */
  4025. list_for_each_entry(encoder, &mode_config->encoder_list,
  4026. base.head) {
  4027. switch (encoder->type) {
  4028. case INTEL_OUTPUT_LVDS:
  4029. has_panel = true;
  4030. has_lvds = true;
  4031. break;
  4032. case INTEL_OUTPUT_EDP:
  4033. has_panel = true;
  4034. if (intel_encoder_is_pch_edp(&encoder->base))
  4035. has_pch_edp = true;
  4036. else
  4037. has_cpu_edp = true;
  4038. break;
  4039. }
  4040. }
  4041. if (HAS_PCH_IBX(dev)) {
  4042. has_ck505 = dev_priv->display_clock_mode;
  4043. can_ssc = has_ck505;
  4044. } else {
  4045. has_ck505 = false;
  4046. can_ssc = true;
  4047. }
  4048. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
  4049. has_panel, has_lvds, has_pch_edp, has_cpu_edp,
  4050. has_ck505);
  4051. /* Ironlake: try to setup display ref clock before DPLL
  4052. * enabling. This is only under driver's control after
  4053. * PCH B stepping, previous chipset stepping should be
  4054. * ignoring this setting.
  4055. */
  4056. val = I915_READ(PCH_DREF_CONTROL);
  4057. /* As we must carefully and slowly disable/enable each source in turn,
  4058. * compute the final state we want first and check if we need to
  4059. * make any changes at all.
  4060. */
  4061. final = val;
  4062. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  4063. if (has_ck505)
  4064. final |= DREF_NONSPREAD_CK505_ENABLE;
  4065. else
  4066. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  4067. final &= ~DREF_SSC_SOURCE_MASK;
  4068. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4069. final &= ~DREF_SSC1_ENABLE;
  4070. if (has_panel) {
  4071. final |= DREF_SSC_SOURCE_ENABLE;
  4072. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  4073. final |= DREF_SSC1_ENABLE;
  4074. if (has_cpu_edp) {
  4075. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  4076. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  4077. else
  4078. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  4079. } else
  4080. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4081. } else {
  4082. final |= DREF_SSC_SOURCE_DISABLE;
  4083. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4084. }
  4085. if (final == val)
  4086. return;
  4087. /* Always enable nonspread source */
  4088. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  4089. if (has_ck505)
  4090. val |= DREF_NONSPREAD_CK505_ENABLE;
  4091. else
  4092. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  4093. if (has_panel) {
  4094. val &= ~DREF_SSC_SOURCE_MASK;
  4095. val |= DREF_SSC_SOURCE_ENABLE;
  4096. /* SSC must be turned on before enabling the CPU output */
  4097. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  4098. DRM_DEBUG_KMS("Using SSC on panel\n");
  4099. val |= DREF_SSC1_ENABLE;
  4100. } else
  4101. val &= ~DREF_SSC1_ENABLE;
  4102. /* Get SSC going before enabling the outputs */
  4103. I915_WRITE(PCH_DREF_CONTROL, val);
  4104. POSTING_READ(PCH_DREF_CONTROL);
  4105. udelay(200);
  4106. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4107. /* Enable CPU source on CPU attached eDP */
  4108. if (has_cpu_edp) {
  4109. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  4110. DRM_DEBUG_KMS("Using SSC on eDP\n");
  4111. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  4112. }
  4113. else
  4114. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  4115. } else
  4116. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4117. I915_WRITE(PCH_DREF_CONTROL, val);
  4118. POSTING_READ(PCH_DREF_CONTROL);
  4119. udelay(200);
  4120. } else {
  4121. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  4122. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4123. /* Turn off CPU output */
  4124. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4125. I915_WRITE(PCH_DREF_CONTROL, val);
  4126. POSTING_READ(PCH_DREF_CONTROL);
  4127. udelay(200);
  4128. /* Turn off the SSC source */
  4129. val &= ~DREF_SSC_SOURCE_MASK;
  4130. val |= DREF_SSC_SOURCE_DISABLE;
  4131. /* Turn off SSC1 */
  4132. val &= ~DREF_SSC1_ENABLE;
  4133. I915_WRITE(PCH_DREF_CONTROL, val);
  4134. POSTING_READ(PCH_DREF_CONTROL);
  4135. udelay(200);
  4136. }
  4137. BUG_ON(val != final);
  4138. }
  4139. /* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
  4140. static void lpt_init_pch_refclk(struct drm_device *dev)
  4141. {
  4142. struct drm_i915_private *dev_priv = dev->dev_private;
  4143. struct drm_mode_config *mode_config = &dev->mode_config;
  4144. struct intel_encoder *encoder;
  4145. bool has_vga = false;
  4146. bool is_sdv = false;
  4147. u32 tmp;
  4148. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  4149. switch (encoder->type) {
  4150. case INTEL_OUTPUT_ANALOG:
  4151. has_vga = true;
  4152. break;
  4153. }
  4154. }
  4155. if (!has_vga)
  4156. return;
  4157. mutex_lock(&dev_priv->dpio_lock);
  4158. /* XXX: Rip out SDV support once Haswell ships for real. */
  4159. if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
  4160. is_sdv = true;
  4161. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  4162. tmp &= ~SBI_SSCCTL_DISABLE;
  4163. tmp |= SBI_SSCCTL_PATHALT;
  4164. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  4165. udelay(24);
  4166. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  4167. tmp &= ~SBI_SSCCTL_PATHALT;
  4168. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  4169. if (!is_sdv) {
  4170. tmp = I915_READ(SOUTH_CHICKEN2);
  4171. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  4172. I915_WRITE(SOUTH_CHICKEN2, tmp);
  4173. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  4174. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  4175. DRM_ERROR("FDI mPHY reset assert timeout\n");
  4176. tmp = I915_READ(SOUTH_CHICKEN2);
  4177. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  4178. I915_WRITE(SOUTH_CHICKEN2, tmp);
  4179. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  4180. FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
  4181. 100))
  4182. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  4183. }
  4184. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  4185. tmp &= ~(0xFF << 24);
  4186. tmp |= (0x12 << 24);
  4187. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  4188. if (!is_sdv) {
  4189. tmp = intel_sbi_read(dev_priv, 0x808C, SBI_MPHY);
  4190. tmp &= ~(0x3 << 6);
  4191. tmp |= (1 << 6) | (1 << 0);
  4192. intel_sbi_write(dev_priv, 0x808C, tmp, SBI_MPHY);
  4193. }
  4194. if (is_sdv) {
  4195. tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
  4196. tmp |= 0x7FFF;
  4197. intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
  4198. }
  4199. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  4200. tmp |= (1 << 11);
  4201. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  4202. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  4203. tmp |= (1 << 11);
  4204. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  4205. if (is_sdv) {
  4206. tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
  4207. tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
  4208. intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
  4209. tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
  4210. tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
  4211. intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
  4212. tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
  4213. tmp |= (0x3F << 8);
  4214. intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
  4215. tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
  4216. tmp |= (0x3F << 8);
  4217. intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
  4218. }
  4219. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  4220. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  4221. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  4222. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  4223. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  4224. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  4225. if (!is_sdv) {
  4226. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  4227. tmp &= ~(7 << 13);
  4228. tmp |= (5 << 13);
  4229. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  4230. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  4231. tmp &= ~(7 << 13);
  4232. tmp |= (5 << 13);
  4233. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  4234. }
  4235. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  4236. tmp &= ~0xFF;
  4237. tmp |= 0x1C;
  4238. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  4239. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  4240. tmp &= ~0xFF;
  4241. tmp |= 0x1C;
  4242. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  4243. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  4244. tmp &= ~(0xFF << 16);
  4245. tmp |= (0x1C << 16);
  4246. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  4247. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  4248. tmp &= ~(0xFF << 16);
  4249. tmp |= (0x1C << 16);
  4250. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  4251. if (!is_sdv) {
  4252. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  4253. tmp |= (1 << 27);
  4254. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  4255. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  4256. tmp |= (1 << 27);
  4257. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  4258. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  4259. tmp &= ~(0xF << 28);
  4260. tmp |= (4 << 28);
  4261. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  4262. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  4263. tmp &= ~(0xF << 28);
  4264. tmp |= (4 << 28);
  4265. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  4266. }
  4267. /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
  4268. tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
  4269. tmp |= SBI_DBUFF0_ENABLE;
  4270. intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
  4271. mutex_unlock(&dev_priv->dpio_lock);
  4272. }
  4273. /*
  4274. * Initialize reference clocks when the driver loads
  4275. */
  4276. void intel_init_pch_refclk(struct drm_device *dev)
  4277. {
  4278. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  4279. ironlake_init_pch_refclk(dev);
  4280. else if (HAS_PCH_LPT(dev))
  4281. lpt_init_pch_refclk(dev);
  4282. }
  4283. static int ironlake_get_refclk(struct drm_crtc *crtc)
  4284. {
  4285. struct drm_device *dev = crtc->dev;
  4286. struct drm_i915_private *dev_priv = dev->dev_private;
  4287. struct intel_encoder *encoder;
  4288. struct intel_encoder *edp_encoder = NULL;
  4289. int num_connectors = 0;
  4290. bool is_lvds = false;
  4291. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4292. switch (encoder->type) {
  4293. case INTEL_OUTPUT_LVDS:
  4294. is_lvds = true;
  4295. break;
  4296. case INTEL_OUTPUT_EDP:
  4297. edp_encoder = encoder;
  4298. break;
  4299. }
  4300. num_connectors++;
  4301. }
  4302. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4303. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  4304. dev_priv->lvds_ssc_freq);
  4305. return dev_priv->lvds_ssc_freq * 1000;
  4306. }
  4307. return 120000;
  4308. }
  4309. static void ironlake_set_pipeconf(struct drm_crtc *crtc,
  4310. struct drm_display_mode *adjusted_mode,
  4311. bool dither)
  4312. {
  4313. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4314. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4315. int pipe = intel_crtc->pipe;
  4316. uint32_t val;
  4317. val = I915_READ(PIPECONF(pipe));
  4318. val &= ~PIPECONF_BPC_MASK;
  4319. switch (intel_crtc->config.pipe_bpp) {
  4320. case 18:
  4321. val |= PIPECONF_6BPC;
  4322. break;
  4323. case 24:
  4324. val |= PIPECONF_8BPC;
  4325. break;
  4326. case 30:
  4327. val |= PIPECONF_10BPC;
  4328. break;
  4329. case 36:
  4330. val |= PIPECONF_12BPC;
  4331. break;
  4332. default:
  4333. /* Case prevented by intel_choose_pipe_bpp_dither. */
  4334. BUG();
  4335. }
  4336. val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
  4337. if (dither)
  4338. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  4339. val &= ~PIPECONF_INTERLACE_MASK;
  4340. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  4341. val |= PIPECONF_INTERLACED_ILK;
  4342. else
  4343. val |= PIPECONF_PROGRESSIVE;
  4344. if (intel_crtc->config.limited_color_range)
  4345. val |= PIPECONF_COLOR_RANGE_SELECT;
  4346. else
  4347. val &= ~PIPECONF_COLOR_RANGE_SELECT;
  4348. I915_WRITE(PIPECONF(pipe), val);
  4349. POSTING_READ(PIPECONF(pipe));
  4350. }
  4351. /*
  4352. * Set up the pipe CSC unit.
  4353. *
  4354. * Currently only full range RGB to limited range RGB conversion
  4355. * is supported, but eventually this should handle various
  4356. * RGB<->YCbCr scenarios as well.
  4357. */
  4358. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  4359. {
  4360. struct drm_device *dev = crtc->dev;
  4361. struct drm_i915_private *dev_priv = dev->dev_private;
  4362. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4363. int pipe = intel_crtc->pipe;
  4364. uint16_t coeff = 0x7800; /* 1.0 */
  4365. /*
  4366. * TODO: Check what kind of values actually come out of the pipe
  4367. * with these coeff/postoff values and adjust to get the best
  4368. * accuracy. Perhaps we even need to take the bpc value into
  4369. * consideration.
  4370. */
  4371. if (intel_crtc->config.limited_color_range)
  4372. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  4373. /*
  4374. * GY/GU and RY/RU should be the other way around according
  4375. * to BSpec, but reality doesn't agree. Just set them up in
  4376. * a way that results in the correct picture.
  4377. */
  4378. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  4379. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  4380. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  4381. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  4382. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  4383. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  4384. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  4385. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  4386. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  4387. if (INTEL_INFO(dev)->gen > 6) {
  4388. uint16_t postoff = 0;
  4389. if (intel_crtc->config.limited_color_range)
  4390. postoff = (16 * (1 << 13) / 255) & 0x1fff;
  4391. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  4392. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  4393. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  4394. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  4395. } else {
  4396. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  4397. if (intel_crtc->config.limited_color_range)
  4398. mode |= CSC_BLACK_SCREEN_OFFSET;
  4399. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  4400. }
  4401. }
  4402. static void haswell_set_pipeconf(struct drm_crtc *crtc,
  4403. struct drm_display_mode *adjusted_mode,
  4404. bool dither)
  4405. {
  4406. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4407. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4408. enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
  4409. uint32_t val;
  4410. val = I915_READ(PIPECONF(cpu_transcoder));
  4411. val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
  4412. if (dither)
  4413. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  4414. val &= ~PIPECONF_INTERLACE_MASK_HSW;
  4415. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  4416. val |= PIPECONF_INTERLACED_ILK;
  4417. else
  4418. val |= PIPECONF_PROGRESSIVE;
  4419. I915_WRITE(PIPECONF(cpu_transcoder), val);
  4420. POSTING_READ(PIPECONF(cpu_transcoder));
  4421. }
  4422. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  4423. struct drm_display_mode *adjusted_mode,
  4424. intel_clock_t *clock,
  4425. bool *has_reduced_clock,
  4426. intel_clock_t *reduced_clock)
  4427. {
  4428. struct drm_device *dev = crtc->dev;
  4429. struct drm_i915_private *dev_priv = dev->dev_private;
  4430. struct intel_encoder *intel_encoder;
  4431. int refclk;
  4432. const intel_limit_t *limit;
  4433. bool ret, is_sdvo = false, is_tv = false, is_lvds = false;
  4434. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  4435. switch (intel_encoder->type) {
  4436. case INTEL_OUTPUT_LVDS:
  4437. is_lvds = true;
  4438. break;
  4439. case INTEL_OUTPUT_SDVO:
  4440. case INTEL_OUTPUT_HDMI:
  4441. is_sdvo = true;
  4442. if (intel_encoder->needs_tv_clock)
  4443. is_tv = true;
  4444. break;
  4445. case INTEL_OUTPUT_TVOUT:
  4446. is_tv = true;
  4447. break;
  4448. }
  4449. }
  4450. refclk = ironlake_get_refclk(crtc);
  4451. /*
  4452. * Returns a set of divisors for the desired target clock with the given
  4453. * refclk, or FALSE. The returned values represent the clock equation:
  4454. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  4455. */
  4456. limit = intel_limit(crtc, refclk);
  4457. ret = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
  4458. clock);
  4459. if (!ret)
  4460. return false;
  4461. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4462. /*
  4463. * Ensure we match the reduced clock's P to the target clock.
  4464. * If the clocks don't match, we can't switch the display clock
  4465. * by using the FP0/FP1. In such case we will disable the LVDS
  4466. * downclock feature.
  4467. */
  4468. *has_reduced_clock = limit->find_pll(limit, crtc,
  4469. dev_priv->lvds_downclock,
  4470. refclk,
  4471. clock,
  4472. reduced_clock);
  4473. }
  4474. if (is_sdvo && is_tv)
  4475. i9xx_adjust_sdvo_tv_clock(adjusted_mode, clock);
  4476. return true;
  4477. }
  4478. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  4479. {
  4480. struct drm_i915_private *dev_priv = dev->dev_private;
  4481. uint32_t temp;
  4482. temp = I915_READ(SOUTH_CHICKEN1);
  4483. if (temp & FDI_BC_BIFURCATION_SELECT)
  4484. return;
  4485. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  4486. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  4487. temp |= FDI_BC_BIFURCATION_SELECT;
  4488. DRM_DEBUG_KMS("enabling fdi C rx\n");
  4489. I915_WRITE(SOUTH_CHICKEN1, temp);
  4490. POSTING_READ(SOUTH_CHICKEN1);
  4491. }
  4492. static bool ironlake_check_fdi_lanes(struct intel_crtc *intel_crtc)
  4493. {
  4494. struct drm_device *dev = intel_crtc->base.dev;
  4495. struct drm_i915_private *dev_priv = dev->dev_private;
  4496. struct intel_crtc *pipe_B_crtc =
  4497. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4498. DRM_DEBUG_KMS("checking fdi config on pipe %i, lanes %i\n",
  4499. intel_crtc->pipe, intel_crtc->fdi_lanes);
  4500. if (intel_crtc->fdi_lanes > 4) {
  4501. DRM_DEBUG_KMS("invalid fdi lane config on pipe %i: %i lanes\n",
  4502. intel_crtc->pipe, intel_crtc->fdi_lanes);
  4503. /* Clamp lanes to avoid programming the hw with bogus values. */
  4504. intel_crtc->fdi_lanes = 4;
  4505. return false;
  4506. }
  4507. if (INTEL_INFO(dev)->num_pipes == 2)
  4508. return true;
  4509. switch (intel_crtc->pipe) {
  4510. case PIPE_A:
  4511. return true;
  4512. case PIPE_B:
  4513. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4514. intel_crtc->fdi_lanes > 2) {
  4515. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
  4516. intel_crtc->pipe, intel_crtc->fdi_lanes);
  4517. /* Clamp lanes to avoid programming the hw with bogus values. */
  4518. intel_crtc->fdi_lanes = 2;
  4519. return false;
  4520. }
  4521. if (intel_crtc->fdi_lanes > 2)
  4522. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  4523. else
  4524. cpt_enable_fdi_bc_bifurcation(dev);
  4525. return true;
  4526. case PIPE_C:
  4527. if (!pipe_B_crtc->base.enabled || pipe_B_crtc->fdi_lanes <= 2) {
  4528. if (intel_crtc->fdi_lanes > 2) {
  4529. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
  4530. intel_crtc->pipe, intel_crtc->fdi_lanes);
  4531. /* Clamp lanes to avoid programming the hw with bogus values. */
  4532. intel_crtc->fdi_lanes = 2;
  4533. return false;
  4534. }
  4535. } else {
  4536. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4537. return false;
  4538. }
  4539. cpt_enable_fdi_bc_bifurcation(dev);
  4540. return true;
  4541. default:
  4542. BUG();
  4543. }
  4544. }
  4545. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  4546. {
  4547. /*
  4548. * Account for spread spectrum to avoid
  4549. * oversubscribing the link. Max center spread
  4550. * is 2.5%; use 5% for safety's sake.
  4551. */
  4552. u32 bps = target_clock * bpp * 21 / 20;
  4553. return bps / (link_bw * 8) + 1;
  4554. }
  4555. static void ironlake_set_m_n(struct drm_crtc *crtc)
  4556. {
  4557. struct drm_device *dev = crtc->dev;
  4558. struct drm_i915_private *dev_priv = dev->dev_private;
  4559. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4560. struct drm_display_mode *adjusted_mode =
  4561. &intel_crtc->config.adjusted_mode;
  4562. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  4563. enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
  4564. struct intel_encoder *intel_encoder, *edp_encoder = NULL;
  4565. struct intel_link_m_n m_n = {0};
  4566. int target_clock, lane, link_bw;
  4567. bool is_dp = false, is_cpu_edp = false;
  4568. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  4569. switch (intel_encoder->type) {
  4570. case INTEL_OUTPUT_DISPLAYPORT:
  4571. is_dp = true;
  4572. break;
  4573. case INTEL_OUTPUT_EDP:
  4574. is_dp = true;
  4575. if (!intel_encoder_is_pch_edp(&intel_encoder->base))
  4576. is_cpu_edp = true;
  4577. edp_encoder = intel_encoder;
  4578. break;
  4579. }
  4580. }
  4581. /* FDI link */
  4582. lane = 0;
  4583. /* CPU eDP doesn't require FDI link, so just set DP M/N
  4584. according to current link config */
  4585. if (is_cpu_edp) {
  4586. intel_edp_link_config(edp_encoder, &lane, &link_bw);
  4587. } else {
  4588. /* FDI is a binary signal running at ~2.7GHz, encoding
  4589. * each output octet as 10 bits. The actual frequency
  4590. * is stored as a divider into a 100MHz clock, and the
  4591. * mode pixel clock is stored in units of 1KHz.
  4592. * Hence the bw of each lane in terms of the mode signal
  4593. * is:
  4594. */
  4595. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4596. }
  4597. /* [e]DP over FDI requires target mode clock instead of link clock. */
  4598. if (edp_encoder)
  4599. target_clock = intel_edp_target_clock(edp_encoder, mode);
  4600. else if (is_dp)
  4601. target_clock = mode->clock;
  4602. else
  4603. target_clock = adjusted_mode->clock;
  4604. if (!lane)
  4605. lane = ironlake_get_lanes_required(target_clock, link_bw,
  4606. intel_crtc->config.pipe_bpp);
  4607. intel_crtc->fdi_lanes = lane;
  4608. if (intel_crtc->config.pixel_multiplier > 1)
  4609. link_bw *= intel_crtc->config.pixel_multiplier;
  4610. intel_link_compute_m_n(intel_crtc->config.pipe_bpp, lane, target_clock,
  4611. link_bw, &m_n);
  4612. I915_WRITE(PIPE_DATA_M1(cpu_transcoder), TU_SIZE(m_n.tu) | m_n.gmch_m);
  4613. I915_WRITE(PIPE_DATA_N1(cpu_transcoder), m_n.gmch_n);
  4614. I915_WRITE(PIPE_LINK_M1(cpu_transcoder), m_n.link_m);
  4615. I915_WRITE(PIPE_LINK_N1(cpu_transcoder), m_n.link_n);
  4616. }
  4617. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  4618. intel_clock_t *clock, u32 fp)
  4619. {
  4620. struct drm_crtc *crtc = &intel_crtc->base;
  4621. struct drm_device *dev = crtc->dev;
  4622. struct drm_i915_private *dev_priv = dev->dev_private;
  4623. struct intel_encoder *intel_encoder;
  4624. uint32_t dpll;
  4625. int factor, num_connectors = 0;
  4626. bool is_lvds = false, is_sdvo = false, is_tv = false;
  4627. bool is_dp = false, is_cpu_edp = false;
  4628. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  4629. switch (intel_encoder->type) {
  4630. case INTEL_OUTPUT_LVDS:
  4631. is_lvds = true;
  4632. break;
  4633. case INTEL_OUTPUT_SDVO:
  4634. case INTEL_OUTPUT_HDMI:
  4635. is_sdvo = true;
  4636. if (intel_encoder->needs_tv_clock)
  4637. is_tv = true;
  4638. break;
  4639. case INTEL_OUTPUT_TVOUT:
  4640. is_tv = true;
  4641. break;
  4642. case INTEL_OUTPUT_DISPLAYPORT:
  4643. is_dp = true;
  4644. break;
  4645. case INTEL_OUTPUT_EDP:
  4646. is_dp = true;
  4647. if (!intel_encoder_is_pch_edp(&intel_encoder->base))
  4648. is_cpu_edp = true;
  4649. break;
  4650. }
  4651. num_connectors++;
  4652. }
  4653. /* Enable autotuning of the PLL clock (if permissible) */
  4654. factor = 21;
  4655. if (is_lvds) {
  4656. if ((intel_panel_use_ssc(dev_priv) &&
  4657. dev_priv->lvds_ssc_freq == 100) ||
  4658. intel_is_dual_link_lvds(dev))
  4659. factor = 25;
  4660. } else if (is_sdvo && is_tv)
  4661. factor = 20;
  4662. if (clock->m < factor * clock->n)
  4663. fp |= FP_CB_TUNE;
  4664. dpll = 0;
  4665. if (is_lvds)
  4666. dpll |= DPLLB_MODE_LVDS;
  4667. else
  4668. dpll |= DPLLB_MODE_DAC_SERIAL;
  4669. if (is_sdvo) {
  4670. if (intel_crtc->config.pixel_multiplier > 1) {
  4671. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  4672. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  4673. }
  4674. dpll |= DPLL_DVO_HIGH_SPEED;
  4675. }
  4676. if (is_dp && !is_cpu_edp)
  4677. dpll |= DPLL_DVO_HIGH_SPEED;
  4678. /* compute bitmask from p1 value */
  4679. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4680. /* also FPA1 */
  4681. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4682. switch (clock->p2) {
  4683. case 5:
  4684. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4685. break;
  4686. case 7:
  4687. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4688. break;
  4689. case 10:
  4690. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4691. break;
  4692. case 14:
  4693. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4694. break;
  4695. }
  4696. if (is_sdvo && is_tv)
  4697. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4698. else if (is_tv)
  4699. /* XXX: just matching BIOS for now */
  4700. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  4701. dpll |= 3;
  4702. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4703. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4704. else
  4705. dpll |= PLL_REF_INPUT_DREFCLK;
  4706. return dpll;
  4707. }
  4708. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  4709. int x, int y,
  4710. struct drm_framebuffer *fb)
  4711. {
  4712. struct drm_device *dev = crtc->dev;
  4713. struct drm_i915_private *dev_priv = dev->dev_private;
  4714. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4715. struct drm_display_mode *adjusted_mode =
  4716. &intel_crtc->config.adjusted_mode;
  4717. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  4718. int pipe = intel_crtc->pipe;
  4719. int plane = intel_crtc->plane;
  4720. int num_connectors = 0;
  4721. intel_clock_t clock, reduced_clock;
  4722. u32 dpll, fp = 0, fp2 = 0;
  4723. bool ok, has_reduced_clock = false;
  4724. bool is_lvds = false, is_dp = false, is_cpu_edp = false;
  4725. struct intel_encoder *encoder;
  4726. int ret;
  4727. bool dither, fdi_config_ok;
  4728. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4729. switch (encoder->type) {
  4730. case INTEL_OUTPUT_LVDS:
  4731. is_lvds = true;
  4732. break;
  4733. case INTEL_OUTPUT_DISPLAYPORT:
  4734. is_dp = true;
  4735. break;
  4736. case INTEL_OUTPUT_EDP:
  4737. is_dp = true;
  4738. if (!intel_encoder_is_pch_edp(&encoder->base))
  4739. is_cpu_edp = true;
  4740. break;
  4741. }
  4742. num_connectors++;
  4743. }
  4744. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  4745. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  4746. ok = ironlake_compute_clocks(crtc, adjusted_mode, &clock,
  4747. &has_reduced_clock, &reduced_clock);
  4748. if (!ok) {
  4749. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4750. return -EINVAL;
  4751. }
  4752. /* Ensure that the cursor is valid for the new mode before changing... */
  4753. intel_crtc_update_cursor(crtc, true);
  4754. /* determine panel color depth */
  4755. dither = intel_crtc->config.dither;
  4756. if (is_lvds && dev_priv->lvds_dither)
  4757. dither = true;
  4758. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  4759. if (has_reduced_clock)
  4760. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  4761. reduced_clock.m2;
  4762. dpll = ironlake_compute_dpll(intel_crtc, &clock, fp);
  4763. DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
  4764. drm_mode_debug_printmodeline(mode);
  4765. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  4766. if (!is_cpu_edp) {
  4767. struct intel_pch_pll *pll;
  4768. pll = intel_get_pch_pll(intel_crtc, dpll, fp);
  4769. if (pll == NULL) {
  4770. DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
  4771. pipe);
  4772. return -EINVAL;
  4773. }
  4774. } else
  4775. intel_put_pch_pll(intel_crtc);
  4776. if (is_dp && !is_cpu_edp)
  4777. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  4778. for_each_encoder_on_crtc(dev, crtc, encoder)
  4779. if (encoder->pre_pll_enable)
  4780. encoder->pre_pll_enable(encoder);
  4781. if (intel_crtc->pch_pll) {
  4782. I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
  4783. /* Wait for the clocks to stabilize. */
  4784. POSTING_READ(intel_crtc->pch_pll->pll_reg);
  4785. udelay(150);
  4786. /* The pixel multiplier can only be updated once the
  4787. * DPLL is enabled and the clocks are stable.
  4788. *
  4789. * So write it again.
  4790. */
  4791. I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
  4792. }
  4793. intel_crtc->lowfreq_avail = false;
  4794. if (intel_crtc->pch_pll) {
  4795. if (is_lvds && has_reduced_clock && i915_powersave) {
  4796. I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
  4797. intel_crtc->lowfreq_avail = true;
  4798. } else {
  4799. I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
  4800. }
  4801. }
  4802. intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
  4803. /* Note, this also computes intel_crtc->fdi_lanes which is used below in
  4804. * ironlake_check_fdi_lanes. */
  4805. ironlake_set_m_n(crtc);
  4806. fdi_config_ok = ironlake_check_fdi_lanes(intel_crtc);
  4807. ironlake_set_pipeconf(crtc, adjusted_mode, dither);
  4808. intel_wait_for_vblank(dev, pipe);
  4809. /* Set up the display plane register */
  4810. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
  4811. POSTING_READ(DSPCNTR(plane));
  4812. ret = intel_pipe_set_base(crtc, x, y, fb);
  4813. intel_update_watermarks(dev);
  4814. intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
  4815. return fdi_config_ok ? ret : -EINVAL;
  4816. }
  4817. static void haswell_modeset_global_resources(struct drm_device *dev)
  4818. {
  4819. struct drm_i915_private *dev_priv = dev->dev_private;
  4820. bool enable = false;
  4821. struct intel_crtc *crtc;
  4822. struct intel_encoder *encoder;
  4823. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
  4824. if (crtc->pipe != PIPE_A && crtc->base.enabled)
  4825. enable = true;
  4826. /* XXX: Should check for edp transcoder here, but thanks to init
  4827. * sequence that's not yet available. Just in case desktop eDP
  4828. * on PORT D is possible on haswell, too. */
  4829. }
  4830. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  4831. base.head) {
  4832. if (encoder->type != INTEL_OUTPUT_EDP &&
  4833. encoder->connectors_active)
  4834. enable = true;
  4835. }
  4836. /* Even the eDP panel fitter is outside the always-on well. */
  4837. if (dev_priv->pch_pf_size)
  4838. enable = true;
  4839. intel_set_power_well(dev, enable);
  4840. }
  4841. static int haswell_crtc_mode_set(struct drm_crtc *crtc,
  4842. int x, int y,
  4843. struct drm_framebuffer *fb)
  4844. {
  4845. struct drm_device *dev = crtc->dev;
  4846. struct drm_i915_private *dev_priv = dev->dev_private;
  4847. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4848. struct drm_display_mode *adjusted_mode =
  4849. &intel_crtc->config.adjusted_mode;
  4850. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  4851. int pipe = intel_crtc->pipe;
  4852. int plane = intel_crtc->plane;
  4853. int num_connectors = 0;
  4854. bool is_dp = false, is_cpu_edp = false;
  4855. struct intel_encoder *encoder;
  4856. int ret;
  4857. bool dither;
  4858. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4859. switch (encoder->type) {
  4860. case INTEL_OUTPUT_DISPLAYPORT:
  4861. is_dp = true;
  4862. break;
  4863. case INTEL_OUTPUT_EDP:
  4864. is_dp = true;
  4865. if (!intel_encoder_is_pch_edp(&encoder->base))
  4866. is_cpu_edp = true;
  4867. break;
  4868. }
  4869. num_connectors++;
  4870. }
  4871. /* We are not sure yet this won't happen. */
  4872. WARN(!HAS_PCH_LPT(dev), "Unexpected PCH type %d\n",
  4873. INTEL_PCH_TYPE(dev));
  4874. WARN(num_connectors != 1, "%d connectors attached to pipe %c\n",
  4875. num_connectors, pipe_name(pipe));
  4876. WARN_ON(I915_READ(PIPECONF(intel_crtc->cpu_transcoder)) &
  4877. (PIPECONF_ENABLE | I965_PIPECONF_ACTIVE));
  4878. WARN_ON(I915_READ(DSPCNTR(plane)) & DISPLAY_PLANE_ENABLE);
  4879. if (!intel_ddi_pll_mode_set(crtc, adjusted_mode->clock))
  4880. return -EINVAL;
  4881. /* Ensure that the cursor is valid for the new mode before changing... */
  4882. intel_crtc_update_cursor(crtc, true);
  4883. /* determine panel color depth */
  4884. dither = intel_crtc->config.dither;
  4885. DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
  4886. drm_mode_debug_printmodeline(mode);
  4887. if (is_dp && !is_cpu_edp)
  4888. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  4889. intel_crtc->lowfreq_avail = false;
  4890. intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
  4891. if (!is_dp || is_cpu_edp)
  4892. ironlake_set_m_n(crtc);
  4893. haswell_set_pipeconf(crtc, adjusted_mode, dither);
  4894. intel_set_pipe_csc(crtc);
  4895. /* Set up the display plane register */
  4896. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
  4897. POSTING_READ(DSPCNTR(plane));
  4898. ret = intel_pipe_set_base(crtc, x, y, fb);
  4899. intel_update_watermarks(dev);
  4900. intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
  4901. return ret;
  4902. }
  4903. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  4904. int x, int y,
  4905. struct drm_framebuffer *fb)
  4906. {
  4907. struct drm_device *dev = crtc->dev;
  4908. struct drm_i915_private *dev_priv = dev->dev_private;
  4909. struct drm_encoder_helper_funcs *encoder_funcs;
  4910. struct intel_encoder *encoder;
  4911. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4912. struct drm_display_mode *adjusted_mode =
  4913. &intel_crtc->config.adjusted_mode;
  4914. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  4915. int pipe = intel_crtc->pipe;
  4916. int ret;
  4917. if (IS_HASWELL(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  4918. intel_crtc->cpu_transcoder = TRANSCODER_EDP;
  4919. else
  4920. intel_crtc->cpu_transcoder = pipe;
  4921. drm_vblank_pre_modeset(dev, pipe);
  4922. ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
  4923. drm_vblank_post_modeset(dev, pipe);
  4924. if (ret != 0)
  4925. return ret;
  4926. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4927. DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
  4928. encoder->base.base.id,
  4929. drm_get_encoder_name(&encoder->base),
  4930. mode->base.id, mode->name);
  4931. if (encoder->mode_set) {
  4932. encoder->mode_set(encoder);
  4933. } else {
  4934. encoder_funcs = encoder->base.helper_private;
  4935. encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
  4936. }
  4937. }
  4938. return 0;
  4939. }
  4940. static bool intel_eld_uptodate(struct drm_connector *connector,
  4941. int reg_eldv, uint32_t bits_eldv,
  4942. int reg_elda, uint32_t bits_elda,
  4943. int reg_edid)
  4944. {
  4945. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  4946. uint8_t *eld = connector->eld;
  4947. uint32_t i;
  4948. i = I915_READ(reg_eldv);
  4949. i &= bits_eldv;
  4950. if (!eld[0])
  4951. return !i;
  4952. if (!i)
  4953. return false;
  4954. i = I915_READ(reg_elda);
  4955. i &= ~bits_elda;
  4956. I915_WRITE(reg_elda, i);
  4957. for (i = 0; i < eld[2]; i++)
  4958. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  4959. return false;
  4960. return true;
  4961. }
  4962. static void g4x_write_eld(struct drm_connector *connector,
  4963. struct drm_crtc *crtc)
  4964. {
  4965. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  4966. uint8_t *eld = connector->eld;
  4967. uint32_t eldv;
  4968. uint32_t len;
  4969. uint32_t i;
  4970. i = I915_READ(G4X_AUD_VID_DID);
  4971. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  4972. eldv = G4X_ELDV_DEVCL_DEVBLC;
  4973. else
  4974. eldv = G4X_ELDV_DEVCTG;
  4975. if (intel_eld_uptodate(connector,
  4976. G4X_AUD_CNTL_ST, eldv,
  4977. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  4978. G4X_HDMIW_HDMIEDID))
  4979. return;
  4980. i = I915_READ(G4X_AUD_CNTL_ST);
  4981. i &= ~(eldv | G4X_ELD_ADDR);
  4982. len = (i >> 9) & 0x1f; /* ELD buffer size */
  4983. I915_WRITE(G4X_AUD_CNTL_ST, i);
  4984. if (!eld[0])
  4985. return;
  4986. len = min_t(uint8_t, eld[2], len);
  4987. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  4988. for (i = 0; i < len; i++)
  4989. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  4990. i = I915_READ(G4X_AUD_CNTL_ST);
  4991. i |= eldv;
  4992. I915_WRITE(G4X_AUD_CNTL_ST, i);
  4993. }
  4994. static void haswell_write_eld(struct drm_connector *connector,
  4995. struct drm_crtc *crtc)
  4996. {
  4997. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  4998. uint8_t *eld = connector->eld;
  4999. struct drm_device *dev = crtc->dev;
  5000. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5001. uint32_t eldv;
  5002. uint32_t i;
  5003. int len;
  5004. int pipe = to_intel_crtc(crtc)->pipe;
  5005. int tmp;
  5006. int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
  5007. int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
  5008. int aud_config = HSW_AUD_CFG(pipe);
  5009. int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
  5010. DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
  5011. /* Audio output enable */
  5012. DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
  5013. tmp = I915_READ(aud_cntrl_st2);
  5014. tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
  5015. I915_WRITE(aud_cntrl_st2, tmp);
  5016. /* Wait for 1 vertical blank */
  5017. intel_wait_for_vblank(dev, pipe);
  5018. /* Set ELD valid state */
  5019. tmp = I915_READ(aud_cntrl_st2);
  5020. DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
  5021. tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
  5022. I915_WRITE(aud_cntrl_st2, tmp);
  5023. tmp = I915_READ(aud_cntrl_st2);
  5024. DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
  5025. /* Enable HDMI mode */
  5026. tmp = I915_READ(aud_config);
  5027. DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
  5028. /* clear N_programing_enable and N_value_index */
  5029. tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
  5030. I915_WRITE(aud_config, tmp);
  5031. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  5032. eldv = AUDIO_ELD_VALID_A << (pipe * 4);
  5033. intel_crtc->eld_vld = true;
  5034. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  5035. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  5036. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  5037. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  5038. } else
  5039. I915_WRITE(aud_config, 0);
  5040. if (intel_eld_uptodate(connector,
  5041. aud_cntrl_st2, eldv,
  5042. aud_cntl_st, IBX_ELD_ADDRESS,
  5043. hdmiw_hdmiedid))
  5044. return;
  5045. i = I915_READ(aud_cntrl_st2);
  5046. i &= ~eldv;
  5047. I915_WRITE(aud_cntrl_st2, i);
  5048. if (!eld[0])
  5049. return;
  5050. i = I915_READ(aud_cntl_st);
  5051. i &= ~IBX_ELD_ADDRESS;
  5052. I915_WRITE(aud_cntl_st, i);
  5053. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  5054. DRM_DEBUG_DRIVER("port num:%d\n", i);
  5055. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  5056. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5057. for (i = 0; i < len; i++)
  5058. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  5059. i = I915_READ(aud_cntrl_st2);
  5060. i |= eldv;
  5061. I915_WRITE(aud_cntrl_st2, i);
  5062. }
  5063. static void ironlake_write_eld(struct drm_connector *connector,
  5064. struct drm_crtc *crtc)
  5065. {
  5066. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5067. uint8_t *eld = connector->eld;
  5068. uint32_t eldv;
  5069. uint32_t i;
  5070. int len;
  5071. int hdmiw_hdmiedid;
  5072. int aud_config;
  5073. int aud_cntl_st;
  5074. int aud_cntrl_st2;
  5075. int pipe = to_intel_crtc(crtc)->pipe;
  5076. if (HAS_PCH_IBX(connector->dev)) {
  5077. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  5078. aud_config = IBX_AUD_CFG(pipe);
  5079. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  5080. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  5081. } else {
  5082. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  5083. aud_config = CPT_AUD_CFG(pipe);
  5084. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  5085. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  5086. }
  5087. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  5088. i = I915_READ(aud_cntl_st);
  5089. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  5090. if (!i) {
  5091. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  5092. /* operate blindly on all ports */
  5093. eldv = IBX_ELD_VALIDB;
  5094. eldv |= IBX_ELD_VALIDB << 4;
  5095. eldv |= IBX_ELD_VALIDB << 8;
  5096. } else {
  5097. DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
  5098. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  5099. }
  5100. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  5101. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  5102. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  5103. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  5104. } else
  5105. I915_WRITE(aud_config, 0);
  5106. if (intel_eld_uptodate(connector,
  5107. aud_cntrl_st2, eldv,
  5108. aud_cntl_st, IBX_ELD_ADDRESS,
  5109. hdmiw_hdmiedid))
  5110. return;
  5111. i = I915_READ(aud_cntrl_st2);
  5112. i &= ~eldv;
  5113. I915_WRITE(aud_cntrl_st2, i);
  5114. if (!eld[0])
  5115. return;
  5116. i = I915_READ(aud_cntl_st);
  5117. i &= ~IBX_ELD_ADDRESS;
  5118. I915_WRITE(aud_cntl_st, i);
  5119. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  5120. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5121. for (i = 0; i < len; i++)
  5122. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  5123. i = I915_READ(aud_cntrl_st2);
  5124. i |= eldv;
  5125. I915_WRITE(aud_cntrl_st2, i);
  5126. }
  5127. void intel_write_eld(struct drm_encoder *encoder,
  5128. struct drm_display_mode *mode)
  5129. {
  5130. struct drm_crtc *crtc = encoder->crtc;
  5131. struct drm_connector *connector;
  5132. struct drm_device *dev = encoder->dev;
  5133. struct drm_i915_private *dev_priv = dev->dev_private;
  5134. connector = drm_select_eld(encoder, mode);
  5135. if (!connector)
  5136. return;
  5137. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5138. connector->base.id,
  5139. drm_get_connector_name(connector),
  5140. connector->encoder->base.id,
  5141. drm_get_encoder_name(connector->encoder));
  5142. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  5143. if (dev_priv->display.write_eld)
  5144. dev_priv->display.write_eld(connector, crtc);
  5145. }
  5146. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  5147. void intel_crtc_load_lut(struct drm_crtc *crtc)
  5148. {
  5149. struct drm_device *dev = crtc->dev;
  5150. struct drm_i915_private *dev_priv = dev->dev_private;
  5151. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5152. int palreg = PALETTE(intel_crtc->pipe);
  5153. int i;
  5154. /* The clocks have to be on to load the palette. */
  5155. if (!crtc->enabled || !intel_crtc->active)
  5156. return;
  5157. /* use legacy palette for Ironlake */
  5158. if (HAS_PCH_SPLIT(dev))
  5159. palreg = LGC_PALETTE(intel_crtc->pipe);
  5160. for (i = 0; i < 256; i++) {
  5161. I915_WRITE(palreg + 4 * i,
  5162. (intel_crtc->lut_r[i] << 16) |
  5163. (intel_crtc->lut_g[i] << 8) |
  5164. intel_crtc->lut_b[i]);
  5165. }
  5166. }
  5167. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  5168. {
  5169. struct drm_device *dev = crtc->dev;
  5170. struct drm_i915_private *dev_priv = dev->dev_private;
  5171. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5172. bool visible = base != 0;
  5173. u32 cntl;
  5174. if (intel_crtc->cursor_visible == visible)
  5175. return;
  5176. cntl = I915_READ(_CURACNTR);
  5177. if (visible) {
  5178. /* On these chipsets we can only modify the base whilst
  5179. * the cursor is disabled.
  5180. */
  5181. I915_WRITE(_CURABASE, base);
  5182. cntl &= ~(CURSOR_FORMAT_MASK);
  5183. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  5184. cntl |= CURSOR_ENABLE |
  5185. CURSOR_GAMMA_ENABLE |
  5186. CURSOR_FORMAT_ARGB;
  5187. } else
  5188. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  5189. I915_WRITE(_CURACNTR, cntl);
  5190. intel_crtc->cursor_visible = visible;
  5191. }
  5192. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  5193. {
  5194. struct drm_device *dev = crtc->dev;
  5195. struct drm_i915_private *dev_priv = dev->dev_private;
  5196. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5197. int pipe = intel_crtc->pipe;
  5198. bool visible = base != 0;
  5199. if (intel_crtc->cursor_visible != visible) {
  5200. uint32_t cntl = I915_READ(CURCNTR(pipe));
  5201. if (base) {
  5202. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  5203. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  5204. cntl |= pipe << 28; /* Connect to correct pipe */
  5205. } else {
  5206. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  5207. cntl |= CURSOR_MODE_DISABLE;
  5208. }
  5209. I915_WRITE(CURCNTR(pipe), cntl);
  5210. intel_crtc->cursor_visible = visible;
  5211. }
  5212. /* and commit changes on next vblank */
  5213. I915_WRITE(CURBASE(pipe), base);
  5214. }
  5215. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  5216. {
  5217. struct drm_device *dev = crtc->dev;
  5218. struct drm_i915_private *dev_priv = dev->dev_private;
  5219. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5220. int pipe = intel_crtc->pipe;
  5221. bool visible = base != 0;
  5222. if (intel_crtc->cursor_visible != visible) {
  5223. uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
  5224. if (base) {
  5225. cntl &= ~CURSOR_MODE;
  5226. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  5227. } else {
  5228. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  5229. cntl |= CURSOR_MODE_DISABLE;
  5230. }
  5231. if (IS_HASWELL(dev))
  5232. cntl |= CURSOR_PIPE_CSC_ENABLE;
  5233. I915_WRITE(CURCNTR_IVB(pipe), cntl);
  5234. intel_crtc->cursor_visible = visible;
  5235. }
  5236. /* and commit changes on next vblank */
  5237. I915_WRITE(CURBASE_IVB(pipe), base);
  5238. }
  5239. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  5240. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  5241. bool on)
  5242. {
  5243. struct drm_device *dev = crtc->dev;
  5244. struct drm_i915_private *dev_priv = dev->dev_private;
  5245. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5246. int pipe = intel_crtc->pipe;
  5247. int x = intel_crtc->cursor_x;
  5248. int y = intel_crtc->cursor_y;
  5249. u32 base, pos;
  5250. bool visible;
  5251. pos = 0;
  5252. if (on && crtc->enabled && crtc->fb) {
  5253. base = intel_crtc->cursor_addr;
  5254. if (x > (int) crtc->fb->width)
  5255. base = 0;
  5256. if (y > (int) crtc->fb->height)
  5257. base = 0;
  5258. } else
  5259. base = 0;
  5260. if (x < 0) {
  5261. if (x + intel_crtc->cursor_width < 0)
  5262. base = 0;
  5263. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  5264. x = -x;
  5265. }
  5266. pos |= x << CURSOR_X_SHIFT;
  5267. if (y < 0) {
  5268. if (y + intel_crtc->cursor_height < 0)
  5269. base = 0;
  5270. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  5271. y = -y;
  5272. }
  5273. pos |= y << CURSOR_Y_SHIFT;
  5274. visible = base != 0;
  5275. if (!visible && !intel_crtc->cursor_visible)
  5276. return;
  5277. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  5278. I915_WRITE(CURPOS_IVB(pipe), pos);
  5279. ivb_update_cursor(crtc, base);
  5280. } else {
  5281. I915_WRITE(CURPOS(pipe), pos);
  5282. if (IS_845G(dev) || IS_I865G(dev))
  5283. i845_update_cursor(crtc, base);
  5284. else
  5285. i9xx_update_cursor(crtc, base);
  5286. }
  5287. }
  5288. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  5289. struct drm_file *file,
  5290. uint32_t handle,
  5291. uint32_t width, uint32_t height)
  5292. {
  5293. struct drm_device *dev = crtc->dev;
  5294. struct drm_i915_private *dev_priv = dev->dev_private;
  5295. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5296. struct drm_i915_gem_object *obj;
  5297. uint32_t addr;
  5298. int ret;
  5299. /* if we want to turn off the cursor ignore width and height */
  5300. if (!handle) {
  5301. DRM_DEBUG_KMS("cursor off\n");
  5302. addr = 0;
  5303. obj = NULL;
  5304. mutex_lock(&dev->struct_mutex);
  5305. goto finish;
  5306. }
  5307. /* Currently we only support 64x64 cursors */
  5308. if (width != 64 || height != 64) {
  5309. DRM_ERROR("we currently only support 64x64 cursors\n");
  5310. return -EINVAL;
  5311. }
  5312. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  5313. if (&obj->base == NULL)
  5314. return -ENOENT;
  5315. if (obj->base.size < width * height * 4) {
  5316. DRM_ERROR("buffer is to small\n");
  5317. ret = -ENOMEM;
  5318. goto fail;
  5319. }
  5320. /* we only need to pin inside GTT if cursor is non-phy */
  5321. mutex_lock(&dev->struct_mutex);
  5322. if (!dev_priv->info->cursor_needs_physical) {
  5323. unsigned alignment;
  5324. if (obj->tiling_mode) {
  5325. DRM_ERROR("cursor cannot be tiled\n");
  5326. ret = -EINVAL;
  5327. goto fail_locked;
  5328. }
  5329. /* Note that the w/a also requires 2 PTE of padding following
  5330. * the bo. We currently fill all unused PTE with the shadow
  5331. * page and so we should always have valid PTE following the
  5332. * cursor preventing the VT-d warning.
  5333. */
  5334. alignment = 0;
  5335. if (need_vtd_wa(dev))
  5336. alignment = 64*1024;
  5337. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  5338. if (ret) {
  5339. DRM_ERROR("failed to move cursor bo into the GTT\n");
  5340. goto fail_locked;
  5341. }
  5342. ret = i915_gem_object_put_fence(obj);
  5343. if (ret) {
  5344. DRM_ERROR("failed to release fence for cursor");
  5345. goto fail_unpin;
  5346. }
  5347. addr = obj->gtt_offset;
  5348. } else {
  5349. int align = IS_I830(dev) ? 16 * 1024 : 256;
  5350. ret = i915_gem_attach_phys_object(dev, obj,
  5351. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  5352. align);
  5353. if (ret) {
  5354. DRM_ERROR("failed to attach phys object\n");
  5355. goto fail_locked;
  5356. }
  5357. addr = obj->phys_obj->handle->busaddr;
  5358. }
  5359. if (IS_GEN2(dev))
  5360. I915_WRITE(CURSIZE, (height << 12) | width);
  5361. finish:
  5362. if (intel_crtc->cursor_bo) {
  5363. if (dev_priv->info->cursor_needs_physical) {
  5364. if (intel_crtc->cursor_bo != obj)
  5365. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  5366. } else
  5367. i915_gem_object_unpin(intel_crtc->cursor_bo);
  5368. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  5369. }
  5370. mutex_unlock(&dev->struct_mutex);
  5371. intel_crtc->cursor_addr = addr;
  5372. intel_crtc->cursor_bo = obj;
  5373. intel_crtc->cursor_width = width;
  5374. intel_crtc->cursor_height = height;
  5375. intel_crtc_update_cursor(crtc, true);
  5376. return 0;
  5377. fail_unpin:
  5378. i915_gem_object_unpin(obj);
  5379. fail_locked:
  5380. mutex_unlock(&dev->struct_mutex);
  5381. fail:
  5382. drm_gem_object_unreference_unlocked(&obj->base);
  5383. return ret;
  5384. }
  5385. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  5386. {
  5387. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5388. intel_crtc->cursor_x = x;
  5389. intel_crtc->cursor_y = y;
  5390. intel_crtc_update_cursor(crtc, true);
  5391. return 0;
  5392. }
  5393. /** Sets the color ramps on behalf of RandR */
  5394. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  5395. u16 blue, int regno)
  5396. {
  5397. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5398. intel_crtc->lut_r[regno] = red >> 8;
  5399. intel_crtc->lut_g[regno] = green >> 8;
  5400. intel_crtc->lut_b[regno] = blue >> 8;
  5401. }
  5402. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  5403. u16 *blue, int regno)
  5404. {
  5405. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5406. *red = intel_crtc->lut_r[regno] << 8;
  5407. *green = intel_crtc->lut_g[regno] << 8;
  5408. *blue = intel_crtc->lut_b[regno] << 8;
  5409. }
  5410. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  5411. u16 *blue, uint32_t start, uint32_t size)
  5412. {
  5413. int end = (start + size > 256) ? 256 : start + size, i;
  5414. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5415. for (i = start; i < end; i++) {
  5416. intel_crtc->lut_r[i] = red[i] >> 8;
  5417. intel_crtc->lut_g[i] = green[i] >> 8;
  5418. intel_crtc->lut_b[i] = blue[i] >> 8;
  5419. }
  5420. intel_crtc_load_lut(crtc);
  5421. }
  5422. /* VESA 640x480x72Hz mode to set on the pipe */
  5423. static struct drm_display_mode load_detect_mode = {
  5424. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  5425. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  5426. };
  5427. static struct drm_framebuffer *
  5428. intel_framebuffer_create(struct drm_device *dev,
  5429. struct drm_mode_fb_cmd2 *mode_cmd,
  5430. struct drm_i915_gem_object *obj)
  5431. {
  5432. struct intel_framebuffer *intel_fb;
  5433. int ret;
  5434. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5435. if (!intel_fb) {
  5436. drm_gem_object_unreference_unlocked(&obj->base);
  5437. return ERR_PTR(-ENOMEM);
  5438. }
  5439. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  5440. if (ret) {
  5441. drm_gem_object_unreference_unlocked(&obj->base);
  5442. kfree(intel_fb);
  5443. return ERR_PTR(ret);
  5444. }
  5445. return &intel_fb->base;
  5446. }
  5447. static u32
  5448. intel_framebuffer_pitch_for_width(int width, int bpp)
  5449. {
  5450. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  5451. return ALIGN(pitch, 64);
  5452. }
  5453. static u32
  5454. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  5455. {
  5456. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  5457. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  5458. }
  5459. static struct drm_framebuffer *
  5460. intel_framebuffer_create_for_mode(struct drm_device *dev,
  5461. struct drm_display_mode *mode,
  5462. int depth, int bpp)
  5463. {
  5464. struct drm_i915_gem_object *obj;
  5465. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  5466. obj = i915_gem_alloc_object(dev,
  5467. intel_framebuffer_size_for_mode(mode, bpp));
  5468. if (obj == NULL)
  5469. return ERR_PTR(-ENOMEM);
  5470. mode_cmd.width = mode->hdisplay;
  5471. mode_cmd.height = mode->vdisplay;
  5472. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  5473. bpp);
  5474. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  5475. return intel_framebuffer_create(dev, &mode_cmd, obj);
  5476. }
  5477. static struct drm_framebuffer *
  5478. mode_fits_in_fbdev(struct drm_device *dev,
  5479. struct drm_display_mode *mode)
  5480. {
  5481. struct drm_i915_private *dev_priv = dev->dev_private;
  5482. struct drm_i915_gem_object *obj;
  5483. struct drm_framebuffer *fb;
  5484. if (dev_priv->fbdev == NULL)
  5485. return NULL;
  5486. obj = dev_priv->fbdev->ifb.obj;
  5487. if (obj == NULL)
  5488. return NULL;
  5489. fb = &dev_priv->fbdev->ifb.base;
  5490. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  5491. fb->bits_per_pixel))
  5492. return NULL;
  5493. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  5494. return NULL;
  5495. return fb;
  5496. }
  5497. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  5498. struct drm_display_mode *mode,
  5499. struct intel_load_detect_pipe *old)
  5500. {
  5501. struct intel_crtc *intel_crtc;
  5502. struct intel_encoder *intel_encoder =
  5503. intel_attached_encoder(connector);
  5504. struct drm_crtc *possible_crtc;
  5505. struct drm_encoder *encoder = &intel_encoder->base;
  5506. struct drm_crtc *crtc = NULL;
  5507. struct drm_device *dev = encoder->dev;
  5508. struct drm_framebuffer *fb;
  5509. int i = -1;
  5510. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5511. connector->base.id, drm_get_connector_name(connector),
  5512. encoder->base.id, drm_get_encoder_name(encoder));
  5513. /*
  5514. * Algorithm gets a little messy:
  5515. *
  5516. * - if the connector already has an assigned crtc, use it (but make
  5517. * sure it's on first)
  5518. *
  5519. * - try to find the first unused crtc that can drive this connector,
  5520. * and use that if we find one
  5521. */
  5522. /* See if we already have a CRTC for this connector */
  5523. if (encoder->crtc) {
  5524. crtc = encoder->crtc;
  5525. mutex_lock(&crtc->mutex);
  5526. old->dpms_mode = connector->dpms;
  5527. old->load_detect_temp = false;
  5528. /* Make sure the crtc and connector are running */
  5529. if (connector->dpms != DRM_MODE_DPMS_ON)
  5530. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  5531. return true;
  5532. }
  5533. /* Find an unused one (if possible) */
  5534. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  5535. i++;
  5536. if (!(encoder->possible_crtcs & (1 << i)))
  5537. continue;
  5538. if (!possible_crtc->enabled) {
  5539. crtc = possible_crtc;
  5540. break;
  5541. }
  5542. }
  5543. /*
  5544. * If we didn't find an unused CRTC, don't use any.
  5545. */
  5546. if (!crtc) {
  5547. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  5548. return false;
  5549. }
  5550. mutex_lock(&crtc->mutex);
  5551. intel_encoder->new_crtc = to_intel_crtc(crtc);
  5552. to_intel_connector(connector)->new_encoder = intel_encoder;
  5553. intel_crtc = to_intel_crtc(crtc);
  5554. old->dpms_mode = connector->dpms;
  5555. old->load_detect_temp = true;
  5556. old->release_fb = NULL;
  5557. if (!mode)
  5558. mode = &load_detect_mode;
  5559. /* We need a framebuffer large enough to accommodate all accesses
  5560. * that the plane may generate whilst we perform load detection.
  5561. * We can not rely on the fbcon either being present (we get called
  5562. * during its initialisation to detect all boot displays, or it may
  5563. * not even exist) or that it is large enough to satisfy the
  5564. * requested mode.
  5565. */
  5566. fb = mode_fits_in_fbdev(dev, mode);
  5567. if (fb == NULL) {
  5568. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  5569. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  5570. old->release_fb = fb;
  5571. } else
  5572. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  5573. if (IS_ERR(fb)) {
  5574. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  5575. mutex_unlock(&crtc->mutex);
  5576. return false;
  5577. }
  5578. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  5579. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  5580. if (old->release_fb)
  5581. old->release_fb->funcs->destroy(old->release_fb);
  5582. mutex_unlock(&crtc->mutex);
  5583. return false;
  5584. }
  5585. /* let the connector get through one full cycle before testing */
  5586. intel_wait_for_vblank(dev, intel_crtc->pipe);
  5587. return true;
  5588. }
  5589. void intel_release_load_detect_pipe(struct drm_connector *connector,
  5590. struct intel_load_detect_pipe *old)
  5591. {
  5592. struct intel_encoder *intel_encoder =
  5593. intel_attached_encoder(connector);
  5594. struct drm_encoder *encoder = &intel_encoder->base;
  5595. struct drm_crtc *crtc = encoder->crtc;
  5596. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5597. connector->base.id, drm_get_connector_name(connector),
  5598. encoder->base.id, drm_get_encoder_name(encoder));
  5599. if (old->load_detect_temp) {
  5600. to_intel_connector(connector)->new_encoder = NULL;
  5601. intel_encoder->new_crtc = NULL;
  5602. intel_set_mode(crtc, NULL, 0, 0, NULL);
  5603. if (old->release_fb) {
  5604. drm_framebuffer_unregister_private(old->release_fb);
  5605. drm_framebuffer_unreference(old->release_fb);
  5606. }
  5607. mutex_unlock(&crtc->mutex);
  5608. return;
  5609. }
  5610. /* Switch crtc and encoder back off if necessary */
  5611. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  5612. connector->funcs->dpms(connector, old->dpms_mode);
  5613. mutex_unlock(&crtc->mutex);
  5614. }
  5615. /* Returns the clock of the currently programmed mode of the given pipe. */
  5616. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  5617. {
  5618. struct drm_i915_private *dev_priv = dev->dev_private;
  5619. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5620. int pipe = intel_crtc->pipe;
  5621. u32 dpll = I915_READ(DPLL(pipe));
  5622. u32 fp;
  5623. intel_clock_t clock;
  5624. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  5625. fp = I915_READ(FP0(pipe));
  5626. else
  5627. fp = I915_READ(FP1(pipe));
  5628. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  5629. if (IS_PINEVIEW(dev)) {
  5630. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  5631. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  5632. } else {
  5633. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  5634. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  5635. }
  5636. if (!IS_GEN2(dev)) {
  5637. if (IS_PINEVIEW(dev))
  5638. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  5639. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  5640. else
  5641. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  5642. DPLL_FPA01_P1_POST_DIV_SHIFT);
  5643. switch (dpll & DPLL_MODE_MASK) {
  5644. case DPLLB_MODE_DAC_SERIAL:
  5645. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  5646. 5 : 10;
  5647. break;
  5648. case DPLLB_MODE_LVDS:
  5649. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  5650. 7 : 14;
  5651. break;
  5652. default:
  5653. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  5654. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  5655. return 0;
  5656. }
  5657. /* XXX: Handle the 100Mhz refclk */
  5658. intel_clock(dev, 96000, &clock);
  5659. } else {
  5660. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  5661. if (is_lvds) {
  5662. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  5663. DPLL_FPA01_P1_POST_DIV_SHIFT);
  5664. clock.p2 = 14;
  5665. if ((dpll & PLL_REF_INPUT_MASK) ==
  5666. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  5667. /* XXX: might not be 66MHz */
  5668. intel_clock(dev, 66000, &clock);
  5669. } else
  5670. intel_clock(dev, 48000, &clock);
  5671. } else {
  5672. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  5673. clock.p1 = 2;
  5674. else {
  5675. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  5676. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  5677. }
  5678. if (dpll & PLL_P2_DIVIDE_BY_4)
  5679. clock.p2 = 4;
  5680. else
  5681. clock.p2 = 2;
  5682. intel_clock(dev, 48000, &clock);
  5683. }
  5684. }
  5685. /* XXX: It would be nice to validate the clocks, but we can't reuse
  5686. * i830PllIsValid() because it relies on the xf86_config connector
  5687. * configuration being accurate, which it isn't necessarily.
  5688. */
  5689. return clock.dot;
  5690. }
  5691. /** Returns the currently programmed mode of the given pipe. */
  5692. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  5693. struct drm_crtc *crtc)
  5694. {
  5695. struct drm_i915_private *dev_priv = dev->dev_private;
  5696. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5697. enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
  5698. struct drm_display_mode *mode;
  5699. int htot = I915_READ(HTOTAL(cpu_transcoder));
  5700. int hsync = I915_READ(HSYNC(cpu_transcoder));
  5701. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  5702. int vsync = I915_READ(VSYNC(cpu_transcoder));
  5703. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  5704. if (!mode)
  5705. return NULL;
  5706. mode->clock = intel_crtc_clock_get(dev, crtc);
  5707. mode->hdisplay = (htot & 0xffff) + 1;
  5708. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  5709. mode->hsync_start = (hsync & 0xffff) + 1;
  5710. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  5711. mode->vdisplay = (vtot & 0xffff) + 1;
  5712. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  5713. mode->vsync_start = (vsync & 0xffff) + 1;
  5714. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  5715. drm_mode_set_name(mode);
  5716. return mode;
  5717. }
  5718. static void intel_increase_pllclock(struct drm_crtc *crtc)
  5719. {
  5720. struct drm_device *dev = crtc->dev;
  5721. drm_i915_private_t *dev_priv = dev->dev_private;
  5722. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5723. int pipe = intel_crtc->pipe;
  5724. int dpll_reg = DPLL(pipe);
  5725. int dpll;
  5726. if (HAS_PCH_SPLIT(dev))
  5727. return;
  5728. if (!dev_priv->lvds_downclock_avail)
  5729. return;
  5730. dpll = I915_READ(dpll_reg);
  5731. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  5732. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  5733. assert_panel_unlocked(dev_priv, pipe);
  5734. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  5735. I915_WRITE(dpll_reg, dpll);
  5736. intel_wait_for_vblank(dev, pipe);
  5737. dpll = I915_READ(dpll_reg);
  5738. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  5739. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  5740. }
  5741. }
  5742. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  5743. {
  5744. struct drm_device *dev = crtc->dev;
  5745. drm_i915_private_t *dev_priv = dev->dev_private;
  5746. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5747. if (HAS_PCH_SPLIT(dev))
  5748. return;
  5749. if (!dev_priv->lvds_downclock_avail)
  5750. return;
  5751. /*
  5752. * Since this is called by a timer, we should never get here in
  5753. * the manual case.
  5754. */
  5755. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  5756. int pipe = intel_crtc->pipe;
  5757. int dpll_reg = DPLL(pipe);
  5758. int dpll;
  5759. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  5760. assert_panel_unlocked(dev_priv, pipe);
  5761. dpll = I915_READ(dpll_reg);
  5762. dpll |= DISPLAY_RATE_SELECT_FPA1;
  5763. I915_WRITE(dpll_reg, dpll);
  5764. intel_wait_for_vblank(dev, pipe);
  5765. dpll = I915_READ(dpll_reg);
  5766. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  5767. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  5768. }
  5769. }
  5770. void intel_mark_busy(struct drm_device *dev)
  5771. {
  5772. i915_update_gfx_val(dev->dev_private);
  5773. }
  5774. void intel_mark_idle(struct drm_device *dev)
  5775. {
  5776. struct drm_crtc *crtc;
  5777. if (!i915_powersave)
  5778. return;
  5779. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5780. if (!crtc->fb)
  5781. continue;
  5782. intel_decrease_pllclock(crtc);
  5783. }
  5784. }
  5785. void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
  5786. {
  5787. struct drm_device *dev = obj->base.dev;
  5788. struct drm_crtc *crtc;
  5789. if (!i915_powersave)
  5790. return;
  5791. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5792. if (!crtc->fb)
  5793. continue;
  5794. if (to_intel_framebuffer(crtc->fb)->obj == obj)
  5795. intel_increase_pllclock(crtc);
  5796. }
  5797. }
  5798. static void intel_crtc_destroy(struct drm_crtc *crtc)
  5799. {
  5800. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5801. struct drm_device *dev = crtc->dev;
  5802. struct intel_unpin_work *work;
  5803. unsigned long flags;
  5804. spin_lock_irqsave(&dev->event_lock, flags);
  5805. work = intel_crtc->unpin_work;
  5806. intel_crtc->unpin_work = NULL;
  5807. spin_unlock_irqrestore(&dev->event_lock, flags);
  5808. if (work) {
  5809. cancel_work_sync(&work->work);
  5810. kfree(work);
  5811. }
  5812. drm_crtc_cleanup(crtc);
  5813. kfree(intel_crtc);
  5814. }
  5815. static void intel_unpin_work_fn(struct work_struct *__work)
  5816. {
  5817. struct intel_unpin_work *work =
  5818. container_of(__work, struct intel_unpin_work, work);
  5819. struct drm_device *dev = work->crtc->dev;
  5820. mutex_lock(&dev->struct_mutex);
  5821. intel_unpin_fb_obj(work->old_fb_obj);
  5822. drm_gem_object_unreference(&work->pending_flip_obj->base);
  5823. drm_gem_object_unreference(&work->old_fb_obj->base);
  5824. intel_update_fbc(dev);
  5825. mutex_unlock(&dev->struct_mutex);
  5826. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  5827. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  5828. kfree(work);
  5829. }
  5830. static void do_intel_finish_page_flip(struct drm_device *dev,
  5831. struct drm_crtc *crtc)
  5832. {
  5833. drm_i915_private_t *dev_priv = dev->dev_private;
  5834. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5835. struct intel_unpin_work *work;
  5836. unsigned long flags;
  5837. /* Ignore early vblank irqs */
  5838. if (intel_crtc == NULL)
  5839. return;
  5840. spin_lock_irqsave(&dev->event_lock, flags);
  5841. work = intel_crtc->unpin_work;
  5842. /* Ensure we don't miss a work->pending update ... */
  5843. smp_rmb();
  5844. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  5845. spin_unlock_irqrestore(&dev->event_lock, flags);
  5846. return;
  5847. }
  5848. /* and that the unpin work is consistent wrt ->pending. */
  5849. smp_rmb();
  5850. intel_crtc->unpin_work = NULL;
  5851. if (work->event)
  5852. drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
  5853. drm_vblank_put(dev, intel_crtc->pipe);
  5854. spin_unlock_irqrestore(&dev->event_lock, flags);
  5855. wake_up_all(&dev_priv->pending_flip_queue);
  5856. queue_work(dev_priv->wq, &work->work);
  5857. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  5858. }
  5859. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  5860. {
  5861. drm_i915_private_t *dev_priv = dev->dev_private;
  5862. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  5863. do_intel_finish_page_flip(dev, crtc);
  5864. }
  5865. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  5866. {
  5867. drm_i915_private_t *dev_priv = dev->dev_private;
  5868. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  5869. do_intel_finish_page_flip(dev, crtc);
  5870. }
  5871. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  5872. {
  5873. drm_i915_private_t *dev_priv = dev->dev_private;
  5874. struct intel_crtc *intel_crtc =
  5875. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  5876. unsigned long flags;
  5877. /* NB: An MMIO update of the plane base pointer will also
  5878. * generate a page-flip completion irq, i.e. every modeset
  5879. * is also accompanied by a spurious intel_prepare_page_flip().
  5880. */
  5881. spin_lock_irqsave(&dev->event_lock, flags);
  5882. if (intel_crtc->unpin_work)
  5883. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  5884. spin_unlock_irqrestore(&dev->event_lock, flags);
  5885. }
  5886. inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  5887. {
  5888. /* Ensure that the work item is consistent when activating it ... */
  5889. smp_wmb();
  5890. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  5891. /* and that it is marked active as soon as the irq could fire. */
  5892. smp_wmb();
  5893. }
  5894. static int intel_gen2_queue_flip(struct drm_device *dev,
  5895. struct drm_crtc *crtc,
  5896. struct drm_framebuffer *fb,
  5897. struct drm_i915_gem_object *obj)
  5898. {
  5899. struct drm_i915_private *dev_priv = dev->dev_private;
  5900. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5901. u32 flip_mask;
  5902. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  5903. int ret;
  5904. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5905. if (ret)
  5906. goto err;
  5907. ret = intel_ring_begin(ring, 6);
  5908. if (ret)
  5909. goto err_unpin;
  5910. /* Can't queue multiple flips, so wait for the previous
  5911. * one to finish before executing the next.
  5912. */
  5913. if (intel_crtc->plane)
  5914. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  5915. else
  5916. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  5917. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  5918. intel_ring_emit(ring, MI_NOOP);
  5919. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  5920. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5921. intel_ring_emit(ring, fb->pitches[0]);
  5922. intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
  5923. intel_ring_emit(ring, 0); /* aux display base address, unused */
  5924. intel_mark_page_flip_active(intel_crtc);
  5925. intel_ring_advance(ring);
  5926. return 0;
  5927. err_unpin:
  5928. intel_unpin_fb_obj(obj);
  5929. err:
  5930. return ret;
  5931. }
  5932. static int intel_gen3_queue_flip(struct drm_device *dev,
  5933. struct drm_crtc *crtc,
  5934. struct drm_framebuffer *fb,
  5935. struct drm_i915_gem_object *obj)
  5936. {
  5937. struct drm_i915_private *dev_priv = dev->dev_private;
  5938. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5939. u32 flip_mask;
  5940. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  5941. int ret;
  5942. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5943. if (ret)
  5944. goto err;
  5945. ret = intel_ring_begin(ring, 6);
  5946. if (ret)
  5947. goto err_unpin;
  5948. if (intel_crtc->plane)
  5949. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  5950. else
  5951. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  5952. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  5953. intel_ring_emit(ring, MI_NOOP);
  5954. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  5955. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5956. intel_ring_emit(ring, fb->pitches[0]);
  5957. intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
  5958. intel_ring_emit(ring, MI_NOOP);
  5959. intel_mark_page_flip_active(intel_crtc);
  5960. intel_ring_advance(ring);
  5961. return 0;
  5962. err_unpin:
  5963. intel_unpin_fb_obj(obj);
  5964. err:
  5965. return ret;
  5966. }
  5967. static int intel_gen4_queue_flip(struct drm_device *dev,
  5968. struct drm_crtc *crtc,
  5969. struct drm_framebuffer *fb,
  5970. struct drm_i915_gem_object *obj)
  5971. {
  5972. struct drm_i915_private *dev_priv = dev->dev_private;
  5973. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5974. uint32_t pf, pipesrc;
  5975. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  5976. int ret;
  5977. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5978. if (ret)
  5979. goto err;
  5980. ret = intel_ring_begin(ring, 4);
  5981. if (ret)
  5982. goto err_unpin;
  5983. /* i965+ uses the linear or tiled offsets from the
  5984. * Display Registers (which do not change across a page-flip)
  5985. * so we need only reprogram the base address.
  5986. */
  5987. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  5988. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5989. intel_ring_emit(ring, fb->pitches[0]);
  5990. intel_ring_emit(ring,
  5991. (obj->gtt_offset + intel_crtc->dspaddr_offset) |
  5992. obj->tiling_mode);
  5993. /* XXX Enabling the panel-fitter across page-flip is so far
  5994. * untested on non-native modes, so ignore it for now.
  5995. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  5996. */
  5997. pf = 0;
  5998. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  5999. intel_ring_emit(ring, pf | pipesrc);
  6000. intel_mark_page_flip_active(intel_crtc);
  6001. intel_ring_advance(ring);
  6002. return 0;
  6003. err_unpin:
  6004. intel_unpin_fb_obj(obj);
  6005. err:
  6006. return ret;
  6007. }
  6008. static int intel_gen6_queue_flip(struct drm_device *dev,
  6009. struct drm_crtc *crtc,
  6010. struct drm_framebuffer *fb,
  6011. struct drm_i915_gem_object *obj)
  6012. {
  6013. struct drm_i915_private *dev_priv = dev->dev_private;
  6014. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6015. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  6016. uint32_t pf, pipesrc;
  6017. int ret;
  6018. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6019. if (ret)
  6020. goto err;
  6021. ret = intel_ring_begin(ring, 4);
  6022. if (ret)
  6023. goto err_unpin;
  6024. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  6025. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6026. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  6027. intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
  6028. /* Contrary to the suggestions in the documentation,
  6029. * "Enable Panel Fitter" does not seem to be required when page
  6030. * flipping with a non-native mode, and worse causes a normal
  6031. * modeset to fail.
  6032. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  6033. */
  6034. pf = 0;
  6035. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  6036. intel_ring_emit(ring, pf | pipesrc);
  6037. intel_mark_page_flip_active(intel_crtc);
  6038. intel_ring_advance(ring);
  6039. return 0;
  6040. err_unpin:
  6041. intel_unpin_fb_obj(obj);
  6042. err:
  6043. return ret;
  6044. }
  6045. /*
  6046. * On gen7 we currently use the blit ring because (in early silicon at least)
  6047. * the render ring doesn't give us interrpts for page flip completion, which
  6048. * means clients will hang after the first flip is queued. Fortunately the
  6049. * blit ring generates interrupts properly, so use it instead.
  6050. */
  6051. static int intel_gen7_queue_flip(struct drm_device *dev,
  6052. struct drm_crtc *crtc,
  6053. struct drm_framebuffer *fb,
  6054. struct drm_i915_gem_object *obj)
  6055. {
  6056. struct drm_i915_private *dev_priv = dev->dev_private;
  6057. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6058. struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
  6059. uint32_t plane_bit = 0;
  6060. int ret;
  6061. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6062. if (ret)
  6063. goto err;
  6064. switch(intel_crtc->plane) {
  6065. case PLANE_A:
  6066. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  6067. break;
  6068. case PLANE_B:
  6069. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  6070. break;
  6071. case PLANE_C:
  6072. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  6073. break;
  6074. default:
  6075. WARN_ONCE(1, "unknown plane in flip command\n");
  6076. ret = -ENODEV;
  6077. goto err_unpin;
  6078. }
  6079. ret = intel_ring_begin(ring, 4);
  6080. if (ret)
  6081. goto err_unpin;
  6082. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  6083. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  6084. intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
  6085. intel_ring_emit(ring, (MI_NOOP));
  6086. intel_mark_page_flip_active(intel_crtc);
  6087. intel_ring_advance(ring);
  6088. return 0;
  6089. err_unpin:
  6090. intel_unpin_fb_obj(obj);
  6091. err:
  6092. return ret;
  6093. }
  6094. static int intel_default_queue_flip(struct drm_device *dev,
  6095. struct drm_crtc *crtc,
  6096. struct drm_framebuffer *fb,
  6097. struct drm_i915_gem_object *obj)
  6098. {
  6099. return -ENODEV;
  6100. }
  6101. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  6102. struct drm_framebuffer *fb,
  6103. struct drm_pending_vblank_event *event)
  6104. {
  6105. struct drm_device *dev = crtc->dev;
  6106. struct drm_i915_private *dev_priv = dev->dev_private;
  6107. struct drm_framebuffer *old_fb = crtc->fb;
  6108. struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
  6109. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6110. struct intel_unpin_work *work;
  6111. unsigned long flags;
  6112. int ret;
  6113. /* Can't change pixel format via MI display flips. */
  6114. if (fb->pixel_format != crtc->fb->pixel_format)
  6115. return -EINVAL;
  6116. /*
  6117. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  6118. * Note that pitch changes could also affect these register.
  6119. */
  6120. if (INTEL_INFO(dev)->gen > 3 &&
  6121. (fb->offsets[0] != crtc->fb->offsets[0] ||
  6122. fb->pitches[0] != crtc->fb->pitches[0]))
  6123. return -EINVAL;
  6124. work = kzalloc(sizeof *work, GFP_KERNEL);
  6125. if (work == NULL)
  6126. return -ENOMEM;
  6127. work->event = event;
  6128. work->crtc = crtc;
  6129. work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
  6130. INIT_WORK(&work->work, intel_unpin_work_fn);
  6131. ret = drm_vblank_get(dev, intel_crtc->pipe);
  6132. if (ret)
  6133. goto free_work;
  6134. /* We borrow the event spin lock for protecting unpin_work */
  6135. spin_lock_irqsave(&dev->event_lock, flags);
  6136. if (intel_crtc->unpin_work) {
  6137. spin_unlock_irqrestore(&dev->event_lock, flags);
  6138. kfree(work);
  6139. drm_vblank_put(dev, intel_crtc->pipe);
  6140. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  6141. return -EBUSY;
  6142. }
  6143. intel_crtc->unpin_work = work;
  6144. spin_unlock_irqrestore(&dev->event_lock, flags);
  6145. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  6146. flush_workqueue(dev_priv->wq);
  6147. ret = i915_mutex_lock_interruptible(dev);
  6148. if (ret)
  6149. goto cleanup;
  6150. /* Reference the objects for the scheduled work. */
  6151. drm_gem_object_reference(&work->old_fb_obj->base);
  6152. drm_gem_object_reference(&obj->base);
  6153. crtc->fb = fb;
  6154. work->pending_flip_obj = obj;
  6155. work->enable_stall_check = true;
  6156. atomic_inc(&intel_crtc->unpin_work_count);
  6157. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  6158. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
  6159. if (ret)
  6160. goto cleanup_pending;
  6161. intel_disable_fbc(dev);
  6162. intel_mark_fb_busy(obj);
  6163. mutex_unlock(&dev->struct_mutex);
  6164. trace_i915_flip_request(intel_crtc->plane, obj);
  6165. return 0;
  6166. cleanup_pending:
  6167. atomic_dec(&intel_crtc->unpin_work_count);
  6168. crtc->fb = old_fb;
  6169. drm_gem_object_unreference(&work->old_fb_obj->base);
  6170. drm_gem_object_unreference(&obj->base);
  6171. mutex_unlock(&dev->struct_mutex);
  6172. cleanup:
  6173. spin_lock_irqsave(&dev->event_lock, flags);
  6174. intel_crtc->unpin_work = NULL;
  6175. spin_unlock_irqrestore(&dev->event_lock, flags);
  6176. drm_vblank_put(dev, intel_crtc->pipe);
  6177. free_work:
  6178. kfree(work);
  6179. return ret;
  6180. }
  6181. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  6182. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  6183. .load_lut = intel_crtc_load_lut,
  6184. };
  6185. bool intel_encoder_check_is_cloned(struct intel_encoder *encoder)
  6186. {
  6187. struct intel_encoder *other_encoder;
  6188. struct drm_crtc *crtc = &encoder->new_crtc->base;
  6189. if (WARN_ON(!crtc))
  6190. return false;
  6191. list_for_each_entry(other_encoder,
  6192. &crtc->dev->mode_config.encoder_list,
  6193. base.head) {
  6194. if (&other_encoder->new_crtc->base != crtc ||
  6195. encoder == other_encoder)
  6196. continue;
  6197. else
  6198. return true;
  6199. }
  6200. return false;
  6201. }
  6202. static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
  6203. struct drm_crtc *crtc)
  6204. {
  6205. struct drm_device *dev;
  6206. struct drm_crtc *tmp;
  6207. int crtc_mask = 1;
  6208. WARN(!crtc, "checking null crtc?\n");
  6209. dev = crtc->dev;
  6210. list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
  6211. if (tmp == crtc)
  6212. break;
  6213. crtc_mask <<= 1;
  6214. }
  6215. if (encoder->possible_crtcs & crtc_mask)
  6216. return true;
  6217. return false;
  6218. }
  6219. /**
  6220. * intel_modeset_update_staged_output_state
  6221. *
  6222. * Updates the staged output configuration state, e.g. after we've read out the
  6223. * current hw state.
  6224. */
  6225. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  6226. {
  6227. struct intel_encoder *encoder;
  6228. struct intel_connector *connector;
  6229. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6230. base.head) {
  6231. connector->new_encoder =
  6232. to_intel_encoder(connector->base.encoder);
  6233. }
  6234. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6235. base.head) {
  6236. encoder->new_crtc =
  6237. to_intel_crtc(encoder->base.crtc);
  6238. }
  6239. }
  6240. /**
  6241. * intel_modeset_commit_output_state
  6242. *
  6243. * This function copies the stage display pipe configuration to the real one.
  6244. */
  6245. static void intel_modeset_commit_output_state(struct drm_device *dev)
  6246. {
  6247. struct intel_encoder *encoder;
  6248. struct intel_connector *connector;
  6249. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6250. base.head) {
  6251. connector->base.encoder = &connector->new_encoder->base;
  6252. }
  6253. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6254. base.head) {
  6255. encoder->base.crtc = &encoder->new_crtc->base;
  6256. }
  6257. }
  6258. static int
  6259. pipe_config_set_bpp(struct drm_crtc *crtc,
  6260. struct drm_framebuffer *fb,
  6261. struct intel_crtc_config *pipe_config)
  6262. {
  6263. struct drm_device *dev = crtc->dev;
  6264. struct drm_connector *connector;
  6265. int bpp;
  6266. switch (fb->pixel_format) {
  6267. case DRM_FORMAT_C8:
  6268. bpp = 8*3; /* since we go through a colormap */
  6269. break;
  6270. case DRM_FORMAT_XRGB1555:
  6271. case DRM_FORMAT_ARGB1555:
  6272. /* checked in intel_framebuffer_init already */
  6273. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  6274. return -EINVAL;
  6275. case DRM_FORMAT_RGB565:
  6276. bpp = 6*3; /* min is 18bpp */
  6277. break;
  6278. case DRM_FORMAT_XBGR8888:
  6279. case DRM_FORMAT_ABGR8888:
  6280. /* checked in intel_framebuffer_init already */
  6281. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  6282. return -EINVAL;
  6283. case DRM_FORMAT_XRGB8888:
  6284. case DRM_FORMAT_ARGB8888:
  6285. bpp = 8*3;
  6286. break;
  6287. case DRM_FORMAT_XRGB2101010:
  6288. case DRM_FORMAT_ARGB2101010:
  6289. case DRM_FORMAT_XBGR2101010:
  6290. case DRM_FORMAT_ABGR2101010:
  6291. /* checked in intel_framebuffer_init already */
  6292. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  6293. return -EINVAL;
  6294. bpp = 10*3;
  6295. break;
  6296. /* TODO: gen4+ supports 16 bpc floating point, too. */
  6297. default:
  6298. DRM_DEBUG_KMS("unsupported depth\n");
  6299. return -EINVAL;
  6300. }
  6301. pipe_config->pipe_bpp = bpp;
  6302. /* Clamp display bpp to EDID value */
  6303. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6304. head) {
  6305. if (connector->encoder && connector->encoder->crtc != crtc)
  6306. continue;
  6307. /* Don't use an invalid EDID bpc value */
  6308. if (connector->display_info.bpc &&
  6309. connector->display_info.bpc * 3 < bpp) {
  6310. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  6311. bpp, connector->display_info.bpc*3);
  6312. pipe_config->pipe_bpp = connector->display_info.bpc*3;
  6313. }
  6314. }
  6315. return bpp;
  6316. }
  6317. static struct intel_crtc_config *
  6318. intel_modeset_pipe_config(struct drm_crtc *crtc,
  6319. struct drm_framebuffer *fb,
  6320. struct drm_display_mode *mode)
  6321. {
  6322. struct drm_device *dev = crtc->dev;
  6323. struct drm_encoder_helper_funcs *encoder_funcs;
  6324. struct intel_encoder *encoder;
  6325. struct intel_crtc_config *pipe_config;
  6326. int plane_bpp;
  6327. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6328. if (!pipe_config)
  6329. return ERR_PTR(-ENOMEM);
  6330. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  6331. drm_mode_copy(&pipe_config->requested_mode, mode);
  6332. plane_bpp = pipe_config_set_bpp(crtc, fb, pipe_config);
  6333. if (plane_bpp < 0)
  6334. goto fail;
  6335. /* Pass our mode to the connectors and the CRTC to give them a chance to
  6336. * adjust it according to limitations or connector properties, and also
  6337. * a chance to reject the mode entirely.
  6338. */
  6339. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6340. base.head) {
  6341. if (&encoder->new_crtc->base != crtc)
  6342. continue;
  6343. if (encoder->compute_config) {
  6344. if (!(encoder->compute_config(encoder, pipe_config))) {
  6345. DRM_DEBUG_KMS("Encoder config failure\n");
  6346. goto fail;
  6347. }
  6348. continue;
  6349. }
  6350. encoder_funcs = encoder->base.helper_private;
  6351. if (!(encoder_funcs->mode_fixup(&encoder->base,
  6352. &pipe_config->requested_mode,
  6353. &pipe_config->adjusted_mode))) {
  6354. DRM_DEBUG_KMS("Encoder fixup failed\n");
  6355. goto fail;
  6356. }
  6357. }
  6358. if (!(intel_crtc_compute_config(crtc, pipe_config))) {
  6359. DRM_DEBUG_KMS("CRTC fixup failed\n");
  6360. goto fail;
  6361. }
  6362. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  6363. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  6364. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  6365. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  6366. return pipe_config;
  6367. fail:
  6368. kfree(pipe_config);
  6369. return ERR_PTR(-EINVAL);
  6370. }
  6371. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  6372. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  6373. static void
  6374. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  6375. unsigned *prepare_pipes, unsigned *disable_pipes)
  6376. {
  6377. struct intel_crtc *intel_crtc;
  6378. struct drm_device *dev = crtc->dev;
  6379. struct intel_encoder *encoder;
  6380. struct intel_connector *connector;
  6381. struct drm_crtc *tmp_crtc;
  6382. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  6383. /* Check which crtcs have changed outputs connected to them, these need
  6384. * to be part of the prepare_pipes mask. We don't (yet) support global
  6385. * modeset across multiple crtcs, so modeset_pipes will only have one
  6386. * bit set at most. */
  6387. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6388. base.head) {
  6389. if (connector->base.encoder == &connector->new_encoder->base)
  6390. continue;
  6391. if (connector->base.encoder) {
  6392. tmp_crtc = connector->base.encoder->crtc;
  6393. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  6394. }
  6395. if (connector->new_encoder)
  6396. *prepare_pipes |=
  6397. 1 << connector->new_encoder->new_crtc->pipe;
  6398. }
  6399. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6400. base.head) {
  6401. if (encoder->base.crtc == &encoder->new_crtc->base)
  6402. continue;
  6403. if (encoder->base.crtc) {
  6404. tmp_crtc = encoder->base.crtc;
  6405. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  6406. }
  6407. if (encoder->new_crtc)
  6408. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  6409. }
  6410. /* Check for any pipes that will be fully disabled ... */
  6411. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  6412. base.head) {
  6413. bool used = false;
  6414. /* Don't try to disable disabled crtcs. */
  6415. if (!intel_crtc->base.enabled)
  6416. continue;
  6417. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6418. base.head) {
  6419. if (encoder->new_crtc == intel_crtc)
  6420. used = true;
  6421. }
  6422. if (!used)
  6423. *disable_pipes |= 1 << intel_crtc->pipe;
  6424. }
  6425. /* set_mode is also used to update properties on life display pipes. */
  6426. intel_crtc = to_intel_crtc(crtc);
  6427. if (crtc->enabled)
  6428. *prepare_pipes |= 1 << intel_crtc->pipe;
  6429. /* We only support modeset on one single crtc, hence we need to do that
  6430. * only for the passed in crtc iff we change anything else than just
  6431. * disable crtcs.
  6432. *
  6433. * This is actually not true, to be fully compatible with the old crtc
  6434. * helper we automatically disable _any_ output (i.e. doesn't need to be
  6435. * connected to the crtc we're modesetting on) if it's disconnected.
  6436. * Which is a rather nutty api (since changed the output configuration
  6437. * without userspace's explicit request can lead to confusion), but
  6438. * alas. Hence we currently need to modeset on all pipes we prepare. */
  6439. if (*prepare_pipes)
  6440. *modeset_pipes = *prepare_pipes;
  6441. /* ... and mask these out. */
  6442. *modeset_pipes &= ~(*disable_pipes);
  6443. *prepare_pipes &= ~(*disable_pipes);
  6444. }
  6445. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  6446. {
  6447. struct drm_encoder *encoder;
  6448. struct drm_device *dev = crtc->dev;
  6449. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  6450. if (encoder->crtc == crtc)
  6451. return true;
  6452. return false;
  6453. }
  6454. static void
  6455. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  6456. {
  6457. struct intel_encoder *intel_encoder;
  6458. struct intel_crtc *intel_crtc;
  6459. struct drm_connector *connector;
  6460. list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
  6461. base.head) {
  6462. if (!intel_encoder->base.crtc)
  6463. continue;
  6464. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  6465. if (prepare_pipes & (1 << intel_crtc->pipe))
  6466. intel_encoder->connectors_active = false;
  6467. }
  6468. intel_modeset_commit_output_state(dev);
  6469. /* Update computed state. */
  6470. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  6471. base.head) {
  6472. intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
  6473. }
  6474. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  6475. if (!connector->encoder || !connector->encoder->crtc)
  6476. continue;
  6477. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  6478. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  6479. struct drm_property *dpms_property =
  6480. dev->mode_config.dpms_property;
  6481. connector->dpms = DRM_MODE_DPMS_ON;
  6482. drm_object_property_set_value(&connector->base,
  6483. dpms_property,
  6484. DRM_MODE_DPMS_ON);
  6485. intel_encoder = to_intel_encoder(connector->encoder);
  6486. intel_encoder->connectors_active = true;
  6487. }
  6488. }
  6489. }
  6490. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  6491. list_for_each_entry((intel_crtc), \
  6492. &(dev)->mode_config.crtc_list, \
  6493. base.head) \
  6494. if (mask & (1 <<(intel_crtc)->pipe)) \
  6495. void
  6496. intel_modeset_check_state(struct drm_device *dev)
  6497. {
  6498. struct intel_crtc *crtc;
  6499. struct intel_encoder *encoder;
  6500. struct intel_connector *connector;
  6501. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6502. base.head) {
  6503. /* This also checks the encoder/connector hw state with the
  6504. * ->get_hw_state callbacks. */
  6505. intel_connector_check_state(connector);
  6506. WARN(&connector->new_encoder->base != connector->base.encoder,
  6507. "connector's staged encoder doesn't match current encoder\n");
  6508. }
  6509. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6510. base.head) {
  6511. bool enabled = false;
  6512. bool active = false;
  6513. enum pipe pipe, tracked_pipe;
  6514. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  6515. encoder->base.base.id,
  6516. drm_get_encoder_name(&encoder->base));
  6517. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  6518. "encoder's stage crtc doesn't match current crtc\n");
  6519. WARN(encoder->connectors_active && !encoder->base.crtc,
  6520. "encoder's active_connectors set, but no crtc\n");
  6521. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6522. base.head) {
  6523. if (connector->base.encoder != &encoder->base)
  6524. continue;
  6525. enabled = true;
  6526. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  6527. active = true;
  6528. }
  6529. WARN(!!encoder->base.crtc != enabled,
  6530. "encoder's enabled state mismatch "
  6531. "(expected %i, found %i)\n",
  6532. !!encoder->base.crtc, enabled);
  6533. WARN(active && !encoder->base.crtc,
  6534. "active encoder with no crtc\n");
  6535. WARN(encoder->connectors_active != active,
  6536. "encoder's computed active state doesn't match tracked active state "
  6537. "(expected %i, found %i)\n", active, encoder->connectors_active);
  6538. active = encoder->get_hw_state(encoder, &pipe);
  6539. WARN(active != encoder->connectors_active,
  6540. "encoder's hw state doesn't match sw tracking "
  6541. "(expected %i, found %i)\n",
  6542. encoder->connectors_active, active);
  6543. if (!encoder->base.crtc)
  6544. continue;
  6545. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  6546. WARN(active && pipe != tracked_pipe,
  6547. "active encoder's pipe doesn't match"
  6548. "(expected %i, found %i)\n",
  6549. tracked_pipe, pipe);
  6550. }
  6551. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  6552. base.head) {
  6553. bool enabled = false;
  6554. bool active = false;
  6555. DRM_DEBUG_KMS("[CRTC:%d]\n",
  6556. crtc->base.base.id);
  6557. WARN(crtc->active && !crtc->base.enabled,
  6558. "active crtc, but not enabled in sw tracking\n");
  6559. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6560. base.head) {
  6561. if (encoder->base.crtc != &crtc->base)
  6562. continue;
  6563. enabled = true;
  6564. if (encoder->connectors_active)
  6565. active = true;
  6566. }
  6567. WARN(active != crtc->active,
  6568. "crtc's computed active state doesn't match tracked active state "
  6569. "(expected %i, found %i)\n", active, crtc->active);
  6570. WARN(enabled != crtc->base.enabled,
  6571. "crtc's computed enabled state doesn't match tracked enabled state "
  6572. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  6573. assert_pipe(dev->dev_private, crtc->pipe, crtc->active);
  6574. }
  6575. }
  6576. int intel_set_mode(struct drm_crtc *crtc,
  6577. struct drm_display_mode *mode,
  6578. int x, int y, struct drm_framebuffer *fb)
  6579. {
  6580. struct drm_device *dev = crtc->dev;
  6581. drm_i915_private_t *dev_priv = dev->dev_private;
  6582. struct drm_display_mode *saved_mode, *saved_hwmode;
  6583. struct intel_crtc_config *pipe_config = NULL;
  6584. struct intel_crtc *intel_crtc;
  6585. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  6586. int ret = 0;
  6587. saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
  6588. if (!saved_mode)
  6589. return -ENOMEM;
  6590. saved_hwmode = saved_mode + 1;
  6591. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  6592. &prepare_pipes, &disable_pipes);
  6593. *saved_hwmode = crtc->hwmode;
  6594. *saved_mode = crtc->mode;
  6595. /* Hack: Because we don't (yet) support global modeset on multiple
  6596. * crtcs, we don't keep track of the new mode for more than one crtc.
  6597. * Hence simply check whether any bit is set in modeset_pipes in all the
  6598. * pieces of code that are not yet converted to deal with mutliple crtcs
  6599. * changing their mode at the same time. */
  6600. if (modeset_pipes) {
  6601. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  6602. if (IS_ERR(pipe_config)) {
  6603. ret = PTR_ERR(pipe_config);
  6604. pipe_config = NULL;
  6605. goto out;
  6606. }
  6607. }
  6608. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  6609. modeset_pipes, prepare_pipes, disable_pipes);
  6610. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  6611. intel_crtc_disable(&intel_crtc->base);
  6612. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  6613. if (intel_crtc->base.enabled)
  6614. dev_priv->display.crtc_disable(&intel_crtc->base);
  6615. }
  6616. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  6617. * to set it here already despite that we pass it down the callchain.
  6618. */
  6619. if (modeset_pipes) {
  6620. crtc->mode = *mode;
  6621. /* mode_set/enable/disable functions rely on a correct pipe
  6622. * config. */
  6623. to_intel_crtc(crtc)->config = *pipe_config;
  6624. }
  6625. /* Only after disabling all output pipelines that will be changed can we
  6626. * update the the output configuration. */
  6627. intel_modeset_update_state(dev, prepare_pipes);
  6628. if (dev_priv->display.modeset_global_resources)
  6629. dev_priv->display.modeset_global_resources(dev);
  6630. /* Set up the DPLL and any encoders state that needs to adjust or depend
  6631. * on the DPLL.
  6632. */
  6633. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  6634. ret = intel_crtc_mode_set(&intel_crtc->base,
  6635. x, y, fb);
  6636. if (ret)
  6637. goto done;
  6638. }
  6639. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  6640. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
  6641. dev_priv->display.crtc_enable(&intel_crtc->base);
  6642. if (modeset_pipes) {
  6643. /* Store real post-adjustment hardware mode. */
  6644. crtc->hwmode = pipe_config->adjusted_mode;
  6645. /* Calculate and store various constants which
  6646. * are later needed by vblank and swap-completion
  6647. * timestamping. They are derived from true hwmode.
  6648. */
  6649. drm_calc_timestamping_constants(crtc);
  6650. }
  6651. /* FIXME: add subpixel order */
  6652. done:
  6653. if (ret && crtc->enabled) {
  6654. crtc->hwmode = *saved_hwmode;
  6655. crtc->mode = *saved_mode;
  6656. } else {
  6657. intel_modeset_check_state(dev);
  6658. }
  6659. out:
  6660. kfree(pipe_config);
  6661. kfree(saved_mode);
  6662. return ret;
  6663. }
  6664. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  6665. {
  6666. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
  6667. }
  6668. #undef for_each_intel_crtc_masked
  6669. static void intel_set_config_free(struct intel_set_config *config)
  6670. {
  6671. if (!config)
  6672. return;
  6673. kfree(config->save_connector_encoders);
  6674. kfree(config->save_encoder_crtcs);
  6675. kfree(config);
  6676. }
  6677. static int intel_set_config_save_state(struct drm_device *dev,
  6678. struct intel_set_config *config)
  6679. {
  6680. struct drm_encoder *encoder;
  6681. struct drm_connector *connector;
  6682. int count;
  6683. config->save_encoder_crtcs =
  6684. kcalloc(dev->mode_config.num_encoder,
  6685. sizeof(struct drm_crtc *), GFP_KERNEL);
  6686. if (!config->save_encoder_crtcs)
  6687. return -ENOMEM;
  6688. config->save_connector_encoders =
  6689. kcalloc(dev->mode_config.num_connector,
  6690. sizeof(struct drm_encoder *), GFP_KERNEL);
  6691. if (!config->save_connector_encoders)
  6692. return -ENOMEM;
  6693. /* Copy data. Note that driver private data is not affected.
  6694. * Should anything bad happen only the expected state is
  6695. * restored, not the drivers personal bookkeeping.
  6696. */
  6697. count = 0;
  6698. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  6699. config->save_encoder_crtcs[count++] = encoder->crtc;
  6700. }
  6701. count = 0;
  6702. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  6703. config->save_connector_encoders[count++] = connector->encoder;
  6704. }
  6705. return 0;
  6706. }
  6707. static void intel_set_config_restore_state(struct drm_device *dev,
  6708. struct intel_set_config *config)
  6709. {
  6710. struct intel_encoder *encoder;
  6711. struct intel_connector *connector;
  6712. int count;
  6713. count = 0;
  6714. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  6715. encoder->new_crtc =
  6716. to_intel_crtc(config->save_encoder_crtcs[count++]);
  6717. }
  6718. count = 0;
  6719. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  6720. connector->new_encoder =
  6721. to_intel_encoder(config->save_connector_encoders[count++]);
  6722. }
  6723. }
  6724. static void
  6725. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  6726. struct intel_set_config *config)
  6727. {
  6728. /* We should be able to check here if the fb has the same properties
  6729. * and then just flip_or_move it */
  6730. if (set->crtc->fb != set->fb) {
  6731. /* If we have no fb then treat it as a full mode set */
  6732. if (set->crtc->fb == NULL) {
  6733. DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
  6734. config->mode_changed = true;
  6735. } else if (set->fb == NULL) {
  6736. config->mode_changed = true;
  6737. } else if (set->fb->pixel_format !=
  6738. set->crtc->fb->pixel_format) {
  6739. config->mode_changed = true;
  6740. } else
  6741. config->fb_changed = true;
  6742. }
  6743. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  6744. config->fb_changed = true;
  6745. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  6746. DRM_DEBUG_KMS("modes are different, full mode set\n");
  6747. drm_mode_debug_printmodeline(&set->crtc->mode);
  6748. drm_mode_debug_printmodeline(set->mode);
  6749. config->mode_changed = true;
  6750. }
  6751. }
  6752. static int
  6753. intel_modeset_stage_output_state(struct drm_device *dev,
  6754. struct drm_mode_set *set,
  6755. struct intel_set_config *config)
  6756. {
  6757. struct drm_crtc *new_crtc;
  6758. struct intel_connector *connector;
  6759. struct intel_encoder *encoder;
  6760. int count, ro;
  6761. /* The upper layers ensure that we either disable a crtc or have a list
  6762. * of connectors. For paranoia, double-check this. */
  6763. WARN_ON(!set->fb && (set->num_connectors != 0));
  6764. WARN_ON(set->fb && (set->num_connectors == 0));
  6765. count = 0;
  6766. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6767. base.head) {
  6768. /* Otherwise traverse passed in connector list and get encoders
  6769. * for them. */
  6770. for (ro = 0; ro < set->num_connectors; ro++) {
  6771. if (set->connectors[ro] == &connector->base) {
  6772. connector->new_encoder = connector->encoder;
  6773. break;
  6774. }
  6775. }
  6776. /* If we disable the crtc, disable all its connectors. Also, if
  6777. * the connector is on the changing crtc but not on the new
  6778. * connector list, disable it. */
  6779. if ((!set->fb || ro == set->num_connectors) &&
  6780. connector->base.encoder &&
  6781. connector->base.encoder->crtc == set->crtc) {
  6782. connector->new_encoder = NULL;
  6783. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  6784. connector->base.base.id,
  6785. drm_get_connector_name(&connector->base));
  6786. }
  6787. if (&connector->new_encoder->base != connector->base.encoder) {
  6788. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  6789. config->mode_changed = true;
  6790. }
  6791. }
  6792. /* connector->new_encoder is now updated for all connectors. */
  6793. /* Update crtc of enabled connectors. */
  6794. count = 0;
  6795. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6796. base.head) {
  6797. if (!connector->new_encoder)
  6798. continue;
  6799. new_crtc = connector->new_encoder->base.crtc;
  6800. for (ro = 0; ro < set->num_connectors; ro++) {
  6801. if (set->connectors[ro] == &connector->base)
  6802. new_crtc = set->crtc;
  6803. }
  6804. /* Make sure the new CRTC will work with the encoder */
  6805. if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
  6806. new_crtc)) {
  6807. return -EINVAL;
  6808. }
  6809. connector->encoder->new_crtc = to_intel_crtc(new_crtc);
  6810. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  6811. connector->base.base.id,
  6812. drm_get_connector_name(&connector->base),
  6813. new_crtc->base.id);
  6814. }
  6815. /* Check for any encoders that needs to be disabled. */
  6816. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6817. base.head) {
  6818. list_for_each_entry(connector,
  6819. &dev->mode_config.connector_list,
  6820. base.head) {
  6821. if (connector->new_encoder == encoder) {
  6822. WARN_ON(!connector->new_encoder->new_crtc);
  6823. goto next_encoder;
  6824. }
  6825. }
  6826. encoder->new_crtc = NULL;
  6827. next_encoder:
  6828. /* Only now check for crtc changes so we don't miss encoders
  6829. * that will be disabled. */
  6830. if (&encoder->new_crtc->base != encoder->base.crtc) {
  6831. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  6832. config->mode_changed = true;
  6833. }
  6834. }
  6835. /* Now we've also updated encoder->new_crtc for all encoders. */
  6836. return 0;
  6837. }
  6838. static int intel_crtc_set_config(struct drm_mode_set *set)
  6839. {
  6840. struct drm_device *dev;
  6841. struct drm_mode_set save_set;
  6842. struct intel_set_config *config;
  6843. int ret;
  6844. BUG_ON(!set);
  6845. BUG_ON(!set->crtc);
  6846. BUG_ON(!set->crtc->helper_private);
  6847. /* Enforce sane interface api - has been abused by the fb helper. */
  6848. BUG_ON(!set->mode && set->fb);
  6849. BUG_ON(set->fb && set->num_connectors == 0);
  6850. if (set->fb) {
  6851. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  6852. set->crtc->base.id, set->fb->base.id,
  6853. (int)set->num_connectors, set->x, set->y);
  6854. } else {
  6855. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  6856. }
  6857. dev = set->crtc->dev;
  6858. ret = -ENOMEM;
  6859. config = kzalloc(sizeof(*config), GFP_KERNEL);
  6860. if (!config)
  6861. goto out_config;
  6862. ret = intel_set_config_save_state(dev, config);
  6863. if (ret)
  6864. goto out_config;
  6865. save_set.crtc = set->crtc;
  6866. save_set.mode = &set->crtc->mode;
  6867. save_set.x = set->crtc->x;
  6868. save_set.y = set->crtc->y;
  6869. save_set.fb = set->crtc->fb;
  6870. /* Compute whether we need a full modeset, only an fb base update or no
  6871. * change at all. In the future we might also check whether only the
  6872. * mode changed, e.g. for LVDS where we only change the panel fitter in
  6873. * such cases. */
  6874. intel_set_config_compute_mode_changes(set, config);
  6875. ret = intel_modeset_stage_output_state(dev, set, config);
  6876. if (ret)
  6877. goto fail;
  6878. if (config->mode_changed) {
  6879. if (set->mode) {
  6880. DRM_DEBUG_KMS("attempting to set mode from"
  6881. " userspace\n");
  6882. drm_mode_debug_printmodeline(set->mode);
  6883. }
  6884. ret = intel_set_mode(set->crtc, set->mode,
  6885. set->x, set->y, set->fb);
  6886. if (ret) {
  6887. DRM_ERROR("failed to set mode on [CRTC:%d], err = %d\n",
  6888. set->crtc->base.id, ret);
  6889. goto fail;
  6890. }
  6891. } else if (config->fb_changed) {
  6892. intel_crtc_wait_for_pending_flips(set->crtc);
  6893. ret = intel_pipe_set_base(set->crtc,
  6894. set->x, set->y, set->fb);
  6895. }
  6896. intel_set_config_free(config);
  6897. return 0;
  6898. fail:
  6899. intel_set_config_restore_state(dev, config);
  6900. /* Try to restore the config */
  6901. if (config->mode_changed &&
  6902. intel_set_mode(save_set.crtc, save_set.mode,
  6903. save_set.x, save_set.y, save_set.fb))
  6904. DRM_ERROR("failed to restore config after modeset failure\n");
  6905. out_config:
  6906. intel_set_config_free(config);
  6907. return ret;
  6908. }
  6909. static const struct drm_crtc_funcs intel_crtc_funcs = {
  6910. .cursor_set = intel_crtc_cursor_set,
  6911. .cursor_move = intel_crtc_cursor_move,
  6912. .gamma_set = intel_crtc_gamma_set,
  6913. .set_config = intel_crtc_set_config,
  6914. .destroy = intel_crtc_destroy,
  6915. .page_flip = intel_crtc_page_flip,
  6916. };
  6917. static void intel_cpu_pll_init(struct drm_device *dev)
  6918. {
  6919. if (HAS_DDI(dev))
  6920. intel_ddi_pll_init(dev);
  6921. }
  6922. static void intel_pch_pll_init(struct drm_device *dev)
  6923. {
  6924. drm_i915_private_t *dev_priv = dev->dev_private;
  6925. int i;
  6926. if (dev_priv->num_pch_pll == 0) {
  6927. DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
  6928. return;
  6929. }
  6930. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  6931. dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
  6932. dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
  6933. dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
  6934. }
  6935. }
  6936. static void intel_crtc_init(struct drm_device *dev, int pipe)
  6937. {
  6938. drm_i915_private_t *dev_priv = dev->dev_private;
  6939. struct intel_crtc *intel_crtc;
  6940. int i;
  6941. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  6942. if (intel_crtc == NULL)
  6943. return;
  6944. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  6945. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  6946. for (i = 0; i < 256; i++) {
  6947. intel_crtc->lut_r[i] = i;
  6948. intel_crtc->lut_g[i] = i;
  6949. intel_crtc->lut_b[i] = i;
  6950. }
  6951. /* Swap pipes & planes for FBC on pre-965 */
  6952. intel_crtc->pipe = pipe;
  6953. intel_crtc->plane = pipe;
  6954. intel_crtc->cpu_transcoder = pipe;
  6955. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  6956. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  6957. intel_crtc->plane = !pipe;
  6958. }
  6959. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  6960. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  6961. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  6962. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  6963. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  6964. }
  6965. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  6966. struct drm_file *file)
  6967. {
  6968. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  6969. struct drm_mode_object *drmmode_obj;
  6970. struct intel_crtc *crtc;
  6971. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  6972. return -ENODEV;
  6973. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  6974. DRM_MODE_OBJECT_CRTC);
  6975. if (!drmmode_obj) {
  6976. DRM_ERROR("no such CRTC id\n");
  6977. return -EINVAL;
  6978. }
  6979. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  6980. pipe_from_crtc_id->pipe = crtc->pipe;
  6981. return 0;
  6982. }
  6983. static int intel_encoder_clones(struct intel_encoder *encoder)
  6984. {
  6985. struct drm_device *dev = encoder->base.dev;
  6986. struct intel_encoder *source_encoder;
  6987. int index_mask = 0;
  6988. int entry = 0;
  6989. list_for_each_entry(source_encoder,
  6990. &dev->mode_config.encoder_list, base.head) {
  6991. if (encoder == source_encoder)
  6992. index_mask |= (1 << entry);
  6993. /* Intel hw has only one MUX where enocoders could be cloned. */
  6994. if (encoder->cloneable && source_encoder->cloneable)
  6995. index_mask |= (1 << entry);
  6996. entry++;
  6997. }
  6998. return index_mask;
  6999. }
  7000. static bool has_edp_a(struct drm_device *dev)
  7001. {
  7002. struct drm_i915_private *dev_priv = dev->dev_private;
  7003. if (!IS_MOBILE(dev))
  7004. return false;
  7005. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  7006. return false;
  7007. if (IS_GEN5(dev) &&
  7008. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  7009. return false;
  7010. return true;
  7011. }
  7012. static void intel_setup_outputs(struct drm_device *dev)
  7013. {
  7014. struct drm_i915_private *dev_priv = dev->dev_private;
  7015. struct intel_encoder *encoder;
  7016. bool dpd_is_edp = false;
  7017. bool has_lvds;
  7018. has_lvds = intel_lvds_init(dev);
  7019. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  7020. /* disable the panel fitter on everything but LVDS */
  7021. I915_WRITE(PFIT_CONTROL, 0);
  7022. }
  7023. if (!(HAS_DDI(dev) && (I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)))
  7024. intel_crt_init(dev);
  7025. if (HAS_DDI(dev)) {
  7026. int found;
  7027. /* Haswell uses DDI functions to detect digital outputs */
  7028. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  7029. /* DDI A only supports eDP */
  7030. if (found)
  7031. intel_ddi_init(dev, PORT_A);
  7032. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  7033. * register */
  7034. found = I915_READ(SFUSE_STRAP);
  7035. if (found & SFUSE_STRAP_DDIB_DETECTED)
  7036. intel_ddi_init(dev, PORT_B);
  7037. if (found & SFUSE_STRAP_DDIC_DETECTED)
  7038. intel_ddi_init(dev, PORT_C);
  7039. if (found & SFUSE_STRAP_DDID_DETECTED)
  7040. intel_ddi_init(dev, PORT_D);
  7041. } else if (HAS_PCH_SPLIT(dev)) {
  7042. int found;
  7043. dpd_is_edp = intel_dpd_is_edp(dev);
  7044. if (has_edp_a(dev))
  7045. intel_dp_init(dev, DP_A, PORT_A);
  7046. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  7047. /* PCH SDVOB multiplex with HDMIB */
  7048. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  7049. if (!found)
  7050. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  7051. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  7052. intel_dp_init(dev, PCH_DP_B, PORT_B);
  7053. }
  7054. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  7055. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  7056. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  7057. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  7058. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  7059. intel_dp_init(dev, PCH_DP_C, PORT_C);
  7060. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  7061. intel_dp_init(dev, PCH_DP_D, PORT_D);
  7062. } else if (IS_VALLEYVIEW(dev)) {
  7063. /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
  7064. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
  7065. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  7066. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
  7067. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  7068. PORT_B);
  7069. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
  7070. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  7071. }
  7072. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  7073. bool found = false;
  7074. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  7075. DRM_DEBUG_KMS("probing SDVOB\n");
  7076. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  7077. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  7078. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  7079. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  7080. }
  7081. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  7082. DRM_DEBUG_KMS("probing DP_B\n");
  7083. intel_dp_init(dev, DP_B, PORT_B);
  7084. }
  7085. }
  7086. /* Before G4X SDVOC doesn't have its own detect register */
  7087. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  7088. DRM_DEBUG_KMS("probing SDVOC\n");
  7089. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  7090. }
  7091. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  7092. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  7093. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  7094. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  7095. }
  7096. if (SUPPORTS_INTEGRATED_DP(dev)) {
  7097. DRM_DEBUG_KMS("probing DP_C\n");
  7098. intel_dp_init(dev, DP_C, PORT_C);
  7099. }
  7100. }
  7101. if (SUPPORTS_INTEGRATED_DP(dev) &&
  7102. (I915_READ(DP_D) & DP_DETECTED)) {
  7103. DRM_DEBUG_KMS("probing DP_D\n");
  7104. intel_dp_init(dev, DP_D, PORT_D);
  7105. }
  7106. } else if (IS_GEN2(dev))
  7107. intel_dvo_init(dev);
  7108. if (SUPPORTS_TV(dev))
  7109. intel_tv_init(dev);
  7110. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  7111. encoder->base.possible_crtcs = encoder->crtc_mask;
  7112. encoder->base.possible_clones =
  7113. intel_encoder_clones(encoder);
  7114. }
  7115. intel_init_pch_refclk(dev);
  7116. drm_helper_move_panel_connectors_to_head(dev);
  7117. }
  7118. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  7119. {
  7120. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  7121. drm_framebuffer_cleanup(fb);
  7122. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  7123. kfree(intel_fb);
  7124. }
  7125. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  7126. struct drm_file *file,
  7127. unsigned int *handle)
  7128. {
  7129. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  7130. struct drm_i915_gem_object *obj = intel_fb->obj;
  7131. return drm_gem_handle_create(file, &obj->base, handle);
  7132. }
  7133. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  7134. .destroy = intel_user_framebuffer_destroy,
  7135. .create_handle = intel_user_framebuffer_create_handle,
  7136. };
  7137. int intel_framebuffer_init(struct drm_device *dev,
  7138. struct intel_framebuffer *intel_fb,
  7139. struct drm_mode_fb_cmd2 *mode_cmd,
  7140. struct drm_i915_gem_object *obj)
  7141. {
  7142. int ret;
  7143. if (obj->tiling_mode == I915_TILING_Y) {
  7144. DRM_DEBUG("hardware does not support tiling Y\n");
  7145. return -EINVAL;
  7146. }
  7147. if (mode_cmd->pitches[0] & 63) {
  7148. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  7149. mode_cmd->pitches[0]);
  7150. return -EINVAL;
  7151. }
  7152. /* FIXME <= Gen4 stride limits are bit unclear */
  7153. if (mode_cmd->pitches[0] > 32768) {
  7154. DRM_DEBUG("pitch (%d) must be at less than 32768\n",
  7155. mode_cmd->pitches[0]);
  7156. return -EINVAL;
  7157. }
  7158. if (obj->tiling_mode != I915_TILING_NONE &&
  7159. mode_cmd->pitches[0] != obj->stride) {
  7160. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  7161. mode_cmd->pitches[0], obj->stride);
  7162. return -EINVAL;
  7163. }
  7164. /* Reject formats not supported by any plane early. */
  7165. switch (mode_cmd->pixel_format) {
  7166. case DRM_FORMAT_C8:
  7167. case DRM_FORMAT_RGB565:
  7168. case DRM_FORMAT_XRGB8888:
  7169. case DRM_FORMAT_ARGB8888:
  7170. break;
  7171. case DRM_FORMAT_XRGB1555:
  7172. case DRM_FORMAT_ARGB1555:
  7173. if (INTEL_INFO(dev)->gen > 3) {
  7174. DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
  7175. return -EINVAL;
  7176. }
  7177. break;
  7178. case DRM_FORMAT_XBGR8888:
  7179. case DRM_FORMAT_ABGR8888:
  7180. case DRM_FORMAT_XRGB2101010:
  7181. case DRM_FORMAT_ARGB2101010:
  7182. case DRM_FORMAT_XBGR2101010:
  7183. case DRM_FORMAT_ABGR2101010:
  7184. if (INTEL_INFO(dev)->gen < 4) {
  7185. DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
  7186. return -EINVAL;
  7187. }
  7188. break;
  7189. case DRM_FORMAT_YUYV:
  7190. case DRM_FORMAT_UYVY:
  7191. case DRM_FORMAT_YVYU:
  7192. case DRM_FORMAT_VYUY:
  7193. if (INTEL_INFO(dev)->gen < 5) {
  7194. DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
  7195. return -EINVAL;
  7196. }
  7197. break;
  7198. default:
  7199. DRM_DEBUG("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
  7200. return -EINVAL;
  7201. }
  7202. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  7203. if (mode_cmd->offsets[0] != 0)
  7204. return -EINVAL;
  7205. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  7206. intel_fb->obj = obj;
  7207. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  7208. if (ret) {
  7209. DRM_ERROR("framebuffer init failed %d\n", ret);
  7210. return ret;
  7211. }
  7212. return 0;
  7213. }
  7214. static struct drm_framebuffer *
  7215. intel_user_framebuffer_create(struct drm_device *dev,
  7216. struct drm_file *filp,
  7217. struct drm_mode_fb_cmd2 *mode_cmd)
  7218. {
  7219. struct drm_i915_gem_object *obj;
  7220. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  7221. mode_cmd->handles[0]));
  7222. if (&obj->base == NULL)
  7223. return ERR_PTR(-ENOENT);
  7224. return intel_framebuffer_create(dev, mode_cmd, obj);
  7225. }
  7226. static const struct drm_mode_config_funcs intel_mode_funcs = {
  7227. .fb_create = intel_user_framebuffer_create,
  7228. .output_poll_changed = intel_fb_output_poll_changed,
  7229. };
  7230. /* Set up chip specific display functions */
  7231. static void intel_init_display(struct drm_device *dev)
  7232. {
  7233. struct drm_i915_private *dev_priv = dev->dev_private;
  7234. if (HAS_DDI(dev)) {
  7235. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  7236. dev_priv->display.crtc_enable = haswell_crtc_enable;
  7237. dev_priv->display.crtc_disable = haswell_crtc_disable;
  7238. dev_priv->display.off = haswell_crtc_off;
  7239. dev_priv->display.update_plane = ironlake_update_plane;
  7240. } else if (HAS_PCH_SPLIT(dev)) {
  7241. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  7242. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  7243. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  7244. dev_priv->display.off = ironlake_crtc_off;
  7245. dev_priv->display.update_plane = ironlake_update_plane;
  7246. } else {
  7247. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  7248. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  7249. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  7250. dev_priv->display.off = i9xx_crtc_off;
  7251. dev_priv->display.update_plane = i9xx_update_plane;
  7252. }
  7253. /* Returns the core display clock speed */
  7254. if (IS_VALLEYVIEW(dev))
  7255. dev_priv->display.get_display_clock_speed =
  7256. valleyview_get_display_clock_speed;
  7257. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  7258. dev_priv->display.get_display_clock_speed =
  7259. i945_get_display_clock_speed;
  7260. else if (IS_I915G(dev))
  7261. dev_priv->display.get_display_clock_speed =
  7262. i915_get_display_clock_speed;
  7263. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  7264. dev_priv->display.get_display_clock_speed =
  7265. i9xx_misc_get_display_clock_speed;
  7266. else if (IS_I915GM(dev))
  7267. dev_priv->display.get_display_clock_speed =
  7268. i915gm_get_display_clock_speed;
  7269. else if (IS_I865G(dev))
  7270. dev_priv->display.get_display_clock_speed =
  7271. i865_get_display_clock_speed;
  7272. else if (IS_I85X(dev))
  7273. dev_priv->display.get_display_clock_speed =
  7274. i855_get_display_clock_speed;
  7275. else /* 852, 830 */
  7276. dev_priv->display.get_display_clock_speed =
  7277. i830_get_display_clock_speed;
  7278. if (HAS_PCH_SPLIT(dev)) {
  7279. if (IS_GEN5(dev)) {
  7280. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  7281. dev_priv->display.write_eld = ironlake_write_eld;
  7282. } else if (IS_GEN6(dev)) {
  7283. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  7284. dev_priv->display.write_eld = ironlake_write_eld;
  7285. } else if (IS_IVYBRIDGE(dev)) {
  7286. /* FIXME: detect B0+ stepping and use auto training */
  7287. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  7288. dev_priv->display.write_eld = ironlake_write_eld;
  7289. dev_priv->display.modeset_global_resources =
  7290. ivb_modeset_global_resources;
  7291. } else if (IS_HASWELL(dev)) {
  7292. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  7293. dev_priv->display.write_eld = haswell_write_eld;
  7294. dev_priv->display.modeset_global_resources =
  7295. haswell_modeset_global_resources;
  7296. }
  7297. } else if (IS_G4X(dev)) {
  7298. dev_priv->display.write_eld = g4x_write_eld;
  7299. }
  7300. /* Default just returns -ENODEV to indicate unsupported */
  7301. dev_priv->display.queue_flip = intel_default_queue_flip;
  7302. switch (INTEL_INFO(dev)->gen) {
  7303. case 2:
  7304. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  7305. break;
  7306. case 3:
  7307. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  7308. break;
  7309. case 4:
  7310. case 5:
  7311. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  7312. break;
  7313. case 6:
  7314. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  7315. break;
  7316. case 7:
  7317. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  7318. break;
  7319. }
  7320. }
  7321. /*
  7322. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  7323. * resume, or other times. This quirk makes sure that's the case for
  7324. * affected systems.
  7325. */
  7326. static void quirk_pipea_force(struct drm_device *dev)
  7327. {
  7328. struct drm_i915_private *dev_priv = dev->dev_private;
  7329. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  7330. DRM_INFO("applying pipe a force quirk\n");
  7331. }
  7332. /*
  7333. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  7334. */
  7335. static void quirk_ssc_force_disable(struct drm_device *dev)
  7336. {
  7337. struct drm_i915_private *dev_priv = dev->dev_private;
  7338. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  7339. DRM_INFO("applying lvds SSC disable quirk\n");
  7340. }
  7341. /*
  7342. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  7343. * brightness value
  7344. */
  7345. static void quirk_invert_brightness(struct drm_device *dev)
  7346. {
  7347. struct drm_i915_private *dev_priv = dev->dev_private;
  7348. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  7349. DRM_INFO("applying inverted panel brightness quirk\n");
  7350. }
  7351. struct intel_quirk {
  7352. int device;
  7353. int subsystem_vendor;
  7354. int subsystem_device;
  7355. void (*hook)(struct drm_device *dev);
  7356. };
  7357. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  7358. struct intel_dmi_quirk {
  7359. void (*hook)(struct drm_device *dev);
  7360. const struct dmi_system_id (*dmi_id_list)[];
  7361. };
  7362. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  7363. {
  7364. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  7365. return 1;
  7366. }
  7367. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  7368. {
  7369. .dmi_id_list = &(const struct dmi_system_id[]) {
  7370. {
  7371. .callback = intel_dmi_reverse_brightness,
  7372. .ident = "NCR Corporation",
  7373. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  7374. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  7375. },
  7376. },
  7377. { } /* terminating entry */
  7378. },
  7379. .hook = quirk_invert_brightness,
  7380. },
  7381. };
  7382. static struct intel_quirk intel_quirks[] = {
  7383. /* HP Mini needs pipe A force quirk (LP: #322104) */
  7384. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  7385. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  7386. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  7387. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  7388. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  7389. /* 830/845 need to leave pipe A & dpll A up */
  7390. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  7391. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  7392. /* Lenovo U160 cannot use SSC on LVDS */
  7393. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  7394. /* Sony Vaio Y cannot use SSC on LVDS */
  7395. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  7396. /* Acer Aspire 5734Z must invert backlight brightness */
  7397. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  7398. /* Acer/eMachines G725 */
  7399. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  7400. /* Acer/eMachines e725 */
  7401. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  7402. /* Acer/Packard Bell NCL20 */
  7403. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  7404. /* Acer Aspire 4736Z */
  7405. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  7406. };
  7407. static void intel_init_quirks(struct drm_device *dev)
  7408. {
  7409. struct pci_dev *d = dev->pdev;
  7410. int i;
  7411. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  7412. struct intel_quirk *q = &intel_quirks[i];
  7413. if (d->device == q->device &&
  7414. (d->subsystem_vendor == q->subsystem_vendor ||
  7415. q->subsystem_vendor == PCI_ANY_ID) &&
  7416. (d->subsystem_device == q->subsystem_device ||
  7417. q->subsystem_device == PCI_ANY_ID))
  7418. q->hook(dev);
  7419. }
  7420. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  7421. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  7422. intel_dmi_quirks[i].hook(dev);
  7423. }
  7424. }
  7425. /* Disable the VGA plane that we never use */
  7426. static void i915_disable_vga(struct drm_device *dev)
  7427. {
  7428. struct drm_i915_private *dev_priv = dev->dev_private;
  7429. u8 sr1;
  7430. u32 vga_reg = i915_vgacntrl_reg(dev);
  7431. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  7432. outb(SR01, VGA_SR_INDEX);
  7433. sr1 = inb(VGA_SR_DATA);
  7434. outb(sr1 | 1<<5, VGA_SR_DATA);
  7435. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  7436. udelay(300);
  7437. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  7438. POSTING_READ(vga_reg);
  7439. }
  7440. void intel_modeset_init_hw(struct drm_device *dev)
  7441. {
  7442. intel_init_power_well(dev);
  7443. intel_prepare_ddi(dev);
  7444. intel_init_clock_gating(dev);
  7445. mutex_lock(&dev->struct_mutex);
  7446. intel_enable_gt_powersave(dev);
  7447. mutex_unlock(&dev->struct_mutex);
  7448. }
  7449. void intel_modeset_init(struct drm_device *dev)
  7450. {
  7451. struct drm_i915_private *dev_priv = dev->dev_private;
  7452. int i, j, ret;
  7453. drm_mode_config_init(dev);
  7454. dev->mode_config.min_width = 0;
  7455. dev->mode_config.min_height = 0;
  7456. dev->mode_config.preferred_depth = 24;
  7457. dev->mode_config.prefer_shadow = 1;
  7458. dev->mode_config.funcs = &intel_mode_funcs;
  7459. intel_init_quirks(dev);
  7460. intel_init_pm(dev);
  7461. intel_init_display(dev);
  7462. if (IS_GEN2(dev)) {
  7463. dev->mode_config.max_width = 2048;
  7464. dev->mode_config.max_height = 2048;
  7465. } else if (IS_GEN3(dev)) {
  7466. dev->mode_config.max_width = 4096;
  7467. dev->mode_config.max_height = 4096;
  7468. } else {
  7469. dev->mode_config.max_width = 8192;
  7470. dev->mode_config.max_height = 8192;
  7471. }
  7472. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  7473. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  7474. INTEL_INFO(dev)->num_pipes,
  7475. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  7476. for (i = 0; i < INTEL_INFO(dev)->num_pipes; i++) {
  7477. intel_crtc_init(dev, i);
  7478. for (j = 0; j < dev_priv->num_plane; j++) {
  7479. ret = intel_plane_init(dev, i, j);
  7480. if (ret)
  7481. DRM_DEBUG_KMS("pipe %d plane %d init failed: %d\n",
  7482. i, j, ret);
  7483. }
  7484. }
  7485. intel_cpu_pll_init(dev);
  7486. intel_pch_pll_init(dev);
  7487. /* Just disable it once at startup */
  7488. i915_disable_vga(dev);
  7489. intel_setup_outputs(dev);
  7490. /* Just in case the BIOS is doing something questionable. */
  7491. intel_disable_fbc(dev);
  7492. }
  7493. static void
  7494. intel_connector_break_all_links(struct intel_connector *connector)
  7495. {
  7496. connector->base.dpms = DRM_MODE_DPMS_OFF;
  7497. connector->base.encoder = NULL;
  7498. connector->encoder->connectors_active = false;
  7499. connector->encoder->base.crtc = NULL;
  7500. }
  7501. static void intel_enable_pipe_a(struct drm_device *dev)
  7502. {
  7503. struct intel_connector *connector;
  7504. struct drm_connector *crt = NULL;
  7505. struct intel_load_detect_pipe load_detect_temp;
  7506. /* We can't just switch on the pipe A, we need to set things up with a
  7507. * proper mode and output configuration. As a gross hack, enable pipe A
  7508. * by enabling the load detect pipe once. */
  7509. list_for_each_entry(connector,
  7510. &dev->mode_config.connector_list,
  7511. base.head) {
  7512. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  7513. crt = &connector->base;
  7514. break;
  7515. }
  7516. }
  7517. if (!crt)
  7518. return;
  7519. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
  7520. intel_release_load_detect_pipe(crt, &load_detect_temp);
  7521. }
  7522. static bool
  7523. intel_check_plane_mapping(struct intel_crtc *crtc)
  7524. {
  7525. struct drm_device *dev = crtc->base.dev;
  7526. struct drm_i915_private *dev_priv = dev->dev_private;
  7527. u32 reg, val;
  7528. if (INTEL_INFO(dev)->num_pipes == 1)
  7529. return true;
  7530. reg = DSPCNTR(!crtc->plane);
  7531. val = I915_READ(reg);
  7532. if ((val & DISPLAY_PLANE_ENABLE) &&
  7533. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  7534. return false;
  7535. return true;
  7536. }
  7537. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  7538. {
  7539. struct drm_device *dev = crtc->base.dev;
  7540. struct drm_i915_private *dev_priv = dev->dev_private;
  7541. u32 reg;
  7542. /* Clear any frame start delays used for debugging left by the BIOS */
  7543. reg = PIPECONF(crtc->cpu_transcoder);
  7544. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  7545. /* We need to sanitize the plane -> pipe mapping first because this will
  7546. * disable the crtc (and hence change the state) if it is wrong. Note
  7547. * that gen4+ has a fixed plane -> pipe mapping. */
  7548. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  7549. struct intel_connector *connector;
  7550. bool plane;
  7551. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  7552. crtc->base.base.id);
  7553. /* Pipe has the wrong plane attached and the plane is active.
  7554. * Temporarily change the plane mapping and disable everything
  7555. * ... */
  7556. plane = crtc->plane;
  7557. crtc->plane = !plane;
  7558. dev_priv->display.crtc_disable(&crtc->base);
  7559. crtc->plane = plane;
  7560. /* ... and break all links. */
  7561. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7562. base.head) {
  7563. if (connector->encoder->base.crtc != &crtc->base)
  7564. continue;
  7565. intel_connector_break_all_links(connector);
  7566. }
  7567. WARN_ON(crtc->active);
  7568. crtc->base.enabled = false;
  7569. }
  7570. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  7571. crtc->pipe == PIPE_A && !crtc->active) {
  7572. /* BIOS forgot to enable pipe A, this mostly happens after
  7573. * resume. Force-enable the pipe to fix this, the update_dpms
  7574. * call below we restore the pipe to the right state, but leave
  7575. * the required bits on. */
  7576. intel_enable_pipe_a(dev);
  7577. }
  7578. /* Adjust the state of the output pipe according to whether we
  7579. * have active connectors/encoders. */
  7580. intel_crtc_update_dpms(&crtc->base);
  7581. if (crtc->active != crtc->base.enabled) {
  7582. struct intel_encoder *encoder;
  7583. /* This can happen either due to bugs in the get_hw_state
  7584. * functions or because the pipe is force-enabled due to the
  7585. * pipe A quirk. */
  7586. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  7587. crtc->base.base.id,
  7588. crtc->base.enabled ? "enabled" : "disabled",
  7589. crtc->active ? "enabled" : "disabled");
  7590. crtc->base.enabled = crtc->active;
  7591. /* Because we only establish the connector -> encoder ->
  7592. * crtc links if something is active, this means the
  7593. * crtc is now deactivated. Break the links. connector
  7594. * -> encoder links are only establish when things are
  7595. * actually up, hence no need to break them. */
  7596. WARN_ON(crtc->active);
  7597. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  7598. WARN_ON(encoder->connectors_active);
  7599. encoder->base.crtc = NULL;
  7600. }
  7601. }
  7602. }
  7603. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  7604. {
  7605. struct intel_connector *connector;
  7606. struct drm_device *dev = encoder->base.dev;
  7607. /* We need to check both for a crtc link (meaning that the
  7608. * encoder is active and trying to read from a pipe) and the
  7609. * pipe itself being active. */
  7610. bool has_active_crtc = encoder->base.crtc &&
  7611. to_intel_crtc(encoder->base.crtc)->active;
  7612. if (encoder->connectors_active && !has_active_crtc) {
  7613. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  7614. encoder->base.base.id,
  7615. drm_get_encoder_name(&encoder->base));
  7616. /* Connector is active, but has no active pipe. This is
  7617. * fallout from our resume register restoring. Disable
  7618. * the encoder manually again. */
  7619. if (encoder->base.crtc) {
  7620. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  7621. encoder->base.base.id,
  7622. drm_get_encoder_name(&encoder->base));
  7623. encoder->disable(encoder);
  7624. }
  7625. /* Inconsistent output/port/pipe state happens presumably due to
  7626. * a bug in one of the get_hw_state functions. Or someplace else
  7627. * in our code, like the register restore mess on resume. Clamp
  7628. * things to off as a safer default. */
  7629. list_for_each_entry(connector,
  7630. &dev->mode_config.connector_list,
  7631. base.head) {
  7632. if (connector->encoder != encoder)
  7633. continue;
  7634. intel_connector_break_all_links(connector);
  7635. }
  7636. }
  7637. /* Enabled encoders without active connectors will be fixed in
  7638. * the crtc fixup. */
  7639. }
  7640. void i915_redisable_vga(struct drm_device *dev)
  7641. {
  7642. struct drm_i915_private *dev_priv = dev->dev_private;
  7643. u32 vga_reg = i915_vgacntrl_reg(dev);
  7644. if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
  7645. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  7646. i915_disable_vga(dev);
  7647. }
  7648. }
  7649. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  7650. * and i915 state tracking structures. */
  7651. void intel_modeset_setup_hw_state(struct drm_device *dev,
  7652. bool force_restore)
  7653. {
  7654. struct drm_i915_private *dev_priv = dev->dev_private;
  7655. enum pipe pipe;
  7656. u32 tmp;
  7657. struct drm_plane *plane;
  7658. struct intel_crtc *crtc;
  7659. struct intel_encoder *encoder;
  7660. struct intel_connector *connector;
  7661. if (HAS_DDI(dev)) {
  7662. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7663. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7664. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7665. case TRANS_DDI_EDP_INPUT_A_ON:
  7666. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7667. pipe = PIPE_A;
  7668. break;
  7669. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7670. pipe = PIPE_B;
  7671. break;
  7672. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7673. pipe = PIPE_C;
  7674. break;
  7675. default:
  7676. /* A bogus value has been programmed, disable
  7677. * the transcoder */
  7678. WARN(1, "Bogus eDP source %08x\n", tmp);
  7679. intel_ddi_disable_transcoder_func(dev_priv,
  7680. TRANSCODER_EDP);
  7681. goto setup_pipes;
  7682. }
  7683. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  7684. crtc->cpu_transcoder = TRANSCODER_EDP;
  7685. DRM_DEBUG_KMS("Pipe %c using transcoder EDP\n",
  7686. pipe_name(pipe));
  7687. }
  7688. }
  7689. setup_pipes:
  7690. for_each_pipe(pipe) {
  7691. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  7692. tmp = I915_READ(PIPECONF(crtc->cpu_transcoder));
  7693. if (tmp & PIPECONF_ENABLE)
  7694. crtc->active = true;
  7695. else
  7696. crtc->active = false;
  7697. crtc->base.enabled = crtc->active;
  7698. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  7699. crtc->base.base.id,
  7700. crtc->active ? "enabled" : "disabled");
  7701. }
  7702. if (HAS_DDI(dev))
  7703. intel_ddi_setup_hw_pll_state(dev);
  7704. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7705. base.head) {
  7706. pipe = 0;
  7707. if (encoder->get_hw_state(encoder, &pipe)) {
  7708. encoder->base.crtc =
  7709. dev_priv->pipe_to_crtc_mapping[pipe];
  7710. } else {
  7711. encoder->base.crtc = NULL;
  7712. }
  7713. encoder->connectors_active = false;
  7714. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
  7715. encoder->base.base.id,
  7716. drm_get_encoder_name(&encoder->base),
  7717. encoder->base.crtc ? "enabled" : "disabled",
  7718. pipe);
  7719. }
  7720. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7721. base.head) {
  7722. if (connector->get_hw_state(connector)) {
  7723. connector->base.dpms = DRM_MODE_DPMS_ON;
  7724. connector->encoder->connectors_active = true;
  7725. connector->base.encoder = &connector->encoder->base;
  7726. } else {
  7727. connector->base.dpms = DRM_MODE_DPMS_OFF;
  7728. connector->base.encoder = NULL;
  7729. }
  7730. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  7731. connector->base.base.id,
  7732. drm_get_connector_name(&connector->base),
  7733. connector->base.encoder ? "enabled" : "disabled");
  7734. }
  7735. /* HW state is read out, now we need to sanitize this mess. */
  7736. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7737. base.head) {
  7738. intel_sanitize_encoder(encoder);
  7739. }
  7740. for_each_pipe(pipe) {
  7741. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  7742. intel_sanitize_crtc(crtc);
  7743. }
  7744. if (force_restore) {
  7745. for_each_pipe(pipe) {
  7746. struct drm_crtc *crtc =
  7747. dev_priv->pipe_to_crtc_mapping[pipe];
  7748. intel_crtc_restore_mode(crtc);
  7749. }
  7750. list_for_each_entry(plane, &dev->mode_config.plane_list, head)
  7751. intel_plane_restore(plane);
  7752. i915_redisable_vga(dev);
  7753. } else {
  7754. intel_modeset_update_staged_output_state(dev);
  7755. }
  7756. intel_modeset_check_state(dev);
  7757. drm_mode_config_reset(dev);
  7758. }
  7759. void intel_modeset_gem_init(struct drm_device *dev)
  7760. {
  7761. intel_modeset_init_hw(dev);
  7762. intel_setup_overlay(dev);
  7763. intel_modeset_setup_hw_state(dev, false);
  7764. }
  7765. void intel_modeset_cleanup(struct drm_device *dev)
  7766. {
  7767. struct drm_i915_private *dev_priv = dev->dev_private;
  7768. struct drm_crtc *crtc;
  7769. struct intel_crtc *intel_crtc;
  7770. drm_kms_helper_poll_fini(dev);
  7771. mutex_lock(&dev->struct_mutex);
  7772. intel_unregister_dsm_handler();
  7773. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  7774. /* Skip inactive CRTCs */
  7775. if (!crtc->fb)
  7776. continue;
  7777. intel_crtc = to_intel_crtc(crtc);
  7778. intel_increase_pllclock(crtc);
  7779. }
  7780. intel_disable_fbc(dev);
  7781. intel_disable_gt_powersave(dev);
  7782. ironlake_teardown_rc6(dev);
  7783. if (IS_VALLEYVIEW(dev))
  7784. vlv_init_dpio(dev);
  7785. mutex_unlock(&dev->struct_mutex);
  7786. /* Disable the irq before mode object teardown, for the irq might
  7787. * enqueue unpin/hotplug work. */
  7788. drm_irq_uninstall(dev);
  7789. cancel_work_sync(&dev_priv->hotplug_work);
  7790. cancel_work_sync(&dev_priv->rps.work);
  7791. /* flush any delayed tasks or pending work */
  7792. flush_scheduled_work();
  7793. drm_mode_config_cleanup(dev);
  7794. intel_cleanup_overlay(dev);
  7795. }
  7796. /*
  7797. * Return which encoder is currently attached for connector.
  7798. */
  7799. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  7800. {
  7801. return &intel_attached_encoder(connector)->base;
  7802. }
  7803. void intel_connector_attach_encoder(struct intel_connector *connector,
  7804. struct intel_encoder *encoder)
  7805. {
  7806. connector->encoder = encoder;
  7807. drm_mode_connector_attach_encoder(&connector->base,
  7808. &encoder->base);
  7809. }
  7810. /*
  7811. * set vga decode state - true == enable VGA decode
  7812. */
  7813. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  7814. {
  7815. struct drm_i915_private *dev_priv = dev->dev_private;
  7816. u16 gmch_ctrl;
  7817. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  7818. if (state)
  7819. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  7820. else
  7821. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  7822. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  7823. return 0;
  7824. }
  7825. #ifdef CONFIG_DEBUG_FS
  7826. #include <linux/seq_file.h>
  7827. struct intel_display_error_state {
  7828. struct intel_cursor_error_state {
  7829. u32 control;
  7830. u32 position;
  7831. u32 base;
  7832. u32 size;
  7833. } cursor[I915_MAX_PIPES];
  7834. struct intel_pipe_error_state {
  7835. u32 conf;
  7836. u32 source;
  7837. u32 htotal;
  7838. u32 hblank;
  7839. u32 hsync;
  7840. u32 vtotal;
  7841. u32 vblank;
  7842. u32 vsync;
  7843. } pipe[I915_MAX_PIPES];
  7844. struct intel_plane_error_state {
  7845. u32 control;
  7846. u32 stride;
  7847. u32 size;
  7848. u32 pos;
  7849. u32 addr;
  7850. u32 surface;
  7851. u32 tile_offset;
  7852. } plane[I915_MAX_PIPES];
  7853. };
  7854. struct intel_display_error_state *
  7855. intel_display_capture_error_state(struct drm_device *dev)
  7856. {
  7857. drm_i915_private_t *dev_priv = dev->dev_private;
  7858. struct intel_display_error_state *error;
  7859. enum transcoder cpu_transcoder;
  7860. int i;
  7861. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  7862. if (error == NULL)
  7863. return NULL;
  7864. for_each_pipe(i) {
  7865. cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
  7866. if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
  7867. error->cursor[i].control = I915_READ(CURCNTR(i));
  7868. error->cursor[i].position = I915_READ(CURPOS(i));
  7869. error->cursor[i].base = I915_READ(CURBASE(i));
  7870. } else {
  7871. error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
  7872. error->cursor[i].position = I915_READ(CURPOS_IVB(i));
  7873. error->cursor[i].base = I915_READ(CURBASE_IVB(i));
  7874. }
  7875. error->plane[i].control = I915_READ(DSPCNTR(i));
  7876. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  7877. if (INTEL_INFO(dev)->gen <= 3) {
  7878. error->plane[i].size = I915_READ(DSPSIZE(i));
  7879. error->plane[i].pos = I915_READ(DSPPOS(i));
  7880. }
  7881. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  7882. error->plane[i].addr = I915_READ(DSPADDR(i));
  7883. if (INTEL_INFO(dev)->gen >= 4) {
  7884. error->plane[i].surface = I915_READ(DSPSURF(i));
  7885. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  7886. }
  7887. error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  7888. error->pipe[i].source = I915_READ(PIPESRC(i));
  7889. error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  7890. error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  7891. error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  7892. error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  7893. error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  7894. error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  7895. }
  7896. return error;
  7897. }
  7898. void
  7899. intel_display_print_error_state(struct seq_file *m,
  7900. struct drm_device *dev,
  7901. struct intel_display_error_state *error)
  7902. {
  7903. int i;
  7904. seq_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  7905. for_each_pipe(i) {
  7906. seq_printf(m, "Pipe [%d]:\n", i);
  7907. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  7908. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  7909. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  7910. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  7911. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  7912. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  7913. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  7914. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  7915. seq_printf(m, "Plane [%d]:\n", i);
  7916. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  7917. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  7918. if (INTEL_INFO(dev)->gen <= 3) {
  7919. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  7920. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  7921. }
  7922. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  7923. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  7924. if (INTEL_INFO(dev)->gen >= 4) {
  7925. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  7926. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  7927. }
  7928. seq_printf(m, "Cursor [%d]:\n", i);
  7929. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  7930. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  7931. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  7932. }
  7933. }
  7934. #endif