intel_display.c 236 KB

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