intel_display.c 252 KB

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