intel_display.c 182 KB

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