intel_display.c 239 KB

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