intel_display.c 219 KB

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