intel_display.c 211 KB

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