intel_display.c 211 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520
  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 const char *state_string(bool enabled)
  977. {
  978. return enabled ? "on" : "off";
  979. }
  980. /* Only for pre-ILK configs */
  981. static void assert_pll(struct drm_i915_private *dev_priv,
  982. enum pipe pipe, bool state)
  983. {
  984. int reg;
  985. u32 val;
  986. bool cur_state;
  987. reg = DPLL(pipe);
  988. val = I915_READ(reg);
  989. cur_state = !!(val & DPLL_VCO_ENABLE);
  990. WARN(cur_state != state,
  991. "PLL state assertion failure (expected %s, current %s)\n",
  992. state_string(state), state_string(cur_state));
  993. }
  994. #define assert_pll_enabled(d, p) assert_pll(d, p, true)
  995. #define assert_pll_disabled(d, p) assert_pll(d, p, false)
  996. /* For ILK+ */
  997. static void assert_pch_pll(struct drm_i915_private *dev_priv,
  998. enum pipe pipe, bool state)
  999. {
  1000. int reg;
  1001. u32 val;
  1002. bool cur_state;
  1003. reg = PCH_DPLL(pipe);
  1004. val = I915_READ(reg);
  1005. cur_state = !!(val & DPLL_VCO_ENABLE);
  1006. WARN(cur_state != state,
  1007. "PCH PLL state assertion failure (expected %s, current %s)\n",
  1008. state_string(state), state_string(cur_state));
  1009. }
  1010. #define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
  1011. #define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
  1012. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1013. enum pipe pipe, bool state)
  1014. {
  1015. int reg;
  1016. u32 val;
  1017. bool cur_state;
  1018. reg = FDI_TX_CTL(pipe);
  1019. val = I915_READ(reg);
  1020. cur_state = !!(val & FDI_TX_ENABLE);
  1021. WARN(cur_state != state,
  1022. "FDI TX state assertion failure (expected %s, current %s)\n",
  1023. state_string(state), state_string(cur_state));
  1024. }
  1025. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1026. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1027. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1028. enum pipe pipe, bool state)
  1029. {
  1030. int reg;
  1031. u32 val;
  1032. bool cur_state;
  1033. reg = FDI_RX_CTL(pipe);
  1034. val = I915_READ(reg);
  1035. cur_state = !!(val & FDI_RX_ENABLE);
  1036. WARN(cur_state != state,
  1037. "FDI RX state assertion failure (expected %s, current %s)\n",
  1038. state_string(state), state_string(cur_state));
  1039. }
  1040. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1041. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1042. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1043. enum pipe pipe)
  1044. {
  1045. int reg;
  1046. u32 val;
  1047. /* ILK FDI PLL is always enabled */
  1048. if (dev_priv->info->gen == 5)
  1049. return;
  1050. reg = FDI_TX_CTL(pipe);
  1051. val = I915_READ(reg);
  1052. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1053. }
  1054. static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
  1055. enum pipe pipe)
  1056. {
  1057. int reg;
  1058. u32 val;
  1059. reg = FDI_RX_CTL(pipe);
  1060. val = I915_READ(reg);
  1061. WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
  1062. }
  1063. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1064. enum pipe pipe)
  1065. {
  1066. int pp_reg, lvds_reg;
  1067. u32 val;
  1068. enum pipe panel_pipe = PIPE_A;
  1069. bool locked = locked;
  1070. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  1071. pp_reg = PCH_PP_CONTROL;
  1072. lvds_reg = PCH_LVDS;
  1073. } else {
  1074. pp_reg = PP_CONTROL;
  1075. lvds_reg = LVDS;
  1076. }
  1077. val = I915_READ(pp_reg);
  1078. if (!(val & PANEL_POWER_ON) ||
  1079. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  1080. locked = false;
  1081. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  1082. panel_pipe = PIPE_B;
  1083. WARN(panel_pipe == pipe && locked,
  1084. "panel assertion failure, pipe %c regs locked\n",
  1085. pipe ? 'B' : 'A');
  1086. }
  1087. static void assert_pipe(struct drm_i915_private *dev_priv,
  1088. enum pipe pipe, bool state)
  1089. {
  1090. int reg;
  1091. u32 val;
  1092. bool cur_state;
  1093. reg = PIPECONF(pipe);
  1094. val = I915_READ(reg);
  1095. cur_state = !!(val & PIPECONF_ENABLE);
  1096. WARN(cur_state != state,
  1097. "pipe %c assertion failure (expected %s, current %s)\n",
  1098. pipe ? 'B' : 'A', state_string(state), state_string(cur_state));
  1099. }
  1100. #define assert_pipe_enabled(d, p) assert_pipe(d, p, true)
  1101. #define assert_pipe_disabled(d, p) assert_pipe(d, p, false)
  1102. static void assert_plane_enabled(struct drm_i915_private *dev_priv,
  1103. enum plane plane)
  1104. {
  1105. int reg;
  1106. u32 val;
  1107. reg = DSPCNTR(plane);
  1108. val = I915_READ(reg);
  1109. WARN(!(val & DISPLAY_PLANE_ENABLE),
  1110. "plane %c assertion failure, should be active but is disabled\n",
  1111. plane ? 'B' : 'A');
  1112. }
  1113. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1114. enum pipe pipe)
  1115. {
  1116. int reg, i;
  1117. u32 val;
  1118. int cur_pipe;
  1119. /* Need to check both planes against the pipe */
  1120. for (i = 0; i < 2; i++) {
  1121. reg = DSPCNTR(i);
  1122. val = I915_READ(reg);
  1123. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1124. DISPPLANE_SEL_PIPE_SHIFT;
  1125. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1126. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1127. i, pipe ? 'B' : 'A');
  1128. }
  1129. }
  1130. static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1131. {
  1132. u32 val;
  1133. bool enabled;
  1134. val = I915_READ(PCH_DREF_CONTROL);
  1135. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1136. DREF_SUPERSPREAD_SOURCE_MASK));
  1137. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1138. }
  1139. static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
  1140. enum pipe pipe)
  1141. {
  1142. int reg;
  1143. u32 val;
  1144. bool enabled;
  1145. reg = TRANSCONF(pipe);
  1146. val = I915_READ(reg);
  1147. enabled = !!(val & TRANS_ENABLE);
  1148. WARN(enabled, "transcoder assertion failed, should be off on pipe %c but is still active\n", pipe ? 'B' :'A');
  1149. }
  1150. /**
  1151. * intel_enable_pll - enable a PLL
  1152. * @dev_priv: i915 private structure
  1153. * @pipe: pipe PLL to enable
  1154. *
  1155. * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
  1156. * make sure the PLL reg is writable first though, since the panel write
  1157. * protect mechanism may be enabled.
  1158. *
  1159. * Note! This is for pre-ILK only.
  1160. */
  1161. static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1162. {
  1163. int reg;
  1164. u32 val;
  1165. /* No really, not for ILK+ */
  1166. BUG_ON(dev_priv->info->gen >= 5);
  1167. /* PLL is protected by panel, make sure we can write it */
  1168. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1169. assert_panel_unlocked(dev_priv, pipe);
  1170. reg = DPLL(pipe);
  1171. val = I915_READ(reg);
  1172. val |= DPLL_VCO_ENABLE;
  1173. /* We do this three times for luck */
  1174. I915_WRITE(reg, val);
  1175. POSTING_READ(reg);
  1176. udelay(150); /* wait for warmup */
  1177. I915_WRITE(reg, val);
  1178. POSTING_READ(reg);
  1179. udelay(150); /* wait for warmup */
  1180. I915_WRITE(reg, val);
  1181. POSTING_READ(reg);
  1182. udelay(150); /* wait for warmup */
  1183. }
  1184. /**
  1185. * intel_disable_pll - disable a PLL
  1186. * @dev_priv: i915 private structure
  1187. * @pipe: pipe PLL to disable
  1188. *
  1189. * Disable the PLL for @pipe, making sure the pipe is off first.
  1190. *
  1191. * Note! This is for pre-ILK only.
  1192. */
  1193. static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1194. {
  1195. int reg;
  1196. u32 val;
  1197. /* Don't disable pipe A or pipe A PLLs if needed */
  1198. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1199. return;
  1200. /* Make sure the pipe isn't still relying on us */
  1201. assert_pipe_disabled(dev_priv, pipe);
  1202. reg = DPLL(pipe);
  1203. val = I915_READ(reg);
  1204. val &= ~DPLL_VCO_ENABLE;
  1205. I915_WRITE(reg, val);
  1206. POSTING_READ(reg);
  1207. }
  1208. /**
  1209. * intel_enable_pch_pll - enable PCH PLL
  1210. * @dev_priv: i915 private structure
  1211. * @pipe: pipe PLL to enable
  1212. *
  1213. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1214. * drives the transcoder clock.
  1215. */
  1216. static void intel_enable_pch_pll(struct drm_i915_private *dev_priv,
  1217. enum pipe pipe)
  1218. {
  1219. int reg;
  1220. u32 val;
  1221. /* PCH only available on ILK+ */
  1222. BUG_ON(dev_priv->info->gen < 5);
  1223. /* PCH refclock must be enabled first */
  1224. assert_pch_refclk_enabled(dev_priv);
  1225. reg = PCH_DPLL(pipe);
  1226. val = I915_READ(reg);
  1227. val |= DPLL_VCO_ENABLE;
  1228. I915_WRITE(reg, val);
  1229. POSTING_READ(reg);
  1230. udelay(200);
  1231. }
  1232. static void intel_disable_pch_pll(struct drm_i915_private *dev_priv,
  1233. enum pipe pipe)
  1234. {
  1235. int reg;
  1236. u32 val;
  1237. /* PCH only available on ILK+ */
  1238. BUG_ON(dev_priv->info->gen < 5);
  1239. /* Make sure transcoder isn't still depending on us */
  1240. assert_transcoder_disabled(dev_priv, pipe);
  1241. reg = PCH_DPLL(pipe);
  1242. val = I915_READ(reg);
  1243. val &= ~DPLL_VCO_ENABLE;
  1244. I915_WRITE(reg, val);
  1245. POSTING_READ(reg);
  1246. udelay(200);
  1247. }
  1248. static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
  1249. enum pipe pipe)
  1250. {
  1251. int reg;
  1252. u32 val;
  1253. /* PCH only available on ILK+ */
  1254. BUG_ON(dev_priv->info->gen < 5);
  1255. /* Make sure PCH DPLL is enabled */
  1256. assert_pch_pll_enabled(dev_priv, pipe);
  1257. /* FDI must be feeding us bits for PCH ports */
  1258. assert_fdi_tx_enabled(dev_priv, pipe);
  1259. assert_fdi_rx_enabled(dev_priv, pipe);
  1260. reg = TRANSCONF(pipe);
  1261. val = I915_READ(reg);
  1262. /*
  1263. * make the BPC in transcoder be consistent with
  1264. * that in pipeconf reg.
  1265. */
  1266. val &= ~PIPE_BPC_MASK;
  1267. val |= I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK;
  1268. I915_WRITE(reg, val | TRANS_ENABLE);
  1269. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1270. DRM_ERROR("failed to enable transcoder %d\n", pipe);
  1271. }
  1272. static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
  1273. enum pipe pipe)
  1274. {
  1275. int reg;
  1276. u32 val;
  1277. /* FDI relies on the transcoder */
  1278. assert_fdi_tx_disabled(dev_priv, pipe);
  1279. assert_fdi_rx_disabled(dev_priv, pipe);
  1280. reg = TRANSCONF(pipe);
  1281. val = I915_READ(reg);
  1282. val &= ~TRANS_ENABLE;
  1283. I915_WRITE(reg, val);
  1284. /* wait for PCH transcoder off, transcoder state */
  1285. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1286. DRM_ERROR("failed to disable transcoder\n");
  1287. }
  1288. /**
  1289. * intel_enable_pipe - enable a pipe, assertiing requirements
  1290. * @dev_priv: i915 private structure
  1291. * @pipe: pipe to enable
  1292. * @pch_port: on ILK+, is this pipe driving a PCH port or not
  1293. *
  1294. * Enable @pipe, making sure that various hardware specific requirements
  1295. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1296. *
  1297. * @pipe should be %PIPE_A or %PIPE_B.
  1298. *
  1299. * Will wait until the pipe is actually running (i.e. first vblank) before
  1300. * returning.
  1301. */
  1302. static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
  1303. bool pch_port)
  1304. {
  1305. int reg;
  1306. u32 val;
  1307. /*
  1308. * A pipe without a PLL won't actually be able to drive bits from
  1309. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1310. * need the check.
  1311. */
  1312. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1313. assert_pll_enabled(dev_priv, pipe);
  1314. else {
  1315. if (pch_port) {
  1316. /* if driving the PCH, we need FDI enabled */
  1317. assert_fdi_rx_pll_enabled(dev_priv, pipe);
  1318. assert_fdi_tx_pll_enabled(dev_priv, pipe);
  1319. }
  1320. /* FIXME: assert CPU port conditions for SNB+ */
  1321. }
  1322. reg = PIPECONF(pipe);
  1323. val = I915_READ(reg);
  1324. val |= PIPECONF_ENABLE;
  1325. I915_WRITE(reg, val);
  1326. POSTING_READ(reg);
  1327. intel_wait_for_vblank(dev_priv->dev, pipe);
  1328. }
  1329. /**
  1330. * intel_disable_pipe - disable a pipe, assertiing requirements
  1331. * @dev_priv: i915 private structure
  1332. * @pipe: pipe to disable
  1333. *
  1334. * Disable @pipe, making sure that various hardware specific requirements
  1335. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1336. *
  1337. * @pipe should be %PIPE_A or %PIPE_B.
  1338. *
  1339. * Will wait until the pipe has shut down before returning.
  1340. */
  1341. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1342. enum pipe pipe)
  1343. {
  1344. int reg;
  1345. u32 val;
  1346. /*
  1347. * Make sure planes won't keep trying to pump pixels to us,
  1348. * or we might hang the display.
  1349. */
  1350. assert_planes_disabled(dev_priv, pipe);
  1351. /* Don't disable pipe A or pipe A PLLs if needed */
  1352. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1353. return;
  1354. reg = PIPECONF(pipe);
  1355. val = I915_READ(reg);
  1356. val &= ~PIPECONF_ENABLE;
  1357. I915_WRITE(reg, val);
  1358. POSTING_READ(reg);
  1359. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1360. }
  1361. /**
  1362. * intel_enable_plane - enable a display plane on a given pipe
  1363. * @dev_priv: i915 private structure
  1364. * @plane: plane to enable
  1365. * @pipe: pipe being fed
  1366. *
  1367. * Enable @plane on @pipe, making sure that @pipe is running first.
  1368. */
  1369. static void intel_enable_plane(struct drm_i915_private *dev_priv,
  1370. enum plane plane, enum pipe pipe)
  1371. {
  1372. int reg;
  1373. u32 val;
  1374. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1375. assert_pipe_enabled(dev_priv, pipe);
  1376. reg = DSPCNTR(plane);
  1377. val = I915_READ(reg);
  1378. val |= DISPLAY_PLANE_ENABLE;
  1379. I915_WRITE(reg, val);
  1380. POSTING_READ(reg);
  1381. intel_wait_for_vblank(dev_priv->dev, pipe);
  1382. }
  1383. /*
  1384. * Plane regs are double buffered, going from enabled->disabled needs a
  1385. * trigger in order to latch. The display address reg provides this.
  1386. */
  1387. static void intel_flush_display_plane(struct drm_i915_private *dev_priv,
  1388. enum plane plane)
  1389. {
  1390. u32 reg = DSPADDR(plane);
  1391. I915_WRITE(reg, I915_READ(reg));
  1392. }
  1393. /**
  1394. * intel_disable_plane - disable a display plane
  1395. * @dev_priv: i915 private structure
  1396. * @plane: plane to disable
  1397. * @pipe: pipe consuming the data
  1398. *
  1399. * Disable @plane; should be an independent operation.
  1400. */
  1401. static void intel_disable_plane(struct drm_i915_private *dev_priv,
  1402. enum plane plane, enum pipe pipe)
  1403. {
  1404. int reg;
  1405. u32 val;
  1406. reg = DSPCNTR(plane);
  1407. val = I915_READ(reg);
  1408. val &= ~DISPLAY_PLANE_ENABLE;
  1409. I915_WRITE(reg, val);
  1410. POSTING_READ(reg);
  1411. intel_flush_display_plane(dev_priv, plane);
  1412. intel_wait_for_vblank(dev_priv->dev, pipe);
  1413. }
  1414. static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1415. {
  1416. struct drm_device *dev = crtc->dev;
  1417. struct drm_i915_private *dev_priv = dev->dev_private;
  1418. struct drm_framebuffer *fb = crtc->fb;
  1419. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1420. struct drm_i915_gem_object *obj = intel_fb->obj;
  1421. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1422. int plane, i;
  1423. u32 fbc_ctl, fbc_ctl2;
  1424. if (fb->pitch == dev_priv->cfb_pitch &&
  1425. obj->fence_reg == dev_priv->cfb_fence &&
  1426. intel_crtc->plane == dev_priv->cfb_plane &&
  1427. I915_READ(FBC_CONTROL) & FBC_CTL_EN)
  1428. return;
  1429. i8xx_disable_fbc(dev);
  1430. dev_priv->cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  1431. if (fb->pitch < dev_priv->cfb_pitch)
  1432. dev_priv->cfb_pitch = fb->pitch;
  1433. /* FBC_CTL wants 64B units */
  1434. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  1435. dev_priv->cfb_fence = obj->fence_reg;
  1436. dev_priv->cfb_plane = intel_crtc->plane;
  1437. plane = dev_priv->cfb_plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  1438. /* Clear old tags */
  1439. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  1440. I915_WRITE(FBC_TAG + (i * 4), 0);
  1441. /* Set it up... */
  1442. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | plane;
  1443. if (obj->tiling_mode != I915_TILING_NONE)
  1444. fbc_ctl2 |= FBC_CTL_CPU_FENCE;
  1445. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  1446. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  1447. /* enable it... */
  1448. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  1449. if (IS_I945GM(dev))
  1450. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  1451. fbc_ctl |= (dev_priv->cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  1452. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  1453. if (obj->tiling_mode != I915_TILING_NONE)
  1454. fbc_ctl |= dev_priv->cfb_fence;
  1455. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1456. DRM_DEBUG_KMS("enabled FBC, pitch %ld, yoff %d, plane %d, ",
  1457. dev_priv->cfb_pitch, crtc->y, dev_priv->cfb_plane);
  1458. }
  1459. void i8xx_disable_fbc(struct drm_device *dev)
  1460. {
  1461. struct drm_i915_private *dev_priv = dev->dev_private;
  1462. u32 fbc_ctl;
  1463. /* Disable compression */
  1464. fbc_ctl = I915_READ(FBC_CONTROL);
  1465. if ((fbc_ctl & FBC_CTL_EN) == 0)
  1466. return;
  1467. fbc_ctl &= ~FBC_CTL_EN;
  1468. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1469. /* Wait for compressing bit to clear */
  1470. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  1471. DRM_DEBUG_KMS("FBC idle timed out\n");
  1472. return;
  1473. }
  1474. DRM_DEBUG_KMS("disabled FBC\n");
  1475. }
  1476. static bool i8xx_fbc_enabled(struct drm_device *dev)
  1477. {
  1478. struct drm_i915_private *dev_priv = dev->dev_private;
  1479. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  1480. }
  1481. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1482. {
  1483. struct drm_device *dev = crtc->dev;
  1484. struct drm_i915_private *dev_priv = dev->dev_private;
  1485. struct drm_framebuffer *fb = crtc->fb;
  1486. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1487. struct drm_i915_gem_object *obj = intel_fb->obj;
  1488. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1489. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1490. unsigned long stall_watermark = 200;
  1491. u32 dpfc_ctl;
  1492. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1493. if (dpfc_ctl & DPFC_CTL_EN) {
  1494. if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
  1495. dev_priv->cfb_fence == obj->fence_reg &&
  1496. dev_priv->cfb_plane == intel_crtc->plane &&
  1497. dev_priv->cfb_y == crtc->y)
  1498. return;
  1499. I915_WRITE(DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
  1500. POSTING_READ(DPFC_CONTROL);
  1501. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1502. }
  1503. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  1504. dev_priv->cfb_fence = obj->fence_reg;
  1505. dev_priv->cfb_plane = intel_crtc->plane;
  1506. dev_priv->cfb_y = crtc->y;
  1507. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  1508. if (obj->tiling_mode != I915_TILING_NONE) {
  1509. dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;
  1510. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  1511. } else {
  1512. I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  1513. }
  1514. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1515. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1516. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1517. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  1518. /* enable it... */
  1519. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  1520. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1521. }
  1522. void g4x_disable_fbc(struct drm_device *dev)
  1523. {
  1524. struct drm_i915_private *dev_priv = dev->dev_private;
  1525. u32 dpfc_ctl;
  1526. /* Disable compression */
  1527. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1528. if (dpfc_ctl & DPFC_CTL_EN) {
  1529. dpfc_ctl &= ~DPFC_CTL_EN;
  1530. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  1531. DRM_DEBUG_KMS("disabled FBC\n");
  1532. }
  1533. }
  1534. static bool g4x_fbc_enabled(struct drm_device *dev)
  1535. {
  1536. struct drm_i915_private *dev_priv = dev->dev_private;
  1537. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  1538. }
  1539. static void sandybridge_blit_fbc_update(struct drm_device *dev)
  1540. {
  1541. struct drm_i915_private *dev_priv = dev->dev_private;
  1542. u32 blt_ecoskpd;
  1543. /* Make sure blitter notifies FBC of writes */
  1544. __gen6_force_wake_get(dev_priv);
  1545. blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
  1546. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
  1547. GEN6_BLITTER_LOCK_SHIFT;
  1548. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1549. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
  1550. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1551. blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
  1552. GEN6_BLITTER_LOCK_SHIFT);
  1553. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1554. POSTING_READ(GEN6_BLITTER_ECOSKPD);
  1555. __gen6_force_wake_put(dev_priv);
  1556. }
  1557. static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1558. {
  1559. struct drm_device *dev = crtc->dev;
  1560. struct drm_i915_private *dev_priv = dev->dev_private;
  1561. struct drm_framebuffer *fb = crtc->fb;
  1562. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1563. struct drm_i915_gem_object *obj = intel_fb->obj;
  1564. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1565. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1566. unsigned long stall_watermark = 200;
  1567. u32 dpfc_ctl;
  1568. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1569. if (dpfc_ctl & DPFC_CTL_EN) {
  1570. if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
  1571. dev_priv->cfb_fence == obj->fence_reg &&
  1572. dev_priv->cfb_plane == intel_crtc->plane &&
  1573. dev_priv->cfb_offset == obj->gtt_offset &&
  1574. dev_priv->cfb_y == crtc->y)
  1575. return;
  1576. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
  1577. POSTING_READ(ILK_DPFC_CONTROL);
  1578. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1579. }
  1580. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  1581. dev_priv->cfb_fence = obj->fence_reg;
  1582. dev_priv->cfb_plane = intel_crtc->plane;
  1583. dev_priv->cfb_offset = obj->gtt_offset;
  1584. dev_priv->cfb_y = crtc->y;
  1585. dpfc_ctl &= DPFC_RESERVED;
  1586. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  1587. if (obj->tiling_mode != I915_TILING_NONE) {
  1588. dpfc_ctl |= (DPFC_CTL_FENCE_EN | dev_priv->cfb_fence);
  1589. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  1590. } else {
  1591. I915_WRITE(ILK_DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  1592. }
  1593. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1594. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1595. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1596. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  1597. I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
  1598. /* enable it... */
  1599. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  1600. if (IS_GEN6(dev)) {
  1601. I915_WRITE(SNB_DPFC_CTL_SA,
  1602. SNB_CPU_FENCE_ENABLE | dev_priv->cfb_fence);
  1603. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  1604. sandybridge_blit_fbc_update(dev);
  1605. }
  1606. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1607. }
  1608. void ironlake_disable_fbc(struct drm_device *dev)
  1609. {
  1610. struct drm_i915_private *dev_priv = dev->dev_private;
  1611. u32 dpfc_ctl;
  1612. /* Disable compression */
  1613. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1614. if (dpfc_ctl & DPFC_CTL_EN) {
  1615. dpfc_ctl &= ~DPFC_CTL_EN;
  1616. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  1617. DRM_DEBUG_KMS("disabled FBC\n");
  1618. }
  1619. }
  1620. static bool ironlake_fbc_enabled(struct drm_device *dev)
  1621. {
  1622. struct drm_i915_private *dev_priv = dev->dev_private;
  1623. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  1624. }
  1625. bool intel_fbc_enabled(struct drm_device *dev)
  1626. {
  1627. struct drm_i915_private *dev_priv = dev->dev_private;
  1628. if (!dev_priv->display.fbc_enabled)
  1629. return false;
  1630. return dev_priv->display.fbc_enabled(dev);
  1631. }
  1632. void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1633. {
  1634. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  1635. if (!dev_priv->display.enable_fbc)
  1636. return;
  1637. dev_priv->display.enable_fbc(crtc, interval);
  1638. }
  1639. void intel_disable_fbc(struct drm_device *dev)
  1640. {
  1641. struct drm_i915_private *dev_priv = dev->dev_private;
  1642. if (!dev_priv->display.disable_fbc)
  1643. return;
  1644. dev_priv->display.disable_fbc(dev);
  1645. }
  1646. /**
  1647. * intel_update_fbc - enable/disable FBC as needed
  1648. * @dev: the drm_device
  1649. *
  1650. * Set up the framebuffer compression hardware at mode set time. We
  1651. * enable it if possible:
  1652. * - plane A only (on pre-965)
  1653. * - no pixel mulitply/line duplication
  1654. * - no alpha buffer discard
  1655. * - no dual wide
  1656. * - framebuffer <= 2048 in width, 1536 in height
  1657. *
  1658. * We can't assume that any compression will take place (worst case),
  1659. * so the compressed buffer has to be the same size as the uncompressed
  1660. * one. It also must reside (along with the line length buffer) in
  1661. * stolen memory.
  1662. *
  1663. * We need to enable/disable FBC on a global basis.
  1664. */
  1665. static void intel_update_fbc(struct drm_device *dev)
  1666. {
  1667. struct drm_i915_private *dev_priv = dev->dev_private;
  1668. struct drm_crtc *crtc = NULL, *tmp_crtc;
  1669. struct intel_crtc *intel_crtc;
  1670. struct drm_framebuffer *fb;
  1671. struct intel_framebuffer *intel_fb;
  1672. struct drm_i915_gem_object *obj;
  1673. DRM_DEBUG_KMS("\n");
  1674. if (!i915_powersave)
  1675. return;
  1676. if (!I915_HAS_FBC(dev))
  1677. return;
  1678. /*
  1679. * If FBC is already on, we just have to verify that we can
  1680. * keep it that way...
  1681. * Need to disable if:
  1682. * - more than one pipe is active
  1683. * - changing FBC params (stride, fence, mode)
  1684. * - new fb is too large to fit in compressed buffer
  1685. * - going to an unsupported config (interlace, pixel multiply, etc.)
  1686. */
  1687. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  1688. if (tmp_crtc->enabled) {
  1689. if (crtc) {
  1690. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  1691. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  1692. goto out_disable;
  1693. }
  1694. crtc = tmp_crtc;
  1695. }
  1696. }
  1697. if (!crtc || crtc->fb == NULL) {
  1698. DRM_DEBUG_KMS("no output, disabling\n");
  1699. dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
  1700. goto out_disable;
  1701. }
  1702. intel_crtc = to_intel_crtc(crtc);
  1703. fb = crtc->fb;
  1704. intel_fb = to_intel_framebuffer(fb);
  1705. obj = intel_fb->obj;
  1706. if (intel_fb->obj->base.size > dev_priv->cfb_size) {
  1707. DRM_DEBUG_KMS("framebuffer too large, disabling "
  1708. "compression\n");
  1709. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  1710. goto out_disable;
  1711. }
  1712. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  1713. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  1714. DRM_DEBUG_KMS("mode incompatible with compression, "
  1715. "disabling\n");
  1716. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  1717. goto out_disable;
  1718. }
  1719. if ((crtc->mode.hdisplay > 2048) ||
  1720. (crtc->mode.vdisplay > 1536)) {
  1721. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  1722. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  1723. goto out_disable;
  1724. }
  1725. if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
  1726. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  1727. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  1728. goto out_disable;
  1729. }
  1730. if (obj->tiling_mode != I915_TILING_X) {
  1731. DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
  1732. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  1733. goto out_disable;
  1734. }
  1735. /* If the kernel debugger is active, always disable compression */
  1736. if (in_dbg_master())
  1737. goto out_disable;
  1738. intel_enable_fbc(crtc, 500);
  1739. return;
  1740. out_disable:
  1741. /* Multiple disables should be harmless */
  1742. if (intel_fbc_enabled(dev)) {
  1743. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  1744. intel_disable_fbc(dev);
  1745. }
  1746. }
  1747. int
  1748. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1749. struct drm_i915_gem_object *obj,
  1750. struct intel_ring_buffer *pipelined)
  1751. {
  1752. u32 alignment;
  1753. int ret;
  1754. switch (obj->tiling_mode) {
  1755. case I915_TILING_NONE:
  1756. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1757. alignment = 128 * 1024;
  1758. else if (INTEL_INFO(dev)->gen >= 4)
  1759. alignment = 4 * 1024;
  1760. else
  1761. alignment = 64 * 1024;
  1762. break;
  1763. case I915_TILING_X:
  1764. /* pin() will align the object as required by fence */
  1765. alignment = 0;
  1766. break;
  1767. case I915_TILING_Y:
  1768. /* FIXME: Is this true? */
  1769. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1770. return -EINVAL;
  1771. default:
  1772. BUG();
  1773. }
  1774. ret = i915_gem_object_pin(obj, alignment, true);
  1775. if (ret)
  1776. return ret;
  1777. ret = i915_gem_object_set_to_display_plane(obj, pipelined);
  1778. if (ret)
  1779. goto err_unpin;
  1780. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1781. * fence, whereas 965+ only requires a fence if using
  1782. * framebuffer compression. For simplicity, we always install
  1783. * a fence as the cost is not that onerous.
  1784. */
  1785. if (obj->tiling_mode != I915_TILING_NONE) {
  1786. ret = i915_gem_object_get_fence(obj, pipelined, false);
  1787. if (ret)
  1788. goto err_unpin;
  1789. }
  1790. return 0;
  1791. err_unpin:
  1792. i915_gem_object_unpin(obj);
  1793. return ret;
  1794. }
  1795. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1796. static int
  1797. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1798. int x, int y, enum mode_set_atomic state)
  1799. {
  1800. struct drm_device *dev = crtc->dev;
  1801. struct drm_i915_private *dev_priv = dev->dev_private;
  1802. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1803. struct intel_framebuffer *intel_fb;
  1804. struct drm_i915_gem_object *obj;
  1805. int plane = intel_crtc->plane;
  1806. unsigned long Start, Offset;
  1807. u32 dspcntr;
  1808. u32 reg;
  1809. switch (plane) {
  1810. case 0:
  1811. case 1:
  1812. break;
  1813. default:
  1814. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1815. return -EINVAL;
  1816. }
  1817. intel_fb = to_intel_framebuffer(fb);
  1818. obj = intel_fb->obj;
  1819. reg = DSPCNTR(plane);
  1820. dspcntr = I915_READ(reg);
  1821. /* Mask out pixel format bits in case we change it */
  1822. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1823. switch (fb->bits_per_pixel) {
  1824. case 8:
  1825. dspcntr |= DISPPLANE_8BPP;
  1826. break;
  1827. case 16:
  1828. if (fb->depth == 15)
  1829. dspcntr |= DISPPLANE_15_16BPP;
  1830. else
  1831. dspcntr |= DISPPLANE_16BPP;
  1832. break;
  1833. case 24:
  1834. case 32:
  1835. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1836. break;
  1837. default:
  1838. DRM_ERROR("Unknown color depth\n");
  1839. return -EINVAL;
  1840. }
  1841. if (INTEL_INFO(dev)->gen >= 4) {
  1842. if (obj->tiling_mode != I915_TILING_NONE)
  1843. dspcntr |= DISPPLANE_TILED;
  1844. else
  1845. dspcntr &= ~DISPPLANE_TILED;
  1846. }
  1847. if (HAS_PCH_SPLIT(dev))
  1848. /* must disable */
  1849. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1850. I915_WRITE(reg, dspcntr);
  1851. Start = obj->gtt_offset;
  1852. Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
  1853. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1854. Start, Offset, x, y, fb->pitch);
  1855. I915_WRITE(DSPSTRIDE(plane), fb->pitch);
  1856. if (INTEL_INFO(dev)->gen >= 4) {
  1857. I915_WRITE(DSPSURF(plane), Start);
  1858. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1859. I915_WRITE(DSPADDR(plane), Offset);
  1860. } else
  1861. I915_WRITE(DSPADDR(plane), Start + Offset);
  1862. POSTING_READ(reg);
  1863. intel_update_fbc(dev);
  1864. intel_increase_pllclock(crtc);
  1865. return 0;
  1866. }
  1867. static int
  1868. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1869. struct drm_framebuffer *old_fb)
  1870. {
  1871. struct drm_device *dev = crtc->dev;
  1872. struct drm_i915_master_private *master_priv;
  1873. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1874. int ret;
  1875. /* no fb bound */
  1876. if (!crtc->fb) {
  1877. DRM_DEBUG_KMS("No FB bound\n");
  1878. return 0;
  1879. }
  1880. switch (intel_crtc->plane) {
  1881. case 0:
  1882. case 1:
  1883. break;
  1884. default:
  1885. return -EINVAL;
  1886. }
  1887. mutex_lock(&dev->struct_mutex);
  1888. ret = intel_pin_and_fence_fb_obj(dev,
  1889. to_intel_framebuffer(crtc->fb)->obj,
  1890. NULL);
  1891. if (ret != 0) {
  1892. mutex_unlock(&dev->struct_mutex);
  1893. return ret;
  1894. }
  1895. if (old_fb) {
  1896. struct drm_i915_private *dev_priv = dev->dev_private;
  1897. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1898. wait_event(dev_priv->pending_flip_queue,
  1899. atomic_read(&obj->pending_flip) == 0);
  1900. /* Big Hammer, we also need to ensure that any pending
  1901. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1902. * current scanout is retired before unpinning the old
  1903. * framebuffer.
  1904. */
  1905. ret = i915_gem_object_flush_gpu(obj, false);
  1906. if (ret) {
  1907. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  1908. mutex_unlock(&dev->struct_mutex);
  1909. return ret;
  1910. }
  1911. }
  1912. ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
  1913. LEAVE_ATOMIC_MODE_SET);
  1914. if (ret) {
  1915. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  1916. mutex_unlock(&dev->struct_mutex);
  1917. return ret;
  1918. }
  1919. if (old_fb) {
  1920. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1921. i915_gem_object_unpin(to_intel_framebuffer(old_fb)->obj);
  1922. }
  1923. mutex_unlock(&dev->struct_mutex);
  1924. if (!dev->primary->master)
  1925. return 0;
  1926. master_priv = dev->primary->master->driver_priv;
  1927. if (!master_priv->sarea_priv)
  1928. return 0;
  1929. if (intel_crtc->pipe) {
  1930. master_priv->sarea_priv->pipeB_x = x;
  1931. master_priv->sarea_priv->pipeB_y = y;
  1932. } else {
  1933. master_priv->sarea_priv->pipeA_x = x;
  1934. master_priv->sarea_priv->pipeA_y = y;
  1935. }
  1936. return 0;
  1937. }
  1938. static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
  1939. {
  1940. struct drm_device *dev = crtc->dev;
  1941. struct drm_i915_private *dev_priv = dev->dev_private;
  1942. u32 dpa_ctl;
  1943. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1944. dpa_ctl = I915_READ(DP_A);
  1945. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1946. if (clock < 200000) {
  1947. u32 temp;
  1948. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1949. /* workaround for 160Mhz:
  1950. 1) program 0x4600c bits 15:0 = 0x8124
  1951. 2) program 0x46010 bit 0 = 1
  1952. 3) program 0x46034 bit 24 = 1
  1953. 4) program 0x64000 bit 14 = 1
  1954. */
  1955. temp = I915_READ(0x4600c);
  1956. temp &= 0xffff0000;
  1957. I915_WRITE(0x4600c, temp | 0x8124);
  1958. temp = I915_READ(0x46010);
  1959. I915_WRITE(0x46010, temp | 1);
  1960. temp = I915_READ(0x46034);
  1961. I915_WRITE(0x46034, temp | (1 << 24));
  1962. } else {
  1963. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1964. }
  1965. I915_WRITE(DP_A, dpa_ctl);
  1966. POSTING_READ(DP_A);
  1967. udelay(500);
  1968. }
  1969. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  1970. {
  1971. struct drm_device *dev = crtc->dev;
  1972. struct drm_i915_private *dev_priv = dev->dev_private;
  1973. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1974. int pipe = intel_crtc->pipe;
  1975. u32 reg, temp;
  1976. /* enable normal train */
  1977. reg = FDI_TX_CTL(pipe);
  1978. temp = I915_READ(reg);
  1979. temp &= ~FDI_LINK_TRAIN_NONE;
  1980. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  1981. I915_WRITE(reg, temp);
  1982. reg = FDI_RX_CTL(pipe);
  1983. temp = I915_READ(reg);
  1984. if (HAS_PCH_CPT(dev)) {
  1985. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1986. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  1987. } else {
  1988. temp &= ~FDI_LINK_TRAIN_NONE;
  1989. temp |= FDI_LINK_TRAIN_NONE;
  1990. }
  1991. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  1992. /* wait one idle pattern time */
  1993. POSTING_READ(reg);
  1994. udelay(1000);
  1995. }
  1996. /* The FDI link training functions for ILK/Ibexpeak. */
  1997. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  1998. {
  1999. struct drm_device *dev = crtc->dev;
  2000. struct drm_i915_private *dev_priv = dev->dev_private;
  2001. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2002. int pipe = intel_crtc->pipe;
  2003. int plane = intel_crtc->plane;
  2004. u32 reg, temp, tries;
  2005. /* FDI needs bits from pipe & plane first */
  2006. assert_pipe_enabled(dev_priv, pipe);
  2007. assert_plane_enabled(dev_priv, plane);
  2008. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2009. for train result */
  2010. reg = FDI_RX_IMR(pipe);
  2011. temp = I915_READ(reg);
  2012. temp &= ~FDI_RX_SYMBOL_LOCK;
  2013. temp &= ~FDI_RX_BIT_LOCK;
  2014. I915_WRITE(reg, temp);
  2015. I915_READ(reg);
  2016. udelay(150);
  2017. /* enable CPU FDI TX and PCH FDI RX */
  2018. reg = FDI_TX_CTL(pipe);
  2019. temp = I915_READ(reg);
  2020. temp &= ~(7 << 19);
  2021. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2022. temp &= ~FDI_LINK_TRAIN_NONE;
  2023. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2024. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2025. reg = FDI_RX_CTL(pipe);
  2026. temp = I915_READ(reg);
  2027. temp &= ~FDI_LINK_TRAIN_NONE;
  2028. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2029. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2030. POSTING_READ(reg);
  2031. udelay(150);
  2032. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2033. if (HAS_PCH_IBX(dev)) {
  2034. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2035. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2036. FDI_RX_PHASE_SYNC_POINTER_EN);
  2037. }
  2038. reg = FDI_RX_IIR(pipe);
  2039. for (tries = 0; tries < 5; tries++) {
  2040. temp = I915_READ(reg);
  2041. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2042. if ((temp & FDI_RX_BIT_LOCK)) {
  2043. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2044. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2045. break;
  2046. }
  2047. }
  2048. if (tries == 5)
  2049. DRM_ERROR("FDI train 1 fail!\n");
  2050. /* Train 2 */
  2051. reg = FDI_TX_CTL(pipe);
  2052. temp = I915_READ(reg);
  2053. temp &= ~FDI_LINK_TRAIN_NONE;
  2054. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2055. I915_WRITE(reg, temp);
  2056. reg = FDI_RX_CTL(pipe);
  2057. temp = I915_READ(reg);
  2058. temp &= ~FDI_LINK_TRAIN_NONE;
  2059. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2060. I915_WRITE(reg, temp);
  2061. POSTING_READ(reg);
  2062. udelay(150);
  2063. reg = FDI_RX_IIR(pipe);
  2064. for (tries = 0; tries < 5; tries++) {
  2065. temp = I915_READ(reg);
  2066. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2067. if (temp & FDI_RX_SYMBOL_LOCK) {
  2068. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2069. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2070. break;
  2071. }
  2072. }
  2073. if (tries == 5)
  2074. DRM_ERROR("FDI train 2 fail!\n");
  2075. DRM_DEBUG_KMS("FDI train done\n");
  2076. }
  2077. static const int snb_b_fdi_train_param [] = {
  2078. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2079. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2080. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2081. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2082. };
  2083. /* The FDI link training functions for SNB/Cougarpoint. */
  2084. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2085. {
  2086. struct drm_device *dev = crtc->dev;
  2087. struct drm_i915_private *dev_priv = dev->dev_private;
  2088. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2089. int pipe = intel_crtc->pipe;
  2090. u32 reg, temp, i;
  2091. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2092. for train result */
  2093. reg = FDI_RX_IMR(pipe);
  2094. temp = I915_READ(reg);
  2095. temp &= ~FDI_RX_SYMBOL_LOCK;
  2096. temp &= ~FDI_RX_BIT_LOCK;
  2097. I915_WRITE(reg, temp);
  2098. POSTING_READ(reg);
  2099. udelay(150);
  2100. /* enable CPU FDI TX and PCH FDI RX */
  2101. reg = FDI_TX_CTL(pipe);
  2102. temp = I915_READ(reg);
  2103. temp &= ~(7 << 19);
  2104. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2105. temp &= ~FDI_LINK_TRAIN_NONE;
  2106. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2107. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2108. /* SNB-B */
  2109. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2110. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2111. reg = FDI_RX_CTL(pipe);
  2112. temp = I915_READ(reg);
  2113. if (HAS_PCH_CPT(dev)) {
  2114. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2115. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2116. } else {
  2117. temp &= ~FDI_LINK_TRAIN_NONE;
  2118. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2119. }
  2120. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2121. POSTING_READ(reg);
  2122. udelay(150);
  2123. for (i = 0; i < 4; i++ ) {
  2124. reg = FDI_TX_CTL(pipe);
  2125. temp = I915_READ(reg);
  2126. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2127. temp |= snb_b_fdi_train_param[i];
  2128. I915_WRITE(reg, temp);
  2129. POSTING_READ(reg);
  2130. udelay(500);
  2131. reg = FDI_RX_IIR(pipe);
  2132. temp = I915_READ(reg);
  2133. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2134. if (temp & FDI_RX_BIT_LOCK) {
  2135. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2136. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2137. break;
  2138. }
  2139. }
  2140. if (i == 4)
  2141. DRM_ERROR("FDI train 1 fail!\n");
  2142. /* Train 2 */
  2143. reg = FDI_TX_CTL(pipe);
  2144. temp = I915_READ(reg);
  2145. temp &= ~FDI_LINK_TRAIN_NONE;
  2146. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2147. if (IS_GEN6(dev)) {
  2148. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2149. /* SNB-B */
  2150. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2151. }
  2152. I915_WRITE(reg, temp);
  2153. reg = FDI_RX_CTL(pipe);
  2154. temp = I915_READ(reg);
  2155. if (HAS_PCH_CPT(dev)) {
  2156. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2157. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2158. } else {
  2159. temp &= ~FDI_LINK_TRAIN_NONE;
  2160. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2161. }
  2162. I915_WRITE(reg, temp);
  2163. POSTING_READ(reg);
  2164. udelay(150);
  2165. for (i = 0; i < 4; i++ ) {
  2166. reg = FDI_TX_CTL(pipe);
  2167. temp = I915_READ(reg);
  2168. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2169. temp |= snb_b_fdi_train_param[i];
  2170. I915_WRITE(reg, temp);
  2171. POSTING_READ(reg);
  2172. udelay(500);
  2173. reg = FDI_RX_IIR(pipe);
  2174. temp = I915_READ(reg);
  2175. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2176. if (temp & FDI_RX_SYMBOL_LOCK) {
  2177. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2178. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2179. break;
  2180. }
  2181. }
  2182. if (i == 4)
  2183. DRM_ERROR("FDI train 2 fail!\n");
  2184. DRM_DEBUG_KMS("FDI train done.\n");
  2185. }
  2186. static void ironlake_fdi_enable(struct drm_crtc *crtc)
  2187. {
  2188. struct drm_device *dev = crtc->dev;
  2189. struct drm_i915_private *dev_priv = dev->dev_private;
  2190. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2191. int pipe = intel_crtc->pipe;
  2192. u32 reg, temp;
  2193. /* Write the TU size bits so error detection works */
  2194. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2195. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2196. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2197. reg = FDI_RX_CTL(pipe);
  2198. temp = I915_READ(reg);
  2199. temp &= ~((0x7 << 19) | (0x7 << 16));
  2200. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2201. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2202. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2203. POSTING_READ(reg);
  2204. udelay(200);
  2205. /* Switch from Rawclk to PCDclk */
  2206. temp = I915_READ(reg);
  2207. I915_WRITE(reg, temp | FDI_PCDCLK);
  2208. POSTING_READ(reg);
  2209. udelay(200);
  2210. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2211. reg = FDI_TX_CTL(pipe);
  2212. temp = I915_READ(reg);
  2213. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2214. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2215. POSTING_READ(reg);
  2216. udelay(100);
  2217. }
  2218. }
  2219. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2220. {
  2221. struct drm_device *dev = crtc->dev;
  2222. struct drm_i915_private *dev_priv = dev->dev_private;
  2223. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2224. int pipe = intel_crtc->pipe;
  2225. u32 reg, temp;
  2226. /* disable CPU FDI tx and PCH FDI rx */
  2227. reg = FDI_TX_CTL(pipe);
  2228. temp = I915_READ(reg);
  2229. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2230. POSTING_READ(reg);
  2231. reg = FDI_RX_CTL(pipe);
  2232. temp = I915_READ(reg);
  2233. temp &= ~(0x7 << 16);
  2234. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2235. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2236. POSTING_READ(reg);
  2237. udelay(100);
  2238. /* Ironlake workaround, disable clock pointer after downing FDI */
  2239. if (HAS_PCH_IBX(dev)) {
  2240. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2241. I915_WRITE(FDI_RX_CHICKEN(pipe),
  2242. I915_READ(FDI_RX_CHICKEN(pipe) &
  2243. ~FDI_RX_PHASE_SYNC_POINTER_EN));
  2244. }
  2245. /* still set train pattern 1 */
  2246. reg = FDI_TX_CTL(pipe);
  2247. temp = I915_READ(reg);
  2248. temp &= ~FDI_LINK_TRAIN_NONE;
  2249. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2250. I915_WRITE(reg, temp);
  2251. reg = FDI_RX_CTL(pipe);
  2252. temp = I915_READ(reg);
  2253. if (HAS_PCH_CPT(dev)) {
  2254. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2255. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2256. } else {
  2257. temp &= ~FDI_LINK_TRAIN_NONE;
  2258. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2259. }
  2260. /* BPC in FDI rx is consistent with that in PIPECONF */
  2261. temp &= ~(0x07 << 16);
  2262. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2263. I915_WRITE(reg, temp);
  2264. POSTING_READ(reg);
  2265. udelay(100);
  2266. }
  2267. /*
  2268. * When we disable a pipe, we need to clear any pending scanline wait events
  2269. * to avoid hanging the ring, which we assume we are waiting on.
  2270. */
  2271. static void intel_clear_scanline_wait(struct drm_device *dev)
  2272. {
  2273. struct drm_i915_private *dev_priv = dev->dev_private;
  2274. struct intel_ring_buffer *ring;
  2275. u32 tmp;
  2276. if (IS_GEN2(dev))
  2277. /* Can't break the hang on i8xx */
  2278. return;
  2279. ring = LP_RING(dev_priv);
  2280. tmp = I915_READ_CTL(ring);
  2281. if (tmp & RING_WAIT)
  2282. I915_WRITE_CTL(ring, tmp);
  2283. }
  2284. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2285. {
  2286. struct drm_i915_gem_object *obj;
  2287. struct drm_i915_private *dev_priv;
  2288. if (crtc->fb == NULL)
  2289. return;
  2290. obj = to_intel_framebuffer(crtc->fb)->obj;
  2291. dev_priv = crtc->dev->dev_private;
  2292. wait_event(dev_priv->pending_flip_queue,
  2293. atomic_read(&obj->pending_flip) == 0);
  2294. }
  2295. static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
  2296. {
  2297. struct drm_device *dev = crtc->dev;
  2298. struct drm_mode_config *mode_config = &dev->mode_config;
  2299. struct intel_encoder *encoder;
  2300. /*
  2301. * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
  2302. * must be driven by its own crtc; no sharing is possible.
  2303. */
  2304. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  2305. if (encoder->base.crtc != crtc)
  2306. continue;
  2307. switch (encoder->type) {
  2308. case INTEL_OUTPUT_EDP:
  2309. if (!intel_encoder_is_pch_edp(&encoder->base))
  2310. return false;
  2311. continue;
  2312. }
  2313. }
  2314. return true;
  2315. }
  2316. /*
  2317. * Enable PCH resources required for PCH ports:
  2318. * - PCH PLLs
  2319. * - FDI training & RX/TX
  2320. * - update transcoder timings
  2321. * - DP transcoding bits
  2322. * - transcoder
  2323. */
  2324. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2325. {
  2326. struct drm_device *dev = crtc->dev;
  2327. struct drm_i915_private *dev_priv = dev->dev_private;
  2328. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2329. int pipe = intel_crtc->pipe;
  2330. u32 reg, temp;
  2331. /* For PCH output, training FDI link */
  2332. if (IS_GEN6(dev))
  2333. gen6_fdi_link_train(crtc);
  2334. else
  2335. ironlake_fdi_link_train(crtc);
  2336. intel_enable_pch_pll(dev_priv, pipe);
  2337. if (HAS_PCH_CPT(dev)) {
  2338. /* Be sure PCH DPLL SEL is set */
  2339. temp = I915_READ(PCH_DPLL_SEL);
  2340. if (pipe == 0 && (temp & TRANSA_DPLL_ENABLE) == 0)
  2341. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  2342. else if (pipe == 1 && (temp & TRANSB_DPLL_ENABLE) == 0)
  2343. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2344. I915_WRITE(PCH_DPLL_SEL, temp);
  2345. }
  2346. /* set transcoder timing, panel must allow it */
  2347. assert_panel_unlocked(dev_priv, pipe);
  2348. I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
  2349. I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
  2350. I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
  2351. I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
  2352. I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
  2353. I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
  2354. intel_fdi_normal_train(crtc);
  2355. /* For PCH DP, enable TRANS_DP_CTL */
  2356. if (HAS_PCH_CPT(dev) &&
  2357. intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  2358. reg = TRANS_DP_CTL(pipe);
  2359. temp = I915_READ(reg);
  2360. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  2361. TRANS_DP_SYNC_MASK |
  2362. TRANS_DP_BPC_MASK);
  2363. temp |= (TRANS_DP_OUTPUT_ENABLE |
  2364. TRANS_DP_ENH_FRAMING);
  2365. temp |= TRANS_DP_8BPC;
  2366. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  2367. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  2368. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  2369. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  2370. switch (intel_trans_dp_port_sel(crtc)) {
  2371. case PCH_DP_B:
  2372. temp |= TRANS_DP_PORT_SEL_B;
  2373. break;
  2374. case PCH_DP_C:
  2375. temp |= TRANS_DP_PORT_SEL_C;
  2376. break;
  2377. case PCH_DP_D:
  2378. temp |= TRANS_DP_PORT_SEL_D;
  2379. break;
  2380. default:
  2381. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  2382. temp |= TRANS_DP_PORT_SEL_B;
  2383. break;
  2384. }
  2385. I915_WRITE(reg, temp);
  2386. }
  2387. intel_enable_transcoder(dev_priv, pipe);
  2388. }
  2389. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  2390. {
  2391. struct drm_device *dev = crtc->dev;
  2392. struct drm_i915_private *dev_priv = dev->dev_private;
  2393. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2394. int pipe = intel_crtc->pipe;
  2395. int plane = intel_crtc->plane;
  2396. u32 temp;
  2397. bool is_pch_port;
  2398. if (intel_crtc->active)
  2399. return;
  2400. intel_crtc->active = true;
  2401. intel_update_watermarks(dev);
  2402. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  2403. temp = I915_READ(PCH_LVDS);
  2404. if ((temp & LVDS_PORT_EN) == 0)
  2405. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  2406. }
  2407. is_pch_port = intel_crtc_driving_pch(crtc);
  2408. if (is_pch_port)
  2409. ironlake_fdi_enable(crtc);
  2410. else
  2411. ironlake_fdi_disable(crtc);
  2412. /* Enable panel fitting for LVDS */
  2413. if (dev_priv->pch_pf_size &&
  2414. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
  2415. /* Force use of hard-coded filter coefficients
  2416. * as some pre-programmed values are broken,
  2417. * e.g. x201.
  2418. */
  2419. I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1,
  2420. PF_ENABLE | PF_FILTER_MED_3x3);
  2421. I915_WRITE(pipe ? PFB_WIN_POS : PFA_WIN_POS,
  2422. dev_priv->pch_pf_pos);
  2423. I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ,
  2424. dev_priv->pch_pf_size);
  2425. }
  2426. intel_enable_pipe(dev_priv, pipe, is_pch_port);
  2427. intel_enable_plane(dev_priv, plane, pipe);
  2428. if (is_pch_port)
  2429. ironlake_pch_enable(crtc);
  2430. intel_crtc_load_lut(crtc);
  2431. intel_update_fbc(dev);
  2432. intel_crtc_update_cursor(crtc, true);
  2433. }
  2434. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  2435. {
  2436. struct drm_device *dev = crtc->dev;
  2437. struct drm_i915_private *dev_priv = dev->dev_private;
  2438. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2439. int pipe = intel_crtc->pipe;
  2440. int plane = intel_crtc->plane;
  2441. u32 reg, temp;
  2442. if (!intel_crtc->active)
  2443. return;
  2444. intel_crtc_wait_for_pending_flips(crtc);
  2445. drm_vblank_off(dev, pipe);
  2446. intel_crtc_update_cursor(crtc, false);
  2447. intel_disable_plane(dev_priv, plane, pipe);
  2448. if (dev_priv->cfb_plane == plane &&
  2449. dev_priv->display.disable_fbc)
  2450. dev_priv->display.disable_fbc(dev);
  2451. intel_disable_pipe(dev_priv, pipe);
  2452. /* Disable PF */
  2453. I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1, 0);
  2454. I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ, 0);
  2455. ironlake_fdi_disable(crtc);
  2456. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  2457. temp = I915_READ(PCH_LVDS);
  2458. if (temp & LVDS_PORT_EN) {
  2459. I915_WRITE(PCH_LVDS, temp & ~LVDS_PORT_EN);
  2460. POSTING_READ(PCH_LVDS);
  2461. udelay(100);
  2462. }
  2463. }
  2464. intel_disable_transcoder(dev_priv, pipe);
  2465. if (HAS_PCH_CPT(dev)) {
  2466. /* disable TRANS_DP_CTL */
  2467. reg = TRANS_DP_CTL(pipe);
  2468. temp = I915_READ(reg);
  2469. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  2470. I915_WRITE(reg, temp);
  2471. /* disable DPLL_SEL */
  2472. temp = I915_READ(PCH_DPLL_SEL);
  2473. if (pipe == 0)
  2474. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  2475. else
  2476. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2477. I915_WRITE(PCH_DPLL_SEL, temp);
  2478. }
  2479. /* disable PCH DPLL */
  2480. intel_disable_pch_pll(dev_priv, pipe);
  2481. /* Switch from PCDclk to Rawclk */
  2482. reg = FDI_RX_CTL(pipe);
  2483. temp = I915_READ(reg);
  2484. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2485. /* Disable CPU FDI TX PLL */
  2486. reg = FDI_TX_CTL(pipe);
  2487. temp = I915_READ(reg);
  2488. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2489. POSTING_READ(reg);
  2490. udelay(100);
  2491. reg = FDI_RX_CTL(pipe);
  2492. temp = I915_READ(reg);
  2493. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2494. /* Wait for the clocks to turn off. */
  2495. POSTING_READ(reg);
  2496. udelay(100);
  2497. intel_crtc->active = false;
  2498. intel_update_watermarks(dev);
  2499. intel_update_fbc(dev);
  2500. intel_clear_scanline_wait(dev);
  2501. }
  2502. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  2503. {
  2504. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2505. int pipe = intel_crtc->pipe;
  2506. int plane = intel_crtc->plane;
  2507. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2508. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2509. */
  2510. switch (mode) {
  2511. case DRM_MODE_DPMS_ON:
  2512. case DRM_MODE_DPMS_STANDBY:
  2513. case DRM_MODE_DPMS_SUSPEND:
  2514. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  2515. ironlake_crtc_enable(crtc);
  2516. break;
  2517. case DRM_MODE_DPMS_OFF:
  2518. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  2519. ironlake_crtc_disable(crtc);
  2520. break;
  2521. }
  2522. }
  2523. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2524. {
  2525. if (!enable && intel_crtc->overlay) {
  2526. struct drm_device *dev = intel_crtc->base.dev;
  2527. mutex_lock(&dev->struct_mutex);
  2528. (void) intel_overlay_switch_off(intel_crtc->overlay, false);
  2529. mutex_unlock(&dev->struct_mutex);
  2530. }
  2531. /* Let userspace switch the overlay on again. In most cases userspace
  2532. * has to recompute where to put it anyway.
  2533. */
  2534. }
  2535. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  2536. {
  2537. struct drm_device *dev = crtc->dev;
  2538. struct drm_i915_private *dev_priv = dev->dev_private;
  2539. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2540. int pipe = intel_crtc->pipe;
  2541. int plane = intel_crtc->plane;
  2542. if (intel_crtc->active)
  2543. return;
  2544. intel_crtc->active = true;
  2545. intel_update_watermarks(dev);
  2546. intel_enable_pll(dev_priv, pipe);
  2547. intel_enable_pipe(dev_priv, pipe, false);
  2548. intel_enable_plane(dev_priv, plane, pipe);
  2549. intel_crtc_load_lut(crtc);
  2550. intel_update_fbc(dev);
  2551. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2552. intel_crtc_dpms_overlay(intel_crtc, true);
  2553. intel_crtc_update_cursor(crtc, true);
  2554. }
  2555. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  2556. {
  2557. struct drm_device *dev = crtc->dev;
  2558. struct drm_i915_private *dev_priv = dev->dev_private;
  2559. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2560. int pipe = intel_crtc->pipe;
  2561. int plane = intel_crtc->plane;
  2562. if (!intel_crtc->active)
  2563. return;
  2564. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2565. intel_crtc_wait_for_pending_flips(crtc);
  2566. drm_vblank_off(dev, pipe);
  2567. intel_crtc_dpms_overlay(intel_crtc, false);
  2568. intel_crtc_update_cursor(crtc, false);
  2569. if (dev_priv->cfb_plane == plane &&
  2570. dev_priv->display.disable_fbc)
  2571. dev_priv->display.disable_fbc(dev);
  2572. intel_disable_plane(dev_priv, plane, pipe);
  2573. intel_disable_pipe(dev_priv, pipe);
  2574. intel_disable_pll(dev_priv, pipe);
  2575. intel_crtc->active = false;
  2576. intel_update_fbc(dev);
  2577. intel_update_watermarks(dev);
  2578. intel_clear_scanline_wait(dev);
  2579. }
  2580. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2581. {
  2582. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2583. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2584. */
  2585. switch (mode) {
  2586. case DRM_MODE_DPMS_ON:
  2587. case DRM_MODE_DPMS_STANDBY:
  2588. case DRM_MODE_DPMS_SUSPEND:
  2589. i9xx_crtc_enable(crtc);
  2590. break;
  2591. case DRM_MODE_DPMS_OFF:
  2592. i9xx_crtc_disable(crtc);
  2593. break;
  2594. }
  2595. }
  2596. /**
  2597. * Sets the power management mode of the pipe and plane.
  2598. */
  2599. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2600. {
  2601. struct drm_device *dev = crtc->dev;
  2602. struct drm_i915_private *dev_priv = dev->dev_private;
  2603. struct drm_i915_master_private *master_priv;
  2604. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2605. int pipe = intel_crtc->pipe;
  2606. bool enabled;
  2607. if (intel_crtc->dpms_mode == mode)
  2608. return;
  2609. intel_crtc->dpms_mode = mode;
  2610. dev_priv->display.dpms(crtc, mode);
  2611. if (!dev->primary->master)
  2612. return;
  2613. master_priv = dev->primary->master->driver_priv;
  2614. if (!master_priv->sarea_priv)
  2615. return;
  2616. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2617. switch (pipe) {
  2618. case 0:
  2619. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2620. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2621. break;
  2622. case 1:
  2623. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2624. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2625. break;
  2626. default:
  2627. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  2628. break;
  2629. }
  2630. }
  2631. static void intel_crtc_disable(struct drm_crtc *crtc)
  2632. {
  2633. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2634. struct drm_device *dev = crtc->dev;
  2635. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  2636. if (crtc->fb) {
  2637. mutex_lock(&dev->struct_mutex);
  2638. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  2639. mutex_unlock(&dev->struct_mutex);
  2640. }
  2641. }
  2642. /* Prepare for a mode set.
  2643. *
  2644. * Note we could be a lot smarter here. We need to figure out which outputs
  2645. * will be enabled, which disabled (in short, how the config will changes)
  2646. * and perform the minimum necessary steps to accomplish that, e.g. updating
  2647. * watermarks, FBC configuration, making sure PLLs are programmed correctly,
  2648. * panel fitting is in the proper state, etc.
  2649. */
  2650. static void i9xx_crtc_prepare(struct drm_crtc *crtc)
  2651. {
  2652. i9xx_crtc_disable(crtc);
  2653. }
  2654. static void i9xx_crtc_commit(struct drm_crtc *crtc)
  2655. {
  2656. i9xx_crtc_enable(crtc);
  2657. }
  2658. static void ironlake_crtc_prepare(struct drm_crtc *crtc)
  2659. {
  2660. ironlake_crtc_disable(crtc);
  2661. }
  2662. static void ironlake_crtc_commit(struct drm_crtc *crtc)
  2663. {
  2664. ironlake_crtc_enable(crtc);
  2665. }
  2666. void intel_encoder_prepare (struct drm_encoder *encoder)
  2667. {
  2668. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2669. /* lvds has its own version of prepare see intel_lvds_prepare */
  2670. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2671. }
  2672. void intel_encoder_commit (struct drm_encoder *encoder)
  2673. {
  2674. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2675. /* lvds has its own version of commit see intel_lvds_commit */
  2676. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2677. }
  2678. void intel_encoder_destroy(struct drm_encoder *encoder)
  2679. {
  2680. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2681. drm_encoder_cleanup(encoder);
  2682. kfree(intel_encoder);
  2683. }
  2684. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2685. struct drm_display_mode *mode,
  2686. struct drm_display_mode *adjusted_mode)
  2687. {
  2688. struct drm_device *dev = crtc->dev;
  2689. if (HAS_PCH_SPLIT(dev)) {
  2690. /* FDI link clock is fixed at 2.7G */
  2691. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  2692. return false;
  2693. }
  2694. /* XXX some encoders set the crtcinfo, others don't.
  2695. * Obviously we need some form of conflict resolution here...
  2696. */
  2697. if (adjusted_mode->crtc_htotal == 0)
  2698. drm_mode_set_crtcinfo(adjusted_mode, 0);
  2699. return true;
  2700. }
  2701. static int i945_get_display_clock_speed(struct drm_device *dev)
  2702. {
  2703. return 400000;
  2704. }
  2705. static int i915_get_display_clock_speed(struct drm_device *dev)
  2706. {
  2707. return 333000;
  2708. }
  2709. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2710. {
  2711. return 200000;
  2712. }
  2713. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2714. {
  2715. u16 gcfgc = 0;
  2716. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2717. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2718. return 133000;
  2719. else {
  2720. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2721. case GC_DISPLAY_CLOCK_333_MHZ:
  2722. return 333000;
  2723. default:
  2724. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2725. return 190000;
  2726. }
  2727. }
  2728. }
  2729. static int i865_get_display_clock_speed(struct drm_device *dev)
  2730. {
  2731. return 266000;
  2732. }
  2733. static int i855_get_display_clock_speed(struct drm_device *dev)
  2734. {
  2735. u16 hpllcc = 0;
  2736. /* Assume that the hardware is in the high speed state. This
  2737. * should be the default.
  2738. */
  2739. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2740. case GC_CLOCK_133_200:
  2741. case GC_CLOCK_100_200:
  2742. return 200000;
  2743. case GC_CLOCK_166_250:
  2744. return 250000;
  2745. case GC_CLOCK_100_133:
  2746. return 133000;
  2747. }
  2748. /* Shouldn't happen */
  2749. return 0;
  2750. }
  2751. static int i830_get_display_clock_speed(struct drm_device *dev)
  2752. {
  2753. return 133000;
  2754. }
  2755. struct fdi_m_n {
  2756. u32 tu;
  2757. u32 gmch_m;
  2758. u32 gmch_n;
  2759. u32 link_m;
  2760. u32 link_n;
  2761. };
  2762. static void
  2763. fdi_reduce_ratio(u32 *num, u32 *den)
  2764. {
  2765. while (*num > 0xffffff || *den > 0xffffff) {
  2766. *num >>= 1;
  2767. *den >>= 1;
  2768. }
  2769. }
  2770. static void
  2771. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  2772. int link_clock, struct fdi_m_n *m_n)
  2773. {
  2774. m_n->tu = 64; /* default size */
  2775. /* BUG_ON(pixel_clock > INT_MAX / 36); */
  2776. m_n->gmch_m = bits_per_pixel * pixel_clock;
  2777. m_n->gmch_n = link_clock * nlanes * 8;
  2778. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  2779. m_n->link_m = pixel_clock;
  2780. m_n->link_n = link_clock;
  2781. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  2782. }
  2783. struct intel_watermark_params {
  2784. unsigned long fifo_size;
  2785. unsigned long max_wm;
  2786. unsigned long default_wm;
  2787. unsigned long guard_size;
  2788. unsigned long cacheline_size;
  2789. };
  2790. /* Pineview has different values for various configs */
  2791. static struct intel_watermark_params pineview_display_wm = {
  2792. PINEVIEW_DISPLAY_FIFO,
  2793. PINEVIEW_MAX_WM,
  2794. PINEVIEW_DFT_WM,
  2795. PINEVIEW_GUARD_WM,
  2796. PINEVIEW_FIFO_LINE_SIZE
  2797. };
  2798. static struct intel_watermark_params pineview_display_hplloff_wm = {
  2799. PINEVIEW_DISPLAY_FIFO,
  2800. PINEVIEW_MAX_WM,
  2801. PINEVIEW_DFT_HPLLOFF_WM,
  2802. PINEVIEW_GUARD_WM,
  2803. PINEVIEW_FIFO_LINE_SIZE
  2804. };
  2805. static struct intel_watermark_params pineview_cursor_wm = {
  2806. PINEVIEW_CURSOR_FIFO,
  2807. PINEVIEW_CURSOR_MAX_WM,
  2808. PINEVIEW_CURSOR_DFT_WM,
  2809. PINEVIEW_CURSOR_GUARD_WM,
  2810. PINEVIEW_FIFO_LINE_SIZE,
  2811. };
  2812. static struct intel_watermark_params pineview_cursor_hplloff_wm = {
  2813. PINEVIEW_CURSOR_FIFO,
  2814. PINEVIEW_CURSOR_MAX_WM,
  2815. PINEVIEW_CURSOR_DFT_WM,
  2816. PINEVIEW_CURSOR_GUARD_WM,
  2817. PINEVIEW_FIFO_LINE_SIZE
  2818. };
  2819. static struct intel_watermark_params g4x_wm_info = {
  2820. G4X_FIFO_SIZE,
  2821. G4X_MAX_WM,
  2822. G4X_MAX_WM,
  2823. 2,
  2824. G4X_FIFO_LINE_SIZE,
  2825. };
  2826. static struct intel_watermark_params g4x_cursor_wm_info = {
  2827. I965_CURSOR_FIFO,
  2828. I965_CURSOR_MAX_WM,
  2829. I965_CURSOR_DFT_WM,
  2830. 2,
  2831. G4X_FIFO_LINE_SIZE,
  2832. };
  2833. static struct intel_watermark_params i965_cursor_wm_info = {
  2834. I965_CURSOR_FIFO,
  2835. I965_CURSOR_MAX_WM,
  2836. I965_CURSOR_DFT_WM,
  2837. 2,
  2838. I915_FIFO_LINE_SIZE,
  2839. };
  2840. static struct intel_watermark_params i945_wm_info = {
  2841. I945_FIFO_SIZE,
  2842. I915_MAX_WM,
  2843. 1,
  2844. 2,
  2845. I915_FIFO_LINE_SIZE
  2846. };
  2847. static struct intel_watermark_params i915_wm_info = {
  2848. I915_FIFO_SIZE,
  2849. I915_MAX_WM,
  2850. 1,
  2851. 2,
  2852. I915_FIFO_LINE_SIZE
  2853. };
  2854. static struct intel_watermark_params i855_wm_info = {
  2855. I855GM_FIFO_SIZE,
  2856. I915_MAX_WM,
  2857. 1,
  2858. 2,
  2859. I830_FIFO_LINE_SIZE
  2860. };
  2861. static struct intel_watermark_params i830_wm_info = {
  2862. I830_FIFO_SIZE,
  2863. I915_MAX_WM,
  2864. 1,
  2865. 2,
  2866. I830_FIFO_LINE_SIZE
  2867. };
  2868. static struct intel_watermark_params ironlake_display_wm_info = {
  2869. ILK_DISPLAY_FIFO,
  2870. ILK_DISPLAY_MAXWM,
  2871. ILK_DISPLAY_DFTWM,
  2872. 2,
  2873. ILK_FIFO_LINE_SIZE
  2874. };
  2875. static struct intel_watermark_params ironlake_cursor_wm_info = {
  2876. ILK_CURSOR_FIFO,
  2877. ILK_CURSOR_MAXWM,
  2878. ILK_CURSOR_DFTWM,
  2879. 2,
  2880. ILK_FIFO_LINE_SIZE
  2881. };
  2882. static struct intel_watermark_params ironlake_display_srwm_info = {
  2883. ILK_DISPLAY_SR_FIFO,
  2884. ILK_DISPLAY_MAX_SRWM,
  2885. ILK_DISPLAY_DFT_SRWM,
  2886. 2,
  2887. ILK_FIFO_LINE_SIZE
  2888. };
  2889. static struct intel_watermark_params ironlake_cursor_srwm_info = {
  2890. ILK_CURSOR_SR_FIFO,
  2891. ILK_CURSOR_MAX_SRWM,
  2892. ILK_CURSOR_DFT_SRWM,
  2893. 2,
  2894. ILK_FIFO_LINE_SIZE
  2895. };
  2896. static struct intel_watermark_params sandybridge_display_wm_info = {
  2897. SNB_DISPLAY_FIFO,
  2898. SNB_DISPLAY_MAXWM,
  2899. SNB_DISPLAY_DFTWM,
  2900. 2,
  2901. SNB_FIFO_LINE_SIZE
  2902. };
  2903. static struct intel_watermark_params sandybridge_cursor_wm_info = {
  2904. SNB_CURSOR_FIFO,
  2905. SNB_CURSOR_MAXWM,
  2906. SNB_CURSOR_DFTWM,
  2907. 2,
  2908. SNB_FIFO_LINE_SIZE
  2909. };
  2910. static struct intel_watermark_params sandybridge_display_srwm_info = {
  2911. SNB_DISPLAY_SR_FIFO,
  2912. SNB_DISPLAY_MAX_SRWM,
  2913. SNB_DISPLAY_DFT_SRWM,
  2914. 2,
  2915. SNB_FIFO_LINE_SIZE
  2916. };
  2917. static struct intel_watermark_params sandybridge_cursor_srwm_info = {
  2918. SNB_CURSOR_SR_FIFO,
  2919. SNB_CURSOR_MAX_SRWM,
  2920. SNB_CURSOR_DFT_SRWM,
  2921. 2,
  2922. SNB_FIFO_LINE_SIZE
  2923. };
  2924. /**
  2925. * intel_calculate_wm - calculate watermark level
  2926. * @clock_in_khz: pixel clock
  2927. * @wm: chip FIFO params
  2928. * @pixel_size: display pixel size
  2929. * @latency_ns: memory latency for the platform
  2930. *
  2931. * Calculate the watermark level (the level at which the display plane will
  2932. * start fetching from memory again). Each chip has a different display
  2933. * FIFO size and allocation, so the caller needs to figure that out and pass
  2934. * in the correct intel_watermark_params structure.
  2935. *
  2936. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  2937. * on the pixel size. When it reaches the watermark level, it'll start
  2938. * fetching FIFO line sized based chunks from memory until the FIFO fills
  2939. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  2940. * will occur, and a display engine hang could result.
  2941. */
  2942. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  2943. struct intel_watermark_params *wm,
  2944. int pixel_size,
  2945. unsigned long latency_ns)
  2946. {
  2947. long entries_required, wm_size;
  2948. /*
  2949. * Note: we need to make sure we don't overflow for various clock &
  2950. * latency values.
  2951. * clocks go from a few thousand to several hundred thousand.
  2952. * latency is usually a few thousand
  2953. */
  2954. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  2955. 1000;
  2956. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  2957. DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries_required);
  2958. wm_size = wm->fifo_size - (entries_required + wm->guard_size);
  2959. DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
  2960. /* Don't promote wm_size to unsigned... */
  2961. if (wm_size > (long)wm->max_wm)
  2962. wm_size = wm->max_wm;
  2963. if (wm_size <= 0)
  2964. wm_size = wm->default_wm;
  2965. return wm_size;
  2966. }
  2967. struct cxsr_latency {
  2968. int is_desktop;
  2969. int is_ddr3;
  2970. unsigned long fsb_freq;
  2971. unsigned long mem_freq;
  2972. unsigned long display_sr;
  2973. unsigned long display_hpll_disable;
  2974. unsigned long cursor_sr;
  2975. unsigned long cursor_hpll_disable;
  2976. };
  2977. static const struct cxsr_latency cxsr_latency_table[] = {
  2978. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  2979. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  2980. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  2981. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  2982. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  2983. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  2984. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  2985. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  2986. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  2987. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  2988. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  2989. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  2990. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  2991. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  2992. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  2993. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  2994. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  2995. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  2996. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  2997. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  2998. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  2999. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  3000. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  3001. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  3002. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  3003. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  3004. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  3005. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  3006. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  3007. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  3008. };
  3009. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  3010. int is_ddr3,
  3011. int fsb,
  3012. int mem)
  3013. {
  3014. const struct cxsr_latency *latency;
  3015. int i;
  3016. if (fsb == 0 || mem == 0)
  3017. return NULL;
  3018. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  3019. latency = &cxsr_latency_table[i];
  3020. if (is_desktop == latency->is_desktop &&
  3021. is_ddr3 == latency->is_ddr3 &&
  3022. fsb == latency->fsb_freq && mem == latency->mem_freq)
  3023. return latency;
  3024. }
  3025. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  3026. return NULL;
  3027. }
  3028. static void pineview_disable_cxsr(struct drm_device *dev)
  3029. {
  3030. struct drm_i915_private *dev_priv = dev->dev_private;
  3031. /* deactivate cxsr */
  3032. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  3033. }
  3034. /*
  3035. * Latency for FIFO fetches is dependent on several factors:
  3036. * - memory configuration (speed, channels)
  3037. * - chipset
  3038. * - current MCH state
  3039. * It can be fairly high in some situations, so here we assume a fairly
  3040. * pessimal value. It's a tradeoff between extra memory fetches (if we
  3041. * set this value too high, the FIFO will fetch frequently to stay full)
  3042. * and power consumption (set it too low to save power and we might see
  3043. * FIFO underruns and display "flicker").
  3044. *
  3045. * A value of 5us seems to be a good balance; safe for very low end
  3046. * platforms but not overly aggressive on lower latency configs.
  3047. */
  3048. static const int latency_ns = 5000;
  3049. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  3050. {
  3051. struct drm_i915_private *dev_priv = dev->dev_private;
  3052. uint32_t dsparb = I915_READ(DSPARB);
  3053. int size;
  3054. size = dsparb & 0x7f;
  3055. if (plane)
  3056. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  3057. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3058. plane ? "B" : "A", size);
  3059. return size;
  3060. }
  3061. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  3062. {
  3063. struct drm_i915_private *dev_priv = dev->dev_private;
  3064. uint32_t dsparb = I915_READ(DSPARB);
  3065. int size;
  3066. size = dsparb & 0x1ff;
  3067. if (plane)
  3068. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  3069. size >>= 1; /* Convert to cachelines */
  3070. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3071. plane ? "B" : "A", size);
  3072. return size;
  3073. }
  3074. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  3075. {
  3076. struct drm_i915_private *dev_priv = dev->dev_private;
  3077. uint32_t dsparb = I915_READ(DSPARB);
  3078. int size;
  3079. size = dsparb & 0x7f;
  3080. size >>= 2; /* Convert to cachelines */
  3081. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3082. plane ? "B" : "A",
  3083. size);
  3084. return size;
  3085. }
  3086. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  3087. {
  3088. struct drm_i915_private *dev_priv = dev->dev_private;
  3089. uint32_t dsparb = I915_READ(DSPARB);
  3090. int size;
  3091. size = dsparb & 0x7f;
  3092. size >>= 1; /* Convert to cachelines */
  3093. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3094. plane ? "B" : "A", size);
  3095. return size;
  3096. }
  3097. static void pineview_update_wm(struct drm_device *dev, int planea_clock,
  3098. int planeb_clock, int sr_hdisplay, int unused,
  3099. int pixel_size)
  3100. {
  3101. struct drm_i915_private *dev_priv = dev->dev_private;
  3102. const struct cxsr_latency *latency;
  3103. u32 reg;
  3104. unsigned long wm;
  3105. int sr_clock;
  3106. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  3107. dev_priv->fsb_freq, dev_priv->mem_freq);
  3108. if (!latency) {
  3109. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  3110. pineview_disable_cxsr(dev);
  3111. return;
  3112. }
  3113. if (!planea_clock || !planeb_clock) {
  3114. sr_clock = planea_clock ? planea_clock : planeb_clock;
  3115. /* Display SR */
  3116. wm = intel_calculate_wm(sr_clock, &pineview_display_wm,
  3117. pixel_size, latency->display_sr);
  3118. reg = I915_READ(DSPFW1);
  3119. reg &= ~DSPFW_SR_MASK;
  3120. reg |= wm << DSPFW_SR_SHIFT;
  3121. I915_WRITE(DSPFW1, reg);
  3122. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  3123. /* cursor SR */
  3124. wm = intel_calculate_wm(sr_clock, &pineview_cursor_wm,
  3125. pixel_size, latency->cursor_sr);
  3126. reg = I915_READ(DSPFW3);
  3127. reg &= ~DSPFW_CURSOR_SR_MASK;
  3128. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  3129. I915_WRITE(DSPFW3, reg);
  3130. /* Display HPLL off SR */
  3131. wm = intel_calculate_wm(sr_clock, &pineview_display_hplloff_wm,
  3132. pixel_size, latency->display_hpll_disable);
  3133. reg = I915_READ(DSPFW3);
  3134. reg &= ~DSPFW_HPLL_SR_MASK;
  3135. reg |= wm & DSPFW_HPLL_SR_MASK;
  3136. I915_WRITE(DSPFW3, reg);
  3137. /* cursor HPLL off SR */
  3138. wm = intel_calculate_wm(sr_clock, &pineview_cursor_hplloff_wm,
  3139. pixel_size, latency->cursor_hpll_disable);
  3140. reg = I915_READ(DSPFW3);
  3141. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  3142. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  3143. I915_WRITE(DSPFW3, reg);
  3144. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  3145. /* activate cxsr */
  3146. I915_WRITE(DSPFW3,
  3147. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  3148. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  3149. } else {
  3150. pineview_disable_cxsr(dev);
  3151. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  3152. }
  3153. }
  3154. static bool g4x_compute_wm0(struct drm_device *dev,
  3155. int plane,
  3156. const struct intel_watermark_params *display,
  3157. int display_latency_ns,
  3158. const struct intel_watermark_params *cursor,
  3159. int cursor_latency_ns,
  3160. int *plane_wm,
  3161. int *cursor_wm)
  3162. {
  3163. struct drm_crtc *crtc;
  3164. int htotal, hdisplay, clock, pixel_size;
  3165. int line_time_us, line_count;
  3166. int entries, tlb_miss;
  3167. crtc = intel_get_crtc_for_plane(dev, plane);
  3168. if (crtc->fb == NULL || !crtc->enabled)
  3169. return false;
  3170. htotal = crtc->mode.htotal;
  3171. hdisplay = crtc->mode.hdisplay;
  3172. clock = crtc->mode.clock;
  3173. pixel_size = crtc->fb->bits_per_pixel / 8;
  3174. /* Use the small buffer method to calculate plane watermark */
  3175. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  3176. tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
  3177. if (tlb_miss > 0)
  3178. entries += tlb_miss;
  3179. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  3180. *plane_wm = entries + display->guard_size;
  3181. if (*plane_wm > (int)display->max_wm)
  3182. *plane_wm = display->max_wm;
  3183. /* Use the large buffer method to calculate cursor watermark */
  3184. line_time_us = ((htotal * 1000) / clock);
  3185. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  3186. entries = line_count * 64 * pixel_size;
  3187. tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
  3188. if (tlb_miss > 0)
  3189. entries += tlb_miss;
  3190. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3191. *cursor_wm = entries + cursor->guard_size;
  3192. if (*cursor_wm > (int)cursor->max_wm)
  3193. *cursor_wm = (int)cursor->max_wm;
  3194. return true;
  3195. }
  3196. /*
  3197. * Check the wm result.
  3198. *
  3199. * If any calculated watermark values is larger than the maximum value that
  3200. * can be programmed into the associated watermark register, that watermark
  3201. * must be disabled.
  3202. */
  3203. static bool g4x_check_srwm(struct drm_device *dev,
  3204. int display_wm, int cursor_wm,
  3205. const struct intel_watermark_params *display,
  3206. const struct intel_watermark_params *cursor)
  3207. {
  3208. DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
  3209. display_wm, cursor_wm);
  3210. if (display_wm > display->max_wm) {
  3211. DRM_DEBUG_KMS("display watermark is too large(%d), disabling\n",
  3212. display_wm, display->max_wm);
  3213. return false;
  3214. }
  3215. if (cursor_wm > cursor->max_wm) {
  3216. DRM_DEBUG_KMS("cursor watermark is too large(%d), disabling\n",
  3217. cursor_wm, cursor->max_wm);
  3218. return false;
  3219. }
  3220. if (!(display_wm || cursor_wm)) {
  3221. DRM_DEBUG_KMS("SR latency is 0, disabling\n");
  3222. return false;
  3223. }
  3224. return true;
  3225. }
  3226. static bool g4x_compute_srwm(struct drm_device *dev,
  3227. int hdisplay, int htotal,
  3228. int pixel_size, int clock, int latency_ns,
  3229. const struct intel_watermark_params *display,
  3230. const struct intel_watermark_params *cursor,
  3231. int *display_wm, int *cursor_wm)
  3232. {
  3233. unsigned long line_time_us;
  3234. int line_count, line_size;
  3235. int small, large;
  3236. int entries;
  3237. if (!latency_ns) {
  3238. *display_wm = *cursor_wm = 0;
  3239. return false;
  3240. }
  3241. line_time_us = (htotal * 1000) / clock;
  3242. line_count = (latency_ns / line_time_us + 1000) / 1000;
  3243. line_size = hdisplay * pixel_size;
  3244. /* Use the minimum of the small and large buffer method for primary */
  3245. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  3246. large = line_count * line_size;
  3247. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  3248. *display_wm = entries + display->guard_size;
  3249. /* calculate the self-refresh watermark for display cursor */
  3250. entries = line_count * pixel_size * 64;
  3251. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3252. *cursor_wm = entries + cursor->guard_size;
  3253. return g4x_check_srwm(dev,
  3254. *display_wm, *cursor_wm,
  3255. display, cursor);
  3256. }
  3257. static void g4x_update_wm(struct drm_device *dev,
  3258. int planea_clock, int planeb_clock,
  3259. int hdisplay, int htotal, int pixel_size)
  3260. {
  3261. static const int sr_latency_ns = 12000;
  3262. struct drm_i915_private *dev_priv = dev->dev_private;
  3263. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  3264. int enabled = 0, plane_sr, cursor_sr, clock;
  3265. if (g4x_compute_wm0(dev, 0,
  3266. &g4x_wm_info, latency_ns,
  3267. &g4x_cursor_wm_info, latency_ns,
  3268. &planea_wm, &cursora_wm))
  3269. enabled++;
  3270. if (g4x_compute_wm0(dev, 1,
  3271. &g4x_wm_info, latency_ns,
  3272. &g4x_cursor_wm_info, latency_ns,
  3273. &planeb_wm, &cursorb_wm))
  3274. enabled++;
  3275. plane_sr = cursor_sr = 0;
  3276. clock = planea_clock ? planea_clock : planeb_clock;
  3277. if (enabled == 1 &&
  3278. g4x_compute_srwm(dev, hdisplay, htotal, pixel_size,
  3279. clock, sr_latency_ns,
  3280. &g4x_wm_info,
  3281. &g4x_cursor_wm_info,
  3282. &plane_sr, &cursor_sr))
  3283. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  3284. else
  3285. I915_WRITE(FW_BLC_SELF,
  3286. I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
  3287. DRM_DEBUG("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  3288. planea_wm, cursora_wm,
  3289. planeb_wm, cursorb_wm,
  3290. plane_sr, cursor_sr);
  3291. I915_WRITE(DSPFW1,
  3292. (plane_sr << DSPFW_SR_SHIFT) |
  3293. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  3294. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  3295. planea_wm);
  3296. I915_WRITE(DSPFW2,
  3297. (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  3298. (cursora_wm << DSPFW_CURSORA_SHIFT));
  3299. /* HPLL off in SR has some issues on G4x... disable it */
  3300. I915_WRITE(DSPFW3,
  3301. (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  3302. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  3303. }
  3304. static void i965_update_wm(struct drm_device *dev, int planea_clock,
  3305. int planeb_clock, int sr_hdisplay, int sr_htotal,
  3306. int pixel_size)
  3307. {
  3308. struct drm_i915_private *dev_priv = dev->dev_private;
  3309. unsigned long line_time_us;
  3310. int sr_clock, sr_entries, srwm = 1;
  3311. int cursor_sr = 16;
  3312. /* Calc sr entries for one plane configs */
  3313. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  3314. /* self-refresh has much higher latency */
  3315. static const int sr_latency_ns = 12000;
  3316. sr_clock = planea_clock ? planea_clock : planeb_clock;
  3317. line_time_us = ((sr_htotal * 1000) / sr_clock);
  3318. /* Use ns/us then divide to preserve precision */
  3319. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3320. pixel_size * sr_hdisplay;
  3321. sr_entries = DIV_ROUND_UP(sr_entries, I915_FIFO_LINE_SIZE);
  3322. DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
  3323. srwm = I965_FIFO_SIZE - sr_entries;
  3324. if (srwm < 0)
  3325. srwm = 1;
  3326. srwm &= 0x1ff;
  3327. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3328. pixel_size * 64;
  3329. sr_entries = DIV_ROUND_UP(sr_entries,
  3330. i965_cursor_wm_info.cacheline_size);
  3331. cursor_sr = i965_cursor_wm_info.fifo_size -
  3332. (sr_entries + i965_cursor_wm_info.guard_size);
  3333. if (cursor_sr > i965_cursor_wm_info.max_wm)
  3334. cursor_sr = i965_cursor_wm_info.max_wm;
  3335. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  3336. "cursor %d\n", srwm, cursor_sr);
  3337. if (IS_CRESTLINE(dev))
  3338. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  3339. } else {
  3340. /* Turn off self refresh if both pipes are enabled */
  3341. if (IS_CRESTLINE(dev))
  3342. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  3343. & ~FW_BLC_SELF_EN);
  3344. }
  3345. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  3346. srwm);
  3347. /* 965 has limitations... */
  3348. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
  3349. (8 << 16) | (8 << 8) | (8 << 0));
  3350. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  3351. /* update cursor SR watermark */
  3352. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  3353. }
  3354. static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
  3355. int planeb_clock, int sr_hdisplay, int sr_htotal,
  3356. int pixel_size)
  3357. {
  3358. struct drm_i915_private *dev_priv = dev->dev_private;
  3359. uint32_t fwater_lo;
  3360. uint32_t fwater_hi;
  3361. int total_size, cacheline_size, cwm, srwm = 1;
  3362. int planea_wm, planeb_wm;
  3363. struct intel_watermark_params planea_params, planeb_params;
  3364. unsigned long line_time_us;
  3365. int sr_clock, sr_entries = 0, sr_enabled = 0;
  3366. /* Create copies of the base settings for each pipe */
  3367. if (IS_CRESTLINE(dev) || IS_I945GM(dev))
  3368. planea_params = planeb_params = i945_wm_info;
  3369. else if (!IS_GEN2(dev))
  3370. planea_params = planeb_params = i915_wm_info;
  3371. else
  3372. planea_params = planeb_params = i855_wm_info;
  3373. /* Grab a couple of global values before we overwrite them */
  3374. total_size = planea_params.fifo_size;
  3375. cacheline_size = planea_params.cacheline_size;
  3376. /* Update per-plane FIFO sizes */
  3377. planea_params.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  3378. planeb_params.fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  3379. planea_wm = intel_calculate_wm(planea_clock, &planea_params,
  3380. pixel_size, latency_ns);
  3381. planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
  3382. pixel_size, latency_ns);
  3383. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  3384. /*
  3385. * Overlay gets an aggressive default since video jitter is bad.
  3386. */
  3387. cwm = 2;
  3388. /* Play safe and disable self-refresh before adjusting watermarks. */
  3389. if (IS_I945G(dev) || IS_I945GM(dev))
  3390. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
  3391. else if (IS_I915GM(dev))
  3392. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  3393. /* Calc sr entries for one plane configs */
  3394. if (HAS_FW_BLC(dev) && sr_hdisplay &&
  3395. (!planea_clock || !planeb_clock)) {
  3396. /* self-refresh has much higher latency */
  3397. static const int sr_latency_ns = 6000;
  3398. sr_clock = planea_clock ? planea_clock : planeb_clock;
  3399. line_time_us = ((sr_htotal * 1000) / sr_clock);
  3400. /* Use ns/us then divide to preserve precision */
  3401. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3402. pixel_size * sr_hdisplay;
  3403. sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
  3404. DRM_DEBUG_KMS("self-refresh entries: %d\n", sr_entries);
  3405. srwm = total_size - sr_entries;
  3406. if (srwm < 0)
  3407. srwm = 1;
  3408. if (IS_I945G(dev) || IS_I945GM(dev))
  3409. I915_WRITE(FW_BLC_SELF,
  3410. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  3411. else if (IS_I915GM(dev))
  3412. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  3413. sr_enabled = 1;
  3414. }
  3415. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  3416. planea_wm, planeb_wm, cwm, srwm);
  3417. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  3418. fwater_hi = (cwm & 0x1f);
  3419. /* Set request length to 8 cachelines per fetch */
  3420. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  3421. fwater_hi = fwater_hi | (1 << 8);
  3422. I915_WRITE(FW_BLC, fwater_lo);
  3423. I915_WRITE(FW_BLC2, fwater_hi);
  3424. if (sr_enabled) {
  3425. if (IS_I945G(dev) || IS_I945GM(dev))
  3426. I915_WRITE(FW_BLC_SELF,
  3427. FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  3428. else if (IS_I915GM(dev))
  3429. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  3430. DRM_DEBUG_KMS("memory self refresh enabled\n");
  3431. } else
  3432. DRM_DEBUG_KMS("memory self refresh disabled\n");
  3433. }
  3434. static void i830_update_wm(struct drm_device *dev, int planea_clock, int unused,
  3435. int unused2, int unused3, int pixel_size)
  3436. {
  3437. struct drm_i915_private *dev_priv = dev->dev_private;
  3438. uint32_t fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  3439. int planea_wm;
  3440. i830_wm_info.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  3441. planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
  3442. pixel_size, latency_ns);
  3443. fwater_lo |= (3<<8) | planea_wm;
  3444. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  3445. I915_WRITE(FW_BLC, fwater_lo);
  3446. }
  3447. #define ILK_LP0_PLANE_LATENCY 700
  3448. #define ILK_LP0_CURSOR_LATENCY 1300
  3449. static bool ironlake_compute_wm0(struct drm_device *dev,
  3450. int pipe,
  3451. const struct intel_watermark_params *display,
  3452. int display_latency_ns,
  3453. const struct intel_watermark_params *cursor,
  3454. int cursor_latency_ns,
  3455. int *plane_wm,
  3456. int *cursor_wm)
  3457. {
  3458. struct drm_crtc *crtc;
  3459. int htotal, hdisplay, clock, pixel_size;
  3460. int line_time_us, line_count;
  3461. int entries, tlb_miss;
  3462. crtc = intel_get_crtc_for_pipe(dev, pipe);
  3463. if (crtc->fb == NULL || !crtc->enabled)
  3464. return false;
  3465. htotal = crtc->mode.htotal;
  3466. hdisplay = crtc->mode.hdisplay;
  3467. clock = crtc->mode.clock;
  3468. pixel_size = crtc->fb->bits_per_pixel / 8;
  3469. /* Use the small buffer method to calculate plane watermark */
  3470. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  3471. tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
  3472. if (tlb_miss > 0)
  3473. entries += tlb_miss;
  3474. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  3475. *plane_wm = entries + display->guard_size;
  3476. if (*plane_wm > (int)display->max_wm)
  3477. *plane_wm = display->max_wm;
  3478. /* Use the large buffer method to calculate cursor watermark */
  3479. line_time_us = ((htotal * 1000) / clock);
  3480. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  3481. entries = line_count * 64 * pixel_size;
  3482. tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
  3483. if (tlb_miss > 0)
  3484. entries += tlb_miss;
  3485. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3486. *cursor_wm = entries + cursor->guard_size;
  3487. if (*cursor_wm > (int)cursor->max_wm)
  3488. *cursor_wm = (int)cursor->max_wm;
  3489. return true;
  3490. }
  3491. /*
  3492. * Check the wm result.
  3493. *
  3494. * If any calculated watermark values is larger than the maximum value that
  3495. * can be programmed into the associated watermark register, that watermark
  3496. * must be disabled.
  3497. */
  3498. static bool ironlake_check_srwm(struct drm_device *dev, int level,
  3499. int fbc_wm, int display_wm, int cursor_wm,
  3500. const struct intel_watermark_params *display,
  3501. const struct intel_watermark_params *cursor)
  3502. {
  3503. struct drm_i915_private *dev_priv = dev->dev_private;
  3504. DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
  3505. " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
  3506. if (fbc_wm > SNB_FBC_MAX_SRWM) {
  3507. DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
  3508. fbc_wm, SNB_FBC_MAX_SRWM, level);
  3509. /* fbc has it's own way to disable FBC WM */
  3510. I915_WRITE(DISP_ARB_CTL,
  3511. I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
  3512. return false;
  3513. }
  3514. if (display_wm > display->max_wm) {
  3515. DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
  3516. display_wm, SNB_DISPLAY_MAX_SRWM, level);
  3517. return false;
  3518. }
  3519. if (cursor_wm > cursor->max_wm) {
  3520. DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
  3521. cursor_wm, SNB_CURSOR_MAX_SRWM, level);
  3522. return false;
  3523. }
  3524. if (!(fbc_wm || display_wm || cursor_wm)) {
  3525. DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
  3526. return false;
  3527. }
  3528. return true;
  3529. }
  3530. /*
  3531. * Compute watermark values of WM[1-3],
  3532. */
  3533. static bool ironlake_compute_srwm(struct drm_device *dev, int level,
  3534. int hdisplay, int htotal,
  3535. int pixel_size, int clock, int latency_ns,
  3536. const struct intel_watermark_params *display,
  3537. const struct intel_watermark_params *cursor,
  3538. int *fbc_wm, int *display_wm, int *cursor_wm)
  3539. {
  3540. unsigned long line_time_us;
  3541. int line_count, line_size;
  3542. int small, large;
  3543. int entries;
  3544. if (!latency_ns) {
  3545. *fbc_wm = *display_wm = *cursor_wm = 0;
  3546. return false;
  3547. }
  3548. line_time_us = (htotal * 1000) / clock;
  3549. line_count = (latency_ns / line_time_us + 1000) / 1000;
  3550. line_size = hdisplay * pixel_size;
  3551. /* Use the minimum of the small and large buffer method for primary */
  3552. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  3553. large = line_count * line_size;
  3554. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  3555. *display_wm = entries + display->guard_size;
  3556. /*
  3557. * Spec says:
  3558. * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
  3559. */
  3560. *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
  3561. /* calculate the self-refresh watermark for display cursor */
  3562. entries = line_count * pixel_size * 64;
  3563. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3564. *cursor_wm = entries + cursor->guard_size;
  3565. return ironlake_check_srwm(dev, level,
  3566. *fbc_wm, *display_wm, *cursor_wm,
  3567. display, cursor);
  3568. }
  3569. static void ironlake_update_wm(struct drm_device *dev,
  3570. int planea_clock, int planeb_clock,
  3571. int hdisplay, int htotal,
  3572. int pixel_size)
  3573. {
  3574. struct drm_i915_private *dev_priv = dev->dev_private;
  3575. int fbc_wm, plane_wm, cursor_wm, enabled;
  3576. int clock;
  3577. enabled = 0;
  3578. if (ironlake_compute_wm0(dev, 0,
  3579. &ironlake_display_wm_info,
  3580. ILK_LP0_PLANE_LATENCY,
  3581. &ironlake_cursor_wm_info,
  3582. ILK_LP0_CURSOR_LATENCY,
  3583. &plane_wm, &cursor_wm)) {
  3584. I915_WRITE(WM0_PIPEA_ILK,
  3585. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3586. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3587. " plane %d, " "cursor: %d\n",
  3588. plane_wm, cursor_wm);
  3589. enabled++;
  3590. }
  3591. if (ironlake_compute_wm0(dev, 1,
  3592. &ironlake_display_wm_info,
  3593. ILK_LP0_PLANE_LATENCY,
  3594. &ironlake_cursor_wm_info,
  3595. ILK_LP0_CURSOR_LATENCY,
  3596. &plane_wm, &cursor_wm)) {
  3597. I915_WRITE(WM0_PIPEB_ILK,
  3598. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3599. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3600. " plane %d, cursor: %d\n",
  3601. plane_wm, cursor_wm);
  3602. enabled++;
  3603. }
  3604. /*
  3605. * Calculate and update the self-refresh watermark only when one
  3606. * display plane is used.
  3607. */
  3608. I915_WRITE(WM3_LP_ILK, 0);
  3609. I915_WRITE(WM2_LP_ILK, 0);
  3610. I915_WRITE(WM1_LP_ILK, 0);
  3611. if (enabled != 1)
  3612. return;
  3613. clock = planea_clock ? planea_clock : planeb_clock;
  3614. /* WM1 */
  3615. if (!ironlake_compute_srwm(dev, 1, hdisplay, htotal, pixel_size,
  3616. clock, ILK_READ_WM1_LATENCY() * 500,
  3617. &ironlake_display_srwm_info,
  3618. &ironlake_cursor_srwm_info,
  3619. &fbc_wm, &plane_wm, &cursor_wm))
  3620. return;
  3621. I915_WRITE(WM1_LP_ILK,
  3622. WM1_LP_SR_EN |
  3623. (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3624. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3625. (plane_wm << WM1_LP_SR_SHIFT) |
  3626. cursor_wm);
  3627. /* WM2 */
  3628. if (!ironlake_compute_srwm(dev, 2, hdisplay, htotal, pixel_size,
  3629. clock, ILK_READ_WM2_LATENCY() * 500,
  3630. &ironlake_display_srwm_info,
  3631. &ironlake_cursor_srwm_info,
  3632. &fbc_wm, &plane_wm, &cursor_wm))
  3633. return;
  3634. I915_WRITE(WM2_LP_ILK,
  3635. WM2_LP_EN |
  3636. (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3637. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3638. (plane_wm << WM1_LP_SR_SHIFT) |
  3639. cursor_wm);
  3640. /*
  3641. * WM3 is unsupported on ILK, probably because we don't have latency
  3642. * data for that power state
  3643. */
  3644. }
  3645. static void sandybridge_update_wm(struct drm_device *dev,
  3646. int planea_clock, int planeb_clock,
  3647. int hdisplay, int htotal,
  3648. int pixel_size)
  3649. {
  3650. struct drm_i915_private *dev_priv = dev->dev_private;
  3651. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  3652. int fbc_wm, plane_wm, cursor_wm, enabled;
  3653. int clock;
  3654. enabled = 0;
  3655. if (ironlake_compute_wm0(dev, 0,
  3656. &sandybridge_display_wm_info, latency,
  3657. &sandybridge_cursor_wm_info, latency,
  3658. &plane_wm, &cursor_wm)) {
  3659. I915_WRITE(WM0_PIPEA_ILK,
  3660. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3661. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3662. " plane %d, " "cursor: %d\n",
  3663. plane_wm, cursor_wm);
  3664. enabled++;
  3665. }
  3666. if (ironlake_compute_wm0(dev, 1,
  3667. &sandybridge_display_wm_info, latency,
  3668. &sandybridge_cursor_wm_info, latency,
  3669. &plane_wm, &cursor_wm)) {
  3670. I915_WRITE(WM0_PIPEB_ILK,
  3671. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3672. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3673. " plane %d, cursor: %d\n",
  3674. plane_wm, cursor_wm);
  3675. enabled++;
  3676. }
  3677. /*
  3678. * Calculate and update the self-refresh watermark only when one
  3679. * display plane is used.
  3680. *
  3681. * SNB support 3 levels of watermark.
  3682. *
  3683. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  3684. * and disabled in the descending order
  3685. *
  3686. */
  3687. I915_WRITE(WM3_LP_ILK, 0);
  3688. I915_WRITE(WM2_LP_ILK, 0);
  3689. I915_WRITE(WM1_LP_ILK, 0);
  3690. if (enabled != 1)
  3691. return;
  3692. clock = planea_clock ? planea_clock : planeb_clock;
  3693. /* WM1 */
  3694. if (!ironlake_compute_srwm(dev, 1, hdisplay, htotal, pixel_size,
  3695. clock, SNB_READ_WM1_LATENCY() * 500,
  3696. &sandybridge_display_srwm_info,
  3697. &sandybridge_cursor_srwm_info,
  3698. &fbc_wm, &plane_wm, &cursor_wm))
  3699. return;
  3700. I915_WRITE(WM1_LP_ILK,
  3701. WM1_LP_SR_EN |
  3702. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3703. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3704. (plane_wm << WM1_LP_SR_SHIFT) |
  3705. cursor_wm);
  3706. /* WM2 */
  3707. if (!ironlake_compute_srwm(dev, 2,
  3708. hdisplay, htotal, pixel_size,
  3709. clock, SNB_READ_WM2_LATENCY() * 500,
  3710. &sandybridge_display_srwm_info,
  3711. &sandybridge_cursor_srwm_info,
  3712. &fbc_wm, &plane_wm, &cursor_wm))
  3713. return;
  3714. I915_WRITE(WM2_LP_ILK,
  3715. WM2_LP_EN |
  3716. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3717. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3718. (plane_wm << WM1_LP_SR_SHIFT) |
  3719. cursor_wm);
  3720. /* WM3 */
  3721. if (!ironlake_compute_srwm(dev, 3,
  3722. hdisplay, htotal, pixel_size,
  3723. clock, SNB_READ_WM3_LATENCY() * 500,
  3724. &sandybridge_display_srwm_info,
  3725. &sandybridge_cursor_srwm_info,
  3726. &fbc_wm, &plane_wm, &cursor_wm))
  3727. return;
  3728. I915_WRITE(WM3_LP_ILK,
  3729. WM3_LP_EN |
  3730. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3731. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3732. (plane_wm << WM1_LP_SR_SHIFT) |
  3733. cursor_wm);
  3734. }
  3735. /**
  3736. * intel_update_watermarks - update FIFO watermark values based on current modes
  3737. *
  3738. * Calculate watermark values for the various WM regs based on current mode
  3739. * and plane configuration.
  3740. *
  3741. * There are several cases to deal with here:
  3742. * - normal (i.e. non-self-refresh)
  3743. * - self-refresh (SR) mode
  3744. * - lines are large relative to FIFO size (buffer can hold up to 2)
  3745. * - lines are small relative to FIFO size (buffer can hold more than 2
  3746. * lines), so need to account for TLB latency
  3747. *
  3748. * The normal calculation is:
  3749. * watermark = dotclock * bytes per pixel * latency
  3750. * where latency is platform & configuration dependent (we assume pessimal
  3751. * values here).
  3752. *
  3753. * The SR calculation is:
  3754. * watermark = (trunc(latency/line time)+1) * surface width *
  3755. * bytes per pixel
  3756. * where
  3757. * line time = htotal / dotclock
  3758. * surface width = hdisplay for normal plane and 64 for cursor
  3759. * and latency is assumed to be high, as above.
  3760. *
  3761. * The final value programmed to the register should always be rounded up,
  3762. * and include an extra 2 entries to account for clock crossings.
  3763. *
  3764. * We don't use the sprite, so we can ignore that. And on Crestline we have
  3765. * to set the non-SR watermarks to 8.
  3766. */
  3767. static void intel_update_watermarks(struct drm_device *dev)
  3768. {
  3769. struct drm_i915_private *dev_priv = dev->dev_private;
  3770. struct drm_crtc *crtc;
  3771. int sr_hdisplay = 0;
  3772. unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
  3773. int enabled = 0, pixel_size = 0;
  3774. int sr_htotal = 0;
  3775. if (!dev_priv->display.update_wm)
  3776. return;
  3777. /* Get the clock config from both planes */
  3778. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3779. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3780. if (intel_crtc->active) {
  3781. enabled++;
  3782. if (intel_crtc->plane == 0) {
  3783. DRM_DEBUG_KMS("plane A (pipe %d) clock: %d\n",
  3784. intel_crtc->pipe, crtc->mode.clock);
  3785. planea_clock = crtc->mode.clock;
  3786. } else {
  3787. DRM_DEBUG_KMS("plane B (pipe %d) clock: %d\n",
  3788. intel_crtc->pipe, crtc->mode.clock);
  3789. planeb_clock = crtc->mode.clock;
  3790. }
  3791. sr_hdisplay = crtc->mode.hdisplay;
  3792. sr_clock = crtc->mode.clock;
  3793. sr_htotal = crtc->mode.htotal;
  3794. if (crtc->fb)
  3795. pixel_size = crtc->fb->bits_per_pixel / 8;
  3796. else
  3797. pixel_size = 4; /* by default */
  3798. }
  3799. }
  3800. if (enabled <= 0)
  3801. return;
  3802. dev_priv->display.update_wm(dev, planea_clock, planeb_clock,
  3803. sr_hdisplay, sr_htotal, pixel_size);
  3804. }
  3805. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  3806. {
  3807. return dev_priv->lvds_use_ssc && i915_panel_use_ssc;
  3808. }
  3809. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  3810. struct drm_display_mode *mode,
  3811. struct drm_display_mode *adjusted_mode,
  3812. int x, int y,
  3813. struct drm_framebuffer *old_fb)
  3814. {
  3815. struct drm_device *dev = crtc->dev;
  3816. struct drm_i915_private *dev_priv = dev->dev_private;
  3817. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3818. int pipe = intel_crtc->pipe;
  3819. int plane = intel_crtc->plane;
  3820. u32 fp_reg, dpll_reg;
  3821. int refclk, num_connectors = 0;
  3822. intel_clock_t clock, reduced_clock;
  3823. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  3824. bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
  3825. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  3826. struct intel_encoder *has_edp_encoder = NULL;
  3827. struct drm_mode_config *mode_config = &dev->mode_config;
  3828. struct intel_encoder *encoder;
  3829. const intel_limit_t *limit;
  3830. int ret;
  3831. struct fdi_m_n m_n = {0};
  3832. u32 reg, temp;
  3833. u32 lvds_sync = 0;
  3834. int target_clock;
  3835. drm_vblank_pre_modeset(dev, pipe);
  3836. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  3837. if (encoder->base.crtc != crtc)
  3838. continue;
  3839. switch (encoder->type) {
  3840. case INTEL_OUTPUT_LVDS:
  3841. is_lvds = true;
  3842. break;
  3843. case INTEL_OUTPUT_SDVO:
  3844. case INTEL_OUTPUT_HDMI:
  3845. is_sdvo = true;
  3846. if (encoder->needs_tv_clock)
  3847. is_tv = true;
  3848. break;
  3849. case INTEL_OUTPUT_DVO:
  3850. is_dvo = true;
  3851. break;
  3852. case INTEL_OUTPUT_TVOUT:
  3853. is_tv = true;
  3854. break;
  3855. case INTEL_OUTPUT_ANALOG:
  3856. is_crt = true;
  3857. break;
  3858. case INTEL_OUTPUT_DISPLAYPORT:
  3859. is_dp = true;
  3860. break;
  3861. case INTEL_OUTPUT_EDP:
  3862. has_edp_encoder = encoder;
  3863. break;
  3864. }
  3865. num_connectors++;
  3866. }
  3867. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  3868. refclk = dev_priv->lvds_ssc_freq * 1000;
  3869. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3870. refclk / 1000);
  3871. } else if (!IS_GEN2(dev)) {
  3872. refclk = 96000;
  3873. if (HAS_PCH_SPLIT(dev) &&
  3874. (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)))
  3875. refclk = 120000; /* 120Mhz refclk */
  3876. } else {
  3877. refclk = 48000;
  3878. }
  3879. /*
  3880. * Returns a set of divisors for the desired target clock with the given
  3881. * refclk, or FALSE. The returned values represent the clock equation:
  3882. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3883. */
  3884. limit = intel_limit(crtc, refclk);
  3885. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  3886. if (!ok) {
  3887. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3888. drm_vblank_post_modeset(dev, pipe);
  3889. return -EINVAL;
  3890. }
  3891. /* Ensure that the cursor is valid for the new mode before changing... */
  3892. intel_crtc_update_cursor(crtc, true);
  3893. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3894. has_reduced_clock = limit->find_pll(limit, crtc,
  3895. dev_priv->lvds_downclock,
  3896. refclk,
  3897. &reduced_clock);
  3898. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  3899. /*
  3900. * If the different P is found, it means that we can't
  3901. * switch the display clock by using the FP0/FP1.
  3902. * In such case we will disable the LVDS downclock
  3903. * feature.
  3904. */
  3905. DRM_DEBUG_KMS("Different P is found for "
  3906. "LVDS clock/downclock\n");
  3907. has_reduced_clock = 0;
  3908. }
  3909. }
  3910. /* SDVO TV has fixed PLL values depend on its clock range,
  3911. this mirrors vbios setting. */
  3912. if (is_sdvo && is_tv) {
  3913. if (adjusted_mode->clock >= 100000
  3914. && adjusted_mode->clock < 140500) {
  3915. clock.p1 = 2;
  3916. clock.p2 = 10;
  3917. clock.n = 3;
  3918. clock.m1 = 16;
  3919. clock.m2 = 8;
  3920. } else if (adjusted_mode->clock >= 140500
  3921. && adjusted_mode->clock <= 200000) {
  3922. clock.p1 = 1;
  3923. clock.p2 = 10;
  3924. clock.n = 6;
  3925. clock.m1 = 12;
  3926. clock.m2 = 8;
  3927. }
  3928. }
  3929. /* FDI link */
  3930. if (HAS_PCH_SPLIT(dev)) {
  3931. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3932. int lane = 0, link_bw, bpp;
  3933. /* CPU eDP doesn't require FDI link, so just set DP M/N
  3934. according to current link config */
  3935. if (has_edp_encoder && !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3936. target_clock = mode->clock;
  3937. intel_edp_link_config(has_edp_encoder,
  3938. &lane, &link_bw);
  3939. } else {
  3940. /* [e]DP over FDI requires target mode clock
  3941. instead of link clock */
  3942. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  3943. target_clock = mode->clock;
  3944. else
  3945. target_clock = adjusted_mode->clock;
  3946. /* FDI is a binary signal running at ~2.7GHz, encoding
  3947. * each output octet as 10 bits. The actual frequency
  3948. * is stored as a divider into a 100MHz clock, and the
  3949. * mode pixel clock is stored in units of 1KHz.
  3950. * Hence the bw of each lane in terms of the mode signal
  3951. * is:
  3952. */
  3953. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  3954. }
  3955. /* determine panel color depth */
  3956. temp = I915_READ(PIPECONF(pipe));
  3957. temp &= ~PIPE_BPC_MASK;
  3958. if (is_lvds) {
  3959. /* the BPC will be 6 if it is 18-bit LVDS panel */
  3960. if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
  3961. temp |= PIPE_8BPC;
  3962. else
  3963. temp |= PIPE_6BPC;
  3964. } else if (has_edp_encoder) {
  3965. switch (dev_priv->edp.bpp/3) {
  3966. case 8:
  3967. temp |= PIPE_8BPC;
  3968. break;
  3969. case 10:
  3970. temp |= PIPE_10BPC;
  3971. break;
  3972. case 6:
  3973. temp |= PIPE_6BPC;
  3974. break;
  3975. case 12:
  3976. temp |= PIPE_12BPC;
  3977. break;
  3978. }
  3979. } else
  3980. temp |= PIPE_8BPC;
  3981. I915_WRITE(PIPECONF(pipe), temp);
  3982. switch (temp & PIPE_BPC_MASK) {
  3983. case PIPE_8BPC:
  3984. bpp = 24;
  3985. break;
  3986. case PIPE_10BPC:
  3987. bpp = 30;
  3988. break;
  3989. case PIPE_6BPC:
  3990. bpp = 18;
  3991. break;
  3992. case PIPE_12BPC:
  3993. bpp = 36;
  3994. break;
  3995. default:
  3996. DRM_ERROR("unknown pipe bpc value\n");
  3997. bpp = 24;
  3998. }
  3999. if (!lane) {
  4000. /*
  4001. * Account for spread spectrum to avoid
  4002. * oversubscribing the link. Max center spread
  4003. * is 2.5%; use 5% for safety's sake.
  4004. */
  4005. u32 bps = target_clock * bpp * 21 / 20;
  4006. lane = bps / (link_bw * 8) + 1;
  4007. }
  4008. intel_crtc->fdi_lanes = lane;
  4009. if (pixel_multiplier > 1)
  4010. link_bw *= pixel_multiplier;
  4011. ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);
  4012. }
  4013. /* Ironlake: try to setup display ref clock before DPLL
  4014. * enabling. This is only under driver's control after
  4015. * PCH B stepping, previous chipset stepping should be
  4016. * ignoring this setting.
  4017. */
  4018. if (HAS_PCH_SPLIT(dev)) {
  4019. /*XXX BIOS treats 16:31 as a mask for 0:15 */
  4020. temp = I915_READ(PCH_DREF_CONTROL);
  4021. /* First clear the current state for output switching */
  4022. temp &= ~DREF_SSC1_ENABLE;
  4023. temp &= ~DREF_SSC4_ENABLE;
  4024. temp &= ~DREF_SUPERSPREAD_SOURCE_MASK;
  4025. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  4026. temp &= ~DREF_SSC_SOURCE_MASK;
  4027. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4028. I915_WRITE(PCH_DREF_CONTROL, temp);
  4029. POSTING_READ(PCH_DREF_CONTROL);
  4030. udelay(200);
  4031. if ((is_lvds || has_edp_encoder) &&
  4032. intel_panel_use_ssc(dev_priv)) {
  4033. temp |= DREF_SSC_SOURCE_ENABLE;
  4034. if (has_edp_encoder) {
  4035. if (!intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4036. /* Enable CPU source on CPU attached eDP */
  4037. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  4038. } else {
  4039. /* Enable SSC on PCH eDP if needed */
  4040. temp |= DREF_SUPERSPREAD_SOURCE_ENABLE;
  4041. }
  4042. I915_WRITE(PCH_DREF_CONTROL, temp);
  4043. }
  4044. if (!dev_priv->display_clock_mode)
  4045. temp |= DREF_SSC1_ENABLE;
  4046. } else {
  4047. if (dev_priv->display_clock_mode)
  4048. temp |= DREF_NONSPREAD_CK505_ENABLE;
  4049. else
  4050. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  4051. if (has_edp_encoder &&
  4052. !intel_encoder_is_pch_edp(&has_edp_encoder->base))
  4053. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  4054. }
  4055. I915_WRITE(PCH_DREF_CONTROL, temp);
  4056. POSTING_READ(PCH_DREF_CONTROL);
  4057. udelay(200);
  4058. }
  4059. if (IS_PINEVIEW(dev)) {
  4060. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  4061. if (has_reduced_clock)
  4062. fp2 = (1 << reduced_clock.n) << 16 |
  4063. reduced_clock.m1 << 8 | reduced_clock.m2;
  4064. } else {
  4065. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  4066. if (has_reduced_clock)
  4067. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  4068. reduced_clock.m2;
  4069. }
  4070. /* Enable autotuning of the PLL clock (if permissible) */
  4071. if (HAS_PCH_SPLIT(dev)) {
  4072. int factor = 21;
  4073. if (is_lvds) {
  4074. if ((intel_panel_use_ssc(dev_priv) &&
  4075. dev_priv->lvds_ssc_freq == 100) ||
  4076. (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
  4077. factor = 25;
  4078. } else if (is_sdvo && is_tv)
  4079. factor = 20;
  4080. if (clock.m1 < factor * clock.n)
  4081. fp |= FP_CB_TUNE;
  4082. }
  4083. dpll = 0;
  4084. if (!HAS_PCH_SPLIT(dev))
  4085. dpll = DPLL_VGA_MODE_DIS;
  4086. if (!IS_GEN2(dev)) {
  4087. if (is_lvds)
  4088. dpll |= DPLLB_MODE_LVDS;
  4089. else
  4090. dpll |= DPLLB_MODE_DAC_SERIAL;
  4091. if (is_sdvo) {
  4092. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4093. if (pixel_multiplier > 1) {
  4094. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4095. dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  4096. else if (HAS_PCH_SPLIT(dev))
  4097. dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  4098. }
  4099. dpll |= DPLL_DVO_HIGH_SPEED;
  4100. }
  4101. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  4102. dpll |= DPLL_DVO_HIGH_SPEED;
  4103. /* compute bitmask from p1 value */
  4104. if (IS_PINEVIEW(dev))
  4105. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4106. else {
  4107. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4108. /* also FPA1 */
  4109. if (HAS_PCH_SPLIT(dev))
  4110. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4111. if (IS_G4X(dev) && has_reduced_clock)
  4112. dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4113. }
  4114. switch (clock.p2) {
  4115. case 5:
  4116. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4117. break;
  4118. case 7:
  4119. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4120. break;
  4121. case 10:
  4122. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4123. break;
  4124. case 14:
  4125. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4126. break;
  4127. }
  4128. if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev))
  4129. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4130. } else {
  4131. if (is_lvds) {
  4132. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4133. } else {
  4134. if (clock.p1 == 2)
  4135. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4136. else
  4137. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4138. if (clock.p2 == 4)
  4139. dpll |= PLL_P2_DIVIDE_BY_4;
  4140. }
  4141. }
  4142. if (is_sdvo && is_tv)
  4143. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4144. else if (is_tv)
  4145. /* XXX: just matching BIOS for now */
  4146. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  4147. dpll |= 3;
  4148. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4149. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4150. else
  4151. dpll |= PLL_REF_INPUT_DREFCLK;
  4152. /* setup pipeconf */
  4153. pipeconf = I915_READ(PIPECONF(pipe));
  4154. /* Set up the display plane register */
  4155. dspcntr = DISPPLANE_GAMMA_ENABLE;
  4156. /* Ironlake's plane is forced to pipe, bit 24 is to
  4157. enable color space conversion */
  4158. if (!HAS_PCH_SPLIT(dev)) {
  4159. if (pipe == 0)
  4160. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  4161. else
  4162. dspcntr |= DISPPLANE_SEL_PIPE_B;
  4163. }
  4164. if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  4165. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  4166. * core speed.
  4167. *
  4168. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  4169. * pipe == 0 check?
  4170. */
  4171. if (mode->clock >
  4172. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  4173. pipeconf |= PIPECONF_DOUBLE_WIDE;
  4174. else
  4175. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  4176. }
  4177. if (!HAS_PCH_SPLIT(dev))
  4178. dpll |= DPLL_VCO_ENABLE;
  4179. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  4180. drm_mode_debug_printmodeline(mode);
  4181. /* assign to Ironlake registers */
  4182. if (HAS_PCH_SPLIT(dev)) {
  4183. fp_reg = PCH_FP0(pipe);
  4184. dpll_reg = PCH_DPLL(pipe);
  4185. } else {
  4186. fp_reg = FP0(pipe);
  4187. dpll_reg = DPLL(pipe);
  4188. }
  4189. /* PCH eDP needs FDI, but CPU eDP does not */
  4190. if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4191. I915_WRITE(fp_reg, fp);
  4192. I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
  4193. POSTING_READ(dpll_reg);
  4194. udelay(150);
  4195. }
  4196. /* enable transcoder DPLL */
  4197. if (HAS_PCH_CPT(dev)) {
  4198. temp = I915_READ(PCH_DPLL_SEL);
  4199. if (pipe == 0)
  4200. temp |= TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL;
  4201. else
  4202. temp |= TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL;
  4203. I915_WRITE(PCH_DPLL_SEL, temp);
  4204. POSTING_READ(PCH_DPLL_SEL);
  4205. udelay(150);
  4206. }
  4207. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  4208. * This is an exception to the general rule that mode_set doesn't turn
  4209. * things on.
  4210. */
  4211. if (is_lvds) {
  4212. reg = LVDS;
  4213. if (HAS_PCH_SPLIT(dev))
  4214. reg = PCH_LVDS;
  4215. temp = I915_READ(reg);
  4216. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  4217. if (pipe == 1) {
  4218. if (HAS_PCH_CPT(dev))
  4219. temp |= PORT_TRANS_B_SEL_CPT;
  4220. else
  4221. temp |= LVDS_PIPEB_SELECT;
  4222. } else {
  4223. if (HAS_PCH_CPT(dev))
  4224. temp &= ~PORT_TRANS_SEL_MASK;
  4225. else
  4226. temp &= ~LVDS_PIPEB_SELECT;
  4227. }
  4228. /* set the corresponsding LVDS_BORDER bit */
  4229. temp |= dev_priv->lvds_border_bits;
  4230. /* Set the B0-B3 data pairs corresponding to whether we're going to
  4231. * set the DPLLs for dual-channel mode or not.
  4232. */
  4233. if (clock.p2 == 7)
  4234. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  4235. else
  4236. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  4237. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  4238. * appropriately here, but we need to look more thoroughly into how
  4239. * panels behave in the two modes.
  4240. */
  4241. /* set the dithering flag on non-PCH LVDS as needed */
  4242. if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
  4243. if (dev_priv->lvds_dither)
  4244. temp |= LVDS_ENABLE_DITHER;
  4245. else
  4246. temp &= ~LVDS_ENABLE_DITHER;
  4247. }
  4248. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  4249. lvds_sync |= LVDS_HSYNC_POLARITY;
  4250. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  4251. lvds_sync |= LVDS_VSYNC_POLARITY;
  4252. if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
  4253. != lvds_sync) {
  4254. char flags[2] = "-+";
  4255. DRM_INFO("Changing LVDS panel from "
  4256. "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
  4257. flags[!(temp & LVDS_HSYNC_POLARITY)],
  4258. flags[!(temp & LVDS_VSYNC_POLARITY)],
  4259. flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
  4260. flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
  4261. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  4262. temp |= lvds_sync;
  4263. }
  4264. I915_WRITE(reg, temp);
  4265. }
  4266. /* set the dithering flag and clear for anything other than a panel. */
  4267. if (HAS_PCH_SPLIT(dev)) {
  4268. pipeconf &= ~PIPECONF_DITHER_EN;
  4269. pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
  4270. if (dev_priv->lvds_dither && (is_lvds || has_edp_encoder)) {
  4271. pipeconf |= PIPECONF_DITHER_EN;
  4272. pipeconf |= PIPECONF_DITHER_TYPE_ST1;
  4273. }
  4274. }
  4275. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4276. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  4277. } else if (HAS_PCH_SPLIT(dev)) {
  4278. /* For non-DP output, clear any trans DP clock recovery setting.*/
  4279. if (pipe == 0) {
  4280. I915_WRITE(TRANSA_DATA_M1, 0);
  4281. I915_WRITE(TRANSA_DATA_N1, 0);
  4282. I915_WRITE(TRANSA_DP_LINK_M1, 0);
  4283. I915_WRITE(TRANSA_DP_LINK_N1, 0);
  4284. } else {
  4285. I915_WRITE(TRANSB_DATA_M1, 0);
  4286. I915_WRITE(TRANSB_DATA_N1, 0);
  4287. I915_WRITE(TRANSB_DP_LINK_M1, 0);
  4288. I915_WRITE(TRANSB_DP_LINK_N1, 0);
  4289. }
  4290. }
  4291. if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4292. I915_WRITE(dpll_reg, dpll);
  4293. /* Wait for the clocks to stabilize. */
  4294. POSTING_READ(dpll_reg);
  4295. udelay(150);
  4296. if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
  4297. temp = 0;
  4298. if (is_sdvo) {
  4299. temp = intel_mode_get_pixel_multiplier(adjusted_mode);
  4300. if (temp > 1)
  4301. temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4302. else
  4303. temp = 0;
  4304. }
  4305. I915_WRITE(DPLL_MD(pipe), temp);
  4306. } else {
  4307. /* The pixel multiplier can only be updated once the
  4308. * DPLL is enabled and the clocks are stable.
  4309. *
  4310. * So write it again.
  4311. */
  4312. I915_WRITE(dpll_reg, dpll);
  4313. }
  4314. }
  4315. intel_crtc->lowfreq_avail = false;
  4316. if (is_lvds && has_reduced_clock && i915_powersave) {
  4317. I915_WRITE(fp_reg + 4, fp2);
  4318. intel_crtc->lowfreq_avail = true;
  4319. if (HAS_PIPE_CXSR(dev)) {
  4320. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4321. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4322. }
  4323. } else {
  4324. I915_WRITE(fp_reg + 4, fp);
  4325. if (HAS_PIPE_CXSR(dev)) {
  4326. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4327. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  4328. }
  4329. }
  4330. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4331. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4332. /* the chip adds 2 halflines automatically */
  4333. adjusted_mode->crtc_vdisplay -= 1;
  4334. adjusted_mode->crtc_vtotal -= 1;
  4335. adjusted_mode->crtc_vblank_start -= 1;
  4336. adjusted_mode->crtc_vblank_end -= 1;
  4337. adjusted_mode->crtc_vsync_end -= 1;
  4338. adjusted_mode->crtc_vsync_start -= 1;
  4339. } else
  4340. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  4341. I915_WRITE(HTOTAL(pipe),
  4342. (adjusted_mode->crtc_hdisplay - 1) |
  4343. ((adjusted_mode->crtc_htotal - 1) << 16));
  4344. I915_WRITE(HBLANK(pipe),
  4345. (adjusted_mode->crtc_hblank_start - 1) |
  4346. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4347. I915_WRITE(HSYNC(pipe),
  4348. (adjusted_mode->crtc_hsync_start - 1) |
  4349. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4350. I915_WRITE(VTOTAL(pipe),
  4351. (adjusted_mode->crtc_vdisplay - 1) |
  4352. ((adjusted_mode->crtc_vtotal - 1) << 16));
  4353. I915_WRITE(VBLANK(pipe),
  4354. (adjusted_mode->crtc_vblank_start - 1) |
  4355. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  4356. I915_WRITE(VSYNC(pipe),
  4357. (adjusted_mode->crtc_vsync_start - 1) |
  4358. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4359. /* pipesrc and dspsize control the size that is scaled from,
  4360. * which should always be the user's requested size.
  4361. */
  4362. if (!HAS_PCH_SPLIT(dev)) {
  4363. I915_WRITE(DSPSIZE(plane),
  4364. ((mode->vdisplay - 1) << 16) |
  4365. (mode->hdisplay - 1));
  4366. I915_WRITE(DSPPOS(plane), 0);
  4367. }
  4368. I915_WRITE(PIPESRC(pipe),
  4369. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  4370. if (HAS_PCH_SPLIT(dev)) {
  4371. I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
  4372. I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
  4373. I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
  4374. I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
  4375. if (has_edp_encoder && !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4376. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  4377. }
  4378. }
  4379. I915_WRITE(PIPECONF(pipe), pipeconf);
  4380. POSTING_READ(PIPECONF(pipe));
  4381. if (!HAS_PCH_SPLIT(dev))
  4382. intel_enable_pipe(dev_priv, pipe, false);
  4383. intel_wait_for_vblank(dev, pipe);
  4384. if (IS_GEN5(dev)) {
  4385. /* enable address swizzle for tiling buffer */
  4386. temp = I915_READ(DISP_ARB_CTL);
  4387. I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
  4388. }
  4389. I915_WRITE(DSPCNTR(plane), dspcntr);
  4390. POSTING_READ(DSPCNTR(plane));
  4391. if (!HAS_PCH_SPLIT(dev))
  4392. intel_enable_plane(dev_priv, plane, pipe);
  4393. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  4394. intel_update_watermarks(dev);
  4395. drm_vblank_post_modeset(dev, pipe);
  4396. return ret;
  4397. }
  4398. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  4399. void intel_crtc_load_lut(struct drm_crtc *crtc)
  4400. {
  4401. struct drm_device *dev = crtc->dev;
  4402. struct drm_i915_private *dev_priv = dev->dev_private;
  4403. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4404. int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
  4405. int i;
  4406. /* The clocks have to be on to load the palette. */
  4407. if (!crtc->enabled)
  4408. return;
  4409. /* use legacy palette for Ironlake */
  4410. if (HAS_PCH_SPLIT(dev))
  4411. palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
  4412. LGC_PALETTE_B;
  4413. for (i = 0; i < 256; i++) {
  4414. I915_WRITE(palreg + 4 * i,
  4415. (intel_crtc->lut_r[i] << 16) |
  4416. (intel_crtc->lut_g[i] << 8) |
  4417. intel_crtc->lut_b[i]);
  4418. }
  4419. }
  4420. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  4421. {
  4422. struct drm_device *dev = crtc->dev;
  4423. struct drm_i915_private *dev_priv = dev->dev_private;
  4424. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4425. bool visible = base != 0;
  4426. u32 cntl;
  4427. if (intel_crtc->cursor_visible == visible)
  4428. return;
  4429. cntl = I915_READ(CURACNTR);
  4430. if (visible) {
  4431. /* On these chipsets we can only modify the base whilst
  4432. * the cursor is disabled.
  4433. */
  4434. I915_WRITE(CURABASE, base);
  4435. cntl &= ~(CURSOR_FORMAT_MASK);
  4436. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  4437. cntl |= CURSOR_ENABLE |
  4438. CURSOR_GAMMA_ENABLE |
  4439. CURSOR_FORMAT_ARGB;
  4440. } else
  4441. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  4442. I915_WRITE(CURACNTR, cntl);
  4443. intel_crtc->cursor_visible = visible;
  4444. }
  4445. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  4446. {
  4447. struct drm_device *dev = crtc->dev;
  4448. struct drm_i915_private *dev_priv = dev->dev_private;
  4449. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4450. int pipe = intel_crtc->pipe;
  4451. bool visible = base != 0;
  4452. if (intel_crtc->cursor_visible != visible) {
  4453. uint32_t cntl = I915_READ(pipe == 0 ? CURACNTR : CURBCNTR);
  4454. if (base) {
  4455. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  4456. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  4457. cntl |= pipe << 28; /* Connect to correct pipe */
  4458. } else {
  4459. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  4460. cntl |= CURSOR_MODE_DISABLE;
  4461. }
  4462. I915_WRITE(pipe == 0 ? CURACNTR : CURBCNTR, cntl);
  4463. intel_crtc->cursor_visible = visible;
  4464. }
  4465. /* and commit changes on next vblank */
  4466. I915_WRITE(pipe == 0 ? CURABASE : CURBBASE, base);
  4467. }
  4468. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  4469. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  4470. bool on)
  4471. {
  4472. struct drm_device *dev = crtc->dev;
  4473. struct drm_i915_private *dev_priv = dev->dev_private;
  4474. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4475. int pipe = intel_crtc->pipe;
  4476. int x = intel_crtc->cursor_x;
  4477. int y = intel_crtc->cursor_y;
  4478. u32 base, pos;
  4479. bool visible;
  4480. pos = 0;
  4481. if (on && crtc->enabled && crtc->fb) {
  4482. base = intel_crtc->cursor_addr;
  4483. if (x > (int) crtc->fb->width)
  4484. base = 0;
  4485. if (y > (int) crtc->fb->height)
  4486. base = 0;
  4487. } else
  4488. base = 0;
  4489. if (x < 0) {
  4490. if (x + intel_crtc->cursor_width < 0)
  4491. base = 0;
  4492. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  4493. x = -x;
  4494. }
  4495. pos |= x << CURSOR_X_SHIFT;
  4496. if (y < 0) {
  4497. if (y + intel_crtc->cursor_height < 0)
  4498. base = 0;
  4499. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  4500. y = -y;
  4501. }
  4502. pos |= y << CURSOR_Y_SHIFT;
  4503. visible = base != 0;
  4504. if (!visible && !intel_crtc->cursor_visible)
  4505. return;
  4506. I915_WRITE(pipe == 0 ? CURAPOS : CURBPOS, pos);
  4507. if (IS_845G(dev) || IS_I865G(dev))
  4508. i845_update_cursor(crtc, base);
  4509. else
  4510. i9xx_update_cursor(crtc, base);
  4511. if (visible)
  4512. intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
  4513. }
  4514. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  4515. struct drm_file *file,
  4516. uint32_t handle,
  4517. uint32_t width, uint32_t height)
  4518. {
  4519. struct drm_device *dev = crtc->dev;
  4520. struct drm_i915_private *dev_priv = dev->dev_private;
  4521. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4522. struct drm_i915_gem_object *obj;
  4523. uint32_t addr;
  4524. int ret;
  4525. DRM_DEBUG_KMS("\n");
  4526. /* if we want to turn off the cursor ignore width and height */
  4527. if (!handle) {
  4528. DRM_DEBUG_KMS("cursor off\n");
  4529. addr = 0;
  4530. obj = NULL;
  4531. mutex_lock(&dev->struct_mutex);
  4532. goto finish;
  4533. }
  4534. /* Currently we only support 64x64 cursors */
  4535. if (width != 64 || height != 64) {
  4536. DRM_ERROR("we currently only support 64x64 cursors\n");
  4537. return -EINVAL;
  4538. }
  4539. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  4540. if (!obj)
  4541. return -ENOENT;
  4542. if (obj->base.size < width * height * 4) {
  4543. DRM_ERROR("buffer is to small\n");
  4544. ret = -ENOMEM;
  4545. goto fail;
  4546. }
  4547. /* we only need to pin inside GTT if cursor is non-phy */
  4548. mutex_lock(&dev->struct_mutex);
  4549. if (!dev_priv->info->cursor_needs_physical) {
  4550. if (obj->tiling_mode) {
  4551. DRM_ERROR("cursor cannot be tiled\n");
  4552. ret = -EINVAL;
  4553. goto fail_locked;
  4554. }
  4555. ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
  4556. if (ret) {
  4557. DRM_ERROR("failed to pin cursor bo\n");
  4558. goto fail_locked;
  4559. }
  4560. ret = i915_gem_object_set_to_gtt_domain(obj, 0);
  4561. if (ret) {
  4562. DRM_ERROR("failed to move cursor bo into the GTT\n");
  4563. goto fail_unpin;
  4564. }
  4565. ret = i915_gem_object_put_fence(obj);
  4566. if (ret) {
  4567. DRM_ERROR("failed to move cursor bo into the GTT\n");
  4568. goto fail_unpin;
  4569. }
  4570. addr = obj->gtt_offset;
  4571. } else {
  4572. int align = IS_I830(dev) ? 16 * 1024 : 256;
  4573. ret = i915_gem_attach_phys_object(dev, obj,
  4574. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  4575. align);
  4576. if (ret) {
  4577. DRM_ERROR("failed to attach phys object\n");
  4578. goto fail_locked;
  4579. }
  4580. addr = obj->phys_obj->handle->busaddr;
  4581. }
  4582. if (IS_GEN2(dev))
  4583. I915_WRITE(CURSIZE, (height << 12) | width);
  4584. finish:
  4585. if (intel_crtc->cursor_bo) {
  4586. if (dev_priv->info->cursor_needs_physical) {
  4587. if (intel_crtc->cursor_bo != obj)
  4588. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  4589. } else
  4590. i915_gem_object_unpin(intel_crtc->cursor_bo);
  4591. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  4592. }
  4593. mutex_unlock(&dev->struct_mutex);
  4594. intel_crtc->cursor_addr = addr;
  4595. intel_crtc->cursor_bo = obj;
  4596. intel_crtc->cursor_width = width;
  4597. intel_crtc->cursor_height = height;
  4598. intel_crtc_update_cursor(crtc, true);
  4599. return 0;
  4600. fail_unpin:
  4601. i915_gem_object_unpin(obj);
  4602. fail_locked:
  4603. mutex_unlock(&dev->struct_mutex);
  4604. fail:
  4605. drm_gem_object_unreference_unlocked(&obj->base);
  4606. return ret;
  4607. }
  4608. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  4609. {
  4610. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4611. intel_crtc->cursor_x = x;
  4612. intel_crtc->cursor_y = y;
  4613. intel_crtc_update_cursor(crtc, true);
  4614. return 0;
  4615. }
  4616. /** Sets the color ramps on behalf of RandR */
  4617. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  4618. u16 blue, int regno)
  4619. {
  4620. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4621. intel_crtc->lut_r[regno] = red >> 8;
  4622. intel_crtc->lut_g[regno] = green >> 8;
  4623. intel_crtc->lut_b[regno] = blue >> 8;
  4624. }
  4625. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  4626. u16 *blue, int regno)
  4627. {
  4628. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4629. *red = intel_crtc->lut_r[regno] << 8;
  4630. *green = intel_crtc->lut_g[regno] << 8;
  4631. *blue = intel_crtc->lut_b[regno] << 8;
  4632. }
  4633. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  4634. u16 *blue, uint32_t start, uint32_t size)
  4635. {
  4636. int end = (start + size > 256) ? 256 : start + size, i;
  4637. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4638. for (i = start; i < end; i++) {
  4639. intel_crtc->lut_r[i] = red[i] >> 8;
  4640. intel_crtc->lut_g[i] = green[i] >> 8;
  4641. intel_crtc->lut_b[i] = blue[i] >> 8;
  4642. }
  4643. intel_crtc_load_lut(crtc);
  4644. }
  4645. /**
  4646. * Get a pipe with a simple mode set on it for doing load-based monitor
  4647. * detection.
  4648. *
  4649. * It will be up to the load-detect code to adjust the pipe as appropriate for
  4650. * its requirements. The pipe will be connected to no other encoders.
  4651. *
  4652. * Currently this code will only succeed if there is a pipe with no encoders
  4653. * configured for it. In the future, it could choose to temporarily disable
  4654. * some outputs to free up a pipe for its use.
  4655. *
  4656. * \return crtc, or NULL if no pipes are available.
  4657. */
  4658. /* VESA 640x480x72Hz mode to set on the pipe */
  4659. static struct drm_display_mode load_detect_mode = {
  4660. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  4661. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  4662. };
  4663. struct drm_crtc *intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  4664. struct drm_connector *connector,
  4665. struct drm_display_mode *mode,
  4666. int *dpms_mode)
  4667. {
  4668. struct intel_crtc *intel_crtc;
  4669. struct drm_crtc *possible_crtc;
  4670. struct drm_crtc *supported_crtc =NULL;
  4671. struct drm_encoder *encoder = &intel_encoder->base;
  4672. struct drm_crtc *crtc = NULL;
  4673. struct drm_device *dev = encoder->dev;
  4674. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  4675. struct drm_crtc_helper_funcs *crtc_funcs;
  4676. int i = -1;
  4677. /*
  4678. * Algorithm gets a little messy:
  4679. * - if the connector already has an assigned crtc, use it (but make
  4680. * sure it's on first)
  4681. * - try to find the first unused crtc that can drive this connector,
  4682. * and use that if we find one
  4683. * - if there are no unused crtcs available, try to use the first
  4684. * one we found that supports the connector
  4685. */
  4686. /* See if we already have a CRTC for this connector */
  4687. if (encoder->crtc) {
  4688. crtc = encoder->crtc;
  4689. /* Make sure the crtc and connector are running */
  4690. intel_crtc = to_intel_crtc(crtc);
  4691. *dpms_mode = intel_crtc->dpms_mode;
  4692. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  4693. crtc_funcs = crtc->helper_private;
  4694. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  4695. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  4696. }
  4697. return crtc;
  4698. }
  4699. /* Find an unused one (if possible) */
  4700. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  4701. i++;
  4702. if (!(encoder->possible_crtcs & (1 << i)))
  4703. continue;
  4704. if (!possible_crtc->enabled) {
  4705. crtc = possible_crtc;
  4706. break;
  4707. }
  4708. if (!supported_crtc)
  4709. supported_crtc = possible_crtc;
  4710. }
  4711. /*
  4712. * If we didn't find an unused CRTC, don't use any.
  4713. */
  4714. if (!crtc) {
  4715. return NULL;
  4716. }
  4717. encoder->crtc = crtc;
  4718. connector->encoder = encoder;
  4719. intel_encoder->load_detect_temp = true;
  4720. intel_crtc = to_intel_crtc(crtc);
  4721. *dpms_mode = intel_crtc->dpms_mode;
  4722. if (!crtc->enabled) {
  4723. if (!mode)
  4724. mode = &load_detect_mode;
  4725. drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
  4726. } else {
  4727. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  4728. crtc_funcs = crtc->helper_private;
  4729. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  4730. }
  4731. /* Add this connector to the crtc */
  4732. encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
  4733. encoder_funcs->commit(encoder);
  4734. }
  4735. /* let the connector get through one full cycle before testing */
  4736. intel_wait_for_vblank(dev, intel_crtc->pipe);
  4737. return crtc;
  4738. }
  4739. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  4740. struct drm_connector *connector, int dpms_mode)
  4741. {
  4742. struct drm_encoder *encoder = &intel_encoder->base;
  4743. struct drm_device *dev = encoder->dev;
  4744. struct drm_crtc *crtc = encoder->crtc;
  4745. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  4746. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  4747. if (intel_encoder->load_detect_temp) {
  4748. encoder->crtc = NULL;
  4749. connector->encoder = NULL;
  4750. intel_encoder->load_detect_temp = false;
  4751. crtc->enabled = drm_helper_crtc_in_use(crtc);
  4752. drm_helper_disable_unused_functions(dev);
  4753. }
  4754. /* Switch crtc and encoder back off if necessary */
  4755. if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
  4756. if (encoder->crtc == crtc)
  4757. encoder_funcs->dpms(encoder, dpms_mode);
  4758. crtc_funcs->dpms(crtc, dpms_mode);
  4759. }
  4760. }
  4761. /* Returns the clock of the currently programmed mode of the given pipe. */
  4762. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  4763. {
  4764. struct drm_i915_private *dev_priv = dev->dev_private;
  4765. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4766. int pipe = intel_crtc->pipe;
  4767. u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
  4768. u32 fp;
  4769. intel_clock_t clock;
  4770. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  4771. fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
  4772. else
  4773. fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
  4774. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  4775. if (IS_PINEVIEW(dev)) {
  4776. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  4777. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4778. } else {
  4779. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  4780. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4781. }
  4782. if (!IS_GEN2(dev)) {
  4783. if (IS_PINEVIEW(dev))
  4784. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  4785. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  4786. else
  4787. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  4788. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4789. switch (dpll & DPLL_MODE_MASK) {
  4790. case DPLLB_MODE_DAC_SERIAL:
  4791. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  4792. 5 : 10;
  4793. break;
  4794. case DPLLB_MODE_LVDS:
  4795. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  4796. 7 : 14;
  4797. break;
  4798. default:
  4799. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  4800. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  4801. return 0;
  4802. }
  4803. /* XXX: Handle the 100Mhz refclk */
  4804. intel_clock(dev, 96000, &clock);
  4805. } else {
  4806. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  4807. if (is_lvds) {
  4808. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  4809. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4810. clock.p2 = 14;
  4811. if ((dpll & PLL_REF_INPUT_MASK) ==
  4812. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  4813. /* XXX: might not be 66MHz */
  4814. intel_clock(dev, 66000, &clock);
  4815. } else
  4816. intel_clock(dev, 48000, &clock);
  4817. } else {
  4818. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  4819. clock.p1 = 2;
  4820. else {
  4821. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  4822. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  4823. }
  4824. if (dpll & PLL_P2_DIVIDE_BY_4)
  4825. clock.p2 = 4;
  4826. else
  4827. clock.p2 = 2;
  4828. intel_clock(dev, 48000, &clock);
  4829. }
  4830. }
  4831. /* XXX: It would be nice to validate the clocks, but we can't reuse
  4832. * i830PllIsValid() because it relies on the xf86_config connector
  4833. * configuration being accurate, which it isn't necessarily.
  4834. */
  4835. return clock.dot;
  4836. }
  4837. /** Returns the currently programmed mode of the given pipe. */
  4838. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  4839. struct drm_crtc *crtc)
  4840. {
  4841. struct drm_i915_private *dev_priv = dev->dev_private;
  4842. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4843. int pipe = intel_crtc->pipe;
  4844. struct drm_display_mode *mode;
  4845. int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  4846. int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  4847. int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  4848. int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  4849. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  4850. if (!mode)
  4851. return NULL;
  4852. mode->clock = intel_crtc_clock_get(dev, crtc);
  4853. mode->hdisplay = (htot & 0xffff) + 1;
  4854. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  4855. mode->hsync_start = (hsync & 0xffff) + 1;
  4856. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  4857. mode->vdisplay = (vtot & 0xffff) + 1;
  4858. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  4859. mode->vsync_start = (vsync & 0xffff) + 1;
  4860. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  4861. drm_mode_set_name(mode);
  4862. drm_mode_set_crtcinfo(mode, 0);
  4863. return mode;
  4864. }
  4865. #define GPU_IDLE_TIMEOUT 500 /* ms */
  4866. /* When this timer fires, we've been idle for awhile */
  4867. static void intel_gpu_idle_timer(unsigned long arg)
  4868. {
  4869. struct drm_device *dev = (struct drm_device *)arg;
  4870. drm_i915_private_t *dev_priv = dev->dev_private;
  4871. if (!list_empty(&dev_priv->mm.active_list)) {
  4872. /* Still processing requests, so just re-arm the timer. */
  4873. mod_timer(&dev_priv->idle_timer, jiffies +
  4874. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4875. return;
  4876. }
  4877. dev_priv->busy = false;
  4878. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4879. }
  4880. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  4881. static void intel_crtc_idle_timer(unsigned long arg)
  4882. {
  4883. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  4884. struct drm_crtc *crtc = &intel_crtc->base;
  4885. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  4886. struct intel_framebuffer *intel_fb;
  4887. intel_fb = to_intel_framebuffer(crtc->fb);
  4888. if (intel_fb && intel_fb->obj->active) {
  4889. /* The framebuffer is still being accessed by the GPU. */
  4890. mod_timer(&intel_crtc->idle_timer, jiffies +
  4891. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4892. return;
  4893. }
  4894. intel_crtc->busy = false;
  4895. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4896. }
  4897. static void intel_increase_pllclock(struct drm_crtc *crtc)
  4898. {
  4899. struct drm_device *dev = crtc->dev;
  4900. drm_i915_private_t *dev_priv = dev->dev_private;
  4901. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4902. int pipe = intel_crtc->pipe;
  4903. int dpll_reg = DPLL(pipe);
  4904. int dpll;
  4905. if (HAS_PCH_SPLIT(dev))
  4906. return;
  4907. if (!dev_priv->lvds_downclock_avail)
  4908. return;
  4909. dpll = I915_READ(dpll_reg);
  4910. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  4911. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  4912. /* Unlock panel regs */
  4913. I915_WRITE(PP_CONTROL,
  4914. I915_READ(PP_CONTROL) | PANEL_UNLOCK_REGS);
  4915. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  4916. I915_WRITE(dpll_reg, dpll);
  4917. POSTING_READ(dpll_reg);
  4918. intel_wait_for_vblank(dev, pipe);
  4919. dpll = I915_READ(dpll_reg);
  4920. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  4921. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  4922. /* ...and lock them again */
  4923. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  4924. }
  4925. /* Schedule downclock */
  4926. mod_timer(&intel_crtc->idle_timer, jiffies +
  4927. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4928. }
  4929. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  4930. {
  4931. struct drm_device *dev = crtc->dev;
  4932. drm_i915_private_t *dev_priv = dev->dev_private;
  4933. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4934. int pipe = intel_crtc->pipe;
  4935. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  4936. int dpll = I915_READ(dpll_reg);
  4937. if (HAS_PCH_SPLIT(dev))
  4938. return;
  4939. if (!dev_priv->lvds_downclock_avail)
  4940. return;
  4941. /*
  4942. * Since this is called by a timer, we should never get here in
  4943. * the manual case.
  4944. */
  4945. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  4946. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  4947. /* Unlock panel regs */
  4948. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  4949. PANEL_UNLOCK_REGS);
  4950. dpll |= DISPLAY_RATE_SELECT_FPA1;
  4951. I915_WRITE(dpll_reg, dpll);
  4952. dpll = I915_READ(dpll_reg);
  4953. intel_wait_for_vblank(dev, pipe);
  4954. dpll = I915_READ(dpll_reg);
  4955. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  4956. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  4957. /* ...and lock them again */
  4958. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  4959. }
  4960. }
  4961. /**
  4962. * intel_idle_update - adjust clocks for idleness
  4963. * @work: work struct
  4964. *
  4965. * Either the GPU or display (or both) went idle. Check the busy status
  4966. * here and adjust the CRTC and GPU clocks as necessary.
  4967. */
  4968. static void intel_idle_update(struct work_struct *work)
  4969. {
  4970. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  4971. idle_work);
  4972. struct drm_device *dev = dev_priv->dev;
  4973. struct drm_crtc *crtc;
  4974. struct intel_crtc *intel_crtc;
  4975. if (!i915_powersave)
  4976. return;
  4977. mutex_lock(&dev->struct_mutex);
  4978. i915_update_gfx_val(dev_priv);
  4979. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4980. /* Skip inactive CRTCs */
  4981. if (!crtc->fb)
  4982. continue;
  4983. intel_crtc = to_intel_crtc(crtc);
  4984. if (!intel_crtc->busy)
  4985. intel_decrease_pllclock(crtc);
  4986. }
  4987. mutex_unlock(&dev->struct_mutex);
  4988. }
  4989. /**
  4990. * intel_mark_busy - mark the GPU and possibly the display busy
  4991. * @dev: drm device
  4992. * @obj: object we're operating on
  4993. *
  4994. * Callers can use this function to indicate that the GPU is busy processing
  4995. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  4996. * buffer), we'll also mark the display as busy, so we know to increase its
  4997. * clock frequency.
  4998. */
  4999. void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
  5000. {
  5001. drm_i915_private_t *dev_priv = dev->dev_private;
  5002. struct drm_crtc *crtc = NULL;
  5003. struct intel_framebuffer *intel_fb;
  5004. struct intel_crtc *intel_crtc;
  5005. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  5006. return;
  5007. if (!dev_priv->busy)
  5008. dev_priv->busy = true;
  5009. else
  5010. mod_timer(&dev_priv->idle_timer, jiffies +
  5011. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  5012. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5013. if (!crtc->fb)
  5014. continue;
  5015. intel_crtc = to_intel_crtc(crtc);
  5016. intel_fb = to_intel_framebuffer(crtc->fb);
  5017. if (intel_fb->obj == obj) {
  5018. if (!intel_crtc->busy) {
  5019. /* Non-busy -> busy, upclock */
  5020. intel_increase_pllclock(crtc);
  5021. intel_crtc->busy = true;
  5022. } else {
  5023. /* Busy -> busy, put off timer */
  5024. mod_timer(&intel_crtc->idle_timer, jiffies +
  5025. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  5026. }
  5027. }
  5028. }
  5029. }
  5030. static void intel_crtc_destroy(struct drm_crtc *crtc)
  5031. {
  5032. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5033. struct drm_device *dev = crtc->dev;
  5034. struct intel_unpin_work *work;
  5035. unsigned long flags;
  5036. spin_lock_irqsave(&dev->event_lock, flags);
  5037. work = intel_crtc->unpin_work;
  5038. intel_crtc->unpin_work = NULL;
  5039. spin_unlock_irqrestore(&dev->event_lock, flags);
  5040. if (work) {
  5041. cancel_work_sync(&work->work);
  5042. kfree(work);
  5043. }
  5044. drm_crtc_cleanup(crtc);
  5045. kfree(intel_crtc);
  5046. }
  5047. static void intel_unpin_work_fn(struct work_struct *__work)
  5048. {
  5049. struct intel_unpin_work *work =
  5050. container_of(__work, struct intel_unpin_work, work);
  5051. mutex_lock(&work->dev->struct_mutex);
  5052. i915_gem_object_unpin(work->old_fb_obj);
  5053. drm_gem_object_unreference(&work->pending_flip_obj->base);
  5054. drm_gem_object_unreference(&work->old_fb_obj->base);
  5055. mutex_unlock(&work->dev->struct_mutex);
  5056. kfree(work);
  5057. }
  5058. static void do_intel_finish_page_flip(struct drm_device *dev,
  5059. struct drm_crtc *crtc)
  5060. {
  5061. drm_i915_private_t *dev_priv = dev->dev_private;
  5062. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5063. struct intel_unpin_work *work;
  5064. struct drm_i915_gem_object *obj;
  5065. struct drm_pending_vblank_event *e;
  5066. struct timeval tnow, tvbl;
  5067. unsigned long flags;
  5068. /* Ignore early vblank irqs */
  5069. if (intel_crtc == NULL)
  5070. return;
  5071. do_gettimeofday(&tnow);
  5072. spin_lock_irqsave(&dev->event_lock, flags);
  5073. work = intel_crtc->unpin_work;
  5074. if (work == NULL || !work->pending) {
  5075. spin_unlock_irqrestore(&dev->event_lock, flags);
  5076. return;
  5077. }
  5078. intel_crtc->unpin_work = NULL;
  5079. if (work->event) {
  5080. e = work->event;
  5081. e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
  5082. /* Called before vblank count and timestamps have
  5083. * been updated for the vblank interval of flip
  5084. * completion? Need to increment vblank count and
  5085. * add one videorefresh duration to returned timestamp
  5086. * to account for this. We assume this happened if we
  5087. * get called over 0.9 frame durations after the last
  5088. * timestamped vblank.
  5089. *
  5090. * This calculation can not be used with vrefresh rates
  5091. * below 5Hz (10Hz to be on the safe side) without
  5092. * promoting to 64 integers.
  5093. */
  5094. if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
  5095. 9 * crtc->framedur_ns) {
  5096. e->event.sequence++;
  5097. tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
  5098. crtc->framedur_ns);
  5099. }
  5100. e->event.tv_sec = tvbl.tv_sec;
  5101. e->event.tv_usec = tvbl.tv_usec;
  5102. list_add_tail(&e->base.link,
  5103. &e->base.file_priv->event_list);
  5104. wake_up_interruptible(&e->base.file_priv->event_wait);
  5105. }
  5106. drm_vblank_put(dev, intel_crtc->pipe);
  5107. spin_unlock_irqrestore(&dev->event_lock, flags);
  5108. obj = work->old_fb_obj;
  5109. atomic_clear_mask(1 << intel_crtc->plane,
  5110. &obj->pending_flip.counter);
  5111. if (atomic_read(&obj->pending_flip) == 0)
  5112. wake_up(&dev_priv->pending_flip_queue);
  5113. schedule_work(&work->work);
  5114. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  5115. }
  5116. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  5117. {
  5118. drm_i915_private_t *dev_priv = dev->dev_private;
  5119. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  5120. do_intel_finish_page_flip(dev, crtc);
  5121. }
  5122. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  5123. {
  5124. drm_i915_private_t *dev_priv = dev->dev_private;
  5125. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  5126. do_intel_finish_page_flip(dev, crtc);
  5127. }
  5128. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  5129. {
  5130. drm_i915_private_t *dev_priv = dev->dev_private;
  5131. struct intel_crtc *intel_crtc =
  5132. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  5133. unsigned long flags;
  5134. spin_lock_irqsave(&dev->event_lock, flags);
  5135. if (intel_crtc->unpin_work) {
  5136. if ((++intel_crtc->unpin_work->pending) > 1)
  5137. DRM_ERROR("Prepared flip multiple times\n");
  5138. } else {
  5139. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  5140. }
  5141. spin_unlock_irqrestore(&dev->event_lock, flags);
  5142. }
  5143. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  5144. struct drm_framebuffer *fb,
  5145. struct drm_pending_vblank_event *event)
  5146. {
  5147. struct drm_device *dev = crtc->dev;
  5148. struct drm_i915_private *dev_priv = dev->dev_private;
  5149. struct intel_framebuffer *intel_fb;
  5150. struct drm_i915_gem_object *obj;
  5151. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5152. struct intel_unpin_work *work;
  5153. unsigned long flags, offset;
  5154. int pipe = intel_crtc->pipe;
  5155. u32 pf, pipesrc;
  5156. int ret;
  5157. work = kzalloc(sizeof *work, GFP_KERNEL);
  5158. if (work == NULL)
  5159. return -ENOMEM;
  5160. work->event = event;
  5161. work->dev = crtc->dev;
  5162. intel_fb = to_intel_framebuffer(crtc->fb);
  5163. work->old_fb_obj = intel_fb->obj;
  5164. INIT_WORK(&work->work, intel_unpin_work_fn);
  5165. /* We borrow the event spin lock for protecting unpin_work */
  5166. spin_lock_irqsave(&dev->event_lock, flags);
  5167. if (intel_crtc->unpin_work) {
  5168. spin_unlock_irqrestore(&dev->event_lock, flags);
  5169. kfree(work);
  5170. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  5171. return -EBUSY;
  5172. }
  5173. intel_crtc->unpin_work = work;
  5174. spin_unlock_irqrestore(&dev->event_lock, flags);
  5175. intel_fb = to_intel_framebuffer(fb);
  5176. obj = intel_fb->obj;
  5177. mutex_lock(&dev->struct_mutex);
  5178. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  5179. if (ret)
  5180. goto cleanup_work;
  5181. /* Reference the objects for the scheduled work. */
  5182. drm_gem_object_reference(&work->old_fb_obj->base);
  5183. drm_gem_object_reference(&obj->base);
  5184. crtc->fb = fb;
  5185. ret = drm_vblank_get(dev, intel_crtc->pipe);
  5186. if (ret)
  5187. goto cleanup_objs;
  5188. if (IS_GEN3(dev) || IS_GEN2(dev)) {
  5189. u32 flip_mask;
  5190. /* Can't queue multiple flips, so wait for the previous
  5191. * one to finish before executing the next.
  5192. */
  5193. ret = BEGIN_LP_RING(2);
  5194. if (ret)
  5195. goto cleanup_objs;
  5196. if (intel_crtc->plane)
  5197. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  5198. else
  5199. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  5200. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  5201. OUT_RING(MI_NOOP);
  5202. ADVANCE_LP_RING();
  5203. }
  5204. work->pending_flip_obj = obj;
  5205. work->enable_stall_check = true;
  5206. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  5207. offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
  5208. ret = BEGIN_LP_RING(4);
  5209. if (ret)
  5210. goto cleanup_objs;
  5211. /* Block clients from rendering to the new back buffer until
  5212. * the flip occurs and the object is no longer visible.
  5213. */
  5214. atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  5215. switch (INTEL_INFO(dev)->gen) {
  5216. case 2:
  5217. OUT_RING(MI_DISPLAY_FLIP |
  5218. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5219. OUT_RING(fb->pitch);
  5220. OUT_RING(obj->gtt_offset + offset);
  5221. OUT_RING(MI_NOOP);
  5222. break;
  5223. case 3:
  5224. OUT_RING(MI_DISPLAY_FLIP_I915 |
  5225. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5226. OUT_RING(fb->pitch);
  5227. OUT_RING(obj->gtt_offset + offset);
  5228. OUT_RING(MI_NOOP);
  5229. break;
  5230. case 4:
  5231. case 5:
  5232. /* i965+ uses the linear or tiled offsets from the
  5233. * Display Registers (which do not change across a page-flip)
  5234. * so we need only reprogram the base address.
  5235. */
  5236. OUT_RING(MI_DISPLAY_FLIP |
  5237. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5238. OUT_RING(fb->pitch);
  5239. OUT_RING(obj->gtt_offset | obj->tiling_mode);
  5240. /* XXX Enabling the panel-fitter across page-flip is so far
  5241. * untested on non-native modes, so ignore it for now.
  5242. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  5243. */
  5244. pf = 0;
  5245. pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
  5246. OUT_RING(pf | pipesrc);
  5247. break;
  5248. case 6:
  5249. OUT_RING(MI_DISPLAY_FLIP |
  5250. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5251. OUT_RING(fb->pitch | obj->tiling_mode);
  5252. OUT_RING(obj->gtt_offset);
  5253. pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  5254. pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
  5255. OUT_RING(pf | pipesrc);
  5256. break;
  5257. }
  5258. ADVANCE_LP_RING();
  5259. mutex_unlock(&dev->struct_mutex);
  5260. trace_i915_flip_request(intel_crtc->plane, obj);
  5261. return 0;
  5262. cleanup_objs:
  5263. drm_gem_object_unreference(&work->old_fb_obj->base);
  5264. drm_gem_object_unreference(&obj->base);
  5265. cleanup_work:
  5266. mutex_unlock(&dev->struct_mutex);
  5267. spin_lock_irqsave(&dev->event_lock, flags);
  5268. intel_crtc->unpin_work = NULL;
  5269. spin_unlock_irqrestore(&dev->event_lock, flags);
  5270. kfree(work);
  5271. return ret;
  5272. }
  5273. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  5274. .dpms = intel_crtc_dpms,
  5275. .mode_fixup = intel_crtc_mode_fixup,
  5276. .mode_set = intel_crtc_mode_set,
  5277. .mode_set_base = intel_pipe_set_base,
  5278. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  5279. .load_lut = intel_crtc_load_lut,
  5280. .disable = intel_crtc_disable,
  5281. };
  5282. static const struct drm_crtc_funcs intel_crtc_funcs = {
  5283. .cursor_set = intel_crtc_cursor_set,
  5284. .cursor_move = intel_crtc_cursor_move,
  5285. .gamma_set = intel_crtc_gamma_set,
  5286. .set_config = drm_crtc_helper_set_config,
  5287. .destroy = intel_crtc_destroy,
  5288. .page_flip = intel_crtc_page_flip,
  5289. };
  5290. static void intel_sanitize_modesetting(struct drm_device *dev,
  5291. int pipe, int plane)
  5292. {
  5293. struct drm_i915_private *dev_priv = dev->dev_private;
  5294. u32 reg, val;
  5295. if (HAS_PCH_SPLIT(dev))
  5296. return;
  5297. /* Who knows what state these registers were left in by the BIOS or
  5298. * grub?
  5299. *
  5300. * If we leave the registers in a conflicting state (e.g. with the
  5301. * display plane reading from the other pipe than the one we intend
  5302. * to use) then when we attempt to teardown the active mode, we will
  5303. * not disable the pipes and planes in the correct order -- leaving
  5304. * a plane reading from a disabled pipe and possibly leading to
  5305. * undefined behaviour.
  5306. */
  5307. reg = DSPCNTR(plane);
  5308. val = I915_READ(reg);
  5309. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  5310. return;
  5311. if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
  5312. return;
  5313. /* This display plane is active and attached to the other CPU pipe. */
  5314. pipe = !pipe;
  5315. /* Disable the plane and wait for it to stop reading from the pipe. */
  5316. intel_disable_plane(dev_priv, plane, pipe);
  5317. intel_disable_pipe(dev_priv, pipe);
  5318. }
  5319. static void intel_crtc_init(struct drm_device *dev, int pipe)
  5320. {
  5321. drm_i915_private_t *dev_priv = dev->dev_private;
  5322. struct intel_crtc *intel_crtc;
  5323. int i;
  5324. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  5325. if (intel_crtc == NULL)
  5326. return;
  5327. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  5328. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  5329. for (i = 0; i < 256; i++) {
  5330. intel_crtc->lut_r[i] = i;
  5331. intel_crtc->lut_g[i] = i;
  5332. intel_crtc->lut_b[i] = i;
  5333. }
  5334. /* Swap pipes & planes for FBC on pre-965 */
  5335. intel_crtc->pipe = pipe;
  5336. intel_crtc->plane = pipe;
  5337. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  5338. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  5339. intel_crtc->plane = !pipe;
  5340. }
  5341. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  5342. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  5343. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  5344. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  5345. intel_crtc->cursor_addr = 0;
  5346. intel_crtc->dpms_mode = -1;
  5347. intel_crtc->active = true; /* force the pipe off on setup_init_config */
  5348. if (HAS_PCH_SPLIT(dev)) {
  5349. intel_helper_funcs.prepare = ironlake_crtc_prepare;
  5350. intel_helper_funcs.commit = ironlake_crtc_commit;
  5351. } else {
  5352. intel_helper_funcs.prepare = i9xx_crtc_prepare;
  5353. intel_helper_funcs.commit = i9xx_crtc_commit;
  5354. }
  5355. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  5356. intel_crtc->busy = false;
  5357. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  5358. (unsigned long)intel_crtc);
  5359. intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
  5360. }
  5361. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  5362. struct drm_file *file)
  5363. {
  5364. drm_i915_private_t *dev_priv = dev->dev_private;
  5365. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  5366. struct drm_mode_object *drmmode_obj;
  5367. struct intel_crtc *crtc;
  5368. if (!dev_priv) {
  5369. DRM_ERROR("called with no initialization\n");
  5370. return -EINVAL;
  5371. }
  5372. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  5373. DRM_MODE_OBJECT_CRTC);
  5374. if (!drmmode_obj) {
  5375. DRM_ERROR("no such CRTC id\n");
  5376. return -EINVAL;
  5377. }
  5378. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  5379. pipe_from_crtc_id->pipe = crtc->pipe;
  5380. return 0;
  5381. }
  5382. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  5383. {
  5384. struct intel_encoder *encoder;
  5385. int index_mask = 0;
  5386. int entry = 0;
  5387. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  5388. if (type_mask & encoder->clone_mask)
  5389. index_mask |= (1 << entry);
  5390. entry++;
  5391. }
  5392. return index_mask;
  5393. }
  5394. static bool has_edp_a(struct drm_device *dev)
  5395. {
  5396. struct drm_i915_private *dev_priv = dev->dev_private;
  5397. if (!IS_MOBILE(dev))
  5398. return false;
  5399. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  5400. return false;
  5401. if (IS_GEN5(dev) &&
  5402. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  5403. return false;
  5404. return true;
  5405. }
  5406. static void intel_setup_outputs(struct drm_device *dev)
  5407. {
  5408. struct drm_i915_private *dev_priv = dev->dev_private;
  5409. struct intel_encoder *encoder;
  5410. bool dpd_is_edp = false;
  5411. bool has_lvds = false;
  5412. if (IS_MOBILE(dev) && !IS_I830(dev))
  5413. has_lvds = intel_lvds_init(dev);
  5414. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  5415. /* disable the panel fitter on everything but LVDS */
  5416. I915_WRITE(PFIT_CONTROL, 0);
  5417. }
  5418. if (HAS_PCH_SPLIT(dev)) {
  5419. dpd_is_edp = intel_dpd_is_edp(dev);
  5420. if (has_edp_a(dev))
  5421. intel_dp_init(dev, DP_A);
  5422. if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  5423. intel_dp_init(dev, PCH_DP_D);
  5424. }
  5425. intel_crt_init(dev);
  5426. if (HAS_PCH_SPLIT(dev)) {
  5427. int found;
  5428. if (I915_READ(HDMIB) & PORT_DETECTED) {
  5429. /* PCH SDVOB multiplex with HDMIB */
  5430. found = intel_sdvo_init(dev, PCH_SDVOB);
  5431. if (!found)
  5432. intel_hdmi_init(dev, HDMIB);
  5433. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  5434. intel_dp_init(dev, PCH_DP_B);
  5435. }
  5436. if (I915_READ(HDMIC) & PORT_DETECTED)
  5437. intel_hdmi_init(dev, HDMIC);
  5438. if (I915_READ(HDMID) & PORT_DETECTED)
  5439. intel_hdmi_init(dev, HDMID);
  5440. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  5441. intel_dp_init(dev, PCH_DP_C);
  5442. if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  5443. intel_dp_init(dev, PCH_DP_D);
  5444. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  5445. bool found = false;
  5446. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  5447. DRM_DEBUG_KMS("probing SDVOB\n");
  5448. found = intel_sdvo_init(dev, SDVOB);
  5449. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  5450. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  5451. intel_hdmi_init(dev, SDVOB);
  5452. }
  5453. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  5454. DRM_DEBUG_KMS("probing DP_B\n");
  5455. intel_dp_init(dev, DP_B);
  5456. }
  5457. }
  5458. /* Before G4X SDVOC doesn't have its own detect register */
  5459. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  5460. DRM_DEBUG_KMS("probing SDVOC\n");
  5461. found = intel_sdvo_init(dev, SDVOC);
  5462. }
  5463. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  5464. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  5465. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  5466. intel_hdmi_init(dev, SDVOC);
  5467. }
  5468. if (SUPPORTS_INTEGRATED_DP(dev)) {
  5469. DRM_DEBUG_KMS("probing DP_C\n");
  5470. intel_dp_init(dev, DP_C);
  5471. }
  5472. }
  5473. if (SUPPORTS_INTEGRATED_DP(dev) &&
  5474. (I915_READ(DP_D) & DP_DETECTED)) {
  5475. DRM_DEBUG_KMS("probing DP_D\n");
  5476. intel_dp_init(dev, DP_D);
  5477. }
  5478. } else if (IS_GEN2(dev))
  5479. intel_dvo_init(dev);
  5480. if (SUPPORTS_TV(dev))
  5481. intel_tv_init(dev);
  5482. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  5483. encoder->base.possible_crtcs = encoder->crtc_mask;
  5484. encoder->base.possible_clones =
  5485. intel_encoder_clones(dev, encoder->clone_mask);
  5486. }
  5487. intel_panel_setup_backlight(dev);
  5488. }
  5489. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  5490. {
  5491. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  5492. drm_framebuffer_cleanup(fb);
  5493. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  5494. kfree(intel_fb);
  5495. }
  5496. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  5497. struct drm_file *file,
  5498. unsigned int *handle)
  5499. {
  5500. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  5501. struct drm_i915_gem_object *obj = intel_fb->obj;
  5502. return drm_gem_handle_create(file, &obj->base, handle);
  5503. }
  5504. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  5505. .destroy = intel_user_framebuffer_destroy,
  5506. .create_handle = intel_user_framebuffer_create_handle,
  5507. };
  5508. int intel_framebuffer_init(struct drm_device *dev,
  5509. struct intel_framebuffer *intel_fb,
  5510. struct drm_mode_fb_cmd *mode_cmd,
  5511. struct drm_i915_gem_object *obj)
  5512. {
  5513. int ret;
  5514. if (obj->tiling_mode == I915_TILING_Y)
  5515. return -EINVAL;
  5516. if (mode_cmd->pitch & 63)
  5517. return -EINVAL;
  5518. switch (mode_cmd->bpp) {
  5519. case 8:
  5520. case 16:
  5521. case 24:
  5522. case 32:
  5523. break;
  5524. default:
  5525. return -EINVAL;
  5526. }
  5527. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  5528. if (ret) {
  5529. DRM_ERROR("framebuffer init failed %d\n", ret);
  5530. return ret;
  5531. }
  5532. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  5533. intel_fb->obj = obj;
  5534. return 0;
  5535. }
  5536. static struct drm_framebuffer *
  5537. intel_user_framebuffer_create(struct drm_device *dev,
  5538. struct drm_file *filp,
  5539. struct drm_mode_fb_cmd *mode_cmd)
  5540. {
  5541. struct drm_i915_gem_object *obj;
  5542. struct intel_framebuffer *intel_fb;
  5543. int ret;
  5544. obj = to_intel_bo(drm_gem_object_lookup(dev, filp, mode_cmd->handle));
  5545. if (!obj)
  5546. return ERR_PTR(-ENOENT);
  5547. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5548. if (!intel_fb)
  5549. return ERR_PTR(-ENOMEM);
  5550. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  5551. if (ret) {
  5552. drm_gem_object_unreference_unlocked(&obj->base);
  5553. kfree(intel_fb);
  5554. return ERR_PTR(ret);
  5555. }
  5556. return &intel_fb->base;
  5557. }
  5558. static const struct drm_mode_config_funcs intel_mode_funcs = {
  5559. .fb_create = intel_user_framebuffer_create,
  5560. .output_poll_changed = intel_fb_output_poll_changed,
  5561. };
  5562. static struct drm_i915_gem_object *
  5563. intel_alloc_context_page(struct drm_device *dev)
  5564. {
  5565. struct drm_i915_gem_object *ctx;
  5566. int ret;
  5567. ctx = i915_gem_alloc_object(dev, 4096);
  5568. if (!ctx) {
  5569. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  5570. return NULL;
  5571. }
  5572. mutex_lock(&dev->struct_mutex);
  5573. ret = i915_gem_object_pin(ctx, 4096, true);
  5574. if (ret) {
  5575. DRM_ERROR("failed to pin power context: %d\n", ret);
  5576. goto err_unref;
  5577. }
  5578. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  5579. if (ret) {
  5580. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  5581. goto err_unpin;
  5582. }
  5583. mutex_unlock(&dev->struct_mutex);
  5584. return ctx;
  5585. err_unpin:
  5586. i915_gem_object_unpin(ctx);
  5587. err_unref:
  5588. drm_gem_object_unreference(&ctx->base);
  5589. mutex_unlock(&dev->struct_mutex);
  5590. return NULL;
  5591. }
  5592. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  5593. {
  5594. struct drm_i915_private *dev_priv = dev->dev_private;
  5595. u16 rgvswctl;
  5596. rgvswctl = I915_READ16(MEMSWCTL);
  5597. if (rgvswctl & MEMCTL_CMD_STS) {
  5598. DRM_DEBUG("gpu busy, RCS change rejected\n");
  5599. return false; /* still busy with another command */
  5600. }
  5601. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  5602. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  5603. I915_WRITE16(MEMSWCTL, rgvswctl);
  5604. POSTING_READ16(MEMSWCTL);
  5605. rgvswctl |= MEMCTL_CMD_STS;
  5606. I915_WRITE16(MEMSWCTL, rgvswctl);
  5607. return true;
  5608. }
  5609. void ironlake_enable_drps(struct drm_device *dev)
  5610. {
  5611. struct drm_i915_private *dev_priv = dev->dev_private;
  5612. u32 rgvmodectl = I915_READ(MEMMODECTL);
  5613. u8 fmax, fmin, fstart, vstart;
  5614. /* Enable temp reporting */
  5615. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  5616. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  5617. /* 100ms RC evaluation intervals */
  5618. I915_WRITE(RCUPEI, 100000);
  5619. I915_WRITE(RCDNEI, 100000);
  5620. /* Set max/min thresholds to 90ms and 80ms respectively */
  5621. I915_WRITE(RCBMAXAVG, 90000);
  5622. I915_WRITE(RCBMINAVG, 80000);
  5623. I915_WRITE(MEMIHYST, 1);
  5624. /* Set up min, max, and cur for interrupt handling */
  5625. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  5626. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  5627. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  5628. MEMMODE_FSTART_SHIFT;
  5629. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  5630. PXVFREQ_PX_SHIFT;
  5631. dev_priv->fmax = fmax; /* IPS callback will increase this */
  5632. dev_priv->fstart = fstart;
  5633. dev_priv->max_delay = fstart;
  5634. dev_priv->min_delay = fmin;
  5635. dev_priv->cur_delay = fstart;
  5636. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  5637. fmax, fmin, fstart);
  5638. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  5639. /*
  5640. * Interrupts will be enabled in ironlake_irq_postinstall
  5641. */
  5642. I915_WRITE(VIDSTART, vstart);
  5643. POSTING_READ(VIDSTART);
  5644. rgvmodectl |= MEMMODE_SWMODE_EN;
  5645. I915_WRITE(MEMMODECTL, rgvmodectl);
  5646. if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  5647. DRM_ERROR("stuck trying to change perf mode\n");
  5648. msleep(1);
  5649. ironlake_set_drps(dev, fstart);
  5650. dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  5651. I915_READ(0x112e0);
  5652. dev_priv->last_time1 = jiffies_to_msecs(jiffies);
  5653. dev_priv->last_count2 = I915_READ(0x112f4);
  5654. getrawmonotonic(&dev_priv->last_time2);
  5655. }
  5656. void ironlake_disable_drps(struct drm_device *dev)
  5657. {
  5658. struct drm_i915_private *dev_priv = dev->dev_private;
  5659. u16 rgvswctl = I915_READ16(MEMSWCTL);
  5660. /* Ack interrupts, disable EFC interrupt */
  5661. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  5662. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  5663. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  5664. I915_WRITE(DEIIR, DE_PCU_EVENT);
  5665. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  5666. /* Go back to the starting frequency */
  5667. ironlake_set_drps(dev, dev_priv->fstart);
  5668. msleep(1);
  5669. rgvswctl |= MEMCTL_CMD_STS;
  5670. I915_WRITE(MEMSWCTL, rgvswctl);
  5671. msleep(1);
  5672. }
  5673. void gen6_set_rps(struct drm_device *dev, u8 val)
  5674. {
  5675. struct drm_i915_private *dev_priv = dev->dev_private;
  5676. u32 swreq;
  5677. swreq = (val & 0x3ff) << 25;
  5678. I915_WRITE(GEN6_RPNSWREQ, swreq);
  5679. }
  5680. void gen6_disable_rps(struct drm_device *dev)
  5681. {
  5682. struct drm_i915_private *dev_priv = dev->dev_private;
  5683. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  5684. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  5685. I915_WRITE(GEN6_PMIER, 0);
  5686. I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
  5687. }
  5688. static unsigned long intel_pxfreq(u32 vidfreq)
  5689. {
  5690. unsigned long freq;
  5691. int div = (vidfreq & 0x3f0000) >> 16;
  5692. int post = (vidfreq & 0x3000) >> 12;
  5693. int pre = (vidfreq & 0x7);
  5694. if (!pre)
  5695. return 0;
  5696. freq = ((div * 133333) / ((1<<post) * pre));
  5697. return freq;
  5698. }
  5699. void intel_init_emon(struct drm_device *dev)
  5700. {
  5701. struct drm_i915_private *dev_priv = dev->dev_private;
  5702. u32 lcfuse;
  5703. u8 pxw[16];
  5704. int i;
  5705. /* Disable to program */
  5706. I915_WRITE(ECR, 0);
  5707. POSTING_READ(ECR);
  5708. /* Program energy weights for various events */
  5709. I915_WRITE(SDEW, 0x15040d00);
  5710. I915_WRITE(CSIEW0, 0x007f0000);
  5711. I915_WRITE(CSIEW1, 0x1e220004);
  5712. I915_WRITE(CSIEW2, 0x04000004);
  5713. for (i = 0; i < 5; i++)
  5714. I915_WRITE(PEW + (i * 4), 0);
  5715. for (i = 0; i < 3; i++)
  5716. I915_WRITE(DEW + (i * 4), 0);
  5717. /* Program P-state weights to account for frequency power adjustment */
  5718. for (i = 0; i < 16; i++) {
  5719. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  5720. unsigned long freq = intel_pxfreq(pxvidfreq);
  5721. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  5722. PXVFREQ_PX_SHIFT;
  5723. unsigned long val;
  5724. val = vid * vid;
  5725. val *= (freq / 1000);
  5726. val *= 255;
  5727. val /= (127*127*900);
  5728. if (val > 0xff)
  5729. DRM_ERROR("bad pxval: %ld\n", val);
  5730. pxw[i] = val;
  5731. }
  5732. /* Render standby states get 0 weight */
  5733. pxw[14] = 0;
  5734. pxw[15] = 0;
  5735. for (i = 0; i < 4; i++) {
  5736. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  5737. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  5738. I915_WRITE(PXW + (i * 4), val);
  5739. }
  5740. /* Adjust magic regs to magic values (more experimental results) */
  5741. I915_WRITE(OGW0, 0);
  5742. I915_WRITE(OGW1, 0);
  5743. I915_WRITE(EG0, 0x00007f00);
  5744. I915_WRITE(EG1, 0x0000000e);
  5745. I915_WRITE(EG2, 0x000e0000);
  5746. I915_WRITE(EG3, 0x68000300);
  5747. I915_WRITE(EG4, 0x42000000);
  5748. I915_WRITE(EG5, 0x00140031);
  5749. I915_WRITE(EG6, 0);
  5750. I915_WRITE(EG7, 0);
  5751. for (i = 0; i < 8; i++)
  5752. I915_WRITE(PXWL + (i * 4), 0);
  5753. /* Enable PMON + select events */
  5754. I915_WRITE(ECR, 0x80000019);
  5755. lcfuse = I915_READ(LCFUSE02);
  5756. dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
  5757. }
  5758. void gen6_enable_rps(struct drm_i915_private *dev_priv)
  5759. {
  5760. u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  5761. u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
  5762. u32 pcu_mbox;
  5763. int cur_freq, min_freq, max_freq;
  5764. int i;
  5765. /* Here begins a magic sequence of register writes to enable
  5766. * auto-downclocking.
  5767. *
  5768. * Perhaps there might be some value in exposing these to
  5769. * userspace...
  5770. */
  5771. I915_WRITE(GEN6_RC_STATE, 0);
  5772. __gen6_force_wake_get(dev_priv);
  5773. /* disable the counters and set deterministic thresholds */
  5774. I915_WRITE(GEN6_RC_CONTROL, 0);
  5775. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  5776. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  5777. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  5778. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  5779. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  5780. for (i = 0; i < I915_NUM_RINGS; i++)
  5781. I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
  5782. I915_WRITE(GEN6_RC_SLEEP, 0);
  5783. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  5784. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  5785. I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
  5786. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  5787. I915_WRITE(GEN6_RC_CONTROL,
  5788. GEN6_RC_CTL_RC6p_ENABLE |
  5789. GEN6_RC_CTL_RC6_ENABLE |
  5790. GEN6_RC_CTL_EI_MODE(1) |
  5791. GEN6_RC_CTL_HW_ENABLE);
  5792. I915_WRITE(GEN6_RPNSWREQ,
  5793. GEN6_FREQUENCY(10) |
  5794. GEN6_OFFSET(0) |
  5795. GEN6_AGGRESSIVE_TURBO);
  5796. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  5797. GEN6_FREQUENCY(12));
  5798. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  5799. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  5800. 18 << 24 |
  5801. 6 << 16);
  5802. I915_WRITE(GEN6_RP_UP_THRESHOLD, 10000);
  5803. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 1000000);
  5804. I915_WRITE(GEN6_RP_UP_EI, 100000);
  5805. I915_WRITE(GEN6_RP_DOWN_EI, 5000000);
  5806. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  5807. I915_WRITE(GEN6_RP_CONTROL,
  5808. GEN6_RP_MEDIA_TURBO |
  5809. GEN6_RP_USE_NORMAL_FREQ |
  5810. GEN6_RP_MEDIA_IS_GFX |
  5811. GEN6_RP_ENABLE |
  5812. GEN6_RP_UP_BUSY_AVG |
  5813. GEN6_RP_DOWN_IDLE_CONT);
  5814. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5815. 500))
  5816. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  5817. I915_WRITE(GEN6_PCODE_DATA, 0);
  5818. I915_WRITE(GEN6_PCODE_MAILBOX,
  5819. GEN6_PCODE_READY |
  5820. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  5821. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5822. 500))
  5823. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  5824. min_freq = (rp_state_cap & 0xff0000) >> 16;
  5825. max_freq = rp_state_cap & 0xff;
  5826. cur_freq = (gt_perf_status & 0xff00) >> 8;
  5827. /* Check for overclock support */
  5828. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5829. 500))
  5830. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  5831. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
  5832. pcu_mbox = I915_READ(GEN6_PCODE_DATA);
  5833. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5834. 500))
  5835. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  5836. if (pcu_mbox & (1<<31)) { /* OC supported */
  5837. max_freq = pcu_mbox & 0xff;
  5838. DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 100);
  5839. }
  5840. /* In units of 100MHz */
  5841. dev_priv->max_delay = max_freq;
  5842. dev_priv->min_delay = min_freq;
  5843. dev_priv->cur_delay = cur_freq;
  5844. /* requires MSI enabled */
  5845. I915_WRITE(GEN6_PMIER,
  5846. GEN6_PM_MBOX_EVENT |
  5847. GEN6_PM_THERMAL_EVENT |
  5848. GEN6_PM_RP_DOWN_TIMEOUT |
  5849. GEN6_PM_RP_UP_THRESHOLD |
  5850. GEN6_PM_RP_DOWN_THRESHOLD |
  5851. GEN6_PM_RP_UP_EI_EXPIRED |
  5852. GEN6_PM_RP_DOWN_EI_EXPIRED);
  5853. I915_WRITE(GEN6_PMIMR, 0);
  5854. /* enable all PM interrupts */
  5855. I915_WRITE(GEN6_PMINTRMSK, 0);
  5856. __gen6_force_wake_put(dev_priv);
  5857. }
  5858. void intel_enable_clock_gating(struct drm_device *dev)
  5859. {
  5860. struct drm_i915_private *dev_priv = dev->dev_private;
  5861. /*
  5862. * Disable clock gating reported to work incorrectly according to the
  5863. * specs, but enable as much else as we can.
  5864. */
  5865. if (HAS_PCH_SPLIT(dev)) {
  5866. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  5867. if (IS_GEN5(dev)) {
  5868. /* Required for FBC */
  5869. dspclk_gate |= DPFCUNIT_CLOCK_GATE_DISABLE |
  5870. DPFCRUNIT_CLOCK_GATE_DISABLE |
  5871. DPFDUNIT_CLOCK_GATE_DISABLE;
  5872. /* Required for CxSR */
  5873. dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
  5874. I915_WRITE(PCH_3DCGDIS0,
  5875. MARIUNIT_CLOCK_GATE_DISABLE |
  5876. SVSMUNIT_CLOCK_GATE_DISABLE);
  5877. I915_WRITE(PCH_3DCGDIS1,
  5878. VFMUNIT_CLOCK_GATE_DISABLE);
  5879. }
  5880. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  5881. /*
  5882. * On Ibex Peak and Cougar Point, we need to disable clock
  5883. * gating for the panel power sequencer or it will fail to
  5884. * start up when no ports are active.
  5885. */
  5886. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  5887. /*
  5888. * According to the spec the following bits should be set in
  5889. * order to enable memory self-refresh
  5890. * The bit 22/21 of 0x42004
  5891. * The bit 5 of 0x42020
  5892. * The bit 15 of 0x45000
  5893. */
  5894. if (IS_GEN5(dev)) {
  5895. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5896. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  5897. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  5898. I915_WRITE(ILK_DSPCLK_GATE,
  5899. (I915_READ(ILK_DSPCLK_GATE) |
  5900. ILK_DPARB_CLK_GATE));
  5901. I915_WRITE(DISP_ARB_CTL,
  5902. (I915_READ(DISP_ARB_CTL) |
  5903. DISP_FBC_WM_DIS));
  5904. I915_WRITE(WM3_LP_ILK, 0);
  5905. I915_WRITE(WM2_LP_ILK, 0);
  5906. I915_WRITE(WM1_LP_ILK, 0);
  5907. }
  5908. /*
  5909. * Based on the document from hardware guys the following bits
  5910. * should be set unconditionally in order to enable FBC.
  5911. * The bit 22 of 0x42000
  5912. * The bit 22 of 0x42004
  5913. * The bit 7,8,9 of 0x42020.
  5914. */
  5915. if (IS_IRONLAKE_M(dev)) {
  5916. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  5917. I915_READ(ILK_DISPLAY_CHICKEN1) |
  5918. ILK_FBCQ_DIS);
  5919. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5920. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5921. ILK_DPARB_GATE);
  5922. I915_WRITE(ILK_DSPCLK_GATE,
  5923. I915_READ(ILK_DSPCLK_GATE) |
  5924. ILK_DPFC_DIS1 |
  5925. ILK_DPFC_DIS2 |
  5926. ILK_CLK_FBC);
  5927. }
  5928. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5929. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5930. ILK_ELPIN_409_SELECT);
  5931. if (IS_GEN5(dev)) {
  5932. I915_WRITE(_3D_CHICKEN2,
  5933. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  5934. _3D_CHICKEN2_WM_READ_PIPELINED);
  5935. }
  5936. if (IS_GEN6(dev)) {
  5937. I915_WRITE(WM3_LP_ILK, 0);
  5938. I915_WRITE(WM2_LP_ILK, 0);
  5939. I915_WRITE(WM1_LP_ILK, 0);
  5940. /*
  5941. * According to the spec the following bits should be
  5942. * set in order to enable memory self-refresh and fbc:
  5943. * The bit21 and bit22 of 0x42000
  5944. * The bit21 and bit22 of 0x42004
  5945. * The bit5 and bit7 of 0x42020
  5946. * The bit14 of 0x70180
  5947. * The bit14 of 0x71180
  5948. */
  5949. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  5950. I915_READ(ILK_DISPLAY_CHICKEN1) |
  5951. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  5952. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5953. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5954. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  5955. I915_WRITE(ILK_DSPCLK_GATE,
  5956. I915_READ(ILK_DSPCLK_GATE) |
  5957. ILK_DPARB_CLK_GATE |
  5958. ILK_DPFD_CLK_GATE);
  5959. I915_WRITE(DSPACNTR,
  5960. I915_READ(DSPACNTR) |
  5961. DISPPLANE_TRICKLE_FEED_DISABLE);
  5962. I915_WRITE(DSPBCNTR,
  5963. I915_READ(DSPBCNTR) |
  5964. DISPPLANE_TRICKLE_FEED_DISABLE);
  5965. }
  5966. } else if (IS_G4X(dev)) {
  5967. uint32_t dspclk_gate;
  5968. I915_WRITE(RENCLK_GATE_D1, 0);
  5969. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  5970. GS_UNIT_CLOCK_GATE_DISABLE |
  5971. CL_UNIT_CLOCK_GATE_DISABLE);
  5972. I915_WRITE(RAMCLK_GATE_D, 0);
  5973. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  5974. OVRUNIT_CLOCK_GATE_DISABLE |
  5975. OVCUNIT_CLOCK_GATE_DISABLE;
  5976. if (IS_GM45(dev))
  5977. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  5978. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  5979. } else if (IS_CRESTLINE(dev)) {
  5980. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  5981. I915_WRITE(RENCLK_GATE_D2, 0);
  5982. I915_WRITE(DSPCLK_GATE_D, 0);
  5983. I915_WRITE(RAMCLK_GATE_D, 0);
  5984. I915_WRITE16(DEUC, 0);
  5985. } else if (IS_BROADWATER(dev)) {
  5986. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  5987. I965_RCC_CLOCK_GATE_DISABLE |
  5988. I965_RCPB_CLOCK_GATE_DISABLE |
  5989. I965_ISC_CLOCK_GATE_DISABLE |
  5990. I965_FBC_CLOCK_GATE_DISABLE);
  5991. I915_WRITE(RENCLK_GATE_D2, 0);
  5992. } else if (IS_GEN3(dev)) {
  5993. u32 dstate = I915_READ(D_STATE);
  5994. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  5995. DSTATE_DOT_CLOCK_GATING;
  5996. I915_WRITE(D_STATE, dstate);
  5997. } else if (IS_I85X(dev) || IS_I865G(dev)) {
  5998. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  5999. } else if (IS_I830(dev)) {
  6000. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  6001. }
  6002. }
  6003. void intel_disable_clock_gating(struct drm_device *dev)
  6004. {
  6005. struct drm_i915_private *dev_priv = dev->dev_private;
  6006. if (dev_priv->renderctx) {
  6007. struct drm_i915_gem_object *obj = dev_priv->renderctx;
  6008. I915_WRITE(CCID, 0);
  6009. POSTING_READ(CCID);
  6010. i915_gem_object_unpin(obj);
  6011. drm_gem_object_unreference(&obj->base);
  6012. dev_priv->renderctx = NULL;
  6013. }
  6014. if (dev_priv->pwrctx) {
  6015. struct drm_i915_gem_object *obj = dev_priv->pwrctx;
  6016. I915_WRITE(PWRCTXA, 0);
  6017. POSTING_READ(PWRCTXA);
  6018. i915_gem_object_unpin(obj);
  6019. drm_gem_object_unreference(&obj->base);
  6020. dev_priv->pwrctx = NULL;
  6021. }
  6022. }
  6023. static void ironlake_disable_rc6(struct drm_device *dev)
  6024. {
  6025. struct drm_i915_private *dev_priv = dev->dev_private;
  6026. /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
  6027. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
  6028. wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
  6029. 10);
  6030. POSTING_READ(CCID);
  6031. I915_WRITE(PWRCTXA, 0);
  6032. POSTING_READ(PWRCTXA);
  6033. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  6034. POSTING_READ(RSTDBYCTL);
  6035. i915_gem_object_unpin(dev_priv->renderctx);
  6036. drm_gem_object_unreference(&dev_priv->renderctx->base);
  6037. dev_priv->renderctx = NULL;
  6038. i915_gem_object_unpin(dev_priv->pwrctx);
  6039. drm_gem_object_unreference(&dev_priv->pwrctx->base);
  6040. dev_priv->pwrctx = NULL;
  6041. }
  6042. void ironlake_enable_rc6(struct drm_device *dev)
  6043. {
  6044. struct drm_i915_private *dev_priv = dev->dev_private;
  6045. int ret;
  6046. /*
  6047. * GPU can automatically power down the render unit if given a page
  6048. * to save state.
  6049. */
  6050. ret = BEGIN_LP_RING(6);
  6051. if (ret) {
  6052. ironlake_disable_rc6(dev);
  6053. return;
  6054. }
  6055. OUT_RING(MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
  6056. OUT_RING(MI_SET_CONTEXT);
  6057. OUT_RING(dev_priv->renderctx->gtt_offset |
  6058. MI_MM_SPACE_GTT |
  6059. MI_SAVE_EXT_STATE_EN |
  6060. MI_RESTORE_EXT_STATE_EN |
  6061. MI_RESTORE_INHIBIT);
  6062. OUT_RING(MI_SUSPEND_FLUSH);
  6063. OUT_RING(MI_NOOP);
  6064. OUT_RING(MI_FLUSH);
  6065. ADVANCE_LP_RING();
  6066. I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
  6067. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  6068. }
  6069. /* Set up chip specific display functions */
  6070. static void intel_init_display(struct drm_device *dev)
  6071. {
  6072. struct drm_i915_private *dev_priv = dev->dev_private;
  6073. /* We always want a DPMS function */
  6074. if (HAS_PCH_SPLIT(dev))
  6075. dev_priv->display.dpms = ironlake_crtc_dpms;
  6076. else
  6077. dev_priv->display.dpms = i9xx_crtc_dpms;
  6078. if (I915_HAS_FBC(dev)) {
  6079. if (HAS_PCH_SPLIT(dev)) {
  6080. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  6081. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  6082. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  6083. } else if (IS_GM45(dev)) {
  6084. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  6085. dev_priv->display.enable_fbc = g4x_enable_fbc;
  6086. dev_priv->display.disable_fbc = g4x_disable_fbc;
  6087. } else if (IS_CRESTLINE(dev)) {
  6088. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  6089. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  6090. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  6091. }
  6092. /* 855GM needs testing */
  6093. }
  6094. /* Returns the core display clock speed */
  6095. if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
  6096. dev_priv->display.get_display_clock_speed =
  6097. i945_get_display_clock_speed;
  6098. else if (IS_I915G(dev))
  6099. dev_priv->display.get_display_clock_speed =
  6100. i915_get_display_clock_speed;
  6101. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  6102. dev_priv->display.get_display_clock_speed =
  6103. i9xx_misc_get_display_clock_speed;
  6104. else if (IS_I915GM(dev))
  6105. dev_priv->display.get_display_clock_speed =
  6106. i915gm_get_display_clock_speed;
  6107. else if (IS_I865G(dev))
  6108. dev_priv->display.get_display_clock_speed =
  6109. i865_get_display_clock_speed;
  6110. else if (IS_I85X(dev))
  6111. dev_priv->display.get_display_clock_speed =
  6112. i855_get_display_clock_speed;
  6113. else /* 852, 830 */
  6114. dev_priv->display.get_display_clock_speed =
  6115. i830_get_display_clock_speed;
  6116. /* For FIFO watermark updates */
  6117. if (HAS_PCH_SPLIT(dev)) {
  6118. if (IS_GEN5(dev)) {
  6119. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  6120. dev_priv->display.update_wm = ironlake_update_wm;
  6121. else {
  6122. DRM_DEBUG_KMS("Failed to get proper latency. "
  6123. "Disable CxSR\n");
  6124. dev_priv->display.update_wm = NULL;
  6125. }
  6126. } else if (IS_GEN6(dev)) {
  6127. if (SNB_READ_WM0_LATENCY()) {
  6128. dev_priv->display.update_wm = sandybridge_update_wm;
  6129. } else {
  6130. DRM_DEBUG_KMS("Failed to read display plane latency. "
  6131. "Disable CxSR\n");
  6132. dev_priv->display.update_wm = NULL;
  6133. }
  6134. } else
  6135. dev_priv->display.update_wm = NULL;
  6136. } else if (IS_PINEVIEW(dev)) {
  6137. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  6138. dev_priv->is_ddr3,
  6139. dev_priv->fsb_freq,
  6140. dev_priv->mem_freq)) {
  6141. DRM_INFO("failed to find known CxSR latency "
  6142. "(found ddr%s fsb freq %d, mem freq %d), "
  6143. "disabling CxSR\n",
  6144. (dev_priv->is_ddr3 == 1) ? "3": "2",
  6145. dev_priv->fsb_freq, dev_priv->mem_freq);
  6146. /* Disable CxSR and never update its watermark again */
  6147. pineview_disable_cxsr(dev);
  6148. dev_priv->display.update_wm = NULL;
  6149. } else
  6150. dev_priv->display.update_wm = pineview_update_wm;
  6151. } else if (IS_G4X(dev))
  6152. dev_priv->display.update_wm = g4x_update_wm;
  6153. else if (IS_GEN4(dev))
  6154. dev_priv->display.update_wm = i965_update_wm;
  6155. else if (IS_GEN3(dev)) {
  6156. dev_priv->display.update_wm = i9xx_update_wm;
  6157. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  6158. } else if (IS_I85X(dev)) {
  6159. dev_priv->display.update_wm = i9xx_update_wm;
  6160. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  6161. } else {
  6162. dev_priv->display.update_wm = i830_update_wm;
  6163. if (IS_845G(dev))
  6164. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  6165. else
  6166. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  6167. }
  6168. }
  6169. /*
  6170. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  6171. * resume, or other times. This quirk makes sure that's the case for
  6172. * affected systems.
  6173. */
  6174. static void quirk_pipea_force (struct drm_device *dev)
  6175. {
  6176. struct drm_i915_private *dev_priv = dev->dev_private;
  6177. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  6178. DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
  6179. }
  6180. struct intel_quirk {
  6181. int device;
  6182. int subsystem_vendor;
  6183. int subsystem_device;
  6184. void (*hook)(struct drm_device *dev);
  6185. };
  6186. struct intel_quirk intel_quirks[] = {
  6187. /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
  6188. { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
  6189. /* HP Mini needs pipe A force quirk (LP: #322104) */
  6190. { 0x27ae,0x103c, 0x361a, quirk_pipea_force },
  6191. /* Thinkpad R31 needs pipe A force quirk */
  6192. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  6193. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  6194. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  6195. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  6196. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  6197. /* ThinkPad X40 needs pipe A force quirk */
  6198. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  6199. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  6200. /* 855 & before need to leave pipe A & dpll A up */
  6201. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  6202. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  6203. };
  6204. static void intel_init_quirks(struct drm_device *dev)
  6205. {
  6206. struct pci_dev *d = dev->pdev;
  6207. int i;
  6208. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  6209. struct intel_quirk *q = &intel_quirks[i];
  6210. if (d->device == q->device &&
  6211. (d->subsystem_vendor == q->subsystem_vendor ||
  6212. q->subsystem_vendor == PCI_ANY_ID) &&
  6213. (d->subsystem_device == q->subsystem_device ||
  6214. q->subsystem_device == PCI_ANY_ID))
  6215. q->hook(dev);
  6216. }
  6217. }
  6218. /* Disable the VGA plane that we never use */
  6219. static void i915_disable_vga(struct drm_device *dev)
  6220. {
  6221. struct drm_i915_private *dev_priv = dev->dev_private;
  6222. u8 sr1;
  6223. u32 vga_reg;
  6224. if (HAS_PCH_SPLIT(dev))
  6225. vga_reg = CPU_VGACNTRL;
  6226. else
  6227. vga_reg = VGACNTRL;
  6228. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  6229. outb(1, VGA_SR_INDEX);
  6230. sr1 = inb(VGA_SR_DATA);
  6231. outb(sr1 | 1<<5, VGA_SR_DATA);
  6232. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  6233. udelay(300);
  6234. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  6235. POSTING_READ(vga_reg);
  6236. }
  6237. void intel_modeset_init(struct drm_device *dev)
  6238. {
  6239. struct drm_i915_private *dev_priv = dev->dev_private;
  6240. int i;
  6241. drm_mode_config_init(dev);
  6242. dev->mode_config.min_width = 0;
  6243. dev->mode_config.min_height = 0;
  6244. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  6245. intel_init_quirks(dev);
  6246. intel_init_display(dev);
  6247. if (IS_GEN2(dev)) {
  6248. dev->mode_config.max_width = 2048;
  6249. dev->mode_config.max_height = 2048;
  6250. } else if (IS_GEN3(dev)) {
  6251. dev->mode_config.max_width = 4096;
  6252. dev->mode_config.max_height = 4096;
  6253. } else {
  6254. dev->mode_config.max_width = 8192;
  6255. dev->mode_config.max_height = 8192;
  6256. }
  6257. dev->mode_config.fb_base = dev->agp->base;
  6258. if (IS_MOBILE(dev) || !IS_GEN2(dev))
  6259. dev_priv->num_pipe = 2;
  6260. else
  6261. dev_priv->num_pipe = 1;
  6262. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  6263. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  6264. for (i = 0; i < dev_priv->num_pipe; i++) {
  6265. intel_crtc_init(dev, i);
  6266. }
  6267. intel_setup_outputs(dev);
  6268. intel_enable_clock_gating(dev);
  6269. /* Just disable it once at startup */
  6270. i915_disable_vga(dev);
  6271. if (IS_IRONLAKE_M(dev)) {
  6272. ironlake_enable_drps(dev);
  6273. intel_init_emon(dev);
  6274. }
  6275. if (IS_GEN6(dev))
  6276. gen6_enable_rps(dev_priv);
  6277. if (IS_IRONLAKE_M(dev)) {
  6278. dev_priv->renderctx = intel_alloc_context_page(dev);
  6279. if (!dev_priv->renderctx)
  6280. goto skip_rc6;
  6281. dev_priv->pwrctx = intel_alloc_context_page(dev);
  6282. if (!dev_priv->pwrctx) {
  6283. i915_gem_object_unpin(dev_priv->renderctx);
  6284. drm_gem_object_unreference(&dev_priv->renderctx->base);
  6285. dev_priv->renderctx = NULL;
  6286. goto skip_rc6;
  6287. }
  6288. ironlake_enable_rc6(dev);
  6289. }
  6290. skip_rc6:
  6291. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  6292. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  6293. (unsigned long)dev);
  6294. intel_setup_overlay(dev);
  6295. }
  6296. void intel_modeset_cleanup(struct drm_device *dev)
  6297. {
  6298. struct drm_i915_private *dev_priv = dev->dev_private;
  6299. struct drm_crtc *crtc;
  6300. struct intel_crtc *intel_crtc;
  6301. drm_kms_helper_poll_fini(dev);
  6302. mutex_lock(&dev->struct_mutex);
  6303. intel_unregister_dsm_handler();
  6304. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  6305. /* Skip inactive CRTCs */
  6306. if (!crtc->fb)
  6307. continue;
  6308. intel_crtc = to_intel_crtc(crtc);
  6309. intel_increase_pllclock(crtc);
  6310. }
  6311. if (dev_priv->display.disable_fbc)
  6312. dev_priv->display.disable_fbc(dev);
  6313. if (IS_IRONLAKE_M(dev))
  6314. ironlake_disable_drps(dev);
  6315. if (IS_GEN6(dev))
  6316. gen6_disable_rps(dev);
  6317. if (IS_IRONLAKE_M(dev))
  6318. ironlake_disable_rc6(dev);
  6319. mutex_unlock(&dev->struct_mutex);
  6320. /* Disable the irq before mode object teardown, for the irq might
  6321. * enqueue unpin/hotplug work. */
  6322. drm_irq_uninstall(dev);
  6323. cancel_work_sync(&dev_priv->hotplug_work);
  6324. /* Shut off idle work before the crtcs get freed. */
  6325. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  6326. intel_crtc = to_intel_crtc(crtc);
  6327. del_timer_sync(&intel_crtc->idle_timer);
  6328. }
  6329. del_timer_sync(&dev_priv->idle_timer);
  6330. cancel_work_sync(&dev_priv->idle_work);
  6331. drm_mode_config_cleanup(dev);
  6332. }
  6333. /*
  6334. * Return which encoder is currently attached for connector.
  6335. */
  6336. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  6337. {
  6338. return &intel_attached_encoder(connector)->base;
  6339. }
  6340. void intel_connector_attach_encoder(struct intel_connector *connector,
  6341. struct intel_encoder *encoder)
  6342. {
  6343. connector->encoder = encoder;
  6344. drm_mode_connector_attach_encoder(&connector->base,
  6345. &encoder->base);
  6346. }
  6347. /*
  6348. * set vga decode state - true == enable VGA decode
  6349. */
  6350. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  6351. {
  6352. struct drm_i915_private *dev_priv = dev->dev_private;
  6353. u16 gmch_ctrl;
  6354. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  6355. if (state)
  6356. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  6357. else
  6358. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  6359. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  6360. return 0;
  6361. }
  6362. #ifdef CONFIG_DEBUG_FS
  6363. #include <linux/seq_file.h>
  6364. struct intel_display_error_state {
  6365. struct intel_cursor_error_state {
  6366. u32 control;
  6367. u32 position;
  6368. u32 base;
  6369. u32 size;
  6370. } cursor[2];
  6371. struct intel_pipe_error_state {
  6372. u32 conf;
  6373. u32 source;
  6374. u32 htotal;
  6375. u32 hblank;
  6376. u32 hsync;
  6377. u32 vtotal;
  6378. u32 vblank;
  6379. u32 vsync;
  6380. } pipe[2];
  6381. struct intel_plane_error_state {
  6382. u32 control;
  6383. u32 stride;
  6384. u32 size;
  6385. u32 pos;
  6386. u32 addr;
  6387. u32 surface;
  6388. u32 tile_offset;
  6389. } plane[2];
  6390. };
  6391. struct intel_display_error_state *
  6392. intel_display_capture_error_state(struct drm_device *dev)
  6393. {
  6394. drm_i915_private_t *dev_priv = dev->dev_private;
  6395. struct intel_display_error_state *error;
  6396. int i;
  6397. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  6398. if (error == NULL)
  6399. return NULL;
  6400. for (i = 0; i < 2; i++) {
  6401. error->cursor[i].control = I915_READ(CURCNTR(i));
  6402. error->cursor[i].position = I915_READ(CURPOS(i));
  6403. error->cursor[i].base = I915_READ(CURBASE(i));
  6404. error->plane[i].control = I915_READ(DSPCNTR(i));
  6405. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  6406. error->plane[i].size = I915_READ(DSPSIZE(i));
  6407. error->plane[i].pos= I915_READ(DSPPOS(i));
  6408. error->plane[i].addr = I915_READ(DSPADDR(i));
  6409. if (INTEL_INFO(dev)->gen >= 4) {
  6410. error->plane[i].surface = I915_READ(DSPSURF(i));
  6411. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  6412. }
  6413. error->pipe[i].conf = I915_READ(PIPECONF(i));
  6414. error->pipe[i].source = I915_READ(PIPESRC(i));
  6415. error->pipe[i].htotal = I915_READ(HTOTAL(i));
  6416. error->pipe[i].hblank = I915_READ(HBLANK(i));
  6417. error->pipe[i].hsync = I915_READ(HSYNC(i));
  6418. error->pipe[i].vtotal = I915_READ(VTOTAL(i));
  6419. error->pipe[i].vblank = I915_READ(VBLANK(i));
  6420. error->pipe[i].vsync = I915_READ(VSYNC(i));
  6421. }
  6422. return error;
  6423. }
  6424. void
  6425. intel_display_print_error_state(struct seq_file *m,
  6426. struct drm_device *dev,
  6427. struct intel_display_error_state *error)
  6428. {
  6429. int i;
  6430. for (i = 0; i < 2; i++) {
  6431. seq_printf(m, "Pipe [%d]:\n", i);
  6432. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  6433. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  6434. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  6435. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  6436. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  6437. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  6438. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  6439. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  6440. seq_printf(m, "Plane [%d]:\n", i);
  6441. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  6442. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  6443. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  6444. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  6445. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  6446. if (INTEL_INFO(dev)->gen >= 4) {
  6447. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  6448. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  6449. }
  6450. seq_printf(m, "Cursor [%d]:\n", i);
  6451. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  6452. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  6453. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  6454. }
  6455. }
  6456. #endif