intel_display.c 267 KB

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