intel_display.c 265 KB

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