intel_display.c 195 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922
  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. int refclk)
  602. {
  603. struct drm_device *dev = crtc->dev;
  604. struct drm_i915_private *dev_priv = dev->dev_private;
  605. const intel_limit_t *limit;
  606. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  607. if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
  608. LVDS_CLKB_POWER_UP) {
  609. /* LVDS dual channel */
  610. if (refclk == 100000)
  611. limit = &intel_limits_ironlake_dual_lvds_100m;
  612. else
  613. limit = &intel_limits_ironlake_dual_lvds;
  614. } else {
  615. if (refclk == 100000)
  616. limit = &intel_limits_ironlake_single_lvds_100m;
  617. else
  618. limit = &intel_limits_ironlake_single_lvds;
  619. }
  620. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  621. HAS_eDP)
  622. limit = &intel_limits_ironlake_display_port;
  623. else
  624. limit = &intel_limits_ironlake_dac;
  625. return limit;
  626. }
  627. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  628. {
  629. struct drm_device *dev = crtc->dev;
  630. struct drm_i915_private *dev_priv = dev->dev_private;
  631. const intel_limit_t *limit;
  632. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  633. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  634. LVDS_CLKB_POWER_UP)
  635. /* LVDS with dual channel */
  636. limit = &intel_limits_g4x_dual_channel_lvds;
  637. else
  638. /* LVDS with dual channel */
  639. limit = &intel_limits_g4x_single_channel_lvds;
  640. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  641. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  642. limit = &intel_limits_g4x_hdmi;
  643. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  644. limit = &intel_limits_g4x_sdvo;
  645. } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  646. limit = &intel_limits_g4x_display_port;
  647. } else /* The option is for other outputs */
  648. limit = &intel_limits_i9xx_sdvo;
  649. return limit;
  650. }
  651. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  652. {
  653. struct drm_device *dev = crtc->dev;
  654. const intel_limit_t *limit;
  655. if (HAS_PCH_SPLIT(dev))
  656. limit = intel_ironlake_limit(crtc, refclk);
  657. else if (IS_G4X(dev)) {
  658. limit = intel_g4x_limit(crtc);
  659. } else if (IS_PINEVIEW(dev)) {
  660. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  661. limit = &intel_limits_pineview_lvds;
  662. else
  663. limit = &intel_limits_pineview_sdvo;
  664. } else if (!IS_GEN2(dev)) {
  665. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  666. limit = &intel_limits_i9xx_lvds;
  667. else
  668. limit = &intel_limits_i9xx_sdvo;
  669. } else {
  670. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  671. limit = &intel_limits_i8xx_lvds;
  672. else
  673. limit = &intel_limits_i8xx_dvo;
  674. }
  675. return limit;
  676. }
  677. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  678. static void pineview_clock(int refclk, intel_clock_t *clock)
  679. {
  680. clock->m = clock->m2 + 2;
  681. clock->p = clock->p1 * clock->p2;
  682. clock->vco = refclk * clock->m / clock->n;
  683. clock->dot = clock->vco / clock->p;
  684. }
  685. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  686. {
  687. if (IS_PINEVIEW(dev)) {
  688. pineview_clock(refclk, clock);
  689. return;
  690. }
  691. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  692. clock->p = clock->p1 * clock->p2;
  693. clock->vco = refclk * clock->m / (clock->n + 2);
  694. clock->dot = clock->vco / clock->p;
  695. }
  696. /**
  697. * Returns whether any output on the specified pipe is of the specified type
  698. */
  699. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  700. {
  701. struct drm_device *dev = crtc->dev;
  702. struct drm_mode_config *mode_config = &dev->mode_config;
  703. struct intel_encoder *encoder;
  704. list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
  705. if (encoder->base.crtc == crtc && encoder->type == type)
  706. return true;
  707. return false;
  708. }
  709. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  710. /**
  711. * Returns whether the given set of divisors are valid for a given refclk with
  712. * the given connectors.
  713. */
  714. static bool intel_PLL_is_valid(struct drm_device *dev,
  715. const intel_limit_t *limit,
  716. const intel_clock_t *clock)
  717. {
  718. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  719. INTELPllInvalid ("p1 out of range\n");
  720. if (clock->p < limit->p.min || limit->p.max < clock->p)
  721. INTELPllInvalid ("p out of range\n");
  722. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  723. INTELPllInvalid ("m2 out of range\n");
  724. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  725. INTELPllInvalid ("m1 out of range\n");
  726. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  727. INTELPllInvalid ("m1 <= m2\n");
  728. if (clock->m < limit->m.min || limit->m.max < clock->m)
  729. INTELPllInvalid ("m out of range\n");
  730. if (clock->n < limit->n.min || limit->n.max < clock->n)
  731. INTELPllInvalid ("n out of range\n");
  732. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  733. INTELPllInvalid ("vco out of range\n");
  734. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  735. * connector, etc., rather than just a single range.
  736. */
  737. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  738. INTELPllInvalid ("dot out of range\n");
  739. return true;
  740. }
  741. static bool
  742. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  743. int target, int refclk, intel_clock_t *best_clock)
  744. {
  745. struct drm_device *dev = crtc->dev;
  746. struct drm_i915_private *dev_priv = dev->dev_private;
  747. intel_clock_t clock;
  748. int err = target;
  749. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  750. (I915_READ(LVDS)) != 0) {
  751. /*
  752. * For LVDS, if the panel is on, just rely on its current
  753. * settings for dual-channel. We haven't figured out how to
  754. * reliably set up different single/dual channel state, if we
  755. * even can.
  756. */
  757. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  758. LVDS_CLKB_POWER_UP)
  759. clock.p2 = limit->p2.p2_fast;
  760. else
  761. clock.p2 = limit->p2.p2_slow;
  762. } else {
  763. if (target < limit->p2.dot_limit)
  764. clock.p2 = limit->p2.p2_slow;
  765. else
  766. clock.p2 = limit->p2.p2_fast;
  767. }
  768. memset (best_clock, 0, sizeof (*best_clock));
  769. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  770. clock.m1++) {
  771. for (clock.m2 = limit->m2.min;
  772. clock.m2 <= limit->m2.max; clock.m2++) {
  773. /* m1 is always 0 in Pineview */
  774. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  775. break;
  776. for (clock.n = limit->n.min;
  777. clock.n <= limit->n.max; clock.n++) {
  778. for (clock.p1 = limit->p1.min;
  779. clock.p1 <= limit->p1.max; clock.p1++) {
  780. int this_err;
  781. intel_clock(dev, refclk, &clock);
  782. if (!intel_PLL_is_valid(dev, limit,
  783. &clock))
  784. continue;
  785. this_err = abs(clock.dot - target);
  786. if (this_err < err) {
  787. *best_clock = clock;
  788. err = this_err;
  789. }
  790. }
  791. }
  792. }
  793. }
  794. return (err != target);
  795. }
  796. static bool
  797. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  798. int target, int refclk, intel_clock_t *best_clock)
  799. {
  800. struct drm_device *dev = crtc->dev;
  801. struct drm_i915_private *dev_priv = dev->dev_private;
  802. intel_clock_t clock;
  803. int max_n;
  804. bool found;
  805. /* approximately equals target * 0.00585 */
  806. int err_most = (target >> 8) + (target >> 9);
  807. found = false;
  808. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  809. int lvds_reg;
  810. if (HAS_PCH_SPLIT(dev))
  811. lvds_reg = PCH_LVDS;
  812. else
  813. lvds_reg = LVDS;
  814. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  815. LVDS_CLKB_POWER_UP)
  816. clock.p2 = limit->p2.p2_fast;
  817. else
  818. clock.p2 = limit->p2.p2_slow;
  819. } else {
  820. if (target < limit->p2.dot_limit)
  821. clock.p2 = limit->p2.p2_slow;
  822. else
  823. clock.p2 = limit->p2.p2_fast;
  824. }
  825. memset(best_clock, 0, sizeof(*best_clock));
  826. max_n = limit->n.max;
  827. /* based on hardware requirement, prefer smaller n to precision */
  828. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  829. /* based on hardware requirement, prefere larger m1,m2 */
  830. for (clock.m1 = limit->m1.max;
  831. clock.m1 >= limit->m1.min; clock.m1--) {
  832. for (clock.m2 = limit->m2.max;
  833. clock.m2 >= limit->m2.min; clock.m2--) {
  834. for (clock.p1 = limit->p1.max;
  835. clock.p1 >= limit->p1.min; clock.p1--) {
  836. int this_err;
  837. intel_clock(dev, refclk, &clock);
  838. if (!intel_PLL_is_valid(dev, limit,
  839. &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. if (IS_GEN6(dev)) {
  1145. I915_WRITE(SNB_DPFC_CTL_SA,
  1146. SNB_CPU_FENCE_ENABLE | dev_priv->cfb_fence);
  1147. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  1148. }
  1149. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1150. }
  1151. void ironlake_disable_fbc(struct drm_device *dev)
  1152. {
  1153. struct drm_i915_private *dev_priv = dev->dev_private;
  1154. u32 dpfc_ctl;
  1155. /* Disable compression */
  1156. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1157. if (dpfc_ctl & DPFC_CTL_EN) {
  1158. dpfc_ctl &= ~DPFC_CTL_EN;
  1159. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  1160. DRM_DEBUG_KMS("disabled FBC\n");
  1161. }
  1162. }
  1163. static bool ironlake_fbc_enabled(struct drm_device *dev)
  1164. {
  1165. struct drm_i915_private *dev_priv = dev->dev_private;
  1166. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  1167. }
  1168. bool intel_fbc_enabled(struct drm_device *dev)
  1169. {
  1170. struct drm_i915_private *dev_priv = dev->dev_private;
  1171. if (!dev_priv->display.fbc_enabled)
  1172. return false;
  1173. return dev_priv->display.fbc_enabled(dev);
  1174. }
  1175. void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1176. {
  1177. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  1178. if (!dev_priv->display.enable_fbc)
  1179. return;
  1180. dev_priv->display.enable_fbc(crtc, interval);
  1181. }
  1182. void intel_disable_fbc(struct drm_device *dev)
  1183. {
  1184. struct drm_i915_private *dev_priv = dev->dev_private;
  1185. if (!dev_priv->display.disable_fbc)
  1186. return;
  1187. dev_priv->display.disable_fbc(dev);
  1188. }
  1189. /**
  1190. * intel_update_fbc - enable/disable FBC as needed
  1191. * @dev: the drm_device
  1192. *
  1193. * Set up the framebuffer compression hardware at mode set time. We
  1194. * enable it if possible:
  1195. * - plane A only (on pre-965)
  1196. * - no pixel mulitply/line duplication
  1197. * - no alpha buffer discard
  1198. * - no dual wide
  1199. * - framebuffer <= 2048 in width, 1536 in height
  1200. *
  1201. * We can't assume that any compression will take place (worst case),
  1202. * so the compressed buffer has to be the same size as the uncompressed
  1203. * one. It also must reside (along with the line length buffer) in
  1204. * stolen memory.
  1205. *
  1206. * We need to enable/disable FBC on a global basis.
  1207. */
  1208. static void intel_update_fbc(struct drm_device *dev)
  1209. {
  1210. struct drm_i915_private *dev_priv = dev->dev_private;
  1211. struct drm_crtc *crtc = NULL, *tmp_crtc;
  1212. struct intel_crtc *intel_crtc;
  1213. struct drm_framebuffer *fb;
  1214. struct intel_framebuffer *intel_fb;
  1215. struct drm_i915_gem_object *obj;
  1216. DRM_DEBUG_KMS("\n");
  1217. if (!i915_powersave)
  1218. return;
  1219. if (!I915_HAS_FBC(dev))
  1220. return;
  1221. /*
  1222. * If FBC is already on, we just have to verify that we can
  1223. * keep it that way...
  1224. * Need to disable if:
  1225. * - more than one pipe is active
  1226. * - changing FBC params (stride, fence, mode)
  1227. * - new fb is too large to fit in compressed buffer
  1228. * - going to an unsupported config (interlace, pixel multiply, etc.)
  1229. */
  1230. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  1231. if (tmp_crtc->enabled) {
  1232. if (crtc) {
  1233. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  1234. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  1235. goto out_disable;
  1236. }
  1237. crtc = tmp_crtc;
  1238. }
  1239. }
  1240. if (!crtc || crtc->fb == NULL) {
  1241. DRM_DEBUG_KMS("no output, disabling\n");
  1242. dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
  1243. goto out_disable;
  1244. }
  1245. intel_crtc = to_intel_crtc(crtc);
  1246. fb = crtc->fb;
  1247. intel_fb = to_intel_framebuffer(fb);
  1248. obj = intel_fb->obj;
  1249. if (intel_fb->obj->base.size > dev_priv->cfb_size) {
  1250. DRM_DEBUG_KMS("framebuffer too large, disabling "
  1251. "compression\n");
  1252. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  1253. goto out_disable;
  1254. }
  1255. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  1256. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  1257. DRM_DEBUG_KMS("mode incompatible with compression, "
  1258. "disabling\n");
  1259. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  1260. goto out_disable;
  1261. }
  1262. if ((crtc->mode.hdisplay > 2048) ||
  1263. (crtc->mode.vdisplay > 1536)) {
  1264. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  1265. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  1266. goto out_disable;
  1267. }
  1268. if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
  1269. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  1270. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  1271. goto out_disable;
  1272. }
  1273. if (obj->tiling_mode != I915_TILING_X) {
  1274. DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
  1275. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  1276. goto out_disable;
  1277. }
  1278. /* If the kernel debugger is active, always disable compression */
  1279. if (in_dbg_master())
  1280. goto out_disable;
  1281. intel_enable_fbc(crtc, 500);
  1282. return;
  1283. out_disable:
  1284. /* Multiple disables should be harmless */
  1285. if (intel_fbc_enabled(dev)) {
  1286. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  1287. intel_disable_fbc(dev);
  1288. }
  1289. }
  1290. int
  1291. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1292. struct drm_i915_gem_object *obj,
  1293. struct intel_ring_buffer *pipelined)
  1294. {
  1295. u32 alignment;
  1296. int ret;
  1297. switch (obj->tiling_mode) {
  1298. case I915_TILING_NONE:
  1299. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1300. alignment = 128 * 1024;
  1301. else if (INTEL_INFO(dev)->gen >= 4)
  1302. alignment = 4 * 1024;
  1303. else
  1304. alignment = 64 * 1024;
  1305. break;
  1306. case I915_TILING_X:
  1307. /* pin() will align the object as required by fence */
  1308. alignment = 0;
  1309. break;
  1310. case I915_TILING_Y:
  1311. /* FIXME: Is this true? */
  1312. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1313. return -EINVAL;
  1314. default:
  1315. BUG();
  1316. }
  1317. ret = i915_gem_object_pin(obj, alignment, true);
  1318. if (ret)
  1319. return ret;
  1320. ret = i915_gem_object_set_to_display_plane(obj, pipelined);
  1321. if (ret)
  1322. goto err_unpin;
  1323. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1324. * fence, whereas 965+ only requires a fence if using
  1325. * framebuffer compression. For simplicity, we always install
  1326. * a fence as the cost is not that onerous.
  1327. */
  1328. if (obj->tiling_mode != I915_TILING_NONE) {
  1329. ret = i915_gem_object_get_fence(obj, pipelined, false);
  1330. if (ret)
  1331. goto err_unpin;
  1332. }
  1333. return 0;
  1334. err_unpin:
  1335. i915_gem_object_unpin(obj);
  1336. return ret;
  1337. }
  1338. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1339. static int
  1340. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1341. int x, int y, enum mode_set_atomic state)
  1342. {
  1343. struct drm_device *dev = crtc->dev;
  1344. struct drm_i915_private *dev_priv = dev->dev_private;
  1345. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1346. struct intel_framebuffer *intel_fb;
  1347. struct drm_i915_gem_object *obj;
  1348. int plane = intel_crtc->plane;
  1349. unsigned long Start, Offset;
  1350. u32 dspcntr;
  1351. u32 reg;
  1352. switch (plane) {
  1353. case 0:
  1354. case 1:
  1355. break;
  1356. default:
  1357. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1358. return -EINVAL;
  1359. }
  1360. intel_fb = to_intel_framebuffer(fb);
  1361. obj = intel_fb->obj;
  1362. reg = DSPCNTR(plane);
  1363. dspcntr = I915_READ(reg);
  1364. /* Mask out pixel format bits in case we change it */
  1365. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1366. switch (fb->bits_per_pixel) {
  1367. case 8:
  1368. dspcntr |= DISPPLANE_8BPP;
  1369. break;
  1370. case 16:
  1371. if (fb->depth == 15)
  1372. dspcntr |= DISPPLANE_15_16BPP;
  1373. else
  1374. dspcntr |= DISPPLANE_16BPP;
  1375. break;
  1376. case 24:
  1377. case 32:
  1378. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1379. break;
  1380. default:
  1381. DRM_ERROR("Unknown color depth\n");
  1382. return -EINVAL;
  1383. }
  1384. if (INTEL_INFO(dev)->gen >= 4) {
  1385. if (obj->tiling_mode != I915_TILING_NONE)
  1386. dspcntr |= DISPPLANE_TILED;
  1387. else
  1388. dspcntr &= ~DISPPLANE_TILED;
  1389. }
  1390. if (HAS_PCH_SPLIT(dev))
  1391. /* must disable */
  1392. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1393. I915_WRITE(reg, dspcntr);
  1394. Start = obj->gtt_offset;
  1395. Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
  1396. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1397. Start, Offset, x, y, fb->pitch);
  1398. I915_WRITE(DSPSTRIDE(plane), fb->pitch);
  1399. if (INTEL_INFO(dev)->gen >= 4) {
  1400. I915_WRITE(DSPSURF(plane), Start);
  1401. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1402. I915_WRITE(DSPADDR(plane), Offset);
  1403. } else
  1404. I915_WRITE(DSPADDR(plane), Start + Offset);
  1405. POSTING_READ(reg);
  1406. intel_update_fbc(dev);
  1407. intel_increase_pllclock(crtc);
  1408. return 0;
  1409. }
  1410. static int
  1411. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1412. struct drm_framebuffer *old_fb)
  1413. {
  1414. struct drm_device *dev = crtc->dev;
  1415. struct drm_i915_master_private *master_priv;
  1416. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1417. int ret;
  1418. /* no fb bound */
  1419. if (!crtc->fb) {
  1420. DRM_DEBUG_KMS("No FB bound\n");
  1421. return 0;
  1422. }
  1423. switch (intel_crtc->plane) {
  1424. case 0:
  1425. case 1:
  1426. break;
  1427. default:
  1428. return -EINVAL;
  1429. }
  1430. mutex_lock(&dev->struct_mutex);
  1431. ret = intel_pin_and_fence_fb_obj(dev,
  1432. to_intel_framebuffer(crtc->fb)->obj,
  1433. NULL);
  1434. if (ret != 0) {
  1435. mutex_unlock(&dev->struct_mutex);
  1436. return ret;
  1437. }
  1438. if (old_fb) {
  1439. struct drm_i915_private *dev_priv = dev->dev_private;
  1440. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1441. wait_event(dev_priv->pending_flip_queue,
  1442. atomic_read(&obj->pending_flip) == 0);
  1443. /* Big Hammer, we also need to ensure that any pending
  1444. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1445. * current scanout is retired before unpinning the old
  1446. * framebuffer.
  1447. */
  1448. ret = i915_gem_object_flush_gpu(obj, false);
  1449. if (ret) {
  1450. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  1451. mutex_unlock(&dev->struct_mutex);
  1452. return ret;
  1453. }
  1454. }
  1455. ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
  1456. LEAVE_ATOMIC_MODE_SET);
  1457. if (ret) {
  1458. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  1459. mutex_unlock(&dev->struct_mutex);
  1460. return ret;
  1461. }
  1462. if (old_fb) {
  1463. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1464. i915_gem_object_unpin(to_intel_framebuffer(old_fb)->obj);
  1465. }
  1466. mutex_unlock(&dev->struct_mutex);
  1467. if (!dev->primary->master)
  1468. return 0;
  1469. master_priv = dev->primary->master->driver_priv;
  1470. if (!master_priv->sarea_priv)
  1471. return 0;
  1472. if (intel_crtc->pipe) {
  1473. master_priv->sarea_priv->pipeB_x = x;
  1474. master_priv->sarea_priv->pipeB_y = y;
  1475. } else {
  1476. master_priv->sarea_priv->pipeA_x = x;
  1477. master_priv->sarea_priv->pipeA_y = y;
  1478. }
  1479. return 0;
  1480. }
  1481. static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
  1482. {
  1483. struct drm_device *dev = crtc->dev;
  1484. struct drm_i915_private *dev_priv = dev->dev_private;
  1485. u32 dpa_ctl;
  1486. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1487. dpa_ctl = I915_READ(DP_A);
  1488. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1489. if (clock < 200000) {
  1490. u32 temp;
  1491. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1492. /* workaround for 160Mhz:
  1493. 1) program 0x4600c bits 15:0 = 0x8124
  1494. 2) program 0x46010 bit 0 = 1
  1495. 3) program 0x46034 bit 24 = 1
  1496. 4) program 0x64000 bit 14 = 1
  1497. */
  1498. temp = I915_READ(0x4600c);
  1499. temp &= 0xffff0000;
  1500. I915_WRITE(0x4600c, temp | 0x8124);
  1501. temp = I915_READ(0x46010);
  1502. I915_WRITE(0x46010, temp | 1);
  1503. temp = I915_READ(0x46034);
  1504. I915_WRITE(0x46034, temp | (1 << 24));
  1505. } else {
  1506. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1507. }
  1508. I915_WRITE(DP_A, dpa_ctl);
  1509. POSTING_READ(DP_A);
  1510. udelay(500);
  1511. }
  1512. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  1513. {
  1514. struct drm_device *dev = crtc->dev;
  1515. struct drm_i915_private *dev_priv = dev->dev_private;
  1516. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1517. int pipe = intel_crtc->pipe;
  1518. u32 reg, temp;
  1519. /* enable normal train */
  1520. reg = FDI_TX_CTL(pipe);
  1521. temp = I915_READ(reg);
  1522. temp &= ~FDI_LINK_TRAIN_NONE;
  1523. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  1524. I915_WRITE(reg, temp);
  1525. reg = FDI_RX_CTL(pipe);
  1526. temp = I915_READ(reg);
  1527. if (HAS_PCH_CPT(dev)) {
  1528. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1529. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  1530. } else {
  1531. temp &= ~FDI_LINK_TRAIN_NONE;
  1532. temp |= FDI_LINK_TRAIN_NONE;
  1533. }
  1534. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  1535. /* wait one idle pattern time */
  1536. POSTING_READ(reg);
  1537. udelay(1000);
  1538. }
  1539. /* The FDI link training functions for ILK/Ibexpeak. */
  1540. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  1541. {
  1542. struct drm_device *dev = crtc->dev;
  1543. struct drm_i915_private *dev_priv = dev->dev_private;
  1544. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1545. int pipe = intel_crtc->pipe;
  1546. u32 reg, temp, tries;
  1547. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1548. for train result */
  1549. reg = FDI_RX_IMR(pipe);
  1550. temp = I915_READ(reg);
  1551. temp &= ~FDI_RX_SYMBOL_LOCK;
  1552. temp &= ~FDI_RX_BIT_LOCK;
  1553. I915_WRITE(reg, temp);
  1554. I915_READ(reg);
  1555. udelay(150);
  1556. /* enable CPU FDI TX and PCH FDI RX */
  1557. reg = FDI_TX_CTL(pipe);
  1558. temp = I915_READ(reg);
  1559. temp &= ~(7 << 19);
  1560. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1561. temp &= ~FDI_LINK_TRAIN_NONE;
  1562. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1563. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  1564. reg = FDI_RX_CTL(pipe);
  1565. temp = I915_READ(reg);
  1566. temp &= ~FDI_LINK_TRAIN_NONE;
  1567. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1568. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  1569. POSTING_READ(reg);
  1570. udelay(150);
  1571. /* Ironlake workaround, enable clock pointer after FDI enable*/
  1572. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_ENABLE);
  1573. reg = FDI_RX_IIR(pipe);
  1574. for (tries = 0; tries < 5; tries++) {
  1575. temp = I915_READ(reg);
  1576. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1577. if ((temp & FDI_RX_BIT_LOCK)) {
  1578. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1579. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  1580. break;
  1581. }
  1582. }
  1583. if (tries == 5)
  1584. DRM_ERROR("FDI train 1 fail!\n");
  1585. /* Train 2 */
  1586. reg = FDI_TX_CTL(pipe);
  1587. temp = I915_READ(reg);
  1588. temp &= ~FDI_LINK_TRAIN_NONE;
  1589. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1590. I915_WRITE(reg, temp);
  1591. reg = FDI_RX_CTL(pipe);
  1592. temp = I915_READ(reg);
  1593. temp &= ~FDI_LINK_TRAIN_NONE;
  1594. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1595. I915_WRITE(reg, temp);
  1596. POSTING_READ(reg);
  1597. udelay(150);
  1598. reg = FDI_RX_IIR(pipe);
  1599. for (tries = 0; tries < 5; tries++) {
  1600. temp = I915_READ(reg);
  1601. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1602. if (temp & FDI_RX_SYMBOL_LOCK) {
  1603. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  1604. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1605. break;
  1606. }
  1607. }
  1608. if (tries == 5)
  1609. DRM_ERROR("FDI train 2 fail!\n");
  1610. DRM_DEBUG_KMS("FDI train done\n");
  1611. }
  1612. static const int const snb_b_fdi_train_param [] = {
  1613. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  1614. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  1615. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  1616. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  1617. };
  1618. /* The FDI link training functions for SNB/Cougarpoint. */
  1619. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  1620. {
  1621. struct drm_device *dev = crtc->dev;
  1622. struct drm_i915_private *dev_priv = dev->dev_private;
  1623. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1624. int pipe = intel_crtc->pipe;
  1625. u32 reg, temp, i;
  1626. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1627. for train result */
  1628. reg = FDI_RX_IMR(pipe);
  1629. temp = I915_READ(reg);
  1630. temp &= ~FDI_RX_SYMBOL_LOCK;
  1631. temp &= ~FDI_RX_BIT_LOCK;
  1632. I915_WRITE(reg, temp);
  1633. POSTING_READ(reg);
  1634. udelay(150);
  1635. /* enable CPU FDI TX and PCH FDI RX */
  1636. reg = FDI_TX_CTL(pipe);
  1637. temp = I915_READ(reg);
  1638. temp &= ~(7 << 19);
  1639. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1640. temp &= ~FDI_LINK_TRAIN_NONE;
  1641. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1642. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1643. /* SNB-B */
  1644. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1645. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  1646. reg = FDI_RX_CTL(pipe);
  1647. temp = I915_READ(reg);
  1648. if (HAS_PCH_CPT(dev)) {
  1649. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1650. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1651. } else {
  1652. temp &= ~FDI_LINK_TRAIN_NONE;
  1653. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1654. }
  1655. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  1656. POSTING_READ(reg);
  1657. udelay(150);
  1658. for (i = 0; i < 4; i++ ) {
  1659. reg = FDI_TX_CTL(pipe);
  1660. temp = I915_READ(reg);
  1661. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1662. temp |= snb_b_fdi_train_param[i];
  1663. I915_WRITE(reg, temp);
  1664. POSTING_READ(reg);
  1665. udelay(500);
  1666. reg = FDI_RX_IIR(pipe);
  1667. temp = I915_READ(reg);
  1668. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1669. if (temp & FDI_RX_BIT_LOCK) {
  1670. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  1671. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1672. break;
  1673. }
  1674. }
  1675. if (i == 4)
  1676. DRM_ERROR("FDI train 1 fail!\n");
  1677. /* Train 2 */
  1678. reg = FDI_TX_CTL(pipe);
  1679. temp = I915_READ(reg);
  1680. temp &= ~FDI_LINK_TRAIN_NONE;
  1681. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1682. if (IS_GEN6(dev)) {
  1683. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1684. /* SNB-B */
  1685. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1686. }
  1687. I915_WRITE(reg, temp);
  1688. reg = FDI_RX_CTL(pipe);
  1689. temp = I915_READ(reg);
  1690. if (HAS_PCH_CPT(dev)) {
  1691. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1692. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  1693. } else {
  1694. temp &= ~FDI_LINK_TRAIN_NONE;
  1695. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1696. }
  1697. I915_WRITE(reg, temp);
  1698. POSTING_READ(reg);
  1699. udelay(150);
  1700. for (i = 0; i < 4; i++ ) {
  1701. reg = FDI_TX_CTL(pipe);
  1702. temp = I915_READ(reg);
  1703. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1704. temp |= snb_b_fdi_train_param[i];
  1705. I915_WRITE(reg, temp);
  1706. POSTING_READ(reg);
  1707. udelay(500);
  1708. reg = FDI_RX_IIR(pipe);
  1709. temp = I915_READ(reg);
  1710. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1711. if (temp & FDI_RX_SYMBOL_LOCK) {
  1712. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  1713. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1714. break;
  1715. }
  1716. }
  1717. if (i == 4)
  1718. DRM_ERROR("FDI train 2 fail!\n");
  1719. DRM_DEBUG_KMS("FDI train done.\n");
  1720. }
  1721. static void ironlake_fdi_enable(struct drm_crtc *crtc)
  1722. {
  1723. struct drm_device *dev = crtc->dev;
  1724. struct drm_i915_private *dev_priv = dev->dev_private;
  1725. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1726. int pipe = intel_crtc->pipe;
  1727. u32 reg, temp;
  1728. /* Write the TU size bits so error detection works */
  1729. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  1730. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  1731. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  1732. reg = FDI_RX_CTL(pipe);
  1733. temp = I915_READ(reg);
  1734. temp &= ~((0x7 << 19) | (0x7 << 16));
  1735. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1736. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  1737. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  1738. POSTING_READ(reg);
  1739. udelay(200);
  1740. /* Switch from Rawclk to PCDclk */
  1741. temp = I915_READ(reg);
  1742. I915_WRITE(reg, temp | FDI_PCDCLK);
  1743. POSTING_READ(reg);
  1744. udelay(200);
  1745. /* Enable CPU FDI TX PLL, always on for Ironlake */
  1746. reg = FDI_TX_CTL(pipe);
  1747. temp = I915_READ(reg);
  1748. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  1749. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  1750. POSTING_READ(reg);
  1751. udelay(100);
  1752. }
  1753. }
  1754. static void intel_flush_display_plane(struct drm_device *dev,
  1755. int plane)
  1756. {
  1757. struct drm_i915_private *dev_priv = dev->dev_private;
  1758. u32 reg = DSPADDR(plane);
  1759. I915_WRITE(reg, I915_READ(reg));
  1760. }
  1761. /*
  1762. * When we disable a pipe, we need to clear any pending scanline wait events
  1763. * to avoid hanging the ring, which we assume we are waiting on.
  1764. */
  1765. static void intel_clear_scanline_wait(struct drm_device *dev)
  1766. {
  1767. struct drm_i915_private *dev_priv = dev->dev_private;
  1768. struct intel_ring_buffer *ring;
  1769. u32 tmp;
  1770. if (IS_GEN2(dev))
  1771. /* Can't break the hang on i8xx */
  1772. return;
  1773. ring = LP_RING(dev_priv);
  1774. tmp = I915_READ_CTL(ring);
  1775. if (tmp & RING_WAIT)
  1776. I915_WRITE_CTL(ring, tmp);
  1777. }
  1778. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  1779. {
  1780. struct drm_i915_gem_object *obj;
  1781. struct drm_i915_private *dev_priv;
  1782. if (crtc->fb == NULL)
  1783. return;
  1784. obj = to_intel_framebuffer(crtc->fb)->obj;
  1785. dev_priv = crtc->dev->dev_private;
  1786. wait_event(dev_priv->pending_flip_queue,
  1787. atomic_read(&obj->pending_flip) == 0);
  1788. }
  1789. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  1790. {
  1791. struct drm_device *dev = crtc->dev;
  1792. struct drm_i915_private *dev_priv = dev->dev_private;
  1793. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1794. int pipe = intel_crtc->pipe;
  1795. int plane = intel_crtc->plane;
  1796. u32 reg, temp;
  1797. if (intel_crtc->active)
  1798. return;
  1799. intel_crtc->active = true;
  1800. intel_update_watermarks(dev);
  1801. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1802. temp = I915_READ(PCH_LVDS);
  1803. if ((temp & LVDS_PORT_EN) == 0)
  1804. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  1805. }
  1806. ironlake_fdi_enable(crtc);
  1807. /* Enable panel fitting for LVDS */
  1808. if (dev_priv->pch_pf_size &&
  1809. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
  1810. /* Force use of hard-coded filter coefficients
  1811. * as some pre-programmed values are broken,
  1812. * e.g. x201.
  1813. */
  1814. I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1,
  1815. PF_ENABLE | PF_FILTER_MED_3x3);
  1816. I915_WRITE(pipe ? PFB_WIN_POS : PFA_WIN_POS,
  1817. dev_priv->pch_pf_pos);
  1818. I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ,
  1819. dev_priv->pch_pf_size);
  1820. }
  1821. /* Enable CPU pipe */
  1822. reg = PIPECONF(pipe);
  1823. temp = I915_READ(reg);
  1824. if ((temp & PIPECONF_ENABLE) == 0) {
  1825. I915_WRITE(reg, temp | PIPECONF_ENABLE);
  1826. POSTING_READ(reg);
  1827. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1828. }
  1829. /* configure and enable CPU plane */
  1830. reg = DSPCNTR(plane);
  1831. temp = I915_READ(reg);
  1832. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  1833. I915_WRITE(reg, temp | DISPLAY_PLANE_ENABLE);
  1834. intel_flush_display_plane(dev, plane);
  1835. }
  1836. /* For PCH output, training FDI link */
  1837. if (IS_GEN6(dev))
  1838. gen6_fdi_link_train(crtc);
  1839. else
  1840. ironlake_fdi_link_train(crtc);
  1841. /* enable PCH DPLL */
  1842. reg = PCH_DPLL(pipe);
  1843. temp = I915_READ(reg);
  1844. if ((temp & DPLL_VCO_ENABLE) == 0) {
  1845. I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
  1846. POSTING_READ(reg);
  1847. udelay(200);
  1848. }
  1849. if (HAS_PCH_CPT(dev)) {
  1850. /* Be sure PCH DPLL SEL is set */
  1851. temp = I915_READ(PCH_DPLL_SEL);
  1852. if (pipe == 0 && (temp & TRANSA_DPLL_ENABLE) == 0)
  1853. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  1854. else if (pipe == 1 && (temp & TRANSB_DPLL_ENABLE) == 0)
  1855. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  1856. I915_WRITE(PCH_DPLL_SEL, temp);
  1857. }
  1858. /* set transcoder timing */
  1859. I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
  1860. I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
  1861. I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
  1862. I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
  1863. I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
  1864. I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
  1865. intel_fdi_normal_train(crtc);
  1866. /* For PCH DP, enable TRANS_DP_CTL */
  1867. if (HAS_PCH_CPT(dev) &&
  1868. intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  1869. reg = TRANS_DP_CTL(pipe);
  1870. temp = I915_READ(reg);
  1871. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  1872. TRANS_DP_SYNC_MASK |
  1873. TRANS_DP_BPC_MASK);
  1874. temp |= (TRANS_DP_OUTPUT_ENABLE |
  1875. TRANS_DP_ENH_FRAMING);
  1876. temp |= TRANS_DP_8BPC;
  1877. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  1878. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  1879. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  1880. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  1881. switch (intel_trans_dp_port_sel(crtc)) {
  1882. case PCH_DP_B:
  1883. temp |= TRANS_DP_PORT_SEL_B;
  1884. break;
  1885. case PCH_DP_C:
  1886. temp |= TRANS_DP_PORT_SEL_C;
  1887. break;
  1888. case PCH_DP_D:
  1889. temp |= TRANS_DP_PORT_SEL_D;
  1890. break;
  1891. default:
  1892. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  1893. temp |= TRANS_DP_PORT_SEL_B;
  1894. break;
  1895. }
  1896. I915_WRITE(reg, temp);
  1897. }
  1898. /* enable PCH transcoder */
  1899. reg = TRANSCONF(pipe);
  1900. temp = I915_READ(reg);
  1901. /*
  1902. * make the BPC in transcoder be consistent with
  1903. * that in pipeconf reg.
  1904. */
  1905. temp &= ~PIPE_BPC_MASK;
  1906. temp |= I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK;
  1907. I915_WRITE(reg, temp | TRANS_ENABLE);
  1908. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1909. DRM_ERROR("failed to enable transcoder %d\n", pipe);
  1910. intel_crtc_load_lut(crtc);
  1911. intel_update_fbc(dev);
  1912. intel_crtc_update_cursor(crtc, true);
  1913. }
  1914. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  1915. {
  1916. struct drm_device *dev = crtc->dev;
  1917. struct drm_i915_private *dev_priv = dev->dev_private;
  1918. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1919. int pipe = intel_crtc->pipe;
  1920. int plane = intel_crtc->plane;
  1921. u32 reg, temp;
  1922. if (!intel_crtc->active)
  1923. return;
  1924. intel_crtc_wait_for_pending_flips(crtc);
  1925. drm_vblank_off(dev, pipe);
  1926. intel_crtc_update_cursor(crtc, false);
  1927. /* Disable display plane */
  1928. reg = DSPCNTR(plane);
  1929. temp = I915_READ(reg);
  1930. if (temp & DISPLAY_PLANE_ENABLE) {
  1931. I915_WRITE(reg, temp & ~DISPLAY_PLANE_ENABLE);
  1932. intel_flush_display_plane(dev, plane);
  1933. }
  1934. if (dev_priv->cfb_plane == plane &&
  1935. dev_priv->display.disable_fbc)
  1936. dev_priv->display.disable_fbc(dev);
  1937. /* disable cpu pipe, disable after all planes disabled */
  1938. reg = PIPECONF(pipe);
  1939. temp = I915_READ(reg);
  1940. if (temp & PIPECONF_ENABLE) {
  1941. I915_WRITE(reg, temp & ~PIPECONF_ENABLE);
  1942. POSTING_READ(reg);
  1943. /* wait for cpu pipe off, pipe state */
  1944. intel_wait_for_pipe_off(dev, intel_crtc->pipe);
  1945. }
  1946. /* Disable PF */
  1947. I915_WRITE(pipe ? PFB_CTL_1 : PFA_CTL_1, 0);
  1948. I915_WRITE(pipe ? PFB_WIN_SZ : PFA_WIN_SZ, 0);
  1949. /* disable CPU FDI tx and PCH FDI rx */
  1950. reg = FDI_TX_CTL(pipe);
  1951. temp = I915_READ(reg);
  1952. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  1953. POSTING_READ(reg);
  1954. reg = FDI_RX_CTL(pipe);
  1955. temp = I915_READ(reg);
  1956. temp &= ~(0x7 << 16);
  1957. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  1958. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  1959. POSTING_READ(reg);
  1960. udelay(100);
  1961. /* Ironlake workaround, disable clock pointer after downing FDI */
  1962. if (HAS_PCH_IBX(dev))
  1963. I915_WRITE(FDI_RX_CHICKEN(pipe),
  1964. I915_READ(FDI_RX_CHICKEN(pipe) &
  1965. ~FDI_RX_PHASE_SYNC_POINTER_ENABLE));
  1966. /* still set train pattern 1 */
  1967. reg = FDI_TX_CTL(pipe);
  1968. temp = I915_READ(reg);
  1969. temp &= ~FDI_LINK_TRAIN_NONE;
  1970. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1971. I915_WRITE(reg, temp);
  1972. reg = FDI_RX_CTL(pipe);
  1973. temp = I915_READ(reg);
  1974. if (HAS_PCH_CPT(dev)) {
  1975. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1976. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1977. } else {
  1978. temp &= ~FDI_LINK_TRAIN_NONE;
  1979. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1980. }
  1981. /* BPC in FDI rx is consistent with that in PIPECONF */
  1982. temp &= ~(0x07 << 16);
  1983. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  1984. I915_WRITE(reg, temp);
  1985. POSTING_READ(reg);
  1986. udelay(100);
  1987. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1988. temp = I915_READ(PCH_LVDS);
  1989. if (temp & LVDS_PORT_EN) {
  1990. I915_WRITE(PCH_LVDS, temp & ~LVDS_PORT_EN);
  1991. POSTING_READ(PCH_LVDS);
  1992. udelay(100);
  1993. }
  1994. }
  1995. /* disable PCH transcoder */
  1996. reg = TRANSCONF(plane);
  1997. temp = I915_READ(reg);
  1998. if (temp & TRANS_ENABLE) {
  1999. I915_WRITE(reg, temp & ~TRANS_ENABLE);
  2000. /* wait for PCH transcoder off, transcoder state */
  2001. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  2002. DRM_ERROR("failed to disable transcoder\n");
  2003. }
  2004. if (HAS_PCH_CPT(dev)) {
  2005. /* disable TRANS_DP_CTL */
  2006. reg = TRANS_DP_CTL(pipe);
  2007. temp = I915_READ(reg);
  2008. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  2009. I915_WRITE(reg, temp);
  2010. /* disable DPLL_SEL */
  2011. temp = I915_READ(PCH_DPLL_SEL);
  2012. if (pipe == 0)
  2013. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  2014. else
  2015. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2016. I915_WRITE(PCH_DPLL_SEL, temp);
  2017. }
  2018. /* disable PCH DPLL */
  2019. reg = PCH_DPLL(pipe);
  2020. temp = I915_READ(reg);
  2021. I915_WRITE(reg, temp & ~DPLL_VCO_ENABLE);
  2022. /* Switch from PCDclk to Rawclk */
  2023. reg = FDI_RX_CTL(pipe);
  2024. temp = I915_READ(reg);
  2025. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2026. /* Disable CPU FDI TX PLL */
  2027. reg = FDI_TX_CTL(pipe);
  2028. temp = I915_READ(reg);
  2029. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2030. POSTING_READ(reg);
  2031. udelay(100);
  2032. reg = FDI_RX_CTL(pipe);
  2033. temp = I915_READ(reg);
  2034. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2035. /* Wait for the clocks to turn off. */
  2036. POSTING_READ(reg);
  2037. udelay(100);
  2038. intel_crtc->active = false;
  2039. intel_update_watermarks(dev);
  2040. intel_update_fbc(dev);
  2041. intel_clear_scanline_wait(dev);
  2042. }
  2043. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  2044. {
  2045. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2046. int pipe = intel_crtc->pipe;
  2047. int plane = intel_crtc->plane;
  2048. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2049. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2050. */
  2051. switch (mode) {
  2052. case DRM_MODE_DPMS_ON:
  2053. case DRM_MODE_DPMS_STANDBY:
  2054. case DRM_MODE_DPMS_SUSPEND:
  2055. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  2056. ironlake_crtc_enable(crtc);
  2057. break;
  2058. case DRM_MODE_DPMS_OFF:
  2059. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  2060. ironlake_crtc_disable(crtc);
  2061. break;
  2062. }
  2063. }
  2064. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2065. {
  2066. if (!enable && intel_crtc->overlay) {
  2067. struct drm_device *dev = intel_crtc->base.dev;
  2068. mutex_lock(&dev->struct_mutex);
  2069. (void) intel_overlay_switch_off(intel_crtc->overlay, false);
  2070. mutex_unlock(&dev->struct_mutex);
  2071. }
  2072. /* Let userspace switch the overlay on again. In most cases userspace
  2073. * has to recompute where to put it anyway.
  2074. */
  2075. }
  2076. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  2077. {
  2078. struct drm_device *dev = crtc->dev;
  2079. struct drm_i915_private *dev_priv = dev->dev_private;
  2080. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2081. int pipe = intel_crtc->pipe;
  2082. int plane = intel_crtc->plane;
  2083. u32 reg, temp;
  2084. if (intel_crtc->active)
  2085. return;
  2086. intel_crtc->active = true;
  2087. intel_update_watermarks(dev);
  2088. /* Enable the DPLL */
  2089. reg = DPLL(pipe);
  2090. temp = I915_READ(reg);
  2091. if ((temp & DPLL_VCO_ENABLE) == 0) {
  2092. I915_WRITE(reg, temp);
  2093. /* Wait for the clocks to stabilize. */
  2094. POSTING_READ(reg);
  2095. udelay(150);
  2096. I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
  2097. /* Wait for the clocks to stabilize. */
  2098. POSTING_READ(reg);
  2099. udelay(150);
  2100. I915_WRITE(reg, temp | DPLL_VCO_ENABLE);
  2101. /* Wait for the clocks to stabilize. */
  2102. POSTING_READ(reg);
  2103. udelay(150);
  2104. }
  2105. /* Enable the pipe */
  2106. reg = PIPECONF(pipe);
  2107. temp = I915_READ(reg);
  2108. if ((temp & PIPECONF_ENABLE) == 0)
  2109. I915_WRITE(reg, temp | PIPECONF_ENABLE);
  2110. /* Enable the plane */
  2111. reg = DSPCNTR(plane);
  2112. temp = I915_READ(reg);
  2113. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  2114. I915_WRITE(reg, temp | DISPLAY_PLANE_ENABLE);
  2115. intel_flush_display_plane(dev, plane);
  2116. }
  2117. intel_crtc_load_lut(crtc);
  2118. intel_update_fbc(dev);
  2119. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2120. intel_crtc_dpms_overlay(intel_crtc, true);
  2121. intel_crtc_update_cursor(crtc, true);
  2122. }
  2123. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  2124. {
  2125. struct drm_device *dev = crtc->dev;
  2126. struct drm_i915_private *dev_priv = dev->dev_private;
  2127. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2128. int pipe = intel_crtc->pipe;
  2129. int plane = intel_crtc->plane;
  2130. u32 reg, temp;
  2131. if (!intel_crtc->active)
  2132. return;
  2133. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2134. intel_crtc_wait_for_pending_flips(crtc);
  2135. drm_vblank_off(dev, pipe);
  2136. intel_crtc_dpms_overlay(intel_crtc, false);
  2137. intel_crtc_update_cursor(crtc, false);
  2138. if (dev_priv->cfb_plane == plane &&
  2139. dev_priv->display.disable_fbc)
  2140. dev_priv->display.disable_fbc(dev);
  2141. /* Disable display plane */
  2142. reg = DSPCNTR(plane);
  2143. temp = I915_READ(reg);
  2144. if (temp & DISPLAY_PLANE_ENABLE) {
  2145. I915_WRITE(reg, temp & ~DISPLAY_PLANE_ENABLE);
  2146. /* Flush the plane changes */
  2147. intel_flush_display_plane(dev, plane);
  2148. /* Wait for vblank for the disable to take effect */
  2149. if (IS_GEN2(dev))
  2150. intel_wait_for_vblank(dev, pipe);
  2151. }
  2152. /* Don't disable pipe A or pipe A PLLs if needed */
  2153. if (pipe == 0 && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  2154. goto done;
  2155. /* Next, disable display pipes */
  2156. reg = PIPECONF(pipe);
  2157. temp = I915_READ(reg);
  2158. if (temp & PIPECONF_ENABLE) {
  2159. I915_WRITE(reg, temp & ~PIPECONF_ENABLE);
  2160. /* Wait for the pipe to turn off */
  2161. POSTING_READ(reg);
  2162. intel_wait_for_pipe_off(dev, pipe);
  2163. }
  2164. reg = DPLL(pipe);
  2165. temp = I915_READ(reg);
  2166. if (temp & DPLL_VCO_ENABLE) {
  2167. I915_WRITE(reg, temp & ~DPLL_VCO_ENABLE);
  2168. /* Wait for the clocks to turn off. */
  2169. POSTING_READ(reg);
  2170. udelay(150);
  2171. }
  2172. done:
  2173. intel_crtc->active = false;
  2174. intel_update_fbc(dev);
  2175. intel_update_watermarks(dev);
  2176. intel_clear_scanline_wait(dev);
  2177. }
  2178. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2179. {
  2180. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2181. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2182. */
  2183. switch (mode) {
  2184. case DRM_MODE_DPMS_ON:
  2185. case DRM_MODE_DPMS_STANDBY:
  2186. case DRM_MODE_DPMS_SUSPEND:
  2187. i9xx_crtc_enable(crtc);
  2188. break;
  2189. case DRM_MODE_DPMS_OFF:
  2190. i9xx_crtc_disable(crtc);
  2191. break;
  2192. }
  2193. }
  2194. /**
  2195. * Sets the power management mode of the pipe and plane.
  2196. */
  2197. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2198. {
  2199. struct drm_device *dev = crtc->dev;
  2200. struct drm_i915_private *dev_priv = dev->dev_private;
  2201. struct drm_i915_master_private *master_priv;
  2202. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2203. int pipe = intel_crtc->pipe;
  2204. bool enabled;
  2205. if (intel_crtc->dpms_mode == mode)
  2206. return;
  2207. intel_crtc->dpms_mode = mode;
  2208. dev_priv->display.dpms(crtc, mode);
  2209. if (!dev->primary->master)
  2210. return;
  2211. master_priv = dev->primary->master->driver_priv;
  2212. if (!master_priv->sarea_priv)
  2213. return;
  2214. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2215. switch (pipe) {
  2216. case 0:
  2217. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2218. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2219. break;
  2220. case 1:
  2221. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2222. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2223. break;
  2224. default:
  2225. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  2226. break;
  2227. }
  2228. }
  2229. static void intel_crtc_disable(struct drm_crtc *crtc)
  2230. {
  2231. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2232. struct drm_device *dev = crtc->dev;
  2233. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  2234. if (crtc->fb) {
  2235. mutex_lock(&dev->struct_mutex);
  2236. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  2237. mutex_unlock(&dev->struct_mutex);
  2238. }
  2239. }
  2240. /* Prepare for a mode set.
  2241. *
  2242. * Note we could be a lot smarter here. We need to figure out which outputs
  2243. * will be enabled, which disabled (in short, how the config will changes)
  2244. * and perform the minimum necessary steps to accomplish that, e.g. updating
  2245. * watermarks, FBC configuration, making sure PLLs are programmed correctly,
  2246. * panel fitting is in the proper state, etc.
  2247. */
  2248. static void i9xx_crtc_prepare(struct drm_crtc *crtc)
  2249. {
  2250. i9xx_crtc_disable(crtc);
  2251. }
  2252. static void i9xx_crtc_commit(struct drm_crtc *crtc)
  2253. {
  2254. i9xx_crtc_enable(crtc);
  2255. }
  2256. static void ironlake_crtc_prepare(struct drm_crtc *crtc)
  2257. {
  2258. ironlake_crtc_disable(crtc);
  2259. }
  2260. static void ironlake_crtc_commit(struct drm_crtc *crtc)
  2261. {
  2262. ironlake_crtc_enable(crtc);
  2263. }
  2264. void intel_encoder_prepare (struct drm_encoder *encoder)
  2265. {
  2266. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2267. /* lvds has its own version of prepare see intel_lvds_prepare */
  2268. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2269. }
  2270. void intel_encoder_commit (struct drm_encoder *encoder)
  2271. {
  2272. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2273. /* lvds has its own version of commit see intel_lvds_commit */
  2274. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2275. }
  2276. void intel_encoder_destroy(struct drm_encoder *encoder)
  2277. {
  2278. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2279. drm_encoder_cleanup(encoder);
  2280. kfree(intel_encoder);
  2281. }
  2282. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2283. struct drm_display_mode *mode,
  2284. struct drm_display_mode *adjusted_mode)
  2285. {
  2286. struct drm_device *dev = crtc->dev;
  2287. if (HAS_PCH_SPLIT(dev)) {
  2288. /* FDI link clock is fixed at 2.7G */
  2289. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  2290. return false;
  2291. }
  2292. /* XXX some encoders set the crtcinfo, others don't.
  2293. * Obviously we need some form of conflict resolution here...
  2294. */
  2295. if (adjusted_mode->crtc_htotal == 0)
  2296. drm_mode_set_crtcinfo(adjusted_mode, 0);
  2297. return true;
  2298. }
  2299. static int i945_get_display_clock_speed(struct drm_device *dev)
  2300. {
  2301. return 400000;
  2302. }
  2303. static int i915_get_display_clock_speed(struct drm_device *dev)
  2304. {
  2305. return 333000;
  2306. }
  2307. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2308. {
  2309. return 200000;
  2310. }
  2311. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2312. {
  2313. u16 gcfgc = 0;
  2314. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2315. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2316. return 133000;
  2317. else {
  2318. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2319. case GC_DISPLAY_CLOCK_333_MHZ:
  2320. return 333000;
  2321. default:
  2322. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2323. return 190000;
  2324. }
  2325. }
  2326. }
  2327. static int i865_get_display_clock_speed(struct drm_device *dev)
  2328. {
  2329. return 266000;
  2330. }
  2331. static int i855_get_display_clock_speed(struct drm_device *dev)
  2332. {
  2333. u16 hpllcc = 0;
  2334. /* Assume that the hardware is in the high speed state. This
  2335. * should be the default.
  2336. */
  2337. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2338. case GC_CLOCK_133_200:
  2339. case GC_CLOCK_100_200:
  2340. return 200000;
  2341. case GC_CLOCK_166_250:
  2342. return 250000;
  2343. case GC_CLOCK_100_133:
  2344. return 133000;
  2345. }
  2346. /* Shouldn't happen */
  2347. return 0;
  2348. }
  2349. static int i830_get_display_clock_speed(struct drm_device *dev)
  2350. {
  2351. return 133000;
  2352. }
  2353. struct fdi_m_n {
  2354. u32 tu;
  2355. u32 gmch_m;
  2356. u32 gmch_n;
  2357. u32 link_m;
  2358. u32 link_n;
  2359. };
  2360. static void
  2361. fdi_reduce_ratio(u32 *num, u32 *den)
  2362. {
  2363. while (*num > 0xffffff || *den > 0xffffff) {
  2364. *num >>= 1;
  2365. *den >>= 1;
  2366. }
  2367. }
  2368. static void
  2369. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  2370. int link_clock, struct fdi_m_n *m_n)
  2371. {
  2372. m_n->tu = 64; /* default size */
  2373. /* BUG_ON(pixel_clock > INT_MAX / 36); */
  2374. m_n->gmch_m = bits_per_pixel * pixel_clock;
  2375. m_n->gmch_n = link_clock * nlanes * 8;
  2376. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  2377. m_n->link_m = pixel_clock;
  2378. m_n->link_n = link_clock;
  2379. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  2380. }
  2381. struct intel_watermark_params {
  2382. unsigned long fifo_size;
  2383. unsigned long max_wm;
  2384. unsigned long default_wm;
  2385. unsigned long guard_size;
  2386. unsigned long cacheline_size;
  2387. };
  2388. /* Pineview has different values for various configs */
  2389. static struct intel_watermark_params pineview_display_wm = {
  2390. PINEVIEW_DISPLAY_FIFO,
  2391. PINEVIEW_MAX_WM,
  2392. PINEVIEW_DFT_WM,
  2393. PINEVIEW_GUARD_WM,
  2394. PINEVIEW_FIFO_LINE_SIZE
  2395. };
  2396. static struct intel_watermark_params pineview_display_hplloff_wm = {
  2397. PINEVIEW_DISPLAY_FIFO,
  2398. PINEVIEW_MAX_WM,
  2399. PINEVIEW_DFT_HPLLOFF_WM,
  2400. PINEVIEW_GUARD_WM,
  2401. PINEVIEW_FIFO_LINE_SIZE
  2402. };
  2403. static struct intel_watermark_params pineview_cursor_wm = {
  2404. PINEVIEW_CURSOR_FIFO,
  2405. PINEVIEW_CURSOR_MAX_WM,
  2406. PINEVIEW_CURSOR_DFT_WM,
  2407. PINEVIEW_CURSOR_GUARD_WM,
  2408. PINEVIEW_FIFO_LINE_SIZE,
  2409. };
  2410. static struct intel_watermark_params pineview_cursor_hplloff_wm = {
  2411. PINEVIEW_CURSOR_FIFO,
  2412. PINEVIEW_CURSOR_MAX_WM,
  2413. PINEVIEW_CURSOR_DFT_WM,
  2414. PINEVIEW_CURSOR_GUARD_WM,
  2415. PINEVIEW_FIFO_LINE_SIZE
  2416. };
  2417. static struct intel_watermark_params g4x_wm_info = {
  2418. G4X_FIFO_SIZE,
  2419. G4X_MAX_WM,
  2420. G4X_MAX_WM,
  2421. 2,
  2422. G4X_FIFO_LINE_SIZE,
  2423. };
  2424. static struct intel_watermark_params g4x_cursor_wm_info = {
  2425. I965_CURSOR_FIFO,
  2426. I965_CURSOR_MAX_WM,
  2427. I965_CURSOR_DFT_WM,
  2428. 2,
  2429. G4X_FIFO_LINE_SIZE,
  2430. };
  2431. static struct intel_watermark_params i965_cursor_wm_info = {
  2432. I965_CURSOR_FIFO,
  2433. I965_CURSOR_MAX_WM,
  2434. I965_CURSOR_DFT_WM,
  2435. 2,
  2436. I915_FIFO_LINE_SIZE,
  2437. };
  2438. static struct intel_watermark_params i945_wm_info = {
  2439. I945_FIFO_SIZE,
  2440. I915_MAX_WM,
  2441. 1,
  2442. 2,
  2443. I915_FIFO_LINE_SIZE
  2444. };
  2445. static struct intel_watermark_params i915_wm_info = {
  2446. I915_FIFO_SIZE,
  2447. I915_MAX_WM,
  2448. 1,
  2449. 2,
  2450. I915_FIFO_LINE_SIZE
  2451. };
  2452. static struct intel_watermark_params i855_wm_info = {
  2453. I855GM_FIFO_SIZE,
  2454. I915_MAX_WM,
  2455. 1,
  2456. 2,
  2457. I830_FIFO_LINE_SIZE
  2458. };
  2459. static struct intel_watermark_params i830_wm_info = {
  2460. I830_FIFO_SIZE,
  2461. I915_MAX_WM,
  2462. 1,
  2463. 2,
  2464. I830_FIFO_LINE_SIZE
  2465. };
  2466. static struct intel_watermark_params ironlake_display_wm_info = {
  2467. ILK_DISPLAY_FIFO,
  2468. ILK_DISPLAY_MAXWM,
  2469. ILK_DISPLAY_DFTWM,
  2470. 2,
  2471. ILK_FIFO_LINE_SIZE
  2472. };
  2473. static struct intel_watermark_params ironlake_cursor_wm_info = {
  2474. ILK_CURSOR_FIFO,
  2475. ILK_CURSOR_MAXWM,
  2476. ILK_CURSOR_DFTWM,
  2477. 2,
  2478. ILK_FIFO_LINE_SIZE
  2479. };
  2480. static struct intel_watermark_params ironlake_display_srwm_info = {
  2481. ILK_DISPLAY_SR_FIFO,
  2482. ILK_DISPLAY_MAX_SRWM,
  2483. ILK_DISPLAY_DFT_SRWM,
  2484. 2,
  2485. ILK_FIFO_LINE_SIZE
  2486. };
  2487. static struct intel_watermark_params ironlake_cursor_srwm_info = {
  2488. ILK_CURSOR_SR_FIFO,
  2489. ILK_CURSOR_MAX_SRWM,
  2490. ILK_CURSOR_DFT_SRWM,
  2491. 2,
  2492. ILK_FIFO_LINE_SIZE
  2493. };
  2494. static struct intel_watermark_params sandybridge_display_wm_info = {
  2495. SNB_DISPLAY_FIFO,
  2496. SNB_DISPLAY_MAXWM,
  2497. SNB_DISPLAY_DFTWM,
  2498. 2,
  2499. SNB_FIFO_LINE_SIZE
  2500. };
  2501. static struct intel_watermark_params sandybridge_cursor_wm_info = {
  2502. SNB_CURSOR_FIFO,
  2503. SNB_CURSOR_MAXWM,
  2504. SNB_CURSOR_DFTWM,
  2505. 2,
  2506. SNB_FIFO_LINE_SIZE
  2507. };
  2508. static struct intel_watermark_params sandybridge_display_srwm_info = {
  2509. SNB_DISPLAY_SR_FIFO,
  2510. SNB_DISPLAY_MAX_SRWM,
  2511. SNB_DISPLAY_DFT_SRWM,
  2512. 2,
  2513. SNB_FIFO_LINE_SIZE
  2514. };
  2515. static struct intel_watermark_params sandybridge_cursor_srwm_info = {
  2516. SNB_CURSOR_SR_FIFO,
  2517. SNB_CURSOR_MAX_SRWM,
  2518. SNB_CURSOR_DFT_SRWM,
  2519. 2,
  2520. SNB_FIFO_LINE_SIZE
  2521. };
  2522. /**
  2523. * intel_calculate_wm - calculate watermark level
  2524. * @clock_in_khz: pixel clock
  2525. * @wm: chip FIFO params
  2526. * @pixel_size: display pixel size
  2527. * @latency_ns: memory latency for the platform
  2528. *
  2529. * Calculate the watermark level (the level at which the display plane will
  2530. * start fetching from memory again). Each chip has a different display
  2531. * FIFO size and allocation, so the caller needs to figure that out and pass
  2532. * in the correct intel_watermark_params structure.
  2533. *
  2534. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  2535. * on the pixel size. When it reaches the watermark level, it'll start
  2536. * fetching FIFO line sized based chunks from memory until the FIFO fills
  2537. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  2538. * will occur, and a display engine hang could result.
  2539. */
  2540. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  2541. struct intel_watermark_params *wm,
  2542. int pixel_size,
  2543. unsigned long latency_ns)
  2544. {
  2545. long entries_required, wm_size;
  2546. /*
  2547. * Note: we need to make sure we don't overflow for various clock &
  2548. * latency values.
  2549. * clocks go from a few thousand to several hundred thousand.
  2550. * latency is usually a few thousand
  2551. */
  2552. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  2553. 1000;
  2554. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  2555. DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries_required);
  2556. wm_size = wm->fifo_size - (entries_required + wm->guard_size);
  2557. DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
  2558. /* Don't promote wm_size to unsigned... */
  2559. if (wm_size > (long)wm->max_wm)
  2560. wm_size = wm->max_wm;
  2561. if (wm_size <= 0)
  2562. wm_size = wm->default_wm;
  2563. return wm_size;
  2564. }
  2565. struct cxsr_latency {
  2566. int is_desktop;
  2567. int is_ddr3;
  2568. unsigned long fsb_freq;
  2569. unsigned long mem_freq;
  2570. unsigned long display_sr;
  2571. unsigned long display_hpll_disable;
  2572. unsigned long cursor_sr;
  2573. unsigned long cursor_hpll_disable;
  2574. };
  2575. static const struct cxsr_latency cxsr_latency_table[] = {
  2576. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  2577. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  2578. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  2579. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  2580. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  2581. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  2582. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  2583. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  2584. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  2585. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  2586. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  2587. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  2588. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  2589. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  2590. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  2591. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  2592. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  2593. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  2594. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  2595. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  2596. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  2597. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  2598. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  2599. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  2600. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  2601. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  2602. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  2603. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  2604. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  2605. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  2606. };
  2607. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  2608. int is_ddr3,
  2609. int fsb,
  2610. int mem)
  2611. {
  2612. const struct cxsr_latency *latency;
  2613. int i;
  2614. if (fsb == 0 || mem == 0)
  2615. return NULL;
  2616. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  2617. latency = &cxsr_latency_table[i];
  2618. if (is_desktop == latency->is_desktop &&
  2619. is_ddr3 == latency->is_ddr3 &&
  2620. fsb == latency->fsb_freq && mem == latency->mem_freq)
  2621. return latency;
  2622. }
  2623. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2624. return NULL;
  2625. }
  2626. static void pineview_disable_cxsr(struct drm_device *dev)
  2627. {
  2628. struct drm_i915_private *dev_priv = dev->dev_private;
  2629. /* deactivate cxsr */
  2630. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  2631. }
  2632. /*
  2633. * Latency for FIFO fetches is dependent on several factors:
  2634. * - memory configuration (speed, channels)
  2635. * - chipset
  2636. * - current MCH state
  2637. * It can be fairly high in some situations, so here we assume a fairly
  2638. * pessimal value. It's a tradeoff between extra memory fetches (if we
  2639. * set this value too high, the FIFO will fetch frequently to stay full)
  2640. * and power consumption (set it too low to save power and we might see
  2641. * FIFO underruns and display "flicker").
  2642. *
  2643. * A value of 5us seems to be a good balance; safe for very low end
  2644. * platforms but not overly aggressive on lower latency configs.
  2645. */
  2646. static const int latency_ns = 5000;
  2647. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  2648. {
  2649. struct drm_i915_private *dev_priv = dev->dev_private;
  2650. uint32_t dsparb = I915_READ(DSPARB);
  2651. int size;
  2652. size = dsparb & 0x7f;
  2653. if (plane)
  2654. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  2655. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2656. plane ? "B" : "A", size);
  2657. return size;
  2658. }
  2659. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  2660. {
  2661. struct drm_i915_private *dev_priv = dev->dev_private;
  2662. uint32_t dsparb = I915_READ(DSPARB);
  2663. int size;
  2664. size = dsparb & 0x1ff;
  2665. if (plane)
  2666. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  2667. size >>= 1; /* Convert to cachelines */
  2668. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2669. plane ? "B" : "A", size);
  2670. return size;
  2671. }
  2672. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  2673. {
  2674. struct drm_i915_private *dev_priv = dev->dev_private;
  2675. uint32_t dsparb = I915_READ(DSPARB);
  2676. int size;
  2677. size = dsparb & 0x7f;
  2678. size >>= 2; /* Convert to cachelines */
  2679. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2680. plane ? "B" : "A",
  2681. size);
  2682. return size;
  2683. }
  2684. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  2685. {
  2686. struct drm_i915_private *dev_priv = dev->dev_private;
  2687. uint32_t dsparb = I915_READ(DSPARB);
  2688. int size;
  2689. size = dsparb & 0x7f;
  2690. size >>= 1; /* Convert to cachelines */
  2691. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2692. plane ? "B" : "A", size);
  2693. return size;
  2694. }
  2695. static void pineview_update_wm(struct drm_device *dev, int planea_clock,
  2696. int planeb_clock, int sr_hdisplay, int unused,
  2697. int pixel_size)
  2698. {
  2699. struct drm_i915_private *dev_priv = dev->dev_private;
  2700. const struct cxsr_latency *latency;
  2701. u32 reg;
  2702. unsigned long wm;
  2703. int sr_clock;
  2704. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  2705. dev_priv->fsb_freq, dev_priv->mem_freq);
  2706. if (!latency) {
  2707. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2708. pineview_disable_cxsr(dev);
  2709. return;
  2710. }
  2711. if (!planea_clock || !planeb_clock) {
  2712. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2713. /* Display SR */
  2714. wm = intel_calculate_wm(sr_clock, &pineview_display_wm,
  2715. pixel_size, latency->display_sr);
  2716. reg = I915_READ(DSPFW1);
  2717. reg &= ~DSPFW_SR_MASK;
  2718. reg |= wm << DSPFW_SR_SHIFT;
  2719. I915_WRITE(DSPFW1, reg);
  2720. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  2721. /* cursor SR */
  2722. wm = intel_calculate_wm(sr_clock, &pineview_cursor_wm,
  2723. pixel_size, latency->cursor_sr);
  2724. reg = I915_READ(DSPFW3);
  2725. reg &= ~DSPFW_CURSOR_SR_MASK;
  2726. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  2727. I915_WRITE(DSPFW3, reg);
  2728. /* Display HPLL off SR */
  2729. wm = intel_calculate_wm(sr_clock, &pineview_display_hplloff_wm,
  2730. pixel_size, latency->display_hpll_disable);
  2731. reg = I915_READ(DSPFW3);
  2732. reg &= ~DSPFW_HPLL_SR_MASK;
  2733. reg |= wm & DSPFW_HPLL_SR_MASK;
  2734. I915_WRITE(DSPFW3, reg);
  2735. /* cursor HPLL off SR */
  2736. wm = intel_calculate_wm(sr_clock, &pineview_cursor_hplloff_wm,
  2737. pixel_size, latency->cursor_hpll_disable);
  2738. reg = I915_READ(DSPFW3);
  2739. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  2740. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  2741. I915_WRITE(DSPFW3, reg);
  2742. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  2743. /* activate cxsr */
  2744. I915_WRITE(DSPFW3,
  2745. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  2746. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  2747. } else {
  2748. pineview_disable_cxsr(dev);
  2749. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  2750. }
  2751. }
  2752. static void g4x_update_wm(struct drm_device *dev, int planea_clock,
  2753. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2754. int pixel_size)
  2755. {
  2756. struct drm_i915_private *dev_priv = dev->dev_private;
  2757. int total_size, cacheline_size;
  2758. int planea_wm, planeb_wm, cursora_wm, cursorb_wm, cursor_sr;
  2759. struct intel_watermark_params planea_params, planeb_params;
  2760. unsigned long line_time_us;
  2761. int sr_clock, sr_entries = 0, entries_required;
  2762. /* Create copies of the base settings for each pipe */
  2763. planea_params = planeb_params = g4x_wm_info;
  2764. /* Grab a couple of global values before we overwrite them */
  2765. total_size = planea_params.fifo_size;
  2766. cacheline_size = planea_params.cacheline_size;
  2767. /*
  2768. * Note: we need to make sure we don't overflow for various clock &
  2769. * latency values.
  2770. * clocks go from a few thousand to several hundred thousand.
  2771. * latency is usually a few thousand
  2772. */
  2773. entries_required = ((planea_clock / 1000) * pixel_size * latency_ns) /
  2774. 1000;
  2775. entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
  2776. planea_wm = entries_required + planea_params.guard_size;
  2777. entries_required = ((planeb_clock / 1000) * pixel_size * latency_ns) /
  2778. 1000;
  2779. entries_required = DIV_ROUND_UP(entries_required, G4X_FIFO_LINE_SIZE);
  2780. planeb_wm = entries_required + planeb_params.guard_size;
  2781. cursora_wm = cursorb_wm = 16;
  2782. cursor_sr = 32;
  2783. DRM_DEBUG("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  2784. /* Calc sr entries for one plane configs */
  2785. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  2786. /* self-refresh has much higher latency */
  2787. static const int sr_latency_ns = 12000;
  2788. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2789. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2790. /* Use ns/us then divide to preserve precision */
  2791. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2792. pixel_size * sr_hdisplay;
  2793. sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
  2794. entries_required = (((sr_latency_ns / line_time_us) +
  2795. 1000) / 1000) * pixel_size * 64;
  2796. entries_required = DIV_ROUND_UP(entries_required,
  2797. g4x_cursor_wm_info.cacheline_size);
  2798. cursor_sr = entries_required + g4x_cursor_wm_info.guard_size;
  2799. if (cursor_sr > g4x_cursor_wm_info.max_wm)
  2800. cursor_sr = g4x_cursor_wm_info.max_wm;
  2801. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  2802. "cursor %d\n", sr_entries, cursor_sr);
  2803. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  2804. } else {
  2805. /* Turn off self refresh if both pipes are enabled */
  2806. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2807. & ~FW_BLC_SELF_EN);
  2808. }
  2809. DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, SR %d\n",
  2810. planea_wm, planeb_wm, sr_entries);
  2811. planea_wm &= 0x3f;
  2812. planeb_wm &= 0x3f;
  2813. I915_WRITE(DSPFW1, (sr_entries << DSPFW_SR_SHIFT) |
  2814. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  2815. (planeb_wm << DSPFW_PLANEB_SHIFT) | planea_wm);
  2816. I915_WRITE(DSPFW2, (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  2817. (cursora_wm << DSPFW_CURSORA_SHIFT));
  2818. /* HPLL off in SR has some issues on G4x... disable it */
  2819. I915_WRITE(DSPFW3, (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  2820. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  2821. }
  2822. static void i965_update_wm(struct drm_device *dev, int planea_clock,
  2823. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2824. int pixel_size)
  2825. {
  2826. struct drm_i915_private *dev_priv = dev->dev_private;
  2827. unsigned long line_time_us;
  2828. int sr_clock, sr_entries, srwm = 1;
  2829. int cursor_sr = 16;
  2830. /* Calc sr entries for one plane configs */
  2831. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  2832. /* self-refresh has much higher latency */
  2833. static const int sr_latency_ns = 12000;
  2834. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2835. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2836. /* Use ns/us then divide to preserve precision */
  2837. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2838. pixel_size * sr_hdisplay;
  2839. sr_entries = DIV_ROUND_UP(sr_entries, I915_FIFO_LINE_SIZE);
  2840. DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
  2841. srwm = I965_FIFO_SIZE - sr_entries;
  2842. if (srwm < 0)
  2843. srwm = 1;
  2844. srwm &= 0x1ff;
  2845. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2846. pixel_size * 64;
  2847. sr_entries = DIV_ROUND_UP(sr_entries,
  2848. i965_cursor_wm_info.cacheline_size);
  2849. cursor_sr = i965_cursor_wm_info.fifo_size -
  2850. (sr_entries + i965_cursor_wm_info.guard_size);
  2851. if (cursor_sr > i965_cursor_wm_info.max_wm)
  2852. cursor_sr = i965_cursor_wm_info.max_wm;
  2853. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  2854. "cursor %d\n", srwm, cursor_sr);
  2855. if (IS_CRESTLINE(dev))
  2856. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  2857. } else {
  2858. /* Turn off self refresh if both pipes are enabled */
  2859. if (IS_CRESTLINE(dev))
  2860. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2861. & ~FW_BLC_SELF_EN);
  2862. }
  2863. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  2864. srwm);
  2865. /* 965 has limitations... */
  2866. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) | (8 << 16) | (8 << 8) |
  2867. (8 << 0));
  2868. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  2869. /* update cursor SR watermark */
  2870. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  2871. }
  2872. static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
  2873. int planeb_clock, int sr_hdisplay, int sr_htotal,
  2874. int pixel_size)
  2875. {
  2876. struct drm_i915_private *dev_priv = dev->dev_private;
  2877. uint32_t fwater_lo;
  2878. uint32_t fwater_hi;
  2879. int total_size, cacheline_size, cwm, srwm = 1;
  2880. int planea_wm, planeb_wm;
  2881. struct intel_watermark_params planea_params, planeb_params;
  2882. unsigned long line_time_us;
  2883. int sr_clock, sr_entries = 0;
  2884. /* Create copies of the base settings for each pipe */
  2885. if (IS_CRESTLINE(dev) || IS_I945GM(dev))
  2886. planea_params = planeb_params = i945_wm_info;
  2887. else if (!IS_GEN2(dev))
  2888. planea_params = planeb_params = i915_wm_info;
  2889. else
  2890. planea_params = planeb_params = i855_wm_info;
  2891. /* Grab a couple of global values before we overwrite them */
  2892. total_size = planea_params.fifo_size;
  2893. cacheline_size = planea_params.cacheline_size;
  2894. /* Update per-plane FIFO sizes */
  2895. planea_params.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  2896. planeb_params.fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  2897. planea_wm = intel_calculate_wm(planea_clock, &planea_params,
  2898. pixel_size, latency_ns);
  2899. planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
  2900. pixel_size, latency_ns);
  2901. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  2902. /*
  2903. * Overlay gets an aggressive default since video jitter is bad.
  2904. */
  2905. cwm = 2;
  2906. /* Calc sr entries for one plane configs */
  2907. if (HAS_FW_BLC(dev) && sr_hdisplay &&
  2908. (!planea_clock || !planeb_clock)) {
  2909. /* self-refresh has much higher latency */
  2910. static const int sr_latency_ns = 6000;
  2911. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2912. line_time_us = ((sr_htotal * 1000) / sr_clock);
  2913. /* Use ns/us then divide to preserve precision */
  2914. sr_entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  2915. pixel_size * sr_hdisplay;
  2916. sr_entries = DIV_ROUND_UP(sr_entries, cacheline_size);
  2917. DRM_DEBUG_KMS("self-refresh entries: %d\n", sr_entries);
  2918. srwm = total_size - sr_entries;
  2919. if (srwm < 0)
  2920. srwm = 1;
  2921. if (IS_I945G(dev) || IS_I945GM(dev))
  2922. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  2923. else if (IS_I915GM(dev)) {
  2924. /* 915M has a smaller SRWM field */
  2925. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  2926. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  2927. }
  2928. } else {
  2929. /* Turn off self refresh if both pipes are enabled */
  2930. if (IS_I945G(dev) || IS_I945GM(dev)) {
  2931. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2932. & ~FW_BLC_SELF_EN);
  2933. } else if (IS_I915GM(dev)) {
  2934. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  2935. }
  2936. }
  2937. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  2938. planea_wm, planeb_wm, cwm, srwm);
  2939. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  2940. fwater_hi = (cwm & 0x1f);
  2941. /* Set request length to 8 cachelines per fetch */
  2942. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  2943. fwater_hi = fwater_hi | (1 << 8);
  2944. I915_WRITE(FW_BLC, fwater_lo);
  2945. I915_WRITE(FW_BLC2, fwater_hi);
  2946. }
  2947. static void i830_update_wm(struct drm_device *dev, int planea_clock, int unused,
  2948. int unused2, int unused3, int pixel_size)
  2949. {
  2950. struct drm_i915_private *dev_priv = dev->dev_private;
  2951. uint32_t fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  2952. int planea_wm;
  2953. i830_wm_info.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  2954. planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
  2955. pixel_size, latency_ns);
  2956. fwater_lo |= (3<<8) | planea_wm;
  2957. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  2958. I915_WRITE(FW_BLC, fwater_lo);
  2959. }
  2960. #define ILK_LP0_PLANE_LATENCY 700
  2961. #define ILK_LP0_CURSOR_LATENCY 1300
  2962. static bool ironlake_compute_wm0(struct drm_device *dev,
  2963. int pipe,
  2964. const struct intel_watermark_params *display,
  2965. int display_latency_ns,
  2966. const struct intel_watermark_params *cursor,
  2967. int cursor_latency_ns,
  2968. int *plane_wm,
  2969. int *cursor_wm)
  2970. {
  2971. struct drm_crtc *crtc;
  2972. int htotal, hdisplay, clock, pixel_size = 0;
  2973. int line_time_us, line_count, entries;
  2974. crtc = intel_get_crtc_for_pipe(dev, pipe);
  2975. if (crtc->fb == NULL || !crtc->enabled)
  2976. return false;
  2977. htotal = crtc->mode.htotal;
  2978. hdisplay = crtc->mode.hdisplay;
  2979. clock = crtc->mode.clock;
  2980. pixel_size = crtc->fb->bits_per_pixel / 8;
  2981. /* Use the small buffer method to calculate plane watermark */
  2982. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  2983. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  2984. *plane_wm = entries + display->guard_size;
  2985. if (*plane_wm > (int)display->max_wm)
  2986. *plane_wm = display->max_wm;
  2987. /* Use the large buffer method to calculate cursor watermark */
  2988. line_time_us = ((htotal * 1000) / clock);
  2989. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  2990. entries = line_count * 64 * pixel_size;
  2991. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  2992. *cursor_wm = entries + cursor->guard_size;
  2993. if (*cursor_wm > (int)cursor->max_wm)
  2994. *cursor_wm = (int)cursor->max_wm;
  2995. return true;
  2996. }
  2997. /*
  2998. * Check the wm result.
  2999. *
  3000. * If any calculated watermark values is larger than the maximum value that
  3001. * can be programmed into the associated watermark register, that watermark
  3002. * must be disabled.
  3003. */
  3004. static bool ironlake_check_srwm(struct drm_device *dev, int level,
  3005. int fbc_wm, int display_wm, int cursor_wm,
  3006. const struct intel_watermark_params *display,
  3007. const struct intel_watermark_params *cursor)
  3008. {
  3009. struct drm_i915_private *dev_priv = dev->dev_private;
  3010. DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
  3011. " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
  3012. if (fbc_wm > SNB_FBC_MAX_SRWM) {
  3013. DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
  3014. fbc_wm, SNB_FBC_MAX_SRWM, level);
  3015. /* fbc has it's own way to disable FBC WM */
  3016. I915_WRITE(DISP_ARB_CTL,
  3017. I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
  3018. return false;
  3019. }
  3020. if (display_wm > display->max_wm) {
  3021. DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
  3022. display_wm, SNB_DISPLAY_MAX_SRWM, level);
  3023. return false;
  3024. }
  3025. if (cursor_wm > cursor->max_wm) {
  3026. DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
  3027. cursor_wm, SNB_CURSOR_MAX_SRWM, level);
  3028. return false;
  3029. }
  3030. if (!(fbc_wm || display_wm || cursor_wm)) {
  3031. DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
  3032. return false;
  3033. }
  3034. return true;
  3035. }
  3036. /*
  3037. * Compute watermark values of WM[1-3],
  3038. */
  3039. static bool ironlake_compute_srwm(struct drm_device *dev, int level,
  3040. int hdisplay, int htotal,
  3041. int pixel_size, int clock, int latency_ns,
  3042. const struct intel_watermark_params *display,
  3043. const struct intel_watermark_params *cursor,
  3044. int *fbc_wm, int *display_wm, int *cursor_wm)
  3045. {
  3046. unsigned long line_time_us;
  3047. int line_count, line_size;
  3048. int small, large;
  3049. int entries;
  3050. if (!latency_ns) {
  3051. *fbc_wm = *display_wm = *cursor_wm = 0;
  3052. return false;
  3053. }
  3054. line_time_us = (htotal * 1000) / clock;
  3055. line_count = (latency_ns / line_time_us + 1000) / 1000;
  3056. line_size = hdisplay * pixel_size;
  3057. /* Use the minimum of the small and large buffer method for primary */
  3058. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  3059. large = line_count * line_size;
  3060. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  3061. *display_wm = entries + display->guard_size;
  3062. /*
  3063. * Spec says:
  3064. * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
  3065. */
  3066. *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
  3067. /* calculate the self-refresh watermark for display cursor */
  3068. entries = line_count * pixel_size * 64;
  3069. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3070. *cursor_wm = entries + cursor->guard_size;
  3071. return ironlake_check_srwm(dev, level,
  3072. *fbc_wm, *display_wm, *cursor_wm,
  3073. display, cursor);
  3074. }
  3075. static void ironlake_update_wm(struct drm_device *dev,
  3076. int planea_clock, int planeb_clock,
  3077. int hdisplay, int htotal,
  3078. int pixel_size)
  3079. {
  3080. struct drm_i915_private *dev_priv = dev->dev_private;
  3081. int fbc_wm, plane_wm, cursor_wm, enabled;
  3082. int clock;
  3083. enabled = 0;
  3084. if (ironlake_compute_wm0(dev, 0,
  3085. &ironlake_display_wm_info,
  3086. ILK_LP0_PLANE_LATENCY,
  3087. &ironlake_cursor_wm_info,
  3088. ILK_LP0_CURSOR_LATENCY,
  3089. &plane_wm, &cursor_wm)) {
  3090. I915_WRITE(WM0_PIPEA_ILK,
  3091. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3092. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3093. " plane %d, " "cursor: %d\n",
  3094. plane_wm, cursor_wm);
  3095. enabled++;
  3096. }
  3097. if (ironlake_compute_wm0(dev, 1,
  3098. &ironlake_display_wm_info,
  3099. ILK_LP0_PLANE_LATENCY,
  3100. &ironlake_cursor_wm_info,
  3101. ILK_LP0_CURSOR_LATENCY,
  3102. &plane_wm, &cursor_wm)) {
  3103. I915_WRITE(WM0_PIPEB_ILK,
  3104. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3105. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3106. " plane %d, cursor: %d\n",
  3107. plane_wm, cursor_wm);
  3108. enabled++;
  3109. }
  3110. /*
  3111. * Calculate and update the self-refresh watermark only when one
  3112. * display plane is used.
  3113. */
  3114. I915_WRITE(WM3_LP_ILK, 0);
  3115. I915_WRITE(WM2_LP_ILK, 0);
  3116. I915_WRITE(WM1_LP_ILK, 0);
  3117. if (enabled != 1)
  3118. return;
  3119. clock = planea_clock ? planea_clock : planeb_clock;
  3120. /* WM1 */
  3121. if (!ironlake_compute_srwm(dev, 1, hdisplay, htotal, pixel_size,
  3122. clock, ILK_READ_WM1_LATENCY() * 500,
  3123. &ironlake_display_srwm_info,
  3124. &ironlake_cursor_srwm_info,
  3125. &fbc_wm, &plane_wm, &cursor_wm))
  3126. return;
  3127. I915_WRITE(WM1_LP_ILK,
  3128. WM1_LP_SR_EN |
  3129. (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3130. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3131. (plane_wm << WM1_LP_SR_SHIFT) |
  3132. cursor_wm);
  3133. /* WM2 */
  3134. if (!ironlake_compute_srwm(dev, 2, hdisplay, htotal, pixel_size,
  3135. clock, ILK_READ_WM2_LATENCY() * 500,
  3136. &ironlake_display_srwm_info,
  3137. &ironlake_cursor_srwm_info,
  3138. &fbc_wm, &plane_wm, &cursor_wm))
  3139. return;
  3140. I915_WRITE(WM2_LP_ILK,
  3141. WM2_LP_EN |
  3142. (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3143. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3144. (plane_wm << WM1_LP_SR_SHIFT) |
  3145. cursor_wm);
  3146. /*
  3147. * WM3 is unsupported on ILK, probably because we don't have latency
  3148. * data for that power state
  3149. */
  3150. }
  3151. static void sandybridge_update_wm(struct drm_device *dev,
  3152. int planea_clock, int planeb_clock,
  3153. int hdisplay, int htotal,
  3154. int pixel_size)
  3155. {
  3156. struct drm_i915_private *dev_priv = dev->dev_private;
  3157. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  3158. int fbc_wm, plane_wm, cursor_wm, enabled;
  3159. int clock;
  3160. enabled = 0;
  3161. if (ironlake_compute_wm0(dev, 0,
  3162. &sandybridge_display_wm_info, latency,
  3163. &sandybridge_cursor_wm_info, latency,
  3164. &plane_wm, &cursor_wm)) {
  3165. I915_WRITE(WM0_PIPEA_ILK,
  3166. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3167. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3168. " plane %d, " "cursor: %d\n",
  3169. plane_wm, cursor_wm);
  3170. enabled++;
  3171. }
  3172. if (ironlake_compute_wm0(dev, 1,
  3173. &sandybridge_display_wm_info, latency,
  3174. &sandybridge_cursor_wm_info, latency,
  3175. &plane_wm, &cursor_wm)) {
  3176. I915_WRITE(WM0_PIPEB_ILK,
  3177. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3178. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3179. " plane %d, cursor: %d\n",
  3180. plane_wm, cursor_wm);
  3181. enabled++;
  3182. }
  3183. /*
  3184. * Calculate and update the self-refresh watermark only when one
  3185. * display plane is used.
  3186. *
  3187. * SNB support 3 levels of watermark.
  3188. *
  3189. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  3190. * and disabled in the descending order
  3191. *
  3192. */
  3193. I915_WRITE(WM3_LP_ILK, 0);
  3194. I915_WRITE(WM2_LP_ILK, 0);
  3195. I915_WRITE(WM1_LP_ILK, 0);
  3196. if (enabled != 1)
  3197. return;
  3198. clock = planea_clock ? planea_clock : planeb_clock;
  3199. /* WM1 */
  3200. if (!ironlake_compute_srwm(dev, 1, hdisplay, htotal, pixel_size,
  3201. clock, SNB_READ_WM1_LATENCY() * 500,
  3202. &sandybridge_display_srwm_info,
  3203. &sandybridge_cursor_srwm_info,
  3204. &fbc_wm, &plane_wm, &cursor_wm))
  3205. return;
  3206. I915_WRITE(WM1_LP_ILK,
  3207. WM1_LP_SR_EN |
  3208. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3209. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3210. (plane_wm << WM1_LP_SR_SHIFT) |
  3211. cursor_wm);
  3212. /* WM2 */
  3213. if (!ironlake_compute_srwm(dev, 2,
  3214. hdisplay, htotal, pixel_size,
  3215. clock, SNB_READ_WM2_LATENCY() * 500,
  3216. &sandybridge_display_srwm_info,
  3217. &sandybridge_cursor_srwm_info,
  3218. &fbc_wm, &plane_wm, &cursor_wm))
  3219. return;
  3220. I915_WRITE(WM2_LP_ILK,
  3221. WM2_LP_EN |
  3222. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3223. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3224. (plane_wm << WM1_LP_SR_SHIFT) |
  3225. cursor_wm);
  3226. /* WM3 */
  3227. if (!ironlake_compute_srwm(dev, 3,
  3228. hdisplay, htotal, pixel_size,
  3229. clock, SNB_READ_WM3_LATENCY() * 500,
  3230. &sandybridge_display_srwm_info,
  3231. &sandybridge_cursor_srwm_info,
  3232. &fbc_wm, &plane_wm, &cursor_wm))
  3233. return;
  3234. I915_WRITE(WM3_LP_ILK,
  3235. WM3_LP_EN |
  3236. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3237. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3238. (plane_wm << WM1_LP_SR_SHIFT) |
  3239. cursor_wm);
  3240. }
  3241. /**
  3242. * intel_update_watermarks - update FIFO watermark values based on current modes
  3243. *
  3244. * Calculate watermark values for the various WM regs based on current mode
  3245. * and plane configuration.
  3246. *
  3247. * There are several cases to deal with here:
  3248. * - normal (i.e. non-self-refresh)
  3249. * - self-refresh (SR) mode
  3250. * - lines are large relative to FIFO size (buffer can hold up to 2)
  3251. * - lines are small relative to FIFO size (buffer can hold more than 2
  3252. * lines), so need to account for TLB latency
  3253. *
  3254. * The normal calculation is:
  3255. * watermark = dotclock * bytes per pixel * latency
  3256. * where latency is platform & configuration dependent (we assume pessimal
  3257. * values here).
  3258. *
  3259. * The SR calculation is:
  3260. * watermark = (trunc(latency/line time)+1) * surface width *
  3261. * bytes per pixel
  3262. * where
  3263. * line time = htotal / dotclock
  3264. * surface width = hdisplay for normal plane and 64 for cursor
  3265. * and latency is assumed to be high, as above.
  3266. *
  3267. * The final value programmed to the register should always be rounded up,
  3268. * and include an extra 2 entries to account for clock crossings.
  3269. *
  3270. * We don't use the sprite, so we can ignore that. And on Crestline we have
  3271. * to set the non-SR watermarks to 8.
  3272. */
  3273. static void intel_update_watermarks(struct drm_device *dev)
  3274. {
  3275. struct drm_i915_private *dev_priv = dev->dev_private;
  3276. struct drm_crtc *crtc;
  3277. int sr_hdisplay = 0;
  3278. unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
  3279. int enabled = 0, pixel_size = 0;
  3280. int sr_htotal = 0;
  3281. if (!dev_priv->display.update_wm)
  3282. return;
  3283. /* Get the clock config from both planes */
  3284. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3285. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3286. if (intel_crtc->active) {
  3287. enabled++;
  3288. if (intel_crtc->plane == 0) {
  3289. DRM_DEBUG_KMS("plane A (pipe %d) clock: %d\n",
  3290. intel_crtc->pipe, crtc->mode.clock);
  3291. planea_clock = crtc->mode.clock;
  3292. } else {
  3293. DRM_DEBUG_KMS("plane B (pipe %d) clock: %d\n",
  3294. intel_crtc->pipe, crtc->mode.clock);
  3295. planeb_clock = crtc->mode.clock;
  3296. }
  3297. sr_hdisplay = crtc->mode.hdisplay;
  3298. sr_clock = crtc->mode.clock;
  3299. sr_htotal = crtc->mode.htotal;
  3300. if (crtc->fb)
  3301. pixel_size = crtc->fb->bits_per_pixel / 8;
  3302. else
  3303. pixel_size = 4; /* by default */
  3304. }
  3305. }
  3306. if (enabled <= 0)
  3307. return;
  3308. dev_priv->display.update_wm(dev, planea_clock, planeb_clock,
  3309. sr_hdisplay, sr_htotal, pixel_size);
  3310. }
  3311. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  3312. struct drm_display_mode *mode,
  3313. struct drm_display_mode *adjusted_mode,
  3314. int x, int y,
  3315. struct drm_framebuffer *old_fb)
  3316. {
  3317. struct drm_device *dev = crtc->dev;
  3318. struct drm_i915_private *dev_priv = dev->dev_private;
  3319. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3320. int pipe = intel_crtc->pipe;
  3321. int plane = intel_crtc->plane;
  3322. u32 fp_reg, dpll_reg;
  3323. int refclk, num_connectors = 0;
  3324. intel_clock_t clock, reduced_clock;
  3325. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  3326. bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
  3327. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  3328. struct intel_encoder *has_edp_encoder = NULL;
  3329. struct drm_mode_config *mode_config = &dev->mode_config;
  3330. struct intel_encoder *encoder;
  3331. const intel_limit_t *limit;
  3332. int ret;
  3333. struct fdi_m_n m_n = {0};
  3334. u32 reg, temp;
  3335. int target_clock;
  3336. drm_vblank_pre_modeset(dev, pipe);
  3337. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  3338. if (encoder->base.crtc != crtc)
  3339. continue;
  3340. switch (encoder->type) {
  3341. case INTEL_OUTPUT_LVDS:
  3342. is_lvds = true;
  3343. break;
  3344. case INTEL_OUTPUT_SDVO:
  3345. case INTEL_OUTPUT_HDMI:
  3346. is_sdvo = true;
  3347. if (encoder->needs_tv_clock)
  3348. is_tv = true;
  3349. break;
  3350. case INTEL_OUTPUT_DVO:
  3351. is_dvo = true;
  3352. break;
  3353. case INTEL_OUTPUT_TVOUT:
  3354. is_tv = true;
  3355. break;
  3356. case INTEL_OUTPUT_ANALOG:
  3357. is_crt = true;
  3358. break;
  3359. case INTEL_OUTPUT_DISPLAYPORT:
  3360. is_dp = true;
  3361. break;
  3362. case INTEL_OUTPUT_EDP:
  3363. has_edp_encoder = encoder;
  3364. break;
  3365. }
  3366. num_connectors++;
  3367. }
  3368. if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2) {
  3369. refclk = dev_priv->lvds_ssc_freq * 1000;
  3370. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3371. refclk / 1000);
  3372. } else if (!IS_GEN2(dev)) {
  3373. refclk = 96000;
  3374. if (HAS_PCH_SPLIT(dev) &&
  3375. (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)))
  3376. refclk = 120000; /* 120Mhz refclk */
  3377. } else {
  3378. refclk = 48000;
  3379. }
  3380. /*
  3381. * Returns a set of divisors for the desired target clock with the given
  3382. * refclk, or FALSE. The returned values represent the clock equation:
  3383. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3384. */
  3385. limit = intel_limit(crtc, refclk);
  3386. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  3387. if (!ok) {
  3388. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3389. drm_vblank_post_modeset(dev, pipe);
  3390. return -EINVAL;
  3391. }
  3392. /* Ensure that the cursor is valid for the new mode before changing... */
  3393. intel_crtc_update_cursor(crtc, true);
  3394. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3395. has_reduced_clock = limit->find_pll(limit, crtc,
  3396. dev_priv->lvds_downclock,
  3397. refclk,
  3398. &reduced_clock);
  3399. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  3400. /*
  3401. * If the different P is found, it means that we can't
  3402. * switch the display clock by using the FP0/FP1.
  3403. * In such case we will disable the LVDS downclock
  3404. * feature.
  3405. */
  3406. DRM_DEBUG_KMS("Different P is found for "
  3407. "LVDS clock/downclock\n");
  3408. has_reduced_clock = 0;
  3409. }
  3410. }
  3411. /* SDVO TV has fixed PLL values depend on its clock range,
  3412. this mirrors vbios setting. */
  3413. if (is_sdvo && is_tv) {
  3414. if (adjusted_mode->clock >= 100000
  3415. && adjusted_mode->clock < 140500) {
  3416. clock.p1 = 2;
  3417. clock.p2 = 10;
  3418. clock.n = 3;
  3419. clock.m1 = 16;
  3420. clock.m2 = 8;
  3421. } else if (adjusted_mode->clock >= 140500
  3422. && adjusted_mode->clock <= 200000) {
  3423. clock.p1 = 1;
  3424. clock.p2 = 10;
  3425. clock.n = 6;
  3426. clock.m1 = 12;
  3427. clock.m2 = 8;
  3428. }
  3429. }
  3430. /* FDI link */
  3431. if (HAS_PCH_SPLIT(dev)) {
  3432. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3433. int lane = 0, link_bw, bpp;
  3434. /* CPU eDP doesn't require FDI link, so just set DP M/N
  3435. according to current link config */
  3436. if (has_edp_encoder && !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3437. target_clock = mode->clock;
  3438. intel_edp_link_config(has_edp_encoder,
  3439. &lane, &link_bw);
  3440. } else {
  3441. /* [e]DP over FDI requires target mode clock
  3442. instead of link clock */
  3443. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  3444. target_clock = mode->clock;
  3445. else
  3446. target_clock = adjusted_mode->clock;
  3447. /* FDI is a binary signal running at ~2.7GHz, encoding
  3448. * each output octet as 10 bits. The actual frequency
  3449. * is stored as a divider into a 100MHz clock, and the
  3450. * mode pixel clock is stored in units of 1KHz.
  3451. * Hence the bw of each lane in terms of the mode signal
  3452. * is:
  3453. */
  3454. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  3455. }
  3456. /* determine panel color depth */
  3457. temp = I915_READ(PIPECONF(pipe));
  3458. temp &= ~PIPE_BPC_MASK;
  3459. if (is_lvds) {
  3460. /* the BPC will be 6 if it is 18-bit LVDS panel */
  3461. if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
  3462. temp |= PIPE_8BPC;
  3463. else
  3464. temp |= PIPE_6BPC;
  3465. } else if (has_edp_encoder) {
  3466. switch (dev_priv->edp.bpp/3) {
  3467. case 8:
  3468. temp |= PIPE_8BPC;
  3469. break;
  3470. case 10:
  3471. temp |= PIPE_10BPC;
  3472. break;
  3473. case 6:
  3474. temp |= PIPE_6BPC;
  3475. break;
  3476. case 12:
  3477. temp |= PIPE_12BPC;
  3478. break;
  3479. }
  3480. } else
  3481. temp |= PIPE_8BPC;
  3482. I915_WRITE(PIPECONF(pipe), temp);
  3483. switch (temp & PIPE_BPC_MASK) {
  3484. case PIPE_8BPC:
  3485. bpp = 24;
  3486. break;
  3487. case PIPE_10BPC:
  3488. bpp = 30;
  3489. break;
  3490. case PIPE_6BPC:
  3491. bpp = 18;
  3492. break;
  3493. case PIPE_12BPC:
  3494. bpp = 36;
  3495. break;
  3496. default:
  3497. DRM_ERROR("unknown pipe bpc value\n");
  3498. bpp = 24;
  3499. }
  3500. if (!lane) {
  3501. /*
  3502. * Account for spread spectrum to avoid
  3503. * oversubscribing the link. Max center spread
  3504. * is 2.5%; use 5% for safety's sake.
  3505. */
  3506. u32 bps = target_clock * bpp * 21 / 20;
  3507. lane = bps / (link_bw * 8) + 1;
  3508. }
  3509. intel_crtc->fdi_lanes = lane;
  3510. if (pixel_multiplier > 1)
  3511. link_bw *= pixel_multiplier;
  3512. ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);
  3513. }
  3514. /* Ironlake: try to setup display ref clock before DPLL
  3515. * enabling. This is only under driver's control after
  3516. * PCH B stepping, previous chipset stepping should be
  3517. * ignoring this setting.
  3518. */
  3519. if (HAS_PCH_SPLIT(dev)) {
  3520. temp = I915_READ(PCH_DREF_CONTROL);
  3521. /* Always enable nonspread source */
  3522. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  3523. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  3524. temp &= ~DREF_SSC_SOURCE_MASK;
  3525. temp |= DREF_SSC_SOURCE_ENABLE;
  3526. I915_WRITE(PCH_DREF_CONTROL, temp);
  3527. POSTING_READ(PCH_DREF_CONTROL);
  3528. udelay(200);
  3529. if (has_edp_encoder) {
  3530. if (dev_priv->lvds_use_ssc) {
  3531. temp |= DREF_SSC1_ENABLE;
  3532. I915_WRITE(PCH_DREF_CONTROL, temp);
  3533. POSTING_READ(PCH_DREF_CONTROL);
  3534. udelay(200);
  3535. }
  3536. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  3537. /* Enable CPU source on CPU attached eDP */
  3538. if (!intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3539. if (dev_priv->lvds_use_ssc)
  3540. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  3541. else
  3542. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  3543. } else {
  3544. /* Enable SSC on PCH eDP if needed */
  3545. if (dev_priv->lvds_use_ssc) {
  3546. DRM_ERROR("enabling SSC on PCH\n");
  3547. temp |= DREF_SUPERSPREAD_SOURCE_ENABLE;
  3548. }
  3549. }
  3550. I915_WRITE(PCH_DREF_CONTROL, temp);
  3551. POSTING_READ(PCH_DREF_CONTROL);
  3552. udelay(200);
  3553. }
  3554. }
  3555. if (IS_PINEVIEW(dev)) {
  3556. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  3557. if (has_reduced_clock)
  3558. fp2 = (1 << reduced_clock.n) << 16 |
  3559. reduced_clock.m1 << 8 | reduced_clock.m2;
  3560. } else {
  3561. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  3562. if (has_reduced_clock)
  3563. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  3564. reduced_clock.m2;
  3565. }
  3566. /* Enable autotuning of the PLL clock (if permissible) */
  3567. if (HAS_PCH_SPLIT(dev)) {
  3568. int factor = 21;
  3569. if (is_lvds) {
  3570. if ((dev_priv->lvds_use_ssc &&
  3571. dev_priv->lvds_ssc_freq == 100) ||
  3572. (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
  3573. factor = 25;
  3574. } else if (is_sdvo && is_tv)
  3575. factor = 20;
  3576. if (clock.m1 < factor * clock.n)
  3577. fp |= FP_CB_TUNE;
  3578. }
  3579. dpll = 0;
  3580. if (!HAS_PCH_SPLIT(dev))
  3581. dpll = DPLL_VGA_MODE_DIS;
  3582. if (!IS_GEN2(dev)) {
  3583. if (is_lvds)
  3584. dpll |= DPLLB_MODE_LVDS;
  3585. else
  3586. dpll |= DPLLB_MODE_DAC_SERIAL;
  3587. if (is_sdvo) {
  3588. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3589. if (pixel_multiplier > 1) {
  3590. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3591. dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  3592. else if (HAS_PCH_SPLIT(dev))
  3593. dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  3594. }
  3595. dpll |= DPLL_DVO_HIGH_SPEED;
  3596. }
  3597. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  3598. dpll |= DPLL_DVO_HIGH_SPEED;
  3599. /* compute bitmask from p1 value */
  3600. if (IS_PINEVIEW(dev))
  3601. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  3602. else {
  3603. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3604. /* also FPA1 */
  3605. if (HAS_PCH_SPLIT(dev))
  3606. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3607. if (IS_G4X(dev) && has_reduced_clock)
  3608. dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3609. }
  3610. switch (clock.p2) {
  3611. case 5:
  3612. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3613. break;
  3614. case 7:
  3615. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3616. break;
  3617. case 10:
  3618. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3619. break;
  3620. case 14:
  3621. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3622. break;
  3623. }
  3624. if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev))
  3625. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  3626. } else {
  3627. if (is_lvds) {
  3628. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3629. } else {
  3630. if (clock.p1 == 2)
  3631. dpll |= PLL_P1_DIVIDE_BY_TWO;
  3632. else
  3633. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3634. if (clock.p2 == 4)
  3635. dpll |= PLL_P2_DIVIDE_BY_4;
  3636. }
  3637. }
  3638. if (is_sdvo && is_tv)
  3639. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3640. else if (is_tv)
  3641. /* XXX: just matching BIOS for now */
  3642. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3643. dpll |= 3;
  3644. else if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2)
  3645. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3646. else
  3647. dpll |= PLL_REF_INPUT_DREFCLK;
  3648. /* setup pipeconf */
  3649. pipeconf = I915_READ(PIPECONF(pipe));
  3650. /* Set up the display plane register */
  3651. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3652. /* Ironlake's plane is forced to pipe, bit 24 is to
  3653. enable color space conversion */
  3654. if (!HAS_PCH_SPLIT(dev)) {
  3655. if (pipe == 0)
  3656. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3657. else
  3658. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3659. }
  3660. if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  3661. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  3662. * core speed.
  3663. *
  3664. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  3665. * pipe == 0 check?
  3666. */
  3667. if (mode->clock >
  3668. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  3669. pipeconf |= PIPECONF_DOUBLE_WIDE;
  3670. else
  3671. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  3672. }
  3673. dspcntr |= DISPLAY_PLANE_ENABLE;
  3674. pipeconf |= PIPECONF_ENABLE;
  3675. dpll |= DPLL_VCO_ENABLE;
  3676. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  3677. drm_mode_debug_printmodeline(mode);
  3678. /* assign to Ironlake registers */
  3679. if (HAS_PCH_SPLIT(dev)) {
  3680. fp_reg = PCH_FP0(pipe);
  3681. dpll_reg = PCH_DPLL(pipe);
  3682. } else {
  3683. fp_reg = FP0(pipe);
  3684. dpll_reg = DPLL(pipe);
  3685. }
  3686. /* PCH eDP needs FDI, but CPU eDP does not */
  3687. if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3688. I915_WRITE(fp_reg, fp);
  3689. I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
  3690. POSTING_READ(dpll_reg);
  3691. udelay(150);
  3692. }
  3693. /* enable transcoder DPLL */
  3694. if (HAS_PCH_CPT(dev)) {
  3695. temp = I915_READ(PCH_DPLL_SEL);
  3696. if (pipe == 0)
  3697. temp |= TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL;
  3698. else
  3699. temp |= TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL;
  3700. I915_WRITE(PCH_DPLL_SEL, temp);
  3701. POSTING_READ(PCH_DPLL_SEL);
  3702. udelay(150);
  3703. }
  3704. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3705. * This is an exception to the general rule that mode_set doesn't turn
  3706. * things on.
  3707. */
  3708. if (is_lvds) {
  3709. reg = LVDS;
  3710. if (HAS_PCH_SPLIT(dev))
  3711. reg = PCH_LVDS;
  3712. temp = I915_READ(reg);
  3713. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  3714. if (pipe == 1) {
  3715. if (HAS_PCH_CPT(dev))
  3716. temp |= PORT_TRANS_B_SEL_CPT;
  3717. else
  3718. temp |= LVDS_PIPEB_SELECT;
  3719. } else {
  3720. if (HAS_PCH_CPT(dev))
  3721. temp &= ~PORT_TRANS_SEL_MASK;
  3722. else
  3723. temp &= ~LVDS_PIPEB_SELECT;
  3724. }
  3725. /* set the corresponsding LVDS_BORDER bit */
  3726. temp |= dev_priv->lvds_border_bits;
  3727. /* Set the B0-B3 data pairs corresponding to whether we're going to
  3728. * set the DPLLs for dual-channel mode or not.
  3729. */
  3730. if (clock.p2 == 7)
  3731. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  3732. else
  3733. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  3734. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  3735. * appropriately here, but we need to look more thoroughly into how
  3736. * panels behave in the two modes.
  3737. */
  3738. /* set the dithering flag on non-PCH LVDS as needed */
  3739. if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
  3740. if (dev_priv->lvds_dither)
  3741. temp |= LVDS_ENABLE_DITHER;
  3742. else
  3743. temp &= ~LVDS_ENABLE_DITHER;
  3744. }
  3745. I915_WRITE(reg, temp);
  3746. }
  3747. /* set the dithering flag and clear for anything other than a panel. */
  3748. if (HAS_PCH_SPLIT(dev)) {
  3749. pipeconf &= ~PIPECONF_DITHER_EN;
  3750. pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
  3751. if (dev_priv->lvds_dither && (is_lvds || has_edp_encoder)) {
  3752. pipeconf |= PIPECONF_DITHER_EN;
  3753. pipeconf |= PIPECONF_DITHER_TYPE_ST1;
  3754. }
  3755. }
  3756. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3757. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3758. } else if (HAS_PCH_SPLIT(dev)) {
  3759. /* For non-DP output, clear any trans DP clock recovery setting.*/
  3760. if (pipe == 0) {
  3761. I915_WRITE(TRANSA_DATA_M1, 0);
  3762. I915_WRITE(TRANSA_DATA_N1, 0);
  3763. I915_WRITE(TRANSA_DP_LINK_M1, 0);
  3764. I915_WRITE(TRANSA_DP_LINK_N1, 0);
  3765. } else {
  3766. I915_WRITE(TRANSB_DATA_M1, 0);
  3767. I915_WRITE(TRANSB_DATA_N1, 0);
  3768. I915_WRITE(TRANSB_DP_LINK_M1, 0);
  3769. I915_WRITE(TRANSB_DP_LINK_N1, 0);
  3770. }
  3771. }
  3772. if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3773. I915_WRITE(dpll_reg, dpll);
  3774. /* Wait for the clocks to stabilize. */
  3775. POSTING_READ(dpll_reg);
  3776. udelay(150);
  3777. if (INTEL_INFO(dev)->gen >= 4 && !HAS_PCH_SPLIT(dev)) {
  3778. temp = 0;
  3779. if (is_sdvo) {
  3780. temp = intel_mode_get_pixel_multiplier(adjusted_mode);
  3781. if (temp > 1)
  3782. temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3783. else
  3784. temp = 0;
  3785. }
  3786. I915_WRITE(DPLL_MD(pipe), temp);
  3787. } else {
  3788. /* The pixel multiplier can only be updated once the
  3789. * DPLL is enabled and the clocks are stable.
  3790. *
  3791. * So write it again.
  3792. */
  3793. I915_WRITE(dpll_reg, dpll);
  3794. }
  3795. }
  3796. intel_crtc->lowfreq_avail = false;
  3797. if (is_lvds && has_reduced_clock && i915_powersave) {
  3798. I915_WRITE(fp_reg + 4, fp2);
  3799. intel_crtc->lowfreq_avail = true;
  3800. if (HAS_PIPE_CXSR(dev)) {
  3801. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  3802. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  3803. }
  3804. } else {
  3805. I915_WRITE(fp_reg + 4, fp);
  3806. if (HAS_PIPE_CXSR(dev)) {
  3807. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  3808. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  3809. }
  3810. }
  3811. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3812. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  3813. /* the chip adds 2 halflines automatically */
  3814. adjusted_mode->crtc_vdisplay -= 1;
  3815. adjusted_mode->crtc_vtotal -= 1;
  3816. adjusted_mode->crtc_vblank_start -= 1;
  3817. adjusted_mode->crtc_vblank_end -= 1;
  3818. adjusted_mode->crtc_vsync_end -= 1;
  3819. adjusted_mode->crtc_vsync_start -= 1;
  3820. } else
  3821. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  3822. I915_WRITE(HTOTAL(pipe),
  3823. (adjusted_mode->crtc_hdisplay - 1) |
  3824. ((adjusted_mode->crtc_htotal - 1) << 16));
  3825. I915_WRITE(HBLANK(pipe),
  3826. (adjusted_mode->crtc_hblank_start - 1) |
  3827. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3828. I915_WRITE(HSYNC(pipe),
  3829. (adjusted_mode->crtc_hsync_start - 1) |
  3830. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3831. I915_WRITE(VTOTAL(pipe),
  3832. (adjusted_mode->crtc_vdisplay - 1) |
  3833. ((adjusted_mode->crtc_vtotal - 1) << 16));
  3834. I915_WRITE(VBLANK(pipe),
  3835. (adjusted_mode->crtc_vblank_start - 1) |
  3836. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  3837. I915_WRITE(VSYNC(pipe),
  3838. (adjusted_mode->crtc_vsync_start - 1) |
  3839. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3840. /* pipesrc and dspsize control the size that is scaled from,
  3841. * which should always be the user's requested size.
  3842. */
  3843. if (!HAS_PCH_SPLIT(dev)) {
  3844. I915_WRITE(DSPSIZE(plane),
  3845. ((mode->vdisplay - 1) << 16) |
  3846. (mode->hdisplay - 1));
  3847. I915_WRITE(DSPPOS(plane), 0);
  3848. }
  3849. I915_WRITE(PIPESRC(pipe),
  3850. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3851. if (HAS_PCH_SPLIT(dev)) {
  3852. I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
  3853. I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
  3854. I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
  3855. I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
  3856. if (has_edp_encoder && !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  3857. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  3858. }
  3859. }
  3860. I915_WRITE(PIPECONF(pipe), pipeconf);
  3861. POSTING_READ(PIPECONF(pipe));
  3862. intel_wait_for_vblank(dev, pipe);
  3863. if (IS_GEN5(dev)) {
  3864. /* enable address swizzle for tiling buffer */
  3865. temp = I915_READ(DISP_ARB_CTL);
  3866. I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
  3867. }
  3868. I915_WRITE(DSPCNTR(plane), dspcntr);
  3869. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  3870. intel_update_watermarks(dev);
  3871. drm_vblank_post_modeset(dev, pipe);
  3872. return ret;
  3873. }
  3874. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3875. void intel_crtc_load_lut(struct drm_crtc *crtc)
  3876. {
  3877. struct drm_device *dev = crtc->dev;
  3878. struct drm_i915_private *dev_priv = dev->dev_private;
  3879. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3880. int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
  3881. int i;
  3882. /* The clocks have to be on to load the palette. */
  3883. if (!crtc->enabled)
  3884. return;
  3885. /* use legacy palette for Ironlake */
  3886. if (HAS_PCH_SPLIT(dev))
  3887. palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
  3888. LGC_PALETTE_B;
  3889. for (i = 0; i < 256; i++) {
  3890. I915_WRITE(palreg + 4 * i,
  3891. (intel_crtc->lut_r[i] << 16) |
  3892. (intel_crtc->lut_g[i] << 8) |
  3893. intel_crtc->lut_b[i]);
  3894. }
  3895. }
  3896. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  3897. {
  3898. struct drm_device *dev = crtc->dev;
  3899. struct drm_i915_private *dev_priv = dev->dev_private;
  3900. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3901. bool visible = base != 0;
  3902. u32 cntl;
  3903. if (intel_crtc->cursor_visible == visible)
  3904. return;
  3905. cntl = I915_READ(CURACNTR);
  3906. if (visible) {
  3907. /* On these chipsets we can only modify the base whilst
  3908. * the cursor is disabled.
  3909. */
  3910. I915_WRITE(CURABASE, base);
  3911. cntl &= ~(CURSOR_FORMAT_MASK);
  3912. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  3913. cntl |= CURSOR_ENABLE |
  3914. CURSOR_GAMMA_ENABLE |
  3915. CURSOR_FORMAT_ARGB;
  3916. } else
  3917. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  3918. I915_WRITE(CURACNTR, cntl);
  3919. intel_crtc->cursor_visible = visible;
  3920. }
  3921. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  3922. {
  3923. struct drm_device *dev = crtc->dev;
  3924. struct drm_i915_private *dev_priv = dev->dev_private;
  3925. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3926. int pipe = intel_crtc->pipe;
  3927. bool visible = base != 0;
  3928. if (intel_crtc->cursor_visible != visible) {
  3929. uint32_t cntl = I915_READ(pipe == 0 ? CURACNTR : CURBCNTR);
  3930. if (base) {
  3931. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  3932. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  3933. cntl |= pipe << 28; /* Connect to correct pipe */
  3934. } else {
  3935. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  3936. cntl |= CURSOR_MODE_DISABLE;
  3937. }
  3938. I915_WRITE(pipe == 0 ? CURACNTR : CURBCNTR, cntl);
  3939. intel_crtc->cursor_visible = visible;
  3940. }
  3941. /* and commit changes on next vblank */
  3942. I915_WRITE(pipe == 0 ? CURABASE : CURBBASE, base);
  3943. }
  3944. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  3945. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  3946. bool on)
  3947. {
  3948. struct drm_device *dev = crtc->dev;
  3949. struct drm_i915_private *dev_priv = dev->dev_private;
  3950. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3951. int pipe = intel_crtc->pipe;
  3952. int x = intel_crtc->cursor_x;
  3953. int y = intel_crtc->cursor_y;
  3954. u32 base, pos;
  3955. bool visible;
  3956. pos = 0;
  3957. if (on && crtc->enabled && crtc->fb) {
  3958. base = intel_crtc->cursor_addr;
  3959. if (x > (int) crtc->fb->width)
  3960. base = 0;
  3961. if (y > (int) crtc->fb->height)
  3962. base = 0;
  3963. } else
  3964. base = 0;
  3965. if (x < 0) {
  3966. if (x + intel_crtc->cursor_width < 0)
  3967. base = 0;
  3968. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  3969. x = -x;
  3970. }
  3971. pos |= x << CURSOR_X_SHIFT;
  3972. if (y < 0) {
  3973. if (y + intel_crtc->cursor_height < 0)
  3974. base = 0;
  3975. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  3976. y = -y;
  3977. }
  3978. pos |= y << CURSOR_Y_SHIFT;
  3979. visible = base != 0;
  3980. if (!visible && !intel_crtc->cursor_visible)
  3981. return;
  3982. I915_WRITE(pipe == 0 ? CURAPOS : CURBPOS, pos);
  3983. if (IS_845G(dev) || IS_I865G(dev))
  3984. i845_update_cursor(crtc, base);
  3985. else
  3986. i9xx_update_cursor(crtc, base);
  3987. if (visible)
  3988. intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
  3989. }
  3990. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  3991. struct drm_file *file,
  3992. uint32_t handle,
  3993. uint32_t width, uint32_t height)
  3994. {
  3995. struct drm_device *dev = crtc->dev;
  3996. struct drm_i915_private *dev_priv = dev->dev_private;
  3997. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3998. struct drm_i915_gem_object *obj;
  3999. uint32_t addr;
  4000. int ret;
  4001. DRM_DEBUG_KMS("\n");
  4002. /* if we want to turn off the cursor ignore width and height */
  4003. if (!handle) {
  4004. DRM_DEBUG_KMS("cursor off\n");
  4005. addr = 0;
  4006. obj = NULL;
  4007. mutex_lock(&dev->struct_mutex);
  4008. goto finish;
  4009. }
  4010. /* Currently we only support 64x64 cursors */
  4011. if (width != 64 || height != 64) {
  4012. DRM_ERROR("we currently only support 64x64 cursors\n");
  4013. return -EINVAL;
  4014. }
  4015. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  4016. if (!obj)
  4017. return -ENOENT;
  4018. if (obj->base.size < width * height * 4) {
  4019. DRM_ERROR("buffer is to small\n");
  4020. ret = -ENOMEM;
  4021. goto fail;
  4022. }
  4023. /* we only need to pin inside GTT if cursor is non-phy */
  4024. mutex_lock(&dev->struct_mutex);
  4025. if (!dev_priv->info->cursor_needs_physical) {
  4026. if (obj->tiling_mode) {
  4027. DRM_ERROR("cursor cannot be tiled\n");
  4028. ret = -EINVAL;
  4029. goto fail_locked;
  4030. }
  4031. ret = i915_gem_object_pin(obj, PAGE_SIZE, true);
  4032. if (ret) {
  4033. DRM_ERROR("failed to pin cursor bo\n");
  4034. goto fail_locked;
  4035. }
  4036. ret = i915_gem_object_set_to_gtt_domain(obj, 0);
  4037. if (ret) {
  4038. DRM_ERROR("failed to move cursor bo into the GTT\n");
  4039. goto fail_unpin;
  4040. }
  4041. ret = i915_gem_object_put_fence(obj);
  4042. if (ret) {
  4043. DRM_ERROR("failed to move cursor bo into the GTT\n");
  4044. goto fail_unpin;
  4045. }
  4046. addr = obj->gtt_offset;
  4047. } else {
  4048. int align = IS_I830(dev) ? 16 * 1024 : 256;
  4049. ret = i915_gem_attach_phys_object(dev, obj,
  4050. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  4051. align);
  4052. if (ret) {
  4053. DRM_ERROR("failed to attach phys object\n");
  4054. goto fail_locked;
  4055. }
  4056. addr = obj->phys_obj->handle->busaddr;
  4057. }
  4058. if (IS_GEN2(dev))
  4059. I915_WRITE(CURSIZE, (height << 12) | width);
  4060. finish:
  4061. if (intel_crtc->cursor_bo) {
  4062. if (dev_priv->info->cursor_needs_physical) {
  4063. if (intel_crtc->cursor_bo != obj)
  4064. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  4065. } else
  4066. i915_gem_object_unpin(intel_crtc->cursor_bo);
  4067. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  4068. }
  4069. mutex_unlock(&dev->struct_mutex);
  4070. intel_crtc->cursor_addr = addr;
  4071. intel_crtc->cursor_bo = obj;
  4072. intel_crtc->cursor_width = width;
  4073. intel_crtc->cursor_height = height;
  4074. intel_crtc_update_cursor(crtc, true);
  4075. return 0;
  4076. fail_unpin:
  4077. i915_gem_object_unpin(obj);
  4078. fail_locked:
  4079. mutex_unlock(&dev->struct_mutex);
  4080. fail:
  4081. drm_gem_object_unreference_unlocked(&obj->base);
  4082. return ret;
  4083. }
  4084. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  4085. {
  4086. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4087. intel_crtc->cursor_x = x;
  4088. intel_crtc->cursor_y = y;
  4089. intel_crtc_update_cursor(crtc, true);
  4090. return 0;
  4091. }
  4092. /** Sets the color ramps on behalf of RandR */
  4093. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  4094. u16 blue, int regno)
  4095. {
  4096. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4097. intel_crtc->lut_r[regno] = red >> 8;
  4098. intel_crtc->lut_g[regno] = green >> 8;
  4099. intel_crtc->lut_b[regno] = blue >> 8;
  4100. }
  4101. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  4102. u16 *blue, int regno)
  4103. {
  4104. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4105. *red = intel_crtc->lut_r[regno] << 8;
  4106. *green = intel_crtc->lut_g[regno] << 8;
  4107. *blue = intel_crtc->lut_b[regno] << 8;
  4108. }
  4109. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  4110. u16 *blue, uint32_t start, uint32_t size)
  4111. {
  4112. int end = (start + size > 256) ? 256 : start + size, i;
  4113. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4114. for (i = start; i < end; i++) {
  4115. intel_crtc->lut_r[i] = red[i] >> 8;
  4116. intel_crtc->lut_g[i] = green[i] >> 8;
  4117. intel_crtc->lut_b[i] = blue[i] >> 8;
  4118. }
  4119. intel_crtc_load_lut(crtc);
  4120. }
  4121. /**
  4122. * Get a pipe with a simple mode set on it for doing load-based monitor
  4123. * detection.
  4124. *
  4125. * It will be up to the load-detect code to adjust the pipe as appropriate for
  4126. * its requirements. The pipe will be connected to no other encoders.
  4127. *
  4128. * Currently this code will only succeed if there is a pipe with no encoders
  4129. * configured for it. In the future, it could choose to temporarily disable
  4130. * some outputs to free up a pipe for its use.
  4131. *
  4132. * \return crtc, or NULL if no pipes are available.
  4133. */
  4134. /* VESA 640x480x72Hz mode to set on the pipe */
  4135. static struct drm_display_mode load_detect_mode = {
  4136. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  4137. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  4138. };
  4139. struct drm_crtc *intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  4140. struct drm_connector *connector,
  4141. struct drm_display_mode *mode,
  4142. int *dpms_mode)
  4143. {
  4144. struct intel_crtc *intel_crtc;
  4145. struct drm_crtc *possible_crtc;
  4146. struct drm_crtc *supported_crtc =NULL;
  4147. struct drm_encoder *encoder = &intel_encoder->base;
  4148. struct drm_crtc *crtc = NULL;
  4149. struct drm_device *dev = encoder->dev;
  4150. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  4151. struct drm_crtc_helper_funcs *crtc_funcs;
  4152. int i = -1;
  4153. /*
  4154. * Algorithm gets a little messy:
  4155. * - if the connector already has an assigned crtc, use it (but make
  4156. * sure it's on first)
  4157. * - try to find the first unused crtc that can drive this connector,
  4158. * and use that if we find one
  4159. * - if there are no unused crtcs available, try to use the first
  4160. * one we found that supports the connector
  4161. */
  4162. /* See if we already have a CRTC for this connector */
  4163. if (encoder->crtc) {
  4164. crtc = encoder->crtc;
  4165. /* Make sure the crtc and connector are running */
  4166. intel_crtc = to_intel_crtc(crtc);
  4167. *dpms_mode = intel_crtc->dpms_mode;
  4168. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  4169. crtc_funcs = crtc->helper_private;
  4170. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  4171. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  4172. }
  4173. return crtc;
  4174. }
  4175. /* Find an unused one (if possible) */
  4176. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  4177. i++;
  4178. if (!(encoder->possible_crtcs & (1 << i)))
  4179. continue;
  4180. if (!possible_crtc->enabled) {
  4181. crtc = possible_crtc;
  4182. break;
  4183. }
  4184. if (!supported_crtc)
  4185. supported_crtc = possible_crtc;
  4186. }
  4187. /*
  4188. * If we didn't find an unused CRTC, don't use any.
  4189. */
  4190. if (!crtc) {
  4191. return NULL;
  4192. }
  4193. encoder->crtc = crtc;
  4194. connector->encoder = encoder;
  4195. intel_encoder->load_detect_temp = true;
  4196. intel_crtc = to_intel_crtc(crtc);
  4197. *dpms_mode = intel_crtc->dpms_mode;
  4198. if (!crtc->enabled) {
  4199. if (!mode)
  4200. mode = &load_detect_mode;
  4201. drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
  4202. } else {
  4203. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  4204. crtc_funcs = crtc->helper_private;
  4205. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  4206. }
  4207. /* Add this connector to the crtc */
  4208. encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
  4209. encoder_funcs->commit(encoder);
  4210. }
  4211. /* let the connector get through one full cycle before testing */
  4212. intel_wait_for_vblank(dev, intel_crtc->pipe);
  4213. return crtc;
  4214. }
  4215. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  4216. struct drm_connector *connector, int dpms_mode)
  4217. {
  4218. struct drm_encoder *encoder = &intel_encoder->base;
  4219. struct drm_device *dev = encoder->dev;
  4220. struct drm_crtc *crtc = encoder->crtc;
  4221. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  4222. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  4223. if (intel_encoder->load_detect_temp) {
  4224. encoder->crtc = NULL;
  4225. connector->encoder = NULL;
  4226. intel_encoder->load_detect_temp = false;
  4227. crtc->enabled = drm_helper_crtc_in_use(crtc);
  4228. drm_helper_disable_unused_functions(dev);
  4229. }
  4230. /* Switch crtc and encoder back off if necessary */
  4231. if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
  4232. if (encoder->crtc == crtc)
  4233. encoder_funcs->dpms(encoder, dpms_mode);
  4234. crtc_funcs->dpms(crtc, dpms_mode);
  4235. }
  4236. }
  4237. /* Returns the clock of the currently programmed mode of the given pipe. */
  4238. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  4239. {
  4240. struct drm_i915_private *dev_priv = dev->dev_private;
  4241. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4242. int pipe = intel_crtc->pipe;
  4243. u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
  4244. u32 fp;
  4245. intel_clock_t clock;
  4246. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  4247. fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
  4248. else
  4249. fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
  4250. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  4251. if (IS_PINEVIEW(dev)) {
  4252. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  4253. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4254. } else {
  4255. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  4256. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4257. }
  4258. if (!IS_GEN2(dev)) {
  4259. if (IS_PINEVIEW(dev))
  4260. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  4261. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  4262. else
  4263. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  4264. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4265. switch (dpll & DPLL_MODE_MASK) {
  4266. case DPLLB_MODE_DAC_SERIAL:
  4267. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  4268. 5 : 10;
  4269. break;
  4270. case DPLLB_MODE_LVDS:
  4271. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  4272. 7 : 14;
  4273. break;
  4274. default:
  4275. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  4276. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  4277. return 0;
  4278. }
  4279. /* XXX: Handle the 100Mhz refclk */
  4280. intel_clock(dev, 96000, &clock);
  4281. } else {
  4282. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  4283. if (is_lvds) {
  4284. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  4285. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4286. clock.p2 = 14;
  4287. if ((dpll & PLL_REF_INPUT_MASK) ==
  4288. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  4289. /* XXX: might not be 66MHz */
  4290. intel_clock(dev, 66000, &clock);
  4291. } else
  4292. intel_clock(dev, 48000, &clock);
  4293. } else {
  4294. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  4295. clock.p1 = 2;
  4296. else {
  4297. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  4298. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  4299. }
  4300. if (dpll & PLL_P2_DIVIDE_BY_4)
  4301. clock.p2 = 4;
  4302. else
  4303. clock.p2 = 2;
  4304. intel_clock(dev, 48000, &clock);
  4305. }
  4306. }
  4307. /* XXX: It would be nice to validate the clocks, but we can't reuse
  4308. * i830PllIsValid() because it relies on the xf86_config connector
  4309. * configuration being accurate, which it isn't necessarily.
  4310. */
  4311. return clock.dot;
  4312. }
  4313. /** Returns the currently programmed mode of the given pipe. */
  4314. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  4315. struct drm_crtc *crtc)
  4316. {
  4317. struct drm_i915_private *dev_priv = dev->dev_private;
  4318. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4319. int pipe = intel_crtc->pipe;
  4320. struct drm_display_mode *mode;
  4321. int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  4322. int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  4323. int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  4324. int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  4325. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  4326. if (!mode)
  4327. return NULL;
  4328. mode->clock = intel_crtc_clock_get(dev, crtc);
  4329. mode->hdisplay = (htot & 0xffff) + 1;
  4330. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  4331. mode->hsync_start = (hsync & 0xffff) + 1;
  4332. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  4333. mode->vdisplay = (vtot & 0xffff) + 1;
  4334. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  4335. mode->vsync_start = (vsync & 0xffff) + 1;
  4336. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  4337. drm_mode_set_name(mode);
  4338. drm_mode_set_crtcinfo(mode, 0);
  4339. return mode;
  4340. }
  4341. #define GPU_IDLE_TIMEOUT 500 /* ms */
  4342. /* When this timer fires, we've been idle for awhile */
  4343. static void intel_gpu_idle_timer(unsigned long arg)
  4344. {
  4345. struct drm_device *dev = (struct drm_device *)arg;
  4346. drm_i915_private_t *dev_priv = dev->dev_private;
  4347. if (!list_empty(&dev_priv->mm.active_list)) {
  4348. /* Still processing requests, so just re-arm the timer. */
  4349. mod_timer(&dev_priv->idle_timer, jiffies +
  4350. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4351. return;
  4352. }
  4353. dev_priv->busy = false;
  4354. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4355. }
  4356. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  4357. static void intel_crtc_idle_timer(unsigned long arg)
  4358. {
  4359. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  4360. struct drm_crtc *crtc = &intel_crtc->base;
  4361. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  4362. struct intel_framebuffer *intel_fb;
  4363. intel_fb = to_intel_framebuffer(crtc->fb);
  4364. if (intel_fb && intel_fb->obj->active) {
  4365. /* The framebuffer is still being accessed by the GPU. */
  4366. mod_timer(&intel_crtc->idle_timer, jiffies +
  4367. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4368. return;
  4369. }
  4370. intel_crtc->busy = false;
  4371. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4372. }
  4373. static void intel_increase_pllclock(struct drm_crtc *crtc)
  4374. {
  4375. struct drm_device *dev = crtc->dev;
  4376. drm_i915_private_t *dev_priv = dev->dev_private;
  4377. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4378. int pipe = intel_crtc->pipe;
  4379. int dpll_reg = DPLL(pipe);
  4380. int dpll;
  4381. if (HAS_PCH_SPLIT(dev))
  4382. return;
  4383. if (!dev_priv->lvds_downclock_avail)
  4384. return;
  4385. dpll = I915_READ(dpll_reg);
  4386. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  4387. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  4388. /* Unlock panel regs */
  4389. I915_WRITE(PP_CONTROL,
  4390. I915_READ(PP_CONTROL) | PANEL_UNLOCK_REGS);
  4391. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  4392. I915_WRITE(dpll_reg, dpll);
  4393. POSTING_READ(dpll_reg);
  4394. intel_wait_for_vblank(dev, pipe);
  4395. dpll = I915_READ(dpll_reg);
  4396. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  4397. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  4398. /* ...and lock them again */
  4399. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  4400. }
  4401. /* Schedule downclock */
  4402. mod_timer(&intel_crtc->idle_timer, jiffies +
  4403. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4404. }
  4405. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  4406. {
  4407. struct drm_device *dev = crtc->dev;
  4408. drm_i915_private_t *dev_priv = dev->dev_private;
  4409. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4410. int pipe = intel_crtc->pipe;
  4411. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  4412. int dpll = I915_READ(dpll_reg);
  4413. if (HAS_PCH_SPLIT(dev))
  4414. return;
  4415. if (!dev_priv->lvds_downclock_avail)
  4416. return;
  4417. /*
  4418. * Since this is called by a timer, we should never get here in
  4419. * the manual case.
  4420. */
  4421. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  4422. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  4423. /* Unlock panel regs */
  4424. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  4425. PANEL_UNLOCK_REGS);
  4426. dpll |= DISPLAY_RATE_SELECT_FPA1;
  4427. I915_WRITE(dpll_reg, dpll);
  4428. dpll = I915_READ(dpll_reg);
  4429. intel_wait_for_vblank(dev, pipe);
  4430. dpll = I915_READ(dpll_reg);
  4431. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  4432. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  4433. /* ...and lock them again */
  4434. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  4435. }
  4436. }
  4437. /**
  4438. * intel_idle_update - adjust clocks for idleness
  4439. * @work: work struct
  4440. *
  4441. * Either the GPU or display (or both) went idle. Check the busy status
  4442. * here and adjust the CRTC and GPU clocks as necessary.
  4443. */
  4444. static void intel_idle_update(struct work_struct *work)
  4445. {
  4446. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  4447. idle_work);
  4448. struct drm_device *dev = dev_priv->dev;
  4449. struct drm_crtc *crtc;
  4450. struct intel_crtc *intel_crtc;
  4451. int enabled = 0;
  4452. if (!i915_powersave)
  4453. return;
  4454. mutex_lock(&dev->struct_mutex);
  4455. i915_update_gfx_val(dev_priv);
  4456. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4457. /* Skip inactive CRTCs */
  4458. if (!crtc->fb)
  4459. continue;
  4460. enabled++;
  4461. intel_crtc = to_intel_crtc(crtc);
  4462. if (!intel_crtc->busy)
  4463. intel_decrease_pllclock(crtc);
  4464. }
  4465. if ((enabled == 1) && (IS_I945G(dev) || IS_I945GM(dev))) {
  4466. DRM_DEBUG_DRIVER("enable memory self refresh on 945\n");
  4467. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  4468. }
  4469. mutex_unlock(&dev->struct_mutex);
  4470. }
  4471. /**
  4472. * intel_mark_busy - mark the GPU and possibly the display busy
  4473. * @dev: drm device
  4474. * @obj: object we're operating on
  4475. *
  4476. * Callers can use this function to indicate that the GPU is busy processing
  4477. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  4478. * buffer), we'll also mark the display as busy, so we know to increase its
  4479. * clock frequency.
  4480. */
  4481. void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
  4482. {
  4483. drm_i915_private_t *dev_priv = dev->dev_private;
  4484. struct drm_crtc *crtc = NULL;
  4485. struct intel_framebuffer *intel_fb;
  4486. struct intel_crtc *intel_crtc;
  4487. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4488. return;
  4489. if (!dev_priv->busy) {
  4490. if (IS_I945G(dev) || IS_I945GM(dev)) {
  4491. u32 fw_blc_self;
  4492. DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
  4493. fw_blc_self = I915_READ(FW_BLC_SELF);
  4494. fw_blc_self &= ~FW_BLC_SELF_EN;
  4495. I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
  4496. }
  4497. dev_priv->busy = true;
  4498. } else
  4499. mod_timer(&dev_priv->idle_timer, jiffies +
  4500. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4501. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4502. if (!crtc->fb)
  4503. continue;
  4504. intel_crtc = to_intel_crtc(crtc);
  4505. intel_fb = to_intel_framebuffer(crtc->fb);
  4506. if (intel_fb->obj == obj) {
  4507. if (!intel_crtc->busy) {
  4508. if (IS_I945G(dev) || IS_I945GM(dev)) {
  4509. u32 fw_blc_self;
  4510. DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
  4511. fw_blc_self = I915_READ(FW_BLC_SELF);
  4512. fw_blc_self &= ~FW_BLC_SELF_EN;
  4513. I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
  4514. }
  4515. /* Non-busy -> busy, upclock */
  4516. intel_increase_pllclock(crtc);
  4517. intel_crtc->busy = true;
  4518. } else {
  4519. /* Busy -> busy, put off timer */
  4520. mod_timer(&intel_crtc->idle_timer, jiffies +
  4521. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4522. }
  4523. }
  4524. }
  4525. }
  4526. static void intel_crtc_destroy(struct drm_crtc *crtc)
  4527. {
  4528. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4529. struct drm_device *dev = crtc->dev;
  4530. struct intel_unpin_work *work;
  4531. unsigned long flags;
  4532. spin_lock_irqsave(&dev->event_lock, flags);
  4533. work = intel_crtc->unpin_work;
  4534. intel_crtc->unpin_work = NULL;
  4535. spin_unlock_irqrestore(&dev->event_lock, flags);
  4536. if (work) {
  4537. cancel_work_sync(&work->work);
  4538. kfree(work);
  4539. }
  4540. drm_crtc_cleanup(crtc);
  4541. kfree(intel_crtc);
  4542. }
  4543. static void intel_unpin_work_fn(struct work_struct *__work)
  4544. {
  4545. struct intel_unpin_work *work =
  4546. container_of(__work, struct intel_unpin_work, work);
  4547. mutex_lock(&work->dev->struct_mutex);
  4548. i915_gem_object_unpin(work->old_fb_obj);
  4549. drm_gem_object_unreference(&work->pending_flip_obj->base);
  4550. drm_gem_object_unreference(&work->old_fb_obj->base);
  4551. mutex_unlock(&work->dev->struct_mutex);
  4552. kfree(work);
  4553. }
  4554. static void do_intel_finish_page_flip(struct drm_device *dev,
  4555. struct drm_crtc *crtc)
  4556. {
  4557. drm_i915_private_t *dev_priv = dev->dev_private;
  4558. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4559. struct intel_unpin_work *work;
  4560. struct drm_i915_gem_object *obj;
  4561. struct drm_pending_vblank_event *e;
  4562. struct timeval tnow, tvbl;
  4563. unsigned long flags;
  4564. /* Ignore early vblank irqs */
  4565. if (intel_crtc == NULL)
  4566. return;
  4567. do_gettimeofday(&tnow);
  4568. spin_lock_irqsave(&dev->event_lock, flags);
  4569. work = intel_crtc->unpin_work;
  4570. if (work == NULL || !work->pending) {
  4571. spin_unlock_irqrestore(&dev->event_lock, flags);
  4572. return;
  4573. }
  4574. intel_crtc->unpin_work = NULL;
  4575. if (work->event) {
  4576. e = work->event;
  4577. e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
  4578. /* Called before vblank count and timestamps have
  4579. * been updated for the vblank interval of flip
  4580. * completion? Need to increment vblank count and
  4581. * add one videorefresh duration to returned timestamp
  4582. * to account for this. We assume this happened if we
  4583. * get called over 0.9 frame durations after the last
  4584. * timestamped vblank.
  4585. *
  4586. * This calculation can not be used with vrefresh rates
  4587. * below 5Hz (10Hz to be on the safe side) without
  4588. * promoting to 64 integers.
  4589. */
  4590. if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
  4591. 9 * crtc->framedur_ns) {
  4592. e->event.sequence++;
  4593. tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
  4594. crtc->framedur_ns);
  4595. }
  4596. e->event.tv_sec = tvbl.tv_sec;
  4597. e->event.tv_usec = tvbl.tv_usec;
  4598. list_add_tail(&e->base.link,
  4599. &e->base.file_priv->event_list);
  4600. wake_up_interruptible(&e->base.file_priv->event_wait);
  4601. }
  4602. drm_vblank_put(dev, intel_crtc->pipe);
  4603. spin_unlock_irqrestore(&dev->event_lock, flags);
  4604. obj = work->old_fb_obj;
  4605. atomic_clear_mask(1 << intel_crtc->plane,
  4606. &obj->pending_flip.counter);
  4607. if (atomic_read(&obj->pending_flip) == 0)
  4608. wake_up(&dev_priv->pending_flip_queue);
  4609. schedule_work(&work->work);
  4610. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  4611. }
  4612. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  4613. {
  4614. drm_i915_private_t *dev_priv = dev->dev_private;
  4615. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  4616. do_intel_finish_page_flip(dev, crtc);
  4617. }
  4618. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  4619. {
  4620. drm_i915_private_t *dev_priv = dev->dev_private;
  4621. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  4622. do_intel_finish_page_flip(dev, crtc);
  4623. }
  4624. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  4625. {
  4626. drm_i915_private_t *dev_priv = dev->dev_private;
  4627. struct intel_crtc *intel_crtc =
  4628. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  4629. unsigned long flags;
  4630. spin_lock_irqsave(&dev->event_lock, flags);
  4631. if (intel_crtc->unpin_work) {
  4632. if ((++intel_crtc->unpin_work->pending) > 1)
  4633. DRM_ERROR("Prepared flip multiple times\n");
  4634. } else {
  4635. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  4636. }
  4637. spin_unlock_irqrestore(&dev->event_lock, flags);
  4638. }
  4639. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  4640. struct drm_framebuffer *fb,
  4641. struct drm_pending_vblank_event *event)
  4642. {
  4643. struct drm_device *dev = crtc->dev;
  4644. struct drm_i915_private *dev_priv = dev->dev_private;
  4645. struct intel_framebuffer *intel_fb;
  4646. struct drm_i915_gem_object *obj;
  4647. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4648. struct intel_unpin_work *work;
  4649. unsigned long flags, offset;
  4650. int pipe = intel_crtc->pipe;
  4651. u32 pf, pipesrc;
  4652. int ret;
  4653. work = kzalloc(sizeof *work, GFP_KERNEL);
  4654. if (work == NULL)
  4655. return -ENOMEM;
  4656. work->event = event;
  4657. work->dev = crtc->dev;
  4658. intel_fb = to_intel_framebuffer(crtc->fb);
  4659. work->old_fb_obj = intel_fb->obj;
  4660. INIT_WORK(&work->work, intel_unpin_work_fn);
  4661. /* We borrow the event spin lock for protecting unpin_work */
  4662. spin_lock_irqsave(&dev->event_lock, flags);
  4663. if (intel_crtc->unpin_work) {
  4664. spin_unlock_irqrestore(&dev->event_lock, flags);
  4665. kfree(work);
  4666. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  4667. return -EBUSY;
  4668. }
  4669. intel_crtc->unpin_work = work;
  4670. spin_unlock_irqrestore(&dev->event_lock, flags);
  4671. intel_fb = to_intel_framebuffer(fb);
  4672. obj = intel_fb->obj;
  4673. mutex_lock(&dev->struct_mutex);
  4674. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  4675. if (ret)
  4676. goto cleanup_work;
  4677. /* Reference the objects for the scheduled work. */
  4678. drm_gem_object_reference(&work->old_fb_obj->base);
  4679. drm_gem_object_reference(&obj->base);
  4680. crtc->fb = fb;
  4681. ret = drm_vblank_get(dev, intel_crtc->pipe);
  4682. if (ret)
  4683. goto cleanup_objs;
  4684. if (IS_GEN3(dev) || IS_GEN2(dev)) {
  4685. u32 flip_mask;
  4686. /* Can't queue multiple flips, so wait for the previous
  4687. * one to finish before executing the next.
  4688. */
  4689. ret = BEGIN_LP_RING(2);
  4690. if (ret)
  4691. goto cleanup_objs;
  4692. if (intel_crtc->plane)
  4693. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  4694. else
  4695. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  4696. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  4697. OUT_RING(MI_NOOP);
  4698. ADVANCE_LP_RING();
  4699. }
  4700. work->pending_flip_obj = obj;
  4701. work->enable_stall_check = true;
  4702. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  4703. offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
  4704. ret = BEGIN_LP_RING(4);
  4705. if (ret)
  4706. goto cleanup_objs;
  4707. /* Block clients from rendering to the new back buffer until
  4708. * the flip occurs and the object is no longer visible.
  4709. */
  4710. atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  4711. switch (INTEL_INFO(dev)->gen) {
  4712. case 2:
  4713. OUT_RING(MI_DISPLAY_FLIP |
  4714. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4715. OUT_RING(fb->pitch);
  4716. OUT_RING(obj->gtt_offset + offset);
  4717. OUT_RING(MI_NOOP);
  4718. break;
  4719. case 3:
  4720. OUT_RING(MI_DISPLAY_FLIP_I915 |
  4721. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4722. OUT_RING(fb->pitch);
  4723. OUT_RING(obj->gtt_offset + offset);
  4724. OUT_RING(MI_NOOP);
  4725. break;
  4726. case 4:
  4727. case 5:
  4728. /* i965+ uses the linear or tiled offsets from the
  4729. * Display Registers (which do not change across a page-flip)
  4730. * so we need only reprogram the base address.
  4731. */
  4732. OUT_RING(MI_DISPLAY_FLIP |
  4733. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4734. OUT_RING(fb->pitch);
  4735. OUT_RING(obj->gtt_offset | obj->tiling_mode);
  4736. /* XXX Enabling the panel-fitter across page-flip is so far
  4737. * untested on non-native modes, so ignore it for now.
  4738. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  4739. */
  4740. pf = 0;
  4741. pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
  4742. OUT_RING(pf | pipesrc);
  4743. break;
  4744. case 6:
  4745. OUT_RING(MI_DISPLAY_FLIP |
  4746. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4747. OUT_RING(fb->pitch | obj->tiling_mode);
  4748. OUT_RING(obj->gtt_offset);
  4749. pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  4750. pipesrc = I915_READ(pipe == 0 ? PIPEASRC : PIPEBSRC) & 0x0fff0fff;
  4751. OUT_RING(pf | pipesrc);
  4752. break;
  4753. }
  4754. ADVANCE_LP_RING();
  4755. mutex_unlock(&dev->struct_mutex);
  4756. trace_i915_flip_request(intel_crtc->plane, obj);
  4757. return 0;
  4758. cleanup_objs:
  4759. drm_gem_object_unreference(&work->old_fb_obj->base);
  4760. drm_gem_object_unreference(&obj->base);
  4761. cleanup_work:
  4762. mutex_unlock(&dev->struct_mutex);
  4763. spin_lock_irqsave(&dev->event_lock, flags);
  4764. intel_crtc->unpin_work = NULL;
  4765. spin_unlock_irqrestore(&dev->event_lock, flags);
  4766. kfree(work);
  4767. return ret;
  4768. }
  4769. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  4770. .dpms = intel_crtc_dpms,
  4771. .mode_fixup = intel_crtc_mode_fixup,
  4772. .mode_set = intel_crtc_mode_set,
  4773. .mode_set_base = intel_pipe_set_base,
  4774. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  4775. .load_lut = intel_crtc_load_lut,
  4776. .disable = intel_crtc_disable,
  4777. };
  4778. static const struct drm_crtc_funcs intel_crtc_funcs = {
  4779. .cursor_set = intel_crtc_cursor_set,
  4780. .cursor_move = intel_crtc_cursor_move,
  4781. .gamma_set = intel_crtc_gamma_set,
  4782. .set_config = drm_crtc_helper_set_config,
  4783. .destroy = intel_crtc_destroy,
  4784. .page_flip = intel_crtc_page_flip,
  4785. };
  4786. static void intel_sanitize_modesetting(struct drm_device *dev,
  4787. int pipe, int plane)
  4788. {
  4789. struct drm_i915_private *dev_priv = dev->dev_private;
  4790. u32 reg, val;
  4791. if (HAS_PCH_SPLIT(dev))
  4792. return;
  4793. /* Who knows what state these registers were left in by the BIOS or
  4794. * grub?
  4795. *
  4796. * If we leave the registers in a conflicting state (e.g. with the
  4797. * display plane reading from the other pipe than the one we intend
  4798. * to use) then when we attempt to teardown the active mode, we will
  4799. * not disable the pipes and planes in the correct order -- leaving
  4800. * a plane reading from a disabled pipe and possibly leading to
  4801. * undefined behaviour.
  4802. */
  4803. reg = DSPCNTR(plane);
  4804. val = I915_READ(reg);
  4805. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  4806. return;
  4807. if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
  4808. return;
  4809. /* This display plane is active and attached to the other CPU pipe. */
  4810. pipe = !pipe;
  4811. /* Disable the plane and wait for it to stop reading from the pipe. */
  4812. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  4813. intel_flush_display_plane(dev, plane);
  4814. if (IS_GEN2(dev))
  4815. intel_wait_for_vblank(dev, pipe);
  4816. if (pipe == 0 && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  4817. return;
  4818. /* Switch off the pipe. */
  4819. reg = PIPECONF(pipe);
  4820. val = I915_READ(reg);
  4821. if (val & PIPECONF_ENABLE) {
  4822. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  4823. intel_wait_for_pipe_off(dev, pipe);
  4824. }
  4825. }
  4826. static void intel_crtc_init(struct drm_device *dev, int pipe)
  4827. {
  4828. drm_i915_private_t *dev_priv = dev->dev_private;
  4829. struct intel_crtc *intel_crtc;
  4830. int i;
  4831. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  4832. if (intel_crtc == NULL)
  4833. return;
  4834. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  4835. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  4836. for (i = 0; i < 256; i++) {
  4837. intel_crtc->lut_r[i] = i;
  4838. intel_crtc->lut_g[i] = i;
  4839. intel_crtc->lut_b[i] = i;
  4840. }
  4841. /* Swap pipes & planes for FBC on pre-965 */
  4842. intel_crtc->pipe = pipe;
  4843. intel_crtc->plane = pipe;
  4844. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  4845. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  4846. intel_crtc->plane = !pipe;
  4847. }
  4848. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  4849. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  4850. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  4851. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  4852. intel_crtc->cursor_addr = 0;
  4853. intel_crtc->dpms_mode = -1;
  4854. intel_crtc->active = true; /* force the pipe off on setup_init_config */
  4855. if (HAS_PCH_SPLIT(dev)) {
  4856. intel_helper_funcs.prepare = ironlake_crtc_prepare;
  4857. intel_helper_funcs.commit = ironlake_crtc_commit;
  4858. } else {
  4859. intel_helper_funcs.prepare = i9xx_crtc_prepare;
  4860. intel_helper_funcs.commit = i9xx_crtc_commit;
  4861. }
  4862. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  4863. intel_crtc->busy = false;
  4864. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  4865. (unsigned long)intel_crtc);
  4866. intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
  4867. }
  4868. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  4869. struct drm_file *file)
  4870. {
  4871. drm_i915_private_t *dev_priv = dev->dev_private;
  4872. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  4873. struct drm_mode_object *drmmode_obj;
  4874. struct intel_crtc *crtc;
  4875. if (!dev_priv) {
  4876. DRM_ERROR("called with no initialization\n");
  4877. return -EINVAL;
  4878. }
  4879. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  4880. DRM_MODE_OBJECT_CRTC);
  4881. if (!drmmode_obj) {
  4882. DRM_ERROR("no such CRTC id\n");
  4883. return -EINVAL;
  4884. }
  4885. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  4886. pipe_from_crtc_id->pipe = crtc->pipe;
  4887. return 0;
  4888. }
  4889. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  4890. {
  4891. struct intel_encoder *encoder;
  4892. int index_mask = 0;
  4893. int entry = 0;
  4894. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  4895. if (type_mask & encoder->clone_mask)
  4896. index_mask |= (1 << entry);
  4897. entry++;
  4898. }
  4899. return index_mask;
  4900. }
  4901. static bool has_edp_a(struct drm_device *dev)
  4902. {
  4903. struct drm_i915_private *dev_priv = dev->dev_private;
  4904. if (!IS_MOBILE(dev))
  4905. return false;
  4906. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  4907. return false;
  4908. if (IS_GEN5(dev) &&
  4909. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  4910. return false;
  4911. return true;
  4912. }
  4913. static void intel_setup_outputs(struct drm_device *dev)
  4914. {
  4915. struct drm_i915_private *dev_priv = dev->dev_private;
  4916. struct intel_encoder *encoder;
  4917. bool dpd_is_edp = false;
  4918. bool has_lvds = false;
  4919. if (IS_MOBILE(dev) && !IS_I830(dev))
  4920. has_lvds = intel_lvds_init(dev);
  4921. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  4922. /* disable the panel fitter on everything but LVDS */
  4923. I915_WRITE(PFIT_CONTROL, 0);
  4924. }
  4925. if (HAS_PCH_SPLIT(dev)) {
  4926. dpd_is_edp = intel_dpd_is_edp(dev);
  4927. if (has_edp_a(dev))
  4928. intel_dp_init(dev, DP_A);
  4929. if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  4930. intel_dp_init(dev, PCH_DP_D);
  4931. }
  4932. intel_crt_init(dev);
  4933. if (HAS_PCH_SPLIT(dev)) {
  4934. int found;
  4935. if (I915_READ(HDMIB) & PORT_DETECTED) {
  4936. /* PCH SDVOB multiplex with HDMIB */
  4937. found = intel_sdvo_init(dev, PCH_SDVOB);
  4938. if (!found)
  4939. intel_hdmi_init(dev, HDMIB);
  4940. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  4941. intel_dp_init(dev, PCH_DP_B);
  4942. }
  4943. if (I915_READ(HDMIC) & PORT_DETECTED)
  4944. intel_hdmi_init(dev, HDMIC);
  4945. if (I915_READ(HDMID) & PORT_DETECTED)
  4946. intel_hdmi_init(dev, HDMID);
  4947. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  4948. intel_dp_init(dev, PCH_DP_C);
  4949. if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  4950. intel_dp_init(dev, PCH_DP_D);
  4951. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  4952. bool found = false;
  4953. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  4954. DRM_DEBUG_KMS("probing SDVOB\n");
  4955. found = intel_sdvo_init(dev, SDVOB);
  4956. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  4957. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  4958. intel_hdmi_init(dev, SDVOB);
  4959. }
  4960. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  4961. DRM_DEBUG_KMS("probing DP_B\n");
  4962. intel_dp_init(dev, DP_B);
  4963. }
  4964. }
  4965. /* Before G4X SDVOC doesn't have its own detect register */
  4966. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  4967. DRM_DEBUG_KMS("probing SDVOC\n");
  4968. found = intel_sdvo_init(dev, SDVOC);
  4969. }
  4970. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  4971. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  4972. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  4973. intel_hdmi_init(dev, SDVOC);
  4974. }
  4975. if (SUPPORTS_INTEGRATED_DP(dev)) {
  4976. DRM_DEBUG_KMS("probing DP_C\n");
  4977. intel_dp_init(dev, DP_C);
  4978. }
  4979. }
  4980. if (SUPPORTS_INTEGRATED_DP(dev) &&
  4981. (I915_READ(DP_D) & DP_DETECTED)) {
  4982. DRM_DEBUG_KMS("probing DP_D\n");
  4983. intel_dp_init(dev, DP_D);
  4984. }
  4985. } else if (IS_GEN2(dev))
  4986. intel_dvo_init(dev);
  4987. if (SUPPORTS_TV(dev))
  4988. intel_tv_init(dev);
  4989. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  4990. encoder->base.possible_crtcs = encoder->crtc_mask;
  4991. encoder->base.possible_clones =
  4992. intel_encoder_clones(dev, encoder->clone_mask);
  4993. }
  4994. intel_panel_setup_backlight(dev);
  4995. }
  4996. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  4997. {
  4998. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  4999. drm_framebuffer_cleanup(fb);
  5000. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  5001. kfree(intel_fb);
  5002. }
  5003. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  5004. struct drm_file *file,
  5005. unsigned int *handle)
  5006. {
  5007. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  5008. struct drm_i915_gem_object *obj = intel_fb->obj;
  5009. return drm_gem_handle_create(file, &obj->base, handle);
  5010. }
  5011. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  5012. .destroy = intel_user_framebuffer_destroy,
  5013. .create_handle = intel_user_framebuffer_create_handle,
  5014. };
  5015. int intel_framebuffer_init(struct drm_device *dev,
  5016. struct intel_framebuffer *intel_fb,
  5017. struct drm_mode_fb_cmd *mode_cmd,
  5018. struct drm_i915_gem_object *obj)
  5019. {
  5020. int ret;
  5021. if (obj->tiling_mode == I915_TILING_Y)
  5022. return -EINVAL;
  5023. if (mode_cmd->pitch & 63)
  5024. return -EINVAL;
  5025. switch (mode_cmd->bpp) {
  5026. case 8:
  5027. case 16:
  5028. case 24:
  5029. case 32:
  5030. break;
  5031. default:
  5032. return -EINVAL;
  5033. }
  5034. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  5035. if (ret) {
  5036. DRM_ERROR("framebuffer init failed %d\n", ret);
  5037. return ret;
  5038. }
  5039. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  5040. intel_fb->obj = obj;
  5041. return 0;
  5042. }
  5043. static struct drm_framebuffer *
  5044. intel_user_framebuffer_create(struct drm_device *dev,
  5045. struct drm_file *filp,
  5046. struct drm_mode_fb_cmd *mode_cmd)
  5047. {
  5048. struct drm_i915_gem_object *obj;
  5049. struct intel_framebuffer *intel_fb;
  5050. int ret;
  5051. obj = to_intel_bo(drm_gem_object_lookup(dev, filp, mode_cmd->handle));
  5052. if (!obj)
  5053. return ERR_PTR(-ENOENT);
  5054. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5055. if (!intel_fb)
  5056. return ERR_PTR(-ENOMEM);
  5057. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  5058. if (ret) {
  5059. drm_gem_object_unreference_unlocked(&obj->base);
  5060. kfree(intel_fb);
  5061. return ERR_PTR(ret);
  5062. }
  5063. return &intel_fb->base;
  5064. }
  5065. static const struct drm_mode_config_funcs intel_mode_funcs = {
  5066. .fb_create = intel_user_framebuffer_create,
  5067. .output_poll_changed = intel_fb_output_poll_changed,
  5068. };
  5069. static struct drm_i915_gem_object *
  5070. intel_alloc_context_page(struct drm_device *dev)
  5071. {
  5072. struct drm_i915_gem_object *ctx;
  5073. int ret;
  5074. ctx = i915_gem_alloc_object(dev, 4096);
  5075. if (!ctx) {
  5076. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  5077. return NULL;
  5078. }
  5079. mutex_lock(&dev->struct_mutex);
  5080. ret = i915_gem_object_pin(ctx, 4096, true);
  5081. if (ret) {
  5082. DRM_ERROR("failed to pin power context: %d\n", ret);
  5083. goto err_unref;
  5084. }
  5085. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  5086. if (ret) {
  5087. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  5088. goto err_unpin;
  5089. }
  5090. mutex_unlock(&dev->struct_mutex);
  5091. return ctx;
  5092. err_unpin:
  5093. i915_gem_object_unpin(ctx);
  5094. err_unref:
  5095. drm_gem_object_unreference(&ctx->base);
  5096. mutex_unlock(&dev->struct_mutex);
  5097. return NULL;
  5098. }
  5099. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  5100. {
  5101. struct drm_i915_private *dev_priv = dev->dev_private;
  5102. u16 rgvswctl;
  5103. rgvswctl = I915_READ16(MEMSWCTL);
  5104. if (rgvswctl & MEMCTL_CMD_STS) {
  5105. DRM_DEBUG("gpu busy, RCS change rejected\n");
  5106. return false; /* still busy with another command */
  5107. }
  5108. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  5109. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  5110. I915_WRITE16(MEMSWCTL, rgvswctl);
  5111. POSTING_READ16(MEMSWCTL);
  5112. rgvswctl |= MEMCTL_CMD_STS;
  5113. I915_WRITE16(MEMSWCTL, rgvswctl);
  5114. return true;
  5115. }
  5116. void ironlake_enable_drps(struct drm_device *dev)
  5117. {
  5118. struct drm_i915_private *dev_priv = dev->dev_private;
  5119. u32 rgvmodectl = I915_READ(MEMMODECTL);
  5120. u8 fmax, fmin, fstart, vstart;
  5121. /* Enable temp reporting */
  5122. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  5123. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  5124. /* 100ms RC evaluation intervals */
  5125. I915_WRITE(RCUPEI, 100000);
  5126. I915_WRITE(RCDNEI, 100000);
  5127. /* Set max/min thresholds to 90ms and 80ms respectively */
  5128. I915_WRITE(RCBMAXAVG, 90000);
  5129. I915_WRITE(RCBMINAVG, 80000);
  5130. I915_WRITE(MEMIHYST, 1);
  5131. /* Set up min, max, and cur for interrupt handling */
  5132. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  5133. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  5134. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  5135. MEMMODE_FSTART_SHIFT;
  5136. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  5137. PXVFREQ_PX_SHIFT;
  5138. dev_priv->fmax = fmax; /* IPS callback will increase this */
  5139. dev_priv->fstart = fstart;
  5140. dev_priv->max_delay = fstart;
  5141. dev_priv->min_delay = fmin;
  5142. dev_priv->cur_delay = fstart;
  5143. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  5144. fmax, fmin, fstart);
  5145. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  5146. /*
  5147. * Interrupts will be enabled in ironlake_irq_postinstall
  5148. */
  5149. I915_WRITE(VIDSTART, vstart);
  5150. POSTING_READ(VIDSTART);
  5151. rgvmodectl |= MEMMODE_SWMODE_EN;
  5152. I915_WRITE(MEMMODECTL, rgvmodectl);
  5153. if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  5154. DRM_ERROR("stuck trying to change perf mode\n");
  5155. msleep(1);
  5156. ironlake_set_drps(dev, fstart);
  5157. dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  5158. I915_READ(0x112e0);
  5159. dev_priv->last_time1 = jiffies_to_msecs(jiffies);
  5160. dev_priv->last_count2 = I915_READ(0x112f4);
  5161. getrawmonotonic(&dev_priv->last_time2);
  5162. }
  5163. void ironlake_disable_drps(struct drm_device *dev)
  5164. {
  5165. struct drm_i915_private *dev_priv = dev->dev_private;
  5166. u16 rgvswctl = I915_READ16(MEMSWCTL);
  5167. /* Ack interrupts, disable EFC interrupt */
  5168. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  5169. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  5170. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  5171. I915_WRITE(DEIIR, DE_PCU_EVENT);
  5172. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  5173. /* Go back to the starting frequency */
  5174. ironlake_set_drps(dev, dev_priv->fstart);
  5175. msleep(1);
  5176. rgvswctl |= MEMCTL_CMD_STS;
  5177. I915_WRITE(MEMSWCTL, rgvswctl);
  5178. msleep(1);
  5179. }
  5180. void gen6_set_rps(struct drm_device *dev, u8 val)
  5181. {
  5182. struct drm_i915_private *dev_priv = dev->dev_private;
  5183. u32 swreq;
  5184. swreq = (val & 0x3ff) << 25;
  5185. I915_WRITE(GEN6_RPNSWREQ, swreq);
  5186. }
  5187. void gen6_disable_rps(struct drm_device *dev)
  5188. {
  5189. struct drm_i915_private *dev_priv = dev->dev_private;
  5190. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  5191. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  5192. I915_WRITE(GEN6_PMIER, 0);
  5193. I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
  5194. }
  5195. static unsigned long intel_pxfreq(u32 vidfreq)
  5196. {
  5197. unsigned long freq;
  5198. int div = (vidfreq & 0x3f0000) >> 16;
  5199. int post = (vidfreq & 0x3000) >> 12;
  5200. int pre = (vidfreq & 0x7);
  5201. if (!pre)
  5202. return 0;
  5203. freq = ((div * 133333) / ((1<<post) * pre));
  5204. return freq;
  5205. }
  5206. void intel_init_emon(struct drm_device *dev)
  5207. {
  5208. struct drm_i915_private *dev_priv = dev->dev_private;
  5209. u32 lcfuse;
  5210. u8 pxw[16];
  5211. int i;
  5212. /* Disable to program */
  5213. I915_WRITE(ECR, 0);
  5214. POSTING_READ(ECR);
  5215. /* Program energy weights for various events */
  5216. I915_WRITE(SDEW, 0x15040d00);
  5217. I915_WRITE(CSIEW0, 0x007f0000);
  5218. I915_WRITE(CSIEW1, 0x1e220004);
  5219. I915_WRITE(CSIEW2, 0x04000004);
  5220. for (i = 0; i < 5; i++)
  5221. I915_WRITE(PEW + (i * 4), 0);
  5222. for (i = 0; i < 3; i++)
  5223. I915_WRITE(DEW + (i * 4), 0);
  5224. /* Program P-state weights to account for frequency power adjustment */
  5225. for (i = 0; i < 16; i++) {
  5226. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  5227. unsigned long freq = intel_pxfreq(pxvidfreq);
  5228. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  5229. PXVFREQ_PX_SHIFT;
  5230. unsigned long val;
  5231. val = vid * vid;
  5232. val *= (freq / 1000);
  5233. val *= 255;
  5234. val /= (127*127*900);
  5235. if (val > 0xff)
  5236. DRM_ERROR("bad pxval: %ld\n", val);
  5237. pxw[i] = val;
  5238. }
  5239. /* Render standby states get 0 weight */
  5240. pxw[14] = 0;
  5241. pxw[15] = 0;
  5242. for (i = 0; i < 4; i++) {
  5243. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  5244. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  5245. I915_WRITE(PXW + (i * 4), val);
  5246. }
  5247. /* Adjust magic regs to magic values (more experimental results) */
  5248. I915_WRITE(OGW0, 0);
  5249. I915_WRITE(OGW1, 0);
  5250. I915_WRITE(EG0, 0x00007f00);
  5251. I915_WRITE(EG1, 0x0000000e);
  5252. I915_WRITE(EG2, 0x000e0000);
  5253. I915_WRITE(EG3, 0x68000300);
  5254. I915_WRITE(EG4, 0x42000000);
  5255. I915_WRITE(EG5, 0x00140031);
  5256. I915_WRITE(EG6, 0);
  5257. I915_WRITE(EG7, 0);
  5258. for (i = 0; i < 8; i++)
  5259. I915_WRITE(PXWL + (i * 4), 0);
  5260. /* Enable PMON + select events */
  5261. I915_WRITE(ECR, 0x80000019);
  5262. lcfuse = I915_READ(LCFUSE02);
  5263. dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
  5264. }
  5265. void gen6_enable_rps(struct drm_i915_private *dev_priv)
  5266. {
  5267. u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  5268. u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
  5269. u32 pcu_mbox;
  5270. int cur_freq, min_freq, max_freq;
  5271. int i;
  5272. /* Here begins a magic sequence of register writes to enable
  5273. * auto-downclocking.
  5274. *
  5275. * Perhaps there might be some value in exposing these to
  5276. * userspace...
  5277. */
  5278. I915_WRITE(GEN6_RC_STATE, 0);
  5279. __gen6_force_wake_get(dev_priv);
  5280. /* disable the counters and set deterministic thresholds */
  5281. I915_WRITE(GEN6_RC_CONTROL, 0);
  5282. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  5283. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  5284. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  5285. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  5286. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  5287. for (i = 0; i < I915_NUM_RINGS; i++)
  5288. I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
  5289. I915_WRITE(GEN6_RC_SLEEP, 0);
  5290. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  5291. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  5292. I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
  5293. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  5294. I915_WRITE(GEN6_RC_CONTROL,
  5295. GEN6_RC_CTL_RC6p_ENABLE |
  5296. GEN6_RC_CTL_RC6_ENABLE |
  5297. GEN6_RC_CTL_EI_MODE(1) |
  5298. GEN6_RC_CTL_HW_ENABLE);
  5299. I915_WRITE(GEN6_RPNSWREQ,
  5300. GEN6_FREQUENCY(10) |
  5301. GEN6_OFFSET(0) |
  5302. GEN6_AGGRESSIVE_TURBO);
  5303. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  5304. GEN6_FREQUENCY(12));
  5305. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  5306. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  5307. 18 << 24 |
  5308. 6 << 16);
  5309. I915_WRITE(GEN6_RP_UP_THRESHOLD, 90000);
  5310. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 100000);
  5311. I915_WRITE(GEN6_RP_UP_EI, 100000);
  5312. I915_WRITE(GEN6_RP_DOWN_EI, 300000);
  5313. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  5314. I915_WRITE(GEN6_RP_CONTROL,
  5315. GEN6_RP_MEDIA_TURBO |
  5316. GEN6_RP_USE_NORMAL_FREQ |
  5317. GEN6_RP_MEDIA_IS_GFX |
  5318. GEN6_RP_ENABLE |
  5319. GEN6_RP_UP_BUSY_MAX |
  5320. GEN6_RP_DOWN_BUSY_MIN);
  5321. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5322. 500))
  5323. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  5324. I915_WRITE(GEN6_PCODE_DATA, 0);
  5325. I915_WRITE(GEN6_PCODE_MAILBOX,
  5326. GEN6_PCODE_READY |
  5327. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  5328. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5329. 500))
  5330. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  5331. min_freq = (rp_state_cap & 0xff0000) >> 16;
  5332. max_freq = rp_state_cap & 0xff;
  5333. cur_freq = (gt_perf_status & 0xff00) >> 8;
  5334. /* Check for overclock support */
  5335. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5336. 500))
  5337. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  5338. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
  5339. pcu_mbox = I915_READ(GEN6_PCODE_DATA);
  5340. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  5341. 500))
  5342. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  5343. if (pcu_mbox & (1<<31)) { /* OC supported */
  5344. max_freq = pcu_mbox & 0xff;
  5345. DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 100);
  5346. }
  5347. /* In units of 100MHz */
  5348. dev_priv->max_delay = max_freq;
  5349. dev_priv->min_delay = min_freq;
  5350. dev_priv->cur_delay = cur_freq;
  5351. /* requires MSI enabled */
  5352. I915_WRITE(GEN6_PMIER,
  5353. GEN6_PM_MBOX_EVENT |
  5354. GEN6_PM_THERMAL_EVENT |
  5355. GEN6_PM_RP_DOWN_TIMEOUT |
  5356. GEN6_PM_RP_UP_THRESHOLD |
  5357. GEN6_PM_RP_DOWN_THRESHOLD |
  5358. GEN6_PM_RP_UP_EI_EXPIRED |
  5359. GEN6_PM_RP_DOWN_EI_EXPIRED);
  5360. I915_WRITE(GEN6_PMIMR, 0);
  5361. /* enable all PM interrupts */
  5362. I915_WRITE(GEN6_PMINTRMSK, 0);
  5363. __gen6_force_wake_put(dev_priv);
  5364. }
  5365. void intel_enable_clock_gating(struct drm_device *dev)
  5366. {
  5367. struct drm_i915_private *dev_priv = dev->dev_private;
  5368. /*
  5369. * Disable clock gating reported to work incorrectly according to the
  5370. * specs, but enable as much else as we can.
  5371. */
  5372. if (HAS_PCH_SPLIT(dev)) {
  5373. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  5374. if (IS_GEN5(dev)) {
  5375. /* Required for FBC */
  5376. dspclk_gate |= DPFDUNIT_CLOCK_GATE_DISABLE;
  5377. /* Required for CxSR */
  5378. dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
  5379. I915_WRITE(PCH_3DCGDIS0,
  5380. MARIUNIT_CLOCK_GATE_DISABLE |
  5381. SVSMUNIT_CLOCK_GATE_DISABLE);
  5382. I915_WRITE(PCH_3DCGDIS1,
  5383. VFMUNIT_CLOCK_GATE_DISABLE);
  5384. }
  5385. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  5386. /*
  5387. * On Ibex Peak and Cougar Point, we need to disable clock
  5388. * gating for the panel power sequencer or it will fail to
  5389. * start up when no ports are active.
  5390. */
  5391. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  5392. /*
  5393. * According to the spec the following bits should be set in
  5394. * order to enable memory self-refresh
  5395. * The bit 22/21 of 0x42004
  5396. * The bit 5 of 0x42020
  5397. * The bit 15 of 0x45000
  5398. */
  5399. if (IS_GEN5(dev)) {
  5400. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5401. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  5402. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  5403. I915_WRITE(ILK_DSPCLK_GATE,
  5404. (I915_READ(ILK_DSPCLK_GATE) |
  5405. ILK_DPARB_CLK_GATE));
  5406. I915_WRITE(DISP_ARB_CTL,
  5407. (I915_READ(DISP_ARB_CTL) |
  5408. DISP_FBC_WM_DIS));
  5409. I915_WRITE(WM3_LP_ILK, 0);
  5410. I915_WRITE(WM2_LP_ILK, 0);
  5411. I915_WRITE(WM1_LP_ILK, 0);
  5412. }
  5413. /*
  5414. * Based on the document from hardware guys the following bits
  5415. * should be set unconditionally in order to enable FBC.
  5416. * The bit 22 of 0x42000
  5417. * The bit 22 of 0x42004
  5418. * The bit 7,8,9 of 0x42020.
  5419. */
  5420. if (IS_IRONLAKE_M(dev)) {
  5421. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  5422. I915_READ(ILK_DISPLAY_CHICKEN1) |
  5423. ILK_FBCQ_DIS);
  5424. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5425. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5426. ILK_DPARB_GATE);
  5427. I915_WRITE(ILK_DSPCLK_GATE,
  5428. I915_READ(ILK_DSPCLK_GATE) |
  5429. ILK_DPFC_DIS1 |
  5430. ILK_DPFC_DIS2 |
  5431. ILK_CLK_FBC);
  5432. }
  5433. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5434. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5435. ILK_ELPIN_409_SELECT);
  5436. if (IS_GEN5(dev)) {
  5437. I915_WRITE(_3D_CHICKEN2,
  5438. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  5439. _3D_CHICKEN2_WM_READ_PIPELINED);
  5440. }
  5441. if (IS_GEN6(dev)) {
  5442. I915_WRITE(WM3_LP_ILK, 0);
  5443. I915_WRITE(WM2_LP_ILK, 0);
  5444. I915_WRITE(WM1_LP_ILK, 0);
  5445. /*
  5446. * According to the spec the following bits should be
  5447. * set in order to enable memory self-refresh and fbc:
  5448. * The bit21 and bit22 of 0x42000
  5449. * The bit21 and bit22 of 0x42004
  5450. * The bit5 and bit7 of 0x42020
  5451. * The bit14 of 0x70180
  5452. * The bit14 of 0x71180
  5453. */
  5454. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  5455. I915_READ(ILK_DISPLAY_CHICKEN1) |
  5456. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  5457. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  5458. I915_READ(ILK_DISPLAY_CHICKEN2) |
  5459. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  5460. I915_WRITE(ILK_DSPCLK_GATE,
  5461. I915_READ(ILK_DSPCLK_GATE) |
  5462. ILK_DPARB_CLK_GATE |
  5463. ILK_DPFD_CLK_GATE);
  5464. I915_WRITE(DSPACNTR,
  5465. I915_READ(DSPACNTR) |
  5466. DISPPLANE_TRICKLE_FEED_DISABLE);
  5467. I915_WRITE(DSPBCNTR,
  5468. I915_READ(DSPBCNTR) |
  5469. DISPPLANE_TRICKLE_FEED_DISABLE);
  5470. }
  5471. } else if (IS_G4X(dev)) {
  5472. uint32_t dspclk_gate;
  5473. I915_WRITE(RENCLK_GATE_D1, 0);
  5474. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  5475. GS_UNIT_CLOCK_GATE_DISABLE |
  5476. CL_UNIT_CLOCK_GATE_DISABLE);
  5477. I915_WRITE(RAMCLK_GATE_D, 0);
  5478. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  5479. OVRUNIT_CLOCK_GATE_DISABLE |
  5480. OVCUNIT_CLOCK_GATE_DISABLE;
  5481. if (IS_GM45(dev))
  5482. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  5483. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  5484. } else if (IS_CRESTLINE(dev)) {
  5485. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  5486. I915_WRITE(RENCLK_GATE_D2, 0);
  5487. I915_WRITE(DSPCLK_GATE_D, 0);
  5488. I915_WRITE(RAMCLK_GATE_D, 0);
  5489. I915_WRITE16(DEUC, 0);
  5490. } else if (IS_BROADWATER(dev)) {
  5491. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  5492. I965_RCC_CLOCK_GATE_DISABLE |
  5493. I965_RCPB_CLOCK_GATE_DISABLE |
  5494. I965_ISC_CLOCK_GATE_DISABLE |
  5495. I965_FBC_CLOCK_GATE_DISABLE);
  5496. I915_WRITE(RENCLK_GATE_D2, 0);
  5497. } else if (IS_GEN3(dev)) {
  5498. u32 dstate = I915_READ(D_STATE);
  5499. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  5500. DSTATE_DOT_CLOCK_GATING;
  5501. I915_WRITE(D_STATE, dstate);
  5502. } else if (IS_I85X(dev) || IS_I865G(dev)) {
  5503. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  5504. } else if (IS_I830(dev)) {
  5505. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  5506. }
  5507. /*
  5508. * GPU can automatically power down the render unit if given a page
  5509. * to save state.
  5510. */
  5511. if (IS_IRONLAKE_M(dev) && 0) { /* XXX causes a failure during suspend */
  5512. if (dev_priv->renderctx == NULL)
  5513. dev_priv->renderctx = intel_alloc_context_page(dev);
  5514. if (dev_priv->renderctx) {
  5515. struct drm_i915_gem_object *obj = dev_priv->renderctx;
  5516. if (BEGIN_LP_RING(4) == 0) {
  5517. OUT_RING(MI_SET_CONTEXT);
  5518. OUT_RING(obj->gtt_offset |
  5519. MI_MM_SPACE_GTT |
  5520. MI_SAVE_EXT_STATE_EN |
  5521. MI_RESTORE_EXT_STATE_EN |
  5522. MI_RESTORE_INHIBIT);
  5523. OUT_RING(MI_NOOP);
  5524. OUT_RING(MI_FLUSH);
  5525. ADVANCE_LP_RING();
  5526. }
  5527. } else
  5528. DRM_DEBUG_KMS("Failed to allocate render context."
  5529. "Disable RC6\n");
  5530. }
  5531. if (IS_GEN4(dev) && IS_MOBILE(dev)) {
  5532. if (dev_priv->pwrctx == NULL)
  5533. dev_priv->pwrctx = intel_alloc_context_page(dev);
  5534. if (dev_priv->pwrctx) {
  5535. struct drm_i915_gem_object *obj = dev_priv->pwrctx;
  5536. I915_WRITE(PWRCTXA, obj->gtt_offset | PWRCTX_EN);
  5537. I915_WRITE(MCHBAR_RENDER_STANDBY,
  5538. I915_READ(MCHBAR_RENDER_STANDBY) & ~RCX_SW_EXIT);
  5539. }
  5540. }
  5541. }
  5542. void intel_disable_clock_gating(struct drm_device *dev)
  5543. {
  5544. struct drm_i915_private *dev_priv = dev->dev_private;
  5545. if (dev_priv->renderctx) {
  5546. struct drm_i915_gem_object *obj = dev_priv->renderctx;
  5547. I915_WRITE(CCID, 0);
  5548. POSTING_READ(CCID);
  5549. i915_gem_object_unpin(obj);
  5550. drm_gem_object_unreference(&obj->base);
  5551. dev_priv->renderctx = NULL;
  5552. }
  5553. if (dev_priv->pwrctx) {
  5554. struct drm_i915_gem_object *obj = dev_priv->pwrctx;
  5555. I915_WRITE(PWRCTXA, 0);
  5556. POSTING_READ(PWRCTXA);
  5557. i915_gem_object_unpin(obj);
  5558. drm_gem_object_unreference(&obj->base);
  5559. dev_priv->pwrctx = NULL;
  5560. }
  5561. }
  5562. /* Set up chip specific display functions */
  5563. static void intel_init_display(struct drm_device *dev)
  5564. {
  5565. struct drm_i915_private *dev_priv = dev->dev_private;
  5566. /* We always want a DPMS function */
  5567. if (HAS_PCH_SPLIT(dev))
  5568. dev_priv->display.dpms = ironlake_crtc_dpms;
  5569. else
  5570. dev_priv->display.dpms = i9xx_crtc_dpms;
  5571. if (I915_HAS_FBC(dev)) {
  5572. if (HAS_PCH_SPLIT(dev)) {
  5573. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  5574. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  5575. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  5576. } else if (IS_GM45(dev)) {
  5577. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  5578. dev_priv->display.enable_fbc = g4x_enable_fbc;
  5579. dev_priv->display.disable_fbc = g4x_disable_fbc;
  5580. } else if (IS_CRESTLINE(dev)) {
  5581. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  5582. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  5583. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  5584. }
  5585. /* 855GM needs testing */
  5586. }
  5587. /* Returns the core display clock speed */
  5588. if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
  5589. dev_priv->display.get_display_clock_speed =
  5590. i945_get_display_clock_speed;
  5591. else if (IS_I915G(dev))
  5592. dev_priv->display.get_display_clock_speed =
  5593. i915_get_display_clock_speed;
  5594. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  5595. dev_priv->display.get_display_clock_speed =
  5596. i9xx_misc_get_display_clock_speed;
  5597. else if (IS_I915GM(dev))
  5598. dev_priv->display.get_display_clock_speed =
  5599. i915gm_get_display_clock_speed;
  5600. else if (IS_I865G(dev))
  5601. dev_priv->display.get_display_clock_speed =
  5602. i865_get_display_clock_speed;
  5603. else if (IS_I85X(dev))
  5604. dev_priv->display.get_display_clock_speed =
  5605. i855_get_display_clock_speed;
  5606. else /* 852, 830 */
  5607. dev_priv->display.get_display_clock_speed =
  5608. i830_get_display_clock_speed;
  5609. /* For FIFO watermark updates */
  5610. if (HAS_PCH_SPLIT(dev)) {
  5611. if (IS_GEN5(dev)) {
  5612. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  5613. dev_priv->display.update_wm = ironlake_update_wm;
  5614. else {
  5615. DRM_DEBUG_KMS("Failed to get proper latency. "
  5616. "Disable CxSR\n");
  5617. dev_priv->display.update_wm = NULL;
  5618. }
  5619. } else if (IS_GEN6(dev)) {
  5620. if (SNB_READ_WM0_LATENCY()) {
  5621. dev_priv->display.update_wm = sandybridge_update_wm;
  5622. } else {
  5623. DRM_DEBUG_KMS("Failed to read display plane latency. "
  5624. "Disable CxSR\n");
  5625. dev_priv->display.update_wm = NULL;
  5626. }
  5627. } else
  5628. dev_priv->display.update_wm = NULL;
  5629. } else if (IS_PINEVIEW(dev)) {
  5630. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  5631. dev_priv->is_ddr3,
  5632. dev_priv->fsb_freq,
  5633. dev_priv->mem_freq)) {
  5634. DRM_INFO("failed to find known CxSR latency "
  5635. "(found ddr%s fsb freq %d, mem freq %d), "
  5636. "disabling CxSR\n",
  5637. (dev_priv->is_ddr3 == 1) ? "3": "2",
  5638. dev_priv->fsb_freq, dev_priv->mem_freq);
  5639. /* Disable CxSR and never update its watermark again */
  5640. pineview_disable_cxsr(dev);
  5641. dev_priv->display.update_wm = NULL;
  5642. } else
  5643. dev_priv->display.update_wm = pineview_update_wm;
  5644. } else if (IS_G4X(dev))
  5645. dev_priv->display.update_wm = g4x_update_wm;
  5646. else if (IS_GEN4(dev))
  5647. dev_priv->display.update_wm = i965_update_wm;
  5648. else if (IS_GEN3(dev)) {
  5649. dev_priv->display.update_wm = i9xx_update_wm;
  5650. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  5651. } else if (IS_I85X(dev)) {
  5652. dev_priv->display.update_wm = i9xx_update_wm;
  5653. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  5654. } else {
  5655. dev_priv->display.update_wm = i830_update_wm;
  5656. if (IS_845G(dev))
  5657. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  5658. else
  5659. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  5660. }
  5661. }
  5662. /*
  5663. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  5664. * resume, or other times. This quirk makes sure that's the case for
  5665. * affected systems.
  5666. */
  5667. static void quirk_pipea_force (struct drm_device *dev)
  5668. {
  5669. struct drm_i915_private *dev_priv = dev->dev_private;
  5670. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  5671. DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
  5672. }
  5673. struct intel_quirk {
  5674. int device;
  5675. int subsystem_vendor;
  5676. int subsystem_device;
  5677. void (*hook)(struct drm_device *dev);
  5678. };
  5679. struct intel_quirk intel_quirks[] = {
  5680. /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
  5681. { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
  5682. /* HP Mini needs pipe A force quirk (LP: #322104) */
  5683. { 0x27ae,0x103c, 0x361a, quirk_pipea_force },
  5684. /* Thinkpad R31 needs pipe A force quirk */
  5685. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  5686. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  5687. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  5688. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  5689. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  5690. /* ThinkPad X40 needs pipe A force quirk */
  5691. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  5692. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  5693. /* 855 & before need to leave pipe A & dpll A up */
  5694. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5695. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5696. };
  5697. static void intel_init_quirks(struct drm_device *dev)
  5698. {
  5699. struct pci_dev *d = dev->pdev;
  5700. int i;
  5701. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  5702. struct intel_quirk *q = &intel_quirks[i];
  5703. if (d->device == q->device &&
  5704. (d->subsystem_vendor == q->subsystem_vendor ||
  5705. q->subsystem_vendor == PCI_ANY_ID) &&
  5706. (d->subsystem_device == q->subsystem_device ||
  5707. q->subsystem_device == PCI_ANY_ID))
  5708. q->hook(dev);
  5709. }
  5710. }
  5711. /* Disable the VGA plane that we never use */
  5712. static void i915_disable_vga(struct drm_device *dev)
  5713. {
  5714. struct drm_i915_private *dev_priv = dev->dev_private;
  5715. u8 sr1;
  5716. u32 vga_reg;
  5717. if (HAS_PCH_SPLIT(dev))
  5718. vga_reg = CPU_VGACNTRL;
  5719. else
  5720. vga_reg = VGACNTRL;
  5721. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  5722. outb(1, VGA_SR_INDEX);
  5723. sr1 = inb(VGA_SR_DATA);
  5724. outb(sr1 | 1<<5, VGA_SR_DATA);
  5725. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  5726. udelay(300);
  5727. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  5728. POSTING_READ(vga_reg);
  5729. }
  5730. void intel_modeset_init(struct drm_device *dev)
  5731. {
  5732. struct drm_i915_private *dev_priv = dev->dev_private;
  5733. int i;
  5734. drm_mode_config_init(dev);
  5735. dev->mode_config.min_width = 0;
  5736. dev->mode_config.min_height = 0;
  5737. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  5738. intel_init_quirks(dev);
  5739. intel_init_display(dev);
  5740. if (IS_GEN2(dev)) {
  5741. dev->mode_config.max_width = 2048;
  5742. dev->mode_config.max_height = 2048;
  5743. } else if (IS_GEN3(dev)) {
  5744. dev->mode_config.max_width = 4096;
  5745. dev->mode_config.max_height = 4096;
  5746. } else {
  5747. dev->mode_config.max_width = 8192;
  5748. dev->mode_config.max_height = 8192;
  5749. }
  5750. dev->mode_config.fb_base = dev->agp->base;
  5751. if (IS_MOBILE(dev) || !IS_GEN2(dev))
  5752. dev_priv->num_pipe = 2;
  5753. else
  5754. dev_priv->num_pipe = 1;
  5755. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  5756. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  5757. for (i = 0; i < dev_priv->num_pipe; i++) {
  5758. intel_crtc_init(dev, i);
  5759. }
  5760. intel_setup_outputs(dev);
  5761. intel_enable_clock_gating(dev);
  5762. /* Just disable it once at startup */
  5763. i915_disable_vga(dev);
  5764. if (IS_IRONLAKE_M(dev)) {
  5765. ironlake_enable_drps(dev);
  5766. intel_init_emon(dev);
  5767. }
  5768. if (IS_GEN6(dev))
  5769. gen6_enable_rps(dev_priv);
  5770. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  5771. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  5772. (unsigned long)dev);
  5773. intel_setup_overlay(dev);
  5774. }
  5775. void intel_modeset_cleanup(struct drm_device *dev)
  5776. {
  5777. struct drm_i915_private *dev_priv = dev->dev_private;
  5778. struct drm_crtc *crtc;
  5779. struct intel_crtc *intel_crtc;
  5780. drm_kms_helper_poll_fini(dev);
  5781. mutex_lock(&dev->struct_mutex);
  5782. intel_unregister_dsm_handler();
  5783. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5784. /* Skip inactive CRTCs */
  5785. if (!crtc->fb)
  5786. continue;
  5787. intel_crtc = to_intel_crtc(crtc);
  5788. intel_increase_pllclock(crtc);
  5789. }
  5790. if (dev_priv->display.disable_fbc)
  5791. dev_priv->display.disable_fbc(dev);
  5792. if (IS_IRONLAKE_M(dev))
  5793. ironlake_disable_drps(dev);
  5794. if (IS_GEN6(dev))
  5795. gen6_disable_rps(dev);
  5796. intel_disable_clock_gating(dev);
  5797. mutex_unlock(&dev->struct_mutex);
  5798. /* Disable the irq before mode object teardown, for the irq might
  5799. * enqueue unpin/hotplug work. */
  5800. drm_irq_uninstall(dev);
  5801. cancel_work_sync(&dev_priv->hotplug_work);
  5802. /* Shut off idle work before the crtcs get freed. */
  5803. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5804. intel_crtc = to_intel_crtc(crtc);
  5805. del_timer_sync(&intel_crtc->idle_timer);
  5806. }
  5807. del_timer_sync(&dev_priv->idle_timer);
  5808. cancel_work_sync(&dev_priv->idle_work);
  5809. drm_mode_config_cleanup(dev);
  5810. }
  5811. /*
  5812. * Return which encoder is currently attached for connector.
  5813. */
  5814. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  5815. {
  5816. return &intel_attached_encoder(connector)->base;
  5817. }
  5818. void intel_connector_attach_encoder(struct intel_connector *connector,
  5819. struct intel_encoder *encoder)
  5820. {
  5821. connector->encoder = encoder;
  5822. drm_mode_connector_attach_encoder(&connector->base,
  5823. &encoder->base);
  5824. }
  5825. /*
  5826. * set vga decode state - true == enable VGA decode
  5827. */
  5828. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  5829. {
  5830. struct drm_i915_private *dev_priv = dev->dev_private;
  5831. u16 gmch_ctrl;
  5832. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  5833. if (state)
  5834. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  5835. else
  5836. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  5837. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  5838. return 0;
  5839. }
  5840. #ifdef CONFIG_DEBUG_FS
  5841. #include <linux/seq_file.h>
  5842. struct intel_display_error_state {
  5843. struct intel_cursor_error_state {
  5844. u32 control;
  5845. u32 position;
  5846. u32 base;
  5847. u32 size;
  5848. } cursor[2];
  5849. struct intel_pipe_error_state {
  5850. u32 conf;
  5851. u32 source;
  5852. u32 htotal;
  5853. u32 hblank;
  5854. u32 hsync;
  5855. u32 vtotal;
  5856. u32 vblank;
  5857. u32 vsync;
  5858. } pipe[2];
  5859. struct intel_plane_error_state {
  5860. u32 control;
  5861. u32 stride;
  5862. u32 size;
  5863. u32 pos;
  5864. u32 addr;
  5865. u32 surface;
  5866. u32 tile_offset;
  5867. } plane[2];
  5868. };
  5869. struct intel_display_error_state *
  5870. intel_display_capture_error_state(struct drm_device *dev)
  5871. {
  5872. drm_i915_private_t *dev_priv = dev->dev_private;
  5873. struct intel_display_error_state *error;
  5874. int i;
  5875. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  5876. if (error == NULL)
  5877. return NULL;
  5878. for (i = 0; i < 2; i++) {
  5879. error->cursor[i].control = I915_READ(CURCNTR(i));
  5880. error->cursor[i].position = I915_READ(CURPOS(i));
  5881. error->cursor[i].base = I915_READ(CURBASE(i));
  5882. error->plane[i].control = I915_READ(DSPCNTR(i));
  5883. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  5884. error->plane[i].size = I915_READ(DSPSIZE(i));
  5885. error->plane[i].pos= I915_READ(DSPPOS(i));
  5886. error->plane[i].addr = I915_READ(DSPADDR(i));
  5887. if (INTEL_INFO(dev)->gen >= 4) {
  5888. error->plane[i].surface = I915_READ(DSPSURF(i));
  5889. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  5890. }
  5891. error->pipe[i].conf = I915_READ(PIPECONF(i));
  5892. error->pipe[i].source = I915_READ(PIPESRC(i));
  5893. error->pipe[i].htotal = I915_READ(HTOTAL(i));
  5894. error->pipe[i].hblank = I915_READ(HBLANK(i));
  5895. error->pipe[i].hsync = I915_READ(HSYNC(i));
  5896. error->pipe[i].vtotal = I915_READ(VTOTAL(i));
  5897. error->pipe[i].vblank = I915_READ(VBLANK(i));
  5898. error->pipe[i].vsync = I915_READ(VSYNC(i));
  5899. }
  5900. return error;
  5901. }
  5902. void
  5903. intel_display_print_error_state(struct seq_file *m,
  5904. struct drm_device *dev,
  5905. struct intel_display_error_state *error)
  5906. {
  5907. int i;
  5908. for (i = 0; i < 2; i++) {
  5909. seq_printf(m, "Pipe [%d]:\n", i);
  5910. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  5911. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  5912. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  5913. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  5914. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  5915. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  5916. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  5917. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  5918. seq_printf(m, "Plane [%d]:\n", i);
  5919. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  5920. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  5921. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  5922. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  5923. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  5924. if (INTEL_INFO(dev)->gen >= 4) {
  5925. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  5926. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  5927. }
  5928. seq_printf(m, "Cursor [%d]:\n", i);
  5929. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  5930. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  5931. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  5932. }
  5933. }
  5934. #endif