intel_display.c 195 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920
  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/module.h>
  27. #include <linux/input.h>
  28. #include <linux/i2c.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/vgaarb.h>
  32. #include "drmP.h"
  33. #include "intel_drv.h"
  34. #include "i915_drm.h"
  35. #include "i915_drv.h"
  36. #include "i915_trace.h"
  37. #include "drm_dp_helper.h"
  38. #include "drm_crtc_helper.h"
  39. #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  40. bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
  41. static void intel_update_watermarks(struct drm_device *dev);
  42. static void intel_increase_pllclock(struct drm_crtc *crtc);
  43. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  44. typedef struct {
  45. /* given values */
  46. int n;
  47. int m1, m2;
  48. int p1, p2;
  49. /* derived values */
  50. int dot;
  51. int vco;
  52. int m;
  53. int p;
  54. } intel_clock_t;
  55. typedef struct {
  56. int min, max;
  57. } intel_range_t;
  58. typedef struct {
  59. int dot_limit;
  60. int p2_slow, p2_fast;
  61. } intel_p2_t;
  62. #define INTEL_P2_NUM 2
  63. typedef struct intel_limit intel_limit_t;
  64. struct intel_limit {
  65. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  66. intel_p2_t p2;
  67. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  68. int, int, intel_clock_t *);
  69. };
  70. #define I8XX_DOT_MIN 25000
  71. #define I8XX_DOT_MAX 350000
  72. #define I8XX_VCO_MIN 930000
  73. #define I8XX_VCO_MAX 1400000
  74. #define I8XX_N_MIN 3
  75. #define I8XX_N_MAX 16
  76. #define I8XX_M_MIN 96
  77. #define I8XX_M_MAX 140
  78. #define I8XX_M1_MIN 18
  79. #define I8XX_M1_MAX 26
  80. #define I8XX_M2_MIN 6
  81. #define I8XX_M2_MAX 16
  82. #define I8XX_P_MIN 4
  83. #define I8XX_P_MAX 128
  84. #define I8XX_P1_MIN 2
  85. #define I8XX_P1_MAX 33
  86. #define I8XX_P1_LVDS_MIN 1
  87. #define I8XX_P1_LVDS_MAX 6
  88. #define I8XX_P2_SLOW 4
  89. #define I8XX_P2_FAST 2
  90. #define I8XX_P2_LVDS_SLOW 14
  91. #define I8XX_P2_LVDS_FAST 7
  92. #define I8XX_P2_SLOW_LIMIT 165000
  93. #define I9XX_DOT_MIN 20000
  94. #define I9XX_DOT_MAX 400000
  95. #define I9XX_VCO_MIN 1400000
  96. #define I9XX_VCO_MAX 2800000
  97. #define PINEVIEW_VCO_MIN 1700000
  98. #define PINEVIEW_VCO_MAX 3500000
  99. #define I9XX_N_MIN 1
  100. #define I9XX_N_MAX 6
  101. /* Pineview's Ncounter is a ring counter */
  102. #define PINEVIEW_N_MIN 3
  103. #define PINEVIEW_N_MAX 6
  104. #define I9XX_M_MIN 70
  105. #define I9XX_M_MAX 120
  106. #define PINEVIEW_M_MIN 2
  107. #define PINEVIEW_M_MAX 256
  108. #define I9XX_M1_MIN 10
  109. #define I9XX_M1_MAX 22
  110. #define I9XX_M2_MIN 5
  111. #define I9XX_M2_MAX 9
  112. /* Pineview M1 is reserved, and must be 0 */
  113. #define PINEVIEW_M1_MIN 0
  114. #define PINEVIEW_M1_MAX 0
  115. #define PINEVIEW_M2_MIN 0
  116. #define PINEVIEW_M2_MAX 254
  117. #define I9XX_P_SDVO_DAC_MIN 5
  118. #define I9XX_P_SDVO_DAC_MAX 80
  119. #define I9XX_P_LVDS_MIN 7
  120. #define I9XX_P_LVDS_MAX 98
  121. #define PINEVIEW_P_LVDS_MIN 7
  122. #define PINEVIEW_P_LVDS_MAX 112
  123. #define I9XX_P1_MIN 1
  124. #define I9XX_P1_MAX 8
  125. #define I9XX_P2_SDVO_DAC_SLOW 10
  126. #define I9XX_P2_SDVO_DAC_FAST 5
  127. #define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
  128. #define I9XX_P2_LVDS_SLOW 14
  129. #define I9XX_P2_LVDS_FAST 7
  130. #define I9XX_P2_LVDS_SLOW_LIMIT 112000
  131. /*The parameter is for SDVO on G4x platform*/
  132. #define G4X_DOT_SDVO_MIN 25000
  133. #define G4X_DOT_SDVO_MAX 270000
  134. #define G4X_VCO_MIN 1750000
  135. #define G4X_VCO_MAX 3500000
  136. #define G4X_N_SDVO_MIN 1
  137. #define G4X_N_SDVO_MAX 4
  138. #define G4X_M_SDVO_MIN 104
  139. #define G4X_M_SDVO_MAX 138
  140. #define G4X_M1_SDVO_MIN 17
  141. #define G4X_M1_SDVO_MAX 23
  142. #define G4X_M2_SDVO_MIN 5
  143. #define G4X_M2_SDVO_MAX 11
  144. #define G4X_P_SDVO_MIN 10
  145. #define G4X_P_SDVO_MAX 30
  146. #define G4X_P1_SDVO_MIN 1
  147. #define G4X_P1_SDVO_MAX 3
  148. #define G4X_P2_SDVO_SLOW 10
  149. #define G4X_P2_SDVO_FAST 10
  150. #define G4X_P2_SDVO_LIMIT 270000
  151. /*The parameter is for HDMI_DAC on G4x platform*/
  152. #define G4X_DOT_HDMI_DAC_MIN 22000
  153. #define G4X_DOT_HDMI_DAC_MAX 400000
  154. #define G4X_N_HDMI_DAC_MIN 1
  155. #define G4X_N_HDMI_DAC_MAX 4
  156. #define G4X_M_HDMI_DAC_MIN 104
  157. #define G4X_M_HDMI_DAC_MAX 138
  158. #define G4X_M1_HDMI_DAC_MIN 16
  159. #define G4X_M1_HDMI_DAC_MAX 23
  160. #define G4X_M2_HDMI_DAC_MIN 5
  161. #define G4X_M2_HDMI_DAC_MAX 11
  162. #define G4X_P_HDMI_DAC_MIN 5
  163. #define G4X_P_HDMI_DAC_MAX 80
  164. #define G4X_P1_HDMI_DAC_MIN 1
  165. #define G4X_P1_HDMI_DAC_MAX 8
  166. #define G4X_P2_HDMI_DAC_SLOW 10
  167. #define G4X_P2_HDMI_DAC_FAST 5
  168. #define G4X_P2_HDMI_DAC_LIMIT 165000
  169. /*The parameter is for SINGLE_CHANNEL_LVDS on G4x platform*/
  170. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MIN 20000
  171. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MAX 115000
  172. #define G4X_N_SINGLE_CHANNEL_LVDS_MIN 1
  173. #define G4X_N_SINGLE_CHANNEL_LVDS_MAX 3
  174. #define G4X_M_SINGLE_CHANNEL_LVDS_MIN 104
  175. #define G4X_M_SINGLE_CHANNEL_LVDS_MAX 138
  176. #define G4X_M1_SINGLE_CHANNEL_LVDS_MIN 17
  177. #define G4X_M1_SINGLE_CHANNEL_LVDS_MAX 23
  178. #define G4X_M2_SINGLE_CHANNEL_LVDS_MIN 5
  179. #define G4X_M2_SINGLE_CHANNEL_LVDS_MAX 11
  180. #define G4X_P_SINGLE_CHANNEL_LVDS_MIN 28
  181. #define G4X_P_SINGLE_CHANNEL_LVDS_MAX 112
  182. #define G4X_P1_SINGLE_CHANNEL_LVDS_MIN 2
  183. #define G4X_P1_SINGLE_CHANNEL_LVDS_MAX 8
  184. #define G4X_P2_SINGLE_CHANNEL_LVDS_SLOW 14
  185. #define G4X_P2_SINGLE_CHANNEL_LVDS_FAST 14
  186. #define G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT 0
  187. /*The parameter is for DUAL_CHANNEL_LVDS on G4x platform*/
  188. #define G4X_DOT_DUAL_CHANNEL_LVDS_MIN 80000
  189. #define G4X_DOT_DUAL_CHANNEL_LVDS_MAX 224000
  190. #define G4X_N_DUAL_CHANNEL_LVDS_MIN 1
  191. #define G4X_N_DUAL_CHANNEL_LVDS_MAX 3
  192. #define G4X_M_DUAL_CHANNEL_LVDS_MIN 104
  193. #define G4X_M_DUAL_CHANNEL_LVDS_MAX 138
  194. #define G4X_M1_DUAL_CHANNEL_LVDS_MIN 17
  195. #define G4X_M1_DUAL_CHANNEL_LVDS_MAX 23
  196. #define G4X_M2_DUAL_CHANNEL_LVDS_MIN 5
  197. #define G4X_M2_DUAL_CHANNEL_LVDS_MAX 11
  198. #define G4X_P_DUAL_CHANNEL_LVDS_MIN 14
  199. #define G4X_P_DUAL_CHANNEL_LVDS_MAX 42
  200. #define G4X_P1_DUAL_CHANNEL_LVDS_MIN 2
  201. #define G4X_P1_DUAL_CHANNEL_LVDS_MAX 6
  202. #define G4X_P2_DUAL_CHANNEL_LVDS_SLOW 7
  203. #define G4X_P2_DUAL_CHANNEL_LVDS_FAST 7
  204. #define G4X_P2_DUAL_CHANNEL_LVDS_LIMIT 0
  205. /*The parameter is for DISPLAY PORT on G4x platform*/
  206. #define G4X_DOT_DISPLAY_PORT_MIN 161670
  207. #define G4X_DOT_DISPLAY_PORT_MAX 227000
  208. #define G4X_N_DISPLAY_PORT_MIN 1
  209. #define G4X_N_DISPLAY_PORT_MAX 2
  210. #define G4X_M_DISPLAY_PORT_MIN 97
  211. #define G4X_M_DISPLAY_PORT_MAX 108
  212. #define G4X_M1_DISPLAY_PORT_MIN 0x10
  213. #define G4X_M1_DISPLAY_PORT_MAX 0x12
  214. #define G4X_M2_DISPLAY_PORT_MIN 0x05
  215. #define G4X_M2_DISPLAY_PORT_MAX 0x06
  216. #define G4X_P_DISPLAY_PORT_MIN 10
  217. #define G4X_P_DISPLAY_PORT_MAX 20
  218. #define G4X_P1_DISPLAY_PORT_MIN 1
  219. #define G4X_P1_DISPLAY_PORT_MAX 2
  220. #define G4X_P2_DISPLAY_PORT_SLOW 10
  221. #define G4X_P2_DISPLAY_PORT_FAST 10
  222. #define G4X_P2_DISPLAY_PORT_LIMIT 0
  223. /* Ironlake / Sandybridge */
  224. /* as we calculate clock using (register_value + 2) for
  225. N/M1/M2, so here the range value for them is (actual_value-2).
  226. */
  227. #define IRONLAKE_DOT_MIN 25000
  228. #define IRONLAKE_DOT_MAX 350000
  229. #define IRONLAKE_VCO_MIN 1760000
  230. #define IRONLAKE_VCO_MAX 3510000
  231. #define IRONLAKE_M1_MIN 12
  232. #define IRONLAKE_M1_MAX 22
  233. #define IRONLAKE_M2_MIN 5
  234. #define IRONLAKE_M2_MAX 9
  235. #define IRONLAKE_P2_DOT_LIMIT 225000 /* 225Mhz */
  236. /* We have parameter ranges for different type of outputs. */
  237. /* DAC & HDMI Refclk 120Mhz */
  238. #define IRONLAKE_DAC_N_MIN 1
  239. #define IRONLAKE_DAC_N_MAX 5
  240. #define IRONLAKE_DAC_M_MIN 79
  241. #define IRONLAKE_DAC_M_MAX 127
  242. #define IRONLAKE_DAC_P_MIN 5
  243. #define IRONLAKE_DAC_P_MAX 80
  244. #define IRONLAKE_DAC_P1_MIN 1
  245. #define IRONLAKE_DAC_P1_MAX 8
  246. #define IRONLAKE_DAC_P2_SLOW 10
  247. #define IRONLAKE_DAC_P2_FAST 5
  248. /* LVDS single-channel 120Mhz refclk */
  249. #define IRONLAKE_LVDS_S_N_MIN 1
  250. #define IRONLAKE_LVDS_S_N_MAX 3
  251. #define IRONLAKE_LVDS_S_M_MIN 79
  252. #define IRONLAKE_LVDS_S_M_MAX 118
  253. #define IRONLAKE_LVDS_S_P_MIN 28
  254. #define IRONLAKE_LVDS_S_P_MAX 112
  255. #define IRONLAKE_LVDS_S_P1_MIN 2
  256. #define IRONLAKE_LVDS_S_P1_MAX 8
  257. #define IRONLAKE_LVDS_S_P2_SLOW 14
  258. #define IRONLAKE_LVDS_S_P2_FAST 14
  259. /* LVDS dual-channel 120Mhz refclk */
  260. #define IRONLAKE_LVDS_D_N_MIN 1
  261. #define IRONLAKE_LVDS_D_N_MAX 3
  262. #define IRONLAKE_LVDS_D_M_MIN 79
  263. #define IRONLAKE_LVDS_D_M_MAX 127
  264. #define IRONLAKE_LVDS_D_P_MIN 14
  265. #define IRONLAKE_LVDS_D_P_MAX 56
  266. #define IRONLAKE_LVDS_D_P1_MIN 2
  267. #define IRONLAKE_LVDS_D_P1_MAX 8
  268. #define IRONLAKE_LVDS_D_P2_SLOW 7
  269. #define IRONLAKE_LVDS_D_P2_FAST 7
  270. /* LVDS single-channel 100Mhz refclk */
  271. #define IRONLAKE_LVDS_S_SSC_N_MIN 1
  272. #define IRONLAKE_LVDS_S_SSC_N_MAX 2
  273. #define IRONLAKE_LVDS_S_SSC_M_MIN 79
  274. #define IRONLAKE_LVDS_S_SSC_M_MAX 126
  275. #define IRONLAKE_LVDS_S_SSC_P_MIN 28
  276. #define IRONLAKE_LVDS_S_SSC_P_MAX 112
  277. #define IRONLAKE_LVDS_S_SSC_P1_MIN 2
  278. #define IRONLAKE_LVDS_S_SSC_P1_MAX 8
  279. #define IRONLAKE_LVDS_S_SSC_P2_SLOW 14
  280. #define IRONLAKE_LVDS_S_SSC_P2_FAST 14
  281. /* LVDS dual-channel 100Mhz refclk */
  282. #define IRONLAKE_LVDS_D_SSC_N_MIN 1
  283. #define IRONLAKE_LVDS_D_SSC_N_MAX 3
  284. #define IRONLAKE_LVDS_D_SSC_M_MIN 79
  285. #define IRONLAKE_LVDS_D_SSC_M_MAX 126
  286. #define IRONLAKE_LVDS_D_SSC_P_MIN 14
  287. #define IRONLAKE_LVDS_D_SSC_P_MAX 42
  288. #define IRONLAKE_LVDS_D_SSC_P1_MIN 2
  289. #define IRONLAKE_LVDS_D_SSC_P1_MAX 6
  290. #define IRONLAKE_LVDS_D_SSC_P2_SLOW 7
  291. #define IRONLAKE_LVDS_D_SSC_P2_FAST 7
  292. /* DisplayPort */
  293. #define IRONLAKE_DP_N_MIN 1
  294. #define IRONLAKE_DP_N_MAX 2
  295. #define IRONLAKE_DP_M_MIN 81
  296. #define IRONLAKE_DP_M_MAX 90
  297. #define IRONLAKE_DP_P_MIN 10
  298. #define IRONLAKE_DP_P_MAX 20
  299. #define IRONLAKE_DP_P2_FAST 10
  300. #define IRONLAKE_DP_P2_SLOW 10
  301. #define IRONLAKE_DP_P2_LIMIT 0
  302. #define IRONLAKE_DP_P1_MIN 1
  303. #define IRONLAKE_DP_P1_MAX 2
  304. /* FDI */
  305. #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
  306. static bool
  307. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  308. int target, int refclk, intel_clock_t *best_clock);
  309. static bool
  310. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  311. int target, int refclk, intel_clock_t *best_clock);
  312. static bool
  313. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  314. int target, int refclk, intel_clock_t *best_clock);
  315. static bool
  316. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  317. int target, int refclk, intel_clock_t *best_clock);
  318. static inline u32 /* units of 100MHz */
  319. intel_fdi_link_freq(struct drm_device *dev)
  320. {
  321. if (IS_GEN5(dev)) {
  322. struct drm_i915_private *dev_priv = dev->dev_private;
  323. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  324. } else
  325. return 27;
  326. }
  327. static const intel_limit_t intel_limits_i8xx_dvo = {
  328. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  329. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  330. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  331. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  332. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  333. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  334. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  335. .p1 = { .min = I8XX_P1_MIN, .max = I8XX_P1_MAX },
  336. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  337. .p2_slow = I8XX_P2_SLOW, .p2_fast = I8XX_P2_FAST },
  338. .find_pll = intel_find_best_PLL,
  339. };
  340. static const intel_limit_t intel_limits_i8xx_lvds = {
  341. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  342. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  343. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  344. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  345. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  346. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  347. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  348. .p1 = { .min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX },
  349. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  350. .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST },
  351. .find_pll = intel_find_best_PLL,
  352. };
  353. static const intel_limit_t intel_limits_i9xx_sdvo = {
  354. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  355. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  356. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  357. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  358. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  359. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  360. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  361. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  362. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  363. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  364. .find_pll = intel_find_best_PLL,
  365. };
  366. static const intel_limit_t intel_limits_i9xx_lvds = {
  367. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  368. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  369. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  370. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  371. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  372. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  373. .p = { .min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX },
  374. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  375. /* The single-channel range is 25-112Mhz, and dual-channel
  376. * is 80-224Mhz. Prefer single channel as much as possible.
  377. */
  378. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  379. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
  380. .find_pll = intel_find_best_PLL,
  381. };
  382. /* below parameter and function is for G4X Chipset Family*/
  383. static const intel_limit_t intel_limits_g4x_sdvo = {
  384. .dot = { .min = G4X_DOT_SDVO_MIN, .max = G4X_DOT_SDVO_MAX },
  385. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  386. .n = { .min = G4X_N_SDVO_MIN, .max = G4X_N_SDVO_MAX },
  387. .m = { .min = G4X_M_SDVO_MIN, .max = G4X_M_SDVO_MAX },
  388. .m1 = { .min = G4X_M1_SDVO_MIN, .max = G4X_M1_SDVO_MAX },
  389. .m2 = { .min = G4X_M2_SDVO_MIN, .max = G4X_M2_SDVO_MAX },
  390. .p = { .min = G4X_P_SDVO_MIN, .max = G4X_P_SDVO_MAX },
  391. .p1 = { .min = G4X_P1_SDVO_MIN, .max = G4X_P1_SDVO_MAX},
  392. .p2 = { .dot_limit = G4X_P2_SDVO_LIMIT,
  393. .p2_slow = G4X_P2_SDVO_SLOW,
  394. .p2_fast = G4X_P2_SDVO_FAST
  395. },
  396. .find_pll = intel_g4x_find_best_PLL,
  397. };
  398. static const intel_limit_t intel_limits_g4x_hdmi = {
  399. .dot = { .min = G4X_DOT_HDMI_DAC_MIN, .max = G4X_DOT_HDMI_DAC_MAX },
  400. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  401. .n = { .min = G4X_N_HDMI_DAC_MIN, .max = G4X_N_HDMI_DAC_MAX },
  402. .m = { .min = G4X_M_HDMI_DAC_MIN, .max = G4X_M_HDMI_DAC_MAX },
  403. .m1 = { .min = G4X_M1_HDMI_DAC_MIN, .max = G4X_M1_HDMI_DAC_MAX },
  404. .m2 = { .min = G4X_M2_HDMI_DAC_MIN, .max = G4X_M2_HDMI_DAC_MAX },
  405. .p = { .min = G4X_P_HDMI_DAC_MIN, .max = G4X_P_HDMI_DAC_MAX },
  406. .p1 = { .min = G4X_P1_HDMI_DAC_MIN, .max = G4X_P1_HDMI_DAC_MAX},
  407. .p2 = { .dot_limit = G4X_P2_HDMI_DAC_LIMIT,
  408. .p2_slow = G4X_P2_HDMI_DAC_SLOW,
  409. .p2_fast = G4X_P2_HDMI_DAC_FAST
  410. },
  411. .find_pll = intel_g4x_find_best_PLL,
  412. };
  413. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  414. .dot = { .min = G4X_DOT_SINGLE_CHANNEL_LVDS_MIN,
  415. .max = G4X_DOT_SINGLE_CHANNEL_LVDS_MAX },
  416. .vco = { .min = G4X_VCO_MIN,
  417. .max = G4X_VCO_MAX },
  418. .n = { .min = G4X_N_SINGLE_CHANNEL_LVDS_MIN,
  419. .max = G4X_N_SINGLE_CHANNEL_LVDS_MAX },
  420. .m = { .min = G4X_M_SINGLE_CHANNEL_LVDS_MIN,
  421. .max = G4X_M_SINGLE_CHANNEL_LVDS_MAX },
  422. .m1 = { .min = G4X_M1_SINGLE_CHANNEL_LVDS_MIN,
  423. .max = G4X_M1_SINGLE_CHANNEL_LVDS_MAX },
  424. .m2 = { .min = G4X_M2_SINGLE_CHANNEL_LVDS_MIN,
  425. .max = G4X_M2_SINGLE_CHANNEL_LVDS_MAX },
  426. .p = { .min = G4X_P_SINGLE_CHANNEL_LVDS_MIN,
  427. .max = G4X_P_SINGLE_CHANNEL_LVDS_MAX },
  428. .p1 = { .min = G4X_P1_SINGLE_CHANNEL_LVDS_MIN,
  429. .max = G4X_P1_SINGLE_CHANNEL_LVDS_MAX },
  430. .p2 = { .dot_limit = G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT,
  431. .p2_slow = G4X_P2_SINGLE_CHANNEL_LVDS_SLOW,
  432. .p2_fast = G4X_P2_SINGLE_CHANNEL_LVDS_FAST
  433. },
  434. .find_pll = intel_g4x_find_best_PLL,
  435. };
  436. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  437. .dot = { .min = G4X_DOT_DUAL_CHANNEL_LVDS_MIN,
  438. .max = G4X_DOT_DUAL_CHANNEL_LVDS_MAX },
  439. .vco = { .min = G4X_VCO_MIN,
  440. .max = G4X_VCO_MAX },
  441. .n = { .min = G4X_N_DUAL_CHANNEL_LVDS_MIN,
  442. .max = G4X_N_DUAL_CHANNEL_LVDS_MAX },
  443. .m = { .min = G4X_M_DUAL_CHANNEL_LVDS_MIN,
  444. .max = G4X_M_DUAL_CHANNEL_LVDS_MAX },
  445. .m1 = { .min = G4X_M1_DUAL_CHANNEL_LVDS_MIN,
  446. .max = G4X_M1_DUAL_CHANNEL_LVDS_MAX },
  447. .m2 = { .min = G4X_M2_DUAL_CHANNEL_LVDS_MIN,
  448. .max = G4X_M2_DUAL_CHANNEL_LVDS_MAX },
  449. .p = { .min = G4X_P_DUAL_CHANNEL_LVDS_MIN,
  450. .max = G4X_P_DUAL_CHANNEL_LVDS_MAX },
  451. .p1 = { .min = G4X_P1_DUAL_CHANNEL_LVDS_MIN,
  452. .max = G4X_P1_DUAL_CHANNEL_LVDS_MAX },
  453. .p2 = { .dot_limit = G4X_P2_DUAL_CHANNEL_LVDS_LIMIT,
  454. .p2_slow = G4X_P2_DUAL_CHANNEL_LVDS_SLOW,
  455. .p2_fast = G4X_P2_DUAL_CHANNEL_LVDS_FAST
  456. },
  457. .find_pll = intel_g4x_find_best_PLL,
  458. };
  459. static const intel_limit_t intel_limits_g4x_display_port = {
  460. .dot = { .min = G4X_DOT_DISPLAY_PORT_MIN,
  461. .max = G4X_DOT_DISPLAY_PORT_MAX },
  462. .vco = { .min = G4X_VCO_MIN,
  463. .max = G4X_VCO_MAX},
  464. .n = { .min = G4X_N_DISPLAY_PORT_MIN,
  465. .max = G4X_N_DISPLAY_PORT_MAX },
  466. .m = { .min = G4X_M_DISPLAY_PORT_MIN,
  467. .max = G4X_M_DISPLAY_PORT_MAX },
  468. .m1 = { .min = G4X_M1_DISPLAY_PORT_MIN,
  469. .max = G4X_M1_DISPLAY_PORT_MAX },
  470. .m2 = { .min = G4X_M2_DISPLAY_PORT_MIN,
  471. .max = G4X_M2_DISPLAY_PORT_MAX },
  472. .p = { .min = G4X_P_DISPLAY_PORT_MIN,
  473. .max = G4X_P_DISPLAY_PORT_MAX },
  474. .p1 = { .min = G4X_P1_DISPLAY_PORT_MIN,
  475. .max = G4X_P1_DISPLAY_PORT_MAX},
  476. .p2 = { .dot_limit = G4X_P2_DISPLAY_PORT_LIMIT,
  477. .p2_slow = G4X_P2_DISPLAY_PORT_SLOW,
  478. .p2_fast = G4X_P2_DISPLAY_PORT_FAST },
  479. .find_pll = intel_find_pll_g4x_dp,
  480. };
  481. static const intel_limit_t intel_limits_pineview_sdvo = {
  482. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
  483. .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
  484. .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
  485. .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
  486. .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
  487. .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
  488. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  489. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  490. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  491. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  492. .find_pll = intel_find_best_PLL,
  493. };
  494. static const intel_limit_t intel_limits_pineview_lvds = {
  495. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  496. .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
  497. .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
  498. .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
  499. .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
  500. .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
  501. .p = { .min = PINEVIEW_P_LVDS_MIN, .max = PINEVIEW_P_LVDS_MAX },
  502. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  503. /* Pineview only supports single-channel mode. */
  504. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  505. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_SLOW },
  506. .find_pll = intel_find_best_PLL,
  507. };
  508. static const intel_limit_t intel_limits_ironlake_dac = {
  509. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  510. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  511. .n = { .min = IRONLAKE_DAC_N_MIN, .max = IRONLAKE_DAC_N_MAX },
  512. .m = { .min = IRONLAKE_DAC_M_MIN, .max = IRONLAKE_DAC_M_MAX },
  513. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  514. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  515. .p = { .min = IRONLAKE_DAC_P_MIN, .max = IRONLAKE_DAC_P_MAX },
  516. .p1 = { .min = IRONLAKE_DAC_P1_MIN, .max = IRONLAKE_DAC_P1_MAX },
  517. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  518. .p2_slow = IRONLAKE_DAC_P2_SLOW,
  519. .p2_fast = IRONLAKE_DAC_P2_FAST },
  520. .find_pll = intel_g4x_find_best_PLL,
  521. };
  522. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  523. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  524. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  525. .n = { .min = IRONLAKE_LVDS_S_N_MIN, .max = IRONLAKE_LVDS_S_N_MAX },
  526. .m = { .min = IRONLAKE_LVDS_S_M_MIN, .max = IRONLAKE_LVDS_S_M_MAX },
  527. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  528. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  529. .p = { .min = IRONLAKE_LVDS_S_P_MIN, .max = IRONLAKE_LVDS_S_P_MAX },
  530. .p1 = { .min = IRONLAKE_LVDS_S_P1_MIN, .max = IRONLAKE_LVDS_S_P1_MAX },
  531. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  532. .p2_slow = IRONLAKE_LVDS_S_P2_SLOW,
  533. .p2_fast = IRONLAKE_LVDS_S_P2_FAST },
  534. .find_pll = intel_g4x_find_best_PLL,
  535. };
  536. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  537. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  538. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  539. .n = { .min = IRONLAKE_LVDS_D_N_MIN, .max = IRONLAKE_LVDS_D_N_MAX },
  540. .m = { .min = IRONLAKE_LVDS_D_M_MIN, .max = IRONLAKE_LVDS_D_M_MAX },
  541. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  542. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  543. .p = { .min = IRONLAKE_LVDS_D_P_MIN, .max = IRONLAKE_LVDS_D_P_MAX },
  544. .p1 = { .min = IRONLAKE_LVDS_D_P1_MIN, .max = IRONLAKE_LVDS_D_P1_MAX },
  545. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  546. .p2_slow = IRONLAKE_LVDS_D_P2_SLOW,
  547. .p2_fast = IRONLAKE_LVDS_D_P2_FAST },
  548. .find_pll = intel_g4x_find_best_PLL,
  549. };
  550. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  551. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  552. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  553. .n = { .min = IRONLAKE_LVDS_S_SSC_N_MIN, .max = IRONLAKE_LVDS_S_SSC_N_MAX },
  554. .m = { .min = IRONLAKE_LVDS_S_SSC_M_MIN, .max = IRONLAKE_LVDS_S_SSC_M_MAX },
  555. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  556. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  557. .p = { .min = IRONLAKE_LVDS_S_SSC_P_MIN, .max = IRONLAKE_LVDS_S_SSC_P_MAX },
  558. .p1 = { .min = IRONLAKE_LVDS_S_SSC_P1_MIN,.max = IRONLAKE_LVDS_S_SSC_P1_MAX },
  559. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  560. .p2_slow = IRONLAKE_LVDS_S_SSC_P2_SLOW,
  561. .p2_fast = IRONLAKE_LVDS_S_SSC_P2_FAST },
  562. .find_pll = intel_g4x_find_best_PLL,
  563. };
  564. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  565. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  566. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  567. .n = { .min = IRONLAKE_LVDS_D_SSC_N_MIN, .max = IRONLAKE_LVDS_D_SSC_N_MAX },
  568. .m = { .min = IRONLAKE_LVDS_D_SSC_M_MIN, .max = IRONLAKE_LVDS_D_SSC_M_MAX },
  569. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  570. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  571. .p = { .min = IRONLAKE_LVDS_D_SSC_P_MIN, .max = IRONLAKE_LVDS_D_SSC_P_MAX },
  572. .p1 = { .min = IRONLAKE_LVDS_D_SSC_P1_MIN,.max = IRONLAKE_LVDS_D_SSC_P1_MAX },
  573. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  574. .p2_slow = IRONLAKE_LVDS_D_SSC_P2_SLOW,
  575. .p2_fast = IRONLAKE_LVDS_D_SSC_P2_FAST },
  576. .find_pll = intel_g4x_find_best_PLL,
  577. };
  578. static const intel_limit_t intel_limits_ironlake_display_port = {
  579. .dot = { .min = IRONLAKE_DOT_MIN,
  580. .max = IRONLAKE_DOT_MAX },
  581. .vco = { .min = IRONLAKE_VCO_MIN,
  582. .max = IRONLAKE_VCO_MAX},
  583. .n = { .min = IRONLAKE_DP_N_MIN,
  584. .max = IRONLAKE_DP_N_MAX },
  585. .m = { .min = IRONLAKE_DP_M_MIN,
  586. .max = IRONLAKE_DP_M_MAX },
  587. .m1 = { .min = IRONLAKE_M1_MIN,
  588. .max = IRONLAKE_M1_MAX },
  589. .m2 = { .min = IRONLAKE_M2_MIN,
  590. .max = IRONLAKE_M2_MAX },
  591. .p = { .min = IRONLAKE_DP_P_MIN,
  592. .max = IRONLAKE_DP_P_MAX },
  593. .p1 = { .min = IRONLAKE_DP_P1_MIN,
  594. .max = IRONLAKE_DP_P1_MAX},
  595. .p2 = { .dot_limit = IRONLAKE_DP_P2_LIMIT,
  596. .p2_slow = IRONLAKE_DP_P2_SLOW,
  597. .p2_fast = IRONLAKE_DP_P2_FAST },
  598. .find_pll = intel_find_pll_ironlake_dp,
  599. };
  600. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  601. int refclk)
  602. {
  603. struct drm_device *dev = crtc->dev;
  604. struct drm_i915_private *dev_priv = dev->dev_private;
  605. const intel_limit_t *limit;
  606. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  607. if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
  608. LVDS_CLKB_POWER_UP) {
  609. /* LVDS dual channel */
  610. if (refclk == 100000)
  611. limit = &intel_limits_ironlake_dual_lvds_100m;
  612. else
  613. limit = &intel_limits_ironlake_dual_lvds;
  614. } else {
  615. if (refclk == 100000)
  616. limit = &intel_limits_ironlake_single_lvds_100m;
  617. else
  618. limit = &intel_limits_ironlake_single_lvds;
  619. }
  620. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  621. HAS_eDP)
  622. limit = &intel_limits_ironlake_display_port;
  623. else
  624. limit = &intel_limits_ironlake_dac;
  625. return limit;
  626. }
  627. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  628. {
  629. struct drm_device *dev = crtc->dev;
  630. struct drm_i915_private *dev_priv = dev->dev_private;
  631. const intel_limit_t *limit;
  632. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  633. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  634. LVDS_CLKB_POWER_UP)
  635. /* LVDS with dual channel */
  636. limit = &intel_limits_g4x_dual_channel_lvds;
  637. else
  638. /* LVDS with dual channel */
  639. limit = &intel_limits_g4x_single_channel_lvds;
  640. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  641. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  642. limit = &intel_limits_g4x_hdmi;
  643. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  644. limit = &intel_limits_g4x_sdvo;
  645. } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  646. limit = &intel_limits_g4x_display_port;
  647. } else /* The option is for other outputs */
  648. limit = &intel_limits_i9xx_sdvo;
  649. return limit;
  650. }
  651. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  652. {
  653. struct drm_device *dev = crtc->dev;
  654. const intel_limit_t *limit;
  655. if (HAS_PCH_SPLIT(dev))
  656. limit = intel_ironlake_limit(crtc, refclk);
  657. else if (IS_G4X(dev)) {
  658. limit = intel_g4x_limit(crtc);
  659. } else if (IS_PINEVIEW(dev)) {
  660. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  661. limit = &intel_limits_pineview_lvds;
  662. else
  663. limit = &intel_limits_pineview_sdvo;
  664. } else if (!IS_GEN2(dev)) {
  665. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  666. limit = &intel_limits_i9xx_lvds;
  667. else
  668. limit = &intel_limits_i9xx_sdvo;
  669. } else {
  670. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  671. limit = &intel_limits_i8xx_lvds;
  672. else
  673. limit = &intel_limits_i8xx_dvo;
  674. }
  675. return limit;
  676. }
  677. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  678. static void pineview_clock(int refclk, intel_clock_t *clock)
  679. {
  680. clock->m = clock->m2 + 2;
  681. clock->p = clock->p1 * clock->p2;
  682. clock->vco = refclk * clock->m / clock->n;
  683. clock->dot = clock->vco / clock->p;
  684. }
  685. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  686. {
  687. if (IS_PINEVIEW(dev)) {
  688. pineview_clock(refclk, clock);
  689. return;
  690. }
  691. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  692. clock->p = clock->p1 * clock->p2;
  693. clock->vco = refclk * clock->m / (clock->n + 2);
  694. clock->dot = clock->vco / clock->p;
  695. }
  696. /**
  697. * Returns whether any output on the specified pipe is of the specified type
  698. */
  699. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  700. {
  701. struct drm_device *dev = crtc->dev;
  702. struct drm_mode_config *mode_config = &dev->mode_config;
  703. struct intel_encoder *encoder;
  704. list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
  705. if (encoder->base.crtc == crtc && encoder->type == type)
  706. return true;
  707. return false;
  708. }
  709. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  710. /**
  711. * Returns whether the given set of divisors are valid for a given refclk with
  712. * the given connectors.
  713. */
  714. static bool intel_PLL_is_valid(struct drm_device *dev,
  715. const intel_limit_t *limit,
  716. const intel_clock_t *clock)
  717. {
  718. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  719. INTELPllInvalid ("p1 out of range\n");
  720. if (clock->p < limit->p.min || limit->p.max < clock->p)
  721. INTELPllInvalid ("p out of range\n");
  722. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  723. INTELPllInvalid ("m2 out of range\n");
  724. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  725. INTELPllInvalid ("m1 out of range\n");
  726. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  727. INTELPllInvalid ("m1 <= m2\n");
  728. if (clock->m < limit->m.min || limit->m.max < clock->m)
  729. INTELPllInvalid ("m out of range\n");
  730. if (clock->n < limit->n.min || limit->n.max < clock->n)
  731. INTELPllInvalid ("n out of range\n");
  732. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  733. INTELPllInvalid ("vco out of range\n");
  734. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  735. * connector, etc., rather than just a single range.
  736. */
  737. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  738. INTELPllInvalid ("dot out of range\n");
  739. return true;
  740. }
  741. static bool
  742. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  743. int target, int refclk, intel_clock_t *best_clock)
  744. {
  745. struct drm_device *dev = crtc->dev;
  746. struct drm_i915_private *dev_priv = dev->dev_private;
  747. intel_clock_t clock;
  748. int err = target;
  749. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  750. (I915_READ(LVDS)) != 0) {
  751. /*
  752. * For LVDS, if the panel is on, just rely on its current
  753. * settings for dual-channel. We haven't figured out how to
  754. * reliably set up different single/dual channel state, if we
  755. * even can.
  756. */
  757. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  758. LVDS_CLKB_POWER_UP)
  759. clock.p2 = limit->p2.p2_fast;
  760. else
  761. clock.p2 = limit->p2.p2_slow;
  762. } else {
  763. if (target < limit->p2.dot_limit)
  764. clock.p2 = limit->p2.p2_slow;
  765. else
  766. clock.p2 = limit->p2.p2_fast;
  767. }
  768. memset (best_clock, 0, sizeof (*best_clock));
  769. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  770. clock.m1++) {
  771. for (clock.m2 = limit->m2.min;
  772. clock.m2 <= limit->m2.max; clock.m2++) {
  773. /* m1 is always 0 in Pineview */
  774. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  775. break;
  776. for (clock.n = limit->n.min;
  777. clock.n <= limit->n.max; clock.n++) {
  778. for (clock.p1 = limit->p1.min;
  779. clock.p1 <= limit->p1.max; clock.p1++) {
  780. int this_err;
  781. intel_clock(dev, refclk, &clock);
  782. if (!intel_PLL_is_valid(dev, limit,
  783. &clock))
  784. continue;
  785. this_err = abs(clock.dot - target);
  786. if (this_err < err) {
  787. *best_clock = clock;
  788. err = this_err;
  789. }
  790. }
  791. }
  792. }
  793. }
  794. return (err != target);
  795. }
  796. static bool
  797. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  798. int target, int refclk, intel_clock_t *best_clock)
  799. {
  800. struct drm_device *dev = crtc->dev;
  801. struct drm_i915_private *dev_priv = dev->dev_private;
  802. intel_clock_t clock;
  803. int max_n;
  804. bool found;
  805. /* approximately equals target * 0.00585 */
  806. int err_most = (target >> 8) + (target >> 9);
  807. found = false;
  808. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  809. int lvds_reg;
  810. if (HAS_PCH_SPLIT(dev))
  811. lvds_reg = PCH_LVDS;
  812. else
  813. lvds_reg = LVDS;
  814. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  815. LVDS_CLKB_POWER_UP)
  816. clock.p2 = limit->p2.p2_fast;
  817. else
  818. clock.p2 = limit->p2.p2_slow;
  819. } else {
  820. if (target < limit->p2.dot_limit)
  821. clock.p2 = limit->p2.p2_slow;
  822. else
  823. clock.p2 = limit->p2.p2_fast;
  824. }
  825. memset(best_clock, 0, sizeof(*best_clock));
  826. max_n = limit->n.max;
  827. /* based on hardware requirement, prefer smaller n to precision */
  828. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  829. /* based on hardware requirement, prefere larger m1,m2 */
  830. for (clock.m1 = limit->m1.max;
  831. clock.m1 >= limit->m1.min; clock.m1--) {
  832. for (clock.m2 = limit->m2.max;
  833. clock.m2 >= limit->m2.min; clock.m2--) {
  834. for (clock.p1 = limit->p1.max;
  835. clock.p1 >= limit->p1.min; clock.p1--) {
  836. int this_err;
  837. intel_clock(dev, refclk, &clock);
  838. if (!intel_PLL_is_valid(dev, limit,
  839. &clock))
  840. continue;
  841. this_err = abs(clock.dot - target);
  842. if (this_err < err_most) {
  843. *best_clock = clock;
  844. err_most = this_err;
  845. max_n = clock.n;
  846. found = true;
  847. }
  848. }
  849. }
  850. }
  851. }
  852. return found;
  853. }
  854. static bool
  855. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  856. int target, int refclk, intel_clock_t *best_clock)
  857. {
  858. struct drm_device *dev = crtc->dev;
  859. intel_clock_t clock;
  860. if (target < 200000) {
  861. clock.n = 1;
  862. clock.p1 = 2;
  863. clock.p2 = 10;
  864. clock.m1 = 12;
  865. clock.m2 = 9;
  866. } else {
  867. clock.n = 2;
  868. clock.p1 = 1;
  869. clock.p2 = 10;
  870. clock.m1 = 14;
  871. clock.m2 = 8;
  872. }
  873. intel_clock(dev, refclk, &clock);
  874. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  875. return true;
  876. }
  877. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  878. static bool
  879. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  880. int target, int refclk, intel_clock_t *best_clock)
  881. {
  882. intel_clock_t clock;
  883. if (target < 200000) {
  884. clock.p1 = 2;
  885. clock.p2 = 10;
  886. clock.n = 2;
  887. clock.m1 = 23;
  888. clock.m2 = 8;
  889. } else {
  890. clock.p1 = 1;
  891. clock.p2 = 10;
  892. clock.n = 1;
  893. clock.m1 = 14;
  894. clock.m2 = 2;
  895. }
  896. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  897. clock.p = (clock.p1 * clock.p2);
  898. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  899. clock.vco = 0;
  900. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  901. return true;
  902. }
  903. /**
  904. * intel_wait_for_vblank - wait for vblank on a given pipe
  905. * @dev: drm device
  906. * @pipe: pipe to wait for
  907. *
  908. * Wait for vblank to occur on a given pipe. Needed for various bits of
  909. * mode setting code.
  910. */
  911. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  912. {
  913. struct drm_i915_private *dev_priv = dev->dev_private;
  914. int pipestat_reg = (pipe == 0 ? PIPEASTAT : PIPEBSTAT);
  915. /* Clear existing vblank status. Note this will clear any other
  916. * sticky status fields as well.
  917. *
  918. * This races with i915_driver_irq_handler() with the result
  919. * that either function could miss a vblank event. Here it is not
  920. * fatal, as we will either wait upon the next vblank interrupt or
  921. * timeout. Generally speaking intel_wait_for_vblank() is only
  922. * called during modeset at which time the GPU should be idle and
  923. * should *not* be performing page flips and thus not waiting on
  924. * vblanks...
  925. * Currently, the result of us stealing a vblank from the irq
  926. * handler is that a single frame will be skipped during swapbuffers.
  927. */
  928. I915_WRITE(pipestat_reg,
  929. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  930. /* Wait for vblank interrupt bit to set */
  931. if (wait_for(I915_READ(pipestat_reg) &
  932. PIPE_VBLANK_INTERRUPT_STATUS,
  933. 50))
  934. DRM_DEBUG_KMS("vblank wait timed out\n");
  935. }
  936. /*
  937. * intel_wait_for_pipe_off - wait for pipe to turn off
  938. * @dev: drm device
  939. * @pipe: pipe to wait for
  940. *
  941. * After disabling a pipe, we can't wait for vblank in the usual way,
  942. * spinning on the vblank interrupt status bit, since we won't actually
  943. * see an interrupt when the pipe is disabled.
  944. *
  945. * On Gen4 and above:
  946. * wait for the pipe register state bit to turn off
  947. *
  948. * Otherwise:
  949. * wait for the display line value to settle (it usually
  950. * ends up stopping at the start of the next frame).
  951. *
  952. */
  953. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  954. {
  955. struct drm_i915_private *dev_priv = dev->dev_private;
  956. if (INTEL_INFO(dev)->gen >= 4) {
  957. int reg = PIPECONF(pipe);
  958. /* Wait for the Pipe State to go off */
  959. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  960. 100))
  961. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  962. } else {
  963. u32 last_line;
  964. int reg = PIPEDSL(pipe);
  965. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  966. /* Wait for the display line to settle */
  967. do {
  968. last_line = I915_READ(reg) & DSL_LINEMASK;
  969. mdelay(5);
  970. } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
  971. time_after(timeout, jiffies));
  972. if (time_after(jiffies, timeout))
  973. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  974. }
  975. }
  976. static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  977. {
  978. struct drm_device *dev = crtc->dev;
  979. struct drm_i915_private *dev_priv = dev->dev_private;
  980. struct drm_framebuffer *fb = crtc->fb;
  981. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  982. struct drm_i915_gem_object *obj = intel_fb->obj;
  983. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  984. int plane, i;
  985. u32 fbc_ctl, fbc_ctl2;
  986. if (fb->pitch == dev_priv->cfb_pitch &&
  987. obj->fence_reg == dev_priv->cfb_fence &&
  988. intel_crtc->plane == dev_priv->cfb_plane &&
  989. I915_READ(FBC_CONTROL) & FBC_CTL_EN)
  990. return;
  991. i8xx_disable_fbc(dev);
  992. dev_priv->cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  993. if (fb->pitch < dev_priv->cfb_pitch)
  994. dev_priv->cfb_pitch = fb->pitch;
  995. /* FBC_CTL wants 64B units */
  996. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  997. dev_priv->cfb_fence = obj->fence_reg;
  998. dev_priv->cfb_plane = intel_crtc->plane;
  999. plane = dev_priv->cfb_plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  1000. /* Clear old tags */
  1001. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  1002. I915_WRITE(FBC_TAG + (i * 4), 0);
  1003. /* Set it up... */
  1004. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | plane;
  1005. if (obj->tiling_mode != I915_TILING_NONE)
  1006. fbc_ctl2 |= FBC_CTL_CPU_FENCE;
  1007. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  1008. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  1009. /* enable it... */
  1010. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  1011. if (IS_I945GM(dev))
  1012. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  1013. fbc_ctl |= (dev_priv->cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  1014. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  1015. if (obj->tiling_mode != I915_TILING_NONE)
  1016. fbc_ctl |= dev_priv->cfb_fence;
  1017. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1018. DRM_DEBUG_KMS("enabled FBC, pitch %ld, yoff %d, plane %d, ",
  1019. dev_priv->cfb_pitch, crtc->y, dev_priv->cfb_plane);
  1020. }
  1021. void i8xx_disable_fbc(struct drm_device *dev)
  1022. {
  1023. struct drm_i915_private *dev_priv = dev->dev_private;
  1024. u32 fbc_ctl;
  1025. /* Disable compression */
  1026. fbc_ctl = I915_READ(FBC_CONTROL);
  1027. if ((fbc_ctl & FBC_CTL_EN) == 0)
  1028. return;
  1029. fbc_ctl &= ~FBC_CTL_EN;
  1030. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1031. /* Wait for compressing bit to clear */
  1032. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  1033. DRM_DEBUG_KMS("FBC idle timed out\n");
  1034. return;
  1035. }
  1036. DRM_DEBUG_KMS("disabled FBC\n");
  1037. }
  1038. static bool i8xx_fbc_enabled(struct drm_device *dev)
  1039. {
  1040. struct drm_i915_private *dev_priv = dev->dev_private;
  1041. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  1042. }
  1043. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1044. {
  1045. struct drm_device *dev = crtc->dev;
  1046. struct drm_i915_private *dev_priv = dev->dev_private;
  1047. struct drm_framebuffer *fb = crtc->fb;
  1048. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1049. struct drm_i915_gem_object *obj = intel_fb->obj;
  1050. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1051. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1052. unsigned long stall_watermark = 200;
  1053. u32 dpfc_ctl;
  1054. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1055. if (dpfc_ctl & DPFC_CTL_EN) {
  1056. if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
  1057. dev_priv->cfb_fence == obj->fence_reg &&
  1058. dev_priv->cfb_plane == intel_crtc->plane &&
  1059. dev_priv->cfb_y == crtc->y)
  1060. return;
  1061. I915_WRITE(DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
  1062. POSTING_READ(DPFC_CONTROL);
  1063. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1064. }
  1065. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  1066. dev_priv->cfb_fence = obj->fence_reg;
  1067. dev_priv->cfb_plane = intel_crtc->plane;
  1068. dev_priv->cfb_y = crtc->y;
  1069. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  1070. if (obj->tiling_mode != I915_TILING_NONE) {
  1071. dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;
  1072. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  1073. } else {
  1074. I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  1075. }
  1076. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1077. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1078. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1079. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  1080. /* enable it... */
  1081. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  1082. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1083. }
  1084. void g4x_disable_fbc(struct drm_device *dev)
  1085. {
  1086. struct drm_i915_private *dev_priv = dev->dev_private;
  1087. u32 dpfc_ctl;
  1088. /* Disable compression */
  1089. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1090. if (dpfc_ctl & DPFC_CTL_EN) {
  1091. dpfc_ctl &= ~DPFC_CTL_EN;
  1092. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  1093. DRM_DEBUG_KMS("disabled FBC\n");
  1094. }
  1095. }
  1096. static bool g4x_fbc_enabled(struct drm_device *dev)
  1097. {
  1098. struct drm_i915_private *dev_priv = dev->dev_private;
  1099. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  1100. }
  1101. static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1102. {
  1103. struct drm_device *dev = crtc->dev;
  1104. struct drm_i915_private *dev_priv = dev->dev_private;
  1105. struct drm_framebuffer *fb = crtc->fb;
  1106. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1107. struct drm_i915_gem_object *obj = intel_fb->obj;
  1108. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1109. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1110. unsigned long stall_watermark = 200;
  1111. u32 dpfc_ctl;
  1112. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1113. if (dpfc_ctl & DPFC_CTL_EN) {
  1114. if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
  1115. dev_priv->cfb_fence == obj->fence_reg &&
  1116. dev_priv->cfb_plane == intel_crtc->plane &&
  1117. dev_priv->cfb_offset == obj->gtt_offset &&
  1118. dev_priv->cfb_y == crtc->y)
  1119. return;
  1120. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
  1121. POSTING_READ(ILK_DPFC_CONTROL);
  1122. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1123. }
  1124. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  1125. dev_priv->cfb_fence = obj->fence_reg;
  1126. dev_priv->cfb_plane = intel_crtc->plane;
  1127. dev_priv->cfb_offset = obj->gtt_offset;
  1128. dev_priv->cfb_y = crtc->y;
  1129. dpfc_ctl &= DPFC_RESERVED;
  1130. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  1131. if (obj->tiling_mode != I915_TILING_NONE) {
  1132. dpfc_ctl |= (DPFC_CTL_FENCE_EN | dev_priv->cfb_fence);
  1133. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  1134. } else {
  1135. I915_WRITE(ILK_DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  1136. }
  1137. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1138. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1139. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1140. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  1141. I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
  1142. /* enable it... */
  1143. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  1144. if (IS_GEN6(dev)) {
  1145. I915_WRITE(SNB_DPFC_CTL_SA,
  1146. SNB_CPU_FENCE_ENABLE | dev_priv->cfb_fence);
  1147. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  1148. }
  1149. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1150. }
  1151. void ironlake_disable_fbc(struct drm_device *dev)
  1152. {
  1153. struct drm_i915_private *dev_priv = dev->dev_private;
  1154. u32 dpfc_ctl;
  1155. /* Disable compression */
  1156. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1157. if (dpfc_ctl & DPFC_CTL_EN) {
  1158. dpfc_ctl &= ~DPFC_CTL_EN;
  1159. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  1160. DRM_DEBUG_KMS("disabled FBC\n");
  1161. }
  1162. }
  1163. static bool ironlake_fbc_enabled(struct drm_device *dev)
  1164. {
  1165. struct drm_i915_private *dev_priv = dev->dev_private;
  1166. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  1167. }
  1168. bool intel_fbc_enabled(struct drm_device *dev)
  1169. {
  1170. struct drm_i915_private *dev_priv = dev->dev_private;
  1171. if (!dev_priv->display.fbc_enabled)
  1172. return false;
  1173. return dev_priv->display.fbc_enabled(dev);
  1174. }
  1175. void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1176. {
  1177. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  1178. if (!dev_priv->display.enable_fbc)
  1179. return;
  1180. dev_priv->display.enable_fbc(crtc, interval);
  1181. }
  1182. void intel_disable_fbc(struct drm_device *dev)
  1183. {
  1184. struct drm_i915_private *dev_priv = dev->dev_private;
  1185. if (!dev_priv->display.disable_fbc)
  1186. return;
  1187. dev_priv->display.disable_fbc(dev);
  1188. }
  1189. /**
  1190. * intel_update_fbc - enable/disable FBC as needed
  1191. * @dev: the drm_device
  1192. *
  1193. * Set up the framebuffer compression hardware at mode set time. We
  1194. * enable it if possible:
  1195. * - plane A only (on pre-965)
  1196. * - no pixel mulitply/line duplication
  1197. * - no alpha buffer discard
  1198. * - no dual wide
  1199. * - framebuffer <= 2048 in width, 1536 in height
  1200. *
  1201. * We can't assume that any compression will take place (worst case),
  1202. * so the compressed buffer has to be the same size as the uncompressed
  1203. * one. It also must reside (along with the line length buffer) in
  1204. * stolen memory.
  1205. *
  1206. * We need to enable/disable FBC on a global basis.
  1207. */
  1208. static void intel_update_fbc(struct drm_device *dev)
  1209. {
  1210. struct drm_i915_private *dev_priv = dev->dev_private;
  1211. struct drm_crtc *crtc = NULL, *tmp_crtc;
  1212. struct intel_crtc *intel_crtc;
  1213. struct drm_framebuffer *fb;
  1214. struct intel_framebuffer *intel_fb;
  1215. struct drm_i915_gem_object *obj;
  1216. DRM_DEBUG_KMS("\n");
  1217. if (!i915_powersave)
  1218. return;
  1219. if (!I915_HAS_FBC(dev))
  1220. return;
  1221. /*
  1222. * If FBC is already on, we just have to verify that we can
  1223. * keep it that way...
  1224. * Need to disable if:
  1225. * - more than one pipe is active
  1226. * - changing FBC params (stride, fence, mode)
  1227. * - new fb is too large to fit in compressed buffer
  1228. * - going to an unsupported config (interlace, pixel multiply, etc.)
  1229. */
  1230. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  1231. if (tmp_crtc->enabled) {
  1232. if (crtc) {
  1233. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  1234. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  1235. goto out_disable;
  1236. }
  1237. crtc = tmp_crtc;
  1238. }
  1239. }
  1240. if (!crtc || crtc->fb == NULL) {
  1241. DRM_DEBUG_KMS("no output, disabling\n");
  1242. dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
  1243. goto out_disable;
  1244. }
  1245. intel_crtc = to_intel_crtc(crtc);
  1246. fb = crtc->fb;
  1247. intel_fb = to_intel_framebuffer(fb);
  1248. obj = intel_fb->obj;
  1249. if (intel_fb->obj->base.size > dev_priv->cfb_size) {
  1250. DRM_DEBUG_KMS("framebuffer too large, disabling "
  1251. "compression\n");
  1252. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  1253. goto out_disable;
  1254. }
  1255. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  1256. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  1257. DRM_DEBUG_KMS("mode incompatible with compression, "
  1258. "disabling\n");
  1259. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  1260. goto out_disable;
  1261. }
  1262. if ((crtc->mode.hdisplay > 2048) ||
  1263. (crtc->mode.vdisplay > 1536)) {
  1264. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  1265. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  1266. goto out_disable;
  1267. }
  1268. if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
  1269. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  1270. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  1271. goto out_disable;
  1272. }
  1273. if (obj->tiling_mode != I915_TILING_X) {
  1274. DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
  1275. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  1276. goto out_disable;
  1277. }
  1278. /* If the kernel debugger is active, always disable compression */
  1279. if (in_dbg_master())
  1280. goto out_disable;
  1281. intel_enable_fbc(crtc, 500);
  1282. return;
  1283. out_disable:
  1284. /* Multiple disables should be harmless */
  1285. if (intel_fbc_enabled(dev)) {
  1286. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  1287. intel_disable_fbc(dev);
  1288. }
  1289. }
  1290. int
  1291. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1292. struct drm_i915_gem_object *obj,
  1293. struct intel_ring_buffer *pipelined)
  1294. {
  1295. u32 alignment;
  1296. int ret;
  1297. switch (obj->tiling_mode) {
  1298. case I915_TILING_NONE:
  1299. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1300. alignment = 128 * 1024;
  1301. else if (INTEL_INFO(dev)->gen >= 4)
  1302. alignment = 4 * 1024;
  1303. else
  1304. alignment = 64 * 1024;
  1305. break;
  1306. case I915_TILING_X:
  1307. /* pin() will align the object as required by fence */
  1308. alignment = 0;
  1309. break;
  1310. case I915_TILING_Y:
  1311. /* FIXME: Is this true? */
  1312. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1313. return -EINVAL;
  1314. default:
  1315. BUG();
  1316. }
  1317. ret = i915_gem_object_pin(obj, alignment, true);
  1318. if (ret)
  1319. return ret;
  1320. ret = i915_gem_object_set_to_display_plane(obj, pipelined);
  1321. if (ret)
  1322. goto err_unpin;
  1323. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1324. * fence, whereas 965+ only requires a fence if using
  1325. * framebuffer compression. For simplicity, we always install
  1326. * a fence as the cost is not that onerous.
  1327. */
  1328. if (obj->tiling_mode != I915_TILING_NONE) {
  1329. ret = i915_gem_object_get_fence(obj, pipelined, false);
  1330. if (ret)
  1331. goto err_unpin;
  1332. }
  1333. return 0;
  1334. err_unpin:
  1335. i915_gem_object_unpin(obj);
  1336. return ret;
  1337. }
  1338. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1339. static int
  1340. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1341. int x, int y, enum mode_set_atomic state)
  1342. {
  1343. struct drm_device *dev = crtc->dev;
  1344. struct drm_i915_private *dev_priv = dev->dev_private;
  1345. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1346. struct intel_framebuffer *intel_fb;
  1347. struct drm_i915_gem_object *obj;
  1348. int plane = intel_crtc->plane;
  1349. unsigned long Start, Offset;
  1350. u32 dspcntr;
  1351. u32 reg;
  1352. switch (plane) {
  1353. case 0:
  1354. case 1:
  1355. break;
  1356. default:
  1357. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1358. return -EINVAL;
  1359. }
  1360. intel_fb = to_intel_framebuffer(fb);
  1361. obj = intel_fb->obj;
  1362. reg = DSPCNTR(plane);
  1363. dspcntr = I915_READ(reg);
  1364. /* Mask out pixel format bits in case we change it */
  1365. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1366. switch (fb->bits_per_pixel) {
  1367. case 8:
  1368. dspcntr |= DISPPLANE_8BPP;
  1369. break;
  1370. case 16:
  1371. if (fb->depth == 15)
  1372. dspcntr |= DISPPLANE_15_16BPP;
  1373. else
  1374. dspcntr |= DISPPLANE_16BPP;
  1375. break;
  1376. case 24:
  1377. case 32:
  1378. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1379. break;
  1380. default:
  1381. DRM_ERROR("Unknown color depth\n");
  1382. return -EINVAL;
  1383. }
  1384. if (INTEL_INFO(dev)->gen >= 4) {
  1385. if (obj->tiling_mode != I915_TILING_NONE)
  1386. dspcntr |= DISPPLANE_TILED;
  1387. else
  1388. dspcntr &= ~DISPPLANE_TILED;
  1389. }
  1390. if (HAS_PCH_SPLIT(dev))
  1391. /* must disable */
  1392. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1393. I915_WRITE(reg, dspcntr);
  1394. Start = obj->gtt_offset;
  1395. Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
  1396. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1397. Start, Offset, x, y, fb->pitch);
  1398. I915_WRITE(DSPSTRIDE(plane), fb->pitch);
  1399. if (INTEL_INFO(dev)->gen >= 4) {
  1400. I915_WRITE(DSPSURF(plane), Start);
  1401. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1402. I915_WRITE(DSPADDR(plane), Offset);
  1403. } else
  1404. I915_WRITE(DSPADDR(plane), Start + Offset);
  1405. POSTING_READ(reg);
  1406. intel_update_fbc(dev);
  1407. intel_increase_pllclock(crtc);
  1408. return 0;
  1409. }
  1410. static int
  1411. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1412. struct drm_framebuffer *old_fb)
  1413. {
  1414. struct drm_device *dev = crtc->dev;
  1415. struct drm_i915_master_private *master_priv;
  1416. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1417. int ret;
  1418. /* no fb bound */
  1419. if (!crtc->fb) {
  1420. DRM_DEBUG_KMS("No FB bound\n");
  1421. return 0;
  1422. }
  1423. switch (intel_crtc->plane) {
  1424. case 0:
  1425. case 1:
  1426. break;
  1427. default:
  1428. return -EINVAL;
  1429. }
  1430. mutex_lock(&dev->struct_mutex);
  1431. ret = intel_pin_and_fence_fb_obj(dev,
  1432. to_intel_framebuffer(crtc->fb)->obj,
  1433. NULL);
  1434. if (ret != 0) {
  1435. mutex_unlock(&dev->struct_mutex);
  1436. return ret;
  1437. }
  1438. if (old_fb) {
  1439. struct drm_i915_private *dev_priv = dev->dev_private;
  1440. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1441. wait_event(dev_priv->pending_flip_queue,
  1442. atomic_read(&obj->pending_flip) == 0);
  1443. /* Big Hammer, we also need to ensure that any pending
  1444. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1445. * current scanout is retired before unpinning the old
  1446. * framebuffer.
  1447. */
  1448. ret = i915_gem_object_flush_gpu(obj, false);
  1449. if (ret) {
  1450. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  1451. mutex_unlock(&dev->struct_mutex);
  1452. return ret;
  1453. }
  1454. }
  1455. ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
  1456. LEAVE_ATOMIC_MODE_SET);
  1457. if (ret) {
  1458. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  1459. mutex_unlock(&dev->struct_mutex);
  1460. return ret;
  1461. }
  1462. if (old_fb) {
  1463. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1464. i915_gem_object_unpin(to_intel_framebuffer(old_fb)->obj);
  1465. }
  1466. mutex_unlock(&dev->struct_mutex);
  1467. if (!dev->primary->master)
  1468. return 0;
  1469. master_priv = dev->primary->master->driver_priv;
  1470. if (!master_priv->sarea_priv)
  1471. return 0;
  1472. if (intel_crtc->pipe) {
  1473. master_priv->sarea_priv->pipeB_x = x;
  1474. master_priv->sarea_priv->pipeB_y = y;
  1475. } else {
  1476. master_priv->sarea_priv->pipeA_x = x;
  1477. master_priv->sarea_priv->pipeA_y = y;
  1478. }
  1479. return 0;
  1480. }
  1481. static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
  1482. {
  1483. struct drm_device *dev = crtc->dev;
  1484. struct drm_i915_private *dev_priv = dev->dev_private;
  1485. u32 dpa_ctl;
  1486. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1487. dpa_ctl = I915_READ(DP_A);
  1488. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1489. if (clock < 200000) {
  1490. u32 temp;
  1491. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1492. /* workaround for 160Mhz:
  1493. 1) program 0x4600c bits 15:0 = 0x8124
  1494. 2) program 0x46010 bit 0 = 1
  1495. 3) program 0x46034 bit 24 = 1
  1496. 4) program 0x64000 bit 14 = 1
  1497. */
  1498. temp = I915_READ(0x4600c);
  1499. temp &= 0xffff0000;
  1500. I915_WRITE(0x4600c, temp | 0x8124);
  1501. temp = I915_READ(0x46010);
  1502. I915_WRITE(0x46010, temp | 1);
  1503. temp = I915_READ(0x46034);
  1504. I915_WRITE(0x46034, temp | (1 << 24));
  1505. } else {
  1506. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1507. }
  1508. I915_WRITE(DP_A, dpa_ctl);
  1509. POSTING_READ(DP_A);
  1510. udelay(500);
  1511. }
  1512. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  1513. {
  1514. struct drm_device *dev = crtc->dev;
  1515. struct drm_i915_private *dev_priv = dev->dev_private;
  1516. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1517. int pipe = intel_crtc->pipe;
  1518. u32 reg, temp;
  1519. /* enable normal train */
  1520. reg = FDI_TX_CTL(pipe);
  1521. temp = I915_READ(reg);
  1522. temp &= ~FDI_LINK_TRAIN_NONE;
  1523. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  1524. I915_WRITE(reg, temp);
  1525. reg = FDI_RX_CTL(pipe);
  1526. temp = I915_READ(reg);
  1527. if (HAS_PCH_CPT(dev)) {
  1528. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1529. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  1530. } else {
  1531. temp &= ~FDI_LINK_TRAIN_NONE;
  1532. temp |= FDI_LINK_TRAIN_NONE;
  1533. }
  1534. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  1535. /* wait one idle pattern time */
  1536. POSTING_READ(reg);
  1537. udelay(1000);
  1538. }
  1539. /* The FDI link training functions for ILK/Ibexpeak. */
  1540. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  1541. {
  1542. struct drm_device *dev = crtc->dev;
  1543. struct drm_i915_private *dev_priv = dev->dev_private;
  1544. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1545. int pipe = intel_crtc->pipe;
  1546. u32 reg, temp, tries;
  1547. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1548. for train result */
  1549. reg = FDI_RX_IMR(pipe);
  1550. temp = I915_READ(reg);
  1551. temp &= ~FDI_RX_SYMBOL_LOCK;
  1552. temp &= ~FDI_RX_BIT_LOCK;
  1553. I915_WRITE(reg, temp);
  1554. I915_READ(reg);
  1555. udelay(150);
  1556. /* enable CPU FDI TX and PCH FDI RX */
  1557. reg = FDI_TX_CTL(pipe);
  1558. temp = I915_READ(reg);
  1559. temp &= ~(7 << 19);
  1560. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1561. temp &= ~FDI_LINK_TRAIN_NONE;
  1562. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1563. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  1564. reg = FDI_RX_CTL(pipe);
  1565. temp = I915_READ(reg);
  1566. temp &= ~FDI_LINK_TRAIN_NONE;
  1567. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1568. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  1569. POSTING_READ(reg);
  1570. udelay(150);
  1571. /* Ironlake workaround, enable clock pointer after FDI enable*/
  1572. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_ENABLE);
  1573. reg = FDI_RX_IIR(pipe);
  1574. for (tries = 0; tries < 5; tries++) {
  1575. temp = I915_READ(reg);
  1576. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1577. if ((temp & FDI_RX_BIT_LOCK)) {
  1578. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1579. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  1580. break;
  1581. }
  1582. }
  1583. if (tries == 5)
  1584. DRM_ERROR("FDI train 1 fail!\n");
  1585. /* Train 2 */
  1586. reg = FDI_TX_CTL(pipe);
  1587. temp = I915_READ(reg);
  1588. temp &= ~FDI_LINK_TRAIN_NONE;
  1589. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1590. I915_WRITE(reg, temp);
  1591. reg = FDI_RX_CTL(pipe);
  1592. temp = I915_READ(reg);
  1593. temp &= ~FDI_LINK_TRAIN_NONE;
  1594. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1595. I915_WRITE(reg, temp);
  1596. POSTING_READ(reg);
  1597. udelay(150);
  1598. reg = FDI_RX_IIR(pipe);
  1599. for (tries = 0; tries < 5; tries++) {
  1600. temp = I915_READ(reg);
  1601. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1602. if (temp & FDI_RX_SYMBOL_LOCK) {
  1603. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  1604. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1605. break;
  1606. }
  1607. }
  1608. if (tries == 5)
  1609. DRM_ERROR("FDI train 2 fail!\n");
  1610. DRM_DEBUG_KMS("FDI train done\n");
  1611. }
  1612. static const int const snb_b_fdi_train_param [] = {
  1613. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  1614. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  1615. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  1616. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  1617. };
  1618. /* The FDI link training functions for SNB/Cougarpoint. */
  1619. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  1620. {
  1621. struct drm_device *dev = crtc->dev;
  1622. struct drm_i915_private *dev_priv = dev->dev_private;
  1623. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1624. int pipe = intel_crtc->pipe;
  1625. u32 reg, temp, i;
  1626. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1627. for train result */
  1628. reg = FDI_RX_IMR(pipe);
  1629. temp = I915_READ(reg);
  1630. temp &= ~FDI_RX_SYMBOL_LOCK;
  1631. temp &= ~FDI_RX_BIT_LOCK;
  1632. I915_WRITE(reg, temp);
  1633. POSTING_READ(reg);
  1634. udelay(150);
  1635. /* enable CPU FDI TX and PCH FDI RX */
  1636. reg = FDI_TX_CTL(pipe);
  1637. temp = I915_READ(reg);
  1638. temp &= ~(7 << 19);
  1639. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1640. temp &= ~FDI_LINK_TRAIN_NONE;
  1641. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1642. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1643. /* SNB-B */
  1644. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1645. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  1646. reg = FDI_RX_CTL(pipe);
  1647. temp = I915_READ(reg);
  1648. if (HAS_PCH_CPT(dev)) {
  1649. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1650. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1651. } else {
  1652. temp &= ~FDI_LINK_TRAIN_NONE;
  1653. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1654. }
  1655. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  1656. POSTING_READ(reg);
  1657. udelay(150);
  1658. for (i = 0; i < 4; i++ ) {
  1659. reg = FDI_TX_CTL(pipe);
  1660. temp = I915_READ(reg);
  1661. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1662. temp |= snb_b_fdi_train_param[i];
  1663. I915_WRITE(reg, temp);
  1664. POSTING_READ(reg);
  1665. udelay(500);
  1666. reg = FDI_RX_IIR(pipe);
  1667. temp = I915_READ(reg);
  1668. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1669. if (temp & FDI_RX_BIT_LOCK) {
  1670. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  1671. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1672. break;
  1673. }
  1674. }
  1675. if (i == 4)
  1676. DRM_ERROR("FDI train 1 fail!\n");
  1677. /* Train 2 */
  1678. reg = FDI_TX_CTL(pipe);
  1679. temp = I915_READ(reg);
  1680. temp &= ~FDI_LINK_TRAIN_NONE;
  1681. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1682. if (IS_GEN6(dev)) {
  1683. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1684. /* SNB-B */
  1685. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1686. }
  1687. I915_WRITE(reg, temp);
  1688. reg = FDI_RX_CTL(pipe);
  1689. temp = I915_READ(reg);
  1690. if (HAS_PCH_CPT(dev)) {
  1691. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1692. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  1693. } else {
  1694. temp &= ~FDI_LINK_TRAIN_NONE;
  1695. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1696. }
  1697. I915_WRITE(reg, temp);
  1698. POSTING_READ(reg);
  1699. udelay(150);
  1700. for (i = 0; i < 4; i++ ) {
  1701. reg = FDI_TX_CTL(pipe);
  1702. temp = I915_READ(reg);
  1703. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1704. temp |= snb_b_fdi_train_param[i];
  1705. I915_WRITE(reg, temp);
  1706. POSTING_READ(reg);
  1707. udelay(500);
  1708. reg = FDI_RX_IIR(pipe);
  1709. temp = I915_READ(reg);
  1710. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1711. if (temp & FDI_RX_SYMBOL_LOCK) {
  1712. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  1713. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1714. break;
  1715. }
  1716. }
  1717. if (i == 4)
  1718. DRM_ERROR("FDI train 2 fail!\n");
  1719. DRM_DEBUG_KMS("FDI train done.\n");
  1720. }
  1721. static void ironlake_fdi_enable(struct drm_crtc *crtc)
  1722. {
  1723. struct drm_device *dev = crtc->dev;
  1724. struct drm_i915_private *dev_priv = dev->dev_private;
  1725. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1726. int pipe = intel_crtc->pipe;
  1727. u32 reg, temp;
  1728. /* Write the TU size bits so error detection works */
  1729. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  1730. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  1731. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  1732. reg = FDI_RX_CTL(pipe);
  1733. temp = I915_READ(reg);
  1734. temp &= ~((0x7 << 19) | (0x7 << 16));
  1735. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1736. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  1737. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  1738. POSTING_READ(reg);
  1739. udelay(200);
  1740. /* Switch from Rawclk to PCDclk */
  1741. temp = I915_READ(reg);
  1742. I915_WRITE(reg, temp | FDI_PCDCLK);
  1743. POSTING_READ(reg);
  1744. udelay(200);
  1745. /* Enable CPU FDI TX PLL, always on for Ironlake */
  1746. reg = FDI_TX_CTL(pipe);
  1747. temp = I915_READ(reg);
  1748. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  1749. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  1750. POSTING_READ(reg);
  1751. udelay(100);
  1752. }
  1753. }
  1754. static void intel_flush_display_plane(struct drm_device *dev,
  1755. int plane)
  1756. {
  1757. struct drm_i915_private *dev_priv = dev->dev_private;
  1758. u32 reg = DSPADDR(plane);
  1759. I915_WRITE(reg, I915_READ(reg));
  1760. }
  1761. /*
  1762. * When we disable a pipe, we need to clear any pending scanline wait events
  1763. * to avoid hanging the ring, which we assume we are waiting on.
  1764. */
  1765. static void intel_clear_scanline_wait(struct drm_device *dev)
  1766. {
  1767. struct drm_i915_private *dev_priv = dev->dev_private;
  1768. struct intel_ring_buffer *ring;
  1769. u32 tmp;
  1770. if (IS_GEN2(dev))
  1771. /* Can't break the hang on i8xx */
  1772. return;
  1773. ring = LP_RING(dev_priv);
  1774. tmp = I915_READ_CTL(ring);
  1775. if (tmp & RING_WAIT)
  1776. I915_WRITE_CTL(ring, tmp);
  1777. }
  1778. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  1779. {
  1780. struct drm_i915_gem_object *obj;
  1781. struct drm_i915_private *dev_priv;
  1782. if (crtc->fb == NULL)
  1783. return;
  1784. obj = to_intel_framebuffer(crtc->fb)->obj;
  1785. dev_priv = crtc->dev->dev_private;
  1786. wait_event(dev_priv->pending_flip_queue,
  1787. atomic_read(&obj->pending_flip) == 0);
  1788. }
  1789. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  1790. {
  1791. struct drm_device *dev = crtc->dev;
  1792. struct drm_i915_private *dev_priv = dev->dev_private;
  1793. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1794. int pipe = intel_crtc->pipe;
  1795. int plane = intel_crtc->plane;
  1796. u32 reg, temp;
  1797. if (intel_crtc->active)
  1798. return;
  1799. intel_crtc->active = true;
  1800. intel_update_watermarks(dev);
  1801. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1802. temp = I915_READ(PCH_LVDS);
  1803. if ((temp & LVDS_PORT_EN) == 0)
  1804. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  1805. }
  1806. ironlake_fdi_enable(crtc);
  1807. /* Enable panel fitting for LVDS */
  1808. if (dev_priv->pch_pf_size &&
  1809. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
  1810. /* Force use of hard-coded filter coefficients
  1811. * as some pre-programmed values are broken,
  1812. * e.g. x201.
  1813. */
  1814. I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1,
  1815. PF_ENABLE | PF_FILTER_MED_3x3);
  1816. I915_WRITE(pipe ? PFB_WIN_POS : PFA_WIN_POS,
  1817. dev_priv->pch_pf_pos);
  1818. I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ,
  1819. dev_priv->pch_pf_size);
  1820. }
  1821. /* Enable CPU pipe */
  1822. reg = PIPECONF(pipe);
  1823. temp = I915_READ(reg);
  1824. if ((temp & PIPECONF_ENABLE) == 0) {
  1825. I915_WRITE(reg, temp | PIPECONF_ENABLE);
  1826. POSTING_READ(reg);
  1827. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1828. }
  1829. /* configure and enable CPU plane */
  1830. reg = DSPCNTR(plane);
  1831. temp = I915_READ(reg);
  1832. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  1833. I915_WRITE(reg, temp | DISPLAY_PLANE_ENABLE);
  1834. intel_flush_display_plane(dev, plane);
  1835. }
  1836. /* For PCH output, training FDI link */
  1837. if (IS_GEN6(dev))
  1838. gen6_fdi_link_train(crtc);
  1839. else
  1840. ironlake_fdi_link_train(crtc);
  1841. /* enable PCH DPLL */
  1842. reg = PCH_DPLL(pipe);
  1843. temp = I915_READ(reg);
  1844. if ((temp & DPLL_VCO_ENABLE) == 0) {
  1845. I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
  1846. POSTING_READ(reg);
  1847. udelay(200);
  1848. }
  1849. if (HAS_PCH_CPT(dev)) {
  1850. /* Be sure PCH DPLL SEL is set */
  1851. temp = I915_READ(PCH_DPLL_SEL);
  1852. if (pipe == 0 && (temp & TRANSA_DPLL_ENABLE) == 0)
  1853. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  1854. else if (pipe == 1 && (temp & TRANSB_DPLL_ENABLE) == 0)
  1855. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  1856. I915_WRITE(PCH_DPLL_SEL, temp);
  1857. }
  1858. /* set transcoder timing */
  1859. I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
  1860. I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
  1861. I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
  1862. I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
  1863. I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
  1864. I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
  1865. intel_fdi_normal_train(crtc);
  1866. /* For PCH DP, enable TRANS_DP_CTL */
  1867. if (HAS_PCH_CPT(dev) &&
  1868. intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  1869. reg = TRANS_DP_CTL(pipe);
  1870. temp = I915_READ(reg);
  1871. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  1872. TRANS_DP_SYNC_MASK |
  1873. TRANS_DP_BPC_MASK);
  1874. temp |= (TRANS_DP_OUTPUT_ENABLE |
  1875. TRANS_DP_ENH_FRAMING);
  1876. temp |= TRANS_DP_8BPC;
  1877. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  1878. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  1879. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  1880. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  1881. switch (intel_trans_dp_port_sel(crtc)) {
  1882. case PCH_DP_B:
  1883. temp |= TRANS_DP_PORT_SEL_B;
  1884. break;
  1885. case PCH_DP_C:
  1886. temp |= TRANS_DP_PORT_SEL_C;
  1887. break;
  1888. case PCH_DP_D:
  1889. temp |= TRANS_DP_PORT_SEL_D;
  1890. break;
  1891. default:
  1892. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  1893. temp |= TRANS_DP_PORT_SEL_B;
  1894. break;
  1895. }
  1896. I915_WRITE(reg, temp);
  1897. }
  1898. /* enable PCH transcoder */
  1899. reg = TRANSCONF(pipe);
  1900. temp = I915_READ(reg);
  1901. /*
  1902. * make the BPC in transcoder be consistent with
  1903. * that in pipeconf reg.
  1904. */
  1905. temp &= ~PIPE_BPC_MASK;
  1906. temp |= I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK;
  1907. I915_WRITE(reg, temp | TRANS_ENABLE);
  1908. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1909. DRM_ERROR("failed to enable transcoder %d\n", pipe);
  1910. intel_crtc_load_lut(crtc);
  1911. intel_update_fbc(dev);
  1912. intel_crtc_update_cursor(crtc, true);
  1913. }
  1914. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  1915. {
  1916. struct drm_device *dev = crtc->dev;
  1917. struct drm_i915_private *dev_priv = dev->dev_private;
  1918. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1919. int pipe = intel_crtc->pipe;
  1920. int plane = intel_crtc->plane;
  1921. u32 reg, temp;
  1922. if (!intel_crtc->active)
  1923. return;
  1924. intel_crtc_wait_for_pending_flips(crtc);
  1925. drm_vblank_off(dev, pipe);
  1926. intel_crtc_update_cursor(crtc, false);
  1927. /* Disable display plane */
  1928. reg = DSPCNTR(plane);
  1929. temp = I915_READ(reg);
  1930. if (temp & DISPLAY_PLANE_ENABLE) {
  1931. I915_WRITE(reg, temp & ~DISPLAY_PLANE_ENABLE);
  1932. intel_flush_display_plane(dev, plane);
  1933. }
  1934. if (dev_priv->cfb_plane == plane &&
  1935. dev_priv->display.disable_fbc)
  1936. dev_priv->display.disable_fbc(dev);
  1937. /* disable cpu pipe, disable after all planes disabled */
  1938. reg = PIPECONF(pipe);
  1939. temp = I915_READ(reg);
  1940. if (temp & PIPECONF_ENABLE) {
  1941. I915_WRITE(reg, temp & ~PIPECONF_ENABLE);
  1942. POSTING_READ(reg);
  1943. /* wait for cpu pipe off, pipe state */
  1944. intel_wait_for_pipe_off(dev, intel_crtc->pipe);
  1945. }
  1946. /* Disable PF */
  1947. I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1, 0);
  1948. I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ, 0);
  1949. /* disable CPU FDI tx and PCH FDI rx */
  1950. reg = FDI_TX_CTL(pipe);
  1951. temp = I915_READ(reg);
  1952. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  1953. POSTING_READ(reg);
  1954. reg = FDI_RX_CTL(pipe);
  1955. temp = I915_READ(reg);
  1956. temp &= ~(0x7 << 16);
  1957. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  1958. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  1959. POSTING_READ(reg);
  1960. udelay(100);
  1961. /* Ironlake workaround, disable clock pointer after downing FDI */
  1962. if (HAS_PCH_IBX(dev))
  1963. I915_WRITE(FDI_RX_CHICKEN(pipe),
  1964. I915_READ(FDI_RX_CHICKEN(pipe) &
  1965. ~FDI_RX_PHASE_SYNC_POINTER_ENABLE));
  1966. /* still set train pattern 1 */
  1967. reg = FDI_TX_CTL(pipe);
  1968. temp = I915_READ(reg);
  1969. temp &= ~FDI_LINK_TRAIN_NONE;
  1970. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1971. I915_WRITE(reg, temp);
  1972. reg = FDI_RX_CTL(pipe);
  1973. temp = I915_READ(reg);
  1974. if (HAS_PCH_CPT(dev)) {
  1975. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1976. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1977. } else {
  1978. temp &= ~FDI_LINK_TRAIN_NONE;
  1979. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1980. }
  1981. /* BPC in FDI rx is consistent with that in PIPECONF */
  1982. temp &= ~(0x07 << 16);
  1983. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  1984. I915_WRITE(reg, temp);
  1985. POSTING_READ(reg);
  1986. udelay(100);
  1987. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1988. temp = I915_READ(PCH_LVDS);
  1989. if (temp & LVDS_PORT_EN) {
  1990. I915_WRITE(PCH_LVDS, temp & ~LVDS_PORT_EN);
  1991. POSTING_READ(PCH_LVDS);
  1992. udelay(100);
  1993. }
  1994. }
  1995. /* disable PCH transcoder */
  1996. reg = TRANSCONF(plane);
  1997. temp = I915_READ(reg);
  1998. if (temp & TRANS_ENABLE) {
  1999. I915_WRITE(reg, temp & ~TRANS_ENABLE);
  2000. /* wait for PCH transcoder off, transcoder state */
  2001. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  2002. DRM_ERROR("failed to disable transcoder\n");
  2003. }
  2004. if (HAS_PCH_CPT(dev)) {
  2005. /* disable TRANS_DP_CTL */
  2006. reg = TRANS_DP_CTL(pipe);
  2007. temp = I915_READ(reg);
  2008. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  2009. I915_WRITE(reg, temp);
  2010. /* disable DPLL_SEL */
  2011. temp = I915_READ(PCH_DPLL_SEL);
  2012. if (pipe == 0)
  2013. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  2014. else
  2015. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2016. I915_WRITE(PCH_DPLL_SEL, temp);
  2017. }
  2018. /* disable PCH DPLL */
  2019. reg = PCH_DPLL(pipe);
  2020. temp = I915_READ(reg);
  2021. I915_WRITE(reg, temp & ~DPLL_VCO_ENABLE);
  2022. /* Switch from PCDclk to Rawclk */
  2023. reg = FDI_RX_CTL(pipe);
  2024. temp = I915_READ(reg);
  2025. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2026. /* Disable CPU FDI TX PLL */
  2027. reg = FDI_TX_CTL(pipe);
  2028. temp = I915_READ(reg);
  2029. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2030. POSTING_READ(reg);
  2031. udelay(100);
  2032. reg = FDI_RX_CTL(pipe);
  2033. temp = I915_READ(reg);
  2034. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2035. /* Wait for the clocks to turn off. */
  2036. POSTING_READ(reg);
  2037. udelay(100);
  2038. intel_crtc->active = false;
  2039. intel_update_watermarks(dev);
  2040. intel_update_fbc(dev);
  2041. intel_clear_scanline_wait(dev);
  2042. }
  2043. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  2044. {
  2045. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2046. int pipe = intel_crtc->pipe;
  2047. int plane = intel_crtc->plane;
  2048. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2049. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2050. */
  2051. switch (mode) {
  2052. case DRM_MODE_DPMS_ON:
  2053. case DRM_MODE_DPMS_STANDBY:
  2054. case DRM_MODE_DPMS_SUSPEND:
  2055. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  2056. ironlake_crtc_enable(crtc);
  2057. break;
  2058. case DRM_MODE_DPMS_OFF:
  2059. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  2060. ironlake_crtc_disable(crtc);
  2061. break;
  2062. }
  2063. }
  2064. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2065. {
  2066. if (!enable && intel_crtc->overlay) {
  2067. struct drm_device *dev = intel_crtc->base.dev;
  2068. mutex_lock(&dev->struct_mutex);
  2069. (void) intel_overlay_switch_off(intel_crtc->overlay, false);
  2070. mutex_unlock(&dev->struct_mutex);
  2071. }
  2072. /* Let userspace switch the overlay on again. In most cases userspace
  2073. * has to recompute where to put it anyway.
  2074. */
  2075. }
  2076. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  2077. {
  2078. struct drm_device *dev = crtc->dev;
  2079. struct drm_i915_private *dev_priv = dev->dev_private;
  2080. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2081. int pipe = intel_crtc->pipe;
  2082. int plane = intel_crtc->plane;
  2083. u32 reg, temp;
  2084. if (intel_crtc->active)
  2085. return;
  2086. intel_crtc->active = true;
  2087. intel_update_watermarks(dev);
  2088. /* Enable the DPLL */
  2089. reg = DPLL(pipe);
  2090. temp = I915_READ(reg);
  2091. if ((temp & DPLL_VCO_ENABLE) == 0) {
  2092. I915_WRITE(reg, temp);
  2093. /* Wait for the clocks to stabilize. */
  2094. POSTING_READ(reg);
  2095. udelay(150);
  2096. I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
  2097. /* Wait for the clocks to stabilize. */
  2098. POSTING_READ(reg);
  2099. udelay(150);
  2100. I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
  2101. /* Wait for the clocks to stabilize. */
  2102. POSTING_READ(reg);
  2103. udelay(150);
  2104. }
  2105. /* Enable the pipe */
  2106. reg = PIPECONF(pipe);
  2107. temp = I915_READ(reg);
  2108. if ((temp & PIPECONF_ENABLE) == 0)
  2109. I915_WRITE(reg, temp | PIPECONF_ENABLE);
  2110. /* Enable the plane */
  2111. reg = DSPCNTR(plane);
  2112. temp = I915_READ(reg);
  2113. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  2114. I915_WRITE(reg, temp | DISPLAY_PLANE_ENABLE);
  2115. intel_flush_display_plane(dev, plane);
  2116. }
  2117. intel_crtc_load_lut(crtc);
  2118. intel_update_fbc(dev);
  2119. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2120. intel_crtc_dpms_overlay(intel_crtc, true);
  2121. intel_crtc_update_cursor(crtc, true);
  2122. }
  2123. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  2124. {
  2125. struct drm_device *dev = crtc->dev;
  2126. struct drm_i915_private *dev_priv = dev->dev_private;
  2127. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2128. int pipe = intel_crtc->pipe;
  2129. int plane = intel_crtc->plane;
  2130. u32 reg, temp;
  2131. if (!intel_crtc->active)
  2132. return;
  2133. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2134. intel_crtc_wait_for_pending_flips(crtc);
  2135. drm_vblank_off(dev, pipe);
  2136. intel_crtc_dpms_overlay(intel_crtc, false);
  2137. intel_crtc_update_cursor(crtc, false);
  2138. if (dev_priv->cfb_plane == plane &&
  2139. dev_priv->display.disable_fbc)
  2140. dev_priv->display.disable_fbc(dev);
  2141. /* Disable display plane */
  2142. reg = DSPCNTR(plane);
  2143. temp = I915_READ(reg);
  2144. if (temp & DISPLAY_PLANE_ENABLE) {
  2145. I915_WRITE(reg, temp & ~DISPLAY_PLANE_ENABLE);
  2146. /* Flush the plane changes */
  2147. intel_flush_display_plane(dev, plane);
  2148. /* Wait for vblank for the disable to take effect */
  2149. if (IS_GEN2(dev))
  2150. intel_wait_for_vblank(dev, pipe);
  2151. }
  2152. /* Don't disable pipe A or pipe A PLLs if needed */
  2153. if (pipe == 0 && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  2154. goto done;
  2155. /* Next, disable display pipes */
  2156. reg = PIPECONF(pipe);
  2157. temp = I915_READ(reg);
  2158. if (temp & PIPECONF_ENABLE) {
  2159. I915_WRITE(reg, temp & ~PIPECONF_ENABLE);
  2160. /* Wait for the pipe to turn off */
  2161. POSTING_READ(reg);
  2162. intel_wait_for_pipe_off(dev, pipe);
  2163. }
  2164. reg = DPLL(pipe);
  2165. temp = I915_READ(reg);
  2166. if (temp & DPLL_VCO_ENABLE) {
  2167. I915_WRITE(reg, temp & ~DPLL_VCO_ENABLE);
  2168. /* Wait for the clocks to turn off. */
  2169. POSTING_READ(reg);
  2170. udelay(150);
  2171. }
  2172. done:
  2173. intel_crtc->active = false;
  2174. intel_update_fbc(dev);
  2175. intel_update_watermarks(dev);
  2176. intel_clear_scanline_wait(dev);
  2177. }
  2178. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2179. {
  2180. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2181. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2182. */
  2183. switch (mode) {
  2184. case DRM_MODE_DPMS_ON:
  2185. case DRM_MODE_DPMS_STANDBY:
  2186. case DRM_MODE_DPMS_SUSPEND:
  2187. i9xx_crtc_enable(crtc);
  2188. break;
  2189. case DRM_MODE_DPMS_OFF:
  2190. i9xx_crtc_disable(crtc);
  2191. break;
  2192. }
  2193. }
  2194. /**
  2195. * Sets the power management mode of the pipe and plane.
  2196. */
  2197. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2198. {
  2199. struct drm_device *dev = crtc->dev;
  2200. struct drm_i915_private *dev_priv = dev->dev_private;
  2201. struct drm_i915_master_private *master_priv;
  2202. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2203. int pipe = intel_crtc->pipe;
  2204. bool enabled;
  2205. if (intel_crtc->dpms_mode == mode)
  2206. return;
  2207. intel_crtc->dpms_mode = mode;
  2208. dev_priv->display.dpms(crtc, mode);
  2209. if (!dev->primary->master)
  2210. return;
  2211. master_priv = dev->primary->master->driver_priv;
  2212. if (!master_priv->sarea_priv)
  2213. return;
  2214. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2215. switch (pipe) {
  2216. case 0:
  2217. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2218. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2219. break;
  2220. case 1:
  2221. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2222. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2223. break;
  2224. default:
  2225. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  2226. break;
  2227. }
  2228. }
  2229. static void intel_crtc_disable(struct drm_crtc *crtc)
  2230. {
  2231. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2232. struct drm_device *dev = crtc->dev;
  2233. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  2234. if (crtc->fb) {
  2235. mutex_lock(&dev->struct_mutex);
  2236. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  2237. mutex_unlock(&dev->struct_mutex);
  2238. }
  2239. }
  2240. /* Prepare for a mode set.
  2241. *
  2242. * Note we could be a lot smarter here. We need to figure out which outputs
  2243. * will be enabled, which disabled (in short, how the config will changes)
  2244. * and perform the minimum necessary steps to accomplish that, e.g. updating
  2245. * watermarks, FBC configuration, making sure PLLs are programmed correctly,
  2246. * panel fitting is in the proper state, etc.
  2247. */
  2248. static void i9xx_crtc_prepare(struct drm_crtc *crtc)
  2249. {
  2250. i9xx_crtc_disable(crtc);
  2251. }
  2252. static void i9xx_crtc_commit(struct drm_crtc *crtc)
  2253. {
  2254. i9xx_crtc_enable(crtc);
  2255. }
  2256. static void ironlake_crtc_prepare(struct drm_crtc *crtc)
  2257. {
  2258. ironlake_crtc_disable(crtc);
  2259. }
  2260. static void ironlake_crtc_commit(struct drm_crtc *crtc)
  2261. {
  2262. ironlake_crtc_enable(crtc);
  2263. }
  2264. void intel_encoder_prepare (struct drm_encoder *encoder)
  2265. {
  2266. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2267. /* lvds has its own version of prepare see intel_lvds_prepare */
  2268. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2269. }
  2270. void intel_encoder_commit (struct drm_encoder *encoder)
  2271. {
  2272. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2273. /* lvds has its own version of commit see intel_lvds_commit */
  2274. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2275. }
  2276. void intel_encoder_destroy(struct drm_encoder *encoder)
  2277. {
  2278. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2279. drm_encoder_cleanup(encoder);
  2280. kfree(intel_encoder);
  2281. }
  2282. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2283. struct drm_display_mode *mode,
  2284. struct drm_display_mode *adjusted_mode)
  2285. {
  2286. struct drm_device *dev = crtc->dev;
  2287. if (HAS_PCH_SPLIT(dev)) {
  2288. /* FDI link clock is fixed at 2.7G */
  2289. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  2290. return false;
  2291. }
  2292. /* XXX some encoders set the crtcinfo, others don't.
  2293. * Obviously we need some form of conflict resolution here...
  2294. */
  2295. if (adjusted_mode->crtc_htotal == 0)
  2296. drm_mode_set_crtcinfo(adjusted_mode, 0);
  2297. return true;
  2298. }
  2299. static int i945_get_display_clock_speed(struct drm_device *dev)
  2300. {
  2301. return 400000;
  2302. }
  2303. static int i915_get_display_clock_speed(struct drm_device *dev)
  2304. {
  2305. return 333000;
  2306. }
  2307. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2308. {
  2309. return 200000;
  2310. }
  2311. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2312. {
  2313. u16 gcfgc = 0;
  2314. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2315. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2316. return 133000;
  2317. else {
  2318. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2319. case GC_DISPLAY_CLOCK_333_MHZ:
  2320. return 333000;
  2321. default:
  2322. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2323. return 190000;
  2324. }
  2325. }
  2326. }
  2327. static int i865_get_display_clock_speed(struct drm_device *dev)
  2328. {
  2329. return 266000;
  2330. }
  2331. static int i855_get_display_clock_speed(struct drm_device *dev)
  2332. {
  2333. u16 hpllcc = 0;
  2334. /* Assume that the hardware is in the high speed state. This
  2335. * should be the default.
  2336. */
  2337. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2338. case GC_CLOCK_133_200:
  2339. case GC_CLOCK_100_200:
  2340. return 200000;
  2341. case GC_CLOCK_166_250:
  2342. return 250000;
  2343. case GC_CLOCK_100_133:
  2344. return 133000;
  2345. }
  2346. /* Shouldn't happen */
  2347. return 0;
  2348. }
  2349. static int i830_get_display_clock_speed(struct drm_device *dev)
  2350. {
  2351. return 133000;
  2352. }
  2353. struct fdi_m_n {
  2354. u32 tu;
  2355. u32 gmch_m;
  2356. u32 gmch_n;
  2357. u32 link_m;
  2358. u32 link_n;
  2359. };
  2360. static void
  2361. fdi_reduce_ratio(u32 *num, u32 *den)
  2362. {
  2363. while (*num > 0xffffff || *den > 0xffffff) {
  2364. *num >>= 1;
  2365. *den >>= 1;
  2366. }
  2367. }
  2368. static void
  2369. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  2370. int link_clock, struct fdi_m_n *m_n)
  2371. {
  2372. m_n->tu = 64; /* default size */
  2373. /* BUG_ON(pixel_clock > INT_MAX / 36); */
  2374. m_n->gmch_m = bits_per_pixel * pixel_clock;
  2375. m_n->gmch_n = link_clock * nlanes * 8;
  2376. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  2377. m_n->link_m = pixel_clock;
  2378. m_n->link_n = link_clock;
  2379. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  2380. }
  2381. struct intel_watermark_params {
  2382. unsigned long fifo_size;
  2383. unsigned long max_wm;
  2384. unsigned long default_wm;
  2385. unsigned long guard_size;
  2386. unsigned long cacheline_size;
  2387. };
  2388. /* Pineview has different values for various configs */
  2389. static struct intel_watermark_params pineview_display_wm = {
  2390. PINEVIEW_DISPLAY_FIFO,
  2391. PINEVIEW_MAX_WM,
  2392. PINEVIEW_DFT_WM,
  2393. PINEVIEW_GUARD_WM,
  2394. PINEVIEW_FIFO_LINE_SIZE
  2395. };
  2396. static struct intel_watermark_params pineview_display_hplloff_wm = {
  2397. PINEVIEW_DISPLAY_FIFO,
  2398. PINEVIEW_MAX_WM,
  2399. PINEVIEW_DFT_HPLLOFF_WM,
  2400. PINEVIEW_GUARD_WM,
  2401. PINEVIEW_FIFO_LINE_SIZE
  2402. };
  2403. static struct intel_watermark_params pineview_cursor_wm = {
  2404. PINEVIEW_CURSOR_FIFO,
  2405. PINEVIEW_CURSOR_MAX_WM,
  2406. PINEVIEW_CURSOR_DFT_WM,
  2407. PINEVIEW_CURSOR_GUARD_WM,
  2408. PINEVIEW_FIFO_LINE_SIZE,
  2409. };
  2410. static struct intel_watermark_params pineview_cursor_hplloff_wm = {
  2411. PINEVIEW_CURSOR_FIFO,
  2412. PINEVIEW_CURSOR_MAX_WM,
  2413. PINEVIEW_CURSOR_DFT_WM,
  2414. PINEVIEW_CURSOR_GUARD_WM,
  2415. PINEVIEW_FIFO_LINE_SIZE
  2416. };
  2417. static struct intel_watermark_params g4x_wm_info = {
  2418. G4X_FIFO_SIZE,
  2419. G4X_MAX_WM,
  2420. G4X_MAX_WM,
  2421. 2,
  2422. G4X_FIFO_LINE_SIZE,
  2423. };
  2424. static struct intel_watermark_params g4x_cursor_wm_info = {
  2425. I965_CURSOR_FIFO,
  2426. I965_CURSOR_MAX_WM,
  2427. I965_CURSOR_DFT_WM,
  2428. 2,
  2429. G4X_FIFO_LINE_SIZE,
  2430. };
  2431. static struct intel_watermark_params i965_cursor_wm_info = {
  2432. I965_CURSOR_FIFO,
  2433. I965_CURSOR_MAX_WM,
  2434. I965_CURSOR_DFT_WM,
  2435. 2,
  2436. I915_FIFO_LINE_SIZE,
  2437. };
  2438. static struct intel_watermark_params i945_wm_info = {
  2439. I945_FIFO_SIZE,
  2440. I915_MAX_WM,
  2441. 1,
  2442. 2,
  2443. I915_FIFO_LINE_SIZE
  2444. };
  2445. static struct intel_watermark_params i915_wm_info = {
  2446. I915_FIFO_SIZE,
  2447. I915_MAX_WM,
  2448. 1,
  2449. 2,
  2450. I915_FIFO_LINE_SIZE
  2451. };
  2452. static struct intel_watermark_params i855_wm_info = {
  2453. I855GM_FIFO_SIZE,
  2454. I915_MAX_WM,
  2455. 1,
  2456. 2,
  2457. I830_FIFO_LINE_SIZE
  2458. };
  2459. static struct intel_watermark_params i830_wm_info = {
  2460. I830_FIFO_SIZE,
  2461. I915_MAX_WM,
  2462. 1,
  2463. 2,
  2464. I830_FIFO_LINE_SIZE
  2465. };
  2466. static struct intel_watermark_params ironlake_display_wm_info = {
  2467. ILK_DISPLAY_FIFO,
  2468. ILK_DISPLAY_MAXWM,
  2469. ILK_DISPLAY_DFTWM,
  2470. 2,
  2471. ILK_FIFO_LINE_SIZE
  2472. };
  2473. static struct intel_watermark_params ironlake_cursor_wm_info = {
  2474. ILK_CURSOR_FIFO,
  2475. ILK_CURSOR_MAXWM,
  2476. ILK_CURSOR_DFTWM,
  2477. 2,
  2478. ILK_FIFO_LINE_SIZE
  2479. };
  2480. static struct intel_watermark_params ironlake_display_srwm_info = {
  2481. ILK_DISPLAY_SR_FIFO,
  2482. ILK_DISPLAY_MAX_SRWM,
  2483. ILK_DISPLAY_DFT_SRWM,
  2484. 2,
  2485. ILK_FIFO_LINE_SIZE
  2486. };
  2487. static struct intel_watermark_params ironlake_cursor_srwm_info = {
  2488. ILK_CURSOR_SR_FIFO,
  2489. ILK_CURSOR_MAX_SRWM,
  2490. ILK_CURSOR_DFT_SRWM,
  2491. 2,
  2492. ILK_FIFO_LINE_SIZE
  2493. };
  2494. static struct intel_watermark_params sandybridge_display_wm_info = {
  2495. SNB_DISPLAY_FIFO,
  2496. SNB_DISPLAY_MAXWM,
  2497. SNB_DISPLAY_DFTWM,
  2498. 2,
  2499. SNB_FIFO_LINE_SIZE
  2500. };
  2501. static struct intel_watermark_params sandybridge_cursor_wm_info = {
  2502. SNB_CURSOR_FIFO,
  2503. SNB_CURSOR_MAXWM,
  2504. SNB_CURSOR_DFTWM,
  2505. 2,
  2506. SNB_FIFO_LINE_SIZE
  2507. };
  2508. static struct intel_watermark_params sandybridge_display_srwm_info = {
  2509. SNB_DISPLAY_SR_FIFO,
  2510. SNB_DISPLAY_MAX_SRWM,
  2511. SNB_DISPLAY_DFT_SRWM,
  2512. 2,
  2513. SNB_FIFO_LINE_SIZE
  2514. };
  2515. static struct intel_watermark_params sandybridge_cursor_srwm_info = {
  2516. SNB_CURSOR_SR_FIFO,
  2517. SNB_CURSOR_MAX_SRWM,
  2518. SNB_CURSOR_DFT_SRWM,
  2519. 2,
  2520. SNB_FIFO_LINE_SIZE
  2521. };
  2522. /**
  2523. * intel_calculate_wm - calculate watermark level
  2524. * @clock_in_khz: pixel clock
  2525. * @wm: chip FIFO params
  2526. * @pixel_size: display pixel size
  2527. * @latency_ns: memory latency for the platform
  2528. *
  2529. * Calculate the watermark level (the level at which the display plane will
  2530. * start fetching from memory again). Each chip has a different display
  2531. * FIFO size and allocation, so the caller needs to figure that out and pass
  2532. * in the correct intel_watermark_params structure.
  2533. *
  2534. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  2535. * on the pixel size. When it reaches the watermark level, it'll start
  2536. * fetching FIFO line sized based chunks from memory until the FIFO fills
  2537. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  2538. * will occur, and a display engine hang could result.
  2539. */
  2540. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  2541. struct intel_watermark_params *wm,
  2542. int pixel_size,
  2543. unsigned long latency_ns)
  2544. {
  2545. long entries_required, wm_size;
  2546. /*
  2547. * Note: we need to make sure we don't overflow for various clock &
  2548. * latency values.
  2549. * clocks go from a few thousand to several hundred thousand.
  2550. * latency is usually a few thousand
  2551. */
  2552. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  2553. 1000;
  2554. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  2555. DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries_required);
  2556. wm_size = wm->fifo_size - (entries_required + wm->guard_size);
  2557. DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
  2558. /* Don't promote wm_size to unsigned... */
  2559. if (wm_size > (long)wm->max_wm)
  2560. wm_size = wm->max_wm;
  2561. if (wm_size <= 0)
  2562. wm_size = wm->default_wm;
  2563. return wm_size;
  2564. }
  2565. struct cxsr_latency {
  2566. int is_desktop;
  2567. int is_ddr3;
  2568. unsigned long fsb_freq;
  2569. unsigned long mem_freq;
  2570. unsigned long display_sr;
  2571. unsigned long display_hpll_disable;
  2572. unsigned long cursor_sr;
  2573. unsigned long cursor_hpll_disable;
  2574. };
  2575. static const struct cxsr_latency cxsr_latency_table[] = {
  2576. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  2577. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  2578. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  2579. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  2580. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  2581. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  2582. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  2583. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  2584. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  2585. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  2586. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  2587. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  2588. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  2589. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  2590. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  2591. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  2592. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  2593. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  2594. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  2595. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  2596. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  2597. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  2598. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  2599. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  2600. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  2601. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  2602. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  2603. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  2604. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  2605. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  2606. };
  2607. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  2608. int is_ddr3,
  2609. int fsb,
  2610. int mem)
  2611. {
  2612. const struct cxsr_latency *latency;
  2613. int i;
  2614. if (fsb == 0 || mem == 0)
  2615. return NULL;
  2616. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  2617. latency = &cxsr_latency_table[i];
  2618. if (is_desktop == latency->is_desktop &&
  2619. is_ddr3 == latency->is_ddr3 &&
  2620. fsb == latency->fsb_freq && mem == latency->mem_freq)
  2621. return latency;
  2622. }
  2623. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2624. return NULL;
  2625. }
  2626. static void pineview_disable_cxsr(struct drm_device *dev)
  2627. {
  2628. struct drm_i915_private *dev_priv = dev->dev_private;
  2629. /* deactivate cxsr */
  2630. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  2631. }
  2632. /*
  2633. * Latency for FIFO fetches is dependent on several factors:
  2634. * - memory configuration (speed, channels)
  2635. * - chipset
  2636. * - current MCH state
  2637. * It can be fairly high in some situations, so here we assume a fairly
  2638. * pessimal value. It's a tradeoff between extra memory fetches (if we
  2639. * set this value too high, the FIFO will fetch frequently to stay full)
  2640. * and power consumption (set it too low to save power and we might see
  2641. * FIFO underruns and display "flicker").
  2642. *
  2643. * A value of 5us seems to be a good balance; safe for very low end
  2644. * platforms but not overly aggressive on lower latency configs.
  2645. */
  2646. static const int latency_ns = 5000;
  2647. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  2648. {
  2649. struct drm_i915_private *dev_priv = dev->dev_private;
  2650. uint32_t dsparb = I915_READ(DSPARB);
  2651. int size;
  2652. size = dsparb & 0x7f;
  2653. if (plane)
  2654. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  2655. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2656. plane ? "B" : "A", size);
  2657. return size;
  2658. }
  2659. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  2660. {
  2661. struct drm_i915_private *dev_priv = dev->dev_private;
  2662. uint32_t dsparb = I915_READ(DSPARB);
  2663. int size;
  2664. size = dsparb & 0x1ff;
  2665. if (plane)
  2666. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  2667. size >>= 1; /* Convert to cachelines */
  2668. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2669. plane ? "B" : "A", size);
  2670. return size;
  2671. }
  2672. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  2673. {
  2674. struct drm_i915_private *dev_priv = dev->dev_private;
  2675. uint32_t dsparb = I915_READ(DSPARB);
  2676. int size;
  2677. size = dsparb & 0x7f;
  2678. size >>= 2; /* Convert to cachelines */
  2679. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2680. plane ? "B" : "A",
  2681. size);
  2682. return size;
  2683. }
  2684. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  2685. {
  2686. struct drm_i915_private *dev_priv = dev->dev_private;
  2687. uint32_t dsparb = I915_READ(DSPARB);
  2688. int size;
  2689. size = dsparb & 0x7f;
  2690. size >>= 1; /* Convert to cachelines */
  2691. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2692. plane ? "B" : "A", size);
  2693. return size;
  2694. }
  2695. static void pineview_update_wm(struct drm_device *dev, int planea_clock,
  2696. int planeb_clock, int sr_hdisplay, int unused,
  2697. int pixel_size)
  2698. {
  2699. struct drm_i915_private *dev_priv = dev->dev_private;
  2700. const struct cxsr_latency *latency;
  2701. u32 reg;
  2702. unsigned long wm;
  2703. int sr_clock;
  2704. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  2705. dev_priv->fsb_freq, dev_priv->mem_freq);
  2706. if (!latency) {
  2707. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2708. pineview_disable_cxsr(dev);
  2709. return;
  2710. }
  2711. if (!planea_clock || !planeb_clock) {
  2712. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2713. /* Display SR */
  2714. wm = intel_calculate_wm(sr_clock, &pineview_display_wm,
  2715. pixel_size, latency->display_sr);
  2716. reg = I915_READ(DSPFW1);
  2717. reg &= ~DSPFW_SR_MASK;
  2718. reg |= wm << DSPFW_SR_SHIFT;
  2719. I915_WRITE(DSPFW1, reg);
  2720. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  2721. /* cursor SR */
  2722. wm = intel_calculate_wm(sr_clock, &pineview_cursor_wm,
  2723. pixel_size, latency->cursor_sr);
  2724. reg = I915_READ(DSPFW3);
  2725. reg &= ~DSPFW_CURSOR_SR_MASK;
  2726. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  2727. I915_WRITE(DSPFW3, reg);
  2728. /* Display HPLL off SR */
  2729. wm = intel_calculate_wm(sr_clock, &pineview_display_hplloff_wm,
  2730. pixel_size, latency->display_hpll_disable);
  2731. reg = I915_READ(DSPFW3);
  2732. reg &= ~DSPFW_HPLL_SR_MASK;
  2733. reg |= wm & DSPFW_HPLL_SR_MASK;
  2734. I915_WRITE(DSPFW3, reg);
  2735. /* cursor HPLL off SR */
  2736. wm = intel_calculate_wm(sr_clock, &pineview_cursor_hplloff_wm,
  2737. pixel_size, latency->cursor_hpll_disable);
  2738. reg = I915_READ(DSPFW3);
  2739. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  2740. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  2741. I915_WRITE(DSPFW3, reg);
  2742. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  2743. /* activate cxsr */
  2744. I915_WRITE(DSPFW3,
  2745. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  2746. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  2747. } else {
  2748. pineview_disable_cxsr(dev);
  2749. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  2750. }
  2751. }
  2752. static void g4x_update_wm(struct drm_device *dev, int planea_clock,
  2753. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2754. int pixel_size)
  2755. {
  2756. struct drm_i915_private *dev_priv = dev->dev_private;
  2757. int total_size, cacheline_size;
  2758. int planea_wm, planeb_wm, cursora_wm, cursorb_wm, cursor_sr;
  2759. struct intel_watermark_params planea_params, planeb_params;
  2760. unsigned long line_time_us;
  2761. int sr_clock, sr_entries = 0, entries_required;
  2762. /* Create copies of the base settings for each pipe */
  2763. planea_params = planeb_params = g4x_wm_info;
  2764. /* Grab a couple of global values before we overwrite them */
  2765. total_size = planea_params.fifo_size;
  2766. cacheline_size = planea_params.cacheline_size;
  2767. /*
  2768. * Note: we need to make sure we don't overflow for various clock &
  2769. * latency values.
  2770. * clocks go from a few thousand to several hundred thousand.
  2771. * latency is usually a few thousand
  2772. */
  2773. entries_required = ((planea_clock / 1000) * pixel_size * latency_ns) /
  2774. 1000;
  2775. entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
  2776. planea_wm = entries_required + planea_params.guard_size;
  2777. entries_required = ((planeb_clock / 1000) * pixel_size * latency_ns) /
  2778. 1000;
  2779. entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
  2780. planeb_wm = entries_required + planeb_params.guard_size;
  2781. cursora_wm = cursorb_wm = 16;
  2782. cursor_sr = 32;
  2783. DRM_DEBUG("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  2784. /* Calc sr entries for one plane configs */
  2785. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  2786. /* self-refresh has much higher latency */
  2787. static const int sr_latency_ns = 12000;
  2788. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2789. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2790. /* Use ns/us then divide to preserve precision */
  2791. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2792. pixel_size * sr_hdisplay;
  2793. sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
  2794. entries_required = (((sr_latency_ns / line_time_us) +
  2795. 1000) / 1000) * pixel_size * 64;
  2796. entries_required = DIV_ROUND_UP(entries_required,
  2797. g4x_cursor_wm_info.cacheline_size);
  2798. cursor_sr = entries_required + g4x_cursor_wm_info.guard_size;
  2799. if (cursor_sr > g4x_cursor_wm_info.max_wm)
  2800. cursor_sr = g4x_cursor_wm_info.max_wm;
  2801. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  2802. "cursor %d\n", sr_entries, cursor_sr);
  2803. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  2804. } else {
  2805. /* Turn off self refresh if both pipes are enabled */
  2806. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2807. & ~FW_BLC_SELF_EN);
  2808. }
  2809. DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, SR %d\n",
  2810. planea_wm, planeb_wm, sr_entries);
  2811. planea_wm &= 0x3f;
  2812. planeb_wm &= 0x3f;
  2813. I915_WRITE(DSPFW1, (sr_entries << DSPFW_SR_SHIFT) |
  2814. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  2815. (planeb_wm << DSPFW_PLANEB_SHIFT) | planea_wm);
  2816. I915_WRITE(DSPFW2, (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  2817. (cursora_wm << DSPFW_CURSORA_SHIFT));
  2818. /* HPLL off in SR has some issues on G4x... disable it */
  2819. I915_WRITE(DSPFW3, (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  2820. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  2821. }
  2822. static void i965_update_wm(struct drm_device *dev, int planea_clock,
  2823. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2824. int pixel_size)
  2825. {
  2826. struct drm_i915_private *dev_priv = dev->dev_private;
  2827. unsigned long line_time_us;
  2828. int sr_clock, sr_entries, srwm = 1;
  2829. int cursor_sr = 16;
  2830. /* Calc sr entries for one plane configs */
  2831. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  2832. /* self-refresh has much higher latency */
  2833. static const int sr_latency_ns = 12000;
  2834. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2835. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2836. /* Use ns/us then divide to preserve precision */
  2837. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2838. pixel_size * sr_hdisplay;
  2839. sr_entries = DIV_ROUND_UP(sr_entries, I915_FIFO_LINE_SIZE);
  2840. DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
  2841. srwm = I965_FIFO_SIZE - sr_entries;
  2842. if (srwm < 0)
  2843. srwm = 1;
  2844. srwm &= 0x1ff;
  2845. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2846. pixel_size * 64;
  2847. sr_entries = DIV_ROUND_UP(sr_entries,
  2848. i965_cursor_wm_info.cacheline_size);
  2849. cursor_sr = i965_cursor_wm_info.fifo_size -
  2850. (sr_entries + i965_cursor_wm_info.guard_size);
  2851. if (cursor_sr > i965_cursor_wm_info.max_wm)
  2852. cursor_sr = i965_cursor_wm_info.max_wm;
  2853. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  2854. "cursor %d\n", srwm, cursor_sr);
  2855. if (IS_CRESTLINE(dev))
  2856. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  2857. } else {
  2858. /* Turn off self refresh if both pipes are enabled */
  2859. if (IS_CRESTLINE(dev))
  2860. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2861. & ~FW_BLC_SELF_EN);
  2862. }
  2863. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  2864. srwm);
  2865. /* 965 has limitations... */
  2866. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) | (8 << 16) | (8 << 8) |
  2867. (8 << 0));
  2868. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  2869. /* update cursor SR watermark */
  2870. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  2871. }
  2872. static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
  2873. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2874. int pixel_size)
  2875. {
  2876. struct drm_i915_private *dev_priv = dev->dev_private;
  2877. uint32_t fwater_lo;
  2878. uint32_t fwater_hi;
  2879. int total_size, cacheline_size, cwm, srwm = 1;
  2880. int planea_wm, planeb_wm;
  2881. struct intel_watermark_params planea_params, planeb_params;
  2882. unsigned long line_time_us;
  2883. int sr_clock, sr_entries = 0;
  2884. /* Create copies of the base settings for each pipe */
  2885. if (IS_CRESTLINE(dev) || IS_I945GM(dev))
  2886. planea_params = planeb_params = i945_wm_info;
  2887. else if (!IS_GEN2(dev))
  2888. planea_params = planeb_params = i915_wm_info;
  2889. else
  2890. planea_params = planeb_params = i855_wm_info;
  2891. /* Grab a couple of global values before we overwrite them */
  2892. total_size = planea_params.fifo_size;
  2893. cacheline_size = planea_params.cacheline_size;
  2894. /* Update per-plane FIFO sizes */
  2895. planea_params.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  2896. planeb_params.fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  2897. planea_wm = intel_calculate_wm(planea_clock, &planea_params,
  2898. pixel_size, latency_ns);
  2899. planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
  2900. pixel_size, latency_ns);
  2901. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  2902. /*
  2903. * Overlay gets an aggressive default since video jitter is bad.
  2904. */
  2905. cwm = 2;
  2906. /* Calc sr entries for one plane configs */
  2907. if (HAS_FW_BLC(dev) && sr_hdisplay &&
  2908. (!planea_clock || !planeb_clock)) {
  2909. /* self-refresh has much higher latency */
  2910. static const int sr_latency_ns = 6000;
  2911. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2912. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2913. /* Use ns/us then divide to preserve precision */
  2914. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2915. pixel_size * sr_hdisplay;
  2916. sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
  2917. DRM_DEBUG_KMS("self-refresh entries: %d\n", sr_entries);
  2918. srwm = total_size - sr_entries;
  2919. if (srwm < 0)
  2920. srwm = 1;
  2921. if (IS_I945G(dev) || IS_I945GM(dev))
  2922. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  2923. else if (IS_I915GM(dev)) {
  2924. /* 915M has a smaller SRWM field */
  2925. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  2926. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  2927. }
  2928. } else {
  2929. /* Turn off self refresh if both pipes are enabled */
  2930. if (IS_I945G(dev) || IS_I945GM(dev)) {
  2931. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2932. & ~FW_BLC_SELF_EN);
  2933. } else if (IS_I915GM(dev)) {
  2934. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  2935. }
  2936. }
  2937. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  2938. planea_wm, planeb_wm, cwm, srwm);
  2939. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  2940. fwater_hi = (cwm & 0x1f);
  2941. /* Set request length to 8 cachelines per fetch */
  2942. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  2943. fwater_hi = fwater_hi | (1 << 8);
  2944. I915_WRITE(FW_BLC, fwater_lo);
  2945. I915_WRITE(FW_BLC2, fwater_hi);
  2946. }
  2947. static void i830_update_wm(struct drm_device *dev, int planea_clock, int unused,
  2948. int unused2, int unused3, int pixel_size)
  2949. {
  2950. struct drm_i915_private *dev_priv = dev->dev_private;
  2951. uint32_t fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  2952. int planea_wm;
  2953. i830_wm_info.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  2954. planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
  2955. pixel_size, latency_ns);
  2956. fwater_lo |= (3<<8) | planea_wm;
  2957. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  2958. I915_WRITE(FW_BLC, fwater_lo);
  2959. }
  2960. #define ILK_LP0_PLANE_LATENCY 700
  2961. #define ILK_LP0_CURSOR_LATENCY 1300
  2962. static bool ironlake_compute_wm0(struct drm_device *dev,
  2963. int pipe,
  2964. const struct intel_watermark_params *display,
  2965. int display_latency_ns,
  2966. const struct intel_watermark_params *cursor,
  2967. int cursor_latency_ns,
  2968. int *plane_wm,
  2969. int *cursor_wm)
  2970. {
  2971. struct drm_crtc *crtc;
  2972. int htotal, hdisplay, clock, pixel_size = 0;
  2973. int line_time_us, line_count, entries;
  2974. crtc = intel_get_crtc_for_pipe(dev, pipe);
  2975. if (crtc->fb == NULL || !crtc->enabled)
  2976. return false;
  2977. htotal = crtc->mode.htotal;
  2978. hdisplay = crtc->mode.hdisplay;
  2979. clock = crtc->mode.clock;
  2980. pixel_size = crtc->fb->bits_per_pixel / 8;
  2981. /* Use the small buffer method to calculate plane watermark */
  2982. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  2983. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  2984. *plane_wm = entries + display->guard_size;
  2985. if (*plane_wm > (int)display->max_wm)
  2986. *plane_wm = display->max_wm;
  2987. /* Use the large buffer method to calculate cursor watermark */
  2988. line_time_us = ((htotal * 1000) / clock);
  2989. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  2990. entries = line_count * 64 * pixel_size;
  2991. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  2992. *cursor_wm = entries + cursor->guard_size;
  2993. if (*cursor_wm > (int)cursor->max_wm)
  2994. *cursor_wm = (int)cursor->max_wm;
  2995. return true;
  2996. }
  2997. /*
  2998. * Check the wm result.
  2999. *
  3000. * If any calculated watermark values is larger than the maximum value that
  3001. * can be programmed into the associated watermark register, that watermark
  3002. * must be disabled.
  3003. */
  3004. static bool ironlake_check_srwm(struct drm_device *dev, int level,
  3005. int fbc_wm, int display_wm, int cursor_wm,
  3006. const struct intel_watermark_params *display,
  3007. const struct intel_watermark_params *cursor)
  3008. {
  3009. struct drm_i915_private *dev_priv = dev->dev_private;
  3010. DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
  3011. " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
  3012. if (fbc_wm > SNB_FBC_MAX_SRWM) {
  3013. DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
  3014. fbc_wm, SNB_FBC_MAX_SRWM, level);
  3015. /* fbc has it's own way to disable FBC WM */
  3016. I915_WRITE(DISP_ARB_CTL,
  3017. I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
  3018. return false;
  3019. }
  3020. if (display_wm > display->max_wm) {
  3021. DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
  3022. display_wm, SNB_DISPLAY_MAX_SRWM, level);
  3023. return false;
  3024. }
  3025. if (cursor_wm > cursor->max_wm) {
  3026. DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
  3027. cursor_wm, SNB_CURSOR_MAX_SRWM, level);
  3028. return false;
  3029. }
  3030. if (!(fbc_wm || display_wm || cursor_wm)) {
  3031. DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
  3032. return false;
  3033. }
  3034. return true;
  3035. }
  3036. /*
  3037. * Compute watermark values of WM[1-3],
  3038. */
  3039. static bool ironlake_compute_srwm(struct drm_device *dev, int level,
  3040. int hdisplay, int htotal,
  3041. int pixel_size, int clock, int latency_ns,
  3042. const struct intel_watermark_params *display,
  3043. const struct intel_watermark_params *cursor,
  3044. int *fbc_wm, int *display_wm, int *cursor_wm)
  3045. {
  3046. unsigned long line_time_us;
  3047. int line_count, line_size;
  3048. int small, large;
  3049. int entries;
  3050. if (!latency_ns) {
  3051. *fbc_wm = *display_wm = *cursor_wm = 0;
  3052. return false;
  3053. }
  3054. line_time_us = (htotal * 1000) / clock;
  3055. line_count = (latency_ns / line_time_us + 1000) / 1000;
  3056. line_size = hdisplay * pixel_size;
  3057. /* Use the minimum of the small and large buffer method for primary */
  3058. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  3059. large = line_count * line_size;
  3060. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  3061. *display_wm = entries + display->guard_size;
  3062. /*
  3063. * Spec says:
  3064. * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
  3065. */
  3066. *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
  3067. /* calculate the self-refresh watermark for display cursor */
  3068. entries = line_count * pixel_size * 64;
  3069. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3070. *cursor_wm = entries + cursor->guard_size;
  3071. return ironlake_check_srwm(dev, level,
  3072. *fbc_wm, *display_wm, *cursor_wm,
  3073. display, cursor);
  3074. }
  3075. static void ironlake_update_wm(struct drm_device *dev,
  3076. int planea_clock, int planeb_clock,
  3077. int hdisplay, int htotal,
  3078. int pixel_size)
  3079. {
  3080. struct drm_i915_private *dev_priv = dev->dev_private;
  3081. int fbc_wm, plane_wm, cursor_wm, enabled;
  3082. int clock;
  3083. enabled = 0;
  3084. if (ironlake_compute_wm0(dev, 0,
  3085. &ironlake_display_wm_info,
  3086. ILK_LP0_PLANE_LATENCY,
  3087. &ironlake_cursor_wm_info,
  3088. ILK_LP0_CURSOR_LATENCY,
  3089. &plane_wm, &cursor_wm)) {
  3090. I915_WRITE(WM0_PIPEA_ILK,
  3091. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3092. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3093. " plane %d, " "cursor: %d\n",
  3094. plane_wm, cursor_wm);
  3095. enabled++;
  3096. }
  3097. if (ironlake_compute_wm0(dev, 1,
  3098. &ironlake_display_wm_info,
  3099. ILK_LP0_PLANE_LATENCY,
  3100. &ironlake_cursor_wm_info,
  3101. ILK_LP0_CURSOR_LATENCY,
  3102. &plane_wm, &cursor_wm)) {
  3103. I915_WRITE(WM0_PIPEB_ILK,
  3104. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3105. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3106. " plane %d, cursor: %d\n",
  3107. plane_wm, cursor_wm);
  3108. enabled++;
  3109. }
  3110. /*
  3111. * Calculate and update the self-refresh watermark only when one
  3112. * display plane is used.
  3113. */
  3114. I915_WRITE(WM3_LP_ILK, 0);
  3115. I915_WRITE(WM2_LP_ILK, 0);
  3116. I915_WRITE(WM1_LP_ILK, 0);
  3117. if (enabled != 1)
  3118. return;
  3119. clock = planea_clock ? planea_clock : planeb_clock;
  3120. /* WM1 */
  3121. if (!ironlake_compute_srwm(dev, 1, hdisplay, htotal, pixel_size,
  3122. clock, ILK_READ_WM1_LATENCY() * 500,
  3123. &ironlake_display_srwm_info,
  3124. &ironlake_cursor_srwm_info,
  3125. &fbc_wm, &plane_wm, &cursor_wm))
  3126. return;
  3127. I915_WRITE(WM1_LP_ILK,
  3128. WM1_LP_SR_EN |
  3129. (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3130. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3131. (plane_wm << WM1_LP_SR_SHIFT) |
  3132. cursor_wm);
  3133. /* WM2 */
  3134. if (!ironlake_compute_srwm(dev, 2, hdisplay, htotal, pixel_size,
  3135. clock, ILK_READ_WM2_LATENCY() * 500,
  3136. &ironlake_display_srwm_info,
  3137. &ironlake_cursor_srwm_info,
  3138. &fbc_wm, &plane_wm, &cursor_wm))
  3139. return;
  3140. I915_WRITE(WM2_LP_ILK,
  3141. WM2_LP_EN |
  3142. (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3143. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3144. (plane_wm << WM1_LP_SR_SHIFT) |
  3145. cursor_wm);
  3146. /*
  3147. * WM3 is unsupported on ILK, probably because we don't have latency
  3148. * data for that power state
  3149. */
  3150. }
  3151. static void sandybridge_update_wm(struct drm_device *dev,
  3152. int planea_clock, int planeb_clock,
  3153. int hdisplay, int htotal,
  3154. int pixel_size)
  3155. {
  3156. struct drm_i915_private *dev_priv = dev->dev_private;
  3157. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  3158. int fbc_wm, plane_wm, cursor_wm, enabled;
  3159. int clock;
  3160. enabled = 0;
  3161. if (ironlake_compute_wm0(dev, 0,
  3162. &sandybridge_display_wm_info, latency,
  3163. &sandybridge_cursor_wm_info, latency,
  3164. &plane_wm, &cursor_wm)) {
  3165. I915_WRITE(WM0_PIPEA_ILK,
  3166. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3167. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3168. " plane %d, " "cursor: %d\n",
  3169. plane_wm, cursor_wm);
  3170. enabled++;
  3171. }
  3172. if (ironlake_compute_wm0(dev, 1,
  3173. &sandybridge_display_wm_info, latency,
  3174. &sandybridge_cursor_wm_info, latency,
  3175. &plane_wm, &cursor_wm)) {
  3176. I915_WRITE(WM0_PIPEB_ILK,
  3177. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3178. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3179. " plane %d, cursor: %d\n",
  3180. plane_wm, cursor_wm);
  3181. enabled++;
  3182. }
  3183. /*
  3184. * Calculate and update the self-refresh watermark only when one
  3185. * display plane is used.
  3186. *
  3187. * SNB support 3 levels of watermark.
  3188. *
  3189. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  3190. * and disabled in the descending order
  3191. *
  3192. */
  3193. I915_WRITE(WM3_LP_ILK, 0);
  3194. I915_WRITE(WM2_LP_ILK, 0);
  3195. I915_WRITE(WM1_LP_ILK, 0);
  3196. if (enabled != 1)
  3197. return;
  3198. clock = planea_clock ? planea_clock : planeb_clock;
  3199. /* WM1 */
  3200. if (!ironlake_compute_srwm(dev, 1, hdisplay, htotal, pixel_size,
  3201. clock, SNB_READ_WM1_LATENCY() * 500,
  3202. &sandybridge_display_srwm_info,
  3203. &sandybridge_cursor_srwm_info,
  3204. &fbc_wm, &plane_wm, &cursor_wm))
  3205. return;
  3206. I915_WRITE(WM1_LP_ILK,
  3207. WM1_LP_SR_EN |
  3208. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3209. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3210. (plane_wm << WM1_LP_SR_SHIFT) |
  3211. cursor_wm);
  3212. /* WM2 */
  3213. if (!ironlake_compute_srwm(dev, 2,
  3214. hdisplay, htotal, pixel_size,
  3215. clock, SNB_READ_WM2_LATENCY() * 500,
  3216. &sandybridge_display_srwm_info,
  3217. &sandybridge_cursor_srwm_info,
  3218. &fbc_wm, &plane_wm, &cursor_wm))
  3219. return;
  3220. I915_WRITE(WM2_LP_ILK,
  3221. WM2_LP_EN |
  3222. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3223. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3224. (plane_wm << WM1_LP_SR_SHIFT) |
  3225. cursor_wm);
  3226. /* WM3 */
  3227. if (!ironlake_compute_srwm(dev, 3,
  3228. hdisplay, htotal, pixel_size,
  3229. clock, SNB_READ_WM3_LATENCY() * 500,
  3230. &sandybridge_display_srwm_info,
  3231. &sandybridge_cursor_srwm_info,
  3232. &fbc_wm, &plane_wm, &cursor_wm))
  3233. return;
  3234. I915_WRITE(WM3_LP_ILK,
  3235. WM3_LP_EN |
  3236. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3237. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3238. (plane_wm << WM1_LP_SR_SHIFT) |
  3239. cursor_wm);
  3240. }
  3241. /**
  3242. * intel_update_watermarks - update FIFO watermark values based on current modes
  3243. *
  3244. * Calculate watermark values for the various WM regs based on current mode
  3245. * and plane configuration.
  3246. *
  3247. * There are several cases to deal with here:
  3248. * - normal (i.e. non-self-refresh)
  3249. * - self-refresh (SR) mode
  3250. * - lines are large relative to FIFO size (buffer can hold up to 2)
  3251. * - lines are small relative to FIFO size (buffer can hold more than 2
  3252. * lines), so need to account for TLB latency
  3253. *
  3254. * The normal calculation is:
  3255. * watermark = dotclock * bytes per pixel * latency
  3256. * where latency is platform & configuration dependent (we assume pessimal
  3257. * values here).
  3258. *
  3259. * The SR calculation is:
  3260. * watermark = (trunc(latency/line time)+1) * surface width *
  3261. * bytes per pixel
  3262. * where
  3263. * line time = htotal / dotclock
  3264. * surface width = hdisplay for normal plane and 64 for cursor
  3265. * and latency is assumed to be high, as above.
  3266. *
  3267. * The final value programmed to the register should always be rounded up,
  3268. * and include an extra 2 entries to account for clock crossings.
  3269. *
  3270. * We don't use the sprite, so we can ignore that. And on Crestline we have
  3271. * to set the non-SR watermarks to 8.
  3272. */
  3273. static void intel_update_watermarks(struct drm_device *dev)
  3274. {
  3275. struct drm_i915_private *dev_priv = dev->dev_private;
  3276. struct drm_crtc *crtc;
  3277. int sr_hdisplay = 0;
  3278. unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
  3279. int enabled = 0, pixel_size = 0;
  3280. int sr_htotal = 0;
  3281. if (!dev_priv->display.update_wm)
  3282. return;
  3283. /* Get the clock config from both planes */
  3284. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3285. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3286. if (intel_crtc->active) {
  3287. enabled++;
  3288. if (intel_crtc->plane == 0) {
  3289. DRM_DEBUG_KMS("plane A (pipe %d) clock: %d\n",
  3290. intel_crtc->pipe, crtc->mode.clock);
  3291. planea_clock = crtc->mode.clock;
  3292. } else {
  3293. DRM_DEBUG_KMS("plane B (pipe %d) clock: %d\n",
  3294. intel_crtc->pipe, crtc->mode.clock);
  3295. planeb_clock = crtc->mode.clock;
  3296. }
  3297. sr_hdisplay = crtc->mode.hdisplay;
  3298. sr_clock = crtc->mode.clock;
  3299. sr_htotal = crtc->mode.htotal;
  3300. if (crtc->fb)
  3301. pixel_size = crtc->fb->bits_per_pixel / 8;
  3302. else
  3303. pixel_size = 4; /* by default */
  3304. }
  3305. }
  3306. if (enabled <= 0)
  3307. return;
  3308. dev_priv->display.update_wm(dev, planea_clock, planeb_clock,
  3309. sr_hdisplay, sr_htotal, pixel_size);
  3310. }
  3311. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  3312. struct drm_display_mode *mode,
  3313. struct drm_display_mode *adjusted_mode,
  3314. int x, int y,
  3315. struct drm_framebuffer *old_fb)
  3316. {
  3317. struct drm_device *dev = crtc->dev;
  3318. struct drm_i915_private *dev_priv = dev->dev_private;
  3319. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3320. int pipe = intel_crtc->pipe;
  3321. int plane = intel_crtc->plane;
  3322. u32 fp_reg, dpll_reg;
  3323. int refclk, num_connectors = 0;
  3324. intel_clock_t clock, reduced_clock;
  3325. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  3326. bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
  3327. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  3328. struct intel_encoder *has_edp_encoder = NULL;
  3329. struct drm_mode_config *mode_config = &dev->mode_config;
  3330. struct intel_encoder *encoder;
  3331. const intel_limit_t *limit;
  3332. int ret;
  3333. struct fdi_m_n m_n = {0};
  3334. u32 reg, temp;
  3335. int target_clock;
  3336. drm_vblank_pre_modeset(dev, pipe);
  3337. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  3338. if (encoder->base.crtc != crtc)
  3339. continue;
  3340. switch (encoder->type) {
  3341. case INTEL_OUTPUT_LVDS:
  3342. is_lvds = true;
  3343. break;
  3344. case INTEL_OUTPUT_SDVO:
  3345. case INTEL_OUTPUT_HDMI:
  3346. is_sdvo = true;
  3347. if (encoder->needs_tv_clock)
  3348. is_tv = true;
  3349. break;
  3350. case INTEL_OUTPUT_DVO:
  3351. is_dvo = true;
  3352. break;
  3353. case INTEL_OUTPUT_TVOUT:
  3354. is_tv = true;
  3355. break;
  3356. case INTEL_OUTPUT_ANALOG:
  3357. is_crt = true;
  3358. break;
  3359. case INTEL_OUTPUT_DISPLAYPORT:
  3360. is_dp = true;
  3361. break;
  3362. case INTEL_OUTPUT_EDP:
  3363. has_edp_encoder = encoder;
  3364. break;
  3365. }
  3366. num_connectors++;
  3367. }
  3368. if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2) {
  3369. refclk = dev_priv->lvds_ssc_freq * 1000;
  3370. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3371. refclk / 1000);
  3372. } else if (!IS_GEN2(dev)) {
  3373. refclk = 96000;
  3374. if (HAS_PCH_SPLIT(dev) &&
  3375. (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)))
  3376. refclk = 120000; /* 120Mhz refclk */
  3377. } else {
  3378. refclk = 48000;
  3379. }
  3380. /*
  3381. * Returns a set of divisors for the desired target clock with the given
  3382. * refclk, or FALSE. The returned values represent the clock equation:
  3383. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3384. */
  3385. limit = intel_limit(crtc, refclk);
  3386. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  3387. if (!ok) {
  3388. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3389. drm_vblank_post_modeset(dev, pipe);
  3390. return -EINVAL;
  3391. }
  3392. /* Ensure that the cursor is valid for the new mode before changing... */
  3393. intel_crtc_update_cursor(crtc, true);
  3394. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3395. has_reduced_clock = limit->find_pll(limit, crtc,
  3396. dev_priv->lvds_downclock,
  3397. refclk,
  3398. &reduced_clock);
  3399. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  3400. /*
  3401. * If the different P is found, it means that we can't
  3402. * switch the display clock by using the FP0/FP1.
  3403. * In such case we will disable the LVDS downclock
  3404. * feature.
  3405. */
  3406. DRM_DEBUG_KMS("Different P is found for "
  3407. "LVDS clock/downclock\n");
  3408. has_reduced_clock = 0;
  3409. }
  3410. }
  3411. /* SDVO TV has fixed PLL values depend on its clock range,
  3412. this mirrors vbios setting. */
  3413. if (is_sdvo && is_tv) {
  3414. if (adjusted_mode->clock >= 100000
  3415. && adjusted_mode->clock < 140500) {
  3416. clock.p1 = 2;
  3417. clock.p2 = 10;
  3418. clock.n = 3;
  3419. clock.m1 = 16;
  3420. clock.m2 = 8;
  3421. } else if (adjusted_mode->clock >= 140500
  3422. && adjusted_mode->clock <= 200000) {
  3423. clock.p1 = 1;
  3424. clock.p2 = 10;
  3425. clock.n = 6;
  3426. clock.m1 = 12;
  3427. clock.m2 = 8;
  3428. }
  3429. }
  3430. /* FDI link */
  3431. if (HAS_PCH_SPLIT(dev)) {
  3432. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3433. int lane = 0, link_bw, bpp;
  3434. /* CPU eDP doesn't require FDI link, so just set DP M/N
  3435. according to current link config */
  3436. if (has_edp_encoder && !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3437. target_clock = mode->clock;
  3438. intel_edp_link_config(has_edp_encoder,
  3439. &lane, &link_bw);
  3440. } else {
  3441. /* [e]DP over FDI requires target mode clock
  3442. instead of link clock */
  3443. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  3444. target_clock = mode->clock;
  3445. else
  3446. target_clock = adjusted_mode->clock;
  3447. /* FDI is a binary signal running at ~2.7GHz, encoding
  3448. * each output octet as 10 bits. The actual frequency
  3449. * is stored as a divider into a 100MHz clock, and the
  3450. * mode pixel clock is stored in units of 1KHz.
  3451. * Hence the bw of each lane in terms of the mode signal
  3452. * is:
  3453. */
  3454. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  3455. }
  3456. /* determine panel color depth */
  3457. temp = I915_READ(PIPECONF(pipe));
  3458. temp &= ~PIPE_BPC_MASK;
  3459. if (is_lvds) {
  3460. /* the BPC will be 6 if it is 18-bit LVDS panel */
  3461. if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
  3462. temp |= PIPE_8BPC;
  3463. else
  3464. temp |= PIPE_6BPC;
  3465. } else if (has_edp_encoder) {
  3466. switch (dev_priv->edp.bpp/3) {
  3467. case 8:
  3468. temp |= PIPE_8BPC;
  3469. break;
  3470. case 10:
  3471. temp |= PIPE_10BPC;
  3472. break;
  3473. case 6:
  3474. temp |= PIPE_6BPC;
  3475. break;
  3476. case 12:
  3477. temp |= PIPE_12BPC;
  3478. break;
  3479. }
  3480. } else
  3481. temp |= PIPE_8BPC;
  3482. I915_WRITE(PIPECONF(pipe), temp);
  3483. switch (temp & PIPE_BPC_MASK) {
  3484. case PIPE_8BPC:
  3485. bpp = 24;
  3486. break;
  3487. case PIPE_10BPC:
  3488. bpp = 30;
  3489. break;
  3490. case PIPE_6BPC:
  3491. bpp = 18;
  3492. break;
  3493. case PIPE_12BPC:
  3494. bpp = 36;
  3495. break;
  3496. default:
  3497. DRM_ERROR("unknown pipe bpc value\n");
  3498. bpp = 24;
  3499. }
  3500. if (!lane) {
  3501. /*
  3502. * Account for spread spectrum to avoid
  3503. * oversubscribing the link. Max center spread
  3504. * is 2.5%; use 5% for safety's sake.
  3505. */
  3506. u32 bps = target_clock * bpp * 21 / 20;
  3507. lane = bps / (link_bw * 8) + 1;
  3508. }
  3509. intel_crtc->fdi_lanes = lane;
  3510. if (pixel_multiplier > 1)
  3511. link_bw *= pixel_multiplier;
  3512. ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);
  3513. }
  3514. /* Ironlake: try to setup display ref clock before DPLL
  3515. * enabling. This is only under driver's control after
  3516. * PCH B stepping, previous chipset stepping should be
  3517. * ignoring this setting.
  3518. */
  3519. if (HAS_PCH_SPLIT(dev)) {
  3520. temp = I915_READ(PCH_DREF_CONTROL);
  3521. /* Always enable nonspread source */
  3522. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  3523. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  3524. temp &= ~DREF_SSC_SOURCE_MASK;
  3525. temp |= DREF_SSC_SOURCE_ENABLE;
  3526. I915_WRITE(PCH_DREF_CONTROL, temp);
  3527. POSTING_READ(PCH_DREF_CONTROL);
  3528. udelay(200);
  3529. if (has_edp_encoder) {
  3530. if (dev_priv->lvds_use_ssc) {
  3531. temp |= DREF_SSC1_ENABLE;
  3532. I915_WRITE(PCH_DREF_CONTROL, temp);
  3533. POSTING_READ(PCH_DREF_CONTROL);
  3534. udelay(200);
  3535. }
  3536. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  3537. /* Enable CPU source on CPU attached eDP */
  3538. if (!intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3539. if (dev_priv->lvds_use_ssc)
  3540. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  3541. else
  3542. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  3543. } else {
  3544. /* Enable SSC on PCH eDP if needed */
  3545. if (dev_priv->lvds_use_ssc) {
  3546. DRM_ERROR("enabling SSC on PCH\n");
  3547. temp |= DREF_SUPERSPREAD_SOURCE_ENABLE;
  3548. }
  3549. }
  3550. I915_WRITE(PCH_DREF_CONTROL, temp);
  3551. POSTING_READ(PCH_DREF_CONTROL);
  3552. udelay(200);
  3553. }
  3554. }
  3555. if (IS_PINEVIEW(dev)) {
  3556. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  3557. if (has_reduced_clock)
  3558. fp2 = (1 << reduced_clock.n) << 16 |
  3559. reduced_clock.m1 << 8 | reduced_clock.m2;
  3560. } else {
  3561. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  3562. if (has_reduced_clock)
  3563. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  3564. reduced_clock.m2;
  3565. }
  3566. /* Enable autotuning of the PLL clock (if permissible) */
  3567. if (HAS_PCH_SPLIT(dev)) {
  3568. int factor = 21;
  3569. if (is_lvds) {
  3570. if ((dev_priv->lvds_use_ssc &&
  3571. dev_priv->lvds_ssc_freq == 100) ||
  3572. (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
  3573. factor = 25;
  3574. } else if (is_sdvo && is_tv)
  3575. factor = 20;
  3576. if (clock.m1 < factor * clock.n)
  3577. fp |= FP_CB_TUNE;
  3578. }
  3579. dpll = 0;
  3580. if (!HAS_PCH_SPLIT(dev))
  3581. dpll = DPLL_VGA_MODE_DIS;
  3582. if (!IS_GEN2(dev)) {
  3583. if (is_lvds)
  3584. dpll |= DPLLB_MODE_LVDS;
  3585. else
  3586. dpll |= DPLLB_MODE_DAC_SERIAL;
  3587. if (is_sdvo) {
  3588. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3589. if (pixel_multiplier > 1) {
  3590. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3591. dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  3592. else if (HAS_PCH_SPLIT(dev))
  3593. dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  3594. }
  3595. dpll |= DPLL_DVO_HIGH_SPEED;
  3596. }
  3597. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  3598. dpll |= DPLL_DVO_HIGH_SPEED;
  3599. /* compute bitmask from p1 value */
  3600. if (IS_PINEVIEW(dev))
  3601. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  3602. else {
  3603. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3604. /* also FPA1 */
  3605. if (HAS_PCH_SPLIT(dev))
  3606. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3607. if (IS_G4X(dev) && has_reduced_clock)
  3608. dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3609. }
  3610. switch (clock.p2) {
  3611. case 5:
  3612. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3613. break;
  3614. case 7:
  3615. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3616. break;
  3617. case 10:
  3618. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3619. break;
  3620. case 14:
  3621. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3622. break;
  3623. }
  3624. if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev))
  3625. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  3626. } else {
  3627. if (is_lvds) {
  3628. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3629. } else {
  3630. if (clock.p1 == 2)
  3631. dpll |= PLL_P1_DIVIDE_BY_TWO;
  3632. else
  3633. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3634. if (clock.p2 == 4)
  3635. dpll |= PLL_P2_DIVIDE_BY_4;
  3636. }
  3637. }
  3638. if (is_sdvo && is_tv)
  3639. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3640. else if (is_tv)
  3641. /* XXX: just matching BIOS for now */
  3642. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3643. dpll |= 3;
  3644. else if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2)
  3645. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3646. else
  3647. dpll |= PLL_REF_INPUT_DREFCLK;
  3648. /* setup pipeconf */
  3649. pipeconf = I915_READ(PIPECONF(pipe));
  3650. /* Set up the display plane register */
  3651. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3652. /* Ironlake's plane is forced to pipe, bit 24 is to
  3653. enable color space conversion */
  3654. if (!HAS_PCH_SPLIT(dev)) {
  3655. if (pipe == 0)
  3656. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3657. else
  3658. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3659. }
  3660. if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  3661. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  3662. * core speed.
  3663. *
  3664. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  3665. * pipe == 0 check?
  3666. */
  3667. if (mode->clock >
  3668. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  3669. pipeconf |= PIPECONF_DOUBLE_WIDE;
  3670. else
  3671. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  3672. }
  3673. dspcntr |= DISPLAY_PLANE_ENABLE;
  3674. pipeconf |= PIPECONF_ENABLE;
  3675. dpll |= DPLL_VCO_ENABLE;
  3676. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  3677. drm_mode_debug_printmodeline(mode);
  3678. /* assign to Ironlake registers */
  3679. if (HAS_PCH_SPLIT(dev)) {
  3680. fp_reg = PCH_FP0(pipe);
  3681. dpll_reg = PCH_DPLL(pipe);
  3682. } else {
  3683. fp_reg = FP0(pipe);
  3684. dpll_reg = DPLL(pipe);
  3685. }
  3686. /* PCH eDP needs FDI, but CPU eDP does not */
  3687. if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3688. I915_WRITE(fp_reg, fp);
  3689. I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
  3690. POSTING_READ(dpll_reg);
  3691. udelay(150);
  3692. }
  3693. /* enable transcoder DPLL */
  3694. if (HAS_PCH_CPT(dev)) {
  3695. temp = I915_READ(PCH_DPLL_SEL);
  3696. if (pipe == 0)
  3697. temp |= TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL;
  3698. else
  3699. temp |= TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL;
  3700. I915_WRITE(PCH_DPLL_SEL, temp);
  3701. POSTING_READ(PCH_DPLL_SEL);
  3702. udelay(150);
  3703. }
  3704. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3705. * This is an exception to the general rule that mode_set doesn't turn
  3706. * things on.
  3707. */
  3708. if (is_lvds) {
  3709. reg = LVDS;
  3710. if (HAS_PCH_SPLIT(dev))
  3711. reg = PCH_LVDS;
  3712. temp = I915_READ(reg);
  3713. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  3714. if (pipe == 1) {
  3715. if (HAS_PCH_CPT(dev))
  3716. temp |= PORT_TRANS_B_SEL_CPT;
  3717. else
  3718. temp |= LVDS_PIPEB_SELECT;
  3719. } else {
  3720. if (HAS_PCH_CPT(dev))
  3721. temp &= ~PORT_TRANS_SEL_MASK;
  3722. else
  3723. temp &= ~LVDS_PIPEB_SELECT;
  3724. }
  3725. /* set the corresponsding LVDS_BORDER bit */
  3726. temp |= dev_priv->lvds_border_bits;
  3727. /* Set the B0-B3 data pairs corresponding to whether we're going to
  3728. * set the DPLLs for dual-channel mode or not.
  3729. */
  3730. if (clock.p2 == 7)
  3731. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  3732. else
  3733. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  3734. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  3735. * appropriately here, but we need to look more thoroughly into how
  3736. * panels behave in the two modes.
  3737. */
  3738. /* set the dithering flag on non-PCH LVDS as needed */
  3739. if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
  3740. if (dev_priv->lvds_dither)
  3741. temp |= LVDS_ENABLE_DITHER;
  3742. else
  3743. temp &= ~LVDS_ENABLE_DITHER;
  3744. }
  3745. I915_WRITE(reg, temp);
  3746. }
  3747. /* set the dithering flag and clear for anything other than a panel. */
  3748. if (HAS_PCH_SPLIT(dev)) {
  3749. pipeconf &= ~PIPECONF_DITHER_EN;
  3750. pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
  3751. if (dev_priv->lvds_dither && (is_lvds || has_edp_encoder)) {
  3752. pipeconf |= PIPECONF_DITHER_EN;
  3753. pipeconf |= PIPECONF_DITHER_TYPE_ST1;
  3754. }
  3755. }
  3756. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3757. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3758. } else if (HAS_PCH_SPLIT(dev)) {
  3759. /* For non-DP output, clear any trans DP clock recovery setting.*/
  3760. if (pipe == 0) {
  3761. I915_WRITE(TRANSA_DATA_M1, 0);
  3762. I915_WRITE(TRANSA_DATA_N1, 0);
  3763. I915_WRITE(TRANSA_DP_LINK_M1, 0);
  3764. I915_WRITE(TRANSA_DP_LINK_N1, 0);
  3765. } else {
  3766. I915_WRITE(TRANSB_DATA_M1, 0);
  3767. I915_WRITE(TRANSB_DATA_N1, 0);
  3768. I915_WRITE(TRANSB_DP_LINK_M1, 0);
  3769. I915_WRITE(TRANSB_DP_LINK_N1, 0);
  3770. }
  3771. }
  3772. if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3773. I915_WRITE(dpll_reg, dpll);
  3774. /* Wait for the clocks to stabilize. */
  3775. POSTING_READ(dpll_reg);
  3776. udelay(150);
  3777. if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
  3778. temp = 0;
  3779. if (is_sdvo) {
  3780. temp = intel_mode_get_pixel_multiplier(adjusted_mode);
  3781. if (temp > 1)
  3782. temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3783. else
  3784. temp = 0;
  3785. }
  3786. I915_WRITE(DPLL_MD(pipe), temp);
  3787. } else {
  3788. /* The pixel multiplier can only be updated once the
  3789. * DPLL is enabled and the clocks are stable.
  3790. *
  3791. * So write it again.
  3792. */
  3793. I915_WRITE(dpll_reg, dpll);
  3794. }
  3795. }
  3796. intel_crtc->lowfreq_avail = false;
  3797. if (is_lvds && has_reduced_clock && i915_powersave) {
  3798. I915_WRITE(fp_reg + 4, fp2);
  3799. intel_crtc->lowfreq_avail = true;
  3800. if (HAS_PIPE_CXSR(dev)) {
  3801. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  3802. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  3803. }
  3804. } else {
  3805. I915_WRITE(fp_reg + 4, fp);
  3806. if (HAS_PIPE_CXSR(dev)) {
  3807. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  3808. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  3809. }
  3810. }
  3811. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3812. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  3813. /* the chip adds 2 halflines automatically */
  3814. adjusted_mode->crtc_vdisplay -= 1;
  3815. adjusted_mode->crtc_vtotal -= 1;
  3816. adjusted_mode->crtc_vblank_start -= 1;
  3817. adjusted_mode->crtc_vblank_end -= 1;
  3818. adjusted_mode->crtc_vsync_end -= 1;
  3819. adjusted_mode->crtc_vsync_start -= 1;
  3820. } else
  3821. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  3822. I915_WRITE(HTOTAL(pipe),
  3823. (adjusted_mode->crtc_hdisplay - 1) |
  3824. ((adjusted_mode->crtc_htotal - 1) << 16));
  3825. I915_WRITE(HBLANK(pipe),
  3826. (adjusted_mode->crtc_hblank_start - 1) |
  3827. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3828. I915_WRITE(HSYNC(pipe),
  3829. (adjusted_mode->crtc_hsync_start - 1) |
  3830. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3831. I915_WRITE(VTOTAL(pipe),
  3832. (adjusted_mode->crtc_vdisplay - 1) |
  3833. ((adjusted_mode->crtc_vtotal - 1) << 16));
  3834. I915_WRITE(VBLANK(pipe),
  3835. (adjusted_mode->crtc_vblank_start - 1) |
  3836. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  3837. I915_WRITE(VSYNC(pipe),
  3838. (adjusted_mode->crtc_vsync_start - 1) |
  3839. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3840. /* pipesrc and dspsize control the size that is scaled from,
  3841. * which should always be the user's requested size.
  3842. */
  3843. if (!HAS_PCH_SPLIT(dev)) {
  3844. I915_WRITE(DSPSIZE(plane),
  3845. ((mode->vdisplay - 1) << 16) |
  3846. (mode->hdisplay - 1));
  3847. I915_WRITE(DSPPOS(plane), 0);
  3848. }
  3849. I915_WRITE(PIPESRC(pipe),
  3850. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3851. if (HAS_PCH_SPLIT(dev)) {
  3852. I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
  3853. I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
  3854. I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
  3855. I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
  3856. if (has_edp_encoder && !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3857. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  3858. }
  3859. }
  3860. I915_WRITE(PIPECONF(pipe), pipeconf);
  3861. POSTING_READ(PIPECONF(pipe));
  3862. intel_wait_for_vblank(dev, pipe);
  3863. if (IS_GEN5(dev)) {
  3864. /* enable address swizzle for tiling buffer */
  3865. temp = I915_READ(DISP_ARB_CTL);
  3866. I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
  3867. }
  3868. I915_WRITE(DSPCNTR(plane), dspcntr);
  3869. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  3870. intel_update_watermarks(dev);
  3871. drm_vblank_post_modeset(dev, pipe);
  3872. return ret;
  3873. }
  3874. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3875. void intel_crtc_load_lut(struct drm_crtc *crtc)
  3876. {
  3877. struct drm_device *dev = crtc->dev;
  3878. struct drm_i915_private *dev_priv = dev->dev_private;
  3879. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3880. int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
  3881. int i;
  3882. /* The clocks have to be on to load the palette. */
  3883. if (!crtc->enabled)
  3884. return;
  3885. /* use legacy palette for Ironlake */
  3886. if (HAS_PCH_SPLIT(dev))
  3887. palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
  3888. LGC_PALETTE_B;
  3889. for (i = 0; i < 256; i++) {
  3890. I915_WRITE(palreg + 4 * i,
  3891. (intel_crtc->lut_r[i] << 16) |
  3892. (intel_crtc->lut_g[i] << 8) |
  3893. intel_crtc->lut_b[i]);
  3894. }
  3895. }
  3896. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  3897. {
  3898. struct drm_device *dev = crtc->dev;
  3899. struct drm_i915_private *dev_priv = dev->dev_private;
  3900. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3901. bool visible = base != 0;
  3902. u32 cntl;
  3903. if (intel_crtc->cursor_visible == visible)
  3904. return;
  3905. cntl = I915_READ(CURACNTR);
  3906. if (visible) {
  3907. /* On these chipsets we can only modify the base whilst
  3908. * the cursor is disabled.
  3909. */
  3910. I915_WRITE(CURABASE, base);
  3911. cntl &= ~(CURSOR_FORMAT_MASK);
  3912. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  3913. cntl |= CURSOR_ENABLE |
  3914. CURSOR_GAMMA_ENABLE |
  3915. CURSOR_FORMAT_ARGB;
  3916. } else
  3917. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  3918. I915_WRITE(CURACNTR, cntl);
  3919. intel_crtc->cursor_visible = visible;
  3920. }
  3921. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  3922. {
  3923. struct drm_device *dev = crtc->dev;
  3924. struct drm_i915_private *dev_priv = dev->dev_private;
  3925. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3926. int pipe = intel_crtc->pipe;
  3927. bool visible = base != 0;
  3928. if (intel_crtc->cursor_visible != visible) {
  3929. uint32_t cntl = I915_READ(pipe == 0 ? CURACNTR : CURBCNTR);
  3930. if (base) {
  3931. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  3932. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  3933. cntl |= pipe << 28; /* Connect to correct pipe */
  3934. } else {
  3935. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  3936. cntl |= CURSOR_MODE_DISABLE;
  3937. }
  3938. I915_WRITE(pipe == 0 ? CURACNTR : CURBCNTR, cntl);
  3939. intel_crtc->cursor_visible = visible;
  3940. }
  3941. /* and commit changes on next vblank */
  3942. I915_WRITE(pipe == 0 ? CURABASE : CURBBASE, base);
  3943. }
  3944. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  3945. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  3946. bool on)
  3947. {
  3948. struct drm_device *dev = crtc->dev;
  3949. struct drm_i915_private *dev_priv = dev->dev_private;
  3950. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3951. int pipe = intel_crtc->pipe;
  3952. int x = intel_crtc->cursor_x;
  3953. int y = intel_crtc->cursor_y;
  3954. u32 base, pos;
  3955. bool visible;
  3956. pos = 0;
  3957. if (on && crtc->enabled && crtc->fb) {
  3958. base = intel_crtc->cursor_addr;
  3959. if (x > (int) crtc->fb->width)
  3960. base = 0;
  3961. if (y > (int) crtc->fb->height)
  3962. base = 0;
  3963. } else
  3964. base = 0;
  3965. if (x < 0) {
  3966. if (x + intel_crtc->cursor_width < 0)
  3967. base = 0;
  3968. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  3969. x = -x;
  3970. }
  3971. pos |= x << CURSOR_X_SHIFT;
  3972. if (y < 0) {
  3973. if (y + intel_crtc->cursor_height < 0)
  3974. base = 0;
  3975. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  3976. y = -y;
  3977. }
  3978. pos |= y << CURSOR_Y_SHIFT;
  3979. visible = base != 0;
  3980. if (!visible && !intel_crtc->cursor_visible)
  3981. return;
  3982. I915_WRITE(pipe == 0 ? CURAPOS : CURBPOS, pos);
  3983. if (IS_845G(dev) || IS_I865G(dev))
  3984. i845_update_cursor(crtc, base);
  3985. else
  3986. i9xx_update_cursor(crtc, base);
  3987. if (visible)
  3988. intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
  3989. }
  3990. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  3991. struct drm_file *file,
  3992. uint32_t handle,
  3993. uint32_t width, uint32_t height)
  3994. {
  3995. struct drm_device *dev = crtc->dev;
  3996. struct drm_i915_private *dev_priv = dev->dev_private;
  3997. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3998. struct drm_i915_gem_object *obj;
  3999. uint32_t addr;
  4000. int ret;
  4001. DRM_DEBUG_KMS("\n");
  4002. /* if we want to turn off the cursor ignore width and height */
  4003. if (!handle) {
  4004. DRM_DEBUG_KMS("cursor off\n");
  4005. addr = 0;
  4006. obj = NULL;
  4007. mutex_lock(&dev->struct_mutex);
  4008. goto finish;
  4009. }
  4010. /* Currently we only support 64x64 cursors */
  4011. if (width != 64 || height != 64) {
  4012. DRM_ERROR("we currently only support 64x64 cursors\n");
  4013. return -EINVAL;
  4014. }
  4015. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  4016. if (!obj)
  4017. return -ENOENT;
  4018. if (obj->base.size < width * height * 4) {
  4019. DRM_ERROR("buffer is to small\n");
  4020. ret = -ENOMEM;
  4021. goto fail;
  4022. }
  4023. /* we only need to pin inside GTT if cursor is non-phy */
  4024. mutex_lock(&dev->struct_mutex);
  4025. if (!dev_priv->info->cursor_needs_physical) {
  4026. if (obj->tiling_mode) {
  4027. DRM_ERROR("cursor cannot be tiled\n");
  4028. ret = -EINVAL;
  4029. goto fail_locked;
  4030. }
  4031. ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
  4032. if (ret) {
  4033. DRM_ERROR("failed to pin cursor bo\n");
  4034. goto fail_locked;
  4035. }
  4036. ret = i915_gem_object_set_to_gtt_domain(obj, 0);
  4037. if (ret) {
  4038. DRM_ERROR("failed to move cursor bo into the GTT\n");
  4039. goto fail_unpin;
  4040. }
  4041. ret = i915_gem_object_put_fence(obj);
  4042. if (ret) {
  4043. DRM_ERROR("failed to move cursor bo into the GTT\n");
  4044. goto fail_unpin;
  4045. }
  4046. addr = obj->gtt_offset;
  4047. } else {
  4048. int align = IS_I830(dev) ? 16 * 1024 : 256;
  4049. ret = i915_gem_attach_phys_object(dev, obj,
  4050. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  4051. align);
  4052. if (ret) {
  4053. DRM_ERROR("failed to attach phys object\n");
  4054. goto fail_locked;
  4055. }
  4056. addr = obj->phys_obj->handle->busaddr;
  4057. }
  4058. if (IS_GEN2(dev))
  4059. I915_WRITE(CURSIZE, (height << 12) | width);
  4060. finish:
  4061. if (intel_crtc->cursor_bo) {
  4062. if (dev_priv->info->cursor_needs_physical) {
  4063. if (intel_crtc->cursor_bo != obj)
  4064. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  4065. } else
  4066. i915_gem_object_unpin(intel_crtc->cursor_bo);
  4067. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  4068. }
  4069. mutex_unlock(&dev->struct_mutex);
  4070. intel_crtc->cursor_addr = addr;
  4071. intel_crtc->cursor_bo = obj;
  4072. intel_crtc->cursor_width = width;
  4073. intel_crtc->cursor_height = height;
  4074. intel_crtc_update_cursor(crtc, true);
  4075. return 0;
  4076. fail_unpin:
  4077. i915_gem_object_unpin(obj);
  4078. fail_locked:
  4079. mutex_unlock(&dev->struct_mutex);
  4080. fail:
  4081. drm_gem_object_unreference_unlocked(&obj->base);
  4082. return ret;
  4083. }
  4084. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  4085. {
  4086. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4087. intel_crtc->cursor_x = x;
  4088. intel_crtc->cursor_y = y;
  4089. intel_crtc_update_cursor(crtc, true);
  4090. return 0;
  4091. }
  4092. /** Sets the color ramps on behalf of RandR */
  4093. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  4094. u16 blue, int regno)
  4095. {
  4096. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4097. intel_crtc->lut_r[regno] = red >> 8;
  4098. intel_crtc->lut_g[regno] = green >> 8;
  4099. intel_crtc->lut_b[regno] = blue >> 8;
  4100. }
  4101. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  4102. u16 *blue, int regno)
  4103. {
  4104. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4105. *red = intel_crtc->lut_r[regno] << 8;
  4106. *green = intel_crtc->lut_g[regno] << 8;
  4107. *blue = intel_crtc->lut_b[regno] << 8;
  4108. }
  4109. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  4110. u16 *blue, uint32_t start, uint32_t size)
  4111. {
  4112. int end = (start + size > 256) ? 256 : start + size, i;
  4113. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4114. for (i = start; i < end; i++) {
  4115. intel_crtc->lut_r[i] = red[i] >> 8;
  4116. intel_crtc->lut_g[i] = green[i] >> 8;
  4117. intel_crtc->lut_b[i] = blue[i] >> 8;
  4118. }
  4119. intel_crtc_load_lut(crtc);
  4120. }
  4121. /**
  4122. * Get a pipe with a simple mode set on it for doing load-based monitor
  4123. * detection.
  4124. *
  4125. * It will be up to the load-detect code to adjust the pipe as appropriate for
  4126. * its requirements. The pipe will be connected to no other encoders.
  4127. *
  4128. * Currently this code will only succeed if there is a pipe with no encoders
  4129. * configured for it. In the future, it could choose to temporarily disable
  4130. * some outputs to free up a pipe for its use.
  4131. *
  4132. * \return crtc, or NULL if no pipes are available.
  4133. */
  4134. /* VESA 640x480x72Hz mode to set on the pipe */
  4135. static struct drm_display_mode load_detect_mode = {
  4136. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  4137. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  4138. };
  4139. struct drm_crtc *intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  4140. struct drm_connector *connector,
  4141. struct drm_display_mode *mode,
  4142. int *dpms_mode)
  4143. {
  4144. struct intel_crtc *intel_crtc;
  4145. struct drm_crtc *possible_crtc;
  4146. struct drm_crtc *supported_crtc =NULL;
  4147. struct drm_encoder *encoder = &intel_encoder->base;
  4148. struct drm_crtc *crtc = NULL;
  4149. struct drm_device *dev = encoder->dev;
  4150. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  4151. struct drm_crtc_helper_funcs *crtc_funcs;
  4152. int i = -1;
  4153. /*
  4154. * Algorithm gets a little messy:
  4155. * - if the connector already has an assigned crtc, use it (but make
  4156. * sure it's on first)
  4157. * - try to find the first unused crtc that can drive this connector,
  4158. * and use that if we find one
  4159. * - if there are no unused crtcs available, try to use the first
  4160. * one we found that supports the connector
  4161. */
  4162. /* See if we already have a CRTC for this connector */
  4163. if (encoder->crtc) {
  4164. crtc = encoder->crtc;
  4165. /* Make sure the crtc and connector are running */
  4166. intel_crtc = to_intel_crtc(crtc);
  4167. *dpms_mode = intel_crtc->dpms_mode;
  4168. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  4169. crtc_funcs = crtc->helper_private;
  4170. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  4171. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  4172. }
  4173. return crtc;
  4174. }
  4175. /* Find an unused one (if possible) */
  4176. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  4177. i++;
  4178. if (!(encoder->possible_crtcs & (1 << i)))
  4179. continue;
  4180. if (!possible_crtc->enabled) {
  4181. crtc = possible_crtc;
  4182. break;
  4183. }
  4184. if (!supported_crtc)
  4185. supported_crtc = possible_crtc;
  4186. }
  4187. /*
  4188. * If we didn't find an unused CRTC, don't use any.
  4189. */
  4190. if (!crtc) {
  4191. return NULL;
  4192. }
  4193. encoder->crtc = crtc;
  4194. connector->encoder = encoder;
  4195. intel_encoder->load_detect_temp = true;
  4196. intel_crtc = to_intel_crtc(crtc);
  4197. *dpms_mode = intel_crtc->dpms_mode;
  4198. if (!crtc->enabled) {
  4199. if (!mode)
  4200. mode = &load_detect_mode;
  4201. drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
  4202. } else {
  4203. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  4204. crtc_funcs = crtc->helper_private;
  4205. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  4206. }
  4207. /* Add this connector to the crtc */
  4208. encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
  4209. encoder_funcs->commit(encoder);
  4210. }
  4211. /* let the connector get through one full cycle before testing */
  4212. intel_wait_for_vblank(dev, intel_crtc->pipe);
  4213. return crtc;
  4214. }
  4215. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  4216. struct drm_connector *connector, int dpms_mode)
  4217. {
  4218. struct drm_encoder *encoder = &intel_encoder->base;
  4219. struct drm_device *dev = encoder->dev;
  4220. struct drm_crtc *crtc = encoder->crtc;
  4221. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  4222. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  4223. if (intel_encoder->load_detect_temp) {
  4224. encoder->crtc = NULL;
  4225. connector->encoder = NULL;
  4226. intel_encoder->load_detect_temp = false;
  4227. crtc->enabled = drm_helper_crtc_in_use(crtc);
  4228. drm_helper_disable_unused_functions(dev);
  4229. }
  4230. /* Switch crtc and encoder back off if necessary */
  4231. if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
  4232. if (encoder->crtc == crtc)
  4233. encoder_funcs->dpms(encoder, dpms_mode);
  4234. crtc_funcs->dpms(crtc, dpms_mode);
  4235. }
  4236. }
  4237. /* Returns the clock of the currently programmed mode of the given pipe. */
  4238. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  4239. {
  4240. struct drm_i915_private *dev_priv = dev->dev_private;
  4241. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4242. int pipe = intel_crtc->pipe;
  4243. u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
  4244. u32 fp;
  4245. intel_clock_t clock;
  4246. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  4247. fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
  4248. else
  4249. fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
  4250. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  4251. if (IS_PINEVIEW(dev)) {
  4252. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  4253. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4254. } else {
  4255. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  4256. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4257. }
  4258. if (!IS_GEN2(dev)) {
  4259. if (IS_PINEVIEW(dev))
  4260. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  4261. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  4262. else
  4263. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  4264. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4265. switch (dpll & DPLL_MODE_MASK) {
  4266. case DPLLB_MODE_DAC_SERIAL:
  4267. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  4268. 5 : 10;
  4269. break;
  4270. case DPLLB_MODE_LVDS:
  4271. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  4272. 7 : 14;
  4273. break;
  4274. default:
  4275. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  4276. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  4277. return 0;
  4278. }
  4279. /* XXX: Handle the 100Mhz refclk */
  4280. intel_clock(dev, 96000, &clock);
  4281. } else {
  4282. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  4283. if (is_lvds) {
  4284. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  4285. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4286. clock.p2 = 14;
  4287. if ((dpll & PLL_REF_INPUT_MASK) ==
  4288. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  4289. /* XXX: might not be 66MHz */
  4290. intel_clock(dev, 66000, &clock);
  4291. } else
  4292. intel_clock(dev, 48000, &clock);
  4293. } else {
  4294. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  4295. clock.p1 = 2;
  4296. else {
  4297. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  4298. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  4299. }
  4300. if (dpll & PLL_P2_DIVIDE_BY_4)
  4301. clock.p2 = 4;
  4302. else
  4303. clock.p2 = 2;
  4304. intel_clock(dev, 48000, &clock);
  4305. }
  4306. }
  4307. /* XXX: It would be nice to validate the clocks, but we can't reuse
  4308. * i830PllIsValid() because it relies on the xf86_config connector
  4309. * configuration being accurate, which it isn't necessarily.
  4310. */
  4311. return clock.dot;
  4312. }
  4313. /** Returns the currently programmed mode of the given pipe. */
  4314. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  4315. struct drm_crtc *crtc)
  4316. {
  4317. struct drm_i915_private *dev_priv = dev->dev_private;
  4318. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4319. int pipe = intel_crtc->pipe;
  4320. struct drm_display_mode *mode;
  4321. int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  4322. int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  4323. int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  4324. int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  4325. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  4326. if (!mode)
  4327. return NULL;
  4328. mode->clock = intel_crtc_clock_get(dev, crtc);
  4329. mode->hdisplay = (htot & 0xffff) + 1;
  4330. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  4331. mode->hsync_start = (hsync & 0xffff) + 1;
  4332. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  4333. mode->vdisplay = (vtot & 0xffff) + 1;
  4334. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  4335. mode->vsync_start = (vsync & 0xffff) + 1;
  4336. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  4337. drm_mode_set_name(mode);
  4338. drm_mode_set_crtcinfo(mode, 0);
  4339. return mode;
  4340. }
  4341. #define GPU_IDLE_TIMEOUT 500 /* ms */
  4342. /* When this timer fires, we've been idle for awhile */
  4343. static void intel_gpu_idle_timer(unsigned long arg)
  4344. {
  4345. struct drm_device *dev = (struct drm_device *)arg;
  4346. drm_i915_private_t *dev_priv = dev->dev_private;
  4347. if (!list_empty(&dev_priv->mm.active_list)) {
  4348. /* Still processing requests, so just re-arm the timer. */
  4349. mod_timer(&dev_priv->idle_timer, jiffies +
  4350. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4351. return;
  4352. }
  4353. dev_priv->busy = false;
  4354. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4355. }
  4356. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  4357. static void intel_crtc_idle_timer(unsigned long arg)
  4358. {
  4359. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  4360. struct drm_crtc *crtc = &intel_crtc->base;
  4361. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  4362. struct intel_framebuffer *intel_fb;
  4363. intel_fb = to_intel_framebuffer(crtc->fb);
  4364. if (intel_fb && intel_fb->obj->active) {
  4365. /* The framebuffer is still being accessed by the GPU. */
  4366. mod_timer(&intel_crtc->idle_timer, jiffies +
  4367. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4368. return;
  4369. }
  4370. intel_crtc->busy = false;
  4371. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4372. }
  4373. static void intel_increase_pllclock(struct drm_crtc *crtc)
  4374. {
  4375. struct drm_device *dev = crtc->dev;
  4376. drm_i915_private_t *dev_priv = dev->dev_private;
  4377. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4378. int pipe = intel_crtc->pipe;
  4379. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  4380. int dpll = I915_READ(dpll_reg);
  4381. if (HAS_PCH_SPLIT(dev))
  4382. return;
  4383. if (!dev_priv->lvds_downclock_avail)
  4384. return;
  4385. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  4386. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  4387. /* Unlock panel regs */
  4388. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  4389. PANEL_UNLOCK_REGS);
  4390. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  4391. I915_WRITE(dpll_reg, dpll);
  4392. dpll = I915_READ(dpll_reg);
  4393. intel_wait_for_vblank(dev, pipe);
  4394. dpll = I915_READ(dpll_reg);
  4395. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  4396. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  4397. /* ...and lock them again */
  4398. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  4399. }
  4400. /* Schedule downclock */
  4401. mod_timer(&intel_crtc->idle_timer, jiffies +
  4402. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4403. }
  4404. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  4405. {
  4406. struct drm_device *dev = crtc->dev;
  4407. drm_i915_private_t *dev_priv = dev->dev_private;
  4408. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4409. int pipe = intel_crtc->pipe;
  4410. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  4411. int dpll = I915_READ(dpll_reg);
  4412. if (HAS_PCH_SPLIT(dev))
  4413. return;
  4414. if (!dev_priv->lvds_downclock_avail)
  4415. return;
  4416. /*
  4417. * Since this is called by a timer, we should never get here in
  4418. * the manual case.
  4419. */
  4420. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  4421. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  4422. /* Unlock panel regs */
  4423. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  4424. PANEL_UNLOCK_REGS);
  4425. dpll |= DISPLAY_RATE_SELECT_FPA1;
  4426. I915_WRITE(dpll_reg, dpll);
  4427. dpll = I915_READ(dpll_reg);
  4428. intel_wait_for_vblank(dev, pipe);
  4429. dpll = I915_READ(dpll_reg);
  4430. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  4431. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  4432. /* ...and lock them again */
  4433. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  4434. }
  4435. }
  4436. /**
  4437. * intel_idle_update - adjust clocks for idleness
  4438. * @work: work struct
  4439. *
  4440. * Either the GPU or display (or both) went idle. Check the busy status
  4441. * here and adjust the CRTC and GPU clocks as necessary.
  4442. */
  4443. static void intel_idle_update(struct work_struct *work)
  4444. {
  4445. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  4446. idle_work);
  4447. struct drm_device *dev = dev_priv->dev;
  4448. struct drm_crtc *crtc;
  4449. struct intel_crtc *intel_crtc;
  4450. int enabled = 0;
  4451. if (!i915_powersave)
  4452. return;
  4453. mutex_lock(&dev->struct_mutex);
  4454. i915_update_gfx_val(dev_priv);
  4455. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4456. /* Skip inactive CRTCs */
  4457. if (!crtc->fb)
  4458. continue;
  4459. enabled++;
  4460. intel_crtc = to_intel_crtc(crtc);
  4461. if (!intel_crtc->busy)
  4462. intel_decrease_pllclock(crtc);
  4463. }
  4464. if ((enabled == 1) && (IS_I945G(dev) || IS_I945GM(dev))) {
  4465. DRM_DEBUG_DRIVER("enable memory self refresh on 945\n");
  4466. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  4467. }
  4468. mutex_unlock(&dev->struct_mutex);
  4469. }
  4470. /**
  4471. * intel_mark_busy - mark the GPU and possibly the display busy
  4472. * @dev: drm device
  4473. * @obj: object we're operating on
  4474. *
  4475. * Callers can use this function to indicate that the GPU is busy processing
  4476. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  4477. * buffer), we'll also mark the display as busy, so we know to increase its
  4478. * clock frequency.
  4479. */
  4480. void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
  4481. {
  4482. drm_i915_private_t *dev_priv = dev->dev_private;
  4483. struct drm_crtc *crtc = NULL;
  4484. struct intel_framebuffer *intel_fb;
  4485. struct intel_crtc *intel_crtc;
  4486. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4487. return;
  4488. if (!dev_priv->busy) {
  4489. if (IS_I945G(dev) || IS_I945GM(dev)) {
  4490. u32 fw_blc_self;
  4491. DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
  4492. fw_blc_self = I915_READ(FW_BLC_SELF);
  4493. fw_blc_self &= ~FW_BLC_SELF_EN;
  4494. I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
  4495. }
  4496. dev_priv->busy = true;
  4497. } else
  4498. mod_timer(&dev_priv->idle_timer, jiffies +
  4499. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4500. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4501. if (!crtc->fb)
  4502. continue;
  4503. intel_crtc = to_intel_crtc(crtc);
  4504. intel_fb = to_intel_framebuffer(crtc->fb);
  4505. if (intel_fb->obj == obj) {
  4506. if (!intel_crtc->busy) {
  4507. if (IS_I945G(dev) || IS_I945GM(dev)) {
  4508. u32 fw_blc_self;
  4509. DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
  4510. fw_blc_self = I915_READ(FW_BLC_SELF);
  4511. fw_blc_self &= ~FW_BLC_SELF_EN;
  4512. I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
  4513. }
  4514. /* Non-busy -> busy, upclock */
  4515. intel_increase_pllclock(crtc);
  4516. intel_crtc->busy = true;
  4517. } else {
  4518. /* Busy -> busy, put off timer */
  4519. mod_timer(&intel_crtc->idle_timer, jiffies +
  4520. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4521. }
  4522. }
  4523. }
  4524. }
  4525. static void intel_crtc_destroy(struct drm_crtc *crtc)
  4526. {
  4527. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4528. struct drm_device *dev = crtc->dev;
  4529. struct intel_unpin_work *work;
  4530. unsigned long flags;
  4531. spin_lock_irqsave(&dev->event_lock, flags);
  4532. work = intel_crtc->unpin_work;
  4533. intel_crtc->unpin_work = NULL;
  4534. spin_unlock_irqrestore(&dev->event_lock, flags);
  4535. if (work) {
  4536. cancel_work_sync(&work->work);
  4537. kfree(work);
  4538. }
  4539. drm_crtc_cleanup(crtc);
  4540. kfree(intel_crtc);
  4541. }
  4542. static void intel_unpin_work_fn(struct work_struct *__work)
  4543. {
  4544. struct intel_unpin_work *work =
  4545. container_of(__work, struct intel_unpin_work, work);
  4546. mutex_lock(&work->dev->struct_mutex);
  4547. i915_gem_object_unpin(work->old_fb_obj);
  4548. drm_gem_object_unreference(&work->pending_flip_obj->base);
  4549. drm_gem_object_unreference(&work->old_fb_obj->base);
  4550. mutex_unlock(&work->dev->struct_mutex);
  4551. kfree(work);
  4552. }
  4553. static void do_intel_finish_page_flip(struct drm_device *dev,
  4554. struct drm_crtc *crtc)
  4555. {
  4556. drm_i915_private_t *dev_priv = dev->dev_private;
  4557. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4558. struct intel_unpin_work *work;
  4559. struct drm_i915_gem_object *obj;
  4560. struct drm_pending_vblank_event *e;
  4561. struct timeval tnow, tvbl;
  4562. unsigned long flags;
  4563. /* Ignore early vblank irqs */
  4564. if (intel_crtc == NULL)
  4565. return;
  4566. do_gettimeofday(&tnow);
  4567. spin_lock_irqsave(&dev->event_lock, flags);
  4568. work = intel_crtc->unpin_work;
  4569. if (work == NULL || !work->pending) {
  4570. spin_unlock_irqrestore(&dev->event_lock, flags);
  4571. return;
  4572. }
  4573. intel_crtc->unpin_work = NULL;
  4574. if (work->event) {
  4575. e = work->event;
  4576. e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
  4577. /* Called before vblank count and timestamps have
  4578. * been updated for the vblank interval of flip
  4579. * completion? Need to increment vblank count and
  4580. * add one videorefresh duration to returned timestamp
  4581. * to account for this. We assume this happened if we
  4582. * get called over 0.9 frame durations after the last
  4583. * timestamped vblank.
  4584. *
  4585. * This calculation can not be used with vrefresh rates
  4586. * below 5Hz (10Hz to be on the safe side) without
  4587. * promoting to 64 integers.
  4588. */
  4589. if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
  4590. 9 * crtc->framedur_ns) {
  4591. e->event.sequence++;
  4592. tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
  4593. crtc->framedur_ns);
  4594. }
  4595. e->event.tv_sec = tvbl.tv_sec;
  4596. e->event.tv_usec = tvbl.tv_usec;
  4597. list_add_tail(&e->base.link,
  4598. &e->base.file_priv->event_list);
  4599. wake_up_interruptible(&e->base.file_priv->event_wait);
  4600. }
  4601. drm_vblank_put(dev, intel_crtc->pipe);
  4602. spin_unlock_irqrestore(&dev->event_lock, flags);
  4603. obj = work->old_fb_obj;
  4604. atomic_clear_mask(1 << intel_crtc->plane,
  4605. &obj->pending_flip.counter);
  4606. if (atomic_read(&obj->pending_flip) == 0)
  4607. wake_up(&dev_priv->pending_flip_queue);
  4608. schedule_work(&work->work);
  4609. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  4610. }
  4611. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  4612. {
  4613. drm_i915_private_t *dev_priv = dev->dev_private;
  4614. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  4615. do_intel_finish_page_flip(dev, crtc);
  4616. }
  4617. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  4618. {
  4619. drm_i915_private_t *dev_priv = dev->dev_private;
  4620. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  4621. do_intel_finish_page_flip(dev, crtc);
  4622. }
  4623. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  4624. {
  4625. drm_i915_private_t *dev_priv = dev->dev_private;
  4626. struct intel_crtc *intel_crtc =
  4627. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  4628. unsigned long flags;
  4629. spin_lock_irqsave(&dev->event_lock, flags);
  4630. if (intel_crtc->unpin_work) {
  4631. if ((++intel_crtc->unpin_work->pending) > 1)
  4632. DRM_ERROR("Prepared flip multiple times\n");
  4633. } else {
  4634. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  4635. }
  4636. spin_unlock_irqrestore(&dev->event_lock, flags);
  4637. }
  4638. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  4639. struct drm_framebuffer *fb,
  4640. struct drm_pending_vblank_event *event)
  4641. {
  4642. struct drm_device *dev = crtc->dev;
  4643. struct drm_i915_private *dev_priv = dev->dev_private;
  4644. struct intel_framebuffer *intel_fb;
  4645. struct drm_i915_gem_object *obj;
  4646. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4647. struct intel_unpin_work *work;
  4648. unsigned long flags, offset;
  4649. int pipe = intel_crtc->pipe;
  4650. u32 pf, pipesrc;
  4651. int ret;
  4652. work = kzalloc(sizeof *work, GFP_KERNEL);
  4653. if (work == NULL)
  4654. return -ENOMEM;
  4655. work->event = event;
  4656. work->dev = crtc->dev;
  4657. intel_fb = to_intel_framebuffer(crtc->fb);
  4658. work->old_fb_obj = intel_fb->obj;
  4659. INIT_WORK(&work->work, intel_unpin_work_fn);
  4660. /* We borrow the event spin lock for protecting unpin_work */
  4661. spin_lock_irqsave(&dev->event_lock, flags);
  4662. if (intel_crtc->unpin_work) {
  4663. spin_unlock_irqrestore(&dev->event_lock, flags);
  4664. kfree(work);
  4665. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  4666. return -EBUSY;
  4667. }
  4668. intel_crtc->unpin_work = work;
  4669. spin_unlock_irqrestore(&dev->event_lock, flags);
  4670. intel_fb = to_intel_framebuffer(fb);
  4671. obj = intel_fb->obj;
  4672. mutex_lock(&dev->struct_mutex);
  4673. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  4674. if (ret)
  4675. goto cleanup_work;
  4676. /* Reference the objects for the scheduled work. */
  4677. drm_gem_object_reference(&work->old_fb_obj->base);
  4678. drm_gem_object_reference(&obj->base);
  4679. crtc->fb = fb;
  4680. ret = drm_vblank_get(dev, intel_crtc->pipe);
  4681. if (ret)
  4682. goto cleanup_objs;
  4683. if (IS_GEN3(dev) || IS_GEN2(dev)) {
  4684. u32 flip_mask;
  4685. /* Can't queue multiple flips, so wait for the previous
  4686. * one to finish before executing the next.
  4687. */
  4688. ret = BEGIN_LP_RING(2);
  4689. if (ret)
  4690. goto cleanup_objs;
  4691. if (intel_crtc->plane)
  4692. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  4693. else
  4694. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  4695. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  4696. OUT_RING(MI_NOOP);
  4697. ADVANCE_LP_RING();
  4698. }
  4699. work->pending_flip_obj = obj;
  4700. work->enable_stall_check = true;
  4701. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  4702. offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
  4703. ret = BEGIN_LP_RING(4);
  4704. if (ret)
  4705. goto cleanup_objs;
  4706. /* Block clients from rendering to the new back buffer until
  4707. * the flip occurs and the object is no longer visible.
  4708. */
  4709. atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  4710. switch (INTEL_INFO(dev)->gen) {
  4711. case 2:
  4712. OUT_RING(MI_DISPLAY_FLIP |
  4713. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4714. OUT_RING(fb->pitch);
  4715. OUT_RING(obj->gtt_offset + offset);
  4716. OUT_RING(MI_NOOP);
  4717. break;
  4718. case 3:
  4719. OUT_RING(MI_DISPLAY_FLIP_I915 |
  4720. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4721. OUT_RING(fb->pitch);
  4722. OUT_RING(obj->gtt_offset + offset);
  4723. OUT_RING(MI_NOOP);
  4724. break;
  4725. case 4:
  4726. case 5:
  4727. /* i965+ uses the linear or tiled offsets from the
  4728. * Display Registers (which do not change across a page-flip)
  4729. * so we need only reprogram the base address.
  4730. */
  4731. OUT_RING(MI_DISPLAY_FLIP |
  4732. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4733. OUT_RING(fb->pitch);
  4734. OUT_RING(obj->gtt_offset | obj->tiling_mode);
  4735. /* XXX Enabling the panel-fitter across page-flip is so far
  4736. * untested on non-native modes, so ignore it for now.
  4737. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  4738. */
  4739. pf = 0;
  4740. pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
  4741. OUT_RING(pf | pipesrc);
  4742. break;
  4743. case 6:
  4744. OUT_RING(MI_DISPLAY_FLIP |
  4745. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4746. OUT_RING(fb->pitch | obj->tiling_mode);
  4747. OUT_RING(obj->gtt_offset);
  4748. pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  4749. pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
  4750. OUT_RING(pf | pipesrc);
  4751. break;
  4752. }
  4753. ADVANCE_LP_RING();
  4754. mutex_unlock(&dev->struct_mutex);
  4755. trace_i915_flip_request(intel_crtc->plane, obj);
  4756. return 0;
  4757. cleanup_objs:
  4758. drm_gem_object_unreference(&work->old_fb_obj->base);
  4759. drm_gem_object_unreference(&obj->base);
  4760. cleanup_work:
  4761. mutex_unlock(&dev->struct_mutex);
  4762. spin_lock_irqsave(&dev->event_lock, flags);
  4763. intel_crtc->unpin_work = NULL;
  4764. spin_unlock_irqrestore(&dev->event_lock, flags);
  4765. kfree(work);
  4766. return ret;
  4767. }
  4768. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  4769. .dpms = intel_crtc_dpms,
  4770. .mode_fixup = intel_crtc_mode_fixup,
  4771. .mode_set = intel_crtc_mode_set,
  4772. .mode_set_base = intel_pipe_set_base,
  4773. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  4774. .load_lut = intel_crtc_load_lut,
  4775. .disable = intel_crtc_disable,
  4776. };
  4777. static const struct drm_crtc_funcs intel_crtc_funcs = {
  4778. .cursor_set = intel_crtc_cursor_set,
  4779. .cursor_move = intel_crtc_cursor_move,
  4780. .gamma_set = intel_crtc_gamma_set,
  4781. .set_config = drm_crtc_helper_set_config,
  4782. .destroy = intel_crtc_destroy,
  4783. .page_flip = intel_crtc_page_flip,
  4784. };
  4785. static void intel_sanitize_modesetting(struct drm_device *dev,
  4786. int pipe, int plane)
  4787. {
  4788. struct drm_i915_private *dev_priv = dev->dev_private;
  4789. u32 reg, val;
  4790. if (HAS_PCH_SPLIT(dev))
  4791. return;
  4792. /* Who knows what state these registers were left in by the BIOS or
  4793. * grub?
  4794. *
  4795. * If we leave the registers in a conflicting state (e.g. with the
  4796. * display plane reading from the other pipe than the one we intend
  4797. * to use) then when we attempt to teardown the active mode, we will
  4798. * not disable the pipes and planes in the correct order -- leaving
  4799. * a plane reading from a disabled pipe and possibly leading to
  4800. * undefined behaviour.
  4801. */
  4802. reg = DSPCNTR(plane);
  4803. val = I915_READ(reg);
  4804. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  4805. return;
  4806. if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
  4807. return;
  4808. /* This display plane is active and attached to the other CPU pipe. */
  4809. pipe = !pipe;
  4810. /* Disable the plane and wait for it to stop reading from the pipe. */
  4811. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  4812. intel_flush_display_plane(dev, plane);
  4813. if (IS_GEN2(dev))
  4814. intel_wait_for_vblank(dev, pipe);
  4815. if (pipe == 0 && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  4816. return;
  4817. /* Switch off the pipe. */
  4818. reg = PIPECONF(pipe);
  4819. val = I915_READ(reg);
  4820. if (val & PIPECONF_ENABLE) {
  4821. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  4822. intel_wait_for_pipe_off(dev, pipe);
  4823. }
  4824. }
  4825. static void intel_crtc_init(struct drm_device *dev, int pipe)
  4826. {
  4827. drm_i915_private_t *dev_priv = dev->dev_private;
  4828. struct intel_crtc *intel_crtc;
  4829. int i;
  4830. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  4831. if (intel_crtc == NULL)
  4832. return;
  4833. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  4834. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  4835. for (i = 0; i < 256; i++) {
  4836. intel_crtc->lut_r[i] = i;
  4837. intel_crtc->lut_g[i] = i;
  4838. intel_crtc->lut_b[i] = i;
  4839. }
  4840. /* Swap pipes & planes for FBC on pre-965 */
  4841. intel_crtc->pipe = pipe;
  4842. intel_crtc->plane = pipe;
  4843. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  4844. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  4845. intel_crtc->plane = !pipe;
  4846. }
  4847. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  4848. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  4849. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  4850. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  4851. intel_crtc->cursor_addr = 0;
  4852. intel_crtc->dpms_mode = -1;
  4853. intel_crtc->active = true; /* force the pipe off on setup_init_config */
  4854. if (HAS_PCH_SPLIT(dev)) {
  4855. intel_helper_funcs.prepare = ironlake_crtc_prepare;
  4856. intel_helper_funcs.commit = ironlake_crtc_commit;
  4857. } else {
  4858. intel_helper_funcs.prepare = i9xx_crtc_prepare;
  4859. intel_helper_funcs.commit = i9xx_crtc_commit;
  4860. }
  4861. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  4862. intel_crtc->busy = false;
  4863. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  4864. (unsigned long)intel_crtc);
  4865. intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
  4866. }
  4867. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  4868. struct drm_file *file)
  4869. {
  4870. drm_i915_private_t *dev_priv = dev->dev_private;
  4871. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  4872. struct drm_mode_object *drmmode_obj;
  4873. struct intel_crtc *crtc;
  4874. if (!dev_priv) {
  4875. DRM_ERROR("called with no initialization\n");
  4876. return -EINVAL;
  4877. }
  4878. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  4879. DRM_MODE_OBJECT_CRTC);
  4880. if (!drmmode_obj) {
  4881. DRM_ERROR("no such CRTC id\n");
  4882. return -EINVAL;
  4883. }
  4884. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  4885. pipe_from_crtc_id->pipe = crtc->pipe;
  4886. return 0;
  4887. }
  4888. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  4889. {
  4890. struct intel_encoder *encoder;
  4891. int index_mask = 0;
  4892. int entry = 0;
  4893. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  4894. if (type_mask & encoder->clone_mask)
  4895. index_mask |= (1 << entry);
  4896. entry++;
  4897. }
  4898. return index_mask;
  4899. }
  4900. static bool has_edp_a(struct drm_device *dev)
  4901. {
  4902. struct drm_i915_private *dev_priv = dev->dev_private;
  4903. if (!IS_MOBILE(dev))
  4904. return false;
  4905. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  4906. return false;
  4907. if (IS_GEN5(dev) &&
  4908. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  4909. return false;
  4910. return true;
  4911. }
  4912. static void intel_setup_outputs(struct drm_device *dev)
  4913. {
  4914. struct drm_i915_private *dev_priv = dev->dev_private;
  4915. struct intel_encoder *encoder;
  4916. bool dpd_is_edp = false;
  4917. bool has_lvds = false;
  4918. if (IS_MOBILE(dev) && !IS_I830(dev))
  4919. has_lvds = intel_lvds_init(dev);
  4920. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  4921. /* disable the panel fitter on everything but LVDS */
  4922. I915_WRITE(PFIT_CONTROL, 0);
  4923. }
  4924. if (HAS_PCH_SPLIT(dev)) {
  4925. dpd_is_edp = intel_dpd_is_edp(dev);
  4926. if (has_edp_a(dev))
  4927. intel_dp_init(dev, DP_A);
  4928. if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  4929. intel_dp_init(dev, PCH_DP_D);
  4930. }
  4931. intel_crt_init(dev);
  4932. if (HAS_PCH_SPLIT(dev)) {
  4933. int found;
  4934. if (I915_READ(HDMIB) & PORT_DETECTED) {
  4935. /* PCH SDVOB multiplex with HDMIB */
  4936. found = intel_sdvo_init(dev, PCH_SDVOB);
  4937. if (!found)
  4938. intel_hdmi_init(dev, HDMIB);
  4939. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  4940. intel_dp_init(dev, PCH_DP_B);
  4941. }
  4942. if (I915_READ(HDMIC) & PORT_DETECTED)
  4943. intel_hdmi_init(dev, HDMIC);
  4944. if (I915_READ(HDMID) & PORT_DETECTED)
  4945. intel_hdmi_init(dev, HDMID);
  4946. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  4947. intel_dp_init(dev, PCH_DP_C);
  4948. if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  4949. intel_dp_init(dev, PCH_DP_D);
  4950. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  4951. bool found = false;
  4952. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  4953. DRM_DEBUG_KMS("probing SDVOB\n");
  4954. found = intel_sdvo_init(dev, SDVOB);
  4955. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  4956. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  4957. intel_hdmi_init(dev, SDVOB);
  4958. }
  4959. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  4960. DRM_DEBUG_KMS("probing DP_B\n");
  4961. intel_dp_init(dev, DP_B);
  4962. }
  4963. }
  4964. /* Before G4X SDVOC doesn't have its own detect register */
  4965. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  4966. DRM_DEBUG_KMS("probing SDVOC\n");
  4967. found = intel_sdvo_init(dev, SDVOC);
  4968. }
  4969. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  4970. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  4971. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  4972. intel_hdmi_init(dev, SDVOC);
  4973. }
  4974. if (SUPPORTS_INTEGRATED_DP(dev)) {
  4975. DRM_DEBUG_KMS("probing DP_C\n");
  4976. intel_dp_init(dev, DP_C);
  4977. }
  4978. }
  4979. if (SUPPORTS_INTEGRATED_DP(dev) &&
  4980. (I915_READ(DP_D) & DP_DETECTED)) {
  4981. DRM_DEBUG_KMS("probing DP_D\n");
  4982. intel_dp_init(dev, DP_D);
  4983. }
  4984. } else if (IS_GEN2(dev))
  4985. intel_dvo_init(dev);
  4986. if (SUPPORTS_TV(dev))
  4987. intel_tv_init(dev);
  4988. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  4989. encoder->base.possible_crtcs = encoder->crtc_mask;
  4990. encoder->base.possible_clones =
  4991. intel_encoder_clones(dev, encoder->clone_mask);
  4992. }
  4993. intel_panel_setup_backlight(dev);
  4994. }
  4995. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  4996. {
  4997. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  4998. drm_framebuffer_cleanup(fb);
  4999. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  5000. kfree(intel_fb);
  5001. }
  5002. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  5003. struct drm_file *file,
  5004. unsigned int *handle)
  5005. {
  5006. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  5007. struct drm_i915_gem_object *obj = intel_fb->obj;
  5008. return drm_gem_handle_create(file, &obj->base, handle);
  5009. }
  5010. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  5011. .destroy = intel_user_framebuffer_destroy,
  5012. .create_handle = intel_user_framebuffer_create_handle,
  5013. };
  5014. int intel_framebuffer_init(struct drm_device *dev,
  5015. struct intel_framebuffer *intel_fb,
  5016. struct drm_mode_fb_cmd *mode_cmd,
  5017. struct drm_i915_gem_object *obj)
  5018. {
  5019. int ret;
  5020. if (obj->tiling_mode == I915_TILING_Y)
  5021. return -EINVAL;
  5022. if (mode_cmd->pitch & 63)
  5023. return -EINVAL;
  5024. switch (mode_cmd->bpp) {
  5025. case 8:
  5026. case 16:
  5027. case 24:
  5028. case 32:
  5029. break;
  5030. default:
  5031. return -EINVAL;
  5032. }
  5033. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  5034. if (ret) {
  5035. DRM_ERROR("framebuffer init failed %d\n", ret);
  5036. return ret;
  5037. }
  5038. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  5039. intel_fb->obj = obj;
  5040. return 0;
  5041. }
  5042. static struct drm_framebuffer *
  5043. intel_user_framebuffer_create(struct drm_device *dev,
  5044. struct drm_file *filp,
  5045. struct drm_mode_fb_cmd *mode_cmd)
  5046. {
  5047. struct drm_i915_gem_object *obj;
  5048. struct intel_framebuffer *intel_fb;
  5049. int ret;
  5050. obj = to_intel_bo(drm_gem_object_lookup(dev, filp, mode_cmd->handle));
  5051. if (!obj)
  5052. return ERR_PTR(-ENOENT);
  5053. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5054. if (!intel_fb)
  5055. return ERR_PTR(-ENOMEM);
  5056. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  5057. if (ret) {
  5058. drm_gem_object_unreference_unlocked(&obj->base);
  5059. kfree(intel_fb);
  5060. return ERR_PTR(ret);
  5061. }
  5062. return &intel_fb->base;
  5063. }
  5064. static const struct drm_mode_config_funcs intel_mode_funcs = {
  5065. .fb_create = intel_user_framebuffer_create,
  5066. .output_poll_changed = intel_fb_output_poll_changed,
  5067. };
  5068. static struct drm_i915_gem_object *
  5069. intel_alloc_context_page(struct drm_device *dev)
  5070. {
  5071. struct drm_i915_gem_object *ctx;
  5072. int ret;
  5073. ctx = i915_gem_alloc_object(dev, 4096);
  5074. if (!ctx) {
  5075. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  5076. return NULL;
  5077. }
  5078. mutex_lock(&dev->struct_mutex);
  5079. ret = i915_gem_object_pin(ctx, 4096, true);
  5080. if (ret) {
  5081. DRM_ERROR("failed to pin power context: %d\n", ret);
  5082. goto err_unref;
  5083. }
  5084. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  5085. if (ret) {
  5086. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  5087. goto err_unpin;
  5088. }
  5089. mutex_unlock(&dev->struct_mutex);
  5090. return ctx;
  5091. err_unpin:
  5092. i915_gem_object_unpin(ctx);
  5093. err_unref:
  5094. drm_gem_object_unreference(&ctx->base);
  5095. mutex_unlock(&dev->struct_mutex);
  5096. return NULL;
  5097. }
  5098. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  5099. {
  5100. struct drm_i915_private *dev_priv = dev->dev_private;
  5101. u16 rgvswctl;
  5102. rgvswctl = I915_READ16(MEMSWCTL);
  5103. if (rgvswctl & MEMCTL_CMD_STS) {
  5104. DRM_DEBUG("gpu busy, RCS change rejected\n");
  5105. return false; /* still busy with another command */
  5106. }
  5107. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  5108. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  5109. I915_WRITE16(MEMSWCTL, rgvswctl);
  5110. POSTING_READ16(MEMSWCTL);
  5111. rgvswctl |= MEMCTL_CMD_STS;
  5112. I915_WRITE16(MEMSWCTL, rgvswctl);
  5113. return true;
  5114. }
  5115. void ironlake_enable_drps(struct drm_device *dev)
  5116. {
  5117. struct drm_i915_private *dev_priv = dev->dev_private;
  5118. u32 rgvmodectl = I915_READ(MEMMODECTL);
  5119. u8 fmax, fmin, fstart, vstart;
  5120. /* Enable temp reporting */
  5121. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  5122. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  5123. /* 100ms RC evaluation intervals */
  5124. I915_WRITE(RCUPEI, 100000);
  5125. I915_WRITE(RCDNEI, 100000);
  5126. /* Set max/min thresholds to 90ms and 80ms respectively */
  5127. I915_WRITE(RCBMAXAVG, 90000);
  5128. I915_WRITE(RCBMINAVG, 80000);
  5129. I915_WRITE(MEMIHYST, 1);
  5130. /* Set up min, max, and cur for interrupt handling */
  5131. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  5132. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  5133. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  5134. MEMMODE_FSTART_SHIFT;
  5135. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  5136. PXVFREQ_PX_SHIFT;
  5137. dev_priv->fmax = fmax; /* IPS callback will increase this */
  5138. dev_priv->fstart = fstart;
  5139. dev_priv->max_delay = fstart;
  5140. dev_priv->min_delay = fmin;
  5141. dev_priv->cur_delay = fstart;
  5142. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  5143. fmax, fmin, fstart);
  5144. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  5145. /*
  5146. * Interrupts will be enabled in ironlake_irq_postinstall
  5147. */
  5148. I915_WRITE(VIDSTART, vstart);
  5149. POSTING_READ(VIDSTART);
  5150. rgvmodectl |= MEMMODE_SWMODE_EN;
  5151. I915_WRITE(MEMMODECTL, rgvmodectl);
  5152. if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  5153. DRM_ERROR("stuck trying to change perf mode\n");
  5154. msleep(1);
  5155. ironlake_set_drps(dev, fstart);
  5156. dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  5157. I915_READ(0x112e0);
  5158. dev_priv->last_time1 = jiffies_to_msecs(jiffies);
  5159. dev_priv->last_count2 = I915_READ(0x112f4);
  5160. getrawmonotonic(&dev_priv->last_time2);
  5161. }
  5162. void ironlake_disable_drps(struct drm_device *dev)
  5163. {
  5164. struct drm_i915_private *dev_priv = dev->dev_private;
  5165. u16 rgvswctl = I915_READ16(MEMSWCTL);
  5166. /* Ack interrupts, disable EFC interrupt */
  5167. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  5168. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  5169. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  5170. I915_WRITE(DEIIR, DE_PCU_EVENT);
  5171. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  5172. /* Go back to the starting frequency */
  5173. ironlake_set_drps(dev, dev_priv->fstart);
  5174. msleep(1);
  5175. rgvswctl |= MEMCTL_CMD_STS;
  5176. I915_WRITE(MEMSWCTL, rgvswctl);
  5177. msleep(1);
  5178. }
  5179. void gen6_set_rps(struct drm_device *dev, u8 val)
  5180. {
  5181. struct drm_i915_private *dev_priv = dev->dev_private;
  5182. u32 swreq;
  5183. swreq = (val & 0x3ff) << 25;
  5184. I915_WRITE(GEN6_RPNSWREQ, swreq);
  5185. }
  5186. void gen6_disable_rps(struct drm_device *dev)
  5187. {
  5188. struct drm_i915_private *dev_priv = dev->dev_private;
  5189. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  5190. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  5191. I915_WRITE(GEN6_PMIER, 0);
  5192. I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
  5193. }
  5194. static unsigned long intel_pxfreq(u32 vidfreq)
  5195. {
  5196. unsigned long freq;
  5197. int div = (vidfreq & 0x3f0000) >> 16;
  5198. int post = (vidfreq & 0x3000) >> 12;
  5199. int pre = (vidfreq & 0x7);
  5200. if (!pre)
  5201. return 0;
  5202. freq = ((div * 133333) / ((1<<post) * pre));
  5203. return freq;
  5204. }
  5205. void intel_init_emon(struct drm_device *dev)
  5206. {
  5207. struct drm_i915_private *dev_priv = dev->dev_private;
  5208. u32 lcfuse;
  5209. u8 pxw[16];
  5210. int i;
  5211. /* Disable to program */
  5212. I915_WRITE(ECR, 0);
  5213. POSTING_READ(ECR);
  5214. /* Program energy weights for various events */
  5215. I915_WRITE(SDEW, 0x15040d00);
  5216. I915_WRITE(CSIEW0, 0x007f0000);
  5217. I915_WRITE(CSIEW1, 0x1e220004);
  5218. I915_WRITE(CSIEW2, 0x04000004);
  5219. for (i = 0; i < 5; i++)
  5220. I915_WRITE(PEW + (i * 4), 0);
  5221. for (i = 0; i < 3; i++)
  5222. I915_WRITE(DEW + (i * 4), 0);
  5223. /* Program P-state weights to account for frequency power adjustment */
  5224. for (i = 0; i < 16; i++) {
  5225. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  5226. unsigned long freq = intel_pxfreq(pxvidfreq);
  5227. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  5228. PXVFREQ_PX_SHIFT;
  5229. unsigned long val;
  5230. val = vid * vid;
  5231. val *= (freq / 1000);
  5232. val *= 255;
  5233. val /= (127*127*900);
  5234. if (val > 0xff)
  5235. DRM_ERROR("bad pxval: %ld\n", val);
  5236. pxw[i] = val;
  5237. }
  5238. /* Render standby states get 0 weight */
  5239. pxw[14] = 0;
  5240. pxw[15] = 0;
  5241. for (i = 0; i < 4; i++) {
  5242. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  5243. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  5244. I915_WRITE(PXW + (i * 4), val);
  5245. }
  5246. /* Adjust magic regs to magic values (more experimental results) */
  5247. I915_WRITE(OGW0, 0);
  5248. I915_WRITE(OGW1, 0);
  5249. I915_WRITE(EG0, 0x00007f00);
  5250. I915_WRITE(EG1, 0x0000000e);
  5251. I915_WRITE(EG2, 0x000e0000);
  5252. I915_WRITE(EG3, 0x68000300);
  5253. I915_WRITE(EG4, 0x42000000);
  5254. I915_WRITE(EG5, 0x00140031);
  5255. I915_WRITE(EG6, 0);
  5256. I915_WRITE(EG7, 0);
  5257. for (i = 0; i < 8; i++)
  5258. I915_WRITE(PXWL + (i * 4), 0);
  5259. /* Enable PMON + select events */
  5260. I915_WRITE(ECR, 0x80000019);
  5261. lcfuse = I915_READ(LCFUSE02);
  5262. dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
  5263. }
  5264. void gen6_enable_rps(struct drm_i915_private *dev_priv)
  5265. {
  5266. u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  5267. u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
  5268. u32 pcu_mbox;
  5269. int cur_freq, min_freq, max_freq;
  5270. int i;
  5271. /* Here begins a magic sequence of register writes to enable
  5272. * auto-downclocking.
  5273. *
  5274. * Perhaps there might be some value in exposing these to
  5275. * userspace...
  5276. */
  5277. I915_WRITE(GEN6_RC_STATE, 0);
  5278. __gen6_force_wake_get(dev_priv);
  5279. /* disable the counters and set deterministic thresholds */
  5280. I915_WRITE(GEN6_RC_CONTROL, 0);
  5281. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  5282. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  5283. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  5284. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  5285. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  5286. for (i = 0; i < I915_NUM_RINGS; i++)
  5287. I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
  5288. I915_WRITE(GEN6_RC_SLEEP, 0);
  5289. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  5290. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  5291. I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
  5292. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  5293. I915_WRITE(GEN6_RC_CONTROL,
  5294. GEN6_RC_CTL_RC6p_ENABLE |
  5295. GEN6_RC_CTL_RC6_ENABLE |
  5296. GEN6_RC_CTL_EI_MODE(1) |
  5297. GEN6_RC_CTL_HW_ENABLE);
  5298. I915_WRITE(GEN6_RPNSWREQ,
  5299. GEN6_FREQUENCY(10) |
  5300. GEN6_OFFSET(0) |
  5301. GEN6_AGGRESSIVE_TURBO);
  5302. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  5303. GEN6_FREQUENCY(12));
  5304. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  5305. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  5306. 18 << 24 |
  5307. 6 << 16);
  5308. I915_WRITE(GEN6_RP_UP_THRESHOLD, 90000);
  5309. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 100000);
  5310. I915_WRITE(GEN6_RP_UP_EI, 100000);
  5311. I915_WRITE(GEN6_RP_DOWN_EI, 300000);
  5312. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  5313. I915_WRITE(GEN6_RP_CONTROL,
  5314. GEN6_RP_MEDIA_TURBO |
  5315. GEN6_RP_USE_NORMAL_FREQ |
  5316. GEN6_RP_MEDIA_IS_GFX |
  5317. GEN6_RP_ENABLE |
  5318. GEN6_RP_UP_BUSY_MAX |
  5319. GEN6_RP_DOWN_BUSY_MIN);
  5320. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5321. 500))
  5322. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  5323. I915_WRITE(GEN6_PCODE_DATA, 0);
  5324. I915_WRITE(GEN6_PCODE_MAILBOX,
  5325. GEN6_PCODE_READY |
  5326. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  5327. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5328. 500))
  5329. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  5330. min_freq = (rp_state_cap & 0xff0000) >> 16;
  5331. max_freq = rp_state_cap & 0xff;
  5332. cur_freq = (gt_perf_status & 0xff00) >> 8;
  5333. /* Check for overclock support */
  5334. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5335. 500))
  5336. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  5337. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
  5338. pcu_mbox = I915_READ(GEN6_PCODE_DATA);
  5339. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5340. 500))
  5341. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  5342. if (pcu_mbox & (1<<31)) { /* OC supported */
  5343. max_freq = pcu_mbox & 0xff;
  5344. DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 100);
  5345. }
  5346. /* In units of 100MHz */
  5347. dev_priv->max_delay = max_freq;
  5348. dev_priv->min_delay = min_freq;
  5349. dev_priv->cur_delay = cur_freq;
  5350. /* requires MSI enabled */
  5351. I915_WRITE(GEN6_PMIER,
  5352. GEN6_PM_MBOX_EVENT |
  5353. GEN6_PM_THERMAL_EVENT |
  5354. GEN6_PM_RP_DOWN_TIMEOUT |
  5355. GEN6_PM_RP_UP_THRESHOLD |
  5356. GEN6_PM_RP_DOWN_THRESHOLD |
  5357. GEN6_PM_RP_UP_EI_EXPIRED |
  5358. GEN6_PM_RP_DOWN_EI_EXPIRED);
  5359. I915_WRITE(GEN6_PMIMR, 0);
  5360. /* enable all PM interrupts */
  5361. I915_WRITE(GEN6_PMINTRMSK, 0);
  5362. __gen6_force_wake_put(dev_priv);
  5363. }
  5364. void intel_enable_clock_gating(struct drm_device *dev)
  5365. {
  5366. struct drm_i915_private *dev_priv = dev->dev_private;
  5367. /*
  5368. * Disable clock gating reported to work incorrectly according to the
  5369. * specs, but enable as much else as we can.
  5370. */
  5371. if (HAS_PCH_SPLIT(dev)) {
  5372. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  5373. if (IS_GEN5(dev)) {
  5374. /* Required for FBC */
  5375. dspclk_gate |= DPFDUNIT_CLOCK_GATE_DISABLE;
  5376. /* Required for CxSR */
  5377. dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
  5378. I915_WRITE(PCH_3DCGDIS0,
  5379. MARIUNIT_CLOCK_GATE_DISABLE |
  5380. SVSMUNIT_CLOCK_GATE_DISABLE);
  5381. I915_WRITE(PCH_3DCGDIS1,
  5382. VFMUNIT_CLOCK_GATE_DISABLE);
  5383. }
  5384. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  5385. /*
  5386. * On Ibex Peak and Cougar Point, we need to disable clock
  5387. * gating for the panel power sequencer or it will fail to
  5388. * start up when no ports are active.
  5389. */
  5390. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  5391. /*
  5392. * According to the spec the following bits should be set in
  5393. * order to enable memory self-refresh
  5394. * The bit 22/21 of 0x42004
  5395. * The bit 5 of 0x42020
  5396. * The bit 15 of 0x45000
  5397. */
  5398. if (IS_GEN5(dev)) {
  5399. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5400. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  5401. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  5402. I915_WRITE(ILK_DSPCLK_GATE,
  5403. (I915_READ(ILK_DSPCLK_GATE) |
  5404. ILK_DPARB_CLK_GATE));
  5405. I915_WRITE(DISP_ARB_CTL,
  5406. (I915_READ(DISP_ARB_CTL) |
  5407. DISP_FBC_WM_DIS));
  5408. I915_WRITE(WM3_LP_ILK, 0);
  5409. I915_WRITE(WM2_LP_ILK, 0);
  5410. I915_WRITE(WM1_LP_ILK, 0);
  5411. }
  5412. /*
  5413. * Based on the document from hardware guys the following bits
  5414. * should be set unconditionally in order to enable FBC.
  5415. * The bit 22 of 0x42000
  5416. * The bit 22 of 0x42004
  5417. * The bit 7,8,9 of 0x42020.
  5418. */
  5419. if (IS_IRONLAKE_M(dev)) {
  5420. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  5421. I915_READ(ILK_DISPLAY_CHICKEN1) |
  5422. ILK_FBCQ_DIS);
  5423. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5424. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5425. ILK_DPARB_GATE);
  5426. I915_WRITE(ILK_DSPCLK_GATE,
  5427. I915_READ(ILK_DSPCLK_GATE) |
  5428. ILK_DPFC_DIS1 |
  5429. ILK_DPFC_DIS2 |
  5430. ILK_CLK_FBC);
  5431. }
  5432. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5433. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5434. ILK_ELPIN_409_SELECT);
  5435. if (IS_GEN5(dev)) {
  5436. I915_WRITE(_3D_CHICKEN2,
  5437. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  5438. _3D_CHICKEN2_WM_READ_PIPELINED);
  5439. }
  5440. if (IS_GEN6(dev)) {
  5441. I915_WRITE(WM3_LP_ILK, 0);
  5442. I915_WRITE(WM2_LP_ILK, 0);
  5443. I915_WRITE(WM1_LP_ILK, 0);
  5444. /*
  5445. * According to the spec the following bits should be
  5446. * set in order to enable memory self-refresh and fbc:
  5447. * The bit21 and bit22 of 0x42000
  5448. * The bit21 and bit22 of 0x42004
  5449. * The bit5 and bit7 of 0x42020
  5450. * The bit14 of 0x70180
  5451. * The bit14 of 0x71180
  5452. */
  5453. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  5454. I915_READ(ILK_DISPLAY_CHICKEN1) |
  5455. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  5456. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5457. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5458. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  5459. I915_WRITE(ILK_DSPCLK_GATE,
  5460. I915_READ(ILK_DSPCLK_GATE) |
  5461. ILK_DPARB_CLK_GATE |
  5462. ILK_DPFD_CLK_GATE);
  5463. I915_WRITE(DSPACNTR,
  5464. I915_READ(DSPACNTR) |
  5465. DISPPLANE_TRICKLE_FEED_DISABLE);
  5466. I915_WRITE(DSPBCNTR,
  5467. I915_READ(DSPBCNTR) |
  5468. DISPPLANE_TRICKLE_FEED_DISABLE);
  5469. }
  5470. } else if (IS_G4X(dev)) {
  5471. uint32_t dspclk_gate;
  5472. I915_WRITE(RENCLK_GATE_D1, 0);
  5473. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  5474. GS_UNIT_CLOCK_GATE_DISABLE |
  5475. CL_UNIT_CLOCK_GATE_DISABLE);
  5476. I915_WRITE(RAMCLK_GATE_D, 0);
  5477. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  5478. OVRUNIT_CLOCK_GATE_DISABLE |
  5479. OVCUNIT_CLOCK_GATE_DISABLE;
  5480. if (IS_GM45(dev))
  5481. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  5482. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  5483. } else if (IS_CRESTLINE(dev)) {
  5484. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  5485. I915_WRITE(RENCLK_GATE_D2, 0);
  5486. I915_WRITE(DSPCLK_GATE_D, 0);
  5487. I915_WRITE(RAMCLK_GATE_D, 0);
  5488. I915_WRITE16(DEUC, 0);
  5489. } else if (IS_BROADWATER(dev)) {
  5490. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  5491. I965_RCC_CLOCK_GATE_DISABLE |
  5492. I965_RCPB_CLOCK_GATE_DISABLE |
  5493. I965_ISC_CLOCK_GATE_DISABLE |
  5494. I965_FBC_CLOCK_GATE_DISABLE);
  5495. I915_WRITE(RENCLK_GATE_D2, 0);
  5496. } else if (IS_GEN3(dev)) {
  5497. u32 dstate = I915_READ(D_STATE);
  5498. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  5499. DSTATE_DOT_CLOCK_GATING;
  5500. I915_WRITE(D_STATE, dstate);
  5501. } else if (IS_I85X(dev) || IS_I865G(dev)) {
  5502. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  5503. } else if (IS_I830(dev)) {
  5504. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  5505. }
  5506. /*
  5507. * GPU can automatically power down the render unit if given a page
  5508. * to save state.
  5509. */
  5510. if (IS_IRONLAKE_M(dev) && 0) { /* XXX causes a failure during suspend */
  5511. if (dev_priv->renderctx == NULL)
  5512. dev_priv->renderctx = intel_alloc_context_page(dev);
  5513. if (dev_priv->renderctx) {
  5514. struct drm_i915_gem_object *obj = dev_priv->renderctx;
  5515. if (BEGIN_LP_RING(4) == 0) {
  5516. OUT_RING(MI_SET_CONTEXT);
  5517. OUT_RING(obj->gtt_offset |
  5518. MI_MM_SPACE_GTT |
  5519. MI_SAVE_EXT_STATE_EN |
  5520. MI_RESTORE_EXT_STATE_EN |
  5521. MI_RESTORE_INHIBIT);
  5522. OUT_RING(MI_NOOP);
  5523. OUT_RING(MI_FLUSH);
  5524. ADVANCE_LP_RING();
  5525. }
  5526. } else
  5527. DRM_DEBUG_KMS("Failed to allocate render context."
  5528. "Disable RC6\n");
  5529. }
  5530. if (IS_GEN4(dev) && IS_MOBILE(dev)) {
  5531. if (dev_priv->pwrctx == NULL)
  5532. dev_priv->pwrctx = intel_alloc_context_page(dev);
  5533. if (dev_priv->pwrctx) {
  5534. struct drm_i915_gem_object *obj = dev_priv->pwrctx;
  5535. I915_WRITE(PWRCTXA, obj->gtt_offset | PWRCTX_EN);
  5536. I915_WRITE(MCHBAR_RENDER_STANDBY,
  5537. I915_READ(MCHBAR_RENDER_STANDBY) & ~RCX_SW_EXIT);
  5538. }
  5539. }
  5540. }
  5541. void intel_disable_clock_gating(struct drm_device *dev)
  5542. {
  5543. struct drm_i915_private *dev_priv = dev->dev_private;
  5544. if (dev_priv->renderctx) {
  5545. struct drm_i915_gem_object *obj = dev_priv->renderctx;
  5546. I915_WRITE(CCID, 0);
  5547. POSTING_READ(CCID);
  5548. i915_gem_object_unpin(obj);
  5549. drm_gem_object_unreference(&obj->base);
  5550. dev_priv->renderctx = NULL;
  5551. }
  5552. if (dev_priv->pwrctx) {
  5553. struct drm_i915_gem_object *obj = dev_priv->pwrctx;
  5554. I915_WRITE(PWRCTXA, 0);
  5555. POSTING_READ(PWRCTXA);
  5556. i915_gem_object_unpin(obj);
  5557. drm_gem_object_unreference(&obj->base);
  5558. dev_priv->pwrctx = NULL;
  5559. }
  5560. }
  5561. /* Set up chip specific display functions */
  5562. static void intel_init_display(struct drm_device *dev)
  5563. {
  5564. struct drm_i915_private *dev_priv = dev->dev_private;
  5565. /* We always want a DPMS function */
  5566. if (HAS_PCH_SPLIT(dev))
  5567. dev_priv->display.dpms = ironlake_crtc_dpms;
  5568. else
  5569. dev_priv->display.dpms = i9xx_crtc_dpms;
  5570. if (I915_HAS_FBC(dev)) {
  5571. if (HAS_PCH_SPLIT(dev)) {
  5572. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  5573. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  5574. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  5575. } else if (IS_GM45(dev)) {
  5576. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  5577. dev_priv->display.enable_fbc = g4x_enable_fbc;
  5578. dev_priv->display.disable_fbc = g4x_disable_fbc;
  5579. } else if (IS_CRESTLINE(dev)) {
  5580. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  5581. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  5582. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  5583. }
  5584. /* 855GM needs testing */
  5585. }
  5586. /* Returns the core display clock speed */
  5587. if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
  5588. dev_priv->display.get_display_clock_speed =
  5589. i945_get_display_clock_speed;
  5590. else if (IS_I915G(dev))
  5591. dev_priv->display.get_display_clock_speed =
  5592. i915_get_display_clock_speed;
  5593. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  5594. dev_priv->display.get_display_clock_speed =
  5595. i9xx_misc_get_display_clock_speed;
  5596. else if (IS_I915GM(dev))
  5597. dev_priv->display.get_display_clock_speed =
  5598. i915gm_get_display_clock_speed;
  5599. else if (IS_I865G(dev))
  5600. dev_priv->display.get_display_clock_speed =
  5601. i865_get_display_clock_speed;
  5602. else if (IS_I85X(dev))
  5603. dev_priv->display.get_display_clock_speed =
  5604. i855_get_display_clock_speed;
  5605. else /* 852, 830 */
  5606. dev_priv->display.get_display_clock_speed =
  5607. i830_get_display_clock_speed;
  5608. /* For FIFO watermark updates */
  5609. if (HAS_PCH_SPLIT(dev)) {
  5610. if (IS_GEN5(dev)) {
  5611. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  5612. dev_priv->display.update_wm = ironlake_update_wm;
  5613. else {
  5614. DRM_DEBUG_KMS("Failed to get proper latency. "
  5615. "Disable CxSR\n");
  5616. dev_priv->display.update_wm = NULL;
  5617. }
  5618. } else if (IS_GEN6(dev)) {
  5619. if (SNB_READ_WM0_LATENCY()) {
  5620. dev_priv->display.update_wm = sandybridge_update_wm;
  5621. } else {
  5622. DRM_DEBUG_KMS("Failed to read display plane latency. "
  5623. "Disable CxSR\n");
  5624. dev_priv->display.update_wm = NULL;
  5625. }
  5626. } else
  5627. dev_priv->display.update_wm = NULL;
  5628. } else if (IS_PINEVIEW(dev)) {
  5629. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  5630. dev_priv->is_ddr3,
  5631. dev_priv->fsb_freq,
  5632. dev_priv->mem_freq)) {
  5633. DRM_INFO("failed to find known CxSR latency "
  5634. "(found ddr%s fsb freq %d, mem freq %d), "
  5635. "disabling CxSR\n",
  5636. (dev_priv->is_ddr3 == 1) ? "3": "2",
  5637. dev_priv->fsb_freq, dev_priv->mem_freq);
  5638. /* Disable CxSR and never update its watermark again */
  5639. pineview_disable_cxsr(dev);
  5640. dev_priv->display.update_wm = NULL;
  5641. } else
  5642. dev_priv->display.update_wm = pineview_update_wm;
  5643. } else if (IS_G4X(dev))
  5644. dev_priv->display.update_wm = g4x_update_wm;
  5645. else if (IS_GEN4(dev))
  5646. dev_priv->display.update_wm = i965_update_wm;
  5647. else if (IS_GEN3(dev)) {
  5648. dev_priv->display.update_wm = i9xx_update_wm;
  5649. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  5650. } else if (IS_I85X(dev)) {
  5651. dev_priv->display.update_wm = i9xx_update_wm;
  5652. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  5653. } else {
  5654. dev_priv->display.update_wm = i830_update_wm;
  5655. if (IS_845G(dev))
  5656. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  5657. else
  5658. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  5659. }
  5660. }
  5661. /*
  5662. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  5663. * resume, or other times. This quirk makes sure that's the case for
  5664. * affected systems.
  5665. */
  5666. static void quirk_pipea_force (struct drm_device *dev)
  5667. {
  5668. struct drm_i915_private *dev_priv = dev->dev_private;
  5669. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  5670. DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
  5671. }
  5672. struct intel_quirk {
  5673. int device;
  5674. int subsystem_vendor;
  5675. int subsystem_device;
  5676. void (*hook)(struct drm_device *dev);
  5677. };
  5678. struct intel_quirk intel_quirks[] = {
  5679. /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
  5680. { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
  5681. /* HP Mini needs pipe A force quirk (LP: #322104) */
  5682. { 0x27ae,0x103c, 0x361a, quirk_pipea_force },
  5683. /* Thinkpad R31 needs pipe A force quirk */
  5684. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  5685. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  5686. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  5687. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  5688. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  5689. /* ThinkPad X40 needs pipe A force quirk */
  5690. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  5691. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  5692. /* 855 & before need to leave pipe A & dpll A up */
  5693. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5694. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5695. };
  5696. static void intel_init_quirks(struct drm_device *dev)
  5697. {
  5698. struct pci_dev *d = dev->pdev;
  5699. int i;
  5700. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  5701. struct intel_quirk *q = &intel_quirks[i];
  5702. if (d->device == q->device &&
  5703. (d->subsystem_vendor == q->subsystem_vendor ||
  5704. q->subsystem_vendor == PCI_ANY_ID) &&
  5705. (d->subsystem_device == q->subsystem_device ||
  5706. q->subsystem_device == PCI_ANY_ID))
  5707. q->hook(dev);
  5708. }
  5709. }
  5710. /* Disable the VGA plane that we never use */
  5711. static void i915_disable_vga(struct drm_device *dev)
  5712. {
  5713. struct drm_i915_private *dev_priv = dev->dev_private;
  5714. u8 sr1;
  5715. u32 vga_reg;
  5716. if (HAS_PCH_SPLIT(dev))
  5717. vga_reg = CPU_VGACNTRL;
  5718. else
  5719. vga_reg = VGACNTRL;
  5720. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  5721. outb(1, VGA_SR_INDEX);
  5722. sr1 = inb(VGA_SR_DATA);
  5723. outb(sr1 | 1<<5, VGA_SR_DATA);
  5724. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  5725. udelay(300);
  5726. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  5727. POSTING_READ(vga_reg);
  5728. }
  5729. void intel_modeset_init(struct drm_device *dev)
  5730. {
  5731. struct drm_i915_private *dev_priv = dev->dev_private;
  5732. int i;
  5733. drm_mode_config_init(dev);
  5734. dev->mode_config.min_width = 0;
  5735. dev->mode_config.min_height = 0;
  5736. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  5737. intel_init_quirks(dev);
  5738. intel_init_display(dev);
  5739. if (IS_GEN2(dev)) {
  5740. dev->mode_config.max_width = 2048;
  5741. dev->mode_config.max_height = 2048;
  5742. } else if (IS_GEN3(dev)) {
  5743. dev->mode_config.max_width = 4096;
  5744. dev->mode_config.max_height = 4096;
  5745. } else {
  5746. dev->mode_config.max_width = 8192;
  5747. dev->mode_config.max_height = 8192;
  5748. }
  5749. dev->mode_config.fb_base = dev->agp->base;
  5750. if (IS_MOBILE(dev) || !IS_GEN2(dev))
  5751. dev_priv->num_pipe = 2;
  5752. else
  5753. dev_priv->num_pipe = 1;
  5754. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  5755. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  5756. for (i = 0; i < dev_priv->num_pipe; i++) {
  5757. intel_crtc_init(dev, i);
  5758. }
  5759. intel_setup_outputs(dev);
  5760. intel_enable_clock_gating(dev);
  5761. /* Just disable it once at startup */
  5762. i915_disable_vga(dev);
  5763. if (IS_IRONLAKE_M(dev)) {
  5764. ironlake_enable_drps(dev);
  5765. intel_init_emon(dev);
  5766. }
  5767. if (IS_GEN6(dev))
  5768. gen6_enable_rps(dev_priv);
  5769. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  5770. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  5771. (unsigned long)dev);
  5772. intel_setup_overlay(dev);
  5773. }
  5774. void intel_modeset_cleanup(struct drm_device *dev)
  5775. {
  5776. struct drm_i915_private *dev_priv = dev->dev_private;
  5777. struct drm_crtc *crtc;
  5778. struct intel_crtc *intel_crtc;
  5779. drm_kms_helper_poll_fini(dev);
  5780. mutex_lock(&dev->struct_mutex);
  5781. intel_unregister_dsm_handler();
  5782. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5783. /* Skip inactive CRTCs */
  5784. if (!crtc->fb)
  5785. continue;
  5786. intel_crtc = to_intel_crtc(crtc);
  5787. intel_increase_pllclock(crtc);
  5788. }
  5789. if (dev_priv->display.disable_fbc)
  5790. dev_priv->display.disable_fbc(dev);
  5791. if (IS_IRONLAKE_M(dev))
  5792. ironlake_disable_drps(dev);
  5793. if (IS_GEN6(dev))
  5794. gen6_disable_rps(dev);
  5795. intel_disable_clock_gating(dev);
  5796. mutex_unlock(&dev->struct_mutex);
  5797. /* Disable the irq before mode object teardown, for the irq might
  5798. * enqueue unpin/hotplug work. */
  5799. drm_irq_uninstall(dev);
  5800. cancel_work_sync(&dev_priv->hotplug_work);
  5801. /* Shut off idle work before the crtcs get freed. */
  5802. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5803. intel_crtc = to_intel_crtc(crtc);
  5804. del_timer_sync(&intel_crtc->idle_timer);
  5805. }
  5806. del_timer_sync(&dev_priv->idle_timer);
  5807. cancel_work_sync(&dev_priv->idle_work);
  5808. drm_mode_config_cleanup(dev);
  5809. }
  5810. /*
  5811. * Return which encoder is currently attached for connector.
  5812. */
  5813. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  5814. {
  5815. return &intel_attached_encoder(connector)->base;
  5816. }
  5817. void intel_connector_attach_encoder(struct intel_connector *connector,
  5818. struct intel_encoder *encoder)
  5819. {
  5820. connector->encoder = encoder;
  5821. drm_mode_connector_attach_encoder(&connector->base,
  5822. &encoder->base);
  5823. }
  5824. /*
  5825. * set vga decode state - true == enable VGA decode
  5826. */
  5827. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  5828. {
  5829. struct drm_i915_private *dev_priv = dev->dev_private;
  5830. u16 gmch_ctrl;
  5831. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  5832. if (state)
  5833. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  5834. else
  5835. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  5836. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  5837. return 0;
  5838. }
  5839. #ifdef CONFIG_DEBUG_FS
  5840. #include <linux/seq_file.h>
  5841. struct intel_display_error_state {
  5842. struct intel_cursor_error_state {
  5843. u32 control;
  5844. u32 position;
  5845. u32 base;
  5846. u32 size;
  5847. } cursor[2];
  5848. struct intel_pipe_error_state {
  5849. u32 conf;
  5850. u32 source;
  5851. u32 htotal;
  5852. u32 hblank;
  5853. u32 hsync;
  5854. u32 vtotal;
  5855. u32 vblank;
  5856. u32 vsync;
  5857. } pipe[2];
  5858. struct intel_plane_error_state {
  5859. u32 control;
  5860. u32 stride;
  5861. u32 size;
  5862. u32 pos;
  5863. u32 addr;
  5864. u32 surface;
  5865. u32 tile_offset;
  5866. } plane[2];
  5867. };
  5868. struct intel_display_error_state *
  5869. intel_display_capture_error_state(struct drm_device *dev)
  5870. {
  5871. drm_i915_private_t *dev_priv = dev->dev_private;
  5872. struct intel_display_error_state *error;
  5873. int i;
  5874. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  5875. if (error == NULL)
  5876. return NULL;
  5877. for (i = 0; i < 2; i++) {
  5878. error->cursor[i].control = I915_READ(CURCNTR(i));
  5879. error->cursor[i].position = I915_READ(CURPOS(i));
  5880. error->cursor[i].base = I915_READ(CURBASE(i));
  5881. error->plane[i].control = I915_READ(DSPCNTR(i));
  5882. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  5883. error->plane[i].size = I915_READ(DSPSIZE(i));
  5884. error->plane[i].pos= I915_READ(DSPPOS(i));
  5885. error->plane[i].addr = I915_READ(DSPADDR(i));
  5886. if (INTEL_INFO(dev)->gen >= 4) {
  5887. error->plane[i].surface = I915_READ(DSPSURF(i));
  5888. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  5889. }
  5890. error->pipe[i].conf = I915_READ(PIPECONF(i));
  5891. error->pipe[i].source = I915_READ(PIPESRC(i));
  5892. error->pipe[i].htotal = I915_READ(HTOTAL(i));
  5893. error->pipe[i].hblank = I915_READ(HBLANK(i));
  5894. error->pipe[i].hsync = I915_READ(HSYNC(i));
  5895. error->pipe[i].vtotal = I915_READ(VTOTAL(i));
  5896. error->pipe[i].vblank = I915_READ(VBLANK(i));
  5897. error->pipe[i].vsync = I915_READ(VSYNC(i));
  5898. }
  5899. return error;
  5900. }
  5901. void
  5902. intel_display_print_error_state(struct seq_file *m,
  5903. struct drm_device *dev,
  5904. struct intel_display_error_state *error)
  5905. {
  5906. int i;
  5907. for (i = 0; i < 2; i++) {
  5908. seq_printf(m, "Pipe [%d]:\n", i);
  5909. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  5910. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  5911. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  5912. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  5913. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  5914. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  5915. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  5916. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  5917. seq_printf(m, "Plane [%d]:\n", i);
  5918. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  5919. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  5920. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  5921. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  5922. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  5923. if (INTEL_INFO(dev)->gen >= 4) {
  5924. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  5925. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  5926. }
  5927. seq_printf(m, "Cursor [%d]:\n", i);
  5928. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  5929. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  5930. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  5931. }
  5932. }
  5933. #endif