intel_display.c 175 KB

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