intel_display.c 179 KB

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