intel_display.c 226 KB

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