intel_display.c 261 KB

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