intel_display.c 175 KB

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