intel_display.c 217 KB

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