intel_display.c 253 KB

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