intel_display.c 253 KB

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