intel_display.c 234 KB

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