intel_display.c 220 KB

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