intel_display.c 178 KB

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