intel_display.c 182 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934
  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/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include "drmP.h"
  35. #include "intel_drv.h"
  36. #include "i915_drm.h"
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include "drm_dp_helper.h"
  40. #include "drm_crtc_helper.h"
  41. #include <linux/dma_remapping.h>
  42. #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  43. bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
  44. static void intel_increase_pllclock(struct drm_crtc *crtc);
  45. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  46. typedef struct {
  47. /* given values */
  48. int n;
  49. int m1, m2;
  50. int p1, p2;
  51. /* derived values */
  52. int dot;
  53. int vco;
  54. int m;
  55. int p;
  56. } intel_clock_t;
  57. typedef struct {
  58. int min, max;
  59. } intel_range_t;
  60. typedef struct {
  61. int dot_limit;
  62. int p2_slow, p2_fast;
  63. } intel_p2_t;
  64. #define INTEL_P2_NUM 2
  65. typedef struct intel_limit intel_limit_t;
  66. struct intel_limit {
  67. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  68. intel_p2_t p2;
  69. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  70. int, int, intel_clock_t *, intel_clock_t *);
  71. };
  72. /* FDI */
  73. #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
  74. static bool
  75. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  76. int target, int refclk, intel_clock_t *match_clock,
  77. intel_clock_t *best_clock);
  78. static bool
  79. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  80. int target, int refclk, intel_clock_t *match_clock,
  81. intel_clock_t *best_clock);
  82. static bool
  83. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  84. int target, int refclk, intel_clock_t *match_clock,
  85. intel_clock_t *best_clock);
  86. static bool
  87. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  88. int target, int refclk, intel_clock_t *match_clock,
  89. intel_clock_t *best_clock);
  90. static inline u32 /* units of 100MHz */
  91. intel_fdi_link_freq(struct drm_device *dev)
  92. {
  93. if (IS_GEN5(dev)) {
  94. struct drm_i915_private *dev_priv = dev->dev_private;
  95. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  96. } else
  97. return 27;
  98. }
  99. static const intel_limit_t intel_limits_i8xx_dvo = {
  100. .dot = { .min = 25000, .max = 350000 },
  101. .vco = { .min = 930000, .max = 1400000 },
  102. .n = { .min = 3, .max = 16 },
  103. .m = { .min = 96, .max = 140 },
  104. .m1 = { .min = 18, .max = 26 },
  105. .m2 = { .min = 6, .max = 16 },
  106. .p = { .min = 4, .max = 128 },
  107. .p1 = { .min = 2, .max = 33 },
  108. .p2 = { .dot_limit = 165000,
  109. .p2_slow = 4, .p2_fast = 2 },
  110. .find_pll = intel_find_best_PLL,
  111. };
  112. static const intel_limit_t intel_limits_i8xx_lvds = {
  113. .dot = { .min = 25000, .max = 350000 },
  114. .vco = { .min = 930000, .max = 1400000 },
  115. .n = { .min = 3, .max = 16 },
  116. .m = { .min = 96, .max = 140 },
  117. .m1 = { .min = 18, .max = 26 },
  118. .m2 = { .min = 6, .max = 16 },
  119. .p = { .min = 4, .max = 128 },
  120. .p1 = { .min = 1, .max = 6 },
  121. .p2 = { .dot_limit = 165000,
  122. .p2_slow = 14, .p2_fast = 7 },
  123. .find_pll = intel_find_best_PLL,
  124. };
  125. static const intel_limit_t intel_limits_i9xx_sdvo = {
  126. .dot = { .min = 20000, .max = 400000 },
  127. .vco = { .min = 1400000, .max = 2800000 },
  128. .n = { .min = 1, .max = 6 },
  129. .m = { .min = 70, .max = 120 },
  130. .m1 = { .min = 10, .max = 22 },
  131. .m2 = { .min = 5, .max = 9 },
  132. .p = { .min = 5, .max = 80 },
  133. .p1 = { .min = 1, .max = 8 },
  134. .p2 = { .dot_limit = 200000,
  135. .p2_slow = 10, .p2_fast = 5 },
  136. .find_pll = intel_find_best_PLL,
  137. };
  138. static const intel_limit_t intel_limits_i9xx_lvds = {
  139. .dot = { .min = 20000, .max = 400000 },
  140. .vco = { .min = 1400000, .max = 2800000 },
  141. .n = { .min = 1, .max = 6 },
  142. .m = { .min = 70, .max = 120 },
  143. .m1 = { .min = 10, .max = 22 },
  144. .m2 = { .min = 5, .max = 9 },
  145. .p = { .min = 7, .max = 98 },
  146. .p1 = { .min = 1, .max = 8 },
  147. .p2 = { .dot_limit = 112000,
  148. .p2_slow = 14, .p2_fast = 7 },
  149. .find_pll = intel_find_best_PLL,
  150. };
  151. static const intel_limit_t intel_limits_g4x_sdvo = {
  152. .dot = { .min = 25000, .max = 270000 },
  153. .vco = { .min = 1750000, .max = 3500000},
  154. .n = { .min = 1, .max = 4 },
  155. .m = { .min = 104, .max = 138 },
  156. .m1 = { .min = 17, .max = 23 },
  157. .m2 = { .min = 5, .max = 11 },
  158. .p = { .min = 10, .max = 30 },
  159. .p1 = { .min = 1, .max = 3},
  160. .p2 = { .dot_limit = 270000,
  161. .p2_slow = 10,
  162. .p2_fast = 10
  163. },
  164. .find_pll = intel_g4x_find_best_PLL,
  165. };
  166. static const intel_limit_t intel_limits_g4x_hdmi = {
  167. .dot = { .min = 22000, .max = 400000 },
  168. .vco = { .min = 1750000, .max = 3500000},
  169. .n = { .min = 1, .max = 4 },
  170. .m = { .min = 104, .max = 138 },
  171. .m1 = { .min = 16, .max = 23 },
  172. .m2 = { .min = 5, .max = 11 },
  173. .p = { .min = 5, .max = 80 },
  174. .p1 = { .min = 1, .max = 8},
  175. .p2 = { .dot_limit = 165000,
  176. .p2_slow = 10, .p2_fast = 5 },
  177. .find_pll = intel_g4x_find_best_PLL,
  178. };
  179. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  180. .dot = { .min = 20000, .max = 115000 },
  181. .vco = { .min = 1750000, .max = 3500000 },
  182. .n = { .min = 1, .max = 3 },
  183. .m = { .min = 104, .max = 138 },
  184. .m1 = { .min = 17, .max = 23 },
  185. .m2 = { .min = 5, .max = 11 },
  186. .p = { .min = 28, .max = 112 },
  187. .p1 = { .min = 2, .max = 8 },
  188. .p2 = { .dot_limit = 0,
  189. .p2_slow = 14, .p2_fast = 14
  190. },
  191. .find_pll = intel_g4x_find_best_PLL,
  192. };
  193. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  194. .dot = { .min = 80000, .max = 224000 },
  195. .vco = { .min = 1750000, .max = 3500000 },
  196. .n = { .min = 1, .max = 3 },
  197. .m = { .min = 104, .max = 138 },
  198. .m1 = { .min = 17, .max = 23 },
  199. .m2 = { .min = 5, .max = 11 },
  200. .p = { .min = 14, .max = 42 },
  201. .p1 = { .min = 2, .max = 6 },
  202. .p2 = { .dot_limit = 0,
  203. .p2_slow = 7, .p2_fast = 7
  204. },
  205. .find_pll = intel_g4x_find_best_PLL,
  206. };
  207. static const intel_limit_t intel_limits_g4x_display_port = {
  208. .dot = { .min = 161670, .max = 227000 },
  209. .vco = { .min = 1750000, .max = 3500000},
  210. .n = { .min = 1, .max = 2 },
  211. .m = { .min = 97, .max = 108 },
  212. .m1 = { .min = 0x10, .max = 0x12 },
  213. .m2 = { .min = 0x05, .max = 0x06 },
  214. .p = { .min = 10, .max = 20 },
  215. .p1 = { .min = 1, .max = 2},
  216. .p2 = { .dot_limit = 0,
  217. .p2_slow = 10, .p2_fast = 10 },
  218. .find_pll = intel_find_pll_g4x_dp,
  219. };
  220. static const intel_limit_t intel_limits_pineview_sdvo = {
  221. .dot = { .min = 20000, .max = 400000},
  222. .vco = { .min = 1700000, .max = 3500000 },
  223. /* Pineview's Ncounter is a ring counter */
  224. .n = { .min = 3, .max = 6 },
  225. .m = { .min = 2, .max = 256 },
  226. /* Pineview only has one combined m divider, which we treat as m2. */
  227. .m1 = { .min = 0, .max = 0 },
  228. .m2 = { .min = 0, .max = 254 },
  229. .p = { .min = 5, .max = 80 },
  230. .p1 = { .min = 1, .max = 8 },
  231. .p2 = { .dot_limit = 200000,
  232. .p2_slow = 10, .p2_fast = 5 },
  233. .find_pll = intel_find_best_PLL,
  234. };
  235. static const intel_limit_t intel_limits_pineview_lvds = {
  236. .dot = { .min = 20000, .max = 400000 },
  237. .vco = { .min = 1700000, .max = 3500000 },
  238. .n = { .min = 3, .max = 6 },
  239. .m = { .min = 2, .max = 256 },
  240. .m1 = { .min = 0, .max = 0 },
  241. .m2 = { .min = 0, .max = 254 },
  242. .p = { .min = 7, .max = 112 },
  243. .p1 = { .min = 1, .max = 8 },
  244. .p2 = { .dot_limit = 112000,
  245. .p2_slow = 14, .p2_fast = 14 },
  246. .find_pll = intel_find_best_PLL,
  247. };
  248. /* Ironlake / Sandybridge
  249. *
  250. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  251. * the range value for them is (actual_value - 2).
  252. */
  253. static const intel_limit_t intel_limits_ironlake_dac = {
  254. .dot = { .min = 25000, .max = 350000 },
  255. .vco = { .min = 1760000, .max = 3510000 },
  256. .n = { .min = 1, .max = 5 },
  257. .m = { .min = 79, .max = 127 },
  258. .m1 = { .min = 12, .max = 22 },
  259. .m2 = { .min = 5, .max = 9 },
  260. .p = { .min = 5, .max = 80 },
  261. .p1 = { .min = 1, .max = 8 },
  262. .p2 = { .dot_limit = 225000,
  263. .p2_slow = 10, .p2_fast = 5 },
  264. .find_pll = intel_g4x_find_best_PLL,
  265. };
  266. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  267. .dot = { .min = 25000, .max = 350000 },
  268. .vco = { .min = 1760000, .max = 3510000 },
  269. .n = { .min = 1, .max = 3 },
  270. .m = { .min = 79, .max = 118 },
  271. .m1 = { .min = 12, .max = 22 },
  272. .m2 = { .min = 5, .max = 9 },
  273. .p = { .min = 28, .max = 112 },
  274. .p1 = { .min = 2, .max = 8 },
  275. .p2 = { .dot_limit = 225000,
  276. .p2_slow = 14, .p2_fast = 14 },
  277. .find_pll = intel_g4x_find_best_PLL,
  278. };
  279. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  280. .dot = { .min = 25000, .max = 350000 },
  281. .vco = { .min = 1760000, .max = 3510000 },
  282. .n = { .min = 1, .max = 3 },
  283. .m = { .min = 79, .max = 127 },
  284. .m1 = { .min = 12, .max = 22 },
  285. .m2 = { .min = 5, .max = 9 },
  286. .p = { .min = 14, .max = 56 },
  287. .p1 = { .min = 2, .max = 8 },
  288. .p2 = { .dot_limit = 225000,
  289. .p2_slow = 7, .p2_fast = 7 },
  290. .find_pll = intel_g4x_find_best_PLL,
  291. };
  292. /* LVDS 100mhz refclk limits. */
  293. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  294. .dot = { .min = 25000, .max = 350000 },
  295. .vco = { .min = 1760000, .max = 3510000 },
  296. .n = { .min = 1, .max = 2 },
  297. .m = { .min = 79, .max = 126 },
  298. .m1 = { .min = 12, .max = 22 },
  299. .m2 = { .min = 5, .max = 9 },
  300. .p = { .min = 28, .max = 112 },
  301. .p1 = { .min = 2, .max = 8 },
  302. .p2 = { .dot_limit = 225000,
  303. .p2_slow = 14, .p2_fast = 14 },
  304. .find_pll = intel_g4x_find_best_PLL,
  305. };
  306. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  307. .dot = { .min = 25000, .max = 350000 },
  308. .vco = { .min = 1760000, .max = 3510000 },
  309. .n = { .min = 1, .max = 3 },
  310. .m = { .min = 79, .max = 126 },
  311. .m1 = { .min = 12, .max = 22 },
  312. .m2 = { .min = 5, .max = 9 },
  313. .p = { .min = 14, .max = 42 },
  314. .p1 = { .min = 2, .max = 6 },
  315. .p2 = { .dot_limit = 225000,
  316. .p2_slow = 7, .p2_fast = 7 },
  317. .find_pll = intel_g4x_find_best_PLL,
  318. };
  319. static const intel_limit_t intel_limits_ironlake_display_port = {
  320. .dot = { .min = 25000, .max = 350000 },
  321. .vco = { .min = 1760000, .max = 3510000},
  322. .n = { .min = 1, .max = 2 },
  323. .m = { .min = 81, .max = 90 },
  324. .m1 = { .min = 12, .max = 22 },
  325. .m2 = { .min = 5, .max = 9 },
  326. .p = { .min = 10, .max = 20 },
  327. .p1 = { .min = 1, .max = 2},
  328. .p2 = { .dot_limit = 0,
  329. .p2_slow = 10, .p2_fast = 10 },
  330. .find_pll = intel_find_pll_ironlake_dp,
  331. };
  332. u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
  333. {
  334. unsigned long flags;
  335. u32 val = 0;
  336. spin_lock_irqsave(&dev_priv->dpio_lock, flags);
  337. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
  338. DRM_ERROR("DPIO idle wait timed out\n");
  339. goto out_unlock;
  340. }
  341. I915_WRITE(DPIO_REG, reg);
  342. I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
  343. DPIO_BYTE);
  344. if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
  345. DRM_ERROR("DPIO read wait timed out\n");
  346. goto out_unlock;
  347. }
  348. val = I915_READ(DPIO_DATA);
  349. out_unlock:
  350. spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
  351. return val;
  352. }
  353. static void vlv_init_dpio(struct drm_device *dev)
  354. {
  355. struct drm_i915_private *dev_priv = dev->dev_private;
  356. /* Reset the DPIO config */
  357. I915_WRITE(DPIO_CTL, 0);
  358. POSTING_READ(DPIO_CTL);
  359. I915_WRITE(DPIO_CTL, 1);
  360. POSTING_READ(DPIO_CTL);
  361. }
  362. static int intel_dual_link_lvds_callback(const struct dmi_system_id *id)
  363. {
  364. DRM_INFO("Forcing lvds to dual link mode on %s\n", id->ident);
  365. return 1;
  366. }
  367. static const struct dmi_system_id intel_dual_link_lvds[] = {
  368. {
  369. .callback = intel_dual_link_lvds_callback,
  370. .ident = "Apple MacBook Pro (Core i5/i7 Series)",
  371. .matches = {
  372. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  373. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro8,2"),
  374. },
  375. },
  376. { } /* terminating entry */
  377. };
  378. static bool is_dual_link_lvds(struct drm_i915_private *dev_priv,
  379. unsigned int reg)
  380. {
  381. unsigned int val;
  382. /* use the module option value if specified */
  383. if (i915_lvds_channel_mode > 0)
  384. return i915_lvds_channel_mode == 2;
  385. if (dmi_check_system(intel_dual_link_lvds))
  386. return true;
  387. if (dev_priv->lvds_val)
  388. val = dev_priv->lvds_val;
  389. else {
  390. /* BIOS should set the proper LVDS register value at boot, but
  391. * in reality, it doesn't set the value when the lid is closed;
  392. * we need to check "the value to be set" in VBT when LVDS
  393. * register is uninitialized.
  394. */
  395. val = I915_READ(reg);
  396. if (!(val & ~LVDS_DETECTED))
  397. val = dev_priv->bios_lvds_val;
  398. dev_priv->lvds_val = val;
  399. }
  400. return (val & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP;
  401. }
  402. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  403. int refclk)
  404. {
  405. struct drm_device *dev = crtc->dev;
  406. struct drm_i915_private *dev_priv = dev->dev_private;
  407. const intel_limit_t *limit;
  408. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  409. if (is_dual_link_lvds(dev_priv, PCH_LVDS)) {
  410. /* LVDS dual channel */
  411. if (refclk == 100000)
  412. limit = &intel_limits_ironlake_dual_lvds_100m;
  413. else
  414. limit = &intel_limits_ironlake_dual_lvds;
  415. } else {
  416. if (refclk == 100000)
  417. limit = &intel_limits_ironlake_single_lvds_100m;
  418. else
  419. limit = &intel_limits_ironlake_single_lvds;
  420. }
  421. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  422. HAS_eDP)
  423. limit = &intel_limits_ironlake_display_port;
  424. else
  425. limit = &intel_limits_ironlake_dac;
  426. return limit;
  427. }
  428. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  429. {
  430. struct drm_device *dev = crtc->dev;
  431. struct drm_i915_private *dev_priv = dev->dev_private;
  432. const intel_limit_t *limit;
  433. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  434. if (is_dual_link_lvds(dev_priv, LVDS))
  435. /* LVDS with dual channel */
  436. limit = &intel_limits_g4x_dual_channel_lvds;
  437. else
  438. /* LVDS with dual channel */
  439. limit = &intel_limits_g4x_single_channel_lvds;
  440. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  441. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  442. limit = &intel_limits_g4x_hdmi;
  443. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  444. limit = &intel_limits_g4x_sdvo;
  445. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  446. limit = &intel_limits_g4x_display_port;
  447. } else /* The option is for other outputs */
  448. limit = &intel_limits_i9xx_sdvo;
  449. return limit;
  450. }
  451. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  452. {
  453. struct drm_device *dev = crtc->dev;
  454. const intel_limit_t *limit;
  455. if (HAS_PCH_SPLIT(dev))
  456. limit = intel_ironlake_limit(crtc, refclk);
  457. else if (IS_G4X(dev)) {
  458. limit = intel_g4x_limit(crtc);
  459. } else if (IS_PINEVIEW(dev)) {
  460. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  461. limit = &intel_limits_pineview_lvds;
  462. else
  463. limit = &intel_limits_pineview_sdvo;
  464. } else if (!IS_GEN2(dev)) {
  465. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  466. limit = &intel_limits_i9xx_lvds;
  467. else
  468. limit = &intel_limits_i9xx_sdvo;
  469. } else {
  470. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  471. limit = &intel_limits_i8xx_lvds;
  472. else
  473. limit = &intel_limits_i8xx_dvo;
  474. }
  475. return limit;
  476. }
  477. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  478. static void pineview_clock(int refclk, intel_clock_t *clock)
  479. {
  480. clock->m = clock->m2 + 2;
  481. clock->p = clock->p1 * clock->p2;
  482. clock->vco = refclk * clock->m / clock->n;
  483. clock->dot = clock->vco / clock->p;
  484. }
  485. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  486. {
  487. if (IS_PINEVIEW(dev)) {
  488. pineview_clock(refclk, clock);
  489. return;
  490. }
  491. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  492. clock->p = clock->p1 * clock->p2;
  493. clock->vco = refclk * clock->m / (clock->n + 2);
  494. clock->dot = clock->vco / clock->p;
  495. }
  496. /**
  497. * Returns whether any output on the specified pipe is of the specified type
  498. */
  499. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  500. {
  501. struct drm_device *dev = crtc->dev;
  502. struct drm_mode_config *mode_config = &dev->mode_config;
  503. struct intel_encoder *encoder;
  504. list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
  505. if (encoder->base.crtc == crtc && encoder->type == type)
  506. return true;
  507. return false;
  508. }
  509. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  510. /**
  511. * Returns whether the given set of divisors are valid for a given refclk with
  512. * the given connectors.
  513. */
  514. static bool intel_PLL_is_valid(struct drm_device *dev,
  515. const intel_limit_t *limit,
  516. const intel_clock_t *clock)
  517. {
  518. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  519. INTELPllInvalid("p1 out of range\n");
  520. if (clock->p < limit->p.min || limit->p.max < clock->p)
  521. INTELPllInvalid("p out of range\n");
  522. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  523. INTELPllInvalid("m2 out of range\n");
  524. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  525. INTELPllInvalid("m1 out of range\n");
  526. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  527. INTELPllInvalid("m1 <= m2\n");
  528. if (clock->m < limit->m.min || limit->m.max < clock->m)
  529. INTELPllInvalid("m out of range\n");
  530. if (clock->n < limit->n.min || limit->n.max < clock->n)
  531. INTELPllInvalid("n out of range\n");
  532. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  533. INTELPllInvalid("vco out of range\n");
  534. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  535. * connector, etc., rather than just a single range.
  536. */
  537. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  538. INTELPllInvalid("dot out of range\n");
  539. return true;
  540. }
  541. static bool
  542. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  543. int target, int refclk, intel_clock_t *match_clock,
  544. intel_clock_t *best_clock)
  545. {
  546. struct drm_device *dev = crtc->dev;
  547. struct drm_i915_private *dev_priv = dev->dev_private;
  548. intel_clock_t clock;
  549. int err = target;
  550. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  551. (I915_READ(LVDS)) != 0) {
  552. /*
  553. * For LVDS, if the panel is on, just rely on its current
  554. * settings for dual-channel. We haven't figured out how to
  555. * reliably set up different single/dual channel state, if we
  556. * even can.
  557. */
  558. if (is_dual_link_lvds(dev_priv, LVDS))
  559. clock.p2 = limit->p2.p2_fast;
  560. else
  561. clock.p2 = limit->p2.p2_slow;
  562. } else {
  563. if (target < limit->p2.dot_limit)
  564. clock.p2 = limit->p2.p2_slow;
  565. else
  566. clock.p2 = limit->p2.p2_fast;
  567. }
  568. memset(best_clock, 0, sizeof(*best_clock));
  569. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  570. clock.m1++) {
  571. for (clock.m2 = limit->m2.min;
  572. clock.m2 <= limit->m2.max; clock.m2++) {
  573. /* m1 is always 0 in Pineview */
  574. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  575. break;
  576. for (clock.n = limit->n.min;
  577. clock.n <= limit->n.max; clock.n++) {
  578. for (clock.p1 = limit->p1.min;
  579. clock.p1 <= limit->p1.max; clock.p1++) {
  580. int this_err;
  581. intel_clock(dev, refclk, &clock);
  582. if (!intel_PLL_is_valid(dev, limit,
  583. &clock))
  584. continue;
  585. if (match_clock &&
  586. clock.p != match_clock->p)
  587. continue;
  588. this_err = abs(clock.dot - target);
  589. if (this_err < err) {
  590. *best_clock = clock;
  591. err = this_err;
  592. }
  593. }
  594. }
  595. }
  596. }
  597. return (err != target);
  598. }
  599. static bool
  600. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  601. int target, int refclk, intel_clock_t *match_clock,
  602. intel_clock_t *best_clock)
  603. {
  604. struct drm_device *dev = crtc->dev;
  605. struct drm_i915_private *dev_priv = dev->dev_private;
  606. intel_clock_t clock;
  607. int max_n;
  608. bool found;
  609. /* approximately equals target * 0.00585 */
  610. int err_most = (target >> 8) + (target >> 9);
  611. found = false;
  612. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  613. int lvds_reg;
  614. if (HAS_PCH_SPLIT(dev))
  615. lvds_reg = PCH_LVDS;
  616. else
  617. lvds_reg = LVDS;
  618. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  619. LVDS_CLKB_POWER_UP)
  620. clock.p2 = limit->p2.p2_fast;
  621. else
  622. clock.p2 = limit->p2.p2_slow;
  623. } else {
  624. if (target < limit->p2.dot_limit)
  625. clock.p2 = limit->p2.p2_slow;
  626. else
  627. clock.p2 = limit->p2.p2_fast;
  628. }
  629. memset(best_clock, 0, sizeof(*best_clock));
  630. max_n = limit->n.max;
  631. /* based on hardware requirement, prefer smaller n to precision */
  632. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  633. /* based on hardware requirement, prefere larger m1,m2 */
  634. for (clock.m1 = limit->m1.max;
  635. clock.m1 >= limit->m1.min; clock.m1--) {
  636. for (clock.m2 = limit->m2.max;
  637. clock.m2 >= limit->m2.min; clock.m2--) {
  638. for (clock.p1 = limit->p1.max;
  639. clock.p1 >= limit->p1.min; clock.p1--) {
  640. int this_err;
  641. intel_clock(dev, refclk, &clock);
  642. if (!intel_PLL_is_valid(dev, limit,
  643. &clock))
  644. continue;
  645. if (match_clock &&
  646. clock.p != match_clock->p)
  647. continue;
  648. this_err = abs(clock.dot - target);
  649. if (this_err < err_most) {
  650. *best_clock = clock;
  651. err_most = this_err;
  652. max_n = clock.n;
  653. found = true;
  654. }
  655. }
  656. }
  657. }
  658. }
  659. return found;
  660. }
  661. static bool
  662. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  663. int target, int refclk, intel_clock_t *match_clock,
  664. intel_clock_t *best_clock)
  665. {
  666. struct drm_device *dev = crtc->dev;
  667. intel_clock_t clock;
  668. if (target < 200000) {
  669. clock.n = 1;
  670. clock.p1 = 2;
  671. clock.p2 = 10;
  672. clock.m1 = 12;
  673. clock.m2 = 9;
  674. } else {
  675. clock.n = 2;
  676. clock.p1 = 1;
  677. clock.p2 = 10;
  678. clock.m1 = 14;
  679. clock.m2 = 8;
  680. }
  681. intel_clock(dev, refclk, &clock);
  682. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  683. return true;
  684. }
  685. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  686. static bool
  687. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  688. int target, int refclk, intel_clock_t *match_clock,
  689. intel_clock_t *best_clock)
  690. {
  691. intel_clock_t clock;
  692. if (target < 200000) {
  693. clock.p1 = 2;
  694. clock.p2 = 10;
  695. clock.n = 2;
  696. clock.m1 = 23;
  697. clock.m2 = 8;
  698. } else {
  699. clock.p1 = 1;
  700. clock.p2 = 10;
  701. clock.n = 1;
  702. clock.m1 = 14;
  703. clock.m2 = 2;
  704. }
  705. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  706. clock.p = (clock.p1 * clock.p2);
  707. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  708. clock.vco = 0;
  709. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  710. return true;
  711. }
  712. static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
  713. {
  714. struct drm_i915_private *dev_priv = dev->dev_private;
  715. u32 frame, frame_reg = PIPEFRAME(pipe);
  716. frame = I915_READ(frame_reg);
  717. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  718. DRM_DEBUG_KMS("vblank wait timed out\n");
  719. }
  720. /**
  721. * intel_wait_for_vblank - wait for vblank on a given pipe
  722. * @dev: drm device
  723. * @pipe: pipe to wait for
  724. *
  725. * Wait for vblank to occur on a given pipe. Needed for various bits of
  726. * mode setting code.
  727. */
  728. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  729. {
  730. struct drm_i915_private *dev_priv = dev->dev_private;
  731. int pipestat_reg = PIPESTAT(pipe);
  732. if (INTEL_INFO(dev)->gen >= 5) {
  733. ironlake_wait_for_vblank(dev, pipe);
  734. return;
  735. }
  736. /* Clear existing vblank status. Note this will clear any other
  737. * sticky status fields as well.
  738. *
  739. * This races with i915_driver_irq_handler() with the result
  740. * that either function could miss a vblank event. Here it is not
  741. * fatal, as we will either wait upon the next vblank interrupt or
  742. * timeout. Generally speaking intel_wait_for_vblank() is only
  743. * called during modeset at which time the GPU should be idle and
  744. * should *not* be performing page flips and thus not waiting on
  745. * vblanks...
  746. * Currently, the result of us stealing a vblank from the irq
  747. * handler is that a single frame will be skipped during swapbuffers.
  748. */
  749. I915_WRITE(pipestat_reg,
  750. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  751. /* Wait for vblank interrupt bit to set */
  752. if (wait_for(I915_READ(pipestat_reg) &
  753. PIPE_VBLANK_INTERRUPT_STATUS,
  754. 50))
  755. DRM_DEBUG_KMS("vblank wait timed out\n");
  756. }
  757. /*
  758. * intel_wait_for_pipe_off - wait for pipe to turn off
  759. * @dev: drm device
  760. * @pipe: pipe to wait for
  761. *
  762. * After disabling a pipe, we can't wait for vblank in the usual way,
  763. * spinning on the vblank interrupt status bit, since we won't actually
  764. * see an interrupt when the pipe is disabled.
  765. *
  766. * On Gen4 and above:
  767. * wait for the pipe register state bit to turn off
  768. *
  769. * Otherwise:
  770. * wait for the display line value to settle (it usually
  771. * ends up stopping at the start of the next frame).
  772. *
  773. */
  774. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  775. {
  776. struct drm_i915_private *dev_priv = dev->dev_private;
  777. if (INTEL_INFO(dev)->gen >= 4) {
  778. int reg = PIPECONF(pipe);
  779. /* Wait for the Pipe State to go off */
  780. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  781. 100))
  782. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  783. } else {
  784. u32 last_line, line_mask;
  785. int reg = PIPEDSL(pipe);
  786. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  787. if (IS_GEN2(dev))
  788. line_mask = DSL_LINEMASK_GEN2;
  789. else
  790. line_mask = DSL_LINEMASK_GEN3;
  791. /* Wait for the display line to settle */
  792. do {
  793. last_line = I915_READ(reg) & line_mask;
  794. mdelay(5);
  795. } while (((I915_READ(reg) & line_mask) != last_line) &&
  796. time_after(timeout, jiffies));
  797. if (time_after(jiffies, timeout))
  798. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  799. }
  800. }
  801. static const char *state_string(bool enabled)
  802. {
  803. return enabled ? "on" : "off";
  804. }
  805. /* Only for pre-ILK configs */
  806. static void assert_pll(struct drm_i915_private *dev_priv,
  807. enum pipe pipe, bool state)
  808. {
  809. int reg;
  810. u32 val;
  811. bool cur_state;
  812. reg = DPLL(pipe);
  813. val = I915_READ(reg);
  814. cur_state = !!(val & DPLL_VCO_ENABLE);
  815. WARN(cur_state != state,
  816. "PLL state assertion failure (expected %s, current %s)\n",
  817. state_string(state), state_string(cur_state));
  818. }
  819. #define assert_pll_enabled(d, p) assert_pll(d, p, true)
  820. #define assert_pll_disabled(d, p) assert_pll(d, p, false)
  821. /* For ILK+ */
  822. static void assert_pch_pll(struct drm_i915_private *dev_priv,
  823. struct intel_crtc *intel_crtc, bool state)
  824. {
  825. int reg;
  826. u32 val;
  827. bool cur_state;
  828. if (!intel_crtc->pch_pll) {
  829. WARN(1, "asserting PCH PLL enabled with no PLL\n");
  830. return;
  831. }
  832. if (HAS_PCH_CPT(dev_priv->dev)) {
  833. u32 pch_dpll;
  834. pch_dpll = I915_READ(PCH_DPLL_SEL);
  835. /* Make sure the selected PLL is enabled to the transcoder */
  836. WARN(!((pch_dpll >> (4 * intel_crtc->pipe)) & 8),
  837. "transcoder %d PLL not enabled\n", intel_crtc->pipe);
  838. }
  839. reg = intel_crtc->pch_pll->pll_reg;
  840. val = I915_READ(reg);
  841. cur_state = !!(val & DPLL_VCO_ENABLE);
  842. WARN(cur_state != state,
  843. "PCH PLL state assertion failure (expected %s, current %s)\n",
  844. state_string(state), state_string(cur_state));
  845. }
  846. #define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
  847. #define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
  848. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  849. enum pipe pipe, bool state)
  850. {
  851. int reg;
  852. u32 val;
  853. bool cur_state;
  854. reg = FDI_TX_CTL(pipe);
  855. val = I915_READ(reg);
  856. cur_state = !!(val & FDI_TX_ENABLE);
  857. WARN(cur_state != state,
  858. "FDI TX state assertion failure (expected %s, current %s)\n",
  859. state_string(state), state_string(cur_state));
  860. }
  861. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  862. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  863. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  864. enum pipe pipe, bool state)
  865. {
  866. int reg;
  867. u32 val;
  868. bool cur_state;
  869. reg = FDI_RX_CTL(pipe);
  870. val = I915_READ(reg);
  871. cur_state = !!(val & FDI_RX_ENABLE);
  872. WARN(cur_state != state,
  873. "FDI RX state assertion failure (expected %s, current %s)\n",
  874. state_string(state), state_string(cur_state));
  875. }
  876. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  877. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  878. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  879. enum pipe pipe)
  880. {
  881. int reg;
  882. u32 val;
  883. /* ILK FDI PLL is always enabled */
  884. if (dev_priv->info->gen == 5)
  885. return;
  886. reg = FDI_TX_CTL(pipe);
  887. val = I915_READ(reg);
  888. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  889. }
  890. static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
  891. enum pipe pipe)
  892. {
  893. int reg;
  894. u32 val;
  895. reg = FDI_RX_CTL(pipe);
  896. val = I915_READ(reg);
  897. WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
  898. }
  899. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  900. enum pipe pipe)
  901. {
  902. int pp_reg, lvds_reg;
  903. u32 val;
  904. enum pipe panel_pipe = PIPE_A;
  905. bool locked = true;
  906. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  907. pp_reg = PCH_PP_CONTROL;
  908. lvds_reg = PCH_LVDS;
  909. } else {
  910. pp_reg = PP_CONTROL;
  911. lvds_reg = LVDS;
  912. }
  913. val = I915_READ(pp_reg);
  914. if (!(val & PANEL_POWER_ON) ||
  915. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  916. locked = false;
  917. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  918. panel_pipe = PIPE_B;
  919. WARN(panel_pipe == pipe && locked,
  920. "panel assertion failure, pipe %c regs locked\n",
  921. pipe_name(pipe));
  922. }
  923. void assert_pipe(struct drm_i915_private *dev_priv,
  924. enum pipe pipe, bool state)
  925. {
  926. int reg;
  927. u32 val;
  928. bool cur_state;
  929. /* if we need the pipe A quirk it must be always on */
  930. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  931. state = true;
  932. reg = PIPECONF(pipe);
  933. val = I915_READ(reg);
  934. cur_state = !!(val & PIPECONF_ENABLE);
  935. WARN(cur_state != state,
  936. "pipe %c assertion failure (expected %s, current %s)\n",
  937. pipe_name(pipe), state_string(state), state_string(cur_state));
  938. }
  939. static void assert_plane(struct drm_i915_private *dev_priv,
  940. enum plane plane, bool state)
  941. {
  942. int reg;
  943. u32 val;
  944. bool cur_state;
  945. reg = DSPCNTR(plane);
  946. val = I915_READ(reg);
  947. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  948. WARN(cur_state != state,
  949. "plane %c assertion failure (expected %s, current %s)\n",
  950. plane_name(plane), state_string(state), state_string(cur_state));
  951. }
  952. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  953. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  954. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  955. enum pipe pipe)
  956. {
  957. int reg, i;
  958. u32 val;
  959. int cur_pipe;
  960. /* Planes are fixed to pipes on ILK+ */
  961. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  962. reg = DSPCNTR(pipe);
  963. val = I915_READ(reg);
  964. WARN((val & DISPLAY_PLANE_ENABLE),
  965. "plane %c assertion failure, should be disabled but not\n",
  966. plane_name(pipe));
  967. return;
  968. }
  969. /* Need to check both planes against the pipe */
  970. for (i = 0; i < 2; i++) {
  971. reg = DSPCNTR(i);
  972. val = I915_READ(reg);
  973. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  974. DISPPLANE_SEL_PIPE_SHIFT;
  975. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  976. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  977. plane_name(i), pipe_name(pipe));
  978. }
  979. }
  980. static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  981. {
  982. u32 val;
  983. bool enabled;
  984. val = I915_READ(PCH_DREF_CONTROL);
  985. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  986. DREF_SUPERSPREAD_SOURCE_MASK));
  987. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  988. }
  989. static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
  990. enum pipe pipe)
  991. {
  992. int reg;
  993. u32 val;
  994. bool enabled;
  995. reg = TRANSCONF(pipe);
  996. val = I915_READ(reg);
  997. enabled = !!(val & TRANS_ENABLE);
  998. WARN(enabled,
  999. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1000. pipe_name(pipe));
  1001. }
  1002. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1003. enum pipe pipe, u32 port_sel, u32 val)
  1004. {
  1005. if ((val & DP_PORT_EN) == 0)
  1006. return false;
  1007. if (HAS_PCH_CPT(dev_priv->dev)) {
  1008. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1009. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1010. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1011. return false;
  1012. } else {
  1013. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1014. return false;
  1015. }
  1016. return true;
  1017. }
  1018. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1019. enum pipe pipe, u32 val)
  1020. {
  1021. if ((val & PORT_ENABLE) == 0)
  1022. return false;
  1023. if (HAS_PCH_CPT(dev_priv->dev)) {
  1024. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1025. return false;
  1026. } else {
  1027. if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
  1028. return false;
  1029. }
  1030. return true;
  1031. }
  1032. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1033. enum pipe pipe, u32 val)
  1034. {
  1035. if ((val & LVDS_PORT_EN) == 0)
  1036. return false;
  1037. if (HAS_PCH_CPT(dev_priv->dev)) {
  1038. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1039. return false;
  1040. } else {
  1041. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1042. return false;
  1043. }
  1044. return true;
  1045. }
  1046. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1047. enum pipe pipe, u32 val)
  1048. {
  1049. if ((val & ADPA_DAC_ENABLE) == 0)
  1050. return false;
  1051. if (HAS_PCH_CPT(dev_priv->dev)) {
  1052. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1053. return false;
  1054. } else {
  1055. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1056. return false;
  1057. }
  1058. return true;
  1059. }
  1060. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1061. enum pipe pipe, int reg, u32 port_sel)
  1062. {
  1063. u32 val = I915_READ(reg);
  1064. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1065. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1066. reg, pipe_name(pipe));
  1067. }
  1068. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1069. enum pipe pipe, int reg)
  1070. {
  1071. u32 val = I915_READ(reg);
  1072. WARN(hdmi_pipe_enabled(dev_priv, val, pipe),
  1073. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1074. reg, pipe_name(pipe));
  1075. }
  1076. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1077. enum pipe pipe)
  1078. {
  1079. int reg;
  1080. u32 val;
  1081. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1082. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1083. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1084. reg = PCH_ADPA;
  1085. val = I915_READ(reg);
  1086. WARN(adpa_pipe_enabled(dev_priv, val, pipe),
  1087. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1088. pipe_name(pipe));
  1089. reg = PCH_LVDS;
  1090. val = I915_READ(reg);
  1091. WARN(lvds_pipe_enabled(dev_priv, val, pipe),
  1092. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1093. pipe_name(pipe));
  1094. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
  1095. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
  1096. assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
  1097. }
  1098. /**
  1099. * intel_enable_pll - enable a PLL
  1100. * @dev_priv: i915 private structure
  1101. * @pipe: pipe PLL to enable
  1102. *
  1103. * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
  1104. * make sure the PLL reg is writable first though, since the panel write
  1105. * protect mechanism may be enabled.
  1106. *
  1107. * Note! This is for pre-ILK only.
  1108. */
  1109. static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1110. {
  1111. int reg;
  1112. u32 val;
  1113. /* No really, not for ILK+ */
  1114. BUG_ON(dev_priv->info->gen >= 5);
  1115. /* PLL is protected by panel, make sure we can write it */
  1116. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1117. assert_panel_unlocked(dev_priv, pipe);
  1118. reg = DPLL(pipe);
  1119. val = I915_READ(reg);
  1120. val |= DPLL_VCO_ENABLE;
  1121. /* We do this three times for luck */
  1122. I915_WRITE(reg, val);
  1123. POSTING_READ(reg);
  1124. udelay(150); /* wait for warmup */
  1125. I915_WRITE(reg, val);
  1126. POSTING_READ(reg);
  1127. udelay(150); /* wait for warmup */
  1128. I915_WRITE(reg, val);
  1129. POSTING_READ(reg);
  1130. udelay(150); /* wait for warmup */
  1131. }
  1132. /**
  1133. * intel_disable_pll - disable a PLL
  1134. * @dev_priv: i915 private structure
  1135. * @pipe: pipe PLL to disable
  1136. *
  1137. * Disable the PLL for @pipe, making sure the pipe is off first.
  1138. *
  1139. * Note! This is for pre-ILK only.
  1140. */
  1141. static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1142. {
  1143. int reg;
  1144. u32 val;
  1145. /* Don't disable pipe A or pipe A PLLs if needed */
  1146. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1147. return;
  1148. /* Make sure the pipe isn't still relying on us */
  1149. assert_pipe_disabled(dev_priv, pipe);
  1150. reg = DPLL(pipe);
  1151. val = I915_READ(reg);
  1152. val &= ~DPLL_VCO_ENABLE;
  1153. I915_WRITE(reg, val);
  1154. POSTING_READ(reg);
  1155. }
  1156. /**
  1157. * intel_enable_pch_pll - enable PCH PLL
  1158. * @dev_priv: i915 private structure
  1159. * @pipe: pipe PLL to enable
  1160. *
  1161. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1162. * drives the transcoder clock.
  1163. */
  1164. static void intel_enable_pch_pll(struct intel_crtc *intel_crtc)
  1165. {
  1166. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  1167. struct intel_pch_pll *pll = intel_crtc->pch_pll;
  1168. int reg;
  1169. u32 val;
  1170. /* PCH only available on ILK+ */
  1171. BUG_ON(dev_priv->info->gen < 5);
  1172. BUG_ON(pll == NULL);
  1173. BUG_ON(pll->refcount == 0);
  1174. DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
  1175. pll->pll_reg, pll->active, pll->on,
  1176. intel_crtc->base.base.id);
  1177. /* PCH refclock must be enabled first */
  1178. assert_pch_refclk_enabled(dev_priv);
  1179. if (pll->active++ && pll->on) {
  1180. assert_pch_pll_enabled(dev_priv, intel_crtc);
  1181. return;
  1182. }
  1183. DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
  1184. reg = pll->pll_reg;
  1185. val = I915_READ(reg);
  1186. val |= DPLL_VCO_ENABLE;
  1187. I915_WRITE(reg, val);
  1188. POSTING_READ(reg);
  1189. udelay(200);
  1190. pll->on = true;
  1191. }
  1192. static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
  1193. {
  1194. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  1195. struct intel_pch_pll *pll = intel_crtc->pch_pll;
  1196. int reg;
  1197. u32 val;
  1198. /* PCH only available on ILK+ */
  1199. BUG_ON(dev_priv->info->gen < 5);
  1200. if (pll == NULL)
  1201. return;
  1202. BUG_ON(pll->refcount == 0);
  1203. DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
  1204. pll->pll_reg, pll->active, pll->on,
  1205. intel_crtc->base.base.id);
  1206. BUG_ON(pll->active == 0);
  1207. if (--pll->active) {
  1208. assert_pch_pll_enabled(dev_priv, intel_crtc);
  1209. return;
  1210. }
  1211. DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
  1212. /* Make sure transcoder isn't still depending on us */
  1213. assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
  1214. reg = pll->pll_reg;
  1215. val = I915_READ(reg);
  1216. val &= ~DPLL_VCO_ENABLE;
  1217. I915_WRITE(reg, val);
  1218. POSTING_READ(reg);
  1219. udelay(200);
  1220. pll->on = false;
  1221. }
  1222. static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
  1223. enum pipe pipe)
  1224. {
  1225. int reg;
  1226. u32 val, pipeconf_val;
  1227. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1228. /* PCH only available on ILK+ */
  1229. BUG_ON(dev_priv->info->gen < 5);
  1230. /* Make sure PCH DPLL is enabled */
  1231. assert_pch_pll_enabled(dev_priv, to_intel_crtc(crtc));
  1232. /* FDI must be feeding us bits for PCH ports */
  1233. assert_fdi_tx_enabled(dev_priv, pipe);
  1234. assert_fdi_rx_enabled(dev_priv, pipe);
  1235. reg = TRANSCONF(pipe);
  1236. val = I915_READ(reg);
  1237. pipeconf_val = I915_READ(PIPECONF(pipe));
  1238. if (HAS_PCH_IBX(dev_priv->dev)) {
  1239. /*
  1240. * make the BPC in transcoder be consistent with
  1241. * that in pipeconf reg.
  1242. */
  1243. val &= ~PIPE_BPC_MASK;
  1244. val |= pipeconf_val & PIPE_BPC_MASK;
  1245. }
  1246. val &= ~TRANS_INTERLACE_MASK;
  1247. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1248. if (HAS_PCH_IBX(dev_priv->dev) &&
  1249. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1250. val |= TRANS_LEGACY_INTERLACED_ILK;
  1251. else
  1252. val |= TRANS_INTERLACED;
  1253. else
  1254. val |= TRANS_PROGRESSIVE;
  1255. I915_WRITE(reg, val | TRANS_ENABLE);
  1256. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1257. DRM_ERROR("failed to enable transcoder %d\n", pipe);
  1258. }
  1259. static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
  1260. enum pipe pipe)
  1261. {
  1262. int reg;
  1263. u32 val;
  1264. /* FDI relies on the transcoder */
  1265. assert_fdi_tx_disabled(dev_priv, pipe);
  1266. assert_fdi_rx_disabled(dev_priv, pipe);
  1267. /* Ports must be off as well */
  1268. assert_pch_ports_disabled(dev_priv, pipe);
  1269. reg = TRANSCONF(pipe);
  1270. val = I915_READ(reg);
  1271. val &= ~TRANS_ENABLE;
  1272. I915_WRITE(reg, val);
  1273. /* wait for PCH transcoder off, transcoder state */
  1274. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1275. DRM_ERROR("failed to disable transcoder %d\n", pipe);
  1276. }
  1277. /**
  1278. * intel_enable_pipe - enable a pipe, asserting requirements
  1279. * @dev_priv: i915 private structure
  1280. * @pipe: pipe to enable
  1281. * @pch_port: on ILK+, is this pipe driving a PCH port or not
  1282. *
  1283. * Enable @pipe, making sure that various hardware specific requirements
  1284. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1285. *
  1286. * @pipe should be %PIPE_A or %PIPE_B.
  1287. *
  1288. * Will wait until the pipe is actually running (i.e. first vblank) before
  1289. * returning.
  1290. */
  1291. static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
  1292. bool pch_port)
  1293. {
  1294. int reg;
  1295. u32 val;
  1296. /*
  1297. * A pipe without a PLL won't actually be able to drive bits from
  1298. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1299. * need the check.
  1300. */
  1301. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1302. assert_pll_enabled(dev_priv, pipe);
  1303. else {
  1304. if (pch_port) {
  1305. /* if driving the PCH, we need FDI enabled */
  1306. assert_fdi_rx_pll_enabled(dev_priv, pipe);
  1307. assert_fdi_tx_pll_enabled(dev_priv, pipe);
  1308. }
  1309. /* FIXME: assert CPU port conditions for SNB+ */
  1310. }
  1311. reg = PIPECONF(pipe);
  1312. val = I915_READ(reg);
  1313. if (val & PIPECONF_ENABLE)
  1314. return;
  1315. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1316. intel_wait_for_vblank(dev_priv->dev, pipe);
  1317. }
  1318. /**
  1319. * intel_disable_pipe - disable a pipe, asserting requirements
  1320. * @dev_priv: i915 private structure
  1321. * @pipe: pipe to disable
  1322. *
  1323. * Disable @pipe, making sure that various hardware specific requirements
  1324. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1325. *
  1326. * @pipe should be %PIPE_A or %PIPE_B.
  1327. *
  1328. * Will wait until the pipe has shut down before returning.
  1329. */
  1330. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1331. enum pipe pipe)
  1332. {
  1333. int reg;
  1334. u32 val;
  1335. /*
  1336. * Make sure planes won't keep trying to pump pixels to us,
  1337. * or we might hang the display.
  1338. */
  1339. assert_planes_disabled(dev_priv, pipe);
  1340. /* Don't disable pipe A or pipe A PLLs if needed */
  1341. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1342. return;
  1343. reg = PIPECONF(pipe);
  1344. val = I915_READ(reg);
  1345. if ((val & PIPECONF_ENABLE) == 0)
  1346. return;
  1347. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1348. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1349. }
  1350. /*
  1351. * Plane regs are double buffered, going from enabled->disabled needs a
  1352. * trigger in order to latch. The display address reg provides this.
  1353. */
  1354. void intel_flush_display_plane(struct drm_i915_private *dev_priv,
  1355. enum plane plane)
  1356. {
  1357. I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
  1358. I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
  1359. }
  1360. /**
  1361. * intel_enable_plane - enable a display plane on a given pipe
  1362. * @dev_priv: i915 private structure
  1363. * @plane: plane to enable
  1364. * @pipe: pipe being fed
  1365. *
  1366. * Enable @plane on @pipe, making sure that @pipe is running first.
  1367. */
  1368. static void intel_enable_plane(struct drm_i915_private *dev_priv,
  1369. enum plane plane, enum pipe pipe)
  1370. {
  1371. int reg;
  1372. u32 val;
  1373. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1374. assert_pipe_enabled(dev_priv, pipe);
  1375. reg = DSPCNTR(plane);
  1376. val = I915_READ(reg);
  1377. if (val & DISPLAY_PLANE_ENABLE)
  1378. return;
  1379. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1380. intel_flush_display_plane(dev_priv, plane);
  1381. intel_wait_for_vblank(dev_priv->dev, pipe);
  1382. }
  1383. /**
  1384. * intel_disable_plane - disable a display plane
  1385. * @dev_priv: i915 private structure
  1386. * @plane: plane to disable
  1387. * @pipe: pipe consuming the data
  1388. *
  1389. * Disable @plane; should be an independent operation.
  1390. */
  1391. static void intel_disable_plane(struct drm_i915_private *dev_priv,
  1392. enum plane plane, enum pipe pipe)
  1393. {
  1394. int reg;
  1395. u32 val;
  1396. reg = DSPCNTR(plane);
  1397. val = I915_READ(reg);
  1398. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  1399. return;
  1400. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1401. intel_flush_display_plane(dev_priv, plane);
  1402. intel_wait_for_vblank(dev_priv->dev, pipe);
  1403. }
  1404. static void disable_pch_dp(struct drm_i915_private *dev_priv,
  1405. enum pipe pipe, int reg, u32 port_sel)
  1406. {
  1407. u32 val = I915_READ(reg);
  1408. if (dp_pipe_enabled(dev_priv, pipe, port_sel, val)) {
  1409. DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
  1410. I915_WRITE(reg, val & ~DP_PORT_EN);
  1411. }
  1412. }
  1413. static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
  1414. enum pipe pipe, int reg)
  1415. {
  1416. u32 val = I915_READ(reg);
  1417. if (hdmi_pipe_enabled(dev_priv, val, pipe)) {
  1418. DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
  1419. reg, pipe);
  1420. I915_WRITE(reg, val & ~PORT_ENABLE);
  1421. }
  1422. }
  1423. /* Disable any ports connected to this transcoder */
  1424. static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
  1425. enum pipe pipe)
  1426. {
  1427. u32 reg, val;
  1428. val = I915_READ(PCH_PP_CONTROL);
  1429. I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
  1430. disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1431. disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1432. disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1433. reg = PCH_ADPA;
  1434. val = I915_READ(reg);
  1435. if (adpa_pipe_enabled(dev_priv, val, pipe))
  1436. I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
  1437. reg = PCH_LVDS;
  1438. val = I915_READ(reg);
  1439. if (lvds_pipe_enabled(dev_priv, val, pipe)) {
  1440. DRM_DEBUG_KMS("disable lvds on pipe %d val 0x%08x\n", pipe, val);
  1441. I915_WRITE(reg, val & ~LVDS_PORT_EN);
  1442. POSTING_READ(reg);
  1443. udelay(100);
  1444. }
  1445. disable_pch_hdmi(dev_priv, pipe, HDMIB);
  1446. disable_pch_hdmi(dev_priv, pipe, HDMIC);
  1447. disable_pch_hdmi(dev_priv, pipe, HDMID);
  1448. }
  1449. int
  1450. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1451. struct drm_i915_gem_object *obj,
  1452. struct intel_ring_buffer *pipelined)
  1453. {
  1454. struct drm_i915_private *dev_priv = dev->dev_private;
  1455. u32 alignment;
  1456. int ret;
  1457. switch (obj->tiling_mode) {
  1458. case I915_TILING_NONE:
  1459. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1460. alignment = 128 * 1024;
  1461. else if (INTEL_INFO(dev)->gen >= 4)
  1462. alignment = 4 * 1024;
  1463. else
  1464. alignment = 64 * 1024;
  1465. break;
  1466. case I915_TILING_X:
  1467. /* pin() will align the object as required by fence */
  1468. alignment = 0;
  1469. break;
  1470. case I915_TILING_Y:
  1471. /* FIXME: Is this true? */
  1472. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1473. return -EINVAL;
  1474. default:
  1475. BUG();
  1476. }
  1477. dev_priv->mm.interruptible = false;
  1478. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1479. if (ret)
  1480. goto err_interruptible;
  1481. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1482. * fence, whereas 965+ only requires a fence if using
  1483. * framebuffer compression. For simplicity, we always install
  1484. * a fence as the cost is not that onerous.
  1485. */
  1486. ret = i915_gem_object_get_fence(obj);
  1487. if (ret)
  1488. goto err_unpin;
  1489. i915_gem_object_pin_fence(obj);
  1490. dev_priv->mm.interruptible = true;
  1491. return 0;
  1492. err_unpin:
  1493. i915_gem_object_unpin(obj);
  1494. err_interruptible:
  1495. dev_priv->mm.interruptible = true;
  1496. return ret;
  1497. }
  1498. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1499. {
  1500. i915_gem_object_unpin_fence(obj);
  1501. i915_gem_object_unpin(obj);
  1502. }
  1503. static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1504. int x, int y)
  1505. {
  1506. struct drm_device *dev = crtc->dev;
  1507. struct drm_i915_private *dev_priv = dev->dev_private;
  1508. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1509. struct intel_framebuffer *intel_fb;
  1510. struct drm_i915_gem_object *obj;
  1511. int plane = intel_crtc->plane;
  1512. unsigned long Start, Offset;
  1513. u32 dspcntr;
  1514. u32 reg;
  1515. switch (plane) {
  1516. case 0:
  1517. case 1:
  1518. break;
  1519. default:
  1520. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1521. return -EINVAL;
  1522. }
  1523. intel_fb = to_intel_framebuffer(fb);
  1524. obj = intel_fb->obj;
  1525. reg = DSPCNTR(plane);
  1526. dspcntr = I915_READ(reg);
  1527. /* Mask out pixel format bits in case we change it */
  1528. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1529. switch (fb->bits_per_pixel) {
  1530. case 8:
  1531. dspcntr |= DISPPLANE_8BPP;
  1532. break;
  1533. case 16:
  1534. if (fb->depth == 15)
  1535. dspcntr |= DISPPLANE_15_16BPP;
  1536. else
  1537. dspcntr |= DISPPLANE_16BPP;
  1538. break;
  1539. case 24:
  1540. case 32:
  1541. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1542. break;
  1543. default:
  1544. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1545. return -EINVAL;
  1546. }
  1547. if (INTEL_INFO(dev)->gen >= 4) {
  1548. if (obj->tiling_mode != I915_TILING_NONE)
  1549. dspcntr |= DISPPLANE_TILED;
  1550. else
  1551. dspcntr &= ~DISPPLANE_TILED;
  1552. }
  1553. I915_WRITE(reg, dspcntr);
  1554. Start = obj->gtt_offset;
  1555. Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1556. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1557. Start, Offset, x, y, fb->pitches[0]);
  1558. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1559. if (INTEL_INFO(dev)->gen >= 4) {
  1560. I915_MODIFY_DISPBASE(DSPSURF(plane), Start);
  1561. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1562. I915_WRITE(DSPADDR(plane), Offset);
  1563. } else
  1564. I915_WRITE(DSPADDR(plane), Start + Offset);
  1565. POSTING_READ(reg);
  1566. return 0;
  1567. }
  1568. static int ironlake_update_plane(struct drm_crtc *crtc,
  1569. struct drm_framebuffer *fb, int x, int y)
  1570. {
  1571. struct drm_device *dev = crtc->dev;
  1572. struct drm_i915_private *dev_priv = dev->dev_private;
  1573. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1574. struct intel_framebuffer *intel_fb;
  1575. struct drm_i915_gem_object *obj;
  1576. int plane = intel_crtc->plane;
  1577. unsigned long Start, Offset;
  1578. u32 dspcntr;
  1579. u32 reg;
  1580. switch (plane) {
  1581. case 0:
  1582. case 1:
  1583. case 2:
  1584. break;
  1585. default:
  1586. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1587. return -EINVAL;
  1588. }
  1589. intel_fb = to_intel_framebuffer(fb);
  1590. obj = intel_fb->obj;
  1591. reg = DSPCNTR(plane);
  1592. dspcntr = I915_READ(reg);
  1593. /* Mask out pixel format bits in case we change it */
  1594. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1595. switch (fb->bits_per_pixel) {
  1596. case 8:
  1597. dspcntr |= DISPPLANE_8BPP;
  1598. break;
  1599. case 16:
  1600. if (fb->depth != 16)
  1601. return -EINVAL;
  1602. dspcntr |= DISPPLANE_16BPP;
  1603. break;
  1604. case 24:
  1605. case 32:
  1606. if (fb->depth == 24)
  1607. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1608. else if (fb->depth == 30)
  1609. dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
  1610. else
  1611. return -EINVAL;
  1612. break;
  1613. default:
  1614. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1615. return -EINVAL;
  1616. }
  1617. if (obj->tiling_mode != I915_TILING_NONE)
  1618. dspcntr |= DISPPLANE_TILED;
  1619. else
  1620. dspcntr &= ~DISPPLANE_TILED;
  1621. /* must disable */
  1622. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1623. I915_WRITE(reg, dspcntr);
  1624. Start = obj->gtt_offset;
  1625. Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  1626. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1627. Start, Offset, x, y, fb->pitches[0]);
  1628. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  1629. I915_MODIFY_DISPBASE(DSPSURF(plane), Start);
  1630. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1631. I915_WRITE(DSPADDR(plane), Offset);
  1632. POSTING_READ(reg);
  1633. return 0;
  1634. }
  1635. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1636. static int
  1637. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1638. int x, int y, enum mode_set_atomic state)
  1639. {
  1640. struct drm_device *dev = crtc->dev;
  1641. struct drm_i915_private *dev_priv = dev->dev_private;
  1642. if (dev_priv->display.disable_fbc)
  1643. dev_priv->display.disable_fbc(dev);
  1644. intel_increase_pllclock(crtc);
  1645. return dev_priv->display.update_plane(crtc, fb, x, y);
  1646. }
  1647. static int
  1648. intel_finish_fb(struct drm_framebuffer *old_fb)
  1649. {
  1650. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1651. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1652. bool was_interruptible = dev_priv->mm.interruptible;
  1653. int ret;
  1654. wait_event(dev_priv->pending_flip_queue,
  1655. atomic_read(&dev_priv->mm.wedged) ||
  1656. atomic_read(&obj->pending_flip) == 0);
  1657. /* Big Hammer, we also need to ensure that any pending
  1658. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1659. * current scanout is retired before unpinning the old
  1660. * framebuffer.
  1661. *
  1662. * This should only fail upon a hung GPU, in which case we
  1663. * can safely continue.
  1664. */
  1665. dev_priv->mm.interruptible = false;
  1666. ret = i915_gem_object_finish_gpu(obj);
  1667. dev_priv->mm.interruptible = was_interruptible;
  1668. return ret;
  1669. }
  1670. static int
  1671. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1672. struct drm_framebuffer *old_fb)
  1673. {
  1674. struct drm_device *dev = crtc->dev;
  1675. struct drm_i915_private *dev_priv = dev->dev_private;
  1676. struct drm_i915_master_private *master_priv;
  1677. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1678. int ret;
  1679. /* no fb bound */
  1680. if (!crtc->fb) {
  1681. DRM_ERROR("No FB bound\n");
  1682. return 0;
  1683. }
  1684. switch (intel_crtc->plane) {
  1685. case 0:
  1686. case 1:
  1687. break;
  1688. case 2:
  1689. if (IS_IVYBRIDGE(dev))
  1690. break;
  1691. /* fall through otherwise */
  1692. default:
  1693. DRM_ERROR("no plane for crtc\n");
  1694. return -EINVAL;
  1695. }
  1696. mutex_lock(&dev->struct_mutex);
  1697. ret = intel_pin_and_fence_fb_obj(dev,
  1698. to_intel_framebuffer(crtc->fb)->obj,
  1699. NULL);
  1700. if (ret != 0) {
  1701. mutex_unlock(&dev->struct_mutex);
  1702. DRM_ERROR("pin & fence failed\n");
  1703. return ret;
  1704. }
  1705. if (old_fb)
  1706. intel_finish_fb(old_fb);
  1707. ret = dev_priv->display.update_plane(crtc, crtc->fb, x, y);
  1708. if (ret) {
  1709. intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
  1710. mutex_unlock(&dev->struct_mutex);
  1711. DRM_ERROR("failed to update base address\n");
  1712. return ret;
  1713. }
  1714. if (old_fb) {
  1715. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1716. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  1717. }
  1718. intel_update_fbc(dev);
  1719. mutex_unlock(&dev->struct_mutex);
  1720. if (!dev->primary->master)
  1721. return 0;
  1722. master_priv = dev->primary->master->driver_priv;
  1723. if (!master_priv->sarea_priv)
  1724. return 0;
  1725. if (intel_crtc->pipe) {
  1726. master_priv->sarea_priv->pipeB_x = x;
  1727. master_priv->sarea_priv->pipeB_y = y;
  1728. } else {
  1729. master_priv->sarea_priv->pipeA_x = x;
  1730. master_priv->sarea_priv->pipeA_y = y;
  1731. }
  1732. return 0;
  1733. }
  1734. static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
  1735. {
  1736. struct drm_device *dev = crtc->dev;
  1737. struct drm_i915_private *dev_priv = dev->dev_private;
  1738. u32 dpa_ctl;
  1739. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1740. dpa_ctl = I915_READ(DP_A);
  1741. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1742. if (clock < 200000) {
  1743. u32 temp;
  1744. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1745. /* workaround for 160Mhz:
  1746. 1) program 0x4600c bits 15:0 = 0x8124
  1747. 2) program 0x46010 bit 0 = 1
  1748. 3) program 0x46034 bit 24 = 1
  1749. 4) program 0x64000 bit 14 = 1
  1750. */
  1751. temp = I915_READ(0x4600c);
  1752. temp &= 0xffff0000;
  1753. I915_WRITE(0x4600c, temp | 0x8124);
  1754. temp = I915_READ(0x46010);
  1755. I915_WRITE(0x46010, temp | 1);
  1756. temp = I915_READ(0x46034);
  1757. I915_WRITE(0x46034, temp | (1 << 24));
  1758. } else {
  1759. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1760. }
  1761. I915_WRITE(DP_A, dpa_ctl);
  1762. POSTING_READ(DP_A);
  1763. udelay(500);
  1764. }
  1765. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  1766. {
  1767. struct drm_device *dev = crtc->dev;
  1768. struct drm_i915_private *dev_priv = dev->dev_private;
  1769. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1770. int pipe = intel_crtc->pipe;
  1771. u32 reg, temp;
  1772. /* enable normal train */
  1773. reg = FDI_TX_CTL(pipe);
  1774. temp = I915_READ(reg);
  1775. if (IS_IVYBRIDGE(dev)) {
  1776. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  1777. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  1778. } else {
  1779. temp &= ~FDI_LINK_TRAIN_NONE;
  1780. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  1781. }
  1782. I915_WRITE(reg, temp);
  1783. reg = FDI_RX_CTL(pipe);
  1784. temp = I915_READ(reg);
  1785. if (HAS_PCH_CPT(dev)) {
  1786. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1787. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  1788. } else {
  1789. temp &= ~FDI_LINK_TRAIN_NONE;
  1790. temp |= FDI_LINK_TRAIN_NONE;
  1791. }
  1792. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  1793. /* wait one idle pattern time */
  1794. POSTING_READ(reg);
  1795. udelay(1000);
  1796. /* IVB wants error correction enabled */
  1797. if (IS_IVYBRIDGE(dev))
  1798. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  1799. FDI_FE_ERRC_ENABLE);
  1800. }
  1801. static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
  1802. {
  1803. struct drm_i915_private *dev_priv = dev->dev_private;
  1804. u32 flags = I915_READ(SOUTH_CHICKEN1);
  1805. flags |= FDI_PHASE_SYNC_OVR(pipe);
  1806. I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
  1807. flags |= FDI_PHASE_SYNC_EN(pipe);
  1808. I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
  1809. POSTING_READ(SOUTH_CHICKEN1);
  1810. }
  1811. /* The FDI link training functions for ILK/Ibexpeak. */
  1812. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  1813. {
  1814. struct drm_device *dev = crtc->dev;
  1815. struct drm_i915_private *dev_priv = dev->dev_private;
  1816. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1817. int pipe = intel_crtc->pipe;
  1818. int plane = intel_crtc->plane;
  1819. u32 reg, temp, tries;
  1820. /* FDI needs bits from pipe & plane first */
  1821. assert_pipe_enabled(dev_priv, pipe);
  1822. assert_plane_enabled(dev_priv, plane);
  1823. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1824. for train result */
  1825. reg = FDI_RX_IMR(pipe);
  1826. temp = I915_READ(reg);
  1827. temp &= ~FDI_RX_SYMBOL_LOCK;
  1828. temp &= ~FDI_RX_BIT_LOCK;
  1829. I915_WRITE(reg, temp);
  1830. I915_READ(reg);
  1831. udelay(150);
  1832. /* enable CPU FDI TX and PCH FDI RX */
  1833. reg = FDI_TX_CTL(pipe);
  1834. temp = I915_READ(reg);
  1835. temp &= ~(7 << 19);
  1836. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1837. temp &= ~FDI_LINK_TRAIN_NONE;
  1838. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1839. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  1840. reg = FDI_RX_CTL(pipe);
  1841. temp = I915_READ(reg);
  1842. temp &= ~FDI_LINK_TRAIN_NONE;
  1843. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1844. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  1845. POSTING_READ(reg);
  1846. udelay(150);
  1847. /* Ironlake workaround, enable clock pointer after FDI enable*/
  1848. if (HAS_PCH_IBX(dev)) {
  1849. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  1850. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  1851. FDI_RX_PHASE_SYNC_POINTER_EN);
  1852. }
  1853. reg = FDI_RX_IIR(pipe);
  1854. for (tries = 0; tries < 5; tries++) {
  1855. temp = I915_READ(reg);
  1856. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1857. if ((temp & FDI_RX_BIT_LOCK)) {
  1858. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1859. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  1860. break;
  1861. }
  1862. }
  1863. if (tries == 5)
  1864. DRM_ERROR("FDI train 1 fail!\n");
  1865. /* Train 2 */
  1866. reg = FDI_TX_CTL(pipe);
  1867. temp = I915_READ(reg);
  1868. temp &= ~FDI_LINK_TRAIN_NONE;
  1869. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1870. I915_WRITE(reg, temp);
  1871. reg = FDI_RX_CTL(pipe);
  1872. temp = I915_READ(reg);
  1873. temp &= ~FDI_LINK_TRAIN_NONE;
  1874. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1875. I915_WRITE(reg, temp);
  1876. POSTING_READ(reg);
  1877. udelay(150);
  1878. reg = FDI_RX_IIR(pipe);
  1879. for (tries = 0; tries < 5; tries++) {
  1880. temp = I915_READ(reg);
  1881. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1882. if (temp & FDI_RX_SYMBOL_LOCK) {
  1883. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  1884. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1885. break;
  1886. }
  1887. }
  1888. if (tries == 5)
  1889. DRM_ERROR("FDI train 2 fail!\n");
  1890. DRM_DEBUG_KMS("FDI train done\n");
  1891. }
  1892. static const int snb_b_fdi_train_param[] = {
  1893. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  1894. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  1895. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  1896. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  1897. };
  1898. /* The FDI link training functions for SNB/Cougarpoint. */
  1899. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  1900. {
  1901. struct drm_device *dev = crtc->dev;
  1902. struct drm_i915_private *dev_priv = dev->dev_private;
  1903. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1904. int pipe = intel_crtc->pipe;
  1905. u32 reg, temp, i, retry;
  1906. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1907. for train result */
  1908. reg = FDI_RX_IMR(pipe);
  1909. temp = I915_READ(reg);
  1910. temp &= ~FDI_RX_SYMBOL_LOCK;
  1911. temp &= ~FDI_RX_BIT_LOCK;
  1912. I915_WRITE(reg, temp);
  1913. POSTING_READ(reg);
  1914. udelay(150);
  1915. /* enable CPU FDI TX and PCH FDI RX */
  1916. reg = FDI_TX_CTL(pipe);
  1917. temp = I915_READ(reg);
  1918. temp &= ~(7 << 19);
  1919. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1920. temp &= ~FDI_LINK_TRAIN_NONE;
  1921. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1922. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1923. /* SNB-B */
  1924. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1925. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  1926. reg = FDI_RX_CTL(pipe);
  1927. temp = I915_READ(reg);
  1928. if (HAS_PCH_CPT(dev)) {
  1929. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1930. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1931. } else {
  1932. temp &= ~FDI_LINK_TRAIN_NONE;
  1933. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1934. }
  1935. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  1936. POSTING_READ(reg);
  1937. udelay(150);
  1938. if (HAS_PCH_CPT(dev))
  1939. cpt_phase_pointer_enable(dev, pipe);
  1940. for (i = 0; i < 4; i++) {
  1941. reg = FDI_TX_CTL(pipe);
  1942. temp = I915_READ(reg);
  1943. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1944. temp |= snb_b_fdi_train_param[i];
  1945. I915_WRITE(reg, temp);
  1946. POSTING_READ(reg);
  1947. udelay(500);
  1948. for (retry = 0; retry < 5; retry++) {
  1949. reg = FDI_RX_IIR(pipe);
  1950. temp = I915_READ(reg);
  1951. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1952. if (temp & FDI_RX_BIT_LOCK) {
  1953. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  1954. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1955. break;
  1956. }
  1957. udelay(50);
  1958. }
  1959. if (retry < 5)
  1960. break;
  1961. }
  1962. if (i == 4)
  1963. DRM_ERROR("FDI train 1 fail!\n");
  1964. /* Train 2 */
  1965. reg = FDI_TX_CTL(pipe);
  1966. temp = I915_READ(reg);
  1967. temp &= ~FDI_LINK_TRAIN_NONE;
  1968. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1969. if (IS_GEN6(dev)) {
  1970. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1971. /* SNB-B */
  1972. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1973. }
  1974. I915_WRITE(reg, temp);
  1975. reg = FDI_RX_CTL(pipe);
  1976. temp = I915_READ(reg);
  1977. if (HAS_PCH_CPT(dev)) {
  1978. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1979. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  1980. } else {
  1981. temp &= ~FDI_LINK_TRAIN_NONE;
  1982. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1983. }
  1984. I915_WRITE(reg, temp);
  1985. POSTING_READ(reg);
  1986. udelay(150);
  1987. for (i = 0; i < 4; i++) {
  1988. reg = FDI_TX_CTL(pipe);
  1989. temp = I915_READ(reg);
  1990. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1991. temp |= snb_b_fdi_train_param[i];
  1992. I915_WRITE(reg, temp);
  1993. POSTING_READ(reg);
  1994. udelay(500);
  1995. for (retry = 0; retry < 5; retry++) {
  1996. reg = FDI_RX_IIR(pipe);
  1997. temp = I915_READ(reg);
  1998. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1999. if (temp & FDI_RX_SYMBOL_LOCK) {
  2000. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2001. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2002. break;
  2003. }
  2004. udelay(50);
  2005. }
  2006. if (retry < 5)
  2007. break;
  2008. }
  2009. if (i == 4)
  2010. DRM_ERROR("FDI train 2 fail!\n");
  2011. DRM_DEBUG_KMS("FDI train done.\n");
  2012. }
  2013. /* Manual link training for Ivy Bridge A0 parts */
  2014. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2015. {
  2016. struct drm_device *dev = crtc->dev;
  2017. struct drm_i915_private *dev_priv = dev->dev_private;
  2018. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2019. int pipe = intel_crtc->pipe;
  2020. u32 reg, temp, i;
  2021. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2022. for train result */
  2023. reg = FDI_RX_IMR(pipe);
  2024. temp = I915_READ(reg);
  2025. temp &= ~FDI_RX_SYMBOL_LOCK;
  2026. temp &= ~FDI_RX_BIT_LOCK;
  2027. I915_WRITE(reg, temp);
  2028. POSTING_READ(reg);
  2029. udelay(150);
  2030. /* enable CPU FDI TX and PCH FDI RX */
  2031. reg = FDI_TX_CTL(pipe);
  2032. temp = I915_READ(reg);
  2033. temp &= ~(7 << 19);
  2034. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2035. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2036. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2037. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2038. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2039. temp |= FDI_COMPOSITE_SYNC;
  2040. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2041. reg = FDI_RX_CTL(pipe);
  2042. temp = I915_READ(reg);
  2043. temp &= ~FDI_LINK_TRAIN_AUTO;
  2044. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2045. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2046. temp |= FDI_COMPOSITE_SYNC;
  2047. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2048. POSTING_READ(reg);
  2049. udelay(150);
  2050. if (HAS_PCH_CPT(dev))
  2051. cpt_phase_pointer_enable(dev, pipe);
  2052. for (i = 0; i < 4; i++) {
  2053. reg = FDI_TX_CTL(pipe);
  2054. temp = I915_READ(reg);
  2055. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2056. temp |= snb_b_fdi_train_param[i];
  2057. I915_WRITE(reg, temp);
  2058. POSTING_READ(reg);
  2059. udelay(500);
  2060. reg = FDI_RX_IIR(pipe);
  2061. temp = I915_READ(reg);
  2062. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2063. if (temp & FDI_RX_BIT_LOCK ||
  2064. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2065. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2066. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2067. break;
  2068. }
  2069. }
  2070. if (i == 4)
  2071. DRM_ERROR("FDI train 1 fail!\n");
  2072. /* Train 2 */
  2073. reg = FDI_TX_CTL(pipe);
  2074. temp = I915_READ(reg);
  2075. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2076. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2077. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2078. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2079. I915_WRITE(reg, temp);
  2080. reg = FDI_RX_CTL(pipe);
  2081. temp = I915_READ(reg);
  2082. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2083. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2084. I915_WRITE(reg, temp);
  2085. POSTING_READ(reg);
  2086. udelay(150);
  2087. for (i = 0; i < 4; i++) {
  2088. reg = FDI_TX_CTL(pipe);
  2089. temp = I915_READ(reg);
  2090. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2091. temp |= snb_b_fdi_train_param[i];
  2092. I915_WRITE(reg, temp);
  2093. POSTING_READ(reg);
  2094. udelay(500);
  2095. reg = FDI_RX_IIR(pipe);
  2096. temp = I915_READ(reg);
  2097. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2098. if (temp & FDI_RX_SYMBOL_LOCK) {
  2099. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2100. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2101. break;
  2102. }
  2103. }
  2104. if (i == 4)
  2105. DRM_ERROR("FDI train 2 fail!\n");
  2106. DRM_DEBUG_KMS("FDI train done.\n");
  2107. }
  2108. static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
  2109. {
  2110. struct drm_device *dev = crtc->dev;
  2111. struct drm_i915_private *dev_priv = dev->dev_private;
  2112. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2113. int pipe = intel_crtc->pipe;
  2114. u32 reg, temp;
  2115. /* Write the TU size bits so error detection works */
  2116. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2117. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2118. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2119. reg = FDI_RX_CTL(pipe);
  2120. temp = I915_READ(reg);
  2121. temp &= ~((0x7 << 19) | (0x7 << 16));
  2122. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2123. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2124. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2125. POSTING_READ(reg);
  2126. udelay(200);
  2127. /* Switch from Rawclk to PCDclk */
  2128. temp = I915_READ(reg);
  2129. I915_WRITE(reg, temp | FDI_PCDCLK);
  2130. POSTING_READ(reg);
  2131. udelay(200);
  2132. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2133. reg = FDI_TX_CTL(pipe);
  2134. temp = I915_READ(reg);
  2135. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2136. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2137. POSTING_READ(reg);
  2138. udelay(100);
  2139. }
  2140. }
  2141. static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
  2142. {
  2143. struct drm_i915_private *dev_priv = dev->dev_private;
  2144. u32 flags = I915_READ(SOUTH_CHICKEN1);
  2145. flags &= ~(FDI_PHASE_SYNC_EN(pipe));
  2146. I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
  2147. flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
  2148. I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
  2149. POSTING_READ(SOUTH_CHICKEN1);
  2150. }
  2151. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2152. {
  2153. struct drm_device *dev = crtc->dev;
  2154. struct drm_i915_private *dev_priv = dev->dev_private;
  2155. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2156. int pipe = intel_crtc->pipe;
  2157. u32 reg, temp;
  2158. /* disable CPU FDI tx and PCH FDI rx */
  2159. reg = FDI_TX_CTL(pipe);
  2160. temp = I915_READ(reg);
  2161. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2162. POSTING_READ(reg);
  2163. reg = FDI_RX_CTL(pipe);
  2164. temp = I915_READ(reg);
  2165. temp &= ~(0x7 << 16);
  2166. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2167. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2168. POSTING_READ(reg);
  2169. udelay(100);
  2170. /* Ironlake workaround, disable clock pointer after downing FDI */
  2171. if (HAS_PCH_IBX(dev)) {
  2172. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2173. I915_WRITE(FDI_RX_CHICKEN(pipe),
  2174. I915_READ(FDI_RX_CHICKEN(pipe) &
  2175. ~FDI_RX_PHASE_SYNC_POINTER_EN));
  2176. } else if (HAS_PCH_CPT(dev)) {
  2177. cpt_phase_pointer_disable(dev, pipe);
  2178. }
  2179. /* still set train pattern 1 */
  2180. reg = FDI_TX_CTL(pipe);
  2181. temp = I915_READ(reg);
  2182. temp &= ~FDI_LINK_TRAIN_NONE;
  2183. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2184. I915_WRITE(reg, temp);
  2185. reg = FDI_RX_CTL(pipe);
  2186. temp = I915_READ(reg);
  2187. if (HAS_PCH_CPT(dev)) {
  2188. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2189. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2190. } else {
  2191. temp &= ~FDI_LINK_TRAIN_NONE;
  2192. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2193. }
  2194. /* BPC in FDI rx is consistent with that in PIPECONF */
  2195. temp &= ~(0x07 << 16);
  2196. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2197. I915_WRITE(reg, temp);
  2198. POSTING_READ(reg);
  2199. udelay(100);
  2200. }
  2201. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2202. {
  2203. struct drm_device *dev = crtc->dev;
  2204. if (crtc->fb == NULL)
  2205. return;
  2206. mutex_lock(&dev->struct_mutex);
  2207. intel_finish_fb(crtc->fb);
  2208. mutex_unlock(&dev->struct_mutex);
  2209. }
  2210. static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
  2211. {
  2212. struct drm_device *dev = crtc->dev;
  2213. struct drm_mode_config *mode_config = &dev->mode_config;
  2214. struct intel_encoder *encoder;
  2215. /*
  2216. * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
  2217. * must be driven by its own crtc; no sharing is possible.
  2218. */
  2219. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  2220. if (encoder->base.crtc != crtc)
  2221. continue;
  2222. switch (encoder->type) {
  2223. case INTEL_OUTPUT_EDP:
  2224. if (!intel_encoder_is_pch_edp(&encoder->base))
  2225. return false;
  2226. continue;
  2227. }
  2228. }
  2229. return true;
  2230. }
  2231. /*
  2232. * Enable PCH resources required for PCH ports:
  2233. * - PCH PLLs
  2234. * - FDI training & RX/TX
  2235. * - update transcoder timings
  2236. * - DP transcoding bits
  2237. * - transcoder
  2238. */
  2239. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2240. {
  2241. struct drm_device *dev = crtc->dev;
  2242. struct drm_i915_private *dev_priv = dev->dev_private;
  2243. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2244. int pipe = intel_crtc->pipe;
  2245. u32 reg, temp;
  2246. /* For PCH output, training FDI link */
  2247. dev_priv->display.fdi_link_train(crtc);
  2248. intel_enable_pch_pll(intel_crtc);
  2249. if (HAS_PCH_CPT(dev)) {
  2250. u32 sel;
  2251. temp = I915_READ(PCH_DPLL_SEL);
  2252. switch (pipe) {
  2253. default:
  2254. case 0:
  2255. temp |= TRANSA_DPLL_ENABLE;
  2256. sel = TRANSA_DPLLB_SEL;
  2257. break;
  2258. case 1:
  2259. temp |= TRANSB_DPLL_ENABLE;
  2260. sel = TRANSB_DPLLB_SEL;
  2261. break;
  2262. case 2:
  2263. temp |= TRANSC_DPLL_ENABLE;
  2264. sel = TRANSC_DPLLB_SEL;
  2265. break;
  2266. }
  2267. if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
  2268. temp |= sel;
  2269. else
  2270. temp &= ~sel;
  2271. I915_WRITE(PCH_DPLL_SEL, temp);
  2272. }
  2273. /* set transcoder timing, panel must allow it */
  2274. assert_panel_unlocked(dev_priv, pipe);
  2275. I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
  2276. I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
  2277. I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
  2278. I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
  2279. I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
  2280. I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
  2281. I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
  2282. intel_fdi_normal_train(crtc);
  2283. /* For PCH DP, enable TRANS_DP_CTL */
  2284. if (HAS_PCH_CPT(dev) &&
  2285. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  2286. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2287. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
  2288. reg = TRANS_DP_CTL(pipe);
  2289. temp = I915_READ(reg);
  2290. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  2291. TRANS_DP_SYNC_MASK |
  2292. TRANS_DP_BPC_MASK);
  2293. temp |= (TRANS_DP_OUTPUT_ENABLE |
  2294. TRANS_DP_ENH_FRAMING);
  2295. temp |= bpc << 9; /* same format but at 11:9 */
  2296. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  2297. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  2298. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  2299. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  2300. switch (intel_trans_dp_port_sel(crtc)) {
  2301. case PCH_DP_B:
  2302. temp |= TRANS_DP_PORT_SEL_B;
  2303. break;
  2304. case PCH_DP_C:
  2305. temp |= TRANS_DP_PORT_SEL_C;
  2306. break;
  2307. case PCH_DP_D:
  2308. temp |= TRANS_DP_PORT_SEL_D;
  2309. break;
  2310. default:
  2311. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  2312. temp |= TRANS_DP_PORT_SEL_B;
  2313. break;
  2314. }
  2315. I915_WRITE(reg, temp);
  2316. }
  2317. intel_enable_transcoder(dev_priv, pipe);
  2318. }
  2319. static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
  2320. {
  2321. struct intel_pch_pll *pll = intel_crtc->pch_pll;
  2322. if (pll == NULL)
  2323. return;
  2324. if (pll->refcount == 0) {
  2325. WARN(1, "bad PCH PLL refcount\n");
  2326. return;
  2327. }
  2328. --pll->refcount;
  2329. intel_crtc->pch_pll = NULL;
  2330. }
  2331. static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
  2332. {
  2333. struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
  2334. struct intel_pch_pll *pll;
  2335. int i;
  2336. pll = intel_crtc->pch_pll;
  2337. if (pll) {
  2338. DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
  2339. intel_crtc->base.base.id, pll->pll_reg);
  2340. goto prepare;
  2341. }
  2342. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  2343. pll = &dev_priv->pch_plls[i];
  2344. /* Only want to check enabled timings first */
  2345. if (pll->refcount == 0)
  2346. continue;
  2347. if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
  2348. fp == I915_READ(pll->fp0_reg)) {
  2349. DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
  2350. intel_crtc->base.base.id,
  2351. pll->pll_reg, pll->refcount, pll->active);
  2352. goto found;
  2353. }
  2354. }
  2355. /* Ok no matching timings, maybe there's a free one? */
  2356. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  2357. pll = &dev_priv->pch_plls[i];
  2358. if (pll->refcount == 0) {
  2359. DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
  2360. intel_crtc->base.base.id, pll->pll_reg);
  2361. goto found;
  2362. }
  2363. }
  2364. return NULL;
  2365. found:
  2366. intel_crtc->pch_pll = pll;
  2367. pll->refcount++;
  2368. DRM_DEBUG_DRIVER("using pll %d for pipe %d\n", i, intel_crtc->pipe);
  2369. prepare: /* separate function? */
  2370. DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
  2371. /* Wait for the clocks to stabilize before rewriting the regs */
  2372. I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
  2373. POSTING_READ(pll->pll_reg);
  2374. udelay(150);
  2375. I915_WRITE(pll->fp0_reg, fp);
  2376. I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
  2377. pll->on = false;
  2378. return pll;
  2379. }
  2380. void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
  2381. {
  2382. struct drm_i915_private *dev_priv = dev->dev_private;
  2383. int dslreg = PIPEDSL(pipe), tc2reg = TRANS_CHICKEN2(pipe);
  2384. u32 temp;
  2385. temp = I915_READ(dslreg);
  2386. udelay(500);
  2387. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  2388. /* Without this, mode sets may fail silently on FDI */
  2389. I915_WRITE(tc2reg, TRANS_AUTOTRAIN_GEN_STALL_DIS);
  2390. udelay(250);
  2391. I915_WRITE(tc2reg, 0);
  2392. if (wait_for(I915_READ(dslreg) != temp, 5))
  2393. DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
  2394. }
  2395. }
  2396. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  2397. {
  2398. struct drm_device *dev = crtc->dev;
  2399. struct drm_i915_private *dev_priv = dev->dev_private;
  2400. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2401. int pipe = intel_crtc->pipe;
  2402. int plane = intel_crtc->plane;
  2403. u32 temp;
  2404. bool is_pch_port;
  2405. if (intel_crtc->active)
  2406. return;
  2407. intel_crtc->active = true;
  2408. intel_update_watermarks(dev);
  2409. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  2410. temp = I915_READ(PCH_LVDS);
  2411. if ((temp & LVDS_PORT_EN) == 0)
  2412. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  2413. }
  2414. is_pch_port = intel_crtc_driving_pch(crtc);
  2415. if (is_pch_port)
  2416. ironlake_fdi_pll_enable(crtc);
  2417. else
  2418. ironlake_fdi_disable(crtc);
  2419. /* Enable panel fitting for LVDS */
  2420. if (dev_priv->pch_pf_size &&
  2421. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
  2422. /* Force use of hard-coded filter coefficients
  2423. * as some pre-programmed values are broken,
  2424. * e.g. x201.
  2425. */
  2426. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  2427. I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
  2428. I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
  2429. }
  2430. /*
  2431. * On ILK+ LUT must be loaded before the pipe is running but with
  2432. * clocks enabled
  2433. */
  2434. intel_crtc_load_lut(crtc);
  2435. intel_enable_pipe(dev_priv, pipe, is_pch_port);
  2436. intel_enable_plane(dev_priv, plane, pipe);
  2437. if (is_pch_port)
  2438. ironlake_pch_enable(crtc);
  2439. mutex_lock(&dev->struct_mutex);
  2440. intel_update_fbc(dev);
  2441. mutex_unlock(&dev->struct_mutex);
  2442. intel_crtc_update_cursor(crtc, true);
  2443. }
  2444. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  2445. {
  2446. struct drm_device *dev = crtc->dev;
  2447. struct drm_i915_private *dev_priv = dev->dev_private;
  2448. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2449. int pipe = intel_crtc->pipe;
  2450. int plane = intel_crtc->plane;
  2451. u32 reg, temp;
  2452. if (!intel_crtc->active)
  2453. return;
  2454. intel_crtc_wait_for_pending_flips(crtc);
  2455. drm_vblank_off(dev, pipe);
  2456. intel_crtc_update_cursor(crtc, false);
  2457. intel_disable_plane(dev_priv, plane, pipe);
  2458. if (dev_priv->cfb_plane == plane)
  2459. intel_disable_fbc(dev);
  2460. intel_disable_pipe(dev_priv, pipe);
  2461. /* Disable PF */
  2462. I915_WRITE(PF_CTL(pipe), 0);
  2463. I915_WRITE(PF_WIN_SZ(pipe), 0);
  2464. ironlake_fdi_disable(crtc);
  2465. /* This is a horrible layering violation; we should be doing this in
  2466. * the connector/encoder ->prepare instead, but we don't always have
  2467. * enough information there about the config to know whether it will
  2468. * actually be necessary or just cause undesired flicker.
  2469. */
  2470. intel_disable_pch_ports(dev_priv, pipe);
  2471. intel_disable_transcoder(dev_priv, pipe);
  2472. if (HAS_PCH_CPT(dev)) {
  2473. /* disable TRANS_DP_CTL */
  2474. reg = TRANS_DP_CTL(pipe);
  2475. temp = I915_READ(reg);
  2476. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  2477. temp |= TRANS_DP_PORT_SEL_NONE;
  2478. I915_WRITE(reg, temp);
  2479. /* disable DPLL_SEL */
  2480. temp = I915_READ(PCH_DPLL_SEL);
  2481. switch (pipe) {
  2482. case 0:
  2483. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  2484. break;
  2485. case 1:
  2486. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2487. break;
  2488. case 2:
  2489. /* C shares PLL A or B */
  2490. temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
  2491. break;
  2492. default:
  2493. BUG(); /* wtf */
  2494. }
  2495. I915_WRITE(PCH_DPLL_SEL, temp);
  2496. }
  2497. /* disable PCH DPLL */
  2498. intel_disable_pch_pll(intel_crtc);
  2499. /* Switch from PCDclk to Rawclk */
  2500. reg = FDI_RX_CTL(pipe);
  2501. temp = I915_READ(reg);
  2502. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2503. /* Disable CPU FDI TX PLL */
  2504. reg = FDI_TX_CTL(pipe);
  2505. temp = I915_READ(reg);
  2506. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2507. POSTING_READ(reg);
  2508. udelay(100);
  2509. reg = FDI_RX_CTL(pipe);
  2510. temp = I915_READ(reg);
  2511. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2512. /* Wait for the clocks to turn off. */
  2513. POSTING_READ(reg);
  2514. udelay(100);
  2515. intel_crtc->active = false;
  2516. intel_update_watermarks(dev);
  2517. mutex_lock(&dev->struct_mutex);
  2518. intel_update_fbc(dev);
  2519. mutex_unlock(&dev->struct_mutex);
  2520. }
  2521. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  2522. {
  2523. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2524. int pipe = intel_crtc->pipe;
  2525. int plane = intel_crtc->plane;
  2526. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2527. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2528. */
  2529. switch (mode) {
  2530. case DRM_MODE_DPMS_ON:
  2531. case DRM_MODE_DPMS_STANDBY:
  2532. case DRM_MODE_DPMS_SUSPEND:
  2533. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  2534. ironlake_crtc_enable(crtc);
  2535. break;
  2536. case DRM_MODE_DPMS_OFF:
  2537. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  2538. ironlake_crtc_disable(crtc);
  2539. break;
  2540. }
  2541. }
  2542. static void ironlake_crtc_off(struct drm_crtc *crtc)
  2543. {
  2544. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2545. intel_put_pch_pll(intel_crtc);
  2546. }
  2547. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2548. {
  2549. if (!enable && intel_crtc->overlay) {
  2550. struct drm_device *dev = intel_crtc->base.dev;
  2551. struct drm_i915_private *dev_priv = dev->dev_private;
  2552. mutex_lock(&dev->struct_mutex);
  2553. dev_priv->mm.interruptible = false;
  2554. (void) intel_overlay_switch_off(intel_crtc->overlay);
  2555. dev_priv->mm.interruptible = true;
  2556. mutex_unlock(&dev->struct_mutex);
  2557. }
  2558. /* Let userspace switch the overlay on again. In most cases userspace
  2559. * has to recompute where to put it anyway.
  2560. */
  2561. }
  2562. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  2563. {
  2564. struct drm_device *dev = crtc->dev;
  2565. struct drm_i915_private *dev_priv = dev->dev_private;
  2566. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2567. int pipe = intel_crtc->pipe;
  2568. int plane = intel_crtc->plane;
  2569. if (intel_crtc->active)
  2570. return;
  2571. intel_crtc->active = true;
  2572. intel_update_watermarks(dev);
  2573. intel_enable_pll(dev_priv, pipe);
  2574. intel_enable_pipe(dev_priv, pipe, false);
  2575. intel_enable_plane(dev_priv, plane, pipe);
  2576. intel_crtc_load_lut(crtc);
  2577. intel_update_fbc(dev);
  2578. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2579. intel_crtc_dpms_overlay(intel_crtc, true);
  2580. intel_crtc_update_cursor(crtc, true);
  2581. }
  2582. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  2583. {
  2584. struct drm_device *dev = crtc->dev;
  2585. struct drm_i915_private *dev_priv = dev->dev_private;
  2586. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2587. int pipe = intel_crtc->pipe;
  2588. int plane = intel_crtc->plane;
  2589. if (!intel_crtc->active)
  2590. return;
  2591. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2592. intel_crtc_wait_for_pending_flips(crtc);
  2593. drm_vblank_off(dev, pipe);
  2594. intel_crtc_dpms_overlay(intel_crtc, false);
  2595. intel_crtc_update_cursor(crtc, false);
  2596. if (dev_priv->cfb_plane == plane)
  2597. intel_disable_fbc(dev);
  2598. intel_disable_plane(dev_priv, plane, pipe);
  2599. intel_disable_pipe(dev_priv, pipe);
  2600. intel_disable_pll(dev_priv, pipe);
  2601. intel_crtc->active = false;
  2602. intel_update_fbc(dev);
  2603. intel_update_watermarks(dev);
  2604. }
  2605. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2606. {
  2607. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2608. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2609. */
  2610. switch (mode) {
  2611. case DRM_MODE_DPMS_ON:
  2612. case DRM_MODE_DPMS_STANDBY:
  2613. case DRM_MODE_DPMS_SUSPEND:
  2614. i9xx_crtc_enable(crtc);
  2615. break;
  2616. case DRM_MODE_DPMS_OFF:
  2617. i9xx_crtc_disable(crtc);
  2618. break;
  2619. }
  2620. }
  2621. static void i9xx_crtc_off(struct drm_crtc *crtc)
  2622. {
  2623. }
  2624. /**
  2625. * Sets the power management mode of the pipe and plane.
  2626. */
  2627. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2628. {
  2629. struct drm_device *dev = crtc->dev;
  2630. struct drm_i915_private *dev_priv = dev->dev_private;
  2631. struct drm_i915_master_private *master_priv;
  2632. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2633. int pipe = intel_crtc->pipe;
  2634. bool enabled;
  2635. if (intel_crtc->dpms_mode == mode)
  2636. return;
  2637. intel_crtc->dpms_mode = mode;
  2638. dev_priv->display.dpms(crtc, mode);
  2639. if (!dev->primary->master)
  2640. return;
  2641. master_priv = dev->primary->master->driver_priv;
  2642. if (!master_priv->sarea_priv)
  2643. return;
  2644. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2645. switch (pipe) {
  2646. case 0:
  2647. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2648. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2649. break;
  2650. case 1:
  2651. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2652. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2653. break;
  2654. default:
  2655. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  2656. break;
  2657. }
  2658. }
  2659. static void intel_crtc_disable(struct drm_crtc *crtc)
  2660. {
  2661. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2662. struct drm_device *dev = crtc->dev;
  2663. struct drm_i915_private *dev_priv = dev->dev_private;
  2664. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  2665. dev_priv->display.off(crtc);
  2666. assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
  2667. assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
  2668. if (crtc->fb) {
  2669. mutex_lock(&dev->struct_mutex);
  2670. intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
  2671. mutex_unlock(&dev->struct_mutex);
  2672. }
  2673. }
  2674. /* Prepare for a mode set.
  2675. *
  2676. * Note we could be a lot smarter here. We need to figure out which outputs
  2677. * will be enabled, which disabled (in short, how the config will changes)
  2678. * and perform the minimum necessary steps to accomplish that, e.g. updating
  2679. * watermarks, FBC configuration, making sure PLLs are programmed correctly,
  2680. * panel fitting is in the proper state, etc.
  2681. */
  2682. static void i9xx_crtc_prepare(struct drm_crtc *crtc)
  2683. {
  2684. i9xx_crtc_disable(crtc);
  2685. }
  2686. static void i9xx_crtc_commit(struct drm_crtc *crtc)
  2687. {
  2688. i9xx_crtc_enable(crtc);
  2689. }
  2690. static void ironlake_crtc_prepare(struct drm_crtc *crtc)
  2691. {
  2692. ironlake_crtc_disable(crtc);
  2693. }
  2694. static void ironlake_crtc_commit(struct drm_crtc *crtc)
  2695. {
  2696. ironlake_crtc_enable(crtc);
  2697. }
  2698. void intel_encoder_prepare(struct drm_encoder *encoder)
  2699. {
  2700. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2701. /* lvds has its own version of prepare see intel_lvds_prepare */
  2702. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2703. }
  2704. void intel_encoder_commit(struct drm_encoder *encoder)
  2705. {
  2706. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2707. struct drm_device *dev = encoder->dev;
  2708. struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  2709. /* lvds has its own version of commit see intel_lvds_commit */
  2710. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2711. if (HAS_PCH_CPT(dev))
  2712. intel_cpt_verify_modeset(dev, intel_crtc->pipe);
  2713. }
  2714. void intel_encoder_destroy(struct drm_encoder *encoder)
  2715. {
  2716. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2717. drm_encoder_cleanup(encoder);
  2718. kfree(intel_encoder);
  2719. }
  2720. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2721. struct drm_display_mode *mode,
  2722. struct drm_display_mode *adjusted_mode)
  2723. {
  2724. struct drm_device *dev = crtc->dev;
  2725. if (HAS_PCH_SPLIT(dev)) {
  2726. /* FDI link clock is fixed at 2.7G */
  2727. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  2728. return false;
  2729. }
  2730. /* All interlaced capable intel hw wants timings in frames. Note though
  2731. * that intel_lvds_mode_fixup does some funny tricks with the crtc
  2732. * timings, so we need to be careful not to clobber these.*/
  2733. if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
  2734. drm_mode_set_crtcinfo(adjusted_mode, 0);
  2735. return true;
  2736. }
  2737. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  2738. {
  2739. return 400000; /* FIXME */
  2740. }
  2741. static int i945_get_display_clock_speed(struct drm_device *dev)
  2742. {
  2743. return 400000;
  2744. }
  2745. static int i915_get_display_clock_speed(struct drm_device *dev)
  2746. {
  2747. return 333000;
  2748. }
  2749. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2750. {
  2751. return 200000;
  2752. }
  2753. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2754. {
  2755. u16 gcfgc = 0;
  2756. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2757. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2758. return 133000;
  2759. else {
  2760. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2761. case GC_DISPLAY_CLOCK_333_MHZ:
  2762. return 333000;
  2763. default:
  2764. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2765. return 190000;
  2766. }
  2767. }
  2768. }
  2769. static int i865_get_display_clock_speed(struct drm_device *dev)
  2770. {
  2771. return 266000;
  2772. }
  2773. static int i855_get_display_clock_speed(struct drm_device *dev)
  2774. {
  2775. u16 hpllcc = 0;
  2776. /* Assume that the hardware is in the high speed state. This
  2777. * should be the default.
  2778. */
  2779. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2780. case GC_CLOCK_133_200:
  2781. case GC_CLOCK_100_200:
  2782. return 200000;
  2783. case GC_CLOCK_166_250:
  2784. return 250000;
  2785. case GC_CLOCK_100_133:
  2786. return 133000;
  2787. }
  2788. /* Shouldn't happen */
  2789. return 0;
  2790. }
  2791. static int i830_get_display_clock_speed(struct drm_device *dev)
  2792. {
  2793. return 133000;
  2794. }
  2795. struct fdi_m_n {
  2796. u32 tu;
  2797. u32 gmch_m;
  2798. u32 gmch_n;
  2799. u32 link_m;
  2800. u32 link_n;
  2801. };
  2802. static void
  2803. fdi_reduce_ratio(u32 *num, u32 *den)
  2804. {
  2805. while (*num > 0xffffff || *den > 0xffffff) {
  2806. *num >>= 1;
  2807. *den >>= 1;
  2808. }
  2809. }
  2810. static void
  2811. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  2812. int link_clock, struct fdi_m_n *m_n)
  2813. {
  2814. m_n->tu = 64; /* default size */
  2815. /* BUG_ON(pixel_clock > INT_MAX / 36); */
  2816. m_n->gmch_m = bits_per_pixel * pixel_clock;
  2817. m_n->gmch_n = link_clock * nlanes * 8;
  2818. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  2819. m_n->link_m = pixel_clock;
  2820. m_n->link_n = link_clock;
  2821. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  2822. }
  2823. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  2824. {
  2825. if (i915_panel_use_ssc >= 0)
  2826. return i915_panel_use_ssc != 0;
  2827. return dev_priv->lvds_use_ssc
  2828. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  2829. }
  2830. /**
  2831. * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
  2832. * @crtc: CRTC structure
  2833. * @mode: requested mode
  2834. *
  2835. * A pipe may be connected to one or more outputs. Based on the depth of the
  2836. * attached framebuffer, choose a good color depth to use on the pipe.
  2837. *
  2838. * If possible, match the pipe depth to the fb depth. In some cases, this
  2839. * isn't ideal, because the connected output supports a lesser or restricted
  2840. * set of depths. Resolve that here:
  2841. * LVDS typically supports only 6bpc, so clamp down in that case
  2842. * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
  2843. * Displays may support a restricted set as well, check EDID and clamp as
  2844. * appropriate.
  2845. * DP may want to dither down to 6bpc to fit larger modes
  2846. *
  2847. * RETURNS:
  2848. * Dithering requirement (i.e. false if display bpc and pipe bpc match,
  2849. * true if they don't match).
  2850. */
  2851. static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
  2852. unsigned int *pipe_bpp,
  2853. struct drm_display_mode *mode)
  2854. {
  2855. struct drm_device *dev = crtc->dev;
  2856. struct drm_i915_private *dev_priv = dev->dev_private;
  2857. struct drm_encoder *encoder;
  2858. struct drm_connector *connector;
  2859. unsigned int display_bpc = UINT_MAX, bpc;
  2860. /* Walk the encoders & connectors on this crtc, get min bpc */
  2861. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  2862. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2863. if (encoder->crtc != crtc)
  2864. continue;
  2865. if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
  2866. unsigned int lvds_bpc;
  2867. if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
  2868. LVDS_A3_POWER_UP)
  2869. lvds_bpc = 8;
  2870. else
  2871. lvds_bpc = 6;
  2872. if (lvds_bpc < display_bpc) {
  2873. DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
  2874. display_bpc = lvds_bpc;
  2875. }
  2876. continue;
  2877. }
  2878. if (intel_encoder->type == INTEL_OUTPUT_EDP) {
  2879. /* Use VBT settings if we have an eDP panel */
  2880. unsigned int edp_bpc = dev_priv->edp.bpp / 3;
  2881. if (edp_bpc < display_bpc) {
  2882. DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
  2883. display_bpc = edp_bpc;
  2884. }
  2885. continue;
  2886. }
  2887. /* Not one of the known troublemakers, check the EDID */
  2888. list_for_each_entry(connector, &dev->mode_config.connector_list,
  2889. head) {
  2890. if (connector->encoder != encoder)
  2891. continue;
  2892. /* Don't use an invalid EDID bpc value */
  2893. if (connector->display_info.bpc &&
  2894. connector->display_info.bpc < display_bpc) {
  2895. DRM_DEBUG_KMS("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
  2896. display_bpc = connector->display_info.bpc;
  2897. }
  2898. }
  2899. /*
  2900. * HDMI is either 12 or 8, so if the display lets 10bpc sneak
  2901. * through, clamp it down. (Note: >12bpc will be caught below.)
  2902. */
  2903. if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
  2904. if (display_bpc > 8 && display_bpc < 12) {
  2905. DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
  2906. display_bpc = 12;
  2907. } else {
  2908. DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
  2909. display_bpc = 8;
  2910. }
  2911. }
  2912. }
  2913. if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
  2914. DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
  2915. display_bpc = 6;
  2916. }
  2917. /*
  2918. * We could just drive the pipe at the highest bpc all the time and
  2919. * enable dithering as needed, but that costs bandwidth. So choose
  2920. * the minimum value that expresses the full color range of the fb but
  2921. * also stays within the max display bpc discovered above.
  2922. */
  2923. switch (crtc->fb->depth) {
  2924. case 8:
  2925. bpc = 8; /* since we go through a colormap */
  2926. break;
  2927. case 15:
  2928. case 16:
  2929. bpc = 6; /* min is 18bpp */
  2930. break;
  2931. case 24:
  2932. bpc = 8;
  2933. break;
  2934. case 30:
  2935. bpc = 10;
  2936. break;
  2937. case 48:
  2938. bpc = 12;
  2939. break;
  2940. default:
  2941. DRM_DEBUG("unsupported depth, assuming 24 bits\n");
  2942. bpc = min((unsigned int)8, display_bpc);
  2943. break;
  2944. }
  2945. display_bpc = min(display_bpc, bpc);
  2946. DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
  2947. bpc, display_bpc);
  2948. *pipe_bpp = display_bpc * 3;
  2949. return display_bpc != bpc;
  2950. }
  2951. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  2952. {
  2953. struct drm_device *dev = crtc->dev;
  2954. struct drm_i915_private *dev_priv = dev->dev_private;
  2955. int refclk;
  2956. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  2957. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  2958. refclk = dev_priv->lvds_ssc_freq * 1000;
  2959. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  2960. refclk / 1000);
  2961. } else if (!IS_GEN2(dev)) {
  2962. refclk = 96000;
  2963. } else {
  2964. refclk = 48000;
  2965. }
  2966. return refclk;
  2967. }
  2968. static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
  2969. intel_clock_t *clock)
  2970. {
  2971. /* SDVO TV has fixed PLL values depend on its clock range,
  2972. this mirrors vbios setting. */
  2973. if (adjusted_mode->clock >= 100000
  2974. && adjusted_mode->clock < 140500) {
  2975. clock->p1 = 2;
  2976. clock->p2 = 10;
  2977. clock->n = 3;
  2978. clock->m1 = 16;
  2979. clock->m2 = 8;
  2980. } else if (adjusted_mode->clock >= 140500
  2981. && adjusted_mode->clock <= 200000) {
  2982. clock->p1 = 1;
  2983. clock->p2 = 10;
  2984. clock->n = 6;
  2985. clock->m1 = 12;
  2986. clock->m2 = 8;
  2987. }
  2988. }
  2989. static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
  2990. intel_clock_t *clock,
  2991. intel_clock_t *reduced_clock)
  2992. {
  2993. struct drm_device *dev = crtc->dev;
  2994. struct drm_i915_private *dev_priv = dev->dev_private;
  2995. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2996. int pipe = intel_crtc->pipe;
  2997. u32 fp, fp2 = 0;
  2998. if (IS_PINEVIEW(dev)) {
  2999. fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
  3000. if (reduced_clock)
  3001. fp2 = (1 << reduced_clock->n) << 16 |
  3002. reduced_clock->m1 << 8 | reduced_clock->m2;
  3003. } else {
  3004. fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
  3005. if (reduced_clock)
  3006. fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
  3007. reduced_clock->m2;
  3008. }
  3009. I915_WRITE(FP0(pipe), fp);
  3010. intel_crtc->lowfreq_avail = false;
  3011. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3012. reduced_clock && i915_powersave) {
  3013. I915_WRITE(FP1(pipe), fp2);
  3014. intel_crtc->lowfreq_avail = true;
  3015. } else {
  3016. I915_WRITE(FP1(pipe), fp);
  3017. }
  3018. }
  3019. static void intel_update_lvds(struct drm_crtc *crtc, intel_clock_t *clock,
  3020. struct drm_display_mode *adjusted_mode)
  3021. {
  3022. struct drm_device *dev = crtc->dev;
  3023. struct drm_i915_private *dev_priv = dev->dev_private;
  3024. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3025. int pipe = intel_crtc->pipe;
  3026. u32 temp;
  3027. temp = I915_READ(LVDS);
  3028. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  3029. if (pipe == 1) {
  3030. temp |= LVDS_PIPEB_SELECT;
  3031. } else {
  3032. temp &= ~LVDS_PIPEB_SELECT;
  3033. }
  3034. /* set the corresponsding LVDS_BORDER bit */
  3035. temp |= dev_priv->lvds_border_bits;
  3036. /* Set the B0-B3 data pairs corresponding to whether we're going to
  3037. * set the DPLLs for dual-channel mode or not.
  3038. */
  3039. if (clock->p2 == 7)
  3040. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  3041. else
  3042. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  3043. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  3044. * appropriately here, but we need to look more thoroughly into how
  3045. * panels behave in the two modes.
  3046. */
  3047. /* set the dithering flag on LVDS as needed */
  3048. if (INTEL_INFO(dev)->gen >= 4) {
  3049. if (dev_priv->lvds_dither)
  3050. temp |= LVDS_ENABLE_DITHER;
  3051. else
  3052. temp &= ~LVDS_ENABLE_DITHER;
  3053. }
  3054. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  3055. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  3056. temp |= LVDS_HSYNC_POLARITY;
  3057. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  3058. temp |= LVDS_VSYNC_POLARITY;
  3059. I915_WRITE(LVDS, temp);
  3060. }
  3061. static void i9xx_update_pll(struct drm_crtc *crtc,
  3062. struct drm_display_mode *mode,
  3063. struct drm_display_mode *adjusted_mode,
  3064. intel_clock_t *clock, intel_clock_t *reduced_clock,
  3065. int num_connectors)
  3066. {
  3067. struct drm_device *dev = crtc->dev;
  3068. struct drm_i915_private *dev_priv = dev->dev_private;
  3069. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3070. int pipe = intel_crtc->pipe;
  3071. u32 dpll;
  3072. bool is_sdvo;
  3073. is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
  3074. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
  3075. dpll = DPLL_VGA_MODE_DIS;
  3076. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  3077. dpll |= DPLLB_MODE_LVDS;
  3078. else
  3079. dpll |= DPLLB_MODE_DAC_SERIAL;
  3080. if (is_sdvo) {
  3081. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3082. if (pixel_multiplier > 1) {
  3083. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3084. dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  3085. }
  3086. dpll |= DPLL_DVO_HIGH_SPEED;
  3087. }
  3088. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  3089. dpll |= DPLL_DVO_HIGH_SPEED;
  3090. /* compute bitmask from p1 value */
  3091. if (IS_PINEVIEW(dev))
  3092. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  3093. else {
  3094. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3095. if (IS_G4X(dev) && reduced_clock)
  3096. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3097. }
  3098. switch (clock->p2) {
  3099. case 5:
  3100. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3101. break;
  3102. case 7:
  3103. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3104. break;
  3105. case 10:
  3106. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3107. break;
  3108. case 14:
  3109. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3110. break;
  3111. }
  3112. if (INTEL_INFO(dev)->gen >= 4)
  3113. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  3114. if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  3115. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3116. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  3117. /* XXX: just matching BIOS for now */
  3118. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3119. dpll |= 3;
  3120. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3121. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3122. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3123. else
  3124. dpll |= PLL_REF_INPUT_DREFCLK;
  3125. dpll |= DPLL_VCO_ENABLE;
  3126. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  3127. POSTING_READ(DPLL(pipe));
  3128. udelay(150);
  3129. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3130. * This is an exception to the general rule that mode_set doesn't turn
  3131. * things on.
  3132. */
  3133. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  3134. intel_update_lvds(crtc, clock, adjusted_mode);
  3135. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
  3136. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3137. I915_WRITE(DPLL(pipe), dpll);
  3138. /* Wait for the clocks to stabilize. */
  3139. POSTING_READ(DPLL(pipe));
  3140. udelay(150);
  3141. if (INTEL_INFO(dev)->gen >= 4) {
  3142. u32 temp = 0;
  3143. if (is_sdvo) {
  3144. temp = intel_mode_get_pixel_multiplier(adjusted_mode);
  3145. if (temp > 1)
  3146. temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3147. else
  3148. temp = 0;
  3149. }
  3150. I915_WRITE(DPLL_MD(pipe), temp);
  3151. } else {
  3152. /* The pixel multiplier can only be updated once the
  3153. * DPLL is enabled and the clocks are stable.
  3154. *
  3155. * So write it again.
  3156. */
  3157. I915_WRITE(DPLL(pipe), dpll);
  3158. }
  3159. }
  3160. static void i8xx_update_pll(struct drm_crtc *crtc,
  3161. struct drm_display_mode *adjusted_mode,
  3162. intel_clock_t *clock,
  3163. int num_connectors)
  3164. {
  3165. struct drm_device *dev = crtc->dev;
  3166. struct drm_i915_private *dev_priv = dev->dev_private;
  3167. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3168. int pipe = intel_crtc->pipe;
  3169. u32 dpll;
  3170. dpll = DPLL_VGA_MODE_DIS;
  3171. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  3172. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3173. } else {
  3174. if (clock->p1 == 2)
  3175. dpll |= PLL_P1_DIVIDE_BY_TWO;
  3176. else
  3177. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3178. if (clock->p2 == 4)
  3179. dpll |= PLL_P2_DIVIDE_BY_4;
  3180. }
  3181. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
  3182. /* XXX: just matching BIOS for now */
  3183. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3184. dpll |= 3;
  3185. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3186. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3187. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3188. else
  3189. dpll |= PLL_REF_INPUT_DREFCLK;
  3190. dpll |= DPLL_VCO_ENABLE;
  3191. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  3192. POSTING_READ(DPLL(pipe));
  3193. udelay(150);
  3194. I915_WRITE(DPLL(pipe), dpll);
  3195. /* Wait for the clocks to stabilize. */
  3196. POSTING_READ(DPLL(pipe));
  3197. udelay(150);
  3198. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3199. * This is an exception to the general rule that mode_set doesn't turn
  3200. * things on.
  3201. */
  3202. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  3203. intel_update_lvds(crtc, clock, adjusted_mode);
  3204. /* The pixel multiplier can only be updated once the
  3205. * DPLL is enabled and the clocks are stable.
  3206. *
  3207. * So write it again.
  3208. */
  3209. I915_WRITE(DPLL(pipe), dpll);
  3210. }
  3211. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  3212. struct drm_display_mode *mode,
  3213. struct drm_display_mode *adjusted_mode,
  3214. int x, int y,
  3215. struct drm_framebuffer *old_fb)
  3216. {
  3217. struct drm_device *dev = crtc->dev;
  3218. struct drm_i915_private *dev_priv = dev->dev_private;
  3219. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3220. int pipe = intel_crtc->pipe;
  3221. int plane = intel_crtc->plane;
  3222. int refclk, num_connectors = 0;
  3223. intel_clock_t clock, reduced_clock;
  3224. u32 dspcntr, pipeconf, vsyncshift;
  3225. bool ok, has_reduced_clock = false, is_sdvo = false;
  3226. bool is_lvds = false, is_tv = false, is_dp = false;
  3227. struct drm_mode_config *mode_config = &dev->mode_config;
  3228. struct intel_encoder *encoder;
  3229. const intel_limit_t *limit;
  3230. int ret;
  3231. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  3232. if (encoder->base.crtc != crtc)
  3233. continue;
  3234. switch (encoder->type) {
  3235. case INTEL_OUTPUT_LVDS:
  3236. is_lvds = true;
  3237. break;
  3238. case INTEL_OUTPUT_SDVO:
  3239. case INTEL_OUTPUT_HDMI:
  3240. is_sdvo = true;
  3241. if (encoder->needs_tv_clock)
  3242. is_tv = true;
  3243. break;
  3244. case INTEL_OUTPUT_TVOUT:
  3245. is_tv = true;
  3246. break;
  3247. case INTEL_OUTPUT_DISPLAYPORT:
  3248. is_dp = true;
  3249. break;
  3250. }
  3251. num_connectors++;
  3252. }
  3253. refclk = i9xx_get_refclk(crtc, num_connectors);
  3254. /*
  3255. * Returns a set of divisors for the desired target clock with the given
  3256. * refclk, or FALSE. The returned values represent the clock equation:
  3257. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3258. */
  3259. limit = intel_limit(crtc, refclk);
  3260. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
  3261. &clock);
  3262. if (!ok) {
  3263. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3264. return -EINVAL;
  3265. }
  3266. /* Ensure that the cursor is valid for the new mode before changing... */
  3267. intel_crtc_update_cursor(crtc, true);
  3268. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3269. /*
  3270. * Ensure we match the reduced clock's P to the target clock.
  3271. * If the clocks don't match, we can't switch the display clock
  3272. * by using the FP0/FP1. In such case we will disable the LVDS
  3273. * downclock feature.
  3274. */
  3275. has_reduced_clock = limit->find_pll(limit, crtc,
  3276. dev_priv->lvds_downclock,
  3277. refclk,
  3278. &clock,
  3279. &reduced_clock);
  3280. }
  3281. if (is_sdvo && is_tv)
  3282. i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
  3283. i9xx_update_pll_dividers(crtc, &clock, has_reduced_clock ?
  3284. &reduced_clock : NULL);
  3285. if (IS_GEN2(dev))
  3286. i8xx_update_pll(crtc, adjusted_mode, &clock, num_connectors);
  3287. else
  3288. i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
  3289. has_reduced_clock ? &reduced_clock : NULL,
  3290. num_connectors);
  3291. /* setup pipeconf */
  3292. pipeconf = I915_READ(PIPECONF(pipe));
  3293. /* Set up the display plane register */
  3294. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3295. if (pipe == 0)
  3296. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3297. else
  3298. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3299. if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  3300. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  3301. * core speed.
  3302. *
  3303. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  3304. * pipe == 0 check?
  3305. */
  3306. if (mode->clock >
  3307. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  3308. pipeconf |= PIPECONF_DOUBLE_WIDE;
  3309. else
  3310. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  3311. }
  3312. /* default to 8bpc */
  3313. pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
  3314. if (is_dp) {
  3315. if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
  3316. pipeconf |= PIPECONF_BPP_6 |
  3317. PIPECONF_DITHER_EN |
  3318. PIPECONF_DITHER_TYPE_SP;
  3319. }
  3320. }
  3321. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  3322. drm_mode_debug_printmodeline(mode);
  3323. if (HAS_PIPE_CXSR(dev)) {
  3324. if (intel_crtc->lowfreq_avail) {
  3325. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  3326. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  3327. } else {
  3328. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  3329. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  3330. }
  3331. }
  3332. pipeconf &= ~PIPECONF_INTERLACE_MASK;
  3333. if (!IS_GEN2(dev) &&
  3334. adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3335. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  3336. /* the chip adds 2 halflines automatically */
  3337. adjusted_mode->crtc_vtotal -= 1;
  3338. adjusted_mode->crtc_vblank_end -= 1;
  3339. vsyncshift = adjusted_mode->crtc_hsync_start
  3340. - adjusted_mode->crtc_htotal/2;
  3341. } else {
  3342. pipeconf |= PIPECONF_PROGRESSIVE;
  3343. vsyncshift = 0;
  3344. }
  3345. if (!IS_GEN3(dev))
  3346. I915_WRITE(VSYNCSHIFT(pipe), vsyncshift);
  3347. I915_WRITE(HTOTAL(pipe),
  3348. (adjusted_mode->crtc_hdisplay - 1) |
  3349. ((adjusted_mode->crtc_htotal - 1) << 16));
  3350. I915_WRITE(HBLANK(pipe),
  3351. (adjusted_mode->crtc_hblank_start - 1) |
  3352. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3353. I915_WRITE(HSYNC(pipe),
  3354. (adjusted_mode->crtc_hsync_start - 1) |
  3355. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3356. I915_WRITE(VTOTAL(pipe),
  3357. (adjusted_mode->crtc_vdisplay - 1) |
  3358. ((adjusted_mode->crtc_vtotal - 1) << 16));
  3359. I915_WRITE(VBLANK(pipe),
  3360. (adjusted_mode->crtc_vblank_start - 1) |
  3361. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  3362. I915_WRITE(VSYNC(pipe),
  3363. (adjusted_mode->crtc_vsync_start - 1) |
  3364. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3365. /* pipesrc and dspsize control the size that is scaled from,
  3366. * which should always be the user's requested size.
  3367. */
  3368. I915_WRITE(DSPSIZE(plane),
  3369. ((mode->vdisplay - 1) << 16) |
  3370. (mode->hdisplay - 1));
  3371. I915_WRITE(DSPPOS(plane), 0);
  3372. I915_WRITE(PIPESRC(pipe),
  3373. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3374. I915_WRITE(PIPECONF(pipe), pipeconf);
  3375. POSTING_READ(PIPECONF(pipe));
  3376. intel_enable_pipe(dev_priv, pipe, false);
  3377. intel_wait_for_vblank(dev, pipe);
  3378. I915_WRITE(DSPCNTR(plane), dspcntr);
  3379. POSTING_READ(DSPCNTR(plane));
  3380. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  3381. intel_update_watermarks(dev);
  3382. return ret;
  3383. }
  3384. /*
  3385. * Initialize reference clocks when the driver loads
  3386. */
  3387. void ironlake_init_pch_refclk(struct drm_device *dev)
  3388. {
  3389. struct drm_i915_private *dev_priv = dev->dev_private;
  3390. struct drm_mode_config *mode_config = &dev->mode_config;
  3391. struct intel_encoder *encoder;
  3392. u32 temp;
  3393. bool has_lvds = false;
  3394. bool has_cpu_edp = false;
  3395. bool has_pch_edp = false;
  3396. bool has_panel = false;
  3397. bool has_ck505 = false;
  3398. bool can_ssc = false;
  3399. /* We need to take the global config into account */
  3400. list_for_each_entry(encoder, &mode_config->encoder_list,
  3401. base.head) {
  3402. switch (encoder->type) {
  3403. case INTEL_OUTPUT_LVDS:
  3404. has_panel = true;
  3405. has_lvds = true;
  3406. break;
  3407. case INTEL_OUTPUT_EDP:
  3408. has_panel = true;
  3409. if (intel_encoder_is_pch_edp(&encoder->base))
  3410. has_pch_edp = true;
  3411. else
  3412. has_cpu_edp = true;
  3413. break;
  3414. }
  3415. }
  3416. if (HAS_PCH_IBX(dev)) {
  3417. has_ck505 = dev_priv->display_clock_mode;
  3418. can_ssc = has_ck505;
  3419. } else {
  3420. has_ck505 = false;
  3421. can_ssc = true;
  3422. }
  3423. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
  3424. has_panel, has_lvds, has_pch_edp, has_cpu_edp,
  3425. has_ck505);
  3426. /* Ironlake: try to setup display ref clock before DPLL
  3427. * enabling. This is only under driver's control after
  3428. * PCH B stepping, previous chipset stepping should be
  3429. * ignoring this setting.
  3430. */
  3431. temp = I915_READ(PCH_DREF_CONTROL);
  3432. /* Always enable nonspread source */
  3433. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  3434. if (has_ck505)
  3435. temp |= DREF_NONSPREAD_CK505_ENABLE;
  3436. else
  3437. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  3438. if (has_panel) {
  3439. temp &= ~DREF_SSC_SOURCE_MASK;
  3440. temp |= DREF_SSC_SOURCE_ENABLE;
  3441. /* SSC must be turned on before enabling the CPU output */
  3442. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  3443. DRM_DEBUG_KMS("Using SSC on panel\n");
  3444. temp |= DREF_SSC1_ENABLE;
  3445. } else
  3446. temp &= ~DREF_SSC1_ENABLE;
  3447. /* Get SSC going before enabling the outputs */
  3448. I915_WRITE(PCH_DREF_CONTROL, temp);
  3449. POSTING_READ(PCH_DREF_CONTROL);
  3450. udelay(200);
  3451. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  3452. /* Enable CPU source on CPU attached eDP */
  3453. if (has_cpu_edp) {
  3454. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  3455. DRM_DEBUG_KMS("Using SSC on eDP\n");
  3456. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  3457. }
  3458. else
  3459. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  3460. } else
  3461. temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  3462. I915_WRITE(PCH_DREF_CONTROL, temp);
  3463. POSTING_READ(PCH_DREF_CONTROL);
  3464. udelay(200);
  3465. } else {
  3466. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  3467. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  3468. /* Turn off CPU output */
  3469. temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  3470. I915_WRITE(PCH_DREF_CONTROL, temp);
  3471. POSTING_READ(PCH_DREF_CONTROL);
  3472. udelay(200);
  3473. /* Turn off the SSC source */
  3474. temp &= ~DREF_SSC_SOURCE_MASK;
  3475. temp |= DREF_SSC_SOURCE_DISABLE;
  3476. /* Turn off SSC1 */
  3477. temp &= ~ DREF_SSC1_ENABLE;
  3478. I915_WRITE(PCH_DREF_CONTROL, temp);
  3479. POSTING_READ(PCH_DREF_CONTROL);
  3480. udelay(200);
  3481. }
  3482. }
  3483. static int ironlake_get_refclk(struct drm_crtc *crtc)
  3484. {
  3485. struct drm_device *dev = crtc->dev;
  3486. struct drm_i915_private *dev_priv = dev->dev_private;
  3487. struct intel_encoder *encoder;
  3488. struct drm_mode_config *mode_config = &dev->mode_config;
  3489. struct intel_encoder *edp_encoder = NULL;
  3490. int num_connectors = 0;
  3491. bool is_lvds = false;
  3492. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  3493. if (encoder->base.crtc != crtc)
  3494. continue;
  3495. switch (encoder->type) {
  3496. case INTEL_OUTPUT_LVDS:
  3497. is_lvds = true;
  3498. break;
  3499. case INTEL_OUTPUT_EDP:
  3500. edp_encoder = encoder;
  3501. break;
  3502. }
  3503. num_connectors++;
  3504. }
  3505. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  3506. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3507. dev_priv->lvds_ssc_freq);
  3508. return dev_priv->lvds_ssc_freq * 1000;
  3509. }
  3510. return 120000;
  3511. }
  3512. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  3513. struct drm_display_mode *mode,
  3514. struct drm_display_mode *adjusted_mode,
  3515. int x, int y,
  3516. struct drm_framebuffer *old_fb)
  3517. {
  3518. struct drm_device *dev = crtc->dev;
  3519. struct drm_i915_private *dev_priv = dev->dev_private;
  3520. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3521. int pipe = intel_crtc->pipe;
  3522. int plane = intel_crtc->plane;
  3523. int refclk, num_connectors = 0;
  3524. intel_clock_t clock, reduced_clock;
  3525. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  3526. bool ok, has_reduced_clock = false, is_sdvo = false;
  3527. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  3528. struct drm_mode_config *mode_config = &dev->mode_config;
  3529. struct intel_encoder *encoder, *edp_encoder = NULL;
  3530. const intel_limit_t *limit;
  3531. int ret;
  3532. struct fdi_m_n m_n = {0};
  3533. u32 temp;
  3534. int target_clock, pixel_multiplier, lane, link_bw, factor;
  3535. unsigned int pipe_bpp;
  3536. bool dither;
  3537. bool is_cpu_edp = false, is_pch_edp = false;
  3538. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  3539. if (encoder->base.crtc != crtc)
  3540. continue;
  3541. switch (encoder->type) {
  3542. case INTEL_OUTPUT_LVDS:
  3543. is_lvds = true;
  3544. break;
  3545. case INTEL_OUTPUT_SDVO:
  3546. case INTEL_OUTPUT_HDMI:
  3547. is_sdvo = true;
  3548. if (encoder->needs_tv_clock)
  3549. is_tv = true;
  3550. break;
  3551. case INTEL_OUTPUT_TVOUT:
  3552. is_tv = true;
  3553. break;
  3554. case INTEL_OUTPUT_ANALOG:
  3555. is_crt = true;
  3556. break;
  3557. case INTEL_OUTPUT_DISPLAYPORT:
  3558. is_dp = true;
  3559. break;
  3560. case INTEL_OUTPUT_EDP:
  3561. is_dp = true;
  3562. if (intel_encoder_is_pch_edp(&encoder->base))
  3563. is_pch_edp = true;
  3564. else
  3565. is_cpu_edp = true;
  3566. edp_encoder = encoder;
  3567. break;
  3568. }
  3569. num_connectors++;
  3570. }
  3571. refclk = ironlake_get_refclk(crtc);
  3572. /*
  3573. * Returns a set of divisors for the desired target clock with the given
  3574. * refclk, or FALSE. The returned values represent the clock equation:
  3575. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3576. */
  3577. limit = intel_limit(crtc, refclk);
  3578. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
  3579. &clock);
  3580. if (!ok) {
  3581. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3582. return -EINVAL;
  3583. }
  3584. /* Ensure that the cursor is valid for the new mode before changing... */
  3585. intel_crtc_update_cursor(crtc, true);
  3586. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3587. /*
  3588. * Ensure we match the reduced clock's P to the target clock.
  3589. * If the clocks don't match, we can't switch the display clock
  3590. * by using the FP0/FP1. In such case we will disable the LVDS
  3591. * downclock feature.
  3592. */
  3593. has_reduced_clock = limit->find_pll(limit, crtc,
  3594. dev_priv->lvds_downclock,
  3595. refclk,
  3596. &clock,
  3597. &reduced_clock);
  3598. }
  3599. /* SDVO TV has fixed PLL values depend on its clock range,
  3600. this mirrors vbios setting. */
  3601. if (is_sdvo && is_tv) {
  3602. if (adjusted_mode->clock >= 100000
  3603. && adjusted_mode->clock < 140500) {
  3604. clock.p1 = 2;
  3605. clock.p2 = 10;
  3606. clock.n = 3;
  3607. clock.m1 = 16;
  3608. clock.m2 = 8;
  3609. } else if (adjusted_mode->clock >= 140500
  3610. && adjusted_mode->clock <= 200000) {
  3611. clock.p1 = 1;
  3612. clock.p2 = 10;
  3613. clock.n = 6;
  3614. clock.m1 = 12;
  3615. clock.m2 = 8;
  3616. }
  3617. }
  3618. /* FDI link */
  3619. pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3620. lane = 0;
  3621. /* CPU eDP doesn't require FDI link, so just set DP M/N
  3622. according to current link config */
  3623. if (is_cpu_edp) {
  3624. target_clock = mode->clock;
  3625. intel_edp_link_config(edp_encoder, &lane, &link_bw);
  3626. } else {
  3627. /* [e]DP over FDI requires target mode clock
  3628. instead of link clock */
  3629. if (is_dp)
  3630. target_clock = mode->clock;
  3631. else
  3632. target_clock = adjusted_mode->clock;
  3633. /* FDI is a binary signal running at ~2.7GHz, encoding
  3634. * each output octet as 10 bits. The actual frequency
  3635. * is stored as a divider into a 100MHz clock, and the
  3636. * mode pixel clock is stored in units of 1KHz.
  3637. * Hence the bw of each lane in terms of the mode signal
  3638. * is:
  3639. */
  3640. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  3641. }
  3642. /* determine panel color depth */
  3643. temp = I915_READ(PIPECONF(pipe));
  3644. temp &= ~PIPE_BPC_MASK;
  3645. dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp, mode);
  3646. switch (pipe_bpp) {
  3647. case 18:
  3648. temp |= PIPE_6BPC;
  3649. break;
  3650. case 24:
  3651. temp |= PIPE_8BPC;
  3652. break;
  3653. case 30:
  3654. temp |= PIPE_10BPC;
  3655. break;
  3656. case 36:
  3657. temp |= PIPE_12BPC;
  3658. break;
  3659. default:
  3660. WARN(1, "intel_choose_pipe_bpp returned invalid value %d\n",
  3661. pipe_bpp);
  3662. temp |= PIPE_8BPC;
  3663. pipe_bpp = 24;
  3664. break;
  3665. }
  3666. intel_crtc->bpp = pipe_bpp;
  3667. I915_WRITE(PIPECONF(pipe), temp);
  3668. if (!lane) {
  3669. /*
  3670. * Account for spread spectrum to avoid
  3671. * oversubscribing the link. Max center spread
  3672. * is 2.5%; use 5% for safety's sake.
  3673. */
  3674. u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
  3675. lane = bps / (link_bw * 8) + 1;
  3676. }
  3677. intel_crtc->fdi_lanes = lane;
  3678. if (pixel_multiplier > 1)
  3679. link_bw *= pixel_multiplier;
  3680. ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
  3681. &m_n);
  3682. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  3683. if (has_reduced_clock)
  3684. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  3685. reduced_clock.m2;
  3686. /* Enable autotuning of the PLL clock (if permissible) */
  3687. factor = 21;
  3688. if (is_lvds) {
  3689. if ((intel_panel_use_ssc(dev_priv) &&
  3690. dev_priv->lvds_ssc_freq == 100) ||
  3691. (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
  3692. factor = 25;
  3693. } else if (is_sdvo && is_tv)
  3694. factor = 20;
  3695. if (clock.m < factor * clock.n)
  3696. fp |= FP_CB_TUNE;
  3697. dpll = 0;
  3698. if (is_lvds)
  3699. dpll |= DPLLB_MODE_LVDS;
  3700. else
  3701. dpll |= DPLLB_MODE_DAC_SERIAL;
  3702. if (is_sdvo) {
  3703. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  3704. if (pixel_multiplier > 1) {
  3705. dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  3706. }
  3707. dpll |= DPLL_DVO_HIGH_SPEED;
  3708. }
  3709. if (is_dp && !is_cpu_edp)
  3710. dpll |= DPLL_DVO_HIGH_SPEED;
  3711. /* compute bitmask from p1 value */
  3712. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3713. /* also FPA1 */
  3714. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3715. switch (clock.p2) {
  3716. case 5:
  3717. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3718. break;
  3719. case 7:
  3720. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3721. break;
  3722. case 10:
  3723. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3724. break;
  3725. case 14:
  3726. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3727. break;
  3728. }
  3729. if (is_sdvo && is_tv)
  3730. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3731. else if (is_tv)
  3732. /* XXX: just matching BIOS for now */
  3733. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3734. dpll |= 3;
  3735. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3736. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3737. else
  3738. dpll |= PLL_REF_INPUT_DREFCLK;
  3739. /* setup pipeconf */
  3740. pipeconf = I915_READ(PIPECONF(pipe));
  3741. /* Set up the display plane register */
  3742. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3743. DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
  3744. drm_mode_debug_printmodeline(mode);
  3745. /* CPU eDP is the only output that doesn't need a PCH PLL of its own */
  3746. if (!is_cpu_edp) {
  3747. struct intel_pch_pll *pll;
  3748. pll = intel_get_pch_pll(intel_crtc, dpll, fp);
  3749. if (pll == NULL) {
  3750. DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
  3751. pipe);
  3752. return -EINVAL;
  3753. }
  3754. } else
  3755. intel_put_pch_pll(intel_crtc);
  3756. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3757. * This is an exception to the general rule that mode_set doesn't turn
  3758. * things on.
  3759. */
  3760. if (is_lvds) {
  3761. temp = I915_READ(PCH_LVDS);
  3762. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  3763. if (HAS_PCH_CPT(dev)) {
  3764. temp &= ~PORT_TRANS_SEL_MASK;
  3765. temp |= PORT_TRANS_SEL_CPT(pipe);
  3766. } else {
  3767. if (pipe == 1)
  3768. temp |= LVDS_PIPEB_SELECT;
  3769. else
  3770. temp &= ~LVDS_PIPEB_SELECT;
  3771. }
  3772. /* set the corresponsding LVDS_BORDER bit */
  3773. temp |= dev_priv->lvds_border_bits;
  3774. /* Set the B0-B3 data pairs corresponding to whether we're going to
  3775. * set the DPLLs for dual-channel mode or not.
  3776. */
  3777. if (clock.p2 == 7)
  3778. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  3779. else
  3780. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  3781. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  3782. * appropriately here, but we need to look more thoroughly into how
  3783. * panels behave in the two modes.
  3784. */
  3785. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  3786. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  3787. temp |= LVDS_HSYNC_POLARITY;
  3788. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  3789. temp |= LVDS_VSYNC_POLARITY;
  3790. I915_WRITE(PCH_LVDS, temp);
  3791. }
  3792. pipeconf &= ~PIPECONF_DITHER_EN;
  3793. pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
  3794. if ((is_lvds && dev_priv->lvds_dither) || dither) {
  3795. pipeconf |= PIPECONF_DITHER_EN;
  3796. pipeconf |= PIPECONF_DITHER_TYPE_SP;
  3797. }
  3798. if (is_dp && !is_cpu_edp) {
  3799. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3800. } else {
  3801. /* For non-DP output, clear any trans DP clock recovery setting.*/
  3802. I915_WRITE(TRANSDATA_M1(pipe), 0);
  3803. I915_WRITE(TRANSDATA_N1(pipe), 0);
  3804. I915_WRITE(TRANSDPLINK_M1(pipe), 0);
  3805. I915_WRITE(TRANSDPLINK_N1(pipe), 0);
  3806. }
  3807. if (intel_crtc->pch_pll) {
  3808. I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
  3809. /* Wait for the clocks to stabilize. */
  3810. POSTING_READ(intel_crtc->pch_pll->pll_reg);
  3811. udelay(150);
  3812. /* The pixel multiplier can only be updated once the
  3813. * DPLL is enabled and the clocks are stable.
  3814. *
  3815. * So write it again.
  3816. */
  3817. I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
  3818. }
  3819. intel_crtc->lowfreq_avail = false;
  3820. if (intel_crtc->pch_pll) {
  3821. if (is_lvds && has_reduced_clock && i915_powersave) {
  3822. I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
  3823. intel_crtc->lowfreq_avail = true;
  3824. if (HAS_PIPE_CXSR(dev)) {
  3825. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  3826. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  3827. }
  3828. } else {
  3829. I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
  3830. if (HAS_PIPE_CXSR(dev)) {
  3831. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  3832. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  3833. }
  3834. }
  3835. }
  3836. pipeconf &= ~PIPECONF_INTERLACE_MASK;
  3837. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3838. pipeconf |= PIPECONF_INTERLACED_ILK;
  3839. /* the chip adds 2 halflines automatically */
  3840. adjusted_mode->crtc_vtotal -= 1;
  3841. adjusted_mode->crtc_vblank_end -= 1;
  3842. I915_WRITE(VSYNCSHIFT(pipe),
  3843. adjusted_mode->crtc_hsync_start
  3844. - adjusted_mode->crtc_htotal/2);
  3845. } else {
  3846. pipeconf |= PIPECONF_PROGRESSIVE;
  3847. I915_WRITE(VSYNCSHIFT(pipe), 0);
  3848. }
  3849. I915_WRITE(HTOTAL(pipe),
  3850. (adjusted_mode->crtc_hdisplay - 1) |
  3851. ((adjusted_mode->crtc_htotal - 1) << 16));
  3852. I915_WRITE(HBLANK(pipe),
  3853. (adjusted_mode->crtc_hblank_start - 1) |
  3854. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3855. I915_WRITE(HSYNC(pipe),
  3856. (adjusted_mode->crtc_hsync_start - 1) |
  3857. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3858. I915_WRITE(VTOTAL(pipe),
  3859. (adjusted_mode->crtc_vdisplay - 1) |
  3860. ((adjusted_mode->crtc_vtotal - 1) << 16));
  3861. I915_WRITE(VBLANK(pipe),
  3862. (adjusted_mode->crtc_vblank_start - 1) |
  3863. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  3864. I915_WRITE(VSYNC(pipe),
  3865. (adjusted_mode->crtc_vsync_start - 1) |
  3866. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3867. /* pipesrc controls the size that is scaled from, which should
  3868. * always be the user's requested size.
  3869. */
  3870. I915_WRITE(PIPESRC(pipe),
  3871. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3872. I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
  3873. I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
  3874. I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
  3875. I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
  3876. if (is_cpu_edp)
  3877. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  3878. I915_WRITE(PIPECONF(pipe), pipeconf);
  3879. POSTING_READ(PIPECONF(pipe));
  3880. intel_wait_for_vblank(dev, pipe);
  3881. I915_WRITE(DSPCNTR(plane), dspcntr);
  3882. POSTING_READ(DSPCNTR(plane));
  3883. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  3884. intel_update_watermarks(dev);
  3885. return ret;
  3886. }
  3887. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  3888. struct drm_display_mode *mode,
  3889. struct drm_display_mode *adjusted_mode,
  3890. int x, int y,
  3891. struct drm_framebuffer *old_fb)
  3892. {
  3893. struct drm_device *dev = crtc->dev;
  3894. struct drm_i915_private *dev_priv = dev->dev_private;
  3895. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3896. int pipe = intel_crtc->pipe;
  3897. int ret;
  3898. drm_vblank_pre_modeset(dev, pipe);
  3899. ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
  3900. x, y, old_fb);
  3901. drm_vblank_post_modeset(dev, pipe);
  3902. if (ret)
  3903. intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
  3904. else
  3905. intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
  3906. return ret;
  3907. }
  3908. static bool intel_eld_uptodate(struct drm_connector *connector,
  3909. int reg_eldv, uint32_t bits_eldv,
  3910. int reg_elda, uint32_t bits_elda,
  3911. int reg_edid)
  3912. {
  3913. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  3914. uint8_t *eld = connector->eld;
  3915. uint32_t i;
  3916. i = I915_READ(reg_eldv);
  3917. i &= bits_eldv;
  3918. if (!eld[0])
  3919. return !i;
  3920. if (!i)
  3921. return false;
  3922. i = I915_READ(reg_elda);
  3923. i &= ~bits_elda;
  3924. I915_WRITE(reg_elda, i);
  3925. for (i = 0; i < eld[2]; i++)
  3926. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  3927. return false;
  3928. return true;
  3929. }
  3930. static void g4x_write_eld(struct drm_connector *connector,
  3931. struct drm_crtc *crtc)
  3932. {
  3933. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  3934. uint8_t *eld = connector->eld;
  3935. uint32_t eldv;
  3936. uint32_t len;
  3937. uint32_t i;
  3938. i = I915_READ(G4X_AUD_VID_DID);
  3939. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  3940. eldv = G4X_ELDV_DEVCL_DEVBLC;
  3941. else
  3942. eldv = G4X_ELDV_DEVCTG;
  3943. if (intel_eld_uptodate(connector,
  3944. G4X_AUD_CNTL_ST, eldv,
  3945. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  3946. G4X_HDMIW_HDMIEDID))
  3947. return;
  3948. i = I915_READ(G4X_AUD_CNTL_ST);
  3949. i &= ~(eldv | G4X_ELD_ADDR);
  3950. len = (i >> 9) & 0x1f; /* ELD buffer size */
  3951. I915_WRITE(G4X_AUD_CNTL_ST, i);
  3952. if (!eld[0])
  3953. return;
  3954. len = min_t(uint8_t, eld[2], len);
  3955. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  3956. for (i = 0; i < len; i++)
  3957. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  3958. i = I915_READ(G4X_AUD_CNTL_ST);
  3959. i |= eldv;
  3960. I915_WRITE(G4X_AUD_CNTL_ST, i);
  3961. }
  3962. static void ironlake_write_eld(struct drm_connector *connector,
  3963. struct drm_crtc *crtc)
  3964. {
  3965. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  3966. uint8_t *eld = connector->eld;
  3967. uint32_t eldv;
  3968. uint32_t i;
  3969. int len;
  3970. int hdmiw_hdmiedid;
  3971. int aud_config;
  3972. int aud_cntl_st;
  3973. int aud_cntrl_st2;
  3974. if (HAS_PCH_IBX(connector->dev)) {
  3975. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID_A;
  3976. aud_config = IBX_AUD_CONFIG_A;
  3977. aud_cntl_st = IBX_AUD_CNTL_ST_A;
  3978. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  3979. } else {
  3980. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID_A;
  3981. aud_config = CPT_AUD_CONFIG_A;
  3982. aud_cntl_st = CPT_AUD_CNTL_ST_A;
  3983. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  3984. }
  3985. i = to_intel_crtc(crtc)->pipe;
  3986. hdmiw_hdmiedid += i * 0x100;
  3987. aud_cntl_st += i * 0x100;
  3988. aud_config += i * 0x100;
  3989. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(i));
  3990. i = I915_READ(aud_cntl_st);
  3991. i = (i >> 29) & 0x3; /* DIP_Port_Select, 0x1 = PortB */
  3992. if (!i) {
  3993. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  3994. /* operate blindly on all ports */
  3995. eldv = IBX_ELD_VALIDB;
  3996. eldv |= IBX_ELD_VALIDB << 4;
  3997. eldv |= IBX_ELD_VALIDB << 8;
  3998. } else {
  3999. DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
  4000. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  4001. }
  4002. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  4003. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  4004. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  4005. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  4006. } else
  4007. I915_WRITE(aud_config, 0);
  4008. if (intel_eld_uptodate(connector,
  4009. aud_cntrl_st2, eldv,
  4010. aud_cntl_st, IBX_ELD_ADDRESS,
  4011. hdmiw_hdmiedid))
  4012. return;
  4013. i = I915_READ(aud_cntrl_st2);
  4014. i &= ~eldv;
  4015. I915_WRITE(aud_cntrl_st2, i);
  4016. if (!eld[0])
  4017. return;
  4018. i = I915_READ(aud_cntl_st);
  4019. i &= ~IBX_ELD_ADDRESS;
  4020. I915_WRITE(aud_cntl_st, i);
  4021. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  4022. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  4023. for (i = 0; i < len; i++)
  4024. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  4025. i = I915_READ(aud_cntrl_st2);
  4026. i |= eldv;
  4027. I915_WRITE(aud_cntrl_st2, i);
  4028. }
  4029. void intel_write_eld(struct drm_encoder *encoder,
  4030. struct drm_display_mode *mode)
  4031. {
  4032. struct drm_crtc *crtc = encoder->crtc;
  4033. struct drm_connector *connector;
  4034. struct drm_device *dev = encoder->dev;
  4035. struct drm_i915_private *dev_priv = dev->dev_private;
  4036. connector = drm_select_eld(encoder, mode);
  4037. if (!connector)
  4038. return;
  4039. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  4040. connector->base.id,
  4041. drm_get_connector_name(connector),
  4042. connector->encoder->base.id,
  4043. drm_get_encoder_name(connector->encoder));
  4044. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  4045. if (dev_priv->display.write_eld)
  4046. dev_priv->display.write_eld(connector, crtc);
  4047. }
  4048. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  4049. void intel_crtc_load_lut(struct drm_crtc *crtc)
  4050. {
  4051. struct drm_device *dev = crtc->dev;
  4052. struct drm_i915_private *dev_priv = dev->dev_private;
  4053. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4054. int palreg = PALETTE(intel_crtc->pipe);
  4055. int i;
  4056. /* The clocks have to be on to load the palette. */
  4057. if (!crtc->enabled || !intel_crtc->active)
  4058. return;
  4059. /* use legacy palette for Ironlake */
  4060. if (HAS_PCH_SPLIT(dev))
  4061. palreg = LGC_PALETTE(intel_crtc->pipe);
  4062. for (i = 0; i < 256; i++) {
  4063. I915_WRITE(palreg + 4 * i,
  4064. (intel_crtc->lut_r[i] << 16) |
  4065. (intel_crtc->lut_g[i] << 8) |
  4066. intel_crtc->lut_b[i]);
  4067. }
  4068. }
  4069. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  4070. {
  4071. struct drm_device *dev = crtc->dev;
  4072. struct drm_i915_private *dev_priv = dev->dev_private;
  4073. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4074. bool visible = base != 0;
  4075. u32 cntl;
  4076. if (intel_crtc->cursor_visible == visible)
  4077. return;
  4078. cntl = I915_READ(_CURACNTR);
  4079. if (visible) {
  4080. /* On these chipsets we can only modify the base whilst
  4081. * the cursor is disabled.
  4082. */
  4083. I915_WRITE(_CURABASE, base);
  4084. cntl &= ~(CURSOR_FORMAT_MASK);
  4085. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  4086. cntl |= CURSOR_ENABLE |
  4087. CURSOR_GAMMA_ENABLE |
  4088. CURSOR_FORMAT_ARGB;
  4089. } else
  4090. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  4091. I915_WRITE(_CURACNTR, cntl);
  4092. intel_crtc->cursor_visible = visible;
  4093. }
  4094. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  4095. {
  4096. struct drm_device *dev = crtc->dev;
  4097. struct drm_i915_private *dev_priv = dev->dev_private;
  4098. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4099. int pipe = intel_crtc->pipe;
  4100. bool visible = base != 0;
  4101. if (intel_crtc->cursor_visible != visible) {
  4102. uint32_t cntl = I915_READ(CURCNTR(pipe));
  4103. if (base) {
  4104. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  4105. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  4106. cntl |= pipe << 28; /* Connect to correct pipe */
  4107. } else {
  4108. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  4109. cntl |= CURSOR_MODE_DISABLE;
  4110. }
  4111. I915_WRITE(CURCNTR(pipe), cntl);
  4112. intel_crtc->cursor_visible = visible;
  4113. }
  4114. /* and commit changes on next vblank */
  4115. I915_WRITE(CURBASE(pipe), base);
  4116. }
  4117. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  4118. {
  4119. struct drm_device *dev = crtc->dev;
  4120. struct drm_i915_private *dev_priv = dev->dev_private;
  4121. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4122. int pipe = intel_crtc->pipe;
  4123. bool visible = base != 0;
  4124. if (intel_crtc->cursor_visible != visible) {
  4125. uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
  4126. if (base) {
  4127. cntl &= ~CURSOR_MODE;
  4128. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  4129. } else {
  4130. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  4131. cntl |= CURSOR_MODE_DISABLE;
  4132. }
  4133. I915_WRITE(CURCNTR_IVB(pipe), cntl);
  4134. intel_crtc->cursor_visible = visible;
  4135. }
  4136. /* and commit changes on next vblank */
  4137. I915_WRITE(CURBASE_IVB(pipe), base);
  4138. }
  4139. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  4140. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  4141. bool on)
  4142. {
  4143. struct drm_device *dev = crtc->dev;
  4144. struct drm_i915_private *dev_priv = dev->dev_private;
  4145. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4146. int pipe = intel_crtc->pipe;
  4147. int x = intel_crtc->cursor_x;
  4148. int y = intel_crtc->cursor_y;
  4149. u32 base, pos;
  4150. bool visible;
  4151. pos = 0;
  4152. if (on && crtc->enabled && crtc->fb) {
  4153. base = intel_crtc->cursor_addr;
  4154. if (x > (int) crtc->fb->width)
  4155. base = 0;
  4156. if (y > (int) crtc->fb->height)
  4157. base = 0;
  4158. } else
  4159. base = 0;
  4160. if (x < 0) {
  4161. if (x + intel_crtc->cursor_width < 0)
  4162. base = 0;
  4163. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  4164. x = -x;
  4165. }
  4166. pos |= x << CURSOR_X_SHIFT;
  4167. if (y < 0) {
  4168. if (y + intel_crtc->cursor_height < 0)
  4169. base = 0;
  4170. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  4171. y = -y;
  4172. }
  4173. pos |= y << CURSOR_Y_SHIFT;
  4174. visible = base != 0;
  4175. if (!visible && !intel_crtc->cursor_visible)
  4176. return;
  4177. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  4178. I915_WRITE(CURPOS_IVB(pipe), pos);
  4179. ivb_update_cursor(crtc, base);
  4180. } else {
  4181. I915_WRITE(CURPOS(pipe), pos);
  4182. if (IS_845G(dev) || IS_I865G(dev))
  4183. i845_update_cursor(crtc, base);
  4184. else
  4185. i9xx_update_cursor(crtc, base);
  4186. }
  4187. }
  4188. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  4189. struct drm_file *file,
  4190. uint32_t handle,
  4191. uint32_t width, uint32_t height)
  4192. {
  4193. struct drm_device *dev = crtc->dev;
  4194. struct drm_i915_private *dev_priv = dev->dev_private;
  4195. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4196. struct drm_i915_gem_object *obj;
  4197. uint32_t addr;
  4198. int ret;
  4199. DRM_DEBUG_KMS("\n");
  4200. /* if we want to turn off the cursor ignore width and height */
  4201. if (!handle) {
  4202. DRM_DEBUG_KMS("cursor off\n");
  4203. addr = 0;
  4204. obj = NULL;
  4205. mutex_lock(&dev->struct_mutex);
  4206. goto finish;
  4207. }
  4208. /* Currently we only support 64x64 cursors */
  4209. if (width != 64 || height != 64) {
  4210. DRM_ERROR("we currently only support 64x64 cursors\n");
  4211. return -EINVAL;
  4212. }
  4213. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  4214. if (&obj->base == NULL)
  4215. return -ENOENT;
  4216. if (obj->base.size < width * height * 4) {
  4217. DRM_ERROR("buffer is to small\n");
  4218. ret = -ENOMEM;
  4219. goto fail;
  4220. }
  4221. /* we only need to pin inside GTT if cursor is non-phy */
  4222. mutex_lock(&dev->struct_mutex);
  4223. if (!dev_priv->info->cursor_needs_physical) {
  4224. if (obj->tiling_mode) {
  4225. DRM_ERROR("cursor cannot be tiled\n");
  4226. ret = -EINVAL;
  4227. goto fail_locked;
  4228. }
  4229. ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
  4230. if (ret) {
  4231. DRM_ERROR("failed to move cursor bo into the GTT\n");
  4232. goto fail_locked;
  4233. }
  4234. ret = i915_gem_object_put_fence(obj);
  4235. if (ret) {
  4236. DRM_ERROR("failed to release fence for cursor");
  4237. goto fail_unpin;
  4238. }
  4239. addr = obj->gtt_offset;
  4240. } else {
  4241. int align = IS_I830(dev) ? 16 * 1024 : 256;
  4242. ret = i915_gem_attach_phys_object(dev, obj,
  4243. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  4244. align);
  4245. if (ret) {
  4246. DRM_ERROR("failed to attach phys object\n");
  4247. goto fail_locked;
  4248. }
  4249. addr = obj->phys_obj->handle->busaddr;
  4250. }
  4251. if (IS_GEN2(dev))
  4252. I915_WRITE(CURSIZE, (height << 12) | width);
  4253. finish:
  4254. if (intel_crtc->cursor_bo) {
  4255. if (dev_priv->info->cursor_needs_physical) {
  4256. if (intel_crtc->cursor_bo != obj)
  4257. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  4258. } else
  4259. i915_gem_object_unpin(intel_crtc->cursor_bo);
  4260. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  4261. }
  4262. mutex_unlock(&dev->struct_mutex);
  4263. intel_crtc->cursor_addr = addr;
  4264. intel_crtc->cursor_bo = obj;
  4265. intel_crtc->cursor_width = width;
  4266. intel_crtc->cursor_height = height;
  4267. intel_crtc_update_cursor(crtc, true);
  4268. return 0;
  4269. fail_unpin:
  4270. i915_gem_object_unpin(obj);
  4271. fail_locked:
  4272. mutex_unlock(&dev->struct_mutex);
  4273. fail:
  4274. drm_gem_object_unreference_unlocked(&obj->base);
  4275. return ret;
  4276. }
  4277. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  4278. {
  4279. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4280. intel_crtc->cursor_x = x;
  4281. intel_crtc->cursor_y = y;
  4282. intel_crtc_update_cursor(crtc, true);
  4283. return 0;
  4284. }
  4285. /** Sets the color ramps on behalf of RandR */
  4286. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  4287. u16 blue, int regno)
  4288. {
  4289. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4290. intel_crtc->lut_r[regno] = red >> 8;
  4291. intel_crtc->lut_g[regno] = green >> 8;
  4292. intel_crtc->lut_b[regno] = blue >> 8;
  4293. }
  4294. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  4295. u16 *blue, int regno)
  4296. {
  4297. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4298. *red = intel_crtc->lut_r[regno] << 8;
  4299. *green = intel_crtc->lut_g[regno] << 8;
  4300. *blue = intel_crtc->lut_b[regno] << 8;
  4301. }
  4302. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  4303. u16 *blue, uint32_t start, uint32_t size)
  4304. {
  4305. int end = (start + size > 256) ? 256 : start + size, i;
  4306. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4307. for (i = start; i < end; i++) {
  4308. intel_crtc->lut_r[i] = red[i] >> 8;
  4309. intel_crtc->lut_g[i] = green[i] >> 8;
  4310. intel_crtc->lut_b[i] = blue[i] >> 8;
  4311. }
  4312. intel_crtc_load_lut(crtc);
  4313. }
  4314. /**
  4315. * Get a pipe with a simple mode set on it for doing load-based monitor
  4316. * detection.
  4317. *
  4318. * It will be up to the load-detect code to adjust the pipe as appropriate for
  4319. * its requirements. The pipe will be connected to no other encoders.
  4320. *
  4321. * Currently this code will only succeed if there is a pipe with no encoders
  4322. * configured for it. In the future, it could choose to temporarily disable
  4323. * some outputs to free up a pipe for its use.
  4324. *
  4325. * \return crtc, or NULL if no pipes are available.
  4326. */
  4327. /* VESA 640x480x72Hz mode to set on the pipe */
  4328. static struct drm_display_mode load_detect_mode = {
  4329. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  4330. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  4331. };
  4332. static struct drm_framebuffer *
  4333. intel_framebuffer_create(struct drm_device *dev,
  4334. struct drm_mode_fb_cmd2 *mode_cmd,
  4335. struct drm_i915_gem_object *obj)
  4336. {
  4337. struct intel_framebuffer *intel_fb;
  4338. int ret;
  4339. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  4340. if (!intel_fb) {
  4341. drm_gem_object_unreference_unlocked(&obj->base);
  4342. return ERR_PTR(-ENOMEM);
  4343. }
  4344. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  4345. if (ret) {
  4346. drm_gem_object_unreference_unlocked(&obj->base);
  4347. kfree(intel_fb);
  4348. return ERR_PTR(ret);
  4349. }
  4350. return &intel_fb->base;
  4351. }
  4352. static u32
  4353. intel_framebuffer_pitch_for_width(int width, int bpp)
  4354. {
  4355. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  4356. return ALIGN(pitch, 64);
  4357. }
  4358. static u32
  4359. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  4360. {
  4361. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  4362. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  4363. }
  4364. static struct drm_framebuffer *
  4365. intel_framebuffer_create_for_mode(struct drm_device *dev,
  4366. struct drm_display_mode *mode,
  4367. int depth, int bpp)
  4368. {
  4369. struct drm_i915_gem_object *obj;
  4370. struct drm_mode_fb_cmd2 mode_cmd;
  4371. obj = i915_gem_alloc_object(dev,
  4372. intel_framebuffer_size_for_mode(mode, bpp));
  4373. if (obj == NULL)
  4374. return ERR_PTR(-ENOMEM);
  4375. mode_cmd.width = mode->hdisplay;
  4376. mode_cmd.height = mode->vdisplay;
  4377. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  4378. bpp);
  4379. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  4380. return intel_framebuffer_create(dev, &mode_cmd, obj);
  4381. }
  4382. static struct drm_framebuffer *
  4383. mode_fits_in_fbdev(struct drm_device *dev,
  4384. struct drm_display_mode *mode)
  4385. {
  4386. struct drm_i915_private *dev_priv = dev->dev_private;
  4387. struct drm_i915_gem_object *obj;
  4388. struct drm_framebuffer *fb;
  4389. if (dev_priv->fbdev == NULL)
  4390. return NULL;
  4391. obj = dev_priv->fbdev->ifb.obj;
  4392. if (obj == NULL)
  4393. return NULL;
  4394. fb = &dev_priv->fbdev->ifb.base;
  4395. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  4396. fb->bits_per_pixel))
  4397. return NULL;
  4398. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  4399. return NULL;
  4400. return fb;
  4401. }
  4402. bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  4403. struct drm_connector *connector,
  4404. struct drm_display_mode *mode,
  4405. struct intel_load_detect_pipe *old)
  4406. {
  4407. struct intel_crtc *intel_crtc;
  4408. struct drm_crtc *possible_crtc;
  4409. struct drm_encoder *encoder = &intel_encoder->base;
  4410. struct drm_crtc *crtc = NULL;
  4411. struct drm_device *dev = encoder->dev;
  4412. struct drm_framebuffer *old_fb;
  4413. int i = -1;
  4414. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  4415. connector->base.id, drm_get_connector_name(connector),
  4416. encoder->base.id, drm_get_encoder_name(encoder));
  4417. /*
  4418. * Algorithm gets a little messy:
  4419. *
  4420. * - if the connector already has an assigned crtc, use it (but make
  4421. * sure it's on first)
  4422. *
  4423. * - try to find the first unused crtc that can drive this connector,
  4424. * and use that if we find one
  4425. */
  4426. /* See if we already have a CRTC for this connector */
  4427. if (encoder->crtc) {
  4428. crtc = encoder->crtc;
  4429. intel_crtc = to_intel_crtc(crtc);
  4430. old->dpms_mode = intel_crtc->dpms_mode;
  4431. old->load_detect_temp = false;
  4432. /* Make sure the crtc and connector are running */
  4433. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  4434. struct drm_encoder_helper_funcs *encoder_funcs;
  4435. struct drm_crtc_helper_funcs *crtc_funcs;
  4436. crtc_funcs = crtc->helper_private;
  4437. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  4438. encoder_funcs = encoder->helper_private;
  4439. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  4440. }
  4441. return true;
  4442. }
  4443. /* Find an unused one (if possible) */
  4444. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  4445. i++;
  4446. if (!(encoder->possible_crtcs & (1 << i)))
  4447. continue;
  4448. if (!possible_crtc->enabled) {
  4449. crtc = possible_crtc;
  4450. break;
  4451. }
  4452. }
  4453. /*
  4454. * If we didn't find an unused CRTC, don't use any.
  4455. */
  4456. if (!crtc) {
  4457. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  4458. return false;
  4459. }
  4460. encoder->crtc = crtc;
  4461. connector->encoder = encoder;
  4462. intel_crtc = to_intel_crtc(crtc);
  4463. old->dpms_mode = intel_crtc->dpms_mode;
  4464. old->load_detect_temp = true;
  4465. old->release_fb = NULL;
  4466. if (!mode)
  4467. mode = &load_detect_mode;
  4468. old_fb = crtc->fb;
  4469. /* We need a framebuffer large enough to accommodate all accesses
  4470. * that the plane may generate whilst we perform load detection.
  4471. * We can not rely on the fbcon either being present (we get called
  4472. * during its initialisation to detect all boot displays, or it may
  4473. * not even exist) or that it is large enough to satisfy the
  4474. * requested mode.
  4475. */
  4476. crtc->fb = mode_fits_in_fbdev(dev, mode);
  4477. if (crtc->fb == NULL) {
  4478. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  4479. crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  4480. old->release_fb = crtc->fb;
  4481. } else
  4482. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  4483. if (IS_ERR(crtc->fb)) {
  4484. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  4485. crtc->fb = old_fb;
  4486. return false;
  4487. }
  4488. if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
  4489. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  4490. if (old->release_fb)
  4491. old->release_fb->funcs->destroy(old->release_fb);
  4492. crtc->fb = old_fb;
  4493. return false;
  4494. }
  4495. /* let the connector get through one full cycle before testing */
  4496. intel_wait_for_vblank(dev, intel_crtc->pipe);
  4497. return true;
  4498. }
  4499. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  4500. struct drm_connector *connector,
  4501. struct intel_load_detect_pipe *old)
  4502. {
  4503. struct drm_encoder *encoder = &intel_encoder->base;
  4504. struct drm_device *dev = encoder->dev;
  4505. struct drm_crtc *crtc = encoder->crtc;
  4506. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  4507. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  4508. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  4509. connector->base.id, drm_get_connector_name(connector),
  4510. encoder->base.id, drm_get_encoder_name(encoder));
  4511. if (old->load_detect_temp) {
  4512. connector->encoder = NULL;
  4513. drm_helper_disable_unused_functions(dev);
  4514. if (old->release_fb)
  4515. old->release_fb->funcs->destroy(old->release_fb);
  4516. return;
  4517. }
  4518. /* Switch crtc and encoder back off if necessary */
  4519. if (old->dpms_mode != DRM_MODE_DPMS_ON) {
  4520. encoder_funcs->dpms(encoder, old->dpms_mode);
  4521. crtc_funcs->dpms(crtc, old->dpms_mode);
  4522. }
  4523. }
  4524. /* Returns the clock of the currently programmed mode of the given pipe. */
  4525. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  4526. {
  4527. struct drm_i915_private *dev_priv = dev->dev_private;
  4528. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4529. int pipe = intel_crtc->pipe;
  4530. u32 dpll = I915_READ(DPLL(pipe));
  4531. u32 fp;
  4532. intel_clock_t clock;
  4533. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  4534. fp = I915_READ(FP0(pipe));
  4535. else
  4536. fp = I915_READ(FP1(pipe));
  4537. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  4538. if (IS_PINEVIEW(dev)) {
  4539. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  4540. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4541. } else {
  4542. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  4543. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  4544. }
  4545. if (!IS_GEN2(dev)) {
  4546. if (IS_PINEVIEW(dev))
  4547. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  4548. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  4549. else
  4550. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  4551. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4552. switch (dpll & DPLL_MODE_MASK) {
  4553. case DPLLB_MODE_DAC_SERIAL:
  4554. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  4555. 5 : 10;
  4556. break;
  4557. case DPLLB_MODE_LVDS:
  4558. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  4559. 7 : 14;
  4560. break;
  4561. default:
  4562. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  4563. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  4564. return 0;
  4565. }
  4566. /* XXX: Handle the 100Mhz refclk */
  4567. intel_clock(dev, 96000, &clock);
  4568. } else {
  4569. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  4570. if (is_lvds) {
  4571. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  4572. DPLL_FPA01_P1_POST_DIV_SHIFT);
  4573. clock.p2 = 14;
  4574. if ((dpll & PLL_REF_INPUT_MASK) ==
  4575. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  4576. /* XXX: might not be 66MHz */
  4577. intel_clock(dev, 66000, &clock);
  4578. } else
  4579. intel_clock(dev, 48000, &clock);
  4580. } else {
  4581. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  4582. clock.p1 = 2;
  4583. else {
  4584. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  4585. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  4586. }
  4587. if (dpll & PLL_P2_DIVIDE_BY_4)
  4588. clock.p2 = 4;
  4589. else
  4590. clock.p2 = 2;
  4591. intel_clock(dev, 48000, &clock);
  4592. }
  4593. }
  4594. /* XXX: It would be nice to validate the clocks, but we can't reuse
  4595. * i830PllIsValid() because it relies on the xf86_config connector
  4596. * configuration being accurate, which it isn't necessarily.
  4597. */
  4598. return clock.dot;
  4599. }
  4600. /** Returns the currently programmed mode of the given pipe. */
  4601. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  4602. struct drm_crtc *crtc)
  4603. {
  4604. struct drm_i915_private *dev_priv = dev->dev_private;
  4605. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4606. int pipe = intel_crtc->pipe;
  4607. struct drm_display_mode *mode;
  4608. int htot = I915_READ(HTOTAL(pipe));
  4609. int hsync = I915_READ(HSYNC(pipe));
  4610. int vtot = I915_READ(VTOTAL(pipe));
  4611. int vsync = I915_READ(VSYNC(pipe));
  4612. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  4613. if (!mode)
  4614. return NULL;
  4615. mode->clock = intel_crtc_clock_get(dev, crtc);
  4616. mode->hdisplay = (htot & 0xffff) + 1;
  4617. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  4618. mode->hsync_start = (hsync & 0xffff) + 1;
  4619. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  4620. mode->vdisplay = (vtot & 0xffff) + 1;
  4621. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  4622. mode->vsync_start = (vsync & 0xffff) + 1;
  4623. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  4624. drm_mode_set_name(mode);
  4625. return mode;
  4626. }
  4627. #define GPU_IDLE_TIMEOUT 500 /* ms */
  4628. /* When this timer fires, we've been idle for awhile */
  4629. static void intel_gpu_idle_timer(unsigned long arg)
  4630. {
  4631. struct drm_device *dev = (struct drm_device *)arg;
  4632. drm_i915_private_t *dev_priv = dev->dev_private;
  4633. if (!list_empty(&dev_priv->mm.active_list)) {
  4634. /* Still processing requests, so just re-arm the timer. */
  4635. mod_timer(&dev_priv->idle_timer, jiffies +
  4636. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4637. return;
  4638. }
  4639. dev_priv->busy = false;
  4640. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4641. }
  4642. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  4643. static void intel_crtc_idle_timer(unsigned long arg)
  4644. {
  4645. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  4646. struct drm_crtc *crtc = &intel_crtc->base;
  4647. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  4648. struct intel_framebuffer *intel_fb;
  4649. intel_fb = to_intel_framebuffer(crtc->fb);
  4650. if (intel_fb && intel_fb->obj->active) {
  4651. /* The framebuffer is still being accessed by the GPU. */
  4652. mod_timer(&intel_crtc->idle_timer, jiffies +
  4653. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4654. return;
  4655. }
  4656. intel_crtc->busy = false;
  4657. queue_work(dev_priv->wq, &dev_priv->idle_work);
  4658. }
  4659. static void intel_increase_pllclock(struct drm_crtc *crtc)
  4660. {
  4661. struct drm_device *dev = crtc->dev;
  4662. drm_i915_private_t *dev_priv = dev->dev_private;
  4663. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4664. int pipe = intel_crtc->pipe;
  4665. int dpll_reg = DPLL(pipe);
  4666. int dpll;
  4667. if (HAS_PCH_SPLIT(dev))
  4668. return;
  4669. if (!dev_priv->lvds_downclock_avail)
  4670. return;
  4671. dpll = I915_READ(dpll_reg);
  4672. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  4673. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  4674. assert_panel_unlocked(dev_priv, pipe);
  4675. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  4676. I915_WRITE(dpll_reg, dpll);
  4677. intel_wait_for_vblank(dev, pipe);
  4678. dpll = I915_READ(dpll_reg);
  4679. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  4680. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  4681. }
  4682. /* Schedule downclock */
  4683. mod_timer(&intel_crtc->idle_timer, jiffies +
  4684. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4685. }
  4686. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  4687. {
  4688. struct drm_device *dev = crtc->dev;
  4689. drm_i915_private_t *dev_priv = dev->dev_private;
  4690. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4691. if (HAS_PCH_SPLIT(dev))
  4692. return;
  4693. if (!dev_priv->lvds_downclock_avail)
  4694. return;
  4695. /*
  4696. * Since this is called by a timer, we should never get here in
  4697. * the manual case.
  4698. */
  4699. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  4700. int pipe = intel_crtc->pipe;
  4701. int dpll_reg = DPLL(pipe);
  4702. int dpll;
  4703. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  4704. assert_panel_unlocked(dev_priv, pipe);
  4705. dpll = I915_READ(dpll_reg);
  4706. dpll |= DISPLAY_RATE_SELECT_FPA1;
  4707. I915_WRITE(dpll_reg, dpll);
  4708. intel_wait_for_vblank(dev, pipe);
  4709. dpll = I915_READ(dpll_reg);
  4710. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  4711. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  4712. }
  4713. }
  4714. /**
  4715. * intel_idle_update - adjust clocks for idleness
  4716. * @work: work struct
  4717. *
  4718. * Either the GPU or display (or both) went idle. Check the busy status
  4719. * here and adjust the CRTC and GPU clocks as necessary.
  4720. */
  4721. static void intel_idle_update(struct work_struct *work)
  4722. {
  4723. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  4724. idle_work);
  4725. struct drm_device *dev = dev_priv->dev;
  4726. struct drm_crtc *crtc;
  4727. struct intel_crtc *intel_crtc;
  4728. if (!i915_powersave)
  4729. return;
  4730. mutex_lock(&dev->struct_mutex);
  4731. i915_update_gfx_val(dev_priv);
  4732. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4733. /* Skip inactive CRTCs */
  4734. if (!crtc->fb)
  4735. continue;
  4736. intel_crtc = to_intel_crtc(crtc);
  4737. if (!intel_crtc->busy)
  4738. intel_decrease_pllclock(crtc);
  4739. }
  4740. mutex_unlock(&dev->struct_mutex);
  4741. }
  4742. /**
  4743. * intel_mark_busy - mark the GPU and possibly the display busy
  4744. * @dev: drm device
  4745. * @obj: object we're operating on
  4746. *
  4747. * Callers can use this function to indicate that the GPU is busy processing
  4748. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  4749. * buffer), we'll also mark the display as busy, so we know to increase its
  4750. * clock frequency.
  4751. */
  4752. void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
  4753. {
  4754. drm_i915_private_t *dev_priv = dev->dev_private;
  4755. struct drm_crtc *crtc = NULL;
  4756. struct intel_framebuffer *intel_fb;
  4757. struct intel_crtc *intel_crtc;
  4758. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4759. return;
  4760. if (!dev_priv->busy) {
  4761. intel_sanitize_pm(dev);
  4762. dev_priv->busy = true;
  4763. } else
  4764. mod_timer(&dev_priv->idle_timer, jiffies +
  4765. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4766. if (obj == NULL)
  4767. return;
  4768. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4769. if (!crtc->fb)
  4770. continue;
  4771. intel_crtc = to_intel_crtc(crtc);
  4772. intel_fb = to_intel_framebuffer(crtc->fb);
  4773. if (intel_fb->obj == obj) {
  4774. if (!intel_crtc->busy) {
  4775. /* Non-busy -> busy, upclock */
  4776. intel_increase_pllclock(crtc);
  4777. intel_crtc->busy = true;
  4778. } else {
  4779. /* Busy -> busy, put off timer */
  4780. mod_timer(&intel_crtc->idle_timer, jiffies +
  4781. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4782. }
  4783. }
  4784. }
  4785. }
  4786. static void intel_crtc_destroy(struct drm_crtc *crtc)
  4787. {
  4788. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4789. struct drm_device *dev = crtc->dev;
  4790. struct intel_unpin_work *work;
  4791. unsigned long flags;
  4792. spin_lock_irqsave(&dev->event_lock, flags);
  4793. work = intel_crtc->unpin_work;
  4794. intel_crtc->unpin_work = NULL;
  4795. spin_unlock_irqrestore(&dev->event_lock, flags);
  4796. if (work) {
  4797. cancel_work_sync(&work->work);
  4798. kfree(work);
  4799. }
  4800. drm_crtc_cleanup(crtc);
  4801. kfree(intel_crtc);
  4802. }
  4803. static void intel_unpin_work_fn(struct work_struct *__work)
  4804. {
  4805. struct intel_unpin_work *work =
  4806. container_of(__work, struct intel_unpin_work, work);
  4807. mutex_lock(&work->dev->struct_mutex);
  4808. intel_unpin_fb_obj(work->old_fb_obj);
  4809. drm_gem_object_unreference(&work->pending_flip_obj->base);
  4810. drm_gem_object_unreference(&work->old_fb_obj->base);
  4811. intel_update_fbc(work->dev);
  4812. mutex_unlock(&work->dev->struct_mutex);
  4813. kfree(work);
  4814. }
  4815. static void do_intel_finish_page_flip(struct drm_device *dev,
  4816. struct drm_crtc *crtc)
  4817. {
  4818. drm_i915_private_t *dev_priv = dev->dev_private;
  4819. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4820. struct intel_unpin_work *work;
  4821. struct drm_i915_gem_object *obj;
  4822. struct drm_pending_vblank_event *e;
  4823. struct timeval tnow, tvbl;
  4824. unsigned long flags;
  4825. /* Ignore early vblank irqs */
  4826. if (intel_crtc == NULL)
  4827. return;
  4828. do_gettimeofday(&tnow);
  4829. spin_lock_irqsave(&dev->event_lock, flags);
  4830. work = intel_crtc->unpin_work;
  4831. if (work == NULL || !work->pending) {
  4832. spin_unlock_irqrestore(&dev->event_lock, flags);
  4833. return;
  4834. }
  4835. intel_crtc->unpin_work = NULL;
  4836. if (work->event) {
  4837. e = work->event;
  4838. e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
  4839. /* Called before vblank count and timestamps have
  4840. * been updated for the vblank interval of flip
  4841. * completion? Need to increment vblank count and
  4842. * add one videorefresh duration to returned timestamp
  4843. * to account for this. We assume this happened if we
  4844. * get called over 0.9 frame durations after the last
  4845. * timestamped vblank.
  4846. *
  4847. * This calculation can not be used with vrefresh rates
  4848. * below 5Hz (10Hz to be on the safe side) without
  4849. * promoting to 64 integers.
  4850. */
  4851. if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
  4852. 9 * crtc->framedur_ns) {
  4853. e->event.sequence++;
  4854. tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
  4855. crtc->framedur_ns);
  4856. }
  4857. e->event.tv_sec = tvbl.tv_sec;
  4858. e->event.tv_usec = tvbl.tv_usec;
  4859. list_add_tail(&e->base.link,
  4860. &e->base.file_priv->event_list);
  4861. wake_up_interruptible(&e->base.file_priv->event_wait);
  4862. }
  4863. drm_vblank_put(dev, intel_crtc->pipe);
  4864. spin_unlock_irqrestore(&dev->event_lock, flags);
  4865. obj = work->old_fb_obj;
  4866. atomic_clear_mask(1 << intel_crtc->plane,
  4867. &obj->pending_flip.counter);
  4868. if (atomic_read(&obj->pending_flip) == 0)
  4869. wake_up(&dev_priv->pending_flip_queue);
  4870. schedule_work(&work->work);
  4871. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  4872. }
  4873. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  4874. {
  4875. drm_i915_private_t *dev_priv = dev->dev_private;
  4876. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  4877. do_intel_finish_page_flip(dev, crtc);
  4878. }
  4879. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  4880. {
  4881. drm_i915_private_t *dev_priv = dev->dev_private;
  4882. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  4883. do_intel_finish_page_flip(dev, crtc);
  4884. }
  4885. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  4886. {
  4887. drm_i915_private_t *dev_priv = dev->dev_private;
  4888. struct intel_crtc *intel_crtc =
  4889. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  4890. unsigned long flags;
  4891. spin_lock_irqsave(&dev->event_lock, flags);
  4892. if (intel_crtc->unpin_work) {
  4893. if ((++intel_crtc->unpin_work->pending) > 1)
  4894. DRM_ERROR("Prepared flip multiple times\n");
  4895. } else {
  4896. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  4897. }
  4898. spin_unlock_irqrestore(&dev->event_lock, flags);
  4899. }
  4900. static int intel_gen2_queue_flip(struct drm_device *dev,
  4901. struct drm_crtc *crtc,
  4902. struct drm_framebuffer *fb,
  4903. struct drm_i915_gem_object *obj)
  4904. {
  4905. struct drm_i915_private *dev_priv = dev->dev_private;
  4906. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4907. unsigned long offset;
  4908. u32 flip_mask;
  4909. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  4910. int ret;
  4911. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  4912. if (ret)
  4913. goto err;
  4914. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  4915. offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
  4916. ret = intel_ring_begin(ring, 6);
  4917. if (ret)
  4918. goto err_unpin;
  4919. /* Can't queue multiple flips, so wait for the previous
  4920. * one to finish before executing the next.
  4921. */
  4922. if (intel_crtc->plane)
  4923. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  4924. else
  4925. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  4926. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  4927. intel_ring_emit(ring, MI_NOOP);
  4928. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  4929. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4930. intel_ring_emit(ring, fb->pitches[0]);
  4931. intel_ring_emit(ring, obj->gtt_offset + offset);
  4932. intel_ring_emit(ring, 0); /* aux display base address, unused */
  4933. intel_ring_advance(ring);
  4934. return 0;
  4935. err_unpin:
  4936. intel_unpin_fb_obj(obj);
  4937. err:
  4938. return ret;
  4939. }
  4940. static int intel_gen3_queue_flip(struct drm_device *dev,
  4941. struct drm_crtc *crtc,
  4942. struct drm_framebuffer *fb,
  4943. struct drm_i915_gem_object *obj)
  4944. {
  4945. struct drm_i915_private *dev_priv = dev->dev_private;
  4946. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4947. unsigned long offset;
  4948. u32 flip_mask;
  4949. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  4950. int ret;
  4951. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  4952. if (ret)
  4953. goto err;
  4954. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  4955. offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
  4956. ret = intel_ring_begin(ring, 6);
  4957. if (ret)
  4958. goto err_unpin;
  4959. if (intel_crtc->plane)
  4960. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  4961. else
  4962. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  4963. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  4964. intel_ring_emit(ring, MI_NOOP);
  4965. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  4966. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4967. intel_ring_emit(ring, fb->pitches[0]);
  4968. intel_ring_emit(ring, obj->gtt_offset + offset);
  4969. intel_ring_emit(ring, MI_NOOP);
  4970. intel_ring_advance(ring);
  4971. return 0;
  4972. err_unpin:
  4973. intel_unpin_fb_obj(obj);
  4974. err:
  4975. return ret;
  4976. }
  4977. static int intel_gen4_queue_flip(struct drm_device *dev,
  4978. struct drm_crtc *crtc,
  4979. struct drm_framebuffer *fb,
  4980. struct drm_i915_gem_object *obj)
  4981. {
  4982. struct drm_i915_private *dev_priv = dev->dev_private;
  4983. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4984. uint32_t pf, pipesrc;
  4985. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  4986. int ret;
  4987. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  4988. if (ret)
  4989. goto err;
  4990. ret = intel_ring_begin(ring, 4);
  4991. if (ret)
  4992. goto err_unpin;
  4993. /* i965+ uses the linear or tiled offsets from the
  4994. * Display Registers (which do not change across a page-flip)
  4995. * so we need only reprogram the base address.
  4996. */
  4997. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  4998. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4999. intel_ring_emit(ring, fb->pitches[0]);
  5000. intel_ring_emit(ring, obj->gtt_offset | obj->tiling_mode);
  5001. /* XXX Enabling the panel-fitter across page-flip is so far
  5002. * untested on non-native modes, so ignore it for now.
  5003. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  5004. */
  5005. pf = 0;
  5006. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  5007. intel_ring_emit(ring, pf | pipesrc);
  5008. intel_ring_advance(ring);
  5009. return 0;
  5010. err_unpin:
  5011. intel_unpin_fb_obj(obj);
  5012. err:
  5013. return ret;
  5014. }
  5015. static int intel_gen6_queue_flip(struct drm_device *dev,
  5016. struct drm_crtc *crtc,
  5017. struct drm_framebuffer *fb,
  5018. struct drm_i915_gem_object *obj)
  5019. {
  5020. struct drm_i915_private *dev_priv = dev->dev_private;
  5021. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5022. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  5023. uint32_t pf, pipesrc;
  5024. int ret;
  5025. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5026. if (ret)
  5027. goto err;
  5028. ret = intel_ring_begin(ring, 4);
  5029. if (ret)
  5030. goto err_unpin;
  5031. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  5032. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5033. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  5034. intel_ring_emit(ring, obj->gtt_offset);
  5035. /* Contrary to the suggestions in the documentation,
  5036. * "Enable Panel Fitter" does not seem to be required when page
  5037. * flipping with a non-native mode, and worse causes a normal
  5038. * modeset to fail.
  5039. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  5040. */
  5041. pf = 0;
  5042. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  5043. intel_ring_emit(ring, pf | pipesrc);
  5044. intel_ring_advance(ring);
  5045. return 0;
  5046. err_unpin:
  5047. intel_unpin_fb_obj(obj);
  5048. err:
  5049. return ret;
  5050. }
  5051. /*
  5052. * On gen7 we currently use the blit ring because (in early silicon at least)
  5053. * the render ring doesn't give us interrpts for page flip completion, which
  5054. * means clients will hang after the first flip is queued. Fortunately the
  5055. * blit ring generates interrupts properly, so use it instead.
  5056. */
  5057. static int intel_gen7_queue_flip(struct drm_device *dev,
  5058. struct drm_crtc *crtc,
  5059. struct drm_framebuffer *fb,
  5060. struct drm_i915_gem_object *obj)
  5061. {
  5062. struct drm_i915_private *dev_priv = dev->dev_private;
  5063. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5064. struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
  5065. int ret;
  5066. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5067. if (ret)
  5068. goto err;
  5069. ret = intel_ring_begin(ring, 4);
  5070. if (ret)
  5071. goto err_unpin;
  5072. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));
  5073. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  5074. intel_ring_emit(ring, (obj->gtt_offset));
  5075. intel_ring_emit(ring, (MI_NOOP));
  5076. intel_ring_advance(ring);
  5077. return 0;
  5078. err_unpin:
  5079. intel_unpin_fb_obj(obj);
  5080. err:
  5081. return ret;
  5082. }
  5083. static int intel_default_queue_flip(struct drm_device *dev,
  5084. struct drm_crtc *crtc,
  5085. struct drm_framebuffer *fb,
  5086. struct drm_i915_gem_object *obj)
  5087. {
  5088. return -ENODEV;
  5089. }
  5090. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  5091. struct drm_framebuffer *fb,
  5092. struct drm_pending_vblank_event *event)
  5093. {
  5094. struct drm_device *dev = crtc->dev;
  5095. struct drm_i915_private *dev_priv = dev->dev_private;
  5096. struct intel_framebuffer *intel_fb;
  5097. struct drm_i915_gem_object *obj;
  5098. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5099. struct intel_unpin_work *work;
  5100. unsigned long flags;
  5101. int ret;
  5102. work = kzalloc(sizeof *work, GFP_KERNEL);
  5103. if (work == NULL)
  5104. return -ENOMEM;
  5105. work->event = event;
  5106. work->dev = crtc->dev;
  5107. intel_fb = to_intel_framebuffer(crtc->fb);
  5108. work->old_fb_obj = intel_fb->obj;
  5109. INIT_WORK(&work->work, intel_unpin_work_fn);
  5110. ret = drm_vblank_get(dev, intel_crtc->pipe);
  5111. if (ret)
  5112. goto free_work;
  5113. /* We borrow the event spin lock for protecting unpin_work */
  5114. spin_lock_irqsave(&dev->event_lock, flags);
  5115. if (intel_crtc->unpin_work) {
  5116. spin_unlock_irqrestore(&dev->event_lock, flags);
  5117. kfree(work);
  5118. drm_vblank_put(dev, intel_crtc->pipe);
  5119. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  5120. return -EBUSY;
  5121. }
  5122. intel_crtc->unpin_work = work;
  5123. spin_unlock_irqrestore(&dev->event_lock, flags);
  5124. intel_fb = to_intel_framebuffer(fb);
  5125. obj = intel_fb->obj;
  5126. mutex_lock(&dev->struct_mutex);
  5127. /* Reference the objects for the scheduled work. */
  5128. drm_gem_object_reference(&work->old_fb_obj->base);
  5129. drm_gem_object_reference(&obj->base);
  5130. crtc->fb = fb;
  5131. work->pending_flip_obj = obj;
  5132. work->enable_stall_check = true;
  5133. /* Block clients from rendering to the new back buffer until
  5134. * the flip occurs and the object is no longer visible.
  5135. */
  5136. atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  5137. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
  5138. if (ret)
  5139. goto cleanup_pending;
  5140. intel_disable_fbc(dev);
  5141. intel_mark_busy(dev, obj);
  5142. mutex_unlock(&dev->struct_mutex);
  5143. trace_i915_flip_request(intel_crtc->plane, obj);
  5144. return 0;
  5145. cleanup_pending:
  5146. atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  5147. drm_gem_object_unreference(&work->old_fb_obj->base);
  5148. drm_gem_object_unreference(&obj->base);
  5149. mutex_unlock(&dev->struct_mutex);
  5150. spin_lock_irqsave(&dev->event_lock, flags);
  5151. intel_crtc->unpin_work = NULL;
  5152. spin_unlock_irqrestore(&dev->event_lock, flags);
  5153. drm_vblank_put(dev, intel_crtc->pipe);
  5154. free_work:
  5155. kfree(work);
  5156. return ret;
  5157. }
  5158. static void intel_sanitize_modesetting(struct drm_device *dev,
  5159. int pipe, int plane)
  5160. {
  5161. struct drm_i915_private *dev_priv = dev->dev_private;
  5162. u32 reg, val;
  5163. /* Clear any frame start delays used for debugging left by the BIOS */
  5164. for_each_pipe(pipe) {
  5165. reg = PIPECONF(pipe);
  5166. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  5167. }
  5168. if (HAS_PCH_SPLIT(dev))
  5169. return;
  5170. /* Who knows what state these registers were left in by the BIOS or
  5171. * grub?
  5172. *
  5173. * If we leave the registers in a conflicting state (e.g. with the
  5174. * display plane reading from the other pipe than the one we intend
  5175. * to use) then when we attempt to teardown the active mode, we will
  5176. * not disable the pipes and planes in the correct order -- leaving
  5177. * a plane reading from a disabled pipe and possibly leading to
  5178. * undefined behaviour.
  5179. */
  5180. reg = DSPCNTR(plane);
  5181. val = I915_READ(reg);
  5182. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  5183. return;
  5184. if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
  5185. return;
  5186. /* This display plane is active and attached to the other CPU pipe. */
  5187. pipe = !pipe;
  5188. /* Disable the plane and wait for it to stop reading from the pipe. */
  5189. intel_disable_plane(dev_priv, plane, pipe);
  5190. intel_disable_pipe(dev_priv, pipe);
  5191. }
  5192. static void intel_crtc_reset(struct drm_crtc *crtc)
  5193. {
  5194. struct drm_device *dev = crtc->dev;
  5195. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5196. /* Reset flags back to the 'unknown' status so that they
  5197. * will be correctly set on the initial modeset.
  5198. */
  5199. intel_crtc->dpms_mode = -1;
  5200. /* We need to fix up any BIOS configuration that conflicts with
  5201. * our expectations.
  5202. */
  5203. intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
  5204. }
  5205. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  5206. .dpms = intel_crtc_dpms,
  5207. .mode_fixup = intel_crtc_mode_fixup,
  5208. .mode_set = intel_crtc_mode_set,
  5209. .mode_set_base = intel_pipe_set_base,
  5210. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  5211. .load_lut = intel_crtc_load_lut,
  5212. .disable = intel_crtc_disable,
  5213. };
  5214. static const struct drm_crtc_funcs intel_crtc_funcs = {
  5215. .reset = intel_crtc_reset,
  5216. .cursor_set = intel_crtc_cursor_set,
  5217. .cursor_move = intel_crtc_cursor_move,
  5218. .gamma_set = intel_crtc_gamma_set,
  5219. .set_config = drm_crtc_helper_set_config,
  5220. .destroy = intel_crtc_destroy,
  5221. .page_flip = intel_crtc_page_flip,
  5222. };
  5223. static void intel_pch_pll_init(struct drm_device *dev)
  5224. {
  5225. drm_i915_private_t *dev_priv = dev->dev_private;
  5226. int i;
  5227. if (dev_priv->num_pch_pll == 0) {
  5228. DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
  5229. return;
  5230. }
  5231. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  5232. dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
  5233. dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
  5234. dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
  5235. }
  5236. }
  5237. static void intel_crtc_init(struct drm_device *dev, int pipe)
  5238. {
  5239. drm_i915_private_t *dev_priv = dev->dev_private;
  5240. struct intel_crtc *intel_crtc;
  5241. int i;
  5242. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  5243. if (intel_crtc == NULL)
  5244. return;
  5245. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  5246. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  5247. for (i = 0; i < 256; i++) {
  5248. intel_crtc->lut_r[i] = i;
  5249. intel_crtc->lut_g[i] = i;
  5250. intel_crtc->lut_b[i] = i;
  5251. }
  5252. /* Swap pipes & planes for FBC on pre-965 */
  5253. intel_crtc->pipe = pipe;
  5254. intel_crtc->plane = pipe;
  5255. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  5256. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  5257. intel_crtc->plane = !pipe;
  5258. }
  5259. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  5260. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  5261. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  5262. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  5263. intel_crtc_reset(&intel_crtc->base);
  5264. intel_crtc->active = true; /* force the pipe off on setup_init_config */
  5265. intel_crtc->bpp = 24; /* default for pre-Ironlake */
  5266. if (HAS_PCH_SPLIT(dev)) {
  5267. intel_helper_funcs.prepare = ironlake_crtc_prepare;
  5268. intel_helper_funcs.commit = ironlake_crtc_commit;
  5269. } else {
  5270. intel_helper_funcs.prepare = i9xx_crtc_prepare;
  5271. intel_helper_funcs.commit = i9xx_crtc_commit;
  5272. }
  5273. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  5274. intel_crtc->busy = false;
  5275. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  5276. (unsigned long)intel_crtc);
  5277. }
  5278. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  5279. struct drm_file *file)
  5280. {
  5281. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  5282. struct drm_mode_object *drmmode_obj;
  5283. struct intel_crtc *crtc;
  5284. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  5285. return -ENODEV;
  5286. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  5287. DRM_MODE_OBJECT_CRTC);
  5288. if (!drmmode_obj) {
  5289. DRM_ERROR("no such CRTC id\n");
  5290. return -EINVAL;
  5291. }
  5292. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  5293. pipe_from_crtc_id->pipe = crtc->pipe;
  5294. return 0;
  5295. }
  5296. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  5297. {
  5298. struct intel_encoder *encoder;
  5299. int index_mask = 0;
  5300. int entry = 0;
  5301. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  5302. if (type_mask & encoder->clone_mask)
  5303. index_mask |= (1 << entry);
  5304. entry++;
  5305. }
  5306. return index_mask;
  5307. }
  5308. static bool has_edp_a(struct drm_device *dev)
  5309. {
  5310. struct drm_i915_private *dev_priv = dev->dev_private;
  5311. if (!IS_MOBILE(dev))
  5312. return false;
  5313. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  5314. return false;
  5315. if (IS_GEN5(dev) &&
  5316. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  5317. return false;
  5318. return true;
  5319. }
  5320. static void intel_setup_outputs(struct drm_device *dev)
  5321. {
  5322. struct drm_i915_private *dev_priv = dev->dev_private;
  5323. struct intel_encoder *encoder;
  5324. bool dpd_is_edp = false;
  5325. bool has_lvds;
  5326. has_lvds = intel_lvds_init(dev);
  5327. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  5328. /* disable the panel fitter on everything but LVDS */
  5329. I915_WRITE(PFIT_CONTROL, 0);
  5330. }
  5331. if (HAS_PCH_SPLIT(dev)) {
  5332. dpd_is_edp = intel_dpd_is_edp(dev);
  5333. if (has_edp_a(dev))
  5334. intel_dp_init(dev, DP_A);
  5335. if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  5336. intel_dp_init(dev, PCH_DP_D);
  5337. }
  5338. intel_crt_init(dev);
  5339. if (HAS_PCH_SPLIT(dev)) {
  5340. int found;
  5341. if (I915_READ(HDMIB) & PORT_DETECTED) {
  5342. /* PCH SDVOB multiplex with HDMIB */
  5343. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  5344. if (!found)
  5345. intel_hdmi_init(dev, HDMIB);
  5346. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  5347. intel_dp_init(dev, PCH_DP_B);
  5348. }
  5349. if (I915_READ(HDMIC) & PORT_DETECTED)
  5350. intel_hdmi_init(dev, HDMIC);
  5351. if (I915_READ(HDMID) & PORT_DETECTED)
  5352. intel_hdmi_init(dev, HDMID);
  5353. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  5354. intel_dp_init(dev, PCH_DP_C);
  5355. if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  5356. intel_dp_init(dev, PCH_DP_D);
  5357. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  5358. bool found = false;
  5359. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  5360. DRM_DEBUG_KMS("probing SDVOB\n");
  5361. found = intel_sdvo_init(dev, SDVOB, true);
  5362. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  5363. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  5364. intel_hdmi_init(dev, SDVOB);
  5365. }
  5366. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  5367. DRM_DEBUG_KMS("probing DP_B\n");
  5368. intel_dp_init(dev, DP_B);
  5369. }
  5370. }
  5371. /* Before G4X SDVOC doesn't have its own detect register */
  5372. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  5373. DRM_DEBUG_KMS("probing SDVOC\n");
  5374. found = intel_sdvo_init(dev, SDVOC, false);
  5375. }
  5376. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  5377. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  5378. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  5379. intel_hdmi_init(dev, SDVOC);
  5380. }
  5381. if (SUPPORTS_INTEGRATED_DP(dev)) {
  5382. DRM_DEBUG_KMS("probing DP_C\n");
  5383. intel_dp_init(dev, DP_C);
  5384. }
  5385. }
  5386. if (SUPPORTS_INTEGRATED_DP(dev) &&
  5387. (I915_READ(DP_D) & DP_DETECTED)) {
  5388. DRM_DEBUG_KMS("probing DP_D\n");
  5389. intel_dp_init(dev, DP_D);
  5390. }
  5391. } else if (IS_GEN2(dev))
  5392. intel_dvo_init(dev);
  5393. if (SUPPORTS_TV(dev))
  5394. intel_tv_init(dev);
  5395. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  5396. encoder->base.possible_crtcs = encoder->crtc_mask;
  5397. encoder->base.possible_clones =
  5398. intel_encoder_clones(dev, encoder->clone_mask);
  5399. }
  5400. /* disable all the possible outputs/crtcs before entering KMS mode */
  5401. drm_helper_disable_unused_functions(dev);
  5402. if (HAS_PCH_SPLIT(dev))
  5403. ironlake_init_pch_refclk(dev);
  5404. }
  5405. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  5406. {
  5407. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  5408. drm_framebuffer_cleanup(fb);
  5409. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  5410. kfree(intel_fb);
  5411. }
  5412. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  5413. struct drm_file *file,
  5414. unsigned int *handle)
  5415. {
  5416. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  5417. struct drm_i915_gem_object *obj = intel_fb->obj;
  5418. return drm_gem_handle_create(file, &obj->base, handle);
  5419. }
  5420. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  5421. .destroy = intel_user_framebuffer_destroy,
  5422. .create_handle = intel_user_framebuffer_create_handle,
  5423. };
  5424. int intel_framebuffer_init(struct drm_device *dev,
  5425. struct intel_framebuffer *intel_fb,
  5426. struct drm_mode_fb_cmd2 *mode_cmd,
  5427. struct drm_i915_gem_object *obj)
  5428. {
  5429. int ret;
  5430. if (obj->tiling_mode == I915_TILING_Y)
  5431. return -EINVAL;
  5432. if (mode_cmd->pitches[0] & 63)
  5433. return -EINVAL;
  5434. switch (mode_cmd->pixel_format) {
  5435. case DRM_FORMAT_RGB332:
  5436. case DRM_FORMAT_RGB565:
  5437. case DRM_FORMAT_XRGB8888:
  5438. case DRM_FORMAT_XBGR8888:
  5439. case DRM_FORMAT_ARGB8888:
  5440. case DRM_FORMAT_XRGB2101010:
  5441. case DRM_FORMAT_ARGB2101010:
  5442. /* RGB formats are common across chipsets */
  5443. break;
  5444. case DRM_FORMAT_YUYV:
  5445. case DRM_FORMAT_UYVY:
  5446. case DRM_FORMAT_YVYU:
  5447. case DRM_FORMAT_VYUY:
  5448. break;
  5449. default:
  5450. DRM_DEBUG_KMS("unsupported pixel format %u\n",
  5451. mode_cmd->pixel_format);
  5452. return -EINVAL;
  5453. }
  5454. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  5455. if (ret) {
  5456. DRM_ERROR("framebuffer init failed %d\n", ret);
  5457. return ret;
  5458. }
  5459. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  5460. intel_fb->obj = obj;
  5461. return 0;
  5462. }
  5463. static struct drm_framebuffer *
  5464. intel_user_framebuffer_create(struct drm_device *dev,
  5465. struct drm_file *filp,
  5466. struct drm_mode_fb_cmd2 *mode_cmd)
  5467. {
  5468. struct drm_i915_gem_object *obj;
  5469. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  5470. mode_cmd->handles[0]));
  5471. if (&obj->base == NULL)
  5472. return ERR_PTR(-ENOENT);
  5473. return intel_framebuffer_create(dev, mode_cmd, obj);
  5474. }
  5475. static const struct drm_mode_config_funcs intel_mode_funcs = {
  5476. .fb_create = intel_user_framebuffer_create,
  5477. .output_poll_changed = intel_fb_output_poll_changed,
  5478. };
  5479. /* Set up chip specific display functions */
  5480. static void intel_init_display(struct drm_device *dev)
  5481. {
  5482. struct drm_i915_private *dev_priv = dev->dev_private;
  5483. /* We always want a DPMS function */
  5484. if (HAS_PCH_SPLIT(dev)) {
  5485. dev_priv->display.dpms = ironlake_crtc_dpms;
  5486. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  5487. dev_priv->display.off = ironlake_crtc_off;
  5488. dev_priv->display.update_plane = ironlake_update_plane;
  5489. } else {
  5490. dev_priv->display.dpms = i9xx_crtc_dpms;
  5491. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  5492. dev_priv->display.off = i9xx_crtc_off;
  5493. dev_priv->display.update_plane = i9xx_update_plane;
  5494. }
  5495. /* Returns the core display clock speed */
  5496. if (IS_VALLEYVIEW(dev))
  5497. dev_priv->display.get_display_clock_speed =
  5498. valleyview_get_display_clock_speed;
  5499. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  5500. dev_priv->display.get_display_clock_speed =
  5501. i945_get_display_clock_speed;
  5502. else if (IS_I915G(dev))
  5503. dev_priv->display.get_display_clock_speed =
  5504. i915_get_display_clock_speed;
  5505. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  5506. dev_priv->display.get_display_clock_speed =
  5507. i9xx_misc_get_display_clock_speed;
  5508. else if (IS_I915GM(dev))
  5509. dev_priv->display.get_display_clock_speed =
  5510. i915gm_get_display_clock_speed;
  5511. else if (IS_I865G(dev))
  5512. dev_priv->display.get_display_clock_speed =
  5513. i865_get_display_clock_speed;
  5514. else if (IS_I85X(dev))
  5515. dev_priv->display.get_display_clock_speed =
  5516. i855_get_display_clock_speed;
  5517. else /* 852, 830 */
  5518. dev_priv->display.get_display_clock_speed =
  5519. i830_get_display_clock_speed;
  5520. if (HAS_PCH_SPLIT(dev)) {
  5521. if (IS_GEN5(dev)) {
  5522. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  5523. dev_priv->display.write_eld = ironlake_write_eld;
  5524. } else if (IS_GEN6(dev)) {
  5525. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  5526. dev_priv->display.write_eld = ironlake_write_eld;
  5527. } else if (IS_IVYBRIDGE(dev)) {
  5528. /* FIXME: detect B0+ stepping and use auto training */
  5529. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  5530. dev_priv->display.write_eld = ironlake_write_eld;
  5531. } else
  5532. dev_priv->display.update_wm = NULL;
  5533. } else if (IS_VALLEYVIEW(dev)) {
  5534. dev_priv->display.force_wake_get = vlv_force_wake_get;
  5535. dev_priv->display.force_wake_put = vlv_force_wake_put;
  5536. } else if (IS_G4X(dev)) {
  5537. dev_priv->display.write_eld = g4x_write_eld;
  5538. }
  5539. /* Default just returns -ENODEV to indicate unsupported */
  5540. dev_priv->display.queue_flip = intel_default_queue_flip;
  5541. switch (INTEL_INFO(dev)->gen) {
  5542. case 2:
  5543. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  5544. break;
  5545. case 3:
  5546. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  5547. break;
  5548. case 4:
  5549. case 5:
  5550. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  5551. break;
  5552. case 6:
  5553. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  5554. break;
  5555. case 7:
  5556. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  5557. break;
  5558. }
  5559. }
  5560. /*
  5561. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  5562. * resume, or other times. This quirk makes sure that's the case for
  5563. * affected systems.
  5564. */
  5565. static void quirk_pipea_force(struct drm_device *dev)
  5566. {
  5567. struct drm_i915_private *dev_priv = dev->dev_private;
  5568. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  5569. DRM_INFO("applying pipe a force quirk\n");
  5570. }
  5571. /*
  5572. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  5573. */
  5574. static void quirk_ssc_force_disable(struct drm_device *dev)
  5575. {
  5576. struct drm_i915_private *dev_priv = dev->dev_private;
  5577. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  5578. DRM_INFO("applying lvds SSC disable quirk\n");
  5579. }
  5580. /*
  5581. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  5582. * brightness value
  5583. */
  5584. static void quirk_invert_brightness(struct drm_device *dev)
  5585. {
  5586. struct drm_i915_private *dev_priv = dev->dev_private;
  5587. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  5588. DRM_INFO("applying inverted panel brightness quirk\n");
  5589. }
  5590. struct intel_quirk {
  5591. int device;
  5592. int subsystem_vendor;
  5593. int subsystem_device;
  5594. void (*hook)(struct drm_device *dev);
  5595. };
  5596. static struct intel_quirk intel_quirks[] = {
  5597. /* HP Mini needs pipe A force quirk (LP: #322104) */
  5598. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  5599. /* Thinkpad R31 needs pipe A force quirk */
  5600. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  5601. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  5602. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  5603. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  5604. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  5605. /* ThinkPad X40 needs pipe A force quirk */
  5606. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  5607. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  5608. /* 855 & before need to leave pipe A & dpll A up */
  5609. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5610. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  5611. /* Lenovo U160 cannot use SSC on LVDS */
  5612. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  5613. /* Sony Vaio Y cannot use SSC on LVDS */
  5614. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  5615. /* Acer Aspire 5734Z must invert backlight brightness */
  5616. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  5617. };
  5618. static void intel_init_quirks(struct drm_device *dev)
  5619. {
  5620. struct pci_dev *d = dev->pdev;
  5621. int i;
  5622. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  5623. struct intel_quirk *q = &intel_quirks[i];
  5624. if (d->device == q->device &&
  5625. (d->subsystem_vendor == q->subsystem_vendor ||
  5626. q->subsystem_vendor == PCI_ANY_ID) &&
  5627. (d->subsystem_device == q->subsystem_device ||
  5628. q->subsystem_device == PCI_ANY_ID))
  5629. q->hook(dev);
  5630. }
  5631. }
  5632. /* Disable the VGA plane that we never use */
  5633. static void i915_disable_vga(struct drm_device *dev)
  5634. {
  5635. struct drm_i915_private *dev_priv = dev->dev_private;
  5636. u8 sr1;
  5637. u32 vga_reg;
  5638. if (HAS_PCH_SPLIT(dev))
  5639. vga_reg = CPU_VGACNTRL;
  5640. else
  5641. vga_reg = VGACNTRL;
  5642. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  5643. outb(SR01, VGA_SR_INDEX);
  5644. sr1 = inb(VGA_SR_DATA);
  5645. outb(sr1 | 1<<5, VGA_SR_DATA);
  5646. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  5647. udelay(300);
  5648. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  5649. POSTING_READ(vga_reg);
  5650. }
  5651. static void ivb_pch_pwm_override(struct drm_device *dev)
  5652. {
  5653. struct drm_i915_private *dev_priv = dev->dev_private;
  5654. /*
  5655. * IVB has CPU eDP backlight regs too, set things up to let the
  5656. * PCH regs control the backlight
  5657. */
  5658. I915_WRITE(BLC_PWM_CPU_CTL2, PWM_ENABLE);
  5659. I915_WRITE(BLC_PWM_CPU_CTL, 0);
  5660. I915_WRITE(BLC_PWM_PCH_CTL1, PWM_ENABLE | (1<<30));
  5661. }
  5662. void intel_modeset_init_hw(struct drm_device *dev)
  5663. {
  5664. struct drm_i915_private *dev_priv = dev->dev_private;
  5665. intel_init_clock_gating(dev);
  5666. if (IS_IRONLAKE_M(dev)) {
  5667. ironlake_enable_drps(dev);
  5668. intel_init_emon(dev);
  5669. }
  5670. if ((IS_GEN6(dev) || IS_GEN7(dev)) && !IS_VALLEYVIEW(dev)) {
  5671. gen6_enable_rps(dev_priv);
  5672. gen6_update_ring_freq(dev_priv);
  5673. }
  5674. if (IS_IVYBRIDGE(dev))
  5675. ivb_pch_pwm_override(dev);
  5676. }
  5677. void intel_modeset_init(struct drm_device *dev)
  5678. {
  5679. struct drm_i915_private *dev_priv = dev->dev_private;
  5680. int i, ret;
  5681. drm_mode_config_init(dev);
  5682. dev->mode_config.min_width = 0;
  5683. dev->mode_config.min_height = 0;
  5684. dev->mode_config.preferred_depth = 24;
  5685. dev->mode_config.prefer_shadow = 1;
  5686. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  5687. intel_init_quirks(dev);
  5688. intel_init_pm(dev);
  5689. intel_init_display(dev);
  5690. if (IS_GEN2(dev)) {
  5691. dev->mode_config.max_width = 2048;
  5692. dev->mode_config.max_height = 2048;
  5693. } else if (IS_GEN3(dev)) {
  5694. dev->mode_config.max_width = 4096;
  5695. dev->mode_config.max_height = 4096;
  5696. } else {
  5697. dev->mode_config.max_width = 8192;
  5698. dev->mode_config.max_height = 8192;
  5699. }
  5700. dev->mode_config.fb_base = dev->agp->base;
  5701. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  5702. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  5703. for (i = 0; i < dev_priv->num_pipe; i++) {
  5704. intel_crtc_init(dev, i);
  5705. ret = intel_plane_init(dev, i);
  5706. if (ret)
  5707. DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
  5708. }
  5709. intel_pch_pll_init(dev);
  5710. /* Just disable it once at startup */
  5711. i915_disable_vga(dev);
  5712. intel_setup_outputs(dev);
  5713. intel_modeset_init_hw(dev);
  5714. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  5715. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  5716. (unsigned long)dev);
  5717. }
  5718. void intel_modeset_gem_init(struct drm_device *dev)
  5719. {
  5720. if (IS_IRONLAKE_M(dev))
  5721. ironlake_enable_rc6(dev);
  5722. intel_setup_overlay(dev);
  5723. }
  5724. void intel_modeset_cleanup(struct drm_device *dev)
  5725. {
  5726. struct drm_i915_private *dev_priv = dev->dev_private;
  5727. struct drm_crtc *crtc;
  5728. struct intel_crtc *intel_crtc;
  5729. drm_kms_helper_poll_fini(dev);
  5730. mutex_lock(&dev->struct_mutex);
  5731. intel_unregister_dsm_handler();
  5732. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5733. /* Skip inactive CRTCs */
  5734. if (!crtc->fb)
  5735. continue;
  5736. intel_crtc = to_intel_crtc(crtc);
  5737. intel_increase_pllclock(crtc);
  5738. }
  5739. intel_disable_fbc(dev);
  5740. if (IS_IRONLAKE_M(dev))
  5741. ironlake_disable_drps(dev);
  5742. if ((IS_GEN6(dev) || IS_GEN7(dev)) && !IS_VALLEYVIEW(dev))
  5743. gen6_disable_rps(dev);
  5744. if (IS_IRONLAKE_M(dev))
  5745. ironlake_disable_rc6(dev);
  5746. if (IS_VALLEYVIEW(dev))
  5747. vlv_init_dpio(dev);
  5748. mutex_unlock(&dev->struct_mutex);
  5749. /* Disable the irq before mode object teardown, for the irq might
  5750. * enqueue unpin/hotplug work. */
  5751. drm_irq_uninstall(dev);
  5752. cancel_work_sync(&dev_priv->hotplug_work);
  5753. cancel_work_sync(&dev_priv->rps_work);
  5754. /* flush any delayed tasks or pending work */
  5755. flush_scheduled_work();
  5756. /* Shut off idle work before the crtcs get freed. */
  5757. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5758. intel_crtc = to_intel_crtc(crtc);
  5759. del_timer_sync(&intel_crtc->idle_timer);
  5760. }
  5761. del_timer_sync(&dev_priv->idle_timer);
  5762. cancel_work_sync(&dev_priv->idle_work);
  5763. drm_mode_config_cleanup(dev);
  5764. }
  5765. /*
  5766. * Return which encoder is currently attached for connector.
  5767. */
  5768. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  5769. {
  5770. return &intel_attached_encoder(connector)->base;
  5771. }
  5772. void intel_connector_attach_encoder(struct intel_connector *connector,
  5773. struct intel_encoder *encoder)
  5774. {
  5775. connector->encoder = encoder;
  5776. drm_mode_connector_attach_encoder(&connector->base,
  5777. &encoder->base);
  5778. }
  5779. /*
  5780. * set vga decode state - true == enable VGA decode
  5781. */
  5782. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  5783. {
  5784. struct drm_i915_private *dev_priv = dev->dev_private;
  5785. u16 gmch_ctrl;
  5786. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  5787. if (state)
  5788. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  5789. else
  5790. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  5791. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  5792. return 0;
  5793. }
  5794. #ifdef CONFIG_DEBUG_FS
  5795. #include <linux/seq_file.h>
  5796. struct intel_display_error_state {
  5797. struct intel_cursor_error_state {
  5798. u32 control;
  5799. u32 position;
  5800. u32 base;
  5801. u32 size;
  5802. } cursor[2];
  5803. struct intel_pipe_error_state {
  5804. u32 conf;
  5805. u32 source;
  5806. u32 htotal;
  5807. u32 hblank;
  5808. u32 hsync;
  5809. u32 vtotal;
  5810. u32 vblank;
  5811. u32 vsync;
  5812. } pipe[2];
  5813. struct intel_plane_error_state {
  5814. u32 control;
  5815. u32 stride;
  5816. u32 size;
  5817. u32 pos;
  5818. u32 addr;
  5819. u32 surface;
  5820. u32 tile_offset;
  5821. } plane[2];
  5822. };
  5823. struct intel_display_error_state *
  5824. intel_display_capture_error_state(struct drm_device *dev)
  5825. {
  5826. drm_i915_private_t *dev_priv = dev->dev_private;
  5827. struct intel_display_error_state *error;
  5828. int i;
  5829. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  5830. if (error == NULL)
  5831. return NULL;
  5832. for (i = 0; i < 2; i++) {
  5833. error->cursor[i].control = I915_READ(CURCNTR(i));
  5834. error->cursor[i].position = I915_READ(CURPOS(i));
  5835. error->cursor[i].base = I915_READ(CURBASE(i));
  5836. error->plane[i].control = I915_READ(DSPCNTR(i));
  5837. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  5838. error->plane[i].size = I915_READ(DSPSIZE(i));
  5839. error->plane[i].pos = I915_READ(DSPPOS(i));
  5840. error->plane[i].addr = I915_READ(DSPADDR(i));
  5841. if (INTEL_INFO(dev)->gen >= 4) {
  5842. error->plane[i].surface = I915_READ(DSPSURF(i));
  5843. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  5844. }
  5845. error->pipe[i].conf = I915_READ(PIPECONF(i));
  5846. error->pipe[i].source = I915_READ(PIPESRC(i));
  5847. error->pipe[i].htotal = I915_READ(HTOTAL(i));
  5848. error->pipe[i].hblank = I915_READ(HBLANK(i));
  5849. error->pipe[i].hsync = I915_READ(HSYNC(i));
  5850. error->pipe[i].vtotal = I915_READ(VTOTAL(i));
  5851. error->pipe[i].vblank = I915_READ(VBLANK(i));
  5852. error->pipe[i].vsync = I915_READ(VSYNC(i));
  5853. }
  5854. return error;
  5855. }
  5856. void
  5857. intel_display_print_error_state(struct seq_file *m,
  5858. struct drm_device *dev,
  5859. struct intel_display_error_state *error)
  5860. {
  5861. int i;
  5862. for (i = 0; i < 2; i++) {
  5863. seq_printf(m, "Pipe [%d]:\n", i);
  5864. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  5865. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  5866. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  5867. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  5868. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  5869. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  5870. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  5871. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  5872. seq_printf(m, "Plane [%d]:\n", i);
  5873. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  5874. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  5875. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  5876. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  5877. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  5878. if (INTEL_INFO(dev)->gen >= 4) {
  5879. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  5880. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  5881. }
  5882. seq_printf(m, "Cursor [%d]:\n", i);
  5883. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  5884. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  5885. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  5886. }
  5887. }
  5888. #endif