intel_display.c 195 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370
  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 "drmP.h"
  35. #include "intel_drv.h"
  36. #include "i915_drm.h"
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include "drm_dp_helper.h"
  40. #include "drm_crtc_helper.h"
  41. #include <linux/dma_remapping.h>
  42. #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  43. bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
  44. static void intel_increase_pllclock(struct drm_crtc *crtc);
  45. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  46. typedef struct {
  47. /* given values */
  48. int n;
  49. int m1, m2;
  50. int p1, p2;
  51. /* derived values */
  52. int dot;
  53. int vco;
  54. int m;
  55. int p;
  56. } intel_clock_t;
  57. typedef struct {
  58. int min, max;
  59. } intel_range_t;
  60. typedef struct {
  61. int dot_limit;
  62. int p2_slow, p2_fast;
  63. } intel_p2_t;
  64. #define INTEL_P2_NUM 2
  65. typedef struct intel_limit intel_limit_t;
  66. struct intel_limit {
  67. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  68. intel_p2_t p2;
  69. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  70. int, int, intel_clock_t *, intel_clock_t *);
  71. };
  72. /* FDI */
  73. #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
  74. static bool
  75. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  76. int target, int refclk, intel_clock_t *match_clock,
  77. intel_clock_t *best_clock);
  78. static bool
  79. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  80. int target, int refclk, intel_clock_t *match_clock,
  81. intel_clock_t *best_clock);
  82. static bool
  83. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  84. int target, int refclk, intel_clock_t *match_clock,
  85. intel_clock_t *best_clock);
  86. static bool
  87. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  88. int target, int refclk, intel_clock_t *match_clock,
  89. intel_clock_t *best_clock);
  90. static bool
  91. intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
  92. int target, int refclk, intel_clock_t *match_clock,
  93. intel_clock_t *best_clock);
  94. static inline u32 /* units of 100MHz */
  95. intel_fdi_link_freq(struct drm_device *dev)
  96. {
  97. if (IS_GEN5(dev)) {
  98. struct drm_i915_private *dev_priv = dev->dev_private;
  99. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  100. } else
  101. return 27;
  102. }
  103. static const intel_limit_t intel_limits_i8xx_dvo = {
  104. .dot = { .min = 25000, .max = 350000 },
  105. .vco = { .min = 930000, .max = 1400000 },
  106. .n = { .min = 3, .max = 16 },
  107. .m = { .min = 96, .max = 140 },
  108. .m1 = { .min = 18, .max = 26 },
  109. .m2 = { .min = 6, .max = 16 },
  110. .p = { .min = 4, .max = 128 },
  111. .p1 = { .min = 2, .max = 33 },
  112. .p2 = { .dot_limit = 165000,
  113. .p2_slow = 4, .p2_fast = 2 },
  114. .find_pll = intel_find_best_PLL,
  115. };
  116. static const intel_limit_t intel_limits_i8xx_lvds = {
  117. .dot = { .min = 25000, .max = 350000 },
  118. .vco = { .min = 930000, .max = 1400000 },
  119. .n = { .min = 3, .max = 16 },
  120. .m = { .min = 96, .max = 140 },
  121. .m1 = { .min = 18, .max = 26 },
  122. .m2 = { .min = 6, .max = 16 },
  123. .p = { .min = 4, .max = 128 },
  124. .p1 = { .min = 1, .max = 6 },
  125. .p2 = { .dot_limit = 165000,
  126. .p2_slow = 14, .p2_fast = 7 },
  127. .find_pll = intel_find_best_PLL,
  128. };
  129. static const intel_limit_t intel_limits_i9xx_sdvo = {
  130. .dot = { .min = 20000, .max = 400000 },
  131. .vco = { .min = 1400000, .max = 2800000 },
  132. .n = { .min = 1, .max = 6 },
  133. .m = { .min = 70, .max = 120 },
  134. .m1 = { .min = 10, .max = 22 },
  135. .m2 = { .min = 5, .max = 9 },
  136. .p = { .min = 5, .max = 80 },
  137. .p1 = { .min = 1, .max = 8 },
  138. .p2 = { .dot_limit = 200000,
  139. .p2_slow = 10, .p2_fast = 5 },
  140. .find_pll = intel_find_best_PLL,
  141. };
  142. static const intel_limit_t intel_limits_i9xx_lvds = {
  143. .dot = { .min = 20000, .max = 400000 },
  144. .vco = { .min = 1400000, .max = 2800000 },
  145. .n = { .min = 1, .max = 6 },
  146. .m = { .min = 70, .max = 120 },
  147. .m1 = { .min = 10, .max = 22 },
  148. .m2 = { .min = 5, .max = 9 },
  149. .p = { .min = 7, .max = 98 },
  150. .p1 = { .min = 1, .max = 8 },
  151. .p2 = { .dot_limit = 112000,
  152. .p2_slow = 14, .p2_fast = 7 },
  153. .find_pll = intel_find_best_PLL,
  154. };
  155. static const intel_limit_t intel_limits_g4x_sdvo = {
  156. .dot = { .min = 25000, .max = 270000 },
  157. .vco = { .min = 1750000, .max = 3500000},
  158. .n = { .min = 1, .max = 4 },
  159. .m = { .min = 104, .max = 138 },
  160. .m1 = { .min = 17, .max = 23 },
  161. .m2 = { .min = 5, .max = 11 },
  162. .p = { .min = 10, .max = 30 },
  163. .p1 = { .min = 1, .max = 3},
  164. .p2 = { .dot_limit = 270000,
  165. .p2_slow = 10,
  166. .p2_fast = 10
  167. },
  168. .find_pll = intel_g4x_find_best_PLL,
  169. };
  170. static const intel_limit_t intel_limits_g4x_hdmi = {
  171. .dot = { .min = 22000, .max = 400000 },
  172. .vco = { .min = 1750000, .max = 3500000},
  173. .n = { .min = 1, .max = 4 },
  174. .m = { .min = 104, .max = 138 },
  175. .m1 = { .min = 16, .max = 23 },
  176. .m2 = { .min = 5, .max = 11 },
  177. .p = { .min = 5, .max = 80 },
  178. .p1 = { .min = 1, .max = 8},
  179. .p2 = { .dot_limit = 165000,
  180. .p2_slow = 10, .p2_fast = 5 },
  181. .find_pll = intel_g4x_find_best_PLL,
  182. };
  183. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  184. .dot = { .min = 20000, .max = 115000 },
  185. .vco = { .min = 1750000, .max = 3500000 },
  186. .n = { .min = 1, .max = 3 },
  187. .m = { .min = 104, .max = 138 },
  188. .m1 = { .min = 17, .max = 23 },
  189. .m2 = { .min = 5, .max = 11 },
  190. .p = { .min = 28, .max = 112 },
  191. .p1 = { .min = 2, .max = 8 },
  192. .p2 = { .dot_limit = 0,
  193. .p2_slow = 14, .p2_fast = 14
  194. },
  195. .find_pll = intel_g4x_find_best_PLL,
  196. };
  197. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  198. .dot = { .min = 80000, .max = 224000 },
  199. .vco = { .min = 1750000, .max = 3500000 },
  200. .n = { .min = 1, .max = 3 },
  201. .m = { .min = 104, .max = 138 },
  202. .m1 = { .min = 17, .max = 23 },
  203. .m2 = { .min = 5, .max = 11 },
  204. .p = { .min = 14, .max = 42 },
  205. .p1 = { .min = 2, .max = 6 },
  206. .p2 = { .dot_limit = 0,
  207. .p2_slow = 7, .p2_fast = 7
  208. },
  209. .find_pll = intel_g4x_find_best_PLL,
  210. };
  211. static const intel_limit_t intel_limits_g4x_display_port = {
  212. .dot = { .min = 161670, .max = 227000 },
  213. .vco = { .min = 1750000, .max = 3500000},
  214. .n = { .min = 1, .max = 2 },
  215. .m = { .min = 97, .max = 108 },
  216. .m1 = { .min = 0x10, .max = 0x12 },
  217. .m2 = { .min = 0x05, .max = 0x06 },
  218. .p = { .min = 10, .max = 20 },
  219. .p1 = { .min = 1, .max = 2},
  220. .p2 = { .dot_limit = 0,
  221. .p2_slow = 10, .p2_fast = 10 },
  222. .find_pll = intel_find_pll_g4x_dp,
  223. };
  224. static const intel_limit_t intel_limits_pineview_sdvo = {
  225. .dot = { .min = 20000, .max = 400000},
  226. .vco = { .min = 1700000, .max = 3500000 },
  227. /* Pineview's Ncounter is a ring counter */
  228. .n = { .min = 3, .max = 6 },
  229. .m = { .min = 2, .max = 256 },
  230. /* Pineview only has one combined m divider, which we treat as m2. */
  231. .m1 = { .min = 0, .max = 0 },
  232. .m2 = { .min = 0, .max = 254 },
  233. .p = { .min = 5, .max = 80 },
  234. .p1 = { .min = 1, .max = 8 },
  235. .p2 = { .dot_limit = 200000,
  236. .p2_slow = 10, .p2_fast = 5 },
  237. .find_pll = intel_find_best_PLL,
  238. };
  239. static const intel_limit_t intel_limits_pineview_lvds = {
  240. .dot = { .min = 20000, .max = 400000 },
  241. .vco = { .min = 1700000, .max = 3500000 },
  242. .n = { .min = 3, .max = 6 },
  243. .m = { .min = 2, .max = 256 },
  244. .m1 = { .min = 0, .max = 0 },
  245. .m2 = { .min = 0, .max = 254 },
  246. .p = { .min = 7, .max = 112 },
  247. .p1 = { .min = 1, .max = 8 },
  248. .p2 = { .dot_limit = 112000,
  249. .p2_slow = 14, .p2_fast = 14 },
  250. .find_pll = intel_find_best_PLL,
  251. };
  252. /* Ironlake / Sandybridge
  253. *
  254. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  255. * the range value for them is (actual_value - 2).
  256. */
  257. static const intel_limit_t intel_limits_ironlake_dac = {
  258. .dot = { .min = 25000, .max = 350000 },
  259. .vco = { .min = 1760000, .max = 3510000 },
  260. .n = { .min = 1, .max = 5 },
  261. .m = { .min = 79, .max = 127 },
  262. .m1 = { .min = 12, .max = 22 },
  263. .m2 = { .min = 5, .max = 9 },
  264. .p = { .min = 5, .max = 80 },
  265. .p1 = { .min = 1, .max = 8 },
  266. .p2 = { .dot_limit = 225000,
  267. .p2_slow = 10, .p2_fast = 5 },
  268. .find_pll = intel_g4x_find_best_PLL,
  269. };
  270. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  271. .dot = { .min = 25000, .max = 350000 },
  272. .vco = { .min = 1760000, .max = 3510000 },
  273. .n = { .min = 1, .max = 3 },
  274. .m = { .min = 79, .max = 118 },
  275. .m1 = { .min = 12, .max = 22 },
  276. .m2 = { .min = 5, .max = 9 },
  277. .p = { .min = 28, .max = 112 },
  278. .p1 = { .min = 2, .max = 8 },
  279. .p2 = { .dot_limit = 225000,
  280. .p2_slow = 14, .p2_fast = 14 },
  281. .find_pll = intel_g4x_find_best_PLL,
  282. };
  283. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  284. .dot = { .min = 25000, .max = 350000 },
  285. .vco = { .min = 1760000, .max = 3510000 },
  286. .n = { .min = 1, .max = 3 },
  287. .m = { .min = 79, .max = 127 },
  288. .m1 = { .min = 12, .max = 22 },
  289. .m2 = { .min = 5, .max = 9 },
  290. .p = { .min = 14, .max = 56 },
  291. .p1 = { .min = 2, .max = 8 },
  292. .p2 = { .dot_limit = 225000,
  293. .p2_slow = 7, .p2_fast = 7 },
  294. .find_pll = intel_g4x_find_best_PLL,
  295. };
  296. /* LVDS 100mhz refclk limits. */
  297. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  298. .dot = { .min = 25000, .max = 350000 },
  299. .vco = { .min = 1760000, .max = 3510000 },
  300. .n = { .min = 1, .max = 2 },
  301. .m = { .min = 79, .max = 126 },
  302. .m1 = { .min = 12, .max = 22 },
  303. .m2 = { .min = 5, .max = 9 },
  304. .p = { .min = 28, .max = 112 },
  305. .p1 = { .min = 2, .max = 8 },
  306. .p2 = { .dot_limit = 225000,
  307. .p2_slow = 14, .p2_fast = 14 },
  308. .find_pll = intel_g4x_find_best_PLL,
  309. };
  310. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  311. .dot = { .min = 25000, .max = 350000 },
  312. .vco = { .min = 1760000, .max = 3510000 },
  313. .n = { .min = 1, .max = 3 },
  314. .m = { .min = 79, .max = 126 },
  315. .m1 = { .min = 12, .max = 22 },
  316. .m2 = { .min = 5, .max = 9 },
  317. .p = { .min = 14, .max = 42 },
  318. .p1 = { .min = 2, .max = 6 },
  319. .p2 = { .dot_limit = 225000,
  320. .p2_slow = 7, .p2_fast = 7 },
  321. .find_pll = intel_g4x_find_best_PLL,
  322. };
  323. static const intel_limit_t intel_limits_ironlake_display_port = {
  324. .dot = { .min = 25000, .max = 350000 },
  325. .vco = { .min = 1760000, .max = 3510000},
  326. .n = { .min = 1, .max = 2 },
  327. .m = { .min = 81, .max = 90 },
  328. .m1 = { .min = 12, .max = 22 },
  329. .m2 = { .min = 5, .max = 9 },
  330. .p = { .min = 10, .max = 20 },
  331. .p1 = { .min = 1, .max = 2},
  332. .p2 = { .dot_limit = 0,
  333. .p2_slow = 10, .p2_fast = 10 },
  334. .find_pll = intel_find_pll_ironlake_dp,
  335. };
  336. static const intel_limit_t intel_limits_vlv_dac = {
  337. .dot = { .min = 25000, .max = 270000 },
  338. .vco = { .min = 4000000, .max = 6000000 },
  339. .n = { .min = 1, .max = 7 },
  340. .m = { .min = 22, .max = 450 }, /* guess */
  341. .m1 = { .min = 2, .max = 3 },
  342. .m2 = { .min = 11, .max = 156 },
  343. .p = { .min = 10, .max = 30 },
  344. .p1 = { .min = 2, .max = 3 },
  345. .p2 = { .dot_limit = 270000,
  346. .p2_slow = 2, .p2_fast = 20 },
  347. .find_pll = intel_vlv_find_best_pll,
  348. };
  349. static const intel_limit_t intel_limits_vlv_hdmi = {
  350. .dot = { .min = 20000, .max = 165000 },
  351. .vco = { .min = 5994000, .max = 4000000 },
  352. .n = { .min = 1, .max = 7 },
  353. .m = { .min = 60, .max = 300 }, /* guess */
  354. .m1 = { .min = 2, .max = 3 },
  355. .m2 = { .min = 11, .max = 156 },
  356. .p = { .min = 10, .max = 30 },
  357. .p1 = { .min = 2, .max = 3 },
  358. .p2 = { .dot_limit = 270000,
  359. .p2_slow = 2, .p2_fast = 20 },
  360. .find_pll = intel_vlv_find_best_pll,
  361. };
  362. static const intel_limit_t intel_limits_vlv_dp = {
  363. .dot = { .min = 162000, .max = 270000 },
  364. .vco = { .min = 5994000, .max = 4000000 },
  365. .n = { .min = 1, .max = 7 },
  366. .m = { .min = 60, .max = 300 }, /* guess */
  367. .m1 = { .min = 2, .max = 3 },
  368. .m2 = { .min = 11, .max = 156 },
  369. .p = { .min = 10, .max = 30 },
  370. .p1 = { .min = 2, .max = 3 },
  371. .p2 = { .dot_limit = 270000,
  372. .p2_slow = 2, .p2_fast = 20 },
  373. .find_pll = intel_vlv_find_best_pll,
  374. };
  375. u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
  376. {
  377. unsigned long flags;
  378. u32 val = 0;
  379. spin_lock_irqsave(&dev_priv->dpio_lock, flags);
  380. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
  381. DRM_ERROR("DPIO idle wait timed out\n");
  382. goto out_unlock;
  383. }
  384. I915_WRITE(DPIO_REG, reg);
  385. I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
  386. DPIO_BYTE);
  387. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
  388. DRM_ERROR("DPIO read wait timed out\n");
  389. goto out_unlock;
  390. }
  391. val = I915_READ(DPIO_DATA);
  392. out_unlock:
  393. spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
  394. return val;
  395. }
  396. static void intel_dpio_write(struct drm_i915_private *dev_priv, int reg,
  397. u32 val)
  398. {
  399. unsigned long flags;
  400. spin_lock_irqsave(&dev_priv->dpio_lock, flags);
  401. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
  402. DRM_ERROR("DPIO idle wait timed out\n");
  403. goto out_unlock;
  404. }
  405. I915_WRITE(DPIO_DATA, val);
  406. I915_WRITE(DPIO_REG, reg);
  407. I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_WRITE | DPIO_PORTID |
  408. DPIO_BYTE);
  409. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100))
  410. DRM_ERROR("DPIO write wait timed out\n");
  411. out_unlock:
  412. spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
  413. }
  414. static void vlv_init_dpio(struct drm_device *dev)
  415. {
  416. struct drm_i915_private *dev_priv = dev->dev_private;
  417. /* Reset the DPIO config */
  418. I915_WRITE(DPIO_CTL, 0);
  419. POSTING_READ(DPIO_CTL);
  420. I915_WRITE(DPIO_CTL, 1);
  421. POSTING_READ(DPIO_CTL);
  422. }
  423. static int intel_dual_link_lvds_callback(const struct dmi_system_id *id)
  424. {
  425. DRM_INFO("Forcing lvds to dual link mode on %s\n", id->ident);
  426. return 1;
  427. }
  428. static const struct dmi_system_id intel_dual_link_lvds[] = {
  429. {
  430. .callback = intel_dual_link_lvds_callback,
  431. .ident = "Apple MacBook Pro (Core i5/i7 Series)",
  432. .matches = {
  433. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  434. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro8,2"),
  435. },
  436. },
  437. { } /* terminating entry */
  438. };
  439. static bool is_dual_link_lvds(struct drm_i915_private *dev_priv,
  440. unsigned int reg)
  441. {
  442. unsigned int val;
  443. /* use the module option value if specified */
  444. if (i915_lvds_channel_mode > 0)
  445. return i915_lvds_channel_mode == 2;
  446. if (dmi_check_system(intel_dual_link_lvds))
  447. return true;
  448. if (dev_priv->lvds_val)
  449. val = dev_priv->lvds_val;
  450. else {
  451. /* BIOS should set the proper LVDS register value at boot, but
  452. * in reality, it doesn't set the value when the lid is closed;
  453. * we need to check "the value to be set" in VBT when LVDS
  454. * register is uninitialized.
  455. */
  456. val = I915_READ(reg);
  457. if (!(val & ~(LVDS_PIPE_MASK | LVDS_DETECTED)))
  458. val = dev_priv->bios_lvds_val;
  459. dev_priv->lvds_val = val;
  460. }
  461. return (val & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP;
  462. }
  463. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  464. int refclk)
  465. {
  466. struct drm_device *dev = crtc->dev;
  467. struct drm_i915_private *dev_priv = dev->dev_private;
  468. const intel_limit_t *limit;
  469. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  470. if (is_dual_link_lvds(dev_priv, PCH_LVDS)) {
  471. /* LVDS dual channel */
  472. if (refclk == 100000)
  473. limit = &intel_limits_ironlake_dual_lvds_100m;
  474. else
  475. limit = &intel_limits_ironlake_dual_lvds;
  476. } else {
  477. if (refclk == 100000)
  478. limit = &intel_limits_ironlake_single_lvds_100m;
  479. else
  480. limit = &intel_limits_ironlake_single_lvds;
  481. }
  482. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  483. HAS_eDP)
  484. limit = &intel_limits_ironlake_display_port;
  485. else
  486. limit = &intel_limits_ironlake_dac;
  487. return limit;
  488. }
  489. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  490. {
  491. struct drm_device *dev = crtc->dev;
  492. struct drm_i915_private *dev_priv = dev->dev_private;
  493. const intel_limit_t *limit;
  494. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  495. if (is_dual_link_lvds(dev_priv, LVDS))
  496. /* LVDS with dual channel */
  497. limit = &intel_limits_g4x_dual_channel_lvds;
  498. else
  499. /* LVDS with dual channel */
  500. limit = &intel_limits_g4x_single_channel_lvds;
  501. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  502. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  503. limit = &intel_limits_g4x_hdmi;
  504. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  505. limit = &intel_limits_g4x_sdvo;
  506. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  507. limit = &intel_limits_g4x_display_port;
  508. } else /* The option is for other outputs */
  509. limit = &intel_limits_i9xx_sdvo;
  510. return limit;
  511. }
  512. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  513. {
  514. struct drm_device *dev = crtc->dev;
  515. const intel_limit_t *limit;
  516. if (HAS_PCH_SPLIT(dev))
  517. limit = intel_ironlake_limit(crtc, refclk);
  518. else if (IS_G4X(dev)) {
  519. limit = intel_g4x_limit(crtc);
  520. } else if (IS_PINEVIEW(dev)) {
  521. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  522. limit = &intel_limits_pineview_lvds;
  523. else
  524. limit = &intel_limits_pineview_sdvo;
  525. } else if (IS_VALLEYVIEW(dev)) {
  526. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
  527. limit = &intel_limits_vlv_dac;
  528. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  529. limit = &intel_limits_vlv_hdmi;
  530. else
  531. limit = &intel_limits_vlv_dp;
  532. } else if (!IS_GEN2(dev)) {
  533. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  534. limit = &intel_limits_i9xx_lvds;
  535. else
  536. limit = &intel_limits_i9xx_sdvo;
  537. } else {
  538. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  539. limit = &intel_limits_i8xx_lvds;
  540. else
  541. limit = &intel_limits_i8xx_dvo;
  542. }
  543. return limit;
  544. }
  545. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  546. static void pineview_clock(int refclk, intel_clock_t *clock)
  547. {
  548. clock->m = clock->m2 + 2;
  549. clock->p = clock->p1 * clock->p2;
  550. clock->vco = refclk * clock->m / clock->n;
  551. clock->dot = clock->vco / clock->p;
  552. }
  553. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  554. {
  555. if (IS_PINEVIEW(dev)) {
  556. pineview_clock(refclk, clock);
  557. return;
  558. }
  559. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  560. clock->p = clock->p1 * clock->p2;
  561. clock->vco = refclk * clock->m / (clock->n + 2);
  562. clock->dot = clock->vco / clock->p;
  563. }
  564. /**
  565. * Returns whether any output on the specified pipe is of the specified type
  566. */
  567. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  568. {
  569. struct drm_device *dev = crtc->dev;
  570. struct intel_encoder *encoder;
  571. for_each_encoder_on_crtc(dev, crtc, encoder)
  572. if (encoder->type == type)
  573. return true;
  574. return false;
  575. }
  576. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  577. /**
  578. * Returns whether the given set of divisors are valid for a given refclk with
  579. * the given connectors.
  580. */
  581. static bool intel_PLL_is_valid(struct drm_device *dev,
  582. const intel_limit_t *limit,
  583. const intel_clock_t *clock)
  584. {
  585. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  586. INTELPllInvalid("p1 out of range\n");
  587. if (clock->p < limit->p.min || limit->p.max < clock->p)
  588. INTELPllInvalid("p out of range\n");
  589. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  590. INTELPllInvalid("m2 out of range\n");
  591. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  592. INTELPllInvalid("m1 out of range\n");
  593. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  594. INTELPllInvalid("m1 <= m2\n");
  595. if (clock->m < limit->m.min || limit->m.max < clock->m)
  596. INTELPllInvalid("m out of range\n");
  597. if (clock->n < limit->n.min || limit->n.max < clock->n)
  598. INTELPllInvalid("n out of range\n");
  599. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  600. INTELPllInvalid("vco out of range\n");
  601. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  602. * connector, etc., rather than just a single range.
  603. */
  604. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  605. INTELPllInvalid("dot out of range\n");
  606. return true;
  607. }
  608. static bool
  609. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  610. int target, int refclk, intel_clock_t *match_clock,
  611. intel_clock_t *best_clock)
  612. {
  613. struct drm_device *dev = crtc->dev;
  614. struct drm_i915_private *dev_priv = dev->dev_private;
  615. intel_clock_t clock;
  616. int err = target;
  617. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  618. (I915_READ(LVDS)) != 0) {
  619. /*
  620. * For LVDS, if the panel is on, just rely on its current
  621. * settings for dual-channel. We haven't figured out how to
  622. * reliably set up different single/dual channel state, if we
  623. * even can.
  624. */
  625. if (is_dual_link_lvds(dev_priv, LVDS))
  626. clock.p2 = limit->p2.p2_fast;
  627. else
  628. clock.p2 = limit->p2.p2_slow;
  629. } else {
  630. if (target < limit->p2.dot_limit)
  631. clock.p2 = limit->p2.p2_slow;
  632. else
  633. clock.p2 = limit->p2.p2_fast;
  634. }
  635. memset(best_clock, 0, sizeof(*best_clock));
  636. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  637. clock.m1++) {
  638. for (clock.m2 = limit->m2.min;
  639. clock.m2 <= limit->m2.max; clock.m2++) {
  640. /* m1 is always 0 in Pineview */
  641. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  642. break;
  643. for (clock.n = limit->n.min;
  644. clock.n <= limit->n.max; clock.n++) {
  645. for (clock.p1 = limit->p1.min;
  646. clock.p1 <= limit->p1.max; clock.p1++) {
  647. int this_err;
  648. intel_clock(dev, refclk, &clock);
  649. if (!intel_PLL_is_valid(dev, limit,
  650. &clock))
  651. continue;
  652. if (match_clock &&
  653. clock.p != match_clock->p)
  654. continue;
  655. this_err = abs(clock.dot - target);
  656. if (this_err < err) {
  657. *best_clock = clock;
  658. err = this_err;
  659. }
  660. }
  661. }
  662. }
  663. }
  664. return (err != target);
  665. }
  666. static bool
  667. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  668. int target, int refclk, intel_clock_t *match_clock,
  669. intel_clock_t *best_clock)
  670. {
  671. struct drm_device *dev = crtc->dev;
  672. struct drm_i915_private *dev_priv = dev->dev_private;
  673. intel_clock_t clock;
  674. int max_n;
  675. bool found;
  676. /* approximately equals target * 0.00585 */
  677. int err_most = (target >> 8) + (target >> 9);
  678. found = false;
  679. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  680. int lvds_reg;
  681. if (HAS_PCH_SPLIT(dev))
  682. lvds_reg = PCH_LVDS;
  683. else
  684. lvds_reg = LVDS;
  685. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  686. LVDS_CLKB_POWER_UP)
  687. clock.p2 = limit->p2.p2_fast;
  688. else
  689. clock.p2 = limit->p2.p2_slow;
  690. } else {
  691. if (target < limit->p2.dot_limit)
  692. clock.p2 = limit->p2.p2_slow;
  693. else
  694. clock.p2 = limit->p2.p2_fast;
  695. }
  696. memset(best_clock, 0, sizeof(*best_clock));
  697. max_n = limit->n.max;
  698. /* based on hardware requirement, prefer smaller n to precision */
  699. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  700. /* based on hardware requirement, prefere larger m1,m2 */
  701. for (clock.m1 = limit->m1.max;
  702. clock.m1 >= limit->m1.min; clock.m1--) {
  703. for (clock.m2 = limit->m2.max;
  704. clock.m2 >= limit->m2.min; clock.m2--) {
  705. for (clock.p1 = limit->p1.max;
  706. clock.p1 >= limit->p1.min; clock.p1--) {
  707. int this_err;
  708. intel_clock(dev, refclk, &clock);
  709. if (!intel_PLL_is_valid(dev, limit,
  710. &clock))
  711. continue;
  712. if (match_clock &&
  713. clock.p != match_clock->p)
  714. continue;
  715. this_err = abs(clock.dot - target);
  716. if (this_err < err_most) {
  717. *best_clock = clock;
  718. err_most = this_err;
  719. max_n = clock.n;
  720. found = true;
  721. }
  722. }
  723. }
  724. }
  725. }
  726. return found;
  727. }
  728. static bool
  729. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  730. int target, int refclk, intel_clock_t *match_clock,
  731. intel_clock_t *best_clock)
  732. {
  733. struct drm_device *dev = crtc->dev;
  734. intel_clock_t clock;
  735. if (target < 200000) {
  736. clock.n = 1;
  737. clock.p1 = 2;
  738. clock.p2 = 10;
  739. clock.m1 = 12;
  740. clock.m2 = 9;
  741. } else {
  742. clock.n = 2;
  743. clock.p1 = 1;
  744. clock.p2 = 10;
  745. clock.m1 = 14;
  746. clock.m2 = 8;
  747. }
  748. intel_clock(dev, refclk, &clock);
  749. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  750. return true;
  751. }
  752. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  753. static bool
  754. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  755. int target, int refclk, intel_clock_t *match_clock,
  756. intel_clock_t *best_clock)
  757. {
  758. intel_clock_t clock;
  759. if (target < 200000) {
  760. clock.p1 = 2;
  761. clock.p2 = 10;
  762. clock.n = 2;
  763. clock.m1 = 23;
  764. clock.m2 = 8;
  765. } else {
  766. clock.p1 = 1;
  767. clock.p2 = 10;
  768. clock.n = 1;
  769. clock.m1 = 14;
  770. clock.m2 = 2;
  771. }
  772. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  773. clock.p = (clock.p1 * clock.p2);
  774. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  775. clock.vco = 0;
  776. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  777. return true;
  778. }
  779. static bool
  780. intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
  781. int target, int refclk, intel_clock_t *match_clock,
  782. intel_clock_t *best_clock)
  783. {
  784. u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
  785. u32 m, n, fastclk;
  786. u32 updrate, minupdate, fracbits, p;
  787. unsigned long bestppm, ppm, absppm;
  788. int dotclk, flag;
  789. dotclk = target * 1000;
  790. bestppm = 1000000;
  791. ppm = absppm = 0;
  792. fastclk = dotclk / (2*100);
  793. updrate = 0;
  794. minupdate = 19200;
  795. fracbits = 1;
  796. n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
  797. bestm1 = bestm2 = bestp1 = bestp2 = 0;
  798. /* based on hardware requirement, prefer smaller n to precision */
  799. for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
  800. updrate = refclk / n;
  801. for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
  802. for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
  803. if (p2 > 10)
  804. p2 = p2 - 1;
  805. p = p1 * p2;
  806. /* based on hardware requirement, prefer bigger m1,m2 values */
  807. for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
  808. m2 = (((2*(fastclk * p * n / m1 )) +
  809. refclk) / (2*refclk));
  810. m = m1 * m2;
  811. vco = updrate * m;
  812. if (vco >= limit->vco.min && vco < limit->vco.max) {
  813. ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
  814. absppm = (ppm > 0) ? ppm : (-ppm);
  815. if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
  816. bestppm = 0;
  817. flag = 1;
  818. }
  819. if (absppm < bestppm - 10) {
  820. bestppm = absppm;
  821. flag = 1;
  822. }
  823. if (flag) {
  824. bestn = n;
  825. bestm1 = m1;
  826. bestm2 = m2;
  827. bestp1 = p1;
  828. bestp2 = p2;
  829. flag = 0;
  830. }
  831. }
  832. }
  833. }
  834. }
  835. }
  836. best_clock->n = bestn;
  837. best_clock->m1 = bestm1;
  838. best_clock->m2 = bestm2;
  839. best_clock->p1 = bestp1;
  840. best_clock->p2 = bestp2;
  841. return true;
  842. }
  843. static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
  844. {
  845. struct drm_i915_private *dev_priv = dev->dev_private;
  846. u32 frame, frame_reg = PIPEFRAME(pipe);
  847. frame = I915_READ(frame_reg);
  848. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  849. DRM_DEBUG_KMS("vblank wait timed out\n");
  850. }
  851. /**
  852. * intel_wait_for_vblank - wait for vblank on a given pipe
  853. * @dev: drm device
  854. * @pipe: pipe to wait for
  855. *
  856. * Wait for vblank to occur on a given pipe. Needed for various bits of
  857. * mode setting code.
  858. */
  859. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  860. {
  861. struct drm_i915_private *dev_priv = dev->dev_private;
  862. int pipestat_reg = PIPESTAT(pipe);
  863. if (INTEL_INFO(dev)->gen >= 5) {
  864. ironlake_wait_for_vblank(dev, pipe);
  865. return;
  866. }
  867. /* Clear existing vblank status. Note this will clear any other
  868. * sticky status fields as well.
  869. *
  870. * This races with i915_driver_irq_handler() with the result
  871. * that either function could miss a vblank event. Here it is not
  872. * fatal, as we will either wait upon the next vblank interrupt or
  873. * timeout. Generally speaking intel_wait_for_vblank() is only
  874. * called during modeset at which time the GPU should be idle and
  875. * should *not* be performing page flips and thus not waiting on
  876. * vblanks...
  877. * Currently, the result of us stealing a vblank from the irq
  878. * handler is that a single frame will be skipped during swapbuffers.
  879. */
  880. I915_WRITE(pipestat_reg,
  881. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  882. /* Wait for vblank interrupt bit to set */
  883. if (wait_for(I915_READ(pipestat_reg) &
  884. PIPE_VBLANK_INTERRUPT_STATUS,
  885. 50))
  886. DRM_DEBUG_KMS("vblank wait timed out\n");
  887. }
  888. /*
  889. * intel_wait_for_pipe_off - wait for pipe to turn off
  890. * @dev: drm device
  891. * @pipe: pipe to wait for
  892. *
  893. * After disabling a pipe, we can't wait for vblank in the usual way,
  894. * spinning on the vblank interrupt status bit, since we won't actually
  895. * see an interrupt when the pipe is disabled.
  896. *
  897. * On Gen4 and above:
  898. * wait for the pipe register state bit to turn off
  899. *
  900. * Otherwise:
  901. * wait for the display line value to settle (it usually
  902. * ends up stopping at the start of the next frame).
  903. *
  904. */
  905. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  906. {
  907. struct drm_i915_private *dev_priv = dev->dev_private;
  908. if (INTEL_INFO(dev)->gen >= 4) {
  909. int reg = PIPECONF(pipe);
  910. /* Wait for the Pipe State to go off */
  911. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  912. 100))
  913. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  914. } else {
  915. u32 last_line, line_mask;
  916. int reg = PIPEDSL(pipe);
  917. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  918. if (IS_GEN2(dev))
  919. line_mask = DSL_LINEMASK_GEN2;
  920. else
  921. line_mask = DSL_LINEMASK_GEN3;
  922. /* Wait for the display line to settle */
  923. do {
  924. last_line = I915_READ(reg) & line_mask;
  925. mdelay(5);
  926. } while (((I915_READ(reg) & line_mask) != last_line) &&
  927. time_after(timeout, jiffies));
  928. if (time_after(jiffies, timeout))
  929. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  930. }
  931. }
  932. static const char *state_string(bool enabled)
  933. {
  934. return enabled ? "on" : "off";
  935. }
  936. /* Only for pre-ILK configs */
  937. static void assert_pll(struct drm_i915_private *dev_priv,
  938. enum pipe pipe, bool state)
  939. {
  940. int reg;
  941. u32 val;
  942. bool cur_state;
  943. reg = DPLL(pipe);
  944. val = I915_READ(reg);
  945. cur_state = !!(val & DPLL_VCO_ENABLE);
  946. WARN(cur_state != state,
  947. "PLL state assertion failure (expected %s, current %s)\n",
  948. state_string(state), state_string(cur_state));
  949. }
  950. #define assert_pll_enabled(d, p) assert_pll(d, p, true)
  951. #define assert_pll_disabled(d, p) assert_pll(d, p, false)
  952. /* For ILK+ */
  953. static void assert_pch_pll(struct drm_i915_private *dev_priv,
  954. struct intel_pch_pll *pll,
  955. struct intel_crtc *crtc,
  956. bool state)
  957. {
  958. u32 val;
  959. bool cur_state;
  960. if (HAS_PCH_LPT(dev_priv->dev)) {
  961. DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
  962. return;
  963. }
  964. if (WARN (!pll,
  965. "asserting PCH PLL %s with no PLL\n", state_string(state)))
  966. return;
  967. val = I915_READ(pll->pll_reg);
  968. cur_state = !!(val & DPLL_VCO_ENABLE);
  969. WARN(cur_state != state,
  970. "PCH PLL state for reg %x assertion failure (expected %s, current %s), val=%08x\n",
  971. pll->pll_reg, state_string(state), state_string(cur_state), val);
  972. /* Make sure the selected PLL is correctly attached to the transcoder */
  973. if (crtc && HAS_PCH_CPT(dev_priv->dev)) {
  974. u32 pch_dpll;
  975. pch_dpll = I915_READ(PCH_DPLL_SEL);
  976. cur_state = pll->pll_reg == _PCH_DPLL_B;
  977. if (!WARN(((pch_dpll >> (4 * crtc->pipe)) & 1) != cur_state,
  978. "PLL[%d] not attached to this transcoder %d: %08x\n",
  979. cur_state, crtc->pipe, pch_dpll)) {
  980. cur_state = !!(val >> (4*crtc->pipe + 3));
  981. WARN(cur_state != state,
  982. "PLL[%d] not %s on this transcoder %d: %08x\n",
  983. pll->pll_reg == _PCH_DPLL_B,
  984. state_string(state),
  985. crtc->pipe,
  986. val);
  987. }
  988. }
  989. }
  990. #define assert_pch_pll_enabled(d, p, c) assert_pch_pll(d, p, c, true)
  991. #define assert_pch_pll_disabled(d, p, c) assert_pch_pll(d, p, c, false)
  992. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  993. enum pipe pipe, bool state)
  994. {
  995. int reg;
  996. u32 val;
  997. bool cur_state;
  998. if (IS_HASWELL(dev_priv->dev)) {
  999. /* On Haswell, DDI is used instead of FDI_TX_CTL */
  1000. reg = DDI_FUNC_CTL(pipe);
  1001. val = I915_READ(reg);
  1002. cur_state = !!(val & PIPE_DDI_FUNC_ENABLE);
  1003. } else {
  1004. reg = FDI_TX_CTL(pipe);
  1005. val = I915_READ(reg);
  1006. cur_state = !!(val & FDI_TX_ENABLE);
  1007. }
  1008. WARN(cur_state != state,
  1009. "FDI TX state assertion failure (expected %s, current %s)\n",
  1010. state_string(state), state_string(cur_state));
  1011. }
  1012. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1013. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1014. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1015. enum pipe pipe, bool state)
  1016. {
  1017. int reg;
  1018. u32 val;
  1019. bool cur_state;
  1020. if (IS_HASWELL(dev_priv->dev) && pipe > 0) {
  1021. DRM_ERROR("Attempting to enable FDI_RX on Haswell pipe > 0\n");
  1022. return;
  1023. } else {
  1024. reg = FDI_RX_CTL(pipe);
  1025. val = I915_READ(reg);
  1026. cur_state = !!(val & FDI_RX_ENABLE);
  1027. }
  1028. WARN(cur_state != state,
  1029. "FDI RX state assertion failure (expected %s, current %s)\n",
  1030. state_string(state), state_string(cur_state));
  1031. }
  1032. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1033. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1034. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1035. enum pipe pipe)
  1036. {
  1037. int reg;
  1038. u32 val;
  1039. /* ILK FDI PLL is always enabled */
  1040. if (dev_priv->info->gen == 5)
  1041. return;
  1042. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1043. if (IS_HASWELL(dev_priv->dev))
  1044. return;
  1045. reg = FDI_TX_CTL(pipe);
  1046. val = I915_READ(reg);
  1047. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1048. }
  1049. static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
  1050. enum pipe pipe)
  1051. {
  1052. int reg;
  1053. u32 val;
  1054. if (IS_HASWELL(dev_priv->dev) && pipe > 0) {
  1055. DRM_ERROR("Attempting to enable FDI on Haswell with pipe > 0\n");
  1056. return;
  1057. }
  1058. reg = FDI_RX_CTL(pipe);
  1059. val = I915_READ(reg);
  1060. WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
  1061. }
  1062. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1063. enum pipe pipe)
  1064. {
  1065. int pp_reg, lvds_reg;
  1066. u32 val;
  1067. enum pipe panel_pipe = PIPE_A;
  1068. bool locked = true;
  1069. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  1070. pp_reg = PCH_PP_CONTROL;
  1071. lvds_reg = PCH_LVDS;
  1072. } else {
  1073. pp_reg = PP_CONTROL;
  1074. lvds_reg = LVDS;
  1075. }
  1076. val = I915_READ(pp_reg);
  1077. if (!(val & PANEL_POWER_ON) ||
  1078. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  1079. locked = false;
  1080. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  1081. panel_pipe = PIPE_B;
  1082. WARN(panel_pipe == pipe && locked,
  1083. "panel assertion failure, pipe %c regs locked\n",
  1084. pipe_name(pipe));
  1085. }
  1086. void assert_pipe(struct drm_i915_private *dev_priv,
  1087. enum pipe pipe, bool state)
  1088. {
  1089. int reg;
  1090. u32 val;
  1091. bool cur_state;
  1092. /* if we need the pipe A quirk it must be always on */
  1093. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  1094. state = true;
  1095. reg = PIPECONF(pipe);
  1096. val = I915_READ(reg);
  1097. cur_state = !!(val & PIPECONF_ENABLE);
  1098. WARN(cur_state != state,
  1099. "pipe %c assertion failure (expected %s, current %s)\n",
  1100. pipe_name(pipe), state_string(state), state_string(cur_state));
  1101. }
  1102. static void assert_plane(struct drm_i915_private *dev_priv,
  1103. enum plane plane, bool state)
  1104. {
  1105. int reg;
  1106. u32 val;
  1107. bool cur_state;
  1108. reg = DSPCNTR(plane);
  1109. val = I915_READ(reg);
  1110. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1111. WARN(cur_state != state,
  1112. "plane %c assertion failure (expected %s, current %s)\n",
  1113. plane_name(plane), state_string(state), state_string(cur_state));
  1114. }
  1115. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1116. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1117. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1118. enum pipe pipe)
  1119. {
  1120. int reg, i;
  1121. u32 val;
  1122. int cur_pipe;
  1123. /* Planes are fixed to pipes on ILK+ */
  1124. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  1125. reg = DSPCNTR(pipe);
  1126. val = I915_READ(reg);
  1127. WARN((val & DISPLAY_PLANE_ENABLE),
  1128. "plane %c assertion failure, should be disabled but not\n",
  1129. plane_name(pipe));
  1130. return;
  1131. }
  1132. /* Need to check both planes against the pipe */
  1133. for (i = 0; i < 2; i++) {
  1134. reg = DSPCNTR(i);
  1135. val = I915_READ(reg);
  1136. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1137. DISPPLANE_SEL_PIPE_SHIFT;
  1138. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1139. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1140. plane_name(i), pipe_name(pipe));
  1141. }
  1142. }
  1143. static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1144. {
  1145. u32 val;
  1146. bool enabled;
  1147. if (HAS_PCH_LPT(dev_priv->dev)) {
  1148. DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
  1149. return;
  1150. }
  1151. val = I915_READ(PCH_DREF_CONTROL);
  1152. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1153. DREF_SUPERSPREAD_SOURCE_MASK));
  1154. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1155. }
  1156. static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
  1157. enum pipe pipe)
  1158. {
  1159. int reg;
  1160. u32 val;
  1161. bool enabled;
  1162. reg = TRANSCONF(pipe);
  1163. val = I915_READ(reg);
  1164. enabled = !!(val & TRANS_ENABLE);
  1165. WARN(enabled,
  1166. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1167. pipe_name(pipe));
  1168. }
  1169. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1170. enum pipe pipe, u32 port_sel, u32 val)
  1171. {
  1172. if ((val & DP_PORT_EN) == 0)
  1173. return false;
  1174. if (HAS_PCH_CPT(dev_priv->dev)) {
  1175. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1176. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1177. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1178. return false;
  1179. } else {
  1180. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1181. return false;
  1182. }
  1183. return true;
  1184. }
  1185. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1186. enum pipe pipe, u32 val)
  1187. {
  1188. if ((val & PORT_ENABLE) == 0)
  1189. return false;
  1190. if (HAS_PCH_CPT(dev_priv->dev)) {
  1191. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1192. return false;
  1193. } else {
  1194. if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
  1195. return false;
  1196. }
  1197. return true;
  1198. }
  1199. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1200. enum pipe pipe, u32 val)
  1201. {
  1202. if ((val & LVDS_PORT_EN) == 0)
  1203. return false;
  1204. if (HAS_PCH_CPT(dev_priv->dev)) {
  1205. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1206. return false;
  1207. } else {
  1208. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1209. return false;
  1210. }
  1211. return true;
  1212. }
  1213. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1214. enum pipe pipe, u32 val)
  1215. {
  1216. if ((val & ADPA_DAC_ENABLE) == 0)
  1217. return false;
  1218. if (HAS_PCH_CPT(dev_priv->dev)) {
  1219. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1220. return false;
  1221. } else {
  1222. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1223. return false;
  1224. }
  1225. return true;
  1226. }
  1227. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1228. enum pipe pipe, int reg, u32 port_sel)
  1229. {
  1230. u32 val = I915_READ(reg);
  1231. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1232. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1233. reg, pipe_name(pipe));
  1234. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_PIPE_B_SELECT),
  1235. "IBX PCH dp port still using transcoder B\n");
  1236. }
  1237. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1238. enum pipe pipe, int reg)
  1239. {
  1240. u32 val = I915_READ(reg);
  1241. WARN(hdmi_pipe_enabled(dev_priv, val, pipe),
  1242. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1243. reg, pipe_name(pipe));
  1244. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_PIPE_B_SELECT),
  1245. "IBX PCH hdmi port still using transcoder B\n");
  1246. }
  1247. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1248. enum pipe pipe)
  1249. {
  1250. int reg;
  1251. u32 val;
  1252. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1253. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1254. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1255. reg = PCH_ADPA;
  1256. val = I915_READ(reg);
  1257. WARN(adpa_pipe_enabled(dev_priv, val, pipe),
  1258. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1259. pipe_name(pipe));
  1260. reg = PCH_LVDS;
  1261. val = I915_READ(reg);
  1262. WARN(lvds_pipe_enabled(dev_priv, val, pipe),
  1263. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1264. pipe_name(pipe));
  1265. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
  1266. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
  1267. assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
  1268. }
  1269. /**
  1270. * intel_enable_pll - enable a PLL
  1271. * @dev_priv: i915 private structure
  1272. * @pipe: pipe PLL to enable
  1273. *
  1274. * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
  1275. * make sure the PLL reg is writable first though, since the panel write
  1276. * protect mechanism may be enabled.
  1277. *
  1278. * Note! This is for pre-ILK only.
  1279. *
  1280. * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
  1281. */
  1282. void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1283. {
  1284. int reg;
  1285. u32 val;
  1286. /* No really, not for ILK+ */
  1287. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
  1288. /* PLL is protected by panel, make sure we can write it */
  1289. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1290. assert_panel_unlocked(dev_priv, pipe);
  1291. reg = DPLL(pipe);
  1292. val = I915_READ(reg);
  1293. val |= DPLL_VCO_ENABLE;
  1294. /* We do this three times for luck */
  1295. I915_WRITE(reg, val);
  1296. POSTING_READ(reg);
  1297. udelay(150); /* wait for warmup */
  1298. I915_WRITE(reg, val);
  1299. POSTING_READ(reg);
  1300. udelay(150); /* wait for warmup */
  1301. I915_WRITE(reg, val);
  1302. POSTING_READ(reg);
  1303. udelay(150); /* wait for warmup */
  1304. }
  1305. /**
  1306. * intel_disable_pll - disable a PLL
  1307. * @dev_priv: i915 private structure
  1308. * @pipe: pipe PLL to disable
  1309. *
  1310. * Disable the PLL for @pipe, making sure the pipe is off first.
  1311. *
  1312. * Note! This is for pre-ILK only.
  1313. */
  1314. static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1315. {
  1316. int reg;
  1317. u32 val;
  1318. /* Don't disable pipe A or pipe A PLLs if needed */
  1319. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1320. return;
  1321. /* Make sure the pipe isn't still relying on us */
  1322. assert_pipe_disabled(dev_priv, pipe);
  1323. reg = DPLL(pipe);
  1324. val = I915_READ(reg);
  1325. val &= ~DPLL_VCO_ENABLE;
  1326. I915_WRITE(reg, val);
  1327. POSTING_READ(reg);
  1328. }
  1329. /* SBI access */
  1330. static void
  1331. intel_sbi_write(struct drm_i915_private *dev_priv, u16 reg, u32 value)
  1332. {
  1333. unsigned long flags;
  1334. spin_lock_irqsave(&dev_priv->dpio_lock, flags);
  1335. if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
  1336. 100)) {
  1337. DRM_ERROR("timeout waiting for SBI to become ready\n");
  1338. goto out_unlock;
  1339. }
  1340. I915_WRITE(SBI_ADDR,
  1341. (reg << 16));
  1342. I915_WRITE(SBI_DATA,
  1343. value);
  1344. I915_WRITE(SBI_CTL_STAT,
  1345. SBI_BUSY |
  1346. SBI_CTL_OP_CRWR);
  1347. if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
  1348. 100)) {
  1349. DRM_ERROR("timeout waiting for SBI to complete write transaction\n");
  1350. goto out_unlock;
  1351. }
  1352. out_unlock:
  1353. spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
  1354. }
  1355. static u32
  1356. intel_sbi_read(struct drm_i915_private *dev_priv, u16 reg)
  1357. {
  1358. unsigned long flags;
  1359. u32 value = 0;
  1360. spin_lock_irqsave(&dev_priv->dpio_lock, flags);
  1361. if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
  1362. 100)) {
  1363. DRM_ERROR("timeout waiting for SBI to become ready\n");
  1364. goto out_unlock;
  1365. }
  1366. I915_WRITE(SBI_ADDR,
  1367. (reg << 16));
  1368. I915_WRITE(SBI_CTL_STAT,
  1369. SBI_BUSY |
  1370. SBI_CTL_OP_CRRD);
  1371. if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
  1372. 100)) {
  1373. DRM_ERROR("timeout waiting for SBI to complete read transaction\n");
  1374. goto out_unlock;
  1375. }
  1376. value = I915_READ(SBI_DATA);
  1377. out_unlock:
  1378. spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
  1379. return value;
  1380. }
  1381. /**
  1382. * intel_enable_pch_pll - enable PCH PLL
  1383. * @dev_priv: i915 private structure
  1384. * @pipe: pipe PLL to enable
  1385. *
  1386. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1387. * drives the transcoder clock.
  1388. */
  1389. static void intel_enable_pch_pll(struct intel_crtc *intel_crtc)
  1390. {
  1391. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  1392. struct intel_pch_pll *pll;
  1393. int reg;
  1394. u32 val;
  1395. /* PCH PLLs only available on ILK, SNB and IVB */
  1396. BUG_ON(dev_priv->info->gen < 5);
  1397. pll = intel_crtc->pch_pll;
  1398. if (pll == NULL)
  1399. return;
  1400. if (WARN_ON(pll->refcount == 0))
  1401. return;
  1402. DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
  1403. pll->pll_reg, pll->active, pll->on,
  1404. intel_crtc->base.base.id);
  1405. /* PCH refclock must be enabled first */
  1406. assert_pch_refclk_enabled(dev_priv);
  1407. if (pll->active++ && pll->on) {
  1408. assert_pch_pll_enabled(dev_priv, pll, NULL);
  1409. return;
  1410. }
  1411. DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
  1412. reg = pll->pll_reg;
  1413. val = I915_READ(reg);
  1414. val |= DPLL_VCO_ENABLE;
  1415. I915_WRITE(reg, val);
  1416. POSTING_READ(reg);
  1417. udelay(200);
  1418. pll->on = true;
  1419. }
  1420. static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
  1421. {
  1422. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  1423. struct intel_pch_pll *pll = intel_crtc->pch_pll;
  1424. int reg;
  1425. u32 val;
  1426. /* PCH only available on ILK+ */
  1427. BUG_ON(dev_priv->info->gen < 5);
  1428. if (pll == NULL)
  1429. return;
  1430. if (WARN_ON(pll->refcount == 0))
  1431. return;
  1432. DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
  1433. pll->pll_reg, pll->active, pll->on,
  1434. intel_crtc->base.base.id);
  1435. if (WARN_ON(pll->active == 0)) {
  1436. assert_pch_pll_disabled(dev_priv, pll, NULL);
  1437. return;
  1438. }
  1439. if (--pll->active) {
  1440. assert_pch_pll_enabled(dev_priv, pll, NULL);
  1441. return;
  1442. }
  1443. DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
  1444. /* Make sure transcoder isn't still depending on us */
  1445. assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
  1446. reg = pll->pll_reg;
  1447. val = I915_READ(reg);
  1448. val &= ~DPLL_VCO_ENABLE;
  1449. I915_WRITE(reg, val);
  1450. POSTING_READ(reg);
  1451. udelay(200);
  1452. pll->on = false;
  1453. }
  1454. static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
  1455. enum pipe pipe)
  1456. {
  1457. int reg;
  1458. u32 val, pipeconf_val;
  1459. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1460. /* PCH only available on ILK+ */
  1461. BUG_ON(dev_priv->info->gen < 5);
  1462. /* Make sure PCH DPLL is enabled */
  1463. assert_pch_pll_enabled(dev_priv,
  1464. to_intel_crtc(crtc)->pch_pll,
  1465. to_intel_crtc(crtc));
  1466. /* FDI must be feeding us bits for PCH ports */
  1467. assert_fdi_tx_enabled(dev_priv, pipe);
  1468. assert_fdi_rx_enabled(dev_priv, pipe);
  1469. if (IS_HASWELL(dev_priv->dev) && pipe > 0) {
  1470. DRM_ERROR("Attempting to enable transcoder on Haswell with pipe > 0\n");
  1471. return;
  1472. }
  1473. reg = TRANSCONF(pipe);
  1474. val = I915_READ(reg);
  1475. pipeconf_val = I915_READ(PIPECONF(pipe));
  1476. if (HAS_PCH_IBX(dev_priv->dev)) {
  1477. /*
  1478. * make the BPC in transcoder be consistent with
  1479. * that in pipeconf reg.
  1480. */
  1481. val &= ~PIPE_BPC_MASK;
  1482. val |= pipeconf_val & PIPE_BPC_MASK;
  1483. }
  1484. val &= ~TRANS_INTERLACE_MASK;
  1485. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1486. if (HAS_PCH_IBX(dev_priv->dev) &&
  1487. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1488. val |= TRANS_LEGACY_INTERLACED_ILK;
  1489. else
  1490. val |= TRANS_INTERLACED;
  1491. else
  1492. val |= TRANS_PROGRESSIVE;
  1493. I915_WRITE(reg, val | TRANS_ENABLE);
  1494. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1495. DRM_ERROR("failed to enable transcoder %d\n", pipe);
  1496. }
  1497. static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
  1498. enum pipe pipe)
  1499. {
  1500. int reg;
  1501. u32 val;
  1502. /* FDI relies on the transcoder */
  1503. assert_fdi_tx_disabled(dev_priv, pipe);
  1504. assert_fdi_rx_disabled(dev_priv, pipe);
  1505. /* Ports must be off as well */
  1506. assert_pch_ports_disabled(dev_priv, pipe);
  1507. reg = TRANSCONF(pipe);
  1508. val = I915_READ(reg);
  1509. val &= ~TRANS_ENABLE;
  1510. I915_WRITE(reg, val);
  1511. /* wait for PCH transcoder off, transcoder state */
  1512. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1513. DRM_ERROR("failed to disable transcoder %d\n", pipe);
  1514. }
  1515. /**
  1516. * intel_enable_pipe - enable a pipe, asserting requirements
  1517. * @dev_priv: i915 private structure
  1518. * @pipe: pipe to enable
  1519. * @pch_port: on ILK+, is this pipe driving a PCH port or not
  1520. *
  1521. * Enable @pipe, making sure that various hardware specific requirements
  1522. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1523. *
  1524. * @pipe should be %PIPE_A or %PIPE_B.
  1525. *
  1526. * Will wait until the pipe is actually running (i.e. first vblank) before
  1527. * returning.
  1528. */
  1529. static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
  1530. bool pch_port)
  1531. {
  1532. int reg;
  1533. u32 val;
  1534. /*
  1535. * A pipe without a PLL won't actually be able to drive bits from
  1536. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1537. * need the check.
  1538. */
  1539. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1540. assert_pll_enabled(dev_priv, pipe);
  1541. else {
  1542. if (pch_port) {
  1543. /* if driving the PCH, we need FDI enabled */
  1544. assert_fdi_rx_pll_enabled(dev_priv, pipe);
  1545. assert_fdi_tx_pll_enabled(dev_priv, pipe);
  1546. }
  1547. /* FIXME: assert CPU port conditions for SNB+ */
  1548. }
  1549. reg = PIPECONF(pipe);
  1550. val = I915_READ(reg);
  1551. if (val & PIPECONF_ENABLE)
  1552. return;
  1553. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1554. intel_wait_for_vblank(dev_priv->dev, pipe);
  1555. }
  1556. /**
  1557. * intel_disable_pipe - disable a pipe, asserting requirements
  1558. * @dev_priv: i915 private structure
  1559. * @pipe: pipe to disable
  1560. *
  1561. * Disable @pipe, making sure that various hardware specific requirements
  1562. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1563. *
  1564. * @pipe should be %PIPE_A or %PIPE_B.
  1565. *
  1566. * Will wait until the pipe has shut down before returning.
  1567. */
  1568. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1569. enum pipe pipe)
  1570. {
  1571. int reg;
  1572. u32 val;
  1573. /*
  1574. * Make sure planes won't keep trying to pump pixels to us,
  1575. * or we might hang the display.
  1576. */
  1577. assert_planes_disabled(dev_priv, pipe);
  1578. /* Don't disable pipe A or pipe A PLLs if needed */
  1579. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1580. return;
  1581. reg = PIPECONF(pipe);
  1582. val = I915_READ(reg);
  1583. if ((val & PIPECONF_ENABLE) == 0)
  1584. return;
  1585. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1586. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1587. }
  1588. /*
  1589. * Plane regs are double buffered, going from enabled->disabled needs a
  1590. * trigger in order to latch. The display address reg provides this.
  1591. */
  1592. void intel_flush_display_plane(struct drm_i915_private *dev_priv,
  1593. enum plane plane)
  1594. {
  1595. I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
  1596. I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
  1597. }
  1598. /**
  1599. * intel_enable_plane - enable a display plane on a given pipe
  1600. * @dev_priv: i915 private structure
  1601. * @plane: plane to enable
  1602. * @pipe: pipe being fed
  1603. *
  1604. * Enable @plane on @pipe, making sure that @pipe is running first.
  1605. */
  1606. static void intel_enable_plane(struct drm_i915_private *dev_priv,
  1607. enum plane plane, enum pipe pipe)
  1608. {
  1609. int reg;
  1610. u32 val;
  1611. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1612. assert_pipe_enabled(dev_priv, pipe);
  1613. reg = DSPCNTR(plane);
  1614. val = I915_READ(reg);
  1615. if (val & DISPLAY_PLANE_ENABLE)
  1616. return;
  1617. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1618. intel_flush_display_plane(dev_priv, plane);
  1619. intel_wait_for_vblank(dev_priv->dev, pipe);
  1620. }
  1621. /**
  1622. * intel_disable_plane - disable a display plane
  1623. * @dev_priv: i915 private structure
  1624. * @plane: plane to disable
  1625. * @pipe: pipe consuming the data
  1626. *
  1627. * Disable @plane; should be an independent operation.
  1628. */
  1629. static void intel_disable_plane(struct drm_i915_private *dev_priv,
  1630. enum plane plane, enum pipe pipe)
  1631. {
  1632. int reg;
  1633. u32 val;
  1634. reg = DSPCNTR(plane);
  1635. val = I915_READ(reg);
  1636. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  1637. return;
  1638. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1639. intel_flush_display_plane(dev_priv, plane);
  1640. intel_wait_for_vblank(dev_priv->dev, pipe);
  1641. }
  1642. static void disable_pch_dp(struct drm_i915_private *dev_priv,
  1643. enum pipe pipe, int reg, u32 port_sel)
  1644. {
  1645. u32 val = I915_READ(reg);
  1646. if (dp_pipe_enabled(dev_priv, pipe, port_sel, val)) {
  1647. DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
  1648. I915_WRITE(reg, val & ~DP_PORT_EN);
  1649. }
  1650. }
  1651. static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
  1652. enum pipe pipe, int reg)
  1653. {
  1654. u32 val = I915_READ(reg);
  1655. if (hdmi_pipe_enabled(dev_priv, val, pipe)) {
  1656. DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
  1657. reg, pipe);
  1658. I915_WRITE(reg, val & ~PORT_ENABLE);
  1659. }
  1660. }
  1661. /* Disable any ports connected to this transcoder */
  1662. static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
  1663. enum pipe pipe)
  1664. {
  1665. u32 reg, val;
  1666. val = I915_READ(PCH_PP_CONTROL);
  1667. I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
  1668. disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1669. disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1670. disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1671. reg = PCH_ADPA;
  1672. val = I915_READ(reg);
  1673. if (adpa_pipe_enabled(dev_priv, val, pipe))
  1674. I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
  1675. reg = PCH_LVDS;
  1676. val = I915_READ(reg);
  1677. if (lvds_pipe_enabled(dev_priv, val, pipe)) {
  1678. DRM_DEBUG_KMS("disable lvds on pipe %d val 0x%08x\n", pipe, val);
  1679. I915_WRITE(reg, val & ~LVDS_PORT_EN);
  1680. POSTING_READ(reg);
  1681. udelay(100);
  1682. }
  1683. disable_pch_hdmi(dev_priv, pipe, HDMIB);
  1684. disable_pch_hdmi(dev_priv, pipe, HDMIC);
  1685. disable_pch_hdmi(dev_priv, pipe, HDMID);
  1686. }
  1687. int
  1688. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1689. struct drm_i915_gem_object *obj,
  1690. struct intel_ring_buffer *pipelined)
  1691. {
  1692. struct drm_i915_private *dev_priv = dev->dev_private;
  1693. u32 alignment;
  1694. int ret;
  1695. switch (obj->tiling_mode) {
  1696. case I915_TILING_NONE:
  1697. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1698. alignment = 128 * 1024;
  1699. else if (INTEL_INFO(dev)->gen >= 4)
  1700. alignment = 4 * 1024;
  1701. else
  1702. alignment = 64 * 1024;
  1703. break;
  1704. case I915_TILING_X:
  1705. /* pin() will align the object as required by fence */
  1706. alignment = 0;
  1707. break;
  1708. case I915_TILING_Y:
  1709. /* FIXME: Is this true? */
  1710. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1711. return -EINVAL;
  1712. default:
  1713. BUG();
  1714. }
  1715. dev_priv->mm.interruptible = false;
  1716. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1717. if (ret)
  1718. goto err_interruptible;
  1719. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1720. * fence, whereas 965+ only requires a fence if using
  1721. * framebuffer compression. For simplicity, we always install
  1722. * a fence as the cost is not that onerous.
  1723. */
  1724. ret = i915_gem_object_get_fence(obj);
  1725. if (ret)
  1726. goto err_unpin;
  1727. i915_gem_object_pin_fence(obj);
  1728. dev_priv->mm.interruptible = true;
  1729. return 0;
  1730. err_unpin:
  1731. i915_gem_object_unpin(obj);
  1732. err_interruptible:
  1733. dev_priv->mm.interruptible = true;
  1734. return ret;
  1735. }
  1736. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1737. {
  1738. i915_gem_object_unpin_fence(obj);
  1739. i915_gem_object_unpin(obj);
  1740. }
  1741. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1742. * is assumed to be a power-of-two. */
  1743. static unsigned long gen4_compute_dspaddr_offset_xtiled(int *x, int *y,
  1744. unsigned int bpp,
  1745. unsigned int pitch)
  1746. {
  1747. int tile_rows, tiles;
  1748. tile_rows = *y / 8;
  1749. *y %= 8;
  1750. tiles = *x / (512/bpp);
  1751. *x %= 512/bpp;
  1752. return tile_rows * pitch * 8 + tiles * 4096;
  1753. }
  1754. static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1755. int x, int y)
  1756. {
  1757. struct drm_device *dev = crtc->dev;
  1758. struct drm_i915_private *dev_priv = dev->dev_private;
  1759. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1760. struct intel_framebuffer *intel_fb;
  1761. struct drm_i915_gem_object *obj;
  1762. int plane = intel_crtc->plane;
  1763. unsigned long linear_offset;
  1764. u32 dspcntr;
  1765. u32 reg;
  1766. switch (plane) {
  1767. case 0:
  1768. case 1:
  1769. break;
  1770. default:
  1771. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1772. return -EINVAL;
  1773. }
  1774. intel_fb = to_intel_framebuffer(fb);
  1775. obj = intel_fb->obj;
  1776. reg = DSPCNTR(plane);
  1777. dspcntr = I915_READ(reg);
  1778. /* Mask out pixel format bits in case we change it */
  1779. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1780. switch (fb->bits_per_pixel) {
  1781. case 8:
  1782. dspcntr |= DISPPLANE_8BPP;
  1783. break;
  1784. case 16:
  1785. if (fb->depth == 15)
  1786. dspcntr |= DISPPLANE_15_16BPP;
  1787. else
  1788. dspcntr |= DISPPLANE_16BPP;
  1789. break;
  1790. case 24:
  1791. case 32:
  1792. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1793. break;
  1794. default:
  1795. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1796. return -EINVAL;
  1797. }
  1798. if (INTEL_INFO(dev)->gen >= 4) {
  1799. if (obj->tiling_mode != I915_TILING_NONE)
  1800. dspcntr |= DISPPLANE_TILED;
  1801. else
  1802. dspcntr &= ~DISPPLANE_TILED;
  1803. }
  1804. I915_WRITE(reg, dspcntr);
  1805. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1806. if (INTEL_INFO(dev)->gen >= 4) {
  1807. intel_crtc->dspaddr_offset =
  1808. gen4_compute_dspaddr_offset_xtiled(&x, &y,
  1809. fb->bits_per_pixel / 8,
  1810. fb->pitches[0]);
  1811. linear_offset -= intel_crtc->dspaddr_offset;
  1812. } else {
  1813. intel_crtc->dspaddr_offset = linear_offset;
  1814. }
  1815. DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
  1816. obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
  1817. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1818. if (INTEL_INFO(dev)->gen >= 4) {
  1819. I915_MODIFY_DISPBASE(DSPSURF(plane),
  1820. obj->gtt_offset + intel_crtc->dspaddr_offset);
  1821. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1822. I915_WRITE(DSPLINOFF(plane), linear_offset);
  1823. } else
  1824. I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
  1825. POSTING_READ(reg);
  1826. return 0;
  1827. }
  1828. static int ironlake_update_plane(struct drm_crtc *crtc,
  1829. struct drm_framebuffer *fb, int x, int y)
  1830. {
  1831. struct drm_device *dev = crtc->dev;
  1832. struct drm_i915_private *dev_priv = dev->dev_private;
  1833. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1834. struct intel_framebuffer *intel_fb;
  1835. struct drm_i915_gem_object *obj;
  1836. int plane = intel_crtc->plane;
  1837. unsigned long linear_offset;
  1838. u32 dspcntr;
  1839. u32 reg;
  1840. switch (plane) {
  1841. case 0:
  1842. case 1:
  1843. case 2:
  1844. break;
  1845. default:
  1846. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1847. return -EINVAL;
  1848. }
  1849. intel_fb = to_intel_framebuffer(fb);
  1850. obj = intel_fb->obj;
  1851. reg = DSPCNTR(plane);
  1852. dspcntr = I915_READ(reg);
  1853. /* Mask out pixel format bits in case we change it */
  1854. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1855. switch (fb->bits_per_pixel) {
  1856. case 8:
  1857. dspcntr |= DISPPLANE_8BPP;
  1858. break;
  1859. case 16:
  1860. if (fb->depth != 16)
  1861. return -EINVAL;
  1862. dspcntr |= DISPPLANE_16BPP;
  1863. break;
  1864. case 24:
  1865. case 32:
  1866. if (fb->depth == 24)
  1867. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1868. else if (fb->depth == 30)
  1869. dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
  1870. else
  1871. return -EINVAL;
  1872. break;
  1873. default:
  1874. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1875. return -EINVAL;
  1876. }
  1877. if (obj->tiling_mode != I915_TILING_NONE)
  1878. dspcntr |= DISPPLANE_TILED;
  1879. else
  1880. dspcntr &= ~DISPPLANE_TILED;
  1881. /* must disable */
  1882. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1883. I915_WRITE(reg, dspcntr);
  1884. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1885. intel_crtc->dspaddr_offset =
  1886. gen4_compute_dspaddr_offset_xtiled(&x, &y,
  1887. fb->bits_per_pixel / 8,
  1888. fb->pitches[0]);
  1889. linear_offset -= intel_crtc->dspaddr_offset;
  1890. DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
  1891. obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
  1892. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1893. I915_MODIFY_DISPBASE(DSPSURF(plane),
  1894. obj->gtt_offset + intel_crtc->dspaddr_offset);
  1895. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1896. I915_WRITE(DSPLINOFF(plane), linear_offset);
  1897. POSTING_READ(reg);
  1898. return 0;
  1899. }
  1900. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1901. static int
  1902. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1903. int x, int y, enum mode_set_atomic state)
  1904. {
  1905. struct drm_device *dev = crtc->dev;
  1906. struct drm_i915_private *dev_priv = dev->dev_private;
  1907. if (dev_priv->display.disable_fbc)
  1908. dev_priv->display.disable_fbc(dev);
  1909. intel_increase_pllclock(crtc);
  1910. return dev_priv->display.update_plane(crtc, fb, x, y);
  1911. }
  1912. static int
  1913. intel_finish_fb(struct drm_framebuffer *old_fb)
  1914. {
  1915. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1916. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1917. bool was_interruptible = dev_priv->mm.interruptible;
  1918. int ret;
  1919. wait_event(dev_priv->pending_flip_queue,
  1920. atomic_read(&dev_priv->mm.wedged) ||
  1921. atomic_read(&obj->pending_flip) == 0);
  1922. /* Big Hammer, we also need to ensure that any pending
  1923. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1924. * current scanout is retired before unpinning the old
  1925. * framebuffer.
  1926. *
  1927. * This should only fail upon a hung GPU, in which case we
  1928. * can safely continue.
  1929. */
  1930. dev_priv->mm.interruptible = false;
  1931. ret = i915_gem_object_finish_gpu(obj);
  1932. dev_priv->mm.interruptible = was_interruptible;
  1933. return ret;
  1934. }
  1935. static int
  1936. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1937. struct drm_framebuffer *old_fb)
  1938. {
  1939. struct drm_device *dev = crtc->dev;
  1940. struct drm_i915_private *dev_priv = dev->dev_private;
  1941. struct drm_i915_master_private *master_priv;
  1942. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1943. int ret;
  1944. /* no fb bound */
  1945. if (!crtc->fb) {
  1946. DRM_ERROR("No FB bound\n");
  1947. return 0;
  1948. }
  1949. if(intel_crtc->plane > dev_priv->num_pipe) {
  1950. DRM_ERROR("no plane for crtc: plane %d, num_pipes %d\n",
  1951. intel_crtc->plane,
  1952. dev_priv->num_pipe);
  1953. return -EINVAL;
  1954. }
  1955. mutex_lock(&dev->struct_mutex);
  1956. ret = intel_pin_and_fence_fb_obj(dev,
  1957. to_intel_framebuffer(crtc->fb)->obj,
  1958. NULL);
  1959. if (ret != 0) {
  1960. mutex_unlock(&dev->struct_mutex);
  1961. DRM_ERROR("pin & fence failed\n");
  1962. return ret;
  1963. }
  1964. if (old_fb)
  1965. intel_finish_fb(old_fb);
  1966. ret = dev_priv->display.update_plane(crtc, crtc->fb, x, y);
  1967. if (ret) {
  1968. intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
  1969. mutex_unlock(&dev->struct_mutex);
  1970. DRM_ERROR("failed to update base address\n");
  1971. return ret;
  1972. }
  1973. if (old_fb) {
  1974. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1975. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  1976. }
  1977. intel_update_fbc(dev);
  1978. mutex_unlock(&dev->struct_mutex);
  1979. if (!dev->primary->master)
  1980. return 0;
  1981. master_priv = dev->primary->master->driver_priv;
  1982. if (!master_priv->sarea_priv)
  1983. return 0;
  1984. if (intel_crtc->pipe) {
  1985. master_priv->sarea_priv->pipeB_x = x;
  1986. master_priv->sarea_priv->pipeB_y = y;
  1987. } else {
  1988. master_priv->sarea_priv->pipeA_x = x;
  1989. master_priv->sarea_priv->pipeA_y = y;
  1990. }
  1991. return 0;
  1992. }
  1993. static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
  1994. {
  1995. struct drm_device *dev = crtc->dev;
  1996. struct drm_i915_private *dev_priv = dev->dev_private;
  1997. u32 dpa_ctl;
  1998. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1999. dpa_ctl = I915_READ(DP_A);
  2000. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  2001. if (clock < 200000) {
  2002. u32 temp;
  2003. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  2004. /* workaround for 160Mhz:
  2005. 1) program 0x4600c bits 15:0 = 0x8124
  2006. 2) program 0x46010 bit 0 = 1
  2007. 3) program 0x46034 bit 24 = 1
  2008. 4) program 0x64000 bit 14 = 1
  2009. */
  2010. temp = I915_READ(0x4600c);
  2011. temp &= 0xffff0000;
  2012. I915_WRITE(0x4600c, temp | 0x8124);
  2013. temp = I915_READ(0x46010);
  2014. I915_WRITE(0x46010, temp | 1);
  2015. temp = I915_READ(0x46034);
  2016. I915_WRITE(0x46034, temp | (1 << 24));
  2017. } else {
  2018. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  2019. }
  2020. I915_WRITE(DP_A, dpa_ctl);
  2021. POSTING_READ(DP_A);
  2022. udelay(500);
  2023. }
  2024. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2025. {
  2026. struct drm_device *dev = crtc->dev;
  2027. struct drm_i915_private *dev_priv = dev->dev_private;
  2028. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2029. int pipe = intel_crtc->pipe;
  2030. u32 reg, temp;
  2031. /* enable normal train */
  2032. reg = FDI_TX_CTL(pipe);
  2033. temp = I915_READ(reg);
  2034. if (IS_IVYBRIDGE(dev)) {
  2035. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2036. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2037. } else {
  2038. temp &= ~FDI_LINK_TRAIN_NONE;
  2039. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2040. }
  2041. I915_WRITE(reg, temp);
  2042. reg = FDI_RX_CTL(pipe);
  2043. temp = I915_READ(reg);
  2044. if (HAS_PCH_CPT(dev)) {
  2045. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2046. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2047. } else {
  2048. temp &= ~FDI_LINK_TRAIN_NONE;
  2049. temp |= FDI_LINK_TRAIN_NONE;
  2050. }
  2051. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2052. /* wait one idle pattern time */
  2053. POSTING_READ(reg);
  2054. udelay(1000);
  2055. /* IVB wants error correction enabled */
  2056. if (IS_IVYBRIDGE(dev))
  2057. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2058. FDI_FE_ERRC_ENABLE);
  2059. }
  2060. static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
  2061. {
  2062. struct drm_i915_private *dev_priv = dev->dev_private;
  2063. u32 flags = I915_READ(SOUTH_CHICKEN1);
  2064. flags |= FDI_PHASE_SYNC_OVR(pipe);
  2065. I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
  2066. flags |= FDI_PHASE_SYNC_EN(pipe);
  2067. I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
  2068. POSTING_READ(SOUTH_CHICKEN1);
  2069. }
  2070. /* The FDI link training functions for ILK/Ibexpeak. */
  2071. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2072. {
  2073. struct drm_device *dev = crtc->dev;
  2074. struct drm_i915_private *dev_priv = dev->dev_private;
  2075. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2076. int pipe = intel_crtc->pipe;
  2077. int plane = intel_crtc->plane;
  2078. u32 reg, temp, tries;
  2079. /* FDI needs bits from pipe & plane first */
  2080. assert_pipe_enabled(dev_priv, pipe);
  2081. assert_plane_enabled(dev_priv, plane);
  2082. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2083. for train result */
  2084. reg = FDI_RX_IMR(pipe);
  2085. temp = I915_READ(reg);
  2086. temp &= ~FDI_RX_SYMBOL_LOCK;
  2087. temp &= ~FDI_RX_BIT_LOCK;
  2088. I915_WRITE(reg, temp);
  2089. I915_READ(reg);
  2090. udelay(150);
  2091. /* enable CPU FDI TX and PCH FDI RX */
  2092. reg = FDI_TX_CTL(pipe);
  2093. temp = I915_READ(reg);
  2094. temp &= ~(7 << 19);
  2095. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2096. temp &= ~FDI_LINK_TRAIN_NONE;
  2097. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2098. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2099. reg = FDI_RX_CTL(pipe);
  2100. temp = I915_READ(reg);
  2101. temp &= ~FDI_LINK_TRAIN_NONE;
  2102. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2103. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2104. POSTING_READ(reg);
  2105. udelay(150);
  2106. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2107. if (HAS_PCH_IBX(dev)) {
  2108. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2109. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2110. FDI_RX_PHASE_SYNC_POINTER_EN);
  2111. }
  2112. reg = FDI_RX_IIR(pipe);
  2113. for (tries = 0; tries < 5; tries++) {
  2114. temp = I915_READ(reg);
  2115. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2116. if ((temp & FDI_RX_BIT_LOCK)) {
  2117. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2118. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2119. break;
  2120. }
  2121. }
  2122. if (tries == 5)
  2123. DRM_ERROR("FDI train 1 fail!\n");
  2124. /* Train 2 */
  2125. reg = FDI_TX_CTL(pipe);
  2126. temp = I915_READ(reg);
  2127. temp &= ~FDI_LINK_TRAIN_NONE;
  2128. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2129. I915_WRITE(reg, temp);
  2130. reg = FDI_RX_CTL(pipe);
  2131. temp = I915_READ(reg);
  2132. temp &= ~FDI_LINK_TRAIN_NONE;
  2133. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2134. I915_WRITE(reg, temp);
  2135. POSTING_READ(reg);
  2136. udelay(150);
  2137. reg = FDI_RX_IIR(pipe);
  2138. for (tries = 0; tries < 5; tries++) {
  2139. temp = I915_READ(reg);
  2140. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2141. if (temp & FDI_RX_SYMBOL_LOCK) {
  2142. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2143. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2144. break;
  2145. }
  2146. }
  2147. if (tries == 5)
  2148. DRM_ERROR("FDI train 2 fail!\n");
  2149. DRM_DEBUG_KMS("FDI train done\n");
  2150. }
  2151. static const int snb_b_fdi_train_param[] = {
  2152. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2153. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2154. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2155. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2156. };
  2157. /* The FDI link training functions for SNB/Cougarpoint. */
  2158. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2159. {
  2160. struct drm_device *dev = crtc->dev;
  2161. struct drm_i915_private *dev_priv = dev->dev_private;
  2162. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2163. int pipe = intel_crtc->pipe;
  2164. u32 reg, temp, i, retry;
  2165. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2166. for train result */
  2167. reg = FDI_RX_IMR(pipe);
  2168. temp = I915_READ(reg);
  2169. temp &= ~FDI_RX_SYMBOL_LOCK;
  2170. temp &= ~FDI_RX_BIT_LOCK;
  2171. I915_WRITE(reg, temp);
  2172. POSTING_READ(reg);
  2173. udelay(150);
  2174. /* enable CPU FDI TX and PCH FDI RX */
  2175. reg = FDI_TX_CTL(pipe);
  2176. temp = I915_READ(reg);
  2177. temp &= ~(7 << 19);
  2178. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2179. temp &= ~FDI_LINK_TRAIN_NONE;
  2180. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2181. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2182. /* SNB-B */
  2183. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2184. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2185. reg = FDI_RX_CTL(pipe);
  2186. temp = I915_READ(reg);
  2187. if (HAS_PCH_CPT(dev)) {
  2188. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2189. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2190. } else {
  2191. temp &= ~FDI_LINK_TRAIN_NONE;
  2192. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2193. }
  2194. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2195. POSTING_READ(reg);
  2196. udelay(150);
  2197. if (HAS_PCH_CPT(dev))
  2198. cpt_phase_pointer_enable(dev, pipe);
  2199. for (i = 0; i < 4; i++) {
  2200. reg = FDI_TX_CTL(pipe);
  2201. temp = I915_READ(reg);
  2202. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2203. temp |= snb_b_fdi_train_param[i];
  2204. I915_WRITE(reg, temp);
  2205. POSTING_READ(reg);
  2206. udelay(500);
  2207. for (retry = 0; retry < 5; retry++) {
  2208. reg = FDI_RX_IIR(pipe);
  2209. temp = I915_READ(reg);
  2210. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2211. if (temp & FDI_RX_BIT_LOCK) {
  2212. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2213. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2214. break;
  2215. }
  2216. udelay(50);
  2217. }
  2218. if (retry < 5)
  2219. break;
  2220. }
  2221. if (i == 4)
  2222. DRM_ERROR("FDI train 1 fail!\n");
  2223. /* Train 2 */
  2224. reg = FDI_TX_CTL(pipe);
  2225. temp = I915_READ(reg);
  2226. temp &= ~FDI_LINK_TRAIN_NONE;
  2227. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2228. if (IS_GEN6(dev)) {
  2229. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2230. /* SNB-B */
  2231. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2232. }
  2233. I915_WRITE(reg, temp);
  2234. reg = FDI_RX_CTL(pipe);
  2235. temp = I915_READ(reg);
  2236. if (HAS_PCH_CPT(dev)) {
  2237. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2238. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2239. } else {
  2240. temp &= ~FDI_LINK_TRAIN_NONE;
  2241. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2242. }
  2243. I915_WRITE(reg, temp);
  2244. POSTING_READ(reg);
  2245. udelay(150);
  2246. for (i = 0; i < 4; i++) {
  2247. reg = FDI_TX_CTL(pipe);
  2248. temp = I915_READ(reg);
  2249. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2250. temp |= snb_b_fdi_train_param[i];
  2251. I915_WRITE(reg, temp);
  2252. POSTING_READ(reg);
  2253. udelay(500);
  2254. for (retry = 0; retry < 5; retry++) {
  2255. reg = FDI_RX_IIR(pipe);
  2256. temp = I915_READ(reg);
  2257. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2258. if (temp & FDI_RX_SYMBOL_LOCK) {
  2259. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2260. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2261. break;
  2262. }
  2263. udelay(50);
  2264. }
  2265. if (retry < 5)
  2266. break;
  2267. }
  2268. if (i == 4)
  2269. DRM_ERROR("FDI train 2 fail!\n");
  2270. DRM_DEBUG_KMS("FDI train done.\n");
  2271. }
  2272. /* Manual link training for Ivy Bridge A0 parts */
  2273. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2274. {
  2275. struct drm_device *dev = crtc->dev;
  2276. struct drm_i915_private *dev_priv = dev->dev_private;
  2277. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2278. int pipe = intel_crtc->pipe;
  2279. u32 reg, temp, i;
  2280. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2281. for train result */
  2282. reg = FDI_RX_IMR(pipe);
  2283. temp = I915_READ(reg);
  2284. temp &= ~FDI_RX_SYMBOL_LOCK;
  2285. temp &= ~FDI_RX_BIT_LOCK;
  2286. I915_WRITE(reg, temp);
  2287. POSTING_READ(reg);
  2288. udelay(150);
  2289. /* enable CPU FDI TX and PCH FDI RX */
  2290. reg = FDI_TX_CTL(pipe);
  2291. temp = I915_READ(reg);
  2292. temp &= ~(7 << 19);
  2293. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2294. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2295. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2296. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2297. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2298. temp |= FDI_COMPOSITE_SYNC;
  2299. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2300. reg = FDI_RX_CTL(pipe);
  2301. temp = I915_READ(reg);
  2302. temp &= ~FDI_LINK_TRAIN_AUTO;
  2303. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2304. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2305. temp |= FDI_COMPOSITE_SYNC;
  2306. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2307. POSTING_READ(reg);
  2308. udelay(150);
  2309. if (HAS_PCH_CPT(dev))
  2310. cpt_phase_pointer_enable(dev, pipe);
  2311. for (i = 0; i < 4; i++) {
  2312. reg = FDI_TX_CTL(pipe);
  2313. temp = I915_READ(reg);
  2314. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2315. temp |= snb_b_fdi_train_param[i];
  2316. I915_WRITE(reg, temp);
  2317. POSTING_READ(reg);
  2318. udelay(500);
  2319. reg = FDI_RX_IIR(pipe);
  2320. temp = I915_READ(reg);
  2321. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2322. if (temp & FDI_RX_BIT_LOCK ||
  2323. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2324. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2325. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2326. break;
  2327. }
  2328. }
  2329. if (i == 4)
  2330. DRM_ERROR("FDI train 1 fail!\n");
  2331. /* Train 2 */
  2332. reg = FDI_TX_CTL(pipe);
  2333. temp = I915_READ(reg);
  2334. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2335. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2336. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2337. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2338. I915_WRITE(reg, temp);
  2339. reg = FDI_RX_CTL(pipe);
  2340. temp = I915_READ(reg);
  2341. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2342. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2343. I915_WRITE(reg, temp);
  2344. POSTING_READ(reg);
  2345. udelay(150);
  2346. for (i = 0; i < 4; i++) {
  2347. reg = FDI_TX_CTL(pipe);
  2348. temp = I915_READ(reg);
  2349. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2350. temp |= snb_b_fdi_train_param[i];
  2351. I915_WRITE(reg, temp);
  2352. POSTING_READ(reg);
  2353. udelay(500);
  2354. reg = FDI_RX_IIR(pipe);
  2355. temp = I915_READ(reg);
  2356. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2357. if (temp & FDI_RX_SYMBOL_LOCK) {
  2358. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2359. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2360. break;
  2361. }
  2362. }
  2363. if (i == 4)
  2364. DRM_ERROR("FDI train 2 fail!\n");
  2365. DRM_DEBUG_KMS("FDI train done.\n");
  2366. }
  2367. static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
  2368. {
  2369. struct drm_device *dev = crtc->dev;
  2370. struct drm_i915_private *dev_priv = dev->dev_private;
  2371. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2372. int pipe = intel_crtc->pipe;
  2373. u32 reg, temp;
  2374. /* Write the TU size bits so error detection works */
  2375. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2376. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2377. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2378. reg = FDI_RX_CTL(pipe);
  2379. temp = I915_READ(reg);
  2380. temp &= ~((0x7 << 19) | (0x7 << 16));
  2381. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2382. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2383. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2384. POSTING_READ(reg);
  2385. udelay(200);
  2386. /* Switch from Rawclk to PCDclk */
  2387. temp = I915_READ(reg);
  2388. I915_WRITE(reg, temp | FDI_PCDCLK);
  2389. POSTING_READ(reg);
  2390. udelay(200);
  2391. /* On Haswell, the PLL configuration for ports and pipes is handled
  2392. * separately, as part of DDI setup */
  2393. if (!IS_HASWELL(dev)) {
  2394. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2395. reg = FDI_TX_CTL(pipe);
  2396. temp = I915_READ(reg);
  2397. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2398. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2399. POSTING_READ(reg);
  2400. udelay(100);
  2401. }
  2402. }
  2403. }
  2404. static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
  2405. {
  2406. struct drm_i915_private *dev_priv = dev->dev_private;
  2407. u32 flags = I915_READ(SOUTH_CHICKEN1);
  2408. flags &= ~(FDI_PHASE_SYNC_EN(pipe));
  2409. I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
  2410. flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
  2411. I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
  2412. POSTING_READ(SOUTH_CHICKEN1);
  2413. }
  2414. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2415. {
  2416. struct drm_device *dev = crtc->dev;
  2417. struct drm_i915_private *dev_priv = dev->dev_private;
  2418. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2419. int pipe = intel_crtc->pipe;
  2420. u32 reg, temp;
  2421. /* disable CPU FDI tx and PCH FDI rx */
  2422. reg = FDI_TX_CTL(pipe);
  2423. temp = I915_READ(reg);
  2424. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2425. POSTING_READ(reg);
  2426. reg = FDI_RX_CTL(pipe);
  2427. temp = I915_READ(reg);
  2428. temp &= ~(0x7 << 16);
  2429. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2430. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2431. POSTING_READ(reg);
  2432. udelay(100);
  2433. /* Ironlake workaround, disable clock pointer after downing FDI */
  2434. if (HAS_PCH_IBX(dev)) {
  2435. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2436. I915_WRITE(FDI_RX_CHICKEN(pipe),
  2437. I915_READ(FDI_RX_CHICKEN(pipe) &
  2438. ~FDI_RX_PHASE_SYNC_POINTER_EN));
  2439. } else if (HAS_PCH_CPT(dev)) {
  2440. cpt_phase_pointer_disable(dev, pipe);
  2441. }
  2442. /* still set train pattern 1 */
  2443. reg = FDI_TX_CTL(pipe);
  2444. temp = I915_READ(reg);
  2445. temp &= ~FDI_LINK_TRAIN_NONE;
  2446. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2447. I915_WRITE(reg, temp);
  2448. reg = FDI_RX_CTL(pipe);
  2449. temp = I915_READ(reg);
  2450. if (HAS_PCH_CPT(dev)) {
  2451. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2452. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2453. } else {
  2454. temp &= ~FDI_LINK_TRAIN_NONE;
  2455. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2456. }
  2457. /* BPC in FDI rx is consistent with that in PIPECONF */
  2458. temp &= ~(0x07 << 16);
  2459. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2460. I915_WRITE(reg, temp);
  2461. POSTING_READ(reg);
  2462. udelay(100);
  2463. }
  2464. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2465. {
  2466. struct drm_device *dev = crtc->dev;
  2467. if (crtc->fb == NULL)
  2468. return;
  2469. mutex_lock(&dev->struct_mutex);
  2470. intel_finish_fb(crtc->fb);
  2471. mutex_unlock(&dev->struct_mutex);
  2472. }
  2473. static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
  2474. {
  2475. struct drm_device *dev = crtc->dev;
  2476. struct intel_encoder *encoder;
  2477. /*
  2478. * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
  2479. * must be driven by its own crtc; no sharing is possible.
  2480. */
  2481. for_each_encoder_on_crtc(dev, crtc, encoder) {
  2482. /* On Haswell, LPT PCH handles the VGA connection via FDI, and Haswell
  2483. * CPU handles all others */
  2484. if (IS_HASWELL(dev)) {
  2485. /* It is still unclear how this will work on PPT, so throw up a warning */
  2486. WARN_ON(!HAS_PCH_LPT(dev));
  2487. if (encoder->type == DRM_MODE_ENCODER_DAC) {
  2488. DRM_DEBUG_KMS("Haswell detected DAC encoder, assuming is PCH\n");
  2489. return true;
  2490. } else {
  2491. DRM_DEBUG_KMS("Haswell detected encoder %d, assuming is CPU\n",
  2492. encoder->type);
  2493. return false;
  2494. }
  2495. }
  2496. switch (encoder->type) {
  2497. case INTEL_OUTPUT_EDP:
  2498. if (!intel_encoder_is_pch_edp(&encoder->base))
  2499. return false;
  2500. continue;
  2501. }
  2502. }
  2503. return true;
  2504. }
  2505. /* Program iCLKIP clock to the desired frequency */
  2506. static void lpt_program_iclkip(struct drm_crtc *crtc)
  2507. {
  2508. struct drm_device *dev = crtc->dev;
  2509. struct drm_i915_private *dev_priv = dev->dev_private;
  2510. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  2511. u32 temp;
  2512. /* It is necessary to ungate the pixclk gate prior to programming
  2513. * the divisors, and gate it back when it is done.
  2514. */
  2515. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  2516. /* Disable SSCCTL */
  2517. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  2518. intel_sbi_read(dev_priv, SBI_SSCCTL6) |
  2519. SBI_SSCCTL_DISABLE);
  2520. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  2521. if (crtc->mode.clock == 20000) {
  2522. auxdiv = 1;
  2523. divsel = 0x41;
  2524. phaseinc = 0x20;
  2525. } else {
  2526. /* The iCLK virtual clock root frequency is in MHz,
  2527. * but the crtc->mode.clock in in KHz. To get the divisors,
  2528. * it is necessary to divide one by another, so we
  2529. * convert the virtual clock precision to KHz here for higher
  2530. * precision.
  2531. */
  2532. u32 iclk_virtual_root_freq = 172800 * 1000;
  2533. u32 iclk_pi_range = 64;
  2534. u32 desired_divisor, msb_divisor_value, pi_value;
  2535. desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
  2536. msb_divisor_value = desired_divisor / iclk_pi_range;
  2537. pi_value = desired_divisor % iclk_pi_range;
  2538. auxdiv = 0;
  2539. divsel = msb_divisor_value - 2;
  2540. phaseinc = pi_value;
  2541. }
  2542. /* This should not happen with any sane values */
  2543. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  2544. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  2545. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  2546. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  2547. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  2548. crtc->mode.clock,
  2549. auxdiv,
  2550. divsel,
  2551. phasedir,
  2552. phaseinc);
  2553. /* Program SSCDIVINTPHASE6 */
  2554. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6);
  2555. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  2556. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  2557. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  2558. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  2559. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  2560. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  2561. intel_sbi_write(dev_priv,
  2562. SBI_SSCDIVINTPHASE6,
  2563. temp);
  2564. /* Program SSCAUXDIV */
  2565. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6);
  2566. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  2567. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  2568. intel_sbi_write(dev_priv,
  2569. SBI_SSCAUXDIV6,
  2570. temp);
  2571. /* Enable modulator and associated divider */
  2572. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6);
  2573. temp &= ~SBI_SSCCTL_DISABLE;
  2574. intel_sbi_write(dev_priv,
  2575. SBI_SSCCTL6,
  2576. temp);
  2577. /* Wait for initialization time */
  2578. udelay(24);
  2579. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  2580. }
  2581. /*
  2582. * Enable PCH resources required for PCH ports:
  2583. * - PCH PLLs
  2584. * - FDI training & RX/TX
  2585. * - update transcoder timings
  2586. * - DP transcoding bits
  2587. * - transcoder
  2588. */
  2589. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2590. {
  2591. struct drm_device *dev = crtc->dev;
  2592. struct drm_i915_private *dev_priv = dev->dev_private;
  2593. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2594. int pipe = intel_crtc->pipe;
  2595. u32 reg, temp;
  2596. assert_transcoder_disabled(dev_priv, pipe);
  2597. /* For PCH output, training FDI link */
  2598. dev_priv->display.fdi_link_train(crtc);
  2599. intel_enable_pch_pll(intel_crtc);
  2600. if (HAS_PCH_LPT(dev)) {
  2601. DRM_DEBUG_KMS("LPT detected: programming iCLKIP\n");
  2602. lpt_program_iclkip(crtc);
  2603. } else if (HAS_PCH_CPT(dev)) {
  2604. u32 sel;
  2605. temp = I915_READ(PCH_DPLL_SEL);
  2606. switch (pipe) {
  2607. default:
  2608. case 0:
  2609. temp |= TRANSA_DPLL_ENABLE;
  2610. sel = TRANSA_DPLLB_SEL;
  2611. break;
  2612. case 1:
  2613. temp |= TRANSB_DPLL_ENABLE;
  2614. sel = TRANSB_DPLLB_SEL;
  2615. break;
  2616. case 2:
  2617. temp |= TRANSC_DPLL_ENABLE;
  2618. sel = TRANSC_DPLLB_SEL;
  2619. break;
  2620. }
  2621. if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
  2622. temp |= sel;
  2623. else
  2624. temp &= ~sel;
  2625. I915_WRITE(PCH_DPLL_SEL, temp);
  2626. }
  2627. /* set transcoder timing, panel must allow it */
  2628. assert_panel_unlocked(dev_priv, pipe);
  2629. I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
  2630. I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
  2631. I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
  2632. I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
  2633. I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
  2634. I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
  2635. I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
  2636. if (!IS_HASWELL(dev))
  2637. intel_fdi_normal_train(crtc);
  2638. /* For PCH DP, enable TRANS_DP_CTL */
  2639. if (HAS_PCH_CPT(dev) &&
  2640. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  2641. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2642. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
  2643. reg = TRANS_DP_CTL(pipe);
  2644. temp = I915_READ(reg);
  2645. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  2646. TRANS_DP_SYNC_MASK |
  2647. TRANS_DP_BPC_MASK);
  2648. temp |= (TRANS_DP_OUTPUT_ENABLE |
  2649. TRANS_DP_ENH_FRAMING);
  2650. temp |= bpc << 9; /* same format but at 11:9 */
  2651. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  2652. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  2653. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  2654. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  2655. switch (intel_trans_dp_port_sel(crtc)) {
  2656. case PCH_DP_B:
  2657. temp |= TRANS_DP_PORT_SEL_B;
  2658. break;
  2659. case PCH_DP_C:
  2660. temp |= TRANS_DP_PORT_SEL_C;
  2661. break;
  2662. case PCH_DP_D:
  2663. temp |= TRANS_DP_PORT_SEL_D;
  2664. break;
  2665. default:
  2666. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  2667. temp |= TRANS_DP_PORT_SEL_B;
  2668. break;
  2669. }
  2670. I915_WRITE(reg, temp);
  2671. }
  2672. intel_enable_transcoder(dev_priv, pipe);
  2673. }
  2674. static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
  2675. {
  2676. struct intel_pch_pll *pll = intel_crtc->pch_pll;
  2677. if (pll == NULL)
  2678. return;
  2679. if (pll->refcount == 0) {
  2680. WARN(1, "bad PCH PLL refcount\n");
  2681. return;
  2682. }
  2683. --pll->refcount;
  2684. intel_crtc->pch_pll = NULL;
  2685. }
  2686. static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
  2687. {
  2688. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  2689. struct intel_pch_pll *pll;
  2690. int i;
  2691. pll = intel_crtc->pch_pll;
  2692. if (pll) {
  2693. DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
  2694. intel_crtc->base.base.id, pll->pll_reg);
  2695. goto prepare;
  2696. }
  2697. if (HAS_PCH_IBX(dev_priv->dev)) {
  2698. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  2699. i = intel_crtc->pipe;
  2700. pll = &dev_priv->pch_plls[i];
  2701. DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
  2702. intel_crtc->base.base.id, pll->pll_reg);
  2703. goto found;
  2704. }
  2705. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  2706. pll = &dev_priv->pch_plls[i];
  2707. /* Only want to check enabled timings first */
  2708. if (pll->refcount == 0)
  2709. continue;
  2710. if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
  2711. fp == I915_READ(pll->fp0_reg)) {
  2712. DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
  2713. intel_crtc->base.base.id,
  2714. pll->pll_reg, pll->refcount, pll->active);
  2715. goto found;
  2716. }
  2717. }
  2718. /* Ok no matching timings, maybe there's a free one? */
  2719. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  2720. pll = &dev_priv->pch_plls[i];
  2721. if (pll->refcount == 0) {
  2722. DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
  2723. intel_crtc->base.base.id, pll->pll_reg);
  2724. goto found;
  2725. }
  2726. }
  2727. return NULL;
  2728. found:
  2729. intel_crtc->pch_pll = pll;
  2730. pll->refcount++;
  2731. DRM_DEBUG_DRIVER("using pll %d for pipe %d\n", i, intel_crtc->pipe);
  2732. prepare: /* separate function? */
  2733. DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
  2734. /* Wait for the clocks to stabilize before rewriting the regs */
  2735. I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
  2736. POSTING_READ(pll->pll_reg);
  2737. udelay(150);
  2738. I915_WRITE(pll->fp0_reg, fp);
  2739. I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
  2740. pll->on = false;
  2741. return pll;
  2742. }
  2743. void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
  2744. {
  2745. struct drm_i915_private *dev_priv = dev->dev_private;
  2746. int dslreg = PIPEDSL(pipe), tc2reg = TRANS_CHICKEN2(pipe);
  2747. u32 temp;
  2748. temp = I915_READ(dslreg);
  2749. udelay(500);
  2750. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  2751. /* Without this, mode sets may fail silently on FDI */
  2752. I915_WRITE(tc2reg, TRANS_AUTOTRAIN_GEN_STALL_DIS);
  2753. udelay(250);
  2754. I915_WRITE(tc2reg, 0);
  2755. if (wait_for(I915_READ(dslreg) != temp, 5))
  2756. DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
  2757. }
  2758. }
  2759. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  2760. {
  2761. struct drm_device *dev = crtc->dev;
  2762. struct drm_i915_private *dev_priv = dev->dev_private;
  2763. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2764. int pipe = intel_crtc->pipe;
  2765. int plane = intel_crtc->plane;
  2766. u32 temp;
  2767. bool is_pch_port;
  2768. if (intel_crtc->active)
  2769. return;
  2770. intel_crtc->active = true;
  2771. intel_update_watermarks(dev);
  2772. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  2773. temp = I915_READ(PCH_LVDS);
  2774. if ((temp & LVDS_PORT_EN) == 0)
  2775. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  2776. }
  2777. is_pch_port = intel_crtc_driving_pch(crtc);
  2778. if (is_pch_port)
  2779. ironlake_fdi_pll_enable(crtc);
  2780. else
  2781. ironlake_fdi_disable(crtc);
  2782. /* Enable panel fitting for LVDS */
  2783. if (dev_priv->pch_pf_size &&
  2784. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
  2785. /* Force use of hard-coded filter coefficients
  2786. * as some pre-programmed values are broken,
  2787. * e.g. x201.
  2788. */
  2789. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  2790. I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
  2791. I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
  2792. }
  2793. /*
  2794. * On ILK+ LUT must be loaded before the pipe is running but with
  2795. * clocks enabled
  2796. */
  2797. intel_crtc_load_lut(crtc);
  2798. intel_enable_pipe(dev_priv, pipe, is_pch_port);
  2799. intel_enable_plane(dev_priv, plane, pipe);
  2800. if (is_pch_port)
  2801. ironlake_pch_enable(crtc);
  2802. mutex_lock(&dev->struct_mutex);
  2803. intel_update_fbc(dev);
  2804. mutex_unlock(&dev->struct_mutex);
  2805. intel_crtc_update_cursor(crtc, true);
  2806. }
  2807. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  2808. {
  2809. struct drm_device *dev = crtc->dev;
  2810. struct drm_i915_private *dev_priv = dev->dev_private;
  2811. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2812. int pipe = intel_crtc->pipe;
  2813. int plane = intel_crtc->plane;
  2814. u32 reg, temp;
  2815. if (!intel_crtc->active)
  2816. return;
  2817. intel_crtc_wait_for_pending_flips(crtc);
  2818. drm_vblank_off(dev, pipe);
  2819. intel_crtc_update_cursor(crtc, false);
  2820. intel_disable_plane(dev_priv, plane, pipe);
  2821. if (dev_priv->cfb_plane == plane)
  2822. intel_disable_fbc(dev);
  2823. intel_disable_pipe(dev_priv, pipe);
  2824. /* Disable PF */
  2825. I915_WRITE(PF_CTL(pipe), 0);
  2826. I915_WRITE(PF_WIN_SZ(pipe), 0);
  2827. ironlake_fdi_disable(crtc);
  2828. /* This is a horrible layering violation; we should be doing this in
  2829. * the connector/encoder ->prepare instead, but we don't always have
  2830. * enough information there about the config to know whether it will
  2831. * actually be necessary or just cause undesired flicker.
  2832. */
  2833. intel_disable_pch_ports(dev_priv, pipe);
  2834. intel_disable_transcoder(dev_priv, pipe);
  2835. if (HAS_PCH_CPT(dev)) {
  2836. /* disable TRANS_DP_CTL */
  2837. reg = TRANS_DP_CTL(pipe);
  2838. temp = I915_READ(reg);
  2839. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  2840. temp |= TRANS_DP_PORT_SEL_NONE;
  2841. I915_WRITE(reg, temp);
  2842. /* disable DPLL_SEL */
  2843. temp = I915_READ(PCH_DPLL_SEL);
  2844. switch (pipe) {
  2845. case 0:
  2846. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  2847. break;
  2848. case 1:
  2849. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2850. break;
  2851. case 2:
  2852. /* C shares PLL A or B */
  2853. temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
  2854. break;
  2855. default:
  2856. BUG(); /* wtf */
  2857. }
  2858. I915_WRITE(PCH_DPLL_SEL, temp);
  2859. }
  2860. /* disable PCH DPLL */
  2861. intel_disable_pch_pll(intel_crtc);
  2862. /* Switch from PCDclk to Rawclk */
  2863. reg = FDI_RX_CTL(pipe);
  2864. temp = I915_READ(reg);
  2865. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2866. /* Disable CPU FDI TX PLL */
  2867. reg = FDI_TX_CTL(pipe);
  2868. temp = I915_READ(reg);
  2869. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2870. POSTING_READ(reg);
  2871. udelay(100);
  2872. reg = FDI_RX_CTL(pipe);
  2873. temp = I915_READ(reg);
  2874. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2875. /* Wait for the clocks to turn off. */
  2876. POSTING_READ(reg);
  2877. udelay(100);
  2878. intel_crtc->active = false;
  2879. intel_update_watermarks(dev);
  2880. mutex_lock(&dev->struct_mutex);
  2881. intel_update_fbc(dev);
  2882. mutex_unlock(&dev->struct_mutex);
  2883. }
  2884. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  2885. {
  2886. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2887. int pipe = intel_crtc->pipe;
  2888. int plane = intel_crtc->plane;
  2889. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2890. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2891. */
  2892. switch (mode) {
  2893. case DRM_MODE_DPMS_ON:
  2894. case DRM_MODE_DPMS_STANDBY:
  2895. case DRM_MODE_DPMS_SUSPEND:
  2896. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  2897. ironlake_crtc_enable(crtc);
  2898. break;
  2899. case DRM_MODE_DPMS_OFF:
  2900. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  2901. ironlake_crtc_disable(crtc);
  2902. break;
  2903. }
  2904. }
  2905. static void ironlake_crtc_off(struct drm_crtc *crtc)
  2906. {
  2907. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2908. intel_put_pch_pll(intel_crtc);
  2909. }
  2910. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2911. {
  2912. if (!enable && intel_crtc->overlay) {
  2913. struct drm_device *dev = intel_crtc->base.dev;
  2914. struct drm_i915_private *dev_priv = dev->dev_private;
  2915. mutex_lock(&dev->struct_mutex);
  2916. dev_priv->mm.interruptible = false;
  2917. (void) intel_overlay_switch_off(intel_crtc->overlay);
  2918. dev_priv->mm.interruptible = true;
  2919. mutex_unlock(&dev->struct_mutex);
  2920. }
  2921. /* Let userspace switch the overlay on again. In most cases userspace
  2922. * has to recompute where to put it anyway.
  2923. */
  2924. }
  2925. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  2926. {
  2927. struct drm_device *dev = crtc->dev;
  2928. struct drm_i915_private *dev_priv = dev->dev_private;
  2929. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2930. int pipe = intel_crtc->pipe;
  2931. int plane = intel_crtc->plane;
  2932. if (intel_crtc->active)
  2933. return;
  2934. intel_crtc->active = true;
  2935. intel_update_watermarks(dev);
  2936. intel_enable_pll(dev_priv, pipe);
  2937. intel_enable_pipe(dev_priv, pipe, false);
  2938. intel_enable_plane(dev_priv, plane, pipe);
  2939. intel_crtc_load_lut(crtc);
  2940. intel_update_fbc(dev);
  2941. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2942. intel_crtc_dpms_overlay(intel_crtc, true);
  2943. intel_crtc_update_cursor(crtc, true);
  2944. }
  2945. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  2946. {
  2947. struct drm_device *dev = crtc->dev;
  2948. struct drm_i915_private *dev_priv = dev->dev_private;
  2949. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2950. int pipe = intel_crtc->pipe;
  2951. int plane = intel_crtc->plane;
  2952. if (!intel_crtc->active)
  2953. return;
  2954. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2955. intel_crtc_wait_for_pending_flips(crtc);
  2956. drm_vblank_off(dev, pipe);
  2957. intel_crtc_dpms_overlay(intel_crtc, false);
  2958. intel_crtc_update_cursor(crtc, false);
  2959. if (dev_priv->cfb_plane == plane)
  2960. intel_disable_fbc(dev);
  2961. intel_disable_plane(dev_priv, plane, pipe);
  2962. intel_disable_pipe(dev_priv, pipe);
  2963. intel_disable_pll(dev_priv, pipe);
  2964. intel_crtc->active = false;
  2965. intel_update_fbc(dev);
  2966. intel_update_watermarks(dev);
  2967. }
  2968. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2969. {
  2970. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2971. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2972. */
  2973. switch (mode) {
  2974. case DRM_MODE_DPMS_ON:
  2975. case DRM_MODE_DPMS_STANDBY:
  2976. case DRM_MODE_DPMS_SUSPEND:
  2977. i9xx_crtc_enable(crtc);
  2978. break;
  2979. case DRM_MODE_DPMS_OFF:
  2980. i9xx_crtc_disable(crtc);
  2981. break;
  2982. }
  2983. }
  2984. static void i9xx_crtc_off(struct drm_crtc *crtc)
  2985. {
  2986. }
  2987. /**
  2988. * Sets the power management mode of the pipe and plane.
  2989. */
  2990. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2991. {
  2992. struct drm_device *dev = crtc->dev;
  2993. struct drm_i915_private *dev_priv = dev->dev_private;
  2994. struct drm_i915_master_private *master_priv;
  2995. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2996. int pipe = intel_crtc->pipe;
  2997. bool enabled;
  2998. if (intel_crtc->dpms_mode == mode)
  2999. return;
  3000. intel_crtc->dpms_mode = mode;
  3001. dev_priv->display.dpms(crtc, mode);
  3002. if (!dev->primary->master)
  3003. return;
  3004. master_priv = dev->primary->master->driver_priv;
  3005. if (!master_priv->sarea_priv)
  3006. return;
  3007. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  3008. switch (pipe) {
  3009. case 0:
  3010. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  3011. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  3012. break;
  3013. case 1:
  3014. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  3015. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  3016. break;
  3017. default:
  3018. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  3019. break;
  3020. }
  3021. }
  3022. static void intel_crtc_disable(struct drm_crtc *crtc)
  3023. {
  3024. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  3025. struct drm_device *dev = crtc->dev;
  3026. struct drm_i915_private *dev_priv = dev->dev_private;
  3027. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  3028. dev_priv->display.off(crtc);
  3029. assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
  3030. assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
  3031. if (crtc->fb) {
  3032. mutex_lock(&dev->struct_mutex);
  3033. intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
  3034. mutex_unlock(&dev->struct_mutex);
  3035. }
  3036. }
  3037. /* Prepare for a mode set.
  3038. *
  3039. * Note we could be a lot smarter here. We need to figure out which outputs
  3040. * will be enabled, which disabled (in short, how the config will changes)
  3041. * and perform the minimum necessary steps to accomplish that, e.g. updating
  3042. * watermarks, FBC configuration, making sure PLLs are programmed correctly,
  3043. * panel fitting is in the proper state, etc.
  3044. */
  3045. static void i9xx_crtc_prepare(struct drm_crtc *crtc)
  3046. {
  3047. i9xx_crtc_disable(crtc);
  3048. }
  3049. static void i9xx_crtc_commit(struct drm_crtc *crtc)
  3050. {
  3051. i9xx_crtc_enable(crtc);
  3052. }
  3053. static void ironlake_crtc_prepare(struct drm_crtc *crtc)
  3054. {
  3055. ironlake_crtc_disable(crtc);
  3056. }
  3057. static void ironlake_crtc_commit(struct drm_crtc *crtc)
  3058. {
  3059. ironlake_crtc_enable(crtc);
  3060. }
  3061. void intel_encoder_prepare(struct drm_encoder *encoder)
  3062. {
  3063. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  3064. /* lvds has its own version of prepare see intel_lvds_prepare */
  3065. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  3066. }
  3067. void intel_encoder_commit(struct drm_encoder *encoder)
  3068. {
  3069. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  3070. struct drm_device *dev = encoder->dev;
  3071. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  3072. /* lvds has its own version of commit see intel_lvds_commit */
  3073. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  3074. if (HAS_PCH_CPT(dev))
  3075. intel_cpt_verify_modeset(dev, intel_crtc->pipe);
  3076. }
  3077. void intel_encoder_destroy(struct drm_encoder *encoder)
  3078. {
  3079. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  3080. drm_encoder_cleanup(encoder);
  3081. kfree(intel_encoder);
  3082. }
  3083. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  3084. const struct drm_display_mode *mode,
  3085. struct drm_display_mode *adjusted_mode)
  3086. {
  3087. struct drm_device *dev = crtc->dev;
  3088. if (HAS_PCH_SPLIT(dev)) {
  3089. /* FDI link clock is fixed at 2.7G */
  3090. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  3091. return false;
  3092. }
  3093. /* All interlaced capable intel hw wants timings in frames. Note though
  3094. * that intel_lvds_mode_fixup does some funny tricks with the crtc
  3095. * timings, so we need to be careful not to clobber these.*/
  3096. if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
  3097. drm_mode_set_crtcinfo(adjusted_mode, 0);
  3098. return true;
  3099. }
  3100. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  3101. {
  3102. return 400000; /* FIXME */
  3103. }
  3104. static int i945_get_display_clock_speed(struct drm_device *dev)
  3105. {
  3106. return 400000;
  3107. }
  3108. static int i915_get_display_clock_speed(struct drm_device *dev)
  3109. {
  3110. return 333000;
  3111. }
  3112. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  3113. {
  3114. return 200000;
  3115. }
  3116. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  3117. {
  3118. u16 gcfgc = 0;
  3119. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  3120. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  3121. return 133000;
  3122. else {
  3123. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  3124. case GC_DISPLAY_CLOCK_333_MHZ:
  3125. return 333000;
  3126. default:
  3127. case GC_DISPLAY_CLOCK_190_200_MHZ:
  3128. return 190000;
  3129. }
  3130. }
  3131. }
  3132. static int i865_get_display_clock_speed(struct drm_device *dev)
  3133. {
  3134. return 266000;
  3135. }
  3136. static int i855_get_display_clock_speed(struct drm_device *dev)
  3137. {
  3138. u16 hpllcc = 0;
  3139. /* Assume that the hardware is in the high speed state. This
  3140. * should be the default.
  3141. */
  3142. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  3143. case GC_CLOCK_133_200:
  3144. case GC_CLOCK_100_200:
  3145. return 200000;
  3146. case GC_CLOCK_166_250:
  3147. return 250000;
  3148. case GC_CLOCK_100_133:
  3149. return 133000;
  3150. }
  3151. /* Shouldn't happen */
  3152. return 0;
  3153. }
  3154. static int i830_get_display_clock_speed(struct drm_device *dev)
  3155. {
  3156. return 133000;
  3157. }
  3158. struct fdi_m_n {
  3159. u32 tu;
  3160. u32 gmch_m;
  3161. u32 gmch_n;
  3162. u32 link_m;
  3163. u32 link_n;
  3164. };
  3165. static void
  3166. fdi_reduce_ratio(u32 *num, u32 *den)
  3167. {
  3168. while (*num > 0xffffff || *den > 0xffffff) {
  3169. *num >>= 1;
  3170. *den >>= 1;
  3171. }
  3172. }
  3173. static void
  3174. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  3175. int link_clock, struct fdi_m_n *m_n)
  3176. {
  3177. m_n->tu = 64; /* default size */
  3178. /* BUG_ON(pixel_clock > INT_MAX / 36); */
  3179. m_n->gmch_m = bits_per_pixel * pixel_clock;
  3180. m_n->gmch_n = link_clock * nlanes * 8;
  3181. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  3182. m_n->link_m = pixel_clock;
  3183. m_n->link_n = link_clock;
  3184. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  3185. }
  3186. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  3187. {
  3188. if (i915_panel_use_ssc >= 0)
  3189. return i915_panel_use_ssc != 0;
  3190. return dev_priv->lvds_use_ssc
  3191. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  3192. }
  3193. /**
  3194. * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
  3195. * @crtc: CRTC structure
  3196. * @mode: requested mode
  3197. *
  3198. * A pipe may be connected to one or more outputs. Based on the depth of the
  3199. * attached framebuffer, choose a good color depth to use on the pipe.
  3200. *
  3201. * If possible, match the pipe depth to the fb depth. In some cases, this
  3202. * isn't ideal, because the connected output supports a lesser or restricted
  3203. * set of depths. Resolve that here:
  3204. * LVDS typically supports only 6bpc, so clamp down in that case
  3205. * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
  3206. * Displays may support a restricted set as well, check EDID and clamp as
  3207. * appropriate.
  3208. * DP may want to dither down to 6bpc to fit larger modes
  3209. *
  3210. * RETURNS:
  3211. * Dithering requirement (i.e. false if display bpc and pipe bpc match,
  3212. * true if they don't match).
  3213. */
  3214. static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
  3215. unsigned int *pipe_bpp,
  3216. struct drm_display_mode *mode)
  3217. {
  3218. struct drm_device *dev = crtc->dev;
  3219. struct drm_i915_private *dev_priv = dev->dev_private;
  3220. struct drm_connector *connector;
  3221. struct intel_encoder *intel_encoder;
  3222. unsigned int display_bpc = UINT_MAX, bpc;
  3223. /* Walk the encoders & connectors on this crtc, get min bpc */
  3224. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  3225. if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
  3226. unsigned int lvds_bpc;
  3227. if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
  3228. LVDS_A3_POWER_UP)
  3229. lvds_bpc = 8;
  3230. else
  3231. lvds_bpc = 6;
  3232. if (lvds_bpc < display_bpc) {
  3233. DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
  3234. display_bpc = lvds_bpc;
  3235. }
  3236. continue;
  3237. }
  3238. if (intel_encoder->type == INTEL_OUTPUT_EDP) {
  3239. /* Use VBT settings if we have an eDP panel */
  3240. unsigned int edp_bpc = dev_priv->edp.bpp / 3;
  3241. if (edp_bpc < display_bpc) {
  3242. DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
  3243. display_bpc = edp_bpc;
  3244. }
  3245. continue;
  3246. }
  3247. /* Not one of the known troublemakers, check the EDID */
  3248. list_for_each_entry(connector, &dev->mode_config.connector_list,
  3249. head) {
  3250. if (connector->encoder != &intel_encoder->base)
  3251. continue;
  3252. /* Don't use an invalid EDID bpc value */
  3253. if (connector->display_info.bpc &&
  3254. connector->display_info.bpc < display_bpc) {
  3255. DRM_DEBUG_KMS("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
  3256. display_bpc = connector->display_info.bpc;
  3257. }
  3258. }
  3259. /*
  3260. * HDMI is either 12 or 8, so if the display lets 10bpc sneak
  3261. * through, clamp it down. (Note: >12bpc will be caught below.)
  3262. */
  3263. if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
  3264. if (display_bpc > 8 && display_bpc < 12) {
  3265. DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
  3266. display_bpc = 12;
  3267. } else {
  3268. DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
  3269. display_bpc = 8;
  3270. }
  3271. }
  3272. }
  3273. if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
  3274. DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
  3275. display_bpc = 6;
  3276. }
  3277. /*
  3278. * We could just drive the pipe at the highest bpc all the time and
  3279. * enable dithering as needed, but that costs bandwidth. So choose
  3280. * the minimum value that expresses the full color range of the fb but
  3281. * also stays within the max display bpc discovered above.
  3282. */
  3283. switch (crtc->fb->depth) {
  3284. case 8:
  3285. bpc = 8; /* since we go through a colormap */
  3286. break;
  3287. case 15:
  3288. case 16:
  3289. bpc = 6; /* min is 18bpp */
  3290. break;
  3291. case 24:
  3292. bpc = 8;
  3293. break;
  3294. case 30:
  3295. bpc = 10;
  3296. break;
  3297. case 48:
  3298. bpc = 12;
  3299. break;
  3300. default:
  3301. DRM_DEBUG("unsupported depth, assuming 24 bits\n");
  3302. bpc = min((unsigned int)8, display_bpc);
  3303. break;
  3304. }
  3305. display_bpc = min(display_bpc, bpc);
  3306. DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
  3307. bpc, display_bpc);
  3308. *pipe_bpp = display_bpc * 3;
  3309. return display_bpc != bpc;
  3310. }
  3311. static int vlv_get_refclk(struct drm_crtc *crtc)
  3312. {
  3313. struct drm_device *dev = crtc->dev;
  3314. struct drm_i915_private *dev_priv = dev->dev_private;
  3315. int refclk = 27000; /* for DP & HDMI */
  3316. return 100000; /* only one validated so far */
  3317. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  3318. refclk = 96000;
  3319. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  3320. if (intel_panel_use_ssc(dev_priv))
  3321. refclk = 100000;
  3322. else
  3323. refclk = 96000;
  3324. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
  3325. refclk = 100000;
  3326. }
  3327. return refclk;
  3328. }
  3329. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  3330. {
  3331. struct drm_device *dev = crtc->dev;
  3332. struct drm_i915_private *dev_priv = dev->dev_private;
  3333. int refclk;
  3334. if (IS_VALLEYVIEW(dev)) {
  3335. refclk = vlv_get_refclk(crtc);
  3336. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3337. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  3338. refclk = dev_priv->lvds_ssc_freq * 1000;
  3339. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3340. refclk / 1000);
  3341. } else if (!IS_GEN2(dev)) {
  3342. refclk = 96000;
  3343. } else {
  3344. refclk = 48000;
  3345. }
  3346. return refclk;
  3347. }
  3348. static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
  3349. intel_clock_t *clock)
  3350. {
  3351. /* SDVO TV has fixed PLL values depend on its clock range,
  3352. this mirrors vbios setting. */
  3353. if (adjusted_mode->clock >= 100000
  3354. && adjusted_mode->clock < 140500) {
  3355. clock->p1 = 2;
  3356. clock->p2 = 10;
  3357. clock->n = 3;
  3358. clock->m1 = 16;
  3359. clock->m2 = 8;
  3360. } else if (adjusted_mode->clock >= 140500
  3361. && adjusted_mode->clock <= 200000) {
  3362. clock->p1 = 1;
  3363. clock->p2 = 10;
  3364. clock->n = 6;
  3365. clock->m1 = 12;
  3366. clock->m2 = 8;
  3367. }
  3368. }
  3369. static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
  3370. intel_clock_t *clock,
  3371. intel_clock_t *reduced_clock)
  3372. {
  3373. struct drm_device *dev = crtc->dev;
  3374. struct drm_i915_private *dev_priv = dev->dev_private;
  3375. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3376. int pipe = intel_crtc->pipe;
  3377. u32 fp, fp2 = 0;
  3378. if (IS_PINEVIEW(dev)) {
  3379. fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
  3380. if (reduced_clock)
  3381. fp2 = (1 << reduced_clock->n) << 16 |
  3382. reduced_clock->m1 << 8 | reduced_clock->m2;
  3383. } else {
  3384. fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
  3385. if (reduced_clock)
  3386. fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
  3387. reduced_clock->m2;
  3388. }
  3389. I915_WRITE(FP0(pipe), fp);
  3390. intel_crtc->lowfreq_avail = false;
  3391. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3392. reduced_clock && i915_powersave) {
  3393. I915_WRITE(FP1(pipe), fp2);
  3394. intel_crtc->lowfreq_avail = true;
  3395. } else {
  3396. I915_WRITE(FP1(pipe), fp);
  3397. }
  3398. }
  3399. static void intel_update_lvds(struct drm_crtc *crtc, intel_clock_t *clock,
  3400. struct drm_display_mode *adjusted_mode)
  3401. {
  3402. struct drm_device *dev = crtc->dev;
  3403. struct drm_i915_private *dev_priv = dev->dev_private;
  3404. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3405. int pipe = intel_crtc->pipe;
  3406. u32 temp;
  3407. temp = I915_READ(LVDS);
  3408. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  3409. if (pipe == 1) {
  3410. temp |= LVDS_PIPEB_SELECT;
  3411. } else {
  3412. temp &= ~LVDS_PIPEB_SELECT;
  3413. }
  3414. /* set the corresponsding LVDS_BORDER bit */
  3415. temp |= dev_priv->lvds_border_bits;
  3416. /* Set the B0-B3 data pairs corresponding to whether we're going to
  3417. * set the DPLLs for dual-channel mode or not.
  3418. */
  3419. if (clock->p2 == 7)
  3420. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  3421. else
  3422. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  3423. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  3424. * appropriately here, but we need to look more thoroughly into how
  3425. * panels behave in the two modes.
  3426. */
  3427. /* set the dithering flag on LVDS as needed */
  3428. if (INTEL_INFO(dev)->gen >= 4) {
  3429. if (dev_priv->lvds_dither)
  3430. temp |= LVDS_ENABLE_DITHER;
  3431. else
  3432. temp &= ~LVDS_ENABLE_DITHER;
  3433. }
  3434. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  3435. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  3436. temp |= LVDS_HSYNC_POLARITY;
  3437. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  3438. temp |= LVDS_VSYNC_POLARITY;
  3439. I915_WRITE(LVDS, temp);
  3440. }
  3441. static void vlv_update_pll(struct drm_crtc *crtc,
  3442. struct drm_display_mode *mode,
  3443. struct drm_display_mode *adjusted_mode,
  3444. intel_clock_t *clock, intel_clock_t *reduced_clock,
  3445. int refclk, int num_connectors)
  3446. {
  3447. struct drm_device *dev = crtc->dev;
  3448. struct drm_i915_private *dev_priv = dev->dev_private;
  3449. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3450. int pipe = intel_crtc->pipe;
  3451. u32 dpll, mdiv, pdiv;
  3452. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  3453. bool is_hdmi;
  3454. is_hdmi = intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
  3455. bestn = clock->n;
  3456. bestm1 = clock->m1;
  3457. bestm2 = clock->m2;
  3458. bestp1 = clock->p1;
  3459. bestp2 = clock->p2;
  3460. /* Enable DPIO clock input */
  3461. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  3462. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  3463. I915_WRITE(DPLL(pipe), dpll);
  3464. POSTING_READ(DPLL(pipe));
  3465. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  3466. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  3467. mdiv |= ((bestn << DPIO_N_SHIFT));
  3468. mdiv |= (1 << DPIO_POST_DIV_SHIFT);
  3469. mdiv |= (1 << DPIO_K_SHIFT);
  3470. mdiv |= DPIO_ENABLE_CALIBRATION;
  3471. intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
  3472. intel_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), 0x01000000);
  3473. pdiv = DPIO_REFSEL_OVERRIDE | (5 << DPIO_PLL_MODESEL_SHIFT) |
  3474. (3 << DPIO_BIAS_CURRENT_CTL_SHIFT) | (1<<20) |
  3475. (8 << DPIO_DRIVER_CTL_SHIFT) | (5 << DPIO_CLK_BIAS_CTL_SHIFT);
  3476. intel_dpio_write(dev_priv, DPIO_REFSFR(pipe), pdiv);
  3477. intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe), 0x009f0051);
  3478. dpll |= DPLL_VCO_ENABLE;
  3479. I915_WRITE(DPLL(pipe), dpll);
  3480. POSTING_READ(DPLL(pipe));
  3481. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  3482. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  3483. if (is_hdmi) {
  3484. u32 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
  3485. if (temp > 1)
  3486. temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3487. else
  3488. temp = 0;
  3489. I915_WRITE(DPLL_MD(pipe), temp);
  3490. POSTING_READ(DPLL_MD(pipe));
  3491. }
  3492. intel_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x641); /* ??? */
  3493. }
  3494. static void i9xx_update_pll(struct drm_crtc *crtc,
  3495. struct drm_display_mode *mode,
  3496. struct drm_display_mode *adjusted_mode,
  3497. intel_clock_t *clock, intel_clock_t *reduced_clock,
  3498. int num_connectors)
  3499. {
  3500. struct drm_device *dev = crtc->dev;
  3501. struct drm_i915_private *dev_priv = dev->dev_private;
  3502. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3503. int pipe = intel_crtc->pipe;
  3504. u32 dpll;
  3505. bool is_sdvo;
  3506. is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
  3507. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
  3508. dpll = DPLL_VGA_MODE_DIS;
  3509. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  3510. dpll |= DPLLB_MODE_LVDS;
  3511. else
  3512. dpll |= DPLLB_MODE_DAC_SERIAL;
  3513. if (is_sdvo) {
  3514. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3515. if (pixel_multiplier > 1) {
  3516. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3517. dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  3518. }
  3519. dpll |= DPLL_DVO_HIGH_SPEED;
  3520. }
  3521. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  3522. dpll |= DPLL_DVO_HIGH_SPEED;
  3523. /* compute bitmask from p1 value */
  3524. if (IS_PINEVIEW(dev))
  3525. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  3526. else {
  3527. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3528. if (IS_G4X(dev) && reduced_clock)
  3529. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3530. }
  3531. switch (clock->p2) {
  3532. case 5:
  3533. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3534. break;
  3535. case 7:
  3536. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3537. break;
  3538. case 10:
  3539. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3540. break;
  3541. case 14:
  3542. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3543. break;
  3544. }
  3545. if (INTEL_INFO(dev)->gen >= 4)
  3546. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  3547. if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  3548. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3549. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  3550. /* XXX: just matching BIOS for now */
  3551. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3552. dpll |= 3;
  3553. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3554. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3555. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3556. else
  3557. dpll |= PLL_REF_INPUT_DREFCLK;
  3558. dpll |= DPLL_VCO_ENABLE;
  3559. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  3560. POSTING_READ(DPLL(pipe));
  3561. udelay(150);
  3562. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3563. * This is an exception to the general rule that mode_set doesn't turn
  3564. * things on.
  3565. */
  3566. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  3567. intel_update_lvds(crtc, clock, adjusted_mode);
  3568. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  3569. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3570. I915_WRITE(DPLL(pipe), dpll);
  3571. /* Wait for the clocks to stabilize. */
  3572. POSTING_READ(DPLL(pipe));
  3573. udelay(150);
  3574. if (INTEL_INFO(dev)->gen >= 4) {
  3575. u32 temp = 0;
  3576. if (is_sdvo) {
  3577. temp = intel_mode_get_pixel_multiplier(adjusted_mode);
  3578. if (temp > 1)
  3579. temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3580. else
  3581. temp = 0;
  3582. }
  3583. I915_WRITE(DPLL_MD(pipe), temp);
  3584. } else {
  3585. /* The pixel multiplier can only be updated once the
  3586. * DPLL is enabled and the clocks are stable.
  3587. *
  3588. * So write it again.
  3589. */
  3590. I915_WRITE(DPLL(pipe), dpll);
  3591. }
  3592. }
  3593. static void i8xx_update_pll(struct drm_crtc *crtc,
  3594. struct drm_display_mode *adjusted_mode,
  3595. intel_clock_t *clock,
  3596. int num_connectors)
  3597. {
  3598. struct drm_device *dev = crtc->dev;
  3599. struct drm_i915_private *dev_priv = dev->dev_private;
  3600. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3601. int pipe = intel_crtc->pipe;
  3602. u32 dpll;
  3603. dpll = DPLL_VGA_MODE_DIS;
  3604. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  3605. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3606. } else {
  3607. if (clock->p1 == 2)
  3608. dpll |= PLL_P1_DIVIDE_BY_TWO;
  3609. else
  3610. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3611. if (clock->p2 == 4)
  3612. dpll |= PLL_P2_DIVIDE_BY_4;
  3613. }
  3614. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  3615. /* XXX: just matching BIOS for now */
  3616. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3617. dpll |= 3;
  3618. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3619. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3620. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3621. else
  3622. dpll |= PLL_REF_INPUT_DREFCLK;
  3623. dpll |= DPLL_VCO_ENABLE;
  3624. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  3625. POSTING_READ(DPLL(pipe));
  3626. udelay(150);
  3627. I915_WRITE(DPLL(pipe), dpll);
  3628. /* Wait for the clocks to stabilize. */
  3629. POSTING_READ(DPLL(pipe));
  3630. udelay(150);
  3631. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3632. * This is an exception to the general rule that mode_set doesn't turn
  3633. * things on.
  3634. */
  3635. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  3636. intel_update_lvds(crtc, clock, adjusted_mode);
  3637. /* The pixel multiplier can only be updated once the
  3638. * DPLL is enabled and the clocks are stable.
  3639. *
  3640. * So write it again.
  3641. */
  3642. I915_WRITE(DPLL(pipe), dpll);
  3643. }
  3644. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  3645. struct drm_display_mode *mode,
  3646. struct drm_display_mode *adjusted_mode,
  3647. int x, int y,
  3648. struct drm_framebuffer *old_fb)
  3649. {
  3650. struct drm_device *dev = crtc->dev;
  3651. struct drm_i915_private *dev_priv = dev->dev_private;
  3652. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3653. int pipe = intel_crtc->pipe;
  3654. int plane = intel_crtc->plane;
  3655. int refclk, num_connectors = 0;
  3656. intel_clock_t clock, reduced_clock;
  3657. u32 dspcntr, pipeconf, vsyncshift;
  3658. bool ok, has_reduced_clock = false, is_sdvo = false;
  3659. bool is_lvds = false, is_tv = false, is_dp = false;
  3660. struct intel_encoder *encoder;
  3661. const intel_limit_t *limit;
  3662. int ret;
  3663. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3664. switch (encoder->type) {
  3665. case INTEL_OUTPUT_LVDS:
  3666. is_lvds = true;
  3667. break;
  3668. case INTEL_OUTPUT_SDVO:
  3669. case INTEL_OUTPUT_HDMI:
  3670. is_sdvo = true;
  3671. if (encoder->needs_tv_clock)
  3672. is_tv = true;
  3673. break;
  3674. case INTEL_OUTPUT_TVOUT:
  3675. is_tv = true;
  3676. break;
  3677. case INTEL_OUTPUT_DISPLAYPORT:
  3678. is_dp = true;
  3679. break;
  3680. }
  3681. num_connectors++;
  3682. }
  3683. refclk = i9xx_get_refclk(crtc, num_connectors);
  3684. /*
  3685. * Returns a set of divisors for the desired target clock with the given
  3686. * refclk, or FALSE. The returned values represent the clock equation:
  3687. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3688. */
  3689. limit = intel_limit(crtc, refclk);
  3690. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
  3691. &clock);
  3692. if (!ok) {
  3693. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3694. return -EINVAL;
  3695. }
  3696. /* Ensure that the cursor is valid for the new mode before changing... */
  3697. intel_crtc_update_cursor(crtc, true);
  3698. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3699. /*
  3700. * Ensure we match the reduced clock's P to the target clock.
  3701. * If the clocks don't match, we can't switch the display clock
  3702. * by using the FP0/FP1. In such case we will disable the LVDS
  3703. * downclock feature.
  3704. */
  3705. has_reduced_clock = limit->find_pll(limit, crtc,
  3706. dev_priv->lvds_downclock,
  3707. refclk,
  3708. &clock,
  3709. &reduced_clock);
  3710. }
  3711. if (is_sdvo && is_tv)
  3712. i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
  3713. i9xx_update_pll_dividers(crtc, &clock, has_reduced_clock ?
  3714. &reduced_clock : NULL);
  3715. if (IS_GEN2(dev))
  3716. i8xx_update_pll(crtc, adjusted_mode, &clock, num_connectors);
  3717. else if (IS_VALLEYVIEW(dev))
  3718. vlv_update_pll(crtc, mode,adjusted_mode, &clock, NULL,
  3719. refclk, num_connectors);
  3720. else
  3721. i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
  3722. has_reduced_clock ? &reduced_clock : NULL,
  3723. num_connectors);
  3724. /* setup pipeconf */
  3725. pipeconf = I915_READ(PIPECONF(pipe));
  3726. /* Set up the display plane register */
  3727. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3728. if (pipe == 0)
  3729. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3730. else
  3731. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3732. if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  3733. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  3734. * core speed.
  3735. *
  3736. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  3737. * pipe == 0 check?
  3738. */
  3739. if (mode->clock >
  3740. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  3741. pipeconf |= PIPECONF_DOUBLE_WIDE;
  3742. else
  3743. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  3744. }
  3745. /* default to 8bpc */
  3746. pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
  3747. if (is_dp) {
  3748. if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
  3749. pipeconf |= PIPECONF_BPP_6 |
  3750. PIPECONF_DITHER_EN |
  3751. PIPECONF_DITHER_TYPE_SP;
  3752. }
  3753. }
  3754. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  3755. drm_mode_debug_printmodeline(mode);
  3756. if (HAS_PIPE_CXSR(dev)) {
  3757. if (intel_crtc->lowfreq_avail) {
  3758. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  3759. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  3760. } else {
  3761. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  3762. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  3763. }
  3764. }
  3765. pipeconf &= ~PIPECONF_INTERLACE_MASK;
  3766. if (!IS_GEN2(dev) &&
  3767. adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3768. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  3769. /* the chip adds 2 halflines automatically */
  3770. adjusted_mode->crtc_vtotal -= 1;
  3771. adjusted_mode->crtc_vblank_end -= 1;
  3772. vsyncshift = adjusted_mode->crtc_hsync_start
  3773. - adjusted_mode->crtc_htotal/2;
  3774. } else {
  3775. pipeconf |= PIPECONF_PROGRESSIVE;
  3776. vsyncshift = 0;
  3777. }
  3778. if (!IS_GEN3(dev))
  3779. I915_WRITE(VSYNCSHIFT(pipe), vsyncshift);
  3780. I915_WRITE(HTOTAL(pipe),
  3781. (adjusted_mode->crtc_hdisplay - 1) |
  3782. ((adjusted_mode->crtc_htotal - 1) << 16));
  3783. I915_WRITE(HBLANK(pipe),
  3784. (adjusted_mode->crtc_hblank_start - 1) |
  3785. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3786. I915_WRITE(HSYNC(pipe),
  3787. (adjusted_mode->crtc_hsync_start - 1) |
  3788. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3789. I915_WRITE(VTOTAL(pipe),
  3790. (adjusted_mode->crtc_vdisplay - 1) |
  3791. ((adjusted_mode->crtc_vtotal - 1) << 16));
  3792. I915_WRITE(VBLANK(pipe),
  3793. (adjusted_mode->crtc_vblank_start - 1) |
  3794. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  3795. I915_WRITE(VSYNC(pipe),
  3796. (adjusted_mode->crtc_vsync_start - 1) |
  3797. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3798. /* pipesrc and dspsize control the size that is scaled from,
  3799. * which should always be the user's requested size.
  3800. */
  3801. I915_WRITE(DSPSIZE(plane),
  3802. ((mode->vdisplay - 1) << 16) |
  3803. (mode->hdisplay - 1));
  3804. I915_WRITE(DSPPOS(plane), 0);
  3805. I915_WRITE(PIPESRC(pipe),
  3806. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3807. I915_WRITE(PIPECONF(pipe), pipeconf);
  3808. POSTING_READ(PIPECONF(pipe));
  3809. intel_enable_pipe(dev_priv, pipe, false);
  3810. intel_wait_for_vblank(dev, pipe);
  3811. I915_WRITE(DSPCNTR(plane), dspcntr);
  3812. POSTING_READ(DSPCNTR(plane));
  3813. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  3814. intel_update_watermarks(dev);
  3815. return ret;
  3816. }
  3817. /*
  3818. * Initialize reference clocks when the driver loads
  3819. */
  3820. void ironlake_init_pch_refclk(struct drm_device *dev)
  3821. {
  3822. struct drm_i915_private *dev_priv = dev->dev_private;
  3823. struct drm_mode_config *mode_config = &dev->mode_config;
  3824. struct intel_encoder *encoder;
  3825. u32 temp;
  3826. bool has_lvds = false;
  3827. bool has_cpu_edp = false;
  3828. bool has_pch_edp = false;
  3829. bool has_panel = false;
  3830. bool has_ck505 = false;
  3831. bool can_ssc = false;
  3832. /* We need to take the global config into account */
  3833. list_for_each_entry(encoder, &mode_config->encoder_list,
  3834. base.head) {
  3835. switch (encoder->type) {
  3836. case INTEL_OUTPUT_LVDS:
  3837. has_panel = true;
  3838. has_lvds = true;
  3839. break;
  3840. case INTEL_OUTPUT_EDP:
  3841. has_panel = true;
  3842. if (intel_encoder_is_pch_edp(&encoder->base))
  3843. has_pch_edp = true;
  3844. else
  3845. has_cpu_edp = true;
  3846. break;
  3847. }
  3848. }
  3849. if (HAS_PCH_IBX(dev)) {
  3850. has_ck505 = dev_priv->display_clock_mode;
  3851. can_ssc = has_ck505;
  3852. } else {
  3853. has_ck505 = false;
  3854. can_ssc = true;
  3855. }
  3856. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
  3857. has_panel, has_lvds, has_pch_edp, has_cpu_edp,
  3858. has_ck505);
  3859. /* Ironlake: try to setup display ref clock before DPLL
  3860. * enabling. This is only under driver's control after
  3861. * PCH B stepping, previous chipset stepping should be
  3862. * ignoring this setting.
  3863. */
  3864. temp = I915_READ(PCH_DREF_CONTROL);
  3865. /* Always enable nonspread source */
  3866. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  3867. if (has_ck505)
  3868. temp |= DREF_NONSPREAD_CK505_ENABLE;
  3869. else
  3870. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  3871. if (has_panel) {
  3872. temp &= ~DREF_SSC_SOURCE_MASK;
  3873. temp |= DREF_SSC_SOURCE_ENABLE;
  3874. /* SSC must be turned on before enabling the CPU output */
  3875. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  3876. DRM_DEBUG_KMS("Using SSC on panel\n");
  3877. temp |= DREF_SSC1_ENABLE;
  3878. } else
  3879. temp &= ~DREF_SSC1_ENABLE;
  3880. /* Get SSC going before enabling the outputs */
  3881. I915_WRITE(PCH_DREF_CONTROL, temp);
  3882. POSTING_READ(PCH_DREF_CONTROL);
  3883. udelay(200);
  3884. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  3885. /* Enable CPU source on CPU attached eDP */
  3886. if (has_cpu_edp) {
  3887. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  3888. DRM_DEBUG_KMS("Using SSC on eDP\n");
  3889. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  3890. }
  3891. else
  3892. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  3893. } else
  3894. temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  3895. I915_WRITE(PCH_DREF_CONTROL, temp);
  3896. POSTING_READ(PCH_DREF_CONTROL);
  3897. udelay(200);
  3898. } else {
  3899. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  3900. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  3901. /* Turn off CPU output */
  3902. temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  3903. I915_WRITE(PCH_DREF_CONTROL, temp);
  3904. POSTING_READ(PCH_DREF_CONTROL);
  3905. udelay(200);
  3906. /* Turn off the SSC source */
  3907. temp &= ~DREF_SSC_SOURCE_MASK;
  3908. temp |= DREF_SSC_SOURCE_DISABLE;
  3909. /* Turn off SSC1 */
  3910. temp &= ~ DREF_SSC1_ENABLE;
  3911. I915_WRITE(PCH_DREF_CONTROL, temp);
  3912. POSTING_READ(PCH_DREF_CONTROL);
  3913. udelay(200);
  3914. }
  3915. }
  3916. static int ironlake_get_refclk(struct drm_crtc *crtc)
  3917. {
  3918. struct drm_device *dev = crtc->dev;
  3919. struct drm_i915_private *dev_priv = dev->dev_private;
  3920. struct intel_encoder *encoder;
  3921. struct intel_encoder *edp_encoder = NULL;
  3922. int num_connectors = 0;
  3923. bool is_lvds = false;
  3924. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3925. switch (encoder->type) {
  3926. case INTEL_OUTPUT_LVDS:
  3927. is_lvds = true;
  3928. break;
  3929. case INTEL_OUTPUT_EDP:
  3930. edp_encoder = encoder;
  3931. break;
  3932. }
  3933. num_connectors++;
  3934. }
  3935. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  3936. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3937. dev_priv->lvds_ssc_freq);
  3938. return dev_priv->lvds_ssc_freq * 1000;
  3939. }
  3940. return 120000;
  3941. }
  3942. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  3943. struct drm_display_mode *mode,
  3944. struct drm_display_mode *adjusted_mode,
  3945. int x, int y,
  3946. struct drm_framebuffer *old_fb)
  3947. {
  3948. struct drm_device *dev = crtc->dev;
  3949. struct drm_i915_private *dev_priv = dev->dev_private;
  3950. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3951. int pipe = intel_crtc->pipe;
  3952. int plane = intel_crtc->plane;
  3953. int refclk, num_connectors = 0;
  3954. intel_clock_t clock, reduced_clock;
  3955. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  3956. bool ok, has_reduced_clock = false, is_sdvo = false;
  3957. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  3958. struct intel_encoder *encoder, *edp_encoder = NULL;
  3959. const intel_limit_t *limit;
  3960. int ret;
  3961. struct fdi_m_n m_n = {0};
  3962. u32 temp;
  3963. int target_clock, pixel_multiplier, lane, link_bw, factor;
  3964. unsigned int pipe_bpp;
  3965. bool dither;
  3966. bool is_cpu_edp = false, is_pch_edp = false;
  3967. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3968. switch (encoder->type) {
  3969. case INTEL_OUTPUT_LVDS:
  3970. is_lvds = true;
  3971. break;
  3972. case INTEL_OUTPUT_SDVO:
  3973. case INTEL_OUTPUT_HDMI:
  3974. is_sdvo = true;
  3975. if (encoder->needs_tv_clock)
  3976. is_tv = true;
  3977. break;
  3978. case INTEL_OUTPUT_TVOUT:
  3979. is_tv = true;
  3980. break;
  3981. case INTEL_OUTPUT_ANALOG:
  3982. is_crt = true;
  3983. break;
  3984. case INTEL_OUTPUT_DISPLAYPORT:
  3985. is_dp = true;
  3986. break;
  3987. case INTEL_OUTPUT_EDP:
  3988. is_dp = true;
  3989. if (intel_encoder_is_pch_edp(&encoder->base))
  3990. is_pch_edp = true;
  3991. else
  3992. is_cpu_edp = true;
  3993. edp_encoder = encoder;
  3994. break;
  3995. }
  3996. num_connectors++;
  3997. }
  3998. refclk = ironlake_get_refclk(crtc);
  3999. /*
  4000. * Returns a set of divisors for the desired target clock with the given
  4001. * refclk, or FALSE. The returned values represent the clock equation:
  4002. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  4003. */
  4004. limit = intel_limit(crtc, refclk);
  4005. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
  4006. &clock);
  4007. if (!ok) {
  4008. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4009. return -EINVAL;
  4010. }
  4011. /* Ensure that the cursor is valid for the new mode before changing... */
  4012. intel_crtc_update_cursor(crtc, true);
  4013. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4014. /*
  4015. * Ensure we match the reduced clock's P to the target clock.
  4016. * If the clocks don't match, we can't switch the display clock
  4017. * by using the FP0/FP1. In such case we will disable the LVDS
  4018. * downclock feature.
  4019. */
  4020. has_reduced_clock = limit->find_pll(limit, crtc,
  4021. dev_priv->lvds_downclock,
  4022. refclk,
  4023. &clock,
  4024. &reduced_clock);
  4025. }
  4026. if (is_sdvo && is_tv)
  4027. i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
  4028. /* FDI link */
  4029. pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4030. lane = 0;
  4031. /* CPU eDP doesn't require FDI link, so just set DP M/N
  4032. according to current link config */
  4033. if (is_cpu_edp) {
  4034. intel_edp_link_config(edp_encoder, &lane, &link_bw);
  4035. } else {
  4036. /* FDI is a binary signal running at ~2.7GHz, encoding
  4037. * each output octet as 10 bits. The actual frequency
  4038. * is stored as a divider into a 100MHz clock, and the
  4039. * mode pixel clock is stored in units of 1KHz.
  4040. * Hence the bw of each lane in terms of the mode signal
  4041. * is:
  4042. */
  4043. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4044. }
  4045. /* [e]DP over FDI requires target mode clock instead of link clock. */
  4046. if (edp_encoder)
  4047. target_clock = intel_edp_target_clock(edp_encoder, mode);
  4048. else if (is_dp)
  4049. target_clock = mode->clock;
  4050. else
  4051. target_clock = adjusted_mode->clock;
  4052. /* determine panel color depth */
  4053. temp = I915_READ(PIPECONF(pipe));
  4054. temp &= ~PIPE_BPC_MASK;
  4055. dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp, mode);
  4056. switch (pipe_bpp) {
  4057. case 18:
  4058. temp |= PIPE_6BPC;
  4059. break;
  4060. case 24:
  4061. temp |= PIPE_8BPC;
  4062. break;
  4063. case 30:
  4064. temp |= PIPE_10BPC;
  4065. break;
  4066. case 36:
  4067. temp |= PIPE_12BPC;
  4068. break;
  4069. default:
  4070. WARN(1, "intel_choose_pipe_bpp returned invalid value %d\n",
  4071. pipe_bpp);
  4072. temp |= PIPE_8BPC;
  4073. pipe_bpp = 24;
  4074. break;
  4075. }
  4076. intel_crtc->bpp = pipe_bpp;
  4077. I915_WRITE(PIPECONF(pipe), temp);
  4078. if (!lane) {
  4079. /*
  4080. * Account for spread spectrum to avoid
  4081. * oversubscribing the link. Max center spread
  4082. * is 2.5%; use 5% for safety's sake.
  4083. */
  4084. u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
  4085. lane = bps / (link_bw * 8) + 1;
  4086. }
  4087. intel_crtc->fdi_lanes = lane;
  4088. if (pixel_multiplier > 1)
  4089. link_bw *= pixel_multiplier;
  4090. ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
  4091. &m_n);
  4092. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  4093. if (has_reduced_clock)
  4094. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  4095. reduced_clock.m2;
  4096. /* Enable autotuning of the PLL clock (if permissible) */
  4097. factor = 21;
  4098. if (is_lvds) {
  4099. if ((intel_panel_use_ssc(dev_priv) &&
  4100. dev_priv->lvds_ssc_freq == 100) ||
  4101. (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
  4102. factor = 25;
  4103. } else if (is_sdvo && is_tv)
  4104. factor = 20;
  4105. if (clock.m < factor * clock.n)
  4106. fp |= FP_CB_TUNE;
  4107. dpll = 0;
  4108. if (is_lvds)
  4109. dpll |= DPLLB_MODE_LVDS;
  4110. else
  4111. dpll |= DPLLB_MODE_DAC_SERIAL;
  4112. if (is_sdvo) {
  4113. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4114. if (pixel_multiplier > 1) {
  4115. dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  4116. }
  4117. dpll |= DPLL_DVO_HIGH_SPEED;
  4118. }
  4119. if (is_dp && !is_cpu_edp)
  4120. dpll |= DPLL_DVO_HIGH_SPEED;
  4121. /* compute bitmask from p1 value */
  4122. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4123. /* also FPA1 */
  4124. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4125. switch (clock.p2) {
  4126. case 5:
  4127. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4128. break;
  4129. case 7:
  4130. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4131. break;
  4132. case 10:
  4133. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4134. break;
  4135. case 14:
  4136. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4137. break;
  4138. }
  4139. if (is_sdvo && is_tv)
  4140. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4141. else if (is_tv)
  4142. /* XXX: just matching BIOS for now */
  4143. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  4144. dpll |= 3;
  4145. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4146. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4147. else
  4148. dpll |= PLL_REF_INPUT_DREFCLK;
  4149. /* setup pipeconf */
  4150. pipeconf = I915_READ(PIPECONF(pipe));
  4151. /* Set up the display plane register */
  4152. dspcntr = DISPPLANE_GAMMA_ENABLE;
  4153. DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
  4154. drm_mode_debug_printmodeline(mode);
  4155. /* CPU eDP is the only output that doesn't need a PCH PLL of its own on
  4156. * pre-Haswell/LPT generation */
  4157. if (HAS_PCH_LPT(dev)) {
  4158. DRM_DEBUG_KMS("LPT detected: no PLL for pipe %d necessary\n",
  4159. pipe);
  4160. } else if (!is_cpu_edp) {
  4161. struct intel_pch_pll *pll;
  4162. pll = intel_get_pch_pll(intel_crtc, dpll, fp);
  4163. if (pll == NULL) {
  4164. DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
  4165. pipe);
  4166. return -EINVAL;
  4167. }
  4168. } else
  4169. intel_put_pch_pll(intel_crtc);
  4170. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  4171. * This is an exception to the general rule that mode_set doesn't turn
  4172. * things on.
  4173. */
  4174. if (is_lvds) {
  4175. temp = I915_READ(PCH_LVDS);
  4176. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  4177. if (HAS_PCH_CPT(dev)) {
  4178. temp &= ~PORT_TRANS_SEL_MASK;
  4179. temp |= PORT_TRANS_SEL_CPT(pipe);
  4180. } else {
  4181. if (pipe == 1)
  4182. temp |= LVDS_PIPEB_SELECT;
  4183. else
  4184. temp &= ~LVDS_PIPEB_SELECT;
  4185. }
  4186. /* set the corresponsding LVDS_BORDER bit */
  4187. temp |= dev_priv->lvds_border_bits;
  4188. /* Set the B0-B3 data pairs corresponding to whether we're going to
  4189. * set the DPLLs for dual-channel mode or not.
  4190. */
  4191. if (clock.p2 == 7)
  4192. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  4193. else
  4194. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  4195. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  4196. * appropriately here, but we need to look more thoroughly into how
  4197. * panels behave in the two modes.
  4198. */
  4199. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  4200. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  4201. temp |= LVDS_HSYNC_POLARITY;
  4202. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  4203. temp |= LVDS_VSYNC_POLARITY;
  4204. I915_WRITE(PCH_LVDS, temp);
  4205. }
  4206. pipeconf &= ~PIPECONF_DITHER_EN;
  4207. pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
  4208. if ((is_lvds && dev_priv->lvds_dither) || dither) {
  4209. pipeconf |= PIPECONF_DITHER_EN;
  4210. pipeconf |= PIPECONF_DITHER_TYPE_SP;
  4211. }
  4212. if (is_dp && !is_cpu_edp) {
  4213. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  4214. } else {
  4215. /* For non-DP output, clear any trans DP clock recovery setting.*/
  4216. I915_WRITE(TRANSDATA_M1(pipe), 0);
  4217. I915_WRITE(TRANSDATA_N1(pipe), 0);
  4218. I915_WRITE(TRANSDPLINK_M1(pipe), 0);
  4219. I915_WRITE(TRANSDPLINK_N1(pipe), 0);
  4220. }
  4221. if (intel_crtc->pch_pll) {
  4222. I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
  4223. /* Wait for the clocks to stabilize. */
  4224. POSTING_READ(intel_crtc->pch_pll->pll_reg);
  4225. udelay(150);
  4226. /* The pixel multiplier can only be updated once the
  4227. * DPLL is enabled and the clocks are stable.
  4228. *
  4229. * So write it again.
  4230. */
  4231. I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
  4232. }
  4233. intel_crtc->lowfreq_avail = false;
  4234. if (intel_crtc->pch_pll) {
  4235. if (is_lvds && has_reduced_clock && i915_powersave) {
  4236. I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
  4237. intel_crtc->lowfreq_avail = true;
  4238. } else {
  4239. I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
  4240. }
  4241. }
  4242. pipeconf &= ~PIPECONF_INTERLACE_MASK;
  4243. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4244. pipeconf |= PIPECONF_INTERLACED_ILK;
  4245. /* the chip adds 2 halflines automatically */
  4246. adjusted_mode->crtc_vtotal -= 1;
  4247. adjusted_mode->crtc_vblank_end -= 1;
  4248. I915_WRITE(VSYNCSHIFT(pipe),
  4249. adjusted_mode->crtc_hsync_start
  4250. - adjusted_mode->crtc_htotal/2);
  4251. } else {
  4252. pipeconf |= PIPECONF_PROGRESSIVE;
  4253. I915_WRITE(VSYNCSHIFT(pipe), 0);
  4254. }
  4255. I915_WRITE(HTOTAL(pipe),
  4256. (adjusted_mode->crtc_hdisplay - 1) |
  4257. ((adjusted_mode->crtc_htotal - 1) << 16));
  4258. I915_WRITE(HBLANK(pipe),
  4259. (adjusted_mode->crtc_hblank_start - 1) |
  4260. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4261. I915_WRITE(HSYNC(pipe),
  4262. (adjusted_mode->crtc_hsync_start - 1) |
  4263. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4264. I915_WRITE(VTOTAL(pipe),
  4265. (adjusted_mode->crtc_vdisplay - 1) |
  4266. ((adjusted_mode->crtc_vtotal - 1) << 16));
  4267. I915_WRITE(VBLANK(pipe),
  4268. (adjusted_mode->crtc_vblank_start - 1) |
  4269. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  4270. I915_WRITE(VSYNC(pipe),
  4271. (adjusted_mode->crtc_vsync_start - 1) |
  4272. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4273. /* pipesrc controls the size that is scaled from, which should
  4274. * always be the user's requested size.
  4275. */
  4276. I915_WRITE(PIPESRC(pipe),
  4277. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  4278. I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
  4279. I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
  4280. I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
  4281. I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
  4282. if (is_cpu_edp)
  4283. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  4284. I915_WRITE(PIPECONF(pipe), pipeconf);
  4285. POSTING_READ(PIPECONF(pipe));
  4286. intel_wait_for_vblank(dev, pipe);
  4287. I915_WRITE(DSPCNTR(plane), dspcntr);
  4288. POSTING_READ(DSPCNTR(plane));
  4289. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  4290. intel_update_watermarks(dev);
  4291. intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
  4292. return ret;
  4293. }
  4294. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  4295. struct drm_display_mode *mode,
  4296. struct drm_display_mode *adjusted_mode,
  4297. int x, int y,
  4298. struct drm_framebuffer *old_fb)
  4299. {
  4300. struct drm_device *dev = crtc->dev;
  4301. struct drm_i915_private *dev_priv = dev->dev_private;
  4302. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4303. int pipe = intel_crtc->pipe;
  4304. int ret;
  4305. drm_vblank_pre_modeset(dev, pipe);
  4306. ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
  4307. x, y, old_fb);
  4308. drm_vblank_post_modeset(dev, pipe);
  4309. if (ret)
  4310. intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
  4311. else
  4312. intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
  4313. return ret;
  4314. }
  4315. static bool intel_eld_uptodate(struct drm_connector *connector,
  4316. int reg_eldv, uint32_t bits_eldv,
  4317. int reg_elda, uint32_t bits_elda,
  4318. int reg_edid)
  4319. {
  4320. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  4321. uint8_t *eld = connector->eld;
  4322. uint32_t i;
  4323. i = I915_READ(reg_eldv);
  4324. i &= bits_eldv;
  4325. if (!eld[0])
  4326. return !i;
  4327. if (!i)
  4328. return false;
  4329. i = I915_READ(reg_elda);
  4330. i &= ~bits_elda;
  4331. I915_WRITE(reg_elda, i);
  4332. for (i = 0; i < eld[2]; i++)
  4333. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  4334. return false;
  4335. return true;
  4336. }
  4337. static void g4x_write_eld(struct drm_connector *connector,
  4338. struct drm_crtc *crtc)
  4339. {
  4340. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  4341. uint8_t *eld = connector->eld;
  4342. uint32_t eldv;
  4343. uint32_t len;
  4344. uint32_t i;
  4345. i = I915_READ(G4X_AUD_VID_DID);
  4346. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  4347. eldv = G4X_ELDV_DEVCL_DEVBLC;
  4348. else
  4349. eldv = G4X_ELDV_DEVCTG;
  4350. if (intel_eld_uptodate(connector,
  4351. G4X_AUD_CNTL_ST, eldv,
  4352. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  4353. G4X_HDMIW_HDMIEDID))
  4354. return;
  4355. i = I915_READ(G4X_AUD_CNTL_ST);
  4356. i &= ~(eldv | G4X_ELD_ADDR);
  4357. len = (i >> 9) & 0x1f; /* ELD buffer size */
  4358. I915_WRITE(G4X_AUD_CNTL_ST, i);
  4359. if (!eld[0])
  4360. return;
  4361. len = min_t(uint8_t, eld[2], len);
  4362. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  4363. for (i = 0; i < len; i++)
  4364. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  4365. i = I915_READ(G4X_AUD_CNTL_ST);
  4366. i |= eldv;
  4367. I915_WRITE(G4X_AUD_CNTL_ST, i);
  4368. }
  4369. static void ironlake_write_eld(struct drm_connector *connector,
  4370. struct drm_crtc *crtc)
  4371. {
  4372. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  4373. uint8_t *eld = connector->eld;
  4374. uint32_t eldv;
  4375. uint32_t i;
  4376. int len;
  4377. int hdmiw_hdmiedid;
  4378. int aud_config;
  4379. int aud_cntl_st;
  4380. int aud_cntrl_st2;
  4381. if (HAS_PCH_IBX(connector->dev)) {
  4382. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID_A;
  4383. aud_config = IBX_AUD_CONFIG_A;
  4384. aud_cntl_st = IBX_AUD_CNTL_ST_A;
  4385. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  4386. } else {
  4387. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID_A;
  4388. aud_config = CPT_AUD_CONFIG_A;
  4389. aud_cntl_st = CPT_AUD_CNTL_ST_A;
  4390. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  4391. }
  4392. i = to_intel_crtc(crtc)->pipe;
  4393. hdmiw_hdmiedid += i * 0x100;
  4394. aud_cntl_st += i * 0x100;
  4395. aud_config += i * 0x100;
  4396. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(i));
  4397. i = I915_READ(aud_cntl_st);
  4398. i = (i >> 29) & 0x3; /* DIP_Port_Select, 0x1 = PortB */
  4399. if (!i) {
  4400. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  4401. /* operate blindly on all ports */
  4402. eldv = IBX_ELD_VALIDB;
  4403. eldv |= IBX_ELD_VALIDB << 4;
  4404. eldv |= IBX_ELD_VALIDB << 8;
  4405. } else {
  4406. DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
  4407. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  4408. }
  4409. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  4410. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  4411. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  4412. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  4413. } else
  4414. I915_WRITE(aud_config, 0);
  4415. if (intel_eld_uptodate(connector,
  4416. aud_cntrl_st2, eldv,
  4417. aud_cntl_st, IBX_ELD_ADDRESS,
  4418. hdmiw_hdmiedid))
  4419. return;
  4420. i = I915_READ(aud_cntrl_st2);
  4421. i &= ~eldv;
  4422. I915_WRITE(aud_cntrl_st2, i);
  4423. if (!eld[0])
  4424. return;
  4425. i = I915_READ(aud_cntl_st);
  4426. i &= ~IBX_ELD_ADDRESS;
  4427. I915_WRITE(aud_cntl_st, i);
  4428. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  4429. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  4430. for (i = 0; i < len; i++)
  4431. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  4432. i = I915_READ(aud_cntrl_st2);
  4433. i |= eldv;
  4434. I915_WRITE(aud_cntrl_st2, i);
  4435. }
  4436. void intel_write_eld(struct drm_encoder *encoder,
  4437. struct drm_display_mode *mode)
  4438. {
  4439. struct drm_crtc *crtc = encoder->crtc;
  4440. struct drm_connector *connector;
  4441. struct drm_device *dev = encoder->dev;
  4442. struct drm_i915_private *dev_priv = dev->dev_private;
  4443. connector = drm_select_eld(encoder, mode);
  4444. if (!connector)
  4445. return;
  4446. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  4447. connector->base.id,
  4448. drm_get_connector_name(connector),
  4449. connector->encoder->base.id,
  4450. drm_get_encoder_name(connector->encoder));
  4451. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  4452. if (dev_priv->display.write_eld)
  4453. dev_priv->display.write_eld(connector, crtc);
  4454. }
  4455. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  4456. void intel_crtc_load_lut(struct drm_crtc *crtc)
  4457. {
  4458. struct drm_device *dev = crtc->dev;
  4459. struct drm_i915_private *dev_priv = dev->dev_private;
  4460. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4461. int palreg = PALETTE(intel_crtc->pipe);
  4462. int i;
  4463. /* The clocks have to be on to load the palette. */
  4464. if (!crtc->enabled || !intel_crtc->active)
  4465. return;
  4466. /* use legacy palette for Ironlake */
  4467. if (HAS_PCH_SPLIT(dev))
  4468. palreg = LGC_PALETTE(intel_crtc->pipe);
  4469. for (i = 0; i < 256; i++) {
  4470. I915_WRITE(palreg + 4 * i,
  4471. (intel_crtc->lut_r[i] << 16) |
  4472. (intel_crtc->lut_g[i] << 8) |
  4473. intel_crtc->lut_b[i]);
  4474. }
  4475. }
  4476. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  4477. {
  4478. struct drm_device *dev = crtc->dev;
  4479. struct drm_i915_private *dev_priv = dev->dev_private;
  4480. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4481. bool visible = base != 0;
  4482. u32 cntl;
  4483. if (intel_crtc->cursor_visible == visible)
  4484. return;
  4485. cntl = I915_READ(_CURACNTR);
  4486. if (visible) {
  4487. /* On these chipsets we can only modify the base whilst
  4488. * the cursor is disabled.
  4489. */
  4490. I915_WRITE(_CURABASE, base);
  4491. cntl &= ~(CURSOR_FORMAT_MASK);
  4492. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  4493. cntl |= CURSOR_ENABLE |
  4494. CURSOR_GAMMA_ENABLE |
  4495. CURSOR_FORMAT_ARGB;
  4496. } else
  4497. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  4498. I915_WRITE(_CURACNTR, cntl);
  4499. intel_crtc->cursor_visible = visible;
  4500. }
  4501. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  4502. {
  4503. struct drm_device *dev = crtc->dev;
  4504. struct drm_i915_private *dev_priv = dev->dev_private;
  4505. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4506. int pipe = intel_crtc->pipe;
  4507. bool visible = base != 0;
  4508. if (intel_crtc->cursor_visible != visible) {
  4509. uint32_t cntl = I915_READ(CURCNTR(pipe));
  4510. if (base) {
  4511. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  4512. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  4513. cntl |= pipe << 28; /* Connect to correct pipe */
  4514. } else {
  4515. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  4516. cntl |= CURSOR_MODE_DISABLE;
  4517. }
  4518. I915_WRITE(CURCNTR(pipe), cntl);
  4519. intel_crtc->cursor_visible = visible;
  4520. }
  4521. /* and commit changes on next vblank */
  4522. I915_WRITE(CURBASE(pipe), base);
  4523. }
  4524. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  4525. {
  4526. struct drm_device *dev = crtc->dev;
  4527. struct drm_i915_private *dev_priv = dev->dev_private;
  4528. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4529. int pipe = intel_crtc->pipe;
  4530. bool visible = base != 0;
  4531. if (intel_crtc->cursor_visible != visible) {
  4532. uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
  4533. if (base) {
  4534. cntl &= ~CURSOR_MODE;
  4535. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  4536. } else {
  4537. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  4538. cntl |= CURSOR_MODE_DISABLE;
  4539. }
  4540. I915_WRITE(CURCNTR_IVB(pipe), cntl);
  4541. intel_crtc->cursor_visible = visible;
  4542. }
  4543. /* and commit changes on next vblank */
  4544. I915_WRITE(CURBASE_IVB(pipe), base);
  4545. }
  4546. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  4547. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  4548. bool on)
  4549. {
  4550. struct drm_device *dev = crtc->dev;
  4551. struct drm_i915_private *dev_priv = dev->dev_private;
  4552. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4553. int pipe = intel_crtc->pipe;
  4554. int x = intel_crtc->cursor_x;
  4555. int y = intel_crtc->cursor_y;
  4556. u32 base, pos;
  4557. bool visible;
  4558. pos = 0;
  4559. if (on && crtc->enabled && crtc->fb) {
  4560. base = intel_crtc->cursor_addr;
  4561. if (x > (int) crtc->fb->width)
  4562. base = 0;
  4563. if (y > (int) crtc->fb->height)
  4564. base = 0;
  4565. } else
  4566. base = 0;
  4567. if (x < 0) {
  4568. if (x + intel_crtc->cursor_width < 0)
  4569. base = 0;
  4570. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  4571. x = -x;
  4572. }
  4573. pos |= x << CURSOR_X_SHIFT;
  4574. if (y < 0) {
  4575. if (y + intel_crtc->cursor_height < 0)
  4576. base = 0;
  4577. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  4578. y = -y;
  4579. }
  4580. pos |= y << CURSOR_Y_SHIFT;
  4581. visible = base != 0;
  4582. if (!visible && !intel_crtc->cursor_visible)
  4583. return;
  4584. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  4585. I915_WRITE(CURPOS_IVB(pipe), pos);
  4586. ivb_update_cursor(crtc, base);
  4587. } else {
  4588. I915_WRITE(CURPOS(pipe), pos);
  4589. if (IS_845G(dev) || IS_I865G(dev))
  4590. i845_update_cursor(crtc, base);
  4591. else
  4592. i9xx_update_cursor(crtc, base);
  4593. }
  4594. }
  4595. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  4596. struct drm_file *file,
  4597. uint32_t handle,
  4598. uint32_t width, uint32_t height)
  4599. {
  4600. struct drm_device *dev = crtc->dev;
  4601. struct drm_i915_private *dev_priv = dev->dev_private;
  4602. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4603. struct drm_i915_gem_object *obj;
  4604. uint32_t addr;
  4605. int ret;
  4606. DRM_DEBUG_KMS("\n");
  4607. /* if we want to turn off the cursor ignore width and height */
  4608. if (!handle) {
  4609. DRM_DEBUG_KMS("cursor off\n");
  4610. addr = 0;
  4611. obj = NULL;
  4612. mutex_lock(&dev->struct_mutex);
  4613. goto finish;
  4614. }
  4615. /* Currently we only support 64x64 cursors */
  4616. if (width != 64 || height != 64) {
  4617. DRM_ERROR("we currently only support 64x64 cursors\n");
  4618. return -EINVAL;
  4619. }
  4620. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  4621. if (&obj->base == NULL)
  4622. return -ENOENT;
  4623. if (obj->base.size < width * height * 4) {
  4624. DRM_ERROR("buffer is to small\n");
  4625. ret = -ENOMEM;
  4626. goto fail;
  4627. }
  4628. /* we only need to pin inside GTT if cursor is non-phy */
  4629. mutex_lock(&dev->struct_mutex);
  4630. if (!dev_priv->info->cursor_needs_physical) {
  4631. if (obj->tiling_mode) {
  4632. DRM_ERROR("cursor cannot be tiled\n");
  4633. ret = -EINVAL;
  4634. goto fail_locked;
  4635. }
  4636. ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
  4637. if (ret) {
  4638. DRM_ERROR("failed to move cursor bo into the GTT\n");
  4639. goto fail_locked;
  4640. }
  4641. ret = i915_gem_object_put_fence(obj);
  4642. if (ret) {
  4643. DRM_ERROR("failed to release fence for cursor");
  4644. goto fail_unpin;
  4645. }
  4646. addr = obj->gtt_offset;
  4647. } else {
  4648. int align = IS_I830(dev) ? 16 * 1024 : 256;
  4649. ret = i915_gem_attach_phys_object(dev, obj,
  4650. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  4651. align);
  4652. if (ret) {
  4653. DRM_ERROR("failed to attach phys object\n");
  4654. goto fail_locked;
  4655. }
  4656. addr = obj->phys_obj->handle->busaddr;
  4657. }
  4658. if (IS_GEN2(dev))
  4659. I915_WRITE(CURSIZE, (height << 12) | width);
  4660. finish:
  4661. if (intel_crtc->cursor_bo) {
  4662. if (dev_priv->info->cursor_needs_physical) {
  4663. if (intel_crtc->cursor_bo != obj)
  4664. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  4665. } else
  4666. i915_gem_object_unpin(intel_crtc->cursor_bo);
  4667. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  4668. }
  4669. mutex_unlock(&dev->struct_mutex);
  4670. intel_crtc->cursor_addr = addr;
  4671. intel_crtc->cursor_bo = obj;
  4672. intel_crtc->cursor_width = width;
  4673. intel_crtc->cursor_height = height;
  4674. intel_crtc_update_cursor(crtc, true);
  4675. return 0;
  4676. fail_unpin:
  4677. i915_gem_object_unpin(obj);
  4678. fail_locked:
  4679. mutex_unlock(&dev->struct_mutex);
  4680. fail:
  4681. drm_gem_object_unreference_unlocked(&obj->base);
  4682. return ret;
  4683. }
  4684. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  4685. {
  4686. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4687. intel_crtc->cursor_x = x;
  4688. intel_crtc->cursor_y = y;
  4689. intel_crtc_update_cursor(crtc, true);
  4690. return 0;
  4691. }
  4692. /** Sets the color ramps on behalf of RandR */
  4693. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  4694. u16 blue, int regno)
  4695. {
  4696. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4697. intel_crtc->lut_r[regno] = red >> 8;
  4698. intel_crtc->lut_g[regno] = green >> 8;
  4699. intel_crtc->lut_b[regno] = blue >> 8;
  4700. }
  4701. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  4702. u16 *blue, int regno)
  4703. {
  4704. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4705. *red = intel_crtc->lut_r[regno] << 8;
  4706. *green = intel_crtc->lut_g[regno] << 8;
  4707. *blue = intel_crtc->lut_b[regno] << 8;
  4708. }
  4709. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  4710. u16 *blue, uint32_t start, uint32_t size)
  4711. {
  4712. int end = (start + size > 256) ? 256 : start + size, i;
  4713. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4714. for (i = start; i < end; i++) {
  4715. intel_crtc->lut_r[i] = red[i] >> 8;
  4716. intel_crtc->lut_g[i] = green[i] >> 8;
  4717. intel_crtc->lut_b[i] = blue[i] >> 8;
  4718. }
  4719. intel_crtc_load_lut(crtc);
  4720. }
  4721. /**
  4722. * Get a pipe with a simple mode set on it for doing load-based monitor
  4723. * detection.
  4724. *
  4725. * It will be up to the load-detect code to adjust the pipe as appropriate for
  4726. * its requirements. The pipe will be connected to no other encoders.
  4727. *
  4728. * Currently this code will only succeed if there is a pipe with no encoders
  4729. * configured for it. In the future, it could choose to temporarily disable
  4730. * some outputs to free up a pipe for its use.
  4731. *
  4732. * \return crtc, or NULL if no pipes are available.
  4733. */
  4734. /* VESA 640x480x72Hz mode to set on the pipe */
  4735. static struct drm_display_mode load_detect_mode = {
  4736. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  4737. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  4738. };
  4739. static struct drm_framebuffer *
  4740. intel_framebuffer_create(struct drm_device *dev,
  4741. struct drm_mode_fb_cmd2 *mode_cmd,
  4742. struct drm_i915_gem_object *obj)
  4743. {
  4744. struct intel_framebuffer *intel_fb;
  4745. int ret;
  4746. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  4747. if (!intel_fb) {
  4748. drm_gem_object_unreference_unlocked(&obj->base);
  4749. return ERR_PTR(-ENOMEM);
  4750. }
  4751. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  4752. if (ret) {
  4753. drm_gem_object_unreference_unlocked(&obj->base);
  4754. kfree(intel_fb);
  4755. return ERR_PTR(ret);
  4756. }
  4757. return &intel_fb->base;
  4758. }
  4759. static u32
  4760. intel_framebuffer_pitch_for_width(int width, int bpp)
  4761. {
  4762. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  4763. return ALIGN(pitch, 64);
  4764. }
  4765. static u32
  4766. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  4767. {
  4768. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  4769. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  4770. }
  4771. static struct drm_framebuffer *
  4772. intel_framebuffer_create_for_mode(struct drm_device *dev,
  4773. struct drm_display_mode *mode,
  4774. int depth, int bpp)
  4775. {
  4776. struct drm_i915_gem_object *obj;
  4777. struct drm_mode_fb_cmd2 mode_cmd;
  4778. obj = i915_gem_alloc_object(dev,
  4779. intel_framebuffer_size_for_mode(mode, bpp));
  4780. if (obj == NULL)
  4781. return ERR_PTR(-ENOMEM);
  4782. mode_cmd.width = mode->hdisplay;
  4783. mode_cmd.height = mode->vdisplay;
  4784. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  4785. bpp);
  4786. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  4787. return intel_framebuffer_create(dev, &mode_cmd, obj);
  4788. }
  4789. static struct drm_framebuffer *
  4790. mode_fits_in_fbdev(struct drm_device *dev,
  4791. struct drm_display_mode *mode)
  4792. {
  4793. struct drm_i915_private *dev_priv = dev->dev_private;
  4794. struct drm_i915_gem_object *obj;
  4795. struct drm_framebuffer *fb;
  4796. if (dev_priv->fbdev == NULL)
  4797. return NULL;
  4798. obj = dev_priv->fbdev->ifb.obj;
  4799. if (obj == NULL)
  4800. return NULL;
  4801. fb = &dev_priv->fbdev->ifb.base;
  4802. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  4803. fb->bits_per_pixel))
  4804. return NULL;
  4805. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  4806. return NULL;
  4807. return fb;
  4808. }
  4809. bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  4810. struct drm_connector *connector,
  4811. struct drm_display_mode *mode,
  4812. struct intel_load_detect_pipe *old)
  4813. {
  4814. struct intel_crtc *intel_crtc;
  4815. struct drm_crtc *possible_crtc;
  4816. struct drm_encoder *encoder = &intel_encoder->base;
  4817. struct drm_crtc *crtc = NULL;
  4818. struct drm_device *dev = encoder->dev;
  4819. struct drm_framebuffer *old_fb;
  4820. int i = -1;
  4821. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  4822. connector->base.id, drm_get_connector_name(connector),
  4823. encoder->base.id, drm_get_encoder_name(encoder));
  4824. /*
  4825. * Algorithm gets a little messy:
  4826. *
  4827. * - if the connector already has an assigned crtc, use it (but make
  4828. * sure it's on first)
  4829. *
  4830. * - try to find the first unused crtc that can drive this connector,
  4831. * and use that if we find one
  4832. */
  4833. /* See if we already have a CRTC for this connector */
  4834. if (encoder->crtc) {
  4835. crtc = encoder->crtc;
  4836. intel_crtc = to_intel_crtc(crtc);
  4837. old->dpms_mode = intel_crtc->dpms_mode;
  4838. old->load_detect_temp = false;
  4839. /* Make sure the crtc and connector are running */
  4840. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  4841. struct drm_encoder_helper_funcs *encoder_funcs;
  4842. struct drm_crtc_helper_funcs *crtc_funcs;
  4843. crtc_funcs = crtc->helper_private;
  4844. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  4845. encoder_funcs = encoder->helper_private;
  4846. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  4847. }
  4848. return true;
  4849. }
  4850. /* Find an unused one (if possible) */
  4851. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  4852. i++;
  4853. if (!(encoder->possible_crtcs & (1 << i)))
  4854. continue;
  4855. if (!possible_crtc->enabled) {
  4856. crtc = possible_crtc;
  4857. break;
  4858. }
  4859. }
  4860. /*
  4861. * If we didn't find an unused CRTC, don't use any.
  4862. */
  4863. if (!crtc) {
  4864. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  4865. return false;
  4866. }
  4867. encoder->crtc = crtc;
  4868. connector->encoder = encoder;
  4869. intel_crtc = to_intel_crtc(crtc);
  4870. old->dpms_mode = intel_crtc->dpms_mode;
  4871. old->load_detect_temp = true;
  4872. old->release_fb = NULL;
  4873. if (!mode)
  4874. mode = &load_detect_mode;
  4875. old_fb = crtc->fb;
  4876. /* We need a framebuffer large enough to accommodate all accesses
  4877. * that the plane may generate whilst we perform load detection.
  4878. * We can not rely on the fbcon either being present (we get called
  4879. * during its initialisation to detect all boot displays, or it may
  4880. * not even exist) or that it is large enough to satisfy the
  4881. * requested mode.
  4882. */
  4883. crtc->fb = mode_fits_in_fbdev(dev, mode);
  4884. if (crtc->fb == NULL) {
  4885. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  4886. crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  4887. old->release_fb = crtc->fb;
  4888. } else
  4889. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  4890. if (IS_ERR(crtc->fb)) {
  4891. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  4892. crtc->fb = old_fb;
  4893. return false;
  4894. }
  4895. if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
  4896. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  4897. if (old->release_fb)
  4898. old->release_fb->funcs->destroy(old->release_fb);
  4899. crtc->fb = old_fb;
  4900. return false;
  4901. }
  4902. /* let the connector get through one full cycle before testing */
  4903. intel_wait_for_vblank(dev, intel_crtc->pipe);
  4904. return true;
  4905. }
  4906. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  4907. struct drm_connector *connector,
  4908. struct intel_load_detect_pipe *old)
  4909. {
  4910. struct drm_encoder *encoder = &intel_encoder->base;
  4911. struct drm_device *dev = encoder->dev;
  4912. struct drm_crtc *crtc = encoder->crtc;
  4913. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  4914. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  4915. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  4916. connector->base.id, drm_get_connector_name(connector),
  4917. encoder->base.id, drm_get_encoder_name(encoder));
  4918. if (old->load_detect_temp) {
  4919. connector->encoder = NULL;
  4920. drm_helper_disable_unused_functions(dev);
  4921. if (old->release_fb)
  4922. old->release_fb->funcs->destroy(old->release_fb);
  4923. return;
  4924. }
  4925. /* Switch crtc and encoder back off if necessary */
  4926. if (old->dpms_mode != DRM_MODE_DPMS_ON) {
  4927. encoder_funcs->dpms(encoder, old->dpms_mode);
  4928. crtc_funcs->dpms(crtc, old->dpms_mode);
  4929. }
  4930. }
  4931. /* Returns the clock of the currently programmed mode of the given pipe. */
  4932. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  4933. {
  4934. struct drm_i915_private *dev_priv = dev->dev_private;
  4935. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4936. int pipe = intel_crtc->pipe;
  4937. u32 dpll = I915_READ(DPLL(pipe));
  4938. u32 fp;
  4939. intel_clock_t clock;
  4940. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  4941. fp = I915_READ(FP0(pipe));
  4942. else
  4943. fp = I915_READ(FP1(pipe));
  4944. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  4945. if (IS_PINEVIEW(dev)) {
  4946. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  4947. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4948. } else {
  4949. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  4950. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4951. }
  4952. if (!IS_GEN2(dev)) {
  4953. if (IS_PINEVIEW(dev))
  4954. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  4955. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  4956. else
  4957. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  4958. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4959. switch (dpll & DPLL_MODE_MASK) {
  4960. case DPLLB_MODE_DAC_SERIAL:
  4961. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  4962. 5 : 10;
  4963. break;
  4964. case DPLLB_MODE_LVDS:
  4965. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  4966. 7 : 14;
  4967. break;
  4968. default:
  4969. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  4970. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  4971. return 0;
  4972. }
  4973. /* XXX: Handle the 100Mhz refclk */
  4974. intel_clock(dev, 96000, &clock);
  4975. } else {
  4976. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  4977. if (is_lvds) {
  4978. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  4979. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4980. clock.p2 = 14;
  4981. if ((dpll & PLL_REF_INPUT_MASK) ==
  4982. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  4983. /* XXX: might not be 66MHz */
  4984. intel_clock(dev, 66000, &clock);
  4985. } else
  4986. intel_clock(dev, 48000, &clock);
  4987. } else {
  4988. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  4989. clock.p1 = 2;
  4990. else {
  4991. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  4992. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  4993. }
  4994. if (dpll & PLL_P2_DIVIDE_BY_4)
  4995. clock.p2 = 4;
  4996. else
  4997. clock.p2 = 2;
  4998. intel_clock(dev, 48000, &clock);
  4999. }
  5000. }
  5001. /* XXX: It would be nice to validate the clocks, but we can't reuse
  5002. * i830PllIsValid() because it relies on the xf86_config connector
  5003. * configuration being accurate, which it isn't necessarily.
  5004. */
  5005. return clock.dot;
  5006. }
  5007. /** Returns the currently programmed mode of the given pipe. */
  5008. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  5009. struct drm_crtc *crtc)
  5010. {
  5011. struct drm_i915_private *dev_priv = dev->dev_private;
  5012. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5013. int pipe = intel_crtc->pipe;
  5014. struct drm_display_mode *mode;
  5015. int htot = I915_READ(HTOTAL(pipe));
  5016. int hsync = I915_READ(HSYNC(pipe));
  5017. int vtot = I915_READ(VTOTAL(pipe));
  5018. int vsync = I915_READ(VSYNC(pipe));
  5019. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  5020. if (!mode)
  5021. return NULL;
  5022. mode->clock = intel_crtc_clock_get(dev, crtc);
  5023. mode->hdisplay = (htot & 0xffff) + 1;
  5024. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  5025. mode->hsync_start = (hsync & 0xffff) + 1;
  5026. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  5027. mode->vdisplay = (vtot & 0xffff) + 1;
  5028. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  5029. mode->vsync_start = (vsync & 0xffff) + 1;
  5030. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  5031. drm_mode_set_name(mode);
  5032. return mode;
  5033. }
  5034. static void intel_increase_pllclock(struct drm_crtc *crtc)
  5035. {
  5036. struct drm_device *dev = crtc->dev;
  5037. drm_i915_private_t *dev_priv = dev->dev_private;
  5038. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5039. int pipe = intel_crtc->pipe;
  5040. int dpll_reg = DPLL(pipe);
  5041. int dpll;
  5042. if (HAS_PCH_SPLIT(dev))
  5043. return;
  5044. if (!dev_priv->lvds_downclock_avail)
  5045. return;
  5046. dpll = I915_READ(dpll_reg);
  5047. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  5048. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  5049. assert_panel_unlocked(dev_priv, pipe);
  5050. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  5051. I915_WRITE(dpll_reg, dpll);
  5052. intel_wait_for_vblank(dev, pipe);
  5053. dpll = I915_READ(dpll_reg);
  5054. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  5055. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  5056. }
  5057. }
  5058. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  5059. {
  5060. struct drm_device *dev = crtc->dev;
  5061. drm_i915_private_t *dev_priv = dev->dev_private;
  5062. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5063. if (HAS_PCH_SPLIT(dev))
  5064. return;
  5065. if (!dev_priv->lvds_downclock_avail)
  5066. return;
  5067. /*
  5068. * Since this is called by a timer, we should never get here in
  5069. * the manual case.
  5070. */
  5071. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  5072. int pipe = intel_crtc->pipe;
  5073. int dpll_reg = DPLL(pipe);
  5074. int dpll;
  5075. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  5076. assert_panel_unlocked(dev_priv, pipe);
  5077. dpll = I915_READ(dpll_reg);
  5078. dpll |= DISPLAY_RATE_SELECT_FPA1;
  5079. I915_WRITE(dpll_reg, dpll);
  5080. intel_wait_for_vblank(dev, pipe);
  5081. dpll = I915_READ(dpll_reg);
  5082. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  5083. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  5084. }
  5085. }
  5086. void intel_mark_busy(struct drm_device *dev)
  5087. {
  5088. i915_update_gfx_val(dev->dev_private);
  5089. }
  5090. void intel_mark_idle(struct drm_device *dev)
  5091. {
  5092. }
  5093. void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
  5094. {
  5095. struct drm_device *dev = obj->base.dev;
  5096. struct drm_crtc *crtc;
  5097. if (!i915_powersave)
  5098. return;
  5099. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5100. if (!crtc->fb)
  5101. continue;
  5102. if (to_intel_framebuffer(crtc->fb)->obj == obj)
  5103. intel_increase_pllclock(crtc);
  5104. }
  5105. }
  5106. void intel_mark_fb_idle(struct drm_i915_gem_object *obj)
  5107. {
  5108. struct drm_device *dev = obj->base.dev;
  5109. struct drm_crtc *crtc;
  5110. if (!i915_powersave)
  5111. return;
  5112. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5113. if (!crtc->fb)
  5114. continue;
  5115. if (to_intel_framebuffer(crtc->fb)->obj == obj)
  5116. intel_decrease_pllclock(crtc);
  5117. }
  5118. }
  5119. static void intel_crtc_destroy(struct drm_crtc *crtc)
  5120. {
  5121. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5122. struct drm_device *dev = crtc->dev;
  5123. struct intel_unpin_work *work;
  5124. unsigned long flags;
  5125. spin_lock_irqsave(&dev->event_lock, flags);
  5126. work = intel_crtc->unpin_work;
  5127. intel_crtc->unpin_work = NULL;
  5128. spin_unlock_irqrestore(&dev->event_lock, flags);
  5129. if (work) {
  5130. cancel_work_sync(&work->work);
  5131. kfree(work);
  5132. }
  5133. drm_crtc_cleanup(crtc);
  5134. kfree(intel_crtc);
  5135. }
  5136. static void intel_unpin_work_fn(struct work_struct *__work)
  5137. {
  5138. struct intel_unpin_work *work =
  5139. container_of(__work, struct intel_unpin_work, work);
  5140. mutex_lock(&work->dev->struct_mutex);
  5141. intel_unpin_fb_obj(work->old_fb_obj);
  5142. drm_gem_object_unreference(&work->pending_flip_obj->base);
  5143. drm_gem_object_unreference(&work->old_fb_obj->base);
  5144. intel_update_fbc(work->dev);
  5145. mutex_unlock(&work->dev->struct_mutex);
  5146. kfree(work);
  5147. }
  5148. static void do_intel_finish_page_flip(struct drm_device *dev,
  5149. struct drm_crtc *crtc)
  5150. {
  5151. drm_i915_private_t *dev_priv = dev->dev_private;
  5152. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5153. struct intel_unpin_work *work;
  5154. struct drm_i915_gem_object *obj;
  5155. struct drm_pending_vblank_event *e;
  5156. struct timeval tnow, tvbl;
  5157. unsigned long flags;
  5158. /* Ignore early vblank irqs */
  5159. if (intel_crtc == NULL)
  5160. return;
  5161. do_gettimeofday(&tnow);
  5162. spin_lock_irqsave(&dev->event_lock, flags);
  5163. work = intel_crtc->unpin_work;
  5164. if (work == NULL || !work->pending) {
  5165. spin_unlock_irqrestore(&dev->event_lock, flags);
  5166. return;
  5167. }
  5168. intel_crtc->unpin_work = NULL;
  5169. if (work->event) {
  5170. e = work->event;
  5171. e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
  5172. /* Called before vblank count and timestamps have
  5173. * been updated for the vblank interval of flip
  5174. * completion? Need to increment vblank count and
  5175. * add one videorefresh duration to returned timestamp
  5176. * to account for this. We assume this happened if we
  5177. * get called over 0.9 frame durations after the last
  5178. * timestamped vblank.
  5179. *
  5180. * This calculation can not be used with vrefresh rates
  5181. * below 5Hz (10Hz to be on the safe side) without
  5182. * promoting to 64 integers.
  5183. */
  5184. if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
  5185. 9 * crtc->framedur_ns) {
  5186. e->event.sequence++;
  5187. tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
  5188. crtc->framedur_ns);
  5189. }
  5190. e->event.tv_sec = tvbl.tv_sec;
  5191. e->event.tv_usec = tvbl.tv_usec;
  5192. list_add_tail(&e->base.link,
  5193. &e->base.file_priv->event_list);
  5194. wake_up_interruptible(&e->base.file_priv->event_wait);
  5195. }
  5196. drm_vblank_put(dev, intel_crtc->pipe);
  5197. spin_unlock_irqrestore(&dev->event_lock, flags);
  5198. obj = work->old_fb_obj;
  5199. atomic_clear_mask(1 << intel_crtc->plane,
  5200. &obj->pending_flip.counter);
  5201. if (atomic_read(&obj->pending_flip) == 0)
  5202. wake_up(&dev_priv->pending_flip_queue);
  5203. schedule_work(&work->work);
  5204. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  5205. }
  5206. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  5207. {
  5208. drm_i915_private_t *dev_priv = dev->dev_private;
  5209. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  5210. do_intel_finish_page_flip(dev, crtc);
  5211. }
  5212. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  5213. {
  5214. drm_i915_private_t *dev_priv = dev->dev_private;
  5215. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  5216. do_intel_finish_page_flip(dev, crtc);
  5217. }
  5218. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  5219. {
  5220. drm_i915_private_t *dev_priv = dev->dev_private;
  5221. struct intel_crtc *intel_crtc =
  5222. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  5223. unsigned long flags;
  5224. spin_lock_irqsave(&dev->event_lock, flags);
  5225. if (intel_crtc->unpin_work) {
  5226. if ((++intel_crtc->unpin_work->pending) > 1)
  5227. DRM_ERROR("Prepared flip multiple times\n");
  5228. } else {
  5229. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  5230. }
  5231. spin_unlock_irqrestore(&dev->event_lock, flags);
  5232. }
  5233. static int intel_gen2_queue_flip(struct drm_device *dev,
  5234. struct drm_crtc *crtc,
  5235. struct drm_framebuffer *fb,
  5236. struct drm_i915_gem_object *obj)
  5237. {
  5238. struct drm_i915_private *dev_priv = dev->dev_private;
  5239. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5240. u32 flip_mask;
  5241. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  5242. int ret;
  5243. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5244. if (ret)
  5245. goto err;
  5246. ret = intel_ring_begin(ring, 6);
  5247. if (ret)
  5248. goto err_unpin;
  5249. /* Can't queue multiple flips, so wait for the previous
  5250. * one to finish before executing the next.
  5251. */
  5252. if (intel_crtc->plane)
  5253. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  5254. else
  5255. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  5256. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  5257. intel_ring_emit(ring, MI_NOOP);
  5258. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  5259. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5260. intel_ring_emit(ring, fb->pitches[0]);
  5261. intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
  5262. intel_ring_emit(ring, 0); /* aux display base address, unused */
  5263. intel_ring_advance(ring);
  5264. return 0;
  5265. err_unpin:
  5266. intel_unpin_fb_obj(obj);
  5267. err:
  5268. return ret;
  5269. }
  5270. static int intel_gen3_queue_flip(struct drm_device *dev,
  5271. struct drm_crtc *crtc,
  5272. struct drm_framebuffer *fb,
  5273. struct drm_i915_gem_object *obj)
  5274. {
  5275. struct drm_i915_private *dev_priv = dev->dev_private;
  5276. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5277. u32 flip_mask;
  5278. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  5279. int ret;
  5280. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5281. if (ret)
  5282. goto err;
  5283. ret = intel_ring_begin(ring, 6);
  5284. if (ret)
  5285. goto err_unpin;
  5286. if (intel_crtc->plane)
  5287. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  5288. else
  5289. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  5290. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  5291. intel_ring_emit(ring, MI_NOOP);
  5292. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  5293. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5294. intel_ring_emit(ring, fb->pitches[0]);
  5295. intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
  5296. intel_ring_emit(ring, MI_NOOP);
  5297. intel_ring_advance(ring);
  5298. return 0;
  5299. err_unpin:
  5300. intel_unpin_fb_obj(obj);
  5301. err:
  5302. return ret;
  5303. }
  5304. static int intel_gen4_queue_flip(struct drm_device *dev,
  5305. struct drm_crtc *crtc,
  5306. struct drm_framebuffer *fb,
  5307. struct drm_i915_gem_object *obj)
  5308. {
  5309. struct drm_i915_private *dev_priv = dev->dev_private;
  5310. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5311. uint32_t pf, pipesrc;
  5312. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  5313. int ret;
  5314. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5315. if (ret)
  5316. goto err;
  5317. ret = intel_ring_begin(ring, 4);
  5318. if (ret)
  5319. goto err_unpin;
  5320. /* i965+ uses the linear or tiled offsets from the
  5321. * Display Registers (which do not change across a page-flip)
  5322. * so we need only reprogram the base address.
  5323. */
  5324. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  5325. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5326. intel_ring_emit(ring, fb->pitches[0]);
  5327. intel_ring_emit(ring,
  5328. (obj->gtt_offset + intel_crtc->dspaddr_offset) |
  5329. obj->tiling_mode);
  5330. /* XXX Enabling the panel-fitter across page-flip is so far
  5331. * untested on non-native modes, so ignore it for now.
  5332. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  5333. */
  5334. pf = 0;
  5335. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  5336. intel_ring_emit(ring, pf | pipesrc);
  5337. intel_ring_advance(ring);
  5338. return 0;
  5339. err_unpin:
  5340. intel_unpin_fb_obj(obj);
  5341. err:
  5342. return ret;
  5343. }
  5344. static int intel_gen6_queue_flip(struct drm_device *dev,
  5345. struct drm_crtc *crtc,
  5346. struct drm_framebuffer *fb,
  5347. struct drm_i915_gem_object *obj)
  5348. {
  5349. struct drm_i915_private *dev_priv = dev->dev_private;
  5350. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5351. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  5352. uint32_t pf, pipesrc;
  5353. int ret;
  5354. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5355. if (ret)
  5356. goto err;
  5357. ret = intel_ring_begin(ring, 4);
  5358. if (ret)
  5359. goto err_unpin;
  5360. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  5361. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5362. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  5363. intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
  5364. /* Contrary to the suggestions in the documentation,
  5365. * "Enable Panel Fitter" does not seem to be required when page
  5366. * flipping with a non-native mode, and worse causes a normal
  5367. * modeset to fail.
  5368. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  5369. */
  5370. pf = 0;
  5371. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  5372. intel_ring_emit(ring, pf | pipesrc);
  5373. intel_ring_advance(ring);
  5374. return 0;
  5375. err_unpin:
  5376. intel_unpin_fb_obj(obj);
  5377. err:
  5378. return ret;
  5379. }
  5380. /*
  5381. * On gen7 we currently use the blit ring because (in early silicon at least)
  5382. * the render ring doesn't give us interrpts for page flip completion, which
  5383. * means clients will hang after the first flip is queued. Fortunately the
  5384. * blit ring generates interrupts properly, so use it instead.
  5385. */
  5386. static int intel_gen7_queue_flip(struct drm_device *dev,
  5387. struct drm_crtc *crtc,
  5388. struct drm_framebuffer *fb,
  5389. struct drm_i915_gem_object *obj)
  5390. {
  5391. struct drm_i915_private *dev_priv = dev->dev_private;
  5392. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5393. struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
  5394. uint32_t plane_bit = 0;
  5395. int ret;
  5396. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5397. if (ret)
  5398. goto err;
  5399. switch(intel_crtc->plane) {
  5400. case PLANE_A:
  5401. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  5402. break;
  5403. case PLANE_B:
  5404. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  5405. break;
  5406. case PLANE_C:
  5407. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  5408. break;
  5409. default:
  5410. WARN_ONCE(1, "unknown plane in flip command\n");
  5411. ret = -ENODEV;
  5412. goto err_unpin;
  5413. }
  5414. ret = intel_ring_begin(ring, 4);
  5415. if (ret)
  5416. goto err_unpin;
  5417. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  5418. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  5419. intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
  5420. intel_ring_emit(ring, (MI_NOOP));
  5421. intel_ring_advance(ring);
  5422. return 0;
  5423. err_unpin:
  5424. intel_unpin_fb_obj(obj);
  5425. err:
  5426. return ret;
  5427. }
  5428. static int intel_default_queue_flip(struct drm_device *dev,
  5429. struct drm_crtc *crtc,
  5430. struct drm_framebuffer *fb,
  5431. struct drm_i915_gem_object *obj)
  5432. {
  5433. return -ENODEV;
  5434. }
  5435. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  5436. struct drm_framebuffer *fb,
  5437. struct drm_pending_vblank_event *event)
  5438. {
  5439. struct drm_device *dev = crtc->dev;
  5440. struct drm_i915_private *dev_priv = dev->dev_private;
  5441. struct intel_framebuffer *intel_fb;
  5442. struct drm_i915_gem_object *obj;
  5443. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5444. struct intel_unpin_work *work;
  5445. unsigned long flags;
  5446. int ret;
  5447. /* Can't change pixel format via MI display flips. */
  5448. if (fb->pixel_format != crtc->fb->pixel_format)
  5449. return -EINVAL;
  5450. /*
  5451. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  5452. * Note that pitch changes could also affect these register.
  5453. */
  5454. if (INTEL_INFO(dev)->gen > 3 &&
  5455. (fb->offsets[0] != crtc->fb->offsets[0] ||
  5456. fb->pitches[0] != crtc->fb->pitches[0]))
  5457. return -EINVAL;
  5458. work = kzalloc(sizeof *work, GFP_KERNEL);
  5459. if (work == NULL)
  5460. return -ENOMEM;
  5461. work->event = event;
  5462. work->dev = crtc->dev;
  5463. intel_fb = to_intel_framebuffer(crtc->fb);
  5464. work->old_fb_obj = intel_fb->obj;
  5465. INIT_WORK(&work->work, intel_unpin_work_fn);
  5466. ret = drm_vblank_get(dev, intel_crtc->pipe);
  5467. if (ret)
  5468. goto free_work;
  5469. /* We borrow the event spin lock for protecting unpin_work */
  5470. spin_lock_irqsave(&dev->event_lock, flags);
  5471. if (intel_crtc->unpin_work) {
  5472. spin_unlock_irqrestore(&dev->event_lock, flags);
  5473. kfree(work);
  5474. drm_vblank_put(dev, intel_crtc->pipe);
  5475. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  5476. return -EBUSY;
  5477. }
  5478. intel_crtc->unpin_work = work;
  5479. spin_unlock_irqrestore(&dev->event_lock, flags);
  5480. intel_fb = to_intel_framebuffer(fb);
  5481. obj = intel_fb->obj;
  5482. ret = i915_mutex_lock_interruptible(dev);
  5483. if (ret)
  5484. goto cleanup;
  5485. /* Reference the objects for the scheduled work. */
  5486. drm_gem_object_reference(&work->old_fb_obj->base);
  5487. drm_gem_object_reference(&obj->base);
  5488. crtc->fb = fb;
  5489. work->pending_flip_obj = obj;
  5490. work->enable_stall_check = true;
  5491. /* Block clients from rendering to the new back buffer until
  5492. * the flip occurs and the object is no longer visible.
  5493. */
  5494. atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  5495. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
  5496. if (ret)
  5497. goto cleanup_pending;
  5498. intel_disable_fbc(dev);
  5499. intel_mark_fb_busy(obj);
  5500. mutex_unlock(&dev->struct_mutex);
  5501. trace_i915_flip_request(intel_crtc->plane, obj);
  5502. return 0;
  5503. cleanup_pending:
  5504. atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  5505. drm_gem_object_unreference(&work->old_fb_obj->base);
  5506. drm_gem_object_unreference(&obj->base);
  5507. mutex_unlock(&dev->struct_mutex);
  5508. cleanup:
  5509. spin_lock_irqsave(&dev->event_lock, flags);
  5510. intel_crtc->unpin_work = NULL;
  5511. spin_unlock_irqrestore(&dev->event_lock, flags);
  5512. drm_vblank_put(dev, intel_crtc->pipe);
  5513. free_work:
  5514. kfree(work);
  5515. return ret;
  5516. }
  5517. static void intel_sanitize_modesetting(struct drm_device *dev,
  5518. int pipe, int plane)
  5519. {
  5520. struct drm_i915_private *dev_priv = dev->dev_private;
  5521. u32 reg, val;
  5522. int i;
  5523. /* Clear any frame start delays used for debugging left by the BIOS */
  5524. for_each_pipe(i) {
  5525. reg = PIPECONF(i);
  5526. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  5527. }
  5528. if (HAS_PCH_SPLIT(dev))
  5529. return;
  5530. /* Who knows what state these registers were left in by the BIOS or
  5531. * grub?
  5532. *
  5533. * If we leave the registers in a conflicting state (e.g. with the
  5534. * display plane reading from the other pipe than the one we intend
  5535. * to use) then when we attempt to teardown the active mode, we will
  5536. * not disable the pipes and planes in the correct order -- leaving
  5537. * a plane reading from a disabled pipe and possibly leading to
  5538. * undefined behaviour.
  5539. */
  5540. reg = DSPCNTR(plane);
  5541. val = I915_READ(reg);
  5542. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  5543. return;
  5544. if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
  5545. return;
  5546. /* This display plane is active and attached to the other CPU pipe. */
  5547. pipe = !pipe;
  5548. /* Disable the plane and wait for it to stop reading from the pipe. */
  5549. intel_disable_plane(dev_priv, plane, pipe);
  5550. intel_disable_pipe(dev_priv, pipe);
  5551. }
  5552. static void intel_crtc_reset(struct drm_crtc *crtc)
  5553. {
  5554. struct drm_device *dev = crtc->dev;
  5555. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5556. /* Reset flags back to the 'unknown' status so that they
  5557. * will be correctly set on the initial modeset.
  5558. */
  5559. intel_crtc->dpms_mode = -1;
  5560. /* We need to fix up any BIOS configuration that conflicts with
  5561. * our expectations.
  5562. */
  5563. intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
  5564. }
  5565. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  5566. .dpms = intel_crtc_dpms,
  5567. .mode_fixup = intel_crtc_mode_fixup,
  5568. .mode_set = intel_crtc_mode_set,
  5569. .mode_set_base = intel_pipe_set_base,
  5570. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  5571. .load_lut = intel_crtc_load_lut,
  5572. .disable = intel_crtc_disable,
  5573. };
  5574. static const struct drm_crtc_funcs intel_crtc_funcs = {
  5575. .reset = intel_crtc_reset,
  5576. .cursor_set = intel_crtc_cursor_set,
  5577. .cursor_move = intel_crtc_cursor_move,
  5578. .gamma_set = intel_crtc_gamma_set,
  5579. .set_config = drm_crtc_helper_set_config,
  5580. .destroy = intel_crtc_destroy,
  5581. .page_flip = intel_crtc_page_flip,
  5582. };
  5583. static void intel_pch_pll_init(struct drm_device *dev)
  5584. {
  5585. drm_i915_private_t *dev_priv = dev->dev_private;
  5586. int i;
  5587. if (dev_priv->num_pch_pll == 0) {
  5588. DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
  5589. return;
  5590. }
  5591. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  5592. dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
  5593. dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
  5594. dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
  5595. }
  5596. }
  5597. static void intel_crtc_init(struct drm_device *dev, int pipe)
  5598. {
  5599. drm_i915_private_t *dev_priv = dev->dev_private;
  5600. struct intel_crtc *intel_crtc;
  5601. int i;
  5602. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  5603. if (intel_crtc == NULL)
  5604. return;
  5605. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  5606. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  5607. for (i = 0; i < 256; i++) {
  5608. intel_crtc->lut_r[i] = i;
  5609. intel_crtc->lut_g[i] = i;
  5610. intel_crtc->lut_b[i] = i;
  5611. }
  5612. /* Swap pipes & planes for FBC on pre-965 */
  5613. intel_crtc->pipe = pipe;
  5614. intel_crtc->plane = pipe;
  5615. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  5616. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  5617. intel_crtc->plane = !pipe;
  5618. }
  5619. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  5620. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  5621. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  5622. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  5623. intel_crtc_reset(&intel_crtc->base);
  5624. intel_crtc->active = true; /* force the pipe off on setup_init_config */
  5625. intel_crtc->bpp = 24; /* default for pre-Ironlake */
  5626. if (HAS_PCH_SPLIT(dev)) {
  5627. intel_helper_funcs.prepare = ironlake_crtc_prepare;
  5628. intel_helper_funcs.commit = ironlake_crtc_commit;
  5629. } else {
  5630. intel_helper_funcs.prepare = i9xx_crtc_prepare;
  5631. intel_helper_funcs.commit = i9xx_crtc_commit;
  5632. }
  5633. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  5634. }
  5635. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  5636. struct drm_file *file)
  5637. {
  5638. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  5639. struct drm_mode_object *drmmode_obj;
  5640. struct intel_crtc *crtc;
  5641. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  5642. return -ENODEV;
  5643. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  5644. DRM_MODE_OBJECT_CRTC);
  5645. if (!drmmode_obj) {
  5646. DRM_ERROR("no such CRTC id\n");
  5647. return -EINVAL;
  5648. }
  5649. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  5650. pipe_from_crtc_id->pipe = crtc->pipe;
  5651. return 0;
  5652. }
  5653. static int intel_encoder_clones(struct intel_encoder *encoder)
  5654. {
  5655. struct drm_device *dev = encoder->base.dev;
  5656. struct intel_encoder *source_encoder;
  5657. int index_mask = 0;
  5658. int entry = 0;
  5659. list_for_each_entry(source_encoder,
  5660. &dev->mode_config.encoder_list, base.head) {
  5661. if (encoder == source_encoder)
  5662. index_mask |= (1 << entry);
  5663. /* Intel hw has only one MUX where enocoders could be cloned. */
  5664. if (encoder->cloneable && source_encoder->cloneable)
  5665. index_mask |= (1 << entry);
  5666. entry++;
  5667. }
  5668. return index_mask;
  5669. }
  5670. static bool has_edp_a(struct drm_device *dev)
  5671. {
  5672. struct drm_i915_private *dev_priv = dev->dev_private;
  5673. if (!IS_MOBILE(dev))
  5674. return false;
  5675. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  5676. return false;
  5677. if (IS_GEN5(dev) &&
  5678. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  5679. return false;
  5680. return true;
  5681. }
  5682. static void intel_setup_outputs(struct drm_device *dev)
  5683. {
  5684. struct drm_i915_private *dev_priv = dev->dev_private;
  5685. struct intel_encoder *encoder;
  5686. bool dpd_is_edp = false;
  5687. bool has_lvds;
  5688. has_lvds = intel_lvds_init(dev);
  5689. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  5690. /* disable the panel fitter on everything but LVDS */
  5691. I915_WRITE(PFIT_CONTROL, 0);
  5692. }
  5693. if (HAS_PCH_SPLIT(dev)) {
  5694. dpd_is_edp = intel_dpd_is_edp(dev);
  5695. if (has_edp_a(dev))
  5696. intel_dp_init(dev, DP_A, PORT_A);
  5697. if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  5698. intel_dp_init(dev, PCH_DP_D, PORT_D);
  5699. }
  5700. intel_crt_init(dev);
  5701. if (IS_HASWELL(dev)) {
  5702. int found;
  5703. /* Haswell uses DDI functions to detect digital outputs */
  5704. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  5705. /* DDI A only supports eDP */
  5706. if (found)
  5707. intel_ddi_init(dev, PORT_A);
  5708. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  5709. * register */
  5710. found = I915_READ(SFUSE_STRAP);
  5711. if (found & SFUSE_STRAP_DDIB_DETECTED)
  5712. intel_ddi_init(dev, PORT_B);
  5713. if (found & SFUSE_STRAP_DDIC_DETECTED)
  5714. intel_ddi_init(dev, PORT_C);
  5715. if (found & SFUSE_STRAP_DDID_DETECTED)
  5716. intel_ddi_init(dev, PORT_D);
  5717. } else if (HAS_PCH_SPLIT(dev)) {
  5718. int found;
  5719. if (I915_READ(HDMIB) & PORT_DETECTED) {
  5720. /* PCH SDVOB multiplex with HDMIB */
  5721. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  5722. if (!found)
  5723. intel_hdmi_init(dev, HDMIB, PORT_B);
  5724. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  5725. intel_dp_init(dev, PCH_DP_B, PORT_B);
  5726. }
  5727. if (I915_READ(HDMIC) & PORT_DETECTED)
  5728. intel_hdmi_init(dev, HDMIC, PORT_C);
  5729. if (!dpd_is_edp && I915_READ(HDMID) & PORT_DETECTED)
  5730. intel_hdmi_init(dev, HDMID, PORT_D);
  5731. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  5732. intel_dp_init(dev, PCH_DP_C, PORT_C);
  5733. if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  5734. intel_dp_init(dev, PCH_DP_D, PORT_D);
  5735. } else if (IS_VALLEYVIEW(dev)) {
  5736. int found;
  5737. if (I915_READ(SDVOB) & PORT_DETECTED) {
  5738. /* SDVOB multiplex with HDMIB */
  5739. found = intel_sdvo_init(dev, SDVOB, true);
  5740. if (!found)
  5741. intel_hdmi_init(dev, SDVOB, PORT_B);
  5742. if (!found && (I915_READ(DP_B) & DP_DETECTED))
  5743. intel_dp_init(dev, DP_B, PORT_B);
  5744. }
  5745. if (I915_READ(SDVOC) & PORT_DETECTED)
  5746. intel_hdmi_init(dev, SDVOC, PORT_C);
  5747. /* Shares lanes with HDMI on SDVOC */
  5748. if (I915_READ(DP_C) & DP_DETECTED)
  5749. intel_dp_init(dev, DP_C, PORT_C);
  5750. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  5751. bool found = false;
  5752. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  5753. DRM_DEBUG_KMS("probing SDVOB\n");
  5754. found = intel_sdvo_init(dev, SDVOB, true);
  5755. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  5756. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  5757. intel_hdmi_init(dev, SDVOB, PORT_B);
  5758. }
  5759. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  5760. DRM_DEBUG_KMS("probing DP_B\n");
  5761. intel_dp_init(dev, DP_B, PORT_B);
  5762. }
  5763. }
  5764. /* Before G4X SDVOC doesn't have its own detect register */
  5765. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  5766. DRM_DEBUG_KMS("probing SDVOC\n");
  5767. found = intel_sdvo_init(dev, SDVOC, false);
  5768. }
  5769. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  5770. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  5771. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  5772. intel_hdmi_init(dev, SDVOC, PORT_C);
  5773. }
  5774. if (SUPPORTS_INTEGRATED_DP(dev)) {
  5775. DRM_DEBUG_KMS("probing DP_C\n");
  5776. intel_dp_init(dev, DP_C, PORT_C);
  5777. }
  5778. }
  5779. if (SUPPORTS_INTEGRATED_DP(dev) &&
  5780. (I915_READ(DP_D) & DP_DETECTED)) {
  5781. DRM_DEBUG_KMS("probing DP_D\n");
  5782. intel_dp_init(dev, DP_D, PORT_D);
  5783. }
  5784. } else if (IS_GEN2(dev))
  5785. intel_dvo_init(dev);
  5786. if (SUPPORTS_TV(dev))
  5787. intel_tv_init(dev);
  5788. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  5789. encoder->base.possible_crtcs = encoder->crtc_mask;
  5790. encoder->base.possible_clones =
  5791. intel_encoder_clones(encoder);
  5792. }
  5793. /* disable all the possible outputs/crtcs before entering KMS mode */
  5794. drm_helper_disable_unused_functions(dev);
  5795. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5796. ironlake_init_pch_refclk(dev);
  5797. }
  5798. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  5799. {
  5800. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  5801. drm_framebuffer_cleanup(fb);
  5802. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  5803. kfree(intel_fb);
  5804. }
  5805. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  5806. struct drm_file *file,
  5807. unsigned int *handle)
  5808. {
  5809. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  5810. struct drm_i915_gem_object *obj = intel_fb->obj;
  5811. return drm_gem_handle_create(file, &obj->base, handle);
  5812. }
  5813. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  5814. .destroy = intel_user_framebuffer_destroy,
  5815. .create_handle = intel_user_framebuffer_create_handle,
  5816. };
  5817. int intel_framebuffer_init(struct drm_device *dev,
  5818. struct intel_framebuffer *intel_fb,
  5819. struct drm_mode_fb_cmd2 *mode_cmd,
  5820. struct drm_i915_gem_object *obj)
  5821. {
  5822. int ret;
  5823. if (obj->tiling_mode == I915_TILING_Y)
  5824. return -EINVAL;
  5825. if (mode_cmd->pitches[0] & 63)
  5826. return -EINVAL;
  5827. switch (mode_cmd->pixel_format) {
  5828. case DRM_FORMAT_RGB332:
  5829. case DRM_FORMAT_RGB565:
  5830. case DRM_FORMAT_XRGB8888:
  5831. case DRM_FORMAT_XBGR8888:
  5832. case DRM_FORMAT_ARGB8888:
  5833. case DRM_FORMAT_XRGB2101010:
  5834. case DRM_FORMAT_ARGB2101010:
  5835. /* RGB formats are common across chipsets */
  5836. break;
  5837. case DRM_FORMAT_YUYV:
  5838. case DRM_FORMAT_UYVY:
  5839. case DRM_FORMAT_YVYU:
  5840. case DRM_FORMAT_VYUY:
  5841. break;
  5842. default:
  5843. DRM_DEBUG_KMS("unsupported pixel format %u\n",
  5844. mode_cmd->pixel_format);
  5845. return -EINVAL;
  5846. }
  5847. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  5848. if (ret) {
  5849. DRM_ERROR("framebuffer init failed %d\n", ret);
  5850. return ret;
  5851. }
  5852. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  5853. intel_fb->obj = obj;
  5854. return 0;
  5855. }
  5856. static struct drm_framebuffer *
  5857. intel_user_framebuffer_create(struct drm_device *dev,
  5858. struct drm_file *filp,
  5859. struct drm_mode_fb_cmd2 *mode_cmd)
  5860. {
  5861. struct drm_i915_gem_object *obj;
  5862. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  5863. mode_cmd->handles[0]));
  5864. if (&obj->base == NULL)
  5865. return ERR_PTR(-ENOENT);
  5866. return intel_framebuffer_create(dev, mode_cmd, obj);
  5867. }
  5868. static const struct drm_mode_config_funcs intel_mode_funcs = {
  5869. .fb_create = intel_user_framebuffer_create,
  5870. .output_poll_changed = intel_fb_output_poll_changed,
  5871. };
  5872. /* Set up chip specific display functions */
  5873. static void intel_init_display(struct drm_device *dev)
  5874. {
  5875. struct drm_i915_private *dev_priv = dev->dev_private;
  5876. /* We always want a DPMS function */
  5877. if (HAS_PCH_SPLIT(dev)) {
  5878. dev_priv->display.dpms = ironlake_crtc_dpms;
  5879. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  5880. dev_priv->display.off = ironlake_crtc_off;
  5881. dev_priv->display.update_plane = ironlake_update_plane;
  5882. } else {
  5883. dev_priv->display.dpms = i9xx_crtc_dpms;
  5884. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  5885. dev_priv->display.off = i9xx_crtc_off;
  5886. dev_priv->display.update_plane = i9xx_update_plane;
  5887. }
  5888. /* Returns the core display clock speed */
  5889. if (IS_VALLEYVIEW(dev))
  5890. dev_priv->display.get_display_clock_speed =
  5891. valleyview_get_display_clock_speed;
  5892. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  5893. dev_priv->display.get_display_clock_speed =
  5894. i945_get_display_clock_speed;
  5895. else if (IS_I915G(dev))
  5896. dev_priv->display.get_display_clock_speed =
  5897. i915_get_display_clock_speed;
  5898. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  5899. dev_priv->display.get_display_clock_speed =
  5900. i9xx_misc_get_display_clock_speed;
  5901. else if (IS_I915GM(dev))
  5902. dev_priv->display.get_display_clock_speed =
  5903. i915gm_get_display_clock_speed;
  5904. else if (IS_I865G(dev))
  5905. dev_priv->display.get_display_clock_speed =
  5906. i865_get_display_clock_speed;
  5907. else if (IS_I85X(dev))
  5908. dev_priv->display.get_display_clock_speed =
  5909. i855_get_display_clock_speed;
  5910. else /* 852, 830 */
  5911. dev_priv->display.get_display_clock_speed =
  5912. i830_get_display_clock_speed;
  5913. if (HAS_PCH_SPLIT(dev)) {
  5914. if (IS_GEN5(dev)) {
  5915. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  5916. dev_priv->display.write_eld = ironlake_write_eld;
  5917. } else if (IS_GEN6(dev)) {
  5918. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  5919. dev_priv->display.write_eld = ironlake_write_eld;
  5920. } else if (IS_IVYBRIDGE(dev)) {
  5921. /* FIXME: detect B0+ stepping and use auto training */
  5922. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  5923. dev_priv->display.write_eld = ironlake_write_eld;
  5924. } else if (IS_HASWELL(dev)) {
  5925. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  5926. dev_priv->display.write_eld = ironlake_write_eld;
  5927. } else
  5928. dev_priv->display.update_wm = NULL;
  5929. } else if (IS_G4X(dev)) {
  5930. dev_priv->display.write_eld = g4x_write_eld;
  5931. }
  5932. /* Default just returns -ENODEV to indicate unsupported */
  5933. dev_priv->display.queue_flip = intel_default_queue_flip;
  5934. switch (INTEL_INFO(dev)->gen) {
  5935. case 2:
  5936. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  5937. break;
  5938. case 3:
  5939. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  5940. break;
  5941. case 4:
  5942. case 5:
  5943. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  5944. break;
  5945. case 6:
  5946. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  5947. break;
  5948. case 7:
  5949. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  5950. break;
  5951. }
  5952. }
  5953. /*
  5954. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  5955. * resume, or other times. This quirk makes sure that's the case for
  5956. * affected systems.
  5957. */
  5958. static void quirk_pipea_force(struct drm_device *dev)
  5959. {
  5960. struct drm_i915_private *dev_priv = dev->dev_private;
  5961. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  5962. DRM_INFO("applying pipe a force quirk\n");
  5963. }
  5964. /*
  5965. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  5966. */
  5967. static void quirk_ssc_force_disable(struct drm_device *dev)
  5968. {
  5969. struct drm_i915_private *dev_priv = dev->dev_private;
  5970. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  5971. DRM_INFO("applying lvds SSC disable quirk\n");
  5972. }
  5973. /*
  5974. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  5975. * brightness value
  5976. */
  5977. static void quirk_invert_brightness(struct drm_device *dev)
  5978. {
  5979. struct drm_i915_private *dev_priv = dev->dev_private;
  5980. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  5981. DRM_INFO("applying inverted panel brightness quirk\n");
  5982. }
  5983. struct intel_quirk {
  5984. int device;
  5985. int subsystem_vendor;
  5986. int subsystem_device;
  5987. void (*hook)(struct drm_device *dev);
  5988. };
  5989. static struct intel_quirk intel_quirks[] = {
  5990. /* HP Mini needs pipe A force quirk (LP: #322104) */
  5991. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  5992. /* Thinkpad R31 needs pipe A force quirk */
  5993. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  5994. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  5995. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  5996. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  5997. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  5998. /* ThinkPad X40 needs pipe A force quirk */
  5999. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  6000. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  6001. /* 855 & before need to leave pipe A & dpll A up */
  6002. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  6003. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  6004. /* Lenovo U160 cannot use SSC on LVDS */
  6005. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  6006. /* Sony Vaio Y cannot use SSC on LVDS */
  6007. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  6008. /* Acer Aspire 5734Z must invert backlight brightness */
  6009. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  6010. };
  6011. static void intel_init_quirks(struct drm_device *dev)
  6012. {
  6013. struct pci_dev *d = dev->pdev;
  6014. int i;
  6015. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  6016. struct intel_quirk *q = &intel_quirks[i];
  6017. if (d->device == q->device &&
  6018. (d->subsystem_vendor == q->subsystem_vendor ||
  6019. q->subsystem_vendor == PCI_ANY_ID) &&
  6020. (d->subsystem_device == q->subsystem_device ||
  6021. q->subsystem_device == PCI_ANY_ID))
  6022. q->hook(dev);
  6023. }
  6024. }
  6025. /* Disable the VGA plane that we never use */
  6026. static void i915_disable_vga(struct drm_device *dev)
  6027. {
  6028. struct drm_i915_private *dev_priv = dev->dev_private;
  6029. u8 sr1;
  6030. u32 vga_reg;
  6031. if (HAS_PCH_SPLIT(dev))
  6032. vga_reg = CPU_VGACNTRL;
  6033. else
  6034. vga_reg = VGACNTRL;
  6035. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  6036. outb(SR01, VGA_SR_INDEX);
  6037. sr1 = inb(VGA_SR_DATA);
  6038. outb(sr1 | 1<<5, VGA_SR_DATA);
  6039. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  6040. udelay(300);
  6041. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  6042. POSTING_READ(vga_reg);
  6043. }
  6044. static void ivb_pch_pwm_override(struct drm_device *dev)
  6045. {
  6046. struct drm_i915_private *dev_priv = dev->dev_private;
  6047. /*
  6048. * IVB has CPU eDP backlight regs too, set things up to let the
  6049. * PCH regs control the backlight
  6050. */
  6051. I915_WRITE(BLC_PWM_CPU_CTL2, BLM_PWM_ENABLE);
  6052. I915_WRITE(BLC_PWM_CPU_CTL, 0);
  6053. I915_WRITE(BLC_PWM_PCH_CTL1, BLM_PCH_PWM_ENABLE | BLM_PCH_OVERRIDE_ENABLE);
  6054. }
  6055. void intel_modeset_init_hw(struct drm_device *dev)
  6056. {
  6057. /* We attempt to init the necessary power wells early in the initialization
  6058. * time, so the subsystems that expect power to be enabled can work.
  6059. */
  6060. intel_init_power_wells(dev);
  6061. intel_prepare_ddi(dev);
  6062. intel_init_clock_gating(dev);
  6063. mutex_lock(&dev->struct_mutex);
  6064. intel_enable_gt_powersave(dev);
  6065. mutex_unlock(&dev->struct_mutex);
  6066. if (IS_IVYBRIDGE(dev))
  6067. ivb_pch_pwm_override(dev);
  6068. }
  6069. void intel_modeset_init(struct drm_device *dev)
  6070. {
  6071. struct drm_i915_private *dev_priv = dev->dev_private;
  6072. int i, ret;
  6073. drm_mode_config_init(dev);
  6074. dev->mode_config.min_width = 0;
  6075. dev->mode_config.min_height = 0;
  6076. dev->mode_config.preferred_depth = 24;
  6077. dev->mode_config.prefer_shadow = 1;
  6078. dev->mode_config.funcs = &intel_mode_funcs;
  6079. intel_init_quirks(dev);
  6080. intel_init_pm(dev);
  6081. intel_init_display(dev);
  6082. if (IS_GEN2(dev)) {
  6083. dev->mode_config.max_width = 2048;
  6084. dev->mode_config.max_height = 2048;
  6085. } else if (IS_GEN3(dev)) {
  6086. dev->mode_config.max_width = 4096;
  6087. dev->mode_config.max_height = 4096;
  6088. } else {
  6089. dev->mode_config.max_width = 8192;
  6090. dev->mode_config.max_height = 8192;
  6091. }
  6092. dev->mode_config.fb_base = dev_priv->mm.gtt_base_addr;
  6093. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  6094. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  6095. for (i = 0; i < dev_priv->num_pipe; i++) {
  6096. intel_crtc_init(dev, i);
  6097. ret = intel_plane_init(dev, i);
  6098. if (ret)
  6099. DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
  6100. }
  6101. intel_pch_pll_init(dev);
  6102. /* Just disable it once at startup */
  6103. i915_disable_vga(dev);
  6104. intel_setup_outputs(dev);
  6105. }
  6106. void intel_modeset_gem_init(struct drm_device *dev)
  6107. {
  6108. intel_modeset_init_hw(dev);
  6109. intel_setup_overlay(dev);
  6110. }
  6111. void intel_modeset_cleanup(struct drm_device *dev)
  6112. {
  6113. struct drm_i915_private *dev_priv = dev->dev_private;
  6114. struct drm_crtc *crtc;
  6115. struct intel_crtc *intel_crtc;
  6116. drm_kms_helper_poll_fini(dev);
  6117. mutex_lock(&dev->struct_mutex);
  6118. intel_unregister_dsm_handler();
  6119. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  6120. /* Skip inactive CRTCs */
  6121. if (!crtc->fb)
  6122. continue;
  6123. intel_crtc = to_intel_crtc(crtc);
  6124. intel_increase_pllclock(crtc);
  6125. }
  6126. intel_disable_fbc(dev);
  6127. intel_disable_gt_powersave(dev);
  6128. ironlake_teardown_rc6(dev);
  6129. if (IS_VALLEYVIEW(dev))
  6130. vlv_init_dpio(dev);
  6131. mutex_unlock(&dev->struct_mutex);
  6132. /* Disable the irq before mode object teardown, for the irq might
  6133. * enqueue unpin/hotplug work. */
  6134. drm_irq_uninstall(dev);
  6135. cancel_work_sync(&dev_priv->hotplug_work);
  6136. cancel_work_sync(&dev_priv->rps.work);
  6137. /* flush any delayed tasks or pending work */
  6138. flush_scheduled_work();
  6139. drm_mode_config_cleanup(dev);
  6140. }
  6141. /*
  6142. * Return which encoder is currently attached for connector.
  6143. */
  6144. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  6145. {
  6146. return &intel_attached_encoder(connector)->base;
  6147. }
  6148. void intel_connector_attach_encoder(struct intel_connector *connector,
  6149. struct intel_encoder *encoder)
  6150. {
  6151. connector->encoder = encoder;
  6152. drm_mode_connector_attach_encoder(&connector->base,
  6153. &encoder->base);
  6154. }
  6155. /*
  6156. * set vga decode state - true == enable VGA decode
  6157. */
  6158. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  6159. {
  6160. struct drm_i915_private *dev_priv = dev->dev_private;
  6161. u16 gmch_ctrl;
  6162. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  6163. if (state)
  6164. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  6165. else
  6166. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  6167. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  6168. return 0;
  6169. }
  6170. #ifdef CONFIG_DEBUG_FS
  6171. #include <linux/seq_file.h>
  6172. struct intel_display_error_state {
  6173. struct intel_cursor_error_state {
  6174. u32 control;
  6175. u32 position;
  6176. u32 base;
  6177. u32 size;
  6178. } cursor[2];
  6179. struct intel_pipe_error_state {
  6180. u32 conf;
  6181. u32 source;
  6182. u32 htotal;
  6183. u32 hblank;
  6184. u32 hsync;
  6185. u32 vtotal;
  6186. u32 vblank;
  6187. u32 vsync;
  6188. } pipe[2];
  6189. struct intel_plane_error_state {
  6190. u32 control;
  6191. u32 stride;
  6192. u32 size;
  6193. u32 pos;
  6194. u32 addr;
  6195. u32 surface;
  6196. u32 tile_offset;
  6197. } plane[2];
  6198. };
  6199. struct intel_display_error_state *
  6200. intel_display_capture_error_state(struct drm_device *dev)
  6201. {
  6202. drm_i915_private_t *dev_priv = dev->dev_private;
  6203. struct intel_display_error_state *error;
  6204. int i;
  6205. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  6206. if (error == NULL)
  6207. return NULL;
  6208. for (i = 0; i < 2; i++) {
  6209. error->cursor[i].control = I915_READ(CURCNTR(i));
  6210. error->cursor[i].position = I915_READ(CURPOS(i));
  6211. error->cursor[i].base = I915_READ(CURBASE(i));
  6212. error->plane[i].control = I915_READ(DSPCNTR(i));
  6213. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  6214. error->plane[i].size = I915_READ(DSPSIZE(i));
  6215. error->plane[i].pos = I915_READ(DSPPOS(i));
  6216. error->plane[i].addr = I915_READ(DSPADDR(i));
  6217. if (INTEL_INFO(dev)->gen >= 4) {
  6218. error->plane[i].surface = I915_READ(DSPSURF(i));
  6219. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  6220. }
  6221. error->pipe[i].conf = I915_READ(PIPECONF(i));
  6222. error->pipe[i].source = I915_READ(PIPESRC(i));
  6223. error->pipe[i].htotal = I915_READ(HTOTAL(i));
  6224. error->pipe[i].hblank = I915_READ(HBLANK(i));
  6225. error->pipe[i].hsync = I915_READ(HSYNC(i));
  6226. error->pipe[i].vtotal = I915_READ(VTOTAL(i));
  6227. error->pipe[i].vblank = I915_READ(VBLANK(i));
  6228. error->pipe[i].vsync = I915_READ(VSYNC(i));
  6229. }
  6230. return error;
  6231. }
  6232. void
  6233. intel_display_print_error_state(struct seq_file *m,
  6234. struct drm_device *dev,
  6235. struct intel_display_error_state *error)
  6236. {
  6237. int i;
  6238. for (i = 0; i < 2; i++) {
  6239. seq_printf(m, "Pipe [%d]:\n", i);
  6240. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  6241. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  6242. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  6243. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  6244. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  6245. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  6246. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  6247. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  6248. seq_printf(m, "Plane [%d]:\n", i);
  6249. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  6250. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  6251. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  6252. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  6253. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  6254. if (INTEL_INFO(dev)->gen >= 4) {
  6255. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  6256. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  6257. }
  6258. seq_printf(m, "Cursor [%d]:\n", i);
  6259. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  6260. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  6261. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  6262. }
  6263. }
  6264. #endif