intel_display.c 184 KB

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