intel_display.c 183 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460
  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/module.h>
  27. #include <linux/input.h>
  28. #include <linux/i2c.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/vgaarb.h>
  32. #include "drmP.h"
  33. #include "intel_drv.h"
  34. #include "i915_drm.h"
  35. #include "i915_drv.h"
  36. #include "i915_trace.h"
  37. #include "drm_dp_helper.h"
  38. #include "drm_crtc_helper.h"
  39. #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  40. bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
  41. static void intel_update_watermarks(struct drm_device *dev);
  42. static void intel_increase_pllclock(struct drm_crtc *crtc);
  43. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  44. typedef struct {
  45. /* given values */
  46. int n;
  47. int m1, m2;
  48. int p1, p2;
  49. /* derived values */
  50. int dot;
  51. int vco;
  52. int m;
  53. int p;
  54. } intel_clock_t;
  55. typedef struct {
  56. int min, max;
  57. } intel_range_t;
  58. typedef struct {
  59. int dot_limit;
  60. int p2_slow, p2_fast;
  61. } intel_p2_t;
  62. #define INTEL_P2_NUM 2
  63. typedef struct intel_limit intel_limit_t;
  64. struct intel_limit {
  65. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  66. intel_p2_t p2;
  67. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  68. int, int, intel_clock_t *);
  69. };
  70. #define I8XX_DOT_MIN 25000
  71. #define I8XX_DOT_MAX 350000
  72. #define I8XX_VCO_MIN 930000
  73. #define I8XX_VCO_MAX 1400000
  74. #define I8XX_N_MIN 3
  75. #define I8XX_N_MAX 16
  76. #define I8XX_M_MIN 96
  77. #define I8XX_M_MAX 140
  78. #define I8XX_M1_MIN 18
  79. #define I8XX_M1_MAX 26
  80. #define I8XX_M2_MIN 6
  81. #define I8XX_M2_MAX 16
  82. #define I8XX_P_MIN 4
  83. #define I8XX_P_MAX 128
  84. #define I8XX_P1_MIN 2
  85. #define I8XX_P1_MAX 33
  86. #define I8XX_P1_LVDS_MIN 1
  87. #define I8XX_P1_LVDS_MAX 6
  88. #define I8XX_P2_SLOW 4
  89. #define I8XX_P2_FAST 2
  90. #define I8XX_P2_LVDS_SLOW 14
  91. #define I8XX_P2_LVDS_FAST 7
  92. #define I8XX_P2_SLOW_LIMIT 165000
  93. #define I9XX_DOT_MIN 20000
  94. #define I9XX_DOT_MAX 400000
  95. #define I9XX_VCO_MIN 1400000
  96. #define I9XX_VCO_MAX 2800000
  97. #define PINEVIEW_VCO_MIN 1700000
  98. #define PINEVIEW_VCO_MAX 3500000
  99. #define I9XX_N_MIN 1
  100. #define I9XX_N_MAX 6
  101. /* Pineview's Ncounter is a ring counter */
  102. #define PINEVIEW_N_MIN 3
  103. #define PINEVIEW_N_MAX 6
  104. #define I9XX_M_MIN 70
  105. #define I9XX_M_MAX 120
  106. #define PINEVIEW_M_MIN 2
  107. #define PINEVIEW_M_MAX 256
  108. #define I9XX_M1_MIN 10
  109. #define I9XX_M1_MAX 22
  110. #define I9XX_M2_MIN 5
  111. #define I9XX_M2_MAX 9
  112. /* Pineview M1 is reserved, and must be 0 */
  113. #define PINEVIEW_M1_MIN 0
  114. #define PINEVIEW_M1_MAX 0
  115. #define PINEVIEW_M2_MIN 0
  116. #define PINEVIEW_M2_MAX 254
  117. #define I9XX_P_SDVO_DAC_MIN 5
  118. #define I9XX_P_SDVO_DAC_MAX 80
  119. #define I9XX_P_LVDS_MIN 7
  120. #define I9XX_P_LVDS_MAX 98
  121. #define PINEVIEW_P_LVDS_MIN 7
  122. #define PINEVIEW_P_LVDS_MAX 112
  123. #define I9XX_P1_MIN 1
  124. #define I9XX_P1_MAX 8
  125. #define I9XX_P2_SDVO_DAC_SLOW 10
  126. #define I9XX_P2_SDVO_DAC_FAST 5
  127. #define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
  128. #define I9XX_P2_LVDS_SLOW 14
  129. #define I9XX_P2_LVDS_FAST 7
  130. #define I9XX_P2_LVDS_SLOW_LIMIT 112000
  131. /*The parameter is for SDVO on G4x platform*/
  132. #define G4X_DOT_SDVO_MIN 25000
  133. #define G4X_DOT_SDVO_MAX 270000
  134. #define G4X_VCO_MIN 1750000
  135. #define G4X_VCO_MAX 3500000
  136. #define G4X_N_SDVO_MIN 1
  137. #define G4X_N_SDVO_MAX 4
  138. #define G4X_M_SDVO_MIN 104
  139. #define G4X_M_SDVO_MAX 138
  140. #define G4X_M1_SDVO_MIN 17
  141. #define G4X_M1_SDVO_MAX 23
  142. #define G4X_M2_SDVO_MIN 5
  143. #define G4X_M2_SDVO_MAX 11
  144. #define G4X_P_SDVO_MIN 10
  145. #define G4X_P_SDVO_MAX 30
  146. #define G4X_P1_SDVO_MIN 1
  147. #define G4X_P1_SDVO_MAX 3
  148. #define G4X_P2_SDVO_SLOW 10
  149. #define G4X_P2_SDVO_FAST 10
  150. #define G4X_P2_SDVO_LIMIT 270000
  151. /*The parameter is for HDMI_DAC on G4x platform*/
  152. #define G4X_DOT_HDMI_DAC_MIN 22000
  153. #define G4X_DOT_HDMI_DAC_MAX 400000
  154. #define G4X_N_HDMI_DAC_MIN 1
  155. #define G4X_N_HDMI_DAC_MAX 4
  156. #define G4X_M_HDMI_DAC_MIN 104
  157. #define G4X_M_HDMI_DAC_MAX 138
  158. #define G4X_M1_HDMI_DAC_MIN 16
  159. #define G4X_M1_HDMI_DAC_MAX 23
  160. #define G4X_M2_HDMI_DAC_MIN 5
  161. #define G4X_M2_HDMI_DAC_MAX 11
  162. #define G4X_P_HDMI_DAC_MIN 5
  163. #define G4X_P_HDMI_DAC_MAX 80
  164. #define G4X_P1_HDMI_DAC_MIN 1
  165. #define G4X_P1_HDMI_DAC_MAX 8
  166. #define G4X_P2_HDMI_DAC_SLOW 10
  167. #define G4X_P2_HDMI_DAC_FAST 5
  168. #define G4X_P2_HDMI_DAC_LIMIT 165000
  169. /*The parameter is for SINGLE_CHANNEL_LVDS on G4x platform*/
  170. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MIN 20000
  171. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MAX 115000
  172. #define G4X_N_SINGLE_CHANNEL_LVDS_MIN 1
  173. #define G4X_N_SINGLE_CHANNEL_LVDS_MAX 3
  174. #define G4X_M_SINGLE_CHANNEL_LVDS_MIN 104
  175. #define G4X_M_SINGLE_CHANNEL_LVDS_MAX 138
  176. #define G4X_M1_SINGLE_CHANNEL_LVDS_MIN 17
  177. #define G4X_M1_SINGLE_CHANNEL_LVDS_MAX 23
  178. #define G4X_M2_SINGLE_CHANNEL_LVDS_MIN 5
  179. #define G4X_M2_SINGLE_CHANNEL_LVDS_MAX 11
  180. #define G4X_P_SINGLE_CHANNEL_LVDS_MIN 28
  181. #define G4X_P_SINGLE_CHANNEL_LVDS_MAX 112
  182. #define G4X_P1_SINGLE_CHANNEL_LVDS_MIN 2
  183. #define G4X_P1_SINGLE_CHANNEL_LVDS_MAX 8
  184. #define G4X_P2_SINGLE_CHANNEL_LVDS_SLOW 14
  185. #define G4X_P2_SINGLE_CHANNEL_LVDS_FAST 14
  186. #define G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT 0
  187. /*The parameter is for DUAL_CHANNEL_LVDS on G4x platform*/
  188. #define G4X_DOT_DUAL_CHANNEL_LVDS_MIN 80000
  189. #define G4X_DOT_DUAL_CHANNEL_LVDS_MAX 224000
  190. #define G4X_N_DUAL_CHANNEL_LVDS_MIN 1
  191. #define G4X_N_DUAL_CHANNEL_LVDS_MAX 3
  192. #define G4X_M_DUAL_CHANNEL_LVDS_MIN 104
  193. #define G4X_M_DUAL_CHANNEL_LVDS_MAX 138
  194. #define G4X_M1_DUAL_CHANNEL_LVDS_MIN 17
  195. #define G4X_M1_DUAL_CHANNEL_LVDS_MAX 23
  196. #define G4X_M2_DUAL_CHANNEL_LVDS_MIN 5
  197. #define G4X_M2_DUAL_CHANNEL_LVDS_MAX 11
  198. #define G4X_P_DUAL_CHANNEL_LVDS_MIN 14
  199. #define G4X_P_DUAL_CHANNEL_LVDS_MAX 42
  200. #define G4X_P1_DUAL_CHANNEL_LVDS_MIN 2
  201. #define G4X_P1_DUAL_CHANNEL_LVDS_MAX 6
  202. #define G4X_P2_DUAL_CHANNEL_LVDS_SLOW 7
  203. #define G4X_P2_DUAL_CHANNEL_LVDS_FAST 7
  204. #define G4X_P2_DUAL_CHANNEL_LVDS_LIMIT 0
  205. /*The parameter is for DISPLAY PORT on G4x platform*/
  206. #define G4X_DOT_DISPLAY_PORT_MIN 161670
  207. #define G4X_DOT_DISPLAY_PORT_MAX 227000
  208. #define G4X_N_DISPLAY_PORT_MIN 1
  209. #define G4X_N_DISPLAY_PORT_MAX 2
  210. #define G4X_M_DISPLAY_PORT_MIN 97
  211. #define G4X_M_DISPLAY_PORT_MAX 108
  212. #define G4X_M1_DISPLAY_PORT_MIN 0x10
  213. #define G4X_M1_DISPLAY_PORT_MAX 0x12
  214. #define G4X_M2_DISPLAY_PORT_MIN 0x05
  215. #define G4X_M2_DISPLAY_PORT_MAX 0x06
  216. #define G4X_P_DISPLAY_PORT_MIN 10
  217. #define G4X_P_DISPLAY_PORT_MAX 20
  218. #define G4X_P1_DISPLAY_PORT_MIN 1
  219. #define G4X_P1_DISPLAY_PORT_MAX 2
  220. #define G4X_P2_DISPLAY_PORT_SLOW 10
  221. #define G4X_P2_DISPLAY_PORT_FAST 10
  222. #define G4X_P2_DISPLAY_PORT_LIMIT 0
  223. /* Ironlake / Sandybridge */
  224. /* as we calculate clock using (register_value + 2) for
  225. N/M1/M2, so here the range value for them is (actual_value-2).
  226. */
  227. #define IRONLAKE_DOT_MIN 25000
  228. #define IRONLAKE_DOT_MAX 350000
  229. #define IRONLAKE_VCO_MIN 1760000
  230. #define IRONLAKE_VCO_MAX 3510000
  231. #define IRONLAKE_M1_MIN 12
  232. #define IRONLAKE_M1_MAX 22
  233. #define IRONLAKE_M2_MIN 5
  234. #define IRONLAKE_M2_MAX 9
  235. #define IRONLAKE_P2_DOT_LIMIT 225000 /* 225Mhz */
  236. /* We have parameter ranges for different type of outputs. */
  237. /* DAC & HDMI Refclk 120Mhz */
  238. #define IRONLAKE_DAC_N_MIN 1
  239. #define IRONLAKE_DAC_N_MAX 5
  240. #define IRONLAKE_DAC_M_MIN 79
  241. #define IRONLAKE_DAC_M_MAX 127
  242. #define IRONLAKE_DAC_P_MIN 5
  243. #define IRONLAKE_DAC_P_MAX 80
  244. #define IRONLAKE_DAC_P1_MIN 1
  245. #define IRONLAKE_DAC_P1_MAX 8
  246. #define IRONLAKE_DAC_P2_SLOW 10
  247. #define IRONLAKE_DAC_P2_FAST 5
  248. /* LVDS single-channel 120Mhz refclk */
  249. #define IRONLAKE_LVDS_S_N_MIN 1
  250. #define IRONLAKE_LVDS_S_N_MAX 3
  251. #define IRONLAKE_LVDS_S_M_MIN 79
  252. #define IRONLAKE_LVDS_S_M_MAX 118
  253. #define IRONLAKE_LVDS_S_P_MIN 28
  254. #define IRONLAKE_LVDS_S_P_MAX 112
  255. #define IRONLAKE_LVDS_S_P1_MIN 2
  256. #define IRONLAKE_LVDS_S_P1_MAX 8
  257. #define IRONLAKE_LVDS_S_P2_SLOW 14
  258. #define IRONLAKE_LVDS_S_P2_FAST 14
  259. /* LVDS dual-channel 120Mhz refclk */
  260. #define IRONLAKE_LVDS_D_N_MIN 1
  261. #define IRONLAKE_LVDS_D_N_MAX 3
  262. #define IRONLAKE_LVDS_D_M_MIN 79
  263. #define IRONLAKE_LVDS_D_M_MAX 127
  264. #define IRONLAKE_LVDS_D_P_MIN 14
  265. #define IRONLAKE_LVDS_D_P_MAX 56
  266. #define IRONLAKE_LVDS_D_P1_MIN 2
  267. #define IRONLAKE_LVDS_D_P1_MAX 8
  268. #define IRONLAKE_LVDS_D_P2_SLOW 7
  269. #define IRONLAKE_LVDS_D_P2_FAST 7
  270. /* LVDS single-channel 100Mhz refclk */
  271. #define IRONLAKE_LVDS_S_SSC_N_MIN 1
  272. #define IRONLAKE_LVDS_S_SSC_N_MAX 2
  273. #define IRONLAKE_LVDS_S_SSC_M_MIN 79
  274. #define IRONLAKE_LVDS_S_SSC_M_MAX 126
  275. #define IRONLAKE_LVDS_S_SSC_P_MIN 28
  276. #define IRONLAKE_LVDS_S_SSC_P_MAX 112
  277. #define IRONLAKE_LVDS_S_SSC_P1_MIN 2
  278. #define IRONLAKE_LVDS_S_SSC_P1_MAX 8
  279. #define IRONLAKE_LVDS_S_SSC_P2_SLOW 14
  280. #define IRONLAKE_LVDS_S_SSC_P2_FAST 14
  281. /* LVDS dual-channel 100Mhz refclk */
  282. #define IRONLAKE_LVDS_D_SSC_N_MIN 1
  283. #define IRONLAKE_LVDS_D_SSC_N_MAX 3
  284. #define IRONLAKE_LVDS_D_SSC_M_MIN 79
  285. #define IRONLAKE_LVDS_D_SSC_M_MAX 126
  286. #define IRONLAKE_LVDS_D_SSC_P_MIN 14
  287. #define IRONLAKE_LVDS_D_SSC_P_MAX 42
  288. #define IRONLAKE_LVDS_D_SSC_P1_MIN 2
  289. #define IRONLAKE_LVDS_D_SSC_P1_MAX 6
  290. #define IRONLAKE_LVDS_D_SSC_P2_SLOW 7
  291. #define IRONLAKE_LVDS_D_SSC_P2_FAST 7
  292. /* DisplayPort */
  293. #define IRONLAKE_DP_N_MIN 1
  294. #define IRONLAKE_DP_N_MAX 2
  295. #define IRONLAKE_DP_M_MIN 81
  296. #define IRONLAKE_DP_M_MAX 90
  297. #define IRONLAKE_DP_P_MIN 10
  298. #define IRONLAKE_DP_P_MAX 20
  299. #define IRONLAKE_DP_P2_FAST 10
  300. #define IRONLAKE_DP_P2_SLOW 10
  301. #define IRONLAKE_DP_P2_LIMIT 0
  302. #define IRONLAKE_DP_P1_MIN 1
  303. #define IRONLAKE_DP_P1_MAX 2
  304. /* FDI */
  305. #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
  306. static bool
  307. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  308. int target, int refclk, intel_clock_t *best_clock);
  309. static bool
  310. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  311. int target, int refclk, intel_clock_t *best_clock);
  312. static bool
  313. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  314. int target, int refclk, intel_clock_t *best_clock);
  315. static bool
  316. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  317. int target, int refclk, intel_clock_t *best_clock);
  318. static inline u32 /* units of 100MHz */
  319. intel_fdi_link_freq(struct drm_device *dev)
  320. {
  321. if (IS_GEN5(dev)) {
  322. struct drm_i915_private *dev_priv = dev->dev_private;
  323. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  324. } else
  325. return 27;
  326. }
  327. static const intel_limit_t intel_limits_i8xx_dvo = {
  328. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  329. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  330. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  331. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  332. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  333. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  334. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  335. .p1 = { .min = I8XX_P1_MIN, .max = I8XX_P1_MAX },
  336. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  337. .p2_slow = I8XX_P2_SLOW, .p2_fast = I8XX_P2_FAST },
  338. .find_pll = intel_find_best_PLL,
  339. };
  340. static const intel_limit_t intel_limits_i8xx_lvds = {
  341. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  342. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  343. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  344. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  345. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  346. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  347. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  348. .p1 = { .min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX },
  349. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  350. .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST },
  351. .find_pll = intel_find_best_PLL,
  352. };
  353. static const intel_limit_t intel_limits_i9xx_sdvo = {
  354. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  355. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  356. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  357. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  358. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  359. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  360. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  361. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  362. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  363. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  364. .find_pll = intel_find_best_PLL,
  365. };
  366. static const intel_limit_t intel_limits_i9xx_lvds = {
  367. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  368. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  369. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  370. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  371. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  372. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  373. .p = { .min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX },
  374. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  375. /* The single-channel range is 25-112Mhz, and dual-channel
  376. * is 80-224Mhz. Prefer single channel as much as possible.
  377. */
  378. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  379. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
  380. .find_pll = intel_find_best_PLL,
  381. };
  382. /* below parameter and function is for G4X Chipset Family*/
  383. static const intel_limit_t intel_limits_g4x_sdvo = {
  384. .dot = { .min = G4X_DOT_SDVO_MIN, .max = G4X_DOT_SDVO_MAX },
  385. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  386. .n = { .min = G4X_N_SDVO_MIN, .max = G4X_N_SDVO_MAX },
  387. .m = { .min = G4X_M_SDVO_MIN, .max = G4X_M_SDVO_MAX },
  388. .m1 = { .min = G4X_M1_SDVO_MIN, .max = G4X_M1_SDVO_MAX },
  389. .m2 = { .min = G4X_M2_SDVO_MIN, .max = G4X_M2_SDVO_MAX },
  390. .p = { .min = G4X_P_SDVO_MIN, .max = G4X_P_SDVO_MAX },
  391. .p1 = { .min = G4X_P1_SDVO_MIN, .max = G4X_P1_SDVO_MAX},
  392. .p2 = { .dot_limit = G4X_P2_SDVO_LIMIT,
  393. .p2_slow = G4X_P2_SDVO_SLOW,
  394. .p2_fast = G4X_P2_SDVO_FAST
  395. },
  396. .find_pll = intel_g4x_find_best_PLL,
  397. };
  398. static const intel_limit_t intel_limits_g4x_hdmi = {
  399. .dot = { .min = G4X_DOT_HDMI_DAC_MIN, .max = G4X_DOT_HDMI_DAC_MAX },
  400. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  401. .n = { .min = G4X_N_HDMI_DAC_MIN, .max = G4X_N_HDMI_DAC_MAX },
  402. .m = { .min = G4X_M_HDMI_DAC_MIN, .max = G4X_M_HDMI_DAC_MAX },
  403. .m1 = { .min = G4X_M1_HDMI_DAC_MIN, .max = G4X_M1_HDMI_DAC_MAX },
  404. .m2 = { .min = G4X_M2_HDMI_DAC_MIN, .max = G4X_M2_HDMI_DAC_MAX },
  405. .p = { .min = G4X_P_HDMI_DAC_MIN, .max = G4X_P_HDMI_DAC_MAX },
  406. .p1 = { .min = G4X_P1_HDMI_DAC_MIN, .max = G4X_P1_HDMI_DAC_MAX},
  407. .p2 = { .dot_limit = G4X_P2_HDMI_DAC_LIMIT,
  408. .p2_slow = G4X_P2_HDMI_DAC_SLOW,
  409. .p2_fast = G4X_P2_HDMI_DAC_FAST
  410. },
  411. .find_pll = intel_g4x_find_best_PLL,
  412. };
  413. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  414. .dot = { .min = G4X_DOT_SINGLE_CHANNEL_LVDS_MIN,
  415. .max = G4X_DOT_SINGLE_CHANNEL_LVDS_MAX },
  416. .vco = { .min = G4X_VCO_MIN,
  417. .max = G4X_VCO_MAX },
  418. .n = { .min = G4X_N_SINGLE_CHANNEL_LVDS_MIN,
  419. .max = G4X_N_SINGLE_CHANNEL_LVDS_MAX },
  420. .m = { .min = G4X_M_SINGLE_CHANNEL_LVDS_MIN,
  421. .max = G4X_M_SINGLE_CHANNEL_LVDS_MAX },
  422. .m1 = { .min = G4X_M1_SINGLE_CHANNEL_LVDS_MIN,
  423. .max = G4X_M1_SINGLE_CHANNEL_LVDS_MAX },
  424. .m2 = { .min = G4X_M2_SINGLE_CHANNEL_LVDS_MIN,
  425. .max = G4X_M2_SINGLE_CHANNEL_LVDS_MAX },
  426. .p = { .min = G4X_P_SINGLE_CHANNEL_LVDS_MIN,
  427. .max = G4X_P_SINGLE_CHANNEL_LVDS_MAX },
  428. .p1 = { .min = G4X_P1_SINGLE_CHANNEL_LVDS_MIN,
  429. .max = G4X_P1_SINGLE_CHANNEL_LVDS_MAX },
  430. .p2 = { .dot_limit = G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT,
  431. .p2_slow = G4X_P2_SINGLE_CHANNEL_LVDS_SLOW,
  432. .p2_fast = G4X_P2_SINGLE_CHANNEL_LVDS_FAST
  433. },
  434. .find_pll = intel_g4x_find_best_PLL,
  435. };
  436. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  437. .dot = { .min = G4X_DOT_DUAL_CHANNEL_LVDS_MIN,
  438. .max = G4X_DOT_DUAL_CHANNEL_LVDS_MAX },
  439. .vco = { .min = G4X_VCO_MIN,
  440. .max = G4X_VCO_MAX },
  441. .n = { .min = G4X_N_DUAL_CHANNEL_LVDS_MIN,
  442. .max = G4X_N_DUAL_CHANNEL_LVDS_MAX },
  443. .m = { .min = G4X_M_DUAL_CHANNEL_LVDS_MIN,
  444. .max = G4X_M_DUAL_CHANNEL_LVDS_MAX },
  445. .m1 = { .min = G4X_M1_DUAL_CHANNEL_LVDS_MIN,
  446. .max = G4X_M1_DUAL_CHANNEL_LVDS_MAX },
  447. .m2 = { .min = G4X_M2_DUAL_CHANNEL_LVDS_MIN,
  448. .max = G4X_M2_DUAL_CHANNEL_LVDS_MAX },
  449. .p = { .min = G4X_P_DUAL_CHANNEL_LVDS_MIN,
  450. .max = G4X_P_DUAL_CHANNEL_LVDS_MAX },
  451. .p1 = { .min = G4X_P1_DUAL_CHANNEL_LVDS_MIN,
  452. .max = G4X_P1_DUAL_CHANNEL_LVDS_MAX },
  453. .p2 = { .dot_limit = G4X_P2_DUAL_CHANNEL_LVDS_LIMIT,
  454. .p2_slow = G4X_P2_DUAL_CHANNEL_LVDS_SLOW,
  455. .p2_fast = G4X_P2_DUAL_CHANNEL_LVDS_FAST
  456. },
  457. .find_pll = intel_g4x_find_best_PLL,
  458. };
  459. static const intel_limit_t intel_limits_g4x_display_port = {
  460. .dot = { .min = G4X_DOT_DISPLAY_PORT_MIN,
  461. .max = G4X_DOT_DISPLAY_PORT_MAX },
  462. .vco = { .min = G4X_VCO_MIN,
  463. .max = G4X_VCO_MAX},
  464. .n = { .min = G4X_N_DISPLAY_PORT_MIN,
  465. .max = G4X_N_DISPLAY_PORT_MAX },
  466. .m = { .min = G4X_M_DISPLAY_PORT_MIN,
  467. .max = G4X_M_DISPLAY_PORT_MAX },
  468. .m1 = { .min = G4X_M1_DISPLAY_PORT_MIN,
  469. .max = G4X_M1_DISPLAY_PORT_MAX },
  470. .m2 = { .min = G4X_M2_DISPLAY_PORT_MIN,
  471. .max = G4X_M2_DISPLAY_PORT_MAX },
  472. .p = { .min = G4X_P_DISPLAY_PORT_MIN,
  473. .max = G4X_P_DISPLAY_PORT_MAX },
  474. .p1 = { .min = G4X_P1_DISPLAY_PORT_MIN,
  475. .max = G4X_P1_DISPLAY_PORT_MAX},
  476. .p2 = { .dot_limit = G4X_P2_DISPLAY_PORT_LIMIT,
  477. .p2_slow = G4X_P2_DISPLAY_PORT_SLOW,
  478. .p2_fast = G4X_P2_DISPLAY_PORT_FAST },
  479. .find_pll = intel_find_pll_g4x_dp,
  480. };
  481. static const intel_limit_t intel_limits_pineview_sdvo = {
  482. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
  483. .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
  484. .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
  485. .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
  486. .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
  487. .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
  488. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  489. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  490. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  491. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  492. .find_pll = intel_find_best_PLL,
  493. };
  494. static const intel_limit_t intel_limits_pineview_lvds = {
  495. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  496. .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
  497. .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
  498. .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
  499. .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
  500. .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
  501. .p = { .min = PINEVIEW_P_LVDS_MIN, .max = PINEVIEW_P_LVDS_MAX },
  502. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  503. /* Pineview only supports single-channel mode. */
  504. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  505. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_SLOW },
  506. .find_pll = intel_find_best_PLL,
  507. };
  508. static const intel_limit_t intel_limits_ironlake_dac = {
  509. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  510. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  511. .n = { .min = IRONLAKE_DAC_N_MIN, .max = IRONLAKE_DAC_N_MAX },
  512. .m = { .min = IRONLAKE_DAC_M_MIN, .max = IRONLAKE_DAC_M_MAX },
  513. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  514. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  515. .p = { .min = IRONLAKE_DAC_P_MIN, .max = IRONLAKE_DAC_P_MAX },
  516. .p1 = { .min = IRONLAKE_DAC_P1_MIN, .max = IRONLAKE_DAC_P1_MAX },
  517. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  518. .p2_slow = IRONLAKE_DAC_P2_SLOW,
  519. .p2_fast = IRONLAKE_DAC_P2_FAST },
  520. .find_pll = intel_g4x_find_best_PLL,
  521. };
  522. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  523. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  524. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  525. .n = { .min = IRONLAKE_LVDS_S_N_MIN, .max = IRONLAKE_LVDS_S_N_MAX },
  526. .m = { .min = IRONLAKE_LVDS_S_M_MIN, .max = IRONLAKE_LVDS_S_M_MAX },
  527. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  528. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  529. .p = { .min = IRONLAKE_LVDS_S_P_MIN, .max = IRONLAKE_LVDS_S_P_MAX },
  530. .p1 = { .min = IRONLAKE_LVDS_S_P1_MIN, .max = IRONLAKE_LVDS_S_P1_MAX },
  531. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  532. .p2_slow = IRONLAKE_LVDS_S_P2_SLOW,
  533. .p2_fast = IRONLAKE_LVDS_S_P2_FAST },
  534. .find_pll = intel_g4x_find_best_PLL,
  535. };
  536. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  537. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  538. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  539. .n = { .min = IRONLAKE_LVDS_D_N_MIN, .max = IRONLAKE_LVDS_D_N_MAX },
  540. .m = { .min = IRONLAKE_LVDS_D_M_MIN, .max = IRONLAKE_LVDS_D_M_MAX },
  541. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  542. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  543. .p = { .min = IRONLAKE_LVDS_D_P_MIN, .max = IRONLAKE_LVDS_D_P_MAX },
  544. .p1 = { .min = IRONLAKE_LVDS_D_P1_MIN, .max = IRONLAKE_LVDS_D_P1_MAX },
  545. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  546. .p2_slow = IRONLAKE_LVDS_D_P2_SLOW,
  547. .p2_fast = IRONLAKE_LVDS_D_P2_FAST },
  548. .find_pll = intel_g4x_find_best_PLL,
  549. };
  550. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  551. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  552. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  553. .n = { .min = IRONLAKE_LVDS_S_SSC_N_MIN, .max = IRONLAKE_LVDS_S_SSC_N_MAX },
  554. .m = { .min = IRONLAKE_LVDS_S_SSC_M_MIN, .max = IRONLAKE_LVDS_S_SSC_M_MAX },
  555. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  556. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  557. .p = { .min = IRONLAKE_LVDS_S_SSC_P_MIN, .max = IRONLAKE_LVDS_S_SSC_P_MAX },
  558. .p1 = { .min = IRONLAKE_LVDS_S_SSC_P1_MIN,.max = IRONLAKE_LVDS_S_SSC_P1_MAX },
  559. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  560. .p2_slow = IRONLAKE_LVDS_S_SSC_P2_SLOW,
  561. .p2_fast = IRONLAKE_LVDS_S_SSC_P2_FAST },
  562. .find_pll = intel_g4x_find_best_PLL,
  563. };
  564. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  565. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  566. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  567. .n = { .min = IRONLAKE_LVDS_D_SSC_N_MIN, .max = IRONLAKE_LVDS_D_SSC_N_MAX },
  568. .m = { .min = IRONLAKE_LVDS_D_SSC_M_MIN, .max = IRONLAKE_LVDS_D_SSC_M_MAX },
  569. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  570. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  571. .p = { .min = IRONLAKE_LVDS_D_SSC_P_MIN, .max = IRONLAKE_LVDS_D_SSC_P_MAX },
  572. .p1 = { .min = IRONLAKE_LVDS_D_SSC_P1_MIN,.max = IRONLAKE_LVDS_D_SSC_P1_MAX },
  573. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  574. .p2_slow = IRONLAKE_LVDS_D_SSC_P2_SLOW,
  575. .p2_fast = IRONLAKE_LVDS_D_SSC_P2_FAST },
  576. .find_pll = intel_g4x_find_best_PLL,
  577. };
  578. static const intel_limit_t intel_limits_ironlake_display_port = {
  579. .dot = { .min = IRONLAKE_DOT_MIN,
  580. .max = IRONLAKE_DOT_MAX },
  581. .vco = { .min = IRONLAKE_VCO_MIN,
  582. .max = IRONLAKE_VCO_MAX},
  583. .n = { .min = IRONLAKE_DP_N_MIN,
  584. .max = IRONLAKE_DP_N_MAX },
  585. .m = { .min = IRONLAKE_DP_M_MIN,
  586. .max = IRONLAKE_DP_M_MAX },
  587. .m1 = { .min = IRONLAKE_M1_MIN,
  588. .max = IRONLAKE_M1_MAX },
  589. .m2 = { .min = IRONLAKE_M2_MIN,
  590. .max = IRONLAKE_M2_MAX },
  591. .p = { .min = IRONLAKE_DP_P_MIN,
  592. .max = IRONLAKE_DP_P_MAX },
  593. .p1 = { .min = IRONLAKE_DP_P1_MIN,
  594. .max = IRONLAKE_DP_P1_MAX},
  595. .p2 = { .dot_limit = IRONLAKE_DP_P2_LIMIT,
  596. .p2_slow = IRONLAKE_DP_P2_SLOW,
  597. .p2_fast = IRONLAKE_DP_P2_FAST },
  598. .find_pll = intel_find_pll_ironlake_dp,
  599. };
  600. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc)
  601. {
  602. struct drm_device *dev = crtc->dev;
  603. struct drm_i915_private *dev_priv = dev->dev_private;
  604. const intel_limit_t *limit;
  605. int refclk = 120;
  606. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  607. if (dev_priv->lvds_use_ssc && dev_priv->lvds_ssc_freq == 100)
  608. refclk = 100;
  609. if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
  610. LVDS_CLKB_POWER_UP) {
  611. /* LVDS dual channel */
  612. if (refclk == 100)
  613. limit = &intel_limits_ironlake_dual_lvds_100m;
  614. else
  615. limit = &intel_limits_ironlake_dual_lvds;
  616. } else {
  617. if (refclk == 100)
  618. limit = &intel_limits_ironlake_single_lvds_100m;
  619. else
  620. limit = &intel_limits_ironlake_single_lvds;
  621. }
  622. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  623. HAS_eDP)
  624. limit = &intel_limits_ironlake_display_port;
  625. else
  626. limit = &intel_limits_ironlake_dac;
  627. return limit;
  628. }
  629. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  630. {
  631. struct drm_device *dev = crtc->dev;
  632. struct drm_i915_private *dev_priv = dev->dev_private;
  633. const intel_limit_t *limit;
  634. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  635. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  636. LVDS_CLKB_POWER_UP)
  637. /* LVDS with dual channel */
  638. limit = &intel_limits_g4x_dual_channel_lvds;
  639. else
  640. /* LVDS with dual channel */
  641. limit = &intel_limits_g4x_single_channel_lvds;
  642. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  643. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  644. limit = &intel_limits_g4x_hdmi;
  645. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  646. limit = &intel_limits_g4x_sdvo;
  647. } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  648. limit = &intel_limits_g4x_display_port;
  649. } else /* The option is for other outputs */
  650. limit = &intel_limits_i9xx_sdvo;
  651. return limit;
  652. }
  653. static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
  654. {
  655. struct drm_device *dev = crtc->dev;
  656. const intel_limit_t *limit;
  657. if (HAS_PCH_SPLIT(dev))
  658. limit = intel_ironlake_limit(crtc);
  659. else if (IS_G4X(dev)) {
  660. limit = intel_g4x_limit(crtc);
  661. } else if (IS_PINEVIEW(dev)) {
  662. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  663. limit = &intel_limits_pineview_lvds;
  664. else
  665. limit = &intel_limits_pineview_sdvo;
  666. } else if (!IS_GEN2(dev)) {
  667. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  668. limit = &intel_limits_i9xx_lvds;
  669. else
  670. limit = &intel_limits_i9xx_sdvo;
  671. } else {
  672. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  673. limit = &intel_limits_i8xx_lvds;
  674. else
  675. limit = &intel_limits_i8xx_dvo;
  676. }
  677. return limit;
  678. }
  679. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  680. static void pineview_clock(int refclk, intel_clock_t *clock)
  681. {
  682. clock->m = clock->m2 + 2;
  683. clock->p = clock->p1 * clock->p2;
  684. clock->vco = refclk * clock->m / clock->n;
  685. clock->dot = clock->vco / clock->p;
  686. }
  687. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  688. {
  689. if (IS_PINEVIEW(dev)) {
  690. pineview_clock(refclk, clock);
  691. return;
  692. }
  693. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  694. clock->p = clock->p1 * clock->p2;
  695. clock->vco = refclk * clock->m / (clock->n + 2);
  696. clock->dot = clock->vco / clock->p;
  697. }
  698. /**
  699. * Returns whether any output on the specified pipe is of the specified type
  700. */
  701. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  702. {
  703. struct drm_device *dev = crtc->dev;
  704. struct drm_mode_config *mode_config = &dev->mode_config;
  705. struct intel_encoder *encoder;
  706. list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
  707. if (encoder->base.crtc == crtc && encoder->type == type)
  708. return true;
  709. return false;
  710. }
  711. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  712. /**
  713. * Returns whether the given set of divisors are valid for a given refclk with
  714. * the given connectors.
  715. */
  716. static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
  717. {
  718. const intel_limit_t *limit = intel_limit (crtc);
  719. struct drm_device *dev = crtc->dev;
  720. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  721. INTELPllInvalid ("p1 out of range\n");
  722. if (clock->p < limit->p.min || limit->p.max < clock->p)
  723. INTELPllInvalid ("p out of range\n");
  724. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  725. INTELPllInvalid ("m2 out of range\n");
  726. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  727. INTELPllInvalid ("m1 out of range\n");
  728. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  729. INTELPllInvalid ("m1 <= m2\n");
  730. if (clock->m < limit->m.min || limit->m.max < clock->m)
  731. INTELPllInvalid ("m out of range\n");
  732. if (clock->n < limit->n.min || limit->n.max < clock->n)
  733. INTELPllInvalid ("n out of range\n");
  734. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  735. INTELPllInvalid ("vco out of range\n");
  736. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  737. * connector, etc., rather than just a single range.
  738. */
  739. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  740. INTELPllInvalid ("dot out of range\n");
  741. return true;
  742. }
  743. static bool
  744. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  745. int target, int refclk, intel_clock_t *best_clock)
  746. {
  747. struct drm_device *dev = crtc->dev;
  748. struct drm_i915_private *dev_priv = dev->dev_private;
  749. intel_clock_t clock;
  750. int err = target;
  751. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  752. (I915_READ(LVDS)) != 0) {
  753. /*
  754. * For LVDS, if the panel is on, just rely on its current
  755. * settings for dual-channel. We haven't figured out how to
  756. * reliably set up different single/dual channel state, if we
  757. * even can.
  758. */
  759. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  760. LVDS_CLKB_POWER_UP)
  761. clock.p2 = limit->p2.p2_fast;
  762. else
  763. clock.p2 = limit->p2.p2_slow;
  764. } else {
  765. if (target < limit->p2.dot_limit)
  766. clock.p2 = limit->p2.p2_slow;
  767. else
  768. clock.p2 = limit->p2.p2_fast;
  769. }
  770. memset (best_clock, 0, sizeof (*best_clock));
  771. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  772. clock.m1++) {
  773. for (clock.m2 = limit->m2.min;
  774. clock.m2 <= limit->m2.max; clock.m2++) {
  775. /* m1 is always 0 in Pineview */
  776. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  777. break;
  778. for (clock.n = limit->n.min;
  779. clock.n <= limit->n.max; clock.n++) {
  780. for (clock.p1 = limit->p1.min;
  781. clock.p1 <= limit->p1.max; clock.p1++) {
  782. int this_err;
  783. intel_clock(dev, refclk, &clock);
  784. if (!intel_PLL_is_valid(crtc, &clock))
  785. continue;
  786. this_err = abs(clock.dot - target);
  787. if (this_err < err) {
  788. *best_clock = clock;
  789. err = this_err;
  790. }
  791. }
  792. }
  793. }
  794. }
  795. return (err != target);
  796. }
  797. static bool
  798. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  799. int target, int refclk, intel_clock_t *best_clock)
  800. {
  801. struct drm_device *dev = crtc->dev;
  802. struct drm_i915_private *dev_priv = dev->dev_private;
  803. intel_clock_t clock;
  804. int max_n;
  805. bool found;
  806. /* approximately equals target * 0.00585 */
  807. int err_most = (target >> 8) + (target >> 9);
  808. found = false;
  809. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  810. int lvds_reg;
  811. if (HAS_PCH_SPLIT(dev))
  812. lvds_reg = PCH_LVDS;
  813. else
  814. lvds_reg = LVDS;
  815. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  816. LVDS_CLKB_POWER_UP)
  817. clock.p2 = limit->p2.p2_fast;
  818. else
  819. clock.p2 = limit->p2.p2_slow;
  820. } else {
  821. if (target < limit->p2.dot_limit)
  822. clock.p2 = limit->p2.p2_slow;
  823. else
  824. clock.p2 = limit->p2.p2_fast;
  825. }
  826. memset(best_clock, 0, sizeof(*best_clock));
  827. max_n = limit->n.max;
  828. /* based on hardware requirement, prefer smaller n to precision */
  829. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  830. /* based on hardware requirement, prefere larger m1,m2 */
  831. for (clock.m1 = limit->m1.max;
  832. clock.m1 >= limit->m1.min; clock.m1--) {
  833. for (clock.m2 = limit->m2.max;
  834. clock.m2 >= limit->m2.min; clock.m2--) {
  835. for (clock.p1 = limit->p1.max;
  836. clock.p1 >= limit->p1.min; clock.p1--) {
  837. int this_err;
  838. intel_clock(dev, refclk, &clock);
  839. if (!intel_PLL_is_valid(crtc, &clock))
  840. continue;
  841. this_err = abs(clock.dot - target) ;
  842. if (this_err < err_most) {
  843. *best_clock = clock;
  844. err_most = this_err;
  845. max_n = clock.n;
  846. found = true;
  847. }
  848. }
  849. }
  850. }
  851. }
  852. return found;
  853. }
  854. static bool
  855. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  856. int target, int refclk, intel_clock_t *best_clock)
  857. {
  858. struct drm_device *dev = crtc->dev;
  859. intel_clock_t clock;
  860. if (target < 200000) {
  861. clock.n = 1;
  862. clock.p1 = 2;
  863. clock.p2 = 10;
  864. clock.m1 = 12;
  865. clock.m2 = 9;
  866. } else {
  867. clock.n = 2;
  868. clock.p1 = 1;
  869. clock.p2 = 10;
  870. clock.m1 = 14;
  871. clock.m2 = 8;
  872. }
  873. intel_clock(dev, refclk, &clock);
  874. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  875. return true;
  876. }
  877. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  878. static bool
  879. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  880. int target, int refclk, intel_clock_t *best_clock)
  881. {
  882. intel_clock_t clock;
  883. if (target < 200000) {
  884. clock.p1 = 2;
  885. clock.p2 = 10;
  886. clock.n = 2;
  887. clock.m1 = 23;
  888. clock.m2 = 8;
  889. } else {
  890. clock.p1 = 1;
  891. clock.p2 = 10;
  892. clock.n = 1;
  893. clock.m1 = 14;
  894. clock.m2 = 2;
  895. }
  896. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  897. clock.p = (clock.p1 * clock.p2);
  898. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  899. clock.vco = 0;
  900. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  901. return true;
  902. }
  903. /**
  904. * intel_wait_for_vblank - wait for vblank on a given pipe
  905. * @dev: drm device
  906. * @pipe: pipe to wait for
  907. *
  908. * Wait for vblank to occur on a given pipe. Needed for various bits of
  909. * mode setting code.
  910. */
  911. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  912. {
  913. struct drm_i915_private *dev_priv = dev->dev_private;
  914. int pipestat_reg = (pipe == 0 ? PIPEASTAT : PIPEBSTAT);
  915. /* Clear existing vblank status. Note this will clear any other
  916. * sticky status fields as well.
  917. *
  918. * This races with i915_driver_irq_handler() with the result
  919. * that either function could miss a vblank event. Here it is not
  920. * fatal, as we will either wait upon the next vblank interrupt or
  921. * timeout. Generally speaking intel_wait_for_vblank() is only
  922. * called during modeset at which time the GPU should be idle and
  923. * should *not* be performing page flips and thus not waiting on
  924. * vblanks...
  925. * Currently, the result of us stealing a vblank from the irq
  926. * handler is that a single frame will be skipped during swapbuffers.
  927. */
  928. I915_WRITE(pipestat_reg,
  929. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  930. /* Wait for vblank interrupt bit to set */
  931. if (wait_for(I915_READ(pipestat_reg) &
  932. PIPE_VBLANK_INTERRUPT_STATUS,
  933. 50))
  934. DRM_DEBUG_KMS("vblank wait timed out\n");
  935. }
  936. /*
  937. * intel_wait_for_pipe_off - wait for pipe to turn off
  938. * @dev: drm device
  939. * @pipe: pipe to wait for
  940. *
  941. * After disabling a pipe, we can't wait for vblank in the usual way,
  942. * spinning on the vblank interrupt status bit, since we won't actually
  943. * see an interrupt when the pipe is disabled.
  944. *
  945. * On Gen4 and above:
  946. * wait for the pipe register state bit to turn off
  947. *
  948. * Otherwise:
  949. * wait for the display line value to settle (it usually
  950. * ends up stopping at the start of the next frame).
  951. *
  952. */
  953. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  954. {
  955. struct drm_i915_private *dev_priv = dev->dev_private;
  956. if (INTEL_INFO(dev)->gen >= 4) {
  957. int reg = PIPECONF(pipe);
  958. /* Wait for the Pipe State to go off */
  959. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  960. 100))
  961. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  962. } else {
  963. u32 last_line;
  964. int reg = PIPEDSL(pipe);
  965. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  966. /* Wait for the display line to settle */
  967. do {
  968. last_line = I915_READ(reg) & DSL_LINEMASK;
  969. mdelay(5);
  970. } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
  971. time_after(timeout, jiffies));
  972. if (time_after(jiffies, timeout))
  973. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  974. }
  975. }
  976. static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  977. {
  978. struct drm_device *dev = crtc->dev;
  979. struct drm_i915_private *dev_priv = dev->dev_private;
  980. struct drm_framebuffer *fb = crtc->fb;
  981. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  982. struct drm_i915_gem_object *obj = intel_fb->obj;
  983. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  984. int plane, i;
  985. u32 fbc_ctl, fbc_ctl2;
  986. if (fb->pitch == dev_priv->cfb_pitch &&
  987. obj->fence_reg == dev_priv->cfb_fence &&
  988. intel_crtc->plane == dev_priv->cfb_plane &&
  989. I915_READ(FBC_CONTROL) & FBC_CTL_EN)
  990. return;
  991. i8xx_disable_fbc(dev);
  992. dev_priv->cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  993. if (fb->pitch < dev_priv->cfb_pitch)
  994. dev_priv->cfb_pitch = fb->pitch;
  995. /* FBC_CTL wants 64B units */
  996. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  997. dev_priv->cfb_fence = obj->fence_reg;
  998. dev_priv->cfb_plane = intel_crtc->plane;
  999. plane = dev_priv->cfb_plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  1000. /* Clear old tags */
  1001. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  1002. I915_WRITE(FBC_TAG + (i * 4), 0);
  1003. /* Set it up... */
  1004. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | plane;
  1005. if (obj->tiling_mode != I915_TILING_NONE)
  1006. fbc_ctl2 |= FBC_CTL_CPU_FENCE;
  1007. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  1008. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  1009. /* enable it... */
  1010. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  1011. if (IS_I945GM(dev))
  1012. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  1013. fbc_ctl |= (dev_priv->cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  1014. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  1015. if (obj->tiling_mode != I915_TILING_NONE)
  1016. fbc_ctl |= dev_priv->cfb_fence;
  1017. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1018. DRM_DEBUG_KMS("enabled FBC, pitch %ld, yoff %d, plane %d, ",
  1019. dev_priv->cfb_pitch, crtc->y, dev_priv->cfb_plane);
  1020. }
  1021. void i8xx_disable_fbc(struct drm_device *dev)
  1022. {
  1023. struct drm_i915_private *dev_priv = dev->dev_private;
  1024. u32 fbc_ctl;
  1025. /* Disable compression */
  1026. fbc_ctl = I915_READ(FBC_CONTROL);
  1027. if ((fbc_ctl & FBC_CTL_EN) == 0)
  1028. return;
  1029. fbc_ctl &= ~FBC_CTL_EN;
  1030. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1031. /* Wait for compressing bit to clear */
  1032. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  1033. DRM_DEBUG_KMS("FBC idle timed out\n");
  1034. return;
  1035. }
  1036. DRM_DEBUG_KMS("disabled FBC\n");
  1037. }
  1038. static bool i8xx_fbc_enabled(struct drm_device *dev)
  1039. {
  1040. struct drm_i915_private *dev_priv = dev->dev_private;
  1041. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  1042. }
  1043. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1044. {
  1045. struct drm_device *dev = crtc->dev;
  1046. struct drm_i915_private *dev_priv = dev->dev_private;
  1047. struct drm_framebuffer *fb = crtc->fb;
  1048. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1049. struct drm_i915_gem_object *obj = intel_fb->obj;
  1050. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1051. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1052. unsigned long stall_watermark = 200;
  1053. u32 dpfc_ctl;
  1054. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1055. if (dpfc_ctl & DPFC_CTL_EN) {
  1056. if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
  1057. dev_priv->cfb_fence == obj->fence_reg &&
  1058. dev_priv->cfb_plane == intel_crtc->plane &&
  1059. dev_priv->cfb_y == crtc->y)
  1060. return;
  1061. I915_WRITE(DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
  1062. POSTING_READ(DPFC_CONTROL);
  1063. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1064. }
  1065. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  1066. dev_priv->cfb_fence = obj->fence_reg;
  1067. dev_priv->cfb_plane = intel_crtc->plane;
  1068. dev_priv->cfb_y = crtc->y;
  1069. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  1070. if (obj->tiling_mode != I915_TILING_NONE) {
  1071. dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;
  1072. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  1073. } else {
  1074. I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  1075. }
  1076. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1077. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1078. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1079. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  1080. /* enable it... */
  1081. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  1082. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1083. }
  1084. void g4x_disable_fbc(struct drm_device *dev)
  1085. {
  1086. struct drm_i915_private *dev_priv = dev->dev_private;
  1087. u32 dpfc_ctl;
  1088. /* Disable compression */
  1089. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1090. if (dpfc_ctl & DPFC_CTL_EN) {
  1091. dpfc_ctl &= ~DPFC_CTL_EN;
  1092. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  1093. DRM_DEBUG_KMS("disabled FBC\n");
  1094. }
  1095. }
  1096. static bool g4x_fbc_enabled(struct drm_device *dev)
  1097. {
  1098. struct drm_i915_private *dev_priv = dev->dev_private;
  1099. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  1100. }
  1101. static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1102. {
  1103. struct drm_device *dev = crtc->dev;
  1104. struct drm_i915_private *dev_priv = dev->dev_private;
  1105. struct drm_framebuffer *fb = crtc->fb;
  1106. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1107. struct drm_i915_gem_object *obj = intel_fb->obj;
  1108. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1109. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1110. unsigned long stall_watermark = 200;
  1111. u32 dpfc_ctl;
  1112. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1113. if (dpfc_ctl & DPFC_CTL_EN) {
  1114. if (dev_priv->cfb_pitch == dev_priv->cfb_pitch / 64 - 1 &&
  1115. dev_priv->cfb_fence == obj->fence_reg &&
  1116. dev_priv->cfb_plane == intel_crtc->plane &&
  1117. dev_priv->cfb_offset == obj->gtt_offset &&
  1118. dev_priv->cfb_y == crtc->y)
  1119. return;
  1120. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
  1121. POSTING_READ(ILK_DPFC_CONTROL);
  1122. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1123. }
  1124. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  1125. dev_priv->cfb_fence = obj->fence_reg;
  1126. dev_priv->cfb_plane = intel_crtc->plane;
  1127. dev_priv->cfb_offset = obj->gtt_offset;
  1128. dev_priv->cfb_y = crtc->y;
  1129. dpfc_ctl &= DPFC_RESERVED;
  1130. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  1131. if (obj->tiling_mode != I915_TILING_NONE) {
  1132. dpfc_ctl |= (DPFC_CTL_FENCE_EN | dev_priv->cfb_fence);
  1133. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  1134. } else {
  1135. I915_WRITE(ILK_DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  1136. }
  1137. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1138. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1139. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1140. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  1141. I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
  1142. /* enable it... */
  1143. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  1144. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1145. }
  1146. void ironlake_disable_fbc(struct drm_device *dev)
  1147. {
  1148. struct drm_i915_private *dev_priv = dev->dev_private;
  1149. u32 dpfc_ctl;
  1150. /* Disable compression */
  1151. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1152. if (dpfc_ctl & DPFC_CTL_EN) {
  1153. dpfc_ctl &= ~DPFC_CTL_EN;
  1154. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  1155. DRM_DEBUG_KMS("disabled FBC\n");
  1156. }
  1157. }
  1158. static bool ironlake_fbc_enabled(struct drm_device *dev)
  1159. {
  1160. struct drm_i915_private *dev_priv = dev->dev_private;
  1161. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  1162. }
  1163. bool intel_fbc_enabled(struct drm_device *dev)
  1164. {
  1165. struct drm_i915_private *dev_priv = dev->dev_private;
  1166. if (!dev_priv->display.fbc_enabled)
  1167. return false;
  1168. return dev_priv->display.fbc_enabled(dev);
  1169. }
  1170. void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1171. {
  1172. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  1173. if (!dev_priv->display.enable_fbc)
  1174. return;
  1175. dev_priv->display.enable_fbc(crtc, interval);
  1176. }
  1177. void intel_disable_fbc(struct drm_device *dev)
  1178. {
  1179. struct drm_i915_private *dev_priv = dev->dev_private;
  1180. if (!dev_priv->display.disable_fbc)
  1181. return;
  1182. dev_priv->display.disable_fbc(dev);
  1183. }
  1184. /**
  1185. * intel_update_fbc - enable/disable FBC as needed
  1186. * @dev: the drm_device
  1187. *
  1188. * Set up the framebuffer compression hardware at mode set time. We
  1189. * enable it if possible:
  1190. * - plane A only (on pre-965)
  1191. * - no pixel mulitply/line duplication
  1192. * - no alpha buffer discard
  1193. * - no dual wide
  1194. * - framebuffer <= 2048 in width, 1536 in height
  1195. *
  1196. * We can't assume that any compression will take place (worst case),
  1197. * so the compressed buffer has to be the same size as the uncompressed
  1198. * one. It also must reside (along with the line length buffer) in
  1199. * stolen memory.
  1200. *
  1201. * We need to enable/disable FBC on a global basis.
  1202. */
  1203. static void intel_update_fbc(struct drm_device *dev)
  1204. {
  1205. struct drm_i915_private *dev_priv = dev->dev_private;
  1206. struct drm_crtc *crtc = NULL, *tmp_crtc;
  1207. struct intel_crtc *intel_crtc;
  1208. struct drm_framebuffer *fb;
  1209. struct intel_framebuffer *intel_fb;
  1210. struct drm_i915_gem_object *obj;
  1211. DRM_DEBUG_KMS("\n");
  1212. if (!i915_powersave)
  1213. return;
  1214. if (!I915_HAS_FBC(dev))
  1215. return;
  1216. /*
  1217. * If FBC is already on, we just have to verify that we can
  1218. * keep it that way...
  1219. * Need to disable if:
  1220. * - more than one pipe is active
  1221. * - changing FBC params (stride, fence, mode)
  1222. * - new fb is too large to fit in compressed buffer
  1223. * - going to an unsupported config (interlace, pixel multiply, etc.)
  1224. */
  1225. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  1226. if (tmp_crtc->enabled) {
  1227. if (crtc) {
  1228. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  1229. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  1230. goto out_disable;
  1231. }
  1232. crtc = tmp_crtc;
  1233. }
  1234. }
  1235. if (!crtc || crtc->fb == NULL) {
  1236. DRM_DEBUG_KMS("no output, disabling\n");
  1237. dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
  1238. goto out_disable;
  1239. }
  1240. intel_crtc = to_intel_crtc(crtc);
  1241. fb = crtc->fb;
  1242. intel_fb = to_intel_framebuffer(fb);
  1243. obj = intel_fb->obj;
  1244. if (intel_fb->obj->base.size > dev_priv->cfb_size) {
  1245. DRM_DEBUG_KMS("framebuffer too large, disabling "
  1246. "compression\n");
  1247. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  1248. goto out_disable;
  1249. }
  1250. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  1251. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  1252. DRM_DEBUG_KMS("mode incompatible with compression, "
  1253. "disabling\n");
  1254. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  1255. goto out_disable;
  1256. }
  1257. if ((crtc->mode.hdisplay > 2048) ||
  1258. (crtc->mode.vdisplay > 1536)) {
  1259. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  1260. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  1261. goto out_disable;
  1262. }
  1263. if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
  1264. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  1265. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  1266. goto out_disable;
  1267. }
  1268. if (obj->tiling_mode != I915_TILING_X) {
  1269. DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
  1270. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  1271. goto out_disable;
  1272. }
  1273. /* If the kernel debugger is active, always disable compression */
  1274. if (in_dbg_master())
  1275. goto out_disable;
  1276. intel_enable_fbc(crtc, 500);
  1277. return;
  1278. out_disable:
  1279. /* Multiple disables should be harmless */
  1280. if (intel_fbc_enabled(dev)) {
  1281. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  1282. intel_disable_fbc(dev);
  1283. }
  1284. }
  1285. int
  1286. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1287. struct drm_i915_gem_object *obj,
  1288. struct intel_ring_buffer *pipelined)
  1289. {
  1290. u32 alignment;
  1291. int ret;
  1292. switch (obj->tiling_mode) {
  1293. case I915_TILING_NONE:
  1294. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1295. alignment = 128 * 1024;
  1296. else if (INTEL_INFO(dev)->gen >= 4)
  1297. alignment = 4 * 1024;
  1298. else
  1299. alignment = 64 * 1024;
  1300. break;
  1301. case I915_TILING_X:
  1302. /* pin() will align the object as required by fence */
  1303. alignment = 0;
  1304. break;
  1305. case I915_TILING_Y:
  1306. /* FIXME: Is this true? */
  1307. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1308. return -EINVAL;
  1309. default:
  1310. BUG();
  1311. }
  1312. ret = i915_gem_object_pin(obj, alignment, true);
  1313. if (ret)
  1314. return ret;
  1315. ret = i915_gem_object_set_to_display_plane(obj, pipelined);
  1316. if (ret)
  1317. goto err_unpin;
  1318. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1319. * fence, whereas 965+ only requires a fence if using
  1320. * framebuffer compression. For simplicity, we always install
  1321. * a fence as the cost is not that onerous.
  1322. */
  1323. if (obj->tiling_mode != I915_TILING_NONE) {
  1324. ret = i915_gem_object_get_fence(obj, pipelined, false);
  1325. if (ret)
  1326. goto err_unpin;
  1327. }
  1328. return 0;
  1329. err_unpin:
  1330. i915_gem_object_unpin(obj);
  1331. return ret;
  1332. }
  1333. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1334. static int
  1335. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1336. int x, int y, enum mode_set_atomic state)
  1337. {
  1338. struct drm_device *dev = crtc->dev;
  1339. struct drm_i915_private *dev_priv = dev->dev_private;
  1340. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1341. struct intel_framebuffer *intel_fb;
  1342. struct drm_i915_gem_object *obj;
  1343. int plane = intel_crtc->plane;
  1344. unsigned long Start, Offset;
  1345. u32 dspcntr;
  1346. u32 reg;
  1347. switch (plane) {
  1348. case 0:
  1349. case 1:
  1350. break;
  1351. default:
  1352. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1353. return -EINVAL;
  1354. }
  1355. intel_fb = to_intel_framebuffer(fb);
  1356. obj = intel_fb->obj;
  1357. reg = DSPCNTR(plane);
  1358. dspcntr = I915_READ(reg);
  1359. /* Mask out pixel format bits in case we change it */
  1360. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1361. switch (fb->bits_per_pixel) {
  1362. case 8:
  1363. dspcntr |= DISPPLANE_8BPP;
  1364. break;
  1365. case 16:
  1366. if (fb->depth == 15)
  1367. dspcntr |= DISPPLANE_15_16BPP;
  1368. else
  1369. dspcntr |= DISPPLANE_16BPP;
  1370. break;
  1371. case 24:
  1372. case 32:
  1373. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1374. break;
  1375. default:
  1376. DRM_ERROR("Unknown color depth\n");
  1377. return -EINVAL;
  1378. }
  1379. if (INTEL_INFO(dev)->gen >= 4) {
  1380. if (obj->tiling_mode != I915_TILING_NONE)
  1381. dspcntr |= DISPPLANE_TILED;
  1382. else
  1383. dspcntr &= ~DISPPLANE_TILED;
  1384. }
  1385. if (HAS_PCH_SPLIT(dev))
  1386. /* must disable */
  1387. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1388. I915_WRITE(reg, dspcntr);
  1389. Start = obj->gtt_offset;
  1390. Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
  1391. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1392. Start, Offset, x, y, fb->pitch);
  1393. I915_WRITE(DSPSTRIDE(plane), fb->pitch);
  1394. if (INTEL_INFO(dev)->gen >= 4) {
  1395. I915_WRITE(DSPSURF(plane), Start);
  1396. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1397. I915_WRITE(DSPADDR(plane), Offset);
  1398. } else
  1399. I915_WRITE(DSPADDR(plane), Start + Offset);
  1400. POSTING_READ(reg);
  1401. intel_update_fbc(dev);
  1402. intel_increase_pllclock(crtc);
  1403. return 0;
  1404. }
  1405. static int
  1406. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1407. struct drm_framebuffer *old_fb)
  1408. {
  1409. struct drm_device *dev = crtc->dev;
  1410. struct drm_i915_master_private *master_priv;
  1411. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1412. int ret;
  1413. /* no fb bound */
  1414. if (!crtc->fb) {
  1415. DRM_DEBUG_KMS("No FB bound\n");
  1416. return 0;
  1417. }
  1418. switch (intel_crtc->plane) {
  1419. case 0:
  1420. case 1:
  1421. break;
  1422. default:
  1423. return -EINVAL;
  1424. }
  1425. mutex_lock(&dev->struct_mutex);
  1426. ret = intel_pin_and_fence_fb_obj(dev,
  1427. to_intel_framebuffer(crtc->fb)->obj,
  1428. NULL);
  1429. if (ret != 0) {
  1430. mutex_unlock(&dev->struct_mutex);
  1431. return ret;
  1432. }
  1433. if (old_fb) {
  1434. struct drm_i915_private *dev_priv = dev->dev_private;
  1435. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1436. wait_event(dev_priv->pending_flip_queue,
  1437. atomic_read(&obj->pending_flip) == 0);
  1438. /* Big Hammer, we also need to ensure that any pending
  1439. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1440. * current scanout is retired before unpinning the old
  1441. * framebuffer.
  1442. */
  1443. ret = i915_gem_object_flush_gpu(obj, false);
  1444. if (ret) {
  1445. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  1446. mutex_unlock(&dev->struct_mutex);
  1447. return ret;
  1448. }
  1449. }
  1450. ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
  1451. LEAVE_ATOMIC_MODE_SET);
  1452. if (ret) {
  1453. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  1454. mutex_unlock(&dev->struct_mutex);
  1455. return ret;
  1456. }
  1457. if (old_fb)
  1458. i915_gem_object_unpin(to_intel_framebuffer(old_fb)->obj);
  1459. mutex_unlock(&dev->struct_mutex);
  1460. if (!dev->primary->master)
  1461. return 0;
  1462. master_priv = dev->primary->master->driver_priv;
  1463. if (!master_priv->sarea_priv)
  1464. return 0;
  1465. if (intel_crtc->pipe) {
  1466. master_priv->sarea_priv->pipeB_x = x;
  1467. master_priv->sarea_priv->pipeB_y = y;
  1468. } else {
  1469. master_priv->sarea_priv->pipeA_x = x;
  1470. master_priv->sarea_priv->pipeA_y = y;
  1471. }
  1472. return 0;
  1473. }
  1474. static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
  1475. {
  1476. struct drm_device *dev = crtc->dev;
  1477. struct drm_i915_private *dev_priv = dev->dev_private;
  1478. u32 dpa_ctl;
  1479. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1480. dpa_ctl = I915_READ(DP_A);
  1481. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1482. if (clock < 200000) {
  1483. u32 temp;
  1484. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1485. /* workaround for 160Mhz:
  1486. 1) program 0x4600c bits 15:0 = 0x8124
  1487. 2) program 0x46010 bit 0 = 1
  1488. 3) program 0x46034 bit 24 = 1
  1489. 4) program 0x64000 bit 14 = 1
  1490. */
  1491. temp = I915_READ(0x4600c);
  1492. temp &= 0xffff0000;
  1493. I915_WRITE(0x4600c, temp | 0x8124);
  1494. temp = I915_READ(0x46010);
  1495. I915_WRITE(0x46010, temp | 1);
  1496. temp = I915_READ(0x46034);
  1497. I915_WRITE(0x46034, temp | (1 << 24));
  1498. } else {
  1499. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1500. }
  1501. I915_WRITE(DP_A, dpa_ctl);
  1502. POSTING_READ(DP_A);
  1503. udelay(500);
  1504. }
  1505. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  1506. {
  1507. struct drm_device *dev = crtc->dev;
  1508. struct drm_i915_private *dev_priv = dev->dev_private;
  1509. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1510. int pipe = intel_crtc->pipe;
  1511. u32 reg, temp;
  1512. /* enable normal train */
  1513. reg = FDI_TX_CTL(pipe);
  1514. temp = I915_READ(reg);
  1515. temp &= ~FDI_LINK_TRAIN_NONE;
  1516. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  1517. I915_WRITE(reg, temp);
  1518. reg = FDI_RX_CTL(pipe);
  1519. temp = I915_READ(reg);
  1520. if (HAS_PCH_CPT(dev)) {
  1521. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1522. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  1523. } else {
  1524. temp &= ~FDI_LINK_TRAIN_NONE;
  1525. temp |= FDI_LINK_TRAIN_NONE;
  1526. }
  1527. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  1528. /* wait one idle pattern time */
  1529. POSTING_READ(reg);
  1530. udelay(1000);
  1531. }
  1532. /* The FDI link training functions for ILK/Ibexpeak. */
  1533. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  1534. {
  1535. struct drm_device *dev = crtc->dev;
  1536. struct drm_i915_private *dev_priv = dev->dev_private;
  1537. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1538. int pipe = intel_crtc->pipe;
  1539. u32 reg, temp, tries;
  1540. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1541. for train result */
  1542. reg = FDI_RX_IMR(pipe);
  1543. temp = I915_READ(reg);
  1544. temp &= ~FDI_RX_SYMBOL_LOCK;
  1545. temp &= ~FDI_RX_BIT_LOCK;
  1546. I915_WRITE(reg, temp);
  1547. I915_READ(reg);
  1548. udelay(150);
  1549. /* enable CPU FDI TX and PCH FDI RX */
  1550. reg = FDI_TX_CTL(pipe);
  1551. temp = I915_READ(reg);
  1552. temp &= ~(7 << 19);
  1553. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1554. temp &= ~FDI_LINK_TRAIN_NONE;
  1555. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1556. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  1557. reg = FDI_RX_CTL(pipe);
  1558. temp = I915_READ(reg);
  1559. temp &= ~FDI_LINK_TRAIN_NONE;
  1560. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1561. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  1562. POSTING_READ(reg);
  1563. udelay(150);
  1564. /* Ironlake workaround, enable clock pointer after FDI enable*/
  1565. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_ENABLE);
  1566. reg = FDI_RX_IIR(pipe);
  1567. for (tries = 0; tries < 5; tries++) {
  1568. temp = I915_READ(reg);
  1569. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1570. if ((temp & FDI_RX_BIT_LOCK)) {
  1571. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1572. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  1573. break;
  1574. }
  1575. }
  1576. if (tries == 5)
  1577. DRM_ERROR("FDI train 1 fail!\n");
  1578. /* Train 2 */
  1579. reg = FDI_TX_CTL(pipe);
  1580. temp = I915_READ(reg);
  1581. temp &= ~FDI_LINK_TRAIN_NONE;
  1582. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1583. I915_WRITE(reg, temp);
  1584. reg = FDI_RX_CTL(pipe);
  1585. temp = I915_READ(reg);
  1586. temp &= ~FDI_LINK_TRAIN_NONE;
  1587. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1588. I915_WRITE(reg, temp);
  1589. POSTING_READ(reg);
  1590. udelay(150);
  1591. reg = FDI_RX_IIR(pipe);
  1592. for (tries = 0; tries < 5; tries++) {
  1593. temp = I915_READ(reg);
  1594. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1595. if (temp & FDI_RX_SYMBOL_LOCK) {
  1596. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  1597. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1598. break;
  1599. }
  1600. }
  1601. if (tries == 5)
  1602. DRM_ERROR("FDI train 2 fail!\n");
  1603. DRM_DEBUG_KMS("FDI train done\n");
  1604. }
  1605. static const int const snb_b_fdi_train_param [] = {
  1606. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  1607. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  1608. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  1609. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  1610. };
  1611. /* The FDI link training functions for SNB/Cougarpoint. */
  1612. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  1613. {
  1614. struct drm_device *dev = crtc->dev;
  1615. struct drm_i915_private *dev_priv = dev->dev_private;
  1616. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1617. int pipe = intel_crtc->pipe;
  1618. u32 reg, temp, i;
  1619. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1620. for train result */
  1621. reg = FDI_RX_IMR(pipe);
  1622. temp = I915_READ(reg);
  1623. temp &= ~FDI_RX_SYMBOL_LOCK;
  1624. temp &= ~FDI_RX_BIT_LOCK;
  1625. I915_WRITE(reg, temp);
  1626. POSTING_READ(reg);
  1627. udelay(150);
  1628. /* enable CPU FDI TX and PCH FDI RX */
  1629. reg = FDI_TX_CTL(pipe);
  1630. temp = I915_READ(reg);
  1631. temp &= ~(7 << 19);
  1632. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1633. temp &= ~FDI_LINK_TRAIN_NONE;
  1634. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1635. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1636. /* SNB-B */
  1637. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1638. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  1639. reg = FDI_RX_CTL(pipe);
  1640. temp = I915_READ(reg);
  1641. if (HAS_PCH_CPT(dev)) {
  1642. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1643. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1644. } else {
  1645. temp &= ~FDI_LINK_TRAIN_NONE;
  1646. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1647. }
  1648. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  1649. POSTING_READ(reg);
  1650. udelay(150);
  1651. for (i = 0; i < 4; i++ ) {
  1652. reg = FDI_TX_CTL(pipe);
  1653. temp = I915_READ(reg);
  1654. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1655. temp |= snb_b_fdi_train_param[i];
  1656. I915_WRITE(reg, temp);
  1657. POSTING_READ(reg);
  1658. udelay(500);
  1659. reg = FDI_RX_IIR(pipe);
  1660. temp = I915_READ(reg);
  1661. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1662. if (temp & FDI_RX_BIT_LOCK) {
  1663. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  1664. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1665. break;
  1666. }
  1667. }
  1668. if (i == 4)
  1669. DRM_ERROR("FDI train 1 fail!\n");
  1670. /* Train 2 */
  1671. reg = FDI_TX_CTL(pipe);
  1672. temp = I915_READ(reg);
  1673. temp &= ~FDI_LINK_TRAIN_NONE;
  1674. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1675. if (IS_GEN6(dev)) {
  1676. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1677. /* SNB-B */
  1678. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1679. }
  1680. I915_WRITE(reg, temp);
  1681. reg = FDI_RX_CTL(pipe);
  1682. temp = I915_READ(reg);
  1683. if (HAS_PCH_CPT(dev)) {
  1684. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1685. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  1686. } else {
  1687. temp &= ~FDI_LINK_TRAIN_NONE;
  1688. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1689. }
  1690. I915_WRITE(reg, temp);
  1691. POSTING_READ(reg);
  1692. udelay(150);
  1693. for (i = 0; i < 4; i++ ) {
  1694. reg = FDI_TX_CTL(pipe);
  1695. temp = I915_READ(reg);
  1696. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1697. temp |= snb_b_fdi_train_param[i];
  1698. I915_WRITE(reg, temp);
  1699. POSTING_READ(reg);
  1700. udelay(500);
  1701. reg = FDI_RX_IIR(pipe);
  1702. temp = I915_READ(reg);
  1703. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1704. if (temp & FDI_RX_SYMBOL_LOCK) {
  1705. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  1706. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1707. break;
  1708. }
  1709. }
  1710. if (i == 4)
  1711. DRM_ERROR("FDI train 2 fail!\n");
  1712. DRM_DEBUG_KMS("FDI train done.\n");
  1713. }
  1714. static void ironlake_fdi_enable(struct drm_crtc *crtc)
  1715. {
  1716. struct drm_device *dev = crtc->dev;
  1717. struct drm_i915_private *dev_priv = dev->dev_private;
  1718. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1719. int pipe = intel_crtc->pipe;
  1720. u32 reg, temp;
  1721. /* Write the TU size bits so error detection works */
  1722. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  1723. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  1724. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  1725. reg = FDI_RX_CTL(pipe);
  1726. temp = I915_READ(reg);
  1727. temp &= ~((0x7 << 19) | (0x7 << 16));
  1728. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1729. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  1730. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  1731. POSTING_READ(reg);
  1732. udelay(200);
  1733. /* Switch from Rawclk to PCDclk */
  1734. temp = I915_READ(reg);
  1735. I915_WRITE(reg, temp | FDI_PCDCLK);
  1736. POSTING_READ(reg);
  1737. udelay(200);
  1738. /* Enable CPU FDI TX PLL, always on for Ironlake */
  1739. reg = FDI_TX_CTL(pipe);
  1740. temp = I915_READ(reg);
  1741. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  1742. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  1743. POSTING_READ(reg);
  1744. udelay(100);
  1745. }
  1746. }
  1747. static void intel_flush_display_plane(struct drm_device *dev,
  1748. int plane)
  1749. {
  1750. struct drm_i915_private *dev_priv = dev->dev_private;
  1751. u32 reg = DSPADDR(plane);
  1752. I915_WRITE(reg, I915_READ(reg));
  1753. }
  1754. /*
  1755. * When we disable a pipe, we need to clear any pending scanline wait events
  1756. * to avoid hanging the ring, which we assume we are waiting on.
  1757. */
  1758. static void intel_clear_scanline_wait(struct drm_device *dev)
  1759. {
  1760. struct drm_i915_private *dev_priv = dev->dev_private;
  1761. struct intel_ring_buffer *ring;
  1762. u32 tmp;
  1763. if (IS_GEN2(dev))
  1764. /* Can't break the hang on i8xx */
  1765. return;
  1766. ring = LP_RING(dev_priv);
  1767. tmp = I915_READ_CTL(ring);
  1768. if (tmp & RING_WAIT)
  1769. I915_WRITE_CTL(ring, tmp);
  1770. }
  1771. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  1772. {
  1773. struct drm_i915_gem_object *obj;
  1774. struct drm_i915_private *dev_priv;
  1775. if (crtc->fb == NULL)
  1776. return;
  1777. obj = to_intel_framebuffer(crtc->fb)->obj;
  1778. dev_priv = crtc->dev->dev_private;
  1779. wait_event(dev_priv->pending_flip_queue,
  1780. atomic_read(&obj->pending_flip) == 0);
  1781. }
  1782. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  1783. {
  1784. struct drm_device *dev = crtc->dev;
  1785. struct drm_i915_private *dev_priv = dev->dev_private;
  1786. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1787. int pipe = intel_crtc->pipe;
  1788. int plane = intel_crtc->plane;
  1789. u32 reg, temp;
  1790. if (intel_crtc->active)
  1791. return;
  1792. intel_crtc->active = true;
  1793. intel_update_watermarks(dev);
  1794. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1795. temp = I915_READ(PCH_LVDS);
  1796. if ((temp & LVDS_PORT_EN) == 0)
  1797. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  1798. }
  1799. ironlake_fdi_enable(crtc);
  1800. /* Enable panel fitting for LVDS */
  1801. if (dev_priv->pch_pf_size &&
  1802. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
  1803. /* Force use of hard-coded filter coefficients
  1804. * as some pre-programmed values are broken,
  1805. * e.g. x201.
  1806. */
  1807. I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1,
  1808. PF_ENABLE | PF_FILTER_MED_3x3);
  1809. I915_WRITE(pipe ? PFB_WIN_POS : PFA_WIN_POS,
  1810. dev_priv->pch_pf_pos);
  1811. I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ,
  1812. dev_priv->pch_pf_size);
  1813. }
  1814. /* Enable CPU pipe */
  1815. reg = PIPECONF(pipe);
  1816. temp = I915_READ(reg);
  1817. if ((temp & PIPECONF_ENABLE) == 0) {
  1818. I915_WRITE(reg, temp | PIPECONF_ENABLE);
  1819. POSTING_READ(reg);
  1820. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1821. }
  1822. /* configure and enable CPU plane */
  1823. reg = DSPCNTR(plane);
  1824. temp = I915_READ(reg);
  1825. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  1826. I915_WRITE(reg, temp | DISPLAY_PLANE_ENABLE);
  1827. intel_flush_display_plane(dev, plane);
  1828. }
  1829. /* For PCH output, training FDI link */
  1830. if (IS_GEN6(dev))
  1831. gen6_fdi_link_train(crtc);
  1832. else
  1833. ironlake_fdi_link_train(crtc);
  1834. /* enable PCH DPLL */
  1835. reg = PCH_DPLL(pipe);
  1836. temp = I915_READ(reg);
  1837. if ((temp & DPLL_VCO_ENABLE) == 0) {
  1838. I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
  1839. POSTING_READ(reg);
  1840. udelay(200);
  1841. }
  1842. if (HAS_PCH_CPT(dev)) {
  1843. /* Be sure PCH DPLL SEL is set */
  1844. temp = I915_READ(PCH_DPLL_SEL);
  1845. if (pipe == 0 && (temp & TRANSA_DPLL_ENABLE) == 0)
  1846. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  1847. else if (pipe == 1 && (temp & TRANSB_DPLL_ENABLE) == 0)
  1848. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  1849. I915_WRITE(PCH_DPLL_SEL, temp);
  1850. }
  1851. /* set transcoder timing */
  1852. I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
  1853. I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
  1854. I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
  1855. I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
  1856. I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
  1857. I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
  1858. intel_fdi_normal_train(crtc);
  1859. /* For PCH DP, enable TRANS_DP_CTL */
  1860. if (HAS_PCH_CPT(dev) &&
  1861. intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  1862. reg = TRANS_DP_CTL(pipe);
  1863. temp = I915_READ(reg);
  1864. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  1865. TRANS_DP_SYNC_MASK |
  1866. TRANS_DP_BPC_MASK);
  1867. temp |= (TRANS_DP_OUTPUT_ENABLE |
  1868. TRANS_DP_ENH_FRAMING);
  1869. temp |= TRANS_DP_8BPC;
  1870. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  1871. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  1872. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  1873. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  1874. switch (intel_trans_dp_port_sel(crtc)) {
  1875. case PCH_DP_B:
  1876. temp |= TRANS_DP_PORT_SEL_B;
  1877. break;
  1878. case PCH_DP_C:
  1879. temp |= TRANS_DP_PORT_SEL_C;
  1880. break;
  1881. case PCH_DP_D:
  1882. temp |= TRANS_DP_PORT_SEL_D;
  1883. break;
  1884. default:
  1885. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  1886. temp |= TRANS_DP_PORT_SEL_B;
  1887. break;
  1888. }
  1889. I915_WRITE(reg, temp);
  1890. }
  1891. /* enable PCH transcoder */
  1892. reg = TRANSCONF(pipe);
  1893. temp = I915_READ(reg);
  1894. /*
  1895. * make the BPC in transcoder be consistent with
  1896. * that in pipeconf reg.
  1897. */
  1898. temp &= ~PIPE_BPC_MASK;
  1899. temp |= I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK;
  1900. I915_WRITE(reg, temp | TRANS_ENABLE);
  1901. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1902. DRM_ERROR("failed to enable transcoder %d\n", pipe);
  1903. intel_crtc_load_lut(crtc);
  1904. intel_update_fbc(dev);
  1905. intel_crtc_update_cursor(crtc, true);
  1906. }
  1907. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  1908. {
  1909. struct drm_device *dev = crtc->dev;
  1910. struct drm_i915_private *dev_priv = dev->dev_private;
  1911. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1912. int pipe = intel_crtc->pipe;
  1913. int plane = intel_crtc->plane;
  1914. u32 reg, temp;
  1915. if (!intel_crtc->active)
  1916. return;
  1917. intel_crtc_wait_for_pending_flips(crtc);
  1918. drm_vblank_off(dev, pipe);
  1919. intel_crtc_update_cursor(crtc, false);
  1920. /* Disable display plane */
  1921. reg = DSPCNTR(plane);
  1922. temp = I915_READ(reg);
  1923. if (temp & DISPLAY_PLANE_ENABLE) {
  1924. I915_WRITE(reg, temp & ~DISPLAY_PLANE_ENABLE);
  1925. intel_flush_display_plane(dev, plane);
  1926. }
  1927. if (dev_priv->cfb_plane == plane &&
  1928. dev_priv->display.disable_fbc)
  1929. dev_priv->display.disable_fbc(dev);
  1930. /* disable cpu pipe, disable after all planes disabled */
  1931. reg = PIPECONF(pipe);
  1932. temp = I915_READ(reg);
  1933. if (temp & PIPECONF_ENABLE) {
  1934. I915_WRITE(reg, temp & ~PIPECONF_ENABLE);
  1935. POSTING_READ(reg);
  1936. /* wait for cpu pipe off, pipe state */
  1937. intel_wait_for_pipe_off(dev, intel_crtc->pipe);
  1938. }
  1939. /* Disable PF */
  1940. I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1, 0);
  1941. I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ, 0);
  1942. /* disable CPU FDI tx and PCH FDI rx */
  1943. reg = FDI_TX_CTL(pipe);
  1944. temp = I915_READ(reg);
  1945. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  1946. POSTING_READ(reg);
  1947. reg = FDI_RX_CTL(pipe);
  1948. temp = I915_READ(reg);
  1949. temp &= ~(0x7 << 16);
  1950. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  1951. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  1952. POSTING_READ(reg);
  1953. udelay(100);
  1954. /* Ironlake workaround, disable clock pointer after downing FDI */
  1955. if (HAS_PCH_IBX(dev))
  1956. I915_WRITE(FDI_RX_CHICKEN(pipe),
  1957. I915_READ(FDI_RX_CHICKEN(pipe) &
  1958. ~FDI_RX_PHASE_SYNC_POINTER_ENABLE));
  1959. /* still set train pattern 1 */
  1960. reg = FDI_TX_CTL(pipe);
  1961. temp = I915_READ(reg);
  1962. temp &= ~FDI_LINK_TRAIN_NONE;
  1963. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1964. I915_WRITE(reg, temp);
  1965. reg = FDI_RX_CTL(pipe);
  1966. temp = I915_READ(reg);
  1967. if (HAS_PCH_CPT(dev)) {
  1968. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1969. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1970. } else {
  1971. temp &= ~FDI_LINK_TRAIN_NONE;
  1972. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1973. }
  1974. /* BPC in FDI rx is consistent with that in PIPECONF */
  1975. temp &= ~(0x07 << 16);
  1976. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  1977. I915_WRITE(reg, temp);
  1978. POSTING_READ(reg);
  1979. udelay(100);
  1980. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1981. temp = I915_READ(PCH_LVDS);
  1982. if (temp & LVDS_PORT_EN) {
  1983. I915_WRITE(PCH_LVDS, temp & ~LVDS_PORT_EN);
  1984. POSTING_READ(PCH_LVDS);
  1985. udelay(100);
  1986. }
  1987. }
  1988. /* disable PCH transcoder */
  1989. reg = TRANSCONF(plane);
  1990. temp = I915_READ(reg);
  1991. if (temp & TRANS_ENABLE) {
  1992. I915_WRITE(reg, temp & ~TRANS_ENABLE);
  1993. /* wait for PCH transcoder off, transcoder state */
  1994. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1995. DRM_ERROR("failed to disable transcoder\n");
  1996. }
  1997. if (HAS_PCH_CPT(dev)) {
  1998. /* disable TRANS_DP_CTL */
  1999. reg = TRANS_DP_CTL(pipe);
  2000. temp = I915_READ(reg);
  2001. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  2002. I915_WRITE(reg, temp);
  2003. /* disable DPLL_SEL */
  2004. temp = I915_READ(PCH_DPLL_SEL);
  2005. if (pipe == 0)
  2006. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  2007. else
  2008. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2009. I915_WRITE(PCH_DPLL_SEL, temp);
  2010. }
  2011. /* disable PCH DPLL */
  2012. reg = PCH_DPLL(pipe);
  2013. temp = I915_READ(reg);
  2014. I915_WRITE(reg, temp & ~DPLL_VCO_ENABLE);
  2015. /* Switch from PCDclk to Rawclk */
  2016. reg = FDI_RX_CTL(pipe);
  2017. temp = I915_READ(reg);
  2018. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2019. /* Disable CPU FDI TX PLL */
  2020. reg = FDI_TX_CTL(pipe);
  2021. temp = I915_READ(reg);
  2022. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2023. POSTING_READ(reg);
  2024. udelay(100);
  2025. reg = FDI_RX_CTL(pipe);
  2026. temp = I915_READ(reg);
  2027. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2028. /* Wait for the clocks to turn off. */
  2029. POSTING_READ(reg);
  2030. udelay(100);
  2031. intel_crtc->active = false;
  2032. intel_update_watermarks(dev);
  2033. intel_update_fbc(dev);
  2034. intel_clear_scanline_wait(dev);
  2035. }
  2036. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  2037. {
  2038. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2039. int pipe = intel_crtc->pipe;
  2040. int plane = intel_crtc->plane;
  2041. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2042. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2043. */
  2044. switch (mode) {
  2045. case DRM_MODE_DPMS_ON:
  2046. case DRM_MODE_DPMS_STANDBY:
  2047. case DRM_MODE_DPMS_SUSPEND:
  2048. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  2049. ironlake_crtc_enable(crtc);
  2050. break;
  2051. case DRM_MODE_DPMS_OFF:
  2052. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  2053. ironlake_crtc_disable(crtc);
  2054. break;
  2055. }
  2056. }
  2057. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2058. {
  2059. if (!enable && intel_crtc->overlay) {
  2060. struct drm_device *dev = intel_crtc->base.dev;
  2061. mutex_lock(&dev->struct_mutex);
  2062. (void) intel_overlay_switch_off(intel_crtc->overlay, false);
  2063. mutex_unlock(&dev->struct_mutex);
  2064. }
  2065. /* Let userspace switch the overlay on again. In most cases userspace
  2066. * has to recompute where to put it anyway.
  2067. */
  2068. }
  2069. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  2070. {
  2071. struct drm_device *dev = crtc->dev;
  2072. struct drm_i915_private *dev_priv = dev->dev_private;
  2073. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2074. int pipe = intel_crtc->pipe;
  2075. int plane = intel_crtc->plane;
  2076. u32 reg, temp;
  2077. if (intel_crtc->active)
  2078. return;
  2079. intel_crtc->active = true;
  2080. intel_update_watermarks(dev);
  2081. /* Enable the DPLL */
  2082. reg = DPLL(pipe);
  2083. temp = I915_READ(reg);
  2084. if ((temp & DPLL_VCO_ENABLE) == 0) {
  2085. I915_WRITE(reg, temp);
  2086. /* Wait for the clocks to stabilize. */
  2087. POSTING_READ(reg);
  2088. udelay(150);
  2089. I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
  2090. /* Wait for the clocks to stabilize. */
  2091. POSTING_READ(reg);
  2092. udelay(150);
  2093. I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
  2094. /* Wait for the clocks to stabilize. */
  2095. POSTING_READ(reg);
  2096. udelay(150);
  2097. }
  2098. /* Enable the pipe */
  2099. reg = PIPECONF(pipe);
  2100. temp = I915_READ(reg);
  2101. if ((temp & PIPECONF_ENABLE) == 0)
  2102. I915_WRITE(reg, temp | PIPECONF_ENABLE);
  2103. /* Enable the plane */
  2104. reg = DSPCNTR(plane);
  2105. temp = I915_READ(reg);
  2106. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  2107. I915_WRITE(reg, temp | DISPLAY_PLANE_ENABLE);
  2108. intel_flush_display_plane(dev, plane);
  2109. }
  2110. intel_crtc_load_lut(crtc);
  2111. intel_update_fbc(dev);
  2112. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2113. intel_crtc_dpms_overlay(intel_crtc, true);
  2114. intel_crtc_update_cursor(crtc, true);
  2115. }
  2116. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  2117. {
  2118. struct drm_device *dev = crtc->dev;
  2119. struct drm_i915_private *dev_priv = dev->dev_private;
  2120. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2121. int pipe = intel_crtc->pipe;
  2122. int plane = intel_crtc->plane;
  2123. u32 reg, temp;
  2124. if (!intel_crtc->active)
  2125. return;
  2126. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2127. intel_crtc_wait_for_pending_flips(crtc);
  2128. drm_vblank_off(dev, pipe);
  2129. intel_crtc_dpms_overlay(intel_crtc, false);
  2130. intel_crtc_update_cursor(crtc, false);
  2131. if (dev_priv->cfb_plane == plane &&
  2132. dev_priv->display.disable_fbc)
  2133. dev_priv->display.disable_fbc(dev);
  2134. /* Disable display plane */
  2135. reg = DSPCNTR(plane);
  2136. temp = I915_READ(reg);
  2137. if (temp & DISPLAY_PLANE_ENABLE) {
  2138. I915_WRITE(reg, temp & ~DISPLAY_PLANE_ENABLE);
  2139. /* Flush the plane changes */
  2140. intel_flush_display_plane(dev, plane);
  2141. /* Wait for vblank for the disable to take effect */
  2142. if (IS_GEN2(dev))
  2143. intel_wait_for_vblank(dev, pipe);
  2144. }
  2145. /* Don't disable pipe A or pipe A PLLs if needed */
  2146. if (pipe == 0 && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  2147. goto done;
  2148. /* Next, disable display pipes */
  2149. reg = PIPECONF(pipe);
  2150. temp = I915_READ(reg);
  2151. if (temp & PIPECONF_ENABLE) {
  2152. I915_WRITE(reg, temp & ~PIPECONF_ENABLE);
  2153. /* Wait for the pipe to turn off */
  2154. POSTING_READ(reg);
  2155. intel_wait_for_pipe_off(dev, pipe);
  2156. }
  2157. reg = DPLL(pipe);
  2158. temp = I915_READ(reg);
  2159. if (temp & DPLL_VCO_ENABLE) {
  2160. I915_WRITE(reg, temp & ~DPLL_VCO_ENABLE);
  2161. /* Wait for the clocks to turn off. */
  2162. POSTING_READ(reg);
  2163. udelay(150);
  2164. }
  2165. done:
  2166. intel_crtc->active = false;
  2167. intel_update_fbc(dev);
  2168. intel_update_watermarks(dev);
  2169. intel_clear_scanline_wait(dev);
  2170. }
  2171. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2172. {
  2173. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2174. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2175. */
  2176. switch (mode) {
  2177. case DRM_MODE_DPMS_ON:
  2178. case DRM_MODE_DPMS_STANDBY:
  2179. case DRM_MODE_DPMS_SUSPEND:
  2180. i9xx_crtc_enable(crtc);
  2181. break;
  2182. case DRM_MODE_DPMS_OFF:
  2183. i9xx_crtc_disable(crtc);
  2184. break;
  2185. }
  2186. }
  2187. /**
  2188. * Sets the power management mode of the pipe and plane.
  2189. */
  2190. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2191. {
  2192. struct drm_device *dev = crtc->dev;
  2193. struct drm_i915_private *dev_priv = dev->dev_private;
  2194. struct drm_i915_master_private *master_priv;
  2195. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2196. int pipe = intel_crtc->pipe;
  2197. bool enabled;
  2198. if (intel_crtc->dpms_mode == mode)
  2199. return;
  2200. intel_crtc->dpms_mode = mode;
  2201. dev_priv->display.dpms(crtc, mode);
  2202. if (!dev->primary->master)
  2203. return;
  2204. master_priv = dev->primary->master->driver_priv;
  2205. if (!master_priv->sarea_priv)
  2206. return;
  2207. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2208. switch (pipe) {
  2209. case 0:
  2210. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2211. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2212. break;
  2213. case 1:
  2214. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2215. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2216. break;
  2217. default:
  2218. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  2219. break;
  2220. }
  2221. }
  2222. static void intel_crtc_disable(struct drm_crtc *crtc)
  2223. {
  2224. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2225. struct drm_device *dev = crtc->dev;
  2226. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  2227. if (crtc->fb) {
  2228. mutex_lock(&dev->struct_mutex);
  2229. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  2230. mutex_unlock(&dev->struct_mutex);
  2231. }
  2232. }
  2233. /* Prepare for a mode set.
  2234. *
  2235. * Note we could be a lot smarter here. We need to figure out which outputs
  2236. * will be enabled, which disabled (in short, how the config will changes)
  2237. * and perform the minimum necessary steps to accomplish that, e.g. updating
  2238. * watermarks, FBC configuration, making sure PLLs are programmed correctly,
  2239. * panel fitting is in the proper state, etc.
  2240. */
  2241. static void i9xx_crtc_prepare(struct drm_crtc *crtc)
  2242. {
  2243. i9xx_crtc_disable(crtc);
  2244. }
  2245. static void i9xx_crtc_commit(struct drm_crtc *crtc)
  2246. {
  2247. i9xx_crtc_enable(crtc);
  2248. }
  2249. static void ironlake_crtc_prepare(struct drm_crtc *crtc)
  2250. {
  2251. ironlake_crtc_disable(crtc);
  2252. }
  2253. static void ironlake_crtc_commit(struct drm_crtc *crtc)
  2254. {
  2255. ironlake_crtc_enable(crtc);
  2256. }
  2257. void intel_encoder_prepare (struct drm_encoder *encoder)
  2258. {
  2259. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2260. /* lvds has its own version of prepare see intel_lvds_prepare */
  2261. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2262. }
  2263. void intel_encoder_commit (struct drm_encoder *encoder)
  2264. {
  2265. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2266. /* lvds has its own version of commit see intel_lvds_commit */
  2267. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2268. }
  2269. void intel_encoder_destroy(struct drm_encoder *encoder)
  2270. {
  2271. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2272. drm_encoder_cleanup(encoder);
  2273. kfree(intel_encoder);
  2274. }
  2275. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2276. struct drm_display_mode *mode,
  2277. struct drm_display_mode *adjusted_mode)
  2278. {
  2279. struct drm_device *dev = crtc->dev;
  2280. if (HAS_PCH_SPLIT(dev)) {
  2281. /* FDI link clock is fixed at 2.7G */
  2282. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  2283. return false;
  2284. }
  2285. /* XXX some encoders set the crtcinfo, others don't.
  2286. * Obviously we need some form of conflict resolution here...
  2287. */
  2288. if (adjusted_mode->crtc_htotal == 0)
  2289. drm_mode_set_crtcinfo(adjusted_mode, 0);
  2290. return true;
  2291. }
  2292. static int i945_get_display_clock_speed(struct drm_device *dev)
  2293. {
  2294. return 400000;
  2295. }
  2296. static int i915_get_display_clock_speed(struct drm_device *dev)
  2297. {
  2298. return 333000;
  2299. }
  2300. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2301. {
  2302. return 200000;
  2303. }
  2304. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2305. {
  2306. u16 gcfgc = 0;
  2307. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2308. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2309. return 133000;
  2310. else {
  2311. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2312. case GC_DISPLAY_CLOCK_333_MHZ:
  2313. return 333000;
  2314. default:
  2315. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2316. return 190000;
  2317. }
  2318. }
  2319. }
  2320. static int i865_get_display_clock_speed(struct drm_device *dev)
  2321. {
  2322. return 266000;
  2323. }
  2324. static int i855_get_display_clock_speed(struct drm_device *dev)
  2325. {
  2326. u16 hpllcc = 0;
  2327. /* Assume that the hardware is in the high speed state. This
  2328. * should be the default.
  2329. */
  2330. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2331. case GC_CLOCK_133_200:
  2332. case GC_CLOCK_100_200:
  2333. return 200000;
  2334. case GC_CLOCK_166_250:
  2335. return 250000;
  2336. case GC_CLOCK_100_133:
  2337. return 133000;
  2338. }
  2339. /* Shouldn't happen */
  2340. return 0;
  2341. }
  2342. static int i830_get_display_clock_speed(struct drm_device *dev)
  2343. {
  2344. return 133000;
  2345. }
  2346. struct fdi_m_n {
  2347. u32 tu;
  2348. u32 gmch_m;
  2349. u32 gmch_n;
  2350. u32 link_m;
  2351. u32 link_n;
  2352. };
  2353. static void
  2354. fdi_reduce_ratio(u32 *num, u32 *den)
  2355. {
  2356. while (*num > 0xffffff || *den > 0xffffff) {
  2357. *num >>= 1;
  2358. *den >>= 1;
  2359. }
  2360. }
  2361. static void
  2362. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  2363. int link_clock, struct fdi_m_n *m_n)
  2364. {
  2365. m_n->tu = 64; /* default size */
  2366. /* BUG_ON(pixel_clock > INT_MAX / 36); */
  2367. m_n->gmch_m = bits_per_pixel * pixel_clock;
  2368. m_n->gmch_n = link_clock * nlanes * 8;
  2369. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  2370. m_n->link_m = pixel_clock;
  2371. m_n->link_n = link_clock;
  2372. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  2373. }
  2374. struct intel_watermark_params {
  2375. unsigned long fifo_size;
  2376. unsigned long max_wm;
  2377. unsigned long default_wm;
  2378. unsigned long guard_size;
  2379. unsigned long cacheline_size;
  2380. };
  2381. /* Pineview has different values for various configs */
  2382. static struct intel_watermark_params pineview_display_wm = {
  2383. PINEVIEW_DISPLAY_FIFO,
  2384. PINEVIEW_MAX_WM,
  2385. PINEVIEW_DFT_WM,
  2386. PINEVIEW_GUARD_WM,
  2387. PINEVIEW_FIFO_LINE_SIZE
  2388. };
  2389. static struct intel_watermark_params pineview_display_hplloff_wm = {
  2390. PINEVIEW_DISPLAY_FIFO,
  2391. PINEVIEW_MAX_WM,
  2392. PINEVIEW_DFT_HPLLOFF_WM,
  2393. PINEVIEW_GUARD_WM,
  2394. PINEVIEW_FIFO_LINE_SIZE
  2395. };
  2396. static struct intel_watermark_params pineview_cursor_wm = {
  2397. PINEVIEW_CURSOR_FIFO,
  2398. PINEVIEW_CURSOR_MAX_WM,
  2399. PINEVIEW_CURSOR_DFT_WM,
  2400. PINEVIEW_CURSOR_GUARD_WM,
  2401. PINEVIEW_FIFO_LINE_SIZE,
  2402. };
  2403. static struct intel_watermark_params pineview_cursor_hplloff_wm = {
  2404. PINEVIEW_CURSOR_FIFO,
  2405. PINEVIEW_CURSOR_MAX_WM,
  2406. PINEVIEW_CURSOR_DFT_WM,
  2407. PINEVIEW_CURSOR_GUARD_WM,
  2408. PINEVIEW_FIFO_LINE_SIZE
  2409. };
  2410. static struct intel_watermark_params g4x_wm_info = {
  2411. G4X_FIFO_SIZE,
  2412. G4X_MAX_WM,
  2413. G4X_MAX_WM,
  2414. 2,
  2415. G4X_FIFO_LINE_SIZE,
  2416. };
  2417. static struct intel_watermark_params g4x_cursor_wm_info = {
  2418. I965_CURSOR_FIFO,
  2419. I965_CURSOR_MAX_WM,
  2420. I965_CURSOR_DFT_WM,
  2421. 2,
  2422. G4X_FIFO_LINE_SIZE,
  2423. };
  2424. static struct intel_watermark_params i965_cursor_wm_info = {
  2425. I965_CURSOR_FIFO,
  2426. I965_CURSOR_MAX_WM,
  2427. I965_CURSOR_DFT_WM,
  2428. 2,
  2429. I915_FIFO_LINE_SIZE,
  2430. };
  2431. static struct intel_watermark_params i945_wm_info = {
  2432. I945_FIFO_SIZE,
  2433. I915_MAX_WM,
  2434. 1,
  2435. 2,
  2436. I915_FIFO_LINE_SIZE
  2437. };
  2438. static struct intel_watermark_params i915_wm_info = {
  2439. I915_FIFO_SIZE,
  2440. I915_MAX_WM,
  2441. 1,
  2442. 2,
  2443. I915_FIFO_LINE_SIZE
  2444. };
  2445. static struct intel_watermark_params i855_wm_info = {
  2446. I855GM_FIFO_SIZE,
  2447. I915_MAX_WM,
  2448. 1,
  2449. 2,
  2450. I830_FIFO_LINE_SIZE
  2451. };
  2452. static struct intel_watermark_params i830_wm_info = {
  2453. I830_FIFO_SIZE,
  2454. I915_MAX_WM,
  2455. 1,
  2456. 2,
  2457. I830_FIFO_LINE_SIZE
  2458. };
  2459. static struct intel_watermark_params ironlake_display_wm_info = {
  2460. ILK_DISPLAY_FIFO,
  2461. ILK_DISPLAY_MAXWM,
  2462. ILK_DISPLAY_DFTWM,
  2463. 2,
  2464. ILK_FIFO_LINE_SIZE
  2465. };
  2466. static struct intel_watermark_params ironlake_cursor_wm_info = {
  2467. ILK_CURSOR_FIFO,
  2468. ILK_CURSOR_MAXWM,
  2469. ILK_CURSOR_DFTWM,
  2470. 2,
  2471. ILK_FIFO_LINE_SIZE
  2472. };
  2473. static struct intel_watermark_params ironlake_display_srwm_info = {
  2474. ILK_DISPLAY_SR_FIFO,
  2475. ILK_DISPLAY_MAX_SRWM,
  2476. ILK_DISPLAY_DFT_SRWM,
  2477. 2,
  2478. ILK_FIFO_LINE_SIZE
  2479. };
  2480. static struct intel_watermark_params ironlake_cursor_srwm_info = {
  2481. ILK_CURSOR_SR_FIFO,
  2482. ILK_CURSOR_MAX_SRWM,
  2483. ILK_CURSOR_DFT_SRWM,
  2484. 2,
  2485. ILK_FIFO_LINE_SIZE
  2486. };
  2487. /**
  2488. * intel_calculate_wm - calculate watermark level
  2489. * @clock_in_khz: pixel clock
  2490. * @wm: chip FIFO params
  2491. * @pixel_size: display pixel size
  2492. * @latency_ns: memory latency for the platform
  2493. *
  2494. * Calculate the watermark level (the level at which the display plane will
  2495. * start fetching from memory again). Each chip has a different display
  2496. * FIFO size and allocation, so the caller needs to figure that out and pass
  2497. * in the correct intel_watermark_params structure.
  2498. *
  2499. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  2500. * on the pixel size. When it reaches the watermark level, it'll start
  2501. * fetching FIFO line sized based chunks from memory until the FIFO fills
  2502. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  2503. * will occur, and a display engine hang could result.
  2504. */
  2505. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  2506. struct intel_watermark_params *wm,
  2507. int pixel_size,
  2508. unsigned long latency_ns)
  2509. {
  2510. long entries_required, wm_size;
  2511. /*
  2512. * Note: we need to make sure we don't overflow for various clock &
  2513. * latency values.
  2514. * clocks go from a few thousand to several hundred thousand.
  2515. * latency is usually a few thousand
  2516. */
  2517. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  2518. 1000;
  2519. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  2520. DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries_required);
  2521. wm_size = wm->fifo_size - (entries_required + wm->guard_size);
  2522. DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
  2523. /* Don't promote wm_size to unsigned... */
  2524. if (wm_size > (long)wm->max_wm)
  2525. wm_size = wm->max_wm;
  2526. if (wm_size <= 0)
  2527. wm_size = wm->default_wm;
  2528. return wm_size;
  2529. }
  2530. struct cxsr_latency {
  2531. int is_desktop;
  2532. int is_ddr3;
  2533. unsigned long fsb_freq;
  2534. unsigned long mem_freq;
  2535. unsigned long display_sr;
  2536. unsigned long display_hpll_disable;
  2537. unsigned long cursor_sr;
  2538. unsigned long cursor_hpll_disable;
  2539. };
  2540. static const struct cxsr_latency cxsr_latency_table[] = {
  2541. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  2542. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  2543. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  2544. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  2545. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  2546. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  2547. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  2548. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  2549. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  2550. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  2551. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  2552. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  2553. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  2554. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  2555. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  2556. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  2557. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  2558. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  2559. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  2560. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  2561. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  2562. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  2563. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  2564. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  2565. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  2566. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  2567. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  2568. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  2569. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  2570. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  2571. };
  2572. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  2573. int is_ddr3,
  2574. int fsb,
  2575. int mem)
  2576. {
  2577. const struct cxsr_latency *latency;
  2578. int i;
  2579. if (fsb == 0 || mem == 0)
  2580. return NULL;
  2581. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  2582. latency = &cxsr_latency_table[i];
  2583. if (is_desktop == latency->is_desktop &&
  2584. is_ddr3 == latency->is_ddr3 &&
  2585. fsb == latency->fsb_freq && mem == latency->mem_freq)
  2586. return latency;
  2587. }
  2588. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2589. return NULL;
  2590. }
  2591. static void pineview_disable_cxsr(struct drm_device *dev)
  2592. {
  2593. struct drm_i915_private *dev_priv = dev->dev_private;
  2594. /* deactivate cxsr */
  2595. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  2596. }
  2597. /*
  2598. * Latency for FIFO fetches is dependent on several factors:
  2599. * - memory configuration (speed, channels)
  2600. * - chipset
  2601. * - current MCH state
  2602. * It can be fairly high in some situations, so here we assume a fairly
  2603. * pessimal value. It's a tradeoff between extra memory fetches (if we
  2604. * set this value too high, the FIFO will fetch frequently to stay full)
  2605. * and power consumption (set it too low to save power and we might see
  2606. * FIFO underruns and display "flicker").
  2607. *
  2608. * A value of 5us seems to be a good balance; safe for very low end
  2609. * platforms but not overly aggressive on lower latency configs.
  2610. */
  2611. static const int latency_ns = 5000;
  2612. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  2613. {
  2614. struct drm_i915_private *dev_priv = dev->dev_private;
  2615. uint32_t dsparb = I915_READ(DSPARB);
  2616. int size;
  2617. size = dsparb & 0x7f;
  2618. if (plane)
  2619. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  2620. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2621. plane ? "B" : "A", size);
  2622. return size;
  2623. }
  2624. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  2625. {
  2626. struct drm_i915_private *dev_priv = dev->dev_private;
  2627. uint32_t dsparb = I915_READ(DSPARB);
  2628. int size;
  2629. size = dsparb & 0x1ff;
  2630. if (plane)
  2631. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  2632. size >>= 1; /* Convert to cachelines */
  2633. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2634. plane ? "B" : "A", size);
  2635. return size;
  2636. }
  2637. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  2638. {
  2639. struct drm_i915_private *dev_priv = dev->dev_private;
  2640. uint32_t dsparb = I915_READ(DSPARB);
  2641. int size;
  2642. size = dsparb & 0x7f;
  2643. size >>= 2; /* Convert to cachelines */
  2644. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2645. plane ? "B" : "A",
  2646. size);
  2647. return size;
  2648. }
  2649. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  2650. {
  2651. struct drm_i915_private *dev_priv = dev->dev_private;
  2652. uint32_t dsparb = I915_READ(DSPARB);
  2653. int size;
  2654. size = dsparb & 0x7f;
  2655. size >>= 1; /* Convert to cachelines */
  2656. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2657. plane ? "B" : "A", size);
  2658. return size;
  2659. }
  2660. static void pineview_update_wm(struct drm_device *dev, int planea_clock,
  2661. int planeb_clock, int sr_hdisplay, int unused,
  2662. int pixel_size)
  2663. {
  2664. struct drm_i915_private *dev_priv = dev->dev_private;
  2665. const struct cxsr_latency *latency;
  2666. u32 reg;
  2667. unsigned long wm;
  2668. int sr_clock;
  2669. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  2670. dev_priv->fsb_freq, dev_priv->mem_freq);
  2671. if (!latency) {
  2672. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2673. pineview_disable_cxsr(dev);
  2674. return;
  2675. }
  2676. if (!planea_clock || !planeb_clock) {
  2677. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2678. /* Display SR */
  2679. wm = intel_calculate_wm(sr_clock, &pineview_display_wm,
  2680. pixel_size, latency->display_sr);
  2681. reg = I915_READ(DSPFW1);
  2682. reg &= ~DSPFW_SR_MASK;
  2683. reg |= wm << DSPFW_SR_SHIFT;
  2684. I915_WRITE(DSPFW1, reg);
  2685. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  2686. /* cursor SR */
  2687. wm = intel_calculate_wm(sr_clock, &pineview_cursor_wm,
  2688. pixel_size, latency->cursor_sr);
  2689. reg = I915_READ(DSPFW3);
  2690. reg &= ~DSPFW_CURSOR_SR_MASK;
  2691. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  2692. I915_WRITE(DSPFW3, reg);
  2693. /* Display HPLL off SR */
  2694. wm = intel_calculate_wm(sr_clock, &pineview_display_hplloff_wm,
  2695. pixel_size, latency->display_hpll_disable);
  2696. reg = I915_READ(DSPFW3);
  2697. reg &= ~DSPFW_HPLL_SR_MASK;
  2698. reg |= wm & DSPFW_HPLL_SR_MASK;
  2699. I915_WRITE(DSPFW3, reg);
  2700. /* cursor HPLL off SR */
  2701. wm = intel_calculate_wm(sr_clock, &pineview_cursor_hplloff_wm,
  2702. pixel_size, latency->cursor_hpll_disable);
  2703. reg = I915_READ(DSPFW3);
  2704. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  2705. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  2706. I915_WRITE(DSPFW3, reg);
  2707. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  2708. /* activate cxsr */
  2709. I915_WRITE(DSPFW3,
  2710. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  2711. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  2712. } else {
  2713. pineview_disable_cxsr(dev);
  2714. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  2715. }
  2716. }
  2717. static void g4x_update_wm(struct drm_device *dev, int planea_clock,
  2718. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2719. int pixel_size)
  2720. {
  2721. struct drm_i915_private *dev_priv = dev->dev_private;
  2722. int total_size, cacheline_size;
  2723. int planea_wm, planeb_wm, cursora_wm, cursorb_wm, cursor_sr;
  2724. struct intel_watermark_params planea_params, planeb_params;
  2725. unsigned long line_time_us;
  2726. int sr_clock, sr_entries = 0, entries_required;
  2727. /* Create copies of the base settings for each pipe */
  2728. planea_params = planeb_params = g4x_wm_info;
  2729. /* Grab a couple of global values before we overwrite them */
  2730. total_size = planea_params.fifo_size;
  2731. cacheline_size = planea_params.cacheline_size;
  2732. /*
  2733. * Note: we need to make sure we don't overflow for various clock &
  2734. * latency values.
  2735. * clocks go from a few thousand to several hundred thousand.
  2736. * latency is usually a few thousand
  2737. */
  2738. entries_required = ((planea_clock / 1000) * pixel_size * latency_ns) /
  2739. 1000;
  2740. entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
  2741. planea_wm = entries_required + planea_params.guard_size;
  2742. entries_required = ((planeb_clock / 1000) * pixel_size * latency_ns) /
  2743. 1000;
  2744. entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
  2745. planeb_wm = entries_required + planeb_params.guard_size;
  2746. cursora_wm = cursorb_wm = 16;
  2747. cursor_sr = 32;
  2748. DRM_DEBUG("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  2749. /* Calc sr entries for one plane configs */
  2750. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  2751. /* self-refresh has much higher latency */
  2752. static const int sr_latency_ns = 12000;
  2753. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2754. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2755. /* Use ns/us then divide to preserve precision */
  2756. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2757. pixel_size * sr_hdisplay;
  2758. sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
  2759. entries_required = (((sr_latency_ns / line_time_us) +
  2760. 1000) / 1000) * pixel_size * 64;
  2761. entries_required = DIV_ROUND_UP(entries_required,
  2762. g4x_cursor_wm_info.cacheline_size);
  2763. cursor_sr = entries_required + g4x_cursor_wm_info.guard_size;
  2764. if (cursor_sr > g4x_cursor_wm_info.max_wm)
  2765. cursor_sr = g4x_cursor_wm_info.max_wm;
  2766. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  2767. "cursor %d\n", sr_entries, cursor_sr);
  2768. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  2769. } else {
  2770. /* Turn off self refresh if both pipes are enabled */
  2771. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2772. & ~FW_BLC_SELF_EN);
  2773. }
  2774. DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, SR %d\n",
  2775. planea_wm, planeb_wm, sr_entries);
  2776. planea_wm &= 0x3f;
  2777. planeb_wm &= 0x3f;
  2778. I915_WRITE(DSPFW1, (sr_entries << DSPFW_SR_SHIFT) |
  2779. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  2780. (planeb_wm << DSPFW_PLANEB_SHIFT) | planea_wm);
  2781. I915_WRITE(DSPFW2, (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  2782. (cursora_wm << DSPFW_CURSORA_SHIFT));
  2783. /* HPLL off in SR has some issues on G4x... disable it */
  2784. I915_WRITE(DSPFW3, (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  2785. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  2786. }
  2787. static void i965_update_wm(struct drm_device *dev, int planea_clock,
  2788. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2789. int pixel_size)
  2790. {
  2791. struct drm_i915_private *dev_priv = dev->dev_private;
  2792. unsigned long line_time_us;
  2793. int sr_clock, sr_entries, srwm = 1;
  2794. int cursor_sr = 16;
  2795. /* Calc sr entries for one plane configs */
  2796. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  2797. /* self-refresh has much higher latency */
  2798. static const int sr_latency_ns = 12000;
  2799. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2800. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2801. /* Use ns/us then divide to preserve precision */
  2802. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2803. pixel_size * sr_hdisplay;
  2804. sr_entries = DIV_ROUND_UP(sr_entries, I915_FIFO_LINE_SIZE);
  2805. DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
  2806. srwm = I965_FIFO_SIZE - sr_entries;
  2807. if (srwm < 0)
  2808. srwm = 1;
  2809. srwm &= 0x1ff;
  2810. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2811. pixel_size * 64;
  2812. sr_entries = DIV_ROUND_UP(sr_entries,
  2813. i965_cursor_wm_info.cacheline_size);
  2814. cursor_sr = i965_cursor_wm_info.fifo_size -
  2815. (sr_entries + i965_cursor_wm_info.guard_size);
  2816. if (cursor_sr > i965_cursor_wm_info.max_wm)
  2817. cursor_sr = i965_cursor_wm_info.max_wm;
  2818. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  2819. "cursor %d\n", srwm, cursor_sr);
  2820. if (IS_CRESTLINE(dev))
  2821. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  2822. } else {
  2823. /* Turn off self refresh if both pipes are enabled */
  2824. if (IS_CRESTLINE(dev))
  2825. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2826. & ~FW_BLC_SELF_EN);
  2827. }
  2828. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  2829. srwm);
  2830. /* 965 has limitations... */
  2831. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) | (8 << 16) | (8 << 8) |
  2832. (8 << 0));
  2833. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  2834. /* update cursor SR watermark */
  2835. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  2836. }
  2837. static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
  2838. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2839. int pixel_size)
  2840. {
  2841. struct drm_i915_private *dev_priv = dev->dev_private;
  2842. uint32_t fwater_lo;
  2843. uint32_t fwater_hi;
  2844. int total_size, cacheline_size, cwm, srwm = 1;
  2845. int planea_wm, planeb_wm;
  2846. struct intel_watermark_params planea_params, planeb_params;
  2847. unsigned long line_time_us;
  2848. int sr_clock, sr_entries = 0;
  2849. /* Create copies of the base settings for each pipe */
  2850. if (IS_CRESTLINE(dev) || IS_I945GM(dev))
  2851. planea_params = planeb_params = i945_wm_info;
  2852. else if (!IS_GEN2(dev))
  2853. planea_params = planeb_params = i915_wm_info;
  2854. else
  2855. planea_params = planeb_params = i855_wm_info;
  2856. /* Grab a couple of global values before we overwrite them */
  2857. total_size = planea_params.fifo_size;
  2858. cacheline_size = planea_params.cacheline_size;
  2859. /* Update per-plane FIFO sizes */
  2860. planea_params.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  2861. planeb_params.fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  2862. planea_wm = intel_calculate_wm(planea_clock, &planea_params,
  2863. pixel_size, latency_ns);
  2864. planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
  2865. pixel_size, latency_ns);
  2866. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  2867. /*
  2868. * Overlay gets an aggressive default since video jitter is bad.
  2869. */
  2870. cwm = 2;
  2871. /* Calc sr entries for one plane configs */
  2872. if (HAS_FW_BLC(dev) && sr_hdisplay &&
  2873. (!planea_clock || !planeb_clock)) {
  2874. /* self-refresh has much higher latency */
  2875. static const int sr_latency_ns = 6000;
  2876. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2877. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2878. /* Use ns/us then divide to preserve precision */
  2879. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2880. pixel_size * sr_hdisplay;
  2881. sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
  2882. DRM_DEBUG_KMS("self-refresh entries: %d\n", sr_entries);
  2883. srwm = total_size - sr_entries;
  2884. if (srwm < 0)
  2885. srwm = 1;
  2886. if (IS_I945G(dev) || IS_I945GM(dev))
  2887. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  2888. else if (IS_I915GM(dev)) {
  2889. /* 915M has a smaller SRWM field */
  2890. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  2891. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  2892. }
  2893. } else {
  2894. /* Turn off self refresh if both pipes are enabled */
  2895. if (IS_I945G(dev) || IS_I945GM(dev)) {
  2896. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2897. & ~FW_BLC_SELF_EN);
  2898. } else if (IS_I915GM(dev)) {
  2899. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  2900. }
  2901. }
  2902. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  2903. planea_wm, planeb_wm, cwm, srwm);
  2904. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  2905. fwater_hi = (cwm & 0x1f);
  2906. /* Set request length to 8 cachelines per fetch */
  2907. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  2908. fwater_hi = fwater_hi | (1 << 8);
  2909. I915_WRITE(FW_BLC, fwater_lo);
  2910. I915_WRITE(FW_BLC2, fwater_hi);
  2911. }
  2912. static void i830_update_wm(struct drm_device *dev, int planea_clock, int unused,
  2913. int unused2, int unused3, int pixel_size)
  2914. {
  2915. struct drm_i915_private *dev_priv = dev->dev_private;
  2916. uint32_t fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  2917. int planea_wm;
  2918. i830_wm_info.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  2919. planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
  2920. pixel_size, latency_ns);
  2921. fwater_lo |= (3<<8) | planea_wm;
  2922. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  2923. I915_WRITE(FW_BLC, fwater_lo);
  2924. }
  2925. #define ILK_LP0_PLANE_LATENCY 700
  2926. #define ILK_LP0_CURSOR_LATENCY 1300
  2927. static bool ironlake_compute_wm0(struct drm_device *dev,
  2928. int pipe,
  2929. int *plane_wm,
  2930. int *cursor_wm)
  2931. {
  2932. struct drm_crtc *crtc;
  2933. int htotal, hdisplay, clock, pixel_size = 0;
  2934. int line_time_us, line_count, entries;
  2935. crtc = intel_get_crtc_for_pipe(dev, pipe);
  2936. if (crtc->fb == NULL || !crtc->enabled)
  2937. return false;
  2938. htotal = crtc->mode.htotal;
  2939. hdisplay = crtc->mode.hdisplay;
  2940. clock = crtc->mode.clock;
  2941. pixel_size = crtc->fb->bits_per_pixel / 8;
  2942. /* Use the small buffer method to calculate plane watermark */
  2943. entries = ((clock * pixel_size / 1000) * ILK_LP0_PLANE_LATENCY) / 1000;
  2944. entries = DIV_ROUND_UP(entries,
  2945. ironlake_display_wm_info.cacheline_size);
  2946. *plane_wm = entries + ironlake_display_wm_info.guard_size;
  2947. if (*plane_wm > (int)ironlake_display_wm_info.max_wm)
  2948. *plane_wm = ironlake_display_wm_info.max_wm;
  2949. /* Use the large buffer method to calculate cursor watermark */
  2950. line_time_us = ((htotal * 1000) / clock);
  2951. line_count = (ILK_LP0_CURSOR_LATENCY / line_time_us + 1000) / 1000;
  2952. entries = line_count * 64 * pixel_size;
  2953. entries = DIV_ROUND_UP(entries,
  2954. ironlake_cursor_wm_info.cacheline_size);
  2955. *cursor_wm = entries + ironlake_cursor_wm_info.guard_size;
  2956. if (*cursor_wm > ironlake_cursor_wm_info.max_wm)
  2957. *cursor_wm = ironlake_cursor_wm_info.max_wm;
  2958. return true;
  2959. }
  2960. static void ironlake_update_wm(struct drm_device *dev,
  2961. int planea_clock, int planeb_clock,
  2962. int sr_hdisplay, int sr_htotal,
  2963. int pixel_size)
  2964. {
  2965. struct drm_i915_private *dev_priv = dev->dev_private;
  2966. int plane_wm, cursor_wm, enabled;
  2967. int tmp;
  2968. enabled = 0;
  2969. if (ironlake_compute_wm0(dev, 0, &plane_wm, &cursor_wm)) {
  2970. I915_WRITE(WM0_PIPEA_ILK,
  2971. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  2972. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  2973. " plane %d, " "cursor: %d\n",
  2974. plane_wm, cursor_wm);
  2975. enabled++;
  2976. }
  2977. if (ironlake_compute_wm0(dev, 1, &plane_wm, &cursor_wm)) {
  2978. I915_WRITE(WM0_PIPEB_ILK,
  2979. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  2980. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  2981. " plane %d, cursor: %d\n",
  2982. plane_wm, cursor_wm);
  2983. enabled++;
  2984. }
  2985. /*
  2986. * Calculate and update the self-refresh watermark only when one
  2987. * display plane is used.
  2988. */
  2989. tmp = 0;
  2990. if (enabled == 1) {
  2991. unsigned long line_time_us;
  2992. int small, large, plane_fbc;
  2993. int sr_clock, entries;
  2994. int line_count, line_size;
  2995. /* Read the self-refresh latency. The unit is 0.5us */
  2996. int ilk_sr_latency = I915_READ(MLTR_ILK) & ILK_SRLT_MASK;
  2997. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2998. line_time_us = (sr_htotal * 1000) / sr_clock;
  2999. /* Use ns/us then divide to preserve precision */
  3000. line_count = ((ilk_sr_latency * 500) / line_time_us + 1000)
  3001. / 1000;
  3002. line_size = sr_hdisplay * pixel_size;
  3003. /* Use the minimum of the small and large buffer method for primary */
  3004. small = ((sr_clock * pixel_size / 1000) * (ilk_sr_latency * 500)) / 1000;
  3005. large = line_count * line_size;
  3006. entries = DIV_ROUND_UP(min(small, large),
  3007. ironlake_display_srwm_info.cacheline_size);
  3008. plane_fbc = entries * 64;
  3009. plane_fbc = DIV_ROUND_UP(plane_fbc, line_size);
  3010. plane_wm = entries + ironlake_display_srwm_info.guard_size;
  3011. if (plane_wm > (int)ironlake_display_srwm_info.max_wm)
  3012. plane_wm = ironlake_display_srwm_info.max_wm;
  3013. /* calculate the self-refresh watermark for display cursor */
  3014. entries = line_count * pixel_size * 64;
  3015. entries = DIV_ROUND_UP(entries,
  3016. ironlake_cursor_srwm_info.cacheline_size);
  3017. cursor_wm = entries + ironlake_cursor_srwm_info.guard_size;
  3018. if (cursor_wm > (int)ironlake_cursor_srwm_info.max_wm)
  3019. cursor_wm = ironlake_cursor_srwm_info.max_wm;
  3020. /* configure watermark and enable self-refresh */
  3021. tmp = (WM1_LP_SR_EN |
  3022. (ilk_sr_latency << WM1_LP_LATENCY_SHIFT) |
  3023. (plane_fbc << WM1_LP_FBC_SHIFT) |
  3024. (plane_wm << WM1_LP_SR_SHIFT) |
  3025. cursor_wm);
  3026. DRM_DEBUG_KMS("self-refresh watermark: display plane %d, fbc lines %d,"
  3027. " cursor %d\n", plane_wm, plane_fbc, cursor_wm);
  3028. }
  3029. I915_WRITE(WM1_LP_ILK, tmp);
  3030. /* XXX setup WM2 and WM3 */
  3031. }
  3032. /**
  3033. * intel_update_watermarks - update FIFO watermark values based on current modes
  3034. *
  3035. * Calculate watermark values for the various WM regs based on current mode
  3036. * and plane configuration.
  3037. *
  3038. * There are several cases to deal with here:
  3039. * - normal (i.e. non-self-refresh)
  3040. * - self-refresh (SR) mode
  3041. * - lines are large relative to FIFO size (buffer can hold up to 2)
  3042. * - lines are small relative to FIFO size (buffer can hold more than 2
  3043. * lines), so need to account for TLB latency
  3044. *
  3045. * The normal calculation is:
  3046. * watermark = dotclock * bytes per pixel * latency
  3047. * where latency is platform & configuration dependent (we assume pessimal
  3048. * values here).
  3049. *
  3050. * The SR calculation is:
  3051. * watermark = (trunc(latency/line time)+1) * surface width *
  3052. * bytes per pixel
  3053. * where
  3054. * line time = htotal / dotclock
  3055. * surface width = hdisplay for normal plane and 64 for cursor
  3056. * and latency is assumed to be high, as above.
  3057. *
  3058. * The final value programmed to the register should always be rounded up,
  3059. * and include an extra 2 entries to account for clock crossings.
  3060. *
  3061. * We don't use the sprite, so we can ignore that. And on Crestline we have
  3062. * to set the non-SR watermarks to 8.
  3063. */
  3064. static void intel_update_watermarks(struct drm_device *dev)
  3065. {
  3066. struct drm_i915_private *dev_priv = dev->dev_private;
  3067. struct drm_crtc *crtc;
  3068. int sr_hdisplay = 0;
  3069. unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
  3070. int enabled = 0, pixel_size = 0;
  3071. int sr_htotal = 0;
  3072. if (!dev_priv->display.update_wm)
  3073. return;
  3074. /* Get the clock config from both planes */
  3075. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3076. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3077. if (intel_crtc->active) {
  3078. enabled++;
  3079. if (intel_crtc->plane == 0) {
  3080. DRM_DEBUG_KMS("plane A (pipe %d) clock: %d\n",
  3081. intel_crtc->pipe, crtc->mode.clock);
  3082. planea_clock = crtc->mode.clock;
  3083. } else {
  3084. DRM_DEBUG_KMS("plane B (pipe %d) clock: %d\n",
  3085. intel_crtc->pipe, crtc->mode.clock);
  3086. planeb_clock = crtc->mode.clock;
  3087. }
  3088. sr_hdisplay = crtc->mode.hdisplay;
  3089. sr_clock = crtc->mode.clock;
  3090. sr_htotal = crtc->mode.htotal;
  3091. if (crtc->fb)
  3092. pixel_size = crtc->fb->bits_per_pixel / 8;
  3093. else
  3094. pixel_size = 4; /* by default */
  3095. }
  3096. }
  3097. if (enabled <= 0)
  3098. return;
  3099. dev_priv->display.update_wm(dev, planea_clock, planeb_clock,
  3100. sr_hdisplay, sr_htotal, pixel_size);
  3101. }
  3102. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  3103. struct drm_display_mode *mode,
  3104. struct drm_display_mode *adjusted_mode,
  3105. int x, int y,
  3106. struct drm_framebuffer *old_fb)
  3107. {
  3108. struct drm_device *dev = crtc->dev;
  3109. struct drm_i915_private *dev_priv = dev->dev_private;
  3110. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3111. int pipe = intel_crtc->pipe;
  3112. int plane = intel_crtc->plane;
  3113. u32 fp_reg, dpll_reg;
  3114. int refclk, num_connectors = 0;
  3115. intel_clock_t clock, reduced_clock;
  3116. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  3117. bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
  3118. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  3119. struct intel_encoder *has_edp_encoder = NULL;
  3120. struct drm_mode_config *mode_config = &dev->mode_config;
  3121. struct intel_encoder *encoder;
  3122. const intel_limit_t *limit;
  3123. int ret;
  3124. struct fdi_m_n m_n = {0};
  3125. u32 reg, temp;
  3126. int target_clock;
  3127. drm_vblank_pre_modeset(dev, pipe);
  3128. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  3129. if (encoder->base.crtc != crtc)
  3130. continue;
  3131. switch (encoder->type) {
  3132. case INTEL_OUTPUT_LVDS:
  3133. is_lvds = true;
  3134. break;
  3135. case INTEL_OUTPUT_SDVO:
  3136. case INTEL_OUTPUT_HDMI:
  3137. is_sdvo = true;
  3138. if (encoder->needs_tv_clock)
  3139. is_tv = true;
  3140. break;
  3141. case INTEL_OUTPUT_DVO:
  3142. is_dvo = true;
  3143. break;
  3144. case INTEL_OUTPUT_TVOUT:
  3145. is_tv = true;
  3146. break;
  3147. case INTEL_OUTPUT_ANALOG:
  3148. is_crt = true;
  3149. break;
  3150. case INTEL_OUTPUT_DISPLAYPORT:
  3151. is_dp = true;
  3152. break;
  3153. case INTEL_OUTPUT_EDP:
  3154. has_edp_encoder = encoder;
  3155. break;
  3156. }
  3157. num_connectors++;
  3158. }
  3159. if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2) {
  3160. refclk = dev_priv->lvds_ssc_freq * 1000;
  3161. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3162. refclk / 1000);
  3163. } else if (!IS_GEN2(dev)) {
  3164. refclk = 96000;
  3165. if (HAS_PCH_SPLIT(dev) &&
  3166. (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)))
  3167. refclk = 120000; /* 120Mhz refclk */
  3168. } else {
  3169. refclk = 48000;
  3170. }
  3171. /*
  3172. * Returns a set of divisors for the desired target clock with the given
  3173. * refclk, or FALSE. The returned values represent the clock equation:
  3174. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3175. */
  3176. limit = intel_limit(crtc);
  3177. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  3178. if (!ok) {
  3179. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3180. drm_vblank_post_modeset(dev, pipe);
  3181. return -EINVAL;
  3182. }
  3183. /* Ensure that the cursor is valid for the new mode before changing... */
  3184. intel_crtc_update_cursor(crtc, true);
  3185. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3186. has_reduced_clock = limit->find_pll(limit, crtc,
  3187. dev_priv->lvds_downclock,
  3188. refclk,
  3189. &reduced_clock);
  3190. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  3191. /*
  3192. * If the different P is found, it means that we can't
  3193. * switch the display clock by using the FP0/FP1.
  3194. * In such case we will disable the LVDS downclock
  3195. * feature.
  3196. */
  3197. DRM_DEBUG_KMS("Different P is found for "
  3198. "LVDS clock/downclock\n");
  3199. has_reduced_clock = 0;
  3200. }
  3201. }
  3202. /* SDVO TV has fixed PLL values depend on its clock range,
  3203. this mirrors vbios setting. */
  3204. if (is_sdvo && is_tv) {
  3205. if (adjusted_mode->clock >= 100000
  3206. && adjusted_mode->clock < 140500) {
  3207. clock.p1 = 2;
  3208. clock.p2 = 10;
  3209. clock.n = 3;
  3210. clock.m1 = 16;
  3211. clock.m2 = 8;
  3212. } else if (adjusted_mode->clock >= 140500
  3213. && adjusted_mode->clock <= 200000) {
  3214. clock.p1 = 1;
  3215. clock.p2 = 10;
  3216. clock.n = 6;
  3217. clock.m1 = 12;
  3218. clock.m2 = 8;
  3219. }
  3220. }
  3221. /* FDI link */
  3222. if (HAS_PCH_SPLIT(dev)) {
  3223. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3224. int lane = 0, link_bw, bpp;
  3225. /* CPU eDP doesn't require FDI link, so just set DP M/N
  3226. according to current link config */
  3227. if (has_edp_encoder && !intel_encoder_is_pch_edp(&encoder->base)) {
  3228. target_clock = mode->clock;
  3229. intel_edp_link_config(has_edp_encoder,
  3230. &lane, &link_bw);
  3231. } else {
  3232. /* [e]DP over FDI requires target mode clock
  3233. instead of link clock */
  3234. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  3235. target_clock = mode->clock;
  3236. else
  3237. target_clock = adjusted_mode->clock;
  3238. /* FDI is a binary signal running at ~2.7GHz, encoding
  3239. * each output octet as 10 bits. The actual frequency
  3240. * is stored as a divider into a 100MHz clock, and the
  3241. * mode pixel clock is stored in units of 1KHz.
  3242. * Hence the bw of each lane in terms of the mode signal
  3243. * is:
  3244. */
  3245. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  3246. }
  3247. /* determine panel color depth */
  3248. temp = I915_READ(PIPECONF(pipe));
  3249. temp &= ~PIPE_BPC_MASK;
  3250. if (is_lvds) {
  3251. /* the BPC will be 6 if it is 18-bit LVDS panel */
  3252. if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
  3253. temp |= PIPE_8BPC;
  3254. else
  3255. temp |= PIPE_6BPC;
  3256. } else if (has_edp_encoder) {
  3257. switch (dev_priv->edp.bpp/3) {
  3258. case 8:
  3259. temp |= PIPE_8BPC;
  3260. break;
  3261. case 10:
  3262. temp |= PIPE_10BPC;
  3263. break;
  3264. case 6:
  3265. temp |= PIPE_6BPC;
  3266. break;
  3267. case 12:
  3268. temp |= PIPE_12BPC;
  3269. break;
  3270. }
  3271. } else
  3272. temp |= PIPE_8BPC;
  3273. I915_WRITE(PIPECONF(pipe), temp);
  3274. switch (temp & PIPE_BPC_MASK) {
  3275. case PIPE_8BPC:
  3276. bpp = 24;
  3277. break;
  3278. case PIPE_10BPC:
  3279. bpp = 30;
  3280. break;
  3281. case PIPE_6BPC:
  3282. bpp = 18;
  3283. break;
  3284. case PIPE_12BPC:
  3285. bpp = 36;
  3286. break;
  3287. default:
  3288. DRM_ERROR("unknown pipe bpc value\n");
  3289. bpp = 24;
  3290. }
  3291. if (!lane) {
  3292. /*
  3293. * Account for spread spectrum to avoid
  3294. * oversubscribing the link. Max center spread
  3295. * is 2.5%; use 5% for safety's sake.
  3296. */
  3297. u32 bps = target_clock * bpp * 21 / 20;
  3298. lane = bps / (link_bw * 8) + 1;
  3299. }
  3300. intel_crtc->fdi_lanes = lane;
  3301. if (pixel_multiplier > 1)
  3302. link_bw *= pixel_multiplier;
  3303. ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);
  3304. }
  3305. /* Ironlake: try to setup display ref clock before DPLL
  3306. * enabling. This is only under driver's control after
  3307. * PCH B stepping, previous chipset stepping should be
  3308. * ignoring this setting.
  3309. */
  3310. if (HAS_PCH_SPLIT(dev)) {
  3311. temp = I915_READ(PCH_DREF_CONTROL);
  3312. /* Always enable nonspread source */
  3313. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  3314. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  3315. temp &= ~DREF_SSC_SOURCE_MASK;
  3316. temp |= DREF_SSC_SOURCE_ENABLE;
  3317. I915_WRITE(PCH_DREF_CONTROL, temp);
  3318. POSTING_READ(PCH_DREF_CONTROL);
  3319. udelay(200);
  3320. if (has_edp_encoder) {
  3321. if (dev_priv->lvds_use_ssc) {
  3322. temp |= DREF_SSC1_ENABLE;
  3323. I915_WRITE(PCH_DREF_CONTROL, temp);
  3324. POSTING_READ(PCH_DREF_CONTROL);
  3325. udelay(200);
  3326. }
  3327. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  3328. /* Enable CPU source on CPU attached eDP */
  3329. if (!intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3330. if (dev_priv->lvds_use_ssc)
  3331. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  3332. else
  3333. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  3334. } else {
  3335. /* Enable SSC on PCH eDP if needed */
  3336. if (dev_priv->lvds_use_ssc) {
  3337. DRM_ERROR("enabling SSC on PCH\n");
  3338. temp |= DREF_SUPERSPREAD_SOURCE_ENABLE;
  3339. }
  3340. }
  3341. I915_WRITE(PCH_DREF_CONTROL, temp);
  3342. POSTING_READ(PCH_DREF_CONTROL);
  3343. udelay(200);
  3344. }
  3345. }
  3346. if (IS_PINEVIEW(dev)) {
  3347. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  3348. if (has_reduced_clock)
  3349. fp2 = (1 << reduced_clock.n) << 16 |
  3350. reduced_clock.m1 << 8 | reduced_clock.m2;
  3351. } else {
  3352. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  3353. if (has_reduced_clock)
  3354. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  3355. reduced_clock.m2;
  3356. }
  3357. /* Enable autotuning of the PLL clock (if permissible) */
  3358. if (HAS_PCH_SPLIT(dev)) {
  3359. int factor = 21;
  3360. if (is_lvds) {
  3361. if ((dev_priv->lvds_use_ssc &&
  3362. dev_priv->lvds_ssc_freq == 100) ||
  3363. (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
  3364. factor = 25;
  3365. } else if (is_sdvo && is_tv)
  3366. factor = 20;
  3367. if (clock.m1 < factor * clock.n)
  3368. fp |= FP_CB_TUNE;
  3369. }
  3370. dpll = 0;
  3371. if (!HAS_PCH_SPLIT(dev))
  3372. dpll = DPLL_VGA_MODE_DIS;
  3373. if (!IS_GEN2(dev)) {
  3374. if (is_lvds)
  3375. dpll |= DPLLB_MODE_LVDS;
  3376. else
  3377. dpll |= DPLLB_MODE_DAC_SERIAL;
  3378. if (is_sdvo) {
  3379. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3380. if (pixel_multiplier > 1) {
  3381. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3382. dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  3383. else if (HAS_PCH_SPLIT(dev))
  3384. dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  3385. }
  3386. dpll |= DPLL_DVO_HIGH_SPEED;
  3387. }
  3388. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  3389. dpll |= DPLL_DVO_HIGH_SPEED;
  3390. /* compute bitmask from p1 value */
  3391. if (IS_PINEVIEW(dev))
  3392. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  3393. else {
  3394. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3395. /* also FPA1 */
  3396. if (HAS_PCH_SPLIT(dev))
  3397. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3398. if (IS_G4X(dev) && has_reduced_clock)
  3399. dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3400. }
  3401. switch (clock.p2) {
  3402. case 5:
  3403. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3404. break;
  3405. case 7:
  3406. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3407. break;
  3408. case 10:
  3409. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3410. break;
  3411. case 14:
  3412. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3413. break;
  3414. }
  3415. if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev))
  3416. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  3417. } else {
  3418. if (is_lvds) {
  3419. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3420. } else {
  3421. if (clock.p1 == 2)
  3422. dpll |= PLL_P1_DIVIDE_BY_TWO;
  3423. else
  3424. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3425. if (clock.p2 == 4)
  3426. dpll |= PLL_P2_DIVIDE_BY_4;
  3427. }
  3428. }
  3429. if (is_sdvo && is_tv)
  3430. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3431. else if (is_tv)
  3432. /* XXX: just matching BIOS for now */
  3433. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3434. dpll |= 3;
  3435. else if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2)
  3436. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3437. else
  3438. dpll |= PLL_REF_INPUT_DREFCLK;
  3439. /* setup pipeconf */
  3440. pipeconf = I915_READ(PIPECONF(pipe));
  3441. /* Set up the display plane register */
  3442. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3443. /* Ironlake's plane is forced to pipe, bit 24 is to
  3444. enable color space conversion */
  3445. if (!HAS_PCH_SPLIT(dev)) {
  3446. if (pipe == 0)
  3447. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3448. else
  3449. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3450. }
  3451. if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  3452. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  3453. * core speed.
  3454. *
  3455. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  3456. * pipe == 0 check?
  3457. */
  3458. if (mode->clock >
  3459. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  3460. pipeconf |= PIPECONF_DOUBLE_WIDE;
  3461. else
  3462. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  3463. }
  3464. dspcntr |= DISPLAY_PLANE_ENABLE;
  3465. pipeconf |= PIPECONF_ENABLE;
  3466. dpll |= DPLL_VCO_ENABLE;
  3467. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  3468. drm_mode_debug_printmodeline(mode);
  3469. /* assign to Ironlake registers */
  3470. if (HAS_PCH_SPLIT(dev)) {
  3471. fp_reg = PCH_FP0(pipe);
  3472. dpll_reg = PCH_DPLL(pipe);
  3473. } else {
  3474. fp_reg = FP0(pipe);
  3475. dpll_reg = DPLL(pipe);
  3476. }
  3477. /* PCH eDP needs FDI, but CPU eDP does not */
  3478. if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3479. I915_WRITE(fp_reg, fp);
  3480. I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
  3481. POSTING_READ(dpll_reg);
  3482. udelay(150);
  3483. }
  3484. /* enable transcoder DPLL */
  3485. if (HAS_PCH_CPT(dev)) {
  3486. temp = I915_READ(PCH_DPLL_SEL);
  3487. if (pipe == 0)
  3488. temp |= TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL;
  3489. else
  3490. temp |= TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL;
  3491. I915_WRITE(PCH_DPLL_SEL, temp);
  3492. POSTING_READ(PCH_DPLL_SEL);
  3493. udelay(150);
  3494. }
  3495. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3496. * This is an exception to the general rule that mode_set doesn't turn
  3497. * things on.
  3498. */
  3499. if (is_lvds) {
  3500. reg = LVDS;
  3501. if (HAS_PCH_SPLIT(dev))
  3502. reg = PCH_LVDS;
  3503. temp = I915_READ(reg);
  3504. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  3505. if (pipe == 1) {
  3506. if (HAS_PCH_CPT(dev))
  3507. temp |= PORT_TRANS_B_SEL_CPT;
  3508. else
  3509. temp |= LVDS_PIPEB_SELECT;
  3510. } else {
  3511. if (HAS_PCH_CPT(dev))
  3512. temp &= ~PORT_TRANS_SEL_MASK;
  3513. else
  3514. temp &= ~LVDS_PIPEB_SELECT;
  3515. }
  3516. /* set the corresponsding LVDS_BORDER bit */
  3517. temp |= dev_priv->lvds_border_bits;
  3518. /* Set the B0-B3 data pairs corresponding to whether we're going to
  3519. * set the DPLLs for dual-channel mode or not.
  3520. */
  3521. if (clock.p2 == 7)
  3522. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  3523. else
  3524. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  3525. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  3526. * appropriately here, but we need to look more thoroughly into how
  3527. * panels behave in the two modes.
  3528. */
  3529. /* set the dithering flag on non-PCH LVDS as needed */
  3530. if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
  3531. if (dev_priv->lvds_dither)
  3532. temp |= LVDS_ENABLE_DITHER;
  3533. else
  3534. temp &= ~LVDS_ENABLE_DITHER;
  3535. }
  3536. I915_WRITE(reg, temp);
  3537. }
  3538. /* set the dithering flag and clear for anything other than a panel. */
  3539. if (HAS_PCH_SPLIT(dev)) {
  3540. pipeconf &= ~PIPECONF_DITHER_EN;
  3541. pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
  3542. if (dev_priv->lvds_dither && (is_lvds || has_edp_encoder)) {
  3543. pipeconf |= PIPECONF_DITHER_EN;
  3544. pipeconf |= PIPECONF_DITHER_TYPE_ST1;
  3545. }
  3546. }
  3547. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3548. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3549. } else if (HAS_PCH_SPLIT(dev)) {
  3550. /* For non-DP output, clear any trans DP clock recovery setting.*/
  3551. if (pipe == 0) {
  3552. I915_WRITE(TRANSA_DATA_M1, 0);
  3553. I915_WRITE(TRANSA_DATA_N1, 0);
  3554. I915_WRITE(TRANSA_DP_LINK_M1, 0);
  3555. I915_WRITE(TRANSA_DP_LINK_N1, 0);
  3556. } else {
  3557. I915_WRITE(TRANSB_DATA_M1, 0);
  3558. I915_WRITE(TRANSB_DATA_N1, 0);
  3559. I915_WRITE(TRANSB_DP_LINK_M1, 0);
  3560. I915_WRITE(TRANSB_DP_LINK_N1, 0);
  3561. }
  3562. }
  3563. if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3564. I915_WRITE(dpll_reg, dpll);
  3565. /* Wait for the clocks to stabilize. */
  3566. POSTING_READ(dpll_reg);
  3567. udelay(150);
  3568. if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
  3569. temp = 0;
  3570. if (is_sdvo) {
  3571. temp = intel_mode_get_pixel_multiplier(adjusted_mode);
  3572. if (temp > 1)
  3573. temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3574. else
  3575. temp = 0;
  3576. }
  3577. I915_WRITE(DPLL_MD(pipe), temp);
  3578. } else {
  3579. /* The pixel multiplier can only be updated once the
  3580. * DPLL is enabled and the clocks are stable.
  3581. *
  3582. * So write it again.
  3583. */
  3584. I915_WRITE(dpll_reg, dpll);
  3585. }
  3586. }
  3587. intel_crtc->lowfreq_avail = false;
  3588. if (is_lvds && has_reduced_clock && i915_powersave) {
  3589. I915_WRITE(fp_reg + 4, fp2);
  3590. intel_crtc->lowfreq_avail = true;
  3591. if (HAS_PIPE_CXSR(dev)) {
  3592. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  3593. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  3594. }
  3595. } else {
  3596. I915_WRITE(fp_reg + 4, fp);
  3597. if (HAS_PIPE_CXSR(dev)) {
  3598. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  3599. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  3600. }
  3601. }
  3602. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3603. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  3604. /* the chip adds 2 halflines automatically */
  3605. adjusted_mode->crtc_vdisplay -= 1;
  3606. adjusted_mode->crtc_vtotal -= 1;
  3607. adjusted_mode->crtc_vblank_start -= 1;
  3608. adjusted_mode->crtc_vblank_end -= 1;
  3609. adjusted_mode->crtc_vsync_end -= 1;
  3610. adjusted_mode->crtc_vsync_start -= 1;
  3611. } else
  3612. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  3613. I915_WRITE(HTOTAL(pipe),
  3614. (adjusted_mode->crtc_hdisplay - 1) |
  3615. ((adjusted_mode->crtc_htotal - 1) << 16));
  3616. I915_WRITE(HBLANK(pipe),
  3617. (adjusted_mode->crtc_hblank_start - 1) |
  3618. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3619. I915_WRITE(HSYNC(pipe),
  3620. (adjusted_mode->crtc_hsync_start - 1) |
  3621. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3622. I915_WRITE(VTOTAL(pipe),
  3623. (adjusted_mode->crtc_vdisplay - 1) |
  3624. ((adjusted_mode->crtc_vtotal - 1) << 16));
  3625. I915_WRITE(VBLANK(pipe),
  3626. (adjusted_mode->crtc_vblank_start - 1) |
  3627. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  3628. I915_WRITE(VSYNC(pipe),
  3629. (adjusted_mode->crtc_vsync_start - 1) |
  3630. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3631. /* pipesrc and dspsize control the size that is scaled from,
  3632. * which should always be the user's requested size.
  3633. */
  3634. if (!HAS_PCH_SPLIT(dev)) {
  3635. I915_WRITE(DSPSIZE(plane),
  3636. ((mode->vdisplay - 1) << 16) |
  3637. (mode->hdisplay - 1));
  3638. I915_WRITE(DSPPOS(plane), 0);
  3639. }
  3640. I915_WRITE(PIPESRC(pipe),
  3641. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3642. if (HAS_PCH_SPLIT(dev)) {
  3643. I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
  3644. I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
  3645. I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
  3646. I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
  3647. if (has_edp_encoder && !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3648. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  3649. }
  3650. }
  3651. I915_WRITE(PIPECONF(pipe), pipeconf);
  3652. POSTING_READ(PIPECONF(pipe));
  3653. intel_wait_for_vblank(dev, pipe);
  3654. if (IS_GEN5(dev)) {
  3655. /* enable address swizzle for tiling buffer */
  3656. temp = I915_READ(DISP_ARB_CTL);
  3657. I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
  3658. }
  3659. I915_WRITE(DSPCNTR(plane), dspcntr);
  3660. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  3661. intel_update_watermarks(dev);
  3662. drm_vblank_post_modeset(dev, pipe);
  3663. return ret;
  3664. }
  3665. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3666. void intel_crtc_load_lut(struct drm_crtc *crtc)
  3667. {
  3668. struct drm_device *dev = crtc->dev;
  3669. struct drm_i915_private *dev_priv = dev->dev_private;
  3670. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3671. int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
  3672. int i;
  3673. /* The clocks have to be on to load the palette. */
  3674. if (!crtc->enabled)
  3675. return;
  3676. /* use legacy palette for Ironlake */
  3677. if (HAS_PCH_SPLIT(dev))
  3678. palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
  3679. LGC_PALETTE_B;
  3680. for (i = 0; i < 256; i++) {
  3681. I915_WRITE(palreg + 4 * i,
  3682. (intel_crtc->lut_r[i] << 16) |
  3683. (intel_crtc->lut_g[i] << 8) |
  3684. intel_crtc->lut_b[i]);
  3685. }
  3686. }
  3687. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  3688. {
  3689. struct drm_device *dev = crtc->dev;
  3690. struct drm_i915_private *dev_priv = dev->dev_private;
  3691. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3692. bool visible = base != 0;
  3693. u32 cntl;
  3694. if (intel_crtc->cursor_visible == visible)
  3695. return;
  3696. cntl = I915_READ(CURACNTR);
  3697. if (visible) {
  3698. /* On these chipsets we can only modify the base whilst
  3699. * the cursor is disabled.
  3700. */
  3701. I915_WRITE(CURABASE, base);
  3702. cntl &= ~(CURSOR_FORMAT_MASK);
  3703. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  3704. cntl |= CURSOR_ENABLE |
  3705. CURSOR_GAMMA_ENABLE |
  3706. CURSOR_FORMAT_ARGB;
  3707. } else
  3708. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  3709. I915_WRITE(CURACNTR, cntl);
  3710. intel_crtc->cursor_visible = visible;
  3711. }
  3712. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  3713. {
  3714. struct drm_device *dev = crtc->dev;
  3715. struct drm_i915_private *dev_priv = dev->dev_private;
  3716. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3717. int pipe = intel_crtc->pipe;
  3718. bool visible = base != 0;
  3719. if (intel_crtc->cursor_visible != visible) {
  3720. uint32_t cntl = I915_READ(pipe == 0 ? CURACNTR : CURBCNTR);
  3721. if (base) {
  3722. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  3723. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  3724. cntl |= pipe << 28; /* Connect to correct pipe */
  3725. } else {
  3726. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  3727. cntl |= CURSOR_MODE_DISABLE;
  3728. }
  3729. I915_WRITE(pipe == 0 ? CURACNTR : CURBCNTR, cntl);
  3730. intel_crtc->cursor_visible = visible;
  3731. }
  3732. /* and commit changes on next vblank */
  3733. I915_WRITE(pipe == 0 ? CURABASE : CURBBASE, base);
  3734. }
  3735. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  3736. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  3737. bool on)
  3738. {
  3739. struct drm_device *dev = crtc->dev;
  3740. struct drm_i915_private *dev_priv = dev->dev_private;
  3741. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3742. int pipe = intel_crtc->pipe;
  3743. int x = intel_crtc->cursor_x;
  3744. int y = intel_crtc->cursor_y;
  3745. u32 base, pos;
  3746. bool visible;
  3747. pos = 0;
  3748. if (on && crtc->enabled && crtc->fb) {
  3749. base = intel_crtc->cursor_addr;
  3750. if (x > (int) crtc->fb->width)
  3751. base = 0;
  3752. if (y > (int) crtc->fb->height)
  3753. base = 0;
  3754. } else
  3755. base = 0;
  3756. if (x < 0) {
  3757. if (x + intel_crtc->cursor_width < 0)
  3758. base = 0;
  3759. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  3760. x = -x;
  3761. }
  3762. pos |= x << CURSOR_X_SHIFT;
  3763. if (y < 0) {
  3764. if (y + intel_crtc->cursor_height < 0)
  3765. base = 0;
  3766. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  3767. y = -y;
  3768. }
  3769. pos |= y << CURSOR_Y_SHIFT;
  3770. visible = base != 0;
  3771. if (!visible && !intel_crtc->cursor_visible)
  3772. return;
  3773. I915_WRITE(pipe == 0 ? CURAPOS : CURBPOS, pos);
  3774. if (IS_845G(dev) || IS_I865G(dev))
  3775. i845_update_cursor(crtc, base);
  3776. else
  3777. i9xx_update_cursor(crtc, base);
  3778. if (visible)
  3779. intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
  3780. }
  3781. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  3782. struct drm_file *file,
  3783. uint32_t handle,
  3784. uint32_t width, uint32_t height)
  3785. {
  3786. struct drm_device *dev = crtc->dev;
  3787. struct drm_i915_private *dev_priv = dev->dev_private;
  3788. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3789. struct drm_i915_gem_object *obj;
  3790. uint32_t addr;
  3791. int ret;
  3792. DRM_DEBUG_KMS("\n");
  3793. /* if we want to turn off the cursor ignore width and height */
  3794. if (!handle) {
  3795. DRM_DEBUG_KMS("cursor off\n");
  3796. addr = 0;
  3797. obj = NULL;
  3798. mutex_lock(&dev->struct_mutex);
  3799. goto finish;
  3800. }
  3801. /* Currently we only support 64x64 cursors */
  3802. if (width != 64 || height != 64) {
  3803. DRM_ERROR("we currently only support 64x64 cursors\n");
  3804. return -EINVAL;
  3805. }
  3806. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  3807. if (!obj)
  3808. return -ENOENT;
  3809. if (obj->base.size < width * height * 4) {
  3810. DRM_ERROR("buffer is to small\n");
  3811. ret = -ENOMEM;
  3812. goto fail;
  3813. }
  3814. /* we only need to pin inside GTT if cursor is non-phy */
  3815. mutex_lock(&dev->struct_mutex);
  3816. if (!dev_priv->info->cursor_needs_physical) {
  3817. if (obj->tiling_mode) {
  3818. DRM_ERROR("cursor cannot be tiled\n");
  3819. ret = -EINVAL;
  3820. goto fail_locked;
  3821. }
  3822. ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
  3823. if (ret) {
  3824. DRM_ERROR("failed to pin cursor bo\n");
  3825. goto fail_locked;
  3826. }
  3827. ret = i915_gem_object_set_to_gtt_domain(obj, 0);
  3828. if (ret) {
  3829. DRM_ERROR("failed to move cursor bo into the GTT\n");
  3830. goto fail_unpin;
  3831. }
  3832. ret = i915_gem_object_put_fence(obj);
  3833. if (ret) {
  3834. DRM_ERROR("failed to move cursor bo into the GTT\n");
  3835. goto fail_unpin;
  3836. }
  3837. addr = obj->gtt_offset;
  3838. } else {
  3839. int align = IS_I830(dev) ? 16 * 1024 : 256;
  3840. ret = i915_gem_attach_phys_object(dev, obj,
  3841. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  3842. align);
  3843. if (ret) {
  3844. DRM_ERROR("failed to attach phys object\n");
  3845. goto fail_locked;
  3846. }
  3847. addr = obj->phys_obj->handle->busaddr;
  3848. }
  3849. if (IS_GEN2(dev))
  3850. I915_WRITE(CURSIZE, (height << 12) | width);
  3851. finish:
  3852. if (intel_crtc->cursor_bo) {
  3853. if (dev_priv->info->cursor_needs_physical) {
  3854. if (intel_crtc->cursor_bo != obj)
  3855. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  3856. } else
  3857. i915_gem_object_unpin(intel_crtc->cursor_bo);
  3858. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  3859. }
  3860. mutex_unlock(&dev->struct_mutex);
  3861. intel_crtc->cursor_addr = addr;
  3862. intel_crtc->cursor_bo = obj;
  3863. intel_crtc->cursor_width = width;
  3864. intel_crtc->cursor_height = height;
  3865. intel_crtc_update_cursor(crtc, true);
  3866. return 0;
  3867. fail_unpin:
  3868. i915_gem_object_unpin(obj);
  3869. fail_locked:
  3870. mutex_unlock(&dev->struct_mutex);
  3871. fail:
  3872. drm_gem_object_unreference_unlocked(&obj->base);
  3873. return ret;
  3874. }
  3875. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  3876. {
  3877. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3878. intel_crtc->cursor_x = x;
  3879. intel_crtc->cursor_y = y;
  3880. intel_crtc_update_cursor(crtc, true);
  3881. return 0;
  3882. }
  3883. /** Sets the color ramps on behalf of RandR */
  3884. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  3885. u16 blue, int regno)
  3886. {
  3887. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3888. intel_crtc->lut_r[regno] = red >> 8;
  3889. intel_crtc->lut_g[regno] = green >> 8;
  3890. intel_crtc->lut_b[regno] = blue >> 8;
  3891. }
  3892. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  3893. u16 *blue, int regno)
  3894. {
  3895. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3896. *red = intel_crtc->lut_r[regno] << 8;
  3897. *green = intel_crtc->lut_g[regno] << 8;
  3898. *blue = intel_crtc->lut_b[regno] << 8;
  3899. }
  3900. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  3901. u16 *blue, uint32_t start, uint32_t size)
  3902. {
  3903. int end = (start + size > 256) ? 256 : start + size, i;
  3904. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3905. for (i = start; i < end; i++) {
  3906. intel_crtc->lut_r[i] = red[i] >> 8;
  3907. intel_crtc->lut_g[i] = green[i] >> 8;
  3908. intel_crtc->lut_b[i] = blue[i] >> 8;
  3909. }
  3910. intel_crtc_load_lut(crtc);
  3911. }
  3912. /**
  3913. * Get a pipe with a simple mode set on it for doing load-based monitor
  3914. * detection.
  3915. *
  3916. * It will be up to the load-detect code to adjust the pipe as appropriate for
  3917. * its requirements. The pipe will be connected to no other encoders.
  3918. *
  3919. * Currently this code will only succeed if there is a pipe with no encoders
  3920. * configured for it. In the future, it could choose to temporarily disable
  3921. * some outputs to free up a pipe for its use.
  3922. *
  3923. * \return crtc, or NULL if no pipes are available.
  3924. */
  3925. /* VESA 640x480x72Hz mode to set on the pipe */
  3926. static struct drm_display_mode load_detect_mode = {
  3927. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  3928. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  3929. };
  3930. struct drm_crtc *intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  3931. struct drm_connector *connector,
  3932. struct drm_display_mode *mode,
  3933. int *dpms_mode)
  3934. {
  3935. struct intel_crtc *intel_crtc;
  3936. struct drm_crtc *possible_crtc;
  3937. struct drm_crtc *supported_crtc =NULL;
  3938. struct drm_encoder *encoder = &intel_encoder->base;
  3939. struct drm_crtc *crtc = NULL;
  3940. struct drm_device *dev = encoder->dev;
  3941. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  3942. struct drm_crtc_helper_funcs *crtc_funcs;
  3943. int i = -1;
  3944. /*
  3945. * Algorithm gets a little messy:
  3946. * - if the connector already has an assigned crtc, use it (but make
  3947. * sure it's on first)
  3948. * - try to find the first unused crtc that can drive this connector,
  3949. * and use that if we find one
  3950. * - if there are no unused crtcs available, try to use the first
  3951. * one we found that supports the connector
  3952. */
  3953. /* See if we already have a CRTC for this connector */
  3954. if (encoder->crtc) {
  3955. crtc = encoder->crtc;
  3956. /* Make sure the crtc and connector are running */
  3957. intel_crtc = to_intel_crtc(crtc);
  3958. *dpms_mode = intel_crtc->dpms_mode;
  3959. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  3960. crtc_funcs = crtc->helper_private;
  3961. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  3962. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  3963. }
  3964. return crtc;
  3965. }
  3966. /* Find an unused one (if possible) */
  3967. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  3968. i++;
  3969. if (!(encoder->possible_crtcs & (1 << i)))
  3970. continue;
  3971. if (!possible_crtc->enabled) {
  3972. crtc = possible_crtc;
  3973. break;
  3974. }
  3975. if (!supported_crtc)
  3976. supported_crtc = possible_crtc;
  3977. }
  3978. /*
  3979. * If we didn't find an unused CRTC, don't use any.
  3980. */
  3981. if (!crtc) {
  3982. return NULL;
  3983. }
  3984. encoder->crtc = crtc;
  3985. connector->encoder = encoder;
  3986. intel_encoder->load_detect_temp = true;
  3987. intel_crtc = to_intel_crtc(crtc);
  3988. *dpms_mode = intel_crtc->dpms_mode;
  3989. if (!crtc->enabled) {
  3990. if (!mode)
  3991. mode = &load_detect_mode;
  3992. drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
  3993. } else {
  3994. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  3995. crtc_funcs = crtc->helper_private;
  3996. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  3997. }
  3998. /* Add this connector to the crtc */
  3999. encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
  4000. encoder_funcs->commit(encoder);
  4001. }
  4002. /* let the connector get through one full cycle before testing */
  4003. intel_wait_for_vblank(dev, intel_crtc->pipe);
  4004. return crtc;
  4005. }
  4006. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  4007. struct drm_connector *connector, int dpms_mode)
  4008. {
  4009. struct drm_encoder *encoder = &intel_encoder->base;
  4010. struct drm_device *dev = encoder->dev;
  4011. struct drm_crtc *crtc = encoder->crtc;
  4012. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  4013. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  4014. if (intel_encoder->load_detect_temp) {
  4015. encoder->crtc = NULL;
  4016. connector->encoder = NULL;
  4017. intel_encoder->load_detect_temp = false;
  4018. crtc->enabled = drm_helper_crtc_in_use(crtc);
  4019. drm_helper_disable_unused_functions(dev);
  4020. }
  4021. /* Switch crtc and encoder back off if necessary */
  4022. if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
  4023. if (encoder->crtc == crtc)
  4024. encoder_funcs->dpms(encoder, dpms_mode);
  4025. crtc_funcs->dpms(crtc, dpms_mode);
  4026. }
  4027. }
  4028. /* Returns the clock of the currently programmed mode of the given pipe. */
  4029. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  4030. {
  4031. struct drm_i915_private *dev_priv = dev->dev_private;
  4032. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4033. int pipe = intel_crtc->pipe;
  4034. u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
  4035. u32 fp;
  4036. intel_clock_t clock;
  4037. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  4038. fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
  4039. else
  4040. fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
  4041. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  4042. if (IS_PINEVIEW(dev)) {
  4043. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  4044. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4045. } else {
  4046. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  4047. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4048. }
  4049. if (!IS_GEN2(dev)) {
  4050. if (IS_PINEVIEW(dev))
  4051. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  4052. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  4053. else
  4054. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  4055. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4056. switch (dpll & DPLL_MODE_MASK) {
  4057. case DPLLB_MODE_DAC_SERIAL:
  4058. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  4059. 5 : 10;
  4060. break;
  4061. case DPLLB_MODE_LVDS:
  4062. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  4063. 7 : 14;
  4064. break;
  4065. default:
  4066. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  4067. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  4068. return 0;
  4069. }
  4070. /* XXX: Handle the 100Mhz refclk */
  4071. intel_clock(dev, 96000, &clock);
  4072. } else {
  4073. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  4074. if (is_lvds) {
  4075. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  4076. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4077. clock.p2 = 14;
  4078. if ((dpll & PLL_REF_INPUT_MASK) ==
  4079. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  4080. /* XXX: might not be 66MHz */
  4081. intel_clock(dev, 66000, &clock);
  4082. } else
  4083. intel_clock(dev, 48000, &clock);
  4084. } else {
  4085. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  4086. clock.p1 = 2;
  4087. else {
  4088. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  4089. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  4090. }
  4091. if (dpll & PLL_P2_DIVIDE_BY_4)
  4092. clock.p2 = 4;
  4093. else
  4094. clock.p2 = 2;
  4095. intel_clock(dev, 48000, &clock);
  4096. }
  4097. }
  4098. /* XXX: It would be nice to validate the clocks, but we can't reuse
  4099. * i830PllIsValid() because it relies on the xf86_config connector
  4100. * configuration being accurate, which it isn't necessarily.
  4101. */
  4102. return clock.dot;
  4103. }
  4104. /** Returns the currently programmed mode of the given pipe. */
  4105. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  4106. struct drm_crtc *crtc)
  4107. {
  4108. struct drm_i915_private *dev_priv = dev->dev_private;
  4109. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4110. int pipe = intel_crtc->pipe;
  4111. struct drm_display_mode *mode;
  4112. int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  4113. int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  4114. int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  4115. int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  4116. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  4117. if (!mode)
  4118. return NULL;
  4119. mode->clock = intel_crtc_clock_get(dev, crtc);
  4120. mode->hdisplay = (htot & 0xffff) + 1;
  4121. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  4122. mode->hsync_start = (hsync & 0xffff) + 1;
  4123. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  4124. mode->vdisplay = (vtot & 0xffff) + 1;
  4125. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  4126. mode->vsync_start = (vsync & 0xffff) + 1;
  4127. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  4128. drm_mode_set_name(mode);
  4129. drm_mode_set_crtcinfo(mode, 0);
  4130. return mode;
  4131. }
  4132. #define GPU_IDLE_TIMEOUT 500 /* ms */
  4133. /* When this timer fires, we've been idle for awhile */
  4134. static void intel_gpu_idle_timer(unsigned long arg)
  4135. {
  4136. struct drm_device *dev = (struct drm_device *)arg;
  4137. drm_i915_private_t *dev_priv = dev->dev_private;
  4138. if (!list_empty(&dev_priv->mm.active_list)) {
  4139. /* Still processing requests, so just re-arm the timer. */
  4140. mod_timer(&dev_priv->idle_timer, jiffies +
  4141. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4142. return;
  4143. }
  4144. dev_priv->busy = false;
  4145. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4146. }
  4147. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  4148. static void intel_crtc_idle_timer(unsigned long arg)
  4149. {
  4150. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  4151. struct drm_crtc *crtc = &intel_crtc->base;
  4152. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  4153. struct intel_framebuffer *intel_fb;
  4154. intel_fb = to_intel_framebuffer(crtc->fb);
  4155. if (intel_fb && intel_fb->obj->active) {
  4156. /* The framebuffer is still being accessed by the GPU. */
  4157. mod_timer(&intel_crtc->idle_timer, jiffies +
  4158. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4159. return;
  4160. }
  4161. intel_crtc->busy = false;
  4162. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4163. }
  4164. static void intel_increase_pllclock(struct drm_crtc *crtc)
  4165. {
  4166. struct drm_device *dev = crtc->dev;
  4167. drm_i915_private_t *dev_priv = dev->dev_private;
  4168. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4169. int pipe = intel_crtc->pipe;
  4170. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  4171. int dpll = I915_READ(dpll_reg);
  4172. if (HAS_PCH_SPLIT(dev))
  4173. return;
  4174. if (!dev_priv->lvds_downclock_avail)
  4175. return;
  4176. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  4177. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  4178. /* Unlock panel regs */
  4179. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  4180. PANEL_UNLOCK_REGS);
  4181. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  4182. I915_WRITE(dpll_reg, dpll);
  4183. dpll = I915_READ(dpll_reg);
  4184. intel_wait_for_vblank(dev, pipe);
  4185. dpll = I915_READ(dpll_reg);
  4186. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  4187. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  4188. /* ...and lock them again */
  4189. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  4190. }
  4191. /* Schedule downclock */
  4192. mod_timer(&intel_crtc->idle_timer, jiffies +
  4193. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4194. }
  4195. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  4196. {
  4197. struct drm_device *dev = crtc->dev;
  4198. drm_i915_private_t *dev_priv = dev->dev_private;
  4199. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4200. int pipe = intel_crtc->pipe;
  4201. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  4202. int dpll = I915_READ(dpll_reg);
  4203. if (HAS_PCH_SPLIT(dev))
  4204. return;
  4205. if (!dev_priv->lvds_downclock_avail)
  4206. return;
  4207. /*
  4208. * Since this is called by a timer, we should never get here in
  4209. * the manual case.
  4210. */
  4211. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  4212. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  4213. /* Unlock panel regs */
  4214. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  4215. PANEL_UNLOCK_REGS);
  4216. dpll |= DISPLAY_RATE_SELECT_FPA1;
  4217. I915_WRITE(dpll_reg, dpll);
  4218. dpll = I915_READ(dpll_reg);
  4219. intel_wait_for_vblank(dev, pipe);
  4220. dpll = I915_READ(dpll_reg);
  4221. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  4222. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  4223. /* ...and lock them again */
  4224. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  4225. }
  4226. }
  4227. /**
  4228. * intel_idle_update - adjust clocks for idleness
  4229. * @work: work struct
  4230. *
  4231. * Either the GPU or display (or both) went idle. Check the busy status
  4232. * here and adjust the CRTC and GPU clocks as necessary.
  4233. */
  4234. static void intel_idle_update(struct work_struct *work)
  4235. {
  4236. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  4237. idle_work);
  4238. struct drm_device *dev = dev_priv->dev;
  4239. struct drm_crtc *crtc;
  4240. struct intel_crtc *intel_crtc;
  4241. int enabled = 0;
  4242. if (!i915_powersave)
  4243. return;
  4244. mutex_lock(&dev->struct_mutex);
  4245. i915_update_gfx_val(dev_priv);
  4246. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4247. /* Skip inactive CRTCs */
  4248. if (!crtc->fb)
  4249. continue;
  4250. enabled++;
  4251. intel_crtc = to_intel_crtc(crtc);
  4252. if (!intel_crtc->busy)
  4253. intel_decrease_pllclock(crtc);
  4254. }
  4255. if ((enabled == 1) && (IS_I945G(dev) || IS_I945GM(dev))) {
  4256. DRM_DEBUG_DRIVER("enable memory self refresh on 945\n");
  4257. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  4258. }
  4259. mutex_unlock(&dev->struct_mutex);
  4260. }
  4261. /**
  4262. * intel_mark_busy - mark the GPU and possibly the display busy
  4263. * @dev: drm device
  4264. * @obj: object we're operating on
  4265. *
  4266. * Callers can use this function to indicate that the GPU is busy processing
  4267. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  4268. * buffer), we'll also mark the display as busy, so we know to increase its
  4269. * clock frequency.
  4270. */
  4271. void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
  4272. {
  4273. drm_i915_private_t *dev_priv = dev->dev_private;
  4274. struct drm_crtc *crtc = NULL;
  4275. struct intel_framebuffer *intel_fb;
  4276. struct intel_crtc *intel_crtc;
  4277. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4278. return;
  4279. if (!dev_priv->busy) {
  4280. if (IS_I945G(dev) || IS_I945GM(dev)) {
  4281. u32 fw_blc_self;
  4282. DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
  4283. fw_blc_self = I915_READ(FW_BLC_SELF);
  4284. fw_blc_self &= ~FW_BLC_SELF_EN;
  4285. I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
  4286. }
  4287. dev_priv->busy = true;
  4288. } else
  4289. mod_timer(&dev_priv->idle_timer, jiffies +
  4290. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4291. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4292. if (!crtc->fb)
  4293. continue;
  4294. intel_crtc = to_intel_crtc(crtc);
  4295. intel_fb = to_intel_framebuffer(crtc->fb);
  4296. if (intel_fb->obj == obj) {
  4297. if (!intel_crtc->busy) {
  4298. if (IS_I945G(dev) || IS_I945GM(dev)) {
  4299. u32 fw_blc_self;
  4300. DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
  4301. fw_blc_self = I915_READ(FW_BLC_SELF);
  4302. fw_blc_self &= ~FW_BLC_SELF_EN;
  4303. I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
  4304. }
  4305. /* Non-busy -> busy, upclock */
  4306. intel_increase_pllclock(crtc);
  4307. intel_crtc->busy = true;
  4308. } else {
  4309. /* Busy -> busy, put off timer */
  4310. mod_timer(&intel_crtc->idle_timer, jiffies +
  4311. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4312. }
  4313. }
  4314. }
  4315. }
  4316. static void intel_crtc_destroy(struct drm_crtc *crtc)
  4317. {
  4318. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4319. struct drm_device *dev = crtc->dev;
  4320. struct intel_unpin_work *work;
  4321. unsigned long flags;
  4322. spin_lock_irqsave(&dev->event_lock, flags);
  4323. work = intel_crtc->unpin_work;
  4324. intel_crtc->unpin_work = NULL;
  4325. spin_unlock_irqrestore(&dev->event_lock, flags);
  4326. if (work) {
  4327. cancel_work_sync(&work->work);
  4328. kfree(work);
  4329. }
  4330. drm_crtc_cleanup(crtc);
  4331. kfree(intel_crtc);
  4332. }
  4333. static void intel_unpin_work_fn(struct work_struct *__work)
  4334. {
  4335. struct intel_unpin_work *work =
  4336. container_of(__work, struct intel_unpin_work, work);
  4337. mutex_lock(&work->dev->struct_mutex);
  4338. i915_gem_object_unpin(work->old_fb_obj);
  4339. drm_gem_object_unreference(&work->pending_flip_obj->base);
  4340. drm_gem_object_unreference(&work->old_fb_obj->base);
  4341. mutex_unlock(&work->dev->struct_mutex);
  4342. kfree(work);
  4343. }
  4344. static void do_intel_finish_page_flip(struct drm_device *dev,
  4345. struct drm_crtc *crtc)
  4346. {
  4347. drm_i915_private_t *dev_priv = dev->dev_private;
  4348. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4349. struct intel_unpin_work *work;
  4350. struct drm_i915_gem_object *obj;
  4351. struct drm_pending_vblank_event *e;
  4352. struct timeval now;
  4353. unsigned long flags;
  4354. /* Ignore early vblank irqs */
  4355. if (intel_crtc == NULL)
  4356. return;
  4357. spin_lock_irqsave(&dev->event_lock, flags);
  4358. work = intel_crtc->unpin_work;
  4359. if (work == NULL || !work->pending) {
  4360. spin_unlock_irqrestore(&dev->event_lock, flags);
  4361. return;
  4362. }
  4363. intel_crtc->unpin_work = NULL;
  4364. drm_vblank_put(dev, intel_crtc->pipe);
  4365. if (work->event) {
  4366. e = work->event;
  4367. do_gettimeofday(&now);
  4368. e->event.sequence = drm_vblank_count(dev, intel_crtc->pipe);
  4369. e->event.tv_sec = now.tv_sec;
  4370. e->event.tv_usec = now.tv_usec;
  4371. list_add_tail(&e->base.link,
  4372. &e->base.file_priv->event_list);
  4373. wake_up_interruptible(&e->base.file_priv->event_wait);
  4374. }
  4375. spin_unlock_irqrestore(&dev->event_lock, flags);
  4376. obj = work->old_fb_obj;
  4377. atomic_clear_mask(1 << intel_crtc->plane,
  4378. &obj->pending_flip.counter);
  4379. if (atomic_read(&obj->pending_flip) == 0)
  4380. wake_up(&dev_priv->pending_flip_queue);
  4381. schedule_work(&work->work);
  4382. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  4383. }
  4384. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  4385. {
  4386. drm_i915_private_t *dev_priv = dev->dev_private;
  4387. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  4388. do_intel_finish_page_flip(dev, crtc);
  4389. }
  4390. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  4391. {
  4392. drm_i915_private_t *dev_priv = dev->dev_private;
  4393. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  4394. do_intel_finish_page_flip(dev, crtc);
  4395. }
  4396. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  4397. {
  4398. drm_i915_private_t *dev_priv = dev->dev_private;
  4399. struct intel_crtc *intel_crtc =
  4400. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  4401. unsigned long flags;
  4402. spin_lock_irqsave(&dev->event_lock, flags);
  4403. if (intel_crtc->unpin_work) {
  4404. if ((++intel_crtc->unpin_work->pending) > 1)
  4405. DRM_ERROR("Prepared flip multiple times\n");
  4406. } else {
  4407. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  4408. }
  4409. spin_unlock_irqrestore(&dev->event_lock, flags);
  4410. }
  4411. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  4412. struct drm_framebuffer *fb,
  4413. struct drm_pending_vblank_event *event)
  4414. {
  4415. struct drm_device *dev = crtc->dev;
  4416. struct drm_i915_private *dev_priv = dev->dev_private;
  4417. struct intel_framebuffer *intel_fb;
  4418. struct drm_i915_gem_object *obj;
  4419. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4420. struct intel_unpin_work *work;
  4421. unsigned long flags, offset;
  4422. int pipe = intel_crtc->pipe;
  4423. u32 pf, pipesrc;
  4424. int ret;
  4425. work = kzalloc(sizeof *work, GFP_KERNEL);
  4426. if (work == NULL)
  4427. return -ENOMEM;
  4428. work->event = event;
  4429. work->dev = crtc->dev;
  4430. intel_fb = to_intel_framebuffer(crtc->fb);
  4431. work->old_fb_obj = intel_fb->obj;
  4432. INIT_WORK(&work->work, intel_unpin_work_fn);
  4433. /* We borrow the event spin lock for protecting unpin_work */
  4434. spin_lock_irqsave(&dev->event_lock, flags);
  4435. if (intel_crtc->unpin_work) {
  4436. spin_unlock_irqrestore(&dev->event_lock, flags);
  4437. kfree(work);
  4438. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  4439. return -EBUSY;
  4440. }
  4441. intel_crtc->unpin_work = work;
  4442. spin_unlock_irqrestore(&dev->event_lock, flags);
  4443. intel_fb = to_intel_framebuffer(fb);
  4444. obj = intel_fb->obj;
  4445. mutex_lock(&dev->struct_mutex);
  4446. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  4447. if (ret)
  4448. goto cleanup_work;
  4449. /* Reference the objects for the scheduled work. */
  4450. drm_gem_object_reference(&work->old_fb_obj->base);
  4451. drm_gem_object_reference(&obj->base);
  4452. crtc->fb = fb;
  4453. ret = drm_vblank_get(dev, intel_crtc->pipe);
  4454. if (ret)
  4455. goto cleanup_objs;
  4456. if (IS_GEN3(dev) || IS_GEN2(dev)) {
  4457. u32 flip_mask;
  4458. /* Can't queue multiple flips, so wait for the previous
  4459. * one to finish before executing the next.
  4460. */
  4461. ret = BEGIN_LP_RING(2);
  4462. if (ret)
  4463. goto cleanup_objs;
  4464. if (intel_crtc->plane)
  4465. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  4466. else
  4467. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  4468. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  4469. OUT_RING(MI_NOOP);
  4470. ADVANCE_LP_RING();
  4471. }
  4472. work->pending_flip_obj = obj;
  4473. work->enable_stall_check = true;
  4474. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  4475. offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
  4476. ret = BEGIN_LP_RING(4);
  4477. if (ret)
  4478. goto cleanup_objs;
  4479. /* Block clients from rendering to the new back buffer until
  4480. * the flip occurs and the object is no longer visible.
  4481. */
  4482. atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  4483. switch (INTEL_INFO(dev)->gen) {
  4484. case 2:
  4485. OUT_RING(MI_DISPLAY_FLIP |
  4486. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4487. OUT_RING(fb->pitch);
  4488. OUT_RING(obj->gtt_offset + offset);
  4489. OUT_RING(MI_NOOP);
  4490. break;
  4491. case 3:
  4492. OUT_RING(MI_DISPLAY_FLIP_I915 |
  4493. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4494. OUT_RING(fb->pitch);
  4495. OUT_RING(obj->gtt_offset + offset);
  4496. OUT_RING(MI_NOOP);
  4497. break;
  4498. case 4:
  4499. case 5:
  4500. /* i965+ uses the linear or tiled offsets from the
  4501. * Display Registers (which do not change across a page-flip)
  4502. * so we need only reprogram the base address.
  4503. */
  4504. OUT_RING(MI_DISPLAY_FLIP |
  4505. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4506. OUT_RING(fb->pitch);
  4507. OUT_RING(obj->gtt_offset | obj->tiling_mode);
  4508. /* XXX Enabling the panel-fitter across page-flip is so far
  4509. * untested on non-native modes, so ignore it for now.
  4510. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  4511. */
  4512. pf = 0;
  4513. pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
  4514. OUT_RING(pf | pipesrc);
  4515. break;
  4516. case 6:
  4517. OUT_RING(MI_DISPLAY_FLIP |
  4518. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4519. OUT_RING(fb->pitch | obj->tiling_mode);
  4520. OUT_RING(obj->gtt_offset);
  4521. pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  4522. pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
  4523. OUT_RING(pf | pipesrc);
  4524. break;
  4525. }
  4526. ADVANCE_LP_RING();
  4527. mutex_unlock(&dev->struct_mutex);
  4528. trace_i915_flip_request(intel_crtc->plane, obj);
  4529. return 0;
  4530. cleanup_objs:
  4531. drm_gem_object_unreference(&work->old_fb_obj->base);
  4532. drm_gem_object_unreference(&obj->base);
  4533. cleanup_work:
  4534. mutex_unlock(&dev->struct_mutex);
  4535. spin_lock_irqsave(&dev->event_lock, flags);
  4536. intel_crtc->unpin_work = NULL;
  4537. spin_unlock_irqrestore(&dev->event_lock, flags);
  4538. kfree(work);
  4539. return ret;
  4540. }
  4541. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  4542. .dpms = intel_crtc_dpms,
  4543. .mode_fixup = intel_crtc_mode_fixup,
  4544. .mode_set = intel_crtc_mode_set,
  4545. .mode_set_base = intel_pipe_set_base,
  4546. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  4547. .load_lut = intel_crtc_load_lut,
  4548. .disable = intel_crtc_disable,
  4549. };
  4550. static const struct drm_crtc_funcs intel_crtc_funcs = {
  4551. .cursor_set = intel_crtc_cursor_set,
  4552. .cursor_move = intel_crtc_cursor_move,
  4553. .gamma_set = intel_crtc_gamma_set,
  4554. .set_config = drm_crtc_helper_set_config,
  4555. .destroy = intel_crtc_destroy,
  4556. .page_flip = intel_crtc_page_flip,
  4557. };
  4558. static void intel_sanitize_modesetting(struct drm_device *dev,
  4559. int pipe, int plane)
  4560. {
  4561. struct drm_i915_private *dev_priv = dev->dev_private;
  4562. u32 reg, val;
  4563. if (HAS_PCH_SPLIT(dev))
  4564. return;
  4565. /* Who knows what state these registers were left in by the BIOS or
  4566. * grub?
  4567. *
  4568. * If we leave the registers in a conflicting state (e.g. with the
  4569. * display plane reading from the other pipe than the one we intend
  4570. * to use) then when we attempt to teardown the active mode, we will
  4571. * not disable the pipes and planes in the correct order -- leaving
  4572. * a plane reading from a disabled pipe and possibly leading to
  4573. * undefined behaviour.
  4574. */
  4575. reg = DSPCNTR(plane);
  4576. val = I915_READ(reg);
  4577. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  4578. return;
  4579. if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
  4580. return;
  4581. /* This display plane is active and attached to the other CPU pipe. */
  4582. pipe = !pipe;
  4583. /* Disable the plane and wait for it to stop reading from the pipe. */
  4584. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  4585. intel_flush_display_plane(dev, plane);
  4586. if (IS_GEN2(dev))
  4587. intel_wait_for_vblank(dev, pipe);
  4588. if (pipe == 0 && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  4589. return;
  4590. /* Switch off the pipe. */
  4591. reg = PIPECONF(pipe);
  4592. val = I915_READ(reg);
  4593. if (val & PIPECONF_ENABLE) {
  4594. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  4595. intel_wait_for_pipe_off(dev, pipe);
  4596. }
  4597. }
  4598. static void intel_crtc_init(struct drm_device *dev, int pipe)
  4599. {
  4600. drm_i915_private_t *dev_priv = dev->dev_private;
  4601. struct intel_crtc *intel_crtc;
  4602. int i;
  4603. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  4604. if (intel_crtc == NULL)
  4605. return;
  4606. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  4607. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  4608. for (i = 0; i < 256; i++) {
  4609. intel_crtc->lut_r[i] = i;
  4610. intel_crtc->lut_g[i] = i;
  4611. intel_crtc->lut_b[i] = i;
  4612. }
  4613. /* Swap pipes & planes for FBC on pre-965 */
  4614. intel_crtc->pipe = pipe;
  4615. intel_crtc->plane = pipe;
  4616. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  4617. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  4618. intel_crtc->plane = !pipe;
  4619. }
  4620. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  4621. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  4622. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  4623. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  4624. intel_crtc->cursor_addr = 0;
  4625. intel_crtc->dpms_mode = -1;
  4626. intel_crtc->active = true; /* force the pipe off on setup_init_config */
  4627. if (HAS_PCH_SPLIT(dev)) {
  4628. intel_helper_funcs.prepare = ironlake_crtc_prepare;
  4629. intel_helper_funcs.commit = ironlake_crtc_commit;
  4630. } else {
  4631. intel_helper_funcs.prepare = i9xx_crtc_prepare;
  4632. intel_helper_funcs.commit = i9xx_crtc_commit;
  4633. }
  4634. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  4635. intel_crtc->busy = false;
  4636. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  4637. (unsigned long)intel_crtc);
  4638. intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
  4639. }
  4640. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  4641. struct drm_file *file)
  4642. {
  4643. drm_i915_private_t *dev_priv = dev->dev_private;
  4644. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  4645. struct drm_mode_object *drmmode_obj;
  4646. struct intel_crtc *crtc;
  4647. if (!dev_priv) {
  4648. DRM_ERROR("called with no initialization\n");
  4649. return -EINVAL;
  4650. }
  4651. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  4652. DRM_MODE_OBJECT_CRTC);
  4653. if (!drmmode_obj) {
  4654. DRM_ERROR("no such CRTC id\n");
  4655. return -EINVAL;
  4656. }
  4657. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  4658. pipe_from_crtc_id->pipe = crtc->pipe;
  4659. return 0;
  4660. }
  4661. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  4662. {
  4663. struct intel_encoder *encoder;
  4664. int index_mask = 0;
  4665. int entry = 0;
  4666. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  4667. if (type_mask & encoder->clone_mask)
  4668. index_mask |= (1 << entry);
  4669. entry++;
  4670. }
  4671. return index_mask;
  4672. }
  4673. static void intel_setup_outputs(struct drm_device *dev)
  4674. {
  4675. struct drm_i915_private *dev_priv = dev->dev_private;
  4676. struct intel_encoder *encoder;
  4677. bool dpd_is_edp = false;
  4678. bool has_lvds = false;
  4679. if (IS_MOBILE(dev) && !IS_I830(dev))
  4680. has_lvds = intel_lvds_init(dev);
  4681. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  4682. /* disable the panel fitter on everything but LVDS */
  4683. I915_WRITE(PFIT_CONTROL, 0);
  4684. }
  4685. if (HAS_PCH_SPLIT(dev)) {
  4686. dpd_is_edp = intel_dpd_is_edp(dev);
  4687. if (IS_MOBILE(dev) && (I915_READ(DP_A) & DP_DETECTED))
  4688. intel_dp_init(dev, DP_A);
  4689. if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  4690. intel_dp_init(dev, PCH_DP_D);
  4691. }
  4692. intel_crt_init(dev);
  4693. if (HAS_PCH_SPLIT(dev)) {
  4694. int found;
  4695. if (I915_READ(HDMIB) & PORT_DETECTED) {
  4696. /* PCH SDVOB multiplex with HDMIB */
  4697. found = intel_sdvo_init(dev, PCH_SDVOB);
  4698. if (!found)
  4699. intel_hdmi_init(dev, HDMIB);
  4700. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  4701. intel_dp_init(dev, PCH_DP_B);
  4702. }
  4703. if (I915_READ(HDMIC) & PORT_DETECTED)
  4704. intel_hdmi_init(dev, HDMIC);
  4705. if (I915_READ(HDMID) & PORT_DETECTED)
  4706. intel_hdmi_init(dev, HDMID);
  4707. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  4708. intel_dp_init(dev, PCH_DP_C);
  4709. if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  4710. intel_dp_init(dev, PCH_DP_D);
  4711. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  4712. bool found = false;
  4713. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  4714. DRM_DEBUG_KMS("probing SDVOB\n");
  4715. found = intel_sdvo_init(dev, SDVOB);
  4716. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  4717. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  4718. intel_hdmi_init(dev, SDVOB);
  4719. }
  4720. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  4721. DRM_DEBUG_KMS("probing DP_B\n");
  4722. intel_dp_init(dev, DP_B);
  4723. }
  4724. }
  4725. /* Before G4X SDVOC doesn't have its own detect register */
  4726. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  4727. DRM_DEBUG_KMS("probing SDVOC\n");
  4728. found = intel_sdvo_init(dev, SDVOC);
  4729. }
  4730. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  4731. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  4732. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  4733. intel_hdmi_init(dev, SDVOC);
  4734. }
  4735. if (SUPPORTS_INTEGRATED_DP(dev)) {
  4736. DRM_DEBUG_KMS("probing DP_C\n");
  4737. intel_dp_init(dev, DP_C);
  4738. }
  4739. }
  4740. if (SUPPORTS_INTEGRATED_DP(dev) &&
  4741. (I915_READ(DP_D) & DP_DETECTED)) {
  4742. DRM_DEBUG_KMS("probing DP_D\n");
  4743. intel_dp_init(dev, DP_D);
  4744. }
  4745. } else if (IS_GEN2(dev))
  4746. intel_dvo_init(dev);
  4747. if (SUPPORTS_TV(dev))
  4748. intel_tv_init(dev);
  4749. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  4750. encoder->base.possible_crtcs = encoder->crtc_mask;
  4751. encoder->base.possible_clones =
  4752. intel_encoder_clones(dev, encoder->clone_mask);
  4753. }
  4754. }
  4755. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  4756. {
  4757. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  4758. drm_framebuffer_cleanup(fb);
  4759. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  4760. kfree(intel_fb);
  4761. }
  4762. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  4763. struct drm_file *file,
  4764. unsigned int *handle)
  4765. {
  4766. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  4767. struct drm_i915_gem_object *obj = intel_fb->obj;
  4768. return drm_gem_handle_create(file, &obj->base, handle);
  4769. }
  4770. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  4771. .destroy = intel_user_framebuffer_destroy,
  4772. .create_handle = intel_user_framebuffer_create_handle,
  4773. };
  4774. int intel_framebuffer_init(struct drm_device *dev,
  4775. struct intel_framebuffer *intel_fb,
  4776. struct drm_mode_fb_cmd *mode_cmd,
  4777. struct drm_i915_gem_object *obj)
  4778. {
  4779. int ret;
  4780. if (obj->tiling_mode == I915_TILING_Y)
  4781. return -EINVAL;
  4782. if (mode_cmd->pitch & 63)
  4783. return -EINVAL;
  4784. switch (mode_cmd->bpp) {
  4785. case 8:
  4786. case 16:
  4787. case 24:
  4788. case 32:
  4789. break;
  4790. default:
  4791. return -EINVAL;
  4792. }
  4793. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  4794. if (ret) {
  4795. DRM_ERROR("framebuffer init failed %d\n", ret);
  4796. return ret;
  4797. }
  4798. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  4799. intel_fb->obj = obj;
  4800. return 0;
  4801. }
  4802. static struct drm_framebuffer *
  4803. intel_user_framebuffer_create(struct drm_device *dev,
  4804. struct drm_file *filp,
  4805. struct drm_mode_fb_cmd *mode_cmd)
  4806. {
  4807. struct drm_i915_gem_object *obj;
  4808. struct intel_framebuffer *intel_fb;
  4809. int ret;
  4810. obj = to_intel_bo(drm_gem_object_lookup(dev, filp, mode_cmd->handle));
  4811. if (!obj)
  4812. return ERR_PTR(-ENOENT);
  4813. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  4814. if (!intel_fb)
  4815. return ERR_PTR(-ENOMEM);
  4816. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  4817. if (ret) {
  4818. drm_gem_object_unreference_unlocked(&obj->base);
  4819. kfree(intel_fb);
  4820. return ERR_PTR(ret);
  4821. }
  4822. return &intel_fb->base;
  4823. }
  4824. static const struct drm_mode_config_funcs intel_mode_funcs = {
  4825. .fb_create = intel_user_framebuffer_create,
  4826. .output_poll_changed = intel_fb_output_poll_changed,
  4827. };
  4828. static struct drm_i915_gem_object *
  4829. intel_alloc_context_page(struct drm_device *dev)
  4830. {
  4831. struct drm_i915_gem_object *ctx;
  4832. int ret;
  4833. ctx = i915_gem_alloc_object(dev, 4096);
  4834. if (!ctx) {
  4835. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  4836. return NULL;
  4837. }
  4838. mutex_lock(&dev->struct_mutex);
  4839. ret = i915_gem_object_pin(ctx, 4096, true);
  4840. if (ret) {
  4841. DRM_ERROR("failed to pin power context: %d\n", ret);
  4842. goto err_unref;
  4843. }
  4844. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  4845. if (ret) {
  4846. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  4847. goto err_unpin;
  4848. }
  4849. mutex_unlock(&dev->struct_mutex);
  4850. return ctx;
  4851. err_unpin:
  4852. i915_gem_object_unpin(ctx);
  4853. err_unref:
  4854. drm_gem_object_unreference(&ctx->base);
  4855. mutex_unlock(&dev->struct_mutex);
  4856. return NULL;
  4857. }
  4858. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  4859. {
  4860. struct drm_i915_private *dev_priv = dev->dev_private;
  4861. u16 rgvswctl;
  4862. rgvswctl = I915_READ16(MEMSWCTL);
  4863. if (rgvswctl & MEMCTL_CMD_STS) {
  4864. DRM_DEBUG("gpu busy, RCS change rejected\n");
  4865. return false; /* still busy with another command */
  4866. }
  4867. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  4868. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  4869. I915_WRITE16(MEMSWCTL, rgvswctl);
  4870. POSTING_READ16(MEMSWCTL);
  4871. rgvswctl |= MEMCTL_CMD_STS;
  4872. I915_WRITE16(MEMSWCTL, rgvswctl);
  4873. return true;
  4874. }
  4875. void ironlake_enable_drps(struct drm_device *dev)
  4876. {
  4877. struct drm_i915_private *dev_priv = dev->dev_private;
  4878. u32 rgvmodectl = I915_READ(MEMMODECTL);
  4879. u8 fmax, fmin, fstart, vstart;
  4880. /* Enable temp reporting */
  4881. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  4882. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  4883. /* 100ms RC evaluation intervals */
  4884. I915_WRITE(RCUPEI, 100000);
  4885. I915_WRITE(RCDNEI, 100000);
  4886. /* Set max/min thresholds to 90ms and 80ms respectively */
  4887. I915_WRITE(RCBMAXAVG, 90000);
  4888. I915_WRITE(RCBMINAVG, 80000);
  4889. I915_WRITE(MEMIHYST, 1);
  4890. /* Set up min, max, and cur for interrupt handling */
  4891. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  4892. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  4893. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  4894. MEMMODE_FSTART_SHIFT;
  4895. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  4896. PXVFREQ_PX_SHIFT;
  4897. dev_priv->fmax = fmax; /* IPS callback will increase this */
  4898. dev_priv->fstart = fstart;
  4899. dev_priv->max_delay = fstart;
  4900. dev_priv->min_delay = fmin;
  4901. dev_priv->cur_delay = fstart;
  4902. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  4903. fmax, fmin, fstart);
  4904. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  4905. /*
  4906. * Interrupts will be enabled in ironlake_irq_postinstall
  4907. */
  4908. I915_WRITE(VIDSTART, vstart);
  4909. POSTING_READ(VIDSTART);
  4910. rgvmodectl |= MEMMODE_SWMODE_EN;
  4911. I915_WRITE(MEMMODECTL, rgvmodectl);
  4912. if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  4913. DRM_ERROR("stuck trying to change perf mode\n");
  4914. msleep(1);
  4915. ironlake_set_drps(dev, fstart);
  4916. dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  4917. I915_READ(0x112e0);
  4918. dev_priv->last_time1 = jiffies_to_msecs(jiffies);
  4919. dev_priv->last_count2 = I915_READ(0x112f4);
  4920. getrawmonotonic(&dev_priv->last_time2);
  4921. }
  4922. void ironlake_disable_drps(struct drm_device *dev)
  4923. {
  4924. struct drm_i915_private *dev_priv = dev->dev_private;
  4925. u16 rgvswctl = I915_READ16(MEMSWCTL);
  4926. /* Ack interrupts, disable EFC interrupt */
  4927. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  4928. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  4929. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  4930. I915_WRITE(DEIIR, DE_PCU_EVENT);
  4931. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  4932. /* Go back to the starting frequency */
  4933. ironlake_set_drps(dev, dev_priv->fstart);
  4934. msleep(1);
  4935. rgvswctl |= MEMCTL_CMD_STS;
  4936. I915_WRITE(MEMSWCTL, rgvswctl);
  4937. msleep(1);
  4938. }
  4939. static unsigned long intel_pxfreq(u32 vidfreq)
  4940. {
  4941. unsigned long freq;
  4942. int div = (vidfreq & 0x3f0000) >> 16;
  4943. int post = (vidfreq & 0x3000) >> 12;
  4944. int pre = (vidfreq & 0x7);
  4945. if (!pre)
  4946. return 0;
  4947. freq = ((div * 133333) / ((1<<post) * pre));
  4948. return freq;
  4949. }
  4950. void intel_init_emon(struct drm_device *dev)
  4951. {
  4952. struct drm_i915_private *dev_priv = dev->dev_private;
  4953. u32 lcfuse;
  4954. u8 pxw[16];
  4955. int i;
  4956. /* Disable to program */
  4957. I915_WRITE(ECR, 0);
  4958. POSTING_READ(ECR);
  4959. /* Program energy weights for various events */
  4960. I915_WRITE(SDEW, 0x15040d00);
  4961. I915_WRITE(CSIEW0, 0x007f0000);
  4962. I915_WRITE(CSIEW1, 0x1e220004);
  4963. I915_WRITE(CSIEW2, 0x04000004);
  4964. for (i = 0; i < 5; i++)
  4965. I915_WRITE(PEW + (i * 4), 0);
  4966. for (i = 0; i < 3; i++)
  4967. I915_WRITE(DEW + (i * 4), 0);
  4968. /* Program P-state weights to account for frequency power adjustment */
  4969. for (i = 0; i < 16; i++) {
  4970. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  4971. unsigned long freq = intel_pxfreq(pxvidfreq);
  4972. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  4973. PXVFREQ_PX_SHIFT;
  4974. unsigned long val;
  4975. val = vid * vid;
  4976. val *= (freq / 1000);
  4977. val *= 255;
  4978. val /= (127*127*900);
  4979. if (val > 0xff)
  4980. DRM_ERROR("bad pxval: %ld\n", val);
  4981. pxw[i] = val;
  4982. }
  4983. /* Render standby states get 0 weight */
  4984. pxw[14] = 0;
  4985. pxw[15] = 0;
  4986. for (i = 0; i < 4; i++) {
  4987. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  4988. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  4989. I915_WRITE(PXW + (i * 4), val);
  4990. }
  4991. /* Adjust magic regs to magic values (more experimental results) */
  4992. I915_WRITE(OGW0, 0);
  4993. I915_WRITE(OGW1, 0);
  4994. I915_WRITE(EG0, 0x00007f00);
  4995. I915_WRITE(EG1, 0x0000000e);
  4996. I915_WRITE(EG2, 0x000e0000);
  4997. I915_WRITE(EG3, 0x68000300);
  4998. I915_WRITE(EG4, 0x42000000);
  4999. I915_WRITE(EG5, 0x00140031);
  5000. I915_WRITE(EG6, 0);
  5001. I915_WRITE(EG7, 0);
  5002. for (i = 0; i < 8; i++)
  5003. I915_WRITE(PXWL + (i * 4), 0);
  5004. /* Enable PMON + select events */
  5005. I915_WRITE(ECR, 0x80000019);
  5006. lcfuse = I915_READ(LCFUSE02);
  5007. dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
  5008. }
  5009. void intel_enable_clock_gating(struct drm_device *dev)
  5010. {
  5011. struct drm_i915_private *dev_priv = dev->dev_private;
  5012. /*
  5013. * Disable clock gating reported to work incorrectly according to the
  5014. * specs, but enable as much else as we can.
  5015. */
  5016. if (HAS_PCH_SPLIT(dev)) {
  5017. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  5018. if (IS_GEN5(dev)) {
  5019. /* Required for FBC */
  5020. dspclk_gate |= DPFDUNIT_CLOCK_GATE_DISABLE;
  5021. /* Required for CxSR */
  5022. dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
  5023. I915_WRITE(PCH_3DCGDIS0,
  5024. MARIUNIT_CLOCK_GATE_DISABLE |
  5025. SVSMUNIT_CLOCK_GATE_DISABLE);
  5026. }
  5027. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  5028. /*
  5029. * On Ibex Peak and Cougar Point, we need to disable clock
  5030. * gating for the panel power sequencer or it will fail to
  5031. * start up when no ports are active.
  5032. */
  5033. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  5034. /*
  5035. * According to the spec the following bits should be set in
  5036. * order to enable memory self-refresh
  5037. * The bit 22/21 of 0x42004
  5038. * The bit 5 of 0x42020
  5039. * The bit 15 of 0x45000
  5040. */
  5041. if (IS_GEN5(dev)) {
  5042. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5043. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  5044. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  5045. I915_WRITE(ILK_DSPCLK_GATE,
  5046. (I915_READ(ILK_DSPCLK_GATE) |
  5047. ILK_DPARB_CLK_GATE));
  5048. I915_WRITE(DISP_ARB_CTL,
  5049. (I915_READ(DISP_ARB_CTL) |
  5050. DISP_FBC_WM_DIS));
  5051. I915_WRITE(WM3_LP_ILK, 0);
  5052. I915_WRITE(WM2_LP_ILK, 0);
  5053. I915_WRITE(WM1_LP_ILK, 0);
  5054. }
  5055. /*
  5056. * Based on the document from hardware guys the following bits
  5057. * should be set unconditionally in order to enable FBC.
  5058. * The bit 22 of 0x42000
  5059. * The bit 22 of 0x42004
  5060. * The bit 7,8,9 of 0x42020.
  5061. */
  5062. if (IS_IRONLAKE_M(dev)) {
  5063. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  5064. I915_READ(ILK_DISPLAY_CHICKEN1) |
  5065. ILK_FBCQ_DIS);
  5066. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5067. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5068. ILK_DPARB_GATE);
  5069. I915_WRITE(ILK_DSPCLK_GATE,
  5070. I915_READ(ILK_DSPCLK_GATE) |
  5071. ILK_DPFC_DIS1 |
  5072. ILK_DPFC_DIS2 |
  5073. ILK_CLK_FBC);
  5074. }
  5075. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5076. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5077. ILK_ELPIN_409_SELECT);
  5078. if (IS_GEN5(dev)) {
  5079. I915_WRITE(_3D_CHICKEN2,
  5080. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  5081. _3D_CHICKEN2_WM_READ_PIPELINED);
  5082. }
  5083. } else if (IS_G4X(dev)) {
  5084. uint32_t dspclk_gate;
  5085. I915_WRITE(RENCLK_GATE_D1, 0);
  5086. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  5087. GS_UNIT_CLOCK_GATE_DISABLE |
  5088. CL_UNIT_CLOCK_GATE_DISABLE);
  5089. I915_WRITE(RAMCLK_GATE_D, 0);
  5090. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  5091. OVRUNIT_CLOCK_GATE_DISABLE |
  5092. OVCUNIT_CLOCK_GATE_DISABLE;
  5093. if (IS_GM45(dev))
  5094. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  5095. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  5096. } else if (IS_CRESTLINE(dev)) {
  5097. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  5098. I915_WRITE(RENCLK_GATE_D2, 0);
  5099. I915_WRITE(DSPCLK_GATE_D, 0);
  5100. I915_WRITE(RAMCLK_GATE_D, 0);
  5101. I915_WRITE16(DEUC, 0);
  5102. } else if (IS_BROADWATER(dev)) {
  5103. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  5104. I965_RCC_CLOCK_GATE_DISABLE |
  5105. I965_RCPB_CLOCK_GATE_DISABLE |
  5106. I965_ISC_CLOCK_GATE_DISABLE |
  5107. I965_FBC_CLOCK_GATE_DISABLE);
  5108. I915_WRITE(RENCLK_GATE_D2, 0);
  5109. } else if (IS_GEN3(dev)) {
  5110. u32 dstate = I915_READ(D_STATE);
  5111. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  5112. DSTATE_DOT_CLOCK_GATING;
  5113. I915_WRITE(D_STATE, dstate);
  5114. } else if (IS_I85X(dev) || IS_I865G(dev)) {
  5115. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  5116. } else if (IS_I830(dev)) {
  5117. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  5118. }
  5119. /*
  5120. * GPU can automatically power down the render unit if given a page
  5121. * to save state.
  5122. */
  5123. if (IS_IRONLAKE_M(dev) && 0) { /* XXX causes a failure during suspend */
  5124. if (dev_priv->renderctx == NULL)
  5125. dev_priv->renderctx = intel_alloc_context_page(dev);
  5126. if (dev_priv->renderctx) {
  5127. struct drm_i915_gem_object *obj = dev_priv->renderctx;
  5128. if (BEGIN_LP_RING(4) == 0) {
  5129. OUT_RING(MI_SET_CONTEXT);
  5130. OUT_RING(obj->gtt_offset |
  5131. MI_MM_SPACE_GTT |
  5132. MI_SAVE_EXT_STATE_EN |
  5133. MI_RESTORE_EXT_STATE_EN |
  5134. MI_RESTORE_INHIBIT);
  5135. OUT_RING(MI_NOOP);
  5136. OUT_RING(MI_FLUSH);
  5137. ADVANCE_LP_RING();
  5138. }
  5139. } else
  5140. DRM_DEBUG_KMS("Failed to allocate render context."
  5141. "Disable RC6\n");
  5142. }
  5143. if (IS_GEN4(dev) && IS_MOBILE(dev)) {
  5144. if (dev_priv->pwrctx == NULL)
  5145. dev_priv->pwrctx = intel_alloc_context_page(dev);
  5146. if (dev_priv->pwrctx) {
  5147. struct drm_i915_gem_object *obj = dev_priv->pwrctx;
  5148. I915_WRITE(PWRCTXA, obj->gtt_offset | PWRCTX_EN);
  5149. I915_WRITE(MCHBAR_RENDER_STANDBY,
  5150. I915_READ(MCHBAR_RENDER_STANDBY) & ~RCX_SW_EXIT);
  5151. }
  5152. }
  5153. }
  5154. void intel_disable_clock_gating(struct drm_device *dev)
  5155. {
  5156. struct drm_i915_private *dev_priv = dev->dev_private;
  5157. if (dev_priv->renderctx) {
  5158. struct drm_i915_gem_object *obj = dev_priv->renderctx;
  5159. I915_WRITE(CCID, 0);
  5160. POSTING_READ(CCID);
  5161. i915_gem_object_unpin(obj);
  5162. drm_gem_object_unreference(&obj->base);
  5163. dev_priv->renderctx = NULL;
  5164. }
  5165. if (dev_priv->pwrctx) {
  5166. struct drm_i915_gem_object *obj = dev_priv->pwrctx;
  5167. I915_WRITE(PWRCTXA, 0);
  5168. POSTING_READ(PWRCTXA);
  5169. i915_gem_object_unpin(obj);
  5170. drm_gem_object_unreference(&obj->base);
  5171. dev_priv->pwrctx = NULL;
  5172. }
  5173. }
  5174. /* Set up chip specific display functions */
  5175. static void intel_init_display(struct drm_device *dev)
  5176. {
  5177. struct drm_i915_private *dev_priv = dev->dev_private;
  5178. /* We always want a DPMS function */
  5179. if (HAS_PCH_SPLIT(dev))
  5180. dev_priv->display.dpms = ironlake_crtc_dpms;
  5181. else
  5182. dev_priv->display.dpms = i9xx_crtc_dpms;
  5183. if (I915_HAS_FBC(dev)) {
  5184. if (IS_IRONLAKE_M(dev)) {
  5185. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  5186. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  5187. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  5188. } else if (IS_GM45(dev)) {
  5189. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  5190. dev_priv->display.enable_fbc = g4x_enable_fbc;
  5191. dev_priv->display.disable_fbc = g4x_disable_fbc;
  5192. } else if (IS_CRESTLINE(dev)) {
  5193. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  5194. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  5195. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  5196. }
  5197. /* 855GM needs testing */
  5198. }
  5199. /* Returns the core display clock speed */
  5200. if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
  5201. dev_priv->display.get_display_clock_speed =
  5202. i945_get_display_clock_speed;
  5203. else if (IS_I915G(dev))
  5204. dev_priv->display.get_display_clock_speed =
  5205. i915_get_display_clock_speed;
  5206. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  5207. dev_priv->display.get_display_clock_speed =
  5208. i9xx_misc_get_display_clock_speed;
  5209. else if (IS_I915GM(dev))
  5210. dev_priv->display.get_display_clock_speed =
  5211. i915gm_get_display_clock_speed;
  5212. else if (IS_I865G(dev))
  5213. dev_priv->display.get_display_clock_speed =
  5214. i865_get_display_clock_speed;
  5215. else if (IS_I85X(dev))
  5216. dev_priv->display.get_display_clock_speed =
  5217. i855_get_display_clock_speed;
  5218. else /* 852, 830 */
  5219. dev_priv->display.get_display_clock_speed =
  5220. i830_get_display_clock_speed;
  5221. /* For FIFO watermark updates */
  5222. if (HAS_PCH_SPLIT(dev)) {
  5223. if (IS_GEN5(dev)) {
  5224. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  5225. dev_priv->display.update_wm = ironlake_update_wm;
  5226. else {
  5227. DRM_DEBUG_KMS("Failed to get proper latency. "
  5228. "Disable CxSR\n");
  5229. dev_priv->display.update_wm = NULL;
  5230. }
  5231. } else
  5232. dev_priv->display.update_wm = NULL;
  5233. } else if (IS_PINEVIEW(dev)) {
  5234. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  5235. dev_priv->is_ddr3,
  5236. dev_priv->fsb_freq,
  5237. dev_priv->mem_freq)) {
  5238. DRM_INFO("failed to find known CxSR latency "
  5239. "(found ddr%s fsb freq %d, mem freq %d), "
  5240. "disabling CxSR\n",
  5241. (dev_priv->is_ddr3 == 1) ? "3": "2",
  5242. dev_priv->fsb_freq, dev_priv->mem_freq);
  5243. /* Disable CxSR and never update its watermark again */
  5244. pineview_disable_cxsr(dev);
  5245. dev_priv->display.update_wm = NULL;
  5246. } else
  5247. dev_priv->display.update_wm = pineview_update_wm;
  5248. } else if (IS_G4X(dev))
  5249. dev_priv->display.update_wm = g4x_update_wm;
  5250. else if (IS_GEN4(dev))
  5251. dev_priv->display.update_wm = i965_update_wm;
  5252. else if (IS_GEN3(dev)) {
  5253. dev_priv->display.update_wm = i9xx_update_wm;
  5254. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  5255. } else if (IS_I85X(dev)) {
  5256. dev_priv->display.update_wm = i9xx_update_wm;
  5257. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  5258. } else {
  5259. dev_priv->display.update_wm = i830_update_wm;
  5260. if (IS_845G(dev))
  5261. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  5262. else
  5263. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  5264. }
  5265. }
  5266. /*
  5267. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  5268. * resume, or other times. This quirk makes sure that's the case for
  5269. * affected systems.
  5270. */
  5271. static void quirk_pipea_force (struct drm_device *dev)
  5272. {
  5273. struct drm_i915_private *dev_priv = dev->dev_private;
  5274. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  5275. DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
  5276. }
  5277. struct intel_quirk {
  5278. int device;
  5279. int subsystem_vendor;
  5280. int subsystem_device;
  5281. void (*hook)(struct drm_device *dev);
  5282. };
  5283. struct intel_quirk intel_quirks[] = {
  5284. /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
  5285. { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
  5286. /* HP Mini needs pipe A force quirk (LP: #322104) */
  5287. { 0x27ae,0x103c, 0x361a, quirk_pipea_force },
  5288. /* Thinkpad R31 needs pipe A force quirk */
  5289. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  5290. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  5291. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  5292. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  5293. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  5294. /* ThinkPad X40 needs pipe A force quirk */
  5295. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  5296. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  5297. /* 855 & before need to leave pipe A & dpll A up */
  5298. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5299. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5300. };
  5301. static void intel_init_quirks(struct drm_device *dev)
  5302. {
  5303. struct pci_dev *d = dev->pdev;
  5304. int i;
  5305. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  5306. struct intel_quirk *q = &intel_quirks[i];
  5307. if (d->device == q->device &&
  5308. (d->subsystem_vendor == q->subsystem_vendor ||
  5309. q->subsystem_vendor == PCI_ANY_ID) &&
  5310. (d->subsystem_device == q->subsystem_device ||
  5311. q->subsystem_device == PCI_ANY_ID))
  5312. q->hook(dev);
  5313. }
  5314. }
  5315. /* Disable the VGA plane that we never use */
  5316. static void i915_disable_vga(struct drm_device *dev)
  5317. {
  5318. struct drm_i915_private *dev_priv = dev->dev_private;
  5319. u8 sr1;
  5320. u32 vga_reg;
  5321. if (HAS_PCH_SPLIT(dev))
  5322. vga_reg = CPU_VGACNTRL;
  5323. else
  5324. vga_reg = VGACNTRL;
  5325. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  5326. outb(1, VGA_SR_INDEX);
  5327. sr1 = inb(VGA_SR_DATA);
  5328. outb(sr1 | 1<<5, VGA_SR_DATA);
  5329. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  5330. udelay(300);
  5331. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  5332. POSTING_READ(vga_reg);
  5333. }
  5334. void intel_modeset_init(struct drm_device *dev)
  5335. {
  5336. struct drm_i915_private *dev_priv = dev->dev_private;
  5337. int i;
  5338. drm_mode_config_init(dev);
  5339. dev->mode_config.min_width = 0;
  5340. dev->mode_config.min_height = 0;
  5341. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  5342. intel_init_quirks(dev);
  5343. intel_init_display(dev);
  5344. if (IS_GEN2(dev)) {
  5345. dev->mode_config.max_width = 2048;
  5346. dev->mode_config.max_height = 2048;
  5347. } else if (IS_GEN3(dev)) {
  5348. dev->mode_config.max_width = 4096;
  5349. dev->mode_config.max_height = 4096;
  5350. } else {
  5351. dev->mode_config.max_width = 8192;
  5352. dev->mode_config.max_height = 8192;
  5353. }
  5354. /* set memory base */
  5355. if (IS_GEN2(dev))
  5356. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
  5357. else
  5358. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
  5359. if (IS_MOBILE(dev) || !IS_GEN2(dev))
  5360. dev_priv->num_pipe = 2;
  5361. else
  5362. dev_priv->num_pipe = 1;
  5363. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  5364. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  5365. for (i = 0; i < dev_priv->num_pipe; i++) {
  5366. intel_crtc_init(dev, i);
  5367. }
  5368. intel_setup_outputs(dev);
  5369. intel_enable_clock_gating(dev);
  5370. /* Just disable it once at startup */
  5371. i915_disable_vga(dev);
  5372. if (IS_IRONLAKE_M(dev)) {
  5373. ironlake_enable_drps(dev);
  5374. intel_init_emon(dev);
  5375. }
  5376. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  5377. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  5378. (unsigned long)dev);
  5379. intel_setup_overlay(dev);
  5380. }
  5381. void intel_modeset_cleanup(struct drm_device *dev)
  5382. {
  5383. struct drm_i915_private *dev_priv = dev->dev_private;
  5384. struct drm_crtc *crtc;
  5385. struct intel_crtc *intel_crtc;
  5386. drm_kms_helper_poll_fini(dev);
  5387. mutex_lock(&dev->struct_mutex);
  5388. intel_unregister_dsm_handler();
  5389. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5390. /* Skip inactive CRTCs */
  5391. if (!crtc->fb)
  5392. continue;
  5393. intel_crtc = to_intel_crtc(crtc);
  5394. intel_increase_pllclock(crtc);
  5395. }
  5396. if (dev_priv->display.disable_fbc)
  5397. dev_priv->display.disable_fbc(dev);
  5398. if (IS_IRONLAKE_M(dev))
  5399. ironlake_disable_drps(dev);
  5400. intel_disable_clock_gating(dev);
  5401. mutex_unlock(&dev->struct_mutex);
  5402. /* Disable the irq before mode object teardown, for the irq might
  5403. * enqueue unpin/hotplug work. */
  5404. drm_irq_uninstall(dev);
  5405. cancel_work_sync(&dev_priv->hotplug_work);
  5406. /* Shut off idle work before the crtcs get freed. */
  5407. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5408. intel_crtc = to_intel_crtc(crtc);
  5409. del_timer_sync(&intel_crtc->idle_timer);
  5410. }
  5411. del_timer_sync(&dev_priv->idle_timer);
  5412. cancel_work_sync(&dev_priv->idle_work);
  5413. drm_mode_config_cleanup(dev);
  5414. }
  5415. /*
  5416. * Return which encoder is currently attached for connector.
  5417. */
  5418. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  5419. {
  5420. return &intel_attached_encoder(connector)->base;
  5421. }
  5422. void intel_connector_attach_encoder(struct intel_connector *connector,
  5423. struct intel_encoder *encoder)
  5424. {
  5425. connector->encoder = encoder;
  5426. drm_mode_connector_attach_encoder(&connector->base,
  5427. &encoder->base);
  5428. }
  5429. /*
  5430. * set vga decode state - true == enable VGA decode
  5431. */
  5432. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  5433. {
  5434. struct drm_i915_private *dev_priv = dev->dev_private;
  5435. u16 gmch_ctrl;
  5436. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  5437. if (state)
  5438. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  5439. else
  5440. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  5441. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  5442. return 0;
  5443. }
  5444. #ifdef CONFIG_DEBUG_FS
  5445. #include <linux/seq_file.h>
  5446. struct intel_display_error_state {
  5447. struct intel_cursor_error_state {
  5448. u32 control;
  5449. u32 position;
  5450. u32 base;
  5451. u32 size;
  5452. } cursor[2];
  5453. struct intel_pipe_error_state {
  5454. u32 conf;
  5455. u32 source;
  5456. u32 htotal;
  5457. u32 hblank;
  5458. u32 hsync;
  5459. u32 vtotal;
  5460. u32 vblank;
  5461. u32 vsync;
  5462. } pipe[2];
  5463. struct intel_plane_error_state {
  5464. u32 control;
  5465. u32 stride;
  5466. u32 size;
  5467. u32 pos;
  5468. u32 addr;
  5469. u32 surface;
  5470. u32 tile_offset;
  5471. } plane[2];
  5472. };
  5473. struct intel_display_error_state *
  5474. intel_display_capture_error_state(struct drm_device *dev)
  5475. {
  5476. drm_i915_private_t *dev_priv = dev->dev_private;
  5477. struct intel_display_error_state *error;
  5478. int i;
  5479. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  5480. if (error == NULL)
  5481. return NULL;
  5482. for (i = 0; i < 2; i++) {
  5483. error->cursor[i].control = I915_READ(CURCNTR(i));
  5484. error->cursor[i].position = I915_READ(CURPOS(i));
  5485. error->cursor[i].base = I915_READ(CURBASE(i));
  5486. error->plane[i].control = I915_READ(DSPCNTR(i));
  5487. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  5488. error->plane[i].size = I915_READ(DSPSIZE(i));
  5489. error->plane[i].pos= I915_READ(DSPPOS(i));
  5490. error->plane[i].addr = I915_READ(DSPADDR(i));
  5491. if (INTEL_INFO(dev)->gen >= 4) {
  5492. error->plane[i].surface = I915_READ(DSPSURF(i));
  5493. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  5494. }
  5495. error->pipe[i].conf = I915_READ(PIPECONF(i));
  5496. error->pipe[i].source = I915_READ(PIPESRC(i));
  5497. error->pipe[i].htotal = I915_READ(HTOTAL(i));
  5498. error->pipe[i].hblank = I915_READ(HBLANK(i));
  5499. error->pipe[i].hsync = I915_READ(HSYNC(i));
  5500. error->pipe[i].vtotal = I915_READ(VTOTAL(i));
  5501. error->pipe[i].vblank = I915_READ(VBLANK(i));
  5502. error->pipe[i].vsync = I915_READ(VSYNC(i));
  5503. }
  5504. return error;
  5505. }
  5506. void
  5507. intel_display_print_error_state(struct seq_file *m,
  5508. struct drm_device *dev,
  5509. struct intel_display_error_state *error)
  5510. {
  5511. int i;
  5512. for (i = 0; i < 2; i++) {
  5513. seq_printf(m, "Pipe [%d]:\n", i);
  5514. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  5515. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  5516. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  5517. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  5518. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  5519. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  5520. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  5521. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  5522. seq_printf(m, "Plane [%d]:\n", i);
  5523. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  5524. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  5525. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  5526. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  5527. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  5528. if (INTEL_INFO(dev)->gen >= 4) {
  5529. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  5530. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  5531. }
  5532. seq_printf(m, "Cursor [%d]:\n", i);
  5533. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  5534. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  5535. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  5536. }
  5537. }
  5538. #endif