intel_display.c 264 KB

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