intel_display.c 211 KB

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