intel_display.c 259 KB

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