intel_display.c 197 KB

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