intel_display.c 195 KB

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