intel_display.c 176 KB

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