intel_display.c 226 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383
  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/cpufreq.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include "drmP.h"
  34. #include "intel_drv.h"
  35. #include "i915_drm.h"
  36. #include "i915_drv.h"
  37. #include "i915_trace.h"
  38. #include "drm_dp_helper.h"
  39. #include "drm_crtc_helper.h"
  40. #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  41. bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
  42. static void intel_update_watermarks(struct drm_device *dev);
  43. static void intel_increase_pllclock(struct drm_crtc *crtc);
  44. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  45. typedef struct {
  46. /* given values */
  47. int n;
  48. int m1, m2;
  49. int p1, p2;
  50. /* derived values */
  51. int dot;
  52. int vco;
  53. int m;
  54. int p;
  55. } intel_clock_t;
  56. typedef struct {
  57. int min, max;
  58. } intel_range_t;
  59. typedef struct {
  60. int dot_limit;
  61. int p2_slow, p2_fast;
  62. } intel_p2_t;
  63. #define INTEL_P2_NUM 2
  64. typedef struct intel_limit intel_limit_t;
  65. struct intel_limit {
  66. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  67. intel_p2_t p2;
  68. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  69. int, int, intel_clock_t *);
  70. };
  71. /* FDI */
  72. #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
  73. static bool
  74. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  75. int target, int refclk, intel_clock_t *best_clock);
  76. static bool
  77. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  78. int target, int refclk, intel_clock_t *best_clock);
  79. static bool
  80. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  81. int target, int refclk, intel_clock_t *best_clock);
  82. static bool
  83. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  84. int target, int refclk, intel_clock_t *best_clock);
  85. static inline u32 /* units of 100MHz */
  86. intel_fdi_link_freq(struct drm_device *dev)
  87. {
  88. if (IS_GEN5(dev)) {
  89. struct drm_i915_private *dev_priv = dev->dev_private;
  90. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  91. } else
  92. return 27;
  93. }
  94. static const intel_limit_t intel_limits_i8xx_dvo = {
  95. .dot = { .min = 25000, .max = 350000 },
  96. .vco = { .min = 930000, .max = 1400000 },
  97. .n = { .min = 3, .max = 16 },
  98. .m = { .min = 96, .max = 140 },
  99. .m1 = { .min = 18, .max = 26 },
  100. .m2 = { .min = 6, .max = 16 },
  101. .p = { .min = 4, .max = 128 },
  102. .p1 = { .min = 2, .max = 33 },
  103. .p2 = { .dot_limit = 165000,
  104. .p2_slow = 4, .p2_fast = 2 },
  105. .find_pll = intel_find_best_PLL,
  106. };
  107. static const intel_limit_t intel_limits_i8xx_lvds = {
  108. .dot = { .min = 25000, .max = 350000 },
  109. .vco = { .min = 930000, .max = 1400000 },
  110. .n = { .min = 3, .max = 16 },
  111. .m = { .min = 96, .max = 140 },
  112. .m1 = { .min = 18, .max = 26 },
  113. .m2 = { .min = 6, .max = 16 },
  114. .p = { .min = 4, .max = 128 },
  115. .p1 = { .min = 1, .max = 6 },
  116. .p2 = { .dot_limit = 165000,
  117. .p2_slow = 14, .p2_fast = 7 },
  118. .find_pll = intel_find_best_PLL,
  119. };
  120. static const intel_limit_t intel_limits_i9xx_sdvo = {
  121. .dot = { .min = 20000, .max = 400000 },
  122. .vco = { .min = 1400000, .max = 2800000 },
  123. .n = { .min = 1, .max = 6 },
  124. .m = { .min = 70, .max = 120 },
  125. .m1 = { .min = 10, .max = 22 },
  126. .m2 = { .min = 5, .max = 9 },
  127. .p = { .min = 5, .max = 80 },
  128. .p1 = { .min = 1, .max = 8 },
  129. .p2 = { .dot_limit = 200000,
  130. .p2_slow = 10, .p2_fast = 5 },
  131. .find_pll = intel_find_best_PLL,
  132. };
  133. static const intel_limit_t intel_limits_i9xx_lvds = {
  134. .dot = { .min = 20000, .max = 400000 },
  135. .vco = { .min = 1400000, .max = 2800000 },
  136. .n = { .min = 1, .max = 6 },
  137. .m = { .min = 70, .max = 120 },
  138. .m1 = { .min = 10, .max = 22 },
  139. .m2 = { .min = 5, .max = 9 },
  140. .p = { .min = 7, .max = 98 },
  141. .p1 = { .min = 1, .max = 8 },
  142. .p2 = { .dot_limit = 112000,
  143. .p2_slow = 14, .p2_fast = 7 },
  144. .find_pll = intel_find_best_PLL,
  145. };
  146. static const intel_limit_t intel_limits_g4x_sdvo = {
  147. .dot = { .min = 25000, .max = 270000 },
  148. .vco = { .min = 1750000, .max = 3500000},
  149. .n = { .min = 1, .max = 4 },
  150. .m = { .min = 104, .max = 138 },
  151. .m1 = { .min = 17, .max = 23 },
  152. .m2 = { .min = 5, .max = 11 },
  153. .p = { .min = 10, .max = 30 },
  154. .p1 = { .min = 1, .max = 3},
  155. .p2 = { .dot_limit = 270000,
  156. .p2_slow = 10,
  157. .p2_fast = 10
  158. },
  159. .find_pll = intel_g4x_find_best_PLL,
  160. };
  161. static const intel_limit_t intel_limits_g4x_hdmi = {
  162. .dot = { .min = 22000, .max = 400000 },
  163. .vco = { .min = 1750000, .max = 3500000},
  164. .n = { .min = 1, .max = 4 },
  165. .m = { .min = 104, .max = 138 },
  166. .m1 = { .min = 16, .max = 23 },
  167. .m2 = { .min = 5, .max = 11 },
  168. .p = { .min = 5, .max = 80 },
  169. .p1 = { .min = 1, .max = 8},
  170. .p2 = { .dot_limit = 165000,
  171. .p2_slow = 10, .p2_fast = 5 },
  172. .find_pll = intel_g4x_find_best_PLL,
  173. };
  174. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  175. .dot = { .min = 20000, .max = 115000 },
  176. .vco = { .min = 1750000, .max = 3500000 },
  177. .n = { .min = 1, .max = 3 },
  178. .m = { .min = 104, .max = 138 },
  179. .m1 = { .min = 17, .max = 23 },
  180. .m2 = { .min = 5, .max = 11 },
  181. .p = { .min = 28, .max = 112 },
  182. .p1 = { .min = 2, .max = 8 },
  183. .p2 = { .dot_limit = 0,
  184. .p2_slow = 14, .p2_fast = 14
  185. },
  186. .find_pll = intel_g4x_find_best_PLL,
  187. };
  188. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  189. .dot = { .min = 80000, .max = 224000 },
  190. .vco = { .min = 1750000, .max = 3500000 },
  191. .n = { .min = 1, .max = 3 },
  192. .m = { .min = 104, .max = 138 },
  193. .m1 = { .min = 17, .max = 23 },
  194. .m2 = { .min = 5, .max = 11 },
  195. .p = { .min = 14, .max = 42 },
  196. .p1 = { .min = 2, .max = 6 },
  197. .p2 = { .dot_limit = 0,
  198. .p2_slow = 7, .p2_fast = 7
  199. },
  200. .find_pll = intel_g4x_find_best_PLL,
  201. };
  202. static const intel_limit_t intel_limits_g4x_display_port = {
  203. .dot = { .min = 161670, .max = 227000 },
  204. .vco = { .min = 1750000, .max = 3500000},
  205. .n = { .min = 1, .max = 2 },
  206. .m = { .min = 97, .max = 108 },
  207. .m1 = { .min = 0x10, .max = 0x12 },
  208. .m2 = { .min = 0x05, .max = 0x06 },
  209. .p = { .min = 10, .max = 20 },
  210. .p1 = { .min = 1, .max = 2},
  211. .p2 = { .dot_limit = 0,
  212. .p2_slow = 10, .p2_fast = 10 },
  213. .find_pll = intel_find_pll_g4x_dp,
  214. };
  215. static const intel_limit_t intel_limits_pineview_sdvo = {
  216. .dot = { .min = 20000, .max = 400000},
  217. .vco = { .min = 1700000, .max = 3500000 },
  218. /* Pineview's Ncounter is a ring counter */
  219. .n = { .min = 3, .max = 6 },
  220. .m = { .min = 2, .max = 256 },
  221. /* Pineview only has one combined m divider, which we treat as m2. */
  222. .m1 = { .min = 0, .max = 0 },
  223. .m2 = { .min = 0, .max = 254 },
  224. .p = { .min = 5, .max = 80 },
  225. .p1 = { .min = 1, .max = 8 },
  226. .p2 = { .dot_limit = 200000,
  227. .p2_slow = 10, .p2_fast = 5 },
  228. .find_pll = intel_find_best_PLL,
  229. };
  230. static const intel_limit_t intel_limits_pineview_lvds = {
  231. .dot = { .min = 20000, .max = 400000 },
  232. .vco = { .min = 1700000, .max = 3500000 },
  233. .n = { .min = 3, .max = 6 },
  234. .m = { .min = 2, .max = 256 },
  235. .m1 = { .min = 0, .max = 0 },
  236. .m2 = { .min = 0, .max = 254 },
  237. .p = { .min = 7, .max = 112 },
  238. .p1 = { .min = 1, .max = 8 },
  239. .p2 = { .dot_limit = 112000,
  240. .p2_slow = 14, .p2_fast = 14 },
  241. .find_pll = intel_find_best_PLL,
  242. };
  243. /* Ironlake / Sandybridge
  244. *
  245. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  246. * the range value for them is (actual_value - 2).
  247. */
  248. static const intel_limit_t intel_limits_ironlake_dac = {
  249. .dot = { .min = 25000, .max = 350000 },
  250. .vco = { .min = 1760000, .max = 3510000 },
  251. .n = { .min = 1, .max = 5 },
  252. .m = { .min = 79, .max = 127 },
  253. .m1 = { .min = 12, .max = 22 },
  254. .m2 = { .min = 5, .max = 9 },
  255. .p = { .min = 5, .max = 80 },
  256. .p1 = { .min = 1, .max = 8 },
  257. .p2 = { .dot_limit = 225000,
  258. .p2_slow = 10, .p2_fast = 5 },
  259. .find_pll = intel_g4x_find_best_PLL,
  260. };
  261. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  262. .dot = { .min = 25000, .max = 350000 },
  263. .vco = { .min = 1760000, .max = 3510000 },
  264. .n = { .min = 1, .max = 3 },
  265. .m = { .min = 79, .max = 118 },
  266. .m1 = { .min = 12, .max = 22 },
  267. .m2 = { .min = 5, .max = 9 },
  268. .p = { .min = 28, .max = 112 },
  269. .p1 = { .min = 2, .max = 8 },
  270. .p2 = { .dot_limit = 225000,
  271. .p2_slow = 14, .p2_fast = 14 },
  272. .find_pll = intel_g4x_find_best_PLL,
  273. };
  274. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  275. .dot = { .min = 25000, .max = 350000 },
  276. .vco = { .min = 1760000, .max = 3510000 },
  277. .n = { .min = 1, .max = 3 },
  278. .m = { .min = 79, .max = 127 },
  279. .m1 = { .min = 12, .max = 22 },
  280. .m2 = { .min = 5, .max = 9 },
  281. .p = { .min = 14, .max = 56 },
  282. .p1 = { .min = 2, .max = 8 },
  283. .p2 = { .dot_limit = 225000,
  284. .p2_slow = 7, .p2_fast = 7 },
  285. .find_pll = intel_g4x_find_best_PLL,
  286. };
  287. /* LVDS 100mhz refclk limits. */
  288. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  289. .dot = { .min = 25000, .max = 350000 },
  290. .vco = { .min = 1760000, .max = 3510000 },
  291. .n = { .min = 1, .max = 2 },
  292. .m = { .min = 79, .max = 126 },
  293. .m1 = { .min = 12, .max = 22 },
  294. .m2 = { .min = 5, .max = 9 },
  295. .p = { .min = 28, .max = 112 },
  296. .p1 = { .min = 2,.max = 8 },
  297. .p2 = { .dot_limit = 225000,
  298. .p2_slow = 14, .p2_fast = 14 },
  299. .find_pll = intel_g4x_find_best_PLL,
  300. };
  301. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  302. .dot = { .min = 25000, .max = 350000 },
  303. .vco = { .min = 1760000, .max = 3510000 },
  304. .n = { .min = 1, .max = 3 },
  305. .m = { .min = 79, .max = 126 },
  306. .m1 = { .min = 12, .max = 22 },
  307. .m2 = { .min = 5, .max = 9 },
  308. .p = { .min = 14, .max = 42 },
  309. .p1 = { .min = 2,.max = 6 },
  310. .p2 = { .dot_limit = 225000,
  311. .p2_slow = 7, .p2_fast = 7 },
  312. .find_pll = intel_g4x_find_best_PLL,
  313. };
  314. static const intel_limit_t intel_limits_ironlake_display_port = {
  315. .dot = { .min = 25000, .max = 350000 },
  316. .vco = { .min = 1760000, .max = 3510000},
  317. .n = { .min = 1, .max = 2 },
  318. .m = { .min = 81, .max = 90 },
  319. .m1 = { .min = 12, .max = 22 },
  320. .m2 = { .min = 5, .max = 9 },
  321. .p = { .min = 10, .max = 20 },
  322. .p1 = { .min = 1, .max = 2},
  323. .p2 = { .dot_limit = 0,
  324. .p2_slow = 10, .p2_fast = 10 },
  325. .find_pll = intel_find_pll_ironlake_dp,
  326. };
  327. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  328. int refclk)
  329. {
  330. struct drm_device *dev = crtc->dev;
  331. struct drm_i915_private *dev_priv = dev->dev_private;
  332. const intel_limit_t *limit;
  333. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  334. if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
  335. LVDS_CLKB_POWER_UP) {
  336. /* LVDS dual channel */
  337. if (refclk == 100000)
  338. limit = &intel_limits_ironlake_dual_lvds_100m;
  339. else
  340. limit = &intel_limits_ironlake_dual_lvds;
  341. } else {
  342. if (refclk == 100000)
  343. limit = &intel_limits_ironlake_single_lvds_100m;
  344. else
  345. limit = &intel_limits_ironlake_single_lvds;
  346. }
  347. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  348. HAS_eDP)
  349. limit = &intel_limits_ironlake_display_port;
  350. else
  351. limit = &intel_limits_ironlake_dac;
  352. return limit;
  353. }
  354. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  355. {
  356. struct drm_device *dev = crtc->dev;
  357. struct drm_i915_private *dev_priv = dev->dev_private;
  358. const intel_limit_t *limit;
  359. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  360. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  361. LVDS_CLKB_POWER_UP)
  362. /* LVDS with dual channel */
  363. limit = &intel_limits_g4x_dual_channel_lvds;
  364. else
  365. /* LVDS with dual channel */
  366. limit = &intel_limits_g4x_single_channel_lvds;
  367. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  368. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  369. limit = &intel_limits_g4x_hdmi;
  370. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  371. limit = &intel_limits_g4x_sdvo;
  372. } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  373. limit = &intel_limits_g4x_display_port;
  374. } else /* The option is for other outputs */
  375. limit = &intel_limits_i9xx_sdvo;
  376. return limit;
  377. }
  378. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  379. {
  380. struct drm_device *dev = crtc->dev;
  381. const intel_limit_t *limit;
  382. if (HAS_PCH_SPLIT(dev))
  383. limit = intel_ironlake_limit(crtc, refclk);
  384. else if (IS_G4X(dev)) {
  385. limit = intel_g4x_limit(crtc);
  386. } else if (IS_PINEVIEW(dev)) {
  387. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  388. limit = &intel_limits_pineview_lvds;
  389. else
  390. limit = &intel_limits_pineview_sdvo;
  391. } else if (!IS_GEN2(dev)) {
  392. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  393. limit = &intel_limits_i9xx_lvds;
  394. else
  395. limit = &intel_limits_i9xx_sdvo;
  396. } else {
  397. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  398. limit = &intel_limits_i8xx_lvds;
  399. else
  400. limit = &intel_limits_i8xx_dvo;
  401. }
  402. return limit;
  403. }
  404. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  405. static void pineview_clock(int refclk, intel_clock_t *clock)
  406. {
  407. clock->m = clock->m2 + 2;
  408. clock->p = clock->p1 * clock->p2;
  409. clock->vco = refclk * clock->m / clock->n;
  410. clock->dot = clock->vco / clock->p;
  411. }
  412. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  413. {
  414. if (IS_PINEVIEW(dev)) {
  415. pineview_clock(refclk, clock);
  416. return;
  417. }
  418. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  419. clock->p = clock->p1 * clock->p2;
  420. clock->vco = refclk * clock->m / (clock->n + 2);
  421. clock->dot = clock->vco / clock->p;
  422. }
  423. /**
  424. * Returns whether any output on the specified pipe is of the specified type
  425. */
  426. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  427. {
  428. struct drm_device *dev = crtc->dev;
  429. struct drm_mode_config *mode_config = &dev->mode_config;
  430. struct intel_encoder *encoder;
  431. list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
  432. if (encoder->base.crtc == crtc && encoder->type == type)
  433. return true;
  434. return false;
  435. }
  436. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  437. /**
  438. * Returns whether the given set of divisors are valid for a given refclk with
  439. * the given connectors.
  440. */
  441. static bool intel_PLL_is_valid(struct drm_device *dev,
  442. const intel_limit_t *limit,
  443. const intel_clock_t *clock)
  444. {
  445. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  446. INTELPllInvalid ("p1 out of range\n");
  447. if (clock->p < limit->p.min || limit->p.max < clock->p)
  448. INTELPllInvalid ("p out of range\n");
  449. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  450. INTELPllInvalid ("m2 out of range\n");
  451. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  452. INTELPllInvalid ("m1 out of range\n");
  453. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  454. INTELPllInvalid ("m1 <= m2\n");
  455. if (clock->m < limit->m.min || limit->m.max < clock->m)
  456. INTELPllInvalid ("m out of range\n");
  457. if (clock->n < limit->n.min || limit->n.max < clock->n)
  458. INTELPllInvalid ("n out of range\n");
  459. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  460. INTELPllInvalid ("vco out of range\n");
  461. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  462. * connector, etc., rather than just a single range.
  463. */
  464. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  465. INTELPllInvalid ("dot out of range\n");
  466. return true;
  467. }
  468. static bool
  469. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  470. int target, int refclk, intel_clock_t *best_clock)
  471. {
  472. struct drm_device *dev = crtc->dev;
  473. struct drm_i915_private *dev_priv = dev->dev_private;
  474. intel_clock_t clock;
  475. int err = target;
  476. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  477. (I915_READ(LVDS)) != 0) {
  478. /*
  479. * For LVDS, if the panel is on, just rely on its current
  480. * settings for dual-channel. We haven't figured out how to
  481. * reliably set up different single/dual channel state, if we
  482. * even can.
  483. */
  484. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  485. LVDS_CLKB_POWER_UP)
  486. clock.p2 = limit->p2.p2_fast;
  487. else
  488. clock.p2 = limit->p2.p2_slow;
  489. } else {
  490. if (target < limit->p2.dot_limit)
  491. clock.p2 = limit->p2.p2_slow;
  492. else
  493. clock.p2 = limit->p2.p2_fast;
  494. }
  495. memset (best_clock, 0, sizeof (*best_clock));
  496. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  497. clock.m1++) {
  498. for (clock.m2 = limit->m2.min;
  499. clock.m2 <= limit->m2.max; clock.m2++) {
  500. /* m1 is always 0 in Pineview */
  501. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  502. break;
  503. for (clock.n = limit->n.min;
  504. clock.n <= limit->n.max; clock.n++) {
  505. for (clock.p1 = limit->p1.min;
  506. clock.p1 <= limit->p1.max; clock.p1++) {
  507. int this_err;
  508. intel_clock(dev, refclk, &clock);
  509. if (!intel_PLL_is_valid(dev, limit,
  510. &clock))
  511. continue;
  512. this_err = abs(clock.dot - target);
  513. if (this_err < err) {
  514. *best_clock = clock;
  515. err = this_err;
  516. }
  517. }
  518. }
  519. }
  520. }
  521. return (err != target);
  522. }
  523. static bool
  524. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  525. int target, int refclk, intel_clock_t *best_clock)
  526. {
  527. struct drm_device *dev = crtc->dev;
  528. struct drm_i915_private *dev_priv = dev->dev_private;
  529. intel_clock_t clock;
  530. int max_n;
  531. bool found;
  532. /* approximately equals target * 0.00585 */
  533. int err_most = (target >> 8) + (target >> 9);
  534. found = false;
  535. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  536. int lvds_reg;
  537. if (HAS_PCH_SPLIT(dev))
  538. lvds_reg = PCH_LVDS;
  539. else
  540. lvds_reg = LVDS;
  541. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  542. LVDS_CLKB_POWER_UP)
  543. clock.p2 = limit->p2.p2_fast;
  544. else
  545. clock.p2 = limit->p2.p2_slow;
  546. } else {
  547. if (target < limit->p2.dot_limit)
  548. clock.p2 = limit->p2.p2_slow;
  549. else
  550. clock.p2 = limit->p2.p2_fast;
  551. }
  552. memset(best_clock, 0, sizeof(*best_clock));
  553. max_n = limit->n.max;
  554. /* based on hardware requirement, prefer smaller n to precision */
  555. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  556. /* based on hardware requirement, prefere larger m1,m2 */
  557. for (clock.m1 = limit->m1.max;
  558. clock.m1 >= limit->m1.min; clock.m1--) {
  559. for (clock.m2 = limit->m2.max;
  560. clock.m2 >= limit->m2.min; clock.m2--) {
  561. for (clock.p1 = limit->p1.max;
  562. clock.p1 >= limit->p1.min; clock.p1--) {
  563. int this_err;
  564. intel_clock(dev, refclk, &clock);
  565. if (!intel_PLL_is_valid(dev, limit,
  566. &clock))
  567. continue;
  568. this_err = abs(clock.dot - target);
  569. if (this_err < err_most) {
  570. *best_clock = clock;
  571. err_most = this_err;
  572. max_n = clock.n;
  573. found = true;
  574. }
  575. }
  576. }
  577. }
  578. }
  579. return found;
  580. }
  581. static bool
  582. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  583. int target, int refclk, intel_clock_t *best_clock)
  584. {
  585. struct drm_device *dev = crtc->dev;
  586. intel_clock_t clock;
  587. if (target < 200000) {
  588. clock.n = 1;
  589. clock.p1 = 2;
  590. clock.p2 = 10;
  591. clock.m1 = 12;
  592. clock.m2 = 9;
  593. } else {
  594. clock.n = 2;
  595. clock.p1 = 1;
  596. clock.p2 = 10;
  597. clock.m1 = 14;
  598. clock.m2 = 8;
  599. }
  600. intel_clock(dev, refclk, &clock);
  601. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  602. return true;
  603. }
  604. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  605. static bool
  606. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  607. int target, int refclk, intel_clock_t *best_clock)
  608. {
  609. intel_clock_t clock;
  610. if (target < 200000) {
  611. clock.p1 = 2;
  612. clock.p2 = 10;
  613. clock.n = 2;
  614. clock.m1 = 23;
  615. clock.m2 = 8;
  616. } else {
  617. clock.p1 = 1;
  618. clock.p2 = 10;
  619. clock.n = 1;
  620. clock.m1 = 14;
  621. clock.m2 = 2;
  622. }
  623. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  624. clock.p = (clock.p1 * clock.p2);
  625. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  626. clock.vco = 0;
  627. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  628. return true;
  629. }
  630. /**
  631. * intel_wait_for_vblank - wait for vblank on a given pipe
  632. * @dev: drm device
  633. * @pipe: pipe to wait for
  634. *
  635. * Wait for vblank to occur on a given pipe. Needed for various bits of
  636. * mode setting code.
  637. */
  638. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  639. {
  640. struct drm_i915_private *dev_priv = dev->dev_private;
  641. int pipestat_reg = PIPESTAT(pipe);
  642. /* Clear existing vblank status. Note this will clear any other
  643. * sticky status fields as well.
  644. *
  645. * This races with i915_driver_irq_handler() with the result
  646. * that either function could miss a vblank event. Here it is not
  647. * fatal, as we will either wait upon the next vblank interrupt or
  648. * timeout. Generally speaking intel_wait_for_vblank() is only
  649. * called during modeset at which time the GPU should be idle and
  650. * should *not* be performing page flips and thus not waiting on
  651. * vblanks...
  652. * Currently, the result of us stealing a vblank from the irq
  653. * handler is that a single frame will be skipped during swapbuffers.
  654. */
  655. I915_WRITE(pipestat_reg,
  656. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  657. /* Wait for vblank interrupt bit to set */
  658. if (wait_for(I915_READ(pipestat_reg) &
  659. PIPE_VBLANK_INTERRUPT_STATUS,
  660. 50))
  661. DRM_DEBUG_KMS("vblank wait timed out\n");
  662. }
  663. /*
  664. * intel_wait_for_pipe_off - wait for pipe to turn off
  665. * @dev: drm device
  666. * @pipe: pipe to wait for
  667. *
  668. * After disabling a pipe, we can't wait for vblank in the usual way,
  669. * spinning on the vblank interrupt status bit, since we won't actually
  670. * see an interrupt when the pipe is disabled.
  671. *
  672. * On Gen4 and above:
  673. * wait for the pipe register state bit to turn off
  674. *
  675. * Otherwise:
  676. * wait for the display line value to settle (it usually
  677. * ends up stopping at the start of the next frame).
  678. *
  679. */
  680. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  681. {
  682. struct drm_i915_private *dev_priv = dev->dev_private;
  683. if (INTEL_INFO(dev)->gen >= 4) {
  684. int reg = PIPECONF(pipe);
  685. /* Wait for the Pipe State to go off */
  686. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  687. 100))
  688. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  689. } else {
  690. u32 last_line;
  691. int reg = PIPEDSL(pipe);
  692. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  693. /* Wait for the display line to settle */
  694. do {
  695. last_line = I915_READ(reg) & DSL_LINEMASK;
  696. mdelay(5);
  697. } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
  698. time_after(timeout, jiffies));
  699. if (time_after(jiffies, timeout))
  700. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  701. }
  702. }
  703. static const char *state_string(bool enabled)
  704. {
  705. return enabled ? "on" : "off";
  706. }
  707. /* Only for pre-ILK configs */
  708. static void assert_pll(struct drm_i915_private *dev_priv,
  709. enum pipe pipe, bool state)
  710. {
  711. int reg;
  712. u32 val;
  713. bool cur_state;
  714. reg = DPLL(pipe);
  715. val = I915_READ(reg);
  716. cur_state = !!(val & DPLL_VCO_ENABLE);
  717. WARN(cur_state != state,
  718. "PLL state assertion failure (expected %s, current %s)\n",
  719. state_string(state), state_string(cur_state));
  720. }
  721. #define assert_pll_enabled(d, p) assert_pll(d, p, true)
  722. #define assert_pll_disabled(d, p) assert_pll(d, p, false)
  723. /* For ILK+ */
  724. static void assert_pch_pll(struct drm_i915_private *dev_priv,
  725. enum pipe pipe, bool state)
  726. {
  727. int reg;
  728. u32 val;
  729. bool cur_state;
  730. reg = PCH_DPLL(pipe);
  731. val = I915_READ(reg);
  732. cur_state = !!(val & DPLL_VCO_ENABLE);
  733. WARN(cur_state != state,
  734. "PCH PLL state assertion failure (expected %s, current %s)\n",
  735. state_string(state), state_string(cur_state));
  736. }
  737. #define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
  738. #define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
  739. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  740. enum pipe pipe, bool state)
  741. {
  742. int reg;
  743. u32 val;
  744. bool cur_state;
  745. reg = FDI_TX_CTL(pipe);
  746. val = I915_READ(reg);
  747. cur_state = !!(val & FDI_TX_ENABLE);
  748. WARN(cur_state != state,
  749. "FDI TX state assertion failure (expected %s, current %s)\n",
  750. state_string(state), state_string(cur_state));
  751. }
  752. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  753. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  754. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  755. enum pipe pipe, bool state)
  756. {
  757. int reg;
  758. u32 val;
  759. bool cur_state;
  760. reg = FDI_RX_CTL(pipe);
  761. val = I915_READ(reg);
  762. cur_state = !!(val & FDI_RX_ENABLE);
  763. WARN(cur_state != state,
  764. "FDI RX state assertion failure (expected %s, current %s)\n",
  765. state_string(state), state_string(cur_state));
  766. }
  767. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  768. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  769. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  770. enum pipe pipe)
  771. {
  772. int reg;
  773. u32 val;
  774. /* ILK FDI PLL is always enabled */
  775. if (dev_priv->info->gen == 5)
  776. return;
  777. reg = FDI_TX_CTL(pipe);
  778. val = I915_READ(reg);
  779. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  780. }
  781. static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
  782. enum pipe pipe)
  783. {
  784. int reg;
  785. u32 val;
  786. reg = FDI_RX_CTL(pipe);
  787. val = I915_READ(reg);
  788. WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
  789. }
  790. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  791. enum pipe pipe)
  792. {
  793. int pp_reg, lvds_reg;
  794. u32 val;
  795. enum pipe panel_pipe = PIPE_A;
  796. bool locked = locked;
  797. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  798. pp_reg = PCH_PP_CONTROL;
  799. lvds_reg = PCH_LVDS;
  800. } else {
  801. pp_reg = PP_CONTROL;
  802. lvds_reg = LVDS;
  803. }
  804. val = I915_READ(pp_reg);
  805. if (!(val & PANEL_POWER_ON) ||
  806. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  807. locked = false;
  808. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  809. panel_pipe = PIPE_B;
  810. WARN(panel_pipe == pipe && locked,
  811. "panel assertion failure, pipe %c regs locked\n",
  812. pipe_name(pipe));
  813. }
  814. static void assert_pipe(struct drm_i915_private *dev_priv,
  815. enum pipe pipe, bool state)
  816. {
  817. int reg;
  818. u32 val;
  819. bool cur_state;
  820. reg = PIPECONF(pipe);
  821. val = I915_READ(reg);
  822. cur_state = !!(val & PIPECONF_ENABLE);
  823. WARN(cur_state != state,
  824. "pipe %c assertion failure (expected %s, current %s)\n",
  825. pipe_name(pipe), state_string(state), state_string(cur_state));
  826. }
  827. #define assert_pipe_enabled(d, p) assert_pipe(d, p, true)
  828. #define assert_pipe_disabled(d, p) assert_pipe(d, p, false)
  829. static void assert_plane_enabled(struct drm_i915_private *dev_priv,
  830. enum plane plane)
  831. {
  832. int reg;
  833. u32 val;
  834. reg = DSPCNTR(plane);
  835. val = I915_READ(reg);
  836. WARN(!(val & DISPLAY_PLANE_ENABLE),
  837. "plane %c assertion failure, should be active but is disabled\n",
  838. plane_name(plane));
  839. }
  840. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  841. enum pipe pipe)
  842. {
  843. int reg, i;
  844. u32 val;
  845. int cur_pipe;
  846. /* Planes are fixed to pipes on ILK+ */
  847. if (HAS_PCH_SPLIT(dev_priv->dev))
  848. return;
  849. /* Need to check both planes against the pipe */
  850. for (i = 0; i < 2; i++) {
  851. reg = DSPCNTR(i);
  852. val = I915_READ(reg);
  853. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  854. DISPPLANE_SEL_PIPE_SHIFT;
  855. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  856. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  857. plane_name(i), pipe_name(pipe));
  858. }
  859. }
  860. static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  861. {
  862. u32 val;
  863. bool enabled;
  864. val = I915_READ(PCH_DREF_CONTROL);
  865. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  866. DREF_SUPERSPREAD_SOURCE_MASK));
  867. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  868. }
  869. static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
  870. enum pipe pipe)
  871. {
  872. int reg;
  873. u32 val;
  874. bool enabled;
  875. reg = TRANSCONF(pipe);
  876. val = I915_READ(reg);
  877. enabled = !!(val & TRANS_ENABLE);
  878. WARN(enabled,
  879. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  880. pipe_name(pipe));
  881. }
  882. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  883. enum pipe pipe, int reg)
  884. {
  885. u32 val = I915_READ(reg);
  886. WARN(DP_PIPE_ENABLED(val, pipe),
  887. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  888. reg, pipe_name(pipe));
  889. }
  890. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  891. enum pipe pipe, int reg)
  892. {
  893. u32 val = I915_READ(reg);
  894. WARN(HDMI_PIPE_ENABLED(val, pipe),
  895. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  896. reg, pipe_name(pipe));
  897. }
  898. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  899. enum pipe pipe)
  900. {
  901. int reg;
  902. u32 val;
  903. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B);
  904. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C);
  905. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D);
  906. reg = PCH_ADPA;
  907. val = I915_READ(reg);
  908. WARN(ADPA_PIPE_ENABLED(val, pipe),
  909. "PCH VGA enabled on transcoder %c, should be disabled\n",
  910. pipe_name(pipe));
  911. reg = PCH_LVDS;
  912. val = I915_READ(reg);
  913. WARN(LVDS_PIPE_ENABLED(val, pipe),
  914. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  915. pipe_name(pipe));
  916. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
  917. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
  918. assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
  919. }
  920. /**
  921. * intel_enable_pll - enable a PLL
  922. * @dev_priv: i915 private structure
  923. * @pipe: pipe PLL to enable
  924. *
  925. * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
  926. * make sure the PLL reg is writable first though, since the panel write
  927. * protect mechanism may be enabled.
  928. *
  929. * Note! This is for pre-ILK only.
  930. */
  931. static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  932. {
  933. int reg;
  934. u32 val;
  935. /* No really, not for ILK+ */
  936. BUG_ON(dev_priv->info->gen >= 5);
  937. /* PLL is protected by panel, make sure we can write it */
  938. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  939. assert_panel_unlocked(dev_priv, pipe);
  940. reg = DPLL(pipe);
  941. val = I915_READ(reg);
  942. val |= DPLL_VCO_ENABLE;
  943. /* We do this three times for luck */
  944. I915_WRITE(reg, val);
  945. POSTING_READ(reg);
  946. udelay(150); /* wait for warmup */
  947. I915_WRITE(reg, val);
  948. POSTING_READ(reg);
  949. udelay(150); /* wait for warmup */
  950. I915_WRITE(reg, val);
  951. POSTING_READ(reg);
  952. udelay(150); /* wait for warmup */
  953. }
  954. /**
  955. * intel_disable_pll - disable a PLL
  956. * @dev_priv: i915 private structure
  957. * @pipe: pipe PLL to disable
  958. *
  959. * Disable the PLL for @pipe, making sure the pipe is off first.
  960. *
  961. * Note! This is for pre-ILK only.
  962. */
  963. static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  964. {
  965. int reg;
  966. u32 val;
  967. /* Don't disable pipe A or pipe A PLLs if needed */
  968. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  969. return;
  970. /* Make sure the pipe isn't still relying on us */
  971. assert_pipe_disabled(dev_priv, pipe);
  972. reg = DPLL(pipe);
  973. val = I915_READ(reg);
  974. val &= ~DPLL_VCO_ENABLE;
  975. I915_WRITE(reg, val);
  976. POSTING_READ(reg);
  977. }
  978. /**
  979. * intel_enable_pch_pll - enable PCH PLL
  980. * @dev_priv: i915 private structure
  981. * @pipe: pipe PLL to enable
  982. *
  983. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  984. * drives the transcoder clock.
  985. */
  986. static void intel_enable_pch_pll(struct drm_i915_private *dev_priv,
  987. enum pipe pipe)
  988. {
  989. int reg;
  990. u32 val;
  991. /* PCH only available on ILK+ */
  992. BUG_ON(dev_priv->info->gen < 5);
  993. /* PCH refclock must be enabled first */
  994. assert_pch_refclk_enabled(dev_priv);
  995. reg = PCH_DPLL(pipe);
  996. val = I915_READ(reg);
  997. val |= DPLL_VCO_ENABLE;
  998. I915_WRITE(reg, val);
  999. POSTING_READ(reg);
  1000. udelay(200);
  1001. }
  1002. static void intel_disable_pch_pll(struct drm_i915_private *dev_priv,
  1003. enum pipe pipe)
  1004. {
  1005. int reg;
  1006. u32 val;
  1007. /* PCH only available on ILK+ */
  1008. BUG_ON(dev_priv->info->gen < 5);
  1009. /* Make sure transcoder isn't still depending on us */
  1010. assert_transcoder_disabled(dev_priv, pipe);
  1011. reg = PCH_DPLL(pipe);
  1012. val = I915_READ(reg);
  1013. val &= ~DPLL_VCO_ENABLE;
  1014. I915_WRITE(reg, val);
  1015. POSTING_READ(reg);
  1016. udelay(200);
  1017. }
  1018. static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
  1019. enum pipe pipe)
  1020. {
  1021. int reg;
  1022. u32 val;
  1023. /* PCH only available on ILK+ */
  1024. BUG_ON(dev_priv->info->gen < 5);
  1025. /* Make sure PCH DPLL is enabled */
  1026. assert_pch_pll_enabled(dev_priv, pipe);
  1027. /* FDI must be feeding us bits for PCH ports */
  1028. assert_fdi_tx_enabled(dev_priv, pipe);
  1029. assert_fdi_rx_enabled(dev_priv, pipe);
  1030. reg = TRANSCONF(pipe);
  1031. val = I915_READ(reg);
  1032. if (HAS_PCH_IBX(dev_priv->dev)) {
  1033. /*
  1034. * make the BPC in transcoder be consistent with
  1035. * that in pipeconf reg.
  1036. */
  1037. val &= ~PIPE_BPC_MASK;
  1038. val |= I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK;
  1039. }
  1040. I915_WRITE(reg, val | TRANS_ENABLE);
  1041. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1042. DRM_ERROR("failed to enable transcoder %d\n", pipe);
  1043. }
  1044. static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
  1045. enum pipe pipe)
  1046. {
  1047. int reg;
  1048. u32 val;
  1049. /* FDI relies on the transcoder */
  1050. assert_fdi_tx_disabled(dev_priv, pipe);
  1051. assert_fdi_rx_disabled(dev_priv, pipe);
  1052. /* Ports must be off as well */
  1053. assert_pch_ports_disabled(dev_priv, pipe);
  1054. reg = TRANSCONF(pipe);
  1055. val = I915_READ(reg);
  1056. val &= ~TRANS_ENABLE;
  1057. I915_WRITE(reg, val);
  1058. /* wait for PCH transcoder off, transcoder state */
  1059. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1060. DRM_ERROR("failed to disable transcoder\n");
  1061. }
  1062. /**
  1063. * intel_enable_pipe - enable a pipe, asserting requirements
  1064. * @dev_priv: i915 private structure
  1065. * @pipe: pipe to enable
  1066. * @pch_port: on ILK+, is this pipe driving a PCH port or not
  1067. *
  1068. * Enable @pipe, making sure that various hardware specific requirements
  1069. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1070. *
  1071. * @pipe should be %PIPE_A or %PIPE_B.
  1072. *
  1073. * Will wait until the pipe is actually running (i.e. first vblank) before
  1074. * returning.
  1075. */
  1076. static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
  1077. bool pch_port)
  1078. {
  1079. int reg;
  1080. u32 val;
  1081. /*
  1082. * A pipe without a PLL won't actually be able to drive bits from
  1083. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1084. * need the check.
  1085. */
  1086. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1087. assert_pll_enabled(dev_priv, pipe);
  1088. else {
  1089. if (pch_port) {
  1090. /* if driving the PCH, we need FDI enabled */
  1091. assert_fdi_rx_pll_enabled(dev_priv, pipe);
  1092. assert_fdi_tx_pll_enabled(dev_priv, pipe);
  1093. }
  1094. /* FIXME: assert CPU port conditions for SNB+ */
  1095. }
  1096. reg = PIPECONF(pipe);
  1097. val = I915_READ(reg);
  1098. if (val & PIPECONF_ENABLE)
  1099. return;
  1100. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1101. intel_wait_for_vblank(dev_priv->dev, pipe);
  1102. }
  1103. /**
  1104. * intel_disable_pipe - disable a pipe, asserting requirements
  1105. * @dev_priv: i915 private structure
  1106. * @pipe: pipe to disable
  1107. *
  1108. * Disable @pipe, making sure that various hardware specific requirements
  1109. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1110. *
  1111. * @pipe should be %PIPE_A or %PIPE_B.
  1112. *
  1113. * Will wait until the pipe has shut down before returning.
  1114. */
  1115. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1116. enum pipe pipe)
  1117. {
  1118. int reg;
  1119. u32 val;
  1120. /*
  1121. * Make sure planes won't keep trying to pump pixels to us,
  1122. * or we might hang the display.
  1123. */
  1124. assert_planes_disabled(dev_priv, pipe);
  1125. /* Don't disable pipe A or pipe A PLLs if needed */
  1126. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1127. return;
  1128. reg = PIPECONF(pipe);
  1129. val = I915_READ(reg);
  1130. if ((val & PIPECONF_ENABLE) == 0)
  1131. return;
  1132. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1133. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1134. }
  1135. /**
  1136. * intel_enable_plane - enable a display plane on a given pipe
  1137. * @dev_priv: i915 private structure
  1138. * @plane: plane to enable
  1139. * @pipe: pipe being fed
  1140. *
  1141. * Enable @plane on @pipe, making sure that @pipe is running first.
  1142. */
  1143. static void intel_enable_plane(struct drm_i915_private *dev_priv,
  1144. enum plane plane, enum pipe pipe)
  1145. {
  1146. int reg;
  1147. u32 val;
  1148. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1149. assert_pipe_enabled(dev_priv, pipe);
  1150. reg = DSPCNTR(plane);
  1151. val = I915_READ(reg);
  1152. if (val & DISPLAY_PLANE_ENABLE)
  1153. return;
  1154. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1155. intel_wait_for_vblank(dev_priv->dev, pipe);
  1156. }
  1157. /*
  1158. * Plane regs are double buffered, going from enabled->disabled needs a
  1159. * trigger in order to latch. The display address reg provides this.
  1160. */
  1161. static void intel_flush_display_plane(struct drm_i915_private *dev_priv,
  1162. enum plane plane)
  1163. {
  1164. u32 reg = DSPADDR(plane);
  1165. I915_WRITE(reg, I915_READ(reg));
  1166. }
  1167. /**
  1168. * intel_disable_plane - disable a display plane
  1169. * @dev_priv: i915 private structure
  1170. * @plane: plane to disable
  1171. * @pipe: pipe consuming the data
  1172. *
  1173. * Disable @plane; should be an independent operation.
  1174. */
  1175. static void intel_disable_plane(struct drm_i915_private *dev_priv,
  1176. enum plane plane, enum pipe pipe)
  1177. {
  1178. int reg;
  1179. u32 val;
  1180. reg = DSPCNTR(plane);
  1181. val = I915_READ(reg);
  1182. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  1183. return;
  1184. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1185. intel_flush_display_plane(dev_priv, plane);
  1186. intel_wait_for_vblank(dev_priv->dev, pipe);
  1187. }
  1188. static void disable_pch_dp(struct drm_i915_private *dev_priv,
  1189. enum pipe pipe, int reg)
  1190. {
  1191. u32 val = I915_READ(reg);
  1192. if (DP_PIPE_ENABLED(val, pipe))
  1193. I915_WRITE(reg, val & ~DP_PORT_EN);
  1194. }
  1195. static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
  1196. enum pipe pipe, int reg)
  1197. {
  1198. u32 val = I915_READ(reg);
  1199. if (HDMI_PIPE_ENABLED(val, pipe))
  1200. I915_WRITE(reg, val & ~PORT_ENABLE);
  1201. }
  1202. /* Disable any ports connected to this transcoder */
  1203. static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
  1204. enum pipe pipe)
  1205. {
  1206. u32 reg, val;
  1207. val = I915_READ(PCH_PP_CONTROL);
  1208. I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
  1209. disable_pch_dp(dev_priv, pipe, PCH_DP_B);
  1210. disable_pch_dp(dev_priv, pipe, PCH_DP_C);
  1211. disable_pch_dp(dev_priv, pipe, PCH_DP_D);
  1212. reg = PCH_ADPA;
  1213. val = I915_READ(reg);
  1214. if (ADPA_PIPE_ENABLED(val, pipe))
  1215. I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
  1216. reg = PCH_LVDS;
  1217. val = I915_READ(reg);
  1218. if (LVDS_PIPE_ENABLED(val, pipe)) {
  1219. I915_WRITE(reg, val & ~LVDS_PORT_EN);
  1220. POSTING_READ(reg);
  1221. udelay(100);
  1222. }
  1223. disable_pch_hdmi(dev_priv, pipe, HDMIB);
  1224. disable_pch_hdmi(dev_priv, pipe, HDMIC);
  1225. disable_pch_hdmi(dev_priv, pipe, HDMID);
  1226. }
  1227. static void i8xx_disable_fbc(struct drm_device *dev)
  1228. {
  1229. struct drm_i915_private *dev_priv = dev->dev_private;
  1230. u32 fbc_ctl;
  1231. /* Disable compression */
  1232. fbc_ctl = I915_READ(FBC_CONTROL);
  1233. if ((fbc_ctl & FBC_CTL_EN) == 0)
  1234. return;
  1235. fbc_ctl &= ~FBC_CTL_EN;
  1236. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1237. /* Wait for compressing bit to clear */
  1238. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  1239. DRM_DEBUG_KMS("FBC idle timed out\n");
  1240. return;
  1241. }
  1242. DRM_DEBUG_KMS("disabled FBC\n");
  1243. }
  1244. static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1245. {
  1246. struct drm_device *dev = crtc->dev;
  1247. struct drm_i915_private *dev_priv = dev->dev_private;
  1248. struct drm_framebuffer *fb = crtc->fb;
  1249. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1250. struct drm_i915_gem_object *obj = intel_fb->obj;
  1251. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1252. int plane, i;
  1253. u32 fbc_ctl, fbc_ctl2;
  1254. if (fb->pitch == dev_priv->cfb_pitch &&
  1255. obj->fence_reg == dev_priv->cfb_fence &&
  1256. intel_crtc->plane == dev_priv->cfb_plane &&
  1257. I915_READ(FBC_CONTROL) & FBC_CTL_EN)
  1258. return;
  1259. i8xx_disable_fbc(dev);
  1260. dev_priv->cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  1261. if (fb->pitch < dev_priv->cfb_pitch)
  1262. dev_priv->cfb_pitch = fb->pitch;
  1263. /* FBC_CTL wants 64B units */
  1264. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  1265. dev_priv->cfb_fence = obj->fence_reg;
  1266. dev_priv->cfb_plane = intel_crtc->plane;
  1267. plane = dev_priv->cfb_plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  1268. /* Clear old tags */
  1269. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  1270. I915_WRITE(FBC_TAG + (i * 4), 0);
  1271. /* Set it up... */
  1272. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | plane;
  1273. if (obj->tiling_mode != I915_TILING_NONE)
  1274. fbc_ctl2 |= FBC_CTL_CPU_FENCE;
  1275. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  1276. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  1277. /* enable it... */
  1278. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  1279. if (IS_I945GM(dev))
  1280. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  1281. fbc_ctl |= (dev_priv->cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  1282. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  1283. if (obj->tiling_mode != I915_TILING_NONE)
  1284. fbc_ctl |= dev_priv->cfb_fence;
  1285. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1286. DRM_DEBUG_KMS("enabled FBC, pitch %ld, yoff %d, plane %d, ",
  1287. dev_priv->cfb_pitch, crtc->y, dev_priv->cfb_plane);
  1288. }
  1289. static bool i8xx_fbc_enabled(struct drm_device *dev)
  1290. {
  1291. struct drm_i915_private *dev_priv = dev->dev_private;
  1292. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  1293. }
  1294. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1295. {
  1296. struct drm_device *dev = crtc->dev;
  1297. struct drm_i915_private *dev_priv = dev->dev_private;
  1298. struct drm_framebuffer *fb = crtc->fb;
  1299. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1300. struct drm_i915_gem_object *obj = intel_fb->obj;
  1301. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1302. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1303. unsigned long stall_watermark = 200;
  1304. u32 dpfc_ctl;
  1305. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1306. if (dpfc_ctl & DPFC_CTL_EN) {
  1307. if (dev_priv->cfb_fence == obj->fence_reg &&
  1308. dev_priv->cfb_plane == intel_crtc->plane &&
  1309. dev_priv->cfb_y == crtc->y)
  1310. return;
  1311. I915_WRITE(DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
  1312. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1313. }
  1314. dev_priv->cfb_fence = obj->fence_reg;
  1315. dev_priv->cfb_plane = intel_crtc->plane;
  1316. dev_priv->cfb_y = crtc->y;
  1317. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  1318. if (obj->tiling_mode != I915_TILING_NONE) {
  1319. dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;
  1320. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  1321. } else {
  1322. I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  1323. }
  1324. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1325. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1326. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1327. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  1328. /* enable it... */
  1329. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  1330. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1331. }
  1332. static void g4x_disable_fbc(struct drm_device *dev)
  1333. {
  1334. struct drm_i915_private *dev_priv = dev->dev_private;
  1335. u32 dpfc_ctl;
  1336. /* Disable compression */
  1337. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1338. if (dpfc_ctl & DPFC_CTL_EN) {
  1339. dpfc_ctl &= ~DPFC_CTL_EN;
  1340. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  1341. DRM_DEBUG_KMS("disabled FBC\n");
  1342. }
  1343. }
  1344. static bool g4x_fbc_enabled(struct drm_device *dev)
  1345. {
  1346. struct drm_i915_private *dev_priv = dev->dev_private;
  1347. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  1348. }
  1349. static void sandybridge_blit_fbc_update(struct drm_device *dev)
  1350. {
  1351. struct drm_i915_private *dev_priv = dev->dev_private;
  1352. u32 blt_ecoskpd;
  1353. /* Make sure blitter notifies FBC of writes */
  1354. gen6_gt_force_wake_get(dev_priv);
  1355. blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
  1356. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
  1357. GEN6_BLITTER_LOCK_SHIFT;
  1358. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1359. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
  1360. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1361. blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
  1362. GEN6_BLITTER_LOCK_SHIFT);
  1363. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1364. POSTING_READ(GEN6_BLITTER_ECOSKPD);
  1365. gen6_gt_force_wake_put(dev_priv);
  1366. }
  1367. static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1368. {
  1369. struct drm_device *dev = crtc->dev;
  1370. struct drm_i915_private *dev_priv = dev->dev_private;
  1371. struct drm_framebuffer *fb = crtc->fb;
  1372. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1373. struct drm_i915_gem_object *obj = intel_fb->obj;
  1374. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1375. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1376. unsigned long stall_watermark = 200;
  1377. u32 dpfc_ctl;
  1378. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1379. if (dpfc_ctl & DPFC_CTL_EN) {
  1380. if (dev_priv->cfb_fence == obj->fence_reg &&
  1381. dev_priv->cfb_plane == intel_crtc->plane &&
  1382. dev_priv->cfb_offset == obj->gtt_offset &&
  1383. dev_priv->cfb_y == crtc->y)
  1384. return;
  1385. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl & ~DPFC_CTL_EN);
  1386. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1387. }
  1388. dev_priv->cfb_fence = obj->fence_reg;
  1389. dev_priv->cfb_plane = intel_crtc->plane;
  1390. dev_priv->cfb_offset = obj->gtt_offset;
  1391. dev_priv->cfb_y = crtc->y;
  1392. dpfc_ctl &= DPFC_RESERVED;
  1393. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  1394. if (obj->tiling_mode != I915_TILING_NONE) {
  1395. dpfc_ctl |= (DPFC_CTL_FENCE_EN | dev_priv->cfb_fence);
  1396. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  1397. } else {
  1398. I915_WRITE(ILK_DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  1399. }
  1400. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1401. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1402. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1403. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  1404. I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
  1405. /* enable it... */
  1406. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  1407. if (IS_GEN6(dev)) {
  1408. I915_WRITE(SNB_DPFC_CTL_SA,
  1409. SNB_CPU_FENCE_ENABLE | dev_priv->cfb_fence);
  1410. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  1411. sandybridge_blit_fbc_update(dev);
  1412. }
  1413. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1414. }
  1415. static void ironlake_disable_fbc(struct drm_device *dev)
  1416. {
  1417. struct drm_i915_private *dev_priv = dev->dev_private;
  1418. u32 dpfc_ctl;
  1419. /* Disable compression */
  1420. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1421. if (dpfc_ctl & DPFC_CTL_EN) {
  1422. dpfc_ctl &= ~DPFC_CTL_EN;
  1423. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  1424. DRM_DEBUG_KMS("disabled FBC\n");
  1425. }
  1426. }
  1427. static bool ironlake_fbc_enabled(struct drm_device *dev)
  1428. {
  1429. struct drm_i915_private *dev_priv = dev->dev_private;
  1430. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  1431. }
  1432. bool intel_fbc_enabled(struct drm_device *dev)
  1433. {
  1434. struct drm_i915_private *dev_priv = dev->dev_private;
  1435. if (!dev_priv->display.fbc_enabled)
  1436. return false;
  1437. return dev_priv->display.fbc_enabled(dev);
  1438. }
  1439. static void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1440. {
  1441. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  1442. if (!dev_priv->display.enable_fbc)
  1443. return;
  1444. dev_priv->display.enable_fbc(crtc, interval);
  1445. }
  1446. void intel_disable_fbc(struct drm_device *dev)
  1447. {
  1448. struct drm_i915_private *dev_priv = dev->dev_private;
  1449. if (!dev_priv->display.disable_fbc)
  1450. return;
  1451. dev_priv->display.disable_fbc(dev);
  1452. }
  1453. /**
  1454. * intel_update_fbc - enable/disable FBC as needed
  1455. * @dev: the drm_device
  1456. *
  1457. * Set up the framebuffer compression hardware at mode set time. We
  1458. * enable it if possible:
  1459. * - plane A only (on pre-965)
  1460. * - no pixel mulitply/line duplication
  1461. * - no alpha buffer discard
  1462. * - no dual wide
  1463. * - framebuffer <= 2048 in width, 1536 in height
  1464. *
  1465. * We can't assume that any compression will take place (worst case),
  1466. * so the compressed buffer has to be the same size as the uncompressed
  1467. * one. It also must reside (along with the line length buffer) in
  1468. * stolen memory.
  1469. *
  1470. * We need to enable/disable FBC on a global basis.
  1471. */
  1472. static void intel_update_fbc(struct drm_device *dev)
  1473. {
  1474. struct drm_i915_private *dev_priv = dev->dev_private;
  1475. struct drm_crtc *crtc = NULL, *tmp_crtc;
  1476. struct intel_crtc *intel_crtc;
  1477. struct drm_framebuffer *fb;
  1478. struct intel_framebuffer *intel_fb;
  1479. struct drm_i915_gem_object *obj;
  1480. DRM_DEBUG_KMS("\n");
  1481. if (!i915_powersave)
  1482. return;
  1483. if (!I915_HAS_FBC(dev))
  1484. return;
  1485. /*
  1486. * If FBC is already on, we just have to verify that we can
  1487. * keep it that way...
  1488. * Need to disable if:
  1489. * - more than one pipe is active
  1490. * - changing FBC params (stride, fence, mode)
  1491. * - new fb is too large to fit in compressed buffer
  1492. * - going to an unsupported config (interlace, pixel multiply, etc.)
  1493. */
  1494. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  1495. if (tmp_crtc->enabled && tmp_crtc->fb) {
  1496. if (crtc) {
  1497. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  1498. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  1499. goto out_disable;
  1500. }
  1501. crtc = tmp_crtc;
  1502. }
  1503. }
  1504. if (!crtc || crtc->fb == NULL) {
  1505. DRM_DEBUG_KMS("no output, disabling\n");
  1506. dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
  1507. goto out_disable;
  1508. }
  1509. intel_crtc = to_intel_crtc(crtc);
  1510. fb = crtc->fb;
  1511. intel_fb = to_intel_framebuffer(fb);
  1512. obj = intel_fb->obj;
  1513. if (!i915_enable_fbc) {
  1514. DRM_DEBUG_KMS("fbc disabled per module param (default off)\n");
  1515. dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
  1516. goto out_disable;
  1517. }
  1518. if (intel_fb->obj->base.size > dev_priv->cfb_size) {
  1519. DRM_DEBUG_KMS("framebuffer too large, disabling "
  1520. "compression\n");
  1521. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  1522. goto out_disable;
  1523. }
  1524. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  1525. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  1526. DRM_DEBUG_KMS("mode incompatible with compression, "
  1527. "disabling\n");
  1528. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  1529. goto out_disable;
  1530. }
  1531. if ((crtc->mode.hdisplay > 2048) ||
  1532. (crtc->mode.vdisplay > 1536)) {
  1533. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  1534. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  1535. goto out_disable;
  1536. }
  1537. if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
  1538. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  1539. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  1540. goto out_disable;
  1541. }
  1542. if (obj->tiling_mode != I915_TILING_X) {
  1543. DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
  1544. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  1545. goto out_disable;
  1546. }
  1547. /* If the kernel debugger is active, always disable compression */
  1548. if (in_dbg_master())
  1549. goto out_disable;
  1550. intel_enable_fbc(crtc, 500);
  1551. return;
  1552. out_disable:
  1553. /* Multiple disables should be harmless */
  1554. if (intel_fbc_enabled(dev)) {
  1555. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  1556. intel_disable_fbc(dev);
  1557. }
  1558. }
  1559. int
  1560. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1561. struct drm_i915_gem_object *obj,
  1562. struct intel_ring_buffer *pipelined)
  1563. {
  1564. struct drm_i915_private *dev_priv = dev->dev_private;
  1565. u32 alignment;
  1566. int ret;
  1567. switch (obj->tiling_mode) {
  1568. case I915_TILING_NONE:
  1569. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1570. alignment = 128 * 1024;
  1571. else if (INTEL_INFO(dev)->gen >= 4)
  1572. alignment = 4 * 1024;
  1573. else
  1574. alignment = 64 * 1024;
  1575. break;
  1576. case I915_TILING_X:
  1577. /* pin() will align the object as required by fence */
  1578. alignment = 0;
  1579. break;
  1580. case I915_TILING_Y:
  1581. /* FIXME: Is this true? */
  1582. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1583. return -EINVAL;
  1584. default:
  1585. BUG();
  1586. }
  1587. dev_priv->mm.interruptible = false;
  1588. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1589. if (ret)
  1590. goto err_interruptible;
  1591. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1592. * fence, whereas 965+ only requires a fence if using
  1593. * framebuffer compression. For simplicity, we always install
  1594. * a fence as the cost is not that onerous.
  1595. */
  1596. if (obj->tiling_mode != I915_TILING_NONE) {
  1597. ret = i915_gem_object_get_fence(obj, pipelined);
  1598. if (ret)
  1599. goto err_unpin;
  1600. }
  1601. dev_priv->mm.interruptible = true;
  1602. return 0;
  1603. err_unpin:
  1604. i915_gem_object_unpin(obj);
  1605. err_interruptible:
  1606. dev_priv->mm.interruptible = true;
  1607. return ret;
  1608. }
  1609. static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1610. int x, int y)
  1611. {
  1612. struct drm_device *dev = crtc->dev;
  1613. struct drm_i915_private *dev_priv = dev->dev_private;
  1614. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1615. struct intel_framebuffer *intel_fb;
  1616. struct drm_i915_gem_object *obj;
  1617. int plane = intel_crtc->plane;
  1618. unsigned long Start, Offset;
  1619. u32 dspcntr;
  1620. u32 reg;
  1621. switch (plane) {
  1622. case 0:
  1623. case 1:
  1624. break;
  1625. default:
  1626. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1627. return -EINVAL;
  1628. }
  1629. intel_fb = to_intel_framebuffer(fb);
  1630. obj = intel_fb->obj;
  1631. reg = DSPCNTR(plane);
  1632. dspcntr = I915_READ(reg);
  1633. /* Mask out pixel format bits in case we change it */
  1634. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1635. switch (fb->bits_per_pixel) {
  1636. case 8:
  1637. dspcntr |= DISPPLANE_8BPP;
  1638. break;
  1639. case 16:
  1640. if (fb->depth == 15)
  1641. dspcntr |= DISPPLANE_15_16BPP;
  1642. else
  1643. dspcntr |= DISPPLANE_16BPP;
  1644. break;
  1645. case 24:
  1646. case 32:
  1647. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1648. break;
  1649. default:
  1650. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1651. return -EINVAL;
  1652. }
  1653. if (INTEL_INFO(dev)->gen >= 4) {
  1654. if (obj->tiling_mode != I915_TILING_NONE)
  1655. dspcntr |= DISPPLANE_TILED;
  1656. else
  1657. dspcntr &= ~DISPPLANE_TILED;
  1658. }
  1659. I915_WRITE(reg, dspcntr);
  1660. Start = obj->gtt_offset;
  1661. Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
  1662. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1663. Start, Offset, x, y, fb->pitch);
  1664. I915_WRITE(DSPSTRIDE(plane), fb->pitch);
  1665. if (INTEL_INFO(dev)->gen >= 4) {
  1666. I915_WRITE(DSPSURF(plane), Start);
  1667. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1668. I915_WRITE(DSPADDR(plane), Offset);
  1669. } else
  1670. I915_WRITE(DSPADDR(plane), Start + Offset);
  1671. POSTING_READ(reg);
  1672. return 0;
  1673. }
  1674. static int ironlake_update_plane(struct drm_crtc *crtc,
  1675. struct drm_framebuffer *fb, int x, int y)
  1676. {
  1677. struct drm_device *dev = crtc->dev;
  1678. struct drm_i915_private *dev_priv = dev->dev_private;
  1679. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1680. struct intel_framebuffer *intel_fb;
  1681. struct drm_i915_gem_object *obj;
  1682. int plane = intel_crtc->plane;
  1683. unsigned long Start, Offset;
  1684. u32 dspcntr;
  1685. u32 reg;
  1686. switch (plane) {
  1687. case 0:
  1688. case 1:
  1689. break;
  1690. default:
  1691. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1692. return -EINVAL;
  1693. }
  1694. intel_fb = to_intel_framebuffer(fb);
  1695. obj = intel_fb->obj;
  1696. reg = DSPCNTR(plane);
  1697. dspcntr = I915_READ(reg);
  1698. /* Mask out pixel format bits in case we change it */
  1699. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1700. switch (fb->bits_per_pixel) {
  1701. case 8:
  1702. dspcntr |= DISPPLANE_8BPP;
  1703. break;
  1704. case 16:
  1705. if (fb->depth != 16)
  1706. return -EINVAL;
  1707. dspcntr |= DISPPLANE_16BPP;
  1708. break;
  1709. case 24:
  1710. case 32:
  1711. if (fb->depth == 24)
  1712. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1713. else if (fb->depth == 30)
  1714. dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
  1715. else
  1716. return -EINVAL;
  1717. break;
  1718. default:
  1719. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1720. return -EINVAL;
  1721. }
  1722. if (obj->tiling_mode != I915_TILING_NONE)
  1723. dspcntr |= DISPPLANE_TILED;
  1724. else
  1725. dspcntr &= ~DISPPLANE_TILED;
  1726. /* must disable */
  1727. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1728. I915_WRITE(reg, dspcntr);
  1729. Start = obj->gtt_offset;
  1730. Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
  1731. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1732. Start, Offset, x, y, fb->pitch);
  1733. I915_WRITE(DSPSTRIDE(plane), fb->pitch);
  1734. I915_WRITE(DSPSURF(plane), Start);
  1735. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1736. I915_WRITE(DSPADDR(plane), Offset);
  1737. POSTING_READ(reg);
  1738. return 0;
  1739. }
  1740. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1741. static int
  1742. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1743. int x, int y, enum mode_set_atomic state)
  1744. {
  1745. struct drm_device *dev = crtc->dev;
  1746. struct drm_i915_private *dev_priv = dev->dev_private;
  1747. int ret;
  1748. ret = dev_priv->display.update_plane(crtc, fb, x, y);
  1749. if (ret)
  1750. return ret;
  1751. intel_update_fbc(dev);
  1752. intel_increase_pllclock(crtc);
  1753. return 0;
  1754. }
  1755. static int
  1756. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1757. struct drm_framebuffer *old_fb)
  1758. {
  1759. struct drm_device *dev = crtc->dev;
  1760. struct drm_i915_master_private *master_priv;
  1761. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1762. int ret;
  1763. /* no fb bound */
  1764. if (!crtc->fb) {
  1765. DRM_DEBUG_KMS("No FB bound\n");
  1766. return 0;
  1767. }
  1768. switch (intel_crtc->plane) {
  1769. case 0:
  1770. case 1:
  1771. break;
  1772. default:
  1773. return -EINVAL;
  1774. }
  1775. mutex_lock(&dev->struct_mutex);
  1776. ret = intel_pin_and_fence_fb_obj(dev,
  1777. to_intel_framebuffer(crtc->fb)->obj,
  1778. NULL);
  1779. if (ret != 0) {
  1780. mutex_unlock(&dev->struct_mutex);
  1781. return ret;
  1782. }
  1783. if (old_fb) {
  1784. struct drm_i915_private *dev_priv = dev->dev_private;
  1785. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1786. wait_event(dev_priv->pending_flip_queue,
  1787. atomic_read(&dev_priv->mm.wedged) ||
  1788. atomic_read(&obj->pending_flip) == 0);
  1789. /* Big Hammer, we also need to ensure that any pending
  1790. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1791. * current scanout is retired before unpinning the old
  1792. * framebuffer.
  1793. *
  1794. * This should only fail upon a hung GPU, in which case we
  1795. * can safely continue.
  1796. */
  1797. ret = i915_gem_object_finish_gpu(obj);
  1798. (void) ret;
  1799. }
  1800. ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
  1801. LEAVE_ATOMIC_MODE_SET);
  1802. if (ret) {
  1803. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  1804. mutex_unlock(&dev->struct_mutex);
  1805. return ret;
  1806. }
  1807. if (old_fb) {
  1808. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1809. i915_gem_object_unpin(to_intel_framebuffer(old_fb)->obj);
  1810. }
  1811. mutex_unlock(&dev->struct_mutex);
  1812. if (!dev->primary->master)
  1813. return 0;
  1814. master_priv = dev->primary->master->driver_priv;
  1815. if (!master_priv->sarea_priv)
  1816. return 0;
  1817. if (intel_crtc->pipe) {
  1818. master_priv->sarea_priv->pipeB_x = x;
  1819. master_priv->sarea_priv->pipeB_y = y;
  1820. } else {
  1821. master_priv->sarea_priv->pipeA_x = x;
  1822. master_priv->sarea_priv->pipeA_y = y;
  1823. }
  1824. return 0;
  1825. }
  1826. static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
  1827. {
  1828. struct drm_device *dev = crtc->dev;
  1829. struct drm_i915_private *dev_priv = dev->dev_private;
  1830. u32 dpa_ctl;
  1831. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1832. dpa_ctl = I915_READ(DP_A);
  1833. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1834. if (clock < 200000) {
  1835. u32 temp;
  1836. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1837. /* workaround for 160Mhz:
  1838. 1) program 0x4600c bits 15:0 = 0x8124
  1839. 2) program 0x46010 bit 0 = 1
  1840. 3) program 0x46034 bit 24 = 1
  1841. 4) program 0x64000 bit 14 = 1
  1842. */
  1843. temp = I915_READ(0x4600c);
  1844. temp &= 0xffff0000;
  1845. I915_WRITE(0x4600c, temp | 0x8124);
  1846. temp = I915_READ(0x46010);
  1847. I915_WRITE(0x46010, temp | 1);
  1848. temp = I915_READ(0x46034);
  1849. I915_WRITE(0x46034, temp | (1 << 24));
  1850. } else {
  1851. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1852. }
  1853. I915_WRITE(DP_A, dpa_ctl);
  1854. POSTING_READ(DP_A);
  1855. udelay(500);
  1856. }
  1857. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  1858. {
  1859. struct drm_device *dev = crtc->dev;
  1860. struct drm_i915_private *dev_priv = dev->dev_private;
  1861. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1862. int pipe = intel_crtc->pipe;
  1863. u32 reg, temp;
  1864. /* enable normal train */
  1865. reg = FDI_TX_CTL(pipe);
  1866. temp = I915_READ(reg);
  1867. if (IS_IVYBRIDGE(dev)) {
  1868. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  1869. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  1870. } else {
  1871. temp &= ~FDI_LINK_TRAIN_NONE;
  1872. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  1873. }
  1874. I915_WRITE(reg, temp);
  1875. reg = FDI_RX_CTL(pipe);
  1876. temp = I915_READ(reg);
  1877. if (HAS_PCH_CPT(dev)) {
  1878. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1879. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  1880. } else {
  1881. temp &= ~FDI_LINK_TRAIN_NONE;
  1882. temp |= FDI_LINK_TRAIN_NONE;
  1883. }
  1884. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  1885. /* wait one idle pattern time */
  1886. POSTING_READ(reg);
  1887. udelay(1000);
  1888. /* IVB wants error correction enabled */
  1889. if (IS_IVYBRIDGE(dev))
  1890. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  1891. FDI_FE_ERRC_ENABLE);
  1892. }
  1893. /* The FDI link training functions for ILK/Ibexpeak. */
  1894. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  1895. {
  1896. struct drm_device *dev = crtc->dev;
  1897. struct drm_i915_private *dev_priv = dev->dev_private;
  1898. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1899. int pipe = intel_crtc->pipe;
  1900. int plane = intel_crtc->plane;
  1901. u32 reg, temp, tries;
  1902. /* FDI needs bits from pipe & plane first */
  1903. assert_pipe_enabled(dev_priv, pipe);
  1904. assert_plane_enabled(dev_priv, plane);
  1905. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1906. for train result */
  1907. reg = FDI_RX_IMR(pipe);
  1908. temp = I915_READ(reg);
  1909. temp &= ~FDI_RX_SYMBOL_LOCK;
  1910. temp &= ~FDI_RX_BIT_LOCK;
  1911. I915_WRITE(reg, temp);
  1912. I915_READ(reg);
  1913. udelay(150);
  1914. /* enable CPU FDI TX and PCH FDI RX */
  1915. reg = FDI_TX_CTL(pipe);
  1916. temp = I915_READ(reg);
  1917. temp &= ~(7 << 19);
  1918. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1919. temp &= ~FDI_LINK_TRAIN_NONE;
  1920. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1921. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  1922. reg = FDI_RX_CTL(pipe);
  1923. temp = I915_READ(reg);
  1924. temp &= ~FDI_LINK_TRAIN_NONE;
  1925. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1926. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  1927. POSTING_READ(reg);
  1928. udelay(150);
  1929. /* Ironlake workaround, enable clock pointer after FDI enable*/
  1930. if (HAS_PCH_IBX(dev)) {
  1931. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  1932. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  1933. FDI_RX_PHASE_SYNC_POINTER_EN);
  1934. }
  1935. reg = FDI_RX_IIR(pipe);
  1936. for (tries = 0; tries < 5; tries++) {
  1937. temp = I915_READ(reg);
  1938. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1939. if ((temp & FDI_RX_BIT_LOCK)) {
  1940. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1941. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  1942. break;
  1943. }
  1944. }
  1945. if (tries == 5)
  1946. DRM_ERROR("FDI train 1 fail!\n");
  1947. /* Train 2 */
  1948. reg = FDI_TX_CTL(pipe);
  1949. temp = I915_READ(reg);
  1950. temp &= ~FDI_LINK_TRAIN_NONE;
  1951. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1952. I915_WRITE(reg, temp);
  1953. reg = FDI_RX_CTL(pipe);
  1954. temp = I915_READ(reg);
  1955. temp &= ~FDI_LINK_TRAIN_NONE;
  1956. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1957. I915_WRITE(reg, temp);
  1958. POSTING_READ(reg);
  1959. udelay(150);
  1960. reg = FDI_RX_IIR(pipe);
  1961. for (tries = 0; tries < 5; tries++) {
  1962. temp = I915_READ(reg);
  1963. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1964. if (temp & FDI_RX_SYMBOL_LOCK) {
  1965. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  1966. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1967. break;
  1968. }
  1969. }
  1970. if (tries == 5)
  1971. DRM_ERROR("FDI train 2 fail!\n");
  1972. DRM_DEBUG_KMS("FDI train done\n");
  1973. }
  1974. static const int snb_b_fdi_train_param [] = {
  1975. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  1976. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  1977. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  1978. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  1979. };
  1980. /* The FDI link training functions for SNB/Cougarpoint. */
  1981. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  1982. {
  1983. struct drm_device *dev = crtc->dev;
  1984. struct drm_i915_private *dev_priv = dev->dev_private;
  1985. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1986. int pipe = intel_crtc->pipe;
  1987. u32 reg, temp, i;
  1988. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1989. for train result */
  1990. reg = FDI_RX_IMR(pipe);
  1991. temp = I915_READ(reg);
  1992. temp &= ~FDI_RX_SYMBOL_LOCK;
  1993. temp &= ~FDI_RX_BIT_LOCK;
  1994. I915_WRITE(reg, temp);
  1995. POSTING_READ(reg);
  1996. udelay(150);
  1997. /* enable CPU FDI TX and PCH FDI RX */
  1998. reg = FDI_TX_CTL(pipe);
  1999. temp = I915_READ(reg);
  2000. temp &= ~(7 << 19);
  2001. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2002. temp &= ~FDI_LINK_TRAIN_NONE;
  2003. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2004. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2005. /* SNB-B */
  2006. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2007. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2008. reg = FDI_RX_CTL(pipe);
  2009. temp = I915_READ(reg);
  2010. if (HAS_PCH_CPT(dev)) {
  2011. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2012. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2013. } else {
  2014. temp &= ~FDI_LINK_TRAIN_NONE;
  2015. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2016. }
  2017. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2018. POSTING_READ(reg);
  2019. udelay(150);
  2020. for (i = 0; i < 4; i++ ) {
  2021. reg = FDI_TX_CTL(pipe);
  2022. temp = I915_READ(reg);
  2023. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2024. temp |= snb_b_fdi_train_param[i];
  2025. I915_WRITE(reg, temp);
  2026. POSTING_READ(reg);
  2027. udelay(500);
  2028. reg = FDI_RX_IIR(pipe);
  2029. temp = I915_READ(reg);
  2030. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2031. if (temp & FDI_RX_BIT_LOCK) {
  2032. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2033. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2034. break;
  2035. }
  2036. }
  2037. if (i == 4)
  2038. DRM_ERROR("FDI train 1 fail!\n");
  2039. /* Train 2 */
  2040. reg = FDI_TX_CTL(pipe);
  2041. temp = I915_READ(reg);
  2042. temp &= ~FDI_LINK_TRAIN_NONE;
  2043. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2044. if (IS_GEN6(dev)) {
  2045. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2046. /* SNB-B */
  2047. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2048. }
  2049. I915_WRITE(reg, temp);
  2050. reg = FDI_RX_CTL(pipe);
  2051. temp = I915_READ(reg);
  2052. if (HAS_PCH_CPT(dev)) {
  2053. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2054. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2055. } else {
  2056. temp &= ~FDI_LINK_TRAIN_NONE;
  2057. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2058. }
  2059. I915_WRITE(reg, temp);
  2060. POSTING_READ(reg);
  2061. udelay(150);
  2062. for (i = 0; i < 4; i++ ) {
  2063. reg = FDI_TX_CTL(pipe);
  2064. temp = I915_READ(reg);
  2065. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2066. temp |= snb_b_fdi_train_param[i];
  2067. I915_WRITE(reg, temp);
  2068. POSTING_READ(reg);
  2069. udelay(500);
  2070. reg = FDI_RX_IIR(pipe);
  2071. temp = I915_READ(reg);
  2072. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2073. if (temp & FDI_RX_SYMBOL_LOCK) {
  2074. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2075. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2076. break;
  2077. }
  2078. }
  2079. if (i == 4)
  2080. DRM_ERROR("FDI train 2 fail!\n");
  2081. DRM_DEBUG_KMS("FDI train done.\n");
  2082. }
  2083. /* Manual link training for Ivy Bridge A0 parts */
  2084. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2085. {
  2086. struct drm_device *dev = crtc->dev;
  2087. struct drm_i915_private *dev_priv = dev->dev_private;
  2088. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2089. int pipe = intel_crtc->pipe;
  2090. u32 reg, temp, i;
  2091. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2092. for train result */
  2093. reg = FDI_RX_IMR(pipe);
  2094. temp = I915_READ(reg);
  2095. temp &= ~FDI_RX_SYMBOL_LOCK;
  2096. temp &= ~FDI_RX_BIT_LOCK;
  2097. I915_WRITE(reg, temp);
  2098. POSTING_READ(reg);
  2099. udelay(150);
  2100. /* enable CPU FDI TX and PCH FDI RX */
  2101. reg = FDI_TX_CTL(pipe);
  2102. temp = I915_READ(reg);
  2103. temp &= ~(7 << 19);
  2104. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2105. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2106. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2107. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2108. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2109. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2110. reg = FDI_RX_CTL(pipe);
  2111. temp = I915_READ(reg);
  2112. temp &= ~FDI_LINK_TRAIN_AUTO;
  2113. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2114. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2115. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2116. POSTING_READ(reg);
  2117. udelay(150);
  2118. for (i = 0; i < 4; i++ ) {
  2119. reg = FDI_TX_CTL(pipe);
  2120. temp = I915_READ(reg);
  2121. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2122. temp |= snb_b_fdi_train_param[i];
  2123. I915_WRITE(reg, temp);
  2124. POSTING_READ(reg);
  2125. udelay(500);
  2126. reg = FDI_RX_IIR(pipe);
  2127. temp = I915_READ(reg);
  2128. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2129. if (temp & FDI_RX_BIT_LOCK ||
  2130. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2131. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2132. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2133. break;
  2134. }
  2135. }
  2136. if (i == 4)
  2137. DRM_ERROR("FDI train 1 fail!\n");
  2138. /* Train 2 */
  2139. reg = FDI_TX_CTL(pipe);
  2140. temp = I915_READ(reg);
  2141. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2142. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2143. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2144. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2145. I915_WRITE(reg, temp);
  2146. reg = FDI_RX_CTL(pipe);
  2147. temp = I915_READ(reg);
  2148. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2149. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2150. I915_WRITE(reg, temp);
  2151. POSTING_READ(reg);
  2152. udelay(150);
  2153. for (i = 0; i < 4; i++ ) {
  2154. reg = FDI_TX_CTL(pipe);
  2155. temp = I915_READ(reg);
  2156. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2157. temp |= snb_b_fdi_train_param[i];
  2158. I915_WRITE(reg, temp);
  2159. POSTING_READ(reg);
  2160. udelay(500);
  2161. reg = FDI_RX_IIR(pipe);
  2162. temp = I915_READ(reg);
  2163. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2164. if (temp & FDI_RX_SYMBOL_LOCK) {
  2165. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2166. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2167. break;
  2168. }
  2169. }
  2170. if (i == 4)
  2171. DRM_ERROR("FDI train 2 fail!\n");
  2172. DRM_DEBUG_KMS("FDI train done.\n");
  2173. }
  2174. static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
  2175. {
  2176. struct drm_device *dev = crtc->dev;
  2177. struct drm_i915_private *dev_priv = dev->dev_private;
  2178. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2179. int pipe = intel_crtc->pipe;
  2180. u32 reg, temp;
  2181. /* Write the TU size bits so error detection works */
  2182. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2183. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2184. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2185. reg = FDI_RX_CTL(pipe);
  2186. temp = I915_READ(reg);
  2187. temp &= ~((0x7 << 19) | (0x7 << 16));
  2188. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2189. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2190. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2191. POSTING_READ(reg);
  2192. udelay(200);
  2193. /* Switch from Rawclk to PCDclk */
  2194. temp = I915_READ(reg);
  2195. I915_WRITE(reg, temp | FDI_PCDCLK);
  2196. POSTING_READ(reg);
  2197. udelay(200);
  2198. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2199. reg = FDI_TX_CTL(pipe);
  2200. temp = I915_READ(reg);
  2201. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2202. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2203. POSTING_READ(reg);
  2204. udelay(100);
  2205. }
  2206. }
  2207. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2208. {
  2209. struct drm_device *dev = crtc->dev;
  2210. struct drm_i915_private *dev_priv = dev->dev_private;
  2211. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2212. int pipe = intel_crtc->pipe;
  2213. u32 reg, temp;
  2214. /* disable CPU FDI tx and PCH FDI rx */
  2215. reg = FDI_TX_CTL(pipe);
  2216. temp = I915_READ(reg);
  2217. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2218. POSTING_READ(reg);
  2219. reg = FDI_RX_CTL(pipe);
  2220. temp = I915_READ(reg);
  2221. temp &= ~(0x7 << 16);
  2222. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2223. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2224. POSTING_READ(reg);
  2225. udelay(100);
  2226. /* Ironlake workaround, disable clock pointer after downing FDI */
  2227. if (HAS_PCH_IBX(dev)) {
  2228. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2229. I915_WRITE(FDI_RX_CHICKEN(pipe),
  2230. I915_READ(FDI_RX_CHICKEN(pipe) &
  2231. ~FDI_RX_PHASE_SYNC_POINTER_EN));
  2232. }
  2233. /* still set train pattern 1 */
  2234. reg = FDI_TX_CTL(pipe);
  2235. temp = I915_READ(reg);
  2236. temp &= ~FDI_LINK_TRAIN_NONE;
  2237. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2238. I915_WRITE(reg, temp);
  2239. reg = FDI_RX_CTL(pipe);
  2240. temp = I915_READ(reg);
  2241. if (HAS_PCH_CPT(dev)) {
  2242. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2243. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2244. } else {
  2245. temp &= ~FDI_LINK_TRAIN_NONE;
  2246. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2247. }
  2248. /* BPC in FDI rx is consistent with that in PIPECONF */
  2249. temp &= ~(0x07 << 16);
  2250. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2251. I915_WRITE(reg, temp);
  2252. POSTING_READ(reg);
  2253. udelay(100);
  2254. }
  2255. /*
  2256. * When we disable a pipe, we need to clear any pending scanline wait events
  2257. * to avoid hanging the ring, which we assume we are waiting on.
  2258. */
  2259. static void intel_clear_scanline_wait(struct drm_device *dev)
  2260. {
  2261. struct drm_i915_private *dev_priv = dev->dev_private;
  2262. struct intel_ring_buffer *ring;
  2263. u32 tmp;
  2264. if (IS_GEN2(dev))
  2265. /* Can't break the hang on i8xx */
  2266. return;
  2267. ring = LP_RING(dev_priv);
  2268. tmp = I915_READ_CTL(ring);
  2269. if (tmp & RING_WAIT)
  2270. I915_WRITE_CTL(ring, tmp);
  2271. }
  2272. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2273. {
  2274. struct drm_i915_gem_object *obj;
  2275. struct drm_i915_private *dev_priv;
  2276. if (crtc->fb == NULL)
  2277. return;
  2278. obj = to_intel_framebuffer(crtc->fb)->obj;
  2279. dev_priv = crtc->dev->dev_private;
  2280. wait_event(dev_priv->pending_flip_queue,
  2281. atomic_read(&obj->pending_flip) == 0);
  2282. }
  2283. static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
  2284. {
  2285. struct drm_device *dev = crtc->dev;
  2286. struct drm_mode_config *mode_config = &dev->mode_config;
  2287. struct intel_encoder *encoder;
  2288. /*
  2289. * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
  2290. * must be driven by its own crtc; no sharing is possible.
  2291. */
  2292. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  2293. if (encoder->base.crtc != crtc)
  2294. continue;
  2295. switch (encoder->type) {
  2296. case INTEL_OUTPUT_EDP:
  2297. if (!intel_encoder_is_pch_edp(&encoder->base))
  2298. return false;
  2299. continue;
  2300. }
  2301. }
  2302. return true;
  2303. }
  2304. /*
  2305. * Enable PCH resources required for PCH ports:
  2306. * - PCH PLLs
  2307. * - FDI training & RX/TX
  2308. * - update transcoder timings
  2309. * - DP transcoding bits
  2310. * - transcoder
  2311. */
  2312. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2313. {
  2314. struct drm_device *dev = crtc->dev;
  2315. struct drm_i915_private *dev_priv = dev->dev_private;
  2316. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2317. int pipe = intel_crtc->pipe;
  2318. u32 reg, temp;
  2319. /* For PCH output, training FDI link */
  2320. dev_priv->display.fdi_link_train(crtc);
  2321. intel_enable_pch_pll(dev_priv, pipe);
  2322. if (HAS_PCH_CPT(dev)) {
  2323. /* Be sure PCH DPLL SEL is set */
  2324. temp = I915_READ(PCH_DPLL_SEL);
  2325. if (pipe == 0 && (temp & TRANSA_DPLL_ENABLE) == 0)
  2326. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  2327. else if (pipe == 1 && (temp & TRANSB_DPLL_ENABLE) == 0)
  2328. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2329. I915_WRITE(PCH_DPLL_SEL, temp);
  2330. }
  2331. /* set transcoder timing, panel must allow it */
  2332. assert_panel_unlocked(dev_priv, pipe);
  2333. I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
  2334. I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
  2335. I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
  2336. I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
  2337. I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
  2338. I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
  2339. intel_fdi_normal_train(crtc);
  2340. /* For PCH DP, enable TRANS_DP_CTL */
  2341. if (HAS_PCH_CPT(dev) &&
  2342. intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  2343. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
  2344. reg = TRANS_DP_CTL(pipe);
  2345. temp = I915_READ(reg);
  2346. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  2347. TRANS_DP_SYNC_MASK |
  2348. TRANS_DP_BPC_MASK);
  2349. temp |= (TRANS_DP_OUTPUT_ENABLE |
  2350. TRANS_DP_ENH_FRAMING);
  2351. temp |= bpc << 9; /* same format but at 11:9 */
  2352. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  2353. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  2354. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  2355. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  2356. switch (intel_trans_dp_port_sel(crtc)) {
  2357. case PCH_DP_B:
  2358. temp |= TRANS_DP_PORT_SEL_B;
  2359. break;
  2360. case PCH_DP_C:
  2361. temp |= TRANS_DP_PORT_SEL_C;
  2362. break;
  2363. case PCH_DP_D:
  2364. temp |= TRANS_DP_PORT_SEL_D;
  2365. break;
  2366. default:
  2367. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  2368. temp |= TRANS_DP_PORT_SEL_B;
  2369. break;
  2370. }
  2371. I915_WRITE(reg, temp);
  2372. }
  2373. intel_enable_transcoder(dev_priv, pipe);
  2374. }
  2375. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  2376. {
  2377. struct drm_device *dev = crtc->dev;
  2378. struct drm_i915_private *dev_priv = dev->dev_private;
  2379. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2380. int pipe = intel_crtc->pipe;
  2381. int plane = intel_crtc->plane;
  2382. u32 temp;
  2383. bool is_pch_port;
  2384. if (intel_crtc->active)
  2385. return;
  2386. intel_crtc->active = true;
  2387. intel_update_watermarks(dev);
  2388. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  2389. temp = I915_READ(PCH_LVDS);
  2390. if ((temp & LVDS_PORT_EN) == 0)
  2391. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  2392. }
  2393. is_pch_port = intel_crtc_driving_pch(crtc);
  2394. if (is_pch_port)
  2395. ironlake_fdi_pll_enable(crtc);
  2396. else
  2397. ironlake_fdi_disable(crtc);
  2398. /* Enable panel fitting for LVDS */
  2399. if (dev_priv->pch_pf_size &&
  2400. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
  2401. /* Force use of hard-coded filter coefficients
  2402. * as some pre-programmed values are broken,
  2403. * e.g. x201.
  2404. */
  2405. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  2406. I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
  2407. I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
  2408. }
  2409. intel_enable_pipe(dev_priv, pipe, is_pch_port);
  2410. intel_enable_plane(dev_priv, plane, pipe);
  2411. if (is_pch_port)
  2412. ironlake_pch_enable(crtc);
  2413. intel_crtc_load_lut(crtc);
  2414. mutex_lock(&dev->struct_mutex);
  2415. intel_update_fbc(dev);
  2416. mutex_unlock(&dev->struct_mutex);
  2417. intel_crtc_update_cursor(crtc, true);
  2418. }
  2419. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  2420. {
  2421. struct drm_device *dev = crtc->dev;
  2422. struct drm_i915_private *dev_priv = dev->dev_private;
  2423. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2424. int pipe = intel_crtc->pipe;
  2425. int plane = intel_crtc->plane;
  2426. u32 reg, temp;
  2427. if (!intel_crtc->active)
  2428. return;
  2429. intel_crtc_wait_for_pending_flips(crtc);
  2430. drm_vblank_off(dev, pipe);
  2431. intel_crtc_update_cursor(crtc, false);
  2432. intel_disable_plane(dev_priv, plane, pipe);
  2433. if (dev_priv->cfb_plane == plane)
  2434. intel_disable_fbc(dev);
  2435. intel_disable_pipe(dev_priv, pipe);
  2436. /* Disable PF */
  2437. I915_WRITE(PF_CTL(pipe), 0);
  2438. I915_WRITE(PF_WIN_SZ(pipe), 0);
  2439. ironlake_fdi_disable(crtc);
  2440. /* This is a horrible layering violation; we should be doing this in
  2441. * the connector/encoder ->prepare instead, but we don't always have
  2442. * enough information there about the config to know whether it will
  2443. * actually be necessary or just cause undesired flicker.
  2444. */
  2445. intel_disable_pch_ports(dev_priv, pipe);
  2446. intel_disable_transcoder(dev_priv, pipe);
  2447. if (HAS_PCH_CPT(dev)) {
  2448. /* disable TRANS_DP_CTL */
  2449. reg = TRANS_DP_CTL(pipe);
  2450. temp = I915_READ(reg);
  2451. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  2452. temp |= TRANS_DP_PORT_SEL_NONE;
  2453. I915_WRITE(reg, temp);
  2454. /* disable DPLL_SEL */
  2455. temp = I915_READ(PCH_DPLL_SEL);
  2456. switch (pipe) {
  2457. case 0:
  2458. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  2459. break;
  2460. case 1:
  2461. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2462. break;
  2463. case 2:
  2464. /* FIXME: manage transcoder PLLs? */
  2465. temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
  2466. break;
  2467. default:
  2468. BUG(); /* wtf */
  2469. }
  2470. I915_WRITE(PCH_DPLL_SEL, temp);
  2471. }
  2472. /* disable PCH DPLL */
  2473. intel_disable_pch_pll(dev_priv, pipe);
  2474. /* Switch from PCDclk to Rawclk */
  2475. reg = FDI_RX_CTL(pipe);
  2476. temp = I915_READ(reg);
  2477. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2478. /* Disable CPU FDI TX PLL */
  2479. reg = FDI_TX_CTL(pipe);
  2480. temp = I915_READ(reg);
  2481. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2482. POSTING_READ(reg);
  2483. udelay(100);
  2484. reg = FDI_RX_CTL(pipe);
  2485. temp = I915_READ(reg);
  2486. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2487. /* Wait for the clocks to turn off. */
  2488. POSTING_READ(reg);
  2489. udelay(100);
  2490. intel_crtc->active = false;
  2491. intel_update_watermarks(dev);
  2492. mutex_lock(&dev->struct_mutex);
  2493. intel_update_fbc(dev);
  2494. intel_clear_scanline_wait(dev);
  2495. mutex_unlock(&dev->struct_mutex);
  2496. }
  2497. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  2498. {
  2499. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2500. int pipe = intel_crtc->pipe;
  2501. int plane = intel_crtc->plane;
  2502. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2503. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2504. */
  2505. switch (mode) {
  2506. case DRM_MODE_DPMS_ON:
  2507. case DRM_MODE_DPMS_STANDBY:
  2508. case DRM_MODE_DPMS_SUSPEND:
  2509. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  2510. ironlake_crtc_enable(crtc);
  2511. break;
  2512. case DRM_MODE_DPMS_OFF:
  2513. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  2514. ironlake_crtc_disable(crtc);
  2515. break;
  2516. }
  2517. }
  2518. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2519. {
  2520. if (!enable && intel_crtc->overlay) {
  2521. struct drm_device *dev = intel_crtc->base.dev;
  2522. struct drm_i915_private *dev_priv = dev->dev_private;
  2523. mutex_lock(&dev->struct_mutex);
  2524. dev_priv->mm.interruptible = false;
  2525. (void) intel_overlay_switch_off(intel_crtc->overlay);
  2526. dev_priv->mm.interruptible = true;
  2527. mutex_unlock(&dev->struct_mutex);
  2528. }
  2529. /* Let userspace switch the overlay on again. In most cases userspace
  2530. * has to recompute where to put it anyway.
  2531. */
  2532. }
  2533. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  2534. {
  2535. struct drm_device *dev = crtc->dev;
  2536. struct drm_i915_private *dev_priv = dev->dev_private;
  2537. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2538. int pipe = intel_crtc->pipe;
  2539. int plane = intel_crtc->plane;
  2540. if (intel_crtc->active)
  2541. return;
  2542. intel_crtc->active = true;
  2543. intel_update_watermarks(dev);
  2544. intel_enable_pll(dev_priv, pipe);
  2545. intel_enable_pipe(dev_priv, pipe, false);
  2546. intel_enable_plane(dev_priv, plane, pipe);
  2547. intel_crtc_load_lut(crtc);
  2548. intel_update_fbc(dev);
  2549. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2550. intel_crtc_dpms_overlay(intel_crtc, true);
  2551. intel_crtc_update_cursor(crtc, true);
  2552. }
  2553. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  2554. {
  2555. struct drm_device *dev = crtc->dev;
  2556. struct drm_i915_private *dev_priv = dev->dev_private;
  2557. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2558. int pipe = intel_crtc->pipe;
  2559. int plane = intel_crtc->plane;
  2560. if (!intel_crtc->active)
  2561. return;
  2562. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2563. intel_crtc_wait_for_pending_flips(crtc);
  2564. drm_vblank_off(dev, pipe);
  2565. intel_crtc_dpms_overlay(intel_crtc, false);
  2566. intel_crtc_update_cursor(crtc, false);
  2567. if (dev_priv->cfb_plane == plane)
  2568. intel_disable_fbc(dev);
  2569. intel_disable_plane(dev_priv, plane, pipe);
  2570. intel_disable_pipe(dev_priv, pipe);
  2571. intel_disable_pll(dev_priv, pipe);
  2572. intel_crtc->active = false;
  2573. intel_update_fbc(dev);
  2574. intel_update_watermarks(dev);
  2575. intel_clear_scanline_wait(dev);
  2576. }
  2577. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2578. {
  2579. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2580. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2581. */
  2582. switch (mode) {
  2583. case DRM_MODE_DPMS_ON:
  2584. case DRM_MODE_DPMS_STANDBY:
  2585. case DRM_MODE_DPMS_SUSPEND:
  2586. i9xx_crtc_enable(crtc);
  2587. break;
  2588. case DRM_MODE_DPMS_OFF:
  2589. i9xx_crtc_disable(crtc);
  2590. break;
  2591. }
  2592. }
  2593. /**
  2594. * Sets the power management mode of the pipe and plane.
  2595. */
  2596. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2597. {
  2598. struct drm_device *dev = crtc->dev;
  2599. struct drm_i915_private *dev_priv = dev->dev_private;
  2600. struct drm_i915_master_private *master_priv;
  2601. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2602. int pipe = intel_crtc->pipe;
  2603. bool enabled;
  2604. if (intel_crtc->dpms_mode == mode)
  2605. return;
  2606. intel_crtc->dpms_mode = mode;
  2607. dev_priv->display.dpms(crtc, mode);
  2608. if (!dev->primary->master)
  2609. return;
  2610. master_priv = dev->primary->master->driver_priv;
  2611. if (!master_priv->sarea_priv)
  2612. return;
  2613. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2614. switch (pipe) {
  2615. case 0:
  2616. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2617. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2618. break;
  2619. case 1:
  2620. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2621. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2622. break;
  2623. default:
  2624. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  2625. break;
  2626. }
  2627. }
  2628. static void intel_crtc_disable(struct drm_crtc *crtc)
  2629. {
  2630. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2631. struct drm_device *dev = crtc->dev;
  2632. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  2633. if (crtc->fb) {
  2634. mutex_lock(&dev->struct_mutex);
  2635. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  2636. mutex_unlock(&dev->struct_mutex);
  2637. }
  2638. }
  2639. /* Prepare for a mode set.
  2640. *
  2641. * Note we could be a lot smarter here. We need to figure out which outputs
  2642. * will be enabled, which disabled (in short, how the config will changes)
  2643. * and perform the minimum necessary steps to accomplish that, e.g. updating
  2644. * watermarks, FBC configuration, making sure PLLs are programmed correctly,
  2645. * panel fitting is in the proper state, etc.
  2646. */
  2647. static void i9xx_crtc_prepare(struct drm_crtc *crtc)
  2648. {
  2649. i9xx_crtc_disable(crtc);
  2650. }
  2651. static void i9xx_crtc_commit(struct drm_crtc *crtc)
  2652. {
  2653. i9xx_crtc_enable(crtc);
  2654. }
  2655. static void ironlake_crtc_prepare(struct drm_crtc *crtc)
  2656. {
  2657. ironlake_crtc_disable(crtc);
  2658. }
  2659. static void ironlake_crtc_commit(struct drm_crtc *crtc)
  2660. {
  2661. ironlake_crtc_enable(crtc);
  2662. }
  2663. void intel_encoder_prepare (struct drm_encoder *encoder)
  2664. {
  2665. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2666. /* lvds has its own version of prepare see intel_lvds_prepare */
  2667. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2668. }
  2669. void intel_encoder_commit (struct drm_encoder *encoder)
  2670. {
  2671. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2672. /* lvds has its own version of commit see intel_lvds_commit */
  2673. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2674. }
  2675. void intel_encoder_destroy(struct drm_encoder *encoder)
  2676. {
  2677. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2678. drm_encoder_cleanup(encoder);
  2679. kfree(intel_encoder);
  2680. }
  2681. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2682. struct drm_display_mode *mode,
  2683. struct drm_display_mode *adjusted_mode)
  2684. {
  2685. struct drm_device *dev = crtc->dev;
  2686. if (HAS_PCH_SPLIT(dev)) {
  2687. /* FDI link clock is fixed at 2.7G */
  2688. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  2689. return false;
  2690. }
  2691. /* XXX some encoders set the crtcinfo, others don't.
  2692. * Obviously we need some form of conflict resolution here...
  2693. */
  2694. if (adjusted_mode->crtc_htotal == 0)
  2695. drm_mode_set_crtcinfo(adjusted_mode, 0);
  2696. return true;
  2697. }
  2698. static int i945_get_display_clock_speed(struct drm_device *dev)
  2699. {
  2700. return 400000;
  2701. }
  2702. static int i915_get_display_clock_speed(struct drm_device *dev)
  2703. {
  2704. return 333000;
  2705. }
  2706. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2707. {
  2708. return 200000;
  2709. }
  2710. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2711. {
  2712. u16 gcfgc = 0;
  2713. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2714. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2715. return 133000;
  2716. else {
  2717. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2718. case GC_DISPLAY_CLOCK_333_MHZ:
  2719. return 333000;
  2720. default:
  2721. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2722. return 190000;
  2723. }
  2724. }
  2725. }
  2726. static int i865_get_display_clock_speed(struct drm_device *dev)
  2727. {
  2728. return 266000;
  2729. }
  2730. static int i855_get_display_clock_speed(struct drm_device *dev)
  2731. {
  2732. u16 hpllcc = 0;
  2733. /* Assume that the hardware is in the high speed state. This
  2734. * should be the default.
  2735. */
  2736. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2737. case GC_CLOCK_133_200:
  2738. case GC_CLOCK_100_200:
  2739. return 200000;
  2740. case GC_CLOCK_166_250:
  2741. return 250000;
  2742. case GC_CLOCK_100_133:
  2743. return 133000;
  2744. }
  2745. /* Shouldn't happen */
  2746. return 0;
  2747. }
  2748. static int i830_get_display_clock_speed(struct drm_device *dev)
  2749. {
  2750. return 133000;
  2751. }
  2752. struct fdi_m_n {
  2753. u32 tu;
  2754. u32 gmch_m;
  2755. u32 gmch_n;
  2756. u32 link_m;
  2757. u32 link_n;
  2758. };
  2759. static void
  2760. fdi_reduce_ratio(u32 *num, u32 *den)
  2761. {
  2762. while (*num > 0xffffff || *den > 0xffffff) {
  2763. *num >>= 1;
  2764. *den >>= 1;
  2765. }
  2766. }
  2767. static void
  2768. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  2769. int link_clock, struct fdi_m_n *m_n)
  2770. {
  2771. m_n->tu = 64; /* default size */
  2772. /* BUG_ON(pixel_clock > INT_MAX / 36); */
  2773. m_n->gmch_m = bits_per_pixel * pixel_clock;
  2774. m_n->gmch_n = link_clock * nlanes * 8;
  2775. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  2776. m_n->link_m = pixel_clock;
  2777. m_n->link_n = link_clock;
  2778. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  2779. }
  2780. struct intel_watermark_params {
  2781. unsigned long fifo_size;
  2782. unsigned long max_wm;
  2783. unsigned long default_wm;
  2784. unsigned long guard_size;
  2785. unsigned long cacheline_size;
  2786. };
  2787. /* Pineview has different values for various configs */
  2788. static const struct intel_watermark_params pineview_display_wm = {
  2789. PINEVIEW_DISPLAY_FIFO,
  2790. PINEVIEW_MAX_WM,
  2791. PINEVIEW_DFT_WM,
  2792. PINEVIEW_GUARD_WM,
  2793. PINEVIEW_FIFO_LINE_SIZE
  2794. };
  2795. static const struct intel_watermark_params pineview_display_hplloff_wm = {
  2796. PINEVIEW_DISPLAY_FIFO,
  2797. PINEVIEW_MAX_WM,
  2798. PINEVIEW_DFT_HPLLOFF_WM,
  2799. PINEVIEW_GUARD_WM,
  2800. PINEVIEW_FIFO_LINE_SIZE
  2801. };
  2802. static const struct intel_watermark_params pineview_cursor_wm = {
  2803. PINEVIEW_CURSOR_FIFO,
  2804. PINEVIEW_CURSOR_MAX_WM,
  2805. PINEVIEW_CURSOR_DFT_WM,
  2806. PINEVIEW_CURSOR_GUARD_WM,
  2807. PINEVIEW_FIFO_LINE_SIZE,
  2808. };
  2809. static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
  2810. PINEVIEW_CURSOR_FIFO,
  2811. PINEVIEW_CURSOR_MAX_WM,
  2812. PINEVIEW_CURSOR_DFT_WM,
  2813. PINEVIEW_CURSOR_GUARD_WM,
  2814. PINEVIEW_FIFO_LINE_SIZE
  2815. };
  2816. static const struct intel_watermark_params g4x_wm_info = {
  2817. G4X_FIFO_SIZE,
  2818. G4X_MAX_WM,
  2819. G4X_MAX_WM,
  2820. 2,
  2821. G4X_FIFO_LINE_SIZE,
  2822. };
  2823. static const struct intel_watermark_params g4x_cursor_wm_info = {
  2824. I965_CURSOR_FIFO,
  2825. I965_CURSOR_MAX_WM,
  2826. I965_CURSOR_DFT_WM,
  2827. 2,
  2828. G4X_FIFO_LINE_SIZE,
  2829. };
  2830. static const struct intel_watermark_params i965_cursor_wm_info = {
  2831. I965_CURSOR_FIFO,
  2832. I965_CURSOR_MAX_WM,
  2833. I965_CURSOR_DFT_WM,
  2834. 2,
  2835. I915_FIFO_LINE_SIZE,
  2836. };
  2837. static const struct intel_watermark_params i945_wm_info = {
  2838. I945_FIFO_SIZE,
  2839. I915_MAX_WM,
  2840. 1,
  2841. 2,
  2842. I915_FIFO_LINE_SIZE
  2843. };
  2844. static const struct intel_watermark_params i915_wm_info = {
  2845. I915_FIFO_SIZE,
  2846. I915_MAX_WM,
  2847. 1,
  2848. 2,
  2849. I915_FIFO_LINE_SIZE
  2850. };
  2851. static const struct intel_watermark_params i855_wm_info = {
  2852. I855GM_FIFO_SIZE,
  2853. I915_MAX_WM,
  2854. 1,
  2855. 2,
  2856. I830_FIFO_LINE_SIZE
  2857. };
  2858. static const struct intel_watermark_params i830_wm_info = {
  2859. I830_FIFO_SIZE,
  2860. I915_MAX_WM,
  2861. 1,
  2862. 2,
  2863. I830_FIFO_LINE_SIZE
  2864. };
  2865. static const struct intel_watermark_params ironlake_display_wm_info = {
  2866. ILK_DISPLAY_FIFO,
  2867. ILK_DISPLAY_MAXWM,
  2868. ILK_DISPLAY_DFTWM,
  2869. 2,
  2870. ILK_FIFO_LINE_SIZE
  2871. };
  2872. static const struct intel_watermark_params ironlake_cursor_wm_info = {
  2873. ILK_CURSOR_FIFO,
  2874. ILK_CURSOR_MAXWM,
  2875. ILK_CURSOR_DFTWM,
  2876. 2,
  2877. ILK_FIFO_LINE_SIZE
  2878. };
  2879. static const struct intel_watermark_params ironlake_display_srwm_info = {
  2880. ILK_DISPLAY_SR_FIFO,
  2881. ILK_DISPLAY_MAX_SRWM,
  2882. ILK_DISPLAY_DFT_SRWM,
  2883. 2,
  2884. ILK_FIFO_LINE_SIZE
  2885. };
  2886. static const struct intel_watermark_params ironlake_cursor_srwm_info = {
  2887. ILK_CURSOR_SR_FIFO,
  2888. ILK_CURSOR_MAX_SRWM,
  2889. ILK_CURSOR_DFT_SRWM,
  2890. 2,
  2891. ILK_FIFO_LINE_SIZE
  2892. };
  2893. static const struct intel_watermark_params sandybridge_display_wm_info = {
  2894. SNB_DISPLAY_FIFO,
  2895. SNB_DISPLAY_MAXWM,
  2896. SNB_DISPLAY_DFTWM,
  2897. 2,
  2898. SNB_FIFO_LINE_SIZE
  2899. };
  2900. static const struct intel_watermark_params sandybridge_cursor_wm_info = {
  2901. SNB_CURSOR_FIFO,
  2902. SNB_CURSOR_MAXWM,
  2903. SNB_CURSOR_DFTWM,
  2904. 2,
  2905. SNB_FIFO_LINE_SIZE
  2906. };
  2907. static const struct intel_watermark_params sandybridge_display_srwm_info = {
  2908. SNB_DISPLAY_SR_FIFO,
  2909. SNB_DISPLAY_MAX_SRWM,
  2910. SNB_DISPLAY_DFT_SRWM,
  2911. 2,
  2912. SNB_FIFO_LINE_SIZE
  2913. };
  2914. static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
  2915. SNB_CURSOR_SR_FIFO,
  2916. SNB_CURSOR_MAX_SRWM,
  2917. SNB_CURSOR_DFT_SRWM,
  2918. 2,
  2919. SNB_FIFO_LINE_SIZE
  2920. };
  2921. /**
  2922. * intel_calculate_wm - calculate watermark level
  2923. * @clock_in_khz: pixel clock
  2924. * @wm: chip FIFO params
  2925. * @pixel_size: display pixel size
  2926. * @latency_ns: memory latency for the platform
  2927. *
  2928. * Calculate the watermark level (the level at which the display plane will
  2929. * start fetching from memory again). Each chip has a different display
  2930. * FIFO size and allocation, so the caller needs to figure that out and pass
  2931. * in the correct intel_watermark_params structure.
  2932. *
  2933. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  2934. * on the pixel size. When it reaches the watermark level, it'll start
  2935. * fetching FIFO line sized based chunks from memory until the FIFO fills
  2936. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  2937. * will occur, and a display engine hang could result.
  2938. */
  2939. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  2940. const struct intel_watermark_params *wm,
  2941. int fifo_size,
  2942. int pixel_size,
  2943. unsigned long latency_ns)
  2944. {
  2945. long entries_required, wm_size;
  2946. /*
  2947. * Note: we need to make sure we don't overflow for various clock &
  2948. * latency values.
  2949. * clocks go from a few thousand to several hundred thousand.
  2950. * latency is usually a few thousand
  2951. */
  2952. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  2953. 1000;
  2954. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  2955. DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
  2956. wm_size = fifo_size - (entries_required + wm->guard_size);
  2957. DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
  2958. /* Don't promote wm_size to unsigned... */
  2959. if (wm_size > (long)wm->max_wm)
  2960. wm_size = wm->max_wm;
  2961. if (wm_size <= 0)
  2962. wm_size = wm->default_wm;
  2963. return wm_size;
  2964. }
  2965. struct cxsr_latency {
  2966. int is_desktop;
  2967. int is_ddr3;
  2968. unsigned long fsb_freq;
  2969. unsigned long mem_freq;
  2970. unsigned long display_sr;
  2971. unsigned long display_hpll_disable;
  2972. unsigned long cursor_sr;
  2973. unsigned long cursor_hpll_disable;
  2974. };
  2975. static const struct cxsr_latency cxsr_latency_table[] = {
  2976. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  2977. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  2978. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  2979. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  2980. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  2981. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  2982. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  2983. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  2984. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  2985. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  2986. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  2987. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  2988. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  2989. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  2990. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  2991. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  2992. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  2993. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  2994. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  2995. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  2996. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  2997. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  2998. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  2999. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  3000. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  3001. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  3002. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  3003. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  3004. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  3005. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  3006. };
  3007. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  3008. int is_ddr3,
  3009. int fsb,
  3010. int mem)
  3011. {
  3012. const struct cxsr_latency *latency;
  3013. int i;
  3014. if (fsb == 0 || mem == 0)
  3015. return NULL;
  3016. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  3017. latency = &cxsr_latency_table[i];
  3018. if (is_desktop == latency->is_desktop &&
  3019. is_ddr3 == latency->is_ddr3 &&
  3020. fsb == latency->fsb_freq && mem == latency->mem_freq)
  3021. return latency;
  3022. }
  3023. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  3024. return NULL;
  3025. }
  3026. static void pineview_disable_cxsr(struct drm_device *dev)
  3027. {
  3028. struct drm_i915_private *dev_priv = dev->dev_private;
  3029. /* deactivate cxsr */
  3030. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  3031. }
  3032. /*
  3033. * Latency for FIFO fetches is dependent on several factors:
  3034. * - memory configuration (speed, channels)
  3035. * - chipset
  3036. * - current MCH state
  3037. * It can be fairly high in some situations, so here we assume a fairly
  3038. * pessimal value. It's a tradeoff between extra memory fetches (if we
  3039. * set this value too high, the FIFO will fetch frequently to stay full)
  3040. * and power consumption (set it too low to save power and we might see
  3041. * FIFO underruns and display "flicker").
  3042. *
  3043. * A value of 5us seems to be a good balance; safe for very low end
  3044. * platforms but not overly aggressive on lower latency configs.
  3045. */
  3046. static const int latency_ns = 5000;
  3047. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  3048. {
  3049. struct drm_i915_private *dev_priv = dev->dev_private;
  3050. uint32_t dsparb = I915_READ(DSPARB);
  3051. int size;
  3052. size = dsparb & 0x7f;
  3053. if (plane)
  3054. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  3055. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3056. plane ? "B" : "A", size);
  3057. return size;
  3058. }
  3059. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  3060. {
  3061. struct drm_i915_private *dev_priv = dev->dev_private;
  3062. uint32_t dsparb = I915_READ(DSPARB);
  3063. int size;
  3064. size = dsparb & 0x1ff;
  3065. if (plane)
  3066. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  3067. size >>= 1; /* Convert to cachelines */
  3068. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3069. plane ? "B" : "A", size);
  3070. return size;
  3071. }
  3072. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  3073. {
  3074. struct drm_i915_private *dev_priv = dev->dev_private;
  3075. uint32_t dsparb = I915_READ(DSPARB);
  3076. int size;
  3077. size = dsparb & 0x7f;
  3078. size >>= 2; /* Convert to cachelines */
  3079. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3080. plane ? "B" : "A",
  3081. size);
  3082. return size;
  3083. }
  3084. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  3085. {
  3086. struct drm_i915_private *dev_priv = dev->dev_private;
  3087. uint32_t dsparb = I915_READ(DSPARB);
  3088. int size;
  3089. size = dsparb & 0x7f;
  3090. size >>= 1; /* Convert to cachelines */
  3091. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3092. plane ? "B" : "A", size);
  3093. return size;
  3094. }
  3095. static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
  3096. {
  3097. struct drm_crtc *crtc, *enabled = NULL;
  3098. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3099. if (crtc->enabled && crtc->fb) {
  3100. if (enabled)
  3101. return NULL;
  3102. enabled = crtc;
  3103. }
  3104. }
  3105. return enabled;
  3106. }
  3107. static void pineview_update_wm(struct drm_device *dev)
  3108. {
  3109. struct drm_i915_private *dev_priv = dev->dev_private;
  3110. struct drm_crtc *crtc;
  3111. const struct cxsr_latency *latency;
  3112. u32 reg;
  3113. unsigned long wm;
  3114. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  3115. dev_priv->fsb_freq, dev_priv->mem_freq);
  3116. if (!latency) {
  3117. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  3118. pineview_disable_cxsr(dev);
  3119. return;
  3120. }
  3121. crtc = single_enabled_crtc(dev);
  3122. if (crtc) {
  3123. int clock = crtc->mode.clock;
  3124. int pixel_size = crtc->fb->bits_per_pixel / 8;
  3125. /* Display SR */
  3126. wm = intel_calculate_wm(clock, &pineview_display_wm,
  3127. pineview_display_wm.fifo_size,
  3128. pixel_size, latency->display_sr);
  3129. reg = I915_READ(DSPFW1);
  3130. reg &= ~DSPFW_SR_MASK;
  3131. reg |= wm << DSPFW_SR_SHIFT;
  3132. I915_WRITE(DSPFW1, reg);
  3133. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  3134. /* cursor SR */
  3135. wm = intel_calculate_wm(clock, &pineview_cursor_wm,
  3136. pineview_display_wm.fifo_size,
  3137. pixel_size, latency->cursor_sr);
  3138. reg = I915_READ(DSPFW3);
  3139. reg &= ~DSPFW_CURSOR_SR_MASK;
  3140. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  3141. I915_WRITE(DSPFW3, reg);
  3142. /* Display HPLL off SR */
  3143. wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
  3144. pineview_display_hplloff_wm.fifo_size,
  3145. pixel_size, latency->display_hpll_disable);
  3146. reg = I915_READ(DSPFW3);
  3147. reg &= ~DSPFW_HPLL_SR_MASK;
  3148. reg |= wm & DSPFW_HPLL_SR_MASK;
  3149. I915_WRITE(DSPFW3, reg);
  3150. /* cursor HPLL off SR */
  3151. wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
  3152. pineview_display_hplloff_wm.fifo_size,
  3153. pixel_size, latency->cursor_hpll_disable);
  3154. reg = I915_READ(DSPFW3);
  3155. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  3156. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  3157. I915_WRITE(DSPFW3, reg);
  3158. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  3159. /* activate cxsr */
  3160. I915_WRITE(DSPFW3,
  3161. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  3162. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  3163. } else {
  3164. pineview_disable_cxsr(dev);
  3165. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  3166. }
  3167. }
  3168. static bool g4x_compute_wm0(struct drm_device *dev,
  3169. int plane,
  3170. const struct intel_watermark_params *display,
  3171. int display_latency_ns,
  3172. const struct intel_watermark_params *cursor,
  3173. int cursor_latency_ns,
  3174. int *plane_wm,
  3175. int *cursor_wm)
  3176. {
  3177. struct drm_crtc *crtc;
  3178. int htotal, hdisplay, clock, pixel_size;
  3179. int line_time_us, line_count;
  3180. int entries, tlb_miss;
  3181. crtc = intel_get_crtc_for_plane(dev, plane);
  3182. if (crtc->fb == NULL || !crtc->enabled) {
  3183. *cursor_wm = cursor->guard_size;
  3184. *plane_wm = display->guard_size;
  3185. return false;
  3186. }
  3187. htotal = crtc->mode.htotal;
  3188. hdisplay = crtc->mode.hdisplay;
  3189. clock = crtc->mode.clock;
  3190. pixel_size = crtc->fb->bits_per_pixel / 8;
  3191. /* Use the small buffer method to calculate plane watermark */
  3192. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  3193. tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
  3194. if (tlb_miss > 0)
  3195. entries += tlb_miss;
  3196. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  3197. *plane_wm = entries + display->guard_size;
  3198. if (*plane_wm > (int)display->max_wm)
  3199. *plane_wm = display->max_wm;
  3200. /* Use the large buffer method to calculate cursor watermark */
  3201. line_time_us = ((htotal * 1000) / clock);
  3202. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  3203. entries = line_count * 64 * pixel_size;
  3204. tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
  3205. if (tlb_miss > 0)
  3206. entries += tlb_miss;
  3207. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3208. *cursor_wm = entries + cursor->guard_size;
  3209. if (*cursor_wm > (int)cursor->max_wm)
  3210. *cursor_wm = (int)cursor->max_wm;
  3211. return true;
  3212. }
  3213. /*
  3214. * Check the wm result.
  3215. *
  3216. * If any calculated watermark values is larger than the maximum value that
  3217. * can be programmed into the associated watermark register, that watermark
  3218. * must be disabled.
  3219. */
  3220. static bool g4x_check_srwm(struct drm_device *dev,
  3221. int display_wm, int cursor_wm,
  3222. const struct intel_watermark_params *display,
  3223. const struct intel_watermark_params *cursor)
  3224. {
  3225. DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
  3226. display_wm, cursor_wm);
  3227. if (display_wm > display->max_wm) {
  3228. DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
  3229. display_wm, display->max_wm);
  3230. return false;
  3231. }
  3232. if (cursor_wm > cursor->max_wm) {
  3233. DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
  3234. cursor_wm, cursor->max_wm);
  3235. return false;
  3236. }
  3237. if (!(display_wm || cursor_wm)) {
  3238. DRM_DEBUG_KMS("SR latency is 0, disabling\n");
  3239. return false;
  3240. }
  3241. return true;
  3242. }
  3243. static bool g4x_compute_srwm(struct drm_device *dev,
  3244. int plane,
  3245. int latency_ns,
  3246. const struct intel_watermark_params *display,
  3247. const struct intel_watermark_params *cursor,
  3248. int *display_wm, int *cursor_wm)
  3249. {
  3250. struct drm_crtc *crtc;
  3251. int hdisplay, htotal, pixel_size, clock;
  3252. unsigned long line_time_us;
  3253. int line_count, line_size;
  3254. int small, large;
  3255. int entries;
  3256. if (!latency_ns) {
  3257. *display_wm = *cursor_wm = 0;
  3258. return false;
  3259. }
  3260. crtc = intel_get_crtc_for_plane(dev, plane);
  3261. hdisplay = crtc->mode.hdisplay;
  3262. htotal = crtc->mode.htotal;
  3263. clock = crtc->mode.clock;
  3264. pixel_size = crtc->fb->bits_per_pixel / 8;
  3265. line_time_us = (htotal * 1000) / clock;
  3266. line_count = (latency_ns / line_time_us + 1000) / 1000;
  3267. line_size = hdisplay * pixel_size;
  3268. /* Use the minimum of the small and large buffer method for primary */
  3269. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  3270. large = line_count * line_size;
  3271. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  3272. *display_wm = entries + display->guard_size;
  3273. /* calculate the self-refresh watermark for display cursor */
  3274. entries = line_count * pixel_size * 64;
  3275. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3276. *cursor_wm = entries + cursor->guard_size;
  3277. return g4x_check_srwm(dev,
  3278. *display_wm, *cursor_wm,
  3279. display, cursor);
  3280. }
  3281. #define single_plane_enabled(mask) is_power_of_2(mask)
  3282. static void g4x_update_wm(struct drm_device *dev)
  3283. {
  3284. static const int sr_latency_ns = 12000;
  3285. struct drm_i915_private *dev_priv = dev->dev_private;
  3286. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  3287. int plane_sr, cursor_sr;
  3288. unsigned int enabled = 0;
  3289. if (g4x_compute_wm0(dev, 0,
  3290. &g4x_wm_info, latency_ns,
  3291. &g4x_cursor_wm_info, latency_ns,
  3292. &planea_wm, &cursora_wm))
  3293. enabled |= 1;
  3294. if (g4x_compute_wm0(dev, 1,
  3295. &g4x_wm_info, latency_ns,
  3296. &g4x_cursor_wm_info, latency_ns,
  3297. &planeb_wm, &cursorb_wm))
  3298. enabled |= 2;
  3299. plane_sr = cursor_sr = 0;
  3300. if (single_plane_enabled(enabled) &&
  3301. g4x_compute_srwm(dev, ffs(enabled) - 1,
  3302. sr_latency_ns,
  3303. &g4x_wm_info,
  3304. &g4x_cursor_wm_info,
  3305. &plane_sr, &cursor_sr))
  3306. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  3307. else
  3308. I915_WRITE(FW_BLC_SELF,
  3309. I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
  3310. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  3311. planea_wm, cursora_wm,
  3312. planeb_wm, cursorb_wm,
  3313. plane_sr, cursor_sr);
  3314. I915_WRITE(DSPFW1,
  3315. (plane_sr << DSPFW_SR_SHIFT) |
  3316. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  3317. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  3318. planea_wm);
  3319. I915_WRITE(DSPFW2,
  3320. (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  3321. (cursora_wm << DSPFW_CURSORA_SHIFT));
  3322. /* HPLL off in SR has some issues on G4x... disable it */
  3323. I915_WRITE(DSPFW3,
  3324. (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  3325. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  3326. }
  3327. static void i965_update_wm(struct drm_device *dev)
  3328. {
  3329. struct drm_i915_private *dev_priv = dev->dev_private;
  3330. struct drm_crtc *crtc;
  3331. int srwm = 1;
  3332. int cursor_sr = 16;
  3333. /* Calc sr entries for one plane configs */
  3334. crtc = single_enabled_crtc(dev);
  3335. if (crtc) {
  3336. /* self-refresh has much higher latency */
  3337. static const int sr_latency_ns = 12000;
  3338. int clock = crtc->mode.clock;
  3339. int htotal = crtc->mode.htotal;
  3340. int hdisplay = crtc->mode.hdisplay;
  3341. int pixel_size = crtc->fb->bits_per_pixel / 8;
  3342. unsigned long line_time_us;
  3343. int entries;
  3344. line_time_us = ((htotal * 1000) / clock);
  3345. /* Use ns/us then divide to preserve precision */
  3346. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3347. pixel_size * hdisplay;
  3348. entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
  3349. srwm = I965_FIFO_SIZE - entries;
  3350. if (srwm < 0)
  3351. srwm = 1;
  3352. srwm &= 0x1ff;
  3353. DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
  3354. entries, srwm);
  3355. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3356. pixel_size * 64;
  3357. entries = DIV_ROUND_UP(entries,
  3358. i965_cursor_wm_info.cacheline_size);
  3359. cursor_sr = i965_cursor_wm_info.fifo_size -
  3360. (entries + i965_cursor_wm_info.guard_size);
  3361. if (cursor_sr > i965_cursor_wm_info.max_wm)
  3362. cursor_sr = i965_cursor_wm_info.max_wm;
  3363. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  3364. "cursor %d\n", srwm, cursor_sr);
  3365. if (IS_CRESTLINE(dev))
  3366. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  3367. } else {
  3368. /* Turn off self refresh if both pipes are enabled */
  3369. if (IS_CRESTLINE(dev))
  3370. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  3371. & ~FW_BLC_SELF_EN);
  3372. }
  3373. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  3374. srwm);
  3375. /* 965 has limitations... */
  3376. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
  3377. (8 << 16) | (8 << 8) | (8 << 0));
  3378. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  3379. /* update cursor SR watermark */
  3380. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  3381. }
  3382. static void i9xx_update_wm(struct drm_device *dev)
  3383. {
  3384. struct drm_i915_private *dev_priv = dev->dev_private;
  3385. const struct intel_watermark_params *wm_info;
  3386. uint32_t fwater_lo;
  3387. uint32_t fwater_hi;
  3388. int cwm, srwm = 1;
  3389. int fifo_size;
  3390. int planea_wm, planeb_wm;
  3391. struct drm_crtc *crtc, *enabled = NULL;
  3392. if (IS_I945GM(dev))
  3393. wm_info = &i945_wm_info;
  3394. else if (!IS_GEN2(dev))
  3395. wm_info = &i915_wm_info;
  3396. else
  3397. wm_info = &i855_wm_info;
  3398. fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  3399. crtc = intel_get_crtc_for_plane(dev, 0);
  3400. if (crtc->enabled && crtc->fb) {
  3401. planea_wm = intel_calculate_wm(crtc->mode.clock,
  3402. wm_info, fifo_size,
  3403. crtc->fb->bits_per_pixel / 8,
  3404. latency_ns);
  3405. enabled = crtc;
  3406. } else
  3407. planea_wm = fifo_size - wm_info->guard_size;
  3408. fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  3409. crtc = intel_get_crtc_for_plane(dev, 1);
  3410. if (crtc->enabled && crtc->fb) {
  3411. planeb_wm = intel_calculate_wm(crtc->mode.clock,
  3412. wm_info, fifo_size,
  3413. crtc->fb->bits_per_pixel / 8,
  3414. latency_ns);
  3415. if (enabled == NULL)
  3416. enabled = crtc;
  3417. else
  3418. enabled = NULL;
  3419. } else
  3420. planeb_wm = fifo_size - wm_info->guard_size;
  3421. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  3422. /*
  3423. * Overlay gets an aggressive default since video jitter is bad.
  3424. */
  3425. cwm = 2;
  3426. /* Play safe and disable self-refresh before adjusting watermarks. */
  3427. if (IS_I945G(dev) || IS_I945GM(dev))
  3428. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
  3429. else if (IS_I915GM(dev))
  3430. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  3431. /* Calc sr entries for one plane configs */
  3432. if (HAS_FW_BLC(dev) && enabled) {
  3433. /* self-refresh has much higher latency */
  3434. static const int sr_latency_ns = 6000;
  3435. int clock = enabled->mode.clock;
  3436. int htotal = enabled->mode.htotal;
  3437. int hdisplay = enabled->mode.hdisplay;
  3438. int pixel_size = enabled->fb->bits_per_pixel / 8;
  3439. unsigned long line_time_us;
  3440. int entries;
  3441. line_time_us = (htotal * 1000) / clock;
  3442. /* Use ns/us then divide to preserve precision */
  3443. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3444. pixel_size * hdisplay;
  3445. entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
  3446. DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
  3447. srwm = wm_info->fifo_size - entries;
  3448. if (srwm < 0)
  3449. srwm = 1;
  3450. if (IS_I945G(dev) || IS_I945GM(dev))
  3451. I915_WRITE(FW_BLC_SELF,
  3452. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  3453. else if (IS_I915GM(dev))
  3454. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  3455. }
  3456. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  3457. planea_wm, planeb_wm, cwm, srwm);
  3458. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  3459. fwater_hi = (cwm & 0x1f);
  3460. /* Set request length to 8 cachelines per fetch */
  3461. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  3462. fwater_hi = fwater_hi | (1 << 8);
  3463. I915_WRITE(FW_BLC, fwater_lo);
  3464. I915_WRITE(FW_BLC2, fwater_hi);
  3465. if (HAS_FW_BLC(dev)) {
  3466. if (enabled) {
  3467. if (IS_I945G(dev) || IS_I945GM(dev))
  3468. I915_WRITE(FW_BLC_SELF,
  3469. FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  3470. else if (IS_I915GM(dev))
  3471. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  3472. DRM_DEBUG_KMS("memory self refresh enabled\n");
  3473. } else
  3474. DRM_DEBUG_KMS("memory self refresh disabled\n");
  3475. }
  3476. }
  3477. static void i830_update_wm(struct drm_device *dev)
  3478. {
  3479. struct drm_i915_private *dev_priv = dev->dev_private;
  3480. struct drm_crtc *crtc;
  3481. uint32_t fwater_lo;
  3482. int planea_wm;
  3483. crtc = single_enabled_crtc(dev);
  3484. if (crtc == NULL)
  3485. return;
  3486. planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
  3487. dev_priv->display.get_fifo_size(dev, 0),
  3488. crtc->fb->bits_per_pixel / 8,
  3489. latency_ns);
  3490. fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  3491. fwater_lo |= (3<<8) | planea_wm;
  3492. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  3493. I915_WRITE(FW_BLC, fwater_lo);
  3494. }
  3495. #define ILK_LP0_PLANE_LATENCY 700
  3496. #define ILK_LP0_CURSOR_LATENCY 1300
  3497. /*
  3498. * Check the wm result.
  3499. *
  3500. * If any calculated watermark values is larger than the maximum value that
  3501. * can be programmed into the associated watermark register, that watermark
  3502. * must be disabled.
  3503. */
  3504. static bool ironlake_check_srwm(struct drm_device *dev, int level,
  3505. int fbc_wm, int display_wm, int cursor_wm,
  3506. const struct intel_watermark_params *display,
  3507. const struct intel_watermark_params *cursor)
  3508. {
  3509. struct drm_i915_private *dev_priv = dev->dev_private;
  3510. DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
  3511. " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
  3512. if (fbc_wm > SNB_FBC_MAX_SRWM) {
  3513. DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
  3514. fbc_wm, SNB_FBC_MAX_SRWM, level);
  3515. /* fbc has it's own way to disable FBC WM */
  3516. I915_WRITE(DISP_ARB_CTL,
  3517. I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
  3518. return false;
  3519. }
  3520. if (display_wm > display->max_wm) {
  3521. DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
  3522. display_wm, SNB_DISPLAY_MAX_SRWM, level);
  3523. return false;
  3524. }
  3525. if (cursor_wm > cursor->max_wm) {
  3526. DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
  3527. cursor_wm, SNB_CURSOR_MAX_SRWM, level);
  3528. return false;
  3529. }
  3530. if (!(fbc_wm || display_wm || cursor_wm)) {
  3531. DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
  3532. return false;
  3533. }
  3534. return true;
  3535. }
  3536. /*
  3537. * Compute watermark values of WM[1-3],
  3538. */
  3539. static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
  3540. int latency_ns,
  3541. const struct intel_watermark_params *display,
  3542. const struct intel_watermark_params *cursor,
  3543. int *fbc_wm, int *display_wm, int *cursor_wm)
  3544. {
  3545. struct drm_crtc *crtc;
  3546. unsigned long line_time_us;
  3547. int hdisplay, htotal, pixel_size, clock;
  3548. int line_count, line_size;
  3549. int small, large;
  3550. int entries;
  3551. if (!latency_ns) {
  3552. *fbc_wm = *display_wm = *cursor_wm = 0;
  3553. return false;
  3554. }
  3555. crtc = intel_get_crtc_for_plane(dev, plane);
  3556. hdisplay = crtc->mode.hdisplay;
  3557. htotal = crtc->mode.htotal;
  3558. clock = crtc->mode.clock;
  3559. pixel_size = crtc->fb->bits_per_pixel / 8;
  3560. line_time_us = (htotal * 1000) / clock;
  3561. line_count = (latency_ns / line_time_us + 1000) / 1000;
  3562. line_size = hdisplay * pixel_size;
  3563. /* Use the minimum of the small and large buffer method for primary */
  3564. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  3565. large = line_count * line_size;
  3566. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  3567. *display_wm = entries + display->guard_size;
  3568. /*
  3569. * Spec says:
  3570. * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
  3571. */
  3572. *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
  3573. /* calculate the self-refresh watermark for display cursor */
  3574. entries = line_count * pixel_size * 64;
  3575. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3576. *cursor_wm = entries + cursor->guard_size;
  3577. return ironlake_check_srwm(dev, level,
  3578. *fbc_wm, *display_wm, *cursor_wm,
  3579. display, cursor);
  3580. }
  3581. static void ironlake_update_wm(struct drm_device *dev)
  3582. {
  3583. struct drm_i915_private *dev_priv = dev->dev_private;
  3584. int fbc_wm, plane_wm, cursor_wm;
  3585. unsigned int enabled;
  3586. enabled = 0;
  3587. if (g4x_compute_wm0(dev, 0,
  3588. &ironlake_display_wm_info,
  3589. ILK_LP0_PLANE_LATENCY,
  3590. &ironlake_cursor_wm_info,
  3591. ILK_LP0_CURSOR_LATENCY,
  3592. &plane_wm, &cursor_wm)) {
  3593. I915_WRITE(WM0_PIPEA_ILK,
  3594. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3595. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3596. " plane %d, " "cursor: %d\n",
  3597. plane_wm, cursor_wm);
  3598. enabled |= 1;
  3599. }
  3600. if (g4x_compute_wm0(dev, 1,
  3601. &ironlake_display_wm_info,
  3602. ILK_LP0_PLANE_LATENCY,
  3603. &ironlake_cursor_wm_info,
  3604. ILK_LP0_CURSOR_LATENCY,
  3605. &plane_wm, &cursor_wm)) {
  3606. I915_WRITE(WM0_PIPEB_ILK,
  3607. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3608. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3609. " plane %d, cursor: %d\n",
  3610. plane_wm, cursor_wm);
  3611. enabled |= 2;
  3612. }
  3613. /*
  3614. * Calculate and update the self-refresh watermark only when one
  3615. * display plane is used.
  3616. */
  3617. I915_WRITE(WM3_LP_ILK, 0);
  3618. I915_WRITE(WM2_LP_ILK, 0);
  3619. I915_WRITE(WM1_LP_ILK, 0);
  3620. if (!single_plane_enabled(enabled))
  3621. return;
  3622. enabled = ffs(enabled) - 1;
  3623. /* WM1 */
  3624. if (!ironlake_compute_srwm(dev, 1, enabled,
  3625. ILK_READ_WM1_LATENCY() * 500,
  3626. &ironlake_display_srwm_info,
  3627. &ironlake_cursor_srwm_info,
  3628. &fbc_wm, &plane_wm, &cursor_wm))
  3629. return;
  3630. I915_WRITE(WM1_LP_ILK,
  3631. WM1_LP_SR_EN |
  3632. (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3633. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3634. (plane_wm << WM1_LP_SR_SHIFT) |
  3635. cursor_wm);
  3636. /* WM2 */
  3637. if (!ironlake_compute_srwm(dev, 2, enabled,
  3638. ILK_READ_WM2_LATENCY() * 500,
  3639. &ironlake_display_srwm_info,
  3640. &ironlake_cursor_srwm_info,
  3641. &fbc_wm, &plane_wm, &cursor_wm))
  3642. return;
  3643. I915_WRITE(WM2_LP_ILK,
  3644. WM2_LP_EN |
  3645. (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3646. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3647. (plane_wm << WM1_LP_SR_SHIFT) |
  3648. cursor_wm);
  3649. /*
  3650. * WM3 is unsupported on ILK, probably because we don't have latency
  3651. * data for that power state
  3652. */
  3653. }
  3654. static void sandybridge_update_wm(struct drm_device *dev)
  3655. {
  3656. struct drm_i915_private *dev_priv = dev->dev_private;
  3657. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  3658. int fbc_wm, plane_wm, cursor_wm;
  3659. unsigned int enabled;
  3660. enabled = 0;
  3661. if (g4x_compute_wm0(dev, 0,
  3662. &sandybridge_display_wm_info, latency,
  3663. &sandybridge_cursor_wm_info, latency,
  3664. &plane_wm, &cursor_wm)) {
  3665. I915_WRITE(WM0_PIPEA_ILK,
  3666. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3667. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3668. " plane %d, " "cursor: %d\n",
  3669. plane_wm, cursor_wm);
  3670. enabled |= 1;
  3671. }
  3672. if (g4x_compute_wm0(dev, 1,
  3673. &sandybridge_display_wm_info, latency,
  3674. &sandybridge_cursor_wm_info, latency,
  3675. &plane_wm, &cursor_wm)) {
  3676. I915_WRITE(WM0_PIPEB_ILK,
  3677. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3678. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3679. " plane %d, cursor: %d\n",
  3680. plane_wm, cursor_wm);
  3681. enabled |= 2;
  3682. }
  3683. /*
  3684. * Calculate and update the self-refresh watermark only when one
  3685. * display plane is used.
  3686. *
  3687. * SNB support 3 levels of watermark.
  3688. *
  3689. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  3690. * and disabled in the descending order
  3691. *
  3692. */
  3693. I915_WRITE(WM3_LP_ILK, 0);
  3694. I915_WRITE(WM2_LP_ILK, 0);
  3695. I915_WRITE(WM1_LP_ILK, 0);
  3696. if (!single_plane_enabled(enabled))
  3697. return;
  3698. enabled = ffs(enabled) - 1;
  3699. /* WM1 */
  3700. if (!ironlake_compute_srwm(dev, 1, enabled,
  3701. SNB_READ_WM1_LATENCY() * 500,
  3702. &sandybridge_display_srwm_info,
  3703. &sandybridge_cursor_srwm_info,
  3704. &fbc_wm, &plane_wm, &cursor_wm))
  3705. return;
  3706. I915_WRITE(WM1_LP_ILK,
  3707. WM1_LP_SR_EN |
  3708. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3709. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3710. (plane_wm << WM1_LP_SR_SHIFT) |
  3711. cursor_wm);
  3712. /* WM2 */
  3713. if (!ironlake_compute_srwm(dev, 2, enabled,
  3714. SNB_READ_WM2_LATENCY() * 500,
  3715. &sandybridge_display_srwm_info,
  3716. &sandybridge_cursor_srwm_info,
  3717. &fbc_wm, &plane_wm, &cursor_wm))
  3718. return;
  3719. I915_WRITE(WM2_LP_ILK,
  3720. WM2_LP_EN |
  3721. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3722. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3723. (plane_wm << WM1_LP_SR_SHIFT) |
  3724. cursor_wm);
  3725. /* WM3 */
  3726. if (!ironlake_compute_srwm(dev, 3, enabled,
  3727. SNB_READ_WM3_LATENCY() * 500,
  3728. &sandybridge_display_srwm_info,
  3729. &sandybridge_cursor_srwm_info,
  3730. &fbc_wm, &plane_wm, &cursor_wm))
  3731. return;
  3732. I915_WRITE(WM3_LP_ILK,
  3733. WM3_LP_EN |
  3734. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3735. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3736. (plane_wm << WM1_LP_SR_SHIFT) |
  3737. cursor_wm);
  3738. }
  3739. /**
  3740. * intel_update_watermarks - update FIFO watermark values based on current modes
  3741. *
  3742. * Calculate watermark values for the various WM regs based on current mode
  3743. * and plane configuration.
  3744. *
  3745. * There are several cases to deal with here:
  3746. * - normal (i.e. non-self-refresh)
  3747. * - self-refresh (SR) mode
  3748. * - lines are large relative to FIFO size (buffer can hold up to 2)
  3749. * - lines are small relative to FIFO size (buffer can hold more than 2
  3750. * lines), so need to account for TLB latency
  3751. *
  3752. * The normal calculation is:
  3753. * watermark = dotclock * bytes per pixel * latency
  3754. * where latency is platform & configuration dependent (we assume pessimal
  3755. * values here).
  3756. *
  3757. * The SR calculation is:
  3758. * watermark = (trunc(latency/line time)+1) * surface width *
  3759. * bytes per pixel
  3760. * where
  3761. * line time = htotal / dotclock
  3762. * surface width = hdisplay for normal plane and 64 for cursor
  3763. * and latency is assumed to be high, as above.
  3764. *
  3765. * The final value programmed to the register should always be rounded up,
  3766. * and include an extra 2 entries to account for clock crossings.
  3767. *
  3768. * We don't use the sprite, so we can ignore that. And on Crestline we have
  3769. * to set the non-SR watermarks to 8.
  3770. */
  3771. static void intel_update_watermarks(struct drm_device *dev)
  3772. {
  3773. struct drm_i915_private *dev_priv = dev->dev_private;
  3774. if (dev_priv->display.update_wm)
  3775. dev_priv->display.update_wm(dev);
  3776. }
  3777. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  3778. {
  3779. return dev_priv->lvds_use_ssc && i915_panel_use_ssc;
  3780. }
  3781. /**
  3782. * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
  3783. * @crtc: CRTC structure
  3784. *
  3785. * A pipe may be connected to one or more outputs. Based on the depth of the
  3786. * attached framebuffer, choose a good color depth to use on the pipe.
  3787. *
  3788. * If possible, match the pipe depth to the fb depth. In some cases, this
  3789. * isn't ideal, because the connected output supports a lesser or restricted
  3790. * set of depths. Resolve that here:
  3791. * LVDS typically supports only 6bpc, so clamp down in that case
  3792. * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
  3793. * Displays may support a restricted set as well, check EDID and clamp as
  3794. * appropriate.
  3795. *
  3796. * RETURNS:
  3797. * Dithering requirement (i.e. false if display bpc and pipe bpc match,
  3798. * true if they don't match).
  3799. */
  3800. static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
  3801. unsigned int *pipe_bpp)
  3802. {
  3803. struct drm_device *dev = crtc->dev;
  3804. struct drm_i915_private *dev_priv = dev->dev_private;
  3805. struct drm_encoder *encoder;
  3806. struct drm_connector *connector;
  3807. unsigned int display_bpc = UINT_MAX, bpc;
  3808. /* Walk the encoders & connectors on this crtc, get min bpc */
  3809. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  3810. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  3811. if (encoder->crtc != crtc)
  3812. continue;
  3813. if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
  3814. unsigned int lvds_bpc;
  3815. if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
  3816. LVDS_A3_POWER_UP)
  3817. lvds_bpc = 8;
  3818. else
  3819. lvds_bpc = 6;
  3820. if (lvds_bpc < display_bpc) {
  3821. DRM_DEBUG_DRIVER("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
  3822. display_bpc = lvds_bpc;
  3823. }
  3824. continue;
  3825. }
  3826. if (intel_encoder->type == INTEL_OUTPUT_EDP) {
  3827. /* Use VBT settings if we have an eDP panel */
  3828. unsigned int edp_bpc = dev_priv->edp.bpp / 3;
  3829. if (edp_bpc < display_bpc) {
  3830. DRM_DEBUG_DRIVER("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
  3831. display_bpc = edp_bpc;
  3832. }
  3833. continue;
  3834. }
  3835. /* Not one of the known troublemakers, check the EDID */
  3836. list_for_each_entry(connector, &dev->mode_config.connector_list,
  3837. head) {
  3838. if (connector->encoder != encoder)
  3839. continue;
  3840. if (connector->display_info.bpc < display_bpc) {
  3841. DRM_DEBUG_DRIVER("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
  3842. display_bpc = connector->display_info.bpc;
  3843. }
  3844. }
  3845. /*
  3846. * HDMI is either 12 or 8, so if the display lets 10bpc sneak
  3847. * through, clamp it down. (Note: >12bpc will be caught below.)
  3848. */
  3849. if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
  3850. if (display_bpc > 8 && display_bpc < 12) {
  3851. DRM_DEBUG_DRIVER("forcing bpc to 12 for HDMI\n");
  3852. display_bpc = 12;
  3853. } else {
  3854. DRM_DEBUG_DRIVER("forcing bpc to 8 for HDMI\n");
  3855. display_bpc = 8;
  3856. }
  3857. }
  3858. }
  3859. /*
  3860. * We could just drive the pipe at the highest bpc all the time and
  3861. * enable dithering as needed, but that costs bandwidth. So choose
  3862. * the minimum value that expresses the full color range of the fb but
  3863. * also stays within the max display bpc discovered above.
  3864. */
  3865. switch (crtc->fb->depth) {
  3866. case 8:
  3867. bpc = 8; /* since we go through a colormap */
  3868. break;
  3869. case 15:
  3870. case 16:
  3871. bpc = 6; /* min is 18bpp */
  3872. break;
  3873. case 24:
  3874. bpc = min((unsigned int)8, display_bpc);
  3875. break;
  3876. case 30:
  3877. bpc = min((unsigned int)10, display_bpc);
  3878. break;
  3879. case 48:
  3880. bpc = min((unsigned int)12, display_bpc);
  3881. break;
  3882. default:
  3883. DRM_DEBUG("unsupported depth, assuming 24 bits\n");
  3884. bpc = min((unsigned int)8, display_bpc);
  3885. break;
  3886. }
  3887. DRM_DEBUG_DRIVER("setting pipe bpc to %d (max display bpc %d)\n",
  3888. bpc, display_bpc);
  3889. *pipe_bpp = bpc * 3;
  3890. return display_bpc != bpc;
  3891. }
  3892. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  3893. struct drm_display_mode *mode,
  3894. struct drm_display_mode *adjusted_mode,
  3895. int x, int y,
  3896. struct drm_framebuffer *old_fb)
  3897. {
  3898. struct drm_device *dev = crtc->dev;
  3899. struct drm_i915_private *dev_priv = dev->dev_private;
  3900. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3901. int pipe = intel_crtc->pipe;
  3902. int plane = intel_crtc->plane;
  3903. int refclk, num_connectors = 0;
  3904. intel_clock_t clock, reduced_clock;
  3905. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  3906. bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
  3907. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  3908. struct drm_mode_config *mode_config = &dev->mode_config;
  3909. struct intel_encoder *encoder;
  3910. const intel_limit_t *limit;
  3911. int ret;
  3912. u32 temp;
  3913. u32 lvds_sync = 0;
  3914. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  3915. if (encoder->base.crtc != crtc)
  3916. continue;
  3917. switch (encoder->type) {
  3918. case INTEL_OUTPUT_LVDS:
  3919. is_lvds = true;
  3920. break;
  3921. case INTEL_OUTPUT_SDVO:
  3922. case INTEL_OUTPUT_HDMI:
  3923. is_sdvo = true;
  3924. if (encoder->needs_tv_clock)
  3925. is_tv = true;
  3926. break;
  3927. case INTEL_OUTPUT_DVO:
  3928. is_dvo = true;
  3929. break;
  3930. case INTEL_OUTPUT_TVOUT:
  3931. is_tv = true;
  3932. break;
  3933. case INTEL_OUTPUT_ANALOG:
  3934. is_crt = true;
  3935. break;
  3936. case INTEL_OUTPUT_DISPLAYPORT:
  3937. is_dp = true;
  3938. break;
  3939. }
  3940. num_connectors++;
  3941. }
  3942. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  3943. refclk = dev_priv->lvds_ssc_freq * 1000;
  3944. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3945. refclk / 1000);
  3946. } else if (!IS_GEN2(dev)) {
  3947. refclk = 96000;
  3948. } else {
  3949. refclk = 48000;
  3950. }
  3951. /*
  3952. * Returns a set of divisors for the desired target clock with the given
  3953. * refclk, or FALSE. The returned values represent the clock equation:
  3954. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3955. */
  3956. limit = intel_limit(crtc, refclk);
  3957. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  3958. if (!ok) {
  3959. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3960. return -EINVAL;
  3961. }
  3962. /* Ensure that the cursor is valid for the new mode before changing... */
  3963. intel_crtc_update_cursor(crtc, true);
  3964. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3965. has_reduced_clock = limit->find_pll(limit, crtc,
  3966. dev_priv->lvds_downclock,
  3967. refclk,
  3968. &reduced_clock);
  3969. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  3970. /*
  3971. * If the different P is found, it means that we can't
  3972. * switch the display clock by using the FP0/FP1.
  3973. * In such case we will disable the LVDS downclock
  3974. * feature.
  3975. */
  3976. DRM_DEBUG_KMS("Different P is found for "
  3977. "LVDS clock/downclock\n");
  3978. has_reduced_clock = 0;
  3979. }
  3980. }
  3981. /* SDVO TV has fixed PLL values depend on its clock range,
  3982. this mirrors vbios setting. */
  3983. if (is_sdvo && is_tv) {
  3984. if (adjusted_mode->clock >= 100000
  3985. && adjusted_mode->clock < 140500) {
  3986. clock.p1 = 2;
  3987. clock.p2 = 10;
  3988. clock.n = 3;
  3989. clock.m1 = 16;
  3990. clock.m2 = 8;
  3991. } else if (adjusted_mode->clock >= 140500
  3992. && adjusted_mode->clock <= 200000) {
  3993. clock.p1 = 1;
  3994. clock.p2 = 10;
  3995. clock.n = 6;
  3996. clock.m1 = 12;
  3997. clock.m2 = 8;
  3998. }
  3999. }
  4000. if (IS_PINEVIEW(dev)) {
  4001. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  4002. if (has_reduced_clock)
  4003. fp2 = (1 << reduced_clock.n) << 16 |
  4004. reduced_clock.m1 << 8 | reduced_clock.m2;
  4005. } else {
  4006. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  4007. if (has_reduced_clock)
  4008. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  4009. reduced_clock.m2;
  4010. }
  4011. dpll = DPLL_VGA_MODE_DIS;
  4012. if (!IS_GEN2(dev)) {
  4013. if (is_lvds)
  4014. dpll |= DPLLB_MODE_LVDS;
  4015. else
  4016. dpll |= DPLLB_MODE_DAC_SERIAL;
  4017. if (is_sdvo) {
  4018. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4019. if (pixel_multiplier > 1) {
  4020. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4021. dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  4022. }
  4023. dpll |= DPLL_DVO_HIGH_SPEED;
  4024. }
  4025. if (is_dp)
  4026. dpll |= DPLL_DVO_HIGH_SPEED;
  4027. /* compute bitmask from p1 value */
  4028. if (IS_PINEVIEW(dev))
  4029. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4030. else {
  4031. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4032. if (IS_G4X(dev) && has_reduced_clock)
  4033. dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4034. }
  4035. switch (clock.p2) {
  4036. case 5:
  4037. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4038. break;
  4039. case 7:
  4040. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4041. break;
  4042. case 10:
  4043. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4044. break;
  4045. case 14:
  4046. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4047. break;
  4048. }
  4049. if (INTEL_INFO(dev)->gen >= 4)
  4050. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4051. } else {
  4052. if (is_lvds) {
  4053. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4054. } else {
  4055. if (clock.p1 == 2)
  4056. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4057. else
  4058. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4059. if (clock.p2 == 4)
  4060. dpll |= PLL_P2_DIVIDE_BY_4;
  4061. }
  4062. }
  4063. if (is_sdvo && is_tv)
  4064. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4065. else if (is_tv)
  4066. /* XXX: just matching BIOS for now */
  4067. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  4068. dpll |= 3;
  4069. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4070. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4071. else
  4072. dpll |= PLL_REF_INPUT_DREFCLK;
  4073. /* setup pipeconf */
  4074. pipeconf = I915_READ(PIPECONF(pipe));
  4075. /* Set up the display plane register */
  4076. dspcntr = DISPPLANE_GAMMA_ENABLE;
  4077. /* Ironlake's plane is forced to pipe, bit 24 is to
  4078. enable color space conversion */
  4079. if (pipe == 0)
  4080. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  4081. else
  4082. dspcntr |= DISPPLANE_SEL_PIPE_B;
  4083. if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  4084. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  4085. * core speed.
  4086. *
  4087. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  4088. * pipe == 0 check?
  4089. */
  4090. if (mode->clock >
  4091. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  4092. pipeconf |= PIPECONF_DOUBLE_WIDE;
  4093. else
  4094. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  4095. }
  4096. dpll |= DPLL_VCO_ENABLE;
  4097. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  4098. drm_mode_debug_printmodeline(mode);
  4099. I915_WRITE(FP0(pipe), fp);
  4100. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  4101. POSTING_READ(DPLL(pipe));
  4102. udelay(150);
  4103. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  4104. * This is an exception to the general rule that mode_set doesn't turn
  4105. * things on.
  4106. */
  4107. if (is_lvds) {
  4108. temp = I915_READ(LVDS);
  4109. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  4110. if (pipe == 1) {
  4111. temp |= LVDS_PIPEB_SELECT;
  4112. } else {
  4113. temp &= ~LVDS_PIPEB_SELECT;
  4114. }
  4115. /* set the corresponsding LVDS_BORDER bit */
  4116. temp |= dev_priv->lvds_border_bits;
  4117. /* Set the B0-B3 data pairs corresponding to whether we're going to
  4118. * set the DPLLs for dual-channel mode or not.
  4119. */
  4120. if (clock.p2 == 7)
  4121. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  4122. else
  4123. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  4124. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  4125. * appropriately here, but we need to look more thoroughly into how
  4126. * panels behave in the two modes.
  4127. */
  4128. /* set the dithering flag on LVDS as needed */
  4129. if (INTEL_INFO(dev)->gen >= 4) {
  4130. if (dev_priv->lvds_dither)
  4131. temp |= LVDS_ENABLE_DITHER;
  4132. else
  4133. temp &= ~LVDS_ENABLE_DITHER;
  4134. }
  4135. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  4136. lvds_sync |= LVDS_HSYNC_POLARITY;
  4137. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  4138. lvds_sync |= LVDS_VSYNC_POLARITY;
  4139. if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
  4140. != lvds_sync) {
  4141. char flags[2] = "-+";
  4142. DRM_INFO("Changing LVDS panel from "
  4143. "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
  4144. flags[!(temp & LVDS_HSYNC_POLARITY)],
  4145. flags[!(temp & LVDS_VSYNC_POLARITY)],
  4146. flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
  4147. flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
  4148. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  4149. temp |= lvds_sync;
  4150. }
  4151. I915_WRITE(LVDS, temp);
  4152. }
  4153. if (is_dp) {
  4154. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  4155. }
  4156. I915_WRITE(DPLL(pipe), dpll);
  4157. /* Wait for the clocks to stabilize. */
  4158. POSTING_READ(DPLL(pipe));
  4159. udelay(150);
  4160. if (INTEL_INFO(dev)->gen >= 4) {
  4161. temp = 0;
  4162. if (is_sdvo) {
  4163. temp = intel_mode_get_pixel_multiplier(adjusted_mode);
  4164. if (temp > 1)
  4165. temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4166. else
  4167. temp = 0;
  4168. }
  4169. I915_WRITE(DPLL_MD(pipe), temp);
  4170. } else {
  4171. /* The pixel multiplier can only be updated once the
  4172. * DPLL is enabled and the clocks are stable.
  4173. *
  4174. * So write it again.
  4175. */
  4176. I915_WRITE(DPLL(pipe), dpll);
  4177. }
  4178. intel_crtc->lowfreq_avail = false;
  4179. if (is_lvds && has_reduced_clock && i915_powersave) {
  4180. I915_WRITE(FP1(pipe), fp2);
  4181. intel_crtc->lowfreq_avail = true;
  4182. if (HAS_PIPE_CXSR(dev)) {
  4183. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4184. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4185. }
  4186. } else {
  4187. I915_WRITE(FP1(pipe), fp);
  4188. if (HAS_PIPE_CXSR(dev)) {
  4189. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4190. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  4191. }
  4192. }
  4193. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4194. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4195. /* the chip adds 2 halflines automatically */
  4196. adjusted_mode->crtc_vdisplay -= 1;
  4197. adjusted_mode->crtc_vtotal -= 1;
  4198. adjusted_mode->crtc_vblank_start -= 1;
  4199. adjusted_mode->crtc_vblank_end -= 1;
  4200. adjusted_mode->crtc_vsync_end -= 1;
  4201. adjusted_mode->crtc_vsync_start -= 1;
  4202. } else
  4203. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  4204. I915_WRITE(HTOTAL(pipe),
  4205. (adjusted_mode->crtc_hdisplay - 1) |
  4206. ((adjusted_mode->crtc_htotal - 1) << 16));
  4207. I915_WRITE(HBLANK(pipe),
  4208. (adjusted_mode->crtc_hblank_start - 1) |
  4209. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4210. I915_WRITE(HSYNC(pipe),
  4211. (adjusted_mode->crtc_hsync_start - 1) |
  4212. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4213. I915_WRITE(VTOTAL(pipe),
  4214. (adjusted_mode->crtc_vdisplay - 1) |
  4215. ((adjusted_mode->crtc_vtotal - 1) << 16));
  4216. I915_WRITE(VBLANK(pipe),
  4217. (adjusted_mode->crtc_vblank_start - 1) |
  4218. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  4219. I915_WRITE(VSYNC(pipe),
  4220. (adjusted_mode->crtc_vsync_start - 1) |
  4221. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4222. /* pipesrc and dspsize control the size that is scaled from,
  4223. * which should always be the user's requested size.
  4224. */
  4225. I915_WRITE(DSPSIZE(plane),
  4226. ((mode->vdisplay - 1) << 16) |
  4227. (mode->hdisplay - 1));
  4228. I915_WRITE(DSPPOS(plane), 0);
  4229. I915_WRITE(PIPESRC(pipe),
  4230. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  4231. I915_WRITE(PIPECONF(pipe), pipeconf);
  4232. POSTING_READ(PIPECONF(pipe));
  4233. intel_enable_pipe(dev_priv, pipe, false);
  4234. intel_wait_for_vblank(dev, pipe);
  4235. I915_WRITE(DSPCNTR(plane), dspcntr);
  4236. POSTING_READ(DSPCNTR(plane));
  4237. intel_enable_plane(dev_priv, plane, pipe);
  4238. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  4239. intel_update_watermarks(dev);
  4240. return ret;
  4241. }
  4242. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  4243. struct drm_display_mode *mode,
  4244. struct drm_display_mode *adjusted_mode,
  4245. int x, int y,
  4246. struct drm_framebuffer *old_fb)
  4247. {
  4248. struct drm_device *dev = crtc->dev;
  4249. struct drm_i915_private *dev_priv = dev->dev_private;
  4250. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4251. int pipe = intel_crtc->pipe;
  4252. int plane = intel_crtc->plane;
  4253. int refclk, num_connectors = 0;
  4254. intel_clock_t clock, reduced_clock;
  4255. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  4256. bool ok, has_reduced_clock = false, is_sdvo = false;
  4257. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  4258. struct intel_encoder *has_edp_encoder = NULL;
  4259. struct drm_mode_config *mode_config = &dev->mode_config;
  4260. struct intel_encoder *encoder;
  4261. const intel_limit_t *limit;
  4262. int ret;
  4263. struct fdi_m_n m_n = {0};
  4264. u32 temp;
  4265. u32 lvds_sync = 0;
  4266. int target_clock, pixel_multiplier, lane, link_bw, factor;
  4267. unsigned int pipe_bpp;
  4268. bool dither;
  4269. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  4270. if (encoder->base.crtc != crtc)
  4271. continue;
  4272. switch (encoder->type) {
  4273. case INTEL_OUTPUT_LVDS:
  4274. is_lvds = true;
  4275. break;
  4276. case INTEL_OUTPUT_SDVO:
  4277. case INTEL_OUTPUT_HDMI:
  4278. is_sdvo = true;
  4279. if (encoder->needs_tv_clock)
  4280. is_tv = true;
  4281. break;
  4282. case INTEL_OUTPUT_TVOUT:
  4283. is_tv = true;
  4284. break;
  4285. case INTEL_OUTPUT_ANALOG:
  4286. is_crt = true;
  4287. break;
  4288. case INTEL_OUTPUT_DISPLAYPORT:
  4289. is_dp = true;
  4290. break;
  4291. case INTEL_OUTPUT_EDP:
  4292. has_edp_encoder = encoder;
  4293. break;
  4294. }
  4295. num_connectors++;
  4296. }
  4297. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4298. refclk = dev_priv->lvds_ssc_freq * 1000;
  4299. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  4300. refclk / 1000);
  4301. } else {
  4302. refclk = 96000;
  4303. if (!has_edp_encoder ||
  4304. intel_encoder_is_pch_edp(&has_edp_encoder->base))
  4305. refclk = 120000; /* 120Mhz refclk */
  4306. }
  4307. /*
  4308. * Returns a set of divisors for the desired target clock with the given
  4309. * refclk, or FALSE. The returned values represent the clock equation:
  4310. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  4311. */
  4312. limit = intel_limit(crtc, refclk);
  4313. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  4314. if (!ok) {
  4315. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4316. return -EINVAL;
  4317. }
  4318. /* Ensure that the cursor is valid for the new mode before changing... */
  4319. intel_crtc_update_cursor(crtc, true);
  4320. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4321. has_reduced_clock = limit->find_pll(limit, crtc,
  4322. dev_priv->lvds_downclock,
  4323. refclk,
  4324. &reduced_clock);
  4325. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  4326. /*
  4327. * If the different P is found, it means that we can't
  4328. * switch the display clock by using the FP0/FP1.
  4329. * In such case we will disable the LVDS downclock
  4330. * feature.
  4331. */
  4332. DRM_DEBUG_KMS("Different P is found for "
  4333. "LVDS clock/downclock\n");
  4334. has_reduced_clock = 0;
  4335. }
  4336. }
  4337. /* SDVO TV has fixed PLL values depend on its clock range,
  4338. this mirrors vbios setting. */
  4339. if (is_sdvo && is_tv) {
  4340. if (adjusted_mode->clock >= 100000
  4341. && adjusted_mode->clock < 140500) {
  4342. clock.p1 = 2;
  4343. clock.p2 = 10;
  4344. clock.n = 3;
  4345. clock.m1 = 16;
  4346. clock.m2 = 8;
  4347. } else if (adjusted_mode->clock >= 140500
  4348. && adjusted_mode->clock <= 200000) {
  4349. clock.p1 = 1;
  4350. clock.p2 = 10;
  4351. clock.n = 6;
  4352. clock.m1 = 12;
  4353. clock.m2 = 8;
  4354. }
  4355. }
  4356. /* FDI link */
  4357. pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4358. lane = 0;
  4359. /* CPU eDP doesn't require FDI link, so just set DP M/N
  4360. according to current link config */
  4361. if (has_edp_encoder &&
  4362. !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4363. target_clock = mode->clock;
  4364. intel_edp_link_config(has_edp_encoder,
  4365. &lane, &link_bw);
  4366. } else {
  4367. /* [e]DP over FDI requires target mode clock
  4368. instead of link clock */
  4369. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  4370. target_clock = mode->clock;
  4371. else
  4372. target_clock = adjusted_mode->clock;
  4373. /* FDI is a binary signal running at ~2.7GHz, encoding
  4374. * each output octet as 10 bits. The actual frequency
  4375. * is stored as a divider into a 100MHz clock, and the
  4376. * mode pixel clock is stored in units of 1KHz.
  4377. * Hence the bw of each lane in terms of the mode signal
  4378. * is:
  4379. */
  4380. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4381. }
  4382. /* determine panel color depth */
  4383. temp = I915_READ(PIPECONF(pipe));
  4384. temp &= ~PIPE_BPC_MASK;
  4385. dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp);
  4386. switch (pipe_bpp) {
  4387. case 18:
  4388. temp |= PIPE_6BPC;
  4389. break;
  4390. case 24:
  4391. temp |= PIPE_8BPC;
  4392. break;
  4393. case 30:
  4394. temp |= PIPE_10BPC;
  4395. break;
  4396. case 36:
  4397. temp |= PIPE_12BPC;
  4398. break;
  4399. default:
  4400. WARN(1, "intel_choose_pipe_bpp returned invalid value\n");
  4401. temp |= PIPE_8BPC;
  4402. pipe_bpp = 24;
  4403. break;
  4404. }
  4405. intel_crtc->bpp = pipe_bpp;
  4406. I915_WRITE(PIPECONF(pipe), temp);
  4407. if (!lane) {
  4408. /*
  4409. * Account for spread spectrum to avoid
  4410. * oversubscribing the link. Max center spread
  4411. * is 2.5%; use 5% for safety's sake.
  4412. */
  4413. u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
  4414. lane = bps / (link_bw * 8) + 1;
  4415. }
  4416. intel_crtc->fdi_lanes = lane;
  4417. if (pixel_multiplier > 1)
  4418. link_bw *= pixel_multiplier;
  4419. ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
  4420. &m_n);
  4421. /* Ironlake: try to setup display ref clock before DPLL
  4422. * enabling. This is only under driver's control after
  4423. * PCH B stepping, previous chipset stepping should be
  4424. * ignoring this setting.
  4425. */
  4426. temp = I915_READ(PCH_DREF_CONTROL);
  4427. /* Always enable nonspread source */
  4428. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  4429. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  4430. temp &= ~DREF_SSC_SOURCE_MASK;
  4431. temp |= DREF_SSC_SOURCE_ENABLE;
  4432. I915_WRITE(PCH_DREF_CONTROL, temp);
  4433. POSTING_READ(PCH_DREF_CONTROL);
  4434. udelay(200);
  4435. if (has_edp_encoder) {
  4436. if (intel_panel_use_ssc(dev_priv)) {
  4437. temp |= DREF_SSC1_ENABLE;
  4438. I915_WRITE(PCH_DREF_CONTROL, temp);
  4439. POSTING_READ(PCH_DREF_CONTROL);
  4440. udelay(200);
  4441. }
  4442. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4443. /* Enable CPU source on CPU attached eDP */
  4444. if (!intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4445. if (intel_panel_use_ssc(dev_priv))
  4446. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  4447. else
  4448. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  4449. } else {
  4450. /* Enable SSC on PCH eDP if needed */
  4451. if (intel_panel_use_ssc(dev_priv)) {
  4452. DRM_ERROR("enabling SSC on PCH\n");
  4453. temp |= DREF_SUPERSPREAD_SOURCE_ENABLE;
  4454. }
  4455. }
  4456. I915_WRITE(PCH_DREF_CONTROL, temp);
  4457. POSTING_READ(PCH_DREF_CONTROL);
  4458. udelay(200);
  4459. }
  4460. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  4461. if (has_reduced_clock)
  4462. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  4463. reduced_clock.m2;
  4464. /* Enable autotuning of the PLL clock (if permissible) */
  4465. factor = 21;
  4466. if (is_lvds) {
  4467. if ((intel_panel_use_ssc(dev_priv) &&
  4468. dev_priv->lvds_ssc_freq == 100) ||
  4469. (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
  4470. factor = 25;
  4471. } else if (is_sdvo && is_tv)
  4472. factor = 20;
  4473. if (clock.m1 < factor * clock.n)
  4474. fp |= FP_CB_TUNE;
  4475. dpll = 0;
  4476. if (is_lvds)
  4477. dpll |= DPLLB_MODE_LVDS;
  4478. else
  4479. dpll |= DPLLB_MODE_DAC_SERIAL;
  4480. if (is_sdvo) {
  4481. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4482. if (pixel_multiplier > 1) {
  4483. dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  4484. }
  4485. dpll |= DPLL_DVO_HIGH_SPEED;
  4486. }
  4487. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  4488. dpll |= DPLL_DVO_HIGH_SPEED;
  4489. /* compute bitmask from p1 value */
  4490. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4491. /* also FPA1 */
  4492. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4493. switch (clock.p2) {
  4494. case 5:
  4495. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4496. break;
  4497. case 7:
  4498. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4499. break;
  4500. case 10:
  4501. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4502. break;
  4503. case 14:
  4504. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4505. break;
  4506. }
  4507. if (is_sdvo && is_tv)
  4508. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4509. else if (is_tv)
  4510. /* XXX: just matching BIOS for now */
  4511. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  4512. dpll |= 3;
  4513. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4514. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4515. else
  4516. dpll |= PLL_REF_INPUT_DREFCLK;
  4517. /* setup pipeconf */
  4518. pipeconf = I915_READ(PIPECONF(pipe));
  4519. /* Set up the display plane register */
  4520. dspcntr = DISPPLANE_GAMMA_ENABLE;
  4521. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  4522. drm_mode_debug_printmodeline(mode);
  4523. /* PCH eDP needs FDI, but CPU eDP does not */
  4524. if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4525. I915_WRITE(PCH_FP0(pipe), fp);
  4526. I915_WRITE(PCH_DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  4527. POSTING_READ(PCH_DPLL(pipe));
  4528. udelay(150);
  4529. }
  4530. /* enable transcoder DPLL */
  4531. if (HAS_PCH_CPT(dev)) {
  4532. temp = I915_READ(PCH_DPLL_SEL);
  4533. switch (pipe) {
  4534. case 0:
  4535. temp |= TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL;
  4536. break;
  4537. case 1:
  4538. temp |= TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL;
  4539. break;
  4540. case 2:
  4541. /* FIXME: manage transcoder PLLs? */
  4542. temp |= TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL;
  4543. break;
  4544. default:
  4545. BUG();
  4546. }
  4547. I915_WRITE(PCH_DPLL_SEL, temp);
  4548. POSTING_READ(PCH_DPLL_SEL);
  4549. udelay(150);
  4550. }
  4551. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  4552. * This is an exception to the general rule that mode_set doesn't turn
  4553. * things on.
  4554. */
  4555. if (is_lvds) {
  4556. temp = I915_READ(PCH_LVDS);
  4557. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  4558. if (pipe == 1) {
  4559. if (HAS_PCH_CPT(dev))
  4560. temp |= PORT_TRANS_B_SEL_CPT;
  4561. else
  4562. temp |= LVDS_PIPEB_SELECT;
  4563. } else {
  4564. if (HAS_PCH_CPT(dev))
  4565. temp &= ~PORT_TRANS_SEL_MASK;
  4566. else
  4567. temp &= ~LVDS_PIPEB_SELECT;
  4568. }
  4569. /* set the corresponsding LVDS_BORDER bit */
  4570. temp |= dev_priv->lvds_border_bits;
  4571. /* Set the B0-B3 data pairs corresponding to whether we're going to
  4572. * set the DPLLs for dual-channel mode or not.
  4573. */
  4574. if (clock.p2 == 7)
  4575. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  4576. else
  4577. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  4578. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  4579. * appropriately here, but we need to look more thoroughly into how
  4580. * panels behave in the two modes.
  4581. */
  4582. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  4583. lvds_sync |= LVDS_HSYNC_POLARITY;
  4584. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  4585. lvds_sync |= LVDS_VSYNC_POLARITY;
  4586. if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
  4587. != lvds_sync) {
  4588. char flags[2] = "-+";
  4589. DRM_INFO("Changing LVDS panel from "
  4590. "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
  4591. flags[!(temp & LVDS_HSYNC_POLARITY)],
  4592. flags[!(temp & LVDS_VSYNC_POLARITY)],
  4593. flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
  4594. flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
  4595. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  4596. temp |= lvds_sync;
  4597. }
  4598. I915_WRITE(PCH_LVDS, temp);
  4599. }
  4600. pipeconf &= ~PIPECONF_DITHER_EN;
  4601. pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
  4602. if ((is_lvds && dev_priv->lvds_dither) || dither) {
  4603. pipeconf |= PIPECONF_DITHER_EN;
  4604. pipeconf |= PIPECONF_DITHER_TYPE_ST1;
  4605. }
  4606. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4607. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  4608. } else {
  4609. /* For non-DP output, clear any trans DP clock recovery setting.*/
  4610. I915_WRITE(TRANSDATA_M1(pipe), 0);
  4611. I915_WRITE(TRANSDATA_N1(pipe), 0);
  4612. I915_WRITE(TRANSDPLINK_M1(pipe), 0);
  4613. I915_WRITE(TRANSDPLINK_N1(pipe), 0);
  4614. }
  4615. if (!has_edp_encoder ||
  4616. intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4617. I915_WRITE(PCH_DPLL(pipe), dpll);
  4618. /* Wait for the clocks to stabilize. */
  4619. POSTING_READ(PCH_DPLL(pipe));
  4620. udelay(150);
  4621. /* The pixel multiplier can only be updated once the
  4622. * DPLL is enabled and the clocks are stable.
  4623. *
  4624. * So write it again.
  4625. */
  4626. I915_WRITE(PCH_DPLL(pipe), dpll);
  4627. }
  4628. intel_crtc->lowfreq_avail = false;
  4629. if (is_lvds && has_reduced_clock && i915_powersave) {
  4630. I915_WRITE(PCH_FP1(pipe), fp2);
  4631. intel_crtc->lowfreq_avail = true;
  4632. if (HAS_PIPE_CXSR(dev)) {
  4633. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4634. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4635. }
  4636. } else {
  4637. I915_WRITE(PCH_FP1(pipe), fp);
  4638. if (HAS_PIPE_CXSR(dev)) {
  4639. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4640. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  4641. }
  4642. }
  4643. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4644. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4645. /* the chip adds 2 halflines automatically */
  4646. adjusted_mode->crtc_vdisplay -= 1;
  4647. adjusted_mode->crtc_vtotal -= 1;
  4648. adjusted_mode->crtc_vblank_start -= 1;
  4649. adjusted_mode->crtc_vblank_end -= 1;
  4650. adjusted_mode->crtc_vsync_end -= 1;
  4651. adjusted_mode->crtc_vsync_start -= 1;
  4652. } else
  4653. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  4654. I915_WRITE(HTOTAL(pipe),
  4655. (adjusted_mode->crtc_hdisplay - 1) |
  4656. ((adjusted_mode->crtc_htotal - 1) << 16));
  4657. I915_WRITE(HBLANK(pipe),
  4658. (adjusted_mode->crtc_hblank_start - 1) |
  4659. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4660. I915_WRITE(HSYNC(pipe),
  4661. (adjusted_mode->crtc_hsync_start - 1) |
  4662. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4663. I915_WRITE(VTOTAL(pipe),
  4664. (adjusted_mode->crtc_vdisplay - 1) |
  4665. ((adjusted_mode->crtc_vtotal - 1) << 16));
  4666. I915_WRITE(VBLANK(pipe),
  4667. (adjusted_mode->crtc_vblank_start - 1) |
  4668. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  4669. I915_WRITE(VSYNC(pipe),
  4670. (adjusted_mode->crtc_vsync_start - 1) |
  4671. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4672. /* pipesrc controls the size that is scaled from, which should
  4673. * always be the user's requested size.
  4674. */
  4675. I915_WRITE(PIPESRC(pipe),
  4676. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  4677. I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
  4678. I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
  4679. I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
  4680. I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
  4681. if (has_edp_encoder &&
  4682. !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4683. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  4684. }
  4685. I915_WRITE(PIPECONF(pipe), pipeconf);
  4686. POSTING_READ(PIPECONF(pipe));
  4687. intel_wait_for_vblank(dev, pipe);
  4688. if (IS_GEN5(dev)) {
  4689. /* enable address swizzle for tiling buffer */
  4690. temp = I915_READ(DISP_ARB_CTL);
  4691. I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
  4692. }
  4693. I915_WRITE(DSPCNTR(plane), dspcntr);
  4694. POSTING_READ(DSPCNTR(plane));
  4695. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  4696. intel_update_watermarks(dev);
  4697. return ret;
  4698. }
  4699. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  4700. struct drm_display_mode *mode,
  4701. struct drm_display_mode *adjusted_mode,
  4702. int x, int y,
  4703. struct drm_framebuffer *old_fb)
  4704. {
  4705. struct drm_device *dev = crtc->dev;
  4706. struct drm_i915_private *dev_priv = dev->dev_private;
  4707. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4708. int pipe = intel_crtc->pipe;
  4709. int ret;
  4710. drm_vblank_pre_modeset(dev, pipe);
  4711. ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
  4712. x, y, old_fb);
  4713. drm_vblank_post_modeset(dev, pipe);
  4714. return ret;
  4715. }
  4716. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  4717. void intel_crtc_load_lut(struct drm_crtc *crtc)
  4718. {
  4719. struct drm_device *dev = crtc->dev;
  4720. struct drm_i915_private *dev_priv = dev->dev_private;
  4721. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4722. int palreg = PALETTE(intel_crtc->pipe);
  4723. int i;
  4724. /* The clocks have to be on to load the palette. */
  4725. if (!crtc->enabled)
  4726. return;
  4727. /* use legacy palette for Ironlake */
  4728. if (HAS_PCH_SPLIT(dev))
  4729. palreg = LGC_PALETTE(intel_crtc->pipe);
  4730. for (i = 0; i < 256; i++) {
  4731. I915_WRITE(palreg + 4 * i,
  4732. (intel_crtc->lut_r[i] << 16) |
  4733. (intel_crtc->lut_g[i] << 8) |
  4734. intel_crtc->lut_b[i]);
  4735. }
  4736. }
  4737. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  4738. {
  4739. struct drm_device *dev = crtc->dev;
  4740. struct drm_i915_private *dev_priv = dev->dev_private;
  4741. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4742. bool visible = base != 0;
  4743. u32 cntl;
  4744. if (intel_crtc->cursor_visible == visible)
  4745. return;
  4746. cntl = I915_READ(_CURACNTR);
  4747. if (visible) {
  4748. /* On these chipsets we can only modify the base whilst
  4749. * the cursor is disabled.
  4750. */
  4751. I915_WRITE(_CURABASE, base);
  4752. cntl &= ~(CURSOR_FORMAT_MASK);
  4753. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  4754. cntl |= CURSOR_ENABLE |
  4755. CURSOR_GAMMA_ENABLE |
  4756. CURSOR_FORMAT_ARGB;
  4757. } else
  4758. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  4759. I915_WRITE(_CURACNTR, cntl);
  4760. intel_crtc->cursor_visible = visible;
  4761. }
  4762. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  4763. {
  4764. struct drm_device *dev = crtc->dev;
  4765. struct drm_i915_private *dev_priv = dev->dev_private;
  4766. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4767. int pipe = intel_crtc->pipe;
  4768. bool visible = base != 0;
  4769. if (intel_crtc->cursor_visible != visible) {
  4770. uint32_t cntl = I915_READ(CURCNTR(pipe));
  4771. if (base) {
  4772. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  4773. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  4774. cntl |= pipe << 28; /* Connect to correct pipe */
  4775. } else {
  4776. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  4777. cntl |= CURSOR_MODE_DISABLE;
  4778. }
  4779. I915_WRITE(CURCNTR(pipe), cntl);
  4780. intel_crtc->cursor_visible = visible;
  4781. }
  4782. /* and commit changes on next vblank */
  4783. I915_WRITE(CURBASE(pipe), base);
  4784. }
  4785. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  4786. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  4787. bool on)
  4788. {
  4789. struct drm_device *dev = crtc->dev;
  4790. struct drm_i915_private *dev_priv = dev->dev_private;
  4791. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4792. int pipe = intel_crtc->pipe;
  4793. int x = intel_crtc->cursor_x;
  4794. int y = intel_crtc->cursor_y;
  4795. u32 base, pos;
  4796. bool visible;
  4797. pos = 0;
  4798. if (on && crtc->enabled && crtc->fb) {
  4799. base = intel_crtc->cursor_addr;
  4800. if (x > (int) crtc->fb->width)
  4801. base = 0;
  4802. if (y > (int) crtc->fb->height)
  4803. base = 0;
  4804. } else
  4805. base = 0;
  4806. if (x < 0) {
  4807. if (x + intel_crtc->cursor_width < 0)
  4808. base = 0;
  4809. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  4810. x = -x;
  4811. }
  4812. pos |= x << CURSOR_X_SHIFT;
  4813. if (y < 0) {
  4814. if (y + intel_crtc->cursor_height < 0)
  4815. base = 0;
  4816. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  4817. y = -y;
  4818. }
  4819. pos |= y << CURSOR_Y_SHIFT;
  4820. visible = base != 0;
  4821. if (!visible && !intel_crtc->cursor_visible)
  4822. return;
  4823. I915_WRITE(CURPOS(pipe), pos);
  4824. if (IS_845G(dev) || IS_I865G(dev))
  4825. i845_update_cursor(crtc, base);
  4826. else
  4827. i9xx_update_cursor(crtc, base);
  4828. if (visible)
  4829. intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
  4830. }
  4831. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  4832. struct drm_file *file,
  4833. uint32_t handle,
  4834. uint32_t width, uint32_t height)
  4835. {
  4836. struct drm_device *dev = crtc->dev;
  4837. struct drm_i915_private *dev_priv = dev->dev_private;
  4838. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4839. struct drm_i915_gem_object *obj;
  4840. uint32_t addr;
  4841. int ret;
  4842. DRM_DEBUG_KMS("\n");
  4843. /* if we want to turn off the cursor ignore width and height */
  4844. if (!handle) {
  4845. DRM_DEBUG_KMS("cursor off\n");
  4846. addr = 0;
  4847. obj = NULL;
  4848. mutex_lock(&dev->struct_mutex);
  4849. goto finish;
  4850. }
  4851. /* Currently we only support 64x64 cursors */
  4852. if (width != 64 || height != 64) {
  4853. DRM_ERROR("we currently only support 64x64 cursors\n");
  4854. return -EINVAL;
  4855. }
  4856. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  4857. if (&obj->base == NULL)
  4858. return -ENOENT;
  4859. if (obj->base.size < width * height * 4) {
  4860. DRM_ERROR("buffer is to small\n");
  4861. ret = -ENOMEM;
  4862. goto fail;
  4863. }
  4864. /* we only need to pin inside GTT if cursor is non-phy */
  4865. mutex_lock(&dev->struct_mutex);
  4866. if (!dev_priv->info->cursor_needs_physical) {
  4867. if (obj->tiling_mode) {
  4868. DRM_ERROR("cursor cannot be tiled\n");
  4869. ret = -EINVAL;
  4870. goto fail_locked;
  4871. }
  4872. ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
  4873. if (ret) {
  4874. DRM_ERROR("failed to move cursor bo into the GTT\n");
  4875. goto fail_locked;
  4876. }
  4877. ret = i915_gem_object_put_fence(obj);
  4878. if (ret) {
  4879. DRM_ERROR("failed to release fence for cursor");
  4880. goto fail_unpin;
  4881. }
  4882. addr = obj->gtt_offset;
  4883. } else {
  4884. int align = IS_I830(dev) ? 16 * 1024 : 256;
  4885. ret = i915_gem_attach_phys_object(dev, obj,
  4886. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  4887. align);
  4888. if (ret) {
  4889. DRM_ERROR("failed to attach phys object\n");
  4890. goto fail_locked;
  4891. }
  4892. addr = obj->phys_obj->handle->busaddr;
  4893. }
  4894. if (IS_GEN2(dev))
  4895. I915_WRITE(CURSIZE, (height << 12) | width);
  4896. finish:
  4897. if (intel_crtc->cursor_bo) {
  4898. if (dev_priv->info->cursor_needs_physical) {
  4899. if (intel_crtc->cursor_bo != obj)
  4900. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  4901. } else
  4902. i915_gem_object_unpin(intel_crtc->cursor_bo);
  4903. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  4904. }
  4905. mutex_unlock(&dev->struct_mutex);
  4906. intel_crtc->cursor_addr = addr;
  4907. intel_crtc->cursor_bo = obj;
  4908. intel_crtc->cursor_width = width;
  4909. intel_crtc->cursor_height = height;
  4910. intel_crtc_update_cursor(crtc, true);
  4911. return 0;
  4912. fail_unpin:
  4913. i915_gem_object_unpin(obj);
  4914. fail_locked:
  4915. mutex_unlock(&dev->struct_mutex);
  4916. fail:
  4917. drm_gem_object_unreference_unlocked(&obj->base);
  4918. return ret;
  4919. }
  4920. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  4921. {
  4922. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4923. intel_crtc->cursor_x = x;
  4924. intel_crtc->cursor_y = y;
  4925. intel_crtc_update_cursor(crtc, true);
  4926. return 0;
  4927. }
  4928. /** Sets the color ramps on behalf of RandR */
  4929. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  4930. u16 blue, int regno)
  4931. {
  4932. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4933. intel_crtc->lut_r[regno] = red >> 8;
  4934. intel_crtc->lut_g[regno] = green >> 8;
  4935. intel_crtc->lut_b[regno] = blue >> 8;
  4936. }
  4937. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  4938. u16 *blue, int regno)
  4939. {
  4940. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4941. *red = intel_crtc->lut_r[regno] << 8;
  4942. *green = intel_crtc->lut_g[regno] << 8;
  4943. *blue = intel_crtc->lut_b[regno] << 8;
  4944. }
  4945. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  4946. u16 *blue, uint32_t start, uint32_t size)
  4947. {
  4948. int end = (start + size > 256) ? 256 : start + size, i;
  4949. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4950. for (i = start; i < end; i++) {
  4951. intel_crtc->lut_r[i] = red[i] >> 8;
  4952. intel_crtc->lut_g[i] = green[i] >> 8;
  4953. intel_crtc->lut_b[i] = blue[i] >> 8;
  4954. }
  4955. intel_crtc_load_lut(crtc);
  4956. }
  4957. /**
  4958. * Get a pipe with a simple mode set on it for doing load-based monitor
  4959. * detection.
  4960. *
  4961. * It will be up to the load-detect code to adjust the pipe as appropriate for
  4962. * its requirements. The pipe will be connected to no other encoders.
  4963. *
  4964. * Currently this code will only succeed if there is a pipe with no encoders
  4965. * configured for it. In the future, it could choose to temporarily disable
  4966. * some outputs to free up a pipe for its use.
  4967. *
  4968. * \return crtc, or NULL if no pipes are available.
  4969. */
  4970. /* VESA 640x480x72Hz mode to set on the pipe */
  4971. static struct drm_display_mode load_detect_mode = {
  4972. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  4973. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  4974. };
  4975. static struct drm_framebuffer *
  4976. intel_framebuffer_create(struct drm_device *dev,
  4977. struct drm_mode_fb_cmd *mode_cmd,
  4978. struct drm_i915_gem_object *obj)
  4979. {
  4980. struct intel_framebuffer *intel_fb;
  4981. int ret;
  4982. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  4983. if (!intel_fb) {
  4984. drm_gem_object_unreference_unlocked(&obj->base);
  4985. return ERR_PTR(-ENOMEM);
  4986. }
  4987. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  4988. if (ret) {
  4989. drm_gem_object_unreference_unlocked(&obj->base);
  4990. kfree(intel_fb);
  4991. return ERR_PTR(ret);
  4992. }
  4993. return &intel_fb->base;
  4994. }
  4995. static u32
  4996. intel_framebuffer_pitch_for_width(int width, int bpp)
  4997. {
  4998. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  4999. return ALIGN(pitch, 64);
  5000. }
  5001. static u32
  5002. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  5003. {
  5004. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  5005. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  5006. }
  5007. static struct drm_framebuffer *
  5008. intel_framebuffer_create_for_mode(struct drm_device *dev,
  5009. struct drm_display_mode *mode,
  5010. int depth, int bpp)
  5011. {
  5012. struct drm_i915_gem_object *obj;
  5013. struct drm_mode_fb_cmd mode_cmd;
  5014. obj = i915_gem_alloc_object(dev,
  5015. intel_framebuffer_size_for_mode(mode, bpp));
  5016. if (obj == NULL)
  5017. return ERR_PTR(-ENOMEM);
  5018. mode_cmd.width = mode->hdisplay;
  5019. mode_cmd.height = mode->vdisplay;
  5020. mode_cmd.depth = depth;
  5021. mode_cmd.bpp = bpp;
  5022. mode_cmd.pitch = intel_framebuffer_pitch_for_width(mode_cmd.width, bpp);
  5023. return intel_framebuffer_create(dev, &mode_cmd, obj);
  5024. }
  5025. static struct drm_framebuffer *
  5026. mode_fits_in_fbdev(struct drm_device *dev,
  5027. struct drm_display_mode *mode)
  5028. {
  5029. struct drm_i915_private *dev_priv = dev->dev_private;
  5030. struct drm_i915_gem_object *obj;
  5031. struct drm_framebuffer *fb;
  5032. if (dev_priv->fbdev == NULL)
  5033. return NULL;
  5034. obj = dev_priv->fbdev->ifb.obj;
  5035. if (obj == NULL)
  5036. return NULL;
  5037. fb = &dev_priv->fbdev->ifb.base;
  5038. if (fb->pitch < intel_framebuffer_pitch_for_width(mode->hdisplay,
  5039. fb->bits_per_pixel))
  5040. return NULL;
  5041. if (obj->base.size < mode->vdisplay * fb->pitch)
  5042. return NULL;
  5043. return fb;
  5044. }
  5045. bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  5046. struct drm_connector *connector,
  5047. struct drm_display_mode *mode,
  5048. struct intel_load_detect_pipe *old)
  5049. {
  5050. struct intel_crtc *intel_crtc;
  5051. struct drm_crtc *possible_crtc;
  5052. struct drm_encoder *encoder = &intel_encoder->base;
  5053. struct drm_crtc *crtc = NULL;
  5054. struct drm_device *dev = encoder->dev;
  5055. struct drm_framebuffer *old_fb;
  5056. int i = -1;
  5057. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5058. connector->base.id, drm_get_connector_name(connector),
  5059. encoder->base.id, drm_get_encoder_name(encoder));
  5060. /*
  5061. * Algorithm gets a little messy:
  5062. *
  5063. * - if the connector already has an assigned crtc, use it (but make
  5064. * sure it's on first)
  5065. *
  5066. * - try to find the first unused crtc that can drive this connector,
  5067. * and use that if we find one
  5068. */
  5069. /* See if we already have a CRTC for this connector */
  5070. if (encoder->crtc) {
  5071. crtc = encoder->crtc;
  5072. intel_crtc = to_intel_crtc(crtc);
  5073. old->dpms_mode = intel_crtc->dpms_mode;
  5074. old->load_detect_temp = false;
  5075. /* Make sure the crtc and connector are running */
  5076. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  5077. struct drm_encoder_helper_funcs *encoder_funcs;
  5078. struct drm_crtc_helper_funcs *crtc_funcs;
  5079. crtc_funcs = crtc->helper_private;
  5080. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  5081. encoder_funcs = encoder->helper_private;
  5082. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  5083. }
  5084. return true;
  5085. }
  5086. /* Find an unused one (if possible) */
  5087. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  5088. i++;
  5089. if (!(encoder->possible_crtcs & (1 << i)))
  5090. continue;
  5091. if (!possible_crtc->enabled) {
  5092. crtc = possible_crtc;
  5093. break;
  5094. }
  5095. }
  5096. /*
  5097. * If we didn't find an unused CRTC, don't use any.
  5098. */
  5099. if (!crtc) {
  5100. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  5101. return false;
  5102. }
  5103. encoder->crtc = crtc;
  5104. connector->encoder = encoder;
  5105. intel_crtc = to_intel_crtc(crtc);
  5106. old->dpms_mode = intel_crtc->dpms_mode;
  5107. old->load_detect_temp = true;
  5108. old->release_fb = NULL;
  5109. if (!mode)
  5110. mode = &load_detect_mode;
  5111. old_fb = crtc->fb;
  5112. /* We need a framebuffer large enough to accommodate all accesses
  5113. * that the plane may generate whilst we perform load detection.
  5114. * We can not rely on the fbcon either being present (we get called
  5115. * during its initialisation to detect all boot displays, or it may
  5116. * not even exist) or that it is large enough to satisfy the
  5117. * requested mode.
  5118. */
  5119. crtc->fb = mode_fits_in_fbdev(dev, mode);
  5120. if (crtc->fb == NULL) {
  5121. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  5122. crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  5123. old->release_fb = crtc->fb;
  5124. } else
  5125. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  5126. if (IS_ERR(crtc->fb)) {
  5127. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  5128. crtc->fb = old_fb;
  5129. return false;
  5130. }
  5131. if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
  5132. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  5133. if (old->release_fb)
  5134. old->release_fb->funcs->destroy(old->release_fb);
  5135. crtc->fb = old_fb;
  5136. return false;
  5137. }
  5138. /* let the connector get through one full cycle before testing */
  5139. intel_wait_for_vblank(dev, intel_crtc->pipe);
  5140. return true;
  5141. }
  5142. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  5143. struct drm_connector *connector,
  5144. struct intel_load_detect_pipe *old)
  5145. {
  5146. struct drm_encoder *encoder = &intel_encoder->base;
  5147. struct drm_device *dev = encoder->dev;
  5148. struct drm_crtc *crtc = encoder->crtc;
  5149. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  5150. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  5151. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5152. connector->base.id, drm_get_connector_name(connector),
  5153. encoder->base.id, drm_get_encoder_name(encoder));
  5154. if (old->load_detect_temp) {
  5155. connector->encoder = NULL;
  5156. drm_helper_disable_unused_functions(dev);
  5157. if (old->release_fb)
  5158. old->release_fb->funcs->destroy(old->release_fb);
  5159. return;
  5160. }
  5161. /* Switch crtc and encoder back off if necessary */
  5162. if (old->dpms_mode != DRM_MODE_DPMS_ON) {
  5163. encoder_funcs->dpms(encoder, old->dpms_mode);
  5164. crtc_funcs->dpms(crtc, old->dpms_mode);
  5165. }
  5166. }
  5167. /* Returns the clock of the currently programmed mode of the given pipe. */
  5168. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  5169. {
  5170. struct drm_i915_private *dev_priv = dev->dev_private;
  5171. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5172. int pipe = intel_crtc->pipe;
  5173. u32 dpll = I915_READ(DPLL(pipe));
  5174. u32 fp;
  5175. intel_clock_t clock;
  5176. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  5177. fp = I915_READ(FP0(pipe));
  5178. else
  5179. fp = I915_READ(FP1(pipe));
  5180. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  5181. if (IS_PINEVIEW(dev)) {
  5182. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  5183. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  5184. } else {
  5185. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  5186. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  5187. }
  5188. if (!IS_GEN2(dev)) {
  5189. if (IS_PINEVIEW(dev))
  5190. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  5191. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  5192. else
  5193. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  5194. DPLL_FPA01_P1_POST_DIV_SHIFT);
  5195. switch (dpll & DPLL_MODE_MASK) {
  5196. case DPLLB_MODE_DAC_SERIAL:
  5197. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  5198. 5 : 10;
  5199. break;
  5200. case DPLLB_MODE_LVDS:
  5201. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  5202. 7 : 14;
  5203. break;
  5204. default:
  5205. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  5206. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  5207. return 0;
  5208. }
  5209. /* XXX: Handle the 100Mhz refclk */
  5210. intel_clock(dev, 96000, &clock);
  5211. } else {
  5212. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  5213. if (is_lvds) {
  5214. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  5215. DPLL_FPA01_P1_POST_DIV_SHIFT);
  5216. clock.p2 = 14;
  5217. if ((dpll & PLL_REF_INPUT_MASK) ==
  5218. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  5219. /* XXX: might not be 66MHz */
  5220. intel_clock(dev, 66000, &clock);
  5221. } else
  5222. intel_clock(dev, 48000, &clock);
  5223. } else {
  5224. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  5225. clock.p1 = 2;
  5226. else {
  5227. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  5228. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  5229. }
  5230. if (dpll & PLL_P2_DIVIDE_BY_4)
  5231. clock.p2 = 4;
  5232. else
  5233. clock.p2 = 2;
  5234. intel_clock(dev, 48000, &clock);
  5235. }
  5236. }
  5237. /* XXX: It would be nice to validate the clocks, but we can't reuse
  5238. * i830PllIsValid() because it relies on the xf86_config connector
  5239. * configuration being accurate, which it isn't necessarily.
  5240. */
  5241. return clock.dot;
  5242. }
  5243. /** Returns the currently programmed mode of the given pipe. */
  5244. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  5245. struct drm_crtc *crtc)
  5246. {
  5247. struct drm_i915_private *dev_priv = dev->dev_private;
  5248. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5249. int pipe = intel_crtc->pipe;
  5250. struct drm_display_mode *mode;
  5251. int htot = I915_READ(HTOTAL(pipe));
  5252. int hsync = I915_READ(HSYNC(pipe));
  5253. int vtot = I915_READ(VTOTAL(pipe));
  5254. int vsync = I915_READ(VSYNC(pipe));
  5255. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  5256. if (!mode)
  5257. return NULL;
  5258. mode->clock = intel_crtc_clock_get(dev, crtc);
  5259. mode->hdisplay = (htot & 0xffff) + 1;
  5260. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  5261. mode->hsync_start = (hsync & 0xffff) + 1;
  5262. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  5263. mode->vdisplay = (vtot & 0xffff) + 1;
  5264. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  5265. mode->vsync_start = (vsync & 0xffff) + 1;
  5266. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  5267. drm_mode_set_name(mode);
  5268. drm_mode_set_crtcinfo(mode, 0);
  5269. return mode;
  5270. }
  5271. #define GPU_IDLE_TIMEOUT 500 /* ms */
  5272. /* When this timer fires, we've been idle for awhile */
  5273. static void intel_gpu_idle_timer(unsigned long arg)
  5274. {
  5275. struct drm_device *dev = (struct drm_device *)arg;
  5276. drm_i915_private_t *dev_priv = dev->dev_private;
  5277. if (!list_empty(&dev_priv->mm.active_list)) {
  5278. /* Still processing requests, so just re-arm the timer. */
  5279. mod_timer(&dev_priv->idle_timer, jiffies +
  5280. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  5281. return;
  5282. }
  5283. dev_priv->busy = false;
  5284. queue_work(dev_priv->wq, &dev_priv->idle_work);
  5285. }
  5286. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  5287. static void intel_crtc_idle_timer(unsigned long arg)
  5288. {
  5289. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  5290. struct drm_crtc *crtc = &intel_crtc->base;
  5291. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  5292. struct intel_framebuffer *intel_fb;
  5293. intel_fb = to_intel_framebuffer(crtc->fb);
  5294. if (intel_fb && intel_fb->obj->active) {
  5295. /* The framebuffer is still being accessed by the GPU. */
  5296. mod_timer(&intel_crtc->idle_timer, jiffies +
  5297. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  5298. return;
  5299. }
  5300. intel_crtc->busy = false;
  5301. queue_work(dev_priv->wq, &dev_priv->idle_work);
  5302. }
  5303. static void intel_increase_pllclock(struct drm_crtc *crtc)
  5304. {
  5305. struct drm_device *dev = crtc->dev;
  5306. drm_i915_private_t *dev_priv = dev->dev_private;
  5307. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5308. int pipe = intel_crtc->pipe;
  5309. int dpll_reg = DPLL(pipe);
  5310. int dpll;
  5311. if (HAS_PCH_SPLIT(dev))
  5312. return;
  5313. if (!dev_priv->lvds_downclock_avail)
  5314. return;
  5315. dpll = I915_READ(dpll_reg);
  5316. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  5317. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  5318. /* Unlock panel regs */
  5319. I915_WRITE(PP_CONTROL,
  5320. I915_READ(PP_CONTROL) | PANEL_UNLOCK_REGS);
  5321. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  5322. I915_WRITE(dpll_reg, dpll);
  5323. intel_wait_for_vblank(dev, pipe);
  5324. dpll = I915_READ(dpll_reg);
  5325. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  5326. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  5327. /* ...and lock them again */
  5328. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  5329. }
  5330. /* Schedule downclock */
  5331. mod_timer(&intel_crtc->idle_timer, jiffies +
  5332. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  5333. }
  5334. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  5335. {
  5336. struct drm_device *dev = crtc->dev;
  5337. drm_i915_private_t *dev_priv = dev->dev_private;
  5338. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5339. int pipe = intel_crtc->pipe;
  5340. int dpll_reg = DPLL(pipe);
  5341. int dpll = I915_READ(dpll_reg);
  5342. if (HAS_PCH_SPLIT(dev))
  5343. return;
  5344. if (!dev_priv->lvds_downclock_avail)
  5345. return;
  5346. /*
  5347. * Since this is called by a timer, we should never get here in
  5348. * the manual case.
  5349. */
  5350. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  5351. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  5352. /* Unlock panel regs */
  5353. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  5354. PANEL_UNLOCK_REGS);
  5355. dpll |= DISPLAY_RATE_SELECT_FPA1;
  5356. I915_WRITE(dpll_reg, dpll);
  5357. intel_wait_for_vblank(dev, pipe);
  5358. dpll = I915_READ(dpll_reg);
  5359. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  5360. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  5361. /* ...and lock them again */
  5362. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  5363. }
  5364. }
  5365. /**
  5366. * intel_idle_update - adjust clocks for idleness
  5367. * @work: work struct
  5368. *
  5369. * Either the GPU or display (or both) went idle. Check the busy status
  5370. * here and adjust the CRTC and GPU clocks as necessary.
  5371. */
  5372. static void intel_idle_update(struct work_struct *work)
  5373. {
  5374. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  5375. idle_work);
  5376. struct drm_device *dev = dev_priv->dev;
  5377. struct drm_crtc *crtc;
  5378. struct intel_crtc *intel_crtc;
  5379. if (!i915_powersave)
  5380. return;
  5381. mutex_lock(&dev->struct_mutex);
  5382. i915_update_gfx_val(dev_priv);
  5383. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5384. /* Skip inactive CRTCs */
  5385. if (!crtc->fb)
  5386. continue;
  5387. intel_crtc = to_intel_crtc(crtc);
  5388. if (!intel_crtc->busy)
  5389. intel_decrease_pllclock(crtc);
  5390. }
  5391. mutex_unlock(&dev->struct_mutex);
  5392. }
  5393. /**
  5394. * intel_mark_busy - mark the GPU and possibly the display busy
  5395. * @dev: drm device
  5396. * @obj: object we're operating on
  5397. *
  5398. * Callers can use this function to indicate that the GPU is busy processing
  5399. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  5400. * buffer), we'll also mark the display as busy, so we know to increase its
  5401. * clock frequency.
  5402. */
  5403. void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
  5404. {
  5405. drm_i915_private_t *dev_priv = dev->dev_private;
  5406. struct drm_crtc *crtc = NULL;
  5407. struct intel_framebuffer *intel_fb;
  5408. struct intel_crtc *intel_crtc;
  5409. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  5410. return;
  5411. if (!dev_priv->busy)
  5412. dev_priv->busy = true;
  5413. else
  5414. mod_timer(&dev_priv->idle_timer, jiffies +
  5415. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  5416. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5417. if (!crtc->fb)
  5418. continue;
  5419. intel_crtc = to_intel_crtc(crtc);
  5420. intel_fb = to_intel_framebuffer(crtc->fb);
  5421. if (intel_fb->obj == obj) {
  5422. if (!intel_crtc->busy) {
  5423. /* Non-busy -> busy, upclock */
  5424. intel_increase_pllclock(crtc);
  5425. intel_crtc->busy = true;
  5426. } else {
  5427. /* Busy -> busy, put off timer */
  5428. mod_timer(&intel_crtc->idle_timer, jiffies +
  5429. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  5430. }
  5431. }
  5432. }
  5433. }
  5434. static void intel_crtc_destroy(struct drm_crtc *crtc)
  5435. {
  5436. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5437. struct drm_device *dev = crtc->dev;
  5438. struct intel_unpin_work *work;
  5439. unsigned long flags;
  5440. spin_lock_irqsave(&dev->event_lock, flags);
  5441. work = intel_crtc->unpin_work;
  5442. intel_crtc->unpin_work = NULL;
  5443. spin_unlock_irqrestore(&dev->event_lock, flags);
  5444. if (work) {
  5445. cancel_work_sync(&work->work);
  5446. kfree(work);
  5447. }
  5448. drm_crtc_cleanup(crtc);
  5449. kfree(intel_crtc);
  5450. }
  5451. static void intel_unpin_work_fn(struct work_struct *__work)
  5452. {
  5453. struct intel_unpin_work *work =
  5454. container_of(__work, struct intel_unpin_work, work);
  5455. mutex_lock(&work->dev->struct_mutex);
  5456. i915_gem_object_unpin(work->old_fb_obj);
  5457. drm_gem_object_unreference(&work->pending_flip_obj->base);
  5458. drm_gem_object_unreference(&work->old_fb_obj->base);
  5459. mutex_unlock(&work->dev->struct_mutex);
  5460. kfree(work);
  5461. }
  5462. static void do_intel_finish_page_flip(struct drm_device *dev,
  5463. struct drm_crtc *crtc)
  5464. {
  5465. drm_i915_private_t *dev_priv = dev->dev_private;
  5466. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5467. struct intel_unpin_work *work;
  5468. struct drm_i915_gem_object *obj;
  5469. struct drm_pending_vblank_event *e;
  5470. struct timeval tnow, tvbl;
  5471. unsigned long flags;
  5472. /* Ignore early vblank irqs */
  5473. if (intel_crtc == NULL)
  5474. return;
  5475. do_gettimeofday(&tnow);
  5476. spin_lock_irqsave(&dev->event_lock, flags);
  5477. work = intel_crtc->unpin_work;
  5478. if (work == NULL || !work->pending) {
  5479. spin_unlock_irqrestore(&dev->event_lock, flags);
  5480. return;
  5481. }
  5482. intel_crtc->unpin_work = NULL;
  5483. if (work->event) {
  5484. e = work->event;
  5485. e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
  5486. /* Called before vblank count and timestamps have
  5487. * been updated for the vblank interval of flip
  5488. * completion? Need to increment vblank count and
  5489. * add one videorefresh duration to returned timestamp
  5490. * to account for this. We assume this happened if we
  5491. * get called over 0.9 frame durations after the last
  5492. * timestamped vblank.
  5493. *
  5494. * This calculation can not be used with vrefresh rates
  5495. * below 5Hz (10Hz to be on the safe side) without
  5496. * promoting to 64 integers.
  5497. */
  5498. if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
  5499. 9 * crtc->framedur_ns) {
  5500. e->event.sequence++;
  5501. tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
  5502. crtc->framedur_ns);
  5503. }
  5504. e->event.tv_sec = tvbl.tv_sec;
  5505. e->event.tv_usec = tvbl.tv_usec;
  5506. list_add_tail(&e->base.link,
  5507. &e->base.file_priv->event_list);
  5508. wake_up_interruptible(&e->base.file_priv->event_wait);
  5509. }
  5510. drm_vblank_put(dev, intel_crtc->pipe);
  5511. spin_unlock_irqrestore(&dev->event_lock, flags);
  5512. obj = work->old_fb_obj;
  5513. atomic_clear_mask(1 << intel_crtc->plane,
  5514. &obj->pending_flip.counter);
  5515. if (atomic_read(&obj->pending_flip) == 0)
  5516. wake_up(&dev_priv->pending_flip_queue);
  5517. schedule_work(&work->work);
  5518. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  5519. }
  5520. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  5521. {
  5522. drm_i915_private_t *dev_priv = dev->dev_private;
  5523. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  5524. do_intel_finish_page_flip(dev, crtc);
  5525. }
  5526. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  5527. {
  5528. drm_i915_private_t *dev_priv = dev->dev_private;
  5529. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  5530. do_intel_finish_page_flip(dev, crtc);
  5531. }
  5532. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  5533. {
  5534. drm_i915_private_t *dev_priv = dev->dev_private;
  5535. struct intel_crtc *intel_crtc =
  5536. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  5537. unsigned long flags;
  5538. spin_lock_irqsave(&dev->event_lock, flags);
  5539. if (intel_crtc->unpin_work) {
  5540. if ((++intel_crtc->unpin_work->pending) > 1)
  5541. DRM_ERROR("Prepared flip multiple times\n");
  5542. } else {
  5543. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  5544. }
  5545. spin_unlock_irqrestore(&dev->event_lock, flags);
  5546. }
  5547. static int intel_gen2_queue_flip(struct drm_device *dev,
  5548. struct drm_crtc *crtc,
  5549. struct drm_framebuffer *fb,
  5550. struct drm_i915_gem_object *obj)
  5551. {
  5552. struct drm_i915_private *dev_priv = dev->dev_private;
  5553. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5554. unsigned long offset;
  5555. u32 flip_mask;
  5556. int ret;
  5557. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  5558. if (ret)
  5559. goto out;
  5560. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  5561. offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
  5562. ret = BEGIN_LP_RING(6);
  5563. if (ret)
  5564. goto out;
  5565. /* Can't queue multiple flips, so wait for the previous
  5566. * one to finish before executing the next.
  5567. */
  5568. if (intel_crtc->plane)
  5569. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  5570. else
  5571. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  5572. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  5573. OUT_RING(MI_NOOP);
  5574. OUT_RING(MI_DISPLAY_FLIP |
  5575. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5576. OUT_RING(fb->pitch);
  5577. OUT_RING(obj->gtt_offset + offset);
  5578. OUT_RING(MI_NOOP);
  5579. ADVANCE_LP_RING();
  5580. out:
  5581. return ret;
  5582. }
  5583. static int intel_gen3_queue_flip(struct drm_device *dev,
  5584. struct drm_crtc *crtc,
  5585. struct drm_framebuffer *fb,
  5586. struct drm_i915_gem_object *obj)
  5587. {
  5588. struct drm_i915_private *dev_priv = dev->dev_private;
  5589. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5590. unsigned long offset;
  5591. u32 flip_mask;
  5592. int ret;
  5593. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  5594. if (ret)
  5595. goto out;
  5596. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  5597. offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
  5598. ret = BEGIN_LP_RING(6);
  5599. if (ret)
  5600. goto out;
  5601. if (intel_crtc->plane)
  5602. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  5603. else
  5604. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  5605. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  5606. OUT_RING(MI_NOOP);
  5607. OUT_RING(MI_DISPLAY_FLIP_I915 |
  5608. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5609. OUT_RING(fb->pitch);
  5610. OUT_RING(obj->gtt_offset + offset);
  5611. OUT_RING(MI_NOOP);
  5612. ADVANCE_LP_RING();
  5613. out:
  5614. return ret;
  5615. }
  5616. static int intel_gen4_queue_flip(struct drm_device *dev,
  5617. struct drm_crtc *crtc,
  5618. struct drm_framebuffer *fb,
  5619. struct drm_i915_gem_object *obj)
  5620. {
  5621. struct drm_i915_private *dev_priv = dev->dev_private;
  5622. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5623. uint32_t pf, pipesrc;
  5624. int ret;
  5625. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  5626. if (ret)
  5627. goto out;
  5628. ret = BEGIN_LP_RING(4);
  5629. if (ret)
  5630. goto out;
  5631. /* i965+ uses the linear or tiled offsets from the
  5632. * Display Registers (which do not change across a page-flip)
  5633. * so we need only reprogram the base address.
  5634. */
  5635. OUT_RING(MI_DISPLAY_FLIP |
  5636. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5637. OUT_RING(fb->pitch);
  5638. OUT_RING(obj->gtt_offset | obj->tiling_mode);
  5639. /* XXX Enabling the panel-fitter across page-flip is so far
  5640. * untested on non-native modes, so ignore it for now.
  5641. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  5642. */
  5643. pf = 0;
  5644. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  5645. OUT_RING(pf | pipesrc);
  5646. ADVANCE_LP_RING();
  5647. out:
  5648. return ret;
  5649. }
  5650. static int intel_gen6_queue_flip(struct drm_device *dev,
  5651. struct drm_crtc *crtc,
  5652. struct drm_framebuffer *fb,
  5653. struct drm_i915_gem_object *obj)
  5654. {
  5655. struct drm_i915_private *dev_priv = dev->dev_private;
  5656. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5657. uint32_t pf, pipesrc;
  5658. int ret;
  5659. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  5660. if (ret)
  5661. goto out;
  5662. ret = BEGIN_LP_RING(4);
  5663. if (ret)
  5664. goto out;
  5665. OUT_RING(MI_DISPLAY_FLIP |
  5666. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5667. OUT_RING(fb->pitch | obj->tiling_mode);
  5668. OUT_RING(obj->gtt_offset);
  5669. pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  5670. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  5671. OUT_RING(pf | pipesrc);
  5672. ADVANCE_LP_RING();
  5673. out:
  5674. return ret;
  5675. }
  5676. /*
  5677. * On gen7 we currently use the blit ring because (in early silicon at least)
  5678. * the render ring doesn't give us interrpts for page flip completion, which
  5679. * means clients will hang after the first flip is queued. Fortunately the
  5680. * blit ring generates interrupts properly, so use it instead.
  5681. */
  5682. static int intel_gen7_queue_flip(struct drm_device *dev,
  5683. struct drm_crtc *crtc,
  5684. struct drm_framebuffer *fb,
  5685. struct drm_i915_gem_object *obj)
  5686. {
  5687. struct drm_i915_private *dev_priv = dev->dev_private;
  5688. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5689. struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
  5690. int ret;
  5691. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5692. if (ret)
  5693. goto out;
  5694. ret = intel_ring_begin(ring, 4);
  5695. if (ret)
  5696. goto out;
  5697. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));
  5698. intel_ring_emit(ring, (fb->pitch | obj->tiling_mode));
  5699. intel_ring_emit(ring, (obj->gtt_offset));
  5700. intel_ring_emit(ring, (MI_NOOP));
  5701. intel_ring_advance(ring);
  5702. out:
  5703. return ret;
  5704. }
  5705. static int intel_default_queue_flip(struct drm_device *dev,
  5706. struct drm_crtc *crtc,
  5707. struct drm_framebuffer *fb,
  5708. struct drm_i915_gem_object *obj)
  5709. {
  5710. return -ENODEV;
  5711. }
  5712. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  5713. struct drm_framebuffer *fb,
  5714. struct drm_pending_vblank_event *event)
  5715. {
  5716. struct drm_device *dev = crtc->dev;
  5717. struct drm_i915_private *dev_priv = dev->dev_private;
  5718. struct intel_framebuffer *intel_fb;
  5719. struct drm_i915_gem_object *obj;
  5720. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5721. struct intel_unpin_work *work;
  5722. unsigned long flags;
  5723. int ret;
  5724. work = kzalloc(sizeof *work, GFP_KERNEL);
  5725. if (work == NULL)
  5726. return -ENOMEM;
  5727. work->event = event;
  5728. work->dev = crtc->dev;
  5729. intel_fb = to_intel_framebuffer(crtc->fb);
  5730. work->old_fb_obj = intel_fb->obj;
  5731. INIT_WORK(&work->work, intel_unpin_work_fn);
  5732. /* We borrow the event spin lock for protecting unpin_work */
  5733. spin_lock_irqsave(&dev->event_lock, flags);
  5734. if (intel_crtc->unpin_work) {
  5735. spin_unlock_irqrestore(&dev->event_lock, flags);
  5736. kfree(work);
  5737. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  5738. return -EBUSY;
  5739. }
  5740. intel_crtc->unpin_work = work;
  5741. spin_unlock_irqrestore(&dev->event_lock, flags);
  5742. intel_fb = to_intel_framebuffer(fb);
  5743. obj = intel_fb->obj;
  5744. mutex_lock(&dev->struct_mutex);
  5745. /* Reference the objects for the scheduled work. */
  5746. drm_gem_object_reference(&work->old_fb_obj->base);
  5747. drm_gem_object_reference(&obj->base);
  5748. crtc->fb = fb;
  5749. ret = drm_vblank_get(dev, intel_crtc->pipe);
  5750. if (ret)
  5751. goto cleanup_objs;
  5752. work->pending_flip_obj = obj;
  5753. work->enable_stall_check = true;
  5754. /* Block clients from rendering to the new back buffer until
  5755. * the flip occurs and the object is no longer visible.
  5756. */
  5757. atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  5758. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
  5759. if (ret)
  5760. goto cleanup_pending;
  5761. mutex_unlock(&dev->struct_mutex);
  5762. trace_i915_flip_request(intel_crtc->plane, obj);
  5763. return 0;
  5764. cleanup_pending:
  5765. atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  5766. cleanup_objs:
  5767. drm_gem_object_unreference(&work->old_fb_obj->base);
  5768. drm_gem_object_unreference(&obj->base);
  5769. mutex_unlock(&dev->struct_mutex);
  5770. spin_lock_irqsave(&dev->event_lock, flags);
  5771. intel_crtc->unpin_work = NULL;
  5772. spin_unlock_irqrestore(&dev->event_lock, flags);
  5773. kfree(work);
  5774. return ret;
  5775. }
  5776. static void intel_sanitize_modesetting(struct drm_device *dev,
  5777. int pipe, int plane)
  5778. {
  5779. struct drm_i915_private *dev_priv = dev->dev_private;
  5780. u32 reg, val;
  5781. if (HAS_PCH_SPLIT(dev))
  5782. return;
  5783. /* Who knows what state these registers were left in by the BIOS or
  5784. * grub?
  5785. *
  5786. * If we leave the registers in a conflicting state (e.g. with the
  5787. * display plane reading from the other pipe than the one we intend
  5788. * to use) then when we attempt to teardown the active mode, we will
  5789. * not disable the pipes and planes in the correct order -- leaving
  5790. * a plane reading from a disabled pipe and possibly leading to
  5791. * undefined behaviour.
  5792. */
  5793. reg = DSPCNTR(plane);
  5794. val = I915_READ(reg);
  5795. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  5796. return;
  5797. if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
  5798. return;
  5799. /* This display plane is active and attached to the other CPU pipe. */
  5800. pipe = !pipe;
  5801. /* Disable the plane and wait for it to stop reading from the pipe. */
  5802. intel_disable_plane(dev_priv, plane, pipe);
  5803. intel_disable_pipe(dev_priv, pipe);
  5804. }
  5805. static void intel_crtc_reset(struct drm_crtc *crtc)
  5806. {
  5807. struct drm_device *dev = crtc->dev;
  5808. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5809. /* Reset flags back to the 'unknown' status so that they
  5810. * will be correctly set on the initial modeset.
  5811. */
  5812. intel_crtc->dpms_mode = -1;
  5813. /* We need to fix up any BIOS configuration that conflicts with
  5814. * our expectations.
  5815. */
  5816. intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
  5817. }
  5818. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  5819. .dpms = intel_crtc_dpms,
  5820. .mode_fixup = intel_crtc_mode_fixup,
  5821. .mode_set = intel_crtc_mode_set,
  5822. .mode_set_base = intel_pipe_set_base,
  5823. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  5824. .load_lut = intel_crtc_load_lut,
  5825. .disable = intel_crtc_disable,
  5826. };
  5827. static const struct drm_crtc_funcs intel_crtc_funcs = {
  5828. .reset = intel_crtc_reset,
  5829. .cursor_set = intel_crtc_cursor_set,
  5830. .cursor_move = intel_crtc_cursor_move,
  5831. .gamma_set = intel_crtc_gamma_set,
  5832. .set_config = drm_crtc_helper_set_config,
  5833. .destroy = intel_crtc_destroy,
  5834. .page_flip = intel_crtc_page_flip,
  5835. };
  5836. static void intel_crtc_init(struct drm_device *dev, int pipe)
  5837. {
  5838. drm_i915_private_t *dev_priv = dev->dev_private;
  5839. struct intel_crtc *intel_crtc;
  5840. int i;
  5841. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  5842. if (intel_crtc == NULL)
  5843. return;
  5844. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  5845. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  5846. for (i = 0; i < 256; i++) {
  5847. intel_crtc->lut_r[i] = i;
  5848. intel_crtc->lut_g[i] = i;
  5849. intel_crtc->lut_b[i] = i;
  5850. }
  5851. /* Swap pipes & planes for FBC on pre-965 */
  5852. intel_crtc->pipe = pipe;
  5853. intel_crtc->plane = pipe;
  5854. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  5855. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  5856. intel_crtc->plane = !pipe;
  5857. }
  5858. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  5859. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  5860. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  5861. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  5862. intel_crtc_reset(&intel_crtc->base);
  5863. intel_crtc->active = true; /* force the pipe off on setup_init_config */
  5864. intel_crtc->bpp = 24; /* default for pre-Ironlake */
  5865. if (HAS_PCH_SPLIT(dev)) {
  5866. intel_helper_funcs.prepare = ironlake_crtc_prepare;
  5867. intel_helper_funcs.commit = ironlake_crtc_commit;
  5868. } else {
  5869. intel_helper_funcs.prepare = i9xx_crtc_prepare;
  5870. intel_helper_funcs.commit = i9xx_crtc_commit;
  5871. }
  5872. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  5873. intel_crtc->busy = false;
  5874. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  5875. (unsigned long)intel_crtc);
  5876. }
  5877. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  5878. struct drm_file *file)
  5879. {
  5880. drm_i915_private_t *dev_priv = dev->dev_private;
  5881. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  5882. struct drm_mode_object *drmmode_obj;
  5883. struct intel_crtc *crtc;
  5884. if (!dev_priv) {
  5885. DRM_ERROR("called with no initialization\n");
  5886. return -EINVAL;
  5887. }
  5888. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  5889. DRM_MODE_OBJECT_CRTC);
  5890. if (!drmmode_obj) {
  5891. DRM_ERROR("no such CRTC id\n");
  5892. return -EINVAL;
  5893. }
  5894. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  5895. pipe_from_crtc_id->pipe = crtc->pipe;
  5896. return 0;
  5897. }
  5898. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  5899. {
  5900. struct intel_encoder *encoder;
  5901. int index_mask = 0;
  5902. int entry = 0;
  5903. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  5904. if (type_mask & encoder->clone_mask)
  5905. index_mask |= (1 << entry);
  5906. entry++;
  5907. }
  5908. return index_mask;
  5909. }
  5910. static bool has_edp_a(struct drm_device *dev)
  5911. {
  5912. struct drm_i915_private *dev_priv = dev->dev_private;
  5913. if (!IS_MOBILE(dev))
  5914. return false;
  5915. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  5916. return false;
  5917. if (IS_GEN5(dev) &&
  5918. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  5919. return false;
  5920. return true;
  5921. }
  5922. static void intel_setup_outputs(struct drm_device *dev)
  5923. {
  5924. struct drm_i915_private *dev_priv = dev->dev_private;
  5925. struct intel_encoder *encoder;
  5926. bool dpd_is_edp = false;
  5927. bool has_lvds = false;
  5928. if (IS_MOBILE(dev) && !IS_I830(dev))
  5929. has_lvds = intel_lvds_init(dev);
  5930. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  5931. /* disable the panel fitter on everything but LVDS */
  5932. I915_WRITE(PFIT_CONTROL, 0);
  5933. }
  5934. if (HAS_PCH_SPLIT(dev)) {
  5935. dpd_is_edp = intel_dpd_is_edp(dev);
  5936. if (has_edp_a(dev))
  5937. intel_dp_init(dev, DP_A);
  5938. if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  5939. intel_dp_init(dev, PCH_DP_D);
  5940. }
  5941. intel_crt_init(dev);
  5942. if (HAS_PCH_SPLIT(dev)) {
  5943. int found;
  5944. if (I915_READ(HDMIB) & PORT_DETECTED) {
  5945. /* PCH SDVOB multiplex with HDMIB */
  5946. found = intel_sdvo_init(dev, PCH_SDVOB);
  5947. if (!found)
  5948. intel_hdmi_init(dev, HDMIB);
  5949. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  5950. intel_dp_init(dev, PCH_DP_B);
  5951. }
  5952. if (I915_READ(HDMIC) & PORT_DETECTED)
  5953. intel_hdmi_init(dev, HDMIC);
  5954. if (I915_READ(HDMID) & PORT_DETECTED)
  5955. intel_hdmi_init(dev, HDMID);
  5956. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  5957. intel_dp_init(dev, PCH_DP_C);
  5958. if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  5959. intel_dp_init(dev, PCH_DP_D);
  5960. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  5961. bool found = false;
  5962. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  5963. DRM_DEBUG_KMS("probing SDVOB\n");
  5964. found = intel_sdvo_init(dev, SDVOB);
  5965. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  5966. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  5967. intel_hdmi_init(dev, SDVOB);
  5968. }
  5969. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  5970. DRM_DEBUG_KMS("probing DP_B\n");
  5971. intel_dp_init(dev, DP_B);
  5972. }
  5973. }
  5974. /* Before G4X SDVOC doesn't have its own detect register */
  5975. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  5976. DRM_DEBUG_KMS("probing SDVOC\n");
  5977. found = intel_sdvo_init(dev, SDVOC);
  5978. }
  5979. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  5980. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  5981. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  5982. intel_hdmi_init(dev, SDVOC);
  5983. }
  5984. if (SUPPORTS_INTEGRATED_DP(dev)) {
  5985. DRM_DEBUG_KMS("probing DP_C\n");
  5986. intel_dp_init(dev, DP_C);
  5987. }
  5988. }
  5989. if (SUPPORTS_INTEGRATED_DP(dev) &&
  5990. (I915_READ(DP_D) & DP_DETECTED)) {
  5991. DRM_DEBUG_KMS("probing DP_D\n");
  5992. intel_dp_init(dev, DP_D);
  5993. }
  5994. } else if (IS_GEN2(dev))
  5995. intel_dvo_init(dev);
  5996. if (SUPPORTS_TV(dev))
  5997. intel_tv_init(dev);
  5998. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  5999. encoder->base.possible_crtcs = encoder->crtc_mask;
  6000. encoder->base.possible_clones =
  6001. intel_encoder_clones(dev, encoder->clone_mask);
  6002. }
  6003. intel_panel_setup_backlight(dev);
  6004. /* disable all the possible outputs/crtcs before entering KMS mode */
  6005. drm_helper_disable_unused_functions(dev);
  6006. }
  6007. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  6008. {
  6009. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  6010. drm_framebuffer_cleanup(fb);
  6011. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  6012. kfree(intel_fb);
  6013. }
  6014. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  6015. struct drm_file *file,
  6016. unsigned int *handle)
  6017. {
  6018. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  6019. struct drm_i915_gem_object *obj = intel_fb->obj;
  6020. return drm_gem_handle_create(file, &obj->base, handle);
  6021. }
  6022. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  6023. .destroy = intel_user_framebuffer_destroy,
  6024. .create_handle = intel_user_framebuffer_create_handle,
  6025. };
  6026. int intel_framebuffer_init(struct drm_device *dev,
  6027. struct intel_framebuffer *intel_fb,
  6028. struct drm_mode_fb_cmd *mode_cmd,
  6029. struct drm_i915_gem_object *obj)
  6030. {
  6031. int ret;
  6032. if (obj->tiling_mode == I915_TILING_Y)
  6033. return -EINVAL;
  6034. if (mode_cmd->pitch & 63)
  6035. return -EINVAL;
  6036. switch (mode_cmd->bpp) {
  6037. case 8:
  6038. case 16:
  6039. /* Only pre-ILK can handle 5:5:5 */
  6040. if (mode_cmd->depth == 15 && !HAS_PCH_SPLIT(dev))
  6041. return -EINVAL;
  6042. break;
  6043. case 24:
  6044. case 32:
  6045. break;
  6046. default:
  6047. return -EINVAL;
  6048. }
  6049. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  6050. if (ret) {
  6051. DRM_ERROR("framebuffer init failed %d\n", ret);
  6052. return ret;
  6053. }
  6054. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  6055. intel_fb->obj = obj;
  6056. return 0;
  6057. }
  6058. static struct drm_framebuffer *
  6059. intel_user_framebuffer_create(struct drm_device *dev,
  6060. struct drm_file *filp,
  6061. struct drm_mode_fb_cmd *mode_cmd)
  6062. {
  6063. struct drm_i915_gem_object *obj;
  6064. obj = to_intel_bo(drm_gem_object_lookup(dev, filp, mode_cmd->handle));
  6065. if (&obj->base == NULL)
  6066. return ERR_PTR(-ENOENT);
  6067. return intel_framebuffer_create(dev, mode_cmd, obj);
  6068. }
  6069. static const struct drm_mode_config_funcs intel_mode_funcs = {
  6070. .fb_create = intel_user_framebuffer_create,
  6071. .output_poll_changed = intel_fb_output_poll_changed,
  6072. };
  6073. static struct drm_i915_gem_object *
  6074. intel_alloc_context_page(struct drm_device *dev)
  6075. {
  6076. struct drm_i915_gem_object *ctx;
  6077. int ret;
  6078. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  6079. ctx = i915_gem_alloc_object(dev, 4096);
  6080. if (!ctx) {
  6081. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  6082. return NULL;
  6083. }
  6084. ret = i915_gem_object_pin(ctx, 4096, true);
  6085. if (ret) {
  6086. DRM_ERROR("failed to pin power context: %d\n", ret);
  6087. goto err_unref;
  6088. }
  6089. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  6090. if (ret) {
  6091. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  6092. goto err_unpin;
  6093. }
  6094. return ctx;
  6095. err_unpin:
  6096. i915_gem_object_unpin(ctx);
  6097. err_unref:
  6098. drm_gem_object_unreference(&ctx->base);
  6099. mutex_unlock(&dev->struct_mutex);
  6100. return NULL;
  6101. }
  6102. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  6103. {
  6104. struct drm_i915_private *dev_priv = dev->dev_private;
  6105. u16 rgvswctl;
  6106. rgvswctl = I915_READ16(MEMSWCTL);
  6107. if (rgvswctl & MEMCTL_CMD_STS) {
  6108. DRM_DEBUG("gpu busy, RCS change rejected\n");
  6109. return false; /* still busy with another command */
  6110. }
  6111. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  6112. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  6113. I915_WRITE16(MEMSWCTL, rgvswctl);
  6114. POSTING_READ16(MEMSWCTL);
  6115. rgvswctl |= MEMCTL_CMD_STS;
  6116. I915_WRITE16(MEMSWCTL, rgvswctl);
  6117. return true;
  6118. }
  6119. void ironlake_enable_drps(struct drm_device *dev)
  6120. {
  6121. struct drm_i915_private *dev_priv = dev->dev_private;
  6122. u32 rgvmodectl = I915_READ(MEMMODECTL);
  6123. u8 fmax, fmin, fstart, vstart;
  6124. /* Enable temp reporting */
  6125. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  6126. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  6127. /* 100ms RC evaluation intervals */
  6128. I915_WRITE(RCUPEI, 100000);
  6129. I915_WRITE(RCDNEI, 100000);
  6130. /* Set max/min thresholds to 90ms and 80ms respectively */
  6131. I915_WRITE(RCBMAXAVG, 90000);
  6132. I915_WRITE(RCBMINAVG, 80000);
  6133. I915_WRITE(MEMIHYST, 1);
  6134. /* Set up min, max, and cur for interrupt handling */
  6135. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  6136. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  6137. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  6138. MEMMODE_FSTART_SHIFT;
  6139. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  6140. PXVFREQ_PX_SHIFT;
  6141. dev_priv->fmax = fmax; /* IPS callback will increase this */
  6142. dev_priv->fstart = fstart;
  6143. dev_priv->max_delay = fstart;
  6144. dev_priv->min_delay = fmin;
  6145. dev_priv->cur_delay = fstart;
  6146. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  6147. fmax, fmin, fstart);
  6148. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  6149. /*
  6150. * Interrupts will be enabled in ironlake_irq_postinstall
  6151. */
  6152. I915_WRITE(VIDSTART, vstart);
  6153. POSTING_READ(VIDSTART);
  6154. rgvmodectl |= MEMMODE_SWMODE_EN;
  6155. I915_WRITE(MEMMODECTL, rgvmodectl);
  6156. if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  6157. DRM_ERROR("stuck trying to change perf mode\n");
  6158. msleep(1);
  6159. ironlake_set_drps(dev, fstart);
  6160. dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  6161. I915_READ(0x112e0);
  6162. dev_priv->last_time1 = jiffies_to_msecs(jiffies);
  6163. dev_priv->last_count2 = I915_READ(0x112f4);
  6164. getrawmonotonic(&dev_priv->last_time2);
  6165. }
  6166. void ironlake_disable_drps(struct drm_device *dev)
  6167. {
  6168. struct drm_i915_private *dev_priv = dev->dev_private;
  6169. u16 rgvswctl = I915_READ16(MEMSWCTL);
  6170. /* Ack interrupts, disable EFC interrupt */
  6171. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  6172. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  6173. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  6174. I915_WRITE(DEIIR, DE_PCU_EVENT);
  6175. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  6176. /* Go back to the starting frequency */
  6177. ironlake_set_drps(dev, dev_priv->fstart);
  6178. msleep(1);
  6179. rgvswctl |= MEMCTL_CMD_STS;
  6180. I915_WRITE(MEMSWCTL, rgvswctl);
  6181. msleep(1);
  6182. }
  6183. void gen6_set_rps(struct drm_device *dev, u8 val)
  6184. {
  6185. struct drm_i915_private *dev_priv = dev->dev_private;
  6186. u32 swreq;
  6187. swreq = (val & 0x3ff) << 25;
  6188. I915_WRITE(GEN6_RPNSWREQ, swreq);
  6189. }
  6190. void gen6_disable_rps(struct drm_device *dev)
  6191. {
  6192. struct drm_i915_private *dev_priv = dev->dev_private;
  6193. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  6194. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  6195. I915_WRITE(GEN6_PMIER, 0);
  6196. spin_lock_irq(&dev_priv->rps_lock);
  6197. dev_priv->pm_iir = 0;
  6198. spin_unlock_irq(&dev_priv->rps_lock);
  6199. I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
  6200. }
  6201. static unsigned long intel_pxfreq(u32 vidfreq)
  6202. {
  6203. unsigned long freq;
  6204. int div = (vidfreq & 0x3f0000) >> 16;
  6205. int post = (vidfreq & 0x3000) >> 12;
  6206. int pre = (vidfreq & 0x7);
  6207. if (!pre)
  6208. return 0;
  6209. freq = ((div * 133333) / ((1<<post) * pre));
  6210. return freq;
  6211. }
  6212. void intel_init_emon(struct drm_device *dev)
  6213. {
  6214. struct drm_i915_private *dev_priv = dev->dev_private;
  6215. u32 lcfuse;
  6216. u8 pxw[16];
  6217. int i;
  6218. /* Disable to program */
  6219. I915_WRITE(ECR, 0);
  6220. POSTING_READ(ECR);
  6221. /* Program energy weights for various events */
  6222. I915_WRITE(SDEW, 0x15040d00);
  6223. I915_WRITE(CSIEW0, 0x007f0000);
  6224. I915_WRITE(CSIEW1, 0x1e220004);
  6225. I915_WRITE(CSIEW2, 0x04000004);
  6226. for (i = 0; i < 5; i++)
  6227. I915_WRITE(PEW + (i * 4), 0);
  6228. for (i = 0; i < 3; i++)
  6229. I915_WRITE(DEW + (i * 4), 0);
  6230. /* Program P-state weights to account for frequency power adjustment */
  6231. for (i = 0; i < 16; i++) {
  6232. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  6233. unsigned long freq = intel_pxfreq(pxvidfreq);
  6234. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  6235. PXVFREQ_PX_SHIFT;
  6236. unsigned long val;
  6237. val = vid * vid;
  6238. val *= (freq / 1000);
  6239. val *= 255;
  6240. val /= (127*127*900);
  6241. if (val > 0xff)
  6242. DRM_ERROR("bad pxval: %ld\n", val);
  6243. pxw[i] = val;
  6244. }
  6245. /* Render standby states get 0 weight */
  6246. pxw[14] = 0;
  6247. pxw[15] = 0;
  6248. for (i = 0; i < 4; i++) {
  6249. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  6250. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  6251. I915_WRITE(PXW + (i * 4), val);
  6252. }
  6253. /* Adjust magic regs to magic values (more experimental results) */
  6254. I915_WRITE(OGW0, 0);
  6255. I915_WRITE(OGW1, 0);
  6256. I915_WRITE(EG0, 0x00007f00);
  6257. I915_WRITE(EG1, 0x0000000e);
  6258. I915_WRITE(EG2, 0x000e0000);
  6259. I915_WRITE(EG3, 0x68000300);
  6260. I915_WRITE(EG4, 0x42000000);
  6261. I915_WRITE(EG5, 0x00140031);
  6262. I915_WRITE(EG6, 0);
  6263. I915_WRITE(EG7, 0);
  6264. for (i = 0; i < 8; i++)
  6265. I915_WRITE(PXWL + (i * 4), 0);
  6266. /* Enable PMON + select events */
  6267. I915_WRITE(ECR, 0x80000019);
  6268. lcfuse = I915_READ(LCFUSE02);
  6269. dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
  6270. }
  6271. void gen6_enable_rps(struct drm_i915_private *dev_priv)
  6272. {
  6273. u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  6274. u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
  6275. u32 pcu_mbox, rc6_mask = 0;
  6276. int cur_freq, min_freq, max_freq;
  6277. int i;
  6278. /* Here begins a magic sequence of register writes to enable
  6279. * auto-downclocking.
  6280. *
  6281. * Perhaps there might be some value in exposing these to
  6282. * userspace...
  6283. */
  6284. I915_WRITE(GEN6_RC_STATE, 0);
  6285. mutex_lock(&dev_priv->dev->struct_mutex);
  6286. gen6_gt_force_wake_get(dev_priv);
  6287. /* disable the counters and set deterministic thresholds */
  6288. I915_WRITE(GEN6_RC_CONTROL, 0);
  6289. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  6290. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  6291. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  6292. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  6293. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  6294. for (i = 0; i < I915_NUM_RINGS; i++)
  6295. I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
  6296. I915_WRITE(GEN6_RC_SLEEP, 0);
  6297. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  6298. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  6299. I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
  6300. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  6301. if (i915_enable_rc6)
  6302. rc6_mask = GEN6_RC_CTL_RC6p_ENABLE |
  6303. GEN6_RC_CTL_RC6_ENABLE;
  6304. I915_WRITE(GEN6_RC_CONTROL,
  6305. rc6_mask |
  6306. GEN6_RC_CTL_EI_MODE(1) |
  6307. GEN6_RC_CTL_HW_ENABLE);
  6308. I915_WRITE(GEN6_RPNSWREQ,
  6309. GEN6_FREQUENCY(10) |
  6310. GEN6_OFFSET(0) |
  6311. GEN6_AGGRESSIVE_TURBO);
  6312. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  6313. GEN6_FREQUENCY(12));
  6314. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  6315. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  6316. 18 << 24 |
  6317. 6 << 16);
  6318. I915_WRITE(GEN6_RP_UP_THRESHOLD, 10000);
  6319. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 1000000);
  6320. I915_WRITE(GEN6_RP_UP_EI, 100000);
  6321. I915_WRITE(GEN6_RP_DOWN_EI, 5000000);
  6322. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  6323. I915_WRITE(GEN6_RP_CONTROL,
  6324. GEN6_RP_MEDIA_TURBO |
  6325. GEN6_RP_USE_NORMAL_FREQ |
  6326. GEN6_RP_MEDIA_IS_GFX |
  6327. GEN6_RP_ENABLE |
  6328. GEN6_RP_UP_BUSY_AVG |
  6329. GEN6_RP_DOWN_IDLE_CONT);
  6330. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  6331. 500))
  6332. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  6333. I915_WRITE(GEN6_PCODE_DATA, 0);
  6334. I915_WRITE(GEN6_PCODE_MAILBOX,
  6335. GEN6_PCODE_READY |
  6336. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  6337. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  6338. 500))
  6339. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  6340. min_freq = (rp_state_cap & 0xff0000) >> 16;
  6341. max_freq = rp_state_cap & 0xff;
  6342. cur_freq = (gt_perf_status & 0xff00) >> 8;
  6343. /* Check for overclock support */
  6344. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  6345. 500))
  6346. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  6347. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
  6348. pcu_mbox = I915_READ(GEN6_PCODE_DATA);
  6349. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  6350. 500))
  6351. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  6352. if (pcu_mbox & (1<<31)) { /* OC supported */
  6353. max_freq = pcu_mbox & 0xff;
  6354. DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);
  6355. }
  6356. /* In units of 100MHz */
  6357. dev_priv->max_delay = max_freq;
  6358. dev_priv->min_delay = min_freq;
  6359. dev_priv->cur_delay = cur_freq;
  6360. /* requires MSI enabled */
  6361. I915_WRITE(GEN6_PMIER,
  6362. GEN6_PM_MBOX_EVENT |
  6363. GEN6_PM_THERMAL_EVENT |
  6364. GEN6_PM_RP_DOWN_TIMEOUT |
  6365. GEN6_PM_RP_UP_THRESHOLD |
  6366. GEN6_PM_RP_DOWN_THRESHOLD |
  6367. GEN6_PM_RP_UP_EI_EXPIRED |
  6368. GEN6_PM_RP_DOWN_EI_EXPIRED);
  6369. spin_lock_irq(&dev_priv->rps_lock);
  6370. WARN_ON(dev_priv->pm_iir != 0);
  6371. I915_WRITE(GEN6_PMIMR, 0);
  6372. spin_unlock_irq(&dev_priv->rps_lock);
  6373. /* enable all PM interrupts */
  6374. I915_WRITE(GEN6_PMINTRMSK, 0);
  6375. gen6_gt_force_wake_put(dev_priv);
  6376. mutex_unlock(&dev_priv->dev->struct_mutex);
  6377. }
  6378. void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
  6379. {
  6380. int min_freq = 15;
  6381. int gpu_freq, ia_freq, max_ia_freq;
  6382. int scaling_factor = 180;
  6383. max_ia_freq = cpufreq_quick_get_max(0);
  6384. /*
  6385. * Default to measured freq if none found, PCU will ensure we don't go
  6386. * over
  6387. */
  6388. if (!max_ia_freq)
  6389. max_ia_freq = tsc_khz;
  6390. /* Convert from kHz to MHz */
  6391. max_ia_freq /= 1000;
  6392. mutex_lock(&dev_priv->dev->struct_mutex);
  6393. /*
  6394. * For each potential GPU frequency, load a ring frequency we'd like
  6395. * to use for memory access. We do this by specifying the IA frequency
  6396. * the PCU should use as a reference to determine the ring frequency.
  6397. */
  6398. for (gpu_freq = dev_priv->max_delay; gpu_freq >= dev_priv->min_delay;
  6399. gpu_freq--) {
  6400. int diff = dev_priv->max_delay - gpu_freq;
  6401. /*
  6402. * For GPU frequencies less than 750MHz, just use the lowest
  6403. * ring freq.
  6404. */
  6405. if (gpu_freq < min_freq)
  6406. ia_freq = 800;
  6407. else
  6408. ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
  6409. ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
  6410. I915_WRITE(GEN6_PCODE_DATA,
  6411. (ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT) |
  6412. gpu_freq);
  6413. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY |
  6414. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  6415. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) &
  6416. GEN6_PCODE_READY) == 0, 10)) {
  6417. DRM_ERROR("pcode write of freq table timed out\n");
  6418. continue;
  6419. }
  6420. }
  6421. mutex_unlock(&dev_priv->dev->struct_mutex);
  6422. }
  6423. static void ironlake_init_clock_gating(struct drm_device *dev)
  6424. {
  6425. struct drm_i915_private *dev_priv = dev->dev_private;
  6426. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  6427. /* Required for FBC */
  6428. dspclk_gate |= DPFCUNIT_CLOCK_GATE_DISABLE |
  6429. DPFCRUNIT_CLOCK_GATE_DISABLE |
  6430. DPFDUNIT_CLOCK_GATE_DISABLE;
  6431. /* Required for CxSR */
  6432. dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
  6433. I915_WRITE(PCH_3DCGDIS0,
  6434. MARIUNIT_CLOCK_GATE_DISABLE |
  6435. SVSMUNIT_CLOCK_GATE_DISABLE);
  6436. I915_WRITE(PCH_3DCGDIS1,
  6437. VFMUNIT_CLOCK_GATE_DISABLE);
  6438. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  6439. /*
  6440. * According to the spec the following bits should be set in
  6441. * order to enable memory self-refresh
  6442. * The bit 22/21 of 0x42004
  6443. * The bit 5 of 0x42020
  6444. * The bit 15 of 0x45000
  6445. */
  6446. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6447. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  6448. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  6449. I915_WRITE(ILK_DSPCLK_GATE,
  6450. (I915_READ(ILK_DSPCLK_GATE) |
  6451. ILK_DPARB_CLK_GATE));
  6452. I915_WRITE(DISP_ARB_CTL,
  6453. (I915_READ(DISP_ARB_CTL) |
  6454. DISP_FBC_WM_DIS));
  6455. I915_WRITE(WM3_LP_ILK, 0);
  6456. I915_WRITE(WM2_LP_ILK, 0);
  6457. I915_WRITE(WM1_LP_ILK, 0);
  6458. /*
  6459. * Based on the document from hardware guys the following bits
  6460. * should be set unconditionally in order to enable FBC.
  6461. * The bit 22 of 0x42000
  6462. * The bit 22 of 0x42004
  6463. * The bit 7,8,9 of 0x42020.
  6464. */
  6465. if (IS_IRONLAKE_M(dev)) {
  6466. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  6467. I915_READ(ILK_DISPLAY_CHICKEN1) |
  6468. ILK_FBCQ_DIS);
  6469. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6470. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6471. ILK_DPARB_GATE);
  6472. I915_WRITE(ILK_DSPCLK_GATE,
  6473. I915_READ(ILK_DSPCLK_GATE) |
  6474. ILK_DPFC_DIS1 |
  6475. ILK_DPFC_DIS2 |
  6476. ILK_CLK_FBC);
  6477. }
  6478. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6479. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6480. ILK_ELPIN_409_SELECT);
  6481. I915_WRITE(_3D_CHICKEN2,
  6482. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  6483. _3D_CHICKEN2_WM_READ_PIPELINED);
  6484. }
  6485. static void gen6_init_clock_gating(struct drm_device *dev)
  6486. {
  6487. struct drm_i915_private *dev_priv = dev->dev_private;
  6488. int pipe;
  6489. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  6490. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  6491. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6492. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6493. ILK_ELPIN_409_SELECT);
  6494. I915_WRITE(WM3_LP_ILK, 0);
  6495. I915_WRITE(WM2_LP_ILK, 0);
  6496. I915_WRITE(WM1_LP_ILK, 0);
  6497. /*
  6498. * According to the spec the following bits should be
  6499. * set in order to enable memory self-refresh and fbc:
  6500. * The bit21 and bit22 of 0x42000
  6501. * The bit21 and bit22 of 0x42004
  6502. * The bit5 and bit7 of 0x42020
  6503. * The bit14 of 0x70180
  6504. * The bit14 of 0x71180
  6505. */
  6506. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  6507. I915_READ(ILK_DISPLAY_CHICKEN1) |
  6508. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  6509. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6510. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6511. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  6512. I915_WRITE(ILK_DSPCLK_GATE,
  6513. I915_READ(ILK_DSPCLK_GATE) |
  6514. ILK_DPARB_CLK_GATE |
  6515. ILK_DPFD_CLK_GATE);
  6516. for_each_pipe(pipe)
  6517. I915_WRITE(DSPCNTR(pipe),
  6518. I915_READ(DSPCNTR(pipe)) |
  6519. DISPPLANE_TRICKLE_FEED_DISABLE);
  6520. }
  6521. static void ivybridge_init_clock_gating(struct drm_device *dev)
  6522. {
  6523. struct drm_i915_private *dev_priv = dev->dev_private;
  6524. int pipe;
  6525. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  6526. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  6527. I915_WRITE(WM3_LP_ILK, 0);
  6528. I915_WRITE(WM2_LP_ILK, 0);
  6529. I915_WRITE(WM1_LP_ILK, 0);
  6530. I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
  6531. for_each_pipe(pipe)
  6532. I915_WRITE(DSPCNTR(pipe),
  6533. I915_READ(DSPCNTR(pipe)) |
  6534. DISPPLANE_TRICKLE_FEED_DISABLE);
  6535. }
  6536. static void g4x_init_clock_gating(struct drm_device *dev)
  6537. {
  6538. struct drm_i915_private *dev_priv = dev->dev_private;
  6539. uint32_t dspclk_gate;
  6540. I915_WRITE(RENCLK_GATE_D1, 0);
  6541. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  6542. GS_UNIT_CLOCK_GATE_DISABLE |
  6543. CL_UNIT_CLOCK_GATE_DISABLE);
  6544. I915_WRITE(RAMCLK_GATE_D, 0);
  6545. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  6546. OVRUNIT_CLOCK_GATE_DISABLE |
  6547. OVCUNIT_CLOCK_GATE_DISABLE;
  6548. if (IS_GM45(dev))
  6549. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  6550. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  6551. }
  6552. static void crestline_init_clock_gating(struct drm_device *dev)
  6553. {
  6554. struct drm_i915_private *dev_priv = dev->dev_private;
  6555. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  6556. I915_WRITE(RENCLK_GATE_D2, 0);
  6557. I915_WRITE(DSPCLK_GATE_D, 0);
  6558. I915_WRITE(RAMCLK_GATE_D, 0);
  6559. I915_WRITE16(DEUC, 0);
  6560. }
  6561. static void broadwater_init_clock_gating(struct drm_device *dev)
  6562. {
  6563. struct drm_i915_private *dev_priv = dev->dev_private;
  6564. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  6565. I965_RCC_CLOCK_GATE_DISABLE |
  6566. I965_RCPB_CLOCK_GATE_DISABLE |
  6567. I965_ISC_CLOCK_GATE_DISABLE |
  6568. I965_FBC_CLOCK_GATE_DISABLE);
  6569. I915_WRITE(RENCLK_GATE_D2, 0);
  6570. }
  6571. static void gen3_init_clock_gating(struct drm_device *dev)
  6572. {
  6573. struct drm_i915_private *dev_priv = dev->dev_private;
  6574. u32 dstate = I915_READ(D_STATE);
  6575. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  6576. DSTATE_DOT_CLOCK_GATING;
  6577. I915_WRITE(D_STATE, dstate);
  6578. }
  6579. static void i85x_init_clock_gating(struct drm_device *dev)
  6580. {
  6581. struct drm_i915_private *dev_priv = dev->dev_private;
  6582. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  6583. }
  6584. static void i830_init_clock_gating(struct drm_device *dev)
  6585. {
  6586. struct drm_i915_private *dev_priv = dev->dev_private;
  6587. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  6588. }
  6589. static void ibx_init_clock_gating(struct drm_device *dev)
  6590. {
  6591. struct drm_i915_private *dev_priv = dev->dev_private;
  6592. /*
  6593. * On Ibex Peak and Cougar Point, we need to disable clock
  6594. * gating for the panel power sequencer or it will fail to
  6595. * start up when no ports are active.
  6596. */
  6597. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  6598. }
  6599. static void cpt_init_clock_gating(struct drm_device *dev)
  6600. {
  6601. struct drm_i915_private *dev_priv = dev->dev_private;
  6602. /*
  6603. * On Ibex Peak and Cougar Point, we need to disable clock
  6604. * gating for the panel power sequencer or it will fail to
  6605. * start up when no ports are active.
  6606. */
  6607. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  6608. I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
  6609. DPLS_EDP_PPS_FIX_DIS);
  6610. }
  6611. static void ironlake_teardown_rc6(struct drm_device *dev)
  6612. {
  6613. struct drm_i915_private *dev_priv = dev->dev_private;
  6614. if (dev_priv->renderctx) {
  6615. i915_gem_object_unpin(dev_priv->renderctx);
  6616. drm_gem_object_unreference(&dev_priv->renderctx->base);
  6617. dev_priv->renderctx = NULL;
  6618. }
  6619. if (dev_priv->pwrctx) {
  6620. i915_gem_object_unpin(dev_priv->pwrctx);
  6621. drm_gem_object_unreference(&dev_priv->pwrctx->base);
  6622. dev_priv->pwrctx = NULL;
  6623. }
  6624. }
  6625. static void ironlake_disable_rc6(struct drm_device *dev)
  6626. {
  6627. struct drm_i915_private *dev_priv = dev->dev_private;
  6628. if (I915_READ(PWRCTXA)) {
  6629. /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
  6630. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
  6631. wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
  6632. 50);
  6633. I915_WRITE(PWRCTXA, 0);
  6634. POSTING_READ(PWRCTXA);
  6635. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  6636. POSTING_READ(RSTDBYCTL);
  6637. }
  6638. ironlake_teardown_rc6(dev);
  6639. }
  6640. static int ironlake_setup_rc6(struct drm_device *dev)
  6641. {
  6642. struct drm_i915_private *dev_priv = dev->dev_private;
  6643. if (dev_priv->renderctx == NULL)
  6644. dev_priv->renderctx = intel_alloc_context_page(dev);
  6645. if (!dev_priv->renderctx)
  6646. return -ENOMEM;
  6647. if (dev_priv->pwrctx == NULL)
  6648. dev_priv->pwrctx = intel_alloc_context_page(dev);
  6649. if (!dev_priv->pwrctx) {
  6650. ironlake_teardown_rc6(dev);
  6651. return -ENOMEM;
  6652. }
  6653. return 0;
  6654. }
  6655. void ironlake_enable_rc6(struct drm_device *dev)
  6656. {
  6657. struct drm_i915_private *dev_priv = dev->dev_private;
  6658. int ret;
  6659. /* rc6 disabled by default due to repeated reports of hanging during
  6660. * boot and resume.
  6661. */
  6662. if (!i915_enable_rc6)
  6663. return;
  6664. mutex_lock(&dev->struct_mutex);
  6665. ret = ironlake_setup_rc6(dev);
  6666. if (ret) {
  6667. mutex_unlock(&dev->struct_mutex);
  6668. return;
  6669. }
  6670. /*
  6671. * GPU can automatically power down the render unit if given a page
  6672. * to save state.
  6673. */
  6674. ret = BEGIN_LP_RING(6);
  6675. if (ret) {
  6676. ironlake_teardown_rc6(dev);
  6677. mutex_unlock(&dev->struct_mutex);
  6678. return;
  6679. }
  6680. OUT_RING(MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
  6681. OUT_RING(MI_SET_CONTEXT);
  6682. OUT_RING(dev_priv->renderctx->gtt_offset |
  6683. MI_MM_SPACE_GTT |
  6684. MI_SAVE_EXT_STATE_EN |
  6685. MI_RESTORE_EXT_STATE_EN |
  6686. MI_RESTORE_INHIBIT);
  6687. OUT_RING(MI_SUSPEND_FLUSH);
  6688. OUT_RING(MI_NOOP);
  6689. OUT_RING(MI_FLUSH);
  6690. ADVANCE_LP_RING();
  6691. /*
  6692. * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
  6693. * does an implicit flush, combined with MI_FLUSH above, it should be
  6694. * safe to assume that renderctx is valid
  6695. */
  6696. ret = intel_wait_ring_idle(LP_RING(dev_priv));
  6697. if (ret) {
  6698. DRM_ERROR("failed to enable ironlake power power savings\n");
  6699. ironlake_teardown_rc6(dev);
  6700. mutex_unlock(&dev->struct_mutex);
  6701. return;
  6702. }
  6703. I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
  6704. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  6705. mutex_unlock(&dev->struct_mutex);
  6706. }
  6707. void intel_init_clock_gating(struct drm_device *dev)
  6708. {
  6709. struct drm_i915_private *dev_priv = dev->dev_private;
  6710. dev_priv->display.init_clock_gating(dev);
  6711. if (dev_priv->display.init_pch_clock_gating)
  6712. dev_priv->display.init_pch_clock_gating(dev);
  6713. }
  6714. /* Set up chip specific display functions */
  6715. static void intel_init_display(struct drm_device *dev)
  6716. {
  6717. struct drm_i915_private *dev_priv = dev->dev_private;
  6718. /* We always want a DPMS function */
  6719. if (HAS_PCH_SPLIT(dev)) {
  6720. dev_priv->display.dpms = ironlake_crtc_dpms;
  6721. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  6722. dev_priv->display.update_plane = ironlake_update_plane;
  6723. } else {
  6724. dev_priv->display.dpms = i9xx_crtc_dpms;
  6725. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  6726. dev_priv->display.update_plane = i9xx_update_plane;
  6727. }
  6728. if (I915_HAS_FBC(dev)) {
  6729. if (HAS_PCH_SPLIT(dev)) {
  6730. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  6731. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  6732. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  6733. } else if (IS_GM45(dev)) {
  6734. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  6735. dev_priv->display.enable_fbc = g4x_enable_fbc;
  6736. dev_priv->display.disable_fbc = g4x_disable_fbc;
  6737. } else if (IS_CRESTLINE(dev)) {
  6738. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  6739. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  6740. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  6741. }
  6742. /* 855GM needs testing */
  6743. }
  6744. /* Returns the core display clock speed */
  6745. if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
  6746. dev_priv->display.get_display_clock_speed =
  6747. i945_get_display_clock_speed;
  6748. else if (IS_I915G(dev))
  6749. dev_priv->display.get_display_clock_speed =
  6750. i915_get_display_clock_speed;
  6751. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  6752. dev_priv->display.get_display_clock_speed =
  6753. i9xx_misc_get_display_clock_speed;
  6754. else if (IS_I915GM(dev))
  6755. dev_priv->display.get_display_clock_speed =
  6756. i915gm_get_display_clock_speed;
  6757. else if (IS_I865G(dev))
  6758. dev_priv->display.get_display_clock_speed =
  6759. i865_get_display_clock_speed;
  6760. else if (IS_I85X(dev))
  6761. dev_priv->display.get_display_clock_speed =
  6762. i855_get_display_clock_speed;
  6763. else /* 852, 830 */
  6764. dev_priv->display.get_display_clock_speed =
  6765. i830_get_display_clock_speed;
  6766. /* For FIFO watermark updates */
  6767. if (HAS_PCH_SPLIT(dev)) {
  6768. if (HAS_PCH_IBX(dev))
  6769. dev_priv->display.init_pch_clock_gating = ibx_init_clock_gating;
  6770. else if (HAS_PCH_CPT(dev))
  6771. dev_priv->display.init_pch_clock_gating = cpt_init_clock_gating;
  6772. if (IS_GEN5(dev)) {
  6773. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  6774. dev_priv->display.update_wm = ironlake_update_wm;
  6775. else {
  6776. DRM_DEBUG_KMS("Failed to get proper latency. "
  6777. "Disable CxSR\n");
  6778. dev_priv->display.update_wm = NULL;
  6779. }
  6780. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  6781. dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
  6782. } else if (IS_GEN6(dev)) {
  6783. if (SNB_READ_WM0_LATENCY()) {
  6784. dev_priv->display.update_wm = sandybridge_update_wm;
  6785. } else {
  6786. DRM_DEBUG_KMS("Failed to read display plane latency. "
  6787. "Disable CxSR\n");
  6788. dev_priv->display.update_wm = NULL;
  6789. }
  6790. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  6791. dev_priv->display.init_clock_gating = gen6_init_clock_gating;
  6792. } else if (IS_IVYBRIDGE(dev)) {
  6793. /* FIXME: detect B0+ stepping and use auto training */
  6794. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  6795. if (SNB_READ_WM0_LATENCY()) {
  6796. dev_priv->display.update_wm = sandybridge_update_wm;
  6797. } else {
  6798. DRM_DEBUG_KMS("Failed to read display plane latency. "
  6799. "Disable CxSR\n");
  6800. dev_priv->display.update_wm = NULL;
  6801. }
  6802. dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
  6803. } else
  6804. dev_priv->display.update_wm = NULL;
  6805. } else if (IS_PINEVIEW(dev)) {
  6806. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  6807. dev_priv->is_ddr3,
  6808. dev_priv->fsb_freq,
  6809. dev_priv->mem_freq)) {
  6810. DRM_INFO("failed to find known CxSR latency "
  6811. "(found ddr%s fsb freq %d, mem freq %d), "
  6812. "disabling CxSR\n",
  6813. (dev_priv->is_ddr3 == 1) ? "3": "2",
  6814. dev_priv->fsb_freq, dev_priv->mem_freq);
  6815. /* Disable CxSR and never update its watermark again */
  6816. pineview_disable_cxsr(dev);
  6817. dev_priv->display.update_wm = NULL;
  6818. } else
  6819. dev_priv->display.update_wm = pineview_update_wm;
  6820. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  6821. } else if (IS_G4X(dev)) {
  6822. dev_priv->display.update_wm = g4x_update_wm;
  6823. dev_priv->display.init_clock_gating = g4x_init_clock_gating;
  6824. } else if (IS_GEN4(dev)) {
  6825. dev_priv->display.update_wm = i965_update_wm;
  6826. if (IS_CRESTLINE(dev))
  6827. dev_priv->display.init_clock_gating = crestline_init_clock_gating;
  6828. else if (IS_BROADWATER(dev))
  6829. dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
  6830. } else if (IS_GEN3(dev)) {
  6831. dev_priv->display.update_wm = i9xx_update_wm;
  6832. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  6833. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  6834. } else if (IS_I865G(dev)) {
  6835. dev_priv->display.update_wm = i830_update_wm;
  6836. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  6837. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  6838. } else if (IS_I85X(dev)) {
  6839. dev_priv->display.update_wm = i9xx_update_wm;
  6840. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  6841. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  6842. } else {
  6843. dev_priv->display.update_wm = i830_update_wm;
  6844. dev_priv->display.init_clock_gating = i830_init_clock_gating;
  6845. if (IS_845G(dev))
  6846. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  6847. else
  6848. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  6849. }
  6850. /* Default just returns -ENODEV to indicate unsupported */
  6851. dev_priv->display.queue_flip = intel_default_queue_flip;
  6852. switch (INTEL_INFO(dev)->gen) {
  6853. case 2:
  6854. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  6855. break;
  6856. case 3:
  6857. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  6858. break;
  6859. case 4:
  6860. case 5:
  6861. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  6862. break;
  6863. case 6:
  6864. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  6865. break;
  6866. case 7:
  6867. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  6868. break;
  6869. }
  6870. }
  6871. /*
  6872. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  6873. * resume, or other times. This quirk makes sure that's the case for
  6874. * affected systems.
  6875. */
  6876. static void quirk_pipea_force (struct drm_device *dev)
  6877. {
  6878. struct drm_i915_private *dev_priv = dev->dev_private;
  6879. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  6880. DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
  6881. }
  6882. struct intel_quirk {
  6883. int device;
  6884. int subsystem_vendor;
  6885. int subsystem_device;
  6886. void (*hook)(struct drm_device *dev);
  6887. };
  6888. struct intel_quirk intel_quirks[] = {
  6889. /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
  6890. { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
  6891. /* HP Mini needs pipe A force quirk (LP: #322104) */
  6892. { 0x27ae,0x103c, 0x361a, quirk_pipea_force },
  6893. /* Thinkpad R31 needs pipe A force quirk */
  6894. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  6895. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  6896. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  6897. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  6898. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  6899. /* ThinkPad X40 needs pipe A force quirk */
  6900. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  6901. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  6902. /* 855 & before need to leave pipe A & dpll A up */
  6903. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  6904. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  6905. };
  6906. static void intel_init_quirks(struct drm_device *dev)
  6907. {
  6908. struct pci_dev *d = dev->pdev;
  6909. int i;
  6910. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  6911. struct intel_quirk *q = &intel_quirks[i];
  6912. if (d->device == q->device &&
  6913. (d->subsystem_vendor == q->subsystem_vendor ||
  6914. q->subsystem_vendor == PCI_ANY_ID) &&
  6915. (d->subsystem_device == q->subsystem_device ||
  6916. q->subsystem_device == PCI_ANY_ID))
  6917. q->hook(dev);
  6918. }
  6919. }
  6920. /* Disable the VGA plane that we never use */
  6921. static void i915_disable_vga(struct drm_device *dev)
  6922. {
  6923. struct drm_i915_private *dev_priv = dev->dev_private;
  6924. u8 sr1;
  6925. u32 vga_reg;
  6926. if (HAS_PCH_SPLIT(dev))
  6927. vga_reg = CPU_VGACNTRL;
  6928. else
  6929. vga_reg = VGACNTRL;
  6930. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  6931. outb(1, VGA_SR_INDEX);
  6932. sr1 = inb(VGA_SR_DATA);
  6933. outb(sr1 | 1<<5, VGA_SR_DATA);
  6934. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  6935. udelay(300);
  6936. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  6937. POSTING_READ(vga_reg);
  6938. }
  6939. void intel_modeset_init(struct drm_device *dev)
  6940. {
  6941. struct drm_i915_private *dev_priv = dev->dev_private;
  6942. int i;
  6943. drm_mode_config_init(dev);
  6944. dev->mode_config.min_width = 0;
  6945. dev->mode_config.min_height = 0;
  6946. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  6947. intel_init_quirks(dev);
  6948. intel_init_display(dev);
  6949. if (IS_GEN2(dev)) {
  6950. dev->mode_config.max_width = 2048;
  6951. dev->mode_config.max_height = 2048;
  6952. } else if (IS_GEN3(dev)) {
  6953. dev->mode_config.max_width = 4096;
  6954. dev->mode_config.max_height = 4096;
  6955. } else {
  6956. dev->mode_config.max_width = 8192;
  6957. dev->mode_config.max_height = 8192;
  6958. }
  6959. dev->mode_config.fb_base = dev->agp->base;
  6960. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  6961. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  6962. for (i = 0; i < dev_priv->num_pipe; i++) {
  6963. intel_crtc_init(dev, i);
  6964. }
  6965. /* Just disable it once at startup */
  6966. i915_disable_vga(dev);
  6967. intel_setup_outputs(dev);
  6968. intel_init_clock_gating(dev);
  6969. if (IS_IRONLAKE_M(dev)) {
  6970. ironlake_enable_drps(dev);
  6971. intel_init_emon(dev);
  6972. }
  6973. if (IS_GEN6(dev) || IS_GEN7(dev)) {
  6974. gen6_enable_rps(dev_priv);
  6975. gen6_update_ring_freq(dev_priv);
  6976. }
  6977. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  6978. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  6979. (unsigned long)dev);
  6980. }
  6981. void intel_modeset_gem_init(struct drm_device *dev)
  6982. {
  6983. if (IS_IRONLAKE_M(dev))
  6984. ironlake_enable_rc6(dev);
  6985. intel_setup_overlay(dev);
  6986. }
  6987. void intel_modeset_cleanup(struct drm_device *dev)
  6988. {
  6989. struct drm_i915_private *dev_priv = dev->dev_private;
  6990. struct drm_crtc *crtc;
  6991. struct intel_crtc *intel_crtc;
  6992. drm_kms_helper_poll_fini(dev);
  6993. mutex_lock(&dev->struct_mutex);
  6994. intel_unregister_dsm_handler();
  6995. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  6996. /* Skip inactive CRTCs */
  6997. if (!crtc->fb)
  6998. continue;
  6999. intel_crtc = to_intel_crtc(crtc);
  7000. intel_increase_pllclock(crtc);
  7001. }
  7002. intel_disable_fbc(dev);
  7003. if (IS_IRONLAKE_M(dev))
  7004. ironlake_disable_drps(dev);
  7005. if (IS_GEN6(dev) || IS_GEN7(dev))
  7006. gen6_disable_rps(dev);
  7007. if (IS_IRONLAKE_M(dev))
  7008. ironlake_disable_rc6(dev);
  7009. mutex_unlock(&dev->struct_mutex);
  7010. /* Disable the irq before mode object teardown, for the irq might
  7011. * enqueue unpin/hotplug work. */
  7012. drm_irq_uninstall(dev);
  7013. cancel_work_sync(&dev_priv->hotplug_work);
  7014. /* Shut off idle work before the crtcs get freed. */
  7015. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  7016. intel_crtc = to_intel_crtc(crtc);
  7017. del_timer_sync(&intel_crtc->idle_timer);
  7018. }
  7019. del_timer_sync(&dev_priv->idle_timer);
  7020. cancel_work_sync(&dev_priv->idle_work);
  7021. drm_mode_config_cleanup(dev);
  7022. }
  7023. /*
  7024. * Return which encoder is currently attached for connector.
  7025. */
  7026. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  7027. {
  7028. return &intel_attached_encoder(connector)->base;
  7029. }
  7030. void intel_connector_attach_encoder(struct intel_connector *connector,
  7031. struct intel_encoder *encoder)
  7032. {
  7033. connector->encoder = encoder;
  7034. drm_mode_connector_attach_encoder(&connector->base,
  7035. &encoder->base);
  7036. }
  7037. /*
  7038. * set vga decode state - true == enable VGA decode
  7039. */
  7040. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  7041. {
  7042. struct drm_i915_private *dev_priv = dev->dev_private;
  7043. u16 gmch_ctrl;
  7044. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  7045. if (state)
  7046. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  7047. else
  7048. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  7049. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  7050. return 0;
  7051. }
  7052. #ifdef CONFIG_DEBUG_FS
  7053. #include <linux/seq_file.h>
  7054. struct intel_display_error_state {
  7055. struct intel_cursor_error_state {
  7056. u32 control;
  7057. u32 position;
  7058. u32 base;
  7059. u32 size;
  7060. } cursor[2];
  7061. struct intel_pipe_error_state {
  7062. u32 conf;
  7063. u32 source;
  7064. u32 htotal;
  7065. u32 hblank;
  7066. u32 hsync;
  7067. u32 vtotal;
  7068. u32 vblank;
  7069. u32 vsync;
  7070. } pipe[2];
  7071. struct intel_plane_error_state {
  7072. u32 control;
  7073. u32 stride;
  7074. u32 size;
  7075. u32 pos;
  7076. u32 addr;
  7077. u32 surface;
  7078. u32 tile_offset;
  7079. } plane[2];
  7080. };
  7081. struct intel_display_error_state *
  7082. intel_display_capture_error_state(struct drm_device *dev)
  7083. {
  7084. drm_i915_private_t *dev_priv = dev->dev_private;
  7085. struct intel_display_error_state *error;
  7086. int i;
  7087. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  7088. if (error == NULL)
  7089. return NULL;
  7090. for (i = 0; i < 2; i++) {
  7091. error->cursor[i].control = I915_READ(CURCNTR(i));
  7092. error->cursor[i].position = I915_READ(CURPOS(i));
  7093. error->cursor[i].base = I915_READ(CURBASE(i));
  7094. error->plane[i].control = I915_READ(DSPCNTR(i));
  7095. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  7096. error->plane[i].size = I915_READ(DSPSIZE(i));
  7097. error->plane[i].pos= I915_READ(DSPPOS(i));
  7098. error->plane[i].addr = I915_READ(DSPADDR(i));
  7099. if (INTEL_INFO(dev)->gen >= 4) {
  7100. error->plane[i].surface = I915_READ(DSPSURF(i));
  7101. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  7102. }
  7103. error->pipe[i].conf = I915_READ(PIPECONF(i));
  7104. error->pipe[i].source = I915_READ(PIPESRC(i));
  7105. error->pipe[i].htotal = I915_READ(HTOTAL(i));
  7106. error->pipe[i].hblank = I915_READ(HBLANK(i));
  7107. error->pipe[i].hsync = I915_READ(HSYNC(i));
  7108. error->pipe[i].vtotal = I915_READ(VTOTAL(i));
  7109. error->pipe[i].vblank = I915_READ(VBLANK(i));
  7110. error->pipe[i].vsync = I915_READ(VSYNC(i));
  7111. }
  7112. return error;
  7113. }
  7114. void
  7115. intel_display_print_error_state(struct seq_file *m,
  7116. struct drm_device *dev,
  7117. struct intel_display_error_state *error)
  7118. {
  7119. int i;
  7120. for (i = 0; i < 2; i++) {
  7121. seq_printf(m, "Pipe [%d]:\n", i);
  7122. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  7123. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  7124. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  7125. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  7126. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  7127. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  7128. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  7129. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  7130. seq_printf(m, "Plane [%d]:\n", i);
  7131. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  7132. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  7133. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  7134. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  7135. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  7136. if (INTEL_INFO(dev)->gen >= 4) {
  7137. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  7138. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  7139. }
  7140. seq_printf(m, "Cursor [%d]:\n", i);
  7141. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  7142. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  7143. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  7144. }
  7145. }
  7146. #endif