intel_display.c 197 KB

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