intel_display.c 176 KB

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