intel_display.c 182 KB

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