intel_display.c 238 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834
  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/cpufreq.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include "drmP.h"
  35. #include "intel_drv.h"
  36. #include "i915_drm.h"
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include "drm_dp_helper.h"
  40. #include "drm_crtc_helper.h"
  41. #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  42. bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
  43. static void intel_update_watermarks(struct drm_device *dev);
  44. static void intel_increase_pllclock(struct drm_crtc *crtc);
  45. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  46. typedef struct {
  47. /* given values */
  48. int n;
  49. int m1, m2;
  50. int p1, p2;
  51. /* derived values */
  52. int dot;
  53. int vco;
  54. int m;
  55. int p;
  56. } intel_clock_t;
  57. typedef struct {
  58. int min, max;
  59. } intel_range_t;
  60. typedef struct {
  61. int dot_limit;
  62. int p2_slow, p2_fast;
  63. } intel_p2_t;
  64. #define INTEL_P2_NUM 2
  65. typedef struct intel_limit intel_limit_t;
  66. struct intel_limit {
  67. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  68. intel_p2_t p2;
  69. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  70. int, int, intel_clock_t *);
  71. };
  72. /* FDI */
  73. #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
  74. static bool
  75. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  76. int target, int refclk, intel_clock_t *best_clock);
  77. static bool
  78. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  79. int target, int refclk, intel_clock_t *best_clock);
  80. static bool
  81. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  82. int target, int refclk, intel_clock_t *best_clock);
  83. static bool
  84. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  85. int target, int refclk, intel_clock_t *best_clock);
  86. static inline u32 /* units of 100MHz */
  87. intel_fdi_link_freq(struct drm_device *dev)
  88. {
  89. if (IS_GEN5(dev)) {
  90. struct drm_i915_private *dev_priv = dev->dev_private;
  91. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  92. } else
  93. return 27;
  94. }
  95. static const intel_limit_t intel_limits_i8xx_dvo = {
  96. .dot = { .min = 25000, .max = 350000 },
  97. .vco = { .min = 930000, .max = 1400000 },
  98. .n = { .min = 3, .max = 16 },
  99. .m = { .min = 96, .max = 140 },
  100. .m1 = { .min = 18, .max = 26 },
  101. .m2 = { .min = 6, .max = 16 },
  102. .p = { .min = 4, .max = 128 },
  103. .p1 = { .min = 2, .max = 33 },
  104. .p2 = { .dot_limit = 165000,
  105. .p2_slow = 4, .p2_fast = 2 },
  106. .find_pll = intel_find_best_PLL,
  107. };
  108. static const intel_limit_t intel_limits_i8xx_lvds = {
  109. .dot = { .min = 25000, .max = 350000 },
  110. .vco = { .min = 930000, .max = 1400000 },
  111. .n = { .min = 3, .max = 16 },
  112. .m = { .min = 96, .max = 140 },
  113. .m1 = { .min = 18, .max = 26 },
  114. .m2 = { .min = 6, .max = 16 },
  115. .p = { .min = 4, .max = 128 },
  116. .p1 = { .min = 1, .max = 6 },
  117. .p2 = { .dot_limit = 165000,
  118. .p2_slow = 14, .p2_fast = 7 },
  119. .find_pll = intel_find_best_PLL,
  120. };
  121. static const intel_limit_t intel_limits_i9xx_sdvo = {
  122. .dot = { .min = 20000, .max = 400000 },
  123. .vco = { .min = 1400000, .max = 2800000 },
  124. .n = { .min = 1, .max = 6 },
  125. .m = { .min = 70, .max = 120 },
  126. .m1 = { .min = 10, .max = 22 },
  127. .m2 = { .min = 5, .max = 9 },
  128. .p = { .min = 5, .max = 80 },
  129. .p1 = { .min = 1, .max = 8 },
  130. .p2 = { .dot_limit = 200000,
  131. .p2_slow = 10, .p2_fast = 5 },
  132. .find_pll = intel_find_best_PLL,
  133. };
  134. static const intel_limit_t intel_limits_i9xx_lvds = {
  135. .dot = { .min = 20000, .max = 400000 },
  136. .vco = { .min = 1400000, .max = 2800000 },
  137. .n = { .min = 1, .max = 6 },
  138. .m = { .min = 70, .max = 120 },
  139. .m1 = { .min = 10, .max = 22 },
  140. .m2 = { .min = 5, .max = 9 },
  141. .p = { .min = 7, .max = 98 },
  142. .p1 = { .min = 1, .max = 8 },
  143. .p2 = { .dot_limit = 112000,
  144. .p2_slow = 14, .p2_fast = 7 },
  145. .find_pll = intel_find_best_PLL,
  146. };
  147. static const intel_limit_t intel_limits_g4x_sdvo = {
  148. .dot = { .min = 25000, .max = 270000 },
  149. .vco = { .min = 1750000, .max = 3500000},
  150. .n = { .min = 1, .max = 4 },
  151. .m = { .min = 104, .max = 138 },
  152. .m1 = { .min = 17, .max = 23 },
  153. .m2 = { .min = 5, .max = 11 },
  154. .p = { .min = 10, .max = 30 },
  155. .p1 = { .min = 1, .max = 3},
  156. .p2 = { .dot_limit = 270000,
  157. .p2_slow = 10,
  158. .p2_fast = 10
  159. },
  160. .find_pll = intel_g4x_find_best_PLL,
  161. };
  162. static const intel_limit_t intel_limits_g4x_hdmi = {
  163. .dot = { .min = 22000, .max = 400000 },
  164. .vco = { .min = 1750000, .max = 3500000},
  165. .n = { .min = 1, .max = 4 },
  166. .m = { .min = 104, .max = 138 },
  167. .m1 = { .min = 16, .max = 23 },
  168. .m2 = { .min = 5, .max = 11 },
  169. .p = { .min = 5, .max = 80 },
  170. .p1 = { .min = 1, .max = 8},
  171. .p2 = { .dot_limit = 165000,
  172. .p2_slow = 10, .p2_fast = 5 },
  173. .find_pll = intel_g4x_find_best_PLL,
  174. };
  175. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  176. .dot = { .min = 20000, .max = 115000 },
  177. .vco = { .min = 1750000, .max = 3500000 },
  178. .n = { .min = 1, .max = 3 },
  179. .m = { .min = 104, .max = 138 },
  180. .m1 = { .min = 17, .max = 23 },
  181. .m2 = { .min = 5, .max = 11 },
  182. .p = { .min = 28, .max = 112 },
  183. .p1 = { .min = 2, .max = 8 },
  184. .p2 = { .dot_limit = 0,
  185. .p2_slow = 14, .p2_fast = 14
  186. },
  187. .find_pll = intel_g4x_find_best_PLL,
  188. };
  189. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  190. .dot = { .min = 80000, .max = 224000 },
  191. .vco = { .min = 1750000, .max = 3500000 },
  192. .n = { .min = 1, .max = 3 },
  193. .m = { .min = 104, .max = 138 },
  194. .m1 = { .min = 17, .max = 23 },
  195. .m2 = { .min = 5, .max = 11 },
  196. .p = { .min = 14, .max = 42 },
  197. .p1 = { .min = 2, .max = 6 },
  198. .p2 = { .dot_limit = 0,
  199. .p2_slow = 7, .p2_fast = 7
  200. },
  201. .find_pll = intel_g4x_find_best_PLL,
  202. };
  203. static const intel_limit_t intel_limits_g4x_display_port = {
  204. .dot = { .min = 161670, .max = 227000 },
  205. .vco = { .min = 1750000, .max = 3500000},
  206. .n = { .min = 1, .max = 2 },
  207. .m = { .min = 97, .max = 108 },
  208. .m1 = { .min = 0x10, .max = 0x12 },
  209. .m2 = { .min = 0x05, .max = 0x06 },
  210. .p = { .min = 10, .max = 20 },
  211. .p1 = { .min = 1, .max = 2},
  212. .p2 = { .dot_limit = 0,
  213. .p2_slow = 10, .p2_fast = 10 },
  214. .find_pll = intel_find_pll_g4x_dp,
  215. };
  216. static const intel_limit_t intel_limits_pineview_sdvo = {
  217. .dot = { .min = 20000, .max = 400000},
  218. .vco = { .min = 1700000, .max = 3500000 },
  219. /* Pineview's Ncounter is a ring counter */
  220. .n = { .min = 3, .max = 6 },
  221. .m = { .min = 2, .max = 256 },
  222. /* Pineview only has one combined m divider, which we treat as m2. */
  223. .m1 = { .min = 0, .max = 0 },
  224. .m2 = { .min = 0, .max = 254 },
  225. .p = { .min = 5, .max = 80 },
  226. .p1 = { .min = 1, .max = 8 },
  227. .p2 = { .dot_limit = 200000,
  228. .p2_slow = 10, .p2_fast = 5 },
  229. .find_pll = intel_find_best_PLL,
  230. };
  231. static const intel_limit_t intel_limits_pineview_lvds = {
  232. .dot = { .min = 20000, .max = 400000 },
  233. .vco = { .min = 1700000, .max = 3500000 },
  234. .n = { .min = 3, .max = 6 },
  235. .m = { .min = 2, .max = 256 },
  236. .m1 = { .min = 0, .max = 0 },
  237. .m2 = { .min = 0, .max = 254 },
  238. .p = { .min = 7, .max = 112 },
  239. .p1 = { .min = 1, .max = 8 },
  240. .p2 = { .dot_limit = 112000,
  241. .p2_slow = 14, .p2_fast = 14 },
  242. .find_pll = intel_find_best_PLL,
  243. };
  244. /* Ironlake / Sandybridge
  245. *
  246. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  247. * the range value for them is (actual_value - 2).
  248. */
  249. static const intel_limit_t intel_limits_ironlake_dac = {
  250. .dot = { .min = 25000, .max = 350000 },
  251. .vco = { .min = 1760000, .max = 3510000 },
  252. .n = { .min = 1, .max = 5 },
  253. .m = { .min = 79, .max = 127 },
  254. .m1 = { .min = 12, .max = 22 },
  255. .m2 = { .min = 5, .max = 9 },
  256. .p = { .min = 5, .max = 80 },
  257. .p1 = { .min = 1, .max = 8 },
  258. .p2 = { .dot_limit = 225000,
  259. .p2_slow = 10, .p2_fast = 5 },
  260. .find_pll = intel_g4x_find_best_PLL,
  261. };
  262. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  263. .dot = { .min = 25000, .max = 350000 },
  264. .vco = { .min = 1760000, .max = 3510000 },
  265. .n = { .min = 1, .max = 3 },
  266. .m = { .min = 79, .max = 118 },
  267. .m1 = { .min = 12, .max = 22 },
  268. .m2 = { .min = 5, .max = 9 },
  269. .p = { .min = 28, .max = 112 },
  270. .p1 = { .min = 2, .max = 8 },
  271. .p2 = { .dot_limit = 225000,
  272. .p2_slow = 14, .p2_fast = 14 },
  273. .find_pll = intel_g4x_find_best_PLL,
  274. };
  275. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  276. .dot = { .min = 25000, .max = 350000 },
  277. .vco = { .min = 1760000, .max = 3510000 },
  278. .n = { .min = 1, .max = 3 },
  279. .m = { .min = 79, .max = 127 },
  280. .m1 = { .min = 12, .max = 22 },
  281. .m2 = { .min = 5, .max = 9 },
  282. .p = { .min = 14, .max = 56 },
  283. .p1 = { .min = 2, .max = 8 },
  284. .p2 = { .dot_limit = 225000,
  285. .p2_slow = 7, .p2_fast = 7 },
  286. .find_pll = intel_g4x_find_best_PLL,
  287. };
  288. /* LVDS 100mhz refclk limits. */
  289. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  290. .dot = { .min = 25000, .max = 350000 },
  291. .vco = { .min = 1760000, .max = 3510000 },
  292. .n = { .min = 1, .max = 2 },
  293. .m = { .min = 79, .max = 126 },
  294. .m1 = { .min = 12, .max = 22 },
  295. .m2 = { .min = 5, .max = 9 },
  296. .p = { .min = 28, .max = 112 },
  297. .p1 = { .min = 2, .max = 8 },
  298. .p2 = { .dot_limit = 225000,
  299. .p2_slow = 14, .p2_fast = 14 },
  300. .find_pll = intel_g4x_find_best_PLL,
  301. };
  302. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  303. .dot = { .min = 25000, .max = 350000 },
  304. .vco = { .min = 1760000, .max = 3510000 },
  305. .n = { .min = 1, .max = 3 },
  306. .m = { .min = 79, .max = 126 },
  307. .m1 = { .min = 12, .max = 22 },
  308. .m2 = { .min = 5, .max = 9 },
  309. .p = { .min = 14, .max = 42 },
  310. .p1 = { .min = 2, .max = 6 },
  311. .p2 = { .dot_limit = 225000,
  312. .p2_slow = 7, .p2_fast = 7 },
  313. .find_pll = intel_g4x_find_best_PLL,
  314. };
  315. static const intel_limit_t intel_limits_ironlake_display_port = {
  316. .dot = { .min = 25000, .max = 350000 },
  317. .vco = { .min = 1760000, .max = 3510000},
  318. .n = { .min = 1, .max = 2 },
  319. .m = { .min = 81, .max = 90 },
  320. .m1 = { .min = 12, .max = 22 },
  321. .m2 = { .min = 5, .max = 9 },
  322. .p = { .min = 10, .max = 20 },
  323. .p1 = { .min = 1, .max = 2},
  324. .p2 = { .dot_limit = 0,
  325. .p2_slow = 10, .p2_fast = 10 },
  326. .find_pll = intel_find_pll_ironlake_dp,
  327. };
  328. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  329. int refclk)
  330. {
  331. struct drm_device *dev = crtc->dev;
  332. struct drm_i915_private *dev_priv = dev->dev_private;
  333. const intel_limit_t *limit;
  334. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  335. if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
  336. LVDS_CLKB_POWER_UP) {
  337. /* LVDS dual channel */
  338. if (refclk == 100000)
  339. limit = &intel_limits_ironlake_dual_lvds_100m;
  340. else
  341. limit = &intel_limits_ironlake_dual_lvds;
  342. } else {
  343. if (refclk == 100000)
  344. limit = &intel_limits_ironlake_single_lvds_100m;
  345. else
  346. limit = &intel_limits_ironlake_single_lvds;
  347. }
  348. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  349. HAS_eDP)
  350. limit = &intel_limits_ironlake_display_port;
  351. else
  352. limit = &intel_limits_ironlake_dac;
  353. return limit;
  354. }
  355. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  356. {
  357. struct drm_device *dev = crtc->dev;
  358. struct drm_i915_private *dev_priv = dev->dev_private;
  359. const intel_limit_t *limit;
  360. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  361. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  362. LVDS_CLKB_POWER_UP)
  363. /* LVDS with dual channel */
  364. limit = &intel_limits_g4x_dual_channel_lvds;
  365. else
  366. /* LVDS with dual channel */
  367. limit = &intel_limits_g4x_single_channel_lvds;
  368. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  369. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  370. limit = &intel_limits_g4x_hdmi;
  371. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  372. limit = &intel_limits_g4x_sdvo;
  373. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  374. limit = &intel_limits_g4x_display_port;
  375. } else /* The option is for other outputs */
  376. limit = &intel_limits_i9xx_sdvo;
  377. return limit;
  378. }
  379. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  380. {
  381. struct drm_device *dev = crtc->dev;
  382. const intel_limit_t *limit;
  383. if (HAS_PCH_SPLIT(dev))
  384. limit = intel_ironlake_limit(crtc, refclk);
  385. else if (IS_G4X(dev)) {
  386. limit = intel_g4x_limit(crtc);
  387. } else if (IS_PINEVIEW(dev)) {
  388. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  389. limit = &intel_limits_pineview_lvds;
  390. else
  391. limit = &intel_limits_pineview_sdvo;
  392. } else if (!IS_GEN2(dev)) {
  393. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  394. limit = &intel_limits_i9xx_lvds;
  395. else
  396. limit = &intel_limits_i9xx_sdvo;
  397. } else {
  398. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  399. limit = &intel_limits_i8xx_lvds;
  400. else
  401. limit = &intel_limits_i8xx_dvo;
  402. }
  403. return limit;
  404. }
  405. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  406. static void pineview_clock(int refclk, intel_clock_t *clock)
  407. {
  408. clock->m = clock->m2 + 2;
  409. clock->p = clock->p1 * clock->p2;
  410. clock->vco = refclk * clock->m / clock->n;
  411. clock->dot = clock->vco / clock->p;
  412. }
  413. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  414. {
  415. if (IS_PINEVIEW(dev)) {
  416. pineview_clock(refclk, clock);
  417. return;
  418. }
  419. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  420. clock->p = clock->p1 * clock->p2;
  421. clock->vco = refclk * clock->m / (clock->n + 2);
  422. clock->dot = clock->vco / clock->p;
  423. }
  424. /**
  425. * Returns whether any output on the specified pipe is of the specified type
  426. */
  427. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  428. {
  429. struct drm_device *dev = crtc->dev;
  430. struct drm_mode_config *mode_config = &dev->mode_config;
  431. struct intel_encoder *encoder;
  432. list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
  433. if (encoder->base.crtc == crtc && encoder->type == type)
  434. return true;
  435. return false;
  436. }
  437. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  438. /**
  439. * Returns whether the given set of divisors are valid for a given refclk with
  440. * the given connectors.
  441. */
  442. static bool intel_PLL_is_valid(struct drm_device *dev,
  443. const intel_limit_t *limit,
  444. const intel_clock_t *clock)
  445. {
  446. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  447. INTELPllInvalid("p1 out of range\n");
  448. if (clock->p < limit->p.min || limit->p.max < clock->p)
  449. INTELPllInvalid("p out of range\n");
  450. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  451. INTELPllInvalid("m2 out of range\n");
  452. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  453. INTELPllInvalid("m1 out of range\n");
  454. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  455. INTELPllInvalid("m1 <= m2\n");
  456. if (clock->m < limit->m.min || limit->m.max < clock->m)
  457. INTELPllInvalid("m out of range\n");
  458. if (clock->n < limit->n.min || limit->n.max < clock->n)
  459. INTELPllInvalid("n out of range\n");
  460. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  461. INTELPllInvalid("vco out of range\n");
  462. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  463. * connector, etc., rather than just a single range.
  464. */
  465. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  466. INTELPllInvalid("dot out of range\n");
  467. return true;
  468. }
  469. static bool
  470. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  471. int target, int refclk, intel_clock_t *best_clock)
  472. {
  473. struct drm_device *dev = crtc->dev;
  474. struct drm_i915_private *dev_priv = dev->dev_private;
  475. intel_clock_t clock;
  476. int err = target;
  477. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  478. (I915_READ(LVDS)) != 0) {
  479. /*
  480. * For LVDS, if the panel is on, just rely on its current
  481. * settings for dual-channel. We haven't figured out how to
  482. * reliably set up different single/dual channel state, if we
  483. * even can.
  484. */
  485. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  486. LVDS_CLKB_POWER_UP)
  487. clock.p2 = limit->p2.p2_fast;
  488. else
  489. clock.p2 = limit->p2.p2_slow;
  490. } else {
  491. if (target < limit->p2.dot_limit)
  492. clock.p2 = limit->p2.p2_slow;
  493. else
  494. clock.p2 = limit->p2.p2_fast;
  495. }
  496. memset(best_clock, 0, sizeof(*best_clock));
  497. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  498. clock.m1++) {
  499. for (clock.m2 = limit->m2.min;
  500. clock.m2 <= limit->m2.max; clock.m2++) {
  501. /* m1 is always 0 in Pineview */
  502. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  503. break;
  504. for (clock.n = limit->n.min;
  505. clock.n <= limit->n.max; clock.n++) {
  506. for (clock.p1 = limit->p1.min;
  507. clock.p1 <= limit->p1.max; clock.p1++) {
  508. int this_err;
  509. intel_clock(dev, refclk, &clock);
  510. if (!intel_PLL_is_valid(dev, limit,
  511. &clock))
  512. continue;
  513. this_err = abs(clock.dot - target);
  514. if (this_err < err) {
  515. *best_clock = clock;
  516. err = this_err;
  517. }
  518. }
  519. }
  520. }
  521. }
  522. return (err != target);
  523. }
  524. static bool
  525. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  526. int target, int refclk, intel_clock_t *best_clock)
  527. {
  528. struct drm_device *dev = crtc->dev;
  529. struct drm_i915_private *dev_priv = dev->dev_private;
  530. intel_clock_t clock;
  531. int max_n;
  532. bool found;
  533. /* approximately equals target * 0.00585 */
  534. int err_most = (target >> 8) + (target >> 9);
  535. found = false;
  536. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  537. int lvds_reg;
  538. if (HAS_PCH_SPLIT(dev))
  539. lvds_reg = PCH_LVDS;
  540. else
  541. lvds_reg = LVDS;
  542. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  543. LVDS_CLKB_POWER_UP)
  544. clock.p2 = limit->p2.p2_fast;
  545. else
  546. clock.p2 = limit->p2.p2_slow;
  547. } else {
  548. if (target < limit->p2.dot_limit)
  549. clock.p2 = limit->p2.p2_slow;
  550. else
  551. clock.p2 = limit->p2.p2_fast;
  552. }
  553. memset(best_clock, 0, sizeof(*best_clock));
  554. max_n = limit->n.max;
  555. /* based on hardware requirement, prefer smaller n to precision */
  556. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  557. /* based on hardware requirement, prefere larger m1,m2 */
  558. for (clock.m1 = limit->m1.max;
  559. clock.m1 >= limit->m1.min; clock.m1--) {
  560. for (clock.m2 = limit->m2.max;
  561. clock.m2 >= limit->m2.min; clock.m2--) {
  562. for (clock.p1 = limit->p1.max;
  563. clock.p1 >= limit->p1.min; clock.p1--) {
  564. int this_err;
  565. intel_clock(dev, refclk, &clock);
  566. if (!intel_PLL_is_valid(dev, limit,
  567. &clock))
  568. continue;
  569. this_err = abs(clock.dot - target);
  570. if (this_err < err_most) {
  571. *best_clock = clock;
  572. err_most = this_err;
  573. max_n = clock.n;
  574. found = true;
  575. }
  576. }
  577. }
  578. }
  579. }
  580. return found;
  581. }
  582. static bool
  583. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  584. int target, int refclk, intel_clock_t *best_clock)
  585. {
  586. struct drm_device *dev = crtc->dev;
  587. intel_clock_t clock;
  588. if (target < 200000) {
  589. clock.n = 1;
  590. clock.p1 = 2;
  591. clock.p2 = 10;
  592. clock.m1 = 12;
  593. clock.m2 = 9;
  594. } else {
  595. clock.n = 2;
  596. clock.p1 = 1;
  597. clock.p2 = 10;
  598. clock.m1 = 14;
  599. clock.m2 = 8;
  600. }
  601. intel_clock(dev, refclk, &clock);
  602. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  603. return true;
  604. }
  605. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  606. static bool
  607. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  608. int target, int refclk, intel_clock_t *best_clock)
  609. {
  610. intel_clock_t clock;
  611. if (target < 200000) {
  612. clock.p1 = 2;
  613. clock.p2 = 10;
  614. clock.n = 2;
  615. clock.m1 = 23;
  616. clock.m2 = 8;
  617. } else {
  618. clock.p1 = 1;
  619. clock.p2 = 10;
  620. clock.n = 1;
  621. clock.m1 = 14;
  622. clock.m2 = 2;
  623. }
  624. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  625. clock.p = (clock.p1 * clock.p2);
  626. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  627. clock.vco = 0;
  628. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  629. return true;
  630. }
  631. /**
  632. * intel_wait_for_vblank - wait for vblank on a given pipe
  633. * @dev: drm device
  634. * @pipe: pipe to wait for
  635. *
  636. * Wait for vblank to occur on a given pipe. Needed for various bits of
  637. * mode setting code.
  638. */
  639. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  640. {
  641. struct drm_i915_private *dev_priv = dev->dev_private;
  642. int pipestat_reg = PIPESTAT(pipe);
  643. /* Clear existing vblank status. Note this will clear any other
  644. * sticky status fields as well.
  645. *
  646. * This races with i915_driver_irq_handler() with the result
  647. * that either function could miss a vblank event. Here it is not
  648. * fatal, as we will either wait upon the next vblank interrupt or
  649. * timeout. Generally speaking intel_wait_for_vblank() is only
  650. * called during modeset at which time the GPU should be idle and
  651. * should *not* be performing page flips and thus not waiting on
  652. * vblanks...
  653. * Currently, the result of us stealing a vblank from the irq
  654. * handler is that a single frame will be skipped during swapbuffers.
  655. */
  656. I915_WRITE(pipestat_reg,
  657. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  658. /* Wait for vblank interrupt bit to set */
  659. if (wait_for(I915_READ(pipestat_reg) &
  660. PIPE_VBLANK_INTERRUPT_STATUS,
  661. 50))
  662. DRM_DEBUG_KMS("vblank wait timed out\n");
  663. }
  664. /*
  665. * intel_wait_for_pipe_off - wait for pipe to turn off
  666. * @dev: drm device
  667. * @pipe: pipe to wait for
  668. *
  669. * After disabling a pipe, we can't wait for vblank in the usual way,
  670. * spinning on the vblank interrupt status bit, since we won't actually
  671. * see an interrupt when the pipe is disabled.
  672. *
  673. * On Gen4 and above:
  674. * wait for the pipe register state bit to turn off
  675. *
  676. * Otherwise:
  677. * wait for the display line value to settle (it usually
  678. * ends up stopping at the start of the next frame).
  679. *
  680. */
  681. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  682. {
  683. struct drm_i915_private *dev_priv = dev->dev_private;
  684. if (INTEL_INFO(dev)->gen >= 4) {
  685. int reg = PIPECONF(pipe);
  686. /* Wait for the Pipe State to go off */
  687. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  688. 100))
  689. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  690. } else {
  691. u32 last_line;
  692. int reg = PIPEDSL(pipe);
  693. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  694. /* Wait for the display line to settle */
  695. do {
  696. last_line = I915_READ(reg) & DSL_LINEMASK;
  697. mdelay(5);
  698. } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
  699. time_after(timeout, jiffies));
  700. if (time_after(jiffies, timeout))
  701. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  702. }
  703. }
  704. static const char *state_string(bool enabled)
  705. {
  706. return enabled ? "on" : "off";
  707. }
  708. /* Only for pre-ILK configs */
  709. static void assert_pll(struct drm_i915_private *dev_priv,
  710. enum pipe pipe, bool state)
  711. {
  712. int reg;
  713. u32 val;
  714. bool cur_state;
  715. reg = DPLL(pipe);
  716. val = I915_READ(reg);
  717. cur_state = !!(val & DPLL_VCO_ENABLE);
  718. WARN(cur_state != state,
  719. "PLL state assertion failure (expected %s, current %s)\n",
  720. state_string(state), state_string(cur_state));
  721. }
  722. #define assert_pll_enabled(d, p) assert_pll(d, p, true)
  723. #define assert_pll_disabled(d, p) assert_pll(d, p, false)
  724. /* For ILK+ */
  725. static void assert_pch_pll(struct drm_i915_private *dev_priv,
  726. enum pipe pipe, bool state)
  727. {
  728. int reg;
  729. u32 val;
  730. bool cur_state;
  731. reg = PCH_DPLL(pipe);
  732. val = I915_READ(reg);
  733. cur_state = !!(val & DPLL_VCO_ENABLE);
  734. WARN(cur_state != state,
  735. "PCH PLL state assertion failure (expected %s, current %s)\n",
  736. state_string(state), state_string(cur_state));
  737. }
  738. #define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
  739. #define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
  740. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  741. enum pipe pipe, bool state)
  742. {
  743. int reg;
  744. u32 val;
  745. bool cur_state;
  746. reg = FDI_TX_CTL(pipe);
  747. val = I915_READ(reg);
  748. cur_state = !!(val & FDI_TX_ENABLE);
  749. WARN(cur_state != state,
  750. "FDI TX state assertion failure (expected %s, current %s)\n",
  751. state_string(state), state_string(cur_state));
  752. }
  753. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  754. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  755. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  756. enum pipe pipe, bool state)
  757. {
  758. int reg;
  759. u32 val;
  760. bool cur_state;
  761. reg = FDI_RX_CTL(pipe);
  762. val = I915_READ(reg);
  763. cur_state = !!(val & FDI_RX_ENABLE);
  764. WARN(cur_state != state,
  765. "FDI RX state assertion failure (expected %s, current %s)\n",
  766. state_string(state), state_string(cur_state));
  767. }
  768. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  769. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  770. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  771. enum pipe pipe)
  772. {
  773. int reg;
  774. u32 val;
  775. /* ILK FDI PLL is always enabled */
  776. if (dev_priv->info->gen == 5)
  777. return;
  778. reg = FDI_TX_CTL(pipe);
  779. val = I915_READ(reg);
  780. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  781. }
  782. static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
  783. enum pipe pipe)
  784. {
  785. int reg;
  786. u32 val;
  787. reg = FDI_RX_CTL(pipe);
  788. val = I915_READ(reg);
  789. WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
  790. }
  791. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  792. enum pipe pipe)
  793. {
  794. int pp_reg, lvds_reg;
  795. u32 val;
  796. enum pipe panel_pipe = PIPE_A;
  797. bool locked = true;
  798. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  799. pp_reg = PCH_PP_CONTROL;
  800. lvds_reg = PCH_LVDS;
  801. } else {
  802. pp_reg = PP_CONTROL;
  803. lvds_reg = LVDS;
  804. }
  805. val = I915_READ(pp_reg);
  806. if (!(val & PANEL_POWER_ON) ||
  807. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  808. locked = false;
  809. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  810. panel_pipe = PIPE_B;
  811. WARN(panel_pipe == pipe && locked,
  812. "panel assertion failure, pipe %c regs locked\n",
  813. pipe_name(pipe));
  814. }
  815. static void assert_pipe(struct drm_i915_private *dev_priv,
  816. enum pipe pipe, bool state)
  817. {
  818. int reg;
  819. u32 val;
  820. bool cur_state;
  821. reg = PIPECONF(pipe);
  822. val = I915_READ(reg);
  823. cur_state = !!(val & PIPECONF_ENABLE);
  824. WARN(cur_state != state,
  825. "pipe %c assertion failure (expected %s, current %s)\n",
  826. pipe_name(pipe), state_string(state), state_string(cur_state));
  827. }
  828. #define assert_pipe_enabled(d, p) assert_pipe(d, p, true)
  829. #define assert_pipe_disabled(d, p) assert_pipe(d, p, false)
  830. static void assert_plane_enabled(struct drm_i915_private *dev_priv,
  831. enum plane plane)
  832. {
  833. int reg;
  834. u32 val;
  835. reg = DSPCNTR(plane);
  836. val = I915_READ(reg);
  837. WARN(!(val & DISPLAY_PLANE_ENABLE),
  838. "plane %c assertion failure, should be active but is disabled\n",
  839. plane_name(plane));
  840. }
  841. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  842. enum pipe pipe)
  843. {
  844. int reg, i;
  845. u32 val;
  846. int cur_pipe;
  847. /* Planes are fixed to pipes on ILK+ */
  848. if (HAS_PCH_SPLIT(dev_priv->dev))
  849. return;
  850. /* Need to check both planes against the pipe */
  851. for (i = 0; i < 2; i++) {
  852. reg = DSPCNTR(i);
  853. val = I915_READ(reg);
  854. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  855. DISPPLANE_SEL_PIPE_SHIFT;
  856. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  857. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  858. plane_name(i), pipe_name(pipe));
  859. }
  860. }
  861. static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  862. {
  863. u32 val;
  864. bool enabled;
  865. val = I915_READ(PCH_DREF_CONTROL);
  866. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  867. DREF_SUPERSPREAD_SOURCE_MASK));
  868. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  869. }
  870. static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
  871. enum pipe pipe)
  872. {
  873. int reg;
  874. u32 val;
  875. bool enabled;
  876. reg = TRANSCONF(pipe);
  877. val = I915_READ(reg);
  878. enabled = !!(val & TRANS_ENABLE);
  879. WARN(enabled,
  880. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  881. pipe_name(pipe));
  882. }
  883. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  884. enum pipe pipe, u32 port_sel, u32 val)
  885. {
  886. if ((val & DP_PORT_EN) == 0)
  887. return false;
  888. if (HAS_PCH_CPT(dev_priv->dev)) {
  889. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  890. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  891. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  892. return false;
  893. } else {
  894. if ((val & DP_PIPE_MASK) != (pipe << 30))
  895. return false;
  896. }
  897. return true;
  898. }
  899. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  900. enum pipe pipe, u32 val)
  901. {
  902. if ((val & PORT_ENABLE) == 0)
  903. return false;
  904. if (HAS_PCH_CPT(dev_priv->dev)) {
  905. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  906. return false;
  907. } else {
  908. if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
  909. return false;
  910. }
  911. return true;
  912. }
  913. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  914. enum pipe pipe, u32 val)
  915. {
  916. if ((val & LVDS_PORT_EN) == 0)
  917. return false;
  918. if (HAS_PCH_CPT(dev_priv->dev)) {
  919. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  920. return false;
  921. } else {
  922. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  923. return false;
  924. }
  925. return true;
  926. }
  927. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  928. enum pipe pipe, u32 val)
  929. {
  930. if ((val & ADPA_DAC_ENABLE) == 0)
  931. return false;
  932. if (HAS_PCH_CPT(dev_priv->dev)) {
  933. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  934. return false;
  935. } else {
  936. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  937. return false;
  938. }
  939. return true;
  940. }
  941. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  942. enum pipe pipe, int reg, u32 port_sel)
  943. {
  944. u32 val = I915_READ(reg);
  945. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  946. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  947. reg, pipe_name(pipe));
  948. }
  949. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  950. enum pipe pipe, int reg)
  951. {
  952. u32 val = I915_READ(reg);
  953. WARN(hdmi_pipe_enabled(dev_priv, val, pipe),
  954. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  955. reg, pipe_name(pipe));
  956. }
  957. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  958. enum pipe pipe)
  959. {
  960. int reg;
  961. u32 val;
  962. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  963. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  964. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  965. reg = PCH_ADPA;
  966. val = I915_READ(reg);
  967. WARN(adpa_pipe_enabled(dev_priv, val, pipe),
  968. "PCH VGA enabled on transcoder %c, should be disabled\n",
  969. pipe_name(pipe));
  970. reg = PCH_LVDS;
  971. val = I915_READ(reg);
  972. WARN(lvds_pipe_enabled(dev_priv, val, pipe),
  973. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  974. pipe_name(pipe));
  975. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
  976. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
  977. assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
  978. }
  979. /**
  980. * intel_enable_pll - enable a PLL
  981. * @dev_priv: i915 private structure
  982. * @pipe: pipe PLL to enable
  983. *
  984. * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
  985. * make sure the PLL reg is writable first though, since the panel write
  986. * protect mechanism may be enabled.
  987. *
  988. * Note! This is for pre-ILK only.
  989. */
  990. static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  991. {
  992. int reg;
  993. u32 val;
  994. /* No really, not for ILK+ */
  995. BUG_ON(dev_priv->info->gen >= 5);
  996. /* PLL is protected by panel, make sure we can write it */
  997. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  998. assert_panel_unlocked(dev_priv, pipe);
  999. reg = DPLL(pipe);
  1000. val = I915_READ(reg);
  1001. val |= DPLL_VCO_ENABLE;
  1002. /* We do this three times for luck */
  1003. I915_WRITE(reg, val);
  1004. POSTING_READ(reg);
  1005. udelay(150); /* wait for warmup */
  1006. I915_WRITE(reg, val);
  1007. POSTING_READ(reg);
  1008. udelay(150); /* wait for warmup */
  1009. I915_WRITE(reg, val);
  1010. POSTING_READ(reg);
  1011. udelay(150); /* wait for warmup */
  1012. }
  1013. /**
  1014. * intel_disable_pll - disable a PLL
  1015. * @dev_priv: i915 private structure
  1016. * @pipe: pipe PLL to disable
  1017. *
  1018. * Disable the PLL for @pipe, making sure the pipe is off first.
  1019. *
  1020. * Note! This is for pre-ILK only.
  1021. */
  1022. static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1023. {
  1024. int reg;
  1025. u32 val;
  1026. /* Don't disable pipe A or pipe A PLLs if needed */
  1027. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1028. return;
  1029. /* Make sure the pipe isn't still relying on us */
  1030. assert_pipe_disabled(dev_priv, pipe);
  1031. reg = DPLL(pipe);
  1032. val = I915_READ(reg);
  1033. val &= ~DPLL_VCO_ENABLE;
  1034. I915_WRITE(reg, val);
  1035. POSTING_READ(reg);
  1036. }
  1037. /**
  1038. * intel_enable_pch_pll - enable PCH PLL
  1039. * @dev_priv: i915 private structure
  1040. * @pipe: pipe PLL to enable
  1041. *
  1042. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1043. * drives the transcoder clock.
  1044. */
  1045. static void intel_enable_pch_pll(struct drm_i915_private *dev_priv,
  1046. enum pipe pipe)
  1047. {
  1048. int reg;
  1049. u32 val;
  1050. if (pipe > 1)
  1051. return;
  1052. /* PCH only available on ILK+ */
  1053. BUG_ON(dev_priv->info->gen < 5);
  1054. /* PCH refclock must be enabled first */
  1055. assert_pch_refclk_enabled(dev_priv);
  1056. reg = PCH_DPLL(pipe);
  1057. val = I915_READ(reg);
  1058. val |= DPLL_VCO_ENABLE;
  1059. I915_WRITE(reg, val);
  1060. POSTING_READ(reg);
  1061. udelay(200);
  1062. }
  1063. static void intel_disable_pch_pll(struct drm_i915_private *dev_priv,
  1064. enum pipe pipe)
  1065. {
  1066. int reg;
  1067. u32 val;
  1068. if (pipe > 1)
  1069. return;
  1070. /* PCH only available on ILK+ */
  1071. BUG_ON(dev_priv->info->gen < 5);
  1072. /* Make sure transcoder isn't still depending on us */
  1073. assert_transcoder_disabled(dev_priv, pipe);
  1074. reg = PCH_DPLL(pipe);
  1075. val = I915_READ(reg);
  1076. val &= ~DPLL_VCO_ENABLE;
  1077. I915_WRITE(reg, val);
  1078. POSTING_READ(reg);
  1079. udelay(200);
  1080. }
  1081. static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
  1082. enum pipe pipe)
  1083. {
  1084. int reg;
  1085. u32 val;
  1086. /* PCH only available on ILK+ */
  1087. BUG_ON(dev_priv->info->gen < 5);
  1088. /* Make sure PCH DPLL is enabled */
  1089. assert_pch_pll_enabled(dev_priv, pipe);
  1090. /* FDI must be feeding us bits for PCH ports */
  1091. assert_fdi_tx_enabled(dev_priv, pipe);
  1092. assert_fdi_rx_enabled(dev_priv, pipe);
  1093. reg = TRANSCONF(pipe);
  1094. val = I915_READ(reg);
  1095. if (HAS_PCH_IBX(dev_priv->dev)) {
  1096. /*
  1097. * make the BPC in transcoder be consistent with
  1098. * that in pipeconf reg.
  1099. */
  1100. val &= ~PIPE_BPC_MASK;
  1101. val |= I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK;
  1102. }
  1103. I915_WRITE(reg, val | TRANS_ENABLE);
  1104. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1105. DRM_ERROR("failed to enable transcoder %d\n", pipe);
  1106. }
  1107. static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
  1108. enum pipe pipe)
  1109. {
  1110. int reg;
  1111. u32 val;
  1112. /* FDI relies on the transcoder */
  1113. assert_fdi_tx_disabled(dev_priv, pipe);
  1114. assert_fdi_rx_disabled(dev_priv, pipe);
  1115. /* Ports must be off as well */
  1116. assert_pch_ports_disabled(dev_priv, pipe);
  1117. reg = TRANSCONF(pipe);
  1118. val = I915_READ(reg);
  1119. val &= ~TRANS_ENABLE;
  1120. I915_WRITE(reg, val);
  1121. /* wait for PCH transcoder off, transcoder state */
  1122. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1123. DRM_ERROR("failed to disable transcoder\n");
  1124. }
  1125. /**
  1126. * intel_enable_pipe - enable a pipe, asserting requirements
  1127. * @dev_priv: i915 private structure
  1128. * @pipe: pipe to enable
  1129. * @pch_port: on ILK+, is this pipe driving a PCH port or not
  1130. *
  1131. * Enable @pipe, making sure that various hardware specific requirements
  1132. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1133. *
  1134. * @pipe should be %PIPE_A or %PIPE_B.
  1135. *
  1136. * Will wait until the pipe is actually running (i.e. first vblank) before
  1137. * returning.
  1138. */
  1139. static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
  1140. bool pch_port)
  1141. {
  1142. int reg;
  1143. u32 val;
  1144. /*
  1145. * A pipe without a PLL won't actually be able to drive bits from
  1146. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1147. * need the check.
  1148. */
  1149. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1150. assert_pll_enabled(dev_priv, pipe);
  1151. else {
  1152. if (pch_port) {
  1153. /* if driving the PCH, we need FDI enabled */
  1154. assert_fdi_rx_pll_enabled(dev_priv, pipe);
  1155. assert_fdi_tx_pll_enabled(dev_priv, pipe);
  1156. }
  1157. /* FIXME: assert CPU port conditions for SNB+ */
  1158. }
  1159. reg = PIPECONF(pipe);
  1160. val = I915_READ(reg);
  1161. if (val & PIPECONF_ENABLE)
  1162. return;
  1163. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1164. intel_wait_for_vblank(dev_priv->dev, pipe);
  1165. }
  1166. /**
  1167. * intel_disable_pipe - disable a pipe, asserting requirements
  1168. * @dev_priv: i915 private structure
  1169. * @pipe: pipe to disable
  1170. *
  1171. * Disable @pipe, making sure that various hardware specific requirements
  1172. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1173. *
  1174. * @pipe should be %PIPE_A or %PIPE_B.
  1175. *
  1176. * Will wait until the pipe has shut down before returning.
  1177. */
  1178. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1179. enum pipe pipe)
  1180. {
  1181. int reg;
  1182. u32 val;
  1183. /*
  1184. * Make sure planes won't keep trying to pump pixels to us,
  1185. * or we might hang the display.
  1186. */
  1187. assert_planes_disabled(dev_priv, pipe);
  1188. /* Don't disable pipe A or pipe A PLLs if needed */
  1189. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1190. return;
  1191. reg = PIPECONF(pipe);
  1192. val = I915_READ(reg);
  1193. if ((val & PIPECONF_ENABLE) == 0)
  1194. return;
  1195. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1196. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1197. }
  1198. /*
  1199. * Plane regs are double buffered, going from enabled->disabled needs a
  1200. * trigger in order to latch. The display address reg provides this.
  1201. */
  1202. static void intel_flush_display_plane(struct drm_i915_private *dev_priv,
  1203. enum plane plane)
  1204. {
  1205. I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
  1206. I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
  1207. }
  1208. /**
  1209. * intel_enable_plane - enable a display plane on a given pipe
  1210. * @dev_priv: i915 private structure
  1211. * @plane: plane to enable
  1212. * @pipe: pipe being fed
  1213. *
  1214. * Enable @plane on @pipe, making sure that @pipe is running first.
  1215. */
  1216. static void intel_enable_plane(struct drm_i915_private *dev_priv,
  1217. enum plane plane, enum pipe pipe)
  1218. {
  1219. int reg;
  1220. u32 val;
  1221. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1222. assert_pipe_enabled(dev_priv, pipe);
  1223. reg = DSPCNTR(plane);
  1224. val = I915_READ(reg);
  1225. if (val & DISPLAY_PLANE_ENABLE)
  1226. return;
  1227. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1228. intel_flush_display_plane(dev_priv, plane);
  1229. intel_wait_for_vblank(dev_priv->dev, pipe);
  1230. }
  1231. /**
  1232. * intel_disable_plane - disable a display plane
  1233. * @dev_priv: i915 private structure
  1234. * @plane: plane to disable
  1235. * @pipe: pipe consuming the data
  1236. *
  1237. * Disable @plane; should be an independent operation.
  1238. */
  1239. static void intel_disable_plane(struct drm_i915_private *dev_priv,
  1240. enum plane plane, enum pipe pipe)
  1241. {
  1242. int reg;
  1243. u32 val;
  1244. reg = DSPCNTR(plane);
  1245. val = I915_READ(reg);
  1246. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  1247. return;
  1248. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1249. intel_flush_display_plane(dev_priv, plane);
  1250. intel_wait_for_vblank(dev_priv->dev, pipe);
  1251. }
  1252. static void disable_pch_dp(struct drm_i915_private *dev_priv,
  1253. enum pipe pipe, int reg, u32 port_sel)
  1254. {
  1255. u32 val = I915_READ(reg);
  1256. if (dp_pipe_enabled(dev_priv, pipe, port_sel, val)) {
  1257. DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
  1258. I915_WRITE(reg, val & ~DP_PORT_EN);
  1259. }
  1260. }
  1261. static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
  1262. enum pipe pipe, int reg)
  1263. {
  1264. u32 val = I915_READ(reg);
  1265. if (hdmi_pipe_enabled(dev_priv, val, pipe)) {
  1266. DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
  1267. reg, pipe);
  1268. I915_WRITE(reg, val & ~PORT_ENABLE);
  1269. }
  1270. }
  1271. /* Disable any ports connected to this transcoder */
  1272. static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
  1273. enum pipe pipe)
  1274. {
  1275. u32 reg, val;
  1276. val = I915_READ(PCH_PP_CONTROL);
  1277. I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
  1278. disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1279. disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1280. disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1281. reg = PCH_ADPA;
  1282. val = I915_READ(reg);
  1283. if (adpa_pipe_enabled(dev_priv, val, pipe))
  1284. I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
  1285. reg = PCH_LVDS;
  1286. val = I915_READ(reg);
  1287. if (lvds_pipe_enabled(dev_priv, val, pipe)) {
  1288. DRM_DEBUG_KMS("disable lvds on pipe %d val 0x%08x\n", pipe, val);
  1289. I915_WRITE(reg, val & ~LVDS_PORT_EN);
  1290. POSTING_READ(reg);
  1291. udelay(100);
  1292. }
  1293. disable_pch_hdmi(dev_priv, pipe, HDMIB);
  1294. disable_pch_hdmi(dev_priv, pipe, HDMIC);
  1295. disable_pch_hdmi(dev_priv, pipe, HDMID);
  1296. }
  1297. static void i8xx_disable_fbc(struct drm_device *dev)
  1298. {
  1299. struct drm_i915_private *dev_priv = dev->dev_private;
  1300. u32 fbc_ctl;
  1301. /* Disable compression */
  1302. fbc_ctl = I915_READ(FBC_CONTROL);
  1303. if ((fbc_ctl & FBC_CTL_EN) == 0)
  1304. return;
  1305. fbc_ctl &= ~FBC_CTL_EN;
  1306. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1307. /* Wait for compressing bit to clear */
  1308. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  1309. DRM_DEBUG_KMS("FBC idle timed out\n");
  1310. return;
  1311. }
  1312. DRM_DEBUG_KMS("disabled FBC\n");
  1313. }
  1314. static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1315. {
  1316. struct drm_device *dev = crtc->dev;
  1317. struct drm_i915_private *dev_priv = dev->dev_private;
  1318. struct drm_framebuffer *fb = crtc->fb;
  1319. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1320. struct drm_i915_gem_object *obj = intel_fb->obj;
  1321. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1322. int cfb_pitch;
  1323. int plane, i;
  1324. u32 fbc_ctl, fbc_ctl2;
  1325. cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  1326. if (fb->pitch < cfb_pitch)
  1327. cfb_pitch = fb->pitch;
  1328. /* FBC_CTL wants 64B units */
  1329. cfb_pitch = (cfb_pitch / 64) - 1;
  1330. plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  1331. /* Clear old tags */
  1332. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  1333. I915_WRITE(FBC_TAG + (i * 4), 0);
  1334. /* Set it up... */
  1335. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
  1336. fbc_ctl2 |= plane;
  1337. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  1338. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  1339. /* enable it... */
  1340. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  1341. if (IS_I945GM(dev))
  1342. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  1343. fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  1344. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  1345. fbc_ctl |= obj->fence_reg;
  1346. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1347. DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
  1348. cfb_pitch, crtc->y, intel_crtc->plane);
  1349. }
  1350. static bool i8xx_fbc_enabled(struct drm_device *dev)
  1351. {
  1352. struct drm_i915_private *dev_priv = dev->dev_private;
  1353. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  1354. }
  1355. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1356. {
  1357. struct drm_device *dev = crtc->dev;
  1358. struct drm_i915_private *dev_priv = dev->dev_private;
  1359. struct drm_framebuffer *fb = crtc->fb;
  1360. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1361. struct drm_i915_gem_object *obj = intel_fb->obj;
  1362. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1363. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1364. unsigned long stall_watermark = 200;
  1365. u32 dpfc_ctl;
  1366. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  1367. dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
  1368. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  1369. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1370. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1371. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1372. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  1373. /* enable it... */
  1374. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  1375. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1376. }
  1377. static void g4x_disable_fbc(struct drm_device *dev)
  1378. {
  1379. struct drm_i915_private *dev_priv = dev->dev_private;
  1380. u32 dpfc_ctl;
  1381. /* Disable compression */
  1382. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1383. if (dpfc_ctl & DPFC_CTL_EN) {
  1384. dpfc_ctl &= ~DPFC_CTL_EN;
  1385. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  1386. DRM_DEBUG_KMS("disabled FBC\n");
  1387. }
  1388. }
  1389. static bool g4x_fbc_enabled(struct drm_device *dev)
  1390. {
  1391. struct drm_i915_private *dev_priv = dev->dev_private;
  1392. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  1393. }
  1394. static void sandybridge_blit_fbc_update(struct drm_device *dev)
  1395. {
  1396. struct drm_i915_private *dev_priv = dev->dev_private;
  1397. u32 blt_ecoskpd;
  1398. /* Make sure blitter notifies FBC of writes */
  1399. gen6_gt_force_wake_get(dev_priv);
  1400. blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
  1401. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
  1402. GEN6_BLITTER_LOCK_SHIFT;
  1403. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1404. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
  1405. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1406. blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
  1407. GEN6_BLITTER_LOCK_SHIFT);
  1408. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1409. POSTING_READ(GEN6_BLITTER_ECOSKPD);
  1410. gen6_gt_force_wake_put(dev_priv);
  1411. }
  1412. static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1413. {
  1414. struct drm_device *dev = crtc->dev;
  1415. struct drm_i915_private *dev_priv = dev->dev_private;
  1416. struct drm_framebuffer *fb = crtc->fb;
  1417. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1418. struct drm_i915_gem_object *obj = intel_fb->obj;
  1419. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1420. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1421. unsigned long stall_watermark = 200;
  1422. u32 dpfc_ctl;
  1423. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1424. dpfc_ctl &= DPFC_RESERVED;
  1425. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  1426. /* Set persistent mode for front-buffer rendering, ala X. */
  1427. dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
  1428. dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
  1429. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  1430. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1431. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1432. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1433. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  1434. I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
  1435. /* enable it... */
  1436. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  1437. if (IS_GEN6(dev)) {
  1438. I915_WRITE(SNB_DPFC_CTL_SA,
  1439. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  1440. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  1441. sandybridge_blit_fbc_update(dev);
  1442. }
  1443. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1444. }
  1445. static void ironlake_disable_fbc(struct drm_device *dev)
  1446. {
  1447. struct drm_i915_private *dev_priv = dev->dev_private;
  1448. u32 dpfc_ctl;
  1449. /* Disable compression */
  1450. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1451. if (dpfc_ctl & DPFC_CTL_EN) {
  1452. dpfc_ctl &= ~DPFC_CTL_EN;
  1453. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  1454. DRM_DEBUG_KMS("disabled FBC\n");
  1455. }
  1456. }
  1457. static bool ironlake_fbc_enabled(struct drm_device *dev)
  1458. {
  1459. struct drm_i915_private *dev_priv = dev->dev_private;
  1460. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  1461. }
  1462. bool intel_fbc_enabled(struct drm_device *dev)
  1463. {
  1464. struct drm_i915_private *dev_priv = dev->dev_private;
  1465. if (!dev_priv->display.fbc_enabled)
  1466. return false;
  1467. return dev_priv->display.fbc_enabled(dev);
  1468. }
  1469. static void intel_fbc_work_fn(struct work_struct *__work)
  1470. {
  1471. struct intel_fbc_work *work =
  1472. container_of(to_delayed_work(__work),
  1473. struct intel_fbc_work, work);
  1474. struct drm_device *dev = work->crtc->dev;
  1475. struct drm_i915_private *dev_priv = dev->dev_private;
  1476. mutex_lock(&dev->struct_mutex);
  1477. if (work == dev_priv->fbc_work) {
  1478. /* Double check that we haven't switched fb without cancelling
  1479. * the prior work.
  1480. */
  1481. if (work->crtc->fb == work->fb) {
  1482. dev_priv->display.enable_fbc(work->crtc,
  1483. work->interval);
  1484. dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
  1485. dev_priv->cfb_fb = work->crtc->fb->base.id;
  1486. dev_priv->cfb_y = work->crtc->y;
  1487. }
  1488. dev_priv->fbc_work = NULL;
  1489. }
  1490. mutex_unlock(&dev->struct_mutex);
  1491. kfree(work);
  1492. }
  1493. static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
  1494. {
  1495. if (dev_priv->fbc_work == NULL)
  1496. return;
  1497. DRM_DEBUG_KMS("cancelling pending FBC enable\n");
  1498. /* Synchronisation is provided by struct_mutex and checking of
  1499. * dev_priv->fbc_work, so we can perform the cancellation
  1500. * entirely asynchronously.
  1501. */
  1502. if (cancel_delayed_work(&dev_priv->fbc_work->work))
  1503. /* tasklet was killed before being run, clean up */
  1504. kfree(dev_priv->fbc_work);
  1505. /* Mark the work as no longer wanted so that if it does
  1506. * wake-up (because the work was already running and waiting
  1507. * for our mutex), it will discover that is no longer
  1508. * necessary to run.
  1509. */
  1510. dev_priv->fbc_work = NULL;
  1511. }
  1512. static void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1513. {
  1514. struct intel_fbc_work *work;
  1515. struct drm_device *dev = crtc->dev;
  1516. struct drm_i915_private *dev_priv = dev->dev_private;
  1517. if (!dev_priv->display.enable_fbc)
  1518. return;
  1519. intel_cancel_fbc_work(dev_priv);
  1520. work = kzalloc(sizeof *work, GFP_KERNEL);
  1521. if (work == NULL) {
  1522. dev_priv->display.enable_fbc(crtc, interval);
  1523. return;
  1524. }
  1525. work->crtc = crtc;
  1526. work->fb = crtc->fb;
  1527. work->interval = interval;
  1528. INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
  1529. dev_priv->fbc_work = work;
  1530. DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
  1531. /* Delay the actual enabling to let pageflipping cease and the
  1532. * display to settle before starting the compression. Note that
  1533. * this delay also serves a second purpose: it allows for a
  1534. * vblank to pass after disabling the FBC before we attempt
  1535. * to modify the control registers.
  1536. *
  1537. * A more complicated solution would involve tracking vblanks
  1538. * following the termination of the page-flipping sequence
  1539. * and indeed performing the enable as a co-routine and not
  1540. * waiting synchronously upon the vblank.
  1541. */
  1542. schedule_delayed_work(&work->work, msecs_to_jiffies(50));
  1543. }
  1544. void intel_disable_fbc(struct drm_device *dev)
  1545. {
  1546. struct drm_i915_private *dev_priv = dev->dev_private;
  1547. intel_cancel_fbc_work(dev_priv);
  1548. if (!dev_priv->display.disable_fbc)
  1549. return;
  1550. dev_priv->display.disable_fbc(dev);
  1551. dev_priv->cfb_plane = -1;
  1552. }
  1553. /**
  1554. * intel_update_fbc - enable/disable FBC as needed
  1555. * @dev: the drm_device
  1556. *
  1557. * Set up the framebuffer compression hardware at mode set time. We
  1558. * enable it if possible:
  1559. * - plane A only (on pre-965)
  1560. * - no pixel mulitply/line duplication
  1561. * - no alpha buffer discard
  1562. * - no dual wide
  1563. * - framebuffer <= 2048 in width, 1536 in height
  1564. *
  1565. * We can't assume that any compression will take place (worst case),
  1566. * so the compressed buffer has to be the same size as the uncompressed
  1567. * one. It also must reside (along with the line length buffer) in
  1568. * stolen memory.
  1569. *
  1570. * We need to enable/disable FBC on a global basis.
  1571. */
  1572. static void intel_update_fbc(struct drm_device *dev)
  1573. {
  1574. struct drm_i915_private *dev_priv = dev->dev_private;
  1575. struct drm_crtc *crtc = NULL, *tmp_crtc;
  1576. struct intel_crtc *intel_crtc;
  1577. struct drm_framebuffer *fb;
  1578. struct intel_framebuffer *intel_fb;
  1579. struct drm_i915_gem_object *obj;
  1580. int enable_fbc;
  1581. DRM_DEBUG_KMS("\n");
  1582. if (!i915_powersave)
  1583. return;
  1584. if (!I915_HAS_FBC(dev))
  1585. return;
  1586. /*
  1587. * If FBC is already on, we just have to verify that we can
  1588. * keep it that way...
  1589. * Need to disable if:
  1590. * - more than one pipe is active
  1591. * - changing FBC params (stride, fence, mode)
  1592. * - new fb is too large to fit in compressed buffer
  1593. * - going to an unsupported config (interlace, pixel multiply, etc.)
  1594. */
  1595. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  1596. if (tmp_crtc->enabled && tmp_crtc->fb) {
  1597. if (crtc) {
  1598. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  1599. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  1600. goto out_disable;
  1601. }
  1602. crtc = tmp_crtc;
  1603. }
  1604. }
  1605. if (!crtc || crtc->fb == NULL) {
  1606. DRM_DEBUG_KMS("no output, disabling\n");
  1607. dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
  1608. goto out_disable;
  1609. }
  1610. intel_crtc = to_intel_crtc(crtc);
  1611. fb = crtc->fb;
  1612. intel_fb = to_intel_framebuffer(fb);
  1613. obj = intel_fb->obj;
  1614. enable_fbc = i915_enable_fbc;
  1615. if (enable_fbc < 0) {
  1616. DRM_DEBUG_KMS("fbc set to per-chip default\n");
  1617. enable_fbc = 1;
  1618. if (INTEL_INFO(dev)->gen <= 5)
  1619. enable_fbc = 0;
  1620. }
  1621. if (!enable_fbc) {
  1622. DRM_DEBUG_KMS("fbc disabled per module param\n");
  1623. dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
  1624. goto out_disable;
  1625. }
  1626. if (intel_fb->obj->base.size > dev_priv->cfb_size) {
  1627. DRM_DEBUG_KMS("framebuffer too large, disabling "
  1628. "compression\n");
  1629. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  1630. goto out_disable;
  1631. }
  1632. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  1633. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  1634. DRM_DEBUG_KMS("mode incompatible with compression, "
  1635. "disabling\n");
  1636. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  1637. goto out_disable;
  1638. }
  1639. if ((crtc->mode.hdisplay > 2048) ||
  1640. (crtc->mode.vdisplay > 1536)) {
  1641. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  1642. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  1643. goto out_disable;
  1644. }
  1645. if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
  1646. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  1647. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  1648. goto out_disable;
  1649. }
  1650. /* The use of a CPU fence is mandatory in order to detect writes
  1651. * by the CPU to the scanout and trigger updates to the FBC.
  1652. */
  1653. if (obj->tiling_mode != I915_TILING_X ||
  1654. obj->fence_reg == I915_FENCE_REG_NONE) {
  1655. DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
  1656. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  1657. goto out_disable;
  1658. }
  1659. /* If the kernel debugger is active, always disable compression */
  1660. if (in_dbg_master())
  1661. goto out_disable;
  1662. /* If the scanout has not changed, don't modify the FBC settings.
  1663. * Note that we make the fundamental assumption that the fb->obj
  1664. * cannot be unpinned (and have its GTT offset and fence revoked)
  1665. * without first being decoupled from the scanout and FBC disabled.
  1666. */
  1667. if (dev_priv->cfb_plane == intel_crtc->plane &&
  1668. dev_priv->cfb_fb == fb->base.id &&
  1669. dev_priv->cfb_y == crtc->y)
  1670. return;
  1671. if (intel_fbc_enabled(dev)) {
  1672. /* We update FBC along two paths, after changing fb/crtc
  1673. * configuration (modeswitching) and after page-flipping
  1674. * finishes. For the latter, we know that not only did
  1675. * we disable the FBC at the start of the page-flip
  1676. * sequence, but also more than one vblank has passed.
  1677. *
  1678. * For the former case of modeswitching, it is possible
  1679. * to switch between two FBC valid configurations
  1680. * instantaneously so we do need to disable the FBC
  1681. * before we can modify its control registers. We also
  1682. * have to wait for the next vblank for that to take
  1683. * effect. However, since we delay enabling FBC we can
  1684. * assume that a vblank has passed since disabling and
  1685. * that we can safely alter the registers in the deferred
  1686. * callback.
  1687. *
  1688. * In the scenario that we go from a valid to invalid
  1689. * and then back to valid FBC configuration we have
  1690. * no strict enforcement that a vblank occurred since
  1691. * disabling the FBC. However, along all current pipe
  1692. * disabling paths we do need to wait for a vblank at
  1693. * some point. And we wait before enabling FBC anyway.
  1694. */
  1695. DRM_DEBUG_KMS("disabling active FBC for update\n");
  1696. intel_disable_fbc(dev);
  1697. }
  1698. intel_enable_fbc(crtc, 500);
  1699. return;
  1700. out_disable:
  1701. /* Multiple disables should be harmless */
  1702. if (intel_fbc_enabled(dev)) {
  1703. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  1704. intel_disable_fbc(dev);
  1705. }
  1706. }
  1707. int
  1708. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1709. struct drm_i915_gem_object *obj,
  1710. struct intel_ring_buffer *pipelined)
  1711. {
  1712. struct drm_i915_private *dev_priv = dev->dev_private;
  1713. u32 alignment;
  1714. int ret;
  1715. switch (obj->tiling_mode) {
  1716. case I915_TILING_NONE:
  1717. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1718. alignment = 128 * 1024;
  1719. else if (INTEL_INFO(dev)->gen >= 4)
  1720. alignment = 4 * 1024;
  1721. else
  1722. alignment = 64 * 1024;
  1723. break;
  1724. case I915_TILING_X:
  1725. /* pin() will align the object as required by fence */
  1726. alignment = 0;
  1727. break;
  1728. case I915_TILING_Y:
  1729. /* FIXME: Is this true? */
  1730. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1731. return -EINVAL;
  1732. default:
  1733. BUG();
  1734. }
  1735. dev_priv->mm.interruptible = false;
  1736. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1737. if (ret)
  1738. goto err_interruptible;
  1739. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1740. * fence, whereas 965+ only requires a fence if using
  1741. * framebuffer compression. For simplicity, we always install
  1742. * a fence as the cost is not that onerous.
  1743. */
  1744. if (obj->tiling_mode != I915_TILING_NONE) {
  1745. ret = i915_gem_object_get_fence(obj, pipelined);
  1746. if (ret)
  1747. goto err_unpin;
  1748. }
  1749. dev_priv->mm.interruptible = true;
  1750. return 0;
  1751. err_unpin:
  1752. i915_gem_object_unpin(obj);
  1753. err_interruptible:
  1754. dev_priv->mm.interruptible = true;
  1755. return ret;
  1756. }
  1757. static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1758. int x, int y)
  1759. {
  1760. struct drm_device *dev = crtc->dev;
  1761. struct drm_i915_private *dev_priv = dev->dev_private;
  1762. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1763. struct intel_framebuffer *intel_fb;
  1764. struct drm_i915_gem_object *obj;
  1765. int plane = intel_crtc->plane;
  1766. unsigned long Start, Offset;
  1767. u32 dspcntr;
  1768. u32 reg;
  1769. switch (plane) {
  1770. case 0:
  1771. case 1:
  1772. break;
  1773. default:
  1774. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1775. return -EINVAL;
  1776. }
  1777. intel_fb = to_intel_framebuffer(fb);
  1778. obj = intel_fb->obj;
  1779. reg = DSPCNTR(plane);
  1780. dspcntr = I915_READ(reg);
  1781. /* Mask out pixel format bits in case we change it */
  1782. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1783. switch (fb->bits_per_pixel) {
  1784. case 8:
  1785. dspcntr |= DISPPLANE_8BPP;
  1786. break;
  1787. case 16:
  1788. if (fb->depth == 15)
  1789. dspcntr |= DISPPLANE_15_16BPP;
  1790. else
  1791. dspcntr |= DISPPLANE_16BPP;
  1792. break;
  1793. case 24:
  1794. case 32:
  1795. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1796. break;
  1797. default:
  1798. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1799. return -EINVAL;
  1800. }
  1801. if (INTEL_INFO(dev)->gen >= 4) {
  1802. if (obj->tiling_mode != I915_TILING_NONE)
  1803. dspcntr |= DISPPLANE_TILED;
  1804. else
  1805. dspcntr &= ~DISPPLANE_TILED;
  1806. }
  1807. I915_WRITE(reg, dspcntr);
  1808. Start = obj->gtt_offset;
  1809. Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
  1810. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1811. Start, Offset, x, y, fb->pitch);
  1812. I915_WRITE(DSPSTRIDE(plane), fb->pitch);
  1813. if (INTEL_INFO(dev)->gen >= 4) {
  1814. I915_WRITE(DSPSURF(plane), Start);
  1815. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1816. I915_WRITE(DSPADDR(plane), Offset);
  1817. } else
  1818. I915_WRITE(DSPADDR(plane), Start + Offset);
  1819. POSTING_READ(reg);
  1820. return 0;
  1821. }
  1822. static int ironlake_update_plane(struct drm_crtc *crtc,
  1823. struct drm_framebuffer *fb, int x, int y)
  1824. {
  1825. struct drm_device *dev = crtc->dev;
  1826. struct drm_i915_private *dev_priv = dev->dev_private;
  1827. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1828. struct intel_framebuffer *intel_fb;
  1829. struct drm_i915_gem_object *obj;
  1830. int plane = intel_crtc->plane;
  1831. unsigned long Start, Offset;
  1832. u32 dspcntr;
  1833. u32 reg;
  1834. switch (plane) {
  1835. case 0:
  1836. case 1:
  1837. case 2:
  1838. break;
  1839. default:
  1840. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1841. return -EINVAL;
  1842. }
  1843. intel_fb = to_intel_framebuffer(fb);
  1844. obj = intel_fb->obj;
  1845. reg = DSPCNTR(plane);
  1846. dspcntr = I915_READ(reg);
  1847. /* Mask out pixel format bits in case we change it */
  1848. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1849. switch (fb->bits_per_pixel) {
  1850. case 8:
  1851. dspcntr |= DISPPLANE_8BPP;
  1852. break;
  1853. case 16:
  1854. if (fb->depth != 16)
  1855. return -EINVAL;
  1856. dspcntr |= DISPPLANE_16BPP;
  1857. break;
  1858. case 24:
  1859. case 32:
  1860. if (fb->depth == 24)
  1861. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1862. else if (fb->depth == 30)
  1863. dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
  1864. else
  1865. return -EINVAL;
  1866. break;
  1867. default:
  1868. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1869. return -EINVAL;
  1870. }
  1871. if (obj->tiling_mode != I915_TILING_NONE)
  1872. dspcntr |= DISPPLANE_TILED;
  1873. else
  1874. dspcntr &= ~DISPPLANE_TILED;
  1875. /* must disable */
  1876. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1877. I915_WRITE(reg, dspcntr);
  1878. Start = obj->gtt_offset;
  1879. Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
  1880. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1881. Start, Offset, x, y, fb->pitch);
  1882. I915_WRITE(DSPSTRIDE(plane), fb->pitch);
  1883. I915_WRITE(DSPSURF(plane), Start);
  1884. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1885. I915_WRITE(DSPADDR(plane), Offset);
  1886. POSTING_READ(reg);
  1887. return 0;
  1888. }
  1889. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1890. static int
  1891. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1892. int x, int y, enum mode_set_atomic state)
  1893. {
  1894. struct drm_device *dev = crtc->dev;
  1895. struct drm_i915_private *dev_priv = dev->dev_private;
  1896. int ret;
  1897. ret = dev_priv->display.update_plane(crtc, fb, x, y);
  1898. if (ret)
  1899. return ret;
  1900. intel_update_fbc(dev);
  1901. intel_increase_pllclock(crtc);
  1902. return 0;
  1903. }
  1904. static int
  1905. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1906. struct drm_framebuffer *old_fb)
  1907. {
  1908. struct drm_device *dev = crtc->dev;
  1909. struct drm_i915_master_private *master_priv;
  1910. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1911. int ret;
  1912. /* no fb bound */
  1913. if (!crtc->fb) {
  1914. DRM_ERROR("No FB bound\n");
  1915. return 0;
  1916. }
  1917. switch (intel_crtc->plane) {
  1918. case 0:
  1919. case 1:
  1920. break;
  1921. case 2:
  1922. if (IS_IVYBRIDGE(dev))
  1923. break;
  1924. /* fall through otherwise */
  1925. default:
  1926. DRM_ERROR("no plane for crtc\n");
  1927. return -EINVAL;
  1928. }
  1929. mutex_lock(&dev->struct_mutex);
  1930. ret = intel_pin_and_fence_fb_obj(dev,
  1931. to_intel_framebuffer(crtc->fb)->obj,
  1932. NULL);
  1933. if (ret != 0) {
  1934. mutex_unlock(&dev->struct_mutex);
  1935. DRM_ERROR("pin & fence failed\n");
  1936. return ret;
  1937. }
  1938. if (old_fb) {
  1939. struct drm_i915_private *dev_priv = dev->dev_private;
  1940. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1941. wait_event(dev_priv->pending_flip_queue,
  1942. atomic_read(&dev_priv->mm.wedged) ||
  1943. atomic_read(&obj->pending_flip) == 0);
  1944. /* Big Hammer, we also need to ensure that any pending
  1945. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1946. * current scanout is retired before unpinning the old
  1947. * framebuffer.
  1948. *
  1949. * This should only fail upon a hung GPU, in which case we
  1950. * can safely continue.
  1951. */
  1952. ret = i915_gem_object_finish_gpu(obj);
  1953. (void) ret;
  1954. }
  1955. ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
  1956. LEAVE_ATOMIC_MODE_SET);
  1957. if (ret) {
  1958. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  1959. mutex_unlock(&dev->struct_mutex);
  1960. DRM_ERROR("failed to update base address\n");
  1961. return ret;
  1962. }
  1963. if (old_fb) {
  1964. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1965. i915_gem_object_unpin(to_intel_framebuffer(old_fb)->obj);
  1966. }
  1967. mutex_unlock(&dev->struct_mutex);
  1968. if (!dev->primary->master)
  1969. return 0;
  1970. master_priv = dev->primary->master->driver_priv;
  1971. if (!master_priv->sarea_priv)
  1972. return 0;
  1973. if (intel_crtc->pipe) {
  1974. master_priv->sarea_priv->pipeB_x = x;
  1975. master_priv->sarea_priv->pipeB_y = y;
  1976. } else {
  1977. master_priv->sarea_priv->pipeA_x = x;
  1978. master_priv->sarea_priv->pipeA_y = y;
  1979. }
  1980. return 0;
  1981. }
  1982. static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
  1983. {
  1984. struct drm_device *dev = crtc->dev;
  1985. struct drm_i915_private *dev_priv = dev->dev_private;
  1986. u32 dpa_ctl;
  1987. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1988. dpa_ctl = I915_READ(DP_A);
  1989. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1990. if (clock < 200000) {
  1991. u32 temp;
  1992. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1993. /* workaround for 160Mhz:
  1994. 1) program 0x4600c bits 15:0 = 0x8124
  1995. 2) program 0x46010 bit 0 = 1
  1996. 3) program 0x46034 bit 24 = 1
  1997. 4) program 0x64000 bit 14 = 1
  1998. */
  1999. temp = I915_READ(0x4600c);
  2000. temp &= 0xffff0000;
  2001. I915_WRITE(0x4600c, temp | 0x8124);
  2002. temp = I915_READ(0x46010);
  2003. I915_WRITE(0x46010, temp | 1);
  2004. temp = I915_READ(0x46034);
  2005. I915_WRITE(0x46034, temp | (1 << 24));
  2006. } else {
  2007. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  2008. }
  2009. I915_WRITE(DP_A, dpa_ctl);
  2010. POSTING_READ(DP_A);
  2011. udelay(500);
  2012. }
  2013. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2014. {
  2015. struct drm_device *dev = crtc->dev;
  2016. struct drm_i915_private *dev_priv = dev->dev_private;
  2017. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2018. int pipe = intel_crtc->pipe;
  2019. u32 reg, temp;
  2020. /* enable normal train */
  2021. reg = FDI_TX_CTL(pipe);
  2022. temp = I915_READ(reg);
  2023. if (IS_IVYBRIDGE(dev)) {
  2024. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2025. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2026. } else {
  2027. temp &= ~FDI_LINK_TRAIN_NONE;
  2028. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2029. }
  2030. I915_WRITE(reg, temp);
  2031. reg = FDI_RX_CTL(pipe);
  2032. temp = I915_READ(reg);
  2033. if (HAS_PCH_CPT(dev)) {
  2034. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2035. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2036. } else {
  2037. temp &= ~FDI_LINK_TRAIN_NONE;
  2038. temp |= FDI_LINK_TRAIN_NONE;
  2039. }
  2040. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2041. /* wait one idle pattern time */
  2042. POSTING_READ(reg);
  2043. udelay(1000);
  2044. /* IVB wants error correction enabled */
  2045. if (IS_IVYBRIDGE(dev))
  2046. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2047. FDI_FE_ERRC_ENABLE);
  2048. }
  2049. static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
  2050. {
  2051. struct drm_i915_private *dev_priv = dev->dev_private;
  2052. u32 flags = I915_READ(SOUTH_CHICKEN1);
  2053. flags |= FDI_PHASE_SYNC_OVR(pipe);
  2054. I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
  2055. flags |= FDI_PHASE_SYNC_EN(pipe);
  2056. I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
  2057. POSTING_READ(SOUTH_CHICKEN1);
  2058. }
  2059. /* The FDI link training functions for ILK/Ibexpeak. */
  2060. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2061. {
  2062. struct drm_device *dev = crtc->dev;
  2063. struct drm_i915_private *dev_priv = dev->dev_private;
  2064. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2065. int pipe = intel_crtc->pipe;
  2066. int plane = intel_crtc->plane;
  2067. u32 reg, temp, tries;
  2068. /* FDI needs bits from pipe & plane first */
  2069. assert_pipe_enabled(dev_priv, pipe);
  2070. assert_plane_enabled(dev_priv, plane);
  2071. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2072. for train result */
  2073. reg = FDI_RX_IMR(pipe);
  2074. temp = I915_READ(reg);
  2075. temp &= ~FDI_RX_SYMBOL_LOCK;
  2076. temp &= ~FDI_RX_BIT_LOCK;
  2077. I915_WRITE(reg, temp);
  2078. I915_READ(reg);
  2079. udelay(150);
  2080. /* enable CPU FDI TX and PCH FDI RX */
  2081. reg = FDI_TX_CTL(pipe);
  2082. temp = I915_READ(reg);
  2083. temp &= ~(7 << 19);
  2084. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2085. temp &= ~FDI_LINK_TRAIN_NONE;
  2086. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2087. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2088. reg = FDI_RX_CTL(pipe);
  2089. temp = I915_READ(reg);
  2090. temp &= ~FDI_LINK_TRAIN_NONE;
  2091. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2092. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2093. POSTING_READ(reg);
  2094. udelay(150);
  2095. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2096. if (HAS_PCH_IBX(dev)) {
  2097. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2098. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2099. FDI_RX_PHASE_SYNC_POINTER_EN);
  2100. }
  2101. reg = FDI_RX_IIR(pipe);
  2102. for (tries = 0; tries < 5; tries++) {
  2103. temp = I915_READ(reg);
  2104. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2105. if ((temp & FDI_RX_BIT_LOCK)) {
  2106. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2107. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2108. break;
  2109. }
  2110. }
  2111. if (tries == 5)
  2112. DRM_ERROR("FDI train 1 fail!\n");
  2113. /* Train 2 */
  2114. reg = FDI_TX_CTL(pipe);
  2115. temp = I915_READ(reg);
  2116. temp &= ~FDI_LINK_TRAIN_NONE;
  2117. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2118. I915_WRITE(reg, temp);
  2119. reg = FDI_RX_CTL(pipe);
  2120. temp = I915_READ(reg);
  2121. temp &= ~FDI_LINK_TRAIN_NONE;
  2122. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2123. I915_WRITE(reg, temp);
  2124. POSTING_READ(reg);
  2125. udelay(150);
  2126. reg = FDI_RX_IIR(pipe);
  2127. for (tries = 0; tries < 5; tries++) {
  2128. temp = I915_READ(reg);
  2129. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2130. if (temp & FDI_RX_SYMBOL_LOCK) {
  2131. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2132. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2133. break;
  2134. }
  2135. }
  2136. if (tries == 5)
  2137. DRM_ERROR("FDI train 2 fail!\n");
  2138. DRM_DEBUG_KMS("FDI train done\n");
  2139. }
  2140. static const int snb_b_fdi_train_param[] = {
  2141. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2142. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2143. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2144. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2145. };
  2146. /* The FDI link training functions for SNB/Cougarpoint. */
  2147. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2148. {
  2149. struct drm_device *dev = crtc->dev;
  2150. struct drm_i915_private *dev_priv = dev->dev_private;
  2151. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2152. int pipe = intel_crtc->pipe;
  2153. u32 reg, temp, i;
  2154. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2155. for train result */
  2156. reg = FDI_RX_IMR(pipe);
  2157. temp = I915_READ(reg);
  2158. temp &= ~FDI_RX_SYMBOL_LOCK;
  2159. temp &= ~FDI_RX_BIT_LOCK;
  2160. I915_WRITE(reg, temp);
  2161. POSTING_READ(reg);
  2162. udelay(150);
  2163. /* enable CPU FDI TX and PCH FDI RX */
  2164. reg = FDI_TX_CTL(pipe);
  2165. temp = I915_READ(reg);
  2166. temp &= ~(7 << 19);
  2167. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2168. temp &= ~FDI_LINK_TRAIN_NONE;
  2169. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2170. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2171. /* SNB-B */
  2172. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2173. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2174. reg = FDI_RX_CTL(pipe);
  2175. temp = I915_READ(reg);
  2176. if (HAS_PCH_CPT(dev)) {
  2177. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2178. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2179. } else {
  2180. temp &= ~FDI_LINK_TRAIN_NONE;
  2181. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2182. }
  2183. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2184. POSTING_READ(reg);
  2185. udelay(150);
  2186. if (HAS_PCH_CPT(dev))
  2187. cpt_phase_pointer_enable(dev, pipe);
  2188. for (i = 0; i < 4; i++) {
  2189. reg = FDI_TX_CTL(pipe);
  2190. temp = I915_READ(reg);
  2191. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2192. temp |= snb_b_fdi_train_param[i];
  2193. I915_WRITE(reg, temp);
  2194. POSTING_READ(reg);
  2195. udelay(500);
  2196. reg = FDI_RX_IIR(pipe);
  2197. temp = I915_READ(reg);
  2198. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2199. if (temp & FDI_RX_BIT_LOCK) {
  2200. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2201. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2202. break;
  2203. }
  2204. }
  2205. if (i == 4)
  2206. DRM_ERROR("FDI train 1 fail!\n");
  2207. /* Train 2 */
  2208. reg = FDI_TX_CTL(pipe);
  2209. temp = I915_READ(reg);
  2210. temp &= ~FDI_LINK_TRAIN_NONE;
  2211. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2212. if (IS_GEN6(dev)) {
  2213. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2214. /* SNB-B */
  2215. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2216. }
  2217. I915_WRITE(reg, temp);
  2218. reg = FDI_RX_CTL(pipe);
  2219. temp = I915_READ(reg);
  2220. if (HAS_PCH_CPT(dev)) {
  2221. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2222. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2223. } else {
  2224. temp &= ~FDI_LINK_TRAIN_NONE;
  2225. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2226. }
  2227. I915_WRITE(reg, temp);
  2228. POSTING_READ(reg);
  2229. udelay(150);
  2230. for (i = 0; i < 4; i++) {
  2231. reg = FDI_TX_CTL(pipe);
  2232. temp = I915_READ(reg);
  2233. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2234. temp |= snb_b_fdi_train_param[i];
  2235. I915_WRITE(reg, temp);
  2236. POSTING_READ(reg);
  2237. udelay(500);
  2238. reg = FDI_RX_IIR(pipe);
  2239. temp = I915_READ(reg);
  2240. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2241. if (temp & FDI_RX_SYMBOL_LOCK) {
  2242. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2243. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2244. break;
  2245. }
  2246. }
  2247. if (i == 4)
  2248. DRM_ERROR("FDI train 2 fail!\n");
  2249. DRM_DEBUG_KMS("FDI train done.\n");
  2250. }
  2251. /* Manual link training for Ivy Bridge A0 parts */
  2252. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2253. {
  2254. struct drm_device *dev = crtc->dev;
  2255. struct drm_i915_private *dev_priv = dev->dev_private;
  2256. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2257. int pipe = intel_crtc->pipe;
  2258. u32 reg, temp, i;
  2259. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2260. for train result */
  2261. reg = FDI_RX_IMR(pipe);
  2262. temp = I915_READ(reg);
  2263. temp &= ~FDI_RX_SYMBOL_LOCK;
  2264. temp &= ~FDI_RX_BIT_LOCK;
  2265. I915_WRITE(reg, temp);
  2266. POSTING_READ(reg);
  2267. udelay(150);
  2268. /* enable CPU FDI TX and PCH FDI RX */
  2269. reg = FDI_TX_CTL(pipe);
  2270. temp = I915_READ(reg);
  2271. temp &= ~(7 << 19);
  2272. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2273. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2274. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2275. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2276. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2277. temp |= FDI_COMPOSITE_SYNC;
  2278. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2279. reg = FDI_RX_CTL(pipe);
  2280. temp = I915_READ(reg);
  2281. temp &= ~FDI_LINK_TRAIN_AUTO;
  2282. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2283. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2284. temp |= FDI_COMPOSITE_SYNC;
  2285. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2286. POSTING_READ(reg);
  2287. udelay(150);
  2288. if (HAS_PCH_CPT(dev))
  2289. cpt_phase_pointer_enable(dev, pipe);
  2290. for (i = 0; i < 4; i++) {
  2291. reg = FDI_TX_CTL(pipe);
  2292. temp = I915_READ(reg);
  2293. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2294. temp |= snb_b_fdi_train_param[i];
  2295. I915_WRITE(reg, temp);
  2296. POSTING_READ(reg);
  2297. udelay(500);
  2298. reg = FDI_RX_IIR(pipe);
  2299. temp = I915_READ(reg);
  2300. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2301. if (temp & FDI_RX_BIT_LOCK ||
  2302. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2303. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2304. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2305. break;
  2306. }
  2307. }
  2308. if (i == 4)
  2309. DRM_ERROR("FDI train 1 fail!\n");
  2310. /* Train 2 */
  2311. reg = FDI_TX_CTL(pipe);
  2312. temp = I915_READ(reg);
  2313. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2314. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2315. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2316. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2317. I915_WRITE(reg, temp);
  2318. reg = FDI_RX_CTL(pipe);
  2319. temp = I915_READ(reg);
  2320. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2321. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2322. I915_WRITE(reg, temp);
  2323. POSTING_READ(reg);
  2324. udelay(150);
  2325. for (i = 0; i < 4; i++) {
  2326. reg = FDI_TX_CTL(pipe);
  2327. temp = I915_READ(reg);
  2328. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2329. temp |= snb_b_fdi_train_param[i];
  2330. I915_WRITE(reg, temp);
  2331. POSTING_READ(reg);
  2332. udelay(500);
  2333. reg = FDI_RX_IIR(pipe);
  2334. temp = I915_READ(reg);
  2335. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2336. if (temp & FDI_RX_SYMBOL_LOCK) {
  2337. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2338. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2339. break;
  2340. }
  2341. }
  2342. if (i == 4)
  2343. DRM_ERROR("FDI train 2 fail!\n");
  2344. DRM_DEBUG_KMS("FDI train done.\n");
  2345. }
  2346. static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
  2347. {
  2348. struct drm_device *dev = crtc->dev;
  2349. struct drm_i915_private *dev_priv = dev->dev_private;
  2350. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2351. int pipe = intel_crtc->pipe;
  2352. u32 reg, temp;
  2353. /* Write the TU size bits so error detection works */
  2354. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2355. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2356. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2357. reg = FDI_RX_CTL(pipe);
  2358. temp = I915_READ(reg);
  2359. temp &= ~((0x7 << 19) | (0x7 << 16));
  2360. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2361. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2362. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2363. POSTING_READ(reg);
  2364. udelay(200);
  2365. /* Switch from Rawclk to PCDclk */
  2366. temp = I915_READ(reg);
  2367. I915_WRITE(reg, temp | FDI_PCDCLK);
  2368. POSTING_READ(reg);
  2369. udelay(200);
  2370. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2371. reg = FDI_TX_CTL(pipe);
  2372. temp = I915_READ(reg);
  2373. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2374. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2375. POSTING_READ(reg);
  2376. udelay(100);
  2377. }
  2378. }
  2379. static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
  2380. {
  2381. struct drm_i915_private *dev_priv = dev->dev_private;
  2382. u32 flags = I915_READ(SOUTH_CHICKEN1);
  2383. flags &= ~(FDI_PHASE_SYNC_EN(pipe));
  2384. I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
  2385. flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
  2386. I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
  2387. POSTING_READ(SOUTH_CHICKEN1);
  2388. }
  2389. static void ironlake_fdi_disable(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. u32 reg, temp;
  2396. /* disable CPU FDI tx and PCH FDI rx */
  2397. reg = FDI_TX_CTL(pipe);
  2398. temp = I915_READ(reg);
  2399. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2400. POSTING_READ(reg);
  2401. reg = FDI_RX_CTL(pipe);
  2402. temp = I915_READ(reg);
  2403. temp &= ~(0x7 << 16);
  2404. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2405. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2406. POSTING_READ(reg);
  2407. udelay(100);
  2408. /* Ironlake workaround, disable clock pointer after downing FDI */
  2409. if (HAS_PCH_IBX(dev)) {
  2410. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2411. I915_WRITE(FDI_RX_CHICKEN(pipe),
  2412. I915_READ(FDI_RX_CHICKEN(pipe) &
  2413. ~FDI_RX_PHASE_SYNC_POINTER_EN));
  2414. } else if (HAS_PCH_CPT(dev)) {
  2415. cpt_phase_pointer_disable(dev, pipe);
  2416. }
  2417. /* still set train pattern 1 */
  2418. reg = FDI_TX_CTL(pipe);
  2419. temp = I915_READ(reg);
  2420. temp &= ~FDI_LINK_TRAIN_NONE;
  2421. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2422. I915_WRITE(reg, temp);
  2423. reg = FDI_RX_CTL(pipe);
  2424. temp = I915_READ(reg);
  2425. if (HAS_PCH_CPT(dev)) {
  2426. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2427. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2428. } else {
  2429. temp &= ~FDI_LINK_TRAIN_NONE;
  2430. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2431. }
  2432. /* BPC in FDI rx is consistent with that in PIPECONF */
  2433. temp &= ~(0x07 << 16);
  2434. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2435. I915_WRITE(reg, temp);
  2436. POSTING_READ(reg);
  2437. udelay(100);
  2438. }
  2439. /*
  2440. * When we disable a pipe, we need to clear any pending scanline wait events
  2441. * to avoid hanging the ring, which we assume we are waiting on.
  2442. */
  2443. static void intel_clear_scanline_wait(struct drm_device *dev)
  2444. {
  2445. struct drm_i915_private *dev_priv = dev->dev_private;
  2446. struct intel_ring_buffer *ring;
  2447. u32 tmp;
  2448. if (IS_GEN2(dev))
  2449. /* Can't break the hang on i8xx */
  2450. return;
  2451. ring = LP_RING(dev_priv);
  2452. tmp = I915_READ_CTL(ring);
  2453. if (tmp & RING_WAIT)
  2454. I915_WRITE_CTL(ring, tmp);
  2455. }
  2456. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2457. {
  2458. struct drm_i915_gem_object *obj;
  2459. struct drm_i915_private *dev_priv;
  2460. if (crtc->fb == NULL)
  2461. return;
  2462. obj = to_intel_framebuffer(crtc->fb)->obj;
  2463. dev_priv = crtc->dev->dev_private;
  2464. wait_event(dev_priv->pending_flip_queue,
  2465. atomic_read(&obj->pending_flip) == 0);
  2466. }
  2467. static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
  2468. {
  2469. struct drm_device *dev = crtc->dev;
  2470. struct drm_mode_config *mode_config = &dev->mode_config;
  2471. struct intel_encoder *encoder;
  2472. /*
  2473. * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
  2474. * must be driven by its own crtc; no sharing is possible.
  2475. */
  2476. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  2477. if (encoder->base.crtc != crtc)
  2478. continue;
  2479. switch (encoder->type) {
  2480. case INTEL_OUTPUT_EDP:
  2481. if (!intel_encoder_is_pch_edp(&encoder->base))
  2482. return false;
  2483. continue;
  2484. }
  2485. }
  2486. return true;
  2487. }
  2488. /*
  2489. * Enable PCH resources required for PCH ports:
  2490. * - PCH PLLs
  2491. * - FDI training & RX/TX
  2492. * - update transcoder timings
  2493. * - DP transcoding bits
  2494. * - transcoder
  2495. */
  2496. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2497. {
  2498. struct drm_device *dev = crtc->dev;
  2499. struct drm_i915_private *dev_priv = dev->dev_private;
  2500. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2501. int pipe = intel_crtc->pipe;
  2502. u32 reg, temp;
  2503. /* For PCH output, training FDI link */
  2504. dev_priv->display.fdi_link_train(crtc);
  2505. intel_enable_pch_pll(dev_priv, pipe);
  2506. if (HAS_PCH_CPT(dev)) {
  2507. /* Be sure PCH DPLL SEL is set */
  2508. temp = I915_READ(PCH_DPLL_SEL);
  2509. if (pipe == 0 && (temp & TRANSA_DPLL_ENABLE) == 0)
  2510. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  2511. else if (pipe == 1 && (temp & TRANSB_DPLL_ENABLE) == 0)
  2512. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2513. else if (pipe == 2 && (temp & TRANSC_DPLL_ENABLE) == 0)
  2514. temp |= (TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
  2515. I915_WRITE(PCH_DPLL_SEL, temp);
  2516. }
  2517. /* set transcoder timing, panel must allow it */
  2518. assert_panel_unlocked(dev_priv, pipe);
  2519. I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
  2520. I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
  2521. I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
  2522. I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
  2523. I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
  2524. I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
  2525. intel_fdi_normal_train(crtc);
  2526. /* For PCH DP, enable TRANS_DP_CTL */
  2527. if (HAS_PCH_CPT(dev) &&
  2528. intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  2529. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
  2530. reg = TRANS_DP_CTL(pipe);
  2531. temp = I915_READ(reg);
  2532. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  2533. TRANS_DP_SYNC_MASK |
  2534. TRANS_DP_BPC_MASK);
  2535. temp |= (TRANS_DP_OUTPUT_ENABLE |
  2536. TRANS_DP_ENH_FRAMING);
  2537. temp |= bpc << 9; /* same format but at 11:9 */
  2538. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  2539. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  2540. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  2541. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  2542. switch (intel_trans_dp_port_sel(crtc)) {
  2543. case PCH_DP_B:
  2544. temp |= TRANS_DP_PORT_SEL_B;
  2545. break;
  2546. case PCH_DP_C:
  2547. temp |= TRANS_DP_PORT_SEL_C;
  2548. break;
  2549. case PCH_DP_D:
  2550. temp |= TRANS_DP_PORT_SEL_D;
  2551. break;
  2552. default:
  2553. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  2554. temp |= TRANS_DP_PORT_SEL_B;
  2555. break;
  2556. }
  2557. I915_WRITE(reg, temp);
  2558. }
  2559. intel_enable_transcoder(dev_priv, pipe);
  2560. }
  2561. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  2562. {
  2563. struct drm_device *dev = crtc->dev;
  2564. struct drm_i915_private *dev_priv = dev->dev_private;
  2565. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2566. int pipe = intel_crtc->pipe;
  2567. int plane = intel_crtc->plane;
  2568. u32 temp;
  2569. bool is_pch_port;
  2570. if (intel_crtc->active)
  2571. return;
  2572. intel_crtc->active = true;
  2573. intel_update_watermarks(dev);
  2574. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  2575. temp = I915_READ(PCH_LVDS);
  2576. if ((temp & LVDS_PORT_EN) == 0)
  2577. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  2578. }
  2579. is_pch_port = intel_crtc_driving_pch(crtc);
  2580. if (is_pch_port)
  2581. ironlake_fdi_pll_enable(crtc);
  2582. else
  2583. ironlake_fdi_disable(crtc);
  2584. /* Enable panel fitting for LVDS */
  2585. if (dev_priv->pch_pf_size &&
  2586. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
  2587. /* Force use of hard-coded filter coefficients
  2588. * as some pre-programmed values are broken,
  2589. * e.g. x201.
  2590. */
  2591. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  2592. I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
  2593. I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
  2594. }
  2595. /*
  2596. * On ILK+ LUT must be loaded before the pipe is running but with
  2597. * clocks enabled
  2598. */
  2599. intel_crtc_load_lut(crtc);
  2600. intel_enable_pipe(dev_priv, pipe, is_pch_port);
  2601. intel_enable_plane(dev_priv, plane, pipe);
  2602. if (is_pch_port)
  2603. ironlake_pch_enable(crtc);
  2604. mutex_lock(&dev->struct_mutex);
  2605. intel_update_fbc(dev);
  2606. mutex_unlock(&dev->struct_mutex);
  2607. intel_crtc_update_cursor(crtc, true);
  2608. }
  2609. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  2610. {
  2611. struct drm_device *dev = crtc->dev;
  2612. struct drm_i915_private *dev_priv = dev->dev_private;
  2613. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2614. int pipe = intel_crtc->pipe;
  2615. int plane = intel_crtc->plane;
  2616. u32 reg, temp;
  2617. if (!intel_crtc->active)
  2618. return;
  2619. intel_crtc_wait_for_pending_flips(crtc);
  2620. drm_vblank_off(dev, pipe);
  2621. intel_crtc_update_cursor(crtc, false);
  2622. intel_disable_plane(dev_priv, plane, pipe);
  2623. if (dev_priv->cfb_plane == plane)
  2624. intel_disable_fbc(dev);
  2625. intel_disable_pipe(dev_priv, pipe);
  2626. /* Disable PF */
  2627. I915_WRITE(PF_CTL(pipe), 0);
  2628. I915_WRITE(PF_WIN_SZ(pipe), 0);
  2629. ironlake_fdi_disable(crtc);
  2630. /* This is a horrible layering violation; we should be doing this in
  2631. * the connector/encoder ->prepare instead, but we don't always have
  2632. * enough information there about the config to know whether it will
  2633. * actually be necessary or just cause undesired flicker.
  2634. */
  2635. intel_disable_pch_ports(dev_priv, pipe);
  2636. intel_disable_transcoder(dev_priv, pipe);
  2637. if (HAS_PCH_CPT(dev)) {
  2638. /* disable TRANS_DP_CTL */
  2639. reg = TRANS_DP_CTL(pipe);
  2640. temp = I915_READ(reg);
  2641. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  2642. temp |= TRANS_DP_PORT_SEL_NONE;
  2643. I915_WRITE(reg, temp);
  2644. /* disable DPLL_SEL */
  2645. temp = I915_READ(PCH_DPLL_SEL);
  2646. switch (pipe) {
  2647. case 0:
  2648. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  2649. break;
  2650. case 1:
  2651. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2652. break;
  2653. case 2:
  2654. /* FIXME: manage transcoder PLLs? */
  2655. temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
  2656. break;
  2657. default:
  2658. BUG(); /* wtf */
  2659. }
  2660. I915_WRITE(PCH_DPLL_SEL, temp);
  2661. }
  2662. /* disable PCH DPLL */
  2663. intel_disable_pch_pll(dev_priv, pipe);
  2664. /* Switch from PCDclk to Rawclk */
  2665. reg = FDI_RX_CTL(pipe);
  2666. temp = I915_READ(reg);
  2667. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2668. /* Disable CPU FDI TX PLL */
  2669. reg = FDI_TX_CTL(pipe);
  2670. temp = I915_READ(reg);
  2671. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2672. POSTING_READ(reg);
  2673. udelay(100);
  2674. reg = FDI_RX_CTL(pipe);
  2675. temp = I915_READ(reg);
  2676. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2677. /* Wait for the clocks to turn off. */
  2678. POSTING_READ(reg);
  2679. udelay(100);
  2680. intel_crtc->active = false;
  2681. intel_update_watermarks(dev);
  2682. mutex_lock(&dev->struct_mutex);
  2683. intel_update_fbc(dev);
  2684. intel_clear_scanline_wait(dev);
  2685. mutex_unlock(&dev->struct_mutex);
  2686. }
  2687. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  2688. {
  2689. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2690. int pipe = intel_crtc->pipe;
  2691. int plane = intel_crtc->plane;
  2692. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2693. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2694. */
  2695. switch (mode) {
  2696. case DRM_MODE_DPMS_ON:
  2697. case DRM_MODE_DPMS_STANDBY:
  2698. case DRM_MODE_DPMS_SUSPEND:
  2699. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  2700. ironlake_crtc_enable(crtc);
  2701. break;
  2702. case DRM_MODE_DPMS_OFF:
  2703. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  2704. ironlake_crtc_disable(crtc);
  2705. break;
  2706. }
  2707. }
  2708. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2709. {
  2710. if (!enable && intel_crtc->overlay) {
  2711. struct drm_device *dev = intel_crtc->base.dev;
  2712. struct drm_i915_private *dev_priv = dev->dev_private;
  2713. mutex_lock(&dev->struct_mutex);
  2714. dev_priv->mm.interruptible = false;
  2715. (void) intel_overlay_switch_off(intel_crtc->overlay);
  2716. dev_priv->mm.interruptible = true;
  2717. mutex_unlock(&dev->struct_mutex);
  2718. }
  2719. /* Let userspace switch the overlay on again. In most cases userspace
  2720. * has to recompute where to put it anyway.
  2721. */
  2722. }
  2723. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  2724. {
  2725. struct drm_device *dev = crtc->dev;
  2726. struct drm_i915_private *dev_priv = dev->dev_private;
  2727. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2728. int pipe = intel_crtc->pipe;
  2729. int plane = intel_crtc->plane;
  2730. if (intel_crtc->active)
  2731. return;
  2732. intel_crtc->active = true;
  2733. intel_update_watermarks(dev);
  2734. intel_enable_pll(dev_priv, pipe);
  2735. intel_enable_pipe(dev_priv, pipe, false);
  2736. intel_enable_plane(dev_priv, plane, pipe);
  2737. intel_crtc_load_lut(crtc);
  2738. intel_update_fbc(dev);
  2739. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2740. intel_crtc_dpms_overlay(intel_crtc, true);
  2741. intel_crtc_update_cursor(crtc, true);
  2742. }
  2743. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  2744. {
  2745. struct drm_device *dev = crtc->dev;
  2746. struct drm_i915_private *dev_priv = dev->dev_private;
  2747. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2748. int pipe = intel_crtc->pipe;
  2749. int plane = intel_crtc->plane;
  2750. if (!intel_crtc->active)
  2751. return;
  2752. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2753. intel_crtc_wait_for_pending_flips(crtc);
  2754. drm_vblank_off(dev, pipe);
  2755. intel_crtc_dpms_overlay(intel_crtc, false);
  2756. intel_crtc_update_cursor(crtc, false);
  2757. if (dev_priv->cfb_plane == plane)
  2758. intel_disable_fbc(dev);
  2759. intel_disable_plane(dev_priv, plane, pipe);
  2760. intel_disable_pipe(dev_priv, pipe);
  2761. intel_disable_pll(dev_priv, pipe);
  2762. intel_crtc->active = false;
  2763. intel_update_fbc(dev);
  2764. intel_update_watermarks(dev);
  2765. intel_clear_scanline_wait(dev);
  2766. }
  2767. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2768. {
  2769. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2770. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2771. */
  2772. switch (mode) {
  2773. case DRM_MODE_DPMS_ON:
  2774. case DRM_MODE_DPMS_STANDBY:
  2775. case DRM_MODE_DPMS_SUSPEND:
  2776. i9xx_crtc_enable(crtc);
  2777. break;
  2778. case DRM_MODE_DPMS_OFF:
  2779. i9xx_crtc_disable(crtc);
  2780. break;
  2781. }
  2782. }
  2783. /**
  2784. * Sets the power management mode of the pipe and plane.
  2785. */
  2786. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2787. {
  2788. struct drm_device *dev = crtc->dev;
  2789. struct drm_i915_private *dev_priv = dev->dev_private;
  2790. struct drm_i915_master_private *master_priv;
  2791. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2792. int pipe = intel_crtc->pipe;
  2793. bool enabled;
  2794. if (intel_crtc->dpms_mode == mode)
  2795. return;
  2796. intel_crtc->dpms_mode = mode;
  2797. dev_priv->display.dpms(crtc, mode);
  2798. if (!dev->primary->master)
  2799. return;
  2800. master_priv = dev->primary->master->driver_priv;
  2801. if (!master_priv->sarea_priv)
  2802. return;
  2803. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2804. switch (pipe) {
  2805. case 0:
  2806. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2807. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2808. break;
  2809. case 1:
  2810. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2811. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2812. break;
  2813. default:
  2814. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  2815. break;
  2816. }
  2817. }
  2818. static void intel_crtc_disable(struct drm_crtc *crtc)
  2819. {
  2820. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2821. struct drm_device *dev = crtc->dev;
  2822. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  2823. if (crtc->fb) {
  2824. mutex_lock(&dev->struct_mutex);
  2825. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  2826. mutex_unlock(&dev->struct_mutex);
  2827. }
  2828. }
  2829. /* Prepare for a mode set.
  2830. *
  2831. * Note we could be a lot smarter here. We need to figure out which outputs
  2832. * will be enabled, which disabled (in short, how the config will changes)
  2833. * and perform the minimum necessary steps to accomplish that, e.g. updating
  2834. * watermarks, FBC configuration, making sure PLLs are programmed correctly,
  2835. * panel fitting is in the proper state, etc.
  2836. */
  2837. static void i9xx_crtc_prepare(struct drm_crtc *crtc)
  2838. {
  2839. i9xx_crtc_disable(crtc);
  2840. }
  2841. static void i9xx_crtc_commit(struct drm_crtc *crtc)
  2842. {
  2843. i9xx_crtc_enable(crtc);
  2844. }
  2845. static void ironlake_crtc_prepare(struct drm_crtc *crtc)
  2846. {
  2847. ironlake_crtc_disable(crtc);
  2848. }
  2849. static void ironlake_crtc_commit(struct drm_crtc *crtc)
  2850. {
  2851. ironlake_crtc_enable(crtc);
  2852. }
  2853. void intel_encoder_prepare(struct drm_encoder *encoder)
  2854. {
  2855. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2856. /* lvds has its own version of prepare see intel_lvds_prepare */
  2857. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2858. }
  2859. void intel_encoder_commit(struct drm_encoder *encoder)
  2860. {
  2861. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2862. /* lvds has its own version of commit see intel_lvds_commit */
  2863. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2864. }
  2865. void intel_encoder_destroy(struct drm_encoder *encoder)
  2866. {
  2867. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2868. drm_encoder_cleanup(encoder);
  2869. kfree(intel_encoder);
  2870. }
  2871. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2872. struct drm_display_mode *mode,
  2873. struct drm_display_mode *adjusted_mode)
  2874. {
  2875. struct drm_device *dev = crtc->dev;
  2876. if (HAS_PCH_SPLIT(dev)) {
  2877. /* FDI link clock is fixed at 2.7G */
  2878. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  2879. return false;
  2880. }
  2881. /* XXX some encoders set the crtcinfo, others don't.
  2882. * Obviously we need some form of conflict resolution here...
  2883. */
  2884. if (adjusted_mode->crtc_htotal == 0)
  2885. drm_mode_set_crtcinfo(adjusted_mode, 0);
  2886. return true;
  2887. }
  2888. static int i945_get_display_clock_speed(struct drm_device *dev)
  2889. {
  2890. return 400000;
  2891. }
  2892. static int i915_get_display_clock_speed(struct drm_device *dev)
  2893. {
  2894. return 333000;
  2895. }
  2896. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2897. {
  2898. return 200000;
  2899. }
  2900. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2901. {
  2902. u16 gcfgc = 0;
  2903. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2904. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2905. return 133000;
  2906. else {
  2907. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2908. case GC_DISPLAY_CLOCK_333_MHZ:
  2909. return 333000;
  2910. default:
  2911. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2912. return 190000;
  2913. }
  2914. }
  2915. }
  2916. static int i865_get_display_clock_speed(struct drm_device *dev)
  2917. {
  2918. return 266000;
  2919. }
  2920. static int i855_get_display_clock_speed(struct drm_device *dev)
  2921. {
  2922. u16 hpllcc = 0;
  2923. /* Assume that the hardware is in the high speed state. This
  2924. * should be the default.
  2925. */
  2926. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2927. case GC_CLOCK_133_200:
  2928. case GC_CLOCK_100_200:
  2929. return 200000;
  2930. case GC_CLOCK_166_250:
  2931. return 250000;
  2932. case GC_CLOCK_100_133:
  2933. return 133000;
  2934. }
  2935. /* Shouldn't happen */
  2936. return 0;
  2937. }
  2938. static int i830_get_display_clock_speed(struct drm_device *dev)
  2939. {
  2940. return 133000;
  2941. }
  2942. struct fdi_m_n {
  2943. u32 tu;
  2944. u32 gmch_m;
  2945. u32 gmch_n;
  2946. u32 link_m;
  2947. u32 link_n;
  2948. };
  2949. static void
  2950. fdi_reduce_ratio(u32 *num, u32 *den)
  2951. {
  2952. while (*num > 0xffffff || *den > 0xffffff) {
  2953. *num >>= 1;
  2954. *den >>= 1;
  2955. }
  2956. }
  2957. static void
  2958. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  2959. int link_clock, struct fdi_m_n *m_n)
  2960. {
  2961. m_n->tu = 64; /* default size */
  2962. /* BUG_ON(pixel_clock > INT_MAX / 36); */
  2963. m_n->gmch_m = bits_per_pixel * pixel_clock;
  2964. m_n->gmch_n = link_clock * nlanes * 8;
  2965. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  2966. m_n->link_m = pixel_clock;
  2967. m_n->link_n = link_clock;
  2968. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  2969. }
  2970. struct intel_watermark_params {
  2971. unsigned long fifo_size;
  2972. unsigned long max_wm;
  2973. unsigned long default_wm;
  2974. unsigned long guard_size;
  2975. unsigned long cacheline_size;
  2976. };
  2977. /* Pineview has different values for various configs */
  2978. static const struct intel_watermark_params pineview_display_wm = {
  2979. PINEVIEW_DISPLAY_FIFO,
  2980. PINEVIEW_MAX_WM,
  2981. PINEVIEW_DFT_WM,
  2982. PINEVIEW_GUARD_WM,
  2983. PINEVIEW_FIFO_LINE_SIZE
  2984. };
  2985. static const struct intel_watermark_params pineview_display_hplloff_wm = {
  2986. PINEVIEW_DISPLAY_FIFO,
  2987. PINEVIEW_MAX_WM,
  2988. PINEVIEW_DFT_HPLLOFF_WM,
  2989. PINEVIEW_GUARD_WM,
  2990. PINEVIEW_FIFO_LINE_SIZE
  2991. };
  2992. static const struct intel_watermark_params pineview_cursor_wm = {
  2993. PINEVIEW_CURSOR_FIFO,
  2994. PINEVIEW_CURSOR_MAX_WM,
  2995. PINEVIEW_CURSOR_DFT_WM,
  2996. PINEVIEW_CURSOR_GUARD_WM,
  2997. PINEVIEW_FIFO_LINE_SIZE,
  2998. };
  2999. static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
  3000. PINEVIEW_CURSOR_FIFO,
  3001. PINEVIEW_CURSOR_MAX_WM,
  3002. PINEVIEW_CURSOR_DFT_WM,
  3003. PINEVIEW_CURSOR_GUARD_WM,
  3004. PINEVIEW_FIFO_LINE_SIZE
  3005. };
  3006. static const struct intel_watermark_params g4x_wm_info = {
  3007. G4X_FIFO_SIZE,
  3008. G4X_MAX_WM,
  3009. G4X_MAX_WM,
  3010. 2,
  3011. G4X_FIFO_LINE_SIZE,
  3012. };
  3013. static const struct intel_watermark_params g4x_cursor_wm_info = {
  3014. I965_CURSOR_FIFO,
  3015. I965_CURSOR_MAX_WM,
  3016. I965_CURSOR_DFT_WM,
  3017. 2,
  3018. G4X_FIFO_LINE_SIZE,
  3019. };
  3020. static const struct intel_watermark_params i965_cursor_wm_info = {
  3021. I965_CURSOR_FIFO,
  3022. I965_CURSOR_MAX_WM,
  3023. I965_CURSOR_DFT_WM,
  3024. 2,
  3025. I915_FIFO_LINE_SIZE,
  3026. };
  3027. static const struct intel_watermark_params i945_wm_info = {
  3028. I945_FIFO_SIZE,
  3029. I915_MAX_WM,
  3030. 1,
  3031. 2,
  3032. I915_FIFO_LINE_SIZE
  3033. };
  3034. static const struct intel_watermark_params i915_wm_info = {
  3035. I915_FIFO_SIZE,
  3036. I915_MAX_WM,
  3037. 1,
  3038. 2,
  3039. I915_FIFO_LINE_SIZE
  3040. };
  3041. static const struct intel_watermark_params i855_wm_info = {
  3042. I855GM_FIFO_SIZE,
  3043. I915_MAX_WM,
  3044. 1,
  3045. 2,
  3046. I830_FIFO_LINE_SIZE
  3047. };
  3048. static const struct intel_watermark_params i830_wm_info = {
  3049. I830_FIFO_SIZE,
  3050. I915_MAX_WM,
  3051. 1,
  3052. 2,
  3053. I830_FIFO_LINE_SIZE
  3054. };
  3055. static const struct intel_watermark_params ironlake_display_wm_info = {
  3056. ILK_DISPLAY_FIFO,
  3057. ILK_DISPLAY_MAXWM,
  3058. ILK_DISPLAY_DFTWM,
  3059. 2,
  3060. ILK_FIFO_LINE_SIZE
  3061. };
  3062. static const struct intel_watermark_params ironlake_cursor_wm_info = {
  3063. ILK_CURSOR_FIFO,
  3064. ILK_CURSOR_MAXWM,
  3065. ILK_CURSOR_DFTWM,
  3066. 2,
  3067. ILK_FIFO_LINE_SIZE
  3068. };
  3069. static const struct intel_watermark_params ironlake_display_srwm_info = {
  3070. ILK_DISPLAY_SR_FIFO,
  3071. ILK_DISPLAY_MAX_SRWM,
  3072. ILK_DISPLAY_DFT_SRWM,
  3073. 2,
  3074. ILK_FIFO_LINE_SIZE
  3075. };
  3076. static const struct intel_watermark_params ironlake_cursor_srwm_info = {
  3077. ILK_CURSOR_SR_FIFO,
  3078. ILK_CURSOR_MAX_SRWM,
  3079. ILK_CURSOR_DFT_SRWM,
  3080. 2,
  3081. ILK_FIFO_LINE_SIZE
  3082. };
  3083. static const struct intel_watermark_params sandybridge_display_wm_info = {
  3084. SNB_DISPLAY_FIFO,
  3085. SNB_DISPLAY_MAXWM,
  3086. SNB_DISPLAY_DFTWM,
  3087. 2,
  3088. SNB_FIFO_LINE_SIZE
  3089. };
  3090. static const struct intel_watermark_params sandybridge_cursor_wm_info = {
  3091. SNB_CURSOR_FIFO,
  3092. SNB_CURSOR_MAXWM,
  3093. SNB_CURSOR_DFTWM,
  3094. 2,
  3095. SNB_FIFO_LINE_SIZE
  3096. };
  3097. static const struct intel_watermark_params sandybridge_display_srwm_info = {
  3098. SNB_DISPLAY_SR_FIFO,
  3099. SNB_DISPLAY_MAX_SRWM,
  3100. SNB_DISPLAY_DFT_SRWM,
  3101. 2,
  3102. SNB_FIFO_LINE_SIZE
  3103. };
  3104. static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
  3105. SNB_CURSOR_SR_FIFO,
  3106. SNB_CURSOR_MAX_SRWM,
  3107. SNB_CURSOR_DFT_SRWM,
  3108. 2,
  3109. SNB_FIFO_LINE_SIZE
  3110. };
  3111. /**
  3112. * intel_calculate_wm - calculate watermark level
  3113. * @clock_in_khz: pixel clock
  3114. * @wm: chip FIFO params
  3115. * @pixel_size: display pixel size
  3116. * @latency_ns: memory latency for the platform
  3117. *
  3118. * Calculate the watermark level (the level at which the display plane will
  3119. * start fetching from memory again). Each chip has a different display
  3120. * FIFO size and allocation, so the caller needs to figure that out and pass
  3121. * in the correct intel_watermark_params structure.
  3122. *
  3123. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  3124. * on the pixel size. When it reaches the watermark level, it'll start
  3125. * fetching FIFO line sized based chunks from memory until the FIFO fills
  3126. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  3127. * will occur, and a display engine hang could result.
  3128. */
  3129. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  3130. const struct intel_watermark_params *wm,
  3131. int fifo_size,
  3132. int pixel_size,
  3133. unsigned long latency_ns)
  3134. {
  3135. long entries_required, wm_size;
  3136. /*
  3137. * Note: we need to make sure we don't overflow for various clock &
  3138. * latency values.
  3139. * clocks go from a few thousand to several hundred thousand.
  3140. * latency is usually a few thousand
  3141. */
  3142. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  3143. 1000;
  3144. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  3145. DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
  3146. wm_size = fifo_size - (entries_required + wm->guard_size);
  3147. DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
  3148. /* Don't promote wm_size to unsigned... */
  3149. if (wm_size > (long)wm->max_wm)
  3150. wm_size = wm->max_wm;
  3151. if (wm_size <= 0)
  3152. wm_size = wm->default_wm;
  3153. return wm_size;
  3154. }
  3155. struct cxsr_latency {
  3156. int is_desktop;
  3157. int is_ddr3;
  3158. unsigned long fsb_freq;
  3159. unsigned long mem_freq;
  3160. unsigned long display_sr;
  3161. unsigned long display_hpll_disable;
  3162. unsigned long cursor_sr;
  3163. unsigned long cursor_hpll_disable;
  3164. };
  3165. static const struct cxsr_latency cxsr_latency_table[] = {
  3166. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  3167. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  3168. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  3169. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  3170. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  3171. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  3172. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  3173. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  3174. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  3175. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  3176. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  3177. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  3178. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  3179. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  3180. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  3181. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  3182. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  3183. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  3184. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  3185. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  3186. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  3187. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  3188. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  3189. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  3190. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  3191. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  3192. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  3193. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  3194. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  3195. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  3196. };
  3197. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  3198. int is_ddr3,
  3199. int fsb,
  3200. int mem)
  3201. {
  3202. const struct cxsr_latency *latency;
  3203. int i;
  3204. if (fsb == 0 || mem == 0)
  3205. return NULL;
  3206. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  3207. latency = &cxsr_latency_table[i];
  3208. if (is_desktop == latency->is_desktop &&
  3209. is_ddr3 == latency->is_ddr3 &&
  3210. fsb == latency->fsb_freq && mem == latency->mem_freq)
  3211. return latency;
  3212. }
  3213. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  3214. return NULL;
  3215. }
  3216. static void pineview_disable_cxsr(struct drm_device *dev)
  3217. {
  3218. struct drm_i915_private *dev_priv = dev->dev_private;
  3219. /* deactivate cxsr */
  3220. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  3221. }
  3222. /*
  3223. * Latency for FIFO fetches is dependent on several factors:
  3224. * - memory configuration (speed, channels)
  3225. * - chipset
  3226. * - current MCH state
  3227. * It can be fairly high in some situations, so here we assume a fairly
  3228. * pessimal value. It's a tradeoff between extra memory fetches (if we
  3229. * set this value too high, the FIFO will fetch frequently to stay full)
  3230. * and power consumption (set it too low to save power and we might see
  3231. * FIFO underruns and display "flicker").
  3232. *
  3233. * A value of 5us seems to be a good balance; safe for very low end
  3234. * platforms but not overly aggressive on lower latency configs.
  3235. */
  3236. static const int latency_ns = 5000;
  3237. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  3238. {
  3239. struct drm_i915_private *dev_priv = dev->dev_private;
  3240. uint32_t dsparb = I915_READ(DSPARB);
  3241. int size;
  3242. size = dsparb & 0x7f;
  3243. if (plane)
  3244. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  3245. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3246. plane ? "B" : "A", size);
  3247. return size;
  3248. }
  3249. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  3250. {
  3251. struct drm_i915_private *dev_priv = dev->dev_private;
  3252. uint32_t dsparb = I915_READ(DSPARB);
  3253. int size;
  3254. size = dsparb & 0x1ff;
  3255. if (plane)
  3256. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  3257. size >>= 1; /* Convert to cachelines */
  3258. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3259. plane ? "B" : "A", size);
  3260. return size;
  3261. }
  3262. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  3263. {
  3264. struct drm_i915_private *dev_priv = dev->dev_private;
  3265. uint32_t dsparb = I915_READ(DSPARB);
  3266. int size;
  3267. size = dsparb & 0x7f;
  3268. size >>= 2; /* Convert to cachelines */
  3269. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3270. plane ? "B" : "A",
  3271. size);
  3272. return size;
  3273. }
  3274. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  3275. {
  3276. struct drm_i915_private *dev_priv = dev->dev_private;
  3277. uint32_t dsparb = I915_READ(DSPARB);
  3278. int size;
  3279. size = dsparb & 0x7f;
  3280. size >>= 1; /* Convert to cachelines */
  3281. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3282. plane ? "B" : "A", size);
  3283. return size;
  3284. }
  3285. static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
  3286. {
  3287. struct drm_crtc *crtc, *enabled = NULL;
  3288. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3289. if (crtc->enabled && crtc->fb) {
  3290. if (enabled)
  3291. return NULL;
  3292. enabled = crtc;
  3293. }
  3294. }
  3295. return enabled;
  3296. }
  3297. static void pineview_update_wm(struct drm_device *dev)
  3298. {
  3299. struct drm_i915_private *dev_priv = dev->dev_private;
  3300. struct drm_crtc *crtc;
  3301. const struct cxsr_latency *latency;
  3302. u32 reg;
  3303. unsigned long wm;
  3304. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  3305. dev_priv->fsb_freq, dev_priv->mem_freq);
  3306. if (!latency) {
  3307. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  3308. pineview_disable_cxsr(dev);
  3309. return;
  3310. }
  3311. crtc = single_enabled_crtc(dev);
  3312. if (crtc) {
  3313. int clock = crtc->mode.clock;
  3314. int pixel_size = crtc->fb->bits_per_pixel / 8;
  3315. /* Display SR */
  3316. wm = intel_calculate_wm(clock, &pineview_display_wm,
  3317. pineview_display_wm.fifo_size,
  3318. pixel_size, latency->display_sr);
  3319. reg = I915_READ(DSPFW1);
  3320. reg &= ~DSPFW_SR_MASK;
  3321. reg |= wm << DSPFW_SR_SHIFT;
  3322. I915_WRITE(DSPFW1, reg);
  3323. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  3324. /* cursor SR */
  3325. wm = intel_calculate_wm(clock, &pineview_cursor_wm,
  3326. pineview_display_wm.fifo_size,
  3327. pixel_size, latency->cursor_sr);
  3328. reg = I915_READ(DSPFW3);
  3329. reg &= ~DSPFW_CURSOR_SR_MASK;
  3330. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  3331. I915_WRITE(DSPFW3, reg);
  3332. /* Display HPLL off SR */
  3333. wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
  3334. pineview_display_hplloff_wm.fifo_size,
  3335. pixel_size, latency->display_hpll_disable);
  3336. reg = I915_READ(DSPFW3);
  3337. reg &= ~DSPFW_HPLL_SR_MASK;
  3338. reg |= wm & DSPFW_HPLL_SR_MASK;
  3339. I915_WRITE(DSPFW3, reg);
  3340. /* cursor HPLL off SR */
  3341. wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
  3342. pineview_display_hplloff_wm.fifo_size,
  3343. pixel_size, latency->cursor_hpll_disable);
  3344. reg = I915_READ(DSPFW3);
  3345. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  3346. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  3347. I915_WRITE(DSPFW3, reg);
  3348. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  3349. /* activate cxsr */
  3350. I915_WRITE(DSPFW3,
  3351. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  3352. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  3353. } else {
  3354. pineview_disable_cxsr(dev);
  3355. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  3356. }
  3357. }
  3358. static bool g4x_compute_wm0(struct drm_device *dev,
  3359. int plane,
  3360. const struct intel_watermark_params *display,
  3361. int display_latency_ns,
  3362. const struct intel_watermark_params *cursor,
  3363. int cursor_latency_ns,
  3364. int *plane_wm,
  3365. int *cursor_wm)
  3366. {
  3367. struct drm_crtc *crtc;
  3368. int htotal, hdisplay, clock, pixel_size;
  3369. int line_time_us, line_count;
  3370. int entries, tlb_miss;
  3371. crtc = intel_get_crtc_for_plane(dev, plane);
  3372. if (crtc->fb == NULL || !crtc->enabled) {
  3373. *cursor_wm = cursor->guard_size;
  3374. *plane_wm = display->guard_size;
  3375. return false;
  3376. }
  3377. htotal = crtc->mode.htotal;
  3378. hdisplay = crtc->mode.hdisplay;
  3379. clock = crtc->mode.clock;
  3380. pixel_size = crtc->fb->bits_per_pixel / 8;
  3381. /* Use the small buffer method to calculate plane watermark */
  3382. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  3383. tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
  3384. if (tlb_miss > 0)
  3385. entries += tlb_miss;
  3386. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  3387. *plane_wm = entries + display->guard_size;
  3388. if (*plane_wm > (int)display->max_wm)
  3389. *plane_wm = display->max_wm;
  3390. /* Use the large buffer method to calculate cursor watermark */
  3391. line_time_us = ((htotal * 1000) / clock);
  3392. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  3393. entries = line_count * 64 * pixel_size;
  3394. tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
  3395. if (tlb_miss > 0)
  3396. entries += tlb_miss;
  3397. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3398. *cursor_wm = entries + cursor->guard_size;
  3399. if (*cursor_wm > (int)cursor->max_wm)
  3400. *cursor_wm = (int)cursor->max_wm;
  3401. return true;
  3402. }
  3403. /*
  3404. * Check the wm result.
  3405. *
  3406. * If any calculated watermark values is larger than the maximum value that
  3407. * can be programmed into the associated watermark register, that watermark
  3408. * must be disabled.
  3409. */
  3410. static bool g4x_check_srwm(struct drm_device *dev,
  3411. int display_wm, int cursor_wm,
  3412. const struct intel_watermark_params *display,
  3413. const struct intel_watermark_params *cursor)
  3414. {
  3415. DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
  3416. display_wm, cursor_wm);
  3417. if (display_wm > display->max_wm) {
  3418. DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
  3419. display_wm, display->max_wm);
  3420. return false;
  3421. }
  3422. if (cursor_wm > cursor->max_wm) {
  3423. DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
  3424. cursor_wm, cursor->max_wm);
  3425. return false;
  3426. }
  3427. if (!(display_wm || cursor_wm)) {
  3428. DRM_DEBUG_KMS("SR latency is 0, disabling\n");
  3429. return false;
  3430. }
  3431. return true;
  3432. }
  3433. static bool g4x_compute_srwm(struct drm_device *dev,
  3434. int plane,
  3435. int latency_ns,
  3436. const struct intel_watermark_params *display,
  3437. const struct intel_watermark_params *cursor,
  3438. int *display_wm, int *cursor_wm)
  3439. {
  3440. struct drm_crtc *crtc;
  3441. int hdisplay, htotal, pixel_size, clock;
  3442. unsigned long line_time_us;
  3443. int line_count, line_size;
  3444. int small, large;
  3445. int entries;
  3446. if (!latency_ns) {
  3447. *display_wm = *cursor_wm = 0;
  3448. return false;
  3449. }
  3450. crtc = intel_get_crtc_for_plane(dev, plane);
  3451. hdisplay = crtc->mode.hdisplay;
  3452. htotal = crtc->mode.htotal;
  3453. clock = crtc->mode.clock;
  3454. pixel_size = crtc->fb->bits_per_pixel / 8;
  3455. line_time_us = (htotal * 1000) / clock;
  3456. line_count = (latency_ns / line_time_us + 1000) / 1000;
  3457. line_size = hdisplay * pixel_size;
  3458. /* Use the minimum of the small and large buffer method for primary */
  3459. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  3460. large = line_count * line_size;
  3461. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  3462. *display_wm = entries + display->guard_size;
  3463. /* calculate the self-refresh watermark for display cursor */
  3464. entries = line_count * pixel_size * 64;
  3465. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3466. *cursor_wm = entries + cursor->guard_size;
  3467. return g4x_check_srwm(dev,
  3468. *display_wm, *cursor_wm,
  3469. display, cursor);
  3470. }
  3471. #define single_plane_enabled(mask) is_power_of_2(mask)
  3472. static void g4x_update_wm(struct drm_device *dev)
  3473. {
  3474. static const int sr_latency_ns = 12000;
  3475. struct drm_i915_private *dev_priv = dev->dev_private;
  3476. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  3477. int plane_sr, cursor_sr;
  3478. unsigned int enabled = 0;
  3479. if (g4x_compute_wm0(dev, 0,
  3480. &g4x_wm_info, latency_ns,
  3481. &g4x_cursor_wm_info, latency_ns,
  3482. &planea_wm, &cursora_wm))
  3483. enabled |= 1;
  3484. if (g4x_compute_wm0(dev, 1,
  3485. &g4x_wm_info, latency_ns,
  3486. &g4x_cursor_wm_info, latency_ns,
  3487. &planeb_wm, &cursorb_wm))
  3488. enabled |= 2;
  3489. plane_sr = cursor_sr = 0;
  3490. if (single_plane_enabled(enabled) &&
  3491. g4x_compute_srwm(dev, ffs(enabled) - 1,
  3492. sr_latency_ns,
  3493. &g4x_wm_info,
  3494. &g4x_cursor_wm_info,
  3495. &plane_sr, &cursor_sr))
  3496. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  3497. else
  3498. I915_WRITE(FW_BLC_SELF,
  3499. I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
  3500. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  3501. planea_wm, cursora_wm,
  3502. planeb_wm, cursorb_wm,
  3503. plane_sr, cursor_sr);
  3504. I915_WRITE(DSPFW1,
  3505. (plane_sr << DSPFW_SR_SHIFT) |
  3506. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  3507. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  3508. planea_wm);
  3509. I915_WRITE(DSPFW2,
  3510. (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  3511. (cursora_wm << DSPFW_CURSORA_SHIFT));
  3512. /* HPLL off in SR has some issues on G4x... disable it */
  3513. I915_WRITE(DSPFW3,
  3514. (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  3515. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  3516. }
  3517. static void i965_update_wm(struct drm_device *dev)
  3518. {
  3519. struct drm_i915_private *dev_priv = dev->dev_private;
  3520. struct drm_crtc *crtc;
  3521. int srwm = 1;
  3522. int cursor_sr = 16;
  3523. /* Calc sr entries for one plane configs */
  3524. crtc = single_enabled_crtc(dev);
  3525. if (crtc) {
  3526. /* self-refresh has much higher latency */
  3527. static const int sr_latency_ns = 12000;
  3528. int clock = crtc->mode.clock;
  3529. int htotal = crtc->mode.htotal;
  3530. int hdisplay = crtc->mode.hdisplay;
  3531. int pixel_size = crtc->fb->bits_per_pixel / 8;
  3532. unsigned long line_time_us;
  3533. int entries;
  3534. line_time_us = ((htotal * 1000) / clock);
  3535. /* Use ns/us then divide to preserve precision */
  3536. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3537. pixel_size * hdisplay;
  3538. entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
  3539. srwm = I965_FIFO_SIZE - entries;
  3540. if (srwm < 0)
  3541. srwm = 1;
  3542. srwm &= 0x1ff;
  3543. DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
  3544. entries, srwm);
  3545. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3546. pixel_size * 64;
  3547. entries = DIV_ROUND_UP(entries,
  3548. i965_cursor_wm_info.cacheline_size);
  3549. cursor_sr = i965_cursor_wm_info.fifo_size -
  3550. (entries + i965_cursor_wm_info.guard_size);
  3551. if (cursor_sr > i965_cursor_wm_info.max_wm)
  3552. cursor_sr = i965_cursor_wm_info.max_wm;
  3553. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  3554. "cursor %d\n", srwm, cursor_sr);
  3555. if (IS_CRESTLINE(dev))
  3556. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  3557. } else {
  3558. /* Turn off self refresh if both pipes are enabled */
  3559. if (IS_CRESTLINE(dev))
  3560. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  3561. & ~FW_BLC_SELF_EN);
  3562. }
  3563. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  3564. srwm);
  3565. /* 965 has limitations... */
  3566. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
  3567. (8 << 16) | (8 << 8) | (8 << 0));
  3568. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  3569. /* update cursor SR watermark */
  3570. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  3571. }
  3572. static void i9xx_update_wm(struct drm_device *dev)
  3573. {
  3574. struct drm_i915_private *dev_priv = dev->dev_private;
  3575. const struct intel_watermark_params *wm_info;
  3576. uint32_t fwater_lo;
  3577. uint32_t fwater_hi;
  3578. int cwm, srwm = 1;
  3579. int fifo_size;
  3580. int planea_wm, planeb_wm;
  3581. struct drm_crtc *crtc, *enabled = NULL;
  3582. if (IS_I945GM(dev))
  3583. wm_info = &i945_wm_info;
  3584. else if (!IS_GEN2(dev))
  3585. wm_info = &i915_wm_info;
  3586. else
  3587. wm_info = &i855_wm_info;
  3588. fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  3589. crtc = intel_get_crtc_for_plane(dev, 0);
  3590. if (crtc->enabled && crtc->fb) {
  3591. planea_wm = intel_calculate_wm(crtc->mode.clock,
  3592. wm_info, fifo_size,
  3593. crtc->fb->bits_per_pixel / 8,
  3594. latency_ns);
  3595. enabled = crtc;
  3596. } else
  3597. planea_wm = fifo_size - wm_info->guard_size;
  3598. fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  3599. crtc = intel_get_crtc_for_plane(dev, 1);
  3600. if (crtc->enabled && crtc->fb) {
  3601. planeb_wm = intel_calculate_wm(crtc->mode.clock,
  3602. wm_info, fifo_size,
  3603. crtc->fb->bits_per_pixel / 8,
  3604. latency_ns);
  3605. if (enabled == NULL)
  3606. enabled = crtc;
  3607. else
  3608. enabled = NULL;
  3609. } else
  3610. planeb_wm = fifo_size - wm_info->guard_size;
  3611. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  3612. /*
  3613. * Overlay gets an aggressive default since video jitter is bad.
  3614. */
  3615. cwm = 2;
  3616. /* Play safe and disable self-refresh before adjusting watermarks. */
  3617. if (IS_I945G(dev) || IS_I945GM(dev))
  3618. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
  3619. else if (IS_I915GM(dev))
  3620. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  3621. /* Calc sr entries for one plane configs */
  3622. if (HAS_FW_BLC(dev) && enabled) {
  3623. /* self-refresh has much higher latency */
  3624. static const int sr_latency_ns = 6000;
  3625. int clock = enabled->mode.clock;
  3626. int htotal = enabled->mode.htotal;
  3627. int hdisplay = enabled->mode.hdisplay;
  3628. int pixel_size = enabled->fb->bits_per_pixel / 8;
  3629. unsigned long line_time_us;
  3630. int entries;
  3631. line_time_us = (htotal * 1000) / clock;
  3632. /* Use ns/us then divide to preserve precision */
  3633. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3634. pixel_size * hdisplay;
  3635. entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
  3636. DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
  3637. srwm = wm_info->fifo_size - entries;
  3638. if (srwm < 0)
  3639. srwm = 1;
  3640. if (IS_I945G(dev) || IS_I945GM(dev))
  3641. I915_WRITE(FW_BLC_SELF,
  3642. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  3643. else if (IS_I915GM(dev))
  3644. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  3645. }
  3646. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  3647. planea_wm, planeb_wm, cwm, srwm);
  3648. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  3649. fwater_hi = (cwm & 0x1f);
  3650. /* Set request length to 8 cachelines per fetch */
  3651. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  3652. fwater_hi = fwater_hi | (1 << 8);
  3653. I915_WRITE(FW_BLC, fwater_lo);
  3654. I915_WRITE(FW_BLC2, fwater_hi);
  3655. if (HAS_FW_BLC(dev)) {
  3656. if (enabled) {
  3657. if (IS_I945G(dev) || IS_I945GM(dev))
  3658. I915_WRITE(FW_BLC_SELF,
  3659. FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  3660. else if (IS_I915GM(dev))
  3661. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  3662. DRM_DEBUG_KMS("memory self refresh enabled\n");
  3663. } else
  3664. DRM_DEBUG_KMS("memory self refresh disabled\n");
  3665. }
  3666. }
  3667. static void i830_update_wm(struct drm_device *dev)
  3668. {
  3669. struct drm_i915_private *dev_priv = dev->dev_private;
  3670. struct drm_crtc *crtc;
  3671. uint32_t fwater_lo;
  3672. int planea_wm;
  3673. crtc = single_enabled_crtc(dev);
  3674. if (crtc == NULL)
  3675. return;
  3676. planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
  3677. dev_priv->display.get_fifo_size(dev, 0),
  3678. crtc->fb->bits_per_pixel / 8,
  3679. latency_ns);
  3680. fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  3681. fwater_lo |= (3<<8) | planea_wm;
  3682. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  3683. I915_WRITE(FW_BLC, fwater_lo);
  3684. }
  3685. #define ILK_LP0_PLANE_LATENCY 700
  3686. #define ILK_LP0_CURSOR_LATENCY 1300
  3687. /*
  3688. * Check the wm result.
  3689. *
  3690. * If any calculated watermark values is larger than the maximum value that
  3691. * can be programmed into the associated watermark register, that watermark
  3692. * must be disabled.
  3693. */
  3694. static bool ironlake_check_srwm(struct drm_device *dev, int level,
  3695. int fbc_wm, int display_wm, int cursor_wm,
  3696. const struct intel_watermark_params *display,
  3697. const struct intel_watermark_params *cursor)
  3698. {
  3699. struct drm_i915_private *dev_priv = dev->dev_private;
  3700. DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
  3701. " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
  3702. if (fbc_wm > SNB_FBC_MAX_SRWM) {
  3703. DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
  3704. fbc_wm, SNB_FBC_MAX_SRWM, level);
  3705. /* fbc has it's own way to disable FBC WM */
  3706. I915_WRITE(DISP_ARB_CTL,
  3707. I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
  3708. return false;
  3709. }
  3710. if (display_wm > display->max_wm) {
  3711. DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
  3712. display_wm, SNB_DISPLAY_MAX_SRWM, level);
  3713. return false;
  3714. }
  3715. if (cursor_wm > cursor->max_wm) {
  3716. DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
  3717. cursor_wm, SNB_CURSOR_MAX_SRWM, level);
  3718. return false;
  3719. }
  3720. if (!(fbc_wm || display_wm || cursor_wm)) {
  3721. DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
  3722. return false;
  3723. }
  3724. return true;
  3725. }
  3726. /*
  3727. * Compute watermark values of WM[1-3],
  3728. */
  3729. static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
  3730. int latency_ns,
  3731. const struct intel_watermark_params *display,
  3732. const struct intel_watermark_params *cursor,
  3733. int *fbc_wm, int *display_wm, int *cursor_wm)
  3734. {
  3735. struct drm_crtc *crtc;
  3736. unsigned long line_time_us;
  3737. int hdisplay, htotal, pixel_size, clock;
  3738. int line_count, line_size;
  3739. int small, large;
  3740. int entries;
  3741. if (!latency_ns) {
  3742. *fbc_wm = *display_wm = *cursor_wm = 0;
  3743. return false;
  3744. }
  3745. crtc = intel_get_crtc_for_plane(dev, plane);
  3746. hdisplay = crtc->mode.hdisplay;
  3747. htotal = crtc->mode.htotal;
  3748. clock = crtc->mode.clock;
  3749. pixel_size = crtc->fb->bits_per_pixel / 8;
  3750. line_time_us = (htotal * 1000) / clock;
  3751. line_count = (latency_ns / line_time_us + 1000) / 1000;
  3752. line_size = hdisplay * pixel_size;
  3753. /* Use the minimum of the small and large buffer method for primary */
  3754. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  3755. large = line_count * line_size;
  3756. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  3757. *display_wm = entries + display->guard_size;
  3758. /*
  3759. * Spec says:
  3760. * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
  3761. */
  3762. *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
  3763. /* calculate the self-refresh watermark for display cursor */
  3764. entries = line_count * pixel_size * 64;
  3765. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3766. *cursor_wm = entries + cursor->guard_size;
  3767. return ironlake_check_srwm(dev, level,
  3768. *fbc_wm, *display_wm, *cursor_wm,
  3769. display, cursor);
  3770. }
  3771. static void ironlake_update_wm(struct drm_device *dev)
  3772. {
  3773. struct drm_i915_private *dev_priv = dev->dev_private;
  3774. int fbc_wm, plane_wm, cursor_wm;
  3775. unsigned int enabled;
  3776. enabled = 0;
  3777. if (g4x_compute_wm0(dev, 0,
  3778. &ironlake_display_wm_info,
  3779. ILK_LP0_PLANE_LATENCY,
  3780. &ironlake_cursor_wm_info,
  3781. ILK_LP0_CURSOR_LATENCY,
  3782. &plane_wm, &cursor_wm)) {
  3783. I915_WRITE(WM0_PIPEA_ILK,
  3784. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3785. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3786. " plane %d, " "cursor: %d\n",
  3787. plane_wm, cursor_wm);
  3788. enabled |= 1;
  3789. }
  3790. if (g4x_compute_wm0(dev, 1,
  3791. &ironlake_display_wm_info,
  3792. ILK_LP0_PLANE_LATENCY,
  3793. &ironlake_cursor_wm_info,
  3794. ILK_LP0_CURSOR_LATENCY,
  3795. &plane_wm, &cursor_wm)) {
  3796. I915_WRITE(WM0_PIPEB_ILK,
  3797. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3798. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3799. " plane %d, cursor: %d\n",
  3800. plane_wm, cursor_wm);
  3801. enabled |= 2;
  3802. }
  3803. /*
  3804. * Calculate and update the self-refresh watermark only when one
  3805. * display plane is used.
  3806. */
  3807. I915_WRITE(WM3_LP_ILK, 0);
  3808. I915_WRITE(WM2_LP_ILK, 0);
  3809. I915_WRITE(WM1_LP_ILK, 0);
  3810. if (!single_plane_enabled(enabled))
  3811. return;
  3812. enabled = ffs(enabled) - 1;
  3813. /* WM1 */
  3814. if (!ironlake_compute_srwm(dev, 1, enabled,
  3815. ILK_READ_WM1_LATENCY() * 500,
  3816. &ironlake_display_srwm_info,
  3817. &ironlake_cursor_srwm_info,
  3818. &fbc_wm, &plane_wm, &cursor_wm))
  3819. return;
  3820. I915_WRITE(WM1_LP_ILK,
  3821. WM1_LP_SR_EN |
  3822. (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3823. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3824. (plane_wm << WM1_LP_SR_SHIFT) |
  3825. cursor_wm);
  3826. /* WM2 */
  3827. if (!ironlake_compute_srwm(dev, 2, enabled,
  3828. ILK_READ_WM2_LATENCY() * 500,
  3829. &ironlake_display_srwm_info,
  3830. &ironlake_cursor_srwm_info,
  3831. &fbc_wm, &plane_wm, &cursor_wm))
  3832. return;
  3833. I915_WRITE(WM2_LP_ILK,
  3834. WM2_LP_EN |
  3835. (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3836. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3837. (plane_wm << WM1_LP_SR_SHIFT) |
  3838. cursor_wm);
  3839. /*
  3840. * WM3 is unsupported on ILK, probably because we don't have latency
  3841. * data for that power state
  3842. */
  3843. }
  3844. static void sandybridge_update_wm(struct drm_device *dev)
  3845. {
  3846. struct drm_i915_private *dev_priv = dev->dev_private;
  3847. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  3848. int fbc_wm, plane_wm, cursor_wm;
  3849. unsigned int enabled;
  3850. enabled = 0;
  3851. if (g4x_compute_wm0(dev, 0,
  3852. &sandybridge_display_wm_info, latency,
  3853. &sandybridge_cursor_wm_info, latency,
  3854. &plane_wm, &cursor_wm)) {
  3855. I915_WRITE(WM0_PIPEA_ILK,
  3856. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3857. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3858. " plane %d, " "cursor: %d\n",
  3859. plane_wm, cursor_wm);
  3860. enabled |= 1;
  3861. }
  3862. if (g4x_compute_wm0(dev, 1,
  3863. &sandybridge_display_wm_info, latency,
  3864. &sandybridge_cursor_wm_info, latency,
  3865. &plane_wm, &cursor_wm)) {
  3866. I915_WRITE(WM0_PIPEB_ILK,
  3867. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3868. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3869. " plane %d, cursor: %d\n",
  3870. plane_wm, cursor_wm);
  3871. enabled |= 2;
  3872. }
  3873. /*
  3874. * Calculate and update the self-refresh watermark only when one
  3875. * display plane is used.
  3876. *
  3877. * SNB support 3 levels of watermark.
  3878. *
  3879. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  3880. * and disabled in the descending order
  3881. *
  3882. */
  3883. I915_WRITE(WM3_LP_ILK, 0);
  3884. I915_WRITE(WM2_LP_ILK, 0);
  3885. I915_WRITE(WM1_LP_ILK, 0);
  3886. if (!single_plane_enabled(enabled))
  3887. return;
  3888. enabled = ffs(enabled) - 1;
  3889. /* WM1 */
  3890. if (!ironlake_compute_srwm(dev, 1, enabled,
  3891. SNB_READ_WM1_LATENCY() * 500,
  3892. &sandybridge_display_srwm_info,
  3893. &sandybridge_cursor_srwm_info,
  3894. &fbc_wm, &plane_wm, &cursor_wm))
  3895. return;
  3896. I915_WRITE(WM1_LP_ILK,
  3897. WM1_LP_SR_EN |
  3898. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3899. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3900. (plane_wm << WM1_LP_SR_SHIFT) |
  3901. cursor_wm);
  3902. /* WM2 */
  3903. if (!ironlake_compute_srwm(dev, 2, enabled,
  3904. SNB_READ_WM2_LATENCY() * 500,
  3905. &sandybridge_display_srwm_info,
  3906. &sandybridge_cursor_srwm_info,
  3907. &fbc_wm, &plane_wm, &cursor_wm))
  3908. return;
  3909. I915_WRITE(WM2_LP_ILK,
  3910. WM2_LP_EN |
  3911. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3912. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3913. (plane_wm << WM1_LP_SR_SHIFT) |
  3914. cursor_wm);
  3915. /* WM3 */
  3916. if (!ironlake_compute_srwm(dev, 3, enabled,
  3917. SNB_READ_WM3_LATENCY() * 500,
  3918. &sandybridge_display_srwm_info,
  3919. &sandybridge_cursor_srwm_info,
  3920. &fbc_wm, &plane_wm, &cursor_wm))
  3921. return;
  3922. I915_WRITE(WM3_LP_ILK,
  3923. WM3_LP_EN |
  3924. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3925. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3926. (plane_wm << WM1_LP_SR_SHIFT) |
  3927. cursor_wm);
  3928. }
  3929. /**
  3930. * intel_update_watermarks - update FIFO watermark values based on current modes
  3931. *
  3932. * Calculate watermark values for the various WM regs based on current mode
  3933. * and plane configuration.
  3934. *
  3935. * There are several cases to deal with here:
  3936. * - normal (i.e. non-self-refresh)
  3937. * - self-refresh (SR) mode
  3938. * - lines are large relative to FIFO size (buffer can hold up to 2)
  3939. * - lines are small relative to FIFO size (buffer can hold more than 2
  3940. * lines), so need to account for TLB latency
  3941. *
  3942. * The normal calculation is:
  3943. * watermark = dotclock * bytes per pixel * latency
  3944. * where latency is platform & configuration dependent (we assume pessimal
  3945. * values here).
  3946. *
  3947. * The SR calculation is:
  3948. * watermark = (trunc(latency/line time)+1) * surface width *
  3949. * bytes per pixel
  3950. * where
  3951. * line time = htotal / dotclock
  3952. * surface width = hdisplay for normal plane and 64 for cursor
  3953. * and latency is assumed to be high, as above.
  3954. *
  3955. * The final value programmed to the register should always be rounded up,
  3956. * and include an extra 2 entries to account for clock crossings.
  3957. *
  3958. * We don't use the sprite, so we can ignore that. And on Crestline we have
  3959. * to set the non-SR watermarks to 8.
  3960. */
  3961. static void intel_update_watermarks(struct drm_device *dev)
  3962. {
  3963. struct drm_i915_private *dev_priv = dev->dev_private;
  3964. if (dev_priv->display.update_wm)
  3965. dev_priv->display.update_wm(dev);
  3966. }
  3967. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  3968. {
  3969. if (i915_panel_use_ssc >= 0)
  3970. return i915_panel_use_ssc != 0;
  3971. return dev_priv->lvds_use_ssc
  3972. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  3973. }
  3974. /**
  3975. * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
  3976. * @crtc: CRTC structure
  3977. *
  3978. * A pipe may be connected to one or more outputs. Based on the depth of the
  3979. * attached framebuffer, choose a good color depth to use on the pipe.
  3980. *
  3981. * If possible, match the pipe depth to the fb depth. In some cases, this
  3982. * isn't ideal, because the connected output supports a lesser or restricted
  3983. * set of depths. Resolve that here:
  3984. * LVDS typically supports only 6bpc, so clamp down in that case
  3985. * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
  3986. * Displays may support a restricted set as well, check EDID and clamp as
  3987. * appropriate.
  3988. *
  3989. * RETURNS:
  3990. * Dithering requirement (i.e. false if display bpc and pipe bpc match,
  3991. * true if they don't match).
  3992. */
  3993. static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
  3994. unsigned int *pipe_bpp)
  3995. {
  3996. struct drm_device *dev = crtc->dev;
  3997. struct drm_i915_private *dev_priv = dev->dev_private;
  3998. struct drm_encoder *encoder;
  3999. struct drm_connector *connector;
  4000. unsigned int display_bpc = UINT_MAX, bpc;
  4001. /* Walk the encoders & connectors on this crtc, get min bpc */
  4002. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  4003. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4004. if (encoder->crtc != crtc)
  4005. continue;
  4006. if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
  4007. unsigned int lvds_bpc;
  4008. if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
  4009. LVDS_A3_POWER_UP)
  4010. lvds_bpc = 8;
  4011. else
  4012. lvds_bpc = 6;
  4013. if (lvds_bpc < display_bpc) {
  4014. DRM_DEBUG_DRIVER("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
  4015. display_bpc = lvds_bpc;
  4016. }
  4017. continue;
  4018. }
  4019. if (intel_encoder->type == INTEL_OUTPUT_EDP) {
  4020. /* Use VBT settings if we have an eDP panel */
  4021. unsigned int edp_bpc = dev_priv->edp.bpp / 3;
  4022. if (edp_bpc < display_bpc) {
  4023. DRM_DEBUG_DRIVER("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
  4024. display_bpc = edp_bpc;
  4025. }
  4026. continue;
  4027. }
  4028. /* Not one of the known troublemakers, check the EDID */
  4029. list_for_each_entry(connector, &dev->mode_config.connector_list,
  4030. head) {
  4031. if (connector->encoder != encoder)
  4032. continue;
  4033. /* Don't use an invalid EDID bpc value */
  4034. if (connector->display_info.bpc &&
  4035. connector->display_info.bpc < display_bpc) {
  4036. DRM_DEBUG_DRIVER("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
  4037. display_bpc = connector->display_info.bpc;
  4038. }
  4039. }
  4040. /*
  4041. * HDMI is either 12 or 8, so if the display lets 10bpc sneak
  4042. * through, clamp it down. (Note: >12bpc will be caught below.)
  4043. */
  4044. if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
  4045. if (display_bpc > 8 && display_bpc < 12) {
  4046. DRM_DEBUG_DRIVER("forcing bpc to 12 for HDMI\n");
  4047. display_bpc = 12;
  4048. } else {
  4049. DRM_DEBUG_DRIVER("forcing bpc to 8 for HDMI\n");
  4050. display_bpc = 8;
  4051. }
  4052. }
  4053. }
  4054. /*
  4055. * We could just drive the pipe at the highest bpc all the time and
  4056. * enable dithering as needed, but that costs bandwidth. So choose
  4057. * the minimum value that expresses the full color range of the fb but
  4058. * also stays within the max display bpc discovered above.
  4059. */
  4060. switch (crtc->fb->depth) {
  4061. case 8:
  4062. bpc = 8; /* since we go through a colormap */
  4063. break;
  4064. case 15:
  4065. case 16:
  4066. bpc = 6; /* min is 18bpp */
  4067. break;
  4068. case 24:
  4069. bpc = 8;
  4070. break;
  4071. case 30:
  4072. bpc = 10;
  4073. break;
  4074. case 48:
  4075. bpc = 12;
  4076. break;
  4077. default:
  4078. DRM_DEBUG("unsupported depth, assuming 24 bits\n");
  4079. bpc = min((unsigned int)8, display_bpc);
  4080. break;
  4081. }
  4082. display_bpc = min(display_bpc, bpc);
  4083. DRM_DEBUG_DRIVER("setting pipe bpc to %d (max display bpc %d)\n",
  4084. bpc, display_bpc);
  4085. *pipe_bpp = display_bpc * 3;
  4086. return display_bpc != bpc;
  4087. }
  4088. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  4089. struct drm_display_mode *mode,
  4090. struct drm_display_mode *adjusted_mode,
  4091. int x, int y,
  4092. struct drm_framebuffer *old_fb)
  4093. {
  4094. struct drm_device *dev = crtc->dev;
  4095. struct drm_i915_private *dev_priv = dev->dev_private;
  4096. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4097. int pipe = intel_crtc->pipe;
  4098. int plane = intel_crtc->plane;
  4099. int refclk, num_connectors = 0;
  4100. intel_clock_t clock, reduced_clock;
  4101. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  4102. bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
  4103. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  4104. struct drm_mode_config *mode_config = &dev->mode_config;
  4105. struct intel_encoder *encoder;
  4106. const intel_limit_t *limit;
  4107. int ret;
  4108. u32 temp;
  4109. u32 lvds_sync = 0;
  4110. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  4111. if (encoder->base.crtc != crtc)
  4112. continue;
  4113. switch (encoder->type) {
  4114. case INTEL_OUTPUT_LVDS:
  4115. is_lvds = true;
  4116. break;
  4117. case INTEL_OUTPUT_SDVO:
  4118. case INTEL_OUTPUT_HDMI:
  4119. is_sdvo = true;
  4120. if (encoder->needs_tv_clock)
  4121. is_tv = true;
  4122. break;
  4123. case INTEL_OUTPUT_DVO:
  4124. is_dvo = true;
  4125. break;
  4126. case INTEL_OUTPUT_TVOUT:
  4127. is_tv = true;
  4128. break;
  4129. case INTEL_OUTPUT_ANALOG:
  4130. is_crt = true;
  4131. break;
  4132. case INTEL_OUTPUT_DISPLAYPORT:
  4133. is_dp = true;
  4134. break;
  4135. }
  4136. num_connectors++;
  4137. }
  4138. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4139. refclk = dev_priv->lvds_ssc_freq * 1000;
  4140. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  4141. refclk / 1000);
  4142. } else if (!IS_GEN2(dev)) {
  4143. refclk = 96000;
  4144. } else {
  4145. refclk = 48000;
  4146. }
  4147. /*
  4148. * Returns a set of divisors for the desired target clock with the given
  4149. * refclk, or FALSE. The returned values represent the clock equation:
  4150. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  4151. */
  4152. limit = intel_limit(crtc, refclk);
  4153. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  4154. if (!ok) {
  4155. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4156. return -EINVAL;
  4157. }
  4158. /* Ensure that the cursor is valid for the new mode before changing... */
  4159. intel_crtc_update_cursor(crtc, true);
  4160. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4161. has_reduced_clock = limit->find_pll(limit, crtc,
  4162. dev_priv->lvds_downclock,
  4163. refclk,
  4164. &reduced_clock);
  4165. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  4166. /*
  4167. * If the different P is found, it means that we can't
  4168. * switch the display clock by using the FP0/FP1.
  4169. * In such case we will disable the LVDS downclock
  4170. * feature.
  4171. */
  4172. DRM_DEBUG_KMS("Different P is found for "
  4173. "LVDS clock/downclock\n");
  4174. has_reduced_clock = 0;
  4175. }
  4176. }
  4177. /* SDVO TV has fixed PLL values depend on its clock range,
  4178. this mirrors vbios setting. */
  4179. if (is_sdvo && is_tv) {
  4180. if (adjusted_mode->clock >= 100000
  4181. && adjusted_mode->clock < 140500) {
  4182. clock.p1 = 2;
  4183. clock.p2 = 10;
  4184. clock.n = 3;
  4185. clock.m1 = 16;
  4186. clock.m2 = 8;
  4187. } else if (adjusted_mode->clock >= 140500
  4188. && adjusted_mode->clock <= 200000) {
  4189. clock.p1 = 1;
  4190. clock.p2 = 10;
  4191. clock.n = 6;
  4192. clock.m1 = 12;
  4193. clock.m2 = 8;
  4194. }
  4195. }
  4196. if (IS_PINEVIEW(dev)) {
  4197. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  4198. if (has_reduced_clock)
  4199. fp2 = (1 << reduced_clock.n) << 16 |
  4200. reduced_clock.m1 << 8 | reduced_clock.m2;
  4201. } else {
  4202. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  4203. if (has_reduced_clock)
  4204. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  4205. reduced_clock.m2;
  4206. }
  4207. dpll = DPLL_VGA_MODE_DIS;
  4208. if (!IS_GEN2(dev)) {
  4209. if (is_lvds)
  4210. dpll |= DPLLB_MODE_LVDS;
  4211. else
  4212. dpll |= DPLLB_MODE_DAC_SERIAL;
  4213. if (is_sdvo) {
  4214. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4215. if (pixel_multiplier > 1) {
  4216. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4217. dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  4218. }
  4219. dpll |= DPLL_DVO_HIGH_SPEED;
  4220. }
  4221. if (is_dp)
  4222. dpll |= DPLL_DVO_HIGH_SPEED;
  4223. /* compute bitmask from p1 value */
  4224. if (IS_PINEVIEW(dev))
  4225. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4226. else {
  4227. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4228. if (IS_G4X(dev) && has_reduced_clock)
  4229. dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4230. }
  4231. switch (clock.p2) {
  4232. case 5:
  4233. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4234. break;
  4235. case 7:
  4236. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4237. break;
  4238. case 10:
  4239. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4240. break;
  4241. case 14:
  4242. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4243. break;
  4244. }
  4245. if (INTEL_INFO(dev)->gen >= 4)
  4246. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4247. } else {
  4248. if (is_lvds) {
  4249. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4250. } else {
  4251. if (clock.p1 == 2)
  4252. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4253. else
  4254. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4255. if (clock.p2 == 4)
  4256. dpll |= PLL_P2_DIVIDE_BY_4;
  4257. }
  4258. }
  4259. if (is_sdvo && is_tv)
  4260. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4261. else if (is_tv)
  4262. /* XXX: just matching BIOS for now */
  4263. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  4264. dpll |= 3;
  4265. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4266. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4267. else
  4268. dpll |= PLL_REF_INPUT_DREFCLK;
  4269. /* setup pipeconf */
  4270. pipeconf = I915_READ(PIPECONF(pipe));
  4271. /* Set up the display plane register */
  4272. dspcntr = DISPPLANE_GAMMA_ENABLE;
  4273. /* Ironlake's plane is forced to pipe, bit 24 is to
  4274. enable color space conversion */
  4275. if (pipe == 0)
  4276. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  4277. else
  4278. dspcntr |= DISPPLANE_SEL_PIPE_B;
  4279. if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  4280. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  4281. * core speed.
  4282. *
  4283. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  4284. * pipe == 0 check?
  4285. */
  4286. if (mode->clock >
  4287. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  4288. pipeconf |= PIPECONF_DOUBLE_WIDE;
  4289. else
  4290. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  4291. }
  4292. dpll |= DPLL_VCO_ENABLE;
  4293. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  4294. drm_mode_debug_printmodeline(mode);
  4295. I915_WRITE(FP0(pipe), fp);
  4296. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  4297. POSTING_READ(DPLL(pipe));
  4298. udelay(150);
  4299. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  4300. * This is an exception to the general rule that mode_set doesn't turn
  4301. * things on.
  4302. */
  4303. if (is_lvds) {
  4304. temp = I915_READ(LVDS);
  4305. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  4306. if (pipe == 1) {
  4307. temp |= LVDS_PIPEB_SELECT;
  4308. } else {
  4309. temp &= ~LVDS_PIPEB_SELECT;
  4310. }
  4311. /* set the corresponsding LVDS_BORDER bit */
  4312. temp |= dev_priv->lvds_border_bits;
  4313. /* Set the B0-B3 data pairs corresponding to whether we're going to
  4314. * set the DPLLs for dual-channel mode or not.
  4315. */
  4316. if (clock.p2 == 7)
  4317. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  4318. else
  4319. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  4320. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  4321. * appropriately here, but we need to look more thoroughly into how
  4322. * panels behave in the two modes.
  4323. */
  4324. /* set the dithering flag on LVDS as needed */
  4325. if (INTEL_INFO(dev)->gen >= 4) {
  4326. if (dev_priv->lvds_dither)
  4327. temp |= LVDS_ENABLE_DITHER;
  4328. else
  4329. temp &= ~LVDS_ENABLE_DITHER;
  4330. }
  4331. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  4332. lvds_sync |= LVDS_HSYNC_POLARITY;
  4333. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  4334. lvds_sync |= LVDS_VSYNC_POLARITY;
  4335. if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
  4336. != lvds_sync) {
  4337. char flags[2] = "-+";
  4338. DRM_INFO("Changing LVDS panel from "
  4339. "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
  4340. flags[!(temp & LVDS_HSYNC_POLARITY)],
  4341. flags[!(temp & LVDS_VSYNC_POLARITY)],
  4342. flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
  4343. flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
  4344. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  4345. temp |= lvds_sync;
  4346. }
  4347. I915_WRITE(LVDS, temp);
  4348. }
  4349. if (is_dp) {
  4350. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  4351. }
  4352. I915_WRITE(DPLL(pipe), dpll);
  4353. /* Wait for the clocks to stabilize. */
  4354. POSTING_READ(DPLL(pipe));
  4355. udelay(150);
  4356. if (INTEL_INFO(dev)->gen >= 4) {
  4357. temp = 0;
  4358. if (is_sdvo) {
  4359. temp = intel_mode_get_pixel_multiplier(adjusted_mode);
  4360. if (temp > 1)
  4361. temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4362. else
  4363. temp = 0;
  4364. }
  4365. I915_WRITE(DPLL_MD(pipe), temp);
  4366. } else {
  4367. /* The pixel multiplier can only be updated once the
  4368. * DPLL is enabled and the clocks are stable.
  4369. *
  4370. * So write it again.
  4371. */
  4372. I915_WRITE(DPLL(pipe), dpll);
  4373. }
  4374. intel_crtc->lowfreq_avail = false;
  4375. if (is_lvds && has_reduced_clock && i915_powersave) {
  4376. I915_WRITE(FP1(pipe), fp2);
  4377. intel_crtc->lowfreq_avail = true;
  4378. if (HAS_PIPE_CXSR(dev)) {
  4379. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4380. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4381. }
  4382. } else {
  4383. I915_WRITE(FP1(pipe), fp);
  4384. if (HAS_PIPE_CXSR(dev)) {
  4385. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4386. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  4387. }
  4388. }
  4389. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4390. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4391. /* the chip adds 2 halflines automatically */
  4392. adjusted_mode->crtc_vdisplay -= 1;
  4393. adjusted_mode->crtc_vtotal -= 1;
  4394. adjusted_mode->crtc_vblank_start -= 1;
  4395. adjusted_mode->crtc_vblank_end -= 1;
  4396. adjusted_mode->crtc_vsync_end -= 1;
  4397. adjusted_mode->crtc_vsync_start -= 1;
  4398. } else
  4399. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  4400. I915_WRITE(HTOTAL(pipe),
  4401. (adjusted_mode->crtc_hdisplay - 1) |
  4402. ((adjusted_mode->crtc_htotal - 1) << 16));
  4403. I915_WRITE(HBLANK(pipe),
  4404. (adjusted_mode->crtc_hblank_start - 1) |
  4405. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4406. I915_WRITE(HSYNC(pipe),
  4407. (adjusted_mode->crtc_hsync_start - 1) |
  4408. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4409. I915_WRITE(VTOTAL(pipe),
  4410. (adjusted_mode->crtc_vdisplay - 1) |
  4411. ((adjusted_mode->crtc_vtotal - 1) << 16));
  4412. I915_WRITE(VBLANK(pipe),
  4413. (adjusted_mode->crtc_vblank_start - 1) |
  4414. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  4415. I915_WRITE(VSYNC(pipe),
  4416. (adjusted_mode->crtc_vsync_start - 1) |
  4417. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4418. /* pipesrc and dspsize control the size that is scaled from,
  4419. * which should always be the user's requested size.
  4420. */
  4421. I915_WRITE(DSPSIZE(plane),
  4422. ((mode->vdisplay - 1) << 16) |
  4423. (mode->hdisplay - 1));
  4424. I915_WRITE(DSPPOS(plane), 0);
  4425. I915_WRITE(PIPESRC(pipe),
  4426. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  4427. I915_WRITE(PIPECONF(pipe), pipeconf);
  4428. POSTING_READ(PIPECONF(pipe));
  4429. intel_enable_pipe(dev_priv, pipe, false);
  4430. intel_wait_for_vblank(dev, pipe);
  4431. I915_WRITE(DSPCNTR(plane), dspcntr);
  4432. POSTING_READ(DSPCNTR(plane));
  4433. intel_enable_plane(dev_priv, plane, pipe);
  4434. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  4435. intel_update_watermarks(dev);
  4436. return ret;
  4437. }
  4438. /*
  4439. * Initialize reference clocks when the driver loads
  4440. */
  4441. void ironlake_init_pch_refclk(struct drm_device *dev)
  4442. {
  4443. struct drm_i915_private *dev_priv = dev->dev_private;
  4444. struct drm_mode_config *mode_config = &dev->mode_config;
  4445. struct intel_encoder *encoder;
  4446. u32 temp;
  4447. bool has_lvds = false;
  4448. bool has_cpu_edp = false;
  4449. bool has_pch_edp = false;
  4450. bool has_panel = false;
  4451. bool has_ck505 = false;
  4452. bool can_ssc = false;
  4453. /* We need to take the global config into account */
  4454. list_for_each_entry(encoder, &mode_config->encoder_list,
  4455. base.head) {
  4456. switch (encoder->type) {
  4457. case INTEL_OUTPUT_LVDS:
  4458. has_panel = true;
  4459. has_lvds = true;
  4460. break;
  4461. case INTEL_OUTPUT_EDP:
  4462. has_panel = true;
  4463. if (intel_encoder_is_pch_edp(&encoder->base))
  4464. has_pch_edp = true;
  4465. else
  4466. has_cpu_edp = true;
  4467. break;
  4468. }
  4469. }
  4470. if (HAS_PCH_IBX(dev)) {
  4471. has_ck505 = dev_priv->display_clock_mode;
  4472. can_ssc = has_ck505;
  4473. } else {
  4474. has_ck505 = false;
  4475. can_ssc = true;
  4476. }
  4477. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
  4478. has_panel, has_lvds, has_pch_edp, has_cpu_edp,
  4479. has_ck505);
  4480. /* Ironlake: try to setup display ref clock before DPLL
  4481. * enabling. This is only under driver's control after
  4482. * PCH B stepping, previous chipset stepping should be
  4483. * ignoring this setting.
  4484. */
  4485. temp = I915_READ(PCH_DREF_CONTROL);
  4486. /* Always enable nonspread source */
  4487. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  4488. if (has_ck505)
  4489. temp |= DREF_NONSPREAD_CK505_ENABLE;
  4490. else
  4491. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  4492. if (has_panel) {
  4493. temp &= ~DREF_SSC_SOURCE_MASK;
  4494. temp |= DREF_SSC_SOURCE_ENABLE;
  4495. /* SSC must be turned on before enabling the CPU output */
  4496. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  4497. DRM_DEBUG_KMS("Using SSC on panel\n");
  4498. temp |= DREF_SSC1_ENABLE;
  4499. }
  4500. /* Get SSC going before enabling the outputs */
  4501. I915_WRITE(PCH_DREF_CONTROL, temp);
  4502. POSTING_READ(PCH_DREF_CONTROL);
  4503. udelay(200);
  4504. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4505. /* Enable CPU source on CPU attached eDP */
  4506. if (has_cpu_edp) {
  4507. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  4508. DRM_DEBUG_KMS("Using SSC on eDP\n");
  4509. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  4510. }
  4511. else
  4512. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  4513. } else
  4514. temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4515. I915_WRITE(PCH_DREF_CONTROL, temp);
  4516. POSTING_READ(PCH_DREF_CONTROL);
  4517. udelay(200);
  4518. } else {
  4519. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  4520. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4521. /* Turn off CPU output */
  4522. temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4523. I915_WRITE(PCH_DREF_CONTROL, temp);
  4524. POSTING_READ(PCH_DREF_CONTROL);
  4525. udelay(200);
  4526. /* Turn off the SSC source */
  4527. temp &= ~DREF_SSC_SOURCE_MASK;
  4528. temp |= DREF_SSC_SOURCE_DISABLE;
  4529. /* Turn off SSC1 */
  4530. temp &= ~ DREF_SSC1_ENABLE;
  4531. I915_WRITE(PCH_DREF_CONTROL, temp);
  4532. POSTING_READ(PCH_DREF_CONTROL);
  4533. udelay(200);
  4534. }
  4535. }
  4536. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  4537. struct drm_display_mode *mode,
  4538. struct drm_display_mode *adjusted_mode,
  4539. int x, int y,
  4540. struct drm_framebuffer *old_fb)
  4541. {
  4542. struct drm_device *dev = crtc->dev;
  4543. struct drm_i915_private *dev_priv = dev->dev_private;
  4544. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4545. int pipe = intel_crtc->pipe;
  4546. int plane = intel_crtc->plane;
  4547. int refclk, num_connectors = 0;
  4548. intel_clock_t clock, reduced_clock;
  4549. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  4550. bool ok, has_reduced_clock = false, is_sdvo = false;
  4551. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  4552. struct intel_encoder *has_edp_encoder = NULL;
  4553. struct drm_mode_config *mode_config = &dev->mode_config;
  4554. struct intel_encoder *encoder;
  4555. const intel_limit_t *limit;
  4556. int ret;
  4557. struct fdi_m_n m_n = {0};
  4558. u32 temp;
  4559. u32 lvds_sync = 0;
  4560. int target_clock, pixel_multiplier, lane, link_bw, factor;
  4561. unsigned int pipe_bpp;
  4562. bool dither;
  4563. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  4564. if (encoder->base.crtc != crtc)
  4565. continue;
  4566. switch (encoder->type) {
  4567. case INTEL_OUTPUT_LVDS:
  4568. is_lvds = true;
  4569. break;
  4570. case INTEL_OUTPUT_SDVO:
  4571. case INTEL_OUTPUT_HDMI:
  4572. is_sdvo = true;
  4573. if (encoder->needs_tv_clock)
  4574. is_tv = true;
  4575. break;
  4576. case INTEL_OUTPUT_TVOUT:
  4577. is_tv = true;
  4578. break;
  4579. case INTEL_OUTPUT_ANALOG:
  4580. is_crt = true;
  4581. break;
  4582. case INTEL_OUTPUT_DISPLAYPORT:
  4583. is_dp = true;
  4584. break;
  4585. case INTEL_OUTPUT_EDP:
  4586. has_edp_encoder = encoder;
  4587. break;
  4588. }
  4589. num_connectors++;
  4590. }
  4591. /*
  4592. * Every reference clock in a PCH system is 120MHz
  4593. */
  4594. refclk = 120000;
  4595. /*
  4596. * Returns a set of divisors for the desired target clock with the given
  4597. * refclk, or FALSE. The returned values represent the clock equation:
  4598. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  4599. */
  4600. limit = intel_limit(crtc, refclk);
  4601. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  4602. if (!ok) {
  4603. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4604. return -EINVAL;
  4605. }
  4606. /* Ensure that the cursor is valid for the new mode before changing... */
  4607. intel_crtc_update_cursor(crtc, true);
  4608. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4609. has_reduced_clock = limit->find_pll(limit, crtc,
  4610. dev_priv->lvds_downclock,
  4611. refclk,
  4612. &reduced_clock);
  4613. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  4614. /*
  4615. * If the different P is found, it means that we can't
  4616. * switch the display clock by using the FP0/FP1.
  4617. * In such case we will disable the LVDS downclock
  4618. * feature.
  4619. */
  4620. DRM_DEBUG_KMS("Different P is found for "
  4621. "LVDS clock/downclock\n");
  4622. has_reduced_clock = 0;
  4623. }
  4624. }
  4625. /* SDVO TV has fixed PLL values depend on its clock range,
  4626. this mirrors vbios setting. */
  4627. if (is_sdvo && is_tv) {
  4628. if (adjusted_mode->clock >= 100000
  4629. && adjusted_mode->clock < 140500) {
  4630. clock.p1 = 2;
  4631. clock.p2 = 10;
  4632. clock.n = 3;
  4633. clock.m1 = 16;
  4634. clock.m2 = 8;
  4635. } else if (adjusted_mode->clock >= 140500
  4636. && adjusted_mode->clock <= 200000) {
  4637. clock.p1 = 1;
  4638. clock.p2 = 10;
  4639. clock.n = 6;
  4640. clock.m1 = 12;
  4641. clock.m2 = 8;
  4642. }
  4643. }
  4644. /* FDI link */
  4645. pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4646. lane = 0;
  4647. /* CPU eDP doesn't require FDI link, so just set DP M/N
  4648. according to current link config */
  4649. if (has_edp_encoder &&
  4650. !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4651. target_clock = mode->clock;
  4652. intel_edp_link_config(has_edp_encoder,
  4653. &lane, &link_bw);
  4654. } else {
  4655. /* [e]DP over FDI requires target mode clock
  4656. instead of link clock */
  4657. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  4658. target_clock = mode->clock;
  4659. else
  4660. target_clock = adjusted_mode->clock;
  4661. /* FDI is a binary signal running at ~2.7GHz, encoding
  4662. * each output octet as 10 bits. The actual frequency
  4663. * is stored as a divider into a 100MHz clock, and the
  4664. * mode pixel clock is stored in units of 1KHz.
  4665. * Hence the bw of each lane in terms of the mode signal
  4666. * is:
  4667. */
  4668. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4669. }
  4670. /* determine panel color depth */
  4671. temp = I915_READ(PIPECONF(pipe));
  4672. temp &= ~PIPE_BPC_MASK;
  4673. dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp);
  4674. switch (pipe_bpp) {
  4675. case 18:
  4676. temp |= PIPE_6BPC;
  4677. break;
  4678. case 24:
  4679. temp |= PIPE_8BPC;
  4680. break;
  4681. case 30:
  4682. temp |= PIPE_10BPC;
  4683. break;
  4684. case 36:
  4685. temp |= PIPE_12BPC;
  4686. break;
  4687. default:
  4688. WARN(1, "intel_choose_pipe_bpp returned invalid value %d\n",
  4689. pipe_bpp);
  4690. temp |= PIPE_8BPC;
  4691. pipe_bpp = 24;
  4692. break;
  4693. }
  4694. intel_crtc->bpp = pipe_bpp;
  4695. I915_WRITE(PIPECONF(pipe), temp);
  4696. if (!lane) {
  4697. /*
  4698. * Account for spread spectrum to avoid
  4699. * oversubscribing the link. Max center spread
  4700. * is 2.5%; use 5% for safety's sake.
  4701. */
  4702. u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
  4703. lane = bps / (link_bw * 8) + 1;
  4704. }
  4705. intel_crtc->fdi_lanes = lane;
  4706. if (pixel_multiplier > 1)
  4707. link_bw *= pixel_multiplier;
  4708. ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
  4709. &m_n);
  4710. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  4711. if (has_reduced_clock)
  4712. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  4713. reduced_clock.m2;
  4714. /* Enable autotuning of the PLL clock (if permissible) */
  4715. factor = 21;
  4716. if (is_lvds) {
  4717. if ((intel_panel_use_ssc(dev_priv) &&
  4718. dev_priv->lvds_ssc_freq == 100) ||
  4719. (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
  4720. factor = 25;
  4721. } else if (is_sdvo && is_tv)
  4722. factor = 20;
  4723. if (clock.m < factor * clock.n)
  4724. fp |= FP_CB_TUNE;
  4725. dpll = 0;
  4726. if (is_lvds)
  4727. dpll |= DPLLB_MODE_LVDS;
  4728. else
  4729. dpll |= DPLLB_MODE_DAC_SERIAL;
  4730. if (is_sdvo) {
  4731. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4732. if (pixel_multiplier > 1) {
  4733. dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  4734. }
  4735. dpll |= DPLL_DVO_HIGH_SPEED;
  4736. }
  4737. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  4738. dpll |= DPLL_DVO_HIGH_SPEED;
  4739. /* compute bitmask from p1 value */
  4740. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4741. /* also FPA1 */
  4742. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4743. switch (clock.p2) {
  4744. case 5:
  4745. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4746. break;
  4747. case 7:
  4748. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4749. break;
  4750. case 10:
  4751. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4752. break;
  4753. case 14:
  4754. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4755. break;
  4756. }
  4757. if (is_sdvo && is_tv)
  4758. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4759. else if (is_tv)
  4760. /* XXX: just matching BIOS for now */
  4761. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  4762. dpll |= 3;
  4763. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4764. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4765. else
  4766. dpll |= PLL_REF_INPUT_DREFCLK;
  4767. /* setup pipeconf */
  4768. pipeconf = I915_READ(PIPECONF(pipe));
  4769. /* Set up the display plane register */
  4770. dspcntr = DISPPLANE_GAMMA_ENABLE;
  4771. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  4772. drm_mode_debug_printmodeline(mode);
  4773. /* PCH eDP needs FDI, but CPU eDP does not */
  4774. if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4775. I915_WRITE(PCH_FP0(pipe), fp);
  4776. I915_WRITE(PCH_DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  4777. POSTING_READ(PCH_DPLL(pipe));
  4778. udelay(150);
  4779. }
  4780. /* enable transcoder DPLL */
  4781. if (HAS_PCH_CPT(dev)) {
  4782. temp = I915_READ(PCH_DPLL_SEL);
  4783. switch (pipe) {
  4784. case 0:
  4785. temp |= TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL;
  4786. break;
  4787. case 1:
  4788. temp |= TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL;
  4789. break;
  4790. case 2:
  4791. /* FIXME: manage transcoder PLLs? */
  4792. temp |= TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL;
  4793. break;
  4794. default:
  4795. BUG();
  4796. }
  4797. I915_WRITE(PCH_DPLL_SEL, temp);
  4798. POSTING_READ(PCH_DPLL_SEL);
  4799. udelay(150);
  4800. }
  4801. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  4802. * This is an exception to the general rule that mode_set doesn't turn
  4803. * things on.
  4804. */
  4805. if (is_lvds) {
  4806. temp = I915_READ(PCH_LVDS);
  4807. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  4808. if (pipe == 1) {
  4809. if (HAS_PCH_CPT(dev))
  4810. temp |= PORT_TRANS_B_SEL_CPT;
  4811. else
  4812. temp |= LVDS_PIPEB_SELECT;
  4813. } else {
  4814. if (HAS_PCH_CPT(dev))
  4815. temp &= ~PORT_TRANS_SEL_MASK;
  4816. else
  4817. temp &= ~LVDS_PIPEB_SELECT;
  4818. }
  4819. /* set the corresponsding LVDS_BORDER bit */
  4820. temp |= dev_priv->lvds_border_bits;
  4821. /* Set the B0-B3 data pairs corresponding to whether we're going to
  4822. * set the DPLLs for dual-channel mode or not.
  4823. */
  4824. if (clock.p2 == 7)
  4825. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  4826. else
  4827. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  4828. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  4829. * appropriately here, but we need to look more thoroughly into how
  4830. * panels behave in the two modes.
  4831. */
  4832. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  4833. lvds_sync |= LVDS_HSYNC_POLARITY;
  4834. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  4835. lvds_sync |= LVDS_VSYNC_POLARITY;
  4836. if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
  4837. != lvds_sync) {
  4838. char flags[2] = "-+";
  4839. DRM_INFO("Changing LVDS panel from "
  4840. "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
  4841. flags[!(temp & LVDS_HSYNC_POLARITY)],
  4842. flags[!(temp & LVDS_VSYNC_POLARITY)],
  4843. flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
  4844. flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
  4845. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  4846. temp |= lvds_sync;
  4847. }
  4848. I915_WRITE(PCH_LVDS, temp);
  4849. }
  4850. pipeconf &= ~PIPECONF_DITHER_EN;
  4851. pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
  4852. if ((is_lvds && dev_priv->lvds_dither) || dither) {
  4853. pipeconf |= PIPECONF_DITHER_EN;
  4854. pipeconf |= PIPECONF_DITHER_TYPE_ST1;
  4855. }
  4856. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4857. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  4858. } else {
  4859. /* For non-DP output, clear any trans DP clock recovery setting.*/
  4860. I915_WRITE(TRANSDATA_M1(pipe), 0);
  4861. I915_WRITE(TRANSDATA_N1(pipe), 0);
  4862. I915_WRITE(TRANSDPLINK_M1(pipe), 0);
  4863. I915_WRITE(TRANSDPLINK_N1(pipe), 0);
  4864. }
  4865. if (!has_edp_encoder ||
  4866. intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4867. I915_WRITE(PCH_DPLL(pipe), dpll);
  4868. /* Wait for the clocks to stabilize. */
  4869. POSTING_READ(PCH_DPLL(pipe));
  4870. udelay(150);
  4871. /* The pixel multiplier can only be updated once the
  4872. * DPLL is enabled and the clocks are stable.
  4873. *
  4874. * So write it again.
  4875. */
  4876. I915_WRITE(PCH_DPLL(pipe), dpll);
  4877. }
  4878. intel_crtc->lowfreq_avail = false;
  4879. if (is_lvds && has_reduced_clock && i915_powersave) {
  4880. I915_WRITE(PCH_FP1(pipe), fp2);
  4881. intel_crtc->lowfreq_avail = true;
  4882. if (HAS_PIPE_CXSR(dev)) {
  4883. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4884. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4885. }
  4886. } else {
  4887. I915_WRITE(PCH_FP1(pipe), fp);
  4888. if (HAS_PIPE_CXSR(dev)) {
  4889. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4890. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  4891. }
  4892. }
  4893. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4894. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4895. /* the chip adds 2 halflines automatically */
  4896. adjusted_mode->crtc_vdisplay -= 1;
  4897. adjusted_mode->crtc_vtotal -= 1;
  4898. adjusted_mode->crtc_vblank_start -= 1;
  4899. adjusted_mode->crtc_vblank_end -= 1;
  4900. adjusted_mode->crtc_vsync_end -= 1;
  4901. adjusted_mode->crtc_vsync_start -= 1;
  4902. } else
  4903. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  4904. I915_WRITE(HTOTAL(pipe),
  4905. (adjusted_mode->crtc_hdisplay - 1) |
  4906. ((adjusted_mode->crtc_htotal - 1) << 16));
  4907. I915_WRITE(HBLANK(pipe),
  4908. (adjusted_mode->crtc_hblank_start - 1) |
  4909. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4910. I915_WRITE(HSYNC(pipe),
  4911. (adjusted_mode->crtc_hsync_start - 1) |
  4912. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4913. I915_WRITE(VTOTAL(pipe),
  4914. (adjusted_mode->crtc_vdisplay - 1) |
  4915. ((adjusted_mode->crtc_vtotal - 1) << 16));
  4916. I915_WRITE(VBLANK(pipe),
  4917. (adjusted_mode->crtc_vblank_start - 1) |
  4918. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  4919. I915_WRITE(VSYNC(pipe),
  4920. (adjusted_mode->crtc_vsync_start - 1) |
  4921. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4922. /* pipesrc controls the size that is scaled from, which should
  4923. * always be the user's requested size.
  4924. */
  4925. I915_WRITE(PIPESRC(pipe),
  4926. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  4927. I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
  4928. I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
  4929. I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
  4930. I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
  4931. if (has_edp_encoder &&
  4932. !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4933. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  4934. }
  4935. I915_WRITE(PIPECONF(pipe), pipeconf);
  4936. POSTING_READ(PIPECONF(pipe));
  4937. intel_wait_for_vblank(dev, pipe);
  4938. if (IS_GEN5(dev)) {
  4939. /* enable address swizzle for tiling buffer */
  4940. temp = I915_READ(DISP_ARB_CTL);
  4941. I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
  4942. }
  4943. I915_WRITE(DSPCNTR(plane), dspcntr);
  4944. POSTING_READ(DSPCNTR(plane));
  4945. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  4946. intel_update_watermarks(dev);
  4947. return ret;
  4948. }
  4949. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  4950. struct drm_display_mode *mode,
  4951. struct drm_display_mode *adjusted_mode,
  4952. int x, int y,
  4953. struct drm_framebuffer *old_fb)
  4954. {
  4955. struct drm_device *dev = crtc->dev;
  4956. struct drm_i915_private *dev_priv = dev->dev_private;
  4957. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4958. int pipe = intel_crtc->pipe;
  4959. int ret;
  4960. drm_vblank_pre_modeset(dev, pipe);
  4961. ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
  4962. x, y, old_fb);
  4963. drm_vblank_post_modeset(dev, pipe);
  4964. intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
  4965. return ret;
  4966. }
  4967. static void g4x_write_eld(struct drm_connector *connector,
  4968. struct drm_crtc *crtc)
  4969. {
  4970. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  4971. uint8_t *eld = connector->eld;
  4972. uint32_t eldv;
  4973. uint32_t len;
  4974. uint32_t i;
  4975. i = I915_READ(G4X_AUD_VID_DID);
  4976. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  4977. eldv = G4X_ELDV_DEVCL_DEVBLC;
  4978. else
  4979. eldv = G4X_ELDV_DEVCTG;
  4980. i = I915_READ(G4X_AUD_CNTL_ST);
  4981. i &= ~(eldv | G4X_ELD_ADDR);
  4982. len = (i >> 9) & 0x1f; /* ELD buffer size */
  4983. I915_WRITE(G4X_AUD_CNTL_ST, i);
  4984. if (!eld[0])
  4985. return;
  4986. len = min_t(uint8_t, eld[2], len);
  4987. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  4988. for (i = 0; i < len; i++)
  4989. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  4990. i = I915_READ(G4X_AUD_CNTL_ST);
  4991. i |= eldv;
  4992. I915_WRITE(G4X_AUD_CNTL_ST, i);
  4993. }
  4994. static void ironlake_write_eld(struct drm_connector *connector,
  4995. struct drm_crtc *crtc)
  4996. {
  4997. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  4998. uint8_t *eld = connector->eld;
  4999. uint32_t eldv;
  5000. uint32_t i;
  5001. int len;
  5002. int hdmiw_hdmiedid;
  5003. int aud_cntl_st;
  5004. int aud_cntrl_st2;
  5005. if (IS_IVYBRIDGE(connector->dev)) {
  5006. hdmiw_hdmiedid = GEN7_HDMIW_HDMIEDID_A;
  5007. aud_cntl_st = GEN7_AUD_CNTRL_ST_A;
  5008. aud_cntrl_st2 = GEN7_AUD_CNTRL_ST2;
  5009. } else {
  5010. hdmiw_hdmiedid = GEN5_HDMIW_HDMIEDID_A;
  5011. aud_cntl_st = GEN5_AUD_CNTL_ST_A;
  5012. aud_cntrl_st2 = GEN5_AUD_CNTL_ST2;
  5013. }
  5014. i = to_intel_crtc(crtc)->pipe;
  5015. hdmiw_hdmiedid += i * 0x100;
  5016. aud_cntl_st += i * 0x100;
  5017. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(i));
  5018. i = I915_READ(aud_cntl_st);
  5019. i = (i >> 29) & 0x3; /* DIP_Port_Select, 0x1 = PortB */
  5020. if (!i) {
  5021. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  5022. /* operate blindly on all ports */
  5023. eldv = GEN5_ELD_VALIDB;
  5024. eldv |= GEN5_ELD_VALIDB << 4;
  5025. eldv |= GEN5_ELD_VALIDB << 8;
  5026. } else {
  5027. DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
  5028. eldv = GEN5_ELD_VALIDB << ((i - 1) * 4);
  5029. }
  5030. i = I915_READ(aud_cntrl_st2);
  5031. i &= ~eldv;
  5032. I915_WRITE(aud_cntrl_st2, i);
  5033. if (!eld[0])
  5034. return;
  5035. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  5036. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  5037. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  5038. }
  5039. i = I915_READ(aud_cntl_st);
  5040. i &= ~GEN5_ELD_ADDRESS;
  5041. I915_WRITE(aud_cntl_st, i);
  5042. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  5043. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5044. for (i = 0; i < len; i++)
  5045. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  5046. i = I915_READ(aud_cntrl_st2);
  5047. i |= eldv;
  5048. I915_WRITE(aud_cntrl_st2, i);
  5049. }
  5050. void intel_write_eld(struct drm_encoder *encoder,
  5051. struct drm_display_mode *mode)
  5052. {
  5053. struct drm_crtc *crtc = encoder->crtc;
  5054. struct drm_connector *connector;
  5055. struct drm_device *dev = encoder->dev;
  5056. struct drm_i915_private *dev_priv = dev->dev_private;
  5057. connector = drm_select_eld(encoder, mode);
  5058. if (!connector)
  5059. return;
  5060. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5061. connector->base.id,
  5062. drm_get_connector_name(connector),
  5063. connector->encoder->base.id,
  5064. drm_get_encoder_name(connector->encoder));
  5065. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  5066. if (dev_priv->display.write_eld)
  5067. dev_priv->display.write_eld(connector, crtc);
  5068. }
  5069. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  5070. void intel_crtc_load_lut(struct drm_crtc *crtc)
  5071. {
  5072. struct drm_device *dev = crtc->dev;
  5073. struct drm_i915_private *dev_priv = dev->dev_private;
  5074. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5075. int palreg = PALETTE(intel_crtc->pipe);
  5076. int i;
  5077. /* The clocks have to be on to load the palette. */
  5078. if (!crtc->enabled)
  5079. return;
  5080. /* use legacy palette for Ironlake */
  5081. if (HAS_PCH_SPLIT(dev))
  5082. palreg = LGC_PALETTE(intel_crtc->pipe);
  5083. for (i = 0; i < 256; i++) {
  5084. I915_WRITE(palreg + 4 * i,
  5085. (intel_crtc->lut_r[i] << 16) |
  5086. (intel_crtc->lut_g[i] << 8) |
  5087. intel_crtc->lut_b[i]);
  5088. }
  5089. }
  5090. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  5091. {
  5092. struct drm_device *dev = crtc->dev;
  5093. struct drm_i915_private *dev_priv = dev->dev_private;
  5094. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5095. bool visible = base != 0;
  5096. u32 cntl;
  5097. if (intel_crtc->cursor_visible == visible)
  5098. return;
  5099. cntl = I915_READ(_CURACNTR);
  5100. if (visible) {
  5101. /* On these chipsets we can only modify the base whilst
  5102. * the cursor is disabled.
  5103. */
  5104. I915_WRITE(_CURABASE, base);
  5105. cntl &= ~(CURSOR_FORMAT_MASK);
  5106. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  5107. cntl |= CURSOR_ENABLE |
  5108. CURSOR_GAMMA_ENABLE |
  5109. CURSOR_FORMAT_ARGB;
  5110. } else
  5111. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  5112. I915_WRITE(_CURACNTR, cntl);
  5113. intel_crtc->cursor_visible = visible;
  5114. }
  5115. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  5116. {
  5117. struct drm_device *dev = crtc->dev;
  5118. struct drm_i915_private *dev_priv = dev->dev_private;
  5119. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5120. int pipe = intel_crtc->pipe;
  5121. bool visible = base != 0;
  5122. if (intel_crtc->cursor_visible != visible) {
  5123. uint32_t cntl = I915_READ(CURCNTR(pipe));
  5124. if (base) {
  5125. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  5126. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  5127. cntl |= pipe << 28; /* Connect to correct pipe */
  5128. } else {
  5129. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  5130. cntl |= CURSOR_MODE_DISABLE;
  5131. }
  5132. I915_WRITE(CURCNTR(pipe), cntl);
  5133. intel_crtc->cursor_visible = visible;
  5134. }
  5135. /* and commit changes on next vblank */
  5136. I915_WRITE(CURBASE(pipe), base);
  5137. }
  5138. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  5139. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  5140. bool on)
  5141. {
  5142. struct drm_device *dev = crtc->dev;
  5143. struct drm_i915_private *dev_priv = dev->dev_private;
  5144. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5145. int pipe = intel_crtc->pipe;
  5146. int x = intel_crtc->cursor_x;
  5147. int y = intel_crtc->cursor_y;
  5148. u32 base, pos;
  5149. bool visible;
  5150. pos = 0;
  5151. if (on && crtc->enabled && crtc->fb) {
  5152. base = intel_crtc->cursor_addr;
  5153. if (x > (int) crtc->fb->width)
  5154. base = 0;
  5155. if (y > (int) crtc->fb->height)
  5156. base = 0;
  5157. } else
  5158. base = 0;
  5159. if (x < 0) {
  5160. if (x + intel_crtc->cursor_width < 0)
  5161. base = 0;
  5162. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  5163. x = -x;
  5164. }
  5165. pos |= x << CURSOR_X_SHIFT;
  5166. if (y < 0) {
  5167. if (y + intel_crtc->cursor_height < 0)
  5168. base = 0;
  5169. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  5170. y = -y;
  5171. }
  5172. pos |= y << CURSOR_Y_SHIFT;
  5173. visible = base != 0;
  5174. if (!visible && !intel_crtc->cursor_visible)
  5175. return;
  5176. I915_WRITE(CURPOS(pipe), pos);
  5177. if (IS_845G(dev) || IS_I865G(dev))
  5178. i845_update_cursor(crtc, base);
  5179. else
  5180. i9xx_update_cursor(crtc, base);
  5181. if (visible)
  5182. intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
  5183. }
  5184. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  5185. struct drm_file *file,
  5186. uint32_t handle,
  5187. uint32_t width, uint32_t height)
  5188. {
  5189. struct drm_device *dev = crtc->dev;
  5190. struct drm_i915_private *dev_priv = dev->dev_private;
  5191. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5192. struct drm_i915_gem_object *obj;
  5193. uint32_t addr;
  5194. int ret;
  5195. DRM_DEBUG_KMS("\n");
  5196. /* if we want to turn off the cursor ignore width and height */
  5197. if (!handle) {
  5198. DRM_DEBUG_KMS("cursor off\n");
  5199. addr = 0;
  5200. obj = NULL;
  5201. mutex_lock(&dev->struct_mutex);
  5202. goto finish;
  5203. }
  5204. /* Currently we only support 64x64 cursors */
  5205. if (width != 64 || height != 64) {
  5206. DRM_ERROR("we currently only support 64x64 cursors\n");
  5207. return -EINVAL;
  5208. }
  5209. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  5210. if (&obj->base == NULL)
  5211. return -ENOENT;
  5212. if (obj->base.size < width * height * 4) {
  5213. DRM_ERROR("buffer is to small\n");
  5214. ret = -ENOMEM;
  5215. goto fail;
  5216. }
  5217. /* we only need to pin inside GTT if cursor is non-phy */
  5218. mutex_lock(&dev->struct_mutex);
  5219. if (!dev_priv->info->cursor_needs_physical) {
  5220. if (obj->tiling_mode) {
  5221. DRM_ERROR("cursor cannot be tiled\n");
  5222. ret = -EINVAL;
  5223. goto fail_locked;
  5224. }
  5225. ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
  5226. if (ret) {
  5227. DRM_ERROR("failed to move cursor bo into the GTT\n");
  5228. goto fail_locked;
  5229. }
  5230. ret = i915_gem_object_put_fence(obj);
  5231. if (ret) {
  5232. DRM_ERROR("failed to release fence for cursor");
  5233. goto fail_unpin;
  5234. }
  5235. addr = obj->gtt_offset;
  5236. } else {
  5237. int align = IS_I830(dev) ? 16 * 1024 : 256;
  5238. ret = i915_gem_attach_phys_object(dev, obj,
  5239. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  5240. align);
  5241. if (ret) {
  5242. DRM_ERROR("failed to attach phys object\n");
  5243. goto fail_locked;
  5244. }
  5245. addr = obj->phys_obj->handle->busaddr;
  5246. }
  5247. if (IS_GEN2(dev))
  5248. I915_WRITE(CURSIZE, (height << 12) | width);
  5249. finish:
  5250. if (intel_crtc->cursor_bo) {
  5251. if (dev_priv->info->cursor_needs_physical) {
  5252. if (intel_crtc->cursor_bo != obj)
  5253. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  5254. } else
  5255. i915_gem_object_unpin(intel_crtc->cursor_bo);
  5256. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  5257. }
  5258. mutex_unlock(&dev->struct_mutex);
  5259. intel_crtc->cursor_addr = addr;
  5260. intel_crtc->cursor_bo = obj;
  5261. intel_crtc->cursor_width = width;
  5262. intel_crtc->cursor_height = height;
  5263. intel_crtc_update_cursor(crtc, true);
  5264. return 0;
  5265. fail_unpin:
  5266. i915_gem_object_unpin(obj);
  5267. fail_locked:
  5268. mutex_unlock(&dev->struct_mutex);
  5269. fail:
  5270. drm_gem_object_unreference_unlocked(&obj->base);
  5271. return ret;
  5272. }
  5273. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  5274. {
  5275. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5276. intel_crtc->cursor_x = x;
  5277. intel_crtc->cursor_y = y;
  5278. intel_crtc_update_cursor(crtc, true);
  5279. return 0;
  5280. }
  5281. /** Sets the color ramps on behalf of RandR */
  5282. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  5283. u16 blue, int regno)
  5284. {
  5285. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5286. intel_crtc->lut_r[regno] = red >> 8;
  5287. intel_crtc->lut_g[regno] = green >> 8;
  5288. intel_crtc->lut_b[regno] = blue >> 8;
  5289. }
  5290. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  5291. u16 *blue, int regno)
  5292. {
  5293. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5294. *red = intel_crtc->lut_r[regno] << 8;
  5295. *green = intel_crtc->lut_g[regno] << 8;
  5296. *blue = intel_crtc->lut_b[regno] << 8;
  5297. }
  5298. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  5299. u16 *blue, uint32_t start, uint32_t size)
  5300. {
  5301. int end = (start + size > 256) ? 256 : start + size, i;
  5302. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5303. for (i = start; i < end; i++) {
  5304. intel_crtc->lut_r[i] = red[i] >> 8;
  5305. intel_crtc->lut_g[i] = green[i] >> 8;
  5306. intel_crtc->lut_b[i] = blue[i] >> 8;
  5307. }
  5308. intel_crtc_load_lut(crtc);
  5309. }
  5310. /**
  5311. * Get a pipe with a simple mode set on it for doing load-based monitor
  5312. * detection.
  5313. *
  5314. * It will be up to the load-detect code to adjust the pipe as appropriate for
  5315. * its requirements. The pipe will be connected to no other encoders.
  5316. *
  5317. * Currently this code will only succeed if there is a pipe with no encoders
  5318. * configured for it. In the future, it could choose to temporarily disable
  5319. * some outputs to free up a pipe for its use.
  5320. *
  5321. * \return crtc, or NULL if no pipes are available.
  5322. */
  5323. /* VESA 640x480x72Hz mode to set on the pipe */
  5324. static struct drm_display_mode load_detect_mode = {
  5325. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  5326. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  5327. };
  5328. static struct drm_framebuffer *
  5329. intel_framebuffer_create(struct drm_device *dev,
  5330. struct drm_mode_fb_cmd *mode_cmd,
  5331. struct drm_i915_gem_object *obj)
  5332. {
  5333. struct intel_framebuffer *intel_fb;
  5334. int ret;
  5335. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5336. if (!intel_fb) {
  5337. drm_gem_object_unreference_unlocked(&obj->base);
  5338. return ERR_PTR(-ENOMEM);
  5339. }
  5340. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  5341. if (ret) {
  5342. drm_gem_object_unreference_unlocked(&obj->base);
  5343. kfree(intel_fb);
  5344. return ERR_PTR(ret);
  5345. }
  5346. return &intel_fb->base;
  5347. }
  5348. static u32
  5349. intel_framebuffer_pitch_for_width(int width, int bpp)
  5350. {
  5351. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  5352. return ALIGN(pitch, 64);
  5353. }
  5354. static u32
  5355. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  5356. {
  5357. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  5358. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  5359. }
  5360. static struct drm_framebuffer *
  5361. intel_framebuffer_create_for_mode(struct drm_device *dev,
  5362. struct drm_display_mode *mode,
  5363. int depth, int bpp)
  5364. {
  5365. struct drm_i915_gem_object *obj;
  5366. struct drm_mode_fb_cmd mode_cmd;
  5367. obj = i915_gem_alloc_object(dev,
  5368. intel_framebuffer_size_for_mode(mode, bpp));
  5369. if (obj == NULL)
  5370. return ERR_PTR(-ENOMEM);
  5371. mode_cmd.width = mode->hdisplay;
  5372. mode_cmd.height = mode->vdisplay;
  5373. mode_cmd.depth = depth;
  5374. mode_cmd.bpp = bpp;
  5375. mode_cmd.pitch = intel_framebuffer_pitch_for_width(mode_cmd.width, bpp);
  5376. return intel_framebuffer_create(dev, &mode_cmd, obj);
  5377. }
  5378. static struct drm_framebuffer *
  5379. mode_fits_in_fbdev(struct drm_device *dev,
  5380. struct drm_display_mode *mode)
  5381. {
  5382. struct drm_i915_private *dev_priv = dev->dev_private;
  5383. struct drm_i915_gem_object *obj;
  5384. struct drm_framebuffer *fb;
  5385. if (dev_priv->fbdev == NULL)
  5386. return NULL;
  5387. obj = dev_priv->fbdev->ifb.obj;
  5388. if (obj == NULL)
  5389. return NULL;
  5390. fb = &dev_priv->fbdev->ifb.base;
  5391. if (fb->pitch < intel_framebuffer_pitch_for_width(mode->hdisplay,
  5392. fb->bits_per_pixel))
  5393. return NULL;
  5394. if (obj->base.size < mode->vdisplay * fb->pitch)
  5395. return NULL;
  5396. return fb;
  5397. }
  5398. bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  5399. struct drm_connector *connector,
  5400. struct drm_display_mode *mode,
  5401. struct intel_load_detect_pipe *old)
  5402. {
  5403. struct intel_crtc *intel_crtc;
  5404. struct drm_crtc *possible_crtc;
  5405. struct drm_encoder *encoder = &intel_encoder->base;
  5406. struct drm_crtc *crtc = NULL;
  5407. struct drm_device *dev = encoder->dev;
  5408. struct drm_framebuffer *old_fb;
  5409. int i = -1;
  5410. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5411. connector->base.id, drm_get_connector_name(connector),
  5412. encoder->base.id, drm_get_encoder_name(encoder));
  5413. /*
  5414. * Algorithm gets a little messy:
  5415. *
  5416. * - if the connector already has an assigned crtc, use it (but make
  5417. * sure it's on first)
  5418. *
  5419. * - try to find the first unused crtc that can drive this connector,
  5420. * and use that if we find one
  5421. */
  5422. /* See if we already have a CRTC for this connector */
  5423. if (encoder->crtc) {
  5424. crtc = encoder->crtc;
  5425. intel_crtc = to_intel_crtc(crtc);
  5426. old->dpms_mode = intel_crtc->dpms_mode;
  5427. old->load_detect_temp = false;
  5428. /* Make sure the crtc and connector are running */
  5429. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  5430. struct drm_encoder_helper_funcs *encoder_funcs;
  5431. struct drm_crtc_helper_funcs *crtc_funcs;
  5432. crtc_funcs = crtc->helper_private;
  5433. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  5434. encoder_funcs = encoder->helper_private;
  5435. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  5436. }
  5437. return true;
  5438. }
  5439. /* Find an unused one (if possible) */
  5440. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  5441. i++;
  5442. if (!(encoder->possible_crtcs & (1 << i)))
  5443. continue;
  5444. if (!possible_crtc->enabled) {
  5445. crtc = possible_crtc;
  5446. break;
  5447. }
  5448. }
  5449. /*
  5450. * If we didn't find an unused CRTC, don't use any.
  5451. */
  5452. if (!crtc) {
  5453. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  5454. return false;
  5455. }
  5456. encoder->crtc = crtc;
  5457. connector->encoder = encoder;
  5458. intel_crtc = to_intel_crtc(crtc);
  5459. old->dpms_mode = intel_crtc->dpms_mode;
  5460. old->load_detect_temp = true;
  5461. old->release_fb = NULL;
  5462. if (!mode)
  5463. mode = &load_detect_mode;
  5464. old_fb = crtc->fb;
  5465. /* We need a framebuffer large enough to accommodate all accesses
  5466. * that the plane may generate whilst we perform load detection.
  5467. * We can not rely on the fbcon either being present (we get called
  5468. * during its initialisation to detect all boot displays, or it may
  5469. * not even exist) or that it is large enough to satisfy the
  5470. * requested mode.
  5471. */
  5472. crtc->fb = mode_fits_in_fbdev(dev, mode);
  5473. if (crtc->fb == NULL) {
  5474. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  5475. crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  5476. old->release_fb = crtc->fb;
  5477. } else
  5478. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  5479. if (IS_ERR(crtc->fb)) {
  5480. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  5481. crtc->fb = old_fb;
  5482. return false;
  5483. }
  5484. if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
  5485. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  5486. if (old->release_fb)
  5487. old->release_fb->funcs->destroy(old->release_fb);
  5488. crtc->fb = old_fb;
  5489. return false;
  5490. }
  5491. /* let the connector get through one full cycle before testing */
  5492. intel_wait_for_vblank(dev, intel_crtc->pipe);
  5493. return true;
  5494. }
  5495. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  5496. struct drm_connector *connector,
  5497. struct intel_load_detect_pipe *old)
  5498. {
  5499. struct drm_encoder *encoder = &intel_encoder->base;
  5500. struct drm_device *dev = encoder->dev;
  5501. struct drm_crtc *crtc = encoder->crtc;
  5502. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  5503. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  5504. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5505. connector->base.id, drm_get_connector_name(connector),
  5506. encoder->base.id, drm_get_encoder_name(encoder));
  5507. if (old->load_detect_temp) {
  5508. connector->encoder = NULL;
  5509. drm_helper_disable_unused_functions(dev);
  5510. if (old->release_fb)
  5511. old->release_fb->funcs->destroy(old->release_fb);
  5512. return;
  5513. }
  5514. /* Switch crtc and encoder back off if necessary */
  5515. if (old->dpms_mode != DRM_MODE_DPMS_ON) {
  5516. encoder_funcs->dpms(encoder, old->dpms_mode);
  5517. crtc_funcs->dpms(crtc, old->dpms_mode);
  5518. }
  5519. }
  5520. /* Returns the clock of the currently programmed mode of the given pipe. */
  5521. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  5522. {
  5523. struct drm_i915_private *dev_priv = dev->dev_private;
  5524. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5525. int pipe = intel_crtc->pipe;
  5526. u32 dpll = I915_READ(DPLL(pipe));
  5527. u32 fp;
  5528. intel_clock_t clock;
  5529. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  5530. fp = I915_READ(FP0(pipe));
  5531. else
  5532. fp = I915_READ(FP1(pipe));
  5533. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  5534. if (IS_PINEVIEW(dev)) {
  5535. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  5536. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  5537. } else {
  5538. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  5539. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  5540. }
  5541. if (!IS_GEN2(dev)) {
  5542. if (IS_PINEVIEW(dev))
  5543. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  5544. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  5545. else
  5546. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  5547. DPLL_FPA01_P1_POST_DIV_SHIFT);
  5548. switch (dpll & DPLL_MODE_MASK) {
  5549. case DPLLB_MODE_DAC_SERIAL:
  5550. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  5551. 5 : 10;
  5552. break;
  5553. case DPLLB_MODE_LVDS:
  5554. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  5555. 7 : 14;
  5556. break;
  5557. default:
  5558. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  5559. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  5560. return 0;
  5561. }
  5562. /* XXX: Handle the 100Mhz refclk */
  5563. intel_clock(dev, 96000, &clock);
  5564. } else {
  5565. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  5566. if (is_lvds) {
  5567. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  5568. DPLL_FPA01_P1_POST_DIV_SHIFT);
  5569. clock.p2 = 14;
  5570. if ((dpll & PLL_REF_INPUT_MASK) ==
  5571. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  5572. /* XXX: might not be 66MHz */
  5573. intel_clock(dev, 66000, &clock);
  5574. } else
  5575. intel_clock(dev, 48000, &clock);
  5576. } else {
  5577. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  5578. clock.p1 = 2;
  5579. else {
  5580. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  5581. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  5582. }
  5583. if (dpll & PLL_P2_DIVIDE_BY_4)
  5584. clock.p2 = 4;
  5585. else
  5586. clock.p2 = 2;
  5587. intel_clock(dev, 48000, &clock);
  5588. }
  5589. }
  5590. /* XXX: It would be nice to validate the clocks, but we can't reuse
  5591. * i830PllIsValid() because it relies on the xf86_config connector
  5592. * configuration being accurate, which it isn't necessarily.
  5593. */
  5594. return clock.dot;
  5595. }
  5596. /** Returns the currently programmed mode of the given pipe. */
  5597. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  5598. struct drm_crtc *crtc)
  5599. {
  5600. struct drm_i915_private *dev_priv = dev->dev_private;
  5601. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5602. int pipe = intel_crtc->pipe;
  5603. struct drm_display_mode *mode;
  5604. int htot = I915_READ(HTOTAL(pipe));
  5605. int hsync = I915_READ(HSYNC(pipe));
  5606. int vtot = I915_READ(VTOTAL(pipe));
  5607. int vsync = I915_READ(VSYNC(pipe));
  5608. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  5609. if (!mode)
  5610. return NULL;
  5611. mode->clock = intel_crtc_clock_get(dev, crtc);
  5612. mode->hdisplay = (htot & 0xffff) + 1;
  5613. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  5614. mode->hsync_start = (hsync & 0xffff) + 1;
  5615. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  5616. mode->vdisplay = (vtot & 0xffff) + 1;
  5617. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  5618. mode->vsync_start = (vsync & 0xffff) + 1;
  5619. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  5620. drm_mode_set_name(mode);
  5621. drm_mode_set_crtcinfo(mode, 0);
  5622. return mode;
  5623. }
  5624. #define GPU_IDLE_TIMEOUT 500 /* ms */
  5625. /* When this timer fires, we've been idle for awhile */
  5626. static void intel_gpu_idle_timer(unsigned long arg)
  5627. {
  5628. struct drm_device *dev = (struct drm_device *)arg;
  5629. drm_i915_private_t *dev_priv = dev->dev_private;
  5630. if (!list_empty(&dev_priv->mm.active_list)) {
  5631. /* Still processing requests, so just re-arm the timer. */
  5632. mod_timer(&dev_priv->idle_timer, jiffies +
  5633. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  5634. return;
  5635. }
  5636. dev_priv->busy = false;
  5637. queue_work(dev_priv->wq, &dev_priv->idle_work);
  5638. }
  5639. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  5640. static void intel_crtc_idle_timer(unsigned long arg)
  5641. {
  5642. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  5643. struct drm_crtc *crtc = &intel_crtc->base;
  5644. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  5645. struct intel_framebuffer *intel_fb;
  5646. intel_fb = to_intel_framebuffer(crtc->fb);
  5647. if (intel_fb && intel_fb->obj->active) {
  5648. /* The framebuffer is still being accessed by the GPU. */
  5649. mod_timer(&intel_crtc->idle_timer, jiffies +
  5650. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  5651. return;
  5652. }
  5653. intel_crtc->busy = false;
  5654. queue_work(dev_priv->wq, &dev_priv->idle_work);
  5655. }
  5656. static void intel_increase_pllclock(struct drm_crtc *crtc)
  5657. {
  5658. struct drm_device *dev = crtc->dev;
  5659. drm_i915_private_t *dev_priv = dev->dev_private;
  5660. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5661. int pipe = intel_crtc->pipe;
  5662. int dpll_reg = DPLL(pipe);
  5663. int dpll;
  5664. if (HAS_PCH_SPLIT(dev))
  5665. return;
  5666. if (!dev_priv->lvds_downclock_avail)
  5667. return;
  5668. dpll = I915_READ(dpll_reg);
  5669. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  5670. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  5671. /* Unlock panel regs */
  5672. I915_WRITE(PP_CONTROL,
  5673. I915_READ(PP_CONTROL) | PANEL_UNLOCK_REGS);
  5674. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  5675. I915_WRITE(dpll_reg, dpll);
  5676. intel_wait_for_vblank(dev, pipe);
  5677. dpll = I915_READ(dpll_reg);
  5678. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  5679. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  5680. /* ...and lock them again */
  5681. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  5682. }
  5683. /* Schedule downclock */
  5684. mod_timer(&intel_crtc->idle_timer, jiffies +
  5685. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  5686. }
  5687. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  5688. {
  5689. struct drm_device *dev = crtc->dev;
  5690. drm_i915_private_t *dev_priv = dev->dev_private;
  5691. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5692. int pipe = intel_crtc->pipe;
  5693. int dpll_reg = DPLL(pipe);
  5694. int dpll = I915_READ(dpll_reg);
  5695. if (HAS_PCH_SPLIT(dev))
  5696. return;
  5697. if (!dev_priv->lvds_downclock_avail)
  5698. return;
  5699. /*
  5700. * Since this is called by a timer, we should never get here in
  5701. * the manual case.
  5702. */
  5703. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  5704. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  5705. /* Unlock panel regs */
  5706. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  5707. PANEL_UNLOCK_REGS);
  5708. dpll |= DISPLAY_RATE_SELECT_FPA1;
  5709. I915_WRITE(dpll_reg, dpll);
  5710. intel_wait_for_vblank(dev, pipe);
  5711. dpll = I915_READ(dpll_reg);
  5712. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  5713. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  5714. /* ...and lock them again */
  5715. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  5716. }
  5717. }
  5718. /**
  5719. * intel_idle_update - adjust clocks for idleness
  5720. * @work: work struct
  5721. *
  5722. * Either the GPU or display (or both) went idle. Check the busy status
  5723. * here and adjust the CRTC and GPU clocks as necessary.
  5724. */
  5725. static void intel_idle_update(struct work_struct *work)
  5726. {
  5727. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  5728. idle_work);
  5729. struct drm_device *dev = dev_priv->dev;
  5730. struct drm_crtc *crtc;
  5731. struct intel_crtc *intel_crtc;
  5732. if (!i915_powersave)
  5733. return;
  5734. mutex_lock(&dev->struct_mutex);
  5735. i915_update_gfx_val(dev_priv);
  5736. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5737. /* Skip inactive CRTCs */
  5738. if (!crtc->fb)
  5739. continue;
  5740. intel_crtc = to_intel_crtc(crtc);
  5741. if (!intel_crtc->busy)
  5742. intel_decrease_pllclock(crtc);
  5743. }
  5744. mutex_unlock(&dev->struct_mutex);
  5745. }
  5746. /**
  5747. * intel_mark_busy - mark the GPU and possibly the display busy
  5748. * @dev: drm device
  5749. * @obj: object we're operating on
  5750. *
  5751. * Callers can use this function to indicate that the GPU is busy processing
  5752. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  5753. * buffer), we'll also mark the display as busy, so we know to increase its
  5754. * clock frequency.
  5755. */
  5756. void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
  5757. {
  5758. drm_i915_private_t *dev_priv = dev->dev_private;
  5759. struct drm_crtc *crtc = NULL;
  5760. struct intel_framebuffer *intel_fb;
  5761. struct intel_crtc *intel_crtc;
  5762. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  5763. return;
  5764. if (!dev_priv->busy)
  5765. dev_priv->busy = true;
  5766. else
  5767. mod_timer(&dev_priv->idle_timer, jiffies +
  5768. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  5769. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5770. if (!crtc->fb)
  5771. continue;
  5772. intel_crtc = to_intel_crtc(crtc);
  5773. intel_fb = to_intel_framebuffer(crtc->fb);
  5774. if (intel_fb->obj == obj) {
  5775. if (!intel_crtc->busy) {
  5776. /* Non-busy -> busy, upclock */
  5777. intel_increase_pllclock(crtc);
  5778. intel_crtc->busy = true;
  5779. } else {
  5780. /* Busy -> busy, put off timer */
  5781. mod_timer(&intel_crtc->idle_timer, jiffies +
  5782. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  5783. }
  5784. }
  5785. }
  5786. }
  5787. static void intel_crtc_destroy(struct drm_crtc *crtc)
  5788. {
  5789. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5790. struct drm_device *dev = crtc->dev;
  5791. struct intel_unpin_work *work;
  5792. unsigned long flags;
  5793. spin_lock_irqsave(&dev->event_lock, flags);
  5794. work = intel_crtc->unpin_work;
  5795. intel_crtc->unpin_work = NULL;
  5796. spin_unlock_irqrestore(&dev->event_lock, flags);
  5797. if (work) {
  5798. cancel_work_sync(&work->work);
  5799. kfree(work);
  5800. }
  5801. drm_crtc_cleanup(crtc);
  5802. kfree(intel_crtc);
  5803. }
  5804. static void intel_unpin_work_fn(struct work_struct *__work)
  5805. {
  5806. struct intel_unpin_work *work =
  5807. container_of(__work, struct intel_unpin_work, work);
  5808. mutex_lock(&work->dev->struct_mutex);
  5809. i915_gem_object_unpin(work->old_fb_obj);
  5810. drm_gem_object_unreference(&work->pending_flip_obj->base);
  5811. drm_gem_object_unreference(&work->old_fb_obj->base);
  5812. intel_update_fbc(work->dev);
  5813. mutex_unlock(&work->dev->struct_mutex);
  5814. kfree(work);
  5815. }
  5816. static void do_intel_finish_page_flip(struct drm_device *dev,
  5817. struct drm_crtc *crtc)
  5818. {
  5819. drm_i915_private_t *dev_priv = dev->dev_private;
  5820. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5821. struct intel_unpin_work *work;
  5822. struct drm_i915_gem_object *obj;
  5823. struct drm_pending_vblank_event *e;
  5824. struct timeval tnow, tvbl;
  5825. unsigned long flags;
  5826. /* Ignore early vblank irqs */
  5827. if (intel_crtc == NULL)
  5828. return;
  5829. do_gettimeofday(&tnow);
  5830. spin_lock_irqsave(&dev->event_lock, flags);
  5831. work = intel_crtc->unpin_work;
  5832. if (work == NULL || !work->pending) {
  5833. spin_unlock_irqrestore(&dev->event_lock, flags);
  5834. return;
  5835. }
  5836. intel_crtc->unpin_work = NULL;
  5837. if (work->event) {
  5838. e = work->event;
  5839. e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
  5840. /* Called before vblank count and timestamps have
  5841. * been updated for the vblank interval of flip
  5842. * completion? Need to increment vblank count and
  5843. * add one videorefresh duration to returned timestamp
  5844. * to account for this. We assume this happened if we
  5845. * get called over 0.9 frame durations after the last
  5846. * timestamped vblank.
  5847. *
  5848. * This calculation can not be used with vrefresh rates
  5849. * below 5Hz (10Hz to be on the safe side) without
  5850. * promoting to 64 integers.
  5851. */
  5852. if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
  5853. 9 * crtc->framedur_ns) {
  5854. e->event.sequence++;
  5855. tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
  5856. crtc->framedur_ns);
  5857. }
  5858. e->event.tv_sec = tvbl.tv_sec;
  5859. e->event.tv_usec = tvbl.tv_usec;
  5860. list_add_tail(&e->base.link,
  5861. &e->base.file_priv->event_list);
  5862. wake_up_interruptible(&e->base.file_priv->event_wait);
  5863. }
  5864. drm_vblank_put(dev, intel_crtc->pipe);
  5865. spin_unlock_irqrestore(&dev->event_lock, flags);
  5866. obj = work->old_fb_obj;
  5867. atomic_clear_mask(1 << intel_crtc->plane,
  5868. &obj->pending_flip.counter);
  5869. if (atomic_read(&obj->pending_flip) == 0)
  5870. wake_up(&dev_priv->pending_flip_queue);
  5871. schedule_work(&work->work);
  5872. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  5873. }
  5874. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  5875. {
  5876. drm_i915_private_t *dev_priv = dev->dev_private;
  5877. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  5878. do_intel_finish_page_flip(dev, crtc);
  5879. }
  5880. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  5881. {
  5882. drm_i915_private_t *dev_priv = dev->dev_private;
  5883. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  5884. do_intel_finish_page_flip(dev, crtc);
  5885. }
  5886. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  5887. {
  5888. drm_i915_private_t *dev_priv = dev->dev_private;
  5889. struct intel_crtc *intel_crtc =
  5890. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  5891. unsigned long flags;
  5892. spin_lock_irqsave(&dev->event_lock, flags);
  5893. if (intel_crtc->unpin_work) {
  5894. if ((++intel_crtc->unpin_work->pending) > 1)
  5895. DRM_ERROR("Prepared flip multiple times\n");
  5896. } else {
  5897. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  5898. }
  5899. spin_unlock_irqrestore(&dev->event_lock, flags);
  5900. }
  5901. static int intel_gen2_queue_flip(struct drm_device *dev,
  5902. struct drm_crtc *crtc,
  5903. struct drm_framebuffer *fb,
  5904. struct drm_i915_gem_object *obj)
  5905. {
  5906. struct drm_i915_private *dev_priv = dev->dev_private;
  5907. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5908. unsigned long offset;
  5909. u32 flip_mask;
  5910. int ret;
  5911. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  5912. if (ret)
  5913. goto out;
  5914. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  5915. offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
  5916. ret = BEGIN_LP_RING(6);
  5917. if (ret)
  5918. goto out;
  5919. /* Can't queue multiple flips, so wait for the previous
  5920. * one to finish before executing the next.
  5921. */
  5922. if (intel_crtc->plane)
  5923. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  5924. else
  5925. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  5926. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  5927. OUT_RING(MI_NOOP);
  5928. OUT_RING(MI_DISPLAY_FLIP |
  5929. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5930. OUT_RING(fb->pitch);
  5931. OUT_RING(obj->gtt_offset + offset);
  5932. OUT_RING(MI_NOOP);
  5933. ADVANCE_LP_RING();
  5934. out:
  5935. return ret;
  5936. }
  5937. static int intel_gen3_queue_flip(struct drm_device *dev,
  5938. struct drm_crtc *crtc,
  5939. struct drm_framebuffer *fb,
  5940. struct drm_i915_gem_object *obj)
  5941. {
  5942. struct drm_i915_private *dev_priv = dev->dev_private;
  5943. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5944. unsigned long offset;
  5945. u32 flip_mask;
  5946. int ret;
  5947. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  5948. if (ret)
  5949. goto out;
  5950. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  5951. offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
  5952. ret = BEGIN_LP_RING(6);
  5953. if (ret)
  5954. goto out;
  5955. if (intel_crtc->plane)
  5956. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  5957. else
  5958. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  5959. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  5960. OUT_RING(MI_NOOP);
  5961. OUT_RING(MI_DISPLAY_FLIP_I915 |
  5962. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5963. OUT_RING(fb->pitch);
  5964. OUT_RING(obj->gtt_offset + offset);
  5965. OUT_RING(MI_NOOP);
  5966. ADVANCE_LP_RING();
  5967. out:
  5968. return ret;
  5969. }
  5970. static int intel_gen4_queue_flip(struct drm_device *dev,
  5971. struct drm_crtc *crtc,
  5972. struct drm_framebuffer *fb,
  5973. struct drm_i915_gem_object *obj)
  5974. {
  5975. struct drm_i915_private *dev_priv = dev->dev_private;
  5976. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5977. uint32_t pf, pipesrc;
  5978. int ret;
  5979. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  5980. if (ret)
  5981. goto out;
  5982. ret = BEGIN_LP_RING(4);
  5983. if (ret)
  5984. goto out;
  5985. /* i965+ uses the linear or tiled offsets from the
  5986. * Display Registers (which do not change across a page-flip)
  5987. * so we need only reprogram the base address.
  5988. */
  5989. OUT_RING(MI_DISPLAY_FLIP |
  5990. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5991. OUT_RING(fb->pitch);
  5992. OUT_RING(obj->gtt_offset | obj->tiling_mode);
  5993. /* XXX Enabling the panel-fitter across page-flip is so far
  5994. * untested on non-native modes, so ignore it for now.
  5995. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  5996. */
  5997. pf = 0;
  5998. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  5999. OUT_RING(pf | pipesrc);
  6000. ADVANCE_LP_RING();
  6001. out:
  6002. return ret;
  6003. }
  6004. static int intel_gen6_queue_flip(struct drm_device *dev,
  6005. struct drm_crtc *crtc,
  6006. struct drm_framebuffer *fb,
  6007. struct drm_i915_gem_object *obj)
  6008. {
  6009. struct drm_i915_private *dev_priv = dev->dev_private;
  6010. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6011. uint32_t pf, pipesrc;
  6012. int ret;
  6013. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  6014. if (ret)
  6015. goto out;
  6016. ret = BEGIN_LP_RING(4);
  6017. if (ret)
  6018. goto out;
  6019. OUT_RING(MI_DISPLAY_FLIP |
  6020. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6021. OUT_RING(fb->pitch | obj->tiling_mode);
  6022. OUT_RING(obj->gtt_offset);
  6023. pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  6024. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  6025. OUT_RING(pf | pipesrc);
  6026. ADVANCE_LP_RING();
  6027. out:
  6028. return ret;
  6029. }
  6030. /*
  6031. * On gen7 we currently use the blit ring because (in early silicon at least)
  6032. * the render ring doesn't give us interrpts for page flip completion, which
  6033. * means clients will hang after the first flip is queued. Fortunately the
  6034. * blit ring generates interrupts properly, so use it instead.
  6035. */
  6036. static int intel_gen7_queue_flip(struct drm_device *dev,
  6037. struct drm_crtc *crtc,
  6038. struct drm_framebuffer *fb,
  6039. struct drm_i915_gem_object *obj)
  6040. {
  6041. struct drm_i915_private *dev_priv = dev->dev_private;
  6042. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6043. struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
  6044. int ret;
  6045. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6046. if (ret)
  6047. goto out;
  6048. ret = intel_ring_begin(ring, 4);
  6049. if (ret)
  6050. goto out;
  6051. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));
  6052. intel_ring_emit(ring, (fb->pitch | obj->tiling_mode));
  6053. intel_ring_emit(ring, (obj->gtt_offset));
  6054. intel_ring_emit(ring, (MI_NOOP));
  6055. intel_ring_advance(ring);
  6056. out:
  6057. return ret;
  6058. }
  6059. static int intel_default_queue_flip(struct drm_device *dev,
  6060. struct drm_crtc *crtc,
  6061. struct drm_framebuffer *fb,
  6062. struct drm_i915_gem_object *obj)
  6063. {
  6064. return -ENODEV;
  6065. }
  6066. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  6067. struct drm_framebuffer *fb,
  6068. struct drm_pending_vblank_event *event)
  6069. {
  6070. struct drm_device *dev = crtc->dev;
  6071. struct drm_i915_private *dev_priv = dev->dev_private;
  6072. struct intel_framebuffer *intel_fb;
  6073. struct drm_i915_gem_object *obj;
  6074. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6075. struct intel_unpin_work *work;
  6076. unsigned long flags;
  6077. int ret;
  6078. work = kzalloc(sizeof *work, GFP_KERNEL);
  6079. if (work == NULL)
  6080. return -ENOMEM;
  6081. work->event = event;
  6082. work->dev = crtc->dev;
  6083. intel_fb = to_intel_framebuffer(crtc->fb);
  6084. work->old_fb_obj = intel_fb->obj;
  6085. INIT_WORK(&work->work, intel_unpin_work_fn);
  6086. /* We borrow the event spin lock for protecting unpin_work */
  6087. spin_lock_irqsave(&dev->event_lock, flags);
  6088. if (intel_crtc->unpin_work) {
  6089. spin_unlock_irqrestore(&dev->event_lock, flags);
  6090. kfree(work);
  6091. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  6092. return -EBUSY;
  6093. }
  6094. intel_crtc->unpin_work = work;
  6095. spin_unlock_irqrestore(&dev->event_lock, flags);
  6096. intel_fb = to_intel_framebuffer(fb);
  6097. obj = intel_fb->obj;
  6098. mutex_lock(&dev->struct_mutex);
  6099. /* Reference the objects for the scheduled work. */
  6100. drm_gem_object_reference(&work->old_fb_obj->base);
  6101. drm_gem_object_reference(&obj->base);
  6102. crtc->fb = fb;
  6103. ret = drm_vblank_get(dev, intel_crtc->pipe);
  6104. if (ret)
  6105. goto cleanup_objs;
  6106. work->pending_flip_obj = obj;
  6107. work->enable_stall_check = true;
  6108. /* Block clients from rendering to the new back buffer until
  6109. * the flip occurs and the object is no longer visible.
  6110. */
  6111. atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  6112. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
  6113. if (ret)
  6114. goto cleanup_pending;
  6115. intel_disable_fbc(dev);
  6116. mutex_unlock(&dev->struct_mutex);
  6117. trace_i915_flip_request(intel_crtc->plane, obj);
  6118. return 0;
  6119. cleanup_pending:
  6120. atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  6121. cleanup_objs:
  6122. drm_gem_object_unreference(&work->old_fb_obj->base);
  6123. drm_gem_object_unreference(&obj->base);
  6124. mutex_unlock(&dev->struct_mutex);
  6125. spin_lock_irqsave(&dev->event_lock, flags);
  6126. intel_crtc->unpin_work = NULL;
  6127. spin_unlock_irqrestore(&dev->event_lock, flags);
  6128. kfree(work);
  6129. return ret;
  6130. }
  6131. static void intel_sanitize_modesetting(struct drm_device *dev,
  6132. int pipe, int plane)
  6133. {
  6134. struct drm_i915_private *dev_priv = dev->dev_private;
  6135. u32 reg, val;
  6136. if (HAS_PCH_SPLIT(dev))
  6137. return;
  6138. /* Who knows what state these registers were left in by the BIOS or
  6139. * grub?
  6140. *
  6141. * If we leave the registers in a conflicting state (e.g. with the
  6142. * display plane reading from the other pipe than the one we intend
  6143. * to use) then when we attempt to teardown the active mode, we will
  6144. * not disable the pipes and planes in the correct order -- leaving
  6145. * a plane reading from a disabled pipe and possibly leading to
  6146. * undefined behaviour.
  6147. */
  6148. reg = DSPCNTR(plane);
  6149. val = I915_READ(reg);
  6150. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  6151. return;
  6152. if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
  6153. return;
  6154. /* This display plane is active and attached to the other CPU pipe. */
  6155. pipe = !pipe;
  6156. /* Disable the plane and wait for it to stop reading from the pipe. */
  6157. intel_disable_plane(dev_priv, plane, pipe);
  6158. intel_disable_pipe(dev_priv, pipe);
  6159. }
  6160. static void intel_crtc_reset(struct drm_crtc *crtc)
  6161. {
  6162. struct drm_device *dev = crtc->dev;
  6163. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6164. /* Reset flags back to the 'unknown' status so that they
  6165. * will be correctly set on the initial modeset.
  6166. */
  6167. intel_crtc->dpms_mode = -1;
  6168. /* We need to fix up any BIOS configuration that conflicts with
  6169. * our expectations.
  6170. */
  6171. intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
  6172. }
  6173. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  6174. .dpms = intel_crtc_dpms,
  6175. .mode_fixup = intel_crtc_mode_fixup,
  6176. .mode_set = intel_crtc_mode_set,
  6177. .mode_set_base = intel_pipe_set_base,
  6178. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  6179. .load_lut = intel_crtc_load_lut,
  6180. .disable = intel_crtc_disable,
  6181. };
  6182. static const struct drm_crtc_funcs intel_crtc_funcs = {
  6183. .reset = intel_crtc_reset,
  6184. .cursor_set = intel_crtc_cursor_set,
  6185. .cursor_move = intel_crtc_cursor_move,
  6186. .gamma_set = intel_crtc_gamma_set,
  6187. .set_config = drm_crtc_helper_set_config,
  6188. .destroy = intel_crtc_destroy,
  6189. .page_flip = intel_crtc_page_flip,
  6190. };
  6191. static void intel_crtc_init(struct drm_device *dev, int pipe)
  6192. {
  6193. drm_i915_private_t *dev_priv = dev->dev_private;
  6194. struct intel_crtc *intel_crtc;
  6195. int i;
  6196. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  6197. if (intel_crtc == NULL)
  6198. return;
  6199. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  6200. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  6201. for (i = 0; i < 256; i++) {
  6202. intel_crtc->lut_r[i] = i;
  6203. intel_crtc->lut_g[i] = i;
  6204. intel_crtc->lut_b[i] = i;
  6205. }
  6206. /* Swap pipes & planes for FBC on pre-965 */
  6207. intel_crtc->pipe = pipe;
  6208. intel_crtc->plane = pipe;
  6209. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  6210. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  6211. intel_crtc->plane = !pipe;
  6212. }
  6213. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  6214. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  6215. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  6216. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  6217. intel_crtc_reset(&intel_crtc->base);
  6218. intel_crtc->active = true; /* force the pipe off on setup_init_config */
  6219. intel_crtc->bpp = 24; /* default for pre-Ironlake */
  6220. if (HAS_PCH_SPLIT(dev)) {
  6221. intel_helper_funcs.prepare = ironlake_crtc_prepare;
  6222. intel_helper_funcs.commit = ironlake_crtc_commit;
  6223. } else {
  6224. intel_helper_funcs.prepare = i9xx_crtc_prepare;
  6225. intel_helper_funcs.commit = i9xx_crtc_commit;
  6226. }
  6227. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  6228. intel_crtc->busy = false;
  6229. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  6230. (unsigned long)intel_crtc);
  6231. }
  6232. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  6233. struct drm_file *file)
  6234. {
  6235. drm_i915_private_t *dev_priv = dev->dev_private;
  6236. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  6237. struct drm_mode_object *drmmode_obj;
  6238. struct intel_crtc *crtc;
  6239. if (!dev_priv) {
  6240. DRM_ERROR("called with no initialization\n");
  6241. return -EINVAL;
  6242. }
  6243. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  6244. DRM_MODE_OBJECT_CRTC);
  6245. if (!drmmode_obj) {
  6246. DRM_ERROR("no such CRTC id\n");
  6247. return -EINVAL;
  6248. }
  6249. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  6250. pipe_from_crtc_id->pipe = crtc->pipe;
  6251. return 0;
  6252. }
  6253. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  6254. {
  6255. struct intel_encoder *encoder;
  6256. int index_mask = 0;
  6257. int entry = 0;
  6258. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  6259. if (type_mask & encoder->clone_mask)
  6260. index_mask |= (1 << entry);
  6261. entry++;
  6262. }
  6263. return index_mask;
  6264. }
  6265. static bool has_edp_a(struct drm_device *dev)
  6266. {
  6267. struct drm_i915_private *dev_priv = dev->dev_private;
  6268. if (!IS_MOBILE(dev))
  6269. return false;
  6270. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  6271. return false;
  6272. if (IS_GEN5(dev) &&
  6273. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  6274. return false;
  6275. return true;
  6276. }
  6277. static void intel_setup_outputs(struct drm_device *dev)
  6278. {
  6279. struct drm_i915_private *dev_priv = dev->dev_private;
  6280. struct intel_encoder *encoder;
  6281. bool dpd_is_edp = false;
  6282. bool has_lvds = false;
  6283. if (IS_MOBILE(dev) && !IS_I830(dev))
  6284. has_lvds = intel_lvds_init(dev);
  6285. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  6286. /* disable the panel fitter on everything but LVDS */
  6287. I915_WRITE(PFIT_CONTROL, 0);
  6288. }
  6289. if (HAS_PCH_SPLIT(dev)) {
  6290. dpd_is_edp = intel_dpd_is_edp(dev);
  6291. if (has_edp_a(dev))
  6292. intel_dp_init(dev, DP_A);
  6293. if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  6294. intel_dp_init(dev, PCH_DP_D);
  6295. }
  6296. intel_crt_init(dev);
  6297. if (HAS_PCH_SPLIT(dev)) {
  6298. int found;
  6299. if (I915_READ(HDMIB) & PORT_DETECTED) {
  6300. /* PCH SDVOB multiplex with HDMIB */
  6301. found = intel_sdvo_init(dev, PCH_SDVOB);
  6302. if (!found)
  6303. intel_hdmi_init(dev, HDMIB);
  6304. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  6305. intel_dp_init(dev, PCH_DP_B);
  6306. }
  6307. if (I915_READ(HDMIC) & PORT_DETECTED)
  6308. intel_hdmi_init(dev, HDMIC);
  6309. if (I915_READ(HDMID) & PORT_DETECTED)
  6310. intel_hdmi_init(dev, HDMID);
  6311. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  6312. intel_dp_init(dev, PCH_DP_C);
  6313. if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  6314. intel_dp_init(dev, PCH_DP_D);
  6315. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  6316. bool found = false;
  6317. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  6318. DRM_DEBUG_KMS("probing SDVOB\n");
  6319. found = intel_sdvo_init(dev, SDVOB);
  6320. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  6321. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  6322. intel_hdmi_init(dev, SDVOB);
  6323. }
  6324. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  6325. DRM_DEBUG_KMS("probing DP_B\n");
  6326. intel_dp_init(dev, DP_B);
  6327. }
  6328. }
  6329. /* Before G4X SDVOC doesn't have its own detect register */
  6330. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  6331. DRM_DEBUG_KMS("probing SDVOC\n");
  6332. found = intel_sdvo_init(dev, SDVOC);
  6333. }
  6334. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  6335. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  6336. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  6337. intel_hdmi_init(dev, SDVOC);
  6338. }
  6339. if (SUPPORTS_INTEGRATED_DP(dev)) {
  6340. DRM_DEBUG_KMS("probing DP_C\n");
  6341. intel_dp_init(dev, DP_C);
  6342. }
  6343. }
  6344. if (SUPPORTS_INTEGRATED_DP(dev) &&
  6345. (I915_READ(DP_D) & DP_DETECTED)) {
  6346. DRM_DEBUG_KMS("probing DP_D\n");
  6347. intel_dp_init(dev, DP_D);
  6348. }
  6349. } else if (IS_GEN2(dev))
  6350. intel_dvo_init(dev);
  6351. if (SUPPORTS_TV(dev))
  6352. intel_tv_init(dev);
  6353. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  6354. encoder->base.possible_crtcs = encoder->crtc_mask;
  6355. encoder->base.possible_clones =
  6356. intel_encoder_clones(dev, encoder->clone_mask);
  6357. }
  6358. /* disable all the possible outputs/crtcs before entering KMS mode */
  6359. drm_helper_disable_unused_functions(dev);
  6360. if (HAS_PCH_SPLIT(dev))
  6361. ironlake_init_pch_refclk(dev);
  6362. }
  6363. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  6364. {
  6365. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  6366. drm_framebuffer_cleanup(fb);
  6367. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  6368. kfree(intel_fb);
  6369. }
  6370. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  6371. struct drm_file *file,
  6372. unsigned int *handle)
  6373. {
  6374. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  6375. struct drm_i915_gem_object *obj = intel_fb->obj;
  6376. return drm_gem_handle_create(file, &obj->base, handle);
  6377. }
  6378. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  6379. .destroy = intel_user_framebuffer_destroy,
  6380. .create_handle = intel_user_framebuffer_create_handle,
  6381. };
  6382. int intel_framebuffer_init(struct drm_device *dev,
  6383. struct intel_framebuffer *intel_fb,
  6384. struct drm_mode_fb_cmd *mode_cmd,
  6385. struct drm_i915_gem_object *obj)
  6386. {
  6387. int ret;
  6388. if (obj->tiling_mode == I915_TILING_Y)
  6389. return -EINVAL;
  6390. if (mode_cmd->pitch & 63)
  6391. return -EINVAL;
  6392. switch (mode_cmd->bpp) {
  6393. case 8:
  6394. case 16:
  6395. /* Only pre-ILK can handle 5:5:5 */
  6396. if (mode_cmd->depth == 15 && !HAS_PCH_SPLIT(dev))
  6397. return -EINVAL;
  6398. break;
  6399. case 24:
  6400. case 32:
  6401. break;
  6402. default:
  6403. return -EINVAL;
  6404. }
  6405. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  6406. if (ret) {
  6407. DRM_ERROR("framebuffer init failed %d\n", ret);
  6408. return ret;
  6409. }
  6410. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  6411. intel_fb->obj = obj;
  6412. return 0;
  6413. }
  6414. static struct drm_framebuffer *
  6415. intel_user_framebuffer_create(struct drm_device *dev,
  6416. struct drm_file *filp,
  6417. struct drm_mode_fb_cmd *mode_cmd)
  6418. {
  6419. struct drm_i915_gem_object *obj;
  6420. obj = to_intel_bo(drm_gem_object_lookup(dev, filp, mode_cmd->handle));
  6421. if (&obj->base == NULL)
  6422. return ERR_PTR(-ENOENT);
  6423. return intel_framebuffer_create(dev, mode_cmd, obj);
  6424. }
  6425. static const struct drm_mode_config_funcs intel_mode_funcs = {
  6426. .fb_create = intel_user_framebuffer_create,
  6427. .output_poll_changed = intel_fb_output_poll_changed,
  6428. };
  6429. static struct drm_i915_gem_object *
  6430. intel_alloc_context_page(struct drm_device *dev)
  6431. {
  6432. struct drm_i915_gem_object *ctx;
  6433. int ret;
  6434. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  6435. ctx = i915_gem_alloc_object(dev, 4096);
  6436. if (!ctx) {
  6437. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  6438. return NULL;
  6439. }
  6440. ret = i915_gem_object_pin(ctx, 4096, true);
  6441. if (ret) {
  6442. DRM_ERROR("failed to pin power context: %d\n", ret);
  6443. goto err_unref;
  6444. }
  6445. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  6446. if (ret) {
  6447. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  6448. goto err_unpin;
  6449. }
  6450. return ctx;
  6451. err_unpin:
  6452. i915_gem_object_unpin(ctx);
  6453. err_unref:
  6454. drm_gem_object_unreference(&ctx->base);
  6455. mutex_unlock(&dev->struct_mutex);
  6456. return NULL;
  6457. }
  6458. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  6459. {
  6460. struct drm_i915_private *dev_priv = dev->dev_private;
  6461. u16 rgvswctl;
  6462. rgvswctl = I915_READ16(MEMSWCTL);
  6463. if (rgvswctl & MEMCTL_CMD_STS) {
  6464. DRM_DEBUG("gpu busy, RCS change rejected\n");
  6465. return false; /* still busy with another command */
  6466. }
  6467. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  6468. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  6469. I915_WRITE16(MEMSWCTL, rgvswctl);
  6470. POSTING_READ16(MEMSWCTL);
  6471. rgvswctl |= MEMCTL_CMD_STS;
  6472. I915_WRITE16(MEMSWCTL, rgvswctl);
  6473. return true;
  6474. }
  6475. void ironlake_enable_drps(struct drm_device *dev)
  6476. {
  6477. struct drm_i915_private *dev_priv = dev->dev_private;
  6478. u32 rgvmodectl = I915_READ(MEMMODECTL);
  6479. u8 fmax, fmin, fstart, vstart;
  6480. /* Enable temp reporting */
  6481. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  6482. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  6483. /* 100ms RC evaluation intervals */
  6484. I915_WRITE(RCUPEI, 100000);
  6485. I915_WRITE(RCDNEI, 100000);
  6486. /* Set max/min thresholds to 90ms and 80ms respectively */
  6487. I915_WRITE(RCBMAXAVG, 90000);
  6488. I915_WRITE(RCBMINAVG, 80000);
  6489. I915_WRITE(MEMIHYST, 1);
  6490. /* Set up min, max, and cur for interrupt handling */
  6491. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  6492. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  6493. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  6494. MEMMODE_FSTART_SHIFT;
  6495. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  6496. PXVFREQ_PX_SHIFT;
  6497. dev_priv->fmax = fmax; /* IPS callback will increase this */
  6498. dev_priv->fstart = fstart;
  6499. dev_priv->max_delay = fstart;
  6500. dev_priv->min_delay = fmin;
  6501. dev_priv->cur_delay = fstart;
  6502. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  6503. fmax, fmin, fstart);
  6504. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  6505. /*
  6506. * Interrupts will be enabled in ironlake_irq_postinstall
  6507. */
  6508. I915_WRITE(VIDSTART, vstart);
  6509. POSTING_READ(VIDSTART);
  6510. rgvmodectl |= MEMMODE_SWMODE_EN;
  6511. I915_WRITE(MEMMODECTL, rgvmodectl);
  6512. if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  6513. DRM_ERROR("stuck trying to change perf mode\n");
  6514. msleep(1);
  6515. ironlake_set_drps(dev, fstart);
  6516. dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  6517. I915_READ(0x112e0);
  6518. dev_priv->last_time1 = jiffies_to_msecs(jiffies);
  6519. dev_priv->last_count2 = I915_READ(0x112f4);
  6520. getrawmonotonic(&dev_priv->last_time2);
  6521. }
  6522. void ironlake_disable_drps(struct drm_device *dev)
  6523. {
  6524. struct drm_i915_private *dev_priv = dev->dev_private;
  6525. u16 rgvswctl = I915_READ16(MEMSWCTL);
  6526. /* Ack interrupts, disable EFC interrupt */
  6527. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  6528. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  6529. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  6530. I915_WRITE(DEIIR, DE_PCU_EVENT);
  6531. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  6532. /* Go back to the starting frequency */
  6533. ironlake_set_drps(dev, dev_priv->fstart);
  6534. msleep(1);
  6535. rgvswctl |= MEMCTL_CMD_STS;
  6536. I915_WRITE(MEMSWCTL, rgvswctl);
  6537. msleep(1);
  6538. }
  6539. void gen6_set_rps(struct drm_device *dev, u8 val)
  6540. {
  6541. struct drm_i915_private *dev_priv = dev->dev_private;
  6542. u32 swreq;
  6543. swreq = (val & 0x3ff) << 25;
  6544. I915_WRITE(GEN6_RPNSWREQ, swreq);
  6545. }
  6546. void gen6_disable_rps(struct drm_device *dev)
  6547. {
  6548. struct drm_i915_private *dev_priv = dev->dev_private;
  6549. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  6550. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  6551. I915_WRITE(GEN6_PMIER, 0);
  6552. /* Complete PM interrupt masking here doesn't race with the rps work
  6553. * item again unmasking PM interrupts because that is using a different
  6554. * register (PMIMR) to mask PM interrupts. The only risk is in leaving
  6555. * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
  6556. spin_lock_irq(&dev_priv->rps_lock);
  6557. dev_priv->pm_iir = 0;
  6558. spin_unlock_irq(&dev_priv->rps_lock);
  6559. I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
  6560. }
  6561. static unsigned long intel_pxfreq(u32 vidfreq)
  6562. {
  6563. unsigned long freq;
  6564. int div = (vidfreq & 0x3f0000) >> 16;
  6565. int post = (vidfreq & 0x3000) >> 12;
  6566. int pre = (vidfreq & 0x7);
  6567. if (!pre)
  6568. return 0;
  6569. freq = ((div * 133333) / ((1<<post) * pre));
  6570. return freq;
  6571. }
  6572. void intel_init_emon(struct drm_device *dev)
  6573. {
  6574. struct drm_i915_private *dev_priv = dev->dev_private;
  6575. u32 lcfuse;
  6576. u8 pxw[16];
  6577. int i;
  6578. /* Disable to program */
  6579. I915_WRITE(ECR, 0);
  6580. POSTING_READ(ECR);
  6581. /* Program energy weights for various events */
  6582. I915_WRITE(SDEW, 0x15040d00);
  6583. I915_WRITE(CSIEW0, 0x007f0000);
  6584. I915_WRITE(CSIEW1, 0x1e220004);
  6585. I915_WRITE(CSIEW2, 0x04000004);
  6586. for (i = 0; i < 5; i++)
  6587. I915_WRITE(PEW + (i * 4), 0);
  6588. for (i = 0; i < 3; i++)
  6589. I915_WRITE(DEW + (i * 4), 0);
  6590. /* Program P-state weights to account for frequency power adjustment */
  6591. for (i = 0; i < 16; i++) {
  6592. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  6593. unsigned long freq = intel_pxfreq(pxvidfreq);
  6594. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  6595. PXVFREQ_PX_SHIFT;
  6596. unsigned long val;
  6597. val = vid * vid;
  6598. val *= (freq / 1000);
  6599. val *= 255;
  6600. val /= (127*127*900);
  6601. if (val > 0xff)
  6602. DRM_ERROR("bad pxval: %ld\n", val);
  6603. pxw[i] = val;
  6604. }
  6605. /* Render standby states get 0 weight */
  6606. pxw[14] = 0;
  6607. pxw[15] = 0;
  6608. for (i = 0; i < 4; i++) {
  6609. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  6610. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  6611. I915_WRITE(PXW + (i * 4), val);
  6612. }
  6613. /* Adjust magic regs to magic values (more experimental results) */
  6614. I915_WRITE(OGW0, 0);
  6615. I915_WRITE(OGW1, 0);
  6616. I915_WRITE(EG0, 0x00007f00);
  6617. I915_WRITE(EG1, 0x0000000e);
  6618. I915_WRITE(EG2, 0x000e0000);
  6619. I915_WRITE(EG3, 0x68000300);
  6620. I915_WRITE(EG4, 0x42000000);
  6621. I915_WRITE(EG5, 0x00140031);
  6622. I915_WRITE(EG6, 0);
  6623. I915_WRITE(EG7, 0);
  6624. for (i = 0; i < 8; i++)
  6625. I915_WRITE(PXWL + (i * 4), 0);
  6626. /* Enable PMON + select events */
  6627. I915_WRITE(ECR, 0x80000019);
  6628. lcfuse = I915_READ(LCFUSE02);
  6629. dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
  6630. }
  6631. void gen6_enable_rps(struct drm_i915_private *dev_priv)
  6632. {
  6633. u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  6634. u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
  6635. u32 pcu_mbox, rc6_mask = 0;
  6636. int cur_freq, min_freq, max_freq;
  6637. int i;
  6638. /* Here begins a magic sequence of register writes to enable
  6639. * auto-downclocking.
  6640. *
  6641. * Perhaps there might be some value in exposing these to
  6642. * userspace...
  6643. */
  6644. I915_WRITE(GEN6_RC_STATE, 0);
  6645. mutex_lock(&dev_priv->dev->struct_mutex);
  6646. gen6_gt_force_wake_get(dev_priv);
  6647. /* disable the counters and set deterministic thresholds */
  6648. I915_WRITE(GEN6_RC_CONTROL, 0);
  6649. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  6650. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  6651. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  6652. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  6653. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  6654. for (i = 0; i < I915_NUM_RINGS; i++)
  6655. I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
  6656. I915_WRITE(GEN6_RC_SLEEP, 0);
  6657. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  6658. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  6659. I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
  6660. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  6661. if (i915_enable_rc6)
  6662. rc6_mask = GEN6_RC_CTL_RC6p_ENABLE |
  6663. GEN6_RC_CTL_RC6_ENABLE;
  6664. I915_WRITE(GEN6_RC_CONTROL,
  6665. rc6_mask |
  6666. GEN6_RC_CTL_EI_MODE(1) |
  6667. GEN6_RC_CTL_HW_ENABLE);
  6668. I915_WRITE(GEN6_RPNSWREQ,
  6669. GEN6_FREQUENCY(10) |
  6670. GEN6_OFFSET(0) |
  6671. GEN6_AGGRESSIVE_TURBO);
  6672. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  6673. GEN6_FREQUENCY(12));
  6674. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  6675. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  6676. 18 << 24 |
  6677. 6 << 16);
  6678. I915_WRITE(GEN6_RP_UP_THRESHOLD, 10000);
  6679. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 1000000);
  6680. I915_WRITE(GEN6_RP_UP_EI, 100000);
  6681. I915_WRITE(GEN6_RP_DOWN_EI, 5000000);
  6682. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  6683. I915_WRITE(GEN6_RP_CONTROL,
  6684. GEN6_RP_MEDIA_TURBO |
  6685. GEN6_RP_USE_NORMAL_FREQ |
  6686. GEN6_RP_MEDIA_IS_GFX |
  6687. GEN6_RP_ENABLE |
  6688. GEN6_RP_UP_BUSY_AVG |
  6689. GEN6_RP_DOWN_IDLE_CONT);
  6690. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  6691. 500))
  6692. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  6693. I915_WRITE(GEN6_PCODE_DATA, 0);
  6694. I915_WRITE(GEN6_PCODE_MAILBOX,
  6695. GEN6_PCODE_READY |
  6696. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  6697. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  6698. 500))
  6699. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  6700. min_freq = (rp_state_cap & 0xff0000) >> 16;
  6701. max_freq = rp_state_cap & 0xff;
  6702. cur_freq = (gt_perf_status & 0xff00) >> 8;
  6703. /* Check for overclock support */
  6704. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  6705. 500))
  6706. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  6707. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
  6708. pcu_mbox = I915_READ(GEN6_PCODE_DATA);
  6709. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  6710. 500))
  6711. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  6712. if (pcu_mbox & (1<<31)) { /* OC supported */
  6713. max_freq = pcu_mbox & 0xff;
  6714. DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);
  6715. }
  6716. /* In units of 100MHz */
  6717. dev_priv->max_delay = max_freq;
  6718. dev_priv->min_delay = min_freq;
  6719. dev_priv->cur_delay = cur_freq;
  6720. /* requires MSI enabled */
  6721. I915_WRITE(GEN6_PMIER,
  6722. GEN6_PM_MBOX_EVENT |
  6723. GEN6_PM_THERMAL_EVENT |
  6724. GEN6_PM_RP_DOWN_TIMEOUT |
  6725. GEN6_PM_RP_UP_THRESHOLD |
  6726. GEN6_PM_RP_DOWN_THRESHOLD |
  6727. GEN6_PM_RP_UP_EI_EXPIRED |
  6728. GEN6_PM_RP_DOWN_EI_EXPIRED);
  6729. spin_lock_irq(&dev_priv->rps_lock);
  6730. WARN_ON(dev_priv->pm_iir != 0);
  6731. I915_WRITE(GEN6_PMIMR, 0);
  6732. spin_unlock_irq(&dev_priv->rps_lock);
  6733. /* enable all PM interrupts */
  6734. I915_WRITE(GEN6_PMINTRMSK, 0);
  6735. gen6_gt_force_wake_put(dev_priv);
  6736. mutex_unlock(&dev_priv->dev->struct_mutex);
  6737. }
  6738. void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
  6739. {
  6740. int min_freq = 15;
  6741. int gpu_freq, ia_freq, max_ia_freq;
  6742. int scaling_factor = 180;
  6743. max_ia_freq = cpufreq_quick_get_max(0);
  6744. /*
  6745. * Default to measured freq if none found, PCU will ensure we don't go
  6746. * over
  6747. */
  6748. if (!max_ia_freq)
  6749. max_ia_freq = tsc_khz;
  6750. /* Convert from kHz to MHz */
  6751. max_ia_freq /= 1000;
  6752. mutex_lock(&dev_priv->dev->struct_mutex);
  6753. /*
  6754. * For each potential GPU frequency, load a ring frequency we'd like
  6755. * to use for memory access. We do this by specifying the IA frequency
  6756. * the PCU should use as a reference to determine the ring frequency.
  6757. */
  6758. for (gpu_freq = dev_priv->max_delay; gpu_freq >= dev_priv->min_delay;
  6759. gpu_freq--) {
  6760. int diff = dev_priv->max_delay - gpu_freq;
  6761. /*
  6762. * For GPU frequencies less than 750MHz, just use the lowest
  6763. * ring freq.
  6764. */
  6765. if (gpu_freq < min_freq)
  6766. ia_freq = 800;
  6767. else
  6768. ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
  6769. ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
  6770. I915_WRITE(GEN6_PCODE_DATA,
  6771. (ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT) |
  6772. gpu_freq);
  6773. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY |
  6774. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  6775. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) &
  6776. GEN6_PCODE_READY) == 0, 10)) {
  6777. DRM_ERROR("pcode write of freq table timed out\n");
  6778. continue;
  6779. }
  6780. }
  6781. mutex_unlock(&dev_priv->dev->struct_mutex);
  6782. }
  6783. static void ironlake_init_clock_gating(struct drm_device *dev)
  6784. {
  6785. struct drm_i915_private *dev_priv = dev->dev_private;
  6786. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  6787. /* Required for FBC */
  6788. dspclk_gate |= DPFCUNIT_CLOCK_GATE_DISABLE |
  6789. DPFCRUNIT_CLOCK_GATE_DISABLE |
  6790. DPFDUNIT_CLOCK_GATE_DISABLE;
  6791. /* Required for CxSR */
  6792. dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
  6793. I915_WRITE(PCH_3DCGDIS0,
  6794. MARIUNIT_CLOCK_GATE_DISABLE |
  6795. SVSMUNIT_CLOCK_GATE_DISABLE);
  6796. I915_WRITE(PCH_3DCGDIS1,
  6797. VFMUNIT_CLOCK_GATE_DISABLE);
  6798. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  6799. /*
  6800. * According to the spec the following bits should be set in
  6801. * order to enable memory self-refresh
  6802. * The bit 22/21 of 0x42004
  6803. * The bit 5 of 0x42020
  6804. * The bit 15 of 0x45000
  6805. */
  6806. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6807. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  6808. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  6809. I915_WRITE(ILK_DSPCLK_GATE,
  6810. (I915_READ(ILK_DSPCLK_GATE) |
  6811. ILK_DPARB_CLK_GATE));
  6812. I915_WRITE(DISP_ARB_CTL,
  6813. (I915_READ(DISP_ARB_CTL) |
  6814. DISP_FBC_WM_DIS));
  6815. I915_WRITE(WM3_LP_ILK, 0);
  6816. I915_WRITE(WM2_LP_ILK, 0);
  6817. I915_WRITE(WM1_LP_ILK, 0);
  6818. /*
  6819. * Based on the document from hardware guys the following bits
  6820. * should be set unconditionally in order to enable FBC.
  6821. * The bit 22 of 0x42000
  6822. * The bit 22 of 0x42004
  6823. * The bit 7,8,9 of 0x42020.
  6824. */
  6825. if (IS_IRONLAKE_M(dev)) {
  6826. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  6827. I915_READ(ILK_DISPLAY_CHICKEN1) |
  6828. ILK_FBCQ_DIS);
  6829. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6830. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6831. ILK_DPARB_GATE);
  6832. I915_WRITE(ILK_DSPCLK_GATE,
  6833. I915_READ(ILK_DSPCLK_GATE) |
  6834. ILK_DPFC_DIS1 |
  6835. ILK_DPFC_DIS2 |
  6836. ILK_CLK_FBC);
  6837. }
  6838. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6839. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6840. ILK_ELPIN_409_SELECT);
  6841. I915_WRITE(_3D_CHICKEN2,
  6842. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  6843. _3D_CHICKEN2_WM_READ_PIPELINED);
  6844. }
  6845. static void gen6_init_clock_gating(struct drm_device *dev)
  6846. {
  6847. struct drm_i915_private *dev_priv = dev->dev_private;
  6848. int pipe;
  6849. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  6850. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  6851. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6852. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6853. ILK_ELPIN_409_SELECT);
  6854. I915_WRITE(WM3_LP_ILK, 0);
  6855. I915_WRITE(WM2_LP_ILK, 0);
  6856. I915_WRITE(WM1_LP_ILK, 0);
  6857. /*
  6858. * According to the spec the following bits should be
  6859. * set in order to enable memory self-refresh and fbc:
  6860. * The bit21 and bit22 of 0x42000
  6861. * The bit21 and bit22 of 0x42004
  6862. * The bit5 and bit7 of 0x42020
  6863. * The bit14 of 0x70180
  6864. * The bit14 of 0x71180
  6865. */
  6866. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  6867. I915_READ(ILK_DISPLAY_CHICKEN1) |
  6868. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  6869. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6870. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6871. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  6872. I915_WRITE(ILK_DSPCLK_GATE,
  6873. I915_READ(ILK_DSPCLK_GATE) |
  6874. ILK_DPARB_CLK_GATE |
  6875. ILK_DPFD_CLK_GATE);
  6876. for_each_pipe(pipe) {
  6877. I915_WRITE(DSPCNTR(pipe),
  6878. I915_READ(DSPCNTR(pipe)) |
  6879. DISPPLANE_TRICKLE_FEED_DISABLE);
  6880. intel_flush_display_plane(dev_priv, pipe);
  6881. }
  6882. }
  6883. static void ivybridge_init_clock_gating(struct drm_device *dev)
  6884. {
  6885. struct drm_i915_private *dev_priv = dev->dev_private;
  6886. int pipe;
  6887. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  6888. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  6889. I915_WRITE(WM3_LP_ILK, 0);
  6890. I915_WRITE(WM2_LP_ILK, 0);
  6891. I915_WRITE(WM1_LP_ILK, 0);
  6892. I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
  6893. for_each_pipe(pipe) {
  6894. I915_WRITE(DSPCNTR(pipe),
  6895. I915_READ(DSPCNTR(pipe)) |
  6896. DISPPLANE_TRICKLE_FEED_DISABLE);
  6897. intel_flush_display_plane(dev_priv, pipe);
  6898. }
  6899. }
  6900. static void g4x_init_clock_gating(struct drm_device *dev)
  6901. {
  6902. struct drm_i915_private *dev_priv = dev->dev_private;
  6903. uint32_t dspclk_gate;
  6904. I915_WRITE(RENCLK_GATE_D1, 0);
  6905. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  6906. GS_UNIT_CLOCK_GATE_DISABLE |
  6907. CL_UNIT_CLOCK_GATE_DISABLE);
  6908. I915_WRITE(RAMCLK_GATE_D, 0);
  6909. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  6910. OVRUNIT_CLOCK_GATE_DISABLE |
  6911. OVCUNIT_CLOCK_GATE_DISABLE;
  6912. if (IS_GM45(dev))
  6913. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  6914. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  6915. }
  6916. static void crestline_init_clock_gating(struct drm_device *dev)
  6917. {
  6918. struct drm_i915_private *dev_priv = dev->dev_private;
  6919. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  6920. I915_WRITE(RENCLK_GATE_D2, 0);
  6921. I915_WRITE(DSPCLK_GATE_D, 0);
  6922. I915_WRITE(RAMCLK_GATE_D, 0);
  6923. I915_WRITE16(DEUC, 0);
  6924. }
  6925. static void broadwater_init_clock_gating(struct drm_device *dev)
  6926. {
  6927. struct drm_i915_private *dev_priv = dev->dev_private;
  6928. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  6929. I965_RCC_CLOCK_GATE_DISABLE |
  6930. I965_RCPB_CLOCK_GATE_DISABLE |
  6931. I965_ISC_CLOCK_GATE_DISABLE |
  6932. I965_FBC_CLOCK_GATE_DISABLE);
  6933. I915_WRITE(RENCLK_GATE_D2, 0);
  6934. }
  6935. static void gen3_init_clock_gating(struct drm_device *dev)
  6936. {
  6937. struct drm_i915_private *dev_priv = dev->dev_private;
  6938. u32 dstate = I915_READ(D_STATE);
  6939. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  6940. DSTATE_DOT_CLOCK_GATING;
  6941. I915_WRITE(D_STATE, dstate);
  6942. }
  6943. static void i85x_init_clock_gating(struct drm_device *dev)
  6944. {
  6945. struct drm_i915_private *dev_priv = dev->dev_private;
  6946. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  6947. }
  6948. static void i830_init_clock_gating(struct drm_device *dev)
  6949. {
  6950. struct drm_i915_private *dev_priv = dev->dev_private;
  6951. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  6952. }
  6953. static void ibx_init_clock_gating(struct drm_device *dev)
  6954. {
  6955. struct drm_i915_private *dev_priv = dev->dev_private;
  6956. /*
  6957. * On Ibex Peak and Cougar Point, we need to disable clock
  6958. * gating for the panel power sequencer or it will fail to
  6959. * start up when no ports are active.
  6960. */
  6961. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  6962. }
  6963. static void cpt_init_clock_gating(struct drm_device *dev)
  6964. {
  6965. struct drm_i915_private *dev_priv = dev->dev_private;
  6966. int pipe;
  6967. /*
  6968. * On Ibex Peak and Cougar Point, we need to disable clock
  6969. * gating for the panel power sequencer or it will fail to
  6970. * start up when no ports are active.
  6971. */
  6972. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  6973. I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
  6974. DPLS_EDP_PPS_FIX_DIS);
  6975. /* Without this, mode sets may fail silently on FDI */
  6976. for_each_pipe(pipe)
  6977. I915_WRITE(TRANS_CHICKEN2(pipe), TRANS_AUTOTRAIN_GEN_STALL_DIS);
  6978. }
  6979. static void ironlake_teardown_rc6(struct drm_device *dev)
  6980. {
  6981. struct drm_i915_private *dev_priv = dev->dev_private;
  6982. if (dev_priv->renderctx) {
  6983. i915_gem_object_unpin(dev_priv->renderctx);
  6984. drm_gem_object_unreference(&dev_priv->renderctx->base);
  6985. dev_priv->renderctx = NULL;
  6986. }
  6987. if (dev_priv->pwrctx) {
  6988. i915_gem_object_unpin(dev_priv->pwrctx);
  6989. drm_gem_object_unreference(&dev_priv->pwrctx->base);
  6990. dev_priv->pwrctx = NULL;
  6991. }
  6992. }
  6993. static void ironlake_disable_rc6(struct drm_device *dev)
  6994. {
  6995. struct drm_i915_private *dev_priv = dev->dev_private;
  6996. if (I915_READ(PWRCTXA)) {
  6997. /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
  6998. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
  6999. wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
  7000. 50);
  7001. I915_WRITE(PWRCTXA, 0);
  7002. POSTING_READ(PWRCTXA);
  7003. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  7004. POSTING_READ(RSTDBYCTL);
  7005. }
  7006. ironlake_teardown_rc6(dev);
  7007. }
  7008. static int ironlake_setup_rc6(struct drm_device *dev)
  7009. {
  7010. struct drm_i915_private *dev_priv = dev->dev_private;
  7011. if (dev_priv->renderctx == NULL)
  7012. dev_priv->renderctx = intel_alloc_context_page(dev);
  7013. if (!dev_priv->renderctx)
  7014. return -ENOMEM;
  7015. if (dev_priv->pwrctx == NULL)
  7016. dev_priv->pwrctx = intel_alloc_context_page(dev);
  7017. if (!dev_priv->pwrctx) {
  7018. ironlake_teardown_rc6(dev);
  7019. return -ENOMEM;
  7020. }
  7021. return 0;
  7022. }
  7023. void ironlake_enable_rc6(struct drm_device *dev)
  7024. {
  7025. struct drm_i915_private *dev_priv = dev->dev_private;
  7026. int ret;
  7027. /* rc6 disabled by default due to repeated reports of hanging during
  7028. * boot and resume.
  7029. */
  7030. if (!i915_enable_rc6)
  7031. return;
  7032. mutex_lock(&dev->struct_mutex);
  7033. ret = ironlake_setup_rc6(dev);
  7034. if (ret) {
  7035. mutex_unlock(&dev->struct_mutex);
  7036. return;
  7037. }
  7038. /*
  7039. * GPU can automatically power down the render unit if given a page
  7040. * to save state.
  7041. */
  7042. ret = BEGIN_LP_RING(6);
  7043. if (ret) {
  7044. ironlake_teardown_rc6(dev);
  7045. mutex_unlock(&dev->struct_mutex);
  7046. return;
  7047. }
  7048. OUT_RING(MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
  7049. OUT_RING(MI_SET_CONTEXT);
  7050. OUT_RING(dev_priv->renderctx->gtt_offset |
  7051. MI_MM_SPACE_GTT |
  7052. MI_SAVE_EXT_STATE_EN |
  7053. MI_RESTORE_EXT_STATE_EN |
  7054. MI_RESTORE_INHIBIT);
  7055. OUT_RING(MI_SUSPEND_FLUSH);
  7056. OUT_RING(MI_NOOP);
  7057. OUT_RING(MI_FLUSH);
  7058. ADVANCE_LP_RING();
  7059. /*
  7060. * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
  7061. * does an implicit flush, combined with MI_FLUSH above, it should be
  7062. * safe to assume that renderctx is valid
  7063. */
  7064. ret = intel_wait_ring_idle(LP_RING(dev_priv));
  7065. if (ret) {
  7066. DRM_ERROR("failed to enable ironlake power power savings\n");
  7067. ironlake_teardown_rc6(dev);
  7068. mutex_unlock(&dev->struct_mutex);
  7069. return;
  7070. }
  7071. I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
  7072. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  7073. mutex_unlock(&dev->struct_mutex);
  7074. }
  7075. void intel_init_clock_gating(struct drm_device *dev)
  7076. {
  7077. struct drm_i915_private *dev_priv = dev->dev_private;
  7078. dev_priv->display.init_clock_gating(dev);
  7079. if (dev_priv->display.init_pch_clock_gating)
  7080. dev_priv->display.init_pch_clock_gating(dev);
  7081. }
  7082. /* Set up chip specific display functions */
  7083. static void intel_init_display(struct drm_device *dev)
  7084. {
  7085. struct drm_i915_private *dev_priv = dev->dev_private;
  7086. /* We always want a DPMS function */
  7087. if (HAS_PCH_SPLIT(dev)) {
  7088. dev_priv->display.dpms = ironlake_crtc_dpms;
  7089. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  7090. dev_priv->display.update_plane = ironlake_update_plane;
  7091. } else {
  7092. dev_priv->display.dpms = i9xx_crtc_dpms;
  7093. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  7094. dev_priv->display.update_plane = i9xx_update_plane;
  7095. }
  7096. if (I915_HAS_FBC(dev)) {
  7097. if (HAS_PCH_SPLIT(dev)) {
  7098. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  7099. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  7100. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  7101. } else if (IS_GM45(dev)) {
  7102. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  7103. dev_priv->display.enable_fbc = g4x_enable_fbc;
  7104. dev_priv->display.disable_fbc = g4x_disable_fbc;
  7105. } else if (IS_CRESTLINE(dev)) {
  7106. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  7107. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  7108. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  7109. }
  7110. /* 855GM needs testing */
  7111. }
  7112. /* Returns the core display clock speed */
  7113. if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  7114. dev_priv->display.get_display_clock_speed =
  7115. i945_get_display_clock_speed;
  7116. else if (IS_I915G(dev))
  7117. dev_priv->display.get_display_clock_speed =
  7118. i915_get_display_clock_speed;
  7119. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  7120. dev_priv->display.get_display_clock_speed =
  7121. i9xx_misc_get_display_clock_speed;
  7122. else if (IS_I915GM(dev))
  7123. dev_priv->display.get_display_clock_speed =
  7124. i915gm_get_display_clock_speed;
  7125. else if (IS_I865G(dev))
  7126. dev_priv->display.get_display_clock_speed =
  7127. i865_get_display_clock_speed;
  7128. else if (IS_I85X(dev))
  7129. dev_priv->display.get_display_clock_speed =
  7130. i855_get_display_clock_speed;
  7131. else /* 852, 830 */
  7132. dev_priv->display.get_display_clock_speed =
  7133. i830_get_display_clock_speed;
  7134. /* For FIFO watermark updates */
  7135. if (HAS_PCH_SPLIT(dev)) {
  7136. if (HAS_PCH_IBX(dev))
  7137. dev_priv->display.init_pch_clock_gating = ibx_init_clock_gating;
  7138. else if (HAS_PCH_CPT(dev))
  7139. dev_priv->display.init_pch_clock_gating = cpt_init_clock_gating;
  7140. if (IS_GEN5(dev)) {
  7141. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  7142. dev_priv->display.update_wm = ironlake_update_wm;
  7143. else {
  7144. DRM_DEBUG_KMS("Failed to get proper latency. "
  7145. "Disable CxSR\n");
  7146. dev_priv->display.update_wm = NULL;
  7147. }
  7148. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  7149. dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
  7150. dev_priv->display.write_eld = ironlake_write_eld;
  7151. } else if (IS_GEN6(dev)) {
  7152. if (SNB_READ_WM0_LATENCY()) {
  7153. dev_priv->display.update_wm = sandybridge_update_wm;
  7154. } else {
  7155. DRM_DEBUG_KMS("Failed to read display plane latency. "
  7156. "Disable CxSR\n");
  7157. dev_priv->display.update_wm = NULL;
  7158. }
  7159. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  7160. dev_priv->display.init_clock_gating = gen6_init_clock_gating;
  7161. dev_priv->display.write_eld = ironlake_write_eld;
  7162. } else if (IS_IVYBRIDGE(dev)) {
  7163. /* FIXME: detect B0+ stepping and use auto training */
  7164. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  7165. if (SNB_READ_WM0_LATENCY()) {
  7166. dev_priv->display.update_wm = sandybridge_update_wm;
  7167. } else {
  7168. DRM_DEBUG_KMS("Failed to read display plane latency. "
  7169. "Disable CxSR\n");
  7170. dev_priv->display.update_wm = NULL;
  7171. }
  7172. dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
  7173. dev_priv->display.write_eld = ironlake_write_eld;
  7174. } else
  7175. dev_priv->display.update_wm = NULL;
  7176. } else if (IS_PINEVIEW(dev)) {
  7177. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  7178. dev_priv->is_ddr3,
  7179. dev_priv->fsb_freq,
  7180. dev_priv->mem_freq)) {
  7181. DRM_INFO("failed to find known CxSR latency "
  7182. "(found ddr%s fsb freq %d, mem freq %d), "
  7183. "disabling CxSR\n",
  7184. (dev_priv->is_ddr3 == 1) ? "3" : "2",
  7185. dev_priv->fsb_freq, dev_priv->mem_freq);
  7186. /* Disable CxSR and never update its watermark again */
  7187. pineview_disable_cxsr(dev);
  7188. dev_priv->display.update_wm = NULL;
  7189. } else
  7190. dev_priv->display.update_wm = pineview_update_wm;
  7191. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  7192. } else if (IS_G4X(dev)) {
  7193. dev_priv->display.write_eld = g4x_write_eld;
  7194. dev_priv->display.update_wm = g4x_update_wm;
  7195. dev_priv->display.init_clock_gating = g4x_init_clock_gating;
  7196. } else if (IS_GEN4(dev)) {
  7197. dev_priv->display.update_wm = i965_update_wm;
  7198. if (IS_CRESTLINE(dev))
  7199. dev_priv->display.init_clock_gating = crestline_init_clock_gating;
  7200. else if (IS_BROADWATER(dev))
  7201. dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
  7202. } else if (IS_GEN3(dev)) {
  7203. dev_priv->display.update_wm = i9xx_update_wm;
  7204. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  7205. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  7206. } else if (IS_I865G(dev)) {
  7207. dev_priv->display.update_wm = i830_update_wm;
  7208. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  7209. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  7210. } else if (IS_I85X(dev)) {
  7211. dev_priv->display.update_wm = i9xx_update_wm;
  7212. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  7213. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  7214. } else {
  7215. dev_priv->display.update_wm = i830_update_wm;
  7216. dev_priv->display.init_clock_gating = i830_init_clock_gating;
  7217. if (IS_845G(dev))
  7218. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  7219. else
  7220. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  7221. }
  7222. /* Default just returns -ENODEV to indicate unsupported */
  7223. dev_priv->display.queue_flip = intel_default_queue_flip;
  7224. switch (INTEL_INFO(dev)->gen) {
  7225. case 2:
  7226. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  7227. break;
  7228. case 3:
  7229. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  7230. break;
  7231. case 4:
  7232. case 5:
  7233. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  7234. break;
  7235. case 6:
  7236. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  7237. break;
  7238. case 7:
  7239. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  7240. break;
  7241. }
  7242. }
  7243. /*
  7244. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  7245. * resume, or other times. This quirk makes sure that's the case for
  7246. * affected systems.
  7247. */
  7248. static void quirk_pipea_force(struct drm_device *dev)
  7249. {
  7250. struct drm_i915_private *dev_priv = dev->dev_private;
  7251. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  7252. DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
  7253. }
  7254. /*
  7255. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  7256. */
  7257. static void quirk_ssc_force_disable(struct drm_device *dev)
  7258. {
  7259. struct drm_i915_private *dev_priv = dev->dev_private;
  7260. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  7261. }
  7262. struct intel_quirk {
  7263. int device;
  7264. int subsystem_vendor;
  7265. int subsystem_device;
  7266. void (*hook)(struct drm_device *dev);
  7267. };
  7268. struct intel_quirk intel_quirks[] = {
  7269. /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
  7270. { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
  7271. /* HP Mini needs pipe A force quirk (LP: #322104) */
  7272. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  7273. /* Thinkpad R31 needs pipe A force quirk */
  7274. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  7275. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  7276. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  7277. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  7278. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  7279. /* ThinkPad X40 needs pipe A force quirk */
  7280. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  7281. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  7282. /* 855 & before need to leave pipe A & dpll A up */
  7283. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  7284. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  7285. /* Lenovo U160 cannot use SSC on LVDS */
  7286. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  7287. /* Sony Vaio Y cannot use SSC on LVDS */
  7288. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  7289. };
  7290. static void intel_init_quirks(struct drm_device *dev)
  7291. {
  7292. struct pci_dev *d = dev->pdev;
  7293. int i;
  7294. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  7295. struct intel_quirk *q = &intel_quirks[i];
  7296. if (d->device == q->device &&
  7297. (d->subsystem_vendor == q->subsystem_vendor ||
  7298. q->subsystem_vendor == PCI_ANY_ID) &&
  7299. (d->subsystem_device == q->subsystem_device ||
  7300. q->subsystem_device == PCI_ANY_ID))
  7301. q->hook(dev);
  7302. }
  7303. }
  7304. /* Disable the VGA plane that we never use */
  7305. static void i915_disable_vga(struct drm_device *dev)
  7306. {
  7307. struct drm_i915_private *dev_priv = dev->dev_private;
  7308. u8 sr1;
  7309. u32 vga_reg;
  7310. if (HAS_PCH_SPLIT(dev))
  7311. vga_reg = CPU_VGACNTRL;
  7312. else
  7313. vga_reg = VGACNTRL;
  7314. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  7315. outb(1, VGA_SR_INDEX);
  7316. sr1 = inb(VGA_SR_DATA);
  7317. outb(sr1 | 1<<5, VGA_SR_DATA);
  7318. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  7319. udelay(300);
  7320. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  7321. POSTING_READ(vga_reg);
  7322. }
  7323. void intel_modeset_init(struct drm_device *dev)
  7324. {
  7325. struct drm_i915_private *dev_priv = dev->dev_private;
  7326. int i;
  7327. drm_mode_config_init(dev);
  7328. dev->mode_config.min_width = 0;
  7329. dev->mode_config.min_height = 0;
  7330. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  7331. intel_init_quirks(dev);
  7332. intel_init_display(dev);
  7333. if (IS_GEN2(dev)) {
  7334. dev->mode_config.max_width = 2048;
  7335. dev->mode_config.max_height = 2048;
  7336. } else if (IS_GEN3(dev)) {
  7337. dev->mode_config.max_width = 4096;
  7338. dev->mode_config.max_height = 4096;
  7339. } else {
  7340. dev->mode_config.max_width = 8192;
  7341. dev->mode_config.max_height = 8192;
  7342. }
  7343. dev->mode_config.fb_base = dev->agp->base;
  7344. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  7345. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  7346. for (i = 0; i < dev_priv->num_pipe; i++) {
  7347. intel_crtc_init(dev, i);
  7348. }
  7349. /* Just disable it once at startup */
  7350. i915_disable_vga(dev);
  7351. intel_setup_outputs(dev);
  7352. intel_init_clock_gating(dev);
  7353. if (IS_IRONLAKE_M(dev)) {
  7354. ironlake_enable_drps(dev);
  7355. intel_init_emon(dev);
  7356. }
  7357. if (IS_GEN6(dev) || IS_GEN7(dev)) {
  7358. gen6_enable_rps(dev_priv);
  7359. gen6_update_ring_freq(dev_priv);
  7360. }
  7361. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  7362. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  7363. (unsigned long)dev);
  7364. }
  7365. void intel_modeset_gem_init(struct drm_device *dev)
  7366. {
  7367. if (IS_IRONLAKE_M(dev))
  7368. ironlake_enable_rc6(dev);
  7369. intel_setup_overlay(dev);
  7370. }
  7371. void intel_modeset_cleanup(struct drm_device *dev)
  7372. {
  7373. struct drm_i915_private *dev_priv = dev->dev_private;
  7374. struct drm_crtc *crtc;
  7375. struct intel_crtc *intel_crtc;
  7376. drm_kms_helper_poll_fini(dev);
  7377. mutex_lock(&dev->struct_mutex);
  7378. intel_unregister_dsm_handler();
  7379. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  7380. /* Skip inactive CRTCs */
  7381. if (!crtc->fb)
  7382. continue;
  7383. intel_crtc = to_intel_crtc(crtc);
  7384. intel_increase_pllclock(crtc);
  7385. }
  7386. intel_disable_fbc(dev);
  7387. if (IS_IRONLAKE_M(dev))
  7388. ironlake_disable_drps(dev);
  7389. if (IS_GEN6(dev) || IS_GEN7(dev))
  7390. gen6_disable_rps(dev);
  7391. if (IS_IRONLAKE_M(dev))
  7392. ironlake_disable_rc6(dev);
  7393. mutex_unlock(&dev->struct_mutex);
  7394. /* Disable the irq before mode object teardown, for the irq might
  7395. * enqueue unpin/hotplug work. */
  7396. drm_irq_uninstall(dev);
  7397. cancel_work_sync(&dev_priv->hotplug_work);
  7398. cancel_work_sync(&dev_priv->rps_work);
  7399. /* flush any delayed tasks or pending work */
  7400. flush_scheduled_work();
  7401. /* Shut off idle work before the crtcs get freed. */
  7402. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  7403. intel_crtc = to_intel_crtc(crtc);
  7404. del_timer_sync(&intel_crtc->idle_timer);
  7405. }
  7406. del_timer_sync(&dev_priv->idle_timer);
  7407. cancel_work_sync(&dev_priv->idle_work);
  7408. drm_mode_config_cleanup(dev);
  7409. }
  7410. /*
  7411. * Return which encoder is currently attached for connector.
  7412. */
  7413. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  7414. {
  7415. return &intel_attached_encoder(connector)->base;
  7416. }
  7417. void intel_connector_attach_encoder(struct intel_connector *connector,
  7418. struct intel_encoder *encoder)
  7419. {
  7420. connector->encoder = encoder;
  7421. drm_mode_connector_attach_encoder(&connector->base,
  7422. &encoder->base);
  7423. }
  7424. /*
  7425. * set vga decode state - true == enable VGA decode
  7426. */
  7427. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  7428. {
  7429. struct drm_i915_private *dev_priv = dev->dev_private;
  7430. u16 gmch_ctrl;
  7431. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  7432. if (state)
  7433. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  7434. else
  7435. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  7436. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  7437. return 0;
  7438. }
  7439. #ifdef CONFIG_DEBUG_FS
  7440. #include <linux/seq_file.h>
  7441. struct intel_display_error_state {
  7442. struct intel_cursor_error_state {
  7443. u32 control;
  7444. u32 position;
  7445. u32 base;
  7446. u32 size;
  7447. } cursor[2];
  7448. struct intel_pipe_error_state {
  7449. u32 conf;
  7450. u32 source;
  7451. u32 htotal;
  7452. u32 hblank;
  7453. u32 hsync;
  7454. u32 vtotal;
  7455. u32 vblank;
  7456. u32 vsync;
  7457. } pipe[2];
  7458. struct intel_plane_error_state {
  7459. u32 control;
  7460. u32 stride;
  7461. u32 size;
  7462. u32 pos;
  7463. u32 addr;
  7464. u32 surface;
  7465. u32 tile_offset;
  7466. } plane[2];
  7467. };
  7468. struct intel_display_error_state *
  7469. intel_display_capture_error_state(struct drm_device *dev)
  7470. {
  7471. drm_i915_private_t *dev_priv = dev->dev_private;
  7472. struct intel_display_error_state *error;
  7473. int i;
  7474. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  7475. if (error == NULL)
  7476. return NULL;
  7477. for (i = 0; i < 2; i++) {
  7478. error->cursor[i].control = I915_READ(CURCNTR(i));
  7479. error->cursor[i].position = I915_READ(CURPOS(i));
  7480. error->cursor[i].base = I915_READ(CURBASE(i));
  7481. error->plane[i].control = I915_READ(DSPCNTR(i));
  7482. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  7483. error->plane[i].size = I915_READ(DSPSIZE(i));
  7484. error->plane[i].pos = I915_READ(DSPPOS(i));
  7485. error->plane[i].addr = I915_READ(DSPADDR(i));
  7486. if (INTEL_INFO(dev)->gen >= 4) {
  7487. error->plane[i].surface = I915_READ(DSPSURF(i));
  7488. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  7489. }
  7490. error->pipe[i].conf = I915_READ(PIPECONF(i));
  7491. error->pipe[i].source = I915_READ(PIPESRC(i));
  7492. error->pipe[i].htotal = I915_READ(HTOTAL(i));
  7493. error->pipe[i].hblank = I915_READ(HBLANK(i));
  7494. error->pipe[i].hsync = I915_READ(HSYNC(i));
  7495. error->pipe[i].vtotal = I915_READ(VTOTAL(i));
  7496. error->pipe[i].vblank = I915_READ(VBLANK(i));
  7497. error->pipe[i].vsync = I915_READ(VSYNC(i));
  7498. }
  7499. return error;
  7500. }
  7501. void
  7502. intel_display_print_error_state(struct seq_file *m,
  7503. struct drm_device *dev,
  7504. struct intel_display_error_state *error)
  7505. {
  7506. int i;
  7507. for (i = 0; i < 2; i++) {
  7508. seq_printf(m, "Pipe [%d]:\n", i);
  7509. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  7510. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  7511. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  7512. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  7513. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  7514. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  7515. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  7516. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  7517. seq_printf(m, "Plane [%d]:\n", i);
  7518. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  7519. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  7520. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  7521. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  7522. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  7523. if (INTEL_INFO(dev)->gen >= 4) {
  7524. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  7525. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  7526. }
  7527. seq_printf(m, "Cursor [%d]:\n", i);
  7528. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  7529. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  7530. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  7531. }
  7532. }
  7533. #endif