intel_display.c 181 KB

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