intel_display.c 176 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091
  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. u32 fbc_ctl;
  953. if (!I915_HAS_FBC(dev))
  954. return;
  955. if (!(I915_READ(FBC_CONTROL) & FBC_CTL_EN))
  956. return; /* Already off, just return */
  957. /* Disable compression */
  958. fbc_ctl = I915_READ(FBC_CONTROL);
  959. fbc_ctl &= ~FBC_CTL_EN;
  960. I915_WRITE(FBC_CONTROL, fbc_ctl);
  961. /* Wait for compressing bit to clear */
  962. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10, 0)) {
  963. DRM_DEBUG_KMS("FBC idle timed out\n");
  964. return;
  965. }
  966. intel_wait_for_vblank(dev);
  967. DRM_DEBUG_KMS("disabled FBC\n");
  968. }
  969. static bool i8xx_fbc_enabled(struct drm_device *dev)
  970. {
  971. struct drm_i915_private *dev_priv = dev->dev_private;
  972. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  973. }
  974. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  975. {
  976. struct drm_device *dev = crtc->dev;
  977. struct drm_i915_private *dev_priv = dev->dev_private;
  978. struct drm_framebuffer *fb = crtc->fb;
  979. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  980. struct drm_i915_gem_object *obj_priv = to_intel_bo(intel_fb->obj);
  981. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  982. int plane = (intel_crtc->plane == 0 ? DPFC_CTL_PLANEA :
  983. DPFC_CTL_PLANEB);
  984. unsigned long stall_watermark = 200;
  985. u32 dpfc_ctl;
  986. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  987. dev_priv->cfb_fence = obj_priv->fence_reg;
  988. dev_priv->cfb_plane = intel_crtc->plane;
  989. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  990. if (obj_priv->tiling_mode != I915_TILING_NONE) {
  991. dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;
  992. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  993. } else {
  994. I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  995. }
  996. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  997. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  998. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  999. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1000. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  1001. /* enable it... */
  1002. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  1003. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1004. }
  1005. void g4x_disable_fbc(struct drm_device *dev)
  1006. {
  1007. struct drm_i915_private *dev_priv = dev->dev_private;
  1008. u32 dpfc_ctl;
  1009. /* Disable compression */
  1010. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1011. dpfc_ctl &= ~DPFC_CTL_EN;
  1012. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  1013. intel_wait_for_vblank(dev);
  1014. DRM_DEBUG_KMS("disabled FBC\n");
  1015. }
  1016. static bool g4x_fbc_enabled(struct drm_device *dev)
  1017. {
  1018. struct drm_i915_private *dev_priv = dev->dev_private;
  1019. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  1020. }
  1021. static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1022. {
  1023. struct drm_device *dev = crtc->dev;
  1024. struct drm_i915_private *dev_priv = dev->dev_private;
  1025. struct drm_framebuffer *fb = crtc->fb;
  1026. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1027. struct drm_i915_gem_object *obj_priv = to_intel_bo(intel_fb->obj);
  1028. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1029. int plane = (intel_crtc->plane == 0) ? DPFC_CTL_PLANEA :
  1030. DPFC_CTL_PLANEB;
  1031. unsigned long stall_watermark = 200;
  1032. u32 dpfc_ctl;
  1033. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  1034. dev_priv->cfb_fence = obj_priv->fence_reg;
  1035. dev_priv->cfb_plane = intel_crtc->plane;
  1036. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1037. dpfc_ctl &= DPFC_RESERVED;
  1038. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  1039. if (obj_priv->tiling_mode != I915_TILING_NONE) {
  1040. dpfc_ctl |= (DPFC_CTL_FENCE_EN | dev_priv->cfb_fence);
  1041. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  1042. } else {
  1043. I915_WRITE(ILK_DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  1044. }
  1045. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  1046. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1047. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1048. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1049. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  1050. I915_WRITE(ILK_FBC_RT_BASE, obj_priv->gtt_offset | ILK_FBC_RT_VALID);
  1051. /* enable it... */
  1052. I915_WRITE(ILK_DPFC_CONTROL, I915_READ(ILK_DPFC_CONTROL) |
  1053. DPFC_CTL_EN);
  1054. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1055. }
  1056. void ironlake_disable_fbc(struct drm_device *dev)
  1057. {
  1058. struct drm_i915_private *dev_priv = dev->dev_private;
  1059. u32 dpfc_ctl;
  1060. /* Disable compression */
  1061. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1062. dpfc_ctl &= ~DPFC_CTL_EN;
  1063. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  1064. intel_wait_for_vblank(dev);
  1065. DRM_DEBUG_KMS("disabled FBC\n");
  1066. }
  1067. static bool ironlake_fbc_enabled(struct drm_device *dev)
  1068. {
  1069. struct drm_i915_private *dev_priv = dev->dev_private;
  1070. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  1071. }
  1072. bool intel_fbc_enabled(struct drm_device *dev)
  1073. {
  1074. struct drm_i915_private *dev_priv = dev->dev_private;
  1075. if (!dev_priv->display.fbc_enabled)
  1076. return false;
  1077. return dev_priv->display.fbc_enabled(dev);
  1078. }
  1079. void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1080. {
  1081. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  1082. if (!dev_priv->display.enable_fbc)
  1083. return;
  1084. dev_priv->display.enable_fbc(crtc, interval);
  1085. }
  1086. void intel_disable_fbc(struct drm_device *dev)
  1087. {
  1088. struct drm_i915_private *dev_priv = dev->dev_private;
  1089. if (!dev_priv->display.disable_fbc)
  1090. return;
  1091. dev_priv->display.disable_fbc(dev);
  1092. }
  1093. /**
  1094. * intel_update_fbc - enable/disable FBC as needed
  1095. * @crtc: CRTC to point the compressor at
  1096. * @mode: mode in use
  1097. *
  1098. * Set up the framebuffer compression hardware at mode set time. We
  1099. * enable it if possible:
  1100. * - plane A only (on pre-965)
  1101. * - no pixel mulitply/line duplication
  1102. * - no alpha buffer discard
  1103. * - no dual wide
  1104. * - framebuffer <= 2048 in width, 1536 in height
  1105. *
  1106. * We can't assume that any compression will take place (worst case),
  1107. * so the compressed buffer has to be the same size as the uncompressed
  1108. * one. It also must reside (along with the line length buffer) in
  1109. * stolen memory.
  1110. *
  1111. * We need to enable/disable FBC on a global basis.
  1112. */
  1113. static void intel_update_fbc(struct drm_crtc *crtc,
  1114. struct drm_display_mode *mode)
  1115. {
  1116. struct drm_device *dev = crtc->dev;
  1117. struct drm_i915_private *dev_priv = dev->dev_private;
  1118. struct drm_framebuffer *fb = crtc->fb;
  1119. struct intel_framebuffer *intel_fb;
  1120. struct drm_i915_gem_object *obj_priv;
  1121. struct drm_crtc *tmp_crtc;
  1122. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1123. int plane = intel_crtc->plane;
  1124. int crtcs_enabled = 0;
  1125. DRM_DEBUG_KMS("\n");
  1126. if (!i915_powersave)
  1127. return;
  1128. if (!I915_HAS_FBC(dev))
  1129. return;
  1130. if (!crtc->fb)
  1131. return;
  1132. intel_fb = to_intel_framebuffer(fb);
  1133. obj_priv = to_intel_bo(intel_fb->obj);
  1134. /*
  1135. * If FBC is already on, we just have to verify that we can
  1136. * keep it that way...
  1137. * Need to disable if:
  1138. * - more than one pipe is active
  1139. * - changing FBC params (stride, fence, mode)
  1140. * - new fb is too large to fit in compressed buffer
  1141. * - going to an unsupported config (interlace, pixel multiply, etc.)
  1142. */
  1143. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  1144. if (tmp_crtc->enabled)
  1145. crtcs_enabled++;
  1146. }
  1147. DRM_DEBUG_KMS("%d pipes active\n", crtcs_enabled);
  1148. if (crtcs_enabled > 1) {
  1149. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  1150. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  1151. goto out_disable;
  1152. }
  1153. if (intel_fb->obj->size > dev_priv->cfb_size) {
  1154. DRM_DEBUG_KMS("framebuffer too large, disabling "
  1155. "compression\n");
  1156. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  1157. goto out_disable;
  1158. }
  1159. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
  1160. (mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
  1161. DRM_DEBUG_KMS("mode incompatible with compression, "
  1162. "disabling\n");
  1163. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  1164. goto out_disable;
  1165. }
  1166. if ((mode->hdisplay > 2048) ||
  1167. (mode->vdisplay > 1536)) {
  1168. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  1169. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  1170. goto out_disable;
  1171. }
  1172. if ((IS_I915GM(dev) || IS_I945GM(dev)) && plane != 0) {
  1173. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  1174. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  1175. goto out_disable;
  1176. }
  1177. if (obj_priv->tiling_mode != I915_TILING_X) {
  1178. DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
  1179. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  1180. goto out_disable;
  1181. }
  1182. /* If the kernel debugger is active, always disable compression */
  1183. if (in_dbg_master())
  1184. goto out_disable;
  1185. if (intel_fbc_enabled(dev)) {
  1186. /* We can re-enable it in this case, but need to update pitch */
  1187. if ((fb->pitch > dev_priv->cfb_pitch) ||
  1188. (obj_priv->fence_reg != dev_priv->cfb_fence) ||
  1189. (plane != dev_priv->cfb_plane))
  1190. intel_disable_fbc(dev);
  1191. }
  1192. /* Now try to turn it back on if possible */
  1193. if (!intel_fbc_enabled(dev))
  1194. intel_enable_fbc(crtc, 500);
  1195. return;
  1196. out_disable:
  1197. /* Multiple disables should be harmless */
  1198. if (intel_fbc_enabled(dev)) {
  1199. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  1200. intel_disable_fbc(dev);
  1201. }
  1202. }
  1203. int
  1204. intel_pin_and_fence_fb_obj(struct drm_device *dev, struct drm_gem_object *obj)
  1205. {
  1206. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1207. u32 alignment;
  1208. int ret;
  1209. switch (obj_priv->tiling_mode) {
  1210. case I915_TILING_NONE:
  1211. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1212. alignment = 128 * 1024;
  1213. else if (IS_I965G(dev))
  1214. alignment = 4 * 1024;
  1215. else
  1216. alignment = 64 * 1024;
  1217. break;
  1218. case I915_TILING_X:
  1219. /* pin() will align the object as required by fence */
  1220. alignment = 0;
  1221. break;
  1222. case I915_TILING_Y:
  1223. /* FIXME: Is this true? */
  1224. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1225. return -EINVAL;
  1226. default:
  1227. BUG();
  1228. }
  1229. ret = i915_gem_object_pin(obj, alignment);
  1230. if (ret != 0)
  1231. return ret;
  1232. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1233. * fence, whereas 965+ only requires a fence if using
  1234. * framebuffer compression. For simplicity, we always install
  1235. * a fence as the cost is not that onerous.
  1236. */
  1237. if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
  1238. obj_priv->tiling_mode != I915_TILING_NONE) {
  1239. ret = i915_gem_object_get_fence_reg(obj);
  1240. if (ret != 0) {
  1241. i915_gem_object_unpin(obj);
  1242. return ret;
  1243. }
  1244. }
  1245. return 0;
  1246. }
  1247. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1248. static int
  1249. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1250. int x, int y)
  1251. {
  1252. struct drm_device *dev = crtc->dev;
  1253. struct drm_i915_private *dev_priv = dev->dev_private;
  1254. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1255. struct intel_framebuffer *intel_fb;
  1256. struct drm_i915_gem_object *obj_priv;
  1257. struct drm_gem_object *obj;
  1258. int plane = intel_crtc->plane;
  1259. unsigned long Start, Offset;
  1260. int dspbase = (plane == 0 ? DSPAADDR : DSPBADDR);
  1261. int dspsurf = (plane == 0 ? DSPASURF : DSPBSURF);
  1262. int dspstride = (plane == 0) ? DSPASTRIDE : DSPBSTRIDE;
  1263. int dsptileoff = (plane == 0 ? DSPATILEOFF : DSPBTILEOFF);
  1264. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  1265. u32 dspcntr;
  1266. switch (plane) {
  1267. case 0:
  1268. case 1:
  1269. break;
  1270. default:
  1271. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1272. return -EINVAL;
  1273. }
  1274. intel_fb = to_intel_framebuffer(fb);
  1275. obj = intel_fb->obj;
  1276. obj_priv = to_intel_bo(obj);
  1277. dspcntr = I915_READ(dspcntr_reg);
  1278. /* Mask out pixel format bits in case we change it */
  1279. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1280. switch (fb->bits_per_pixel) {
  1281. case 8:
  1282. dspcntr |= DISPPLANE_8BPP;
  1283. break;
  1284. case 16:
  1285. if (fb->depth == 15)
  1286. dspcntr |= DISPPLANE_15_16BPP;
  1287. else
  1288. dspcntr |= DISPPLANE_16BPP;
  1289. break;
  1290. case 24:
  1291. case 32:
  1292. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1293. break;
  1294. default:
  1295. DRM_ERROR("Unknown color depth\n");
  1296. return -EINVAL;
  1297. }
  1298. if (IS_I965G(dev)) {
  1299. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1300. dspcntr |= DISPPLANE_TILED;
  1301. else
  1302. dspcntr &= ~DISPPLANE_TILED;
  1303. }
  1304. if (IS_IRONLAKE(dev))
  1305. /* must disable */
  1306. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1307. I915_WRITE(dspcntr_reg, dspcntr);
  1308. Start = obj_priv->gtt_offset;
  1309. Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
  1310. DRM_DEBUG("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
  1311. I915_WRITE(dspstride, fb->pitch);
  1312. if (IS_I965G(dev)) {
  1313. I915_WRITE(dspbase, Offset);
  1314. I915_READ(dspbase);
  1315. I915_WRITE(dspsurf, Start);
  1316. I915_READ(dspsurf);
  1317. I915_WRITE(dsptileoff, (y << 16) | x);
  1318. } else {
  1319. I915_WRITE(dspbase, Start + Offset);
  1320. I915_READ(dspbase);
  1321. }
  1322. if ((IS_I965G(dev) || plane == 0))
  1323. intel_update_fbc(crtc, &crtc->mode);
  1324. intel_wait_for_vblank(dev);
  1325. intel_increase_pllclock(crtc, true);
  1326. return 0;
  1327. }
  1328. static int
  1329. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1330. struct drm_framebuffer *old_fb)
  1331. {
  1332. struct drm_device *dev = crtc->dev;
  1333. struct drm_i915_private *dev_priv = dev->dev_private;
  1334. struct drm_i915_master_private *master_priv;
  1335. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1336. struct intel_framebuffer *intel_fb;
  1337. struct drm_i915_gem_object *obj_priv;
  1338. struct drm_gem_object *obj;
  1339. int pipe = intel_crtc->pipe;
  1340. int plane = intel_crtc->plane;
  1341. unsigned long Start, Offset;
  1342. int dspbase = (plane == 0 ? DSPAADDR : DSPBADDR);
  1343. int dspsurf = (plane == 0 ? DSPASURF : DSPBSURF);
  1344. int dspstride = (plane == 0) ? DSPASTRIDE : DSPBSTRIDE;
  1345. int dsptileoff = (plane == 0 ? DSPATILEOFF : DSPBTILEOFF);
  1346. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  1347. u32 dspcntr;
  1348. int ret;
  1349. /* no fb bound */
  1350. if (!crtc->fb) {
  1351. DRM_DEBUG_KMS("No FB bound\n");
  1352. return 0;
  1353. }
  1354. switch (plane) {
  1355. case 0:
  1356. case 1:
  1357. break;
  1358. default:
  1359. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1360. return -EINVAL;
  1361. }
  1362. intel_fb = to_intel_framebuffer(crtc->fb);
  1363. obj = intel_fb->obj;
  1364. obj_priv = to_intel_bo(obj);
  1365. mutex_lock(&dev->struct_mutex);
  1366. ret = intel_pin_and_fence_fb_obj(dev, obj);
  1367. if (ret != 0) {
  1368. mutex_unlock(&dev->struct_mutex);
  1369. return ret;
  1370. }
  1371. ret = i915_gem_object_set_to_display_plane(obj);
  1372. if (ret != 0) {
  1373. i915_gem_object_unpin(obj);
  1374. mutex_unlock(&dev->struct_mutex);
  1375. return ret;
  1376. }
  1377. dspcntr = I915_READ(dspcntr_reg);
  1378. /* Mask out pixel format bits in case we change it */
  1379. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1380. switch (crtc->fb->bits_per_pixel) {
  1381. case 8:
  1382. dspcntr |= DISPPLANE_8BPP;
  1383. break;
  1384. case 16:
  1385. if (crtc->fb->depth == 15)
  1386. dspcntr |= DISPPLANE_15_16BPP;
  1387. else
  1388. dspcntr |= DISPPLANE_16BPP;
  1389. break;
  1390. case 24:
  1391. case 32:
  1392. if (crtc->fb->depth == 30)
  1393. dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
  1394. else
  1395. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1396. break;
  1397. default:
  1398. DRM_ERROR("Unknown color depth\n");
  1399. i915_gem_object_unpin(obj);
  1400. mutex_unlock(&dev->struct_mutex);
  1401. return -EINVAL;
  1402. }
  1403. if (IS_I965G(dev)) {
  1404. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1405. dspcntr |= DISPPLANE_TILED;
  1406. else
  1407. dspcntr &= ~DISPPLANE_TILED;
  1408. }
  1409. if (HAS_PCH_SPLIT(dev))
  1410. /* must disable */
  1411. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1412. I915_WRITE(dspcntr_reg, dspcntr);
  1413. Start = obj_priv->gtt_offset;
  1414. Offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
  1415. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1416. Start, Offset, x, y, crtc->fb->pitch);
  1417. I915_WRITE(dspstride, crtc->fb->pitch);
  1418. if (IS_I965G(dev)) {
  1419. I915_WRITE(dspsurf, Start);
  1420. I915_WRITE(dsptileoff, (y << 16) | x);
  1421. I915_WRITE(dspbase, Offset);
  1422. } else {
  1423. I915_WRITE(dspbase, Start + Offset);
  1424. }
  1425. POSTING_READ(dspbase);
  1426. if ((IS_I965G(dev) || plane == 0))
  1427. intel_update_fbc(crtc, &crtc->mode);
  1428. intel_wait_for_vblank(dev);
  1429. if (old_fb) {
  1430. intel_fb = to_intel_framebuffer(old_fb);
  1431. obj_priv = to_intel_bo(intel_fb->obj);
  1432. i915_gem_object_unpin(intel_fb->obj);
  1433. }
  1434. intel_increase_pllclock(crtc, true);
  1435. mutex_unlock(&dev->struct_mutex);
  1436. if (!dev->primary->master)
  1437. return 0;
  1438. master_priv = dev->primary->master->driver_priv;
  1439. if (!master_priv->sarea_priv)
  1440. return 0;
  1441. if (pipe) {
  1442. master_priv->sarea_priv->pipeB_x = x;
  1443. master_priv->sarea_priv->pipeB_y = y;
  1444. } else {
  1445. master_priv->sarea_priv->pipeA_x = x;
  1446. master_priv->sarea_priv->pipeA_y = y;
  1447. }
  1448. return 0;
  1449. }
  1450. /* Disable the VGA plane that we never use */
  1451. static void i915_disable_vga (struct drm_device *dev)
  1452. {
  1453. struct drm_i915_private *dev_priv = dev->dev_private;
  1454. u8 sr1;
  1455. u32 vga_reg;
  1456. if (HAS_PCH_SPLIT(dev))
  1457. vga_reg = CPU_VGACNTRL;
  1458. else
  1459. vga_reg = VGACNTRL;
  1460. if (I915_READ(vga_reg) & VGA_DISP_DISABLE)
  1461. return;
  1462. I915_WRITE8(VGA_SR_INDEX, 1);
  1463. sr1 = I915_READ8(VGA_SR_DATA);
  1464. I915_WRITE8(VGA_SR_DATA, sr1 | (1 << 5));
  1465. udelay(100);
  1466. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  1467. }
  1468. static void ironlake_disable_pll_edp (struct drm_crtc *crtc)
  1469. {
  1470. struct drm_device *dev = crtc->dev;
  1471. struct drm_i915_private *dev_priv = dev->dev_private;
  1472. u32 dpa_ctl;
  1473. DRM_DEBUG_KMS("\n");
  1474. dpa_ctl = I915_READ(DP_A);
  1475. dpa_ctl &= ~DP_PLL_ENABLE;
  1476. I915_WRITE(DP_A, dpa_ctl);
  1477. }
  1478. static void ironlake_enable_pll_edp (struct drm_crtc *crtc)
  1479. {
  1480. struct drm_device *dev = crtc->dev;
  1481. struct drm_i915_private *dev_priv = dev->dev_private;
  1482. u32 dpa_ctl;
  1483. dpa_ctl = I915_READ(DP_A);
  1484. dpa_ctl |= DP_PLL_ENABLE;
  1485. I915_WRITE(DP_A, dpa_ctl);
  1486. udelay(200);
  1487. }
  1488. static void ironlake_set_pll_edp (struct drm_crtc *crtc, int clock)
  1489. {
  1490. struct drm_device *dev = crtc->dev;
  1491. struct drm_i915_private *dev_priv = dev->dev_private;
  1492. u32 dpa_ctl;
  1493. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1494. dpa_ctl = I915_READ(DP_A);
  1495. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1496. if (clock < 200000) {
  1497. u32 temp;
  1498. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1499. /* workaround for 160Mhz:
  1500. 1) program 0x4600c bits 15:0 = 0x8124
  1501. 2) program 0x46010 bit 0 = 1
  1502. 3) program 0x46034 bit 24 = 1
  1503. 4) program 0x64000 bit 14 = 1
  1504. */
  1505. temp = I915_READ(0x4600c);
  1506. temp &= 0xffff0000;
  1507. I915_WRITE(0x4600c, temp | 0x8124);
  1508. temp = I915_READ(0x46010);
  1509. I915_WRITE(0x46010, temp | 1);
  1510. temp = I915_READ(0x46034);
  1511. I915_WRITE(0x46034, temp | (1 << 24));
  1512. } else {
  1513. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1514. }
  1515. I915_WRITE(DP_A, dpa_ctl);
  1516. udelay(500);
  1517. }
  1518. /* The FDI link training functions for ILK/Ibexpeak. */
  1519. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  1520. {
  1521. struct drm_device *dev = crtc->dev;
  1522. struct drm_i915_private *dev_priv = dev->dev_private;
  1523. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1524. int pipe = intel_crtc->pipe;
  1525. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  1526. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  1527. int fdi_rx_iir_reg = (pipe == 0) ? FDI_RXA_IIR : FDI_RXB_IIR;
  1528. int fdi_rx_imr_reg = (pipe == 0) ? FDI_RXA_IMR : FDI_RXB_IMR;
  1529. u32 temp, tries = 0;
  1530. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1531. for train result */
  1532. temp = I915_READ(fdi_rx_imr_reg);
  1533. temp &= ~FDI_RX_SYMBOL_LOCK;
  1534. temp &= ~FDI_RX_BIT_LOCK;
  1535. I915_WRITE(fdi_rx_imr_reg, temp);
  1536. I915_READ(fdi_rx_imr_reg);
  1537. udelay(150);
  1538. /* enable CPU FDI TX and PCH FDI RX */
  1539. temp = I915_READ(fdi_tx_reg);
  1540. temp |= FDI_TX_ENABLE;
  1541. temp &= ~(7 << 19);
  1542. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1543. temp &= ~FDI_LINK_TRAIN_NONE;
  1544. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1545. I915_WRITE(fdi_tx_reg, temp);
  1546. I915_READ(fdi_tx_reg);
  1547. temp = I915_READ(fdi_rx_reg);
  1548. temp &= ~FDI_LINK_TRAIN_NONE;
  1549. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1550. I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENABLE);
  1551. I915_READ(fdi_rx_reg);
  1552. udelay(150);
  1553. for (tries = 0; tries < 5; tries++) {
  1554. temp = I915_READ(fdi_rx_iir_reg);
  1555. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1556. if ((temp & FDI_RX_BIT_LOCK)) {
  1557. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1558. I915_WRITE(fdi_rx_iir_reg,
  1559. temp | FDI_RX_BIT_LOCK);
  1560. break;
  1561. }
  1562. }
  1563. if (tries == 5)
  1564. DRM_DEBUG_KMS("FDI train 1 fail!\n");
  1565. /* Train 2 */
  1566. temp = I915_READ(fdi_tx_reg);
  1567. temp &= ~FDI_LINK_TRAIN_NONE;
  1568. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1569. I915_WRITE(fdi_tx_reg, temp);
  1570. temp = I915_READ(fdi_rx_reg);
  1571. temp &= ~FDI_LINK_TRAIN_NONE;
  1572. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1573. I915_WRITE(fdi_rx_reg, temp);
  1574. udelay(150);
  1575. tries = 0;
  1576. for (tries = 0; tries < 5; tries++) {
  1577. temp = I915_READ(fdi_rx_iir_reg);
  1578. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1579. if (temp & FDI_RX_SYMBOL_LOCK) {
  1580. I915_WRITE(fdi_rx_iir_reg,
  1581. temp | FDI_RX_SYMBOL_LOCK);
  1582. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1583. break;
  1584. }
  1585. }
  1586. if (tries == 5)
  1587. DRM_DEBUG_KMS("FDI train 2 fail!\n");
  1588. DRM_DEBUG_KMS("FDI train done\n");
  1589. }
  1590. static int snb_b_fdi_train_param [] = {
  1591. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  1592. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  1593. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  1594. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  1595. };
  1596. /* The FDI link training functions for SNB/Cougarpoint. */
  1597. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  1598. {
  1599. struct drm_device *dev = crtc->dev;
  1600. struct drm_i915_private *dev_priv = dev->dev_private;
  1601. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1602. int pipe = intel_crtc->pipe;
  1603. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  1604. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  1605. int fdi_rx_iir_reg = (pipe == 0) ? FDI_RXA_IIR : FDI_RXB_IIR;
  1606. int fdi_rx_imr_reg = (pipe == 0) ? FDI_RXA_IMR : FDI_RXB_IMR;
  1607. u32 temp, i;
  1608. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1609. for train result */
  1610. temp = I915_READ(fdi_rx_imr_reg);
  1611. temp &= ~FDI_RX_SYMBOL_LOCK;
  1612. temp &= ~FDI_RX_BIT_LOCK;
  1613. I915_WRITE(fdi_rx_imr_reg, temp);
  1614. I915_READ(fdi_rx_imr_reg);
  1615. udelay(150);
  1616. /* enable CPU FDI TX and PCH FDI RX */
  1617. temp = I915_READ(fdi_tx_reg);
  1618. temp |= FDI_TX_ENABLE;
  1619. temp &= ~(7 << 19);
  1620. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1621. temp &= ~FDI_LINK_TRAIN_NONE;
  1622. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1623. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1624. /* SNB-B */
  1625. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1626. I915_WRITE(fdi_tx_reg, temp);
  1627. I915_READ(fdi_tx_reg);
  1628. temp = I915_READ(fdi_rx_reg);
  1629. if (HAS_PCH_CPT(dev)) {
  1630. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1631. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1632. } else {
  1633. temp &= ~FDI_LINK_TRAIN_NONE;
  1634. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1635. }
  1636. I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENABLE);
  1637. I915_READ(fdi_rx_reg);
  1638. udelay(150);
  1639. for (i = 0; i < 4; i++ ) {
  1640. temp = I915_READ(fdi_tx_reg);
  1641. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1642. temp |= snb_b_fdi_train_param[i];
  1643. I915_WRITE(fdi_tx_reg, temp);
  1644. udelay(500);
  1645. temp = I915_READ(fdi_rx_iir_reg);
  1646. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1647. if (temp & FDI_RX_BIT_LOCK) {
  1648. I915_WRITE(fdi_rx_iir_reg,
  1649. temp | FDI_RX_BIT_LOCK);
  1650. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1651. break;
  1652. }
  1653. }
  1654. if (i == 4)
  1655. DRM_DEBUG_KMS("FDI train 1 fail!\n");
  1656. /* Train 2 */
  1657. temp = I915_READ(fdi_tx_reg);
  1658. temp &= ~FDI_LINK_TRAIN_NONE;
  1659. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1660. if (IS_GEN6(dev)) {
  1661. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1662. /* SNB-B */
  1663. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1664. }
  1665. I915_WRITE(fdi_tx_reg, temp);
  1666. temp = I915_READ(fdi_rx_reg);
  1667. if (HAS_PCH_CPT(dev)) {
  1668. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1669. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  1670. } else {
  1671. temp &= ~FDI_LINK_TRAIN_NONE;
  1672. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1673. }
  1674. I915_WRITE(fdi_rx_reg, temp);
  1675. udelay(150);
  1676. for (i = 0; i < 4; i++ ) {
  1677. temp = I915_READ(fdi_tx_reg);
  1678. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1679. temp |= snb_b_fdi_train_param[i];
  1680. I915_WRITE(fdi_tx_reg, temp);
  1681. udelay(500);
  1682. temp = I915_READ(fdi_rx_iir_reg);
  1683. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1684. if (temp & FDI_RX_SYMBOL_LOCK) {
  1685. I915_WRITE(fdi_rx_iir_reg,
  1686. temp | FDI_RX_SYMBOL_LOCK);
  1687. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1688. break;
  1689. }
  1690. }
  1691. if (i == 4)
  1692. DRM_DEBUG_KMS("FDI train 2 fail!\n");
  1693. DRM_DEBUG_KMS("FDI train done.\n");
  1694. }
  1695. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  1696. {
  1697. struct drm_device *dev = crtc->dev;
  1698. struct drm_i915_private *dev_priv = dev->dev_private;
  1699. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1700. int pipe = intel_crtc->pipe;
  1701. int plane = intel_crtc->plane;
  1702. int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
  1703. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  1704. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  1705. int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
  1706. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  1707. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  1708. int transconf_reg = (pipe == 0) ? TRANSACONF : TRANSBCONF;
  1709. int pf_ctl_reg = (pipe == 0) ? PFA_CTL_1 : PFB_CTL_1;
  1710. int pf_win_size = (pipe == 0) ? PFA_WIN_SZ : PFB_WIN_SZ;
  1711. int pf_win_pos = (pipe == 0) ? PFA_WIN_POS : PFB_WIN_POS;
  1712. int cpu_htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  1713. int cpu_hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  1714. int cpu_hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  1715. int cpu_vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  1716. int cpu_vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  1717. int cpu_vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  1718. int trans_htot_reg = (pipe == 0) ? TRANS_HTOTAL_A : TRANS_HTOTAL_B;
  1719. int trans_hblank_reg = (pipe == 0) ? TRANS_HBLANK_A : TRANS_HBLANK_B;
  1720. int trans_hsync_reg = (pipe == 0) ? TRANS_HSYNC_A : TRANS_HSYNC_B;
  1721. int trans_vtot_reg = (pipe == 0) ? TRANS_VTOTAL_A : TRANS_VTOTAL_B;
  1722. int trans_vblank_reg = (pipe == 0) ? TRANS_VBLANK_A : TRANS_VBLANK_B;
  1723. int trans_vsync_reg = (pipe == 0) ? TRANS_VSYNC_A : TRANS_VSYNC_B;
  1724. int trans_dpll_sel = (pipe == 0) ? 0 : 1;
  1725. u32 temp;
  1726. u32 pipe_bpc;
  1727. temp = I915_READ(pipeconf_reg);
  1728. pipe_bpc = temp & PIPE_BPC_MASK;
  1729. /* XXX: When our outputs are all unaware of DPMS modes other than off
  1730. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  1731. */
  1732. switch (mode) {
  1733. case DRM_MODE_DPMS_ON:
  1734. case DRM_MODE_DPMS_STANDBY:
  1735. case DRM_MODE_DPMS_SUSPEND:
  1736. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  1737. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1738. temp = I915_READ(PCH_LVDS);
  1739. if ((temp & LVDS_PORT_EN) == 0) {
  1740. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  1741. POSTING_READ(PCH_LVDS);
  1742. }
  1743. }
  1744. if (HAS_eDP) {
  1745. /* enable eDP PLL */
  1746. ironlake_enable_pll_edp(crtc);
  1747. } else {
  1748. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  1749. temp = I915_READ(fdi_rx_reg);
  1750. /*
  1751. * make the BPC in FDI Rx be consistent with that in
  1752. * pipeconf reg.
  1753. */
  1754. temp &= ~(0x7 << 16);
  1755. temp |= (pipe_bpc << 11);
  1756. temp &= ~(7 << 19);
  1757. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1758. I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE);
  1759. I915_READ(fdi_rx_reg);
  1760. udelay(200);
  1761. /* Switch from Rawclk to PCDclk */
  1762. temp = I915_READ(fdi_rx_reg);
  1763. I915_WRITE(fdi_rx_reg, temp | FDI_SEL_PCDCLK);
  1764. I915_READ(fdi_rx_reg);
  1765. udelay(200);
  1766. /* Enable CPU FDI TX PLL, always on for Ironlake */
  1767. temp = I915_READ(fdi_tx_reg);
  1768. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  1769. I915_WRITE(fdi_tx_reg, temp | FDI_TX_PLL_ENABLE);
  1770. I915_READ(fdi_tx_reg);
  1771. udelay(100);
  1772. }
  1773. }
  1774. /* Enable panel fitting for LVDS */
  1775. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)
  1776. || HAS_eDP || intel_pch_has_edp(crtc)) {
  1777. if (dev_priv->pch_pf_size) {
  1778. temp = I915_READ(pf_ctl_reg);
  1779. I915_WRITE(pf_ctl_reg, temp | PF_ENABLE | PF_FILTER_MED_3x3);
  1780. I915_WRITE(pf_win_pos, dev_priv->pch_pf_pos);
  1781. I915_WRITE(pf_win_size, dev_priv->pch_pf_size);
  1782. } else
  1783. I915_WRITE(pf_ctl_reg, temp & ~PF_ENABLE);
  1784. }
  1785. /* Enable CPU pipe */
  1786. temp = I915_READ(pipeconf_reg);
  1787. if ((temp & PIPEACONF_ENABLE) == 0) {
  1788. I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  1789. I915_READ(pipeconf_reg);
  1790. udelay(100);
  1791. }
  1792. /* configure and enable CPU plane */
  1793. temp = I915_READ(dspcntr_reg);
  1794. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  1795. I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
  1796. /* Flush the plane changes */
  1797. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  1798. }
  1799. if (!HAS_eDP) {
  1800. /* For PCH output, training FDI link */
  1801. if (IS_GEN6(dev))
  1802. gen6_fdi_link_train(crtc);
  1803. else
  1804. ironlake_fdi_link_train(crtc);
  1805. /* enable PCH DPLL */
  1806. temp = I915_READ(pch_dpll_reg);
  1807. if ((temp & DPLL_VCO_ENABLE) == 0) {
  1808. I915_WRITE(pch_dpll_reg, temp | DPLL_VCO_ENABLE);
  1809. I915_READ(pch_dpll_reg);
  1810. }
  1811. udelay(200);
  1812. if (HAS_PCH_CPT(dev)) {
  1813. /* Be sure PCH DPLL SEL is set */
  1814. temp = I915_READ(PCH_DPLL_SEL);
  1815. if (trans_dpll_sel == 0 &&
  1816. (temp & TRANSA_DPLL_ENABLE) == 0)
  1817. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  1818. else if (trans_dpll_sel == 1 &&
  1819. (temp & TRANSB_DPLL_ENABLE) == 0)
  1820. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  1821. I915_WRITE(PCH_DPLL_SEL, temp);
  1822. I915_READ(PCH_DPLL_SEL);
  1823. }
  1824. /* set transcoder timing */
  1825. I915_WRITE(trans_htot_reg, I915_READ(cpu_htot_reg));
  1826. I915_WRITE(trans_hblank_reg, I915_READ(cpu_hblank_reg));
  1827. I915_WRITE(trans_hsync_reg, I915_READ(cpu_hsync_reg));
  1828. I915_WRITE(trans_vtot_reg, I915_READ(cpu_vtot_reg));
  1829. I915_WRITE(trans_vblank_reg, I915_READ(cpu_vblank_reg));
  1830. I915_WRITE(trans_vsync_reg, I915_READ(cpu_vsync_reg));
  1831. /* enable normal train */
  1832. temp = I915_READ(fdi_tx_reg);
  1833. temp &= ~FDI_LINK_TRAIN_NONE;
  1834. I915_WRITE(fdi_tx_reg, temp | FDI_LINK_TRAIN_NONE |
  1835. FDI_TX_ENHANCE_FRAME_ENABLE);
  1836. I915_READ(fdi_tx_reg);
  1837. temp = I915_READ(fdi_rx_reg);
  1838. if (HAS_PCH_CPT(dev)) {
  1839. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1840. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  1841. } else {
  1842. temp &= ~FDI_LINK_TRAIN_NONE;
  1843. temp |= FDI_LINK_TRAIN_NONE;
  1844. }
  1845. I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  1846. I915_READ(fdi_rx_reg);
  1847. /* wait one idle pattern time */
  1848. udelay(100);
  1849. /* For PCH DP, enable TRANS_DP_CTL */
  1850. if (HAS_PCH_CPT(dev) &&
  1851. intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  1852. int trans_dp_ctl = (pipe == 0) ? TRANS_DP_CTL_A : TRANS_DP_CTL_B;
  1853. int reg;
  1854. reg = I915_READ(trans_dp_ctl);
  1855. reg &= ~(TRANS_DP_PORT_SEL_MASK |
  1856. TRANS_DP_SYNC_MASK);
  1857. reg |= (TRANS_DP_OUTPUT_ENABLE |
  1858. TRANS_DP_ENH_FRAMING);
  1859. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  1860. reg |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  1861. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  1862. reg |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  1863. switch (intel_trans_dp_port_sel(crtc)) {
  1864. case PCH_DP_B:
  1865. reg |= TRANS_DP_PORT_SEL_B;
  1866. break;
  1867. case PCH_DP_C:
  1868. reg |= TRANS_DP_PORT_SEL_C;
  1869. break;
  1870. case PCH_DP_D:
  1871. reg |= TRANS_DP_PORT_SEL_D;
  1872. break;
  1873. default:
  1874. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  1875. reg |= TRANS_DP_PORT_SEL_B;
  1876. break;
  1877. }
  1878. I915_WRITE(trans_dp_ctl, reg);
  1879. POSTING_READ(trans_dp_ctl);
  1880. }
  1881. /* enable PCH transcoder */
  1882. temp = I915_READ(transconf_reg);
  1883. /*
  1884. * make the BPC in transcoder be consistent with
  1885. * that in pipeconf reg.
  1886. */
  1887. temp &= ~PIPE_BPC_MASK;
  1888. temp |= pipe_bpc;
  1889. I915_WRITE(transconf_reg, temp | TRANS_ENABLE);
  1890. I915_READ(transconf_reg);
  1891. if (wait_for(I915_READ(transconf_reg) & TRANS_STATE_ENABLE, 10, 0))
  1892. DRM_ERROR("failed to enable transcoder\n");
  1893. }
  1894. intel_crtc_load_lut(crtc);
  1895. intel_update_fbc(crtc, &crtc->mode);
  1896. break;
  1897. case DRM_MODE_DPMS_OFF:
  1898. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  1899. drm_vblank_off(dev, pipe);
  1900. /* Disable display plane */
  1901. temp = I915_READ(dspcntr_reg);
  1902. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  1903. I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
  1904. /* Flush the plane changes */
  1905. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  1906. I915_READ(dspbase_reg);
  1907. }
  1908. if (dev_priv->cfb_plane == plane &&
  1909. dev_priv->display.disable_fbc)
  1910. dev_priv->display.disable_fbc(dev);
  1911. i915_disable_vga(dev);
  1912. /* disable cpu pipe, disable after all planes disabled */
  1913. temp = I915_READ(pipeconf_reg);
  1914. if ((temp & PIPEACONF_ENABLE) != 0) {
  1915. I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  1916. /* wait for cpu pipe off, pipe state */
  1917. if (wait_for((I915_READ(pipeconf_reg) & I965_PIPECONF_ACTIVE) == 0, 50, 1))
  1918. DRM_ERROR("failed to turn off cpu pipe\n");
  1919. } else
  1920. DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
  1921. udelay(100);
  1922. /* Disable PF */
  1923. temp = I915_READ(pf_ctl_reg);
  1924. if ((temp & PF_ENABLE) != 0) {
  1925. I915_WRITE(pf_ctl_reg, temp & ~PF_ENABLE);
  1926. I915_READ(pf_ctl_reg);
  1927. }
  1928. I915_WRITE(pf_win_size, 0);
  1929. POSTING_READ(pf_win_size);
  1930. /* disable CPU FDI tx and PCH FDI rx */
  1931. temp = I915_READ(fdi_tx_reg);
  1932. I915_WRITE(fdi_tx_reg, temp & ~FDI_TX_ENABLE);
  1933. I915_READ(fdi_tx_reg);
  1934. temp = I915_READ(fdi_rx_reg);
  1935. /* BPC in FDI rx is consistent with that in pipeconf */
  1936. temp &= ~(0x07 << 16);
  1937. temp |= (pipe_bpc << 11);
  1938. I915_WRITE(fdi_rx_reg, temp & ~FDI_RX_ENABLE);
  1939. I915_READ(fdi_rx_reg);
  1940. udelay(100);
  1941. /* still set train pattern 1 */
  1942. temp = I915_READ(fdi_tx_reg);
  1943. temp &= ~FDI_LINK_TRAIN_NONE;
  1944. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1945. I915_WRITE(fdi_tx_reg, temp);
  1946. POSTING_READ(fdi_tx_reg);
  1947. temp = I915_READ(fdi_rx_reg);
  1948. if (HAS_PCH_CPT(dev)) {
  1949. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1950. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1951. } else {
  1952. temp &= ~FDI_LINK_TRAIN_NONE;
  1953. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1954. }
  1955. I915_WRITE(fdi_rx_reg, temp);
  1956. POSTING_READ(fdi_rx_reg);
  1957. udelay(100);
  1958. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1959. temp = I915_READ(PCH_LVDS);
  1960. I915_WRITE(PCH_LVDS, temp & ~LVDS_PORT_EN);
  1961. I915_READ(PCH_LVDS);
  1962. udelay(100);
  1963. }
  1964. /* disable PCH transcoder */
  1965. temp = I915_READ(transconf_reg);
  1966. if ((temp & TRANS_ENABLE) != 0) {
  1967. I915_WRITE(transconf_reg, temp & ~TRANS_ENABLE);
  1968. /* wait for PCH transcoder off, transcoder state */
  1969. if (wait_for((I915_READ(transconf_reg) & TRANS_STATE_ENABLE) == 0, 50, 1))
  1970. DRM_ERROR("failed to disable transcoder\n");
  1971. }
  1972. temp = I915_READ(transconf_reg);
  1973. /* BPC in transcoder is consistent with that in pipeconf */
  1974. temp &= ~PIPE_BPC_MASK;
  1975. temp |= pipe_bpc;
  1976. I915_WRITE(transconf_reg, temp);
  1977. I915_READ(transconf_reg);
  1978. udelay(100);
  1979. if (HAS_PCH_CPT(dev)) {
  1980. /* disable TRANS_DP_CTL */
  1981. int trans_dp_ctl = (pipe == 0) ? TRANS_DP_CTL_A : TRANS_DP_CTL_B;
  1982. int reg;
  1983. reg = I915_READ(trans_dp_ctl);
  1984. reg &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  1985. I915_WRITE(trans_dp_ctl, reg);
  1986. POSTING_READ(trans_dp_ctl);
  1987. /* disable DPLL_SEL */
  1988. temp = I915_READ(PCH_DPLL_SEL);
  1989. if (trans_dpll_sel == 0)
  1990. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  1991. else
  1992. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  1993. I915_WRITE(PCH_DPLL_SEL, temp);
  1994. I915_READ(PCH_DPLL_SEL);
  1995. }
  1996. /* disable PCH DPLL */
  1997. temp = I915_READ(pch_dpll_reg);
  1998. I915_WRITE(pch_dpll_reg, temp & ~DPLL_VCO_ENABLE);
  1999. I915_READ(pch_dpll_reg);
  2000. if (HAS_eDP) {
  2001. ironlake_disable_pll_edp(crtc);
  2002. }
  2003. /* Switch from PCDclk to Rawclk */
  2004. temp = I915_READ(fdi_rx_reg);
  2005. temp &= ~FDI_SEL_PCDCLK;
  2006. I915_WRITE(fdi_rx_reg, temp);
  2007. I915_READ(fdi_rx_reg);
  2008. /* Disable CPU FDI TX PLL */
  2009. temp = I915_READ(fdi_tx_reg);
  2010. I915_WRITE(fdi_tx_reg, temp & ~FDI_TX_PLL_ENABLE);
  2011. I915_READ(fdi_tx_reg);
  2012. udelay(100);
  2013. temp = I915_READ(fdi_rx_reg);
  2014. temp &= ~FDI_RX_PLL_ENABLE;
  2015. I915_WRITE(fdi_rx_reg, temp);
  2016. I915_READ(fdi_rx_reg);
  2017. /* Wait for the clocks to turn off. */
  2018. udelay(100);
  2019. break;
  2020. }
  2021. }
  2022. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2023. {
  2024. struct intel_overlay *overlay;
  2025. int ret;
  2026. if (!enable && intel_crtc->overlay) {
  2027. overlay = intel_crtc->overlay;
  2028. mutex_lock(&overlay->dev->struct_mutex);
  2029. for (;;) {
  2030. ret = intel_overlay_switch_off(overlay);
  2031. if (ret == 0)
  2032. break;
  2033. ret = intel_overlay_recover_from_interrupt(overlay, 0);
  2034. if (ret != 0) {
  2035. /* overlay doesn't react anymore. Usually
  2036. * results in a black screen and an unkillable
  2037. * X server. */
  2038. BUG();
  2039. overlay->hw_wedged = HW_WEDGED;
  2040. break;
  2041. }
  2042. }
  2043. mutex_unlock(&overlay->dev->struct_mutex);
  2044. }
  2045. /* Let userspace switch the overlay on again. In most cases userspace
  2046. * has to recompute where to put it anyway. */
  2047. return;
  2048. }
  2049. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2050. {
  2051. struct drm_device *dev = crtc->dev;
  2052. struct drm_i915_private *dev_priv = dev->dev_private;
  2053. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2054. int pipe = intel_crtc->pipe;
  2055. int plane = intel_crtc->plane;
  2056. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  2057. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  2058. int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
  2059. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  2060. u32 temp;
  2061. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2062. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2063. */
  2064. switch (mode) {
  2065. case DRM_MODE_DPMS_ON:
  2066. case DRM_MODE_DPMS_STANDBY:
  2067. case DRM_MODE_DPMS_SUSPEND:
  2068. /* Enable the DPLL */
  2069. temp = I915_READ(dpll_reg);
  2070. if ((temp & DPLL_VCO_ENABLE) == 0) {
  2071. I915_WRITE(dpll_reg, temp);
  2072. I915_READ(dpll_reg);
  2073. /* Wait for the clocks to stabilize. */
  2074. udelay(150);
  2075. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  2076. I915_READ(dpll_reg);
  2077. /* Wait for the clocks to stabilize. */
  2078. udelay(150);
  2079. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  2080. I915_READ(dpll_reg);
  2081. /* Wait for the clocks to stabilize. */
  2082. udelay(150);
  2083. }
  2084. /* Enable the pipe */
  2085. temp = I915_READ(pipeconf_reg);
  2086. if ((temp & PIPEACONF_ENABLE) == 0)
  2087. I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  2088. /* Enable the plane */
  2089. temp = I915_READ(dspcntr_reg);
  2090. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  2091. I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
  2092. /* Flush the plane changes */
  2093. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  2094. }
  2095. intel_crtc_load_lut(crtc);
  2096. if ((IS_I965G(dev) || plane == 0))
  2097. intel_update_fbc(crtc, &crtc->mode);
  2098. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2099. intel_crtc_dpms_overlay(intel_crtc, true);
  2100. break;
  2101. case DRM_MODE_DPMS_OFF:
  2102. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2103. intel_crtc_dpms_overlay(intel_crtc, false);
  2104. drm_vblank_off(dev, pipe);
  2105. if (dev_priv->cfb_plane == plane &&
  2106. dev_priv->display.disable_fbc)
  2107. dev_priv->display.disable_fbc(dev);
  2108. /* Disable the VGA plane that we never use */
  2109. i915_disable_vga(dev);
  2110. /* Disable display plane */
  2111. temp = I915_READ(dspcntr_reg);
  2112. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  2113. I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
  2114. /* Flush the plane changes */
  2115. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  2116. I915_READ(dspbase_reg);
  2117. }
  2118. if (!IS_I9XX(dev)) {
  2119. /* Wait for vblank for the disable to take effect */
  2120. intel_wait_for_vblank(dev);
  2121. }
  2122. /* Don't disable pipe A or pipe A PLLs if needed */
  2123. if (pipeconf_reg == PIPEACONF &&
  2124. (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  2125. goto skip_pipe_off;
  2126. /* Next, disable display pipes */
  2127. temp = I915_READ(pipeconf_reg);
  2128. if ((temp & PIPEACONF_ENABLE) != 0) {
  2129. I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  2130. I915_READ(pipeconf_reg);
  2131. }
  2132. /* Wait for vblank for the disable to take effect. */
  2133. intel_wait_for_vblank(dev);
  2134. temp = I915_READ(dpll_reg);
  2135. if ((temp & DPLL_VCO_ENABLE) != 0) {
  2136. I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
  2137. I915_READ(dpll_reg);
  2138. }
  2139. skip_pipe_off:
  2140. /* Wait for the clocks to turn off. */
  2141. udelay(150);
  2142. break;
  2143. }
  2144. }
  2145. /**
  2146. * Sets the power management mode of the pipe and plane.
  2147. */
  2148. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2149. {
  2150. struct drm_device *dev = crtc->dev;
  2151. struct drm_i915_private *dev_priv = dev->dev_private;
  2152. struct drm_i915_master_private *master_priv;
  2153. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2154. int pipe = intel_crtc->pipe;
  2155. bool enabled;
  2156. intel_crtc->dpms_mode = mode;
  2157. intel_crtc->cursor_on = mode == DRM_MODE_DPMS_ON;
  2158. /* When switching on the display, ensure that SR is disabled
  2159. * with multiple pipes prior to enabling to new pipe.
  2160. *
  2161. * When switching off the display, make sure the cursor is
  2162. * properly hidden prior to disabling the pipe.
  2163. */
  2164. if (mode == DRM_MODE_DPMS_ON)
  2165. intel_update_watermarks(dev);
  2166. else
  2167. intel_crtc_update_cursor(crtc);
  2168. dev_priv->display.dpms(crtc, mode);
  2169. if (mode == DRM_MODE_DPMS_ON)
  2170. intel_crtc_update_cursor(crtc);
  2171. else
  2172. intel_update_watermarks(dev);
  2173. if (!dev->primary->master)
  2174. return;
  2175. master_priv = dev->primary->master->driver_priv;
  2176. if (!master_priv->sarea_priv)
  2177. return;
  2178. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2179. switch (pipe) {
  2180. case 0:
  2181. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2182. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2183. break;
  2184. case 1:
  2185. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2186. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2187. break;
  2188. default:
  2189. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  2190. break;
  2191. }
  2192. }
  2193. static void intel_crtc_prepare (struct drm_crtc *crtc)
  2194. {
  2195. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2196. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  2197. }
  2198. static void intel_crtc_commit (struct drm_crtc *crtc)
  2199. {
  2200. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2201. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  2202. }
  2203. void intel_encoder_prepare (struct drm_encoder *encoder)
  2204. {
  2205. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2206. /* lvds has its own version of prepare see intel_lvds_prepare */
  2207. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2208. }
  2209. void intel_encoder_commit (struct drm_encoder *encoder)
  2210. {
  2211. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2212. /* lvds has its own version of commit see intel_lvds_commit */
  2213. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2214. }
  2215. void intel_encoder_destroy(struct drm_encoder *encoder)
  2216. {
  2217. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  2218. if (intel_encoder->ddc_bus)
  2219. intel_i2c_destroy(intel_encoder->ddc_bus);
  2220. if (intel_encoder->i2c_bus)
  2221. intel_i2c_destroy(intel_encoder->i2c_bus);
  2222. drm_encoder_cleanup(encoder);
  2223. kfree(intel_encoder);
  2224. }
  2225. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2226. struct drm_display_mode *mode,
  2227. struct drm_display_mode *adjusted_mode)
  2228. {
  2229. struct drm_device *dev = crtc->dev;
  2230. if (HAS_PCH_SPLIT(dev)) {
  2231. /* FDI link clock is fixed at 2.7G */
  2232. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  2233. return false;
  2234. }
  2235. return true;
  2236. }
  2237. static int i945_get_display_clock_speed(struct drm_device *dev)
  2238. {
  2239. return 400000;
  2240. }
  2241. static int i915_get_display_clock_speed(struct drm_device *dev)
  2242. {
  2243. return 333000;
  2244. }
  2245. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2246. {
  2247. return 200000;
  2248. }
  2249. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2250. {
  2251. u16 gcfgc = 0;
  2252. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2253. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2254. return 133000;
  2255. else {
  2256. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2257. case GC_DISPLAY_CLOCK_333_MHZ:
  2258. return 333000;
  2259. default:
  2260. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2261. return 190000;
  2262. }
  2263. }
  2264. }
  2265. static int i865_get_display_clock_speed(struct drm_device *dev)
  2266. {
  2267. return 266000;
  2268. }
  2269. static int i855_get_display_clock_speed(struct drm_device *dev)
  2270. {
  2271. u16 hpllcc = 0;
  2272. /* Assume that the hardware is in the high speed state. This
  2273. * should be the default.
  2274. */
  2275. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2276. case GC_CLOCK_133_200:
  2277. case GC_CLOCK_100_200:
  2278. return 200000;
  2279. case GC_CLOCK_166_250:
  2280. return 250000;
  2281. case GC_CLOCK_100_133:
  2282. return 133000;
  2283. }
  2284. /* Shouldn't happen */
  2285. return 0;
  2286. }
  2287. static int i830_get_display_clock_speed(struct drm_device *dev)
  2288. {
  2289. return 133000;
  2290. }
  2291. /**
  2292. * Return the pipe currently connected to the panel fitter,
  2293. * or -1 if the panel fitter is not present or not in use
  2294. */
  2295. int intel_panel_fitter_pipe (struct drm_device *dev)
  2296. {
  2297. struct drm_i915_private *dev_priv = dev->dev_private;
  2298. u32 pfit_control;
  2299. /* i830 doesn't have a panel fitter */
  2300. if (IS_I830(dev))
  2301. return -1;
  2302. pfit_control = I915_READ(PFIT_CONTROL);
  2303. /* See if the panel fitter is in use */
  2304. if ((pfit_control & PFIT_ENABLE) == 0)
  2305. return -1;
  2306. /* 965 can place panel fitter on either pipe */
  2307. if (IS_I965G(dev))
  2308. return (pfit_control >> 29) & 0x3;
  2309. /* older chips can only use pipe 1 */
  2310. return 1;
  2311. }
  2312. struct fdi_m_n {
  2313. u32 tu;
  2314. u32 gmch_m;
  2315. u32 gmch_n;
  2316. u32 link_m;
  2317. u32 link_n;
  2318. };
  2319. static void
  2320. fdi_reduce_ratio(u32 *num, u32 *den)
  2321. {
  2322. while (*num > 0xffffff || *den > 0xffffff) {
  2323. *num >>= 1;
  2324. *den >>= 1;
  2325. }
  2326. }
  2327. #define DATA_N 0x800000
  2328. #define LINK_N 0x80000
  2329. static void
  2330. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  2331. int link_clock, struct fdi_m_n *m_n)
  2332. {
  2333. u64 temp;
  2334. m_n->tu = 64; /* default size */
  2335. temp = (u64) DATA_N * pixel_clock;
  2336. temp = div_u64(temp, link_clock);
  2337. m_n->gmch_m = div_u64(temp * bits_per_pixel, nlanes);
  2338. m_n->gmch_m >>= 3; /* convert to bytes_per_pixel */
  2339. m_n->gmch_n = DATA_N;
  2340. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  2341. temp = (u64) LINK_N * pixel_clock;
  2342. m_n->link_m = div_u64(temp, link_clock);
  2343. m_n->link_n = LINK_N;
  2344. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  2345. }
  2346. struct intel_watermark_params {
  2347. unsigned long fifo_size;
  2348. unsigned long max_wm;
  2349. unsigned long default_wm;
  2350. unsigned long guard_size;
  2351. unsigned long cacheline_size;
  2352. };
  2353. /* Pineview has different values for various configs */
  2354. static struct intel_watermark_params pineview_display_wm = {
  2355. PINEVIEW_DISPLAY_FIFO,
  2356. PINEVIEW_MAX_WM,
  2357. PINEVIEW_DFT_WM,
  2358. PINEVIEW_GUARD_WM,
  2359. PINEVIEW_FIFO_LINE_SIZE
  2360. };
  2361. static struct intel_watermark_params pineview_display_hplloff_wm = {
  2362. PINEVIEW_DISPLAY_FIFO,
  2363. PINEVIEW_MAX_WM,
  2364. PINEVIEW_DFT_HPLLOFF_WM,
  2365. PINEVIEW_GUARD_WM,
  2366. PINEVIEW_FIFO_LINE_SIZE
  2367. };
  2368. static struct intel_watermark_params pineview_cursor_wm = {
  2369. PINEVIEW_CURSOR_FIFO,
  2370. PINEVIEW_CURSOR_MAX_WM,
  2371. PINEVIEW_CURSOR_DFT_WM,
  2372. PINEVIEW_CURSOR_GUARD_WM,
  2373. PINEVIEW_FIFO_LINE_SIZE,
  2374. };
  2375. static struct intel_watermark_params pineview_cursor_hplloff_wm = {
  2376. PINEVIEW_CURSOR_FIFO,
  2377. PINEVIEW_CURSOR_MAX_WM,
  2378. PINEVIEW_CURSOR_DFT_WM,
  2379. PINEVIEW_CURSOR_GUARD_WM,
  2380. PINEVIEW_FIFO_LINE_SIZE
  2381. };
  2382. static struct intel_watermark_params g4x_wm_info = {
  2383. G4X_FIFO_SIZE,
  2384. G4X_MAX_WM,
  2385. G4X_MAX_WM,
  2386. 2,
  2387. G4X_FIFO_LINE_SIZE,
  2388. };
  2389. static struct intel_watermark_params g4x_cursor_wm_info = {
  2390. I965_CURSOR_FIFO,
  2391. I965_CURSOR_MAX_WM,
  2392. I965_CURSOR_DFT_WM,
  2393. 2,
  2394. G4X_FIFO_LINE_SIZE,
  2395. };
  2396. static struct intel_watermark_params i965_cursor_wm_info = {
  2397. I965_CURSOR_FIFO,
  2398. I965_CURSOR_MAX_WM,
  2399. I965_CURSOR_DFT_WM,
  2400. 2,
  2401. I915_FIFO_LINE_SIZE,
  2402. };
  2403. static struct intel_watermark_params i945_wm_info = {
  2404. I945_FIFO_SIZE,
  2405. I915_MAX_WM,
  2406. 1,
  2407. 2,
  2408. I915_FIFO_LINE_SIZE
  2409. };
  2410. static struct intel_watermark_params i915_wm_info = {
  2411. I915_FIFO_SIZE,
  2412. I915_MAX_WM,
  2413. 1,
  2414. 2,
  2415. I915_FIFO_LINE_SIZE
  2416. };
  2417. static struct intel_watermark_params i855_wm_info = {
  2418. I855GM_FIFO_SIZE,
  2419. I915_MAX_WM,
  2420. 1,
  2421. 2,
  2422. I830_FIFO_LINE_SIZE
  2423. };
  2424. static struct intel_watermark_params i830_wm_info = {
  2425. I830_FIFO_SIZE,
  2426. I915_MAX_WM,
  2427. 1,
  2428. 2,
  2429. I830_FIFO_LINE_SIZE
  2430. };
  2431. static struct intel_watermark_params ironlake_display_wm_info = {
  2432. ILK_DISPLAY_FIFO,
  2433. ILK_DISPLAY_MAXWM,
  2434. ILK_DISPLAY_DFTWM,
  2435. 2,
  2436. ILK_FIFO_LINE_SIZE
  2437. };
  2438. static struct intel_watermark_params ironlake_cursor_wm_info = {
  2439. ILK_CURSOR_FIFO,
  2440. ILK_CURSOR_MAXWM,
  2441. ILK_CURSOR_DFTWM,
  2442. 2,
  2443. ILK_FIFO_LINE_SIZE
  2444. };
  2445. static struct intel_watermark_params ironlake_display_srwm_info = {
  2446. ILK_DISPLAY_SR_FIFO,
  2447. ILK_DISPLAY_MAX_SRWM,
  2448. ILK_DISPLAY_DFT_SRWM,
  2449. 2,
  2450. ILK_FIFO_LINE_SIZE
  2451. };
  2452. static struct intel_watermark_params ironlake_cursor_srwm_info = {
  2453. ILK_CURSOR_SR_FIFO,
  2454. ILK_CURSOR_MAX_SRWM,
  2455. ILK_CURSOR_DFT_SRWM,
  2456. 2,
  2457. ILK_FIFO_LINE_SIZE
  2458. };
  2459. /**
  2460. * intel_calculate_wm - calculate watermark level
  2461. * @clock_in_khz: pixel clock
  2462. * @wm: chip FIFO params
  2463. * @pixel_size: display pixel size
  2464. * @latency_ns: memory latency for the platform
  2465. *
  2466. * Calculate the watermark level (the level at which the display plane will
  2467. * start fetching from memory again). Each chip has a different display
  2468. * FIFO size and allocation, so the caller needs to figure that out and pass
  2469. * in the correct intel_watermark_params structure.
  2470. *
  2471. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  2472. * on the pixel size. When it reaches the watermark level, it'll start
  2473. * fetching FIFO line sized based chunks from memory until the FIFO fills
  2474. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  2475. * will occur, and a display engine hang could result.
  2476. */
  2477. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  2478. struct intel_watermark_params *wm,
  2479. int pixel_size,
  2480. unsigned long latency_ns)
  2481. {
  2482. long entries_required, wm_size;
  2483. /*
  2484. * Note: we need to make sure we don't overflow for various clock &
  2485. * latency values.
  2486. * clocks go from a few thousand to several hundred thousand.
  2487. * latency is usually a few thousand
  2488. */
  2489. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  2490. 1000;
  2491. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  2492. DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries_required);
  2493. wm_size = wm->fifo_size - (entries_required + wm->guard_size);
  2494. DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
  2495. /* Don't promote wm_size to unsigned... */
  2496. if (wm_size > (long)wm->max_wm)
  2497. wm_size = wm->max_wm;
  2498. if (wm_size <= 0) {
  2499. wm_size = wm->default_wm;
  2500. DRM_ERROR("Insufficient FIFO for plane, expect flickering:"
  2501. " entries required = %ld, available = %lu.\n",
  2502. entries_required + wm->guard_size,
  2503. wm->fifo_size);
  2504. }
  2505. return wm_size;
  2506. }
  2507. struct cxsr_latency {
  2508. int is_desktop;
  2509. int is_ddr3;
  2510. unsigned long fsb_freq;
  2511. unsigned long mem_freq;
  2512. unsigned long display_sr;
  2513. unsigned long display_hpll_disable;
  2514. unsigned long cursor_sr;
  2515. unsigned long cursor_hpll_disable;
  2516. };
  2517. static const struct cxsr_latency cxsr_latency_table[] = {
  2518. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  2519. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  2520. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  2521. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  2522. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  2523. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  2524. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  2525. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  2526. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  2527. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  2528. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  2529. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  2530. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  2531. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  2532. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  2533. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  2534. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  2535. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  2536. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  2537. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  2538. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  2539. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  2540. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  2541. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  2542. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  2543. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  2544. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  2545. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  2546. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  2547. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  2548. };
  2549. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  2550. int is_ddr3,
  2551. int fsb,
  2552. int mem)
  2553. {
  2554. const struct cxsr_latency *latency;
  2555. int i;
  2556. if (fsb == 0 || mem == 0)
  2557. return NULL;
  2558. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  2559. latency = &cxsr_latency_table[i];
  2560. if (is_desktop == latency->is_desktop &&
  2561. is_ddr3 == latency->is_ddr3 &&
  2562. fsb == latency->fsb_freq && mem == latency->mem_freq)
  2563. return latency;
  2564. }
  2565. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2566. return NULL;
  2567. }
  2568. static void pineview_disable_cxsr(struct drm_device *dev)
  2569. {
  2570. struct drm_i915_private *dev_priv = dev->dev_private;
  2571. /* deactivate cxsr */
  2572. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  2573. }
  2574. /*
  2575. * Latency for FIFO fetches is dependent on several factors:
  2576. * - memory configuration (speed, channels)
  2577. * - chipset
  2578. * - current MCH state
  2579. * It can be fairly high in some situations, so here we assume a fairly
  2580. * pessimal value. It's a tradeoff between extra memory fetches (if we
  2581. * set this value too high, the FIFO will fetch frequently to stay full)
  2582. * and power consumption (set it too low to save power and we might see
  2583. * FIFO underruns and display "flicker").
  2584. *
  2585. * A value of 5us seems to be a good balance; safe for very low end
  2586. * platforms but not overly aggressive on lower latency configs.
  2587. */
  2588. static const int latency_ns = 5000;
  2589. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  2590. {
  2591. struct drm_i915_private *dev_priv = dev->dev_private;
  2592. uint32_t dsparb = I915_READ(DSPARB);
  2593. int size;
  2594. size = dsparb & 0x7f;
  2595. if (plane)
  2596. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  2597. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2598. plane ? "B" : "A", size);
  2599. return size;
  2600. }
  2601. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  2602. {
  2603. struct drm_i915_private *dev_priv = dev->dev_private;
  2604. uint32_t dsparb = I915_READ(DSPARB);
  2605. int size;
  2606. size = dsparb & 0x1ff;
  2607. if (plane)
  2608. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  2609. size >>= 1; /* Convert to cachelines */
  2610. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2611. plane ? "B" : "A", size);
  2612. return size;
  2613. }
  2614. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  2615. {
  2616. struct drm_i915_private *dev_priv = dev->dev_private;
  2617. uint32_t dsparb = I915_READ(DSPARB);
  2618. int size;
  2619. size = dsparb & 0x7f;
  2620. size >>= 2; /* Convert to cachelines */
  2621. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2622. plane ? "B" : "A",
  2623. size);
  2624. return size;
  2625. }
  2626. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  2627. {
  2628. struct drm_i915_private *dev_priv = dev->dev_private;
  2629. uint32_t dsparb = I915_READ(DSPARB);
  2630. int size;
  2631. size = dsparb & 0x7f;
  2632. size >>= 1; /* Convert to cachelines */
  2633. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2634. plane ? "B" : "A", size);
  2635. return size;
  2636. }
  2637. static void pineview_update_wm(struct drm_device *dev, int planea_clock,
  2638. int planeb_clock, int sr_hdisplay, int unused,
  2639. int pixel_size)
  2640. {
  2641. struct drm_i915_private *dev_priv = dev->dev_private;
  2642. const struct cxsr_latency *latency;
  2643. u32 reg;
  2644. unsigned long wm;
  2645. int sr_clock;
  2646. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  2647. dev_priv->fsb_freq, dev_priv->mem_freq);
  2648. if (!latency) {
  2649. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2650. pineview_disable_cxsr(dev);
  2651. return;
  2652. }
  2653. if (!planea_clock || !planeb_clock) {
  2654. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2655. /* Display SR */
  2656. wm = intel_calculate_wm(sr_clock, &pineview_display_wm,
  2657. pixel_size, latency->display_sr);
  2658. reg = I915_READ(DSPFW1);
  2659. reg &= ~DSPFW_SR_MASK;
  2660. reg |= wm << DSPFW_SR_SHIFT;
  2661. I915_WRITE(DSPFW1, reg);
  2662. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  2663. /* cursor SR */
  2664. wm = intel_calculate_wm(sr_clock, &pineview_cursor_wm,
  2665. pixel_size, latency->cursor_sr);
  2666. reg = I915_READ(DSPFW3);
  2667. reg &= ~DSPFW_CURSOR_SR_MASK;
  2668. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  2669. I915_WRITE(DSPFW3, reg);
  2670. /* Display HPLL off SR */
  2671. wm = intel_calculate_wm(sr_clock, &pineview_display_hplloff_wm,
  2672. pixel_size, latency->display_hpll_disable);
  2673. reg = I915_READ(DSPFW3);
  2674. reg &= ~DSPFW_HPLL_SR_MASK;
  2675. reg |= wm & DSPFW_HPLL_SR_MASK;
  2676. I915_WRITE(DSPFW3, reg);
  2677. /* cursor HPLL off SR */
  2678. wm = intel_calculate_wm(sr_clock, &pineview_cursor_hplloff_wm,
  2679. pixel_size, latency->cursor_hpll_disable);
  2680. reg = I915_READ(DSPFW3);
  2681. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  2682. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  2683. I915_WRITE(DSPFW3, reg);
  2684. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  2685. /* activate cxsr */
  2686. I915_WRITE(DSPFW3,
  2687. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  2688. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  2689. } else {
  2690. pineview_disable_cxsr(dev);
  2691. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  2692. }
  2693. }
  2694. static void g4x_update_wm(struct drm_device *dev, int planea_clock,
  2695. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2696. int pixel_size)
  2697. {
  2698. struct drm_i915_private *dev_priv = dev->dev_private;
  2699. int total_size, cacheline_size;
  2700. int planea_wm, planeb_wm, cursora_wm, cursorb_wm, cursor_sr;
  2701. struct intel_watermark_params planea_params, planeb_params;
  2702. unsigned long line_time_us;
  2703. int sr_clock, sr_entries = 0, entries_required;
  2704. /* Create copies of the base settings for each pipe */
  2705. planea_params = planeb_params = g4x_wm_info;
  2706. /* Grab a couple of global values before we overwrite them */
  2707. total_size = planea_params.fifo_size;
  2708. cacheline_size = planea_params.cacheline_size;
  2709. /*
  2710. * Note: we need to make sure we don't overflow for various clock &
  2711. * latency values.
  2712. * clocks go from a few thousand to several hundred thousand.
  2713. * latency is usually a few thousand
  2714. */
  2715. entries_required = ((planea_clock / 1000) * pixel_size * latency_ns) /
  2716. 1000;
  2717. entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
  2718. planea_wm = entries_required + planea_params.guard_size;
  2719. entries_required = ((planeb_clock / 1000) * pixel_size * latency_ns) /
  2720. 1000;
  2721. entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
  2722. planeb_wm = entries_required + planeb_params.guard_size;
  2723. cursora_wm = cursorb_wm = 16;
  2724. cursor_sr = 32;
  2725. DRM_DEBUG("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  2726. /* Calc sr entries for one plane configs */
  2727. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  2728. /* self-refresh has much higher latency */
  2729. static const int sr_latency_ns = 12000;
  2730. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2731. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2732. /* Use ns/us then divide to preserve precision */
  2733. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2734. pixel_size * sr_hdisplay;
  2735. sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
  2736. entries_required = (((sr_latency_ns / line_time_us) +
  2737. 1000) / 1000) * pixel_size * 64;
  2738. entries_required = DIV_ROUND_UP(entries_required,
  2739. g4x_cursor_wm_info.cacheline_size);
  2740. cursor_sr = entries_required + g4x_cursor_wm_info.guard_size;
  2741. if (cursor_sr > g4x_cursor_wm_info.max_wm)
  2742. cursor_sr = g4x_cursor_wm_info.max_wm;
  2743. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  2744. "cursor %d\n", sr_entries, cursor_sr);
  2745. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  2746. } else {
  2747. /* Turn off self refresh if both pipes are enabled */
  2748. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2749. & ~FW_BLC_SELF_EN);
  2750. }
  2751. DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, SR %d\n",
  2752. planea_wm, planeb_wm, sr_entries);
  2753. planea_wm &= 0x3f;
  2754. planeb_wm &= 0x3f;
  2755. I915_WRITE(DSPFW1, (sr_entries << DSPFW_SR_SHIFT) |
  2756. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  2757. (planeb_wm << DSPFW_PLANEB_SHIFT) | planea_wm);
  2758. I915_WRITE(DSPFW2, (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  2759. (cursora_wm << DSPFW_CURSORA_SHIFT));
  2760. /* HPLL off in SR has some issues on G4x... disable it */
  2761. I915_WRITE(DSPFW3, (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  2762. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  2763. }
  2764. static void i965_update_wm(struct drm_device *dev, int planea_clock,
  2765. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2766. int pixel_size)
  2767. {
  2768. struct drm_i915_private *dev_priv = dev->dev_private;
  2769. unsigned long line_time_us;
  2770. int sr_clock, sr_entries, srwm = 1;
  2771. int cursor_sr = 16;
  2772. /* Calc sr entries for one plane configs */
  2773. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  2774. /* self-refresh has much higher latency */
  2775. static const int sr_latency_ns = 12000;
  2776. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2777. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2778. /* Use ns/us then divide to preserve precision */
  2779. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2780. pixel_size * sr_hdisplay;
  2781. sr_entries = DIV_ROUND_UP(sr_entries, I915_FIFO_LINE_SIZE);
  2782. DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
  2783. srwm = I965_FIFO_SIZE - sr_entries;
  2784. if (srwm < 0)
  2785. srwm = 1;
  2786. srwm &= 0x1ff;
  2787. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2788. pixel_size * 64;
  2789. sr_entries = DIV_ROUND_UP(sr_entries,
  2790. i965_cursor_wm_info.cacheline_size);
  2791. cursor_sr = i965_cursor_wm_info.fifo_size -
  2792. (sr_entries + i965_cursor_wm_info.guard_size);
  2793. if (cursor_sr > i965_cursor_wm_info.max_wm)
  2794. cursor_sr = i965_cursor_wm_info.max_wm;
  2795. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  2796. "cursor %d\n", srwm, cursor_sr);
  2797. if (IS_I965GM(dev))
  2798. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  2799. } else {
  2800. /* Turn off self refresh if both pipes are enabled */
  2801. if (IS_I965GM(dev))
  2802. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2803. & ~FW_BLC_SELF_EN);
  2804. }
  2805. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  2806. srwm);
  2807. /* 965 has limitations... */
  2808. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) | (8 << 16) | (8 << 8) |
  2809. (8 << 0));
  2810. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  2811. /* update cursor SR watermark */
  2812. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  2813. }
  2814. static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
  2815. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2816. int pixel_size)
  2817. {
  2818. struct drm_i915_private *dev_priv = dev->dev_private;
  2819. uint32_t fwater_lo;
  2820. uint32_t fwater_hi;
  2821. int total_size, cacheline_size, cwm, srwm = 1;
  2822. int planea_wm, planeb_wm;
  2823. struct intel_watermark_params planea_params, planeb_params;
  2824. unsigned long line_time_us;
  2825. int sr_clock, sr_entries = 0;
  2826. /* Create copies of the base settings for each pipe */
  2827. if (IS_I965GM(dev) || IS_I945GM(dev))
  2828. planea_params = planeb_params = i945_wm_info;
  2829. else if (IS_I9XX(dev))
  2830. planea_params = planeb_params = i915_wm_info;
  2831. else
  2832. planea_params = planeb_params = i855_wm_info;
  2833. /* Grab a couple of global values before we overwrite them */
  2834. total_size = planea_params.fifo_size;
  2835. cacheline_size = planea_params.cacheline_size;
  2836. /* Update per-plane FIFO sizes */
  2837. planea_params.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  2838. planeb_params.fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  2839. planea_wm = intel_calculate_wm(planea_clock, &planea_params,
  2840. pixel_size, latency_ns);
  2841. planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
  2842. pixel_size, latency_ns);
  2843. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  2844. /*
  2845. * Overlay gets an aggressive default since video jitter is bad.
  2846. */
  2847. cwm = 2;
  2848. /* Calc sr entries for one plane configs */
  2849. if (HAS_FW_BLC(dev) && sr_hdisplay &&
  2850. (!planea_clock || !planeb_clock)) {
  2851. /* self-refresh has much higher latency */
  2852. static const int sr_latency_ns = 6000;
  2853. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2854. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2855. /* Use ns/us then divide to preserve precision */
  2856. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2857. pixel_size * sr_hdisplay;
  2858. sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
  2859. DRM_DEBUG_KMS("self-refresh entries: %d\n", sr_entries);
  2860. srwm = total_size - sr_entries;
  2861. if (srwm < 0)
  2862. srwm = 1;
  2863. if (IS_I945G(dev) || IS_I945GM(dev))
  2864. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  2865. else if (IS_I915GM(dev)) {
  2866. /* 915M has a smaller SRWM field */
  2867. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  2868. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  2869. }
  2870. } else {
  2871. /* Turn off self refresh if both pipes are enabled */
  2872. if (IS_I945G(dev) || IS_I945GM(dev)) {
  2873. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2874. & ~FW_BLC_SELF_EN);
  2875. } else if (IS_I915GM(dev)) {
  2876. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  2877. }
  2878. }
  2879. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  2880. planea_wm, planeb_wm, cwm, srwm);
  2881. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  2882. fwater_hi = (cwm & 0x1f);
  2883. /* Set request length to 8 cachelines per fetch */
  2884. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  2885. fwater_hi = fwater_hi | (1 << 8);
  2886. I915_WRITE(FW_BLC, fwater_lo);
  2887. I915_WRITE(FW_BLC2, fwater_hi);
  2888. }
  2889. static void i830_update_wm(struct drm_device *dev, int planea_clock, int unused,
  2890. int unused2, int unused3, int pixel_size)
  2891. {
  2892. struct drm_i915_private *dev_priv = dev->dev_private;
  2893. uint32_t fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  2894. int planea_wm;
  2895. i830_wm_info.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  2896. planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
  2897. pixel_size, latency_ns);
  2898. fwater_lo |= (3<<8) | planea_wm;
  2899. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  2900. I915_WRITE(FW_BLC, fwater_lo);
  2901. }
  2902. #define ILK_LP0_PLANE_LATENCY 700
  2903. #define ILK_LP0_CURSOR_LATENCY 1300
  2904. static void ironlake_update_wm(struct drm_device *dev, int planea_clock,
  2905. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2906. int pixel_size)
  2907. {
  2908. struct drm_i915_private *dev_priv = dev->dev_private;
  2909. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  2910. int sr_wm, cursor_wm;
  2911. unsigned long line_time_us;
  2912. int sr_clock, entries_required;
  2913. u32 reg_value;
  2914. int line_count;
  2915. int planea_htotal = 0, planeb_htotal = 0;
  2916. struct drm_crtc *crtc;
  2917. /* Need htotal for all active display plane */
  2918. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2919. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2920. if (intel_crtc->dpms_mode == DRM_MODE_DPMS_ON) {
  2921. if (intel_crtc->plane == 0)
  2922. planea_htotal = crtc->mode.htotal;
  2923. else
  2924. planeb_htotal = crtc->mode.htotal;
  2925. }
  2926. }
  2927. /* Calculate and update the watermark for plane A */
  2928. if (planea_clock) {
  2929. entries_required = ((planea_clock / 1000) * pixel_size *
  2930. ILK_LP0_PLANE_LATENCY) / 1000;
  2931. entries_required = DIV_ROUND_UP(entries_required,
  2932. ironlake_display_wm_info.cacheline_size);
  2933. planea_wm = entries_required +
  2934. ironlake_display_wm_info.guard_size;
  2935. if (planea_wm > (int)ironlake_display_wm_info.max_wm)
  2936. planea_wm = ironlake_display_wm_info.max_wm;
  2937. /* Use the large buffer method to calculate cursor watermark */
  2938. line_time_us = (planea_htotal * 1000) / planea_clock;
  2939. /* Use ns/us then divide to preserve precision */
  2940. line_count = (ILK_LP0_CURSOR_LATENCY / line_time_us + 1000) / 1000;
  2941. /* calculate the cursor watermark for cursor A */
  2942. entries_required = line_count * 64 * pixel_size;
  2943. entries_required = DIV_ROUND_UP(entries_required,
  2944. ironlake_cursor_wm_info.cacheline_size);
  2945. cursora_wm = entries_required + ironlake_cursor_wm_info.guard_size;
  2946. if (cursora_wm > ironlake_cursor_wm_info.max_wm)
  2947. cursora_wm = ironlake_cursor_wm_info.max_wm;
  2948. reg_value = I915_READ(WM0_PIPEA_ILK);
  2949. reg_value &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  2950. reg_value |= (planea_wm << WM0_PIPE_PLANE_SHIFT) |
  2951. (cursora_wm & WM0_PIPE_CURSOR_MASK);
  2952. I915_WRITE(WM0_PIPEA_ILK, reg_value);
  2953. DRM_DEBUG_KMS("FIFO watermarks For pipe A - plane %d, "
  2954. "cursor: %d\n", planea_wm, cursora_wm);
  2955. }
  2956. /* Calculate and update the watermark for plane B */
  2957. if (planeb_clock) {
  2958. entries_required = ((planeb_clock / 1000) * pixel_size *
  2959. ILK_LP0_PLANE_LATENCY) / 1000;
  2960. entries_required = DIV_ROUND_UP(entries_required,
  2961. ironlake_display_wm_info.cacheline_size);
  2962. planeb_wm = entries_required +
  2963. ironlake_display_wm_info.guard_size;
  2964. if (planeb_wm > (int)ironlake_display_wm_info.max_wm)
  2965. planeb_wm = ironlake_display_wm_info.max_wm;
  2966. /* Use the large buffer method to calculate cursor watermark */
  2967. line_time_us = (planeb_htotal * 1000) / planeb_clock;
  2968. /* Use ns/us then divide to preserve precision */
  2969. line_count = (ILK_LP0_CURSOR_LATENCY / line_time_us + 1000) / 1000;
  2970. /* calculate the cursor watermark for cursor B */
  2971. entries_required = line_count * 64 * pixel_size;
  2972. entries_required = DIV_ROUND_UP(entries_required,
  2973. ironlake_cursor_wm_info.cacheline_size);
  2974. cursorb_wm = entries_required + ironlake_cursor_wm_info.guard_size;
  2975. if (cursorb_wm > ironlake_cursor_wm_info.max_wm)
  2976. cursorb_wm = ironlake_cursor_wm_info.max_wm;
  2977. reg_value = I915_READ(WM0_PIPEB_ILK);
  2978. reg_value &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  2979. reg_value |= (planeb_wm << WM0_PIPE_PLANE_SHIFT) |
  2980. (cursorb_wm & WM0_PIPE_CURSOR_MASK);
  2981. I915_WRITE(WM0_PIPEB_ILK, reg_value);
  2982. DRM_DEBUG_KMS("FIFO watermarks For pipe B - plane %d, "
  2983. "cursor: %d\n", planeb_wm, cursorb_wm);
  2984. }
  2985. /*
  2986. * Calculate and update the self-refresh watermark only when one
  2987. * display plane is used.
  2988. */
  2989. if (!planea_clock || !planeb_clock) {
  2990. /* Read the self-refresh latency. The unit is 0.5us */
  2991. int ilk_sr_latency = I915_READ(MLTR_ILK) & ILK_SRLT_MASK;
  2992. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2993. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2994. /* Use ns/us then divide to preserve precision */
  2995. line_count = ((ilk_sr_latency * 500) / line_time_us + 1000)
  2996. / 1000;
  2997. /* calculate the self-refresh watermark for display plane */
  2998. entries_required = line_count * sr_hdisplay * pixel_size;
  2999. entries_required = DIV_ROUND_UP(entries_required,
  3000. ironlake_display_srwm_info.cacheline_size);
  3001. sr_wm = entries_required +
  3002. ironlake_display_srwm_info.guard_size;
  3003. /* calculate the self-refresh watermark for display cursor */
  3004. entries_required = line_count * pixel_size * 64;
  3005. entries_required = DIV_ROUND_UP(entries_required,
  3006. ironlake_cursor_srwm_info.cacheline_size);
  3007. cursor_wm = entries_required +
  3008. ironlake_cursor_srwm_info.guard_size;
  3009. /* configure watermark and enable self-refresh */
  3010. reg_value = I915_READ(WM1_LP_ILK);
  3011. reg_value &= ~(WM1_LP_LATENCY_MASK | WM1_LP_SR_MASK |
  3012. WM1_LP_CURSOR_MASK);
  3013. reg_value |= WM1_LP_SR_EN |
  3014. (ilk_sr_latency << WM1_LP_LATENCY_SHIFT) |
  3015. (sr_wm << WM1_LP_SR_SHIFT) | cursor_wm;
  3016. I915_WRITE(WM1_LP_ILK, reg_value);
  3017. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  3018. "cursor %d\n", sr_wm, cursor_wm);
  3019. } else {
  3020. /* Turn off self refresh if both pipes are enabled */
  3021. I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
  3022. }
  3023. }
  3024. /**
  3025. * intel_update_watermarks - update FIFO watermark values based on current modes
  3026. *
  3027. * Calculate watermark values for the various WM regs based on current mode
  3028. * and plane configuration.
  3029. *
  3030. * There are several cases to deal with here:
  3031. * - normal (i.e. non-self-refresh)
  3032. * - self-refresh (SR) mode
  3033. * - lines are large relative to FIFO size (buffer can hold up to 2)
  3034. * - lines are small relative to FIFO size (buffer can hold more than 2
  3035. * lines), so need to account for TLB latency
  3036. *
  3037. * The normal calculation is:
  3038. * watermark = dotclock * bytes per pixel * latency
  3039. * where latency is platform & configuration dependent (we assume pessimal
  3040. * values here).
  3041. *
  3042. * The SR calculation is:
  3043. * watermark = (trunc(latency/line time)+1) * surface width *
  3044. * bytes per pixel
  3045. * where
  3046. * line time = htotal / dotclock
  3047. * surface width = hdisplay for normal plane and 64 for cursor
  3048. * and latency is assumed to be high, as above.
  3049. *
  3050. * The final value programmed to the register should always be rounded up,
  3051. * and include an extra 2 entries to account for clock crossings.
  3052. *
  3053. * We don't use the sprite, so we can ignore that. And on Crestline we have
  3054. * to set the non-SR watermarks to 8.
  3055. */
  3056. static void intel_update_watermarks(struct drm_device *dev)
  3057. {
  3058. struct drm_i915_private *dev_priv = dev->dev_private;
  3059. struct drm_crtc *crtc;
  3060. int sr_hdisplay = 0;
  3061. unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
  3062. int enabled = 0, pixel_size = 0;
  3063. int sr_htotal = 0;
  3064. if (!dev_priv->display.update_wm)
  3065. return;
  3066. /* Get the clock config from both planes */
  3067. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3068. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3069. if (intel_crtc->dpms_mode == DRM_MODE_DPMS_ON) {
  3070. enabled++;
  3071. if (intel_crtc->plane == 0) {
  3072. DRM_DEBUG_KMS("plane A (pipe %d) clock: %d\n",
  3073. intel_crtc->pipe, crtc->mode.clock);
  3074. planea_clock = crtc->mode.clock;
  3075. } else {
  3076. DRM_DEBUG_KMS("plane B (pipe %d) clock: %d\n",
  3077. intel_crtc->pipe, crtc->mode.clock);
  3078. planeb_clock = crtc->mode.clock;
  3079. }
  3080. sr_hdisplay = crtc->mode.hdisplay;
  3081. sr_clock = crtc->mode.clock;
  3082. sr_htotal = crtc->mode.htotal;
  3083. if (crtc->fb)
  3084. pixel_size = crtc->fb->bits_per_pixel / 8;
  3085. else
  3086. pixel_size = 4; /* by default */
  3087. }
  3088. }
  3089. if (enabled <= 0)
  3090. return;
  3091. dev_priv->display.update_wm(dev, planea_clock, planeb_clock,
  3092. sr_hdisplay, sr_htotal, pixel_size);
  3093. }
  3094. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  3095. struct drm_display_mode *mode,
  3096. struct drm_display_mode *adjusted_mode,
  3097. int x, int y,
  3098. struct drm_framebuffer *old_fb)
  3099. {
  3100. struct drm_device *dev = crtc->dev;
  3101. struct drm_i915_private *dev_priv = dev->dev_private;
  3102. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3103. int pipe = intel_crtc->pipe;
  3104. int plane = intel_crtc->plane;
  3105. int fp_reg = (pipe == 0) ? FPA0 : FPB0;
  3106. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  3107. int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
  3108. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  3109. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  3110. int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  3111. int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  3112. int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  3113. int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  3114. int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  3115. int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  3116. int dspsize_reg = (plane == 0) ? DSPASIZE : DSPBSIZE;
  3117. int dsppos_reg = (plane == 0) ? DSPAPOS : DSPBPOS;
  3118. int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
  3119. int refclk, num_connectors = 0;
  3120. intel_clock_t clock, reduced_clock;
  3121. u32 dpll = 0, fp = 0, fp2 = 0, dspcntr, pipeconf;
  3122. bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
  3123. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  3124. bool is_edp = false;
  3125. struct drm_mode_config *mode_config = &dev->mode_config;
  3126. struct drm_encoder *encoder;
  3127. struct intel_encoder *intel_encoder = NULL;
  3128. const intel_limit_t *limit;
  3129. int ret;
  3130. struct fdi_m_n m_n = {0};
  3131. int data_m1_reg = (pipe == 0) ? PIPEA_DATA_M1 : PIPEB_DATA_M1;
  3132. int data_n1_reg = (pipe == 0) ? PIPEA_DATA_N1 : PIPEB_DATA_N1;
  3133. int link_m1_reg = (pipe == 0) ? PIPEA_LINK_M1 : PIPEB_LINK_M1;
  3134. int link_n1_reg = (pipe == 0) ? PIPEA_LINK_N1 : PIPEB_LINK_N1;
  3135. int pch_fp_reg = (pipe == 0) ? PCH_FPA0 : PCH_FPB0;
  3136. int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
  3137. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  3138. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  3139. int trans_dpll_sel = (pipe == 0) ? 0 : 1;
  3140. int lvds_reg = LVDS;
  3141. u32 temp;
  3142. int sdvo_pixel_multiply;
  3143. int target_clock;
  3144. drm_vblank_pre_modeset(dev, pipe);
  3145. list_for_each_entry(encoder, &mode_config->encoder_list, head) {
  3146. if (!encoder || encoder->crtc != crtc)
  3147. continue;
  3148. intel_encoder = enc_to_intel_encoder(encoder);
  3149. switch (intel_encoder->type) {
  3150. case INTEL_OUTPUT_LVDS:
  3151. is_lvds = true;
  3152. break;
  3153. case INTEL_OUTPUT_SDVO:
  3154. case INTEL_OUTPUT_HDMI:
  3155. is_sdvo = true;
  3156. if (intel_encoder->needs_tv_clock)
  3157. is_tv = true;
  3158. break;
  3159. case INTEL_OUTPUT_DVO:
  3160. is_dvo = true;
  3161. break;
  3162. case INTEL_OUTPUT_TVOUT:
  3163. is_tv = true;
  3164. break;
  3165. case INTEL_OUTPUT_ANALOG:
  3166. is_crt = true;
  3167. break;
  3168. case INTEL_OUTPUT_DISPLAYPORT:
  3169. is_dp = true;
  3170. break;
  3171. case INTEL_OUTPUT_EDP:
  3172. is_edp = true;
  3173. break;
  3174. }
  3175. num_connectors++;
  3176. }
  3177. if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2) {
  3178. refclk = dev_priv->lvds_ssc_freq * 1000;
  3179. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3180. refclk / 1000);
  3181. } else if (IS_I9XX(dev)) {
  3182. refclk = 96000;
  3183. if (HAS_PCH_SPLIT(dev))
  3184. refclk = 120000; /* 120Mhz refclk */
  3185. } else {
  3186. refclk = 48000;
  3187. }
  3188. /*
  3189. * Returns a set of divisors for the desired target clock with the given
  3190. * refclk, or FALSE. The returned values represent the clock equation:
  3191. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3192. */
  3193. limit = intel_limit(crtc);
  3194. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  3195. if (!ok) {
  3196. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3197. drm_vblank_post_modeset(dev, pipe);
  3198. return -EINVAL;
  3199. }
  3200. /* Ensure that the cursor is valid for the new mode before changing... */
  3201. intel_crtc_update_cursor(crtc);
  3202. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3203. has_reduced_clock = limit->find_pll(limit, crtc,
  3204. dev_priv->lvds_downclock,
  3205. refclk,
  3206. &reduced_clock);
  3207. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  3208. /*
  3209. * If the different P is found, it means that we can't
  3210. * switch the display clock by using the FP0/FP1.
  3211. * In such case we will disable the LVDS downclock
  3212. * feature.
  3213. */
  3214. DRM_DEBUG_KMS("Different P is found for "
  3215. "LVDS clock/downclock\n");
  3216. has_reduced_clock = 0;
  3217. }
  3218. }
  3219. /* SDVO TV has fixed PLL values depend on its clock range,
  3220. this mirrors vbios setting. */
  3221. if (is_sdvo && is_tv) {
  3222. if (adjusted_mode->clock >= 100000
  3223. && adjusted_mode->clock < 140500) {
  3224. clock.p1 = 2;
  3225. clock.p2 = 10;
  3226. clock.n = 3;
  3227. clock.m1 = 16;
  3228. clock.m2 = 8;
  3229. } else if (adjusted_mode->clock >= 140500
  3230. && adjusted_mode->clock <= 200000) {
  3231. clock.p1 = 1;
  3232. clock.p2 = 10;
  3233. clock.n = 6;
  3234. clock.m1 = 12;
  3235. clock.m2 = 8;
  3236. }
  3237. }
  3238. /* FDI link */
  3239. if (HAS_PCH_SPLIT(dev)) {
  3240. int lane = 0, link_bw, bpp;
  3241. /* eDP doesn't require FDI link, so just set DP M/N
  3242. according to current link config */
  3243. if (is_edp) {
  3244. target_clock = mode->clock;
  3245. intel_edp_link_config(intel_encoder,
  3246. &lane, &link_bw);
  3247. } else {
  3248. /* DP over FDI requires target mode clock
  3249. instead of link clock */
  3250. if (is_dp)
  3251. target_clock = mode->clock;
  3252. else
  3253. target_clock = adjusted_mode->clock;
  3254. link_bw = 270000;
  3255. }
  3256. /* determine panel color depth */
  3257. temp = I915_READ(pipeconf_reg);
  3258. temp &= ~PIPE_BPC_MASK;
  3259. if (is_lvds) {
  3260. int lvds_reg = I915_READ(PCH_LVDS);
  3261. /* the BPC will be 6 if it is 18-bit LVDS panel */
  3262. if ((lvds_reg & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
  3263. temp |= PIPE_8BPC;
  3264. else
  3265. temp |= PIPE_6BPC;
  3266. } else if (is_edp || (is_dp && intel_pch_has_edp(crtc))) {
  3267. switch (dev_priv->edp_bpp/3) {
  3268. case 8:
  3269. temp |= PIPE_8BPC;
  3270. break;
  3271. case 10:
  3272. temp |= PIPE_10BPC;
  3273. break;
  3274. case 6:
  3275. temp |= PIPE_6BPC;
  3276. break;
  3277. case 12:
  3278. temp |= PIPE_12BPC;
  3279. break;
  3280. }
  3281. } else
  3282. temp |= PIPE_8BPC;
  3283. I915_WRITE(pipeconf_reg, temp);
  3284. I915_READ(pipeconf_reg);
  3285. switch (temp & PIPE_BPC_MASK) {
  3286. case PIPE_8BPC:
  3287. bpp = 24;
  3288. break;
  3289. case PIPE_10BPC:
  3290. bpp = 30;
  3291. break;
  3292. case PIPE_6BPC:
  3293. bpp = 18;
  3294. break;
  3295. case PIPE_12BPC:
  3296. bpp = 36;
  3297. break;
  3298. default:
  3299. DRM_ERROR("unknown pipe bpc value\n");
  3300. bpp = 24;
  3301. }
  3302. if (!lane) {
  3303. /*
  3304. * Account for spread spectrum to avoid
  3305. * oversubscribing the link. Max center spread
  3306. * is 2.5%; use 5% for safety's sake.
  3307. */
  3308. u32 bps = target_clock * bpp * 21 / 20;
  3309. lane = bps / (link_bw * 8) + 1;
  3310. }
  3311. intel_crtc->fdi_lanes = lane;
  3312. ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);
  3313. }
  3314. /* Ironlake: try to setup display ref clock before DPLL
  3315. * enabling. This is only under driver's control after
  3316. * PCH B stepping, previous chipset stepping should be
  3317. * ignoring this setting.
  3318. */
  3319. if (HAS_PCH_SPLIT(dev)) {
  3320. temp = I915_READ(PCH_DREF_CONTROL);
  3321. /* Always enable nonspread source */
  3322. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  3323. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  3324. I915_WRITE(PCH_DREF_CONTROL, temp);
  3325. POSTING_READ(PCH_DREF_CONTROL);
  3326. temp &= ~DREF_SSC_SOURCE_MASK;
  3327. temp |= DREF_SSC_SOURCE_ENABLE;
  3328. I915_WRITE(PCH_DREF_CONTROL, temp);
  3329. POSTING_READ(PCH_DREF_CONTROL);
  3330. udelay(200);
  3331. if (is_edp) {
  3332. if (dev_priv->lvds_use_ssc) {
  3333. temp |= DREF_SSC1_ENABLE;
  3334. I915_WRITE(PCH_DREF_CONTROL, temp);
  3335. POSTING_READ(PCH_DREF_CONTROL);
  3336. udelay(200);
  3337. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  3338. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  3339. I915_WRITE(PCH_DREF_CONTROL, temp);
  3340. POSTING_READ(PCH_DREF_CONTROL);
  3341. } else {
  3342. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  3343. I915_WRITE(PCH_DREF_CONTROL, temp);
  3344. POSTING_READ(PCH_DREF_CONTROL);
  3345. }
  3346. }
  3347. }
  3348. if (IS_PINEVIEW(dev)) {
  3349. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  3350. if (has_reduced_clock)
  3351. fp2 = (1 << reduced_clock.n) << 16 |
  3352. reduced_clock.m1 << 8 | reduced_clock.m2;
  3353. } else {
  3354. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  3355. if (has_reduced_clock)
  3356. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  3357. reduced_clock.m2;
  3358. }
  3359. if (!HAS_PCH_SPLIT(dev))
  3360. dpll = DPLL_VGA_MODE_DIS;
  3361. if (IS_I9XX(dev)) {
  3362. if (is_lvds)
  3363. dpll |= DPLLB_MODE_LVDS;
  3364. else
  3365. dpll |= DPLLB_MODE_DAC_SERIAL;
  3366. if (is_sdvo) {
  3367. dpll |= DPLL_DVO_HIGH_SPEED;
  3368. sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  3369. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3370. dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  3371. else if (HAS_PCH_SPLIT(dev))
  3372. dpll |= (sdvo_pixel_multiply - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  3373. }
  3374. if (is_dp)
  3375. dpll |= DPLL_DVO_HIGH_SPEED;
  3376. /* compute bitmask from p1 value */
  3377. if (IS_PINEVIEW(dev))
  3378. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  3379. else {
  3380. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3381. /* also FPA1 */
  3382. if (HAS_PCH_SPLIT(dev))
  3383. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3384. if (IS_G4X(dev) && has_reduced_clock)
  3385. dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3386. }
  3387. switch (clock.p2) {
  3388. case 5:
  3389. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3390. break;
  3391. case 7:
  3392. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3393. break;
  3394. case 10:
  3395. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3396. break;
  3397. case 14:
  3398. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3399. break;
  3400. }
  3401. if (IS_I965G(dev) && !HAS_PCH_SPLIT(dev))
  3402. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  3403. } else {
  3404. if (is_lvds) {
  3405. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3406. } else {
  3407. if (clock.p1 == 2)
  3408. dpll |= PLL_P1_DIVIDE_BY_TWO;
  3409. else
  3410. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3411. if (clock.p2 == 4)
  3412. dpll |= PLL_P2_DIVIDE_BY_4;
  3413. }
  3414. }
  3415. if (is_sdvo && is_tv)
  3416. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3417. else if (is_tv)
  3418. /* XXX: just matching BIOS for now */
  3419. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3420. dpll |= 3;
  3421. else if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2)
  3422. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3423. else
  3424. dpll |= PLL_REF_INPUT_DREFCLK;
  3425. /* setup pipeconf */
  3426. pipeconf = I915_READ(pipeconf_reg);
  3427. /* Set up the display plane register */
  3428. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3429. /* Ironlake's plane is forced to pipe, bit 24 is to
  3430. enable color space conversion */
  3431. if (!HAS_PCH_SPLIT(dev)) {
  3432. if (pipe == 0)
  3433. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3434. else
  3435. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3436. }
  3437. if (pipe == 0 && !IS_I965G(dev)) {
  3438. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  3439. * core speed.
  3440. *
  3441. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  3442. * pipe == 0 check?
  3443. */
  3444. if (mode->clock >
  3445. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  3446. pipeconf |= PIPEACONF_DOUBLE_WIDE;
  3447. else
  3448. pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
  3449. }
  3450. dspcntr |= DISPLAY_PLANE_ENABLE;
  3451. pipeconf |= PIPEACONF_ENABLE;
  3452. dpll |= DPLL_VCO_ENABLE;
  3453. /* Disable the panel fitter if it was on our pipe */
  3454. if (!HAS_PCH_SPLIT(dev) && intel_panel_fitter_pipe(dev) == pipe)
  3455. I915_WRITE(PFIT_CONTROL, 0);
  3456. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  3457. drm_mode_debug_printmodeline(mode);
  3458. /* assign to Ironlake registers */
  3459. if (HAS_PCH_SPLIT(dev)) {
  3460. fp_reg = pch_fp_reg;
  3461. dpll_reg = pch_dpll_reg;
  3462. }
  3463. if (is_edp) {
  3464. ironlake_disable_pll_edp(crtc);
  3465. } else if ((dpll & DPLL_VCO_ENABLE)) {
  3466. I915_WRITE(fp_reg, fp);
  3467. I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
  3468. I915_READ(dpll_reg);
  3469. udelay(150);
  3470. }
  3471. /* enable transcoder DPLL */
  3472. if (HAS_PCH_CPT(dev)) {
  3473. temp = I915_READ(PCH_DPLL_SEL);
  3474. if (trans_dpll_sel == 0)
  3475. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  3476. else
  3477. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  3478. I915_WRITE(PCH_DPLL_SEL, temp);
  3479. I915_READ(PCH_DPLL_SEL);
  3480. udelay(150);
  3481. }
  3482. if (HAS_PCH_SPLIT(dev)) {
  3483. pipeconf &= ~PIPE_ENABLE_DITHER;
  3484. pipeconf &= ~PIPE_DITHER_TYPE_MASK;
  3485. }
  3486. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3487. * This is an exception to the general rule that mode_set doesn't turn
  3488. * things on.
  3489. */
  3490. if (is_lvds) {
  3491. u32 lvds;
  3492. if (HAS_PCH_SPLIT(dev))
  3493. lvds_reg = PCH_LVDS;
  3494. lvds = I915_READ(lvds_reg);
  3495. lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  3496. if (pipe == 1) {
  3497. if (HAS_PCH_CPT(dev))
  3498. lvds |= PORT_TRANS_B_SEL_CPT;
  3499. else
  3500. lvds |= LVDS_PIPEB_SELECT;
  3501. } else {
  3502. if (HAS_PCH_CPT(dev))
  3503. lvds &= ~PORT_TRANS_SEL_MASK;
  3504. else
  3505. lvds &= ~LVDS_PIPEB_SELECT;
  3506. }
  3507. /* set the corresponsding LVDS_BORDER bit */
  3508. lvds |= dev_priv->lvds_border_bits;
  3509. /* Set the B0-B3 data pairs corresponding to whether we're going to
  3510. * set the DPLLs for dual-channel mode or not.
  3511. */
  3512. if (clock.p2 == 7)
  3513. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  3514. else
  3515. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  3516. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  3517. * appropriately here, but we need to look more thoroughly into how
  3518. * panels behave in the two modes.
  3519. */
  3520. /* set the dithering flag */
  3521. if (IS_I965G(dev)) {
  3522. if (dev_priv->lvds_dither) {
  3523. if (HAS_PCH_SPLIT(dev)) {
  3524. pipeconf |= PIPE_ENABLE_DITHER;
  3525. pipeconf |= PIPE_DITHER_TYPE_ST01;
  3526. } else
  3527. lvds |= LVDS_ENABLE_DITHER;
  3528. } else {
  3529. if (!HAS_PCH_SPLIT(dev)) {
  3530. lvds &= ~LVDS_ENABLE_DITHER;
  3531. }
  3532. }
  3533. }
  3534. I915_WRITE(lvds_reg, lvds);
  3535. I915_READ(lvds_reg);
  3536. }
  3537. if (is_dp)
  3538. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3539. else if (HAS_PCH_SPLIT(dev)) {
  3540. /* For non-DP output, clear any trans DP clock recovery setting.*/
  3541. if (pipe == 0) {
  3542. I915_WRITE(TRANSA_DATA_M1, 0);
  3543. I915_WRITE(TRANSA_DATA_N1, 0);
  3544. I915_WRITE(TRANSA_DP_LINK_M1, 0);
  3545. I915_WRITE(TRANSA_DP_LINK_N1, 0);
  3546. } else {
  3547. I915_WRITE(TRANSB_DATA_M1, 0);
  3548. I915_WRITE(TRANSB_DATA_N1, 0);
  3549. I915_WRITE(TRANSB_DP_LINK_M1, 0);
  3550. I915_WRITE(TRANSB_DP_LINK_N1, 0);
  3551. }
  3552. }
  3553. if (!is_edp) {
  3554. I915_WRITE(fp_reg, fp);
  3555. I915_WRITE(dpll_reg, dpll);
  3556. I915_READ(dpll_reg);
  3557. /* Wait for the clocks to stabilize. */
  3558. udelay(150);
  3559. if (IS_I965G(dev) && !HAS_PCH_SPLIT(dev)) {
  3560. if (is_sdvo) {
  3561. sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  3562. I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
  3563. ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
  3564. } else
  3565. I915_WRITE(dpll_md_reg, 0);
  3566. } else {
  3567. /* write it again -- the BIOS does, after all */
  3568. I915_WRITE(dpll_reg, dpll);
  3569. }
  3570. I915_READ(dpll_reg);
  3571. /* Wait for the clocks to stabilize. */
  3572. udelay(150);
  3573. }
  3574. if (is_lvds && has_reduced_clock && i915_powersave) {
  3575. I915_WRITE(fp_reg + 4, fp2);
  3576. intel_crtc->lowfreq_avail = true;
  3577. if (HAS_PIPE_CXSR(dev)) {
  3578. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  3579. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  3580. }
  3581. } else {
  3582. I915_WRITE(fp_reg + 4, fp);
  3583. intel_crtc->lowfreq_avail = false;
  3584. if (HAS_PIPE_CXSR(dev)) {
  3585. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  3586. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  3587. }
  3588. }
  3589. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3590. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  3591. /* the chip adds 2 halflines automatically */
  3592. adjusted_mode->crtc_vdisplay -= 1;
  3593. adjusted_mode->crtc_vtotal -= 1;
  3594. adjusted_mode->crtc_vblank_start -= 1;
  3595. adjusted_mode->crtc_vblank_end -= 1;
  3596. adjusted_mode->crtc_vsync_end -= 1;
  3597. adjusted_mode->crtc_vsync_start -= 1;
  3598. } else
  3599. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  3600. I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
  3601. ((adjusted_mode->crtc_htotal - 1) << 16));
  3602. I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
  3603. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3604. I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
  3605. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3606. I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
  3607. ((adjusted_mode->crtc_vtotal - 1) << 16));
  3608. I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
  3609. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  3610. I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
  3611. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3612. /* pipesrc and dspsize control the size that is scaled from, which should
  3613. * always be the user's requested size.
  3614. */
  3615. if (!HAS_PCH_SPLIT(dev)) {
  3616. I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) |
  3617. (mode->hdisplay - 1));
  3618. I915_WRITE(dsppos_reg, 0);
  3619. }
  3620. I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3621. if (HAS_PCH_SPLIT(dev)) {
  3622. I915_WRITE(data_m1_reg, TU_SIZE(m_n.tu) | m_n.gmch_m);
  3623. I915_WRITE(data_n1_reg, TU_SIZE(m_n.tu) | m_n.gmch_n);
  3624. I915_WRITE(link_m1_reg, m_n.link_m);
  3625. I915_WRITE(link_n1_reg, m_n.link_n);
  3626. if (is_edp) {
  3627. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  3628. } else {
  3629. /* enable FDI RX PLL too */
  3630. temp = I915_READ(fdi_rx_reg);
  3631. I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE);
  3632. I915_READ(fdi_rx_reg);
  3633. udelay(200);
  3634. /* enable FDI TX PLL too */
  3635. temp = I915_READ(fdi_tx_reg);
  3636. I915_WRITE(fdi_tx_reg, temp | FDI_TX_PLL_ENABLE);
  3637. I915_READ(fdi_tx_reg);
  3638. /* enable FDI RX PCDCLK */
  3639. temp = I915_READ(fdi_rx_reg);
  3640. I915_WRITE(fdi_rx_reg, temp | FDI_SEL_PCDCLK);
  3641. I915_READ(fdi_rx_reg);
  3642. udelay(200);
  3643. }
  3644. }
  3645. I915_WRITE(pipeconf_reg, pipeconf);
  3646. I915_READ(pipeconf_reg);
  3647. intel_wait_for_vblank(dev);
  3648. if (IS_IRONLAKE(dev)) {
  3649. /* enable address swizzle for tiling buffer */
  3650. temp = I915_READ(DISP_ARB_CTL);
  3651. I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
  3652. }
  3653. I915_WRITE(dspcntr_reg, dspcntr);
  3654. /* Flush the plane changes */
  3655. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  3656. if ((IS_I965G(dev) || plane == 0))
  3657. intel_update_fbc(crtc, &crtc->mode);
  3658. intel_update_watermarks(dev);
  3659. drm_vblank_post_modeset(dev, pipe);
  3660. return ret;
  3661. }
  3662. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3663. void intel_crtc_load_lut(struct drm_crtc *crtc)
  3664. {
  3665. struct drm_device *dev = crtc->dev;
  3666. struct drm_i915_private *dev_priv = dev->dev_private;
  3667. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3668. int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
  3669. int i;
  3670. /* The clocks have to be on to load the palette. */
  3671. if (!crtc->enabled)
  3672. return;
  3673. /* use legacy palette for Ironlake */
  3674. if (HAS_PCH_SPLIT(dev))
  3675. palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
  3676. LGC_PALETTE_B;
  3677. for (i = 0; i < 256; i++) {
  3678. I915_WRITE(palreg + 4 * i,
  3679. (intel_crtc->lut_r[i] << 16) |
  3680. (intel_crtc->lut_g[i] << 8) |
  3681. intel_crtc->lut_b[i]);
  3682. }
  3683. }
  3684. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  3685. static void intel_crtc_update_cursor(struct drm_crtc *crtc)
  3686. {
  3687. struct drm_device *dev = crtc->dev;
  3688. struct drm_i915_private *dev_priv = dev->dev_private;
  3689. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3690. int pipe = intel_crtc->pipe;
  3691. int x = intel_crtc->cursor_x;
  3692. int y = intel_crtc->cursor_y;
  3693. uint32_t base, pos;
  3694. bool visible;
  3695. pos = 0;
  3696. if (intel_crtc->cursor_on && crtc->fb) {
  3697. base = intel_crtc->cursor_addr;
  3698. if (x > (int) crtc->fb->width)
  3699. base = 0;
  3700. if (y > (int) crtc->fb->height)
  3701. base = 0;
  3702. } else
  3703. base = 0;
  3704. if (x < 0) {
  3705. if (x + intel_crtc->cursor_width < 0)
  3706. base = 0;
  3707. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  3708. x = -x;
  3709. }
  3710. pos |= x << CURSOR_X_SHIFT;
  3711. if (y < 0) {
  3712. if (y + intel_crtc->cursor_height < 0)
  3713. base = 0;
  3714. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  3715. y = -y;
  3716. }
  3717. pos |= y << CURSOR_Y_SHIFT;
  3718. visible = base != 0;
  3719. if (!visible && !intel_crtc->cursor_visble)
  3720. return;
  3721. I915_WRITE(pipe == 0 ? CURAPOS : CURBPOS, pos);
  3722. if (intel_crtc->cursor_visble != visible) {
  3723. uint32_t cntl = I915_READ(pipe == 0 ? CURACNTR : CURBCNTR);
  3724. if (base) {
  3725. /* Hooray for CUR*CNTR differences */
  3726. if (IS_MOBILE(dev) || IS_I9XX(dev)) {
  3727. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  3728. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  3729. cntl |= pipe << 28; /* Connect to correct pipe */
  3730. } else {
  3731. cntl &= ~(CURSOR_FORMAT_MASK);
  3732. cntl |= CURSOR_ENABLE;
  3733. cntl |= CURSOR_FORMAT_ARGB | CURSOR_GAMMA_ENABLE;
  3734. }
  3735. } else {
  3736. if (IS_MOBILE(dev) || IS_I9XX(dev)) {
  3737. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  3738. cntl |= CURSOR_MODE_DISABLE;
  3739. } else {
  3740. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  3741. }
  3742. }
  3743. I915_WRITE(pipe == 0 ? CURACNTR : CURBCNTR, cntl);
  3744. intel_crtc->cursor_visble = visible;
  3745. }
  3746. /* and commit changes on next vblank */
  3747. I915_WRITE(pipe == 0 ? CURABASE : CURBBASE, base);
  3748. if (visible)
  3749. intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
  3750. }
  3751. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  3752. struct drm_file *file_priv,
  3753. uint32_t handle,
  3754. uint32_t width, uint32_t height)
  3755. {
  3756. struct drm_device *dev = crtc->dev;
  3757. struct drm_i915_private *dev_priv = dev->dev_private;
  3758. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3759. struct drm_gem_object *bo;
  3760. struct drm_i915_gem_object *obj_priv;
  3761. uint32_t addr;
  3762. int ret;
  3763. DRM_DEBUG_KMS("\n");
  3764. /* if we want to turn off the cursor ignore width and height */
  3765. if (!handle) {
  3766. DRM_DEBUG_KMS("cursor off\n");
  3767. addr = 0;
  3768. bo = NULL;
  3769. mutex_lock(&dev->struct_mutex);
  3770. goto finish;
  3771. }
  3772. /* Currently we only support 64x64 cursors */
  3773. if (width != 64 || height != 64) {
  3774. DRM_ERROR("we currently only support 64x64 cursors\n");
  3775. return -EINVAL;
  3776. }
  3777. bo = drm_gem_object_lookup(dev, file_priv, handle);
  3778. if (!bo)
  3779. return -ENOENT;
  3780. obj_priv = to_intel_bo(bo);
  3781. if (bo->size < width * height * 4) {
  3782. DRM_ERROR("buffer is to small\n");
  3783. ret = -ENOMEM;
  3784. goto fail;
  3785. }
  3786. /* we only need to pin inside GTT if cursor is non-phy */
  3787. mutex_lock(&dev->struct_mutex);
  3788. if (!dev_priv->info->cursor_needs_physical) {
  3789. ret = i915_gem_object_pin(bo, PAGE_SIZE);
  3790. if (ret) {
  3791. DRM_ERROR("failed to pin cursor bo\n");
  3792. goto fail_locked;
  3793. }
  3794. ret = i915_gem_object_set_to_gtt_domain(bo, 0);
  3795. if (ret) {
  3796. DRM_ERROR("failed to move cursor bo into the GTT\n");
  3797. goto fail_unpin;
  3798. }
  3799. addr = obj_priv->gtt_offset;
  3800. } else {
  3801. ret = i915_gem_attach_phys_object(dev, bo,
  3802. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1);
  3803. if (ret) {
  3804. DRM_ERROR("failed to attach phys object\n");
  3805. goto fail_locked;
  3806. }
  3807. addr = obj_priv->phys_obj->handle->busaddr;
  3808. }
  3809. if (!IS_I9XX(dev))
  3810. I915_WRITE(CURSIZE, (height << 12) | width);
  3811. finish:
  3812. if (intel_crtc->cursor_bo) {
  3813. if (dev_priv->info->cursor_needs_physical) {
  3814. if (intel_crtc->cursor_bo != bo)
  3815. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  3816. } else
  3817. i915_gem_object_unpin(intel_crtc->cursor_bo);
  3818. drm_gem_object_unreference(intel_crtc->cursor_bo);
  3819. }
  3820. mutex_unlock(&dev->struct_mutex);
  3821. intel_crtc->cursor_addr = addr;
  3822. intel_crtc->cursor_bo = bo;
  3823. intel_crtc->cursor_width = width;
  3824. intel_crtc->cursor_height = height;
  3825. intel_crtc_update_cursor(crtc);
  3826. return 0;
  3827. fail_unpin:
  3828. i915_gem_object_unpin(bo);
  3829. fail_locked:
  3830. mutex_unlock(&dev->struct_mutex);
  3831. fail:
  3832. drm_gem_object_unreference_unlocked(bo);
  3833. return ret;
  3834. }
  3835. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  3836. {
  3837. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3838. intel_crtc->cursor_x = x;
  3839. intel_crtc->cursor_y = y;
  3840. intel_crtc_update_cursor(crtc);
  3841. return 0;
  3842. }
  3843. /** Sets the color ramps on behalf of RandR */
  3844. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  3845. u16 blue, int regno)
  3846. {
  3847. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3848. intel_crtc->lut_r[regno] = red >> 8;
  3849. intel_crtc->lut_g[regno] = green >> 8;
  3850. intel_crtc->lut_b[regno] = blue >> 8;
  3851. }
  3852. void intel_crtc_fb_gamma_get(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. *red = intel_crtc->lut_r[regno] << 8;
  3857. *green = intel_crtc->lut_g[regno] << 8;
  3858. *blue = intel_crtc->lut_b[regno] << 8;
  3859. }
  3860. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  3861. u16 *blue, uint32_t size)
  3862. {
  3863. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3864. int i;
  3865. if (size != 256)
  3866. return;
  3867. for (i = 0; i < 256; i++) {
  3868. intel_crtc->lut_r[i] = red[i] >> 8;
  3869. intel_crtc->lut_g[i] = green[i] >> 8;
  3870. intel_crtc->lut_b[i] = blue[i] >> 8;
  3871. }
  3872. intel_crtc_load_lut(crtc);
  3873. }
  3874. /**
  3875. * Get a pipe with a simple mode set on it for doing load-based monitor
  3876. * detection.
  3877. *
  3878. * It will be up to the load-detect code to adjust the pipe as appropriate for
  3879. * its requirements. The pipe will be connected to no other encoders.
  3880. *
  3881. * Currently this code will only succeed if there is a pipe with no encoders
  3882. * configured for it. In the future, it could choose to temporarily disable
  3883. * some outputs to free up a pipe for its use.
  3884. *
  3885. * \return crtc, or NULL if no pipes are available.
  3886. */
  3887. /* VESA 640x480x72Hz mode to set on the pipe */
  3888. static struct drm_display_mode load_detect_mode = {
  3889. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  3890. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  3891. };
  3892. struct drm_crtc *intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  3893. struct drm_connector *connector,
  3894. struct drm_display_mode *mode,
  3895. int *dpms_mode)
  3896. {
  3897. struct intel_crtc *intel_crtc;
  3898. struct drm_crtc *possible_crtc;
  3899. struct drm_crtc *supported_crtc =NULL;
  3900. struct drm_encoder *encoder = &intel_encoder->enc;
  3901. struct drm_crtc *crtc = NULL;
  3902. struct drm_device *dev = encoder->dev;
  3903. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  3904. struct drm_crtc_helper_funcs *crtc_funcs;
  3905. int i = -1;
  3906. /*
  3907. * Algorithm gets a little messy:
  3908. * - if the connector already has an assigned crtc, use it (but make
  3909. * sure it's on first)
  3910. * - try to find the first unused crtc that can drive this connector,
  3911. * and use that if we find one
  3912. * - if there are no unused crtcs available, try to use the first
  3913. * one we found that supports the connector
  3914. */
  3915. /* See if we already have a CRTC for this connector */
  3916. if (encoder->crtc) {
  3917. crtc = encoder->crtc;
  3918. /* Make sure the crtc and connector are running */
  3919. intel_crtc = to_intel_crtc(crtc);
  3920. *dpms_mode = intel_crtc->dpms_mode;
  3921. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  3922. crtc_funcs = crtc->helper_private;
  3923. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  3924. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  3925. }
  3926. return crtc;
  3927. }
  3928. /* Find an unused one (if possible) */
  3929. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  3930. i++;
  3931. if (!(encoder->possible_crtcs & (1 << i)))
  3932. continue;
  3933. if (!possible_crtc->enabled) {
  3934. crtc = possible_crtc;
  3935. break;
  3936. }
  3937. if (!supported_crtc)
  3938. supported_crtc = possible_crtc;
  3939. }
  3940. /*
  3941. * If we didn't find an unused CRTC, don't use any.
  3942. */
  3943. if (!crtc) {
  3944. return NULL;
  3945. }
  3946. encoder->crtc = crtc;
  3947. connector->encoder = encoder;
  3948. intel_encoder->load_detect_temp = true;
  3949. intel_crtc = to_intel_crtc(crtc);
  3950. *dpms_mode = intel_crtc->dpms_mode;
  3951. if (!crtc->enabled) {
  3952. if (!mode)
  3953. mode = &load_detect_mode;
  3954. drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
  3955. } else {
  3956. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  3957. crtc_funcs = crtc->helper_private;
  3958. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  3959. }
  3960. /* Add this connector to the crtc */
  3961. encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
  3962. encoder_funcs->commit(encoder);
  3963. }
  3964. /* let the connector get through one full cycle before testing */
  3965. intel_wait_for_vblank(dev);
  3966. return crtc;
  3967. }
  3968. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  3969. struct drm_connector *connector, int dpms_mode)
  3970. {
  3971. struct drm_encoder *encoder = &intel_encoder->enc;
  3972. struct drm_device *dev = encoder->dev;
  3973. struct drm_crtc *crtc = encoder->crtc;
  3974. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  3975. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  3976. if (intel_encoder->load_detect_temp) {
  3977. encoder->crtc = NULL;
  3978. connector->encoder = NULL;
  3979. intel_encoder->load_detect_temp = false;
  3980. crtc->enabled = drm_helper_crtc_in_use(crtc);
  3981. drm_helper_disable_unused_functions(dev);
  3982. }
  3983. /* Switch crtc and encoder back off if necessary */
  3984. if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
  3985. if (encoder->crtc == crtc)
  3986. encoder_funcs->dpms(encoder, dpms_mode);
  3987. crtc_funcs->dpms(crtc, dpms_mode);
  3988. }
  3989. }
  3990. /* Returns the clock of the currently programmed mode of the given pipe. */
  3991. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  3992. {
  3993. struct drm_i915_private *dev_priv = dev->dev_private;
  3994. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3995. int pipe = intel_crtc->pipe;
  3996. u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
  3997. u32 fp;
  3998. intel_clock_t clock;
  3999. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  4000. fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
  4001. else
  4002. fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
  4003. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  4004. if (IS_PINEVIEW(dev)) {
  4005. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  4006. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4007. } else {
  4008. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  4009. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4010. }
  4011. if (IS_I9XX(dev)) {
  4012. if (IS_PINEVIEW(dev))
  4013. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  4014. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  4015. else
  4016. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  4017. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4018. switch (dpll & DPLL_MODE_MASK) {
  4019. case DPLLB_MODE_DAC_SERIAL:
  4020. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  4021. 5 : 10;
  4022. break;
  4023. case DPLLB_MODE_LVDS:
  4024. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  4025. 7 : 14;
  4026. break;
  4027. default:
  4028. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  4029. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  4030. return 0;
  4031. }
  4032. /* XXX: Handle the 100Mhz refclk */
  4033. intel_clock(dev, 96000, &clock);
  4034. } else {
  4035. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  4036. if (is_lvds) {
  4037. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  4038. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4039. clock.p2 = 14;
  4040. if ((dpll & PLL_REF_INPUT_MASK) ==
  4041. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  4042. /* XXX: might not be 66MHz */
  4043. intel_clock(dev, 66000, &clock);
  4044. } else
  4045. intel_clock(dev, 48000, &clock);
  4046. } else {
  4047. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  4048. clock.p1 = 2;
  4049. else {
  4050. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  4051. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  4052. }
  4053. if (dpll & PLL_P2_DIVIDE_BY_4)
  4054. clock.p2 = 4;
  4055. else
  4056. clock.p2 = 2;
  4057. intel_clock(dev, 48000, &clock);
  4058. }
  4059. }
  4060. /* XXX: It would be nice to validate the clocks, but we can't reuse
  4061. * i830PllIsValid() because it relies on the xf86_config connector
  4062. * configuration being accurate, which it isn't necessarily.
  4063. */
  4064. return clock.dot;
  4065. }
  4066. /** Returns the currently programmed mode of the given pipe. */
  4067. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  4068. struct drm_crtc *crtc)
  4069. {
  4070. struct drm_i915_private *dev_priv = dev->dev_private;
  4071. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4072. int pipe = intel_crtc->pipe;
  4073. struct drm_display_mode *mode;
  4074. int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  4075. int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  4076. int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  4077. int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  4078. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  4079. if (!mode)
  4080. return NULL;
  4081. mode->clock = intel_crtc_clock_get(dev, crtc);
  4082. mode->hdisplay = (htot & 0xffff) + 1;
  4083. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  4084. mode->hsync_start = (hsync & 0xffff) + 1;
  4085. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  4086. mode->vdisplay = (vtot & 0xffff) + 1;
  4087. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  4088. mode->vsync_start = (vsync & 0xffff) + 1;
  4089. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  4090. drm_mode_set_name(mode);
  4091. drm_mode_set_crtcinfo(mode, 0);
  4092. return mode;
  4093. }
  4094. #define GPU_IDLE_TIMEOUT 500 /* ms */
  4095. /* When this timer fires, we've been idle for awhile */
  4096. static void intel_gpu_idle_timer(unsigned long arg)
  4097. {
  4098. struct drm_device *dev = (struct drm_device *)arg;
  4099. drm_i915_private_t *dev_priv = dev->dev_private;
  4100. DRM_DEBUG_DRIVER("idle timer fired, downclocking\n");
  4101. dev_priv->busy = false;
  4102. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4103. }
  4104. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  4105. static void intel_crtc_idle_timer(unsigned long arg)
  4106. {
  4107. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  4108. struct drm_crtc *crtc = &intel_crtc->base;
  4109. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  4110. DRM_DEBUG_DRIVER("idle timer fired, downclocking\n");
  4111. intel_crtc->busy = false;
  4112. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4113. }
  4114. static void intel_increase_pllclock(struct drm_crtc *crtc, bool schedule)
  4115. {
  4116. struct drm_device *dev = crtc->dev;
  4117. drm_i915_private_t *dev_priv = dev->dev_private;
  4118. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4119. int pipe = intel_crtc->pipe;
  4120. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  4121. int dpll = I915_READ(dpll_reg);
  4122. if (HAS_PCH_SPLIT(dev))
  4123. return;
  4124. if (!dev_priv->lvds_downclock_avail)
  4125. return;
  4126. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  4127. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  4128. /* Unlock panel regs */
  4129. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  4130. PANEL_UNLOCK_REGS);
  4131. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  4132. I915_WRITE(dpll_reg, dpll);
  4133. dpll = I915_READ(dpll_reg);
  4134. intel_wait_for_vblank(dev);
  4135. dpll = I915_READ(dpll_reg);
  4136. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  4137. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  4138. /* ...and lock them again */
  4139. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  4140. }
  4141. /* Schedule downclock */
  4142. if (schedule)
  4143. mod_timer(&intel_crtc->idle_timer, jiffies +
  4144. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4145. }
  4146. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  4147. {
  4148. struct drm_device *dev = crtc->dev;
  4149. drm_i915_private_t *dev_priv = dev->dev_private;
  4150. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4151. int pipe = intel_crtc->pipe;
  4152. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  4153. int dpll = I915_READ(dpll_reg);
  4154. if (HAS_PCH_SPLIT(dev))
  4155. return;
  4156. if (!dev_priv->lvds_downclock_avail)
  4157. return;
  4158. /*
  4159. * Since this is called by a timer, we should never get here in
  4160. * the manual case.
  4161. */
  4162. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  4163. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  4164. /* Unlock panel regs */
  4165. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  4166. PANEL_UNLOCK_REGS);
  4167. dpll |= DISPLAY_RATE_SELECT_FPA1;
  4168. I915_WRITE(dpll_reg, dpll);
  4169. dpll = I915_READ(dpll_reg);
  4170. intel_wait_for_vblank(dev);
  4171. dpll = I915_READ(dpll_reg);
  4172. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  4173. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  4174. /* ...and lock them again */
  4175. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  4176. }
  4177. }
  4178. /**
  4179. * intel_idle_update - adjust clocks for idleness
  4180. * @work: work struct
  4181. *
  4182. * Either the GPU or display (or both) went idle. Check the busy status
  4183. * here and adjust the CRTC and GPU clocks as necessary.
  4184. */
  4185. static void intel_idle_update(struct work_struct *work)
  4186. {
  4187. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  4188. idle_work);
  4189. struct drm_device *dev = dev_priv->dev;
  4190. struct drm_crtc *crtc;
  4191. struct intel_crtc *intel_crtc;
  4192. int enabled = 0;
  4193. if (!i915_powersave)
  4194. return;
  4195. mutex_lock(&dev->struct_mutex);
  4196. i915_update_gfx_val(dev_priv);
  4197. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4198. /* Skip inactive CRTCs */
  4199. if (!crtc->fb)
  4200. continue;
  4201. enabled++;
  4202. intel_crtc = to_intel_crtc(crtc);
  4203. if (!intel_crtc->busy)
  4204. intel_decrease_pllclock(crtc);
  4205. }
  4206. if ((enabled == 1) && (IS_I945G(dev) || IS_I945GM(dev))) {
  4207. DRM_DEBUG_DRIVER("enable memory self refresh on 945\n");
  4208. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  4209. }
  4210. mutex_unlock(&dev->struct_mutex);
  4211. }
  4212. /**
  4213. * intel_mark_busy - mark the GPU and possibly the display busy
  4214. * @dev: drm device
  4215. * @obj: object we're operating on
  4216. *
  4217. * Callers can use this function to indicate that the GPU is busy processing
  4218. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  4219. * buffer), we'll also mark the display as busy, so we know to increase its
  4220. * clock frequency.
  4221. */
  4222. void intel_mark_busy(struct drm_device *dev, struct drm_gem_object *obj)
  4223. {
  4224. drm_i915_private_t *dev_priv = dev->dev_private;
  4225. struct drm_crtc *crtc = NULL;
  4226. struct intel_framebuffer *intel_fb;
  4227. struct intel_crtc *intel_crtc;
  4228. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4229. return;
  4230. if (!dev_priv->busy) {
  4231. if (IS_I945G(dev) || IS_I945GM(dev)) {
  4232. u32 fw_blc_self;
  4233. DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
  4234. fw_blc_self = I915_READ(FW_BLC_SELF);
  4235. fw_blc_self &= ~FW_BLC_SELF_EN;
  4236. I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
  4237. }
  4238. dev_priv->busy = true;
  4239. } else
  4240. mod_timer(&dev_priv->idle_timer, jiffies +
  4241. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4242. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4243. if (!crtc->fb)
  4244. continue;
  4245. intel_crtc = to_intel_crtc(crtc);
  4246. intel_fb = to_intel_framebuffer(crtc->fb);
  4247. if (intel_fb->obj == obj) {
  4248. if (!intel_crtc->busy) {
  4249. if (IS_I945G(dev) || IS_I945GM(dev)) {
  4250. u32 fw_blc_self;
  4251. DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
  4252. fw_blc_self = I915_READ(FW_BLC_SELF);
  4253. fw_blc_self &= ~FW_BLC_SELF_EN;
  4254. I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
  4255. }
  4256. /* Non-busy -> busy, upclock */
  4257. intel_increase_pllclock(crtc, true);
  4258. intel_crtc->busy = true;
  4259. } else {
  4260. /* Busy -> busy, put off timer */
  4261. mod_timer(&intel_crtc->idle_timer, jiffies +
  4262. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4263. }
  4264. }
  4265. }
  4266. }
  4267. static void intel_crtc_destroy(struct drm_crtc *crtc)
  4268. {
  4269. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4270. drm_crtc_cleanup(crtc);
  4271. kfree(intel_crtc);
  4272. }
  4273. struct intel_unpin_work {
  4274. struct work_struct work;
  4275. struct drm_device *dev;
  4276. struct drm_gem_object *old_fb_obj;
  4277. struct drm_gem_object *pending_flip_obj;
  4278. struct drm_pending_vblank_event *event;
  4279. int pending;
  4280. };
  4281. static void intel_unpin_work_fn(struct work_struct *__work)
  4282. {
  4283. struct intel_unpin_work *work =
  4284. container_of(__work, struct intel_unpin_work, work);
  4285. mutex_lock(&work->dev->struct_mutex);
  4286. i915_gem_object_unpin(work->old_fb_obj);
  4287. drm_gem_object_unreference(work->pending_flip_obj);
  4288. drm_gem_object_unreference(work->old_fb_obj);
  4289. mutex_unlock(&work->dev->struct_mutex);
  4290. kfree(work);
  4291. }
  4292. static void do_intel_finish_page_flip(struct drm_device *dev,
  4293. struct drm_crtc *crtc)
  4294. {
  4295. drm_i915_private_t *dev_priv = dev->dev_private;
  4296. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4297. struct intel_unpin_work *work;
  4298. struct drm_i915_gem_object *obj_priv;
  4299. struct drm_pending_vblank_event *e;
  4300. struct timeval now;
  4301. unsigned long flags;
  4302. /* Ignore early vblank irqs */
  4303. if (intel_crtc == NULL)
  4304. return;
  4305. spin_lock_irqsave(&dev->event_lock, flags);
  4306. work = intel_crtc->unpin_work;
  4307. if (work == NULL || !work->pending) {
  4308. spin_unlock_irqrestore(&dev->event_lock, flags);
  4309. return;
  4310. }
  4311. intel_crtc->unpin_work = NULL;
  4312. drm_vblank_put(dev, intel_crtc->pipe);
  4313. if (work->event) {
  4314. e = work->event;
  4315. do_gettimeofday(&now);
  4316. e->event.sequence = drm_vblank_count(dev, intel_crtc->pipe);
  4317. e->event.tv_sec = now.tv_sec;
  4318. e->event.tv_usec = now.tv_usec;
  4319. list_add_tail(&e->base.link,
  4320. &e->base.file_priv->event_list);
  4321. wake_up_interruptible(&e->base.file_priv->event_wait);
  4322. }
  4323. spin_unlock_irqrestore(&dev->event_lock, flags);
  4324. obj_priv = to_intel_bo(work->pending_flip_obj);
  4325. /* Initial scanout buffer will have a 0 pending flip count */
  4326. if ((atomic_read(&obj_priv->pending_flip) == 0) ||
  4327. atomic_dec_and_test(&obj_priv->pending_flip))
  4328. DRM_WAKEUP(&dev_priv->pending_flip_queue);
  4329. schedule_work(&work->work);
  4330. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  4331. }
  4332. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  4333. {
  4334. drm_i915_private_t *dev_priv = dev->dev_private;
  4335. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  4336. do_intel_finish_page_flip(dev, crtc);
  4337. }
  4338. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  4339. {
  4340. drm_i915_private_t *dev_priv = dev->dev_private;
  4341. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  4342. do_intel_finish_page_flip(dev, crtc);
  4343. }
  4344. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  4345. {
  4346. drm_i915_private_t *dev_priv = dev->dev_private;
  4347. struct intel_crtc *intel_crtc =
  4348. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  4349. unsigned long flags;
  4350. spin_lock_irqsave(&dev->event_lock, flags);
  4351. if (intel_crtc->unpin_work) {
  4352. intel_crtc->unpin_work->pending = 1;
  4353. } else {
  4354. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  4355. }
  4356. spin_unlock_irqrestore(&dev->event_lock, flags);
  4357. }
  4358. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  4359. struct drm_framebuffer *fb,
  4360. struct drm_pending_vblank_event *event)
  4361. {
  4362. struct drm_device *dev = crtc->dev;
  4363. struct drm_i915_private *dev_priv = dev->dev_private;
  4364. struct intel_framebuffer *intel_fb;
  4365. struct drm_i915_gem_object *obj_priv;
  4366. struct drm_gem_object *obj;
  4367. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4368. struct intel_unpin_work *work;
  4369. unsigned long flags, offset;
  4370. int pipesrc_reg = (intel_crtc->pipe == 0) ? PIPEASRC : PIPEBSRC;
  4371. int ret, pipesrc;
  4372. u32 flip_mask;
  4373. work = kzalloc(sizeof *work, GFP_KERNEL);
  4374. if (work == NULL)
  4375. return -ENOMEM;
  4376. work->event = event;
  4377. work->dev = crtc->dev;
  4378. intel_fb = to_intel_framebuffer(crtc->fb);
  4379. work->old_fb_obj = intel_fb->obj;
  4380. INIT_WORK(&work->work, intel_unpin_work_fn);
  4381. /* We borrow the event spin lock for protecting unpin_work */
  4382. spin_lock_irqsave(&dev->event_lock, flags);
  4383. if (intel_crtc->unpin_work) {
  4384. spin_unlock_irqrestore(&dev->event_lock, flags);
  4385. kfree(work);
  4386. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  4387. return -EBUSY;
  4388. }
  4389. intel_crtc->unpin_work = work;
  4390. spin_unlock_irqrestore(&dev->event_lock, flags);
  4391. intel_fb = to_intel_framebuffer(fb);
  4392. obj = intel_fb->obj;
  4393. mutex_lock(&dev->struct_mutex);
  4394. ret = intel_pin_and_fence_fb_obj(dev, obj);
  4395. if (ret)
  4396. goto cleanup_work;
  4397. /* Reference the objects for the scheduled work. */
  4398. drm_gem_object_reference(work->old_fb_obj);
  4399. drm_gem_object_reference(obj);
  4400. crtc->fb = fb;
  4401. ret = i915_gem_object_flush_write_domain(obj);
  4402. if (ret)
  4403. goto cleanup_objs;
  4404. ret = drm_vblank_get(dev, intel_crtc->pipe);
  4405. if (ret)
  4406. goto cleanup_objs;
  4407. obj_priv = to_intel_bo(obj);
  4408. atomic_inc(&obj_priv->pending_flip);
  4409. work->pending_flip_obj = obj;
  4410. if (intel_crtc->plane)
  4411. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  4412. else
  4413. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  4414. if (IS_GEN3(dev) || IS_GEN2(dev)) {
  4415. BEGIN_LP_RING(2);
  4416. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  4417. OUT_RING(0);
  4418. ADVANCE_LP_RING();
  4419. }
  4420. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  4421. offset = obj_priv->gtt_offset;
  4422. offset += (crtc->y * fb->pitch) + (crtc->x * (fb->bits_per_pixel) / 8);
  4423. BEGIN_LP_RING(4);
  4424. if (IS_I965G(dev)) {
  4425. OUT_RING(MI_DISPLAY_FLIP |
  4426. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4427. OUT_RING(fb->pitch);
  4428. OUT_RING(offset | obj_priv->tiling_mode);
  4429. pipesrc = I915_READ(pipesrc_reg);
  4430. OUT_RING(pipesrc & 0x0fff0fff);
  4431. } else if (IS_GEN3(dev)) {
  4432. OUT_RING(MI_DISPLAY_FLIP_I915 |
  4433. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4434. OUT_RING(fb->pitch);
  4435. OUT_RING(offset);
  4436. OUT_RING(MI_NOOP);
  4437. } else {
  4438. OUT_RING(MI_DISPLAY_FLIP |
  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. /* 100ms RC evaluation intervals */
  4748. I915_WRITE(RCUPEI, 100000);
  4749. I915_WRITE(RCDNEI, 100000);
  4750. /* Set max/min thresholds to 90ms and 80ms respectively */
  4751. I915_WRITE(RCBMAXAVG, 90000);
  4752. I915_WRITE(RCBMINAVG, 80000);
  4753. I915_WRITE(MEMIHYST, 1);
  4754. /* Set up min, max, and cur for interrupt handling */
  4755. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  4756. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  4757. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  4758. MEMMODE_FSTART_SHIFT;
  4759. fstart = fmax;
  4760. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  4761. PXVFREQ_PX_SHIFT;
  4762. dev_priv->fmax = fstart; /* IPS callback will increase this */
  4763. dev_priv->fstart = fstart;
  4764. dev_priv->max_delay = fmax;
  4765. dev_priv->min_delay = fmin;
  4766. dev_priv->cur_delay = fstart;
  4767. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n", fmax, fmin,
  4768. fstart);
  4769. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  4770. /*
  4771. * Interrupts will be enabled in ironlake_irq_postinstall
  4772. */
  4773. I915_WRITE(VIDSTART, vstart);
  4774. POSTING_READ(VIDSTART);
  4775. rgvmodectl |= MEMMODE_SWMODE_EN;
  4776. I915_WRITE(MEMMODECTL, rgvmodectl);
  4777. if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 1, 0))
  4778. DRM_ERROR("stuck trying to change perf mode\n");
  4779. msleep(1);
  4780. ironlake_set_drps(dev, fstart);
  4781. dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  4782. I915_READ(0x112e0);
  4783. dev_priv->last_time1 = jiffies_to_msecs(jiffies);
  4784. dev_priv->last_count2 = I915_READ(0x112f4);
  4785. getrawmonotonic(&dev_priv->last_time2);
  4786. }
  4787. void ironlake_disable_drps(struct drm_device *dev)
  4788. {
  4789. struct drm_i915_private *dev_priv = dev->dev_private;
  4790. u16 rgvswctl = I915_READ16(MEMSWCTL);
  4791. /* Ack interrupts, disable EFC interrupt */
  4792. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  4793. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  4794. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  4795. I915_WRITE(DEIIR, DE_PCU_EVENT);
  4796. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  4797. /* Go back to the starting frequency */
  4798. ironlake_set_drps(dev, dev_priv->fstart);
  4799. msleep(1);
  4800. rgvswctl |= MEMCTL_CMD_STS;
  4801. I915_WRITE(MEMSWCTL, rgvswctl);
  4802. msleep(1);
  4803. }
  4804. static unsigned long intel_pxfreq(u32 vidfreq)
  4805. {
  4806. unsigned long freq;
  4807. int div = (vidfreq & 0x3f0000) >> 16;
  4808. int post = (vidfreq & 0x3000) >> 12;
  4809. int pre = (vidfreq & 0x7);
  4810. if (!pre)
  4811. return 0;
  4812. freq = ((div * 133333) / ((1<<post) * pre));
  4813. return freq;
  4814. }
  4815. void intel_init_emon(struct drm_device *dev)
  4816. {
  4817. struct drm_i915_private *dev_priv = dev->dev_private;
  4818. u32 lcfuse;
  4819. u8 pxw[16];
  4820. int i;
  4821. /* Disable to program */
  4822. I915_WRITE(ECR, 0);
  4823. POSTING_READ(ECR);
  4824. /* Program energy weights for various events */
  4825. I915_WRITE(SDEW, 0x15040d00);
  4826. I915_WRITE(CSIEW0, 0x007f0000);
  4827. I915_WRITE(CSIEW1, 0x1e220004);
  4828. I915_WRITE(CSIEW2, 0x04000004);
  4829. for (i = 0; i < 5; i++)
  4830. I915_WRITE(PEW + (i * 4), 0);
  4831. for (i = 0; i < 3; i++)
  4832. I915_WRITE(DEW + (i * 4), 0);
  4833. /* Program P-state weights to account for frequency power adjustment */
  4834. for (i = 0; i < 16; i++) {
  4835. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  4836. unsigned long freq = intel_pxfreq(pxvidfreq);
  4837. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  4838. PXVFREQ_PX_SHIFT;
  4839. unsigned long val;
  4840. val = vid * vid;
  4841. val *= (freq / 1000);
  4842. val *= 255;
  4843. val /= (127*127*900);
  4844. if (val > 0xff)
  4845. DRM_ERROR("bad pxval: %ld\n", val);
  4846. pxw[i] = val;
  4847. }
  4848. /* Render standby states get 0 weight */
  4849. pxw[14] = 0;
  4850. pxw[15] = 0;
  4851. for (i = 0; i < 4; i++) {
  4852. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  4853. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  4854. I915_WRITE(PXW + (i * 4), val);
  4855. }
  4856. /* Adjust magic regs to magic values (more experimental results) */
  4857. I915_WRITE(OGW0, 0);
  4858. I915_WRITE(OGW1, 0);
  4859. I915_WRITE(EG0, 0x00007f00);
  4860. I915_WRITE(EG1, 0x0000000e);
  4861. I915_WRITE(EG2, 0x000e0000);
  4862. I915_WRITE(EG3, 0x68000300);
  4863. I915_WRITE(EG4, 0x42000000);
  4864. I915_WRITE(EG5, 0x00140031);
  4865. I915_WRITE(EG6, 0);
  4866. I915_WRITE(EG7, 0);
  4867. for (i = 0; i < 8; i++)
  4868. I915_WRITE(PXWL + (i * 4), 0);
  4869. /* Enable PMON + select events */
  4870. I915_WRITE(ECR, 0x80000019);
  4871. lcfuse = I915_READ(LCFUSE02);
  4872. dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
  4873. }
  4874. void intel_init_clock_gating(struct drm_device *dev)
  4875. {
  4876. struct drm_i915_private *dev_priv = dev->dev_private;
  4877. /*
  4878. * Disable clock gating reported to work incorrectly according to the
  4879. * specs, but enable as much else as we can.
  4880. */
  4881. if (HAS_PCH_SPLIT(dev)) {
  4882. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  4883. if (IS_IRONLAKE(dev)) {
  4884. /* Required for FBC */
  4885. dspclk_gate |= DPFDUNIT_CLOCK_GATE_DISABLE;
  4886. /* Required for CxSR */
  4887. dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
  4888. I915_WRITE(PCH_3DCGDIS0,
  4889. MARIUNIT_CLOCK_GATE_DISABLE |
  4890. SVSMUNIT_CLOCK_GATE_DISABLE);
  4891. }
  4892. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  4893. /*
  4894. * According to the spec the following bits should be set in
  4895. * order to enable memory self-refresh
  4896. * The bit 22/21 of 0x42004
  4897. * The bit 5 of 0x42020
  4898. * The bit 15 of 0x45000
  4899. */
  4900. if (IS_IRONLAKE(dev)) {
  4901. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  4902. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  4903. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  4904. I915_WRITE(ILK_DSPCLK_GATE,
  4905. (I915_READ(ILK_DSPCLK_GATE) |
  4906. ILK_DPARB_CLK_GATE));
  4907. I915_WRITE(DISP_ARB_CTL,
  4908. (I915_READ(DISP_ARB_CTL) |
  4909. DISP_FBC_WM_DIS));
  4910. }
  4911. /*
  4912. * Based on the document from hardware guys the following bits
  4913. * should be set unconditionally in order to enable FBC.
  4914. * The bit 22 of 0x42000
  4915. * The bit 22 of 0x42004
  4916. * The bit 7,8,9 of 0x42020.
  4917. */
  4918. if (IS_IRONLAKE_M(dev)) {
  4919. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  4920. I915_READ(ILK_DISPLAY_CHICKEN1) |
  4921. ILK_FBCQ_DIS);
  4922. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  4923. I915_READ(ILK_DISPLAY_CHICKEN2) |
  4924. ILK_DPARB_GATE);
  4925. I915_WRITE(ILK_DSPCLK_GATE,
  4926. I915_READ(ILK_DSPCLK_GATE) |
  4927. ILK_DPFC_DIS1 |
  4928. ILK_DPFC_DIS2 |
  4929. ILK_CLK_FBC);
  4930. }
  4931. return;
  4932. } else if (IS_G4X(dev)) {
  4933. uint32_t dspclk_gate;
  4934. I915_WRITE(RENCLK_GATE_D1, 0);
  4935. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  4936. GS_UNIT_CLOCK_GATE_DISABLE |
  4937. CL_UNIT_CLOCK_GATE_DISABLE);
  4938. I915_WRITE(RAMCLK_GATE_D, 0);
  4939. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  4940. OVRUNIT_CLOCK_GATE_DISABLE |
  4941. OVCUNIT_CLOCK_GATE_DISABLE;
  4942. if (IS_GM45(dev))
  4943. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  4944. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  4945. } else if (IS_I965GM(dev)) {
  4946. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  4947. I915_WRITE(RENCLK_GATE_D2, 0);
  4948. I915_WRITE(DSPCLK_GATE_D, 0);
  4949. I915_WRITE(RAMCLK_GATE_D, 0);
  4950. I915_WRITE16(DEUC, 0);
  4951. } else if (IS_I965G(dev)) {
  4952. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  4953. I965_RCC_CLOCK_GATE_DISABLE |
  4954. I965_RCPB_CLOCK_GATE_DISABLE |
  4955. I965_ISC_CLOCK_GATE_DISABLE |
  4956. I965_FBC_CLOCK_GATE_DISABLE);
  4957. I915_WRITE(RENCLK_GATE_D2, 0);
  4958. } else if (IS_I9XX(dev)) {
  4959. u32 dstate = I915_READ(D_STATE);
  4960. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  4961. DSTATE_DOT_CLOCK_GATING;
  4962. I915_WRITE(D_STATE, dstate);
  4963. } else if (IS_I85X(dev) || IS_I865G(dev)) {
  4964. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  4965. } else if (IS_I830(dev)) {
  4966. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  4967. }
  4968. /*
  4969. * GPU can automatically power down the render unit if given a page
  4970. * to save state.
  4971. */
  4972. if (I915_HAS_RC6(dev) && drm_core_check_feature(dev, DRIVER_MODESET)) {
  4973. struct drm_i915_gem_object *obj_priv = NULL;
  4974. if (dev_priv->pwrctx) {
  4975. obj_priv = to_intel_bo(dev_priv->pwrctx);
  4976. } else {
  4977. struct drm_gem_object *pwrctx;
  4978. pwrctx = intel_alloc_power_context(dev);
  4979. if (pwrctx) {
  4980. dev_priv->pwrctx = pwrctx;
  4981. obj_priv = to_intel_bo(pwrctx);
  4982. }
  4983. }
  4984. if (obj_priv) {
  4985. I915_WRITE(PWRCTXA, obj_priv->gtt_offset | PWRCTX_EN);
  4986. I915_WRITE(MCHBAR_RENDER_STANDBY,
  4987. I915_READ(MCHBAR_RENDER_STANDBY) & ~RCX_SW_EXIT);
  4988. }
  4989. }
  4990. }
  4991. /* Set up chip specific display functions */
  4992. static void intel_init_display(struct drm_device *dev)
  4993. {
  4994. struct drm_i915_private *dev_priv = dev->dev_private;
  4995. /* We always want a DPMS function */
  4996. if (HAS_PCH_SPLIT(dev))
  4997. dev_priv->display.dpms = ironlake_crtc_dpms;
  4998. else
  4999. dev_priv->display.dpms = i9xx_crtc_dpms;
  5000. if (I915_HAS_FBC(dev)) {
  5001. if (IS_IRONLAKE_M(dev)) {
  5002. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  5003. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  5004. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  5005. } else if (IS_GM45(dev)) {
  5006. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  5007. dev_priv->display.enable_fbc = g4x_enable_fbc;
  5008. dev_priv->display.disable_fbc = g4x_disable_fbc;
  5009. } else if (IS_I965GM(dev)) {
  5010. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  5011. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  5012. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  5013. }
  5014. /* 855GM needs testing */
  5015. }
  5016. /* Returns the core display clock speed */
  5017. if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
  5018. dev_priv->display.get_display_clock_speed =
  5019. i945_get_display_clock_speed;
  5020. else if (IS_I915G(dev))
  5021. dev_priv->display.get_display_clock_speed =
  5022. i915_get_display_clock_speed;
  5023. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  5024. dev_priv->display.get_display_clock_speed =
  5025. i9xx_misc_get_display_clock_speed;
  5026. else if (IS_I915GM(dev))
  5027. dev_priv->display.get_display_clock_speed =
  5028. i915gm_get_display_clock_speed;
  5029. else if (IS_I865G(dev))
  5030. dev_priv->display.get_display_clock_speed =
  5031. i865_get_display_clock_speed;
  5032. else if (IS_I85X(dev))
  5033. dev_priv->display.get_display_clock_speed =
  5034. i855_get_display_clock_speed;
  5035. else /* 852, 830 */
  5036. dev_priv->display.get_display_clock_speed =
  5037. i830_get_display_clock_speed;
  5038. /* For FIFO watermark updates */
  5039. if (HAS_PCH_SPLIT(dev)) {
  5040. if (IS_IRONLAKE(dev)) {
  5041. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  5042. dev_priv->display.update_wm = ironlake_update_wm;
  5043. else {
  5044. DRM_DEBUG_KMS("Failed to get proper latency. "
  5045. "Disable CxSR\n");
  5046. dev_priv->display.update_wm = NULL;
  5047. }
  5048. } else
  5049. dev_priv->display.update_wm = NULL;
  5050. } else if (IS_PINEVIEW(dev)) {
  5051. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  5052. dev_priv->is_ddr3,
  5053. dev_priv->fsb_freq,
  5054. dev_priv->mem_freq)) {
  5055. DRM_INFO("failed to find known CxSR latency "
  5056. "(found ddr%s fsb freq %d, mem freq %d), "
  5057. "disabling CxSR\n",
  5058. (dev_priv->is_ddr3 == 1) ? "3": "2",
  5059. dev_priv->fsb_freq, dev_priv->mem_freq);
  5060. /* Disable CxSR and never update its watermark again */
  5061. pineview_disable_cxsr(dev);
  5062. dev_priv->display.update_wm = NULL;
  5063. } else
  5064. dev_priv->display.update_wm = pineview_update_wm;
  5065. } else if (IS_G4X(dev))
  5066. dev_priv->display.update_wm = g4x_update_wm;
  5067. else if (IS_I965G(dev))
  5068. dev_priv->display.update_wm = i965_update_wm;
  5069. else if (IS_I9XX(dev)) {
  5070. dev_priv->display.update_wm = i9xx_update_wm;
  5071. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  5072. } else if (IS_I85X(dev)) {
  5073. dev_priv->display.update_wm = i9xx_update_wm;
  5074. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  5075. } else {
  5076. dev_priv->display.update_wm = i830_update_wm;
  5077. if (IS_845G(dev))
  5078. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  5079. else
  5080. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  5081. }
  5082. }
  5083. /*
  5084. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  5085. * resume, or other times. This quirk makes sure that's the case for
  5086. * affected systems.
  5087. */
  5088. static void quirk_pipea_force (struct drm_device *dev)
  5089. {
  5090. struct drm_i915_private *dev_priv = dev->dev_private;
  5091. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  5092. DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
  5093. }
  5094. struct intel_quirk {
  5095. int device;
  5096. int subsystem_vendor;
  5097. int subsystem_device;
  5098. void (*hook)(struct drm_device *dev);
  5099. };
  5100. struct intel_quirk intel_quirks[] = {
  5101. /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
  5102. { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
  5103. /* HP Mini needs pipe A force quirk (LP: #322104) */
  5104. { 0x27ae,0x103c, 0x361a, quirk_pipea_force },
  5105. /* Thinkpad R31 needs pipe A force quirk */
  5106. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  5107. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  5108. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  5109. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  5110. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  5111. /* ThinkPad X40 needs pipe A force quirk */
  5112. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  5113. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  5114. /* 855 & before need to leave pipe A & dpll A up */
  5115. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5116. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5117. };
  5118. static void intel_init_quirks(struct drm_device *dev)
  5119. {
  5120. struct pci_dev *d = dev->pdev;
  5121. int i;
  5122. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  5123. struct intel_quirk *q = &intel_quirks[i];
  5124. if (d->device == q->device &&
  5125. (d->subsystem_vendor == q->subsystem_vendor ||
  5126. q->subsystem_vendor == PCI_ANY_ID) &&
  5127. (d->subsystem_device == q->subsystem_device ||
  5128. q->subsystem_device == PCI_ANY_ID))
  5129. q->hook(dev);
  5130. }
  5131. }
  5132. void intel_modeset_init(struct drm_device *dev)
  5133. {
  5134. struct drm_i915_private *dev_priv = dev->dev_private;
  5135. int i;
  5136. drm_mode_config_init(dev);
  5137. dev->mode_config.min_width = 0;
  5138. dev->mode_config.min_height = 0;
  5139. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  5140. intel_init_quirks(dev);
  5141. intel_init_display(dev);
  5142. if (IS_I965G(dev)) {
  5143. dev->mode_config.max_width = 8192;
  5144. dev->mode_config.max_height = 8192;
  5145. } else if (IS_I9XX(dev)) {
  5146. dev->mode_config.max_width = 4096;
  5147. dev->mode_config.max_height = 4096;
  5148. } else {
  5149. dev->mode_config.max_width = 2048;
  5150. dev->mode_config.max_height = 2048;
  5151. }
  5152. /* set memory base */
  5153. if (IS_I9XX(dev))
  5154. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
  5155. else
  5156. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
  5157. if (IS_MOBILE(dev) || IS_I9XX(dev))
  5158. dev_priv->num_pipe = 2;
  5159. else
  5160. dev_priv->num_pipe = 1;
  5161. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  5162. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  5163. for (i = 0; i < dev_priv->num_pipe; i++) {
  5164. intel_crtc_init(dev, i);
  5165. }
  5166. intel_setup_outputs(dev);
  5167. intel_init_clock_gating(dev);
  5168. if (IS_IRONLAKE_M(dev)) {
  5169. ironlake_enable_drps(dev);
  5170. intel_init_emon(dev);
  5171. }
  5172. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  5173. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  5174. (unsigned long)dev);
  5175. intel_setup_overlay(dev);
  5176. }
  5177. void intel_modeset_cleanup(struct drm_device *dev)
  5178. {
  5179. struct drm_i915_private *dev_priv = dev->dev_private;
  5180. struct drm_crtc *crtc;
  5181. struct intel_crtc *intel_crtc;
  5182. mutex_lock(&dev->struct_mutex);
  5183. drm_kms_helper_poll_fini(dev);
  5184. intel_fbdev_fini(dev);
  5185. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5186. /* Skip inactive CRTCs */
  5187. if (!crtc->fb)
  5188. continue;
  5189. intel_crtc = to_intel_crtc(crtc);
  5190. intel_increase_pllclock(crtc, false);
  5191. del_timer_sync(&intel_crtc->idle_timer);
  5192. }
  5193. del_timer_sync(&dev_priv->idle_timer);
  5194. if (dev_priv->display.disable_fbc)
  5195. dev_priv->display.disable_fbc(dev);
  5196. if (dev_priv->pwrctx) {
  5197. struct drm_i915_gem_object *obj_priv;
  5198. obj_priv = to_intel_bo(dev_priv->pwrctx);
  5199. I915_WRITE(PWRCTXA, obj_priv->gtt_offset &~ PWRCTX_EN);
  5200. I915_READ(PWRCTXA);
  5201. i915_gem_object_unpin(dev_priv->pwrctx);
  5202. drm_gem_object_unreference(dev_priv->pwrctx);
  5203. }
  5204. if (IS_IRONLAKE_M(dev))
  5205. ironlake_disable_drps(dev);
  5206. mutex_unlock(&dev->struct_mutex);
  5207. drm_mode_config_cleanup(dev);
  5208. }
  5209. /*
  5210. * Return which encoder is currently attached for connector.
  5211. */
  5212. struct drm_encoder *intel_attached_encoder (struct drm_connector *connector)
  5213. {
  5214. struct drm_mode_object *obj;
  5215. struct drm_encoder *encoder;
  5216. int i;
  5217. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  5218. if (connector->encoder_ids[i] == 0)
  5219. break;
  5220. obj = drm_mode_object_find(connector->dev,
  5221. connector->encoder_ids[i],
  5222. DRM_MODE_OBJECT_ENCODER);
  5223. if (!obj)
  5224. continue;
  5225. encoder = obj_to_encoder(obj);
  5226. return encoder;
  5227. }
  5228. return NULL;
  5229. }
  5230. /*
  5231. * set vga decode state - true == enable VGA decode
  5232. */
  5233. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  5234. {
  5235. struct drm_i915_private *dev_priv = dev->dev_private;
  5236. u16 gmch_ctrl;
  5237. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  5238. if (state)
  5239. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  5240. else
  5241. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  5242. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  5243. return 0;
  5244. }