intel_display.c 345 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539
  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 <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_dp_helper.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include <linux/dma_remapping.h>
  42. #define DIV_ROUND_CLOSEST_ULL(ll, d) \
  43. ({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
  44. static void intel_increase_pllclock(struct drm_crtc *crtc);
  45. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  46. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  47. struct intel_crtc_config *pipe_config);
  48. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  49. struct intel_crtc_config *pipe_config);
  50. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  51. int x, int y, struct drm_framebuffer *old_fb);
  52. static int intel_framebuffer_init(struct drm_device *dev,
  53. struct intel_framebuffer *ifb,
  54. struct drm_mode_fb_cmd2 *mode_cmd,
  55. struct drm_i915_gem_object *obj);
  56. static void intel_dp_set_m_n(struct intel_crtc *crtc);
  57. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  58. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  59. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  60. struct intel_link_m_n *m_n);
  61. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  62. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  63. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  64. static void vlv_prepare_pll(struct intel_crtc *crtc);
  65. typedef struct {
  66. int min, max;
  67. } intel_range_t;
  68. typedef struct {
  69. int dot_limit;
  70. int p2_slow, p2_fast;
  71. } intel_p2_t;
  72. typedef struct intel_limit intel_limit_t;
  73. struct intel_limit {
  74. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  75. intel_p2_t p2;
  76. };
  77. int
  78. intel_pch_rawclk(struct drm_device *dev)
  79. {
  80. struct drm_i915_private *dev_priv = dev->dev_private;
  81. WARN_ON(!HAS_PCH_SPLIT(dev));
  82. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  83. }
  84. static inline u32 /* units of 100MHz */
  85. intel_fdi_link_freq(struct drm_device *dev)
  86. {
  87. if (IS_GEN5(dev)) {
  88. struct drm_i915_private *dev_priv = dev->dev_private;
  89. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  90. } else
  91. return 27;
  92. }
  93. static const intel_limit_t intel_limits_i8xx_dac = {
  94. .dot = { .min = 25000, .max = 350000 },
  95. .vco = { .min = 908000, .max = 1512000 },
  96. .n = { .min = 2, .max = 16 },
  97. .m = { .min = 96, .max = 140 },
  98. .m1 = { .min = 18, .max = 26 },
  99. .m2 = { .min = 6, .max = 16 },
  100. .p = { .min = 4, .max = 128 },
  101. .p1 = { .min = 2, .max = 33 },
  102. .p2 = { .dot_limit = 165000,
  103. .p2_slow = 4, .p2_fast = 2 },
  104. };
  105. static const intel_limit_t intel_limits_i8xx_dvo = {
  106. .dot = { .min = 25000, .max = 350000 },
  107. .vco = { .min = 908000, .max = 1512000 },
  108. .n = { .min = 2, .max = 16 },
  109. .m = { .min = 96, .max = 140 },
  110. .m1 = { .min = 18, .max = 26 },
  111. .m2 = { .min = 6, .max = 16 },
  112. .p = { .min = 4, .max = 128 },
  113. .p1 = { .min = 2, .max = 33 },
  114. .p2 = { .dot_limit = 165000,
  115. .p2_slow = 4, .p2_fast = 4 },
  116. };
  117. static const intel_limit_t intel_limits_i8xx_lvds = {
  118. .dot = { .min = 25000, .max = 350000 },
  119. .vco = { .min = 908000, .max = 1512000 },
  120. .n = { .min = 2, .max = 16 },
  121. .m = { .min = 96, .max = 140 },
  122. .m1 = { .min = 18, .max = 26 },
  123. .m2 = { .min = 6, .max = 16 },
  124. .p = { .min = 4, .max = 128 },
  125. .p1 = { .min = 1, .max = 6 },
  126. .p2 = { .dot_limit = 165000,
  127. .p2_slow = 14, .p2_fast = 7 },
  128. };
  129. static const intel_limit_t intel_limits_i9xx_sdvo = {
  130. .dot = { .min = 20000, .max = 400000 },
  131. .vco = { .min = 1400000, .max = 2800000 },
  132. .n = { .min = 1, .max = 6 },
  133. .m = { .min = 70, .max = 120 },
  134. .m1 = { .min = 8, .max = 18 },
  135. .m2 = { .min = 3, .max = 7 },
  136. .p = { .min = 5, .max = 80 },
  137. .p1 = { .min = 1, .max = 8 },
  138. .p2 = { .dot_limit = 200000,
  139. .p2_slow = 10, .p2_fast = 5 },
  140. };
  141. static const intel_limit_t intel_limits_i9xx_lvds = {
  142. .dot = { .min = 20000, .max = 400000 },
  143. .vco = { .min = 1400000, .max = 2800000 },
  144. .n = { .min = 1, .max = 6 },
  145. .m = { .min = 70, .max = 120 },
  146. .m1 = { .min = 8, .max = 18 },
  147. .m2 = { .min = 3, .max = 7 },
  148. .p = { .min = 7, .max = 98 },
  149. .p1 = { .min = 1, .max = 8 },
  150. .p2 = { .dot_limit = 112000,
  151. .p2_slow = 14, .p2_fast = 7 },
  152. };
  153. static const intel_limit_t intel_limits_g4x_sdvo = {
  154. .dot = { .min = 25000, .max = 270000 },
  155. .vco = { .min = 1750000, .max = 3500000},
  156. .n = { .min = 1, .max = 4 },
  157. .m = { .min = 104, .max = 138 },
  158. .m1 = { .min = 17, .max = 23 },
  159. .m2 = { .min = 5, .max = 11 },
  160. .p = { .min = 10, .max = 30 },
  161. .p1 = { .min = 1, .max = 3},
  162. .p2 = { .dot_limit = 270000,
  163. .p2_slow = 10,
  164. .p2_fast = 10
  165. },
  166. };
  167. static const intel_limit_t intel_limits_g4x_hdmi = {
  168. .dot = { .min = 22000, .max = 400000 },
  169. .vco = { .min = 1750000, .max = 3500000},
  170. .n = { .min = 1, .max = 4 },
  171. .m = { .min = 104, .max = 138 },
  172. .m1 = { .min = 16, .max = 23 },
  173. .m2 = { .min = 5, .max = 11 },
  174. .p = { .min = 5, .max = 80 },
  175. .p1 = { .min = 1, .max = 8},
  176. .p2 = { .dot_limit = 165000,
  177. .p2_slow = 10, .p2_fast = 5 },
  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. };
  192. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  193. .dot = { .min = 80000, .max = 224000 },
  194. .vco = { .min = 1750000, .max = 3500000 },
  195. .n = { .min = 1, .max = 3 },
  196. .m = { .min = 104, .max = 138 },
  197. .m1 = { .min = 17, .max = 23 },
  198. .m2 = { .min = 5, .max = 11 },
  199. .p = { .min = 14, .max = 42 },
  200. .p1 = { .min = 2, .max = 6 },
  201. .p2 = { .dot_limit = 0,
  202. .p2_slow = 7, .p2_fast = 7
  203. },
  204. };
  205. static const intel_limit_t intel_limits_pineview_sdvo = {
  206. .dot = { .min = 20000, .max = 400000},
  207. .vco = { .min = 1700000, .max = 3500000 },
  208. /* Pineview's Ncounter is a ring counter */
  209. .n = { .min = 3, .max = 6 },
  210. .m = { .min = 2, .max = 256 },
  211. /* Pineview only has one combined m divider, which we treat as m2. */
  212. .m1 = { .min = 0, .max = 0 },
  213. .m2 = { .min = 0, .max = 254 },
  214. .p = { .min = 5, .max = 80 },
  215. .p1 = { .min = 1, .max = 8 },
  216. .p2 = { .dot_limit = 200000,
  217. .p2_slow = 10, .p2_fast = 5 },
  218. };
  219. static const intel_limit_t intel_limits_pineview_lvds = {
  220. .dot = { .min = 20000, .max = 400000 },
  221. .vco = { .min = 1700000, .max = 3500000 },
  222. .n = { .min = 3, .max = 6 },
  223. .m = { .min = 2, .max = 256 },
  224. .m1 = { .min = 0, .max = 0 },
  225. .m2 = { .min = 0, .max = 254 },
  226. .p = { .min = 7, .max = 112 },
  227. .p1 = { .min = 1, .max = 8 },
  228. .p2 = { .dot_limit = 112000,
  229. .p2_slow = 14, .p2_fast = 14 },
  230. };
  231. /* Ironlake / Sandybridge
  232. *
  233. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  234. * the range value for them is (actual_value - 2).
  235. */
  236. static const intel_limit_t intel_limits_ironlake_dac = {
  237. .dot = { .min = 25000, .max = 350000 },
  238. .vco = { .min = 1760000, .max = 3510000 },
  239. .n = { .min = 1, .max = 5 },
  240. .m = { .min = 79, .max = 127 },
  241. .m1 = { .min = 12, .max = 22 },
  242. .m2 = { .min = 5, .max = 9 },
  243. .p = { .min = 5, .max = 80 },
  244. .p1 = { .min = 1, .max = 8 },
  245. .p2 = { .dot_limit = 225000,
  246. .p2_slow = 10, .p2_fast = 5 },
  247. };
  248. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  249. .dot = { .min = 25000, .max = 350000 },
  250. .vco = { .min = 1760000, .max = 3510000 },
  251. .n = { .min = 1, .max = 3 },
  252. .m = { .min = 79, .max = 118 },
  253. .m1 = { .min = 12, .max = 22 },
  254. .m2 = { .min = 5, .max = 9 },
  255. .p = { .min = 28, .max = 112 },
  256. .p1 = { .min = 2, .max = 8 },
  257. .p2 = { .dot_limit = 225000,
  258. .p2_slow = 14, .p2_fast = 14 },
  259. };
  260. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  261. .dot = { .min = 25000, .max = 350000 },
  262. .vco = { .min = 1760000, .max = 3510000 },
  263. .n = { .min = 1, .max = 3 },
  264. .m = { .min = 79, .max = 127 },
  265. .m1 = { .min = 12, .max = 22 },
  266. .m2 = { .min = 5, .max = 9 },
  267. .p = { .min = 14, .max = 56 },
  268. .p1 = { .min = 2, .max = 8 },
  269. .p2 = { .dot_limit = 225000,
  270. .p2_slow = 7, .p2_fast = 7 },
  271. };
  272. /* LVDS 100mhz refclk limits. */
  273. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  274. .dot = { .min = 25000, .max = 350000 },
  275. .vco = { .min = 1760000, .max = 3510000 },
  276. .n = { .min = 1, .max = 2 },
  277. .m = { .min = 79, .max = 126 },
  278. .m1 = { .min = 12, .max = 22 },
  279. .m2 = { .min = 5, .max = 9 },
  280. .p = { .min = 28, .max = 112 },
  281. .p1 = { .min = 2, .max = 8 },
  282. .p2 = { .dot_limit = 225000,
  283. .p2_slow = 14, .p2_fast = 14 },
  284. };
  285. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  286. .dot = { .min = 25000, .max = 350000 },
  287. .vco = { .min = 1760000, .max = 3510000 },
  288. .n = { .min = 1, .max = 3 },
  289. .m = { .min = 79, .max = 126 },
  290. .m1 = { .min = 12, .max = 22 },
  291. .m2 = { .min = 5, .max = 9 },
  292. .p = { .min = 14, .max = 42 },
  293. .p1 = { .min = 2, .max = 6 },
  294. .p2 = { .dot_limit = 225000,
  295. .p2_slow = 7, .p2_fast = 7 },
  296. };
  297. static const intel_limit_t intel_limits_vlv = {
  298. /*
  299. * These are the data rate limits (measured in fast clocks)
  300. * since those are the strictest limits we have. The fast
  301. * clock and actual rate limits are more relaxed, so checking
  302. * them would make no difference.
  303. */
  304. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  305. .vco = { .min = 4000000, .max = 6000000 },
  306. .n = { .min = 1, .max = 7 },
  307. .m1 = { .min = 2, .max = 3 },
  308. .m2 = { .min = 11, .max = 156 },
  309. .p1 = { .min = 2, .max = 3 },
  310. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  311. };
  312. static const intel_limit_t intel_limits_chv = {
  313. /*
  314. * These are the data rate limits (measured in fast clocks)
  315. * since those are the strictest limits we have. The fast
  316. * clock and actual rate limits are more relaxed, so checking
  317. * them would make no difference.
  318. */
  319. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  320. .vco = { .min = 4860000, .max = 6700000 },
  321. .n = { .min = 1, .max = 1 },
  322. .m1 = { .min = 2, .max = 2 },
  323. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  324. .p1 = { .min = 2, .max = 4 },
  325. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  326. };
  327. static void vlv_clock(int refclk, intel_clock_t *clock)
  328. {
  329. clock->m = clock->m1 * clock->m2;
  330. clock->p = clock->p1 * clock->p2;
  331. if (WARN_ON(clock->n == 0 || clock->p == 0))
  332. return;
  333. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  334. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  335. }
  336. /**
  337. * Returns whether any output on the specified pipe is of the specified type
  338. */
  339. static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  340. {
  341. struct drm_device *dev = crtc->dev;
  342. struct intel_encoder *encoder;
  343. for_each_encoder_on_crtc(dev, crtc, encoder)
  344. if (encoder->type == type)
  345. return true;
  346. return false;
  347. }
  348. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  349. int refclk)
  350. {
  351. struct drm_device *dev = crtc->dev;
  352. const intel_limit_t *limit;
  353. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  354. if (intel_is_dual_link_lvds(dev)) {
  355. if (refclk == 100000)
  356. limit = &intel_limits_ironlake_dual_lvds_100m;
  357. else
  358. limit = &intel_limits_ironlake_dual_lvds;
  359. } else {
  360. if (refclk == 100000)
  361. limit = &intel_limits_ironlake_single_lvds_100m;
  362. else
  363. limit = &intel_limits_ironlake_single_lvds;
  364. }
  365. } else
  366. limit = &intel_limits_ironlake_dac;
  367. return limit;
  368. }
  369. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  370. {
  371. struct drm_device *dev = crtc->dev;
  372. const intel_limit_t *limit;
  373. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  374. if (intel_is_dual_link_lvds(dev))
  375. limit = &intel_limits_g4x_dual_channel_lvds;
  376. else
  377. limit = &intel_limits_g4x_single_channel_lvds;
  378. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  379. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  380. limit = &intel_limits_g4x_hdmi;
  381. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  382. limit = &intel_limits_g4x_sdvo;
  383. } else /* The option is for other outputs */
  384. limit = &intel_limits_i9xx_sdvo;
  385. return limit;
  386. }
  387. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  388. {
  389. struct drm_device *dev = crtc->dev;
  390. const intel_limit_t *limit;
  391. if (HAS_PCH_SPLIT(dev))
  392. limit = intel_ironlake_limit(crtc, refclk);
  393. else if (IS_G4X(dev)) {
  394. limit = intel_g4x_limit(crtc);
  395. } else if (IS_PINEVIEW(dev)) {
  396. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  397. limit = &intel_limits_pineview_lvds;
  398. else
  399. limit = &intel_limits_pineview_sdvo;
  400. } else if (IS_CHERRYVIEW(dev)) {
  401. limit = &intel_limits_chv;
  402. } else if (IS_VALLEYVIEW(dev)) {
  403. limit = &intel_limits_vlv;
  404. } else if (!IS_GEN2(dev)) {
  405. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  406. limit = &intel_limits_i9xx_lvds;
  407. else
  408. limit = &intel_limits_i9xx_sdvo;
  409. } else {
  410. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  411. limit = &intel_limits_i8xx_lvds;
  412. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
  413. limit = &intel_limits_i8xx_dvo;
  414. else
  415. limit = &intel_limits_i8xx_dac;
  416. }
  417. return limit;
  418. }
  419. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  420. static void pineview_clock(int refclk, intel_clock_t *clock)
  421. {
  422. clock->m = clock->m2 + 2;
  423. clock->p = clock->p1 * clock->p2;
  424. if (WARN_ON(clock->n == 0 || clock->p == 0))
  425. return;
  426. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  427. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  428. }
  429. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  430. {
  431. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  432. }
  433. static void i9xx_clock(int refclk, intel_clock_t *clock)
  434. {
  435. clock->m = i9xx_dpll_compute_m(clock);
  436. clock->p = clock->p1 * clock->p2;
  437. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  438. return;
  439. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  440. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  441. }
  442. static void chv_clock(int refclk, intel_clock_t *clock)
  443. {
  444. clock->m = clock->m1 * clock->m2;
  445. clock->p = clock->p1 * clock->p2;
  446. if (WARN_ON(clock->n == 0 || clock->p == 0))
  447. return;
  448. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  449. clock->n << 22);
  450. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  451. }
  452. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  453. /**
  454. * Returns whether the given set of divisors are valid for a given refclk with
  455. * the given connectors.
  456. */
  457. static bool intel_PLL_is_valid(struct drm_device *dev,
  458. const intel_limit_t *limit,
  459. const intel_clock_t *clock)
  460. {
  461. if (clock->n < limit->n.min || limit->n.max < clock->n)
  462. INTELPllInvalid("n out of range\n");
  463. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  464. INTELPllInvalid("p1 out of range\n");
  465. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  466. INTELPllInvalid("m2 out of range\n");
  467. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  468. INTELPllInvalid("m1 out of range\n");
  469. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  470. if (clock->m1 <= clock->m2)
  471. INTELPllInvalid("m1 <= m2\n");
  472. if (!IS_VALLEYVIEW(dev)) {
  473. if (clock->p < limit->p.min || limit->p.max < clock->p)
  474. INTELPllInvalid("p out of range\n");
  475. if (clock->m < limit->m.min || limit->m.max < clock->m)
  476. INTELPllInvalid("m out of range\n");
  477. }
  478. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  479. INTELPllInvalid("vco out of range\n");
  480. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  481. * connector, etc., rather than just a single range.
  482. */
  483. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  484. INTELPllInvalid("dot out of range\n");
  485. return true;
  486. }
  487. static bool
  488. i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  489. int target, int refclk, intel_clock_t *match_clock,
  490. intel_clock_t *best_clock)
  491. {
  492. struct drm_device *dev = crtc->dev;
  493. intel_clock_t clock;
  494. int err = target;
  495. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  496. /*
  497. * For LVDS just rely on its current settings for dual-channel.
  498. * We haven't figured out how to reliably set up different
  499. * single/dual channel state, if we even can.
  500. */
  501. if (intel_is_dual_link_lvds(dev))
  502. clock.p2 = limit->p2.p2_fast;
  503. else
  504. clock.p2 = limit->p2.p2_slow;
  505. } else {
  506. if (target < limit->p2.dot_limit)
  507. clock.p2 = limit->p2.p2_slow;
  508. else
  509. clock.p2 = limit->p2.p2_fast;
  510. }
  511. memset(best_clock, 0, sizeof(*best_clock));
  512. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  513. clock.m1++) {
  514. for (clock.m2 = limit->m2.min;
  515. clock.m2 <= limit->m2.max; clock.m2++) {
  516. if (clock.m2 >= clock.m1)
  517. break;
  518. for (clock.n = limit->n.min;
  519. clock.n <= limit->n.max; clock.n++) {
  520. for (clock.p1 = limit->p1.min;
  521. clock.p1 <= limit->p1.max; clock.p1++) {
  522. int this_err;
  523. i9xx_clock(refclk, &clock);
  524. if (!intel_PLL_is_valid(dev, limit,
  525. &clock))
  526. continue;
  527. if (match_clock &&
  528. clock.p != match_clock->p)
  529. continue;
  530. this_err = abs(clock.dot - target);
  531. if (this_err < err) {
  532. *best_clock = clock;
  533. err = this_err;
  534. }
  535. }
  536. }
  537. }
  538. }
  539. return (err != target);
  540. }
  541. static bool
  542. pnv_find_best_dpll(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. intel_clock_t clock;
  548. int err = target;
  549. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  550. /*
  551. * For LVDS just rely on its current settings for dual-channel.
  552. * We haven't figured out how to reliably set up different
  553. * single/dual channel state, if we even can.
  554. */
  555. if (intel_is_dual_link_lvds(dev))
  556. clock.p2 = limit->p2.p2_fast;
  557. else
  558. clock.p2 = limit->p2.p2_slow;
  559. } else {
  560. if (target < limit->p2.dot_limit)
  561. clock.p2 = limit->p2.p2_slow;
  562. else
  563. clock.p2 = limit->p2.p2_fast;
  564. }
  565. memset(best_clock, 0, sizeof(*best_clock));
  566. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  567. clock.m1++) {
  568. for (clock.m2 = limit->m2.min;
  569. clock.m2 <= limit->m2.max; clock.m2++) {
  570. for (clock.n = limit->n.min;
  571. clock.n <= limit->n.max; clock.n++) {
  572. for (clock.p1 = limit->p1.min;
  573. clock.p1 <= limit->p1.max; clock.p1++) {
  574. int this_err;
  575. pineview_clock(refclk, &clock);
  576. if (!intel_PLL_is_valid(dev, limit,
  577. &clock))
  578. continue;
  579. if (match_clock &&
  580. clock.p != match_clock->p)
  581. continue;
  582. this_err = abs(clock.dot - target);
  583. if (this_err < err) {
  584. *best_clock = clock;
  585. err = this_err;
  586. }
  587. }
  588. }
  589. }
  590. }
  591. return (err != target);
  592. }
  593. static bool
  594. g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  595. int target, int refclk, intel_clock_t *match_clock,
  596. intel_clock_t *best_clock)
  597. {
  598. struct drm_device *dev = crtc->dev;
  599. intel_clock_t clock;
  600. int max_n;
  601. bool found;
  602. /* approximately equals target * 0.00585 */
  603. int err_most = (target >> 8) + (target >> 9);
  604. found = false;
  605. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  606. if (intel_is_dual_link_lvds(dev))
  607. clock.p2 = limit->p2.p2_fast;
  608. else
  609. clock.p2 = limit->p2.p2_slow;
  610. } else {
  611. if (target < limit->p2.dot_limit)
  612. clock.p2 = limit->p2.p2_slow;
  613. else
  614. clock.p2 = limit->p2.p2_fast;
  615. }
  616. memset(best_clock, 0, sizeof(*best_clock));
  617. max_n = limit->n.max;
  618. /* based on hardware requirement, prefer smaller n to precision */
  619. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  620. /* based on hardware requirement, prefere larger m1,m2 */
  621. for (clock.m1 = limit->m1.max;
  622. clock.m1 >= limit->m1.min; clock.m1--) {
  623. for (clock.m2 = limit->m2.max;
  624. clock.m2 >= limit->m2.min; clock.m2--) {
  625. for (clock.p1 = limit->p1.max;
  626. clock.p1 >= limit->p1.min; clock.p1--) {
  627. int this_err;
  628. i9xx_clock(refclk, &clock);
  629. if (!intel_PLL_is_valid(dev, limit,
  630. &clock))
  631. continue;
  632. this_err = abs(clock.dot - target);
  633. if (this_err < err_most) {
  634. *best_clock = clock;
  635. err_most = this_err;
  636. max_n = clock.n;
  637. found = true;
  638. }
  639. }
  640. }
  641. }
  642. }
  643. return found;
  644. }
  645. static bool
  646. vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  647. int target, int refclk, intel_clock_t *match_clock,
  648. intel_clock_t *best_clock)
  649. {
  650. struct drm_device *dev = crtc->dev;
  651. intel_clock_t clock;
  652. unsigned int bestppm = 1000000;
  653. /* min update 19.2 MHz */
  654. int max_n = min(limit->n.max, refclk / 19200);
  655. bool found = false;
  656. target *= 5; /* fast clock */
  657. memset(best_clock, 0, sizeof(*best_clock));
  658. /* based on hardware requirement, prefer smaller n to precision */
  659. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  660. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  661. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  662. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  663. clock.p = clock.p1 * clock.p2;
  664. /* based on hardware requirement, prefer bigger m1,m2 values */
  665. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  666. unsigned int ppm, diff;
  667. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  668. refclk * clock.m1);
  669. vlv_clock(refclk, &clock);
  670. if (!intel_PLL_is_valid(dev, limit,
  671. &clock))
  672. continue;
  673. diff = abs(clock.dot - target);
  674. ppm = div_u64(1000000ULL * diff, target);
  675. if (ppm < 100 && clock.p > best_clock->p) {
  676. bestppm = 0;
  677. *best_clock = clock;
  678. found = true;
  679. }
  680. if (bestppm >= 10 && ppm < bestppm - 10) {
  681. bestppm = ppm;
  682. *best_clock = clock;
  683. found = true;
  684. }
  685. }
  686. }
  687. }
  688. }
  689. return found;
  690. }
  691. static bool
  692. chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  693. int target, int refclk, intel_clock_t *match_clock,
  694. intel_clock_t *best_clock)
  695. {
  696. struct drm_device *dev = crtc->dev;
  697. intel_clock_t clock;
  698. uint64_t m2;
  699. int found = false;
  700. memset(best_clock, 0, sizeof(*best_clock));
  701. /*
  702. * Based on hardware doc, the n always set to 1, and m1 always
  703. * set to 2. If requires to support 200Mhz refclk, we need to
  704. * revisit this because n may not 1 anymore.
  705. */
  706. clock.n = 1, clock.m1 = 2;
  707. target *= 5; /* fast clock */
  708. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  709. for (clock.p2 = limit->p2.p2_fast;
  710. clock.p2 >= limit->p2.p2_slow;
  711. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  712. clock.p = clock.p1 * clock.p2;
  713. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  714. clock.n) << 22, refclk * clock.m1);
  715. if (m2 > INT_MAX/clock.m1)
  716. continue;
  717. clock.m2 = m2;
  718. chv_clock(refclk, &clock);
  719. if (!intel_PLL_is_valid(dev, limit, &clock))
  720. continue;
  721. /* based on hardware requirement, prefer bigger p
  722. */
  723. if (clock.p > best_clock->p) {
  724. *best_clock = clock;
  725. found = true;
  726. }
  727. }
  728. }
  729. return found;
  730. }
  731. bool intel_crtc_active(struct drm_crtc *crtc)
  732. {
  733. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  734. /* Be paranoid as we can arrive here with only partial
  735. * state retrieved from the hardware during setup.
  736. *
  737. * We can ditch the adjusted_mode.crtc_clock check as soon
  738. * as Haswell has gained clock readout/fastboot support.
  739. *
  740. * We can ditch the crtc->primary->fb check as soon as we can
  741. * properly reconstruct framebuffers.
  742. */
  743. return intel_crtc->active && crtc->primary->fb &&
  744. intel_crtc->config.adjusted_mode.crtc_clock;
  745. }
  746. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  747. enum pipe pipe)
  748. {
  749. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  750. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  751. return intel_crtc->config.cpu_transcoder;
  752. }
  753. static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
  754. {
  755. struct drm_i915_private *dev_priv = dev->dev_private;
  756. u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
  757. frame = I915_READ(frame_reg);
  758. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  759. WARN(1, "vblank wait timed out\n");
  760. }
  761. /**
  762. * intel_wait_for_vblank - wait for vblank on a given pipe
  763. * @dev: drm device
  764. * @pipe: pipe to wait for
  765. *
  766. * Wait for vblank to occur on a given pipe. Needed for various bits of
  767. * mode setting code.
  768. */
  769. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  770. {
  771. struct drm_i915_private *dev_priv = dev->dev_private;
  772. int pipestat_reg = PIPESTAT(pipe);
  773. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  774. g4x_wait_for_vblank(dev, pipe);
  775. return;
  776. }
  777. /* Clear existing vblank status. Note this will clear any other
  778. * sticky status fields as well.
  779. *
  780. * This races with i915_driver_irq_handler() with the result
  781. * that either function could miss a vblank event. Here it is not
  782. * fatal, as we will either wait upon the next vblank interrupt or
  783. * timeout. Generally speaking intel_wait_for_vblank() is only
  784. * called during modeset at which time the GPU should be idle and
  785. * should *not* be performing page flips and thus not waiting on
  786. * vblanks...
  787. * Currently, the result of us stealing a vblank from the irq
  788. * handler is that a single frame will be skipped during swapbuffers.
  789. */
  790. I915_WRITE(pipestat_reg,
  791. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  792. /* Wait for vblank interrupt bit to set */
  793. if (wait_for(I915_READ(pipestat_reg) &
  794. PIPE_VBLANK_INTERRUPT_STATUS,
  795. 50))
  796. DRM_DEBUG_KMS("vblank wait timed out\n");
  797. }
  798. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  799. {
  800. struct drm_i915_private *dev_priv = dev->dev_private;
  801. u32 reg = PIPEDSL(pipe);
  802. u32 line1, line2;
  803. u32 line_mask;
  804. if (IS_GEN2(dev))
  805. line_mask = DSL_LINEMASK_GEN2;
  806. else
  807. line_mask = DSL_LINEMASK_GEN3;
  808. line1 = I915_READ(reg) & line_mask;
  809. mdelay(5);
  810. line2 = I915_READ(reg) & line_mask;
  811. return line1 == line2;
  812. }
  813. /*
  814. * intel_wait_for_pipe_off - wait for pipe to turn off
  815. * @dev: drm device
  816. * @pipe: pipe to wait for
  817. *
  818. * After disabling a pipe, we can't wait for vblank in the usual way,
  819. * spinning on the vblank interrupt status bit, since we won't actually
  820. * see an interrupt when the pipe is disabled.
  821. *
  822. * On Gen4 and above:
  823. * wait for the pipe register state bit to turn off
  824. *
  825. * Otherwise:
  826. * wait for the display line value to settle (it usually
  827. * ends up stopping at the start of the next frame).
  828. *
  829. */
  830. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  831. {
  832. struct drm_i915_private *dev_priv = dev->dev_private;
  833. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  834. pipe);
  835. if (INTEL_INFO(dev)->gen >= 4) {
  836. int reg = PIPECONF(cpu_transcoder);
  837. /* Wait for the Pipe State to go off */
  838. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  839. 100))
  840. WARN(1, "pipe_off wait timed out\n");
  841. } else {
  842. /* Wait for the display line to settle */
  843. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  844. WARN(1, "pipe_off wait timed out\n");
  845. }
  846. }
  847. /*
  848. * ibx_digital_port_connected - is the specified port connected?
  849. * @dev_priv: i915 private structure
  850. * @port: the port to test
  851. *
  852. * Returns true if @port is connected, false otherwise.
  853. */
  854. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  855. struct intel_digital_port *port)
  856. {
  857. u32 bit;
  858. if (HAS_PCH_IBX(dev_priv->dev)) {
  859. switch (port->port) {
  860. case PORT_B:
  861. bit = SDE_PORTB_HOTPLUG;
  862. break;
  863. case PORT_C:
  864. bit = SDE_PORTC_HOTPLUG;
  865. break;
  866. case PORT_D:
  867. bit = SDE_PORTD_HOTPLUG;
  868. break;
  869. default:
  870. return true;
  871. }
  872. } else {
  873. switch (port->port) {
  874. case PORT_B:
  875. bit = SDE_PORTB_HOTPLUG_CPT;
  876. break;
  877. case PORT_C:
  878. bit = SDE_PORTC_HOTPLUG_CPT;
  879. break;
  880. case PORT_D:
  881. bit = SDE_PORTD_HOTPLUG_CPT;
  882. break;
  883. default:
  884. return true;
  885. }
  886. }
  887. return I915_READ(SDEISR) & bit;
  888. }
  889. static const char *state_string(bool enabled)
  890. {
  891. return enabled ? "on" : "off";
  892. }
  893. /* Only for pre-ILK configs */
  894. void assert_pll(struct drm_i915_private *dev_priv,
  895. enum pipe pipe, bool state)
  896. {
  897. int reg;
  898. u32 val;
  899. bool cur_state;
  900. reg = DPLL(pipe);
  901. val = I915_READ(reg);
  902. cur_state = !!(val & DPLL_VCO_ENABLE);
  903. WARN(cur_state != state,
  904. "PLL state assertion failure (expected %s, current %s)\n",
  905. state_string(state), state_string(cur_state));
  906. }
  907. /* XXX: the dsi pll is shared between MIPI DSI ports */
  908. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  909. {
  910. u32 val;
  911. bool cur_state;
  912. mutex_lock(&dev_priv->dpio_lock);
  913. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  914. mutex_unlock(&dev_priv->dpio_lock);
  915. cur_state = val & DSI_PLL_VCO_EN;
  916. WARN(cur_state != state,
  917. "DSI PLL state assertion failure (expected %s, current %s)\n",
  918. state_string(state), state_string(cur_state));
  919. }
  920. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  921. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  922. struct intel_shared_dpll *
  923. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  924. {
  925. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  926. if (crtc->config.shared_dpll < 0)
  927. return NULL;
  928. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  929. }
  930. /* For ILK+ */
  931. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  932. struct intel_shared_dpll *pll,
  933. bool state)
  934. {
  935. bool cur_state;
  936. struct intel_dpll_hw_state hw_state;
  937. if (HAS_PCH_LPT(dev_priv->dev)) {
  938. DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
  939. return;
  940. }
  941. if (WARN (!pll,
  942. "asserting DPLL %s with no DPLL\n", state_string(state)))
  943. return;
  944. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  945. WARN(cur_state != state,
  946. "%s assertion failure (expected %s, current %s)\n",
  947. pll->name, state_string(state), state_string(cur_state));
  948. }
  949. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  950. enum pipe pipe, bool state)
  951. {
  952. int reg;
  953. u32 val;
  954. bool cur_state;
  955. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  956. pipe);
  957. if (HAS_DDI(dev_priv->dev)) {
  958. /* DDI does not have a specific FDI_TX register */
  959. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  960. val = I915_READ(reg);
  961. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  962. } else {
  963. reg = FDI_TX_CTL(pipe);
  964. val = I915_READ(reg);
  965. cur_state = !!(val & FDI_TX_ENABLE);
  966. }
  967. WARN(cur_state != state,
  968. "FDI TX state assertion failure (expected %s, current %s)\n",
  969. state_string(state), state_string(cur_state));
  970. }
  971. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  972. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  973. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  974. enum pipe pipe, bool state)
  975. {
  976. int reg;
  977. u32 val;
  978. bool cur_state;
  979. reg = FDI_RX_CTL(pipe);
  980. val = I915_READ(reg);
  981. cur_state = !!(val & FDI_RX_ENABLE);
  982. WARN(cur_state != state,
  983. "FDI RX state assertion failure (expected %s, current %s)\n",
  984. state_string(state), state_string(cur_state));
  985. }
  986. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  987. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  988. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  989. enum pipe pipe)
  990. {
  991. int reg;
  992. u32 val;
  993. /* ILK FDI PLL is always enabled */
  994. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  995. return;
  996. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  997. if (HAS_DDI(dev_priv->dev))
  998. return;
  999. reg = FDI_TX_CTL(pipe);
  1000. val = I915_READ(reg);
  1001. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1002. }
  1003. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1004. enum pipe pipe, bool state)
  1005. {
  1006. int reg;
  1007. u32 val;
  1008. bool cur_state;
  1009. reg = FDI_RX_CTL(pipe);
  1010. val = I915_READ(reg);
  1011. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1012. WARN(cur_state != state,
  1013. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1014. state_string(state), state_string(cur_state));
  1015. }
  1016. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1017. enum pipe pipe)
  1018. {
  1019. int pp_reg, lvds_reg;
  1020. u32 val;
  1021. enum pipe panel_pipe = PIPE_A;
  1022. bool locked = true;
  1023. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  1024. pp_reg = PCH_PP_CONTROL;
  1025. lvds_reg = PCH_LVDS;
  1026. } else {
  1027. pp_reg = PP_CONTROL;
  1028. lvds_reg = LVDS;
  1029. }
  1030. val = I915_READ(pp_reg);
  1031. if (!(val & PANEL_POWER_ON) ||
  1032. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  1033. locked = false;
  1034. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  1035. panel_pipe = PIPE_B;
  1036. WARN(panel_pipe == pipe && locked,
  1037. "panel assertion failure, pipe %c regs locked\n",
  1038. pipe_name(pipe));
  1039. }
  1040. static void assert_cursor(struct drm_i915_private *dev_priv,
  1041. enum pipe pipe, bool state)
  1042. {
  1043. struct drm_device *dev = dev_priv->dev;
  1044. bool cur_state;
  1045. if (IS_845G(dev) || IS_I865G(dev))
  1046. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1047. else
  1048. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1049. WARN(cur_state != state,
  1050. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1051. pipe_name(pipe), state_string(state), state_string(cur_state));
  1052. }
  1053. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1054. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1055. void assert_pipe(struct drm_i915_private *dev_priv,
  1056. enum pipe pipe, bool state)
  1057. {
  1058. int reg;
  1059. u32 val;
  1060. bool cur_state;
  1061. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1062. pipe);
  1063. /* if we need the pipe A quirk it must be always on */
  1064. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  1065. state = true;
  1066. if (!intel_display_power_enabled(dev_priv,
  1067. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1068. cur_state = false;
  1069. } else {
  1070. reg = PIPECONF(cpu_transcoder);
  1071. val = I915_READ(reg);
  1072. cur_state = !!(val & PIPECONF_ENABLE);
  1073. }
  1074. WARN(cur_state != state,
  1075. "pipe %c assertion failure (expected %s, current %s)\n",
  1076. pipe_name(pipe), state_string(state), state_string(cur_state));
  1077. }
  1078. static void assert_plane(struct drm_i915_private *dev_priv,
  1079. enum plane plane, bool state)
  1080. {
  1081. int reg;
  1082. u32 val;
  1083. bool cur_state;
  1084. reg = DSPCNTR(plane);
  1085. val = I915_READ(reg);
  1086. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1087. WARN(cur_state != state,
  1088. "plane %c assertion failure (expected %s, current %s)\n",
  1089. plane_name(plane), state_string(state), state_string(cur_state));
  1090. }
  1091. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1092. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1093. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1094. enum pipe pipe)
  1095. {
  1096. struct drm_device *dev = dev_priv->dev;
  1097. int reg, i;
  1098. u32 val;
  1099. int cur_pipe;
  1100. /* Primary planes are fixed to pipes on gen4+ */
  1101. if (INTEL_INFO(dev)->gen >= 4) {
  1102. reg = DSPCNTR(pipe);
  1103. val = I915_READ(reg);
  1104. WARN(val & DISPLAY_PLANE_ENABLE,
  1105. "plane %c assertion failure, should be disabled but not\n",
  1106. plane_name(pipe));
  1107. return;
  1108. }
  1109. /* Need to check both planes against the pipe */
  1110. for_each_pipe(i) {
  1111. reg = DSPCNTR(i);
  1112. val = I915_READ(reg);
  1113. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1114. DISPPLANE_SEL_PIPE_SHIFT;
  1115. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1116. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1117. plane_name(i), pipe_name(pipe));
  1118. }
  1119. }
  1120. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1121. enum pipe pipe)
  1122. {
  1123. struct drm_device *dev = dev_priv->dev;
  1124. int reg, sprite;
  1125. u32 val;
  1126. if (IS_VALLEYVIEW(dev)) {
  1127. for_each_sprite(pipe, sprite) {
  1128. reg = SPCNTR(pipe, sprite);
  1129. val = I915_READ(reg);
  1130. WARN(val & SP_ENABLE,
  1131. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1132. sprite_name(pipe, sprite), pipe_name(pipe));
  1133. }
  1134. } else if (INTEL_INFO(dev)->gen >= 7) {
  1135. reg = SPRCTL(pipe);
  1136. val = I915_READ(reg);
  1137. WARN(val & SPRITE_ENABLE,
  1138. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1139. plane_name(pipe), pipe_name(pipe));
  1140. } else if (INTEL_INFO(dev)->gen >= 5) {
  1141. reg = DVSCNTR(pipe);
  1142. val = I915_READ(reg);
  1143. WARN(val & DVS_ENABLE,
  1144. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1145. plane_name(pipe), pipe_name(pipe));
  1146. }
  1147. }
  1148. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1149. {
  1150. u32 val;
  1151. bool enabled;
  1152. WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1153. val = I915_READ(PCH_DREF_CONTROL);
  1154. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1155. DREF_SUPERSPREAD_SOURCE_MASK));
  1156. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1157. }
  1158. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1159. enum pipe pipe)
  1160. {
  1161. int reg;
  1162. u32 val;
  1163. bool enabled;
  1164. reg = PCH_TRANSCONF(pipe);
  1165. val = I915_READ(reg);
  1166. enabled = !!(val & TRANS_ENABLE);
  1167. WARN(enabled,
  1168. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1169. pipe_name(pipe));
  1170. }
  1171. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1172. enum pipe pipe, u32 port_sel, u32 val)
  1173. {
  1174. if ((val & DP_PORT_EN) == 0)
  1175. return false;
  1176. if (HAS_PCH_CPT(dev_priv->dev)) {
  1177. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1178. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1179. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1180. return false;
  1181. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1182. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1183. return false;
  1184. } else {
  1185. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1186. return false;
  1187. }
  1188. return true;
  1189. }
  1190. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1191. enum pipe pipe, u32 val)
  1192. {
  1193. if ((val & SDVO_ENABLE) == 0)
  1194. return false;
  1195. if (HAS_PCH_CPT(dev_priv->dev)) {
  1196. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1197. return false;
  1198. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1199. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1200. return false;
  1201. } else {
  1202. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1203. return false;
  1204. }
  1205. return true;
  1206. }
  1207. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1208. enum pipe pipe, u32 val)
  1209. {
  1210. if ((val & LVDS_PORT_EN) == 0)
  1211. return false;
  1212. if (HAS_PCH_CPT(dev_priv->dev)) {
  1213. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1214. return false;
  1215. } else {
  1216. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1217. return false;
  1218. }
  1219. return true;
  1220. }
  1221. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1222. enum pipe pipe, u32 val)
  1223. {
  1224. if ((val & ADPA_DAC_ENABLE) == 0)
  1225. return false;
  1226. if (HAS_PCH_CPT(dev_priv->dev)) {
  1227. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1228. return false;
  1229. } else {
  1230. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1231. return false;
  1232. }
  1233. return true;
  1234. }
  1235. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1236. enum pipe pipe, int reg, u32 port_sel)
  1237. {
  1238. u32 val = I915_READ(reg);
  1239. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1240. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1241. reg, pipe_name(pipe));
  1242. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1243. && (val & DP_PIPEB_SELECT),
  1244. "IBX PCH dp port still using transcoder B\n");
  1245. }
  1246. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1247. enum pipe pipe, int reg)
  1248. {
  1249. u32 val = I915_READ(reg);
  1250. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1251. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1252. reg, pipe_name(pipe));
  1253. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1254. && (val & SDVO_PIPE_B_SELECT),
  1255. "IBX PCH hdmi port still using transcoder B\n");
  1256. }
  1257. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1258. enum pipe pipe)
  1259. {
  1260. int reg;
  1261. u32 val;
  1262. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1263. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1264. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1265. reg = PCH_ADPA;
  1266. val = I915_READ(reg);
  1267. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1268. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1269. pipe_name(pipe));
  1270. reg = PCH_LVDS;
  1271. val = I915_READ(reg);
  1272. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1273. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1274. pipe_name(pipe));
  1275. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1276. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1277. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1278. }
  1279. static void intel_init_dpio(struct drm_device *dev)
  1280. {
  1281. struct drm_i915_private *dev_priv = dev->dev_private;
  1282. if (!IS_VALLEYVIEW(dev))
  1283. return;
  1284. /*
  1285. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1286. * CHV x1 PHY (DP/HDMI D)
  1287. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1288. */
  1289. if (IS_CHERRYVIEW(dev)) {
  1290. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1291. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1292. } else {
  1293. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1294. }
  1295. }
  1296. static void intel_reset_dpio(struct drm_device *dev)
  1297. {
  1298. struct drm_i915_private *dev_priv = dev->dev_private;
  1299. if (!IS_VALLEYVIEW(dev))
  1300. return;
  1301. /*
  1302. * Enable the CRI clock source so we can get at the display and the
  1303. * reference clock for VGA hotplug / manual detection.
  1304. */
  1305. I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
  1306. DPLL_REFA_CLK_ENABLE_VLV |
  1307. DPLL_INTEGRATED_CRI_CLK_VLV);
  1308. if (IS_CHERRYVIEW(dev)) {
  1309. enum dpio_phy phy;
  1310. u32 val;
  1311. for (phy = DPIO_PHY0; phy < I915_NUM_PHYS_VLV; phy++) {
  1312. /* Poll for phypwrgood signal */
  1313. if (wait_for(I915_READ(DISPLAY_PHY_STATUS) &
  1314. PHY_POWERGOOD(phy), 1))
  1315. DRM_ERROR("Display PHY %d is not power up\n", phy);
  1316. /*
  1317. * Deassert common lane reset for PHY.
  1318. *
  1319. * This should only be done on init and resume from S3
  1320. * with both PLLs disabled, or we risk losing DPIO and
  1321. * PLL synchronization.
  1322. */
  1323. val = I915_READ(DISPLAY_PHY_CONTROL);
  1324. I915_WRITE(DISPLAY_PHY_CONTROL,
  1325. PHY_COM_LANE_RESET_DEASSERT(phy, val));
  1326. }
  1327. } else {
  1328. /*
  1329. * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
  1330. * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
  1331. * a. GUnit 0x2110 bit[0] set to 1 (def 0)
  1332. * b. The other bits such as sfr settings / modesel may all
  1333. * be set to 0.
  1334. *
  1335. * This should only be done on init and resume from S3 with
  1336. * both PLLs disabled, or we risk losing DPIO and PLL
  1337. * synchronization.
  1338. */
  1339. I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
  1340. }
  1341. }
  1342. static void vlv_enable_pll(struct intel_crtc *crtc)
  1343. {
  1344. struct drm_device *dev = crtc->base.dev;
  1345. struct drm_i915_private *dev_priv = dev->dev_private;
  1346. int reg = DPLL(crtc->pipe);
  1347. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1348. assert_pipe_disabled(dev_priv, crtc->pipe);
  1349. /* No really, not for ILK+ */
  1350. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1351. /* PLL is protected by panel, make sure we can write it */
  1352. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1353. assert_panel_unlocked(dev_priv, crtc->pipe);
  1354. I915_WRITE(reg, dpll);
  1355. POSTING_READ(reg);
  1356. udelay(150);
  1357. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1358. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1359. I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
  1360. POSTING_READ(DPLL_MD(crtc->pipe));
  1361. /* We do this three times for luck */
  1362. I915_WRITE(reg, dpll);
  1363. POSTING_READ(reg);
  1364. udelay(150); /* wait for warmup */
  1365. I915_WRITE(reg, dpll);
  1366. POSTING_READ(reg);
  1367. udelay(150); /* wait for warmup */
  1368. I915_WRITE(reg, dpll);
  1369. POSTING_READ(reg);
  1370. udelay(150); /* wait for warmup */
  1371. }
  1372. static void chv_enable_pll(struct intel_crtc *crtc)
  1373. {
  1374. struct drm_device *dev = crtc->base.dev;
  1375. struct drm_i915_private *dev_priv = dev->dev_private;
  1376. int pipe = crtc->pipe;
  1377. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1378. u32 tmp;
  1379. assert_pipe_disabled(dev_priv, crtc->pipe);
  1380. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1381. mutex_lock(&dev_priv->dpio_lock);
  1382. /* Enable back the 10bit clock to display controller */
  1383. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1384. tmp |= DPIO_DCLKP_EN;
  1385. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1386. /*
  1387. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1388. */
  1389. udelay(1);
  1390. /* Enable PLL */
  1391. I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
  1392. /* Check PLL is locked */
  1393. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1394. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1395. /* not sure when this should be written */
  1396. I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
  1397. POSTING_READ(DPLL_MD(pipe));
  1398. mutex_unlock(&dev_priv->dpio_lock);
  1399. }
  1400. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1401. {
  1402. struct drm_device *dev = crtc->base.dev;
  1403. struct drm_i915_private *dev_priv = dev->dev_private;
  1404. int reg = DPLL(crtc->pipe);
  1405. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1406. assert_pipe_disabled(dev_priv, crtc->pipe);
  1407. /* No really, not for ILK+ */
  1408. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1409. /* PLL is protected by panel, make sure we can write it */
  1410. if (IS_MOBILE(dev) && !IS_I830(dev))
  1411. assert_panel_unlocked(dev_priv, crtc->pipe);
  1412. I915_WRITE(reg, dpll);
  1413. /* Wait for the clocks to stabilize. */
  1414. POSTING_READ(reg);
  1415. udelay(150);
  1416. if (INTEL_INFO(dev)->gen >= 4) {
  1417. I915_WRITE(DPLL_MD(crtc->pipe),
  1418. crtc->config.dpll_hw_state.dpll_md);
  1419. } else {
  1420. /* The pixel multiplier can only be updated once the
  1421. * DPLL is enabled and the clocks are stable.
  1422. *
  1423. * So write it again.
  1424. */
  1425. I915_WRITE(reg, dpll);
  1426. }
  1427. /* We do this three times for luck */
  1428. I915_WRITE(reg, dpll);
  1429. POSTING_READ(reg);
  1430. udelay(150); /* wait for warmup */
  1431. I915_WRITE(reg, dpll);
  1432. POSTING_READ(reg);
  1433. udelay(150); /* wait for warmup */
  1434. I915_WRITE(reg, dpll);
  1435. POSTING_READ(reg);
  1436. udelay(150); /* wait for warmup */
  1437. }
  1438. /**
  1439. * i9xx_disable_pll - disable a PLL
  1440. * @dev_priv: i915 private structure
  1441. * @pipe: pipe PLL to disable
  1442. *
  1443. * Disable the PLL for @pipe, making sure the pipe is off first.
  1444. *
  1445. * Note! This is for pre-ILK only.
  1446. */
  1447. static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1448. {
  1449. /* Don't disable pipe A or pipe A PLLs if needed */
  1450. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1451. return;
  1452. /* Make sure the pipe isn't still relying on us */
  1453. assert_pipe_disabled(dev_priv, pipe);
  1454. I915_WRITE(DPLL(pipe), 0);
  1455. POSTING_READ(DPLL(pipe));
  1456. }
  1457. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1458. {
  1459. u32 val = 0;
  1460. /* Make sure the pipe isn't still relying on us */
  1461. assert_pipe_disabled(dev_priv, pipe);
  1462. /*
  1463. * Leave integrated clock source and reference clock enabled for pipe B.
  1464. * The latter is needed for VGA hotplug / manual detection.
  1465. */
  1466. if (pipe == PIPE_B)
  1467. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1468. I915_WRITE(DPLL(pipe), val);
  1469. POSTING_READ(DPLL(pipe));
  1470. }
  1471. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1472. {
  1473. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1474. u32 val;
  1475. /* Make sure the pipe isn't still relying on us */
  1476. assert_pipe_disabled(dev_priv, pipe);
  1477. /* Set PLL en = 0 */
  1478. val = DPLL_SSC_REF_CLOCK_CHV;
  1479. if (pipe != PIPE_A)
  1480. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1481. I915_WRITE(DPLL(pipe), val);
  1482. POSTING_READ(DPLL(pipe));
  1483. mutex_lock(&dev_priv->dpio_lock);
  1484. /* Disable 10bit clock to display controller */
  1485. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1486. val &= ~DPIO_DCLKP_EN;
  1487. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1488. mutex_unlock(&dev_priv->dpio_lock);
  1489. }
  1490. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1491. struct intel_digital_port *dport)
  1492. {
  1493. u32 port_mask;
  1494. int dpll_reg;
  1495. switch (dport->port) {
  1496. case PORT_B:
  1497. port_mask = DPLL_PORTB_READY_MASK;
  1498. dpll_reg = DPLL(0);
  1499. break;
  1500. case PORT_C:
  1501. port_mask = DPLL_PORTC_READY_MASK;
  1502. dpll_reg = DPLL(0);
  1503. break;
  1504. case PORT_D:
  1505. port_mask = DPLL_PORTD_READY_MASK;
  1506. dpll_reg = DPIO_PHY_STATUS;
  1507. break;
  1508. default:
  1509. BUG();
  1510. }
  1511. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1512. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1513. port_name(dport->port), I915_READ(dpll_reg));
  1514. }
  1515. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1516. {
  1517. struct drm_device *dev = crtc->base.dev;
  1518. struct drm_i915_private *dev_priv = dev->dev_private;
  1519. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1520. WARN_ON(!pll->refcount);
  1521. if (pll->active == 0) {
  1522. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1523. WARN_ON(pll->on);
  1524. assert_shared_dpll_disabled(dev_priv, pll);
  1525. pll->mode_set(dev_priv, pll);
  1526. }
  1527. }
  1528. /**
  1529. * intel_enable_shared_dpll - enable PCH PLL
  1530. * @dev_priv: i915 private structure
  1531. * @pipe: pipe PLL to enable
  1532. *
  1533. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1534. * drives the transcoder clock.
  1535. */
  1536. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1537. {
  1538. struct drm_device *dev = crtc->base.dev;
  1539. struct drm_i915_private *dev_priv = dev->dev_private;
  1540. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1541. if (WARN_ON(pll == NULL))
  1542. return;
  1543. if (WARN_ON(pll->refcount == 0))
  1544. return;
  1545. DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
  1546. pll->name, pll->active, pll->on,
  1547. crtc->base.base.id);
  1548. if (pll->active++) {
  1549. WARN_ON(!pll->on);
  1550. assert_shared_dpll_enabled(dev_priv, pll);
  1551. return;
  1552. }
  1553. WARN_ON(pll->on);
  1554. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1555. pll->enable(dev_priv, pll);
  1556. pll->on = true;
  1557. }
  1558. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1559. {
  1560. struct drm_device *dev = crtc->base.dev;
  1561. struct drm_i915_private *dev_priv = dev->dev_private;
  1562. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1563. /* PCH only available on ILK+ */
  1564. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1565. if (WARN_ON(pll == NULL))
  1566. return;
  1567. if (WARN_ON(pll->refcount == 0))
  1568. return;
  1569. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1570. pll->name, pll->active, pll->on,
  1571. crtc->base.base.id);
  1572. if (WARN_ON(pll->active == 0)) {
  1573. assert_shared_dpll_disabled(dev_priv, pll);
  1574. return;
  1575. }
  1576. assert_shared_dpll_enabled(dev_priv, pll);
  1577. WARN_ON(!pll->on);
  1578. if (--pll->active)
  1579. return;
  1580. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1581. pll->disable(dev_priv, pll);
  1582. pll->on = false;
  1583. }
  1584. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1585. enum pipe pipe)
  1586. {
  1587. struct drm_device *dev = dev_priv->dev;
  1588. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1589. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1590. uint32_t reg, val, pipeconf_val;
  1591. /* PCH only available on ILK+ */
  1592. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1593. /* Make sure PCH DPLL is enabled */
  1594. assert_shared_dpll_enabled(dev_priv,
  1595. intel_crtc_to_shared_dpll(intel_crtc));
  1596. /* FDI must be feeding us bits for PCH ports */
  1597. assert_fdi_tx_enabled(dev_priv, pipe);
  1598. assert_fdi_rx_enabled(dev_priv, pipe);
  1599. if (HAS_PCH_CPT(dev)) {
  1600. /* Workaround: Set the timing override bit before enabling the
  1601. * pch transcoder. */
  1602. reg = TRANS_CHICKEN2(pipe);
  1603. val = I915_READ(reg);
  1604. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1605. I915_WRITE(reg, val);
  1606. }
  1607. reg = PCH_TRANSCONF(pipe);
  1608. val = I915_READ(reg);
  1609. pipeconf_val = I915_READ(PIPECONF(pipe));
  1610. if (HAS_PCH_IBX(dev_priv->dev)) {
  1611. /*
  1612. * make the BPC in transcoder be consistent with
  1613. * that in pipeconf reg.
  1614. */
  1615. val &= ~PIPECONF_BPC_MASK;
  1616. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1617. }
  1618. val &= ~TRANS_INTERLACE_MASK;
  1619. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1620. if (HAS_PCH_IBX(dev_priv->dev) &&
  1621. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1622. val |= TRANS_LEGACY_INTERLACED_ILK;
  1623. else
  1624. val |= TRANS_INTERLACED;
  1625. else
  1626. val |= TRANS_PROGRESSIVE;
  1627. I915_WRITE(reg, val | TRANS_ENABLE);
  1628. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1629. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1630. }
  1631. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1632. enum transcoder cpu_transcoder)
  1633. {
  1634. u32 val, pipeconf_val;
  1635. /* PCH only available on ILK+ */
  1636. BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
  1637. /* FDI must be feeding us bits for PCH ports */
  1638. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1639. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1640. /* Workaround: set timing override bit. */
  1641. val = I915_READ(_TRANSA_CHICKEN2);
  1642. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1643. I915_WRITE(_TRANSA_CHICKEN2, val);
  1644. val = TRANS_ENABLE;
  1645. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1646. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1647. PIPECONF_INTERLACED_ILK)
  1648. val |= TRANS_INTERLACED;
  1649. else
  1650. val |= TRANS_PROGRESSIVE;
  1651. I915_WRITE(LPT_TRANSCONF, val);
  1652. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1653. DRM_ERROR("Failed to enable PCH transcoder\n");
  1654. }
  1655. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1656. enum pipe pipe)
  1657. {
  1658. struct drm_device *dev = dev_priv->dev;
  1659. uint32_t reg, val;
  1660. /* FDI relies on the transcoder */
  1661. assert_fdi_tx_disabled(dev_priv, pipe);
  1662. assert_fdi_rx_disabled(dev_priv, pipe);
  1663. /* Ports must be off as well */
  1664. assert_pch_ports_disabled(dev_priv, pipe);
  1665. reg = PCH_TRANSCONF(pipe);
  1666. val = I915_READ(reg);
  1667. val &= ~TRANS_ENABLE;
  1668. I915_WRITE(reg, val);
  1669. /* wait for PCH transcoder off, transcoder state */
  1670. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1671. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1672. if (!HAS_PCH_IBX(dev)) {
  1673. /* Workaround: Clear the timing override chicken bit again. */
  1674. reg = TRANS_CHICKEN2(pipe);
  1675. val = I915_READ(reg);
  1676. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1677. I915_WRITE(reg, val);
  1678. }
  1679. }
  1680. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1681. {
  1682. u32 val;
  1683. val = I915_READ(LPT_TRANSCONF);
  1684. val &= ~TRANS_ENABLE;
  1685. I915_WRITE(LPT_TRANSCONF, val);
  1686. /* wait for PCH transcoder off, transcoder state */
  1687. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1688. DRM_ERROR("Failed to disable PCH transcoder\n");
  1689. /* Workaround: clear timing override bit. */
  1690. val = I915_READ(_TRANSA_CHICKEN2);
  1691. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1692. I915_WRITE(_TRANSA_CHICKEN2, val);
  1693. }
  1694. /**
  1695. * intel_enable_pipe - enable a pipe, asserting requirements
  1696. * @crtc: crtc responsible for the pipe
  1697. *
  1698. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1699. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1700. */
  1701. static void intel_enable_pipe(struct intel_crtc *crtc)
  1702. {
  1703. struct drm_device *dev = crtc->base.dev;
  1704. struct drm_i915_private *dev_priv = dev->dev_private;
  1705. enum pipe pipe = crtc->pipe;
  1706. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1707. pipe);
  1708. enum pipe pch_transcoder;
  1709. int reg;
  1710. u32 val;
  1711. assert_planes_disabled(dev_priv, pipe);
  1712. assert_cursor_disabled(dev_priv, pipe);
  1713. assert_sprites_disabled(dev_priv, pipe);
  1714. if (HAS_PCH_LPT(dev_priv->dev))
  1715. pch_transcoder = TRANSCODER_A;
  1716. else
  1717. pch_transcoder = pipe;
  1718. /*
  1719. * A pipe without a PLL won't actually be able to drive bits from
  1720. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1721. * need the check.
  1722. */
  1723. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1724. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
  1725. assert_dsi_pll_enabled(dev_priv);
  1726. else
  1727. assert_pll_enabled(dev_priv, pipe);
  1728. else {
  1729. if (crtc->config.has_pch_encoder) {
  1730. /* if driving the PCH, we need FDI enabled */
  1731. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1732. assert_fdi_tx_pll_enabled(dev_priv,
  1733. (enum pipe) cpu_transcoder);
  1734. }
  1735. /* FIXME: assert CPU port conditions for SNB+ */
  1736. }
  1737. reg = PIPECONF(cpu_transcoder);
  1738. val = I915_READ(reg);
  1739. if (val & PIPECONF_ENABLE) {
  1740. WARN_ON(!(pipe == PIPE_A &&
  1741. dev_priv->quirks & QUIRK_PIPEA_FORCE));
  1742. return;
  1743. }
  1744. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1745. POSTING_READ(reg);
  1746. }
  1747. /**
  1748. * intel_disable_pipe - disable a pipe, asserting requirements
  1749. * @dev_priv: i915 private structure
  1750. * @pipe: pipe to disable
  1751. *
  1752. * Disable @pipe, making sure that various hardware specific requirements
  1753. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1754. *
  1755. * @pipe should be %PIPE_A or %PIPE_B.
  1756. *
  1757. * Will wait until the pipe has shut down before returning.
  1758. */
  1759. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1760. enum pipe pipe)
  1761. {
  1762. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1763. pipe);
  1764. int reg;
  1765. u32 val;
  1766. /*
  1767. * Make sure planes won't keep trying to pump pixels to us,
  1768. * or we might hang the display.
  1769. */
  1770. assert_planes_disabled(dev_priv, pipe);
  1771. assert_cursor_disabled(dev_priv, pipe);
  1772. assert_sprites_disabled(dev_priv, pipe);
  1773. /* Don't disable pipe A or pipe A PLLs if needed */
  1774. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1775. return;
  1776. reg = PIPECONF(cpu_transcoder);
  1777. val = I915_READ(reg);
  1778. if ((val & PIPECONF_ENABLE) == 0)
  1779. return;
  1780. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1781. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1782. }
  1783. /*
  1784. * Plane regs are double buffered, going from enabled->disabled needs a
  1785. * trigger in order to latch. The display address reg provides this.
  1786. */
  1787. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1788. enum plane plane)
  1789. {
  1790. struct drm_device *dev = dev_priv->dev;
  1791. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1792. I915_WRITE(reg, I915_READ(reg));
  1793. POSTING_READ(reg);
  1794. }
  1795. /**
  1796. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1797. * @dev_priv: i915 private structure
  1798. * @plane: plane to enable
  1799. * @pipe: pipe being fed
  1800. *
  1801. * Enable @plane on @pipe, making sure that @pipe is running first.
  1802. */
  1803. static void intel_enable_primary_hw_plane(struct drm_i915_private *dev_priv,
  1804. enum plane plane, enum pipe pipe)
  1805. {
  1806. struct intel_crtc *intel_crtc =
  1807. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1808. int reg;
  1809. u32 val;
  1810. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1811. assert_pipe_enabled(dev_priv, pipe);
  1812. if (intel_crtc->primary_enabled)
  1813. return;
  1814. intel_crtc->primary_enabled = true;
  1815. reg = DSPCNTR(plane);
  1816. val = I915_READ(reg);
  1817. WARN_ON(val & DISPLAY_PLANE_ENABLE);
  1818. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1819. intel_flush_primary_plane(dev_priv, plane);
  1820. }
  1821. /**
  1822. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1823. * @dev_priv: i915 private structure
  1824. * @plane: plane to disable
  1825. * @pipe: pipe consuming the data
  1826. *
  1827. * Disable @plane; should be an independent operation.
  1828. */
  1829. static void intel_disable_primary_hw_plane(struct drm_i915_private *dev_priv,
  1830. enum plane plane, enum pipe pipe)
  1831. {
  1832. struct intel_crtc *intel_crtc =
  1833. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1834. int reg;
  1835. u32 val;
  1836. if (!intel_crtc->primary_enabled)
  1837. return;
  1838. intel_crtc->primary_enabled = false;
  1839. reg = DSPCNTR(plane);
  1840. val = I915_READ(reg);
  1841. WARN_ON((val & DISPLAY_PLANE_ENABLE) == 0);
  1842. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1843. intel_flush_primary_plane(dev_priv, plane);
  1844. }
  1845. static bool need_vtd_wa(struct drm_device *dev)
  1846. {
  1847. #ifdef CONFIG_INTEL_IOMMU
  1848. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1849. return true;
  1850. #endif
  1851. return false;
  1852. }
  1853. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1854. {
  1855. int tile_height;
  1856. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1857. return ALIGN(height, tile_height);
  1858. }
  1859. int
  1860. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1861. struct drm_i915_gem_object *obj,
  1862. struct intel_engine_cs *pipelined)
  1863. {
  1864. struct drm_i915_private *dev_priv = dev->dev_private;
  1865. u32 alignment;
  1866. int ret;
  1867. switch (obj->tiling_mode) {
  1868. case I915_TILING_NONE:
  1869. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1870. alignment = 128 * 1024;
  1871. else if (INTEL_INFO(dev)->gen >= 4)
  1872. alignment = 4 * 1024;
  1873. else
  1874. alignment = 64 * 1024;
  1875. break;
  1876. case I915_TILING_X:
  1877. /* pin() will align the object as required by fence */
  1878. alignment = 0;
  1879. break;
  1880. case I915_TILING_Y:
  1881. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1882. return -EINVAL;
  1883. default:
  1884. BUG();
  1885. }
  1886. /* Note that the w/a also requires 64 PTE of padding following the
  1887. * bo. We currently fill all unused PTE with the shadow page and so
  1888. * we should always have valid PTE following the scanout preventing
  1889. * the VT-d warning.
  1890. */
  1891. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1892. alignment = 256 * 1024;
  1893. dev_priv->mm.interruptible = false;
  1894. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1895. if (ret)
  1896. goto err_interruptible;
  1897. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1898. * fence, whereas 965+ only requires a fence if using
  1899. * framebuffer compression. For simplicity, we always install
  1900. * a fence as the cost is not that onerous.
  1901. */
  1902. ret = i915_gem_object_get_fence(obj);
  1903. if (ret)
  1904. goto err_unpin;
  1905. i915_gem_object_pin_fence(obj);
  1906. dev_priv->mm.interruptible = true;
  1907. return 0;
  1908. err_unpin:
  1909. i915_gem_object_unpin_from_display_plane(obj);
  1910. err_interruptible:
  1911. dev_priv->mm.interruptible = true;
  1912. return ret;
  1913. }
  1914. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1915. {
  1916. i915_gem_object_unpin_fence(obj);
  1917. i915_gem_object_unpin_from_display_plane(obj);
  1918. }
  1919. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1920. * is assumed to be a power-of-two. */
  1921. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1922. unsigned int tiling_mode,
  1923. unsigned int cpp,
  1924. unsigned int pitch)
  1925. {
  1926. if (tiling_mode != I915_TILING_NONE) {
  1927. unsigned int tile_rows, tiles;
  1928. tile_rows = *y / 8;
  1929. *y %= 8;
  1930. tiles = *x / (512/cpp);
  1931. *x %= 512/cpp;
  1932. return tile_rows * pitch * 8 + tiles * 4096;
  1933. } else {
  1934. unsigned int offset;
  1935. offset = *y * pitch + *x * cpp;
  1936. *y = 0;
  1937. *x = (offset & 4095) / cpp;
  1938. return offset & -4096;
  1939. }
  1940. }
  1941. int intel_format_to_fourcc(int format)
  1942. {
  1943. switch (format) {
  1944. case DISPPLANE_8BPP:
  1945. return DRM_FORMAT_C8;
  1946. case DISPPLANE_BGRX555:
  1947. return DRM_FORMAT_XRGB1555;
  1948. case DISPPLANE_BGRX565:
  1949. return DRM_FORMAT_RGB565;
  1950. default:
  1951. case DISPPLANE_BGRX888:
  1952. return DRM_FORMAT_XRGB8888;
  1953. case DISPPLANE_RGBX888:
  1954. return DRM_FORMAT_XBGR8888;
  1955. case DISPPLANE_BGRX101010:
  1956. return DRM_FORMAT_XRGB2101010;
  1957. case DISPPLANE_RGBX101010:
  1958. return DRM_FORMAT_XBGR2101010;
  1959. }
  1960. }
  1961. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  1962. struct intel_plane_config *plane_config)
  1963. {
  1964. struct drm_device *dev = crtc->base.dev;
  1965. struct drm_i915_gem_object *obj = NULL;
  1966. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  1967. u32 base = plane_config->base;
  1968. if (plane_config->size == 0)
  1969. return false;
  1970. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  1971. plane_config->size);
  1972. if (!obj)
  1973. return false;
  1974. if (plane_config->tiled) {
  1975. obj->tiling_mode = I915_TILING_X;
  1976. obj->stride = crtc->base.primary->fb->pitches[0];
  1977. }
  1978. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  1979. mode_cmd.width = crtc->base.primary->fb->width;
  1980. mode_cmd.height = crtc->base.primary->fb->height;
  1981. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  1982. mutex_lock(&dev->struct_mutex);
  1983. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  1984. &mode_cmd, obj)) {
  1985. DRM_DEBUG_KMS("intel fb init failed\n");
  1986. goto out_unref_obj;
  1987. }
  1988. mutex_unlock(&dev->struct_mutex);
  1989. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  1990. return true;
  1991. out_unref_obj:
  1992. drm_gem_object_unreference(&obj->base);
  1993. mutex_unlock(&dev->struct_mutex);
  1994. return false;
  1995. }
  1996. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  1997. struct intel_plane_config *plane_config)
  1998. {
  1999. struct drm_device *dev = intel_crtc->base.dev;
  2000. struct drm_crtc *c;
  2001. struct intel_crtc *i;
  2002. struct intel_framebuffer *fb;
  2003. if (!intel_crtc->base.primary->fb)
  2004. return;
  2005. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2006. return;
  2007. kfree(intel_crtc->base.primary->fb);
  2008. intel_crtc->base.primary->fb = NULL;
  2009. /*
  2010. * Failed to alloc the obj, check to see if we should share
  2011. * an fb with another CRTC instead
  2012. */
  2013. for_each_crtc(dev, c) {
  2014. i = to_intel_crtc(c);
  2015. if (c == &intel_crtc->base)
  2016. continue;
  2017. if (!i->active || !c->primary->fb)
  2018. continue;
  2019. fb = to_intel_framebuffer(c->primary->fb);
  2020. if (i915_gem_obj_ggtt_offset(fb->obj) == plane_config->base) {
  2021. drm_framebuffer_reference(c->primary->fb);
  2022. intel_crtc->base.primary->fb = c->primary->fb;
  2023. break;
  2024. }
  2025. }
  2026. }
  2027. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2028. struct drm_framebuffer *fb,
  2029. int x, int y)
  2030. {
  2031. struct drm_device *dev = crtc->dev;
  2032. struct drm_i915_private *dev_priv = dev->dev_private;
  2033. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2034. struct intel_framebuffer *intel_fb;
  2035. struct drm_i915_gem_object *obj;
  2036. int plane = intel_crtc->plane;
  2037. unsigned long linear_offset;
  2038. u32 dspcntr;
  2039. u32 reg;
  2040. intel_fb = to_intel_framebuffer(fb);
  2041. obj = intel_fb->obj;
  2042. reg = DSPCNTR(plane);
  2043. dspcntr = I915_READ(reg);
  2044. /* Mask out pixel format bits in case we change it */
  2045. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  2046. switch (fb->pixel_format) {
  2047. case DRM_FORMAT_C8:
  2048. dspcntr |= DISPPLANE_8BPP;
  2049. break;
  2050. case DRM_FORMAT_XRGB1555:
  2051. case DRM_FORMAT_ARGB1555:
  2052. dspcntr |= DISPPLANE_BGRX555;
  2053. break;
  2054. case DRM_FORMAT_RGB565:
  2055. dspcntr |= DISPPLANE_BGRX565;
  2056. break;
  2057. case DRM_FORMAT_XRGB8888:
  2058. case DRM_FORMAT_ARGB8888:
  2059. dspcntr |= DISPPLANE_BGRX888;
  2060. break;
  2061. case DRM_FORMAT_XBGR8888:
  2062. case DRM_FORMAT_ABGR8888:
  2063. dspcntr |= DISPPLANE_RGBX888;
  2064. break;
  2065. case DRM_FORMAT_XRGB2101010:
  2066. case DRM_FORMAT_ARGB2101010:
  2067. dspcntr |= DISPPLANE_BGRX101010;
  2068. break;
  2069. case DRM_FORMAT_XBGR2101010:
  2070. case DRM_FORMAT_ABGR2101010:
  2071. dspcntr |= DISPPLANE_RGBX101010;
  2072. break;
  2073. default:
  2074. BUG();
  2075. }
  2076. if (INTEL_INFO(dev)->gen >= 4) {
  2077. if (obj->tiling_mode != I915_TILING_NONE)
  2078. dspcntr |= DISPPLANE_TILED;
  2079. else
  2080. dspcntr &= ~DISPPLANE_TILED;
  2081. }
  2082. if (IS_G4X(dev))
  2083. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2084. I915_WRITE(reg, dspcntr);
  2085. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  2086. if (INTEL_INFO(dev)->gen >= 4) {
  2087. intel_crtc->dspaddr_offset =
  2088. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2089. fb->bits_per_pixel / 8,
  2090. fb->pitches[0]);
  2091. linear_offset -= intel_crtc->dspaddr_offset;
  2092. } else {
  2093. intel_crtc->dspaddr_offset = linear_offset;
  2094. }
  2095. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2096. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2097. fb->pitches[0]);
  2098. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2099. if (INTEL_INFO(dev)->gen >= 4) {
  2100. I915_WRITE(DSPSURF(plane),
  2101. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2102. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2103. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2104. } else
  2105. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2106. POSTING_READ(reg);
  2107. }
  2108. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2109. struct drm_framebuffer *fb,
  2110. int x, int y)
  2111. {
  2112. struct drm_device *dev = crtc->dev;
  2113. struct drm_i915_private *dev_priv = dev->dev_private;
  2114. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2115. struct intel_framebuffer *intel_fb;
  2116. struct drm_i915_gem_object *obj;
  2117. int plane = intel_crtc->plane;
  2118. unsigned long linear_offset;
  2119. u32 dspcntr;
  2120. u32 reg;
  2121. intel_fb = to_intel_framebuffer(fb);
  2122. obj = intel_fb->obj;
  2123. reg = DSPCNTR(plane);
  2124. dspcntr = I915_READ(reg);
  2125. /* Mask out pixel format bits in case we change it */
  2126. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  2127. switch (fb->pixel_format) {
  2128. case DRM_FORMAT_C8:
  2129. dspcntr |= DISPPLANE_8BPP;
  2130. break;
  2131. case DRM_FORMAT_RGB565:
  2132. dspcntr |= DISPPLANE_BGRX565;
  2133. break;
  2134. case DRM_FORMAT_XRGB8888:
  2135. case DRM_FORMAT_ARGB8888:
  2136. dspcntr |= DISPPLANE_BGRX888;
  2137. break;
  2138. case DRM_FORMAT_XBGR8888:
  2139. case DRM_FORMAT_ABGR8888:
  2140. dspcntr |= DISPPLANE_RGBX888;
  2141. break;
  2142. case DRM_FORMAT_XRGB2101010:
  2143. case DRM_FORMAT_ARGB2101010:
  2144. dspcntr |= DISPPLANE_BGRX101010;
  2145. break;
  2146. case DRM_FORMAT_XBGR2101010:
  2147. case DRM_FORMAT_ABGR2101010:
  2148. dspcntr |= DISPPLANE_RGBX101010;
  2149. break;
  2150. default:
  2151. BUG();
  2152. }
  2153. if (obj->tiling_mode != I915_TILING_NONE)
  2154. dspcntr |= DISPPLANE_TILED;
  2155. else
  2156. dspcntr &= ~DISPPLANE_TILED;
  2157. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2158. dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
  2159. else
  2160. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2161. I915_WRITE(reg, dspcntr);
  2162. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  2163. intel_crtc->dspaddr_offset =
  2164. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2165. fb->bits_per_pixel / 8,
  2166. fb->pitches[0]);
  2167. linear_offset -= intel_crtc->dspaddr_offset;
  2168. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2169. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2170. fb->pitches[0]);
  2171. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2172. I915_WRITE(DSPSURF(plane),
  2173. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2174. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2175. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2176. } else {
  2177. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2178. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2179. }
  2180. POSTING_READ(reg);
  2181. }
  2182. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2183. static int
  2184. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2185. int x, int y, enum mode_set_atomic state)
  2186. {
  2187. struct drm_device *dev = crtc->dev;
  2188. struct drm_i915_private *dev_priv = dev->dev_private;
  2189. if (dev_priv->display.disable_fbc)
  2190. dev_priv->display.disable_fbc(dev);
  2191. intel_increase_pllclock(crtc);
  2192. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2193. return 0;
  2194. }
  2195. void intel_display_handle_reset(struct drm_device *dev)
  2196. {
  2197. struct drm_i915_private *dev_priv = dev->dev_private;
  2198. struct drm_crtc *crtc;
  2199. /*
  2200. * Flips in the rings have been nuked by the reset,
  2201. * so complete all pending flips so that user space
  2202. * will get its events and not get stuck.
  2203. *
  2204. * Also update the base address of all primary
  2205. * planes to the the last fb to make sure we're
  2206. * showing the correct fb after a reset.
  2207. *
  2208. * Need to make two loops over the crtcs so that we
  2209. * don't try to grab a crtc mutex before the
  2210. * pending_flip_queue really got woken up.
  2211. */
  2212. for_each_crtc(dev, crtc) {
  2213. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2214. enum plane plane = intel_crtc->plane;
  2215. intel_prepare_page_flip(dev, plane);
  2216. intel_finish_page_flip_plane(dev, plane);
  2217. }
  2218. for_each_crtc(dev, crtc) {
  2219. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2220. drm_modeset_lock(&crtc->mutex, NULL);
  2221. /*
  2222. * FIXME: Once we have proper support for primary planes (and
  2223. * disabling them without disabling the entire crtc) allow again
  2224. * a NULL crtc->primary->fb.
  2225. */
  2226. if (intel_crtc->active && crtc->primary->fb)
  2227. dev_priv->display.update_primary_plane(crtc,
  2228. crtc->primary->fb,
  2229. crtc->x,
  2230. crtc->y);
  2231. drm_modeset_unlock(&crtc->mutex);
  2232. }
  2233. }
  2234. static int
  2235. intel_finish_fb(struct drm_framebuffer *old_fb)
  2236. {
  2237. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  2238. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2239. bool was_interruptible = dev_priv->mm.interruptible;
  2240. int ret;
  2241. /* Big Hammer, we also need to ensure that any pending
  2242. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2243. * current scanout is retired before unpinning the old
  2244. * framebuffer.
  2245. *
  2246. * This should only fail upon a hung GPU, in which case we
  2247. * can safely continue.
  2248. */
  2249. dev_priv->mm.interruptible = false;
  2250. ret = i915_gem_object_finish_gpu(obj);
  2251. dev_priv->mm.interruptible = was_interruptible;
  2252. return ret;
  2253. }
  2254. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2255. {
  2256. struct drm_device *dev = crtc->dev;
  2257. struct drm_i915_private *dev_priv = dev->dev_private;
  2258. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2259. unsigned long flags;
  2260. bool pending;
  2261. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2262. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2263. return false;
  2264. spin_lock_irqsave(&dev->event_lock, flags);
  2265. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2266. spin_unlock_irqrestore(&dev->event_lock, flags);
  2267. return pending;
  2268. }
  2269. static int
  2270. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  2271. struct drm_framebuffer *fb)
  2272. {
  2273. struct drm_device *dev = crtc->dev;
  2274. struct drm_i915_private *dev_priv = dev->dev_private;
  2275. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2276. struct drm_framebuffer *old_fb;
  2277. int ret;
  2278. if (intel_crtc_has_pending_flip(crtc)) {
  2279. DRM_ERROR("pipe is still busy with an old pageflip\n");
  2280. return -EBUSY;
  2281. }
  2282. /* no fb bound */
  2283. if (!fb) {
  2284. DRM_ERROR("No FB bound\n");
  2285. return 0;
  2286. }
  2287. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  2288. DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
  2289. plane_name(intel_crtc->plane),
  2290. INTEL_INFO(dev)->num_pipes);
  2291. return -EINVAL;
  2292. }
  2293. mutex_lock(&dev->struct_mutex);
  2294. ret = intel_pin_and_fence_fb_obj(dev,
  2295. to_intel_framebuffer(fb)->obj,
  2296. NULL);
  2297. mutex_unlock(&dev->struct_mutex);
  2298. if (ret != 0) {
  2299. DRM_ERROR("pin & fence failed\n");
  2300. return ret;
  2301. }
  2302. /*
  2303. * Update pipe size and adjust fitter if needed: the reason for this is
  2304. * that in compute_mode_changes we check the native mode (not the pfit
  2305. * mode) to see if we can flip rather than do a full mode set. In the
  2306. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2307. * pfit state, we'll end up with a big fb scanned out into the wrong
  2308. * sized surface.
  2309. *
  2310. * To fix this properly, we need to hoist the checks up into
  2311. * compute_mode_changes (or above), check the actual pfit state and
  2312. * whether the platform allows pfit disable with pipe active, and only
  2313. * then update the pipesrc and pfit state, even on the flip path.
  2314. */
  2315. if (i915.fastboot) {
  2316. const struct drm_display_mode *adjusted_mode =
  2317. &intel_crtc->config.adjusted_mode;
  2318. I915_WRITE(PIPESRC(intel_crtc->pipe),
  2319. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2320. (adjusted_mode->crtc_vdisplay - 1));
  2321. if (!intel_crtc->config.pch_pfit.enabled &&
  2322. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2323. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2324. I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
  2325. I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
  2326. I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
  2327. }
  2328. intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2329. intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2330. }
  2331. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2332. old_fb = crtc->primary->fb;
  2333. crtc->primary->fb = fb;
  2334. crtc->x = x;
  2335. crtc->y = y;
  2336. if (old_fb) {
  2337. if (intel_crtc->active && old_fb != fb)
  2338. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2339. mutex_lock(&dev->struct_mutex);
  2340. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  2341. mutex_unlock(&dev->struct_mutex);
  2342. }
  2343. mutex_lock(&dev->struct_mutex);
  2344. intel_update_fbc(dev);
  2345. intel_edp_psr_update(dev);
  2346. mutex_unlock(&dev->struct_mutex);
  2347. return 0;
  2348. }
  2349. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2350. {
  2351. struct drm_device *dev = crtc->dev;
  2352. struct drm_i915_private *dev_priv = dev->dev_private;
  2353. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2354. int pipe = intel_crtc->pipe;
  2355. u32 reg, temp;
  2356. /* enable normal train */
  2357. reg = FDI_TX_CTL(pipe);
  2358. temp = I915_READ(reg);
  2359. if (IS_IVYBRIDGE(dev)) {
  2360. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2361. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2362. } else {
  2363. temp &= ~FDI_LINK_TRAIN_NONE;
  2364. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2365. }
  2366. I915_WRITE(reg, temp);
  2367. reg = FDI_RX_CTL(pipe);
  2368. temp = I915_READ(reg);
  2369. if (HAS_PCH_CPT(dev)) {
  2370. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2371. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2372. } else {
  2373. temp &= ~FDI_LINK_TRAIN_NONE;
  2374. temp |= FDI_LINK_TRAIN_NONE;
  2375. }
  2376. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2377. /* wait one idle pattern time */
  2378. POSTING_READ(reg);
  2379. udelay(1000);
  2380. /* IVB wants error correction enabled */
  2381. if (IS_IVYBRIDGE(dev))
  2382. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2383. FDI_FE_ERRC_ENABLE);
  2384. }
  2385. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2386. {
  2387. return crtc->base.enabled && crtc->active &&
  2388. crtc->config.has_pch_encoder;
  2389. }
  2390. static void ivb_modeset_global_resources(struct drm_device *dev)
  2391. {
  2392. struct drm_i915_private *dev_priv = dev->dev_private;
  2393. struct intel_crtc *pipe_B_crtc =
  2394. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2395. struct intel_crtc *pipe_C_crtc =
  2396. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2397. uint32_t temp;
  2398. /*
  2399. * When everything is off disable fdi C so that we could enable fdi B
  2400. * with all lanes. Note that we don't care about enabled pipes without
  2401. * an enabled pch encoder.
  2402. */
  2403. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2404. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2405. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2406. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2407. temp = I915_READ(SOUTH_CHICKEN1);
  2408. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2409. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2410. I915_WRITE(SOUTH_CHICKEN1, temp);
  2411. }
  2412. }
  2413. /* The FDI link training functions for ILK/Ibexpeak. */
  2414. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2415. {
  2416. struct drm_device *dev = crtc->dev;
  2417. struct drm_i915_private *dev_priv = dev->dev_private;
  2418. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2419. int pipe = intel_crtc->pipe;
  2420. u32 reg, temp, tries;
  2421. /* FDI needs bits from pipe first */
  2422. assert_pipe_enabled(dev_priv, pipe);
  2423. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2424. for train result */
  2425. reg = FDI_RX_IMR(pipe);
  2426. temp = I915_READ(reg);
  2427. temp &= ~FDI_RX_SYMBOL_LOCK;
  2428. temp &= ~FDI_RX_BIT_LOCK;
  2429. I915_WRITE(reg, temp);
  2430. I915_READ(reg);
  2431. udelay(150);
  2432. /* enable CPU FDI TX and PCH FDI RX */
  2433. reg = FDI_TX_CTL(pipe);
  2434. temp = I915_READ(reg);
  2435. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2436. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2437. temp &= ~FDI_LINK_TRAIN_NONE;
  2438. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2439. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2440. reg = FDI_RX_CTL(pipe);
  2441. temp = I915_READ(reg);
  2442. temp &= ~FDI_LINK_TRAIN_NONE;
  2443. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2444. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2445. POSTING_READ(reg);
  2446. udelay(150);
  2447. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2448. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2449. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2450. FDI_RX_PHASE_SYNC_POINTER_EN);
  2451. reg = FDI_RX_IIR(pipe);
  2452. for (tries = 0; tries < 5; tries++) {
  2453. temp = I915_READ(reg);
  2454. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2455. if ((temp & FDI_RX_BIT_LOCK)) {
  2456. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2457. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2458. break;
  2459. }
  2460. }
  2461. if (tries == 5)
  2462. DRM_ERROR("FDI train 1 fail!\n");
  2463. /* Train 2 */
  2464. reg = FDI_TX_CTL(pipe);
  2465. temp = I915_READ(reg);
  2466. temp &= ~FDI_LINK_TRAIN_NONE;
  2467. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2468. I915_WRITE(reg, temp);
  2469. reg = FDI_RX_CTL(pipe);
  2470. temp = I915_READ(reg);
  2471. temp &= ~FDI_LINK_TRAIN_NONE;
  2472. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2473. I915_WRITE(reg, temp);
  2474. POSTING_READ(reg);
  2475. udelay(150);
  2476. reg = FDI_RX_IIR(pipe);
  2477. for (tries = 0; tries < 5; tries++) {
  2478. temp = I915_READ(reg);
  2479. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2480. if (temp & FDI_RX_SYMBOL_LOCK) {
  2481. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2482. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2483. break;
  2484. }
  2485. }
  2486. if (tries == 5)
  2487. DRM_ERROR("FDI train 2 fail!\n");
  2488. DRM_DEBUG_KMS("FDI train done\n");
  2489. }
  2490. static const int snb_b_fdi_train_param[] = {
  2491. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2492. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2493. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2494. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2495. };
  2496. /* The FDI link training functions for SNB/Cougarpoint. */
  2497. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2498. {
  2499. struct drm_device *dev = crtc->dev;
  2500. struct drm_i915_private *dev_priv = dev->dev_private;
  2501. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2502. int pipe = intel_crtc->pipe;
  2503. u32 reg, temp, i, retry;
  2504. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2505. for train result */
  2506. reg = FDI_RX_IMR(pipe);
  2507. temp = I915_READ(reg);
  2508. temp &= ~FDI_RX_SYMBOL_LOCK;
  2509. temp &= ~FDI_RX_BIT_LOCK;
  2510. I915_WRITE(reg, temp);
  2511. POSTING_READ(reg);
  2512. udelay(150);
  2513. /* enable CPU FDI TX and PCH FDI RX */
  2514. reg = FDI_TX_CTL(pipe);
  2515. temp = I915_READ(reg);
  2516. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2517. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2518. temp &= ~FDI_LINK_TRAIN_NONE;
  2519. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2520. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2521. /* SNB-B */
  2522. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2523. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2524. I915_WRITE(FDI_RX_MISC(pipe),
  2525. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2526. reg = FDI_RX_CTL(pipe);
  2527. temp = I915_READ(reg);
  2528. if (HAS_PCH_CPT(dev)) {
  2529. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2530. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2531. } else {
  2532. temp &= ~FDI_LINK_TRAIN_NONE;
  2533. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2534. }
  2535. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2536. POSTING_READ(reg);
  2537. udelay(150);
  2538. for (i = 0; i < 4; i++) {
  2539. reg = FDI_TX_CTL(pipe);
  2540. temp = I915_READ(reg);
  2541. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2542. temp |= snb_b_fdi_train_param[i];
  2543. I915_WRITE(reg, temp);
  2544. POSTING_READ(reg);
  2545. udelay(500);
  2546. for (retry = 0; retry < 5; retry++) {
  2547. reg = FDI_RX_IIR(pipe);
  2548. temp = I915_READ(reg);
  2549. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2550. if (temp & FDI_RX_BIT_LOCK) {
  2551. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2552. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2553. break;
  2554. }
  2555. udelay(50);
  2556. }
  2557. if (retry < 5)
  2558. break;
  2559. }
  2560. if (i == 4)
  2561. DRM_ERROR("FDI train 1 fail!\n");
  2562. /* Train 2 */
  2563. reg = FDI_TX_CTL(pipe);
  2564. temp = I915_READ(reg);
  2565. temp &= ~FDI_LINK_TRAIN_NONE;
  2566. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2567. if (IS_GEN6(dev)) {
  2568. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2569. /* SNB-B */
  2570. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2571. }
  2572. I915_WRITE(reg, temp);
  2573. reg = FDI_RX_CTL(pipe);
  2574. temp = I915_READ(reg);
  2575. if (HAS_PCH_CPT(dev)) {
  2576. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2577. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2578. } else {
  2579. temp &= ~FDI_LINK_TRAIN_NONE;
  2580. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2581. }
  2582. I915_WRITE(reg, temp);
  2583. POSTING_READ(reg);
  2584. udelay(150);
  2585. for (i = 0; i < 4; i++) {
  2586. reg = FDI_TX_CTL(pipe);
  2587. temp = I915_READ(reg);
  2588. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2589. temp |= snb_b_fdi_train_param[i];
  2590. I915_WRITE(reg, temp);
  2591. POSTING_READ(reg);
  2592. udelay(500);
  2593. for (retry = 0; retry < 5; retry++) {
  2594. reg = FDI_RX_IIR(pipe);
  2595. temp = I915_READ(reg);
  2596. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2597. if (temp & FDI_RX_SYMBOL_LOCK) {
  2598. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2599. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2600. break;
  2601. }
  2602. udelay(50);
  2603. }
  2604. if (retry < 5)
  2605. break;
  2606. }
  2607. if (i == 4)
  2608. DRM_ERROR("FDI train 2 fail!\n");
  2609. DRM_DEBUG_KMS("FDI train done.\n");
  2610. }
  2611. /* Manual link training for Ivy Bridge A0 parts */
  2612. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2613. {
  2614. struct drm_device *dev = crtc->dev;
  2615. struct drm_i915_private *dev_priv = dev->dev_private;
  2616. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2617. int pipe = intel_crtc->pipe;
  2618. u32 reg, temp, i, j;
  2619. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2620. for train result */
  2621. reg = FDI_RX_IMR(pipe);
  2622. temp = I915_READ(reg);
  2623. temp &= ~FDI_RX_SYMBOL_LOCK;
  2624. temp &= ~FDI_RX_BIT_LOCK;
  2625. I915_WRITE(reg, temp);
  2626. POSTING_READ(reg);
  2627. udelay(150);
  2628. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2629. I915_READ(FDI_RX_IIR(pipe)));
  2630. /* Try each vswing and preemphasis setting twice before moving on */
  2631. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2632. /* disable first in case we need to retry */
  2633. reg = FDI_TX_CTL(pipe);
  2634. temp = I915_READ(reg);
  2635. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2636. temp &= ~FDI_TX_ENABLE;
  2637. I915_WRITE(reg, temp);
  2638. reg = FDI_RX_CTL(pipe);
  2639. temp = I915_READ(reg);
  2640. temp &= ~FDI_LINK_TRAIN_AUTO;
  2641. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2642. temp &= ~FDI_RX_ENABLE;
  2643. I915_WRITE(reg, temp);
  2644. /* enable CPU FDI TX and PCH FDI RX */
  2645. reg = FDI_TX_CTL(pipe);
  2646. temp = I915_READ(reg);
  2647. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2648. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2649. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2650. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2651. temp |= snb_b_fdi_train_param[j/2];
  2652. temp |= FDI_COMPOSITE_SYNC;
  2653. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2654. I915_WRITE(FDI_RX_MISC(pipe),
  2655. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2656. reg = FDI_RX_CTL(pipe);
  2657. temp = I915_READ(reg);
  2658. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2659. temp |= FDI_COMPOSITE_SYNC;
  2660. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2661. POSTING_READ(reg);
  2662. udelay(1); /* should be 0.5us */
  2663. for (i = 0; i < 4; i++) {
  2664. reg = FDI_RX_IIR(pipe);
  2665. temp = I915_READ(reg);
  2666. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2667. if (temp & FDI_RX_BIT_LOCK ||
  2668. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2669. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2670. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2671. i);
  2672. break;
  2673. }
  2674. udelay(1); /* should be 0.5us */
  2675. }
  2676. if (i == 4) {
  2677. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2678. continue;
  2679. }
  2680. /* Train 2 */
  2681. reg = FDI_TX_CTL(pipe);
  2682. temp = I915_READ(reg);
  2683. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2684. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2685. I915_WRITE(reg, temp);
  2686. reg = FDI_RX_CTL(pipe);
  2687. temp = I915_READ(reg);
  2688. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2689. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2690. I915_WRITE(reg, temp);
  2691. POSTING_READ(reg);
  2692. udelay(2); /* should be 1.5us */
  2693. for (i = 0; i < 4; i++) {
  2694. reg = FDI_RX_IIR(pipe);
  2695. temp = I915_READ(reg);
  2696. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2697. if (temp & FDI_RX_SYMBOL_LOCK ||
  2698. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2699. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2700. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2701. i);
  2702. goto train_done;
  2703. }
  2704. udelay(2); /* should be 1.5us */
  2705. }
  2706. if (i == 4)
  2707. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2708. }
  2709. train_done:
  2710. DRM_DEBUG_KMS("FDI train done.\n");
  2711. }
  2712. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2713. {
  2714. struct drm_device *dev = intel_crtc->base.dev;
  2715. struct drm_i915_private *dev_priv = dev->dev_private;
  2716. int pipe = intel_crtc->pipe;
  2717. u32 reg, temp;
  2718. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2719. reg = FDI_RX_CTL(pipe);
  2720. temp = I915_READ(reg);
  2721. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2722. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2723. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2724. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2725. POSTING_READ(reg);
  2726. udelay(200);
  2727. /* Switch from Rawclk to PCDclk */
  2728. temp = I915_READ(reg);
  2729. I915_WRITE(reg, temp | FDI_PCDCLK);
  2730. POSTING_READ(reg);
  2731. udelay(200);
  2732. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2733. reg = FDI_TX_CTL(pipe);
  2734. temp = I915_READ(reg);
  2735. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2736. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2737. POSTING_READ(reg);
  2738. udelay(100);
  2739. }
  2740. }
  2741. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2742. {
  2743. struct drm_device *dev = intel_crtc->base.dev;
  2744. struct drm_i915_private *dev_priv = dev->dev_private;
  2745. int pipe = intel_crtc->pipe;
  2746. u32 reg, temp;
  2747. /* Switch from PCDclk to Rawclk */
  2748. reg = FDI_RX_CTL(pipe);
  2749. temp = I915_READ(reg);
  2750. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2751. /* Disable CPU FDI TX PLL */
  2752. reg = FDI_TX_CTL(pipe);
  2753. temp = I915_READ(reg);
  2754. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2755. POSTING_READ(reg);
  2756. udelay(100);
  2757. reg = FDI_RX_CTL(pipe);
  2758. temp = I915_READ(reg);
  2759. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2760. /* Wait for the clocks to turn off. */
  2761. POSTING_READ(reg);
  2762. udelay(100);
  2763. }
  2764. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2765. {
  2766. struct drm_device *dev = crtc->dev;
  2767. struct drm_i915_private *dev_priv = dev->dev_private;
  2768. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2769. int pipe = intel_crtc->pipe;
  2770. u32 reg, temp;
  2771. /* disable CPU FDI tx and PCH FDI rx */
  2772. reg = FDI_TX_CTL(pipe);
  2773. temp = I915_READ(reg);
  2774. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2775. POSTING_READ(reg);
  2776. reg = FDI_RX_CTL(pipe);
  2777. temp = I915_READ(reg);
  2778. temp &= ~(0x7 << 16);
  2779. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2780. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2781. POSTING_READ(reg);
  2782. udelay(100);
  2783. /* Ironlake workaround, disable clock pointer after downing FDI */
  2784. if (HAS_PCH_IBX(dev))
  2785. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2786. /* still set train pattern 1 */
  2787. reg = FDI_TX_CTL(pipe);
  2788. temp = I915_READ(reg);
  2789. temp &= ~FDI_LINK_TRAIN_NONE;
  2790. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2791. I915_WRITE(reg, temp);
  2792. reg = FDI_RX_CTL(pipe);
  2793. temp = I915_READ(reg);
  2794. if (HAS_PCH_CPT(dev)) {
  2795. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2796. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2797. } else {
  2798. temp &= ~FDI_LINK_TRAIN_NONE;
  2799. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2800. }
  2801. /* BPC in FDI rx is consistent with that in PIPECONF */
  2802. temp &= ~(0x07 << 16);
  2803. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2804. I915_WRITE(reg, temp);
  2805. POSTING_READ(reg);
  2806. udelay(100);
  2807. }
  2808. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  2809. {
  2810. struct intel_crtc *crtc;
  2811. /* Note that we don't need to be called with mode_config.lock here
  2812. * as our list of CRTC objects is static for the lifetime of the
  2813. * device and so cannot disappear as we iterate. Similarly, we can
  2814. * happily treat the predicates as racy, atomic checks as userspace
  2815. * cannot claim and pin a new fb without at least acquring the
  2816. * struct_mutex and so serialising with us.
  2817. */
  2818. for_each_intel_crtc(dev, crtc) {
  2819. if (atomic_read(&crtc->unpin_work_count) == 0)
  2820. continue;
  2821. if (crtc->unpin_work)
  2822. intel_wait_for_vblank(dev, crtc->pipe);
  2823. return true;
  2824. }
  2825. return false;
  2826. }
  2827. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2828. {
  2829. struct drm_device *dev = crtc->dev;
  2830. struct drm_i915_private *dev_priv = dev->dev_private;
  2831. if (crtc->primary->fb == NULL)
  2832. return;
  2833. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  2834. WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  2835. !intel_crtc_has_pending_flip(crtc),
  2836. 60*HZ) == 0);
  2837. mutex_lock(&dev->struct_mutex);
  2838. intel_finish_fb(crtc->primary->fb);
  2839. mutex_unlock(&dev->struct_mutex);
  2840. }
  2841. /* Program iCLKIP clock to the desired frequency */
  2842. static void lpt_program_iclkip(struct drm_crtc *crtc)
  2843. {
  2844. struct drm_device *dev = crtc->dev;
  2845. struct drm_i915_private *dev_priv = dev->dev_private;
  2846. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  2847. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  2848. u32 temp;
  2849. mutex_lock(&dev_priv->dpio_lock);
  2850. /* It is necessary to ungate the pixclk gate prior to programming
  2851. * the divisors, and gate it back when it is done.
  2852. */
  2853. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  2854. /* Disable SSCCTL */
  2855. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  2856. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  2857. SBI_SSCCTL_DISABLE,
  2858. SBI_ICLK);
  2859. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  2860. if (clock == 20000) {
  2861. auxdiv = 1;
  2862. divsel = 0x41;
  2863. phaseinc = 0x20;
  2864. } else {
  2865. /* The iCLK virtual clock root frequency is in MHz,
  2866. * but the adjusted_mode->crtc_clock in in KHz. To get the
  2867. * divisors, it is necessary to divide one by another, so we
  2868. * convert the virtual clock precision to KHz here for higher
  2869. * precision.
  2870. */
  2871. u32 iclk_virtual_root_freq = 172800 * 1000;
  2872. u32 iclk_pi_range = 64;
  2873. u32 desired_divisor, msb_divisor_value, pi_value;
  2874. desired_divisor = (iclk_virtual_root_freq / clock);
  2875. msb_divisor_value = desired_divisor / iclk_pi_range;
  2876. pi_value = desired_divisor % iclk_pi_range;
  2877. auxdiv = 0;
  2878. divsel = msb_divisor_value - 2;
  2879. phaseinc = pi_value;
  2880. }
  2881. /* This should not happen with any sane values */
  2882. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  2883. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  2884. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  2885. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  2886. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  2887. clock,
  2888. auxdiv,
  2889. divsel,
  2890. phasedir,
  2891. phaseinc);
  2892. /* Program SSCDIVINTPHASE6 */
  2893. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  2894. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  2895. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  2896. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  2897. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  2898. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  2899. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  2900. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  2901. /* Program SSCAUXDIV */
  2902. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  2903. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  2904. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  2905. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  2906. /* Enable modulator and associated divider */
  2907. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  2908. temp &= ~SBI_SSCCTL_DISABLE;
  2909. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  2910. /* Wait for initialization time */
  2911. udelay(24);
  2912. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  2913. mutex_unlock(&dev_priv->dpio_lock);
  2914. }
  2915. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  2916. enum pipe pch_transcoder)
  2917. {
  2918. struct drm_device *dev = crtc->base.dev;
  2919. struct drm_i915_private *dev_priv = dev->dev_private;
  2920. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  2921. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  2922. I915_READ(HTOTAL(cpu_transcoder)));
  2923. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  2924. I915_READ(HBLANK(cpu_transcoder)));
  2925. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  2926. I915_READ(HSYNC(cpu_transcoder)));
  2927. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  2928. I915_READ(VTOTAL(cpu_transcoder)));
  2929. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  2930. I915_READ(VBLANK(cpu_transcoder)));
  2931. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  2932. I915_READ(VSYNC(cpu_transcoder)));
  2933. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  2934. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  2935. }
  2936. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  2937. {
  2938. struct drm_i915_private *dev_priv = dev->dev_private;
  2939. uint32_t temp;
  2940. temp = I915_READ(SOUTH_CHICKEN1);
  2941. if (temp & FDI_BC_BIFURCATION_SELECT)
  2942. return;
  2943. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2944. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2945. temp |= FDI_BC_BIFURCATION_SELECT;
  2946. DRM_DEBUG_KMS("enabling fdi C rx\n");
  2947. I915_WRITE(SOUTH_CHICKEN1, temp);
  2948. POSTING_READ(SOUTH_CHICKEN1);
  2949. }
  2950. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  2951. {
  2952. struct drm_device *dev = intel_crtc->base.dev;
  2953. struct drm_i915_private *dev_priv = dev->dev_private;
  2954. switch (intel_crtc->pipe) {
  2955. case PIPE_A:
  2956. break;
  2957. case PIPE_B:
  2958. if (intel_crtc->config.fdi_lanes > 2)
  2959. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  2960. else
  2961. cpt_enable_fdi_bc_bifurcation(dev);
  2962. break;
  2963. case PIPE_C:
  2964. cpt_enable_fdi_bc_bifurcation(dev);
  2965. break;
  2966. default:
  2967. BUG();
  2968. }
  2969. }
  2970. /*
  2971. * Enable PCH resources required for PCH ports:
  2972. * - PCH PLLs
  2973. * - FDI training & RX/TX
  2974. * - update transcoder timings
  2975. * - DP transcoding bits
  2976. * - transcoder
  2977. */
  2978. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2979. {
  2980. struct drm_device *dev = crtc->dev;
  2981. struct drm_i915_private *dev_priv = dev->dev_private;
  2982. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2983. int pipe = intel_crtc->pipe;
  2984. u32 reg, temp;
  2985. assert_pch_transcoder_disabled(dev_priv, pipe);
  2986. if (IS_IVYBRIDGE(dev))
  2987. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  2988. /* Write the TU size bits before fdi link training, so that error
  2989. * detection works. */
  2990. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2991. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2992. /* For PCH output, training FDI link */
  2993. dev_priv->display.fdi_link_train(crtc);
  2994. /* We need to program the right clock selection before writing the pixel
  2995. * mutliplier into the DPLL. */
  2996. if (HAS_PCH_CPT(dev)) {
  2997. u32 sel;
  2998. temp = I915_READ(PCH_DPLL_SEL);
  2999. temp |= TRANS_DPLL_ENABLE(pipe);
  3000. sel = TRANS_DPLLB_SEL(pipe);
  3001. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  3002. temp |= sel;
  3003. else
  3004. temp &= ~sel;
  3005. I915_WRITE(PCH_DPLL_SEL, temp);
  3006. }
  3007. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3008. * transcoder, and we actually should do this to not upset any PCH
  3009. * transcoder that already use the clock when we share it.
  3010. *
  3011. * Note that enable_shared_dpll tries to do the right thing, but
  3012. * get_shared_dpll unconditionally resets the pll - we need that to have
  3013. * the right LVDS enable sequence. */
  3014. intel_enable_shared_dpll(intel_crtc);
  3015. /* set transcoder timing, panel must allow it */
  3016. assert_panel_unlocked(dev_priv, pipe);
  3017. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3018. intel_fdi_normal_train(crtc);
  3019. /* For PCH DP, enable TRANS_DP_CTL */
  3020. if (HAS_PCH_CPT(dev) &&
  3021. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  3022. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  3023. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3024. reg = TRANS_DP_CTL(pipe);
  3025. temp = I915_READ(reg);
  3026. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3027. TRANS_DP_SYNC_MASK |
  3028. TRANS_DP_BPC_MASK);
  3029. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3030. TRANS_DP_ENH_FRAMING);
  3031. temp |= bpc << 9; /* same format but at 11:9 */
  3032. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3033. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3034. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3035. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3036. switch (intel_trans_dp_port_sel(crtc)) {
  3037. case PCH_DP_B:
  3038. temp |= TRANS_DP_PORT_SEL_B;
  3039. break;
  3040. case PCH_DP_C:
  3041. temp |= TRANS_DP_PORT_SEL_C;
  3042. break;
  3043. case PCH_DP_D:
  3044. temp |= TRANS_DP_PORT_SEL_D;
  3045. break;
  3046. default:
  3047. BUG();
  3048. }
  3049. I915_WRITE(reg, temp);
  3050. }
  3051. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3052. }
  3053. static void lpt_pch_enable(struct drm_crtc *crtc)
  3054. {
  3055. struct drm_device *dev = crtc->dev;
  3056. struct drm_i915_private *dev_priv = dev->dev_private;
  3057. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3058. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3059. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3060. lpt_program_iclkip(crtc);
  3061. /* Set transcoder timing. */
  3062. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3063. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3064. }
  3065. static void intel_put_shared_dpll(struct intel_crtc *crtc)
  3066. {
  3067. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3068. if (pll == NULL)
  3069. return;
  3070. if (pll->refcount == 0) {
  3071. WARN(1, "bad %s refcount\n", pll->name);
  3072. return;
  3073. }
  3074. if (--pll->refcount == 0) {
  3075. WARN_ON(pll->on);
  3076. WARN_ON(pll->active);
  3077. }
  3078. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3079. }
  3080. static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3081. {
  3082. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3083. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3084. enum intel_dpll_id i;
  3085. if (pll) {
  3086. DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
  3087. crtc->base.base.id, pll->name);
  3088. intel_put_shared_dpll(crtc);
  3089. }
  3090. if (HAS_PCH_IBX(dev_priv->dev)) {
  3091. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3092. i = (enum intel_dpll_id) crtc->pipe;
  3093. pll = &dev_priv->shared_dplls[i];
  3094. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3095. crtc->base.base.id, pll->name);
  3096. WARN_ON(pll->refcount);
  3097. goto found;
  3098. }
  3099. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3100. pll = &dev_priv->shared_dplls[i];
  3101. /* Only want to check enabled timings first */
  3102. if (pll->refcount == 0)
  3103. continue;
  3104. if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
  3105. sizeof(pll->hw_state)) == 0) {
  3106. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
  3107. crtc->base.base.id,
  3108. pll->name, pll->refcount, pll->active);
  3109. goto found;
  3110. }
  3111. }
  3112. /* Ok no matching timings, maybe there's a free one? */
  3113. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3114. pll = &dev_priv->shared_dplls[i];
  3115. if (pll->refcount == 0) {
  3116. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3117. crtc->base.base.id, pll->name);
  3118. goto found;
  3119. }
  3120. }
  3121. return NULL;
  3122. found:
  3123. if (pll->refcount == 0)
  3124. pll->hw_state = crtc->config.dpll_hw_state;
  3125. crtc->config.shared_dpll = i;
  3126. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3127. pipe_name(crtc->pipe));
  3128. pll->refcount++;
  3129. return pll;
  3130. }
  3131. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3132. {
  3133. struct drm_i915_private *dev_priv = dev->dev_private;
  3134. int dslreg = PIPEDSL(pipe);
  3135. u32 temp;
  3136. temp = I915_READ(dslreg);
  3137. udelay(500);
  3138. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3139. if (wait_for(I915_READ(dslreg) != temp, 5))
  3140. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3141. }
  3142. }
  3143. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3144. {
  3145. struct drm_device *dev = crtc->base.dev;
  3146. struct drm_i915_private *dev_priv = dev->dev_private;
  3147. int pipe = crtc->pipe;
  3148. if (crtc->config.pch_pfit.enabled) {
  3149. /* Force use of hard-coded filter coefficients
  3150. * as some pre-programmed values are broken,
  3151. * e.g. x201.
  3152. */
  3153. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3154. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3155. PF_PIPE_SEL_IVB(pipe));
  3156. else
  3157. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3158. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3159. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3160. }
  3161. }
  3162. static void intel_enable_planes(struct drm_crtc *crtc)
  3163. {
  3164. struct drm_device *dev = crtc->dev;
  3165. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3166. struct drm_plane *plane;
  3167. struct intel_plane *intel_plane;
  3168. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3169. intel_plane = to_intel_plane(plane);
  3170. if (intel_plane->pipe == pipe)
  3171. intel_plane_restore(&intel_plane->base);
  3172. }
  3173. }
  3174. static void intel_disable_planes(struct drm_crtc *crtc)
  3175. {
  3176. struct drm_device *dev = crtc->dev;
  3177. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3178. struct drm_plane *plane;
  3179. struct intel_plane *intel_plane;
  3180. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3181. intel_plane = to_intel_plane(plane);
  3182. if (intel_plane->pipe == pipe)
  3183. intel_plane_disable(&intel_plane->base);
  3184. }
  3185. }
  3186. void hsw_enable_ips(struct intel_crtc *crtc)
  3187. {
  3188. struct drm_device *dev = crtc->base.dev;
  3189. struct drm_i915_private *dev_priv = dev->dev_private;
  3190. if (!crtc->config.ips_enabled)
  3191. return;
  3192. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3193. intel_wait_for_vblank(dev, crtc->pipe);
  3194. assert_plane_enabled(dev_priv, crtc->plane);
  3195. if (IS_BROADWELL(dev)) {
  3196. mutex_lock(&dev_priv->rps.hw_lock);
  3197. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3198. mutex_unlock(&dev_priv->rps.hw_lock);
  3199. /* Quoting Art Runyan: "its not safe to expect any particular
  3200. * value in IPS_CTL bit 31 after enabling IPS through the
  3201. * mailbox." Moreover, the mailbox may return a bogus state,
  3202. * so we need to just enable it and continue on.
  3203. */
  3204. } else {
  3205. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3206. /* The bit only becomes 1 in the next vblank, so this wait here
  3207. * is essentially intel_wait_for_vblank. If we don't have this
  3208. * and don't wait for vblanks until the end of crtc_enable, then
  3209. * the HW state readout code will complain that the expected
  3210. * IPS_CTL value is not the one we read. */
  3211. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3212. DRM_ERROR("Timed out waiting for IPS enable\n");
  3213. }
  3214. }
  3215. void hsw_disable_ips(struct intel_crtc *crtc)
  3216. {
  3217. struct drm_device *dev = crtc->base.dev;
  3218. struct drm_i915_private *dev_priv = dev->dev_private;
  3219. if (!crtc->config.ips_enabled)
  3220. return;
  3221. assert_plane_enabled(dev_priv, crtc->plane);
  3222. if (IS_BROADWELL(dev)) {
  3223. mutex_lock(&dev_priv->rps.hw_lock);
  3224. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3225. mutex_unlock(&dev_priv->rps.hw_lock);
  3226. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3227. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3228. DRM_ERROR("Timed out waiting for IPS disable\n");
  3229. } else {
  3230. I915_WRITE(IPS_CTL, 0);
  3231. POSTING_READ(IPS_CTL);
  3232. }
  3233. /* We need to wait for a vblank before we can disable the plane. */
  3234. intel_wait_for_vblank(dev, crtc->pipe);
  3235. }
  3236. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3237. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3238. {
  3239. struct drm_device *dev = crtc->dev;
  3240. struct drm_i915_private *dev_priv = dev->dev_private;
  3241. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3242. enum pipe pipe = intel_crtc->pipe;
  3243. int palreg = PALETTE(pipe);
  3244. int i;
  3245. bool reenable_ips = false;
  3246. /* The clocks have to be on to load the palette. */
  3247. if (!crtc->enabled || !intel_crtc->active)
  3248. return;
  3249. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3250. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  3251. assert_dsi_pll_enabled(dev_priv);
  3252. else
  3253. assert_pll_enabled(dev_priv, pipe);
  3254. }
  3255. /* use legacy palette for Ironlake */
  3256. if (HAS_PCH_SPLIT(dev))
  3257. palreg = LGC_PALETTE(pipe);
  3258. /* Workaround : Do not read or write the pipe palette/gamma data while
  3259. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3260. */
  3261. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3262. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3263. GAMMA_MODE_MODE_SPLIT)) {
  3264. hsw_disable_ips(intel_crtc);
  3265. reenable_ips = true;
  3266. }
  3267. for (i = 0; i < 256; i++) {
  3268. I915_WRITE(palreg + 4 * i,
  3269. (intel_crtc->lut_r[i] << 16) |
  3270. (intel_crtc->lut_g[i] << 8) |
  3271. intel_crtc->lut_b[i]);
  3272. }
  3273. if (reenable_ips)
  3274. hsw_enable_ips(intel_crtc);
  3275. }
  3276. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3277. {
  3278. if (!enable && intel_crtc->overlay) {
  3279. struct drm_device *dev = intel_crtc->base.dev;
  3280. struct drm_i915_private *dev_priv = dev->dev_private;
  3281. mutex_lock(&dev->struct_mutex);
  3282. dev_priv->mm.interruptible = false;
  3283. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3284. dev_priv->mm.interruptible = true;
  3285. mutex_unlock(&dev->struct_mutex);
  3286. }
  3287. /* Let userspace switch the overlay on again. In most cases userspace
  3288. * has to recompute where to put it anyway.
  3289. */
  3290. }
  3291. /**
  3292. * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
  3293. * cursor plane briefly if not already running after enabling the display
  3294. * plane.
  3295. * This workaround avoids occasional blank screens when self refresh is
  3296. * enabled.
  3297. */
  3298. static void
  3299. g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
  3300. {
  3301. u32 cntl = I915_READ(CURCNTR(pipe));
  3302. if ((cntl & CURSOR_MODE) == 0) {
  3303. u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
  3304. I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
  3305. I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
  3306. intel_wait_for_vblank(dev_priv->dev, pipe);
  3307. I915_WRITE(CURCNTR(pipe), cntl);
  3308. I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
  3309. I915_WRITE(FW_BLC_SELF, fw_bcl_self);
  3310. }
  3311. }
  3312. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3313. {
  3314. struct drm_device *dev = crtc->dev;
  3315. struct drm_i915_private *dev_priv = dev->dev_private;
  3316. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3317. int pipe = intel_crtc->pipe;
  3318. int plane = intel_crtc->plane;
  3319. intel_enable_primary_hw_plane(dev_priv, plane, pipe);
  3320. intel_enable_planes(crtc);
  3321. /* The fixup needs to happen before cursor is enabled */
  3322. if (IS_G4X(dev))
  3323. g4x_fixup_plane(dev_priv, pipe);
  3324. intel_crtc_update_cursor(crtc, true);
  3325. intel_crtc_dpms_overlay(intel_crtc, true);
  3326. hsw_enable_ips(intel_crtc);
  3327. mutex_lock(&dev->struct_mutex);
  3328. intel_update_fbc(dev);
  3329. intel_edp_psr_update(dev);
  3330. mutex_unlock(&dev->struct_mutex);
  3331. }
  3332. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3333. {
  3334. struct drm_device *dev = crtc->dev;
  3335. struct drm_i915_private *dev_priv = dev->dev_private;
  3336. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3337. int pipe = intel_crtc->pipe;
  3338. int plane = intel_crtc->plane;
  3339. intel_crtc_wait_for_pending_flips(crtc);
  3340. drm_crtc_vblank_off(crtc);
  3341. if (dev_priv->fbc.plane == plane)
  3342. intel_disable_fbc(dev);
  3343. hsw_disable_ips(intel_crtc);
  3344. intel_crtc_dpms_overlay(intel_crtc, false);
  3345. intel_crtc_update_cursor(crtc, false);
  3346. intel_disable_planes(crtc);
  3347. intel_disable_primary_hw_plane(dev_priv, plane, pipe);
  3348. }
  3349. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3350. {
  3351. struct drm_device *dev = crtc->dev;
  3352. struct drm_i915_private *dev_priv = dev->dev_private;
  3353. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3354. struct intel_encoder *encoder;
  3355. int pipe = intel_crtc->pipe;
  3356. enum plane plane = intel_crtc->plane;
  3357. WARN_ON(!crtc->enabled);
  3358. if (intel_crtc->active)
  3359. return;
  3360. if (intel_crtc->config.has_pch_encoder)
  3361. intel_prepare_shared_dpll(intel_crtc);
  3362. if (intel_crtc->config.has_dp_encoder)
  3363. intel_dp_set_m_n(intel_crtc);
  3364. intel_set_pipe_timings(intel_crtc);
  3365. if (intel_crtc->config.has_pch_encoder) {
  3366. intel_cpu_transcoder_set_m_n(intel_crtc,
  3367. &intel_crtc->config.fdi_m_n);
  3368. }
  3369. ironlake_set_pipeconf(crtc);
  3370. /* Set up the display plane register */
  3371. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
  3372. POSTING_READ(DSPCNTR(plane));
  3373. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3374. crtc->x, crtc->y);
  3375. intel_crtc->active = true;
  3376. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3377. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3378. for_each_encoder_on_crtc(dev, crtc, encoder)
  3379. if (encoder->pre_enable)
  3380. encoder->pre_enable(encoder);
  3381. if (intel_crtc->config.has_pch_encoder) {
  3382. /* Note: FDI PLL enabling _must_ be done before we enable the
  3383. * cpu pipes, hence this is separate from all the other fdi/pch
  3384. * enabling. */
  3385. ironlake_fdi_pll_enable(intel_crtc);
  3386. } else {
  3387. assert_fdi_tx_disabled(dev_priv, pipe);
  3388. assert_fdi_rx_disabled(dev_priv, pipe);
  3389. }
  3390. ironlake_pfit_enable(intel_crtc);
  3391. /*
  3392. * On ILK+ LUT must be loaded before the pipe is running but with
  3393. * clocks enabled
  3394. */
  3395. intel_crtc_load_lut(crtc);
  3396. intel_update_watermarks(crtc);
  3397. intel_enable_pipe(intel_crtc);
  3398. if (intel_crtc->config.has_pch_encoder)
  3399. ironlake_pch_enable(crtc);
  3400. for_each_encoder_on_crtc(dev, crtc, encoder)
  3401. encoder->enable(encoder);
  3402. if (HAS_PCH_CPT(dev))
  3403. cpt_verify_modeset(dev, intel_crtc->pipe);
  3404. intel_crtc_enable_planes(crtc);
  3405. drm_crtc_vblank_on(crtc);
  3406. }
  3407. /* IPS only exists on ULT machines and is tied to pipe A. */
  3408. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3409. {
  3410. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3411. }
  3412. /*
  3413. * This implements the workaround described in the "notes" section of the mode
  3414. * set sequence documentation. When going from no pipes or single pipe to
  3415. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3416. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3417. */
  3418. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3419. {
  3420. struct drm_device *dev = crtc->base.dev;
  3421. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3422. /* We want to get the other_active_crtc only if there's only 1 other
  3423. * active crtc. */
  3424. for_each_intel_crtc(dev, crtc_it) {
  3425. if (!crtc_it->active || crtc_it == crtc)
  3426. continue;
  3427. if (other_active_crtc)
  3428. return;
  3429. other_active_crtc = crtc_it;
  3430. }
  3431. if (!other_active_crtc)
  3432. return;
  3433. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3434. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3435. }
  3436. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3437. {
  3438. struct drm_device *dev = crtc->dev;
  3439. struct drm_i915_private *dev_priv = dev->dev_private;
  3440. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3441. struct intel_encoder *encoder;
  3442. int pipe = intel_crtc->pipe;
  3443. enum plane plane = intel_crtc->plane;
  3444. WARN_ON(!crtc->enabled);
  3445. if (intel_crtc->active)
  3446. return;
  3447. if (intel_crtc->config.has_dp_encoder)
  3448. intel_dp_set_m_n(intel_crtc);
  3449. intel_set_pipe_timings(intel_crtc);
  3450. if (intel_crtc->config.has_pch_encoder) {
  3451. intel_cpu_transcoder_set_m_n(intel_crtc,
  3452. &intel_crtc->config.fdi_m_n);
  3453. }
  3454. haswell_set_pipeconf(crtc);
  3455. intel_set_pipe_csc(crtc);
  3456. /* Set up the display plane register */
  3457. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
  3458. POSTING_READ(DSPCNTR(plane));
  3459. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3460. crtc->x, crtc->y);
  3461. intel_crtc->active = true;
  3462. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3463. if (intel_crtc->config.has_pch_encoder)
  3464. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3465. if (intel_crtc->config.has_pch_encoder)
  3466. dev_priv->display.fdi_link_train(crtc);
  3467. for_each_encoder_on_crtc(dev, crtc, encoder)
  3468. if (encoder->pre_enable)
  3469. encoder->pre_enable(encoder);
  3470. intel_ddi_enable_pipe_clock(intel_crtc);
  3471. ironlake_pfit_enable(intel_crtc);
  3472. /*
  3473. * On ILK+ LUT must be loaded before the pipe is running but with
  3474. * clocks enabled
  3475. */
  3476. intel_crtc_load_lut(crtc);
  3477. intel_ddi_set_pipe_settings(crtc);
  3478. intel_ddi_enable_transcoder_func(crtc);
  3479. intel_update_watermarks(crtc);
  3480. intel_enable_pipe(intel_crtc);
  3481. if (intel_crtc->config.has_pch_encoder)
  3482. lpt_pch_enable(crtc);
  3483. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3484. encoder->enable(encoder);
  3485. intel_opregion_notify_encoder(encoder, true);
  3486. }
  3487. /* If we change the relative order between pipe/planes enabling, we need
  3488. * to change the workaround. */
  3489. haswell_mode_set_planes_workaround(intel_crtc);
  3490. intel_crtc_enable_planes(crtc);
  3491. drm_crtc_vblank_on(crtc);
  3492. }
  3493. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3494. {
  3495. struct drm_device *dev = crtc->base.dev;
  3496. struct drm_i915_private *dev_priv = dev->dev_private;
  3497. int pipe = crtc->pipe;
  3498. /* To avoid upsetting the power well on haswell only disable the pfit if
  3499. * it's in use. The hw state code will make sure we get this right. */
  3500. if (crtc->config.pch_pfit.enabled) {
  3501. I915_WRITE(PF_CTL(pipe), 0);
  3502. I915_WRITE(PF_WIN_POS(pipe), 0);
  3503. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3504. }
  3505. }
  3506. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3507. {
  3508. struct drm_device *dev = crtc->dev;
  3509. struct drm_i915_private *dev_priv = dev->dev_private;
  3510. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3511. struct intel_encoder *encoder;
  3512. int pipe = intel_crtc->pipe;
  3513. u32 reg, temp;
  3514. if (!intel_crtc->active)
  3515. return;
  3516. intel_crtc_disable_planes(crtc);
  3517. for_each_encoder_on_crtc(dev, crtc, encoder)
  3518. encoder->disable(encoder);
  3519. if (intel_crtc->config.has_pch_encoder)
  3520. intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
  3521. intel_disable_pipe(dev_priv, pipe);
  3522. ironlake_pfit_disable(intel_crtc);
  3523. for_each_encoder_on_crtc(dev, crtc, encoder)
  3524. if (encoder->post_disable)
  3525. encoder->post_disable(encoder);
  3526. if (intel_crtc->config.has_pch_encoder) {
  3527. ironlake_fdi_disable(crtc);
  3528. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3529. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3530. if (HAS_PCH_CPT(dev)) {
  3531. /* disable TRANS_DP_CTL */
  3532. reg = TRANS_DP_CTL(pipe);
  3533. temp = I915_READ(reg);
  3534. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3535. TRANS_DP_PORT_SEL_MASK);
  3536. temp |= TRANS_DP_PORT_SEL_NONE;
  3537. I915_WRITE(reg, temp);
  3538. /* disable DPLL_SEL */
  3539. temp = I915_READ(PCH_DPLL_SEL);
  3540. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3541. I915_WRITE(PCH_DPLL_SEL, temp);
  3542. }
  3543. /* disable PCH DPLL */
  3544. intel_disable_shared_dpll(intel_crtc);
  3545. ironlake_fdi_pll_disable(intel_crtc);
  3546. }
  3547. intel_crtc->active = false;
  3548. intel_update_watermarks(crtc);
  3549. mutex_lock(&dev->struct_mutex);
  3550. intel_update_fbc(dev);
  3551. intel_edp_psr_update(dev);
  3552. mutex_unlock(&dev->struct_mutex);
  3553. }
  3554. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3555. {
  3556. struct drm_device *dev = crtc->dev;
  3557. struct drm_i915_private *dev_priv = dev->dev_private;
  3558. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3559. struct intel_encoder *encoder;
  3560. int pipe = intel_crtc->pipe;
  3561. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3562. if (!intel_crtc->active)
  3563. return;
  3564. intel_crtc_disable_planes(crtc);
  3565. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3566. intel_opregion_notify_encoder(encoder, false);
  3567. encoder->disable(encoder);
  3568. }
  3569. if (intel_crtc->config.has_pch_encoder)
  3570. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
  3571. intel_disable_pipe(dev_priv, pipe);
  3572. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3573. ironlake_pfit_disable(intel_crtc);
  3574. intel_ddi_disable_pipe_clock(intel_crtc);
  3575. for_each_encoder_on_crtc(dev, crtc, encoder)
  3576. if (encoder->post_disable)
  3577. encoder->post_disable(encoder);
  3578. if (intel_crtc->config.has_pch_encoder) {
  3579. lpt_disable_pch_transcoder(dev_priv);
  3580. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3581. intel_ddi_fdi_disable(crtc);
  3582. }
  3583. intel_crtc->active = false;
  3584. intel_update_watermarks(crtc);
  3585. mutex_lock(&dev->struct_mutex);
  3586. intel_update_fbc(dev);
  3587. intel_edp_psr_update(dev);
  3588. mutex_unlock(&dev->struct_mutex);
  3589. }
  3590. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3591. {
  3592. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3593. intel_put_shared_dpll(intel_crtc);
  3594. }
  3595. static void haswell_crtc_off(struct drm_crtc *crtc)
  3596. {
  3597. intel_ddi_put_crtc_pll(crtc);
  3598. }
  3599. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3600. {
  3601. struct drm_device *dev = crtc->base.dev;
  3602. struct drm_i915_private *dev_priv = dev->dev_private;
  3603. struct intel_crtc_config *pipe_config = &crtc->config;
  3604. if (!crtc->config.gmch_pfit.control)
  3605. return;
  3606. /*
  3607. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3608. * according to register description and PRM.
  3609. */
  3610. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3611. assert_pipe_disabled(dev_priv, crtc->pipe);
  3612. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3613. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3614. /* Border color in case we don't scale up to the full screen. Black by
  3615. * default, change to something else for debugging. */
  3616. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3617. }
  3618. #define for_each_power_domain(domain, mask) \
  3619. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3620. if ((1 << (domain)) & (mask))
  3621. enum intel_display_power_domain
  3622. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3623. {
  3624. struct drm_device *dev = intel_encoder->base.dev;
  3625. struct intel_digital_port *intel_dig_port;
  3626. switch (intel_encoder->type) {
  3627. case INTEL_OUTPUT_UNKNOWN:
  3628. /* Only DDI platforms should ever use this output type */
  3629. WARN_ON_ONCE(!HAS_DDI(dev));
  3630. case INTEL_OUTPUT_DISPLAYPORT:
  3631. case INTEL_OUTPUT_HDMI:
  3632. case INTEL_OUTPUT_EDP:
  3633. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3634. switch (intel_dig_port->port) {
  3635. case PORT_A:
  3636. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3637. case PORT_B:
  3638. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3639. case PORT_C:
  3640. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3641. case PORT_D:
  3642. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3643. default:
  3644. WARN_ON_ONCE(1);
  3645. return POWER_DOMAIN_PORT_OTHER;
  3646. }
  3647. case INTEL_OUTPUT_ANALOG:
  3648. return POWER_DOMAIN_PORT_CRT;
  3649. case INTEL_OUTPUT_DSI:
  3650. return POWER_DOMAIN_PORT_DSI;
  3651. default:
  3652. return POWER_DOMAIN_PORT_OTHER;
  3653. }
  3654. }
  3655. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3656. {
  3657. struct drm_device *dev = crtc->dev;
  3658. struct intel_encoder *intel_encoder;
  3659. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3660. enum pipe pipe = intel_crtc->pipe;
  3661. bool pfit_enabled = intel_crtc->config.pch_pfit.enabled;
  3662. unsigned long mask;
  3663. enum transcoder transcoder;
  3664. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3665. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3666. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3667. if (pfit_enabled)
  3668. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3669. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3670. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3671. return mask;
  3672. }
  3673. void intel_display_set_init_power(struct drm_i915_private *dev_priv,
  3674. bool enable)
  3675. {
  3676. if (dev_priv->power_domains.init_power_on == enable)
  3677. return;
  3678. if (enable)
  3679. intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
  3680. else
  3681. intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
  3682. dev_priv->power_domains.init_power_on = enable;
  3683. }
  3684. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3685. {
  3686. struct drm_i915_private *dev_priv = dev->dev_private;
  3687. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3688. struct intel_crtc *crtc;
  3689. /*
  3690. * First get all needed power domains, then put all unneeded, to avoid
  3691. * any unnecessary toggling of the power wells.
  3692. */
  3693. for_each_intel_crtc(dev, crtc) {
  3694. enum intel_display_power_domain domain;
  3695. if (!crtc->base.enabled)
  3696. continue;
  3697. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3698. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3699. intel_display_power_get(dev_priv, domain);
  3700. }
  3701. for_each_intel_crtc(dev, crtc) {
  3702. enum intel_display_power_domain domain;
  3703. for_each_power_domain(domain, crtc->enabled_power_domains)
  3704. intel_display_power_put(dev_priv, domain);
  3705. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  3706. }
  3707. intel_display_set_init_power(dev_priv, false);
  3708. }
  3709. int valleyview_get_vco(struct drm_i915_private *dev_priv)
  3710. {
  3711. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  3712. /* Obtain SKU information */
  3713. mutex_lock(&dev_priv->dpio_lock);
  3714. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  3715. CCK_FUSE_HPLL_FREQ_MASK;
  3716. mutex_unlock(&dev_priv->dpio_lock);
  3717. return vco_freq[hpll_freq];
  3718. }
  3719. /* Adjust CDclk dividers to allow high res or save power if possible */
  3720. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  3721. {
  3722. struct drm_i915_private *dev_priv = dev->dev_private;
  3723. u32 val, cmd;
  3724. WARN_ON(valleyview_cur_cdclk(dev_priv) != dev_priv->vlv_cdclk_freq);
  3725. dev_priv->vlv_cdclk_freq = cdclk;
  3726. if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
  3727. cmd = 2;
  3728. else if (cdclk == 266)
  3729. cmd = 1;
  3730. else
  3731. cmd = 0;
  3732. mutex_lock(&dev_priv->rps.hw_lock);
  3733. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3734. val &= ~DSPFREQGUAR_MASK;
  3735. val |= (cmd << DSPFREQGUAR_SHIFT);
  3736. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3737. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3738. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  3739. 50)) {
  3740. DRM_ERROR("timed out waiting for CDclk change\n");
  3741. }
  3742. mutex_unlock(&dev_priv->rps.hw_lock);
  3743. if (cdclk == 400) {
  3744. u32 divider, vco;
  3745. vco = valleyview_get_vco(dev_priv);
  3746. divider = ((vco << 1) / cdclk) - 1;
  3747. mutex_lock(&dev_priv->dpio_lock);
  3748. /* adjust cdclk divider */
  3749. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3750. val &= ~0xf;
  3751. val |= divider;
  3752. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  3753. mutex_unlock(&dev_priv->dpio_lock);
  3754. }
  3755. mutex_lock(&dev_priv->dpio_lock);
  3756. /* adjust self-refresh exit latency value */
  3757. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  3758. val &= ~0x7f;
  3759. /*
  3760. * For high bandwidth configs, we set a higher latency in the bunit
  3761. * so that the core display fetch happens in time to avoid underruns.
  3762. */
  3763. if (cdclk == 400)
  3764. val |= 4500 / 250; /* 4.5 usec */
  3765. else
  3766. val |= 3000 / 250; /* 3.0 usec */
  3767. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  3768. mutex_unlock(&dev_priv->dpio_lock);
  3769. /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
  3770. intel_i2c_reset(dev);
  3771. }
  3772. int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
  3773. {
  3774. int cur_cdclk, vco;
  3775. int divider;
  3776. vco = valleyview_get_vco(dev_priv);
  3777. mutex_lock(&dev_priv->dpio_lock);
  3778. divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3779. mutex_unlock(&dev_priv->dpio_lock);
  3780. divider &= 0xf;
  3781. cur_cdclk = (vco << 1) / (divider + 1);
  3782. return cur_cdclk;
  3783. }
  3784. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  3785. int max_pixclk)
  3786. {
  3787. /*
  3788. * Really only a few cases to deal with, as only 4 CDclks are supported:
  3789. * 200MHz
  3790. * 267MHz
  3791. * 320MHz
  3792. * 400MHz
  3793. * So we check to see whether we're above 90% of the lower bin and
  3794. * adjust if needed.
  3795. */
  3796. if (max_pixclk > 288000) {
  3797. return 400;
  3798. } else if (max_pixclk > 240000) {
  3799. return 320;
  3800. } else
  3801. return 266;
  3802. /* Looks like the 200MHz CDclk freq doesn't work on some configs */
  3803. }
  3804. /* compute the max pixel clock for new configuration */
  3805. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  3806. {
  3807. struct drm_device *dev = dev_priv->dev;
  3808. struct intel_crtc *intel_crtc;
  3809. int max_pixclk = 0;
  3810. for_each_intel_crtc(dev, intel_crtc) {
  3811. if (intel_crtc->new_enabled)
  3812. max_pixclk = max(max_pixclk,
  3813. intel_crtc->new_config->adjusted_mode.crtc_clock);
  3814. }
  3815. return max_pixclk;
  3816. }
  3817. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  3818. unsigned *prepare_pipes)
  3819. {
  3820. struct drm_i915_private *dev_priv = dev->dev_private;
  3821. struct intel_crtc *intel_crtc;
  3822. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3823. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  3824. dev_priv->vlv_cdclk_freq)
  3825. return;
  3826. /* disable/enable all currently active pipes while we change cdclk */
  3827. for_each_intel_crtc(dev, intel_crtc)
  3828. if (intel_crtc->base.enabled)
  3829. *prepare_pipes |= (1 << intel_crtc->pipe);
  3830. }
  3831. static void valleyview_modeset_global_resources(struct drm_device *dev)
  3832. {
  3833. struct drm_i915_private *dev_priv = dev->dev_private;
  3834. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3835. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  3836. if (req_cdclk != dev_priv->vlv_cdclk_freq)
  3837. valleyview_set_cdclk(dev, req_cdclk);
  3838. modeset_update_crtc_power_domains(dev);
  3839. }
  3840. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  3841. {
  3842. struct drm_device *dev = crtc->dev;
  3843. struct drm_i915_private *dev_priv = dev->dev_private;
  3844. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3845. struct intel_encoder *encoder;
  3846. int pipe = intel_crtc->pipe;
  3847. int plane = intel_crtc->plane;
  3848. bool is_dsi;
  3849. u32 dspcntr;
  3850. WARN_ON(!crtc->enabled);
  3851. if (intel_crtc->active)
  3852. return;
  3853. vlv_prepare_pll(intel_crtc);
  3854. /* Set up the display plane register */
  3855. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3856. if (intel_crtc->config.has_dp_encoder)
  3857. intel_dp_set_m_n(intel_crtc);
  3858. intel_set_pipe_timings(intel_crtc);
  3859. /* pipesrc and dspsize control the size that is scaled from,
  3860. * which should always be the user's requested size.
  3861. */
  3862. I915_WRITE(DSPSIZE(plane),
  3863. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  3864. (intel_crtc->config.pipe_src_w - 1));
  3865. I915_WRITE(DSPPOS(plane), 0);
  3866. i9xx_set_pipeconf(intel_crtc);
  3867. I915_WRITE(DSPCNTR(plane), dspcntr);
  3868. POSTING_READ(DSPCNTR(plane));
  3869. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3870. crtc->x, crtc->y);
  3871. intel_crtc->active = true;
  3872. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3873. for_each_encoder_on_crtc(dev, crtc, encoder)
  3874. if (encoder->pre_pll_enable)
  3875. encoder->pre_pll_enable(encoder);
  3876. is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
  3877. if (!is_dsi) {
  3878. if (IS_CHERRYVIEW(dev))
  3879. chv_enable_pll(intel_crtc);
  3880. else
  3881. vlv_enable_pll(intel_crtc);
  3882. }
  3883. for_each_encoder_on_crtc(dev, crtc, encoder)
  3884. if (encoder->pre_enable)
  3885. encoder->pre_enable(encoder);
  3886. i9xx_pfit_enable(intel_crtc);
  3887. intel_crtc_load_lut(crtc);
  3888. intel_update_watermarks(crtc);
  3889. intel_enable_pipe(intel_crtc);
  3890. for_each_encoder_on_crtc(dev, crtc, encoder)
  3891. encoder->enable(encoder);
  3892. intel_crtc_enable_planes(crtc);
  3893. drm_crtc_vblank_on(crtc);
  3894. /* Underruns don't raise interrupts, so check manually. */
  3895. i9xx_check_fifo_underruns(dev);
  3896. }
  3897. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  3898. {
  3899. struct drm_device *dev = crtc->base.dev;
  3900. struct drm_i915_private *dev_priv = dev->dev_private;
  3901. I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
  3902. I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
  3903. }
  3904. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  3905. {
  3906. struct drm_device *dev = crtc->dev;
  3907. struct drm_i915_private *dev_priv = dev->dev_private;
  3908. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3909. struct intel_encoder *encoder;
  3910. int pipe = intel_crtc->pipe;
  3911. int plane = intel_crtc->plane;
  3912. u32 dspcntr;
  3913. WARN_ON(!crtc->enabled);
  3914. if (intel_crtc->active)
  3915. return;
  3916. i9xx_set_pll_dividers(intel_crtc);
  3917. /* Set up the display plane register */
  3918. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3919. if (pipe == 0)
  3920. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3921. else
  3922. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3923. if (intel_crtc->config.has_dp_encoder)
  3924. intel_dp_set_m_n(intel_crtc);
  3925. intel_set_pipe_timings(intel_crtc);
  3926. /* pipesrc and dspsize control the size that is scaled from,
  3927. * which should always be the user's requested size.
  3928. */
  3929. I915_WRITE(DSPSIZE(plane),
  3930. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  3931. (intel_crtc->config.pipe_src_w - 1));
  3932. I915_WRITE(DSPPOS(plane), 0);
  3933. i9xx_set_pipeconf(intel_crtc);
  3934. I915_WRITE(DSPCNTR(plane), dspcntr);
  3935. POSTING_READ(DSPCNTR(plane));
  3936. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3937. crtc->x, crtc->y);
  3938. intel_crtc->active = true;
  3939. if (!IS_GEN2(dev))
  3940. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3941. for_each_encoder_on_crtc(dev, crtc, encoder)
  3942. if (encoder->pre_enable)
  3943. encoder->pre_enable(encoder);
  3944. i9xx_enable_pll(intel_crtc);
  3945. i9xx_pfit_enable(intel_crtc);
  3946. intel_crtc_load_lut(crtc);
  3947. intel_update_watermarks(crtc);
  3948. intel_enable_pipe(intel_crtc);
  3949. for_each_encoder_on_crtc(dev, crtc, encoder)
  3950. encoder->enable(encoder);
  3951. intel_crtc_enable_planes(crtc);
  3952. /*
  3953. * Gen2 reports pipe underruns whenever all planes are disabled.
  3954. * So don't enable underrun reporting before at least some planes
  3955. * are enabled.
  3956. * FIXME: Need to fix the logic to work when we turn off all planes
  3957. * but leave the pipe running.
  3958. */
  3959. if (IS_GEN2(dev))
  3960. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3961. drm_crtc_vblank_on(crtc);
  3962. /* Underruns don't raise interrupts, so check manually. */
  3963. i9xx_check_fifo_underruns(dev);
  3964. }
  3965. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  3966. {
  3967. struct drm_device *dev = crtc->base.dev;
  3968. struct drm_i915_private *dev_priv = dev->dev_private;
  3969. if (!crtc->config.gmch_pfit.control)
  3970. return;
  3971. assert_pipe_disabled(dev_priv, crtc->pipe);
  3972. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  3973. I915_READ(PFIT_CONTROL));
  3974. I915_WRITE(PFIT_CONTROL, 0);
  3975. }
  3976. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  3977. {
  3978. struct drm_device *dev = crtc->dev;
  3979. struct drm_i915_private *dev_priv = dev->dev_private;
  3980. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3981. struct intel_encoder *encoder;
  3982. int pipe = intel_crtc->pipe;
  3983. if (!intel_crtc->active)
  3984. return;
  3985. /*
  3986. * Gen2 reports pipe underruns whenever all planes are disabled.
  3987. * So diasble underrun reporting before all the planes get disabled.
  3988. * FIXME: Need to fix the logic to work when we turn off all planes
  3989. * but leave the pipe running.
  3990. */
  3991. if (IS_GEN2(dev))
  3992. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  3993. intel_crtc_disable_planes(crtc);
  3994. for_each_encoder_on_crtc(dev, crtc, encoder)
  3995. encoder->disable(encoder);
  3996. /*
  3997. * On gen2 planes are double buffered but the pipe isn't, so we must
  3998. * wait for planes to fully turn off before disabling the pipe.
  3999. */
  4000. if (IS_GEN2(dev))
  4001. intel_wait_for_vblank(dev, pipe);
  4002. intel_disable_pipe(dev_priv, pipe);
  4003. i9xx_pfit_disable(intel_crtc);
  4004. for_each_encoder_on_crtc(dev, crtc, encoder)
  4005. if (encoder->post_disable)
  4006. encoder->post_disable(encoder);
  4007. if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
  4008. if (IS_CHERRYVIEW(dev))
  4009. chv_disable_pll(dev_priv, pipe);
  4010. else if (IS_VALLEYVIEW(dev))
  4011. vlv_disable_pll(dev_priv, pipe);
  4012. else
  4013. i9xx_disable_pll(dev_priv, pipe);
  4014. }
  4015. if (!IS_GEN2(dev))
  4016. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  4017. intel_crtc->active = false;
  4018. intel_update_watermarks(crtc);
  4019. mutex_lock(&dev->struct_mutex);
  4020. intel_update_fbc(dev);
  4021. intel_edp_psr_update(dev);
  4022. mutex_unlock(&dev->struct_mutex);
  4023. }
  4024. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4025. {
  4026. }
  4027. static void intel_crtc_update_sarea(struct drm_crtc *crtc,
  4028. bool enabled)
  4029. {
  4030. struct drm_device *dev = crtc->dev;
  4031. struct drm_i915_master_private *master_priv;
  4032. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4033. int pipe = intel_crtc->pipe;
  4034. if (!dev->primary->master)
  4035. return;
  4036. master_priv = dev->primary->master->driver_priv;
  4037. if (!master_priv->sarea_priv)
  4038. return;
  4039. switch (pipe) {
  4040. case 0:
  4041. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  4042. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  4043. break;
  4044. case 1:
  4045. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  4046. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  4047. break;
  4048. default:
  4049. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  4050. break;
  4051. }
  4052. }
  4053. /**
  4054. * Sets the power management mode of the pipe and plane.
  4055. */
  4056. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4057. {
  4058. struct drm_device *dev = crtc->dev;
  4059. struct drm_i915_private *dev_priv = dev->dev_private;
  4060. struct intel_encoder *intel_encoder;
  4061. bool enable = false;
  4062. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4063. enable |= intel_encoder->connectors_active;
  4064. if (enable)
  4065. dev_priv->display.crtc_enable(crtc);
  4066. else
  4067. dev_priv->display.crtc_disable(crtc);
  4068. intel_crtc_update_sarea(crtc, enable);
  4069. }
  4070. static void intel_crtc_disable(struct drm_crtc *crtc)
  4071. {
  4072. struct drm_device *dev = crtc->dev;
  4073. struct drm_connector *connector;
  4074. struct drm_i915_private *dev_priv = dev->dev_private;
  4075. /* crtc should still be enabled when we disable it. */
  4076. WARN_ON(!crtc->enabled);
  4077. dev_priv->display.crtc_disable(crtc);
  4078. intel_crtc_update_sarea(crtc, false);
  4079. dev_priv->display.off(crtc);
  4080. assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
  4081. assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  4082. assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
  4083. if (crtc->primary->fb) {
  4084. mutex_lock(&dev->struct_mutex);
  4085. intel_unpin_fb_obj(to_intel_framebuffer(crtc->primary->fb)->obj);
  4086. mutex_unlock(&dev->struct_mutex);
  4087. crtc->primary->fb = NULL;
  4088. }
  4089. /* Update computed state. */
  4090. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4091. if (!connector->encoder || !connector->encoder->crtc)
  4092. continue;
  4093. if (connector->encoder->crtc != crtc)
  4094. continue;
  4095. connector->dpms = DRM_MODE_DPMS_OFF;
  4096. to_intel_encoder(connector->encoder)->connectors_active = false;
  4097. }
  4098. }
  4099. void intel_encoder_destroy(struct drm_encoder *encoder)
  4100. {
  4101. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4102. drm_encoder_cleanup(encoder);
  4103. kfree(intel_encoder);
  4104. }
  4105. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4106. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4107. * state of the entire output pipe. */
  4108. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4109. {
  4110. if (mode == DRM_MODE_DPMS_ON) {
  4111. encoder->connectors_active = true;
  4112. intel_crtc_update_dpms(encoder->base.crtc);
  4113. } else {
  4114. encoder->connectors_active = false;
  4115. intel_crtc_update_dpms(encoder->base.crtc);
  4116. }
  4117. }
  4118. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4119. * internal consistency). */
  4120. static void intel_connector_check_state(struct intel_connector *connector)
  4121. {
  4122. if (connector->get_hw_state(connector)) {
  4123. struct intel_encoder *encoder = connector->encoder;
  4124. struct drm_crtc *crtc;
  4125. bool encoder_enabled;
  4126. enum pipe pipe;
  4127. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4128. connector->base.base.id,
  4129. connector->base.name);
  4130. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4131. "wrong connector dpms state\n");
  4132. WARN(connector->base.encoder != &encoder->base,
  4133. "active connector not linked to encoder\n");
  4134. WARN(!encoder->connectors_active,
  4135. "encoder->connectors_active not set\n");
  4136. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4137. WARN(!encoder_enabled, "encoder not enabled\n");
  4138. if (WARN_ON(!encoder->base.crtc))
  4139. return;
  4140. crtc = encoder->base.crtc;
  4141. WARN(!crtc->enabled, "crtc not enabled\n");
  4142. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4143. WARN(pipe != to_intel_crtc(crtc)->pipe,
  4144. "encoder active on the wrong pipe\n");
  4145. }
  4146. }
  4147. /* Even simpler default implementation, if there's really no special case to
  4148. * consider. */
  4149. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4150. {
  4151. /* All the simple cases only support two dpms states. */
  4152. if (mode != DRM_MODE_DPMS_ON)
  4153. mode = DRM_MODE_DPMS_OFF;
  4154. if (mode == connector->dpms)
  4155. return;
  4156. connector->dpms = mode;
  4157. /* Only need to change hw state when actually enabled */
  4158. if (connector->encoder)
  4159. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4160. intel_modeset_check_state(connector->dev);
  4161. }
  4162. /* Simple connector->get_hw_state implementation for encoders that support only
  4163. * one connector and no cloning and hence the encoder state determines the state
  4164. * of the connector. */
  4165. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4166. {
  4167. enum pipe pipe = 0;
  4168. struct intel_encoder *encoder = connector->encoder;
  4169. return encoder->get_hw_state(encoder, &pipe);
  4170. }
  4171. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4172. struct intel_crtc_config *pipe_config)
  4173. {
  4174. struct drm_i915_private *dev_priv = dev->dev_private;
  4175. struct intel_crtc *pipe_B_crtc =
  4176. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4177. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4178. pipe_name(pipe), pipe_config->fdi_lanes);
  4179. if (pipe_config->fdi_lanes > 4) {
  4180. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4181. pipe_name(pipe), pipe_config->fdi_lanes);
  4182. return false;
  4183. }
  4184. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4185. if (pipe_config->fdi_lanes > 2) {
  4186. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4187. pipe_config->fdi_lanes);
  4188. return false;
  4189. } else {
  4190. return true;
  4191. }
  4192. }
  4193. if (INTEL_INFO(dev)->num_pipes == 2)
  4194. return true;
  4195. /* Ivybridge 3 pipe is really complicated */
  4196. switch (pipe) {
  4197. case PIPE_A:
  4198. return true;
  4199. case PIPE_B:
  4200. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4201. pipe_config->fdi_lanes > 2) {
  4202. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4203. pipe_name(pipe), pipe_config->fdi_lanes);
  4204. return false;
  4205. }
  4206. return true;
  4207. case PIPE_C:
  4208. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4209. pipe_B_crtc->config.fdi_lanes <= 2) {
  4210. if (pipe_config->fdi_lanes > 2) {
  4211. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4212. pipe_name(pipe), pipe_config->fdi_lanes);
  4213. return false;
  4214. }
  4215. } else {
  4216. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4217. return false;
  4218. }
  4219. return true;
  4220. default:
  4221. BUG();
  4222. }
  4223. }
  4224. #define RETRY 1
  4225. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4226. struct intel_crtc_config *pipe_config)
  4227. {
  4228. struct drm_device *dev = intel_crtc->base.dev;
  4229. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4230. int lane, link_bw, fdi_dotclock;
  4231. bool setup_ok, needs_recompute = false;
  4232. retry:
  4233. /* FDI is a binary signal running at ~2.7GHz, encoding
  4234. * each output octet as 10 bits. The actual frequency
  4235. * is stored as a divider into a 100MHz clock, and the
  4236. * mode pixel clock is stored in units of 1KHz.
  4237. * Hence the bw of each lane in terms of the mode signal
  4238. * is:
  4239. */
  4240. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4241. fdi_dotclock = adjusted_mode->crtc_clock;
  4242. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4243. pipe_config->pipe_bpp);
  4244. pipe_config->fdi_lanes = lane;
  4245. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4246. link_bw, &pipe_config->fdi_m_n);
  4247. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4248. intel_crtc->pipe, pipe_config);
  4249. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4250. pipe_config->pipe_bpp -= 2*3;
  4251. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4252. pipe_config->pipe_bpp);
  4253. needs_recompute = true;
  4254. pipe_config->bw_constrained = true;
  4255. goto retry;
  4256. }
  4257. if (needs_recompute)
  4258. return RETRY;
  4259. return setup_ok ? 0 : -EINVAL;
  4260. }
  4261. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4262. struct intel_crtc_config *pipe_config)
  4263. {
  4264. pipe_config->ips_enabled = i915.enable_ips &&
  4265. hsw_crtc_supports_ips(crtc) &&
  4266. pipe_config->pipe_bpp <= 24;
  4267. }
  4268. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4269. struct intel_crtc_config *pipe_config)
  4270. {
  4271. struct drm_device *dev = crtc->base.dev;
  4272. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4273. /* FIXME should check pixel clock limits on all platforms */
  4274. if (INTEL_INFO(dev)->gen < 4) {
  4275. struct drm_i915_private *dev_priv = dev->dev_private;
  4276. int clock_limit =
  4277. dev_priv->display.get_display_clock_speed(dev);
  4278. /*
  4279. * Enable pixel doubling when the dot clock
  4280. * is > 90% of the (display) core speed.
  4281. *
  4282. * GDG double wide on either pipe,
  4283. * otherwise pipe A only.
  4284. */
  4285. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4286. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4287. clock_limit *= 2;
  4288. pipe_config->double_wide = true;
  4289. }
  4290. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4291. return -EINVAL;
  4292. }
  4293. /*
  4294. * Pipe horizontal size must be even in:
  4295. * - DVO ganged mode
  4296. * - LVDS dual channel mode
  4297. * - Double wide pipe
  4298. */
  4299. if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4300. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4301. pipe_config->pipe_src_w &= ~1;
  4302. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4303. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4304. */
  4305. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4306. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4307. return -EINVAL;
  4308. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4309. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4310. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4311. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4312. * for lvds. */
  4313. pipe_config->pipe_bpp = 8*3;
  4314. }
  4315. if (HAS_IPS(dev))
  4316. hsw_compute_ips_config(crtc, pipe_config);
  4317. /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
  4318. * clock survives for now. */
  4319. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  4320. pipe_config->shared_dpll = crtc->config.shared_dpll;
  4321. if (pipe_config->has_pch_encoder)
  4322. return ironlake_fdi_compute_config(crtc, pipe_config);
  4323. return 0;
  4324. }
  4325. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4326. {
  4327. return 400000; /* FIXME */
  4328. }
  4329. static int i945_get_display_clock_speed(struct drm_device *dev)
  4330. {
  4331. return 400000;
  4332. }
  4333. static int i915_get_display_clock_speed(struct drm_device *dev)
  4334. {
  4335. return 333000;
  4336. }
  4337. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4338. {
  4339. return 200000;
  4340. }
  4341. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4342. {
  4343. u16 gcfgc = 0;
  4344. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4345. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4346. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4347. return 267000;
  4348. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4349. return 333000;
  4350. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4351. return 444000;
  4352. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4353. return 200000;
  4354. default:
  4355. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4356. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4357. return 133000;
  4358. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4359. return 167000;
  4360. }
  4361. }
  4362. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4363. {
  4364. u16 gcfgc = 0;
  4365. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4366. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4367. return 133000;
  4368. else {
  4369. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4370. case GC_DISPLAY_CLOCK_333_MHZ:
  4371. return 333000;
  4372. default:
  4373. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4374. return 190000;
  4375. }
  4376. }
  4377. }
  4378. static int i865_get_display_clock_speed(struct drm_device *dev)
  4379. {
  4380. return 266000;
  4381. }
  4382. static int i855_get_display_clock_speed(struct drm_device *dev)
  4383. {
  4384. u16 hpllcc = 0;
  4385. /* Assume that the hardware is in the high speed state. This
  4386. * should be the default.
  4387. */
  4388. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4389. case GC_CLOCK_133_200:
  4390. case GC_CLOCK_100_200:
  4391. return 200000;
  4392. case GC_CLOCK_166_250:
  4393. return 250000;
  4394. case GC_CLOCK_100_133:
  4395. return 133000;
  4396. }
  4397. /* Shouldn't happen */
  4398. return 0;
  4399. }
  4400. static int i830_get_display_clock_speed(struct drm_device *dev)
  4401. {
  4402. return 133000;
  4403. }
  4404. static void
  4405. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4406. {
  4407. while (*num > DATA_LINK_M_N_MASK ||
  4408. *den > DATA_LINK_M_N_MASK) {
  4409. *num >>= 1;
  4410. *den >>= 1;
  4411. }
  4412. }
  4413. static void compute_m_n(unsigned int m, unsigned int n,
  4414. uint32_t *ret_m, uint32_t *ret_n)
  4415. {
  4416. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4417. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4418. intel_reduce_m_n_ratio(ret_m, ret_n);
  4419. }
  4420. void
  4421. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4422. int pixel_clock, int link_clock,
  4423. struct intel_link_m_n *m_n)
  4424. {
  4425. m_n->tu = 64;
  4426. compute_m_n(bits_per_pixel * pixel_clock,
  4427. link_clock * nlanes * 8,
  4428. &m_n->gmch_m, &m_n->gmch_n);
  4429. compute_m_n(pixel_clock, link_clock,
  4430. &m_n->link_m, &m_n->link_n);
  4431. }
  4432. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4433. {
  4434. if (i915.panel_use_ssc >= 0)
  4435. return i915.panel_use_ssc != 0;
  4436. return dev_priv->vbt.lvds_use_ssc
  4437. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4438. }
  4439. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  4440. {
  4441. struct drm_device *dev = crtc->dev;
  4442. struct drm_i915_private *dev_priv = dev->dev_private;
  4443. int refclk;
  4444. if (IS_VALLEYVIEW(dev)) {
  4445. refclk = 100000;
  4446. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4447. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4448. refclk = dev_priv->vbt.lvds_ssc_freq;
  4449. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4450. } else if (!IS_GEN2(dev)) {
  4451. refclk = 96000;
  4452. } else {
  4453. refclk = 48000;
  4454. }
  4455. return refclk;
  4456. }
  4457. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4458. {
  4459. return (1 << dpll->n) << 16 | dpll->m2;
  4460. }
  4461. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4462. {
  4463. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4464. }
  4465. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4466. intel_clock_t *reduced_clock)
  4467. {
  4468. struct drm_device *dev = crtc->base.dev;
  4469. u32 fp, fp2 = 0;
  4470. if (IS_PINEVIEW(dev)) {
  4471. fp = pnv_dpll_compute_fp(&crtc->config.dpll);
  4472. if (reduced_clock)
  4473. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4474. } else {
  4475. fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
  4476. if (reduced_clock)
  4477. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4478. }
  4479. crtc->config.dpll_hw_state.fp0 = fp;
  4480. crtc->lowfreq_avail = false;
  4481. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4482. reduced_clock && i915.powersave) {
  4483. crtc->config.dpll_hw_state.fp1 = fp2;
  4484. crtc->lowfreq_avail = true;
  4485. } else {
  4486. crtc->config.dpll_hw_state.fp1 = fp;
  4487. }
  4488. }
  4489. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4490. pipe)
  4491. {
  4492. u32 reg_val;
  4493. /*
  4494. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4495. * and set it to a reasonable value instead.
  4496. */
  4497. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4498. reg_val &= 0xffffff00;
  4499. reg_val |= 0x00000030;
  4500. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4501. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4502. reg_val &= 0x8cffffff;
  4503. reg_val = 0x8c000000;
  4504. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4505. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4506. reg_val &= 0xffffff00;
  4507. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4508. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4509. reg_val &= 0x00ffffff;
  4510. reg_val |= 0xb0000000;
  4511. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4512. }
  4513. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4514. struct intel_link_m_n *m_n)
  4515. {
  4516. struct drm_device *dev = crtc->base.dev;
  4517. struct drm_i915_private *dev_priv = dev->dev_private;
  4518. int pipe = crtc->pipe;
  4519. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4520. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4521. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4522. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4523. }
  4524. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4525. struct intel_link_m_n *m_n)
  4526. {
  4527. struct drm_device *dev = crtc->base.dev;
  4528. struct drm_i915_private *dev_priv = dev->dev_private;
  4529. int pipe = crtc->pipe;
  4530. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4531. if (INTEL_INFO(dev)->gen >= 5) {
  4532. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4533. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4534. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4535. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4536. } else {
  4537. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4538. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4539. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4540. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4541. }
  4542. }
  4543. static void intel_dp_set_m_n(struct intel_crtc *crtc)
  4544. {
  4545. if (crtc->config.has_pch_encoder)
  4546. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4547. else
  4548. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4549. }
  4550. static void vlv_update_pll(struct intel_crtc *crtc)
  4551. {
  4552. u32 dpll, dpll_md;
  4553. /*
  4554. * Enable DPIO clock input. We should never disable the reference
  4555. * clock for pipe B, since VGA hotplug / manual detection depends
  4556. * on it.
  4557. */
  4558. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4559. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4560. /* We should never disable this, set it here for state tracking */
  4561. if (crtc->pipe == PIPE_B)
  4562. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4563. dpll |= DPLL_VCO_ENABLE;
  4564. crtc->config.dpll_hw_state.dpll = dpll;
  4565. dpll_md = (crtc->config.pixel_multiplier - 1)
  4566. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4567. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4568. }
  4569. static void vlv_prepare_pll(struct intel_crtc *crtc)
  4570. {
  4571. struct drm_device *dev = crtc->base.dev;
  4572. struct drm_i915_private *dev_priv = dev->dev_private;
  4573. int pipe = crtc->pipe;
  4574. u32 mdiv;
  4575. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4576. u32 coreclk, reg_val;
  4577. mutex_lock(&dev_priv->dpio_lock);
  4578. bestn = crtc->config.dpll.n;
  4579. bestm1 = crtc->config.dpll.m1;
  4580. bestm2 = crtc->config.dpll.m2;
  4581. bestp1 = crtc->config.dpll.p1;
  4582. bestp2 = crtc->config.dpll.p2;
  4583. /* See eDP HDMI DPIO driver vbios notes doc */
  4584. /* PLL B needs special handling */
  4585. if (pipe == PIPE_B)
  4586. vlv_pllb_recal_opamp(dev_priv, pipe);
  4587. /* Set up Tx target for periodic Rcomp update */
  4588. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4589. /* Disable target IRef on PLL */
  4590. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4591. reg_val &= 0x00ffffff;
  4592. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4593. /* Disable fast lock */
  4594. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4595. /* Set idtafcrecal before PLL is enabled */
  4596. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4597. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4598. mdiv |= ((bestn << DPIO_N_SHIFT));
  4599. mdiv |= (1 << DPIO_K_SHIFT);
  4600. /*
  4601. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4602. * but we don't support that).
  4603. * Note: don't use the DAC post divider as it seems unstable.
  4604. */
  4605. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4606. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4607. mdiv |= DPIO_ENABLE_CALIBRATION;
  4608. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4609. /* Set HBR and RBR LPF coefficients */
  4610. if (crtc->config.port_clock == 162000 ||
  4611. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
  4612. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
  4613. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4614. 0x009f0003);
  4615. else
  4616. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4617. 0x00d0000f);
  4618. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
  4619. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
  4620. /* Use SSC source */
  4621. if (pipe == PIPE_A)
  4622. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4623. 0x0df40000);
  4624. else
  4625. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4626. 0x0df70000);
  4627. } else { /* HDMI or VGA */
  4628. /* Use bend source */
  4629. if (pipe == PIPE_A)
  4630. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4631. 0x0df70000);
  4632. else
  4633. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4634. 0x0df40000);
  4635. }
  4636. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  4637. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  4638. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
  4639. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
  4640. coreclk |= 0x01000000;
  4641. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  4642. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  4643. mutex_unlock(&dev_priv->dpio_lock);
  4644. }
  4645. static void chv_update_pll(struct intel_crtc *crtc)
  4646. {
  4647. struct drm_device *dev = crtc->base.dev;
  4648. struct drm_i915_private *dev_priv = dev->dev_private;
  4649. int pipe = crtc->pipe;
  4650. int dpll_reg = DPLL(crtc->pipe);
  4651. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  4652. u32 loopfilter, intcoeff;
  4653. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  4654. int refclk;
  4655. crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  4656. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  4657. DPLL_VCO_ENABLE;
  4658. if (pipe != PIPE_A)
  4659. crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4660. crtc->config.dpll_hw_state.dpll_md =
  4661. (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4662. bestn = crtc->config.dpll.n;
  4663. bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
  4664. bestm1 = crtc->config.dpll.m1;
  4665. bestm2 = crtc->config.dpll.m2 >> 22;
  4666. bestp1 = crtc->config.dpll.p1;
  4667. bestp2 = crtc->config.dpll.p2;
  4668. /*
  4669. * Enable Refclk and SSC
  4670. */
  4671. I915_WRITE(dpll_reg,
  4672. crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  4673. mutex_lock(&dev_priv->dpio_lock);
  4674. /* p1 and p2 divider */
  4675. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  4676. 5 << DPIO_CHV_S1_DIV_SHIFT |
  4677. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  4678. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  4679. 1 << DPIO_CHV_K_DIV_SHIFT);
  4680. /* Feedback post-divider - m2 */
  4681. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  4682. /* Feedback refclk divider - n and m1 */
  4683. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  4684. DPIO_CHV_M1_DIV_BY_2 |
  4685. 1 << DPIO_CHV_N_DIV_SHIFT);
  4686. /* M2 fraction division */
  4687. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  4688. /* M2 fraction division enable */
  4689. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  4690. DPIO_CHV_FRAC_DIV_EN |
  4691. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  4692. /* Loop filter */
  4693. refclk = i9xx_get_refclk(&crtc->base, 0);
  4694. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  4695. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  4696. if (refclk == 100000)
  4697. intcoeff = 11;
  4698. else if (refclk == 38400)
  4699. intcoeff = 10;
  4700. else
  4701. intcoeff = 9;
  4702. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  4703. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  4704. /* AFC Recal */
  4705. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  4706. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  4707. DPIO_AFC_RECAL);
  4708. mutex_unlock(&dev_priv->dpio_lock);
  4709. }
  4710. static void i9xx_update_pll(struct intel_crtc *crtc,
  4711. intel_clock_t *reduced_clock,
  4712. int num_connectors)
  4713. {
  4714. struct drm_device *dev = crtc->base.dev;
  4715. struct drm_i915_private *dev_priv = dev->dev_private;
  4716. u32 dpll;
  4717. bool is_sdvo;
  4718. struct dpll *clock = &crtc->config.dpll;
  4719. i9xx_update_pll_dividers(crtc, reduced_clock);
  4720. is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
  4721. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  4722. dpll = DPLL_VGA_MODE_DIS;
  4723. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
  4724. dpll |= DPLLB_MODE_LVDS;
  4725. else
  4726. dpll |= DPLLB_MODE_DAC_SERIAL;
  4727. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  4728. dpll |= (crtc->config.pixel_multiplier - 1)
  4729. << SDVO_MULTIPLIER_SHIFT_HIRES;
  4730. }
  4731. if (is_sdvo)
  4732. dpll |= DPLL_SDVO_HIGH_SPEED;
  4733. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
  4734. dpll |= DPLL_SDVO_HIGH_SPEED;
  4735. /* compute bitmask from p1 value */
  4736. if (IS_PINEVIEW(dev))
  4737. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4738. else {
  4739. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4740. if (IS_G4X(dev) && reduced_clock)
  4741. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4742. }
  4743. switch (clock->p2) {
  4744. case 5:
  4745. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4746. break;
  4747. case 7:
  4748. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4749. break;
  4750. case 10:
  4751. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4752. break;
  4753. case 14:
  4754. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4755. break;
  4756. }
  4757. if (INTEL_INFO(dev)->gen >= 4)
  4758. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4759. if (crtc->config.sdvo_tv_clock)
  4760. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4761. else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4762. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4763. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4764. else
  4765. dpll |= PLL_REF_INPUT_DREFCLK;
  4766. dpll |= DPLL_VCO_ENABLE;
  4767. crtc->config.dpll_hw_state.dpll = dpll;
  4768. if (INTEL_INFO(dev)->gen >= 4) {
  4769. u32 dpll_md = (crtc->config.pixel_multiplier - 1)
  4770. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4771. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4772. }
  4773. }
  4774. static void i8xx_update_pll(struct intel_crtc *crtc,
  4775. intel_clock_t *reduced_clock,
  4776. int num_connectors)
  4777. {
  4778. struct drm_device *dev = crtc->base.dev;
  4779. struct drm_i915_private *dev_priv = dev->dev_private;
  4780. u32 dpll;
  4781. struct dpll *clock = &crtc->config.dpll;
  4782. i9xx_update_pll_dividers(crtc, reduced_clock);
  4783. dpll = DPLL_VGA_MODE_DIS;
  4784. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
  4785. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4786. } else {
  4787. if (clock->p1 == 2)
  4788. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4789. else
  4790. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4791. if (clock->p2 == 4)
  4792. dpll |= PLL_P2_DIVIDE_BY_4;
  4793. }
  4794. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
  4795. dpll |= DPLL_DVO_2X_MODE;
  4796. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4797. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4798. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4799. else
  4800. dpll |= PLL_REF_INPUT_DREFCLK;
  4801. dpll |= DPLL_VCO_ENABLE;
  4802. crtc->config.dpll_hw_state.dpll = dpll;
  4803. }
  4804. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  4805. {
  4806. struct drm_device *dev = intel_crtc->base.dev;
  4807. struct drm_i915_private *dev_priv = dev->dev_private;
  4808. enum pipe pipe = intel_crtc->pipe;
  4809. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  4810. struct drm_display_mode *adjusted_mode =
  4811. &intel_crtc->config.adjusted_mode;
  4812. uint32_t crtc_vtotal, crtc_vblank_end;
  4813. int vsyncshift = 0;
  4814. /* We need to be careful not to changed the adjusted mode, for otherwise
  4815. * the hw state checker will get angry at the mismatch. */
  4816. crtc_vtotal = adjusted_mode->crtc_vtotal;
  4817. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  4818. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4819. /* the chip adds 2 halflines automatically */
  4820. crtc_vtotal -= 1;
  4821. crtc_vblank_end -= 1;
  4822. if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  4823. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  4824. else
  4825. vsyncshift = adjusted_mode->crtc_hsync_start -
  4826. adjusted_mode->crtc_htotal / 2;
  4827. if (vsyncshift < 0)
  4828. vsyncshift += adjusted_mode->crtc_htotal;
  4829. }
  4830. if (INTEL_INFO(dev)->gen > 3)
  4831. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  4832. I915_WRITE(HTOTAL(cpu_transcoder),
  4833. (adjusted_mode->crtc_hdisplay - 1) |
  4834. ((adjusted_mode->crtc_htotal - 1) << 16));
  4835. I915_WRITE(HBLANK(cpu_transcoder),
  4836. (adjusted_mode->crtc_hblank_start - 1) |
  4837. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4838. I915_WRITE(HSYNC(cpu_transcoder),
  4839. (adjusted_mode->crtc_hsync_start - 1) |
  4840. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4841. I915_WRITE(VTOTAL(cpu_transcoder),
  4842. (adjusted_mode->crtc_vdisplay - 1) |
  4843. ((crtc_vtotal - 1) << 16));
  4844. I915_WRITE(VBLANK(cpu_transcoder),
  4845. (adjusted_mode->crtc_vblank_start - 1) |
  4846. ((crtc_vblank_end - 1) << 16));
  4847. I915_WRITE(VSYNC(cpu_transcoder),
  4848. (adjusted_mode->crtc_vsync_start - 1) |
  4849. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4850. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  4851. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  4852. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  4853. * bits. */
  4854. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  4855. (pipe == PIPE_B || pipe == PIPE_C))
  4856. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  4857. /* pipesrc controls the size that is scaled from, which should
  4858. * always be the user's requested size.
  4859. */
  4860. I915_WRITE(PIPESRC(pipe),
  4861. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  4862. (intel_crtc->config.pipe_src_h - 1));
  4863. }
  4864. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  4865. struct intel_crtc_config *pipe_config)
  4866. {
  4867. struct drm_device *dev = crtc->base.dev;
  4868. struct drm_i915_private *dev_priv = dev->dev_private;
  4869. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  4870. uint32_t tmp;
  4871. tmp = I915_READ(HTOTAL(cpu_transcoder));
  4872. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  4873. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  4874. tmp = I915_READ(HBLANK(cpu_transcoder));
  4875. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  4876. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  4877. tmp = I915_READ(HSYNC(cpu_transcoder));
  4878. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  4879. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  4880. tmp = I915_READ(VTOTAL(cpu_transcoder));
  4881. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  4882. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  4883. tmp = I915_READ(VBLANK(cpu_transcoder));
  4884. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  4885. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  4886. tmp = I915_READ(VSYNC(cpu_transcoder));
  4887. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  4888. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  4889. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  4890. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  4891. pipe_config->adjusted_mode.crtc_vtotal += 1;
  4892. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  4893. }
  4894. tmp = I915_READ(PIPESRC(crtc->pipe));
  4895. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  4896. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  4897. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  4898. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  4899. }
  4900. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  4901. struct intel_crtc_config *pipe_config)
  4902. {
  4903. mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  4904. mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
  4905. mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  4906. mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  4907. mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  4908. mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  4909. mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  4910. mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  4911. mode->flags = pipe_config->adjusted_mode.flags;
  4912. mode->clock = pipe_config->adjusted_mode.crtc_clock;
  4913. mode->flags |= pipe_config->adjusted_mode.flags;
  4914. }
  4915. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  4916. {
  4917. struct drm_device *dev = intel_crtc->base.dev;
  4918. struct drm_i915_private *dev_priv = dev->dev_private;
  4919. uint32_t pipeconf;
  4920. pipeconf = 0;
  4921. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  4922. I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
  4923. pipeconf |= PIPECONF_ENABLE;
  4924. if (intel_crtc->config.double_wide)
  4925. pipeconf |= PIPECONF_DOUBLE_WIDE;
  4926. /* only g4x and later have fancy bpc/dither controls */
  4927. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  4928. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  4929. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  4930. pipeconf |= PIPECONF_DITHER_EN |
  4931. PIPECONF_DITHER_TYPE_SP;
  4932. switch (intel_crtc->config.pipe_bpp) {
  4933. case 18:
  4934. pipeconf |= PIPECONF_6BPC;
  4935. break;
  4936. case 24:
  4937. pipeconf |= PIPECONF_8BPC;
  4938. break;
  4939. case 30:
  4940. pipeconf |= PIPECONF_10BPC;
  4941. break;
  4942. default:
  4943. /* Case prevented by intel_choose_pipe_bpp_dither. */
  4944. BUG();
  4945. }
  4946. }
  4947. if (HAS_PIPE_CXSR(dev)) {
  4948. if (intel_crtc->lowfreq_avail) {
  4949. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4950. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4951. } else {
  4952. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4953. }
  4954. }
  4955. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  4956. if (INTEL_INFO(dev)->gen < 4 ||
  4957. intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  4958. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4959. else
  4960. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  4961. } else
  4962. pipeconf |= PIPECONF_PROGRESSIVE;
  4963. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  4964. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  4965. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  4966. POSTING_READ(PIPECONF(intel_crtc->pipe));
  4967. }
  4968. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  4969. int x, int y,
  4970. struct drm_framebuffer *fb)
  4971. {
  4972. struct drm_device *dev = crtc->dev;
  4973. struct drm_i915_private *dev_priv = dev->dev_private;
  4974. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4975. int refclk, num_connectors = 0;
  4976. intel_clock_t clock, reduced_clock;
  4977. bool ok, has_reduced_clock = false;
  4978. bool is_lvds = false, is_dsi = false;
  4979. struct intel_encoder *encoder;
  4980. const intel_limit_t *limit;
  4981. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4982. switch (encoder->type) {
  4983. case INTEL_OUTPUT_LVDS:
  4984. is_lvds = true;
  4985. break;
  4986. case INTEL_OUTPUT_DSI:
  4987. is_dsi = true;
  4988. break;
  4989. }
  4990. num_connectors++;
  4991. }
  4992. if (is_dsi)
  4993. return 0;
  4994. if (!intel_crtc->config.clock_set) {
  4995. refclk = i9xx_get_refclk(crtc, num_connectors);
  4996. /*
  4997. * Returns a set of divisors for the desired target clock with
  4998. * the given refclk, or FALSE. The returned values represent
  4999. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5000. * 2) / p1 / p2.
  5001. */
  5002. limit = intel_limit(crtc, refclk);
  5003. ok = dev_priv->display.find_dpll(limit, crtc,
  5004. intel_crtc->config.port_clock,
  5005. refclk, NULL, &clock);
  5006. if (!ok) {
  5007. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5008. return -EINVAL;
  5009. }
  5010. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5011. /*
  5012. * Ensure we match the reduced clock's P to the target
  5013. * clock. If the clocks don't match, we can't switch
  5014. * the display clock by using the FP0/FP1. In such case
  5015. * we will disable the LVDS downclock feature.
  5016. */
  5017. has_reduced_clock =
  5018. dev_priv->display.find_dpll(limit, crtc,
  5019. dev_priv->lvds_downclock,
  5020. refclk, &clock,
  5021. &reduced_clock);
  5022. }
  5023. /* Compat-code for transition, will disappear. */
  5024. intel_crtc->config.dpll.n = clock.n;
  5025. intel_crtc->config.dpll.m1 = clock.m1;
  5026. intel_crtc->config.dpll.m2 = clock.m2;
  5027. intel_crtc->config.dpll.p1 = clock.p1;
  5028. intel_crtc->config.dpll.p2 = clock.p2;
  5029. }
  5030. if (IS_GEN2(dev)) {
  5031. i8xx_update_pll(intel_crtc,
  5032. has_reduced_clock ? &reduced_clock : NULL,
  5033. num_connectors);
  5034. } else if (IS_CHERRYVIEW(dev)) {
  5035. chv_update_pll(intel_crtc);
  5036. } else if (IS_VALLEYVIEW(dev)) {
  5037. vlv_update_pll(intel_crtc);
  5038. } else {
  5039. i9xx_update_pll(intel_crtc,
  5040. has_reduced_clock ? &reduced_clock : NULL,
  5041. num_connectors);
  5042. }
  5043. return 0;
  5044. }
  5045. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5046. struct intel_crtc_config *pipe_config)
  5047. {
  5048. struct drm_device *dev = crtc->base.dev;
  5049. struct drm_i915_private *dev_priv = dev->dev_private;
  5050. uint32_t tmp;
  5051. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5052. return;
  5053. tmp = I915_READ(PFIT_CONTROL);
  5054. if (!(tmp & PFIT_ENABLE))
  5055. return;
  5056. /* Check whether the pfit is attached to our pipe. */
  5057. if (INTEL_INFO(dev)->gen < 4) {
  5058. if (crtc->pipe != PIPE_B)
  5059. return;
  5060. } else {
  5061. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5062. return;
  5063. }
  5064. pipe_config->gmch_pfit.control = tmp;
  5065. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5066. if (INTEL_INFO(dev)->gen < 5)
  5067. pipe_config->gmch_pfit.lvds_border_bits =
  5068. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5069. }
  5070. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5071. struct intel_crtc_config *pipe_config)
  5072. {
  5073. struct drm_device *dev = crtc->base.dev;
  5074. struct drm_i915_private *dev_priv = dev->dev_private;
  5075. int pipe = pipe_config->cpu_transcoder;
  5076. intel_clock_t clock;
  5077. u32 mdiv;
  5078. int refclk = 100000;
  5079. mutex_lock(&dev_priv->dpio_lock);
  5080. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5081. mutex_unlock(&dev_priv->dpio_lock);
  5082. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5083. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5084. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5085. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5086. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5087. vlv_clock(refclk, &clock);
  5088. /* clock.dot is the fast clock */
  5089. pipe_config->port_clock = clock.dot / 5;
  5090. }
  5091. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5092. struct intel_plane_config *plane_config)
  5093. {
  5094. struct drm_device *dev = crtc->base.dev;
  5095. struct drm_i915_private *dev_priv = dev->dev_private;
  5096. u32 val, base, offset;
  5097. int pipe = crtc->pipe, plane = crtc->plane;
  5098. int fourcc, pixel_format;
  5099. int aligned_height;
  5100. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5101. if (!crtc->base.primary->fb) {
  5102. DRM_DEBUG_KMS("failed to alloc fb\n");
  5103. return;
  5104. }
  5105. val = I915_READ(DSPCNTR(plane));
  5106. if (INTEL_INFO(dev)->gen >= 4)
  5107. if (val & DISPPLANE_TILED)
  5108. plane_config->tiled = true;
  5109. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5110. fourcc = intel_format_to_fourcc(pixel_format);
  5111. crtc->base.primary->fb->pixel_format = fourcc;
  5112. crtc->base.primary->fb->bits_per_pixel =
  5113. drm_format_plane_cpp(fourcc, 0) * 8;
  5114. if (INTEL_INFO(dev)->gen >= 4) {
  5115. if (plane_config->tiled)
  5116. offset = I915_READ(DSPTILEOFF(plane));
  5117. else
  5118. offset = I915_READ(DSPLINOFF(plane));
  5119. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5120. } else {
  5121. base = I915_READ(DSPADDR(plane));
  5122. }
  5123. plane_config->base = base;
  5124. val = I915_READ(PIPESRC(pipe));
  5125. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5126. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5127. val = I915_READ(DSPSTRIDE(pipe));
  5128. crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
  5129. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5130. plane_config->tiled);
  5131. plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
  5132. aligned_height, PAGE_SIZE);
  5133. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5134. pipe, plane, crtc->base.primary->fb->width,
  5135. crtc->base.primary->fb->height,
  5136. crtc->base.primary->fb->bits_per_pixel, base,
  5137. crtc->base.primary->fb->pitches[0],
  5138. plane_config->size);
  5139. }
  5140. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5141. struct intel_crtc_config *pipe_config)
  5142. {
  5143. struct drm_device *dev = crtc->base.dev;
  5144. struct drm_i915_private *dev_priv = dev->dev_private;
  5145. int pipe = pipe_config->cpu_transcoder;
  5146. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5147. intel_clock_t clock;
  5148. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5149. int refclk = 100000;
  5150. mutex_lock(&dev_priv->dpio_lock);
  5151. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5152. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5153. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5154. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5155. mutex_unlock(&dev_priv->dpio_lock);
  5156. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5157. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5158. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5159. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5160. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5161. chv_clock(refclk, &clock);
  5162. /* clock.dot is the fast clock */
  5163. pipe_config->port_clock = clock.dot / 5;
  5164. }
  5165. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5166. struct intel_crtc_config *pipe_config)
  5167. {
  5168. struct drm_device *dev = crtc->base.dev;
  5169. struct drm_i915_private *dev_priv = dev->dev_private;
  5170. uint32_t tmp;
  5171. if (!intel_display_power_enabled(dev_priv,
  5172. POWER_DOMAIN_PIPE(crtc->pipe)))
  5173. return false;
  5174. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5175. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5176. tmp = I915_READ(PIPECONF(crtc->pipe));
  5177. if (!(tmp & PIPECONF_ENABLE))
  5178. return false;
  5179. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5180. switch (tmp & PIPECONF_BPC_MASK) {
  5181. case PIPECONF_6BPC:
  5182. pipe_config->pipe_bpp = 18;
  5183. break;
  5184. case PIPECONF_8BPC:
  5185. pipe_config->pipe_bpp = 24;
  5186. break;
  5187. case PIPECONF_10BPC:
  5188. pipe_config->pipe_bpp = 30;
  5189. break;
  5190. default:
  5191. break;
  5192. }
  5193. }
  5194. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5195. pipe_config->limited_color_range = true;
  5196. if (INTEL_INFO(dev)->gen < 4)
  5197. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5198. intel_get_pipe_timings(crtc, pipe_config);
  5199. i9xx_get_pfit_config(crtc, pipe_config);
  5200. if (INTEL_INFO(dev)->gen >= 4) {
  5201. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5202. pipe_config->pixel_multiplier =
  5203. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5204. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5205. pipe_config->dpll_hw_state.dpll_md = tmp;
  5206. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5207. tmp = I915_READ(DPLL(crtc->pipe));
  5208. pipe_config->pixel_multiplier =
  5209. ((tmp & SDVO_MULTIPLIER_MASK)
  5210. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5211. } else {
  5212. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5213. * port and will be fixed up in the encoder->get_config
  5214. * function. */
  5215. pipe_config->pixel_multiplier = 1;
  5216. }
  5217. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5218. if (!IS_VALLEYVIEW(dev)) {
  5219. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5220. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5221. } else {
  5222. /* Mask out read-only status bits. */
  5223. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5224. DPLL_PORTC_READY_MASK |
  5225. DPLL_PORTB_READY_MASK);
  5226. }
  5227. if (IS_CHERRYVIEW(dev))
  5228. chv_crtc_clock_get(crtc, pipe_config);
  5229. else if (IS_VALLEYVIEW(dev))
  5230. vlv_crtc_clock_get(crtc, pipe_config);
  5231. else
  5232. i9xx_crtc_clock_get(crtc, pipe_config);
  5233. return true;
  5234. }
  5235. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5236. {
  5237. struct drm_i915_private *dev_priv = dev->dev_private;
  5238. struct drm_mode_config *mode_config = &dev->mode_config;
  5239. struct intel_encoder *encoder;
  5240. u32 val, final;
  5241. bool has_lvds = false;
  5242. bool has_cpu_edp = false;
  5243. bool has_panel = false;
  5244. bool has_ck505 = false;
  5245. bool can_ssc = false;
  5246. /* We need to take the global config into account */
  5247. list_for_each_entry(encoder, &mode_config->encoder_list,
  5248. base.head) {
  5249. switch (encoder->type) {
  5250. case INTEL_OUTPUT_LVDS:
  5251. has_panel = true;
  5252. has_lvds = true;
  5253. break;
  5254. case INTEL_OUTPUT_EDP:
  5255. has_panel = true;
  5256. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5257. has_cpu_edp = true;
  5258. break;
  5259. }
  5260. }
  5261. if (HAS_PCH_IBX(dev)) {
  5262. has_ck505 = dev_priv->vbt.display_clock_mode;
  5263. can_ssc = has_ck505;
  5264. } else {
  5265. has_ck505 = false;
  5266. can_ssc = true;
  5267. }
  5268. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5269. has_panel, has_lvds, has_ck505);
  5270. /* Ironlake: try to setup display ref clock before DPLL
  5271. * enabling. This is only under driver's control after
  5272. * PCH B stepping, previous chipset stepping should be
  5273. * ignoring this setting.
  5274. */
  5275. val = I915_READ(PCH_DREF_CONTROL);
  5276. /* As we must carefully and slowly disable/enable each source in turn,
  5277. * compute the final state we want first and check if we need to
  5278. * make any changes at all.
  5279. */
  5280. final = val;
  5281. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5282. if (has_ck505)
  5283. final |= DREF_NONSPREAD_CK505_ENABLE;
  5284. else
  5285. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5286. final &= ~DREF_SSC_SOURCE_MASK;
  5287. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5288. final &= ~DREF_SSC1_ENABLE;
  5289. if (has_panel) {
  5290. final |= DREF_SSC_SOURCE_ENABLE;
  5291. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5292. final |= DREF_SSC1_ENABLE;
  5293. if (has_cpu_edp) {
  5294. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5295. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5296. else
  5297. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5298. } else
  5299. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5300. } else {
  5301. final |= DREF_SSC_SOURCE_DISABLE;
  5302. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5303. }
  5304. if (final == val)
  5305. return;
  5306. /* Always enable nonspread source */
  5307. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5308. if (has_ck505)
  5309. val |= DREF_NONSPREAD_CK505_ENABLE;
  5310. else
  5311. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5312. if (has_panel) {
  5313. val &= ~DREF_SSC_SOURCE_MASK;
  5314. val |= DREF_SSC_SOURCE_ENABLE;
  5315. /* SSC must be turned on before enabling the CPU output */
  5316. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5317. DRM_DEBUG_KMS("Using SSC on panel\n");
  5318. val |= DREF_SSC1_ENABLE;
  5319. } else
  5320. val &= ~DREF_SSC1_ENABLE;
  5321. /* Get SSC going before enabling the outputs */
  5322. I915_WRITE(PCH_DREF_CONTROL, val);
  5323. POSTING_READ(PCH_DREF_CONTROL);
  5324. udelay(200);
  5325. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5326. /* Enable CPU source on CPU attached eDP */
  5327. if (has_cpu_edp) {
  5328. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5329. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5330. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5331. } else
  5332. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5333. } else
  5334. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5335. I915_WRITE(PCH_DREF_CONTROL, val);
  5336. POSTING_READ(PCH_DREF_CONTROL);
  5337. udelay(200);
  5338. } else {
  5339. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5340. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5341. /* Turn off CPU output */
  5342. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5343. I915_WRITE(PCH_DREF_CONTROL, val);
  5344. POSTING_READ(PCH_DREF_CONTROL);
  5345. udelay(200);
  5346. /* Turn off the SSC source */
  5347. val &= ~DREF_SSC_SOURCE_MASK;
  5348. val |= DREF_SSC_SOURCE_DISABLE;
  5349. /* Turn off SSC1 */
  5350. val &= ~DREF_SSC1_ENABLE;
  5351. I915_WRITE(PCH_DREF_CONTROL, val);
  5352. POSTING_READ(PCH_DREF_CONTROL);
  5353. udelay(200);
  5354. }
  5355. BUG_ON(val != final);
  5356. }
  5357. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5358. {
  5359. uint32_t tmp;
  5360. tmp = I915_READ(SOUTH_CHICKEN2);
  5361. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5362. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5363. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5364. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5365. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5366. tmp = I915_READ(SOUTH_CHICKEN2);
  5367. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5368. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5369. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5370. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5371. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5372. }
  5373. /* WaMPhyProgramming:hsw */
  5374. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5375. {
  5376. uint32_t tmp;
  5377. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5378. tmp &= ~(0xFF << 24);
  5379. tmp |= (0x12 << 24);
  5380. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5381. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5382. tmp |= (1 << 11);
  5383. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5384. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5385. tmp |= (1 << 11);
  5386. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5387. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5388. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5389. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5390. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5391. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5392. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5393. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5394. tmp &= ~(7 << 13);
  5395. tmp |= (5 << 13);
  5396. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5397. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5398. tmp &= ~(7 << 13);
  5399. tmp |= (5 << 13);
  5400. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5401. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5402. tmp &= ~0xFF;
  5403. tmp |= 0x1C;
  5404. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5405. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5406. tmp &= ~0xFF;
  5407. tmp |= 0x1C;
  5408. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5409. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5410. tmp &= ~(0xFF << 16);
  5411. tmp |= (0x1C << 16);
  5412. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5413. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5414. tmp &= ~(0xFF << 16);
  5415. tmp |= (0x1C << 16);
  5416. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5417. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5418. tmp |= (1 << 27);
  5419. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5420. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5421. tmp |= (1 << 27);
  5422. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5423. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5424. tmp &= ~(0xF << 28);
  5425. tmp |= (4 << 28);
  5426. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5427. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5428. tmp &= ~(0xF << 28);
  5429. tmp |= (4 << 28);
  5430. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5431. }
  5432. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5433. * Programming" based on the parameters passed:
  5434. * - Sequence to enable CLKOUT_DP
  5435. * - Sequence to enable CLKOUT_DP without spread
  5436. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5437. */
  5438. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5439. bool with_fdi)
  5440. {
  5441. struct drm_i915_private *dev_priv = dev->dev_private;
  5442. uint32_t reg, tmp;
  5443. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5444. with_spread = true;
  5445. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5446. with_fdi, "LP PCH doesn't have FDI\n"))
  5447. with_fdi = false;
  5448. mutex_lock(&dev_priv->dpio_lock);
  5449. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5450. tmp &= ~SBI_SSCCTL_DISABLE;
  5451. tmp |= SBI_SSCCTL_PATHALT;
  5452. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5453. udelay(24);
  5454. if (with_spread) {
  5455. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5456. tmp &= ~SBI_SSCCTL_PATHALT;
  5457. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5458. if (with_fdi) {
  5459. lpt_reset_fdi_mphy(dev_priv);
  5460. lpt_program_fdi_mphy(dev_priv);
  5461. }
  5462. }
  5463. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5464. SBI_GEN0 : SBI_DBUFF0;
  5465. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5466. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5467. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5468. mutex_unlock(&dev_priv->dpio_lock);
  5469. }
  5470. /* Sequence to disable CLKOUT_DP */
  5471. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5472. {
  5473. struct drm_i915_private *dev_priv = dev->dev_private;
  5474. uint32_t reg, tmp;
  5475. mutex_lock(&dev_priv->dpio_lock);
  5476. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5477. SBI_GEN0 : SBI_DBUFF0;
  5478. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5479. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5480. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5481. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5482. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5483. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5484. tmp |= SBI_SSCCTL_PATHALT;
  5485. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5486. udelay(32);
  5487. }
  5488. tmp |= SBI_SSCCTL_DISABLE;
  5489. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5490. }
  5491. mutex_unlock(&dev_priv->dpio_lock);
  5492. }
  5493. static void lpt_init_pch_refclk(struct drm_device *dev)
  5494. {
  5495. struct drm_mode_config *mode_config = &dev->mode_config;
  5496. struct intel_encoder *encoder;
  5497. bool has_vga = false;
  5498. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  5499. switch (encoder->type) {
  5500. case INTEL_OUTPUT_ANALOG:
  5501. has_vga = true;
  5502. break;
  5503. }
  5504. }
  5505. if (has_vga)
  5506. lpt_enable_clkout_dp(dev, true, true);
  5507. else
  5508. lpt_disable_clkout_dp(dev);
  5509. }
  5510. /*
  5511. * Initialize reference clocks when the driver loads
  5512. */
  5513. void intel_init_pch_refclk(struct drm_device *dev)
  5514. {
  5515. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5516. ironlake_init_pch_refclk(dev);
  5517. else if (HAS_PCH_LPT(dev))
  5518. lpt_init_pch_refclk(dev);
  5519. }
  5520. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5521. {
  5522. struct drm_device *dev = crtc->dev;
  5523. struct drm_i915_private *dev_priv = dev->dev_private;
  5524. struct intel_encoder *encoder;
  5525. int num_connectors = 0;
  5526. bool is_lvds = false;
  5527. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5528. switch (encoder->type) {
  5529. case INTEL_OUTPUT_LVDS:
  5530. is_lvds = true;
  5531. break;
  5532. }
  5533. num_connectors++;
  5534. }
  5535. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5536. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5537. dev_priv->vbt.lvds_ssc_freq);
  5538. return dev_priv->vbt.lvds_ssc_freq;
  5539. }
  5540. return 120000;
  5541. }
  5542. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5543. {
  5544. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5545. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5546. int pipe = intel_crtc->pipe;
  5547. uint32_t val;
  5548. val = 0;
  5549. switch (intel_crtc->config.pipe_bpp) {
  5550. case 18:
  5551. val |= PIPECONF_6BPC;
  5552. break;
  5553. case 24:
  5554. val |= PIPECONF_8BPC;
  5555. break;
  5556. case 30:
  5557. val |= PIPECONF_10BPC;
  5558. break;
  5559. case 36:
  5560. val |= PIPECONF_12BPC;
  5561. break;
  5562. default:
  5563. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5564. BUG();
  5565. }
  5566. if (intel_crtc->config.dither)
  5567. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5568. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5569. val |= PIPECONF_INTERLACED_ILK;
  5570. else
  5571. val |= PIPECONF_PROGRESSIVE;
  5572. if (intel_crtc->config.limited_color_range)
  5573. val |= PIPECONF_COLOR_RANGE_SELECT;
  5574. I915_WRITE(PIPECONF(pipe), val);
  5575. POSTING_READ(PIPECONF(pipe));
  5576. }
  5577. /*
  5578. * Set up the pipe CSC unit.
  5579. *
  5580. * Currently only full range RGB to limited range RGB conversion
  5581. * is supported, but eventually this should handle various
  5582. * RGB<->YCbCr scenarios as well.
  5583. */
  5584. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  5585. {
  5586. struct drm_device *dev = crtc->dev;
  5587. struct drm_i915_private *dev_priv = dev->dev_private;
  5588. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5589. int pipe = intel_crtc->pipe;
  5590. uint16_t coeff = 0x7800; /* 1.0 */
  5591. /*
  5592. * TODO: Check what kind of values actually come out of the pipe
  5593. * with these coeff/postoff values and adjust to get the best
  5594. * accuracy. Perhaps we even need to take the bpc value into
  5595. * consideration.
  5596. */
  5597. if (intel_crtc->config.limited_color_range)
  5598. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  5599. /*
  5600. * GY/GU and RY/RU should be the other way around according
  5601. * to BSpec, but reality doesn't agree. Just set them up in
  5602. * a way that results in the correct picture.
  5603. */
  5604. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  5605. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  5606. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  5607. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  5608. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  5609. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  5610. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  5611. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  5612. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  5613. if (INTEL_INFO(dev)->gen > 6) {
  5614. uint16_t postoff = 0;
  5615. if (intel_crtc->config.limited_color_range)
  5616. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  5617. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  5618. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  5619. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  5620. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  5621. } else {
  5622. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  5623. if (intel_crtc->config.limited_color_range)
  5624. mode |= CSC_BLACK_SCREEN_OFFSET;
  5625. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  5626. }
  5627. }
  5628. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  5629. {
  5630. struct drm_device *dev = crtc->dev;
  5631. struct drm_i915_private *dev_priv = dev->dev_private;
  5632. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5633. enum pipe pipe = intel_crtc->pipe;
  5634. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5635. uint32_t val;
  5636. val = 0;
  5637. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  5638. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5639. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5640. val |= PIPECONF_INTERLACED_ILK;
  5641. else
  5642. val |= PIPECONF_PROGRESSIVE;
  5643. I915_WRITE(PIPECONF(cpu_transcoder), val);
  5644. POSTING_READ(PIPECONF(cpu_transcoder));
  5645. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  5646. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  5647. if (IS_BROADWELL(dev)) {
  5648. val = 0;
  5649. switch (intel_crtc->config.pipe_bpp) {
  5650. case 18:
  5651. val |= PIPEMISC_DITHER_6_BPC;
  5652. break;
  5653. case 24:
  5654. val |= PIPEMISC_DITHER_8_BPC;
  5655. break;
  5656. case 30:
  5657. val |= PIPEMISC_DITHER_10_BPC;
  5658. break;
  5659. case 36:
  5660. val |= PIPEMISC_DITHER_12_BPC;
  5661. break;
  5662. default:
  5663. /* Case prevented by pipe_config_set_bpp. */
  5664. BUG();
  5665. }
  5666. if (intel_crtc->config.dither)
  5667. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  5668. I915_WRITE(PIPEMISC(pipe), val);
  5669. }
  5670. }
  5671. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  5672. intel_clock_t *clock,
  5673. bool *has_reduced_clock,
  5674. intel_clock_t *reduced_clock)
  5675. {
  5676. struct drm_device *dev = crtc->dev;
  5677. struct drm_i915_private *dev_priv = dev->dev_private;
  5678. struct intel_encoder *intel_encoder;
  5679. int refclk;
  5680. const intel_limit_t *limit;
  5681. bool ret, is_lvds = false;
  5682. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5683. switch (intel_encoder->type) {
  5684. case INTEL_OUTPUT_LVDS:
  5685. is_lvds = true;
  5686. break;
  5687. }
  5688. }
  5689. refclk = ironlake_get_refclk(crtc);
  5690. /*
  5691. * Returns a set of divisors for the desired target clock with the given
  5692. * refclk, or FALSE. The returned values represent the clock equation:
  5693. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  5694. */
  5695. limit = intel_limit(crtc, refclk);
  5696. ret = dev_priv->display.find_dpll(limit, crtc,
  5697. to_intel_crtc(crtc)->config.port_clock,
  5698. refclk, NULL, clock);
  5699. if (!ret)
  5700. return false;
  5701. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5702. /*
  5703. * Ensure we match the reduced clock's P to the target clock.
  5704. * If the clocks don't match, we can't switch the display clock
  5705. * by using the FP0/FP1. In such case we will disable the LVDS
  5706. * downclock feature.
  5707. */
  5708. *has_reduced_clock =
  5709. dev_priv->display.find_dpll(limit, crtc,
  5710. dev_priv->lvds_downclock,
  5711. refclk, clock,
  5712. reduced_clock);
  5713. }
  5714. return true;
  5715. }
  5716. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  5717. {
  5718. /*
  5719. * Account for spread spectrum to avoid
  5720. * oversubscribing the link. Max center spread
  5721. * is 2.5%; use 5% for safety's sake.
  5722. */
  5723. u32 bps = target_clock * bpp * 21 / 20;
  5724. return DIV_ROUND_UP(bps, link_bw * 8);
  5725. }
  5726. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  5727. {
  5728. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  5729. }
  5730. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  5731. u32 *fp,
  5732. intel_clock_t *reduced_clock, u32 *fp2)
  5733. {
  5734. struct drm_crtc *crtc = &intel_crtc->base;
  5735. struct drm_device *dev = crtc->dev;
  5736. struct drm_i915_private *dev_priv = dev->dev_private;
  5737. struct intel_encoder *intel_encoder;
  5738. uint32_t dpll;
  5739. int factor, num_connectors = 0;
  5740. bool is_lvds = false, is_sdvo = false;
  5741. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5742. switch (intel_encoder->type) {
  5743. case INTEL_OUTPUT_LVDS:
  5744. is_lvds = true;
  5745. break;
  5746. case INTEL_OUTPUT_SDVO:
  5747. case INTEL_OUTPUT_HDMI:
  5748. is_sdvo = true;
  5749. break;
  5750. }
  5751. num_connectors++;
  5752. }
  5753. /* Enable autotuning of the PLL clock (if permissible) */
  5754. factor = 21;
  5755. if (is_lvds) {
  5756. if ((intel_panel_use_ssc(dev_priv) &&
  5757. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  5758. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  5759. factor = 25;
  5760. } else if (intel_crtc->config.sdvo_tv_clock)
  5761. factor = 20;
  5762. if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
  5763. *fp |= FP_CB_TUNE;
  5764. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  5765. *fp2 |= FP_CB_TUNE;
  5766. dpll = 0;
  5767. if (is_lvds)
  5768. dpll |= DPLLB_MODE_LVDS;
  5769. else
  5770. dpll |= DPLLB_MODE_DAC_SERIAL;
  5771. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  5772. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  5773. if (is_sdvo)
  5774. dpll |= DPLL_SDVO_HIGH_SPEED;
  5775. if (intel_crtc->config.has_dp_encoder)
  5776. dpll |= DPLL_SDVO_HIGH_SPEED;
  5777. /* compute bitmask from p1 value */
  5778. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5779. /* also FPA1 */
  5780. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5781. switch (intel_crtc->config.dpll.p2) {
  5782. case 5:
  5783. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5784. break;
  5785. case 7:
  5786. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5787. break;
  5788. case 10:
  5789. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5790. break;
  5791. case 14:
  5792. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5793. break;
  5794. }
  5795. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5796. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5797. else
  5798. dpll |= PLL_REF_INPUT_DREFCLK;
  5799. return dpll | DPLL_VCO_ENABLE;
  5800. }
  5801. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  5802. int x, int y,
  5803. struct drm_framebuffer *fb)
  5804. {
  5805. struct drm_device *dev = crtc->dev;
  5806. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5807. int num_connectors = 0;
  5808. intel_clock_t clock, reduced_clock;
  5809. u32 dpll = 0, fp = 0, fp2 = 0;
  5810. bool ok, has_reduced_clock = false;
  5811. bool is_lvds = false;
  5812. struct intel_encoder *encoder;
  5813. struct intel_shared_dpll *pll;
  5814. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5815. switch (encoder->type) {
  5816. case INTEL_OUTPUT_LVDS:
  5817. is_lvds = true;
  5818. break;
  5819. }
  5820. num_connectors++;
  5821. }
  5822. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  5823. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  5824. ok = ironlake_compute_clocks(crtc, &clock,
  5825. &has_reduced_clock, &reduced_clock);
  5826. if (!ok && !intel_crtc->config.clock_set) {
  5827. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5828. return -EINVAL;
  5829. }
  5830. /* Compat-code for transition, will disappear. */
  5831. if (!intel_crtc->config.clock_set) {
  5832. intel_crtc->config.dpll.n = clock.n;
  5833. intel_crtc->config.dpll.m1 = clock.m1;
  5834. intel_crtc->config.dpll.m2 = clock.m2;
  5835. intel_crtc->config.dpll.p1 = clock.p1;
  5836. intel_crtc->config.dpll.p2 = clock.p2;
  5837. }
  5838. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  5839. if (intel_crtc->config.has_pch_encoder) {
  5840. fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
  5841. if (has_reduced_clock)
  5842. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  5843. dpll = ironlake_compute_dpll(intel_crtc,
  5844. &fp, &reduced_clock,
  5845. has_reduced_clock ? &fp2 : NULL);
  5846. intel_crtc->config.dpll_hw_state.dpll = dpll;
  5847. intel_crtc->config.dpll_hw_state.fp0 = fp;
  5848. if (has_reduced_clock)
  5849. intel_crtc->config.dpll_hw_state.fp1 = fp2;
  5850. else
  5851. intel_crtc->config.dpll_hw_state.fp1 = fp;
  5852. pll = intel_get_shared_dpll(intel_crtc);
  5853. if (pll == NULL) {
  5854. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  5855. pipe_name(intel_crtc->pipe));
  5856. return -EINVAL;
  5857. }
  5858. } else
  5859. intel_put_shared_dpll(intel_crtc);
  5860. if (is_lvds && has_reduced_clock && i915.powersave)
  5861. intel_crtc->lowfreq_avail = true;
  5862. else
  5863. intel_crtc->lowfreq_avail = false;
  5864. return 0;
  5865. }
  5866. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  5867. struct intel_link_m_n *m_n)
  5868. {
  5869. struct drm_device *dev = crtc->base.dev;
  5870. struct drm_i915_private *dev_priv = dev->dev_private;
  5871. enum pipe pipe = crtc->pipe;
  5872. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  5873. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  5874. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  5875. & ~TU_SIZE_MASK;
  5876. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  5877. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  5878. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5879. }
  5880. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  5881. enum transcoder transcoder,
  5882. struct intel_link_m_n *m_n)
  5883. {
  5884. struct drm_device *dev = crtc->base.dev;
  5885. struct drm_i915_private *dev_priv = dev->dev_private;
  5886. enum pipe pipe = crtc->pipe;
  5887. if (INTEL_INFO(dev)->gen >= 5) {
  5888. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  5889. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  5890. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  5891. & ~TU_SIZE_MASK;
  5892. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  5893. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  5894. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5895. } else {
  5896. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  5897. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  5898. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  5899. & ~TU_SIZE_MASK;
  5900. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  5901. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  5902. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5903. }
  5904. }
  5905. void intel_dp_get_m_n(struct intel_crtc *crtc,
  5906. struct intel_crtc_config *pipe_config)
  5907. {
  5908. if (crtc->config.has_pch_encoder)
  5909. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  5910. else
  5911. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  5912. &pipe_config->dp_m_n);
  5913. }
  5914. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  5915. struct intel_crtc_config *pipe_config)
  5916. {
  5917. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  5918. &pipe_config->fdi_m_n);
  5919. }
  5920. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  5921. struct intel_crtc_config *pipe_config)
  5922. {
  5923. struct drm_device *dev = crtc->base.dev;
  5924. struct drm_i915_private *dev_priv = dev->dev_private;
  5925. uint32_t tmp;
  5926. tmp = I915_READ(PF_CTL(crtc->pipe));
  5927. if (tmp & PF_ENABLE) {
  5928. pipe_config->pch_pfit.enabled = true;
  5929. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  5930. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  5931. /* We currently do not free assignements of panel fitters on
  5932. * ivb/hsw (since we don't use the higher upscaling modes which
  5933. * differentiates them) so just WARN about this case for now. */
  5934. if (IS_GEN7(dev)) {
  5935. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  5936. PF_PIPE_SEL_IVB(crtc->pipe));
  5937. }
  5938. }
  5939. }
  5940. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  5941. struct intel_plane_config *plane_config)
  5942. {
  5943. struct drm_device *dev = crtc->base.dev;
  5944. struct drm_i915_private *dev_priv = dev->dev_private;
  5945. u32 val, base, offset;
  5946. int pipe = crtc->pipe, plane = crtc->plane;
  5947. int fourcc, pixel_format;
  5948. int aligned_height;
  5949. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5950. if (!crtc->base.primary->fb) {
  5951. DRM_DEBUG_KMS("failed to alloc fb\n");
  5952. return;
  5953. }
  5954. val = I915_READ(DSPCNTR(plane));
  5955. if (INTEL_INFO(dev)->gen >= 4)
  5956. if (val & DISPPLANE_TILED)
  5957. plane_config->tiled = true;
  5958. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5959. fourcc = intel_format_to_fourcc(pixel_format);
  5960. crtc->base.primary->fb->pixel_format = fourcc;
  5961. crtc->base.primary->fb->bits_per_pixel =
  5962. drm_format_plane_cpp(fourcc, 0) * 8;
  5963. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5964. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5965. offset = I915_READ(DSPOFFSET(plane));
  5966. } else {
  5967. if (plane_config->tiled)
  5968. offset = I915_READ(DSPTILEOFF(plane));
  5969. else
  5970. offset = I915_READ(DSPLINOFF(plane));
  5971. }
  5972. plane_config->base = base;
  5973. val = I915_READ(PIPESRC(pipe));
  5974. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5975. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5976. val = I915_READ(DSPSTRIDE(pipe));
  5977. crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
  5978. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5979. plane_config->tiled);
  5980. plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
  5981. aligned_height, PAGE_SIZE);
  5982. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5983. pipe, plane, crtc->base.primary->fb->width,
  5984. crtc->base.primary->fb->height,
  5985. crtc->base.primary->fb->bits_per_pixel, base,
  5986. crtc->base.primary->fb->pitches[0],
  5987. plane_config->size);
  5988. }
  5989. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  5990. struct intel_crtc_config *pipe_config)
  5991. {
  5992. struct drm_device *dev = crtc->base.dev;
  5993. struct drm_i915_private *dev_priv = dev->dev_private;
  5994. uint32_t tmp;
  5995. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5996. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5997. tmp = I915_READ(PIPECONF(crtc->pipe));
  5998. if (!(tmp & PIPECONF_ENABLE))
  5999. return false;
  6000. switch (tmp & PIPECONF_BPC_MASK) {
  6001. case PIPECONF_6BPC:
  6002. pipe_config->pipe_bpp = 18;
  6003. break;
  6004. case PIPECONF_8BPC:
  6005. pipe_config->pipe_bpp = 24;
  6006. break;
  6007. case PIPECONF_10BPC:
  6008. pipe_config->pipe_bpp = 30;
  6009. break;
  6010. case PIPECONF_12BPC:
  6011. pipe_config->pipe_bpp = 36;
  6012. break;
  6013. default:
  6014. break;
  6015. }
  6016. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6017. pipe_config->limited_color_range = true;
  6018. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6019. struct intel_shared_dpll *pll;
  6020. pipe_config->has_pch_encoder = true;
  6021. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6022. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6023. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6024. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6025. if (HAS_PCH_IBX(dev_priv->dev)) {
  6026. pipe_config->shared_dpll =
  6027. (enum intel_dpll_id) crtc->pipe;
  6028. } else {
  6029. tmp = I915_READ(PCH_DPLL_SEL);
  6030. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6031. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6032. else
  6033. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6034. }
  6035. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6036. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6037. &pipe_config->dpll_hw_state));
  6038. tmp = pipe_config->dpll_hw_state.dpll;
  6039. pipe_config->pixel_multiplier =
  6040. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6041. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6042. ironlake_pch_clock_get(crtc, pipe_config);
  6043. } else {
  6044. pipe_config->pixel_multiplier = 1;
  6045. }
  6046. intel_get_pipe_timings(crtc, pipe_config);
  6047. ironlake_get_pfit_config(crtc, pipe_config);
  6048. return true;
  6049. }
  6050. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6051. {
  6052. struct drm_device *dev = dev_priv->dev;
  6053. struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
  6054. struct intel_crtc *crtc;
  6055. for_each_intel_crtc(dev, crtc)
  6056. WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6057. pipe_name(crtc->pipe));
  6058. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6059. WARN(plls->spll_refcount, "SPLL enabled\n");
  6060. WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
  6061. WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
  6062. WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6063. WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6064. "CPU PWM1 enabled\n");
  6065. WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6066. "CPU PWM2 enabled\n");
  6067. WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6068. "PCH PWM1 enabled\n");
  6069. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6070. "Utility pin enabled\n");
  6071. WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6072. /*
  6073. * In theory we can still leave IRQs enabled, as long as only the HPD
  6074. * interrupts remain enabled. We used to check for that, but since it's
  6075. * gen-specific and since we only disable LCPLL after we fully disable
  6076. * the interrupts, the check below should be enough.
  6077. */
  6078. WARN(!dev_priv->pm.irqs_disabled, "IRQs enabled\n");
  6079. }
  6080. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6081. {
  6082. struct drm_device *dev = dev_priv->dev;
  6083. if (IS_HASWELL(dev)) {
  6084. mutex_lock(&dev_priv->rps.hw_lock);
  6085. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6086. val))
  6087. DRM_ERROR("Failed to disable D_COMP\n");
  6088. mutex_unlock(&dev_priv->rps.hw_lock);
  6089. } else {
  6090. I915_WRITE(D_COMP, val);
  6091. }
  6092. POSTING_READ(D_COMP);
  6093. }
  6094. /*
  6095. * This function implements pieces of two sequences from BSpec:
  6096. * - Sequence for display software to disable LCPLL
  6097. * - Sequence for display software to allow package C8+
  6098. * The steps implemented here are just the steps that actually touch the LCPLL
  6099. * register. Callers should take care of disabling all the display engine
  6100. * functions, doing the mode unset, fixing interrupts, etc.
  6101. */
  6102. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6103. bool switch_to_fclk, bool allow_power_down)
  6104. {
  6105. uint32_t val;
  6106. assert_can_disable_lcpll(dev_priv);
  6107. val = I915_READ(LCPLL_CTL);
  6108. if (switch_to_fclk) {
  6109. val |= LCPLL_CD_SOURCE_FCLK;
  6110. I915_WRITE(LCPLL_CTL, val);
  6111. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6112. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6113. DRM_ERROR("Switching to FCLK failed\n");
  6114. val = I915_READ(LCPLL_CTL);
  6115. }
  6116. val |= LCPLL_PLL_DISABLE;
  6117. I915_WRITE(LCPLL_CTL, val);
  6118. POSTING_READ(LCPLL_CTL);
  6119. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6120. DRM_ERROR("LCPLL still locked\n");
  6121. val = I915_READ(D_COMP);
  6122. val |= D_COMP_COMP_DISABLE;
  6123. hsw_write_dcomp(dev_priv, val);
  6124. ndelay(100);
  6125. if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
  6126. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6127. if (allow_power_down) {
  6128. val = I915_READ(LCPLL_CTL);
  6129. val |= LCPLL_POWER_DOWN_ALLOW;
  6130. I915_WRITE(LCPLL_CTL, val);
  6131. POSTING_READ(LCPLL_CTL);
  6132. }
  6133. }
  6134. /*
  6135. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6136. * source.
  6137. */
  6138. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6139. {
  6140. uint32_t val;
  6141. unsigned long irqflags;
  6142. val = I915_READ(LCPLL_CTL);
  6143. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6144. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6145. return;
  6146. /*
  6147. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6148. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6149. *
  6150. * The other problem is that hsw_restore_lcpll() is called as part of
  6151. * the runtime PM resume sequence, so we can't just call
  6152. * gen6_gt_force_wake_get() because that function calls
  6153. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6154. * while we are on the resume sequence. So to solve this problem we have
  6155. * to call special forcewake code that doesn't touch runtime PM and
  6156. * doesn't enable the forcewake delayed work.
  6157. */
  6158. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6159. if (dev_priv->uncore.forcewake_count++ == 0)
  6160. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6161. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6162. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6163. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6164. I915_WRITE(LCPLL_CTL, val);
  6165. POSTING_READ(LCPLL_CTL);
  6166. }
  6167. val = I915_READ(D_COMP);
  6168. val |= D_COMP_COMP_FORCE;
  6169. val &= ~D_COMP_COMP_DISABLE;
  6170. hsw_write_dcomp(dev_priv, val);
  6171. val = I915_READ(LCPLL_CTL);
  6172. val &= ~LCPLL_PLL_DISABLE;
  6173. I915_WRITE(LCPLL_CTL, val);
  6174. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6175. DRM_ERROR("LCPLL not locked yet\n");
  6176. if (val & LCPLL_CD_SOURCE_FCLK) {
  6177. val = I915_READ(LCPLL_CTL);
  6178. val &= ~LCPLL_CD_SOURCE_FCLK;
  6179. I915_WRITE(LCPLL_CTL, val);
  6180. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6181. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6182. DRM_ERROR("Switching back to LCPLL failed\n");
  6183. }
  6184. /* See the big comment above. */
  6185. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6186. if (--dev_priv->uncore.forcewake_count == 0)
  6187. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6188. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6189. }
  6190. /*
  6191. * Package states C8 and deeper are really deep PC states that can only be
  6192. * reached when all the devices on the system allow it, so even if the graphics
  6193. * device allows PC8+, it doesn't mean the system will actually get to these
  6194. * states. Our driver only allows PC8+ when going into runtime PM.
  6195. *
  6196. * The requirements for PC8+ are that all the outputs are disabled, the power
  6197. * well is disabled and most interrupts are disabled, and these are also
  6198. * requirements for runtime PM. When these conditions are met, we manually do
  6199. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6200. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6201. * hang the machine.
  6202. *
  6203. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6204. * the state of some registers, so when we come back from PC8+ we need to
  6205. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6206. * need to take care of the registers kept by RC6. Notice that this happens even
  6207. * if we don't put the device in PCI D3 state (which is what currently happens
  6208. * because of the runtime PM support).
  6209. *
  6210. * For more, read "Display Sequences for Package C8" on the hardware
  6211. * documentation.
  6212. */
  6213. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6214. {
  6215. struct drm_device *dev = dev_priv->dev;
  6216. uint32_t val;
  6217. DRM_DEBUG_KMS("Enabling package C8+\n");
  6218. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6219. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6220. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6221. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6222. }
  6223. lpt_disable_clkout_dp(dev);
  6224. hsw_disable_lcpll(dev_priv, true, true);
  6225. }
  6226. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6227. {
  6228. struct drm_device *dev = dev_priv->dev;
  6229. uint32_t val;
  6230. DRM_DEBUG_KMS("Disabling package C8+\n");
  6231. hsw_restore_lcpll(dev_priv);
  6232. lpt_init_pch_refclk(dev);
  6233. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6234. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6235. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6236. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6237. }
  6238. intel_prepare_ddi(dev);
  6239. }
  6240. static void snb_modeset_global_resources(struct drm_device *dev)
  6241. {
  6242. modeset_update_crtc_power_domains(dev);
  6243. }
  6244. static void haswell_modeset_global_resources(struct drm_device *dev)
  6245. {
  6246. modeset_update_crtc_power_domains(dev);
  6247. }
  6248. static int haswell_crtc_mode_set(struct drm_crtc *crtc,
  6249. int x, int y,
  6250. struct drm_framebuffer *fb)
  6251. {
  6252. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6253. if (!intel_ddi_pll_select(intel_crtc))
  6254. return -EINVAL;
  6255. intel_ddi_pll_enable(intel_crtc);
  6256. intel_crtc->lowfreq_avail = false;
  6257. return 0;
  6258. }
  6259. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6260. struct intel_crtc_config *pipe_config)
  6261. {
  6262. struct drm_device *dev = crtc->base.dev;
  6263. struct drm_i915_private *dev_priv = dev->dev_private;
  6264. enum intel_display_power_domain pfit_domain;
  6265. uint32_t tmp;
  6266. if (!intel_display_power_enabled(dev_priv,
  6267. POWER_DOMAIN_PIPE(crtc->pipe)))
  6268. return false;
  6269. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6270. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6271. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6272. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6273. enum pipe trans_edp_pipe;
  6274. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6275. default:
  6276. WARN(1, "unknown pipe linked to edp transcoder\n");
  6277. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6278. case TRANS_DDI_EDP_INPUT_A_ON:
  6279. trans_edp_pipe = PIPE_A;
  6280. break;
  6281. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6282. trans_edp_pipe = PIPE_B;
  6283. break;
  6284. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6285. trans_edp_pipe = PIPE_C;
  6286. break;
  6287. }
  6288. if (trans_edp_pipe == crtc->pipe)
  6289. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6290. }
  6291. if (!intel_display_power_enabled(dev_priv,
  6292. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6293. return false;
  6294. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6295. if (!(tmp & PIPECONF_ENABLE))
  6296. return false;
  6297. /*
  6298. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6299. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6300. * the PCH transcoder is on.
  6301. */
  6302. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6303. if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
  6304. I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6305. pipe_config->has_pch_encoder = true;
  6306. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6307. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6308. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6309. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6310. }
  6311. intel_get_pipe_timings(crtc, pipe_config);
  6312. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6313. if (intel_display_power_enabled(dev_priv, pfit_domain))
  6314. ironlake_get_pfit_config(crtc, pipe_config);
  6315. if (IS_HASWELL(dev))
  6316. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6317. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6318. pipe_config->pixel_multiplier = 1;
  6319. return true;
  6320. }
  6321. static struct {
  6322. int clock;
  6323. u32 config;
  6324. } hdmi_audio_clock[] = {
  6325. { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  6326. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  6327. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  6328. { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  6329. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  6330. { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  6331. { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  6332. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  6333. { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  6334. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  6335. };
  6336. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  6337. static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
  6338. {
  6339. int i;
  6340. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  6341. if (mode->clock == hdmi_audio_clock[i].clock)
  6342. break;
  6343. }
  6344. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  6345. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
  6346. i = 1;
  6347. }
  6348. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  6349. hdmi_audio_clock[i].clock,
  6350. hdmi_audio_clock[i].config);
  6351. return hdmi_audio_clock[i].config;
  6352. }
  6353. static bool intel_eld_uptodate(struct drm_connector *connector,
  6354. int reg_eldv, uint32_t bits_eldv,
  6355. int reg_elda, uint32_t bits_elda,
  6356. int reg_edid)
  6357. {
  6358. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6359. uint8_t *eld = connector->eld;
  6360. uint32_t i;
  6361. i = I915_READ(reg_eldv);
  6362. i &= bits_eldv;
  6363. if (!eld[0])
  6364. return !i;
  6365. if (!i)
  6366. return false;
  6367. i = I915_READ(reg_elda);
  6368. i &= ~bits_elda;
  6369. I915_WRITE(reg_elda, i);
  6370. for (i = 0; i < eld[2]; i++)
  6371. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  6372. return false;
  6373. return true;
  6374. }
  6375. static void g4x_write_eld(struct drm_connector *connector,
  6376. struct drm_crtc *crtc,
  6377. struct drm_display_mode *mode)
  6378. {
  6379. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6380. uint8_t *eld = connector->eld;
  6381. uint32_t eldv;
  6382. uint32_t len;
  6383. uint32_t i;
  6384. i = I915_READ(G4X_AUD_VID_DID);
  6385. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  6386. eldv = G4X_ELDV_DEVCL_DEVBLC;
  6387. else
  6388. eldv = G4X_ELDV_DEVCTG;
  6389. if (intel_eld_uptodate(connector,
  6390. G4X_AUD_CNTL_ST, eldv,
  6391. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  6392. G4X_HDMIW_HDMIEDID))
  6393. return;
  6394. i = I915_READ(G4X_AUD_CNTL_ST);
  6395. i &= ~(eldv | G4X_ELD_ADDR);
  6396. len = (i >> 9) & 0x1f; /* ELD buffer size */
  6397. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6398. if (!eld[0])
  6399. return;
  6400. len = min_t(uint8_t, eld[2], len);
  6401. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6402. for (i = 0; i < len; i++)
  6403. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  6404. i = I915_READ(G4X_AUD_CNTL_ST);
  6405. i |= eldv;
  6406. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6407. }
  6408. static void haswell_write_eld(struct drm_connector *connector,
  6409. struct drm_crtc *crtc,
  6410. struct drm_display_mode *mode)
  6411. {
  6412. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6413. uint8_t *eld = connector->eld;
  6414. uint32_t eldv;
  6415. uint32_t i;
  6416. int len;
  6417. int pipe = to_intel_crtc(crtc)->pipe;
  6418. int tmp;
  6419. int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
  6420. int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
  6421. int aud_config = HSW_AUD_CFG(pipe);
  6422. int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
  6423. /* Audio output enable */
  6424. DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
  6425. tmp = I915_READ(aud_cntrl_st2);
  6426. tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
  6427. I915_WRITE(aud_cntrl_st2, tmp);
  6428. POSTING_READ(aud_cntrl_st2);
  6429. assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  6430. /* Set ELD valid state */
  6431. tmp = I915_READ(aud_cntrl_st2);
  6432. DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
  6433. tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
  6434. I915_WRITE(aud_cntrl_st2, tmp);
  6435. tmp = I915_READ(aud_cntrl_st2);
  6436. DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
  6437. /* Enable HDMI mode */
  6438. tmp = I915_READ(aud_config);
  6439. DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
  6440. /* clear N_programing_enable and N_value_index */
  6441. tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
  6442. I915_WRITE(aud_config, tmp);
  6443. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6444. eldv = AUDIO_ELD_VALID_A << (pipe * 4);
  6445. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6446. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6447. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6448. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6449. } else {
  6450. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6451. }
  6452. if (intel_eld_uptodate(connector,
  6453. aud_cntrl_st2, eldv,
  6454. aud_cntl_st, IBX_ELD_ADDRESS,
  6455. hdmiw_hdmiedid))
  6456. return;
  6457. i = I915_READ(aud_cntrl_st2);
  6458. i &= ~eldv;
  6459. I915_WRITE(aud_cntrl_st2, i);
  6460. if (!eld[0])
  6461. return;
  6462. i = I915_READ(aud_cntl_st);
  6463. i &= ~IBX_ELD_ADDRESS;
  6464. I915_WRITE(aud_cntl_st, i);
  6465. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  6466. DRM_DEBUG_DRIVER("port num:%d\n", i);
  6467. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6468. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6469. for (i = 0; i < len; i++)
  6470. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6471. i = I915_READ(aud_cntrl_st2);
  6472. i |= eldv;
  6473. I915_WRITE(aud_cntrl_st2, i);
  6474. }
  6475. static void ironlake_write_eld(struct drm_connector *connector,
  6476. struct drm_crtc *crtc,
  6477. struct drm_display_mode *mode)
  6478. {
  6479. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6480. uint8_t *eld = connector->eld;
  6481. uint32_t eldv;
  6482. uint32_t i;
  6483. int len;
  6484. int hdmiw_hdmiedid;
  6485. int aud_config;
  6486. int aud_cntl_st;
  6487. int aud_cntrl_st2;
  6488. int pipe = to_intel_crtc(crtc)->pipe;
  6489. if (HAS_PCH_IBX(connector->dev)) {
  6490. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  6491. aud_config = IBX_AUD_CFG(pipe);
  6492. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  6493. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  6494. } else if (IS_VALLEYVIEW(connector->dev)) {
  6495. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  6496. aud_config = VLV_AUD_CFG(pipe);
  6497. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  6498. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  6499. } else {
  6500. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  6501. aud_config = CPT_AUD_CFG(pipe);
  6502. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  6503. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  6504. }
  6505. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6506. if (IS_VALLEYVIEW(connector->dev)) {
  6507. struct intel_encoder *intel_encoder;
  6508. struct intel_digital_port *intel_dig_port;
  6509. intel_encoder = intel_attached_encoder(connector);
  6510. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  6511. i = intel_dig_port->port;
  6512. } else {
  6513. i = I915_READ(aud_cntl_st);
  6514. i = (i >> 29) & DIP_PORT_SEL_MASK;
  6515. /* DIP_Port_Select, 0x1 = PortB */
  6516. }
  6517. if (!i) {
  6518. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  6519. /* operate blindly on all ports */
  6520. eldv = IBX_ELD_VALIDB;
  6521. eldv |= IBX_ELD_VALIDB << 4;
  6522. eldv |= IBX_ELD_VALIDB << 8;
  6523. } else {
  6524. DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
  6525. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  6526. }
  6527. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6528. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6529. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6530. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6531. } else {
  6532. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6533. }
  6534. if (intel_eld_uptodate(connector,
  6535. aud_cntrl_st2, eldv,
  6536. aud_cntl_st, IBX_ELD_ADDRESS,
  6537. hdmiw_hdmiedid))
  6538. return;
  6539. i = I915_READ(aud_cntrl_st2);
  6540. i &= ~eldv;
  6541. I915_WRITE(aud_cntrl_st2, i);
  6542. if (!eld[0])
  6543. return;
  6544. i = I915_READ(aud_cntl_st);
  6545. i &= ~IBX_ELD_ADDRESS;
  6546. I915_WRITE(aud_cntl_st, i);
  6547. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6548. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6549. for (i = 0; i < len; i++)
  6550. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6551. i = I915_READ(aud_cntrl_st2);
  6552. i |= eldv;
  6553. I915_WRITE(aud_cntrl_st2, i);
  6554. }
  6555. void intel_write_eld(struct drm_encoder *encoder,
  6556. struct drm_display_mode *mode)
  6557. {
  6558. struct drm_crtc *crtc = encoder->crtc;
  6559. struct drm_connector *connector;
  6560. struct drm_device *dev = encoder->dev;
  6561. struct drm_i915_private *dev_priv = dev->dev_private;
  6562. connector = drm_select_eld(encoder, mode);
  6563. if (!connector)
  6564. return;
  6565. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6566. connector->base.id,
  6567. connector->name,
  6568. connector->encoder->base.id,
  6569. connector->encoder->name);
  6570. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  6571. if (dev_priv->display.write_eld)
  6572. dev_priv->display.write_eld(connector, crtc, mode);
  6573. }
  6574. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6575. {
  6576. struct drm_device *dev = crtc->dev;
  6577. struct drm_i915_private *dev_priv = dev->dev_private;
  6578. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6579. bool visible = base != 0;
  6580. u32 cntl;
  6581. if (intel_crtc->cursor_visible == visible)
  6582. return;
  6583. cntl = I915_READ(_CURACNTR);
  6584. if (visible) {
  6585. /* On these chipsets we can only modify the base whilst
  6586. * the cursor is disabled.
  6587. */
  6588. I915_WRITE(_CURABASE, base);
  6589. cntl &= ~(CURSOR_FORMAT_MASK);
  6590. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  6591. cntl |= CURSOR_ENABLE |
  6592. CURSOR_GAMMA_ENABLE |
  6593. CURSOR_FORMAT_ARGB;
  6594. } else
  6595. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  6596. I915_WRITE(_CURACNTR, cntl);
  6597. intel_crtc->cursor_visible = visible;
  6598. }
  6599. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6600. {
  6601. struct drm_device *dev = crtc->dev;
  6602. struct drm_i915_private *dev_priv = dev->dev_private;
  6603. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6604. int pipe = intel_crtc->pipe;
  6605. bool visible = base != 0;
  6606. if (intel_crtc->cursor_visible != visible) {
  6607. int16_t width = intel_crtc->cursor_width;
  6608. uint32_t cntl = I915_READ(CURCNTR(pipe));
  6609. if (base) {
  6610. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  6611. cntl |= MCURSOR_GAMMA_ENABLE;
  6612. switch (width) {
  6613. case 64:
  6614. cntl |= CURSOR_MODE_64_ARGB_AX;
  6615. break;
  6616. case 128:
  6617. cntl |= CURSOR_MODE_128_ARGB_AX;
  6618. break;
  6619. case 256:
  6620. cntl |= CURSOR_MODE_256_ARGB_AX;
  6621. break;
  6622. default:
  6623. WARN_ON(1);
  6624. return;
  6625. }
  6626. cntl |= pipe << 28; /* Connect to correct pipe */
  6627. } else {
  6628. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  6629. cntl |= CURSOR_MODE_DISABLE;
  6630. }
  6631. I915_WRITE(CURCNTR(pipe), cntl);
  6632. intel_crtc->cursor_visible = visible;
  6633. }
  6634. /* and commit changes on next vblank */
  6635. POSTING_READ(CURCNTR(pipe));
  6636. I915_WRITE(CURBASE(pipe), base);
  6637. POSTING_READ(CURBASE(pipe));
  6638. }
  6639. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  6640. {
  6641. struct drm_device *dev = crtc->dev;
  6642. struct drm_i915_private *dev_priv = dev->dev_private;
  6643. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6644. int pipe = intel_crtc->pipe;
  6645. bool visible = base != 0;
  6646. if (intel_crtc->cursor_visible != visible) {
  6647. int16_t width = intel_crtc->cursor_width;
  6648. uint32_t cntl = I915_READ(CURCNTR(pipe));
  6649. if (base) {
  6650. cntl &= ~CURSOR_MODE;
  6651. cntl |= MCURSOR_GAMMA_ENABLE;
  6652. switch (width) {
  6653. case 64:
  6654. cntl |= CURSOR_MODE_64_ARGB_AX;
  6655. break;
  6656. case 128:
  6657. cntl |= CURSOR_MODE_128_ARGB_AX;
  6658. break;
  6659. case 256:
  6660. cntl |= CURSOR_MODE_256_ARGB_AX;
  6661. break;
  6662. default:
  6663. WARN_ON(1);
  6664. return;
  6665. }
  6666. } else {
  6667. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  6668. cntl |= CURSOR_MODE_DISABLE;
  6669. }
  6670. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  6671. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6672. cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
  6673. }
  6674. I915_WRITE(CURCNTR(pipe), cntl);
  6675. intel_crtc->cursor_visible = visible;
  6676. }
  6677. /* and commit changes on next vblank */
  6678. POSTING_READ(CURCNTR(pipe));
  6679. I915_WRITE(CURBASE(pipe), base);
  6680. POSTING_READ(CURBASE(pipe));
  6681. }
  6682. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6683. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6684. bool on)
  6685. {
  6686. struct drm_device *dev = crtc->dev;
  6687. struct drm_i915_private *dev_priv = dev->dev_private;
  6688. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6689. int pipe = intel_crtc->pipe;
  6690. int x = intel_crtc->cursor_x;
  6691. int y = intel_crtc->cursor_y;
  6692. u32 base = 0, pos = 0;
  6693. bool visible;
  6694. if (on)
  6695. base = intel_crtc->cursor_addr;
  6696. if (x >= intel_crtc->config.pipe_src_w)
  6697. base = 0;
  6698. if (y >= intel_crtc->config.pipe_src_h)
  6699. base = 0;
  6700. if (x < 0) {
  6701. if (x + intel_crtc->cursor_width <= 0)
  6702. base = 0;
  6703. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  6704. x = -x;
  6705. }
  6706. pos |= x << CURSOR_X_SHIFT;
  6707. if (y < 0) {
  6708. if (y + intel_crtc->cursor_height <= 0)
  6709. base = 0;
  6710. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  6711. y = -y;
  6712. }
  6713. pos |= y << CURSOR_Y_SHIFT;
  6714. visible = base != 0;
  6715. if (!visible && !intel_crtc->cursor_visible)
  6716. return;
  6717. I915_WRITE(CURPOS(pipe), pos);
  6718. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev))
  6719. ivb_update_cursor(crtc, base);
  6720. else if (IS_845G(dev) || IS_I865G(dev))
  6721. i845_update_cursor(crtc, base);
  6722. else
  6723. i9xx_update_cursor(crtc, base);
  6724. }
  6725. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  6726. struct drm_file *file,
  6727. uint32_t handle,
  6728. uint32_t width, uint32_t height)
  6729. {
  6730. struct drm_device *dev = crtc->dev;
  6731. struct drm_i915_private *dev_priv = dev->dev_private;
  6732. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6733. struct drm_i915_gem_object *obj;
  6734. unsigned old_width;
  6735. uint32_t addr;
  6736. int ret;
  6737. /* if we want to turn off the cursor ignore width and height */
  6738. if (!handle) {
  6739. DRM_DEBUG_KMS("cursor off\n");
  6740. addr = 0;
  6741. obj = NULL;
  6742. mutex_lock(&dev->struct_mutex);
  6743. goto finish;
  6744. }
  6745. /* Check for which cursor types we support */
  6746. if (!((width == 64 && height == 64) ||
  6747. (width == 128 && height == 128 && !IS_GEN2(dev)) ||
  6748. (width == 256 && height == 256 && !IS_GEN2(dev)))) {
  6749. DRM_DEBUG("Cursor dimension not supported\n");
  6750. return -EINVAL;
  6751. }
  6752. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  6753. if (&obj->base == NULL)
  6754. return -ENOENT;
  6755. if (obj->base.size < width * height * 4) {
  6756. DRM_DEBUG_KMS("buffer is to small\n");
  6757. ret = -ENOMEM;
  6758. goto fail;
  6759. }
  6760. /* we only need to pin inside GTT if cursor is non-phy */
  6761. mutex_lock(&dev->struct_mutex);
  6762. if (!INTEL_INFO(dev)->cursor_needs_physical) {
  6763. unsigned alignment;
  6764. if (obj->tiling_mode) {
  6765. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  6766. ret = -EINVAL;
  6767. goto fail_locked;
  6768. }
  6769. /* Note that the w/a also requires 2 PTE of padding following
  6770. * the bo. We currently fill all unused PTE with the shadow
  6771. * page and so we should always have valid PTE following the
  6772. * cursor preventing the VT-d warning.
  6773. */
  6774. alignment = 0;
  6775. if (need_vtd_wa(dev))
  6776. alignment = 64*1024;
  6777. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  6778. if (ret) {
  6779. DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
  6780. goto fail_locked;
  6781. }
  6782. ret = i915_gem_object_put_fence(obj);
  6783. if (ret) {
  6784. DRM_DEBUG_KMS("failed to release fence for cursor");
  6785. goto fail_unpin;
  6786. }
  6787. addr = i915_gem_obj_ggtt_offset(obj);
  6788. } else {
  6789. int align = IS_I830(dev) ? 16 * 1024 : 256;
  6790. ret = i915_gem_attach_phys_object(dev, obj,
  6791. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  6792. align);
  6793. if (ret) {
  6794. DRM_DEBUG_KMS("failed to attach phys object\n");
  6795. goto fail_locked;
  6796. }
  6797. addr = obj->phys_obj->handle->busaddr;
  6798. }
  6799. if (IS_GEN2(dev))
  6800. I915_WRITE(CURSIZE, (height << 12) | width);
  6801. finish:
  6802. if (intel_crtc->cursor_bo) {
  6803. if (INTEL_INFO(dev)->cursor_needs_physical) {
  6804. if (intel_crtc->cursor_bo != obj)
  6805. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  6806. } else
  6807. i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
  6808. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  6809. }
  6810. mutex_unlock(&dev->struct_mutex);
  6811. old_width = intel_crtc->cursor_width;
  6812. intel_crtc->cursor_addr = addr;
  6813. intel_crtc->cursor_bo = obj;
  6814. intel_crtc->cursor_width = width;
  6815. intel_crtc->cursor_height = height;
  6816. if (intel_crtc->active) {
  6817. if (old_width != width)
  6818. intel_update_watermarks(crtc);
  6819. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6820. }
  6821. return 0;
  6822. fail_unpin:
  6823. i915_gem_object_unpin_from_display_plane(obj);
  6824. fail_locked:
  6825. mutex_unlock(&dev->struct_mutex);
  6826. fail:
  6827. drm_gem_object_unreference_unlocked(&obj->base);
  6828. return ret;
  6829. }
  6830. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  6831. {
  6832. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6833. intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
  6834. intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
  6835. if (intel_crtc->active)
  6836. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6837. return 0;
  6838. }
  6839. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  6840. u16 *blue, uint32_t start, uint32_t size)
  6841. {
  6842. int end = (start + size > 256) ? 256 : start + size, i;
  6843. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6844. for (i = start; i < end; i++) {
  6845. intel_crtc->lut_r[i] = red[i] >> 8;
  6846. intel_crtc->lut_g[i] = green[i] >> 8;
  6847. intel_crtc->lut_b[i] = blue[i] >> 8;
  6848. }
  6849. intel_crtc_load_lut(crtc);
  6850. }
  6851. /* VESA 640x480x72Hz mode to set on the pipe */
  6852. static struct drm_display_mode load_detect_mode = {
  6853. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  6854. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  6855. };
  6856. struct drm_framebuffer *
  6857. __intel_framebuffer_create(struct drm_device *dev,
  6858. struct drm_mode_fb_cmd2 *mode_cmd,
  6859. struct drm_i915_gem_object *obj)
  6860. {
  6861. struct intel_framebuffer *intel_fb;
  6862. int ret;
  6863. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6864. if (!intel_fb) {
  6865. drm_gem_object_unreference_unlocked(&obj->base);
  6866. return ERR_PTR(-ENOMEM);
  6867. }
  6868. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  6869. if (ret)
  6870. goto err;
  6871. return &intel_fb->base;
  6872. err:
  6873. drm_gem_object_unreference_unlocked(&obj->base);
  6874. kfree(intel_fb);
  6875. return ERR_PTR(ret);
  6876. }
  6877. static struct drm_framebuffer *
  6878. intel_framebuffer_create(struct drm_device *dev,
  6879. struct drm_mode_fb_cmd2 *mode_cmd,
  6880. struct drm_i915_gem_object *obj)
  6881. {
  6882. struct drm_framebuffer *fb;
  6883. int ret;
  6884. ret = i915_mutex_lock_interruptible(dev);
  6885. if (ret)
  6886. return ERR_PTR(ret);
  6887. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  6888. mutex_unlock(&dev->struct_mutex);
  6889. return fb;
  6890. }
  6891. static u32
  6892. intel_framebuffer_pitch_for_width(int width, int bpp)
  6893. {
  6894. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  6895. return ALIGN(pitch, 64);
  6896. }
  6897. static u32
  6898. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  6899. {
  6900. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  6901. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  6902. }
  6903. static struct drm_framebuffer *
  6904. intel_framebuffer_create_for_mode(struct drm_device *dev,
  6905. struct drm_display_mode *mode,
  6906. int depth, int bpp)
  6907. {
  6908. struct drm_i915_gem_object *obj;
  6909. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  6910. obj = i915_gem_alloc_object(dev,
  6911. intel_framebuffer_size_for_mode(mode, bpp));
  6912. if (obj == NULL)
  6913. return ERR_PTR(-ENOMEM);
  6914. mode_cmd.width = mode->hdisplay;
  6915. mode_cmd.height = mode->vdisplay;
  6916. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  6917. bpp);
  6918. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  6919. return intel_framebuffer_create(dev, &mode_cmd, obj);
  6920. }
  6921. static struct drm_framebuffer *
  6922. mode_fits_in_fbdev(struct drm_device *dev,
  6923. struct drm_display_mode *mode)
  6924. {
  6925. #ifdef CONFIG_DRM_I915_FBDEV
  6926. struct drm_i915_private *dev_priv = dev->dev_private;
  6927. struct drm_i915_gem_object *obj;
  6928. struct drm_framebuffer *fb;
  6929. if (!dev_priv->fbdev)
  6930. return NULL;
  6931. if (!dev_priv->fbdev->fb)
  6932. return NULL;
  6933. obj = dev_priv->fbdev->fb->obj;
  6934. BUG_ON(!obj);
  6935. fb = &dev_priv->fbdev->fb->base;
  6936. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  6937. fb->bits_per_pixel))
  6938. return NULL;
  6939. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  6940. return NULL;
  6941. return fb;
  6942. #else
  6943. return NULL;
  6944. #endif
  6945. }
  6946. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  6947. struct drm_display_mode *mode,
  6948. struct intel_load_detect_pipe *old,
  6949. struct drm_modeset_acquire_ctx *ctx)
  6950. {
  6951. struct intel_crtc *intel_crtc;
  6952. struct intel_encoder *intel_encoder =
  6953. intel_attached_encoder(connector);
  6954. struct drm_crtc *possible_crtc;
  6955. struct drm_encoder *encoder = &intel_encoder->base;
  6956. struct drm_crtc *crtc = NULL;
  6957. struct drm_device *dev = encoder->dev;
  6958. struct drm_framebuffer *fb;
  6959. struct drm_mode_config *config = &dev->mode_config;
  6960. int ret, i = -1;
  6961. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6962. connector->base.id, connector->name,
  6963. encoder->base.id, encoder->name);
  6964. drm_modeset_acquire_init(ctx, 0);
  6965. retry:
  6966. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  6967. if (ret)
  6968. goto fail_unlock;
  6969. /*
  6970. * Algorithm gets a little messy:
  6971. *
  6972. * - if the connector already has an assigned crtc, use it (but make
  6973. * sure it's on first)
  6974. *
  6975. * - try to find the first unused crtc that can drive this connector,
  6976. * and use that if we find one
  6977. */
  6978. /* See if we already have a CRTC for this connector */
  6979. if (encoder->crtc) {
  6980. crtc = encoder->crtc;
  6981. ret = drm_modeset_lock(&crtc->mutex, ctx);
  6982. if (ret)
  6983. goto fail_unlock;
  6984. old->dpms_mode = connector->dpms;
  6985. old->load_detect_temp = false;
  6986. /* Make sure the crtc and connector are running */
  6987. if (connector->dpms != DRM_MODE_DPMS_ON)
  6988. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  6989. return true;
  6990. }
  6991. /* Find an unused one (if possible) */
  6992. for_each_crtc(dev, possible_crtc) {
  6993. i++;
  6994. if (!(encoder->possible_crtcs & (1 << i)))
  6995. continue;
  6996. if (!possible_crtc->enabled) {
  6997. crtc = possible_crtc;
  6998. break;
  6999. }
  7000. }
  7001. /*
  7002. * If we didn't find an unused CRTC, don't use any.
  7003. */
  7004. if (!crtc) {
  7005. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7006. goto fail_unlock;
  7007. }
  7008. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7009. if (ret)
  7010. goto fail_unlock;
  7011. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7012. to_intel_connector(connector)->new_encoder = intel_encoder;
  7013. intel_crtc = to_intel_crtc(crtc);
  7014. intel_crtc->new_enabled = true;
  7015. intel_crtc->new_config = &intel_crtc->config;
  7016. old->dpms_mode = connector->dpms;
  7017. old->load_detect_temp = true;
  7018. old->release_fb = NULL;
  7019. if (!mode)
  7020. mode = &load_detect_mode;
  7021. /* We need a framebuffer large enough to accommodate all accesses
  7022. * that the plane may generate whilst we perform load detection.
  7023. * We can not rely on the fbcon either being present (we get called
  7024. * during its initialisation to detect all boot displays, or it may
  7025. * not even exist) or that it is large enough to satisfy the
  7026. * requested mode.
  7027. */
  7028. fb = mode_fits_in_fbdev(dev, mode);
  7029. if (fb == NULL) {
  7030. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7031. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7032. old->release_fb = fb;
  7033. } else
  7034. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7035. if (IS_ERR(fb)) {
  7036. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7037. goto fail;
  7038. }
  7039. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7040. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7041. if (old->release_fb)
  7042. old->release_fb->funcs->destroy(old->release_fb);
  7043. goto fail;
  7044. }
  7045. /* let the connector get through one full cycle before testing */
  7046. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7047. return true;
  7048. fail:
  7049. intel_crtc->new_enabled = crtc->enabled;
  7050. if (intel_crtc->new_enabled)
  7051. intel_crtc->new_config = &intel_crtc->config;
  7052. else
  7053. intel_crtc->new_config = NULL;
  7054. fail_unlock:
  7055. if (ret == -EDEADLK) {
  7056. drm_modeset_backoff(ctx);
  7057. goto retry;
  7058. }
  7059. drm_modeset_drop_locks(ctx);
  7060. drm_modeset_acquire_fini(ctx);
  7061. return false;
  7062. }
  7063. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7064. struct intel_load_detect_pipe *old,
  7065. struct drm_modeset_acquire_ctx *ctx)
  7066. {
  7067. struct intel_encoder *intel_encoder =
  7068. intel_attached_encoder(connector);
  7069. struct drm_encoder *encoder = &intel_encoder->base;
  7070. struct drm_crtc *crtc = encoder->crtc;
  7071. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7072. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7073. connector->base.id, connector->name,
  7074. encoder->base.id, encoder->name);
  7075. if (old->load_detect_temp) {
  7076. to_intel_connector(connector)->new_encoder = NULL;
  7077. intel_encoder->new_crtc = NULL;
  7078. intel_crtc->new_enabled = false;
  7079. intel_crtc->new_config = NULL;
  7080. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7081. if (old->release_fb) {
  7082. drm_framebuffer_unregister_private(old->release_fb);
  7083. drm_framebuffer_unreference(old->release_fb);
  7084. }
  7085. goto unlock;
  7086. return;
  7087. }
  7088. /* Switch crtc and encoder back off if necessary */
  7089. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7090. connector->funcs->dpms(connector, old->dpms_mode);
  7091. unlock:
  7092. drm_modeset_drop_locks(ctx);
  7093. drm_modeset_acquire_fini(ctx);
  7094. }
  7095. static int i9xx_pll_refclk(struct drm_device *dev,
  7096. const struct intel_crtc_config *pipe_config)
  7097. {
  7098. struct drm_i915_private *dev_priv = dev->dev_private;
  7099. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7100. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7101. return dev_priv->vbt.lvds_ssc_freq;
  7102. else if (HAS_PCH_SPLIT(dev))
  7103. return 120000;
  7104. else if (!IS_GEN2(dev))
  7105. return 96000;
  7106. else
  7107. return 48000;
  7108. }
  7109. /* Returns the clock of the currently programmed mode of the given pipe. */
  7110. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7111. struct intel_crtc_config *pipe_config)
  7112. {
  7113. struct drm_device *dev = crtc->base.dev;
  7114. struct drm_i915_private *dev_priv = dev->dev_private;
  7115. int pipe = pipe_config->cpu_transcoder;
  7116. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7117. u32 fp;
  7118. intel_clock_t clock;
  7119. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7120. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7121. fp = pipe_config->dpll_hw_state.fp0;
  7122. else
  7123. fp = pipe_config->dpll_hw_state.fp1;
  7124. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7125. if (IS_PINEVIEW(dev)) {
  7126. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7127. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7128. } else {
  7129. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7130. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7131. }
  7132. if (!IS_GEN2(dev)) {
  7133. if (IS_PINEVIEW(dev))
  7134. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7135. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7136. else
  7137. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7138. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7139. switch (dpll & DPLL_MODE_MASK) {
  7140. case DPLLB_MODE_DAC_SERIAL:
  7141. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7142. 5 : 10;
  7143. break;
  7144. case DPLLB_MODE_LVDS:
  7145. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7146. 7 : 14;
  7147. break;
  7148. default:
  7149. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7150. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7151. return;
  7152. }
  7153. if (IS_PINEVIEW(dev))
  7154. pineview_clock(refclk, &clock);
  7155. else
  7156. i9xx_clock(refclk, &clock);
  7157. } else {
  7158. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7159. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7160. if (is_lvds) {
  7161. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7162. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7163. if (lvds & LVDS_CLKB_POWER_UP)
  7164. clock.p2 = 7;
  7165. else
  7166. clock.p2 = 14;
  7167. } else {
  7168. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7169. clock.p1 = 2;
  7170. else {
  7171. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7172. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7173. }
  7174. if (dpll & PLL_P2_DIVIDE_BY_4)
  7175. clock.p2 = 4;
  7176. else
  7177. clock.p2 = 2;
  7178. }
  7179. i9xx_clock(refclk, &clock);
  7180. }
  7181. /*
  7182. * This value includes pixel_multiplier. We will use
  7183. * port_clock to compute adjusted_mode.crtc_clock in the
  7184. * encoder's get_config() function.
  7185. */
  7186. pipe_config->port_clock = clock.dot;
  7187. }
  7188. int intel_dotclock_calculate(int link_freq,
  7189. const struct intel_link_m_n *m_n)
  7190. {
  7191. /*
  7192. * The calculation for the data clock is:
  7193. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7194. * But we want to avoid losing precison if possible, so:
  7195. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7196. *
  7197. * and the link clock is simpler:
  7198. * link_clock = (m * link_clock) / n
  7199. */
  7200. if (!m_n->link_n)
  7201. return 0;
  7202. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7203. }
  7204. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7205. struct intel_crtc_config *pipe_config)
  7206. {
  7207. struct drm_device *dev = crtc->base.dev;
  7208. /* read out port_clock from the DPLL */
  7209. i9xx_crtc_clock_get(crtc, pipe_config);
  7210. /*
  7211. * This value does not include pixel_multiplier.
  7212. * We will check that port_clock and adjusted_mode.crtc_clock
  7213. * agree once we know their relationship in the encoder's
  7214. * get_config() function.
  7215. */
  7216. pipe_config->adjusted_mode.crtc_clock =
  7217. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7218. &pipe_config->fdi_m_n);
  7219. }
  7220. /** Returns the currently programmed mode of the given pipe. */
  7221. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7222. struct drm_crtc *crtc)
  7223. {
  7224. struct drm_i915_private *dev_priv = dev->dev_private;
  7225. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7226. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7227. struct drm_display_mode *mode;
  7228. struct intel_crtc_config pipe_config;
  7229. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7230. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7231. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7232. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7233. enum pipe pipe = intel_crtc->pipe;
  7234. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7235. if (!mode)
  7236. return NULL;
  7237. /*
  7238. * Construct a pipe_config sufficient for getting the clock info
  7239. * back out of crtc_clock_get.
  7240. *
  7241. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7242. * to use a real value here instead.
  7243. */
  7244. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7245. pipe_config.pixel_multiplier = 1;
  7246. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7247. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7248. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7249. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7250. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7251. mode->hdisplay = (htot & 0xffff) + 1;
  7252. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7253. mode->hsync_start = (hsync & 0xffff) + 1;
  7254. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7255. mode->vdisplay = (vtot & 0xffff) + 1;
  7256. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7257. mode->vsync_start = (vsync & 0xffff) + 1;
  7258. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7259. drm_mode_set_name(mode);
  7260. return mode;
  7261. }
  7262. static void intel_increase_pllclock(struct drm_crtc *crtc)
  7263. {
  7264. struct drm_device *dev = crtc->dev;
  7265. struct drm_i915_private *dev_priv = dev->dev_private;
  7266. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7267. int pipe = intel_crtc->pipe;
  7268. int dpll_reg = DPLL(pipe);
  7269. int dpll;
  7270. if (HAS_PCH_SPLIT(dev))
  7271. return;
  7272. if (!dev_priv->lvds_downclock_avail)
  7273. return;
  7274. dpll = I915_READ(dpll_reg);
  7275. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  7276. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  7277. assert_panel_unlocked(dev_priv, pipe);
  7278. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  7279. I915_WRITE(dpll_reg, dpll);
  7280. intel_wait_for_vblank(dev, pipe);
  7281. dpll = I915_READ(dpll_reg);
  7282. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  7283. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  7284. }
  7285. }
  7286. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7287. {
  7288. struct drm_device *dev = crtc->dev;
  7289. struct drm_i915_private *dev_priv = dev->dev_private;
  7290. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7291. if (HAS_PCH_SPLIT(dev))
  7292. return;
  7293. if (!dev_priv->lvds_downclock_avail)
  7294. return;
  7295. /*
  7296. * Since this is called by a timer, we should never get here in
  7297. * the manual case.
  7298. */
  7299. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7300. int pipe = intel_crtc->pipe;
  7301. int dpll_reg = DPLL(pipe);
  7302. int dpll;
  7303. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7304. assert_panel_unlocked(dev_priv, pipe);
  7305. dpll = I915_READ(dpll_reg);
  7306. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7307. I915_WRITE(dpll_reg, dpll);
  7308. intel_wait_for_vblank(dev, pipe);
  7309. dpll = I915_READ(dpll_reg);
  7310. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7311. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7312. }
  7313. }
  7314. void intel_mark_busy(struct drm_device *dev)
  7315. {
  7316. struct drm_i915_private *dev_priv = dev->dev_private;
  7317. if (dev_priv->mm.busy)
  7318. return;
  7319. intel_runtime_pm_get(dev_priv);
  7320. i915_update_gfx_val(dev_priv);
  7321. dev_priv->mm.busy = true;
  7322. }
  7323. void intel_mark_idle(struct drm_device *dev)
  7324. {
  7325. struct drm_i915_private *dev_priv = dev->dev_private;
  7326. struct drm_crtc *crtc;
  7327. if (!dev_priv->mm.busy)
  7328. return;
  7329. dev_priv->mm.busy = false;
  7330. if (!i915.powersave)
  7331. goto out;
  7332. for_each_crtc(dev, crtc) {
  7333. if (!crtc->primary->fb)
  7334. continue;
  7335. intel_decrease_pllclock(crtc);
  7336. }
  7337. if (INTEL_INFO(dev)->gen >= 6)
  7338. gen6_rps_idle(dev->dev_private);
  7339. out:
  7340. intel_runtime_pm_put(dev_priv);
  7341. }
  7342. void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
  7343. struct intel_engine_cs *ring)
  7344. {
  7345. struct drm_device *dev = obj->base.dev;
  7346. struct drm_crtc *crtc;
  7347. if (!i915.powersave)
  7348. return;
  7349. for_each_crtc(dev, crtc) {
  7350. if (!crtc->primary->fb)
  7351. continue;
  7352. if (to_intel_framebuffer(crtc->primary->fb)->obj != obj)
  7353. continue;
  7354. intel_increase_pllclock(crtc);
  7355. if (ring && intel_fbc_enabled(dev))
  7356. ring->fbc_dirty = true;
  7357. }
  7358. }
  7359. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7360. {
  7361. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7362. struct drm_device *dev = crtc->dev;
  7363. struct intel_unpin_work *work;
  7364. unsigned long flags;
  7365. spin_lock_irqsave(&dev->event_lock, flags);
  7366. work = intel_crtc->unpin_work;
  7367. intel_crtc->unpin_work = NULL;
  7368. spin_unlock_irqrestore(&dev->event_lock, flags);
  7369. if (work) {
  7370. cancel_work_sync(&work->work);
  7371. kfree(work);
  7372. }
  7373. intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
  7374. drm_crtc_cleanup(crtc);
  7375. kfree(intel_crtc);
  7376. }
  7377. static void intel_unpin_work_fn(struct work_struct *__work)
  7378. {
  7379. struct intel_unpin_work *work =
  7380. container_of(__work, struct intel_unpin_work, work);
  7381. struct drm_device *dev = work->crtc->dev;
  7382. mutex_lock(&dev->struct_mutex);
  7383. intel_unpin_fb_obj(work->old_fb_obj);
  7384. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7385. drm_gem_object_unreference(&work->old_fb_obj->base);
  7386. intel_update_fbc(dev);
  7387. mutex_unlock(&dev->struct_mutex);
  7388. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7389. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7390. kfree(work);
  7391. }
  7392. static void do_intel_finish_page_flip(struct drm_device *dev,
  7393. struct drm_crtc *crtc)
  7394. {
  7395. struct drm_i915_private *dev_priv = dev->dev_private;
  7396. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7397. struct intel_unpin_work *work;
  7398. unsigned long flags;
  7399. /* Ignore early vblank irqs */
  7400. if (intel_crtc == NULL)
  7401. return;
  7402. spin_lock_irqsave(&dev->event_lock, flags);
  7403. work = intel_crtc->unpin_work;
  7404. /* Ensure we don't miss a work->pending update ... */
  7405. smp_rmb();
  7406. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7407. spin_unlock_irqrestore(&dev->event_lock, flags);
  7408. return;
  7409. }
  7410. /* and that the unpin work is consistent wrt ->pending. */
  7411. smp_rmb();
  7412. intel_crtc->unpin_work = NULL;
  7413. if (work->event)
  7414. drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
  7415. drm_crtc_vblank_put(crtc);
  7416. spin_unlock_irqrestore(&dev->event_lock, flags);
  7417. wake_up_all(&dev_priv->pending_flip_queue);
  7418. queue_work(dev_priv->wq, &work->work);
  7419. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  7420. }
  7421. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7422. {
  7423. struct drm_i915_private *dev_priv = dev->dev_private;
  7424. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7425. do_intel_finish_page_flip(dev, crtc);
  7426. }
  7427. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7428. {
  7429. struct drm_i915_private *dev_priv = dev->dev_private;
  7430. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7431. do_intel_finish_page_flip(dev, crtc);
  7432. }
  7433. /* Is 'a' after or equal to 'b'? */
  7434. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7435. {
  7436. return !((a - b) & 0x80000000);
  7437. }
  7438. static bool page_flip_finished(struct intel_crtc *crtc)
  7439. {
  7440. struct drm_device *dev = crtc->base.dev;
  7441. struct drm_i915_private *dev_priv = dev->dev_private;
  7442. /*
  7443. * The relevant registers doen't exist on pre-ctg.
  7444. * As the flip done interrupt doesn't trigger for mmio
  7445. * flips on gmch platforms, a flip count check isn't
  7446. * really needed there. But since ctg has the registers,
  7447. * include it in the check anyway.
  7448. */
  7449. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7450. return true;
  7451. /*
  7452. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7453. * used the same base address. In that case the mmio flip might
  7454. * have completed, but the CS hasn't even executed the flip yet.
  7455. *
  7456. * A flip count check isn't enough as the CS might have updated
  7457. * the base address just after start of vblank, but before we
  7458. * managed to process the interrupt. This means we'd complete the
  7459. * CS flip too soon.
  7460. *
  7461. * Combining both checks should get us a good enough result. It may
  7462. * still happen that the CS flip has been executed, but has not
  7463. * yet actually completed. But in case the base address is the same
  7464. * anyway, we don't really care.
  7465. */
  7466. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7467. crtc->unpin_work->gtt_offset &&
  7468. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7469. crtc->unpin_work->flip_count);
  7470. }
  7471. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7472. {
  7473. struct drm_i915_private *dev_priv = dev->dev_private;
  7474. struct intel_crtc *intel_crtc =
  7475. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7476. unsigned long flags;
  7477. /* NB: An MMIO update of the plane base pointer will also
  7478. * generate a page-flip completion irq, i.e. every modeset
  7479. * is also accompanied by a spurious intel_prepare_page_flip().
  7480. */
  7481. spin_lock_irqsave(&dev->event_lock, flags);
  7482. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7483. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7484. spin_unlock_irqrestore(&dev->event_lock, flags);
  7485. }
  7486. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7487. {
  7488. /* Ensure that the work item is consistent when activating it ... */
  7489. smp_wmb();
  7490. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7491. /* and that it is marked active as soon as the irq could fire. */
  7492. smp_wmb();
  7493. }
  7494. static int intel_gen2_queue_flip(struct drm_device *dev,
  7495. struct drm_crtc *crtc,
  7496. struct drm_framebuffer *fb,
  7497. struct drm_i915_gem_object *obj,
  7498. struct intel_engine_cs *ring,
  7499. uint32_t flags)
  7500. {
  7501. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7502. u32 flip_mask;
  7503. int ret;
  7504. ret = intel_ring_begin(ring, 6);
  7505. if (ret)
  7506. return ret;
  7507. /* Can't queue multiple flips, so wait for the previous
  7508. * one to finish before executing the next.
  7509. */
  7510. if (intel_crtc->plane)
  7511. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7512. else
  7513. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7514. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7515. intel_ring_emit(ring, MI_NOOP);
  7516. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7517. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7518. intel_ring_emit(ring, fb->pitches[0]);
  7519. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7520. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7521. intel_mark_page_flip_active(intel_crtc);
  7522. __intel_ring_advance(ring);
  7523. return 0;
  7524. }
  7525. static int intel_gen3_queue_flip(struct drm_device *dev,
  7526. struct drm_crtc *crtc,
  7527. struct drm_framebuffer *fb,
  7528. struct drm_i915_gem_object *obj,
  7529. struct intel_engine_cs *ring,
  7530. uint32_t flags)
  7531. {
  7532. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7533. u32 flip_mask;
  7534. int ret;
  7535. ret = intel_ring_begin(ring, 6);
  7536. if (ret)
  7537. return ret;
  7538. if (intel_crtc->plane)
  7539. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7540. else
  7541. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7542. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7543. intel_ring_emit(ring, MI_NOOP);
  7544. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7545. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7546. intel_ring_emit(ring, fb->pitches[0]);
  7547. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7548. intel_ring_emit(ring, MI_NOOP);
  7549. intel_mark_page_flip_active(intel_crtc);
  7550. __intel_ring_advance(ring);
  7551. return 0;
  7552. }
  7553. static int intel_gen4_queue_flip(struct drm_device *dev,
  7554. struct drm_crtc *crtc,
  7555. struct drm_framebuffer *fb,
  7556. struct drm_i915_gem_object *obj,
  7557. struct intel_engine_cs *ring,
  7558. uint32_t flags)
  7559. {
  7560. struct drm_i915_private *dev_priv = dev->dev_private;
  7561. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7562. uint32_t pf, pipesrc;
  7563. int ret;
  7564. ret = intel_ring_begin(ring, 4);
  7565. if (ret)
  7566. return ret;
  7567. /* i965+ uses the linear or tiled offsets from the
  7568. * Display Registers (which do not change across a page-flip)
  7569. * so we need only reprogram the base address.
  7570. */
  7571. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7572. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7573. intel_ring_emit(ring, fb->pitches[0]);
  7574. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7575. obj->tiling_mode);
  7576. /* XXX Enabling the panel-fitter across page-flip is so far
  7577. * untested on non-native modes, so ignore it for now.
  7578. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7579. */
  7580. pf = 0;
  7581. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7582. intel_ring_emit(ring, pf | pipesrc);
  7583. intel_mark_page_flip_active(intel_crtc);
  7584. __intel_ring_advance(ring);
  7585. return 0;
  7586. }
  7587. static int intel_gen6_queue_flip(struct drm_device *dev,
  7588. struct drm_crtc *crtc,
  7589. struct drm_framebuffer *fb,
  7590. struct drm_i915_gem_object *obj,
  7591. struct intel_engine_cs *ring,
  7592. uint32_t flags)
  7593. {
  7594. struct drm_i915_private *dev_priv = dev->dev_private;
  7595. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7596. uint32_t pf, pipesrc;
  7597. int ret;
  7598. ret = intel_ring_begin(ring, 4);
  7599. if (ret)
  7600. return ret;
  7601. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7602. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7603. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7604. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7605. /* Contrary to the suggestions in the documentation,
  7606. * "Enable Panel Fitter" does not seem to be required when page
  7607. * flipping with a non-native mode, and worse causes a normal
  7608. * modeset to fail.
  7609. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7610. */
  7611. pf = 0;
  7612. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7613. intel_ring_emit(ring, pf | pipesrc);
  7614. intel_mark_page_flip_active(intel_crtc);
  7615. __intel_ring_advance(ring);
  7616. return 0;
  7617. }
  7618. static int intel_gen7_queue_flip(struct drm_device *dev,
  7619. struct drm_crtc *crtc,
  7620. struct drm_framebuffer *fb,
  7621. struct drm_i915_gem_object *obj,
  7622. struct intel_engine_cs *ring,
  7623. uint32_t flags)
  7624. {
  7625. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7626. uint32_t plane_bit = 0;
  7627. int len, ret;
  7628. switch (intel_crtc->plane) {
  7629. case PLANE_A:
  7630. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7631. break;
  7632. case PLANE_B:
  7633. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7634. break;
  7635. case PLANE_C:
  7636. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7637. break;
  7638. default:
  7639. WARN_ONCE(1, "unknown plane in flip command\n");
  7640. return -ENODEV;
  7641. }
  7642. len = 4;
  7643. if (ring->id == RCS) {
  7644. len += 6;
  7645. /*
  7646. * On Gen 8, SRM is now taking an extra dword to accommodate
  7647. * 48bits addresses, and we need a NOOP for the batch size to
  7648. * stay even.
  7649. */
  7650. if (IS_GEN8(dev))
  7651. len += 2;
  7652. }
  7653. /*
  7654. * BSpec MI_DISPLAY_FLIP for IVB:
  7655. * "The full packet must be contained within the same cache line."
  7656. *
  7657. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7658. * cacheline, if we ever start emitting more commands before
  7659. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7660. * then do the cacheline alignment, and finally emit the
  7661. * MI_DISPLAY_FLIP.
  7662. */
  7663. ret = intel_ring_cacheline_align(ring);
  7664. if (ret)
  7665. return ret;
  7666. ret = intel_ring_begin(ring, len);
  7667. if (ret)
  7668. return ret;
  7669. /* Unmask the flip-done completion message. Note that the bspec says that
  7670. * we should do this for both the BCS and RCS, and that we must not unmask
  7671. * more than one flip event at any time (or ensure that one flip message
  7672. * can be sent by waiting for flip-done prior to queueing new flips).
  7673. * Experimentation says that BCS works despite DERRMR masking all
  7674. * flip-done completion events and that unmasking all planes at once
  7675. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7676. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7677. */
  7678. if (ring->id == RCS) {
  7679. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7680. intel_ring_emit(ring, DERRMR);
  7681. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7682. DERRMR_PIPEB_PRI_FLIP_DONE |
  7683. DERRMR_PIPEC_PRI_FLIP_DONE));
  7684. if (IS_GEN8(dev))
  7685. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7686. MI_SRM_LRM_GLOBAL_GTT);
  7687. else
  7688. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7689. MI_SRM_LRM_GLOBAL_GTT);
  7690. intel_ring_emit(ring, DERRMR);
  7691. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7692. if (IS_GEN8(dev)) {
  7693. intel_ring_emit(ring, 0);
  7694. intel_ring_emit(ring, MI_NOOP);
  7695. }
  7696. }
  7697. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7698. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7699. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7700. intel_ring_emit(ring, (MI_NOOP));
  7701. intel_mark_page_flip_active(intel_crtc);
  7702. __intel_ring_advance(ring);
  7703. return 0;
  7704. }
  7705. static int intel_default_queue_flip(struct drm_device *dev,
  7706. struct drm_crtc *crtc,
  7707. struct drm_framebuffer *fb,
  7708. struct drm_i915_gem_object *obj,
  7709. struct intel_engine_cs *ring,
  7710. uint32_t flags)
  7711. {
  7712. return -ENODEV;
  7713. }
  7714. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  7715. struct drm_framebuffer *fb,
  7716. struct drm_pending_vblank_event *event,
  7717. uint32_t page_flip_flags)
  7718. {
  7719. struct drm_device *dev = crtc->dev;
  7720. struct drm_i915_private *dev_priv = dev->dev_private;
  7721. struct drm_framebuffer *old_fb = crtc->primary->fb;
  7722. struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
  7723. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7724. struct intel_unpin_work *work;
  7725. struct intel_engine_cs *ring;
  7726. unsigned long flags;
  7727. int ret;
  7728. /* Can't change pixel format via MI display flips. */
  7729. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  7730. return -EINVAL;
  7731. /*
  7732. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  7733. * Note that pitch changes could also affect these register.
  7734. */
  7735. if (INTEL_INFO(dev)->gen > 3 &&
  7736. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  7737. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  7738. return -EINVAL;
  7739. if (i915_terminally_wedged(&dev_priv->gpu_error))
  7740. goto out_hang;
  7741. work = kzalloc(sizeof(*work), GFP_KERNEL);
  7742. if (work == NULL)
  7743. return -ENOMEM;
  7744. work->event = event;
  7745. work->crtc = crtc;
  7746. work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
  7747. INIT_WORK(&work->work, intel_unpin_work_fn);
  7748. ret = drm_crtc_vblank_get(crtc);
  7749. if (ret)
  7750. goto free_work;
  7751. /* We borrow the event spin lock for protecting unpin_work */
  7752. spin_lock_irqsave(&dev->event_lock, flags);
  7753. if (intel_crtc->unpin_work) {
  7754. spin_unlock_irqrestore(&dev->event_lock, flags);
  7755. kfree(work);
  7756. drm_crtc_vblank_put(crtc);
  7757. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  7758. return -EBUSY;
  7759. }
  7760. intel_crtc->unpin_work = work;
  7761. spin_unlock_irqrestore(&dev->event_lock, flags);
  7762. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  7763. flush_workqueue(dev_priv->wq);
  7764. ret = i915_mutex_lock_interruptible(dev);
  7765. if (ret)
  7766. goto cleanup;
  7767. /* Reference the objects for the scheduled work. */
  7768. drm_gem_object_reference(&work->old_fb_obj->base);
  7769. drm_gem_object_reference(&obj->base);
  7770. crtc->primary->fb = fb;
  7771. work->pending_flip_obj = obj;
  7772. work->enable_stall_check = true;
  7773. atomic_inc(&intel_crtc->unpin_work_count);
  7774. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  7775. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  7776. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(intel_crtc->pipe)) + 1;
  7777. if (IS_VALLEYVIEW(dev)) {
  7778. ring = &dev_priv->ring[BCS];
  7779. } else if (INTEL_INFO(dev)->gen >= 7) {
  7780. ring = obj->ring;
  7781. if (ring == NULL || ring->id != RCS)
  7782. ring = &dev_priv->ring[BCS];
  7783. } else {
  7784. ring = &dev_priv->ring[RCS];
  7785. }
  7786. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  7787. if (ret)
  7788. goto cleanup_pending;
  7789. work->gtt_offset =
  7790. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  7791. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring, page_flip_flags);
  7792. if (ret)
  7793. goto cleanup_unpin;
  7794. intel_disable_fbc(dev);
  7795. intel_mark_fb_busy(obj, NULL);
  7796. mutex_unlock(&dev->struct_mutex);
  7797. trace_i915_flip_request(intel_crtc->plane, obj);
  7798. return 0;
  7799. cleanup_unpin:
  7800. intel_unpin_fb_obj(obj);
  7801. cleanup_pending:
  7802. atomic_dec(&intel_crtc->unpin_work_count);
  7803. crtc->primary->fb = old_fb;
  7804. drm_gem_object_unreference(&work->old_fb_obj->base);
  7805. drm_gem_object_unreference(&obj->base);
  7806. mutex_unlock(&dev->struct_mutex);
  7807. cleanup:
  7808. spin_lock_irqsave(&dev->event_lock, flags);
  7809. intel_crtc->unpin_work = NULL;
  7810. spin_unlock_irqrestore(&dev->event_lock, flags);
  7811. drm_crtc_vblank_put(crtc);
  7812. free_work:
  7813. kfree(work);
  7814. if (ret == -EIO) {
  7815. out_hang:
  7816. intel_crtc_wait_for_pending_flips(crtc);
  7817. ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
  7818. if (ret == 0 && event)
  7819. drm_send_vblank_event(dev, intel_crtc->pipe, event);
  7820. }
  7821. return ret;
  7822. }
  7823. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  7824. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  7825. .load_lut = intel_crtc_load_lut,
  7826. };
  7827. /**
  7828. * intel_modeset_update_staged_output_state
  7829. *
  7830. * Updates the staged output configuration state, e.g. after we've read out the
  7831. * current hw state.
  7832. */
  7833. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  7834. {
  7835. struct intel_crtc *crtc;
  7836. struct intel_encoder *encoder;
  7837. struct intel_connector *connector;
  7838. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7839. base.head) {
  7840. connector->new_encoder =
  7841. to_intel_encoder(connector->base.encoder);
  7842. }
  7843. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7844. base.head) {
  7845. encoder->new_crtc =
  7846. to_intel_crtc(encoder->base.crtc);
  7847. }
  7848. for_each_intel_crtc(dev, crtc) {
  7849. crtc->new_enabled = crtc->base.enabled;
  7850. if (crtc->new_enabled)
  7851. crtc->new_config = &crtc->config;
  7852. else
  7853. crtc->new_config = NULL;
  7854. }
  7855. }
  7856. /**
  7857. * intel_modeset_commit_output_state
  7858. *
  7859. * This function copies the stage display pipe configuration to the real one.
  7860. */
  7861. static void intel_modeset_commit_output_state(struct drm_device *dev)
  7862. {
  7863. struct intel_crtc *crtc;
  7864. struct intel_encoder *encoder;
  7865. struct intel_connector *connector;
  7866. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7867. base.head) {
  7868. connector->base.encoder = &connector->new_encoder->base;
  7869. }
  7870. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7871. base.head) {
  7872. encoder->base.crtc = &encoder->new_crtc->base;
  7873. }
  7874. for_each_intel_crtc(dev, crtc) {
  7875. crtc->base.enabled = crtc->new_enabled;
  7876. }
  7877. }
  7878. static void
  7879. connected_sink_compute_bpp(struct intel_connector *connector,
  7880. struct intel_crtc_config *pipe_config)
  7881. {
  7882. int bpp = pipe_config->pipe_bpp;
  7883. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  7884. connector->base.base.id,
  7885. connector->base.name);
  7886. /* Don't use an invalid EDID bpc value */
  7887. if (connector->base.display_info.bpc &&
  7888. connector->base.display_info.bpc * 3 < bpp) {
  7889. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  7890. bpp, connector->base.display_info.bpc*3);
  7891. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  7892. }
  7893. /* Clamp bpp to 8 on screens without EDID 1.4 */
  7894. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  7895. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  7896. bpp);
  7897. pipe_config->pipe_bpp = 24;
  7898. }
  7899. }
  7900. static int
  7901. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  7902. struct drm_framebuffer *fb,
  7903. struct intel_crtc_config *pipe_config)
  7904. {
  7905. struct drm_device *dev = crtc->base.dev;
  7906. struct intel_connector *connector;
  7907. int bpp;
  7908. switch (fb->pixel_format) {
  7909. case DRM_FORMAT_C8:
  7910. bpp = 8*3; /* since we go through a colormap */
  7911. break;
  7912. case DRM_FORMAT_XRGB1555:
  7913. case DRM_FORMAT_ARGB1555:
  7914. /* checked in intel_framebuffer_init already */
  7915. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  7916. return -EINVAL;
  7917. case DRM_FORMAT_RGB565:
  7918. bpp = 6*3; /* min is 18bpp */
  7919. break;
  7920. case DRM_FORMAT_XBGR8888:
  7921. case DRM_FORMAT_ABGR8888:
  7922. /* checked in intel_framebuffer_init already */
  7923. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  7924. return -EINVAL;
  7925. case DRM_FORMAT_XRGB8888:
  7926. case DRM_FORMAT_ARGB8888:
  7927. bpp = 8*3;
  7928. break;
  7929. case DRM_FORMAT_XRGB2101010:
  7930. case DRM_FORMAT_ARGB2101010:
  7931. case DRM_FORMAT_XBGR2101010:
  7932. case DRM_FORMAT_ABGR2101010:
  7933. /* checked in intel_framebuffer_init already */
  7934. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  7935. return -EINVAL;
  7936. bpp = 10*3;
  7937. break;
  7938. /* TODO: gen4+ supports 16 bpc floating point, too. */
  7939. default:
  7940. DRM_DEBUG_KMS("unsupported depth\n");
  7941. return -EINVAL;
  7942. }
  7943. pipe_config->pipe_bpp = bpp;
  7944. /* Clamp display bpp to EDID value */
  7945. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7946. base.head) {
  7947. if (!connector->new_encoder ||
  7948. connector->new_encoder->new_crtc != crtc)
  7949. continue;
  7950. connected_sink_compute_bpp(connector, pipe_config);
  7951. }
  7952. return bpp;
  7953. }
  7954. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  7955. {
  7956. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  7957. "type: 0x%x flags: 0x%x\n",
  7958. mode->crtc_clock,
  7959. mode->crtc_hdisplay, mode->crtc_hsync_start,
  7960. mode->crtc_hsync_end, mode->crtc_htotal,
  7961. mode->crtc_vdisplay, mode->crtc_vsync_start,
  7962. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  7963. }
  7964. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  7965. struct intel_crtc_config *pipe_config,
  7966. const char *context)
  7967. {
  7968. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  7969. context, pipe_name(crtc->pipe));
  7970. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  7971. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  7972. pipe_config->pipe_bpp, pipe_config->dither);
  7973. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  7974. pipe_config->has_pch_encoder,
  7975. pipe_config->fdi_lanes,
  7976. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  7977. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  7978. pipe_config->fdi_m_n.tu);
  7979. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  7980. pipe_config->has_dp_encoder,
  7981. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  7982. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  7983. pipe_config->dp_m_n.tu);
  7984. DRM_DEBUG_KMS("requested mode:\n");
  7985. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  7986. DRM_DEBUG_KMS("adjusted mode:\n");
  7987. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  7988. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  7989. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  7990. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  7991. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  7992. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  7993. pipe_config->gmch_pfit.control,
  7994. pipe_config->gmch_pfit.pgm_ratios,
  7995. pipe_config->gmch_pfit.lvds_border_bits);
  7996. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  7997. pipe_config->pch_pfit.pos,
  7998. pipe_config->pch_pfit.size,
  7999. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  8000. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  8001. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  8002. }
  8003. static bool encoders_cloneable(const struct intel_encoder *a,
  8004. const struct intel_encoder *b)
  8005. {
  8006. /* masks could be asymmetric, so check both ways */
  8007. return a == b || (a->cloneable & (1 << b->type) &&
  8008. b->cloneable & (1 << a->type));
  8009. }
  8010. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8011. struct intel_encoder *encoder)
  8012. {
  8013. struct drm_device *dev = crtc->base.dev;
  8014. struct intel_encoder *source_encoder;
  8015. list_for_each_entry(source_encoder,
  8016. &dev->mode_config.encoder_list, base.head) {
  8017. if (source_encoder->new_crtc != crtc)
  8018. continue;
  8019. if (!encoders_cloneable(encoder, source_encoder))
  8020. return false;
  8021. }
  8022. return true;
  8023. }
  8024. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8025. {
  8026. struct drm_device *dev = crtc->base.dev;
  8027. struct intel_encoder *encoder;
  8028. list_for_each_entry(encoder,
  8029. &dev->mode_config.encoder_list, base.head) {
  8030. if (encoder->new_crtc != crtc)
  8031. continue;
  8032. if (!check_single_encoder_cloning(crtc, encoder))
  8033. return false;
  8034. }
  8035. return true;
  8036. }
  8037. static struct intel_crtc_config *
  8038. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8039. struct drm_framebuffer *fb,
  8040. struct drm_display_mode *mode)
  8041. {
  8042. struct drm_device *dev = crtc->dev;
  8043. struct intel_encoder *encoder;
  8044. struct intel_crtc_config *pipe_config;
  8045. int plane_bpp, ret = -EINVAL;
  8046. bool retry = true;
  8047. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8048. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8049. return ERR_PTR(-EINVAL);
  8050. }
  8051. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8052. if (!pipe_config)
  8053. return ERR_PTR(-ENOMEM);
  8054. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  8055. drm_mode_copy(&pipe_config->requested_mode, mode);
  8056. pipe_config->cpu_transcoder =
  8057. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8058. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8059. /*
  8060. * Sanitize sync polarity flags based on requested ones. If neither
  8061. * positive or negative polarity is requested, treat this as meaning
  8062. * negative polarity.
  8063. */
  8064. if (!(pipe_config->adjusted_mode.flags &
  8065. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8066. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8067. if (!(pipe_config->adjusted_mode.flags &
  8068. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8069. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8070. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8071. * plane pixel format and any sink constraints into account. Returns the
  8072. * source plane bpp so that dithering can be selected on mismatches
  8073. * after encoders and crtc also have had their say. */
  8074. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8075. fb, pipe_config);
  8076. if (plane_bpp < 0)
  8077. goto fail;
  8078. /*
  8079. * Determine the real pipe dimensions. Note that stereo modes can
  8080. * increase the actual pipe size due to the frame doubling and
  8081. * insertion of additional space for blanks between the frame. This
  8082. * is stored in the crtc timings. We use the requested mode to do this
  8083. * computation to clearly distinguish it from the adjusted mode, which
  8084. * can be changed by the connectors in the below retry loop.
  8085. */
  8086. drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
  8087. pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
  8088. pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
  8089. encoder_retry:
  8090. /* Ensure the port clock defaults are reset when retrying. */
  8091. pipe_config->port_clock = 0;
  8092. pipe_config->pixel_multiplier = 1;
  8093. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8094. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  8095. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8096. * adjust it according to limitations or connector properties, and also
  8097. * a chance to reject the mode entirely.
  8098. */
  8099. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8100. base.head) {
  8101. if (&encoder->new_crtc->base != crtc)
  8102. continue;
  8103. if (!(encoder->compute_config(encoder, pipe_config))) {
  8104. DRM_DEBUG_KMS("Encoder config failure\n");
  8105. goto fail;
  8106. }
  8107. }
  8108. /* Set default port clock if not overwritten by the encoder. Needs to be
  8109. * done afterwards in case the encoder adjusts the mode. */
  8110. if (!pipe_config->port_clock)
  8111. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  8112. * pipe_config->pixel_multiplier;
  8113. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8114. if (ret < 0) {
  8115. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8116. goto fail;
  8117. }
  8118. if (ret == RETRY) {
  8119. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8120. ret = -EINVAL;
  8121. goto fail;
  8122. }
  8123. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8124. retry = false;
  8125. goto encoder_retry;
  8126. }
  8127. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8128. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8129. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8130. return pipe_config;
  8131. fail:
  8132. kfree(pipe_config);
  8133. return ERR_PTR(ret);
  8134. }
  8135. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8136. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8137. static void
  8138. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8139. unsigned *prepare_pipes, unsigned *disable_pipes)
  8140. {
  8141. struct intel_crtc *intel_crtc;
  8142. struct drm_device *dev = crtc->dev;
  8143. struct intel_encoder *encoder;
  8144. struct intel_connector *connector;
  8145. struct drm_crtc *tmp_crtc;
  8146. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8147. /* Check which crtcs have changed outputs connected to them, these need
  8148. * to be part of the prepare_pipes mask. We don't (yet) support global
  8149. * modeset across multiple crtcs, so modeset_pipes will only have one
  8150. * bit set at most. */
  8151. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8152. base.head) {
  8153. if (connector->base.encoder == &connector->new_encoder->base)
  8154. continue;
  8155. if (connector->base.encoder) {
  8156. tmp_crtc = connector->base.encoder->crtc;
  8157. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8158. }
  8159. if (connector->new_encoder)
  8160. *prepare_pipes |=
  8161. 1 << connector->new_encoder->new_crtc->pipe;
  8162. }
  8163. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8164. base.head) {
  8165. if (encoder->base.crtc == &encoder->new_crtc->base)
  8166. continue;
  8167. if (encoder->base.crtc) {
  8168. tmp_crtc = encoder->base.crtc;
  8169. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8170. }
  8171. if (encoder->new_crtc)
  8172. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8173. }
  8174. /* Check for pipes that will be enabled/disabled ... */
  8175. for_each_intel_crtc(dev, intel_crtc) {
  8176. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8177. continue;
  8178. if (!intel_crtc->new_enabled)
  8179. *disable_pipes |= 1 << intel_crtc->pipe;
  8180. else
  8181. *prepare_pipes |= 1 << intel_crtc->pipe;
  8182. }
  8183. /* set_mode is also used to update properties on life display pipes. */
  8184. intel_crtc = to_intel_crtc(crtc);
  8185. if (intel_crtc->new_enabled)
  8186. *prepare_pipes |= 1 << intel_crtc->pipe;
  8187. /*
  8188. * For simplicity do a full modeset on any pipe where the output routing
  8189. * changed. We could be more clever, but that would require us to be
  8190. * more careful with calling the relevant encoder->mode_set functions.
  8191. */
  8192. if (*prepare_pipes)
  8193. *modeset_pipes = *prepare_pipes;
  8194. /* ... and mask these out. */
  8195. *modeset_pipes &= ~(*disable_pipes);
  8196. *prepare_pipes &= ~(*disable_pipes);
  8197. /*
  8198. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8199. * obies this rule, but the modeset restore mode of
  8200. * intel_modeset_setup_hw_state does not.
  8201. */
  8202. *modeset_pipes &= 1 << intel_crtc->pipe;
  8203. *prepare_pipes &= 1 << intel_crtc->pipe;
  8204. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8205. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8206. }
  8207. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8208. {
  8209. struct drm_encoder *encoder;
  8210. struct drm_device *dev = crtc->dev;
  8211. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8212. if (encoder->crtc == crtc)
  8213. return true;
  8214. return false;
  8215. }
  8216. static void
  8217. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8218. {
  8219. struct intel_encoder *intel_encoder;
  8220. struct intel_crtc *intel_crtc;
  8221. struct drm_connector *connector;
  8222. list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
  8223. base.head) {
  8224. if (!intel_encoder->base.crtc)
  8225. continue;
  8226. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8227. if (prepare_pipes & (1 << intel_crtc->pipe))
  8228. intel_encoder->connectors_active = false;
  8229. }
  8230. intel_modeset_commit_output_state(dev);
  8231. /* Double check state. */
  8232. for_each_intel_crtc(dev, intel_crtc) {
  8233. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8234. WARN_ON(intel_crtc->new_config &&
  8235. intel_crtc->new_config != &intel_crtc->config);
  8236. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8237. }
  8238. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8239. if (!connector->encoder || !connector->encoder->crtc)
  8240. continue;
  8241. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8242. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8243. struct drm_property *dpms_property =
  8244. dev->mode_config.dpms_property;
  8245. connector->dpms = DRM_MODE_DPMS_ON;
  8246. drm_object_property_set_value(&connector->base,
  8247. dpms_property,
  8248. DRM_MODE_DPMS_ON);
  8249. intel_encoder = to_intel_encoder(connector->encoder);
  8250. intel_encoder->connectors_active = true;
  8251. }
  8252. }
  8253. }
  8254. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8255. {
  8256. int diff;
  8257. if (clock1 == clock2)
  8258. return true;
  8259. if (!clock1 || !clock2)
  8260. return false;
  8261. diff = abs(clock1 - clock2);
  8262. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8263. return true;
  8264. return false;
  8265. }
  8266. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8267. list_for_each_entry((intel_crtc), \
  8268. &(dev)->mode_config.crtc_list, \
  8269. base.head) \
  8270. if (mask & (1 <<(intel_crtc)->pipe))
  8271. static bool
  8272. intel_pipe_config_compare(struct drm_device *dev,
  8273. struct intel_crtc_config *current_config,
  8274. struct intel_crtc_config *pipe_config)
  8275. {
  8276. #define PIPE_CONF_CHECK_X(name) \
  8277. if (current_config->name != pipe_config->name) { \
  8278. DRM_ERROR("mismatch in " #name " " \
  8279. "(expected 0x%08x, found 0x%08x)\n", \
  8280. current_config->name, \
  8281. pipe_config->name); \
  8282. return false; \
  8283. }
  8284. #define PIPE_CONF_CHECK_I(name) \
  8285. if (current_config->name != pipe_config->name) { \
  8286. DRM_ERROR("mismatch in " #name " " \
  8287. "(expected %i, found %i)\n", \
  8288. current_config->name, \
  8289. pipe_config->name); \
  8290. return false; \
  8291. }
  8292. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8293. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8294. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8295. "(expected %i, found %i)\n", \
  8296. current_config->name & (mask), \
  8297. pipe_config->name & (mask)); \
  8298. return false; \
  8299. }
  8300. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8301. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8302. DRM_ERROR("mismatch in " #name " " \
  8303. "(expected %i, found %i)\n", \
  8304. current_config->name, \
  8305. pipe_config->name); \
  8306. return false; \
  8307. }
  8308. #define PIPE_CONF_QUIRK(quirk) \
  8309. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8310. PIPE_CONF_CHECK_I(cpu_transcoder);
  8311. PIPE_CONF_CHECK_I(has_pch_encoder);
  8312. PIPE_CONF_CHECK_I(fdi_lanes);
  8313. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8314. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8315. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8316. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8317. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8318. PIPE_CONF_CHECK_I(has_dp_encoder);
  8319. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8320. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8321. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8322. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8323. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8324. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  8325. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  8326. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  8327. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  8328. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  8329. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  8330. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  8331. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  8332. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  8333. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  8334. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  8335. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  8336. PIPE_CONF_CHECK_I(pixel_multiplier);
  8337. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8338. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8339. IS_VALLEYVIEW(dev))
  8340. PIPE_CONF_CHECK_I(limited_color_range);
  8341. PIPE_CONF_CHECK_I(has_audio);
  8342. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8343. DRM_MODE_FLAG_INTERLACE);
  8344. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8345. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8346. DRM_MODE_FLAG_PHSYNC);
  8347. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8348. DRM_MODE_FLAG_NHSYNC);
  8349. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8350. DRM_MODE_FLAG_PVSYNC);
  8351. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8352. DRM_MODE_FLAG_NVSYNC);
  8353. }
  8354. PIPE_CONF_CHECK_I(pipe_src_w);
  8355. PIPE_CONF_CHECK_I(pipe_src_h);
  8356. /*
  8357. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8358. * screen. Since we don't yet re-compute the pipe config when moving
  8359. * just the lvds port away to another pipe the sw tracking won't match.
  8360. *
  8361. * Proper atomic modesets with recomputed global state will fix this.
  8362. * Until then just don't check gmch state for inherited modes.
  8363. */
  8364. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8365. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8366. /* pfit ratios are autocomputed by the hw on gen4+ */
  8367. if (INTEL_INFO(dev)->gen < 4)
  8368. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8369. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8370. }
  8371. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8372. if (current_config->pch_pfit.enabled) {
  8373. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8374. PIPE_CONF_CHECK_I(pch_pfit.size);
  8375. }
  8376. /* BDW+ don't expose a synchronous way to read the state */
  8377. if (IS_HASWELL(dev))
  8378. PIPE_CONF_CHECK_I(ips_enabled);
  8379. PIPE_CONF_CHECK_I(double_wide);
  8380. PIPE_CONF_CHECK_I(shared_dpll);
  8381. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8382. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8383. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8384. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8385. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8386. PIPE_CONF_CHECK_I(pipe_bpp);
  8387. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  8388. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8389. #undef PIPE_CONF_CHECK_X
  8390. #undef PIPE_CONF_CHECK_I
  8391. #undef PIPE_CONF_CHECK_FLAGS
  8392. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8393. #undef PIPE_CONF_QUIRK
  8394. return true;
  8395. }
  8396. static void
  8397. check_connector_state(struct drm_device *dev)
  8398. {
  8399. struct intel_connector *connector;
  8400. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8401. base.head) {
  8402. /* This also checks the encoder/connector hw state with the
  8403. * ->get_hw_state callbacks. */
  8404. intel_connector_check_state(connector);
  8405. WARN(&connector->new_encoder->base != connector->base.encoder,
  8406. "connector's staged encoder doesn't match current encoder\n");
  8407. }
  8408. }
  8409. static void
  8410. check_encoder_state(struct drm_device *dev)
  8411. {
  8412. struct intel_encoder *encoder;
  8413. struct intel_connector *connector;
  8414. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8415. base.head) {
  8416. bool enabled = false;
  8417. bool active = false;
  8418. enum pipe pipe, tracked_pipe;
  8419. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8420. encoder->base.base.id,
  8421. encoder->base.name);
  8422. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  8423. "encoder's stage crtc doesn't match current crtc\n");
  8424. WARN(encoder->connectors_active && !encoder->base.crtc,
  8425. "encoder's active_connectors set, but no crtc\n");
  8426. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8427. base.head) {
  8428. if (connector->base.encoder != &encoder->base)
  8429. continue;
  8430. enabled = true;
  8431. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  8432. active = true;
  8433. }
  8434. WARN(!!encoder->base.crtc != enabled,
  8435. "encoder's enabled state mismatch "
  8436. "(expected %i, found %i)\n",
  8437. !!encoder->base.crtc, enabled);
  8438. WARN(active && !encoder->base.crtc,
  8439. "active encoder with no crtc\n");
  8440. WARN(encoder->connectors_active != active,
  8441. "encoder's computed active state doesn't match tracked active state "
  8442. "(expected %i, found %i)\n", active, encoder->connectors_active);
  8443. active = encoder->get_hw_state(encoder, &pipe);
  8444. WARN(active != encoder->connectors_active,
  8445. "encoder's hw state doesn't match sw tracking "
  8446. "(expected %i, found %i)\n",
  8447. encoder->connectors_active, active);
  8448. if (!encoder->base.crtc)
  8449. continue;
  8450. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  8451. WARN(active && pipe != tracked_pipe,
  8452. "active encoder's pipe doesn't match"
  8453. "(expected %i, found %i)\n",
  8454. tracked_pipe, pipe);
  8455. }
  8456. }
  8457. static void
  8458. check_crtc_state(struct drm_device *dev)
  8459. {
  8460. struct drm_i915_private *dev_priv = dev->dev_private;
  8461. struct intel_crtc *crtc;
  8462. struct intel_encoder *encoder;
  8463. struct intel_crtc_config pipe_config;
  8464. for_each_intel_crtc(dev, crtc) {
  8465. bool enabled = false;
  8466. bool active = false;
  8467. memset(&pipe_config, 0, sizeof(pipe_config));
  8468. DRM_DEBUG_KMS("[CRTC:%d]\n",
  8469. crtc->base.base.id);
  8470. WARN(crtc->active && !crtc->base.enabled,
  8471. "active crtc, but not enabled in sw tracking\n");
  8472. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8473. base.head) {
  8474. if (encoder->base.crtc != &crtc->base)
  8475. continue;
  8476. enabled = true;
  8477. if (encoder->connectors_active)
  8478. active = true;
  8479. }
  8480. WARN(active != crtc->active,
  8481. "crtc's computed active state doesn't match tracked active state "
  8482. "(expected %i, found %i)\n", active, crtc->active);
  8483. WARN(enabled != crtc->base.enabled,
  8484. "crtc's computed enabled state doesn't match tracked enabled state "
  8485. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  8486. active = dev_priv->display.get_pipe_config(crtc,
  8487. &pipe_config);
  8488. /* hw state is inconsistent with the pipe A quirk */
  8489. if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  8490. active = crtc->active;
  8491. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8492. base.head) {
  8493. enum pipe pipe;
  8494. if (encoder->base.crtc != &crtc->base)
  8495. continue;
  8496. if (encoder->get_hw_state(encoder, &pipe))
  8497. encoder->get_config(encoder, &pipe_config);
  8498. }
  8499. WARN(crtc->active != active,
  8500. "crtc active state doesn't match with hw state "
  8501. "(expected %i, found %i)\n", crtc->active, active);
  8502. if (active &&
  8503. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  8504. WARN(1, "pipe state doesn't match!\n");
  8505. intel_dump_pipe_config(crtc, &pipe_config,
  8506. "[hw state]");
  8507. intel_dump_pipe_config(crtc, &crtc->config,
  8508. "[sw state]");
  8509. }
  8510. }
  8511. }
  8512. static void
  8513. check_shared_dpll_state(struct drm_device *dev)
  8514. {
  8515. struct drm_i915_private *dev_priv = dev->dev_private;
  8516. struct intel_crtc *crtc;
  8517. struct intel_dpll_hw_state dpll_hw_state;
  8518. int i;
  8519. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  8520. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  8521. int enabled_crtcs = 0, active_crtcs = 0;
  8522. bool active;
  8523. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  8524. DRM_DEBUG_KMS("%s\n", pll->name);
  8525. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  8526. WARN(pll->active > pll->refcount,
  8527. "more active pll users than references: %i vs %i\n",
  8528. pll->active, pll->refcount);
  8529. WARN(pll->active && !pll->on,
  8530. "pll in active use but not on in sw tracking\n");
  8531. WARN(pll->on && !pll->active,
  8532. "pll in on but not on in use in sw tracking\n");
  8533. WARN(pll->on != active,
  8534. "pll on state mismatch (expected %i, found %i)\n",
  8535. pll->on, active);
  8536. for_each_intel_crtc(dev, crtc) {
  8537. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  8538. enabled_crtcs++;
  8539. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  8540. active_crtcs++;
  8541. }
  8542. WARN(pll->active != active_crtcs,
  8543. "pll active crtcs mismatch (expected %i, found %i)\n",
  8544. pll->active, active_crtcs);
  8545. WARN(pll->refcount != enabled_crtcs,
  8546. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  8547. pll->refcount, enabled_crtcs);
  8548. WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
  8549. sizeof(dpll_hw_state)),
  8550. "pll hw state mismatch\n");
  8551. }
  8552. }
  8553. void
  8554. intel_modeset_check_state(struct drm_device *dev)
  8555. {
  8556. check_connector_state(dev);
  8557. check_encoder_state(dev);
  8558. check_crtc_state(dev);
  8559. check_shared_dpll_state(dev);
  8560. }
  8561. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  8562. int dotclock)
  8563. {
  8564. /*
  8565. * FDI already provided one idea for the dotclock.
  8566. * Yell if the encoder disagrees.
  8567. */
  8568. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  8569. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  8570. pipe_config->adjusted_mode.crtc_clock, dotclock);
  8571. }
  8572. static void update_scanline_offset(struct intel_crtc *crtc)
  8573. {
  8574. struct drm_device *dev = crtc->base.dev;
  8575. /*
  8576. * The scanline counter increments at the leading edge of hsync.
  8577. *
  8578. * On most platforms it starts counting from vtotal-1 on the
  8579. * first active line. That means the scanline counter value is
  8580. * always one less than what we would expect. Ie. just after
  8581. * start of vblank, which also occurs at start of hsync (on the
  8582. * last active line), the scanline counter will read vblank_start-1.
  8583. *
  8584. * On gen2 the scanline counter starts counting from 1 instead
  8585. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  8586. * to keep the value positive), instead of adding one.
  8587. *
  8588. * On HSW+ the behaviour of the scanline counter depends on the output
  8589. * type. For DP ports it behaves like most other platforms, but on HDMI
  8590. * there's an extra 1 line difference. So we need to add two instead of
  8591. * one to the value.
  8592. */
  8593. if (IS_GEN2(dev)) {
  8594. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  8595. int vtotal;
  8596. vtotal = mode->crtc_vtotal;
  8597. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  8598. vtotal /= 2;
  8599. crtc->scanline_offset = vtotal - 1;
  8600. } else if (HAS_DDI(dev) &&
  8601. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
  8602. crtc->scanline_offset = 2;
  8603. } else
  8604. crtc->scanline_offset = 1;
  8605. }
  8606. static int __intel_set_mode(struct drm_crtc *crtc,
  8607. struct drm_display_mode *mode,
  8608. int x, int y, struct drm_framebuffer *fb)
  8609. {
  8610. struct drm_device *dev = crtc->dev;
  8611. struct drm_i915_private *dev_priv = dev->dev_private;
  8612. struct drm_display_mode *saved_mode;
  8613. struct intel_crtc_config *pipe_config = NULL;
  8614. struct intel_crtc *intel_crtc;
  8615. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  8616. int ret = 0;
  8617. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  8618. if (!saved_mode)
  8619. return -ENOMEM;
  8620. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  8621. &prepare_pipes, &disable_pipes);
  8622. *saved_mode = crtc->mode;
  8623. /* Hack: Because we don't (yet) support global modeset on multiple
  8624. * crtcs, we don't keep track of the new mode for more than one crtc.
  8625. * Hence simply check whether any bit is set in modeset_pipes in all the
  8626. * pieces of code that are not yet converted to deal with mutliple crtcs
  8627. * changing their mode at the same time. */
  8628. if (modeset_pipes) {
  8629. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  8630. if (IS_ERR(pipe_config)) {
  8631. ret = PTR_ERR(pipe_config);
  8632. pipe_config = NULL;
  8633. goto out;
  8634. }
  8635. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  8636. "[modeset]");
  8637. to_intel_crtc(crtc)->new_config = pipe_config;
  8638. }
  8639. /*
  8640. * See if the config requires any additional preparation, e.g.
  8641. * to adjust global state with pipes off. We need to do this
  8642. * here so we can get the modeset_pipe updated config for the new
  8643. * mode set on this crtc. For other crtcs we need to use the
  8644. * adjusted_mode bits in the crtc directly.
  8645. */
  8646. if (IS_VALLEYVIEW(dev)) {
  8647. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  8648. /* may have added more to prepare_pipes than we should */
  8649. prepare_pipes &= ~disable_pipes;
  8650. }
  8651. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  8652. intel_crtc_disable(&intel_crtc->base);
  8653. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  8654. if (intel_crtc->base.enabled)
  8655. dev_priv->display.crtc_disable(&intel_crtc->base);
  8656. }
  8657. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  8658. * to set it here already despite that we pass it down the callchain.
  8659. */
  8660. if (modeset_pipes) {
  8661. crtc->mode = *mode;
  8662. /* mode_set/enable/disable functions rely on a correct pipe
  8663. * config. */
  8664. to_intel_crtc(crtc)->config = *pipe_config;
  8665. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  8666. /*
  8667. * Calculate and store various constants which
  8668. * are later needed by vblank and swap-completion
  8669. * timestamping. They are derived from true hwmode.
  8670. */
  8671. drm_calc_timestamping_constants(crtc,
  8672. &pipe_config->adjusted_mode);
  8673. }
  8674. /* Only after disabling all output pipelines that will be changed can we
  8675. * update the the output configuration. */
  8676. intel_modeset_update_state(dev, prepare_pipes);
  8677. if (dev_priv->display.modeset_global_resources)
  8678. dev_priv->display.modeset_global_resources(dev);
  8679. /* Set up the DPLL and any encoders state that needs to adjust or depend
  8680. * on the DPLL.
  8681. */
  8682. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  8683. struct drm_framebuffer *old_fb;
  8684. mutex_lock(&dev->struct_mutex);
  8685. ret = intel_pin_and_fence_fb_obj(dev,
  8686. to_intel_framebuffer(fb)->obj,
  8687. NULL);
  8688. if (ret != 0) {
  8689. DRM_ERROR("pin & fence failed\n");
  8690. mutex_unlock(&dev->struct_mutex);
  8691. goto done;
  8692. }
  8693. old_fb = crtc->primary->fb;
  8694. if (old_fb)
  8695. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  8696. mutex_unlock(&dev->struct_mutex);
  8697. crtc->primary->fb = fb;
  8698. crtc->x = x;
  8699. crtc->y = y;
  8700. ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
  8701. x, y, fb);
  8702. if (ret)
  8703. goto done;
  8704. }
  8705. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  8706. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  8707. update_scanline_offset(intel_crtc);
  8708. dev_priv->display.crtc_enable(&intel_crtc->base);
  8709. }
  8710. /* FIXME: add subpixel order */
  8711. done:
  8712. if (ret && crtc->enabled)
  8713. crtc->mode = *saved_mode;
  8714. out:
  8715. kfree(pipe_config);
  8716. kfree(saved_mode);
  8717. return ret;
  8718. }
  8719. static int intel_set_mode(struct drm_crtc *crtc,
  8720. struct drm_display_mode *mode,
  8721. int x, int y, struct drm_framebuffer *fb)
  8722. {
  8723. int ret;
  8724. ret = __intel_set_mode(crtc, mode, x, y, fb);
  8725. if (ret == 0)
  8726. intel_modeset_check_state(crtc->dev);
  8727. return ret;
  8728. }
  8729. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  8730. {
  8731. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  8732. }
  8733. #undef for_each_intel_crtc_masked
  8734. static void intel_set_config_free(struct intel_set_config *config)
  8735. {
  8736. if (!config)
  8737. return;
  8738. kfree(config->save_connector_encoders);
  8739. kfree(config->save_encoder_crtcs);
  8740. kfree(config->save_crtc_enabled);
  8741. kfree(config);
  8742. }
  8743. static int intel_set_config_save_state(struct drm_device *dev,
  8744. struct intel_set_config *config)
  8745. {
  8746. struct drm_crtc *crtc;
  8747. struct drm_encoder *encoder;
  8748. struct drm_connector *connector;
  8749. int count;
  8750. config->save_crtc_enabled =
  8751. kcalloc(dev->mode_config.num_crtc,
  8752. sizeof(bool), GFP_KERNEL);
  8753. if (!config->save_crtc_enabled)
  8754. return -ENOMEM;
  8755. config->save_encoder_crtcs =
  8756. kcalloc(dev->mode_config.num_encoder,
  8757. sizeof(struct drm_crtc *), GFP_KERNEL);
  8758. if (!config->save_encoder_crtcs)
  8759. return -ENOMEM;
  8760. config->save_connector_encoders =
  8761. kcalloc(dev->mode_config.num_connector,
  8762. sizeof(struct drm_encoder *), GFP_KERNEL);
  8763. if (!config->save_connector_encoders)
  8764. return -ENOMEM;
  8765. /* Copy data. Note that driver private data is not affected.
  8766. * Should anything bad happen only the expected state is
  8767. * restored, not the drivers personal bookkeeping.
  8768. */
  8769. count = 0;
  8770. for_each_crtc(dev, crtc) {
  8771. config->save_crtc_enabled[count++] = crtc->enabled;
  8772. }
  8773. count = 0;
  8774. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  8775. config->save_encoder_crtcs[count++] = encoder->crtc;
  8776. }
  8777. count = 0;
  8778. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8779. config->save_connector_encoders[count++] = connector->encoder;
  8780. }
  8781. return 0;
  8782. }
  8783. static void intel_set_config_restore_state(struct drm_device *dev,
  8784. struct intel_set_config *config)
  8785. {
  8786. struct intel_crtc *crtc;
  8787. struct intel_encoder *encoder;
  8788. struct intel_connector *connector;
  8789. int count;
  8790. count = 0;
  8791. for_each_intel_crtc(dev, crtc) {
  8792. crtc->new_enabled = config->save_crtc_enabled[count++];
  8793. if (crtc->new_enabled)
  8794. crtc->new_config = &crtc->config;
  8795. else
  8796. crtc->new_config = NULL;
  8797. }
  8798. count = 0;
  8799. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  8800. encoder->new_crtc =
  8801. to_intel_crtc(config->save_encoder_crtcs[count++]);
  8802. }
  8803. count = 0;
  8804. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  8805. connector->new_encoder =
  8806. to_intel_encoder(config->save_connector_encoders[count++]);
  8807. }
  8808. }
  8809. static bool
  8810. is_crtc_connector_off(struct drm_mode_set *set)
  8811. {
  8812. int i;
  8813. if (set->num_connectors == 0)
  8814. return false;
  8815. if (WARN_ON(set->connectors == NULL))
  8816. return false;
  8817. for (i = 0; i < set->num_connectors; i++)
  8818. if (set->connectors[i]->encoder &&
  8819. set->connectors[i]->encoder->crtc == set->crtc &&
  8820. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  8821. return true;
  8822. return false;
  8823. }
  8824. static void
  8825. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  8826. struct intel_set_config *config)
  8827. {
  8828. /* We should be able to check here if the fb has the same properties
  8829. * and then just flip_or_move it */
  8830. if (is_crtc_connector_off(set)) {
  8831. config->mode_changed = true;
  8832. } else if (set->crtc->primary->fb != set->fb) {
  8833. /* If we have no fb then treat it as a full mode set */
  8834. if (set->crtc->primary->fb == NULL) {
  8835. struct intel_crtc *intel_crtc =
  8836. to_intel_crtc(set->crtc);
  8837. if (intel_crtc->active && i915.fastboot) {
  8838. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  8839. config->fb_changed = true;
  8840. } else {
  8841. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  8842. config->mode_changed = true;
  8843. }
  8844. } else if (set->fb == NULL) {
  8845. config->mode_changed = true;
  8846. } else if (set->fb->pixel_format !=
  8847. set->crtc->primary->fb->pixel_format) {
  8848. config->mode_changed = true;
  8849. } else {
  8850. config->fb_changed = true;
  8851. }
  8852. }
  8853. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  8854. config->fb_changed = true;
  8855. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  8856. DRM_DEBUG_KMS("modes are different, full mode set\n");
  8857. drm_mode_debug_printmodeline(&set->crtc->mode);
  8858. drm_mode_debug_printmodeline(set->mode);
  8859. config->mode_changed = true;
  8860. }
  8861. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  8862. set->crtc->base.id, config->mode_changed, config->fb_changed);
  8863. }
  8864. static int
  8865. intel_modeset_stage_output_state(struct drm_device *dev,
  8866. struct drm_mode_set *set,
  8867. struct intel_set_config *config)
  8868. {
  8869. struct intel_connector *connector;
  8870. struct intel_encoder *encoder;
  8871. struct intel_crtc *crtc;
  8872. int ro;
  8873. /* The upper layers ensure that we either disable a crtc or have a list
  8874. * of connectors. For paranoia, double-check this. */
  8875. WARN_ON(!set->fb && (set->num_connectors != 0));
  8876. WARN_ON(set->fb && (set->num_connectors == 0));
  8877. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8878. base.head) {
  8879. /* Otherwise traverse passed in connector list and get encoders
  8880. * for them. */
  8881. for (ro = 0; ro < set->num_connectors; ro++) {
  8882. if (set->connectors[ro] == &connector->base) {
  8883. connector->new_encoder = connector->encoder;
  8884. break;
  8885. }
  8886. }
  8887. /* If we disable the crtc, disable all its connectors. Also, if
  8888. * the connector is on the changing crtc but not on the new
  8889. * connector list, disable it. */
  8890. if ((!set->fb || ro == set->num_connectors) &&
  8891. connector->base.encoder &&
  8892. connector->base.encoder->crtc == set->crtc) {
  8893. connector->new_encoder = NULL;
  8894. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  8895. connector->base.base.id,
  8896. connector->base.name);
  8897. }
  8898. if (&connector->new_encoder->base != connector->base.encoder) {
  8899. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  8900. config->mode_changed = true;
  8901. }
  8902. }
  8903. /* connector->new_encoder is now updated for all connectors. */
  8904. /* Update crtc of enabled connectors. */
  8905. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8906. base.head) {
  8907. struct drm_crtc *new_crtc;
  8908. if (!connector->new_encoder)
  8909. continue;
  8910. new_crtc = connector->new_encoder->base.crtc;
  8911. for (ro = 0; ro < set->num_connectors; ro++) {
  8912. if (set->connectors[ro] == &connector->base)
  8913. new_crtc = set->crtc;
  8914. }
  8915. /* Make sure the new CRTC will work with the encoder */
  8916. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  8917. new_crtc)) {
  8918. return -EINVAL;
  8919. }
  8920. connector->encoder->new_crtc = to_intel_crtc(new_crtc);
  8921. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  8922. connector->base.base.id,
  8923. connector->base.name,
  8924. new_crtc->base.id);
  8925. }
  8926. /* Check for any encoders that needs to be disabled. */
  8927. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8928. base.head) {
  8929. int num_connectors = 0;
  8930. list_for_each_entry(connector,
  8931. &dev->mode_config.connector_list,
  8932. base.head) {
  8933. if (connector->new_encoder == encoder) {
  8934. WARN_ON(!connector->new_encoder->new_crtc);
  8935. num_connectors++;
  8936. }
  8937. }
  8938. if (num_connectors == 0)
  8939. encoder->new_crtc = NULL;
  8940. else if (num_connectors > 1)
  8941. return -EINVAL;
  8942. /* Only now check for crtc changes so we don't miss encoders
  8943. * that will be disabled. */
  8944. if (&encoder->new_crtc->base != encoder->base.crtc) {
  8945. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  8946. config->mode_changed = true;
  8947. }
  8948. }
  8949. /* Now we've also updated encoder->new_crtc for all encoders. */
  8950. for_each_intel_crtc(dev, crtc) {
  8951. crtc->new_enabled = false;
  8952. list_for_each_entry(encoder,
  8953. &dev->mode_config.encoder_list,
  8954. base.head) {
  8955. if (encoder->new_crtc == crtc) {
  8956. crtc->new_enabled = true;
  8957. break;
  8958. }
  8959. }
  8960. if (crtc->new_enabled != crtc->base.enabled) {
  8961. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  8962. crtc->new_enabled ? "en" : "dis");
  8963. config->mode_changed = true;
  8964. }
  8965. if (crtc->new_enabled)
  8966. crtc->new_config = &crtc->config;
  8967. else
  8968. crtc->new_config = NULL;
  8969. }
  8970. return 0;
  8971. }
  8972. static void disable_crtc_nofb(struct intel_crtc *crtc)
  8973. {
  8974. struct drm_device *dev = crtc->base.dev;
  8975. struct intel_encoder *encoder;
  8976. struct intel_connector *connector;
  8977. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  8978. pipe_name(crtc->pipe));
  8979. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  8980. if (connector->new_encoder &&
  8981. connector->new_encoder->new_crtc == crtc)
  8982. connector->new_encoder = NULL;
  8983. }
  8984. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  8985. if (encoder->new_crtc == crtc)
  8986. encoder->new_crtc = NULL;
  8987. }
  8988. crtc->new_enabled = false;
  8989. crtc->new_config = NULL;
  8990. }
  8991. static int intel_crtc_set_config(struct drm_mode_set *set)
  8992. {
  8993. struct drm_device *dev;
  8994. struct drm_mode_set save_set;
  8995. struct intel_set_config *config;
  8996. int ret;
  8997. BUG_ON(!set);
  8998. BUG_ON(!set->crtc);
  8999. BUG_ON(!set->crtc->helper_private);
  9000. /* Enforce sane interface api - has been abused by the fb helper. */
  9001. BUG_ON(!set->mode && set->fb);
  9002. BUG_ON(set->fb && set->num_connectors == 0);
  9003. if (set->fb) {
  9004. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  9005. set->crtc->base.id, set->fb->base.id,
  9006. (int)set->num_connectors, set->x, set->y);
  9007. } else {
  9008. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9009. }
  9010. dev = set->crtc->dev;
  9011. ret = -ENOMEM;
  9012. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9013. if (!config)
  9014. goto out_config;
  9015. ret = intel_set_config_save_state(dev, config);
  9016. if (ret)
  9017. goto out_config;
  9018. save_set.crtc = set->crtc;
  9019. save_set.mode = &set->crtc->mode;
  9020. save_set.x = set->crtc->x;
  9021. save_set.y = set->crtc->y;
  9022. save_set.fb = set->crtc->primary->fb;
  9023. /* Compute whether we need a full modeset, only an fb base update or no
  9024. * change at all. In the future we might also check whether only the
  9025. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9026. * such cases. */
  9027. intel_set_config_compute_mode_changes(set, config);
  9028. ret = intel_modeset_stage_output_state(dev, set, config);
  9029. if (ret)
  9030. goto fail;
  9031. if (config->mode_changed) {
  9032. ret = intel_set_mode(set->crtc, set->mode,
  9033. set->x, set->y, set->fb);
  9034. } else if (config->fb_changed) {
  9035. intel_crtc_wait_for_pending_flips(set->crtc);
  9036. ret = intel_pipe_set_base(set->crtc,
  9037. set->x, set->y, set->fb);
  9038. /*
  9039. * In the fastboot case this may be our only check of the
  9040. * state after boot. It would be better to only do it on
  9041. * the first update, but we don't have a nice way of doing that
  9042. * (and really, set_config isn't used much for high freq page
  9043. * flipping, so increasing its cost here shouldn't be a big
  9044. * deal).
  9045. */
  9046. if (i915.fastboot && ret == 0)
  9047. intel_modeset_check_state(set->crtc->dev);
  9048. }
  9049. if (ret) {
  9050. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9051. set->crtc->base.id, ret);
  9052. fail:
  9053. intel_set_config_restore_state(dev, config);
  9054. /*
  9055. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9056. * force the pipe off to avoid oopsing in the modeset code
  9057. * due to fb==NULL. This should only happen during boot since
  9058. * we don't yet reconstruct the FB from the hardware state.
  9059. */
  9060. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9061. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9062. /* Try to restore the config */
  9063. if (config->mode_changed &&
  9064. intel_set_mode(save_set.crtc, save_set.mode,
  9065. save_set.x, save_set.y, save_set.fb))
  9066. DRM_ERROR("failed to restore config after modeset failure\n");
  9067. }
  9068. out_config:
  9069. intel_set_config_free(config);
  9070. return ret;
  9071. }
  9072. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9073. .cursor_set = intel_crtc_cursor_set,
  9074. .cursor_move = intel_crtc_cursor_move,
  9075. .gamma_set = intel_crtc_gamma_set,
  9076. .set_config = intel_crtc_set_config,
  9077. .destroy = intel_crtc_destroy,
  9078. .page_flip = intel_crtc_page_flip,
  9079. };
  9080. static void intel_cpu_pll_init(struct drm_device *dev)
  9081. {
  9082. if (HAS_DDI(dev))
  9083. intel_ddi_pll_init(dev);
  9084. }
  9085. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9086. struct intel_shared_dpll *pll,
  9087. struct intel_dpll_hw_state *hw_state)
  9088. {
  9089. uint32_t val;
  9090. val = I915_READ(PCH_DPLL(pll->id));
  9091. hw_state->dpll = val;
  9092. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9093. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9094. return val & DPLL_VCO_ENABLE;
  9095. }
  9096. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9097. struct intel_shared_dpll *pll)
  9098. {
  9099. I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
  9100. I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
  9101. }
  9102. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9103. struct intel_shared_dpll *pll)
  9104. {
  9105. /* PCH refclock must be enabled first */
  9106. ibx_assert_pch_refclk_enabled(dev_priv);
  9107. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9108. /* Wait for the clocks to stabilize. */
  9109. POSTING_READ(PCH_DPLL(pll->id));
  9110. udelay(150);
  9111. /* The pixel multiplier can only be updated once the
  9112. * DPLL is enabled and the clocks are stable.
  9113. *
  9114. * So write it again.
  9115. */
  9116. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9117. POSTING_READ(PCH_DPLL(pll->id));
  9118. udelay(200);
  9119. }
  9120. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9121. struct intel_shared_dpll *pll)
  9122. {
  9123. struct drm_device *dev = dev_priv->dev;
  9124. struct intel_crtc *crtc;
  9125. /* Make sure no transcoder isn't still depending on us. */
  9126. for_each_intel_crtc(dev, crtc) {
  9127. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9128. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9129. }
  9130. I915_WRITE(PCH_DPLL(pll->id), 0);
  9131. POSTING_READ(PCH_DPLL(pll->id));
  9132. udelay(200);
  9133. }
  9134. static char *ibx_pch_dpll_names[] = {
  9135. "PCH DPLL A",
  9136. "PCH DPLL B",
  9137. };
  9138. static void ibx_pch_dpll_init(struct drm_device *dev)
  9139. {
  9140. struct drm_i915_private *dev_priv = dev->dev_private;
  9141. int i;
  9142. dev_priv->num_shared_dpll = 2;
  9143. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9144. dev_priv->shared_dplls[i].id = i;
  9145. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9146. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9147. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9148. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9149. dev_priv->shared_dplls[i].get_hw_state =
  9150. ibx_pch_dpll_get_hw_state;
  9151. }
  9152. }
  9153. static void intel_shared_dpll_init(struct drm_device *dev)
  9154. {
  9155. struct drm_i915_private *dev_priv = dev->dev_private;
  9156. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9157. ibx_pch_dpll_init(dev);
  9158. else
  9159. dev_priv->num_shared_dpll = 0;
  9160. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9161. }
  9162. static void intel_crtc_init(struct drm_device *dev, int pipe)
  9163. {
  9164. struct drm_i915_private *dev_priv = dev->dev_private;
  9165. struct intel_crtc *intel_crtc;
  9166. int i;
  9167. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  9168. if (intel_crtc == NULL)
  9169. return;
  9170. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  9171. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  9172. for (i = 0; i < 256; i++) {
  9173. intel_crtc->lut_r[i] = i;
  9174. intel_crtc->lut_g[i] = i;
  9175. intel_crtc->lut_b[i] = i;
  9176. }
  9177. /*
  9178. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  9179. * is hooked to plane B. Hence we want plane A feeding pipe B.
  9180. */
  9181. intel_crtc->pipe = pipe;
  9182. intel_crtc->plane = pipe;
  9183. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  9184. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  9185. intel_crtc->plane = !pipe;
  9186. }
  9187. init_waitqueue_head(&intel_crtc->vbl_wait);
  9188. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  9189. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  9190. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  9191. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  9192. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  9193. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  9194. }
  9195. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  9196. {
  9197. struct drm_encoder *encoder = connector->base.encoder;
  9198. struct drm_device *dev = connector->base.dev;
  9199. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  9200. if (!encoder)
  9201. return INVALID_PIPE;
  9202. return to_intel_crtc(encoder->crtc)->pipe;
  9203. }
  9204. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  9205. struct drm_file *file)
  9206. {
  9207. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  9208. struct drm_mode_object *drmmode_obj;
  9209. struct intel_crtc *crtc;
  9210. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  9211. return -ENODEV;
  9212. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  9213. DRM_MODE_OBJECT_CRTC);
  9214. if (!drmmode_obj) {
  9215. DRM_ERROR("no such CRTC id\n");
  9216. return -ENOENT;
  9217. }
  9218. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  9219. pipe_from_crtc_id->pipe = crtc->pipe;
  9220. return 0;
  9221. }
  9222. static int intel_encoder_clones(struct intel_encoder *encoder)
  9223. {
  9224. struct drm_device *dev = encoder->base.dev;
  9225. struct intel_encoder *source_encoder;
  9226. int index_mask = 0;
  9227. int entry = 0;
  9228. list_for_each_entry(source_encoder,
  9229. &dev->mode_config.encoder_list, base.head) {
  9230. if (encoders_cloneable(encoder, source_encoder))
  9231. index_mask |= (1 << entry);
  9232. entry++;
  9233. }
  9234. return index_mask;
  9235. }
  9236. static bool has_edp_a(struct drm_device *dev)
  9237. {
  9238. struct drm_i915_private *dev_priv = dev->dev_private;
  9239. if (!IS_MOBILE(dev))
  9240. return false;
  9241. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  9242. return false;
  9243. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  9244. return false;
  9245. return true;
  9246. }
  9247. const char *intel_output_name(int output)
  9248. {
  9249. static const char *names[] = {
  9250. [INTEL_OUTPUT_UNUSED] = "Unused",
  9251. [INTEL_OUTPUT_ANALOG] = "Analog",
  9252. [INTEL_OUTPUT_DVO] = "DVO",
  9253. [INTEL_OUTPUT_SDVO] = "SDVO",
  9254. [INTEL_OUTPUT_LVDS] = "LVDS",
  9255. [INTEL_OUTPUT_TVOUT] = "TV",
  9256. [INTEL_OUTPUT_HDMI] = "HDMI",
  9257. [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
  9258. [INTEL_OUTPUT_EDP] = "eDP",
  9259. [INTEL_OUTPUT_DSI] = "DSI",
  9260. [INTEL_OUTPUT_UNKNOWN] = "Unknown",
  9261. };
  9262. if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
  9263. return "Invalid";
  9264. return names[output];
  9265. }
  9266. static void intel_setup_outputs(struct drm_device *dev)
  9267. {
  9268. struct drm_i915_private *dev_priv = dev->dev_private;
  9269. struct intel_encoder *encoder;
  9270. bool dpd_is_edp = false;
  9271. intel_lvds_init(dev);
  9272. if (!IS_ULT(dev) && !IS_CHERRYVIEW(dev))
  9273. intel_crt_init(dev);
  9274. if (HAS_DDI(dev)) {
  9275. int found;
  9276. /* Haswell uses DDI functions to detect digital outputs */
  9277. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  9278. /* DDI A only supports eDP */
  9279. if (found)
  9280. intel_ddi_init(dev, PORT_A);
  9281. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  9282. * register */
  9283. found = I915_READ(SFUSE_STRAP);
  9284. if (found & SFUSE_STRAP_DDIB_DETECTED)
  9285. intel_ddi_init(dev, PORT_B);
  9286. if (found & SFUSE_STRAP_DDIC_DETECTED)
  9287. intel_ddi_init(dev, PORT_C);
  9288. if (found & SFUSE_STRAP_DDID_DETECTED)
  9289. intel_ddi_init(dev, PORT_D);
  9290. } else if (HAS_PCH_SPLIT(dev)) {
  9291. int found;
  9292. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  9293. if (has_edp_a(dev))
  9294. intel_dp_init(dev, DP_A, PORT_A);
  9295. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  9296. /* PCH SDVOB multiplex with HDMIB */
  9297. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  9298. if (!found)
  9299. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  9300. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  9301. intel_dp_init(dev, PCH_DP_B, PORT_B);
  9302. }
  9303. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  9304. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  9305. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  9306. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  9307. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  9308. intel_dp_init(dev, PCH_DP_C, PORT_C);
  9309. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  9310. intel_dp_init(dev, PCH_DP_D, PORT_D);
  9311. } else if (IS_VALLEYVIEW(dev)) {
  9312. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
  9313. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  9314. PORT_B);
  9315. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
  9316. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  9317. }
  9318. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
  9319. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  9320. PORT_C);
  9321. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
  9322. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  9323. }
  9324. if (IS_CHERRYVIEW(dev)) {
  9325. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
  9326. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  9327. PORT_D);
  9328. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  9329. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  9330. }
  9331. }
  9332. intel_dsi_init(dev);
  9333. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  9334. bool found = false;
  9335. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  9336. DRM_DEBUG_KMS("probing SDVOB\n");
  9337. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  9338. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  9339. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  9340. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  9341. }
  9342. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  9343. intel_dp_init(dev, DP_B, PORT_B);
  9344. }
  9345. /* Before G4X SDVOC doesn't have its own detect register */
  9346. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  9347. DRM_DEBUG_KMS("probing SDVOC\n");
  9348. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  9349. }
  9350. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  9351. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  9352. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  9353. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  9354. }
  9355. if (SUPPORTS_INTEGRATED_DP(dev))
  9356. intel_dp_init(dev, DP_C, PORT_C);
  9357. }
  9358. if (SUPPORTS_INTEGRATED_DP(dev) &&
  9359. (I915_READ(DP_D) & DP_DETECTED))
  9360. intel_dp_init(dev, DP_D, PORT_D);
  9361. } else if (IS_GEN2(dev))
  9362. intel_dvo_init(dev);
  9363. if (SUPPORTS_TV(dev))
  9364. intel_tv_init(dev);
  9365. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  9366. encoder->base.possible_crtcs = encoder->crtc_mask;
  9367. encoder->base.possible_clones =
  9368. intel_encoder_clones(encoder);
  9369. }
  9370. intel_init_pch_refclk(dev);
  9371. drm_helper_move_panel_connectors_to_head(dev);
  9372. }
  9373. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  9374. {
  9375. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  9376. drm_framebuffer_cleanup(fb);
  9377. WARN_ON(!intel_fb->obj->framebuffer_references--);
  9378. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  9379. kfree(intel_fb);
  9380. }
  9381. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  9382. struct drm_file *file,
  9383. unsigned int *handle)
  9384. {
  9385. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  9386. struct drm_i915_gem_object *obj = intel_fb->obj;
  9387. return drm_gem_handle_create(file, &obj->base, handle);
  9388. }
  9389. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  9390. .destroy = intel_user_framebuffer_destroy,
  9391. .create_handle = intel_user_framebuffer_create_handle,
  9392. };
  9393. static int intel_framebuffer_init(struct drm_device *dev,
  9394. struct intel_framebuffer *intel_fb,
  9395. struct drm_mode_fb_cmd2 *mode_cmd,
  9396. struct drm_i915_gem_object *obj)
  9397. {
  9398. int aligned_height;
  9399. int pitch_limit;
  9400. int ret;
  9401. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  9402. if (obj->tiling_mode == I915_TILING_Y) {
  9403. DRM_DEBUG("hardware does not support tiling Y\n");
  9404. return -EINVAL;
  9405. }
  9406. if (mode_cmd->pitches[0] & 63) {
  9407. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  9408. mode_cmd->pitches[0]);
  9409. return -EINVAL;
  9410. }
  9411. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  9412. pitch_limit = 32*1024;
  9413. } else if (INTEL_INFO(dev)->gen >= 4) {
  9414. if (obj->tiling_mode)
  9415. pitch_limit = 16*1024;
  9416. else
  9417. pitch_limit = 32*1024;
  9418. } else if (INTEL_INFO(dev)->gen >= 3) {
  9419. if (obj->tiling_mode)
  9420. pitch_limit = 8*1024;
  9421. else
  9422. pitch_limit = 16*1024;
  9423. } else
  9424. /* XXX DSPC is limited to 4k tiled */
  9425. pitch_limit = 8*1024;
  9426. if (mode_cmd->pitches[0] > pitch_limit) {
  9427. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  9428. obj->tiling_mode ? "tiled" : "linear",
  9429. mode_cmd->pitches[0], pitch_limit);
  9430. return -EINVAL;
  9431. }
  9432. if (obj->tiling_mode != I915_TILING_NONE &&
  9433. mode_cmd->pitches[0] != obj->stride) {
  9434. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  9435. mode_cmd->pitches[0], obj->stride);
  9436. return -EINVAL;
  9437. }
  9438. /* Reject formats not supported by any plane early. */
  9439. switch (mode_cmd->pixel_format) {
  9440. case DRM_FORMAT_C8:
  9441. case DRM_FORMAT_RGB565:
  9442. case DRM_FORMAT_XRGB8888:
  9443. case DRM_FORMAT_ARGB8888:
  9444. break;
  9445. case DRM_FORMAT_XRGB1555:
  9446. case DRM_FORMAT_ARGB1555:
  9447. if (INTEL_INFO(dev)->gen > 3) {
  9448. DRM_DEBUG("unsupported pixel format: %s\n",
  9449. drm_get_format_name(mode_cmd->pixel_format));
  9450. return -EINVAL;
  9451. }
  9452. break;
  9453. case DRM_FORMAT_XBGR8888:
  9454. case DRM_FORMAT_ABGR8888:
  9455. case DRM_FORMAT_XRGB2101010:
  9456. case DRM_FORMAT_ARGB2101010:
  9457. case DRM_FORMAT_XBGR2101010:
  9458. case DRM_FORMAT_ABGR2101010:
  9459. if (INTEL_INFO(dev)->gen < 4) {
  9460. DRM_DEBUG("unsupported pixel format: %s\n",
  9461. drm_get_format_name(mode_cmd->pixel_format));
  9462. return -EINVAL;
  9463. }
  9464. break;
  9465. case DRM_FORMAT_YUYV:
  9466. case DRM_FORMAT_UYVY:
  9467. case DRM_FORMAT_YVYU:
  9468. case DRM_FORMAT_VYUY:
  9469. if (INTEL_INFO(dev)->gen < 5) {
  9470. DRM_DEBUG("unsupported pixel format: %s\n",
  9471. drm_get_format_name(mode_cmd->pixel_format));
  9472. return -EINVAL;
  9473. }
  9474. break;
  9475. default:
  9476. DRM_DEBUG("unsupported pixel format: %s\n",
  9477. drm_get_format_name(mode_cmd->pixel_format));
  9478. return -EINVAL;
  9479. }
  9480. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  9481. if (mode_cmd->offsets[0] != 0)
  9482. return -EINVAL;
  9483. aligned_height = intel_align_height(dev, mode_cmd->height,
  9484. obj->tiling_mode);
  9485. /* FIXME drm helper for size checks (especially planar formats)? */
  9486. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  9487. return -EINVAL;
  9488. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  9489. intel_fb->obj = obj;
  9490. intel_fb->obj->framebuffer_references++;
  9491. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  9492. if (ret) {
  9493. DRM_ERROR("framebuffer init failed %d\n", ret);
  9494. return ret;
  9495. }
  9496. return 0;
  9497. }
  9498. static struct drm_framebuffer *
  9499. intel_user_framebuffer_create(struct drm_device *dev,
  9500. struct drm_file *filp,
  9501. struct drm_mode_fb_cmd2 *mode_cmd)
  9502. {
  9503. struct drm_i915_gem_object *obj;
  9504. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  9505. mode_cmd->handles[0]));
  9506. if (&obj->base == NULL)
  9507. return ERR_PTR(-ENOENT);
  9508. return intel_framebuffer_create(dev, mode_cmd, obj);
  9509. }
  9510. #ifndef CONFIG_DRM_I915_FBDEV
  9511. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  9512. {
  9513. }
  9514. #endif
  9515. static const struct drm_mode_config_funcs intel_mode_funcs = {
  9516. .fb_create = intel_user_framebuffer_create,
  9517. .output_poll_changed = intel_fbdev_output_poll_changed,
  9518. };
  9519. /* Set up chip specific display functions */
  9520. static void intel_init_display(struct drm_device *dev)
  9521. {
  9522. struct drm_i915_private *dev_priv = dev->dev_private;
  9523. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  9524. dev_priv->display.find_dpll = g4x_find_best_dpll;
  9525. else if (IS_CHERRYVIEW(dev))
  9526. dev_priv->display.find_dpll = chv_find_best_dpll;
  9527. else if (IS_VALLEYVIEW(dev))
  9528. dev_priv->display.find_dpll = vlv_find_best_dpll;
  9529. else if (IS_PINEVIEW(dev))
  9530. dev_priv->display.find_dpll = pnv_find_best_dpll;
  9531. else
  9532. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  9533. if (HAS_DDI(dev)) {
  9534. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  9535. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  9536. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  9537. dev_priv->display.crtc_enable = haswell_crtc_enable;
  9538. dev_priv->display.crtc_disable = haswell_crtc_disable;
  9539. dev_priv->display.off = haswell_crtc_off;
  9540. dev_priv->display.update_primary_plane =
  9541. ironlake_update_primary_plane;
  9542. } else if (HAS_PCH_SPLIT(dev)) {
  9543. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  9544. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  9545. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  9546. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  9547. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  9548. dev_priv->display.off = ironlake_crtc_off;
  9549. dev_priv->display.update_primary_plane =
  9550. ironlake_update_primary_plane;
  9551. } else if (IS_VALLEYVIEW(dev)) {
  9552. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  9553. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  9554. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  9555. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  9556. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  9557. dev_priv->display.off = i9xx_crtc_off;
  9558. dev_priv->display.update_primary_plane =
  9559. i9xx_update_primary_plane;
  9560. } else {
  9561. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  9562. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  9563. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  9564. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  9565. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  9566. dev_priv->display.off = i9xx_crtc_off;
  9567. dev_priv->display.update_primary_plane =
  9568. i9xx_update_primary_plane;
  9569. }
  9570. /* Returns the core display clock speed */
  9571. if (IS_VALLEYVIEW(dev))
  9572. dev_priv->display.get_display_clock_speed =
  9573. valleyview_get_display_clock_speed;
  9574. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  9575. dev_priv->display.get_display_clock_speed =
  9576. i945_get_display_clock_speed;
  9577. else if (IS_I915G(dev))
  9578. dev_priv->display.get_display_clock_speed =
  9579. i915_get_display_clock_speed;
  9580. else if (IS_I945GM(dev) || IS_845G(dev))
  9581. dev_priv->display.get_display_clock_speed =
  9582. i9xx_misc_get_display_clock_speed;
  9583. else if (IS_PINEVIEW(dev))
  9584. dev_priv->display.get_display_clock_speed =
  9585. pnv_get_display_clock_speed;
  9586. else if (IS_I915GM(dev))
  9587. dev_priv->display.get_display_clock_speed =
  9588. i915gm_get_display_clock_speed;
  9589. else if (IS_I865G(dev))
  9590. dev_priv->display.get_display_clock_speed =
  9591. i865_get_display_clock_speed;
  9592. else if (IS_I85X(dev))
  9593. dev_priv->display.get_display_clock_speed =
  9594. i855_get_display_clock_speed;
  9595. else /* 852, 830 */
  9596. dev_priv->display.get_display_clock_speed =
  9597. i830_get_display_clock_speed;
  9598. if (HAS_PCH_SPLIT(dev)) {
  9599. if (IS_GEN5(dev)) {
  9600. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  9601. dev_priv->display.write_eld = ironlake_write_eld;
  9602. } else if (IS_GEN6(dev)) {
  9603. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  9604. dev_priv->display.write_eld = ironlake_write_eld;
  9605. dev_priv->display.modeset_global_resources =
  9606. snb_modeset_global_resources;
  9607. } else if (IS_IVYBRIDGE(dev)) {
  9608. /* FIXME: detect B0+ stepping and use auto training */
  9609. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  9610. dev_priv->display.write_eld = ironlake_write_eld;
  9611. dev_priv->display.modeset_global_resources =
  9612. ivb_modeset_global_resources;
  9613. } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
  9614. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  9615. dev_priv->display.write_eld = haswell_write_eld;
  9616. dev_priv->display.modeset_global_resources =
  9617. haswell_modeset_global_resources;
  9618. }
  9619. } else if (IS_G4X(dev)) {
  9620. dev_priv->display.write_eld = g4x_write_eld;
  9621. } else if (IS_VALLEYVIEW(dev)) {
  9622. dev_priv->display.modeset_global_resources =
  9623. valleyview_modeset_global_resources;
  9624. dev_priv->display.write_eld = ironlake_write_eld;
  9625. }
  9626. /* Default just returns -ENODEV to indicate unsupported */
  9627. dev_priv->display.queue_flip = intel_default_queue_flip;
  9628. switch (INTEL_INFO(dev)->gen) {
  9629. case 2:
  9630. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  9631. break;
  9632. case 3:
  9633. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  9634. break;
  9635. case 4:
  9636. case 5:
  9637. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  9638. break;
  9639. case 6:
  9640. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  9641. break;
  9642. case 7:
  9643. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  9644. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  9645. break;
  9646. }
  9647. intel_panel_init_backlight_funcs(dev);
  9648. }
  9649. /*
  9650. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  9651. * resume, or other times. This quirk makes sure that's the case for
  9652. * affected systems.
  9653. */
  9654. static void quirk_pipea_force(struct drm_device *dev)
  9655. {
  9656. struct drm_i915_private *dev_priv = dev->dev_private;
  9657. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  9658. DRM_INFO("applying pipe a force quirk\n");
  9659. }
  9660. /*
  9661. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  9662. */
  9663. static void quirk_ssc_force_disable(struct drm_device *dev)
  9664. {
  9665. struct drm_i915_private *dev_priv = dev->dev_private;
  9666. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  9667. DRM_INFO("applying lvds SSC disable quirk\n");
  9668. }
  9669. /*
  9670. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  9671. * brightness value
  9672. */
  9673. static void quirk_invert_brightness(struct drm_device *dev)
  9674. {
  9675. struct drm_i915_private *dev_priv = dev->dev_private;
  9676. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  9677. DRM_INFO("applying inverted panel brightness quirk\n");
  9678. }
  9679. struct intel_quirk {
  9680. int device;
  9681. int subsystem_vendor;
  9682. int subsystem_device;
  9683. void (*hook)(struct drm_device *dev);
  9684. };
  9685. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  9686. struct intel_dmi_quirk {
  9687. void (*hook)(struct drm_device *dev);
  9688. const struct dmi_system_id (*dmi_id_list)[];
  9689. };
  9690. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  9691. {
  9692. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  9693. return 1;
  9694. }
  9695. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  9696. {
  9697. .dmi_id_list = &(const struct dmi_system_id[]) {
  9698. {
  9699. .callback = intel_dmi_reverse_brightness,
  9700. .ident = "NCR Corporation",
  9701. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  9702. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  9703. },
  9704. },
  9705. { } /* terminating entry */
  9706. },
  9707. .hook = quirk_invert_brightness,
  9708. },
  9709. };
  9710. static struct intel_quirk intel_quirks[] = {
  9711. /* HP Mini needs pipe A force quirk (LP: #322104) */
  9712. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  9713. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  9714. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  9715. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  9716. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  9717. /* 830 needs to leave pipe A & dpll A up */
  9718. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  9719. /* Lenovo U160 cannot use SSC on LVDS */
  9720. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  9721. /* Sony Vaio Y cannot use SSC on LVDS */
  9722. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  9723. /* Acer Aspire 5734Z must invert backlight brightness */
  9724. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  9725. /* Acer/eMachines G725 */
  9726. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  9727. /* Acer/eMachines e725 */
  9728. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  9729. /* Acer/Packard Bell NCL20 */
  9730. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  9731. /* Acer Aspire 4736Z */
  9732. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  9733. /* Acer Aspire 5336 */
  9734. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  9735. };
  9736. static void intel_init_quirks(struct drm_device *dev)
  9737. {
  9738. struct pci_dev *d = dev->pdev;
  9739. int i;
  9740. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  9741. struct intel_quirk *q = &intel_quirks[i];
  9742. if (d->device == q->device &&
  9743. (d->subsystem_vendor == q->subsystem_vendor ||
  9744. q->subsystem_vendor == PCI_ANY_ID) &&
  9745. (d->subsystem_device == q->subsystem_device ||
  9746. q->subsystem_device == PCI_ANY_ID))
  9747. q->hook(dev);
  9748. }
  9749. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  9750. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  9751. intel_dmi_quirks[i].hook(dev);
  9752. }
  9753. }
  9754. /* Disable the VGA plane that we never use */
  9755. static void i915_disable_vga(struct drm_device *dev)
  9756. {
  9757. struct drm_i915_private *dev_priv = dev->dev_private;
  9758. u8 sr1;
  9759. u32 vga_reg = i915_vgacntrl_reg(dev);
  9760. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  9761. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  9762. outb(SR01, VGA_SR_INDEX);
  9763. sr1 = inb(VGA_SR_DATA);
  9764. outb(sr1 | 1<<5, VGA_SR_DATA);
  9765. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  9766. udelay(300);
  9767. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  9768. POSTING_READ(vga_reg);
  9769. }
  9770. void intel_modeset_init_hw(struct drm_device *dev)
  9771. {
  9772. intel_prepare_ddi(dev);
  9773. intel_init_clock_gating(dev);
  9774. intel_reset_dpio(dev);
  9775. intel_enable_gt_powersave(dev);
  9776. }
  9777. void intel_modeset_suspend_hw(struct drm_device *dev)
  9778. {
  9779. intel_suspend_hw(dev);
  9780. }
  9781. void intel_modeset_init(struct drm_device *dev)
  9782. {
  9783. struct drm_i915_private *dev_priv = dev->dev_private;
  9784. int sprite, ret;
  9785. enum pipe pipe;
  9786. struct intel_crtc *crtc;
  9787. drm_mode_config_init(dev);
  9788. dev->mode_config.min_width = 0;
  9789. dev->mode_config.min_height = 0;
  9790. dev->mode_config.preferred_depth = 24;
  9791. dev->mode_config.prefer_shadow = 1;
  9792. dev->mode_config.funcs = &intel_mode_funcs;
  9793. intel_init_quirks(dev);
  9794. intel_init_pm(dev);
  9795. if (INTEL_INFO(dev)->num_pipes == 0)
  9796. return;
  9797. intel_init_display(dev);
  9798. if (IS_GEN2(dev)) {
  9799. dev->mode_config.max_width = 2048;
  9800. dev->mode_config.max_height = 2048;
  9801. } else if (IS_GEN3(dev)) {
  9802. dev->mode_config.max_width = 4096;
  9803. dev->mode_config.max_height = 4096;
  9804. } else {
  9805. dev->mode_config.max_width = 8192;
  9806. dev->mode_config.max_height = 8192;
  9807. }
  9808. if (IS_GEN2(dev)) {
  9809. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  9810. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  9811. } else {
  9812. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  9813. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  9814. }
  9815. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  9816. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  9817. INTEL_INFO(dev)->num_pipes,
  9818. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  9819. for_each_pipe(pipe) {
  9820. intel_crtc_init(dev, pipe);
  9821. for_each_sprite(pipe, sprite) {
  9822. ret = intel_plane_init(dev, pipe, sprite);
  9823. if (ret)
  9824. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  9825. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  9826. }
  9827. }
  9828. intel_init_dpio(dev);
  9829. intel_reset_dpio(dev);
  9830. intel_cpu_pll_init(dev);
  9831. intel_shared_dpll_init(dev);
  9832. /* Just disable it once at startup */
  9833. i915_disable_vga(dev);
  9834. intel_setup_outputs(dev);
  9835. /* Just in case the BIOS is doing something questionable. */
  9836. intel_disable_fbc(dev);
  9837. drm_modeset_lock_all(dev);
  9838. intel_modeset_setup_hw_state(dev, false);
  9839. drm_modeset_unlock_all(dev);
  9840. for_each_intel_crtc(dev, crtc) {
  9841. if (!crtc->active)
  9842. continue;
  9843. /*
  9844. * Note that reserving the BIOS fb up front prevents us
  9845. * from stuffing other stolen allocations like the ring
  9846. * on top. This prevents some ugliness at boot time, and
  9847. * can even allow for smooth boot transitions if the BIOS
  9848. * fb is large enough for the active pipe configuration.
  9849. */
  9850. if (dev_priv->display.get_plane_config) {
  9851. dev_priv->display.get_plane_config(crtc,
  9852. &crtc->plane_config);
  9853. /*
  9854. * If the fb is shared between multiple heads, we'll
  9855. * just get the first one.
  9856. */
  9857. intel_find_plane_obj(crtc, &crtc->plane_config);
  9858. }
  9859. }
  9860. }
  9861. static void
  9862. intel_connector_break_all_links(struct intel_connector *connector)
  9863. {
  9864. connector->base.dpms = DRM_MODE_DPMS_OFF;
  9865. connector->base.encoder = NULL;
  9866. connector->encoder->connectors_active = false;
  9867. connector->encoder->base.crtc = NULL;
  9868. }
  9869. static void intel_enable_pipe_a(struct drm_device *dev)
  9870. {
  9871. struct intel_connector *connector;
  9872. struct drm_connector *crt = NULL;
  9873. struct intel_load_detect_pipe load_detect_temp;
  9874. struct drm_modeset_acquire_ctx ctx;
  9875. /* We can't just switch on the pipe A, we need to set things up with a
  9876. * proper mode and output configuration. As a gross hack, enable pipe A
  9877. * by enabling the load detect pipe once. */
  9878. list_for_each_entry(connector,
  9879. &dev->mode_config.connector_list,
  9880. base.head) {
  9881. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  9882. crt = &connector->base;
  9883. break;
  9884. }
  9885. }
  9886. if (!crt)
  9887. return;
  9888. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, &ctx))
  9889. intel_release_load_detect_pipe(crt, &load_detect_temp, &ctx);
  9890. }
  9891. static bool
  9892. intel_check_plane_mapping(struct intel_crtc *crtc)
  9893. {
  9894. struct drm_device *dev = crtc->base.dev;
  9895. struct drm_i915_private *dev_priv = dev->dev_private;
  9896. u32 reg, val;
  9897. if (INTEL_INFO(dev)->num_pipes == 1)
  9898. return true;
  9899. reg = DSPCNTR(!crtc->plane);
  9900. val = I915_READ(reg);
  9901. if ((val & DISPLAY_PLANE_ENABLE) &&
  9902. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  9903. return false;
  9904. return true;
  9905. }
  9906. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  9907. {
  9908. struct drm_device *dev = crtc->base.dev;
  9909. struct drm_i915_private *dev_priv = dev->dev_private;
  9910. u32 reg;
  9911. /* Clear any frame start delays used for debugging left by the BIOS */
  9912. reg = PIPECONF(crtc->config.cpu_transcoder);
  9913. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  9914. /* restore vblank interrupts to correct state */
  9915. if (crtc->active)
  9916. drm_vblank_on(dev, crtc->pipe);
  9917. else
  9918. drm_vblank_off(dev, crtc->pipe);
  9919. /* We need to sanitize the plane -> pipe mapping first because this will
  9920. * disable the crtc (and hence change the state) if it is wrong. Note
  9921. * that gen4+ has a fixed plane -> pipe mapping. */
  9922. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  9923. struct intel_connector *connector;
  9924. bool plane;
  9925. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  9926. crtc->base.base.id);
  9927. /* Pipe has the wrong plane attached and the plane is active.
  9928. * Temporarily change the plane mapping and disable everything
  9929. * ... */
  9930. plane = crtc->plane;
  9931. crtc->plane = !plane;
  9932. dev_priv->display.crtc_disable(&crtc->base);
  9933. crtc->plane = plane;
  9934. /* ... and break all links. */
  9935. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9936. base.head) {
  9937. if (connector->encoder->base.crtc != &crtc->base)
  9938. continue;
  9939. intel_connector_break_all_links(connector);
  9940. }
  9941. WARN_ON(crtc->active);
  9942. crtc->base.enabled = false;
  9943. }
  9944. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  9945. crtc->pipe == PIPE_A && !crtc->active) {
  9946. /* BIOS forgot to enable pipe A, this mostly happens after
  9947. * resume. Force-enable the pipe to fix this, the update_dpms
  9948. * call below we restore the pipe to the right state, but leave
  9949. * the required bits on. */
  9950. intel_enable_pipe_a(dev);
  9951. }
  9952. /* Adjust the state of the output pipe according to whether we
  9953. * have active connectors/encoders. */
  9954. intel_crtc_update_dpms(&crtc->base);
  9955. if (crtc->active != crtc->base.enabled) {
  9956. struct intel_encoder *encoder;
  9957. /* This can happen either due to bugs in the get_hw_state
  9958. * functions or because the pipe is force-enabled due to the
  9959. * pipe A quirk. */
  9960. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  9961. crtc->base.base.id,
  9962. crtc->base.enabled ? "enabled" : "disabled",
  9963. crtc->active ? "enabled" : "disabled");
  9964. crtc->base.enabled = crtc->active;
  9965. /* Because we only establish the connector -> encoder ->
  9966. * crtc links if something is active, this means the
  9967. * crtc is now deactivated. Break the links. connector
  9968. * -> encoder links are only establish when things are
  9969. * actually up, hence no need to break them. */
  9970. WARN_ON(crtc->active);
  9971. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  9972. WARN_ON(encoder->connectors_active);
  9973. encoder->base.crtc = NULL;
  9974. }
  9975. }
  9976. if (crtc->active || IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen < 5) {
  9977. /*
  9978. * We start out with underrun reporting disabled to avoid races.
  9979. * For correct bookkeeping mark this on active crtcs.
  9980. *
  9981. * Also on gmch platforms we dont have any hardware bits to
  9982. * disable the underrun reporting. Which means we need to start
  9983. * out with underrun reporting disabled also on inactive pipes,
  9984. * since otherwise we'll complain about the garbage we read when
  9985. * e.g. coming up after runtime pm.
  9986. *
  9987. * No protection against concurrent access is required - at
  9988. * worst a fifo underrun happens which also sets this to false.
  9989. */
  9990. crtc->cpu_fifo_underrun_disabled = true;
  9991. crtc->pch_fifo_underrun_disabled = true;
  9992. update_scanline_offset(crtc);
  9993. }
  9994. }
  9995. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  9996. {
  9997. struct intel_connector *connector;
  9998. struct drm_device *dev = encoder->base.dev;
  9999. /* We need to check both for a crtc link (meaning that the
  10000. * encoder is active and trying to read from a pipe) and the
  10001. * pipe itself being active. */
  10002. bool has_active_crtc = encoder->base.crtc &&
  10003. to_intel_crtc(encoder->base.crtc)->active;
  10004. if (encoder->connectors_active && !has_active_crtc) {
  10005. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  10006. encoder->base.base.id,
  10007. encoder->base.name);
  10008. /* Connector is active, but has no active pipe. This is
  10009. * fallout from our resume register restoring. Disable
  10010. * the encoder manually again. */
  10011. if (encoder->base.crtc) {
  10012. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  10013. encoder->base.base.id,
  10014. encoder->base.name);
  10015. encoder->disable(encoder);
  10016. }
  10017. /* Inconsistent output/port/pipe state happens presumably due to
  10018. * a bug in one of the get_hw_state functions. Or someplace else
  10019. * in our code, like the register restore mess on resume. Clamp
  10020. * things to off as a safer default. */
  10021. list_for_each_entry(connector,
  10022. &dev->mode_config.connector_list,
  10023. base.head) {
  10024. if (connector->encoder != encoder)
  10025. continue;
  10026. intel_connector_break_all_links(connector);
  10027. }
  10028. }
  10029. /* Enabled encoders without active connectors will be fixed in
  10030. * the crtc fixup. */
  10031. }
  10032. void i915_redisable_vga_power_on(struct drm_device *dev)
  10033. {
  10034. struct drm_i915_private *dev_priv = dev->dev_private;
  10035. u32 vga_reg = i915_vgacntrl_reg(dev);
  10036. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  10037. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  10038. i915_disable_vga(dev);
  10039. }
  10040. }
  10041. void i915_redisable_vga(struct drm_device *dev)
  10042. {
  10043. struct drm_i915_private *dev_priv = dev->dev_private;
  10044. /* This function can be called both from intel_modeset_setup_hw_state or
  10045. * at a very early point in our resume sequence, where the power well
  10046. * structures are not yet restored. Since this function is at a very
  10047. * paranoid "someone might have enabled VGA while we were not looking"
  10048. * level, just check if the power well is enabled instead of trying to
  10049. * follow the "don't touch the power well if we don't need it" policy
  10050. * the rest of the driver uses. */
  10051. if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
  10052. return;
  10053. i915_redisable_vga_power_on(dev);
  10054. }
  10055. static bool primary_get_hw_state(struct intel_crtc *crtc)
  10056. {
  10057. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  10058. if (!crtc->active)
  10059. return false;
  10060. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  10061. }
  10062. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  10063. {
  10064. struct drm_i915_private *dev_priv = dev->dev_private;
  10065. enum pipe pipe;
  10066. struct intel_crtc *crtc;
  10067. struct intel_encoder *encoder;
  10068. struct intel_connector *connector;
  10069. int i;
  10070. for_each_intel_crtc(dev, crtc) {
  10071. memset(&crtc->config, 0, sizeof(crtc->config));
  10072. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  10073. crtc->active = dev_priv->display.get_pipe_config(crtc,
  10074. &crtc->config);
  10075. crtc->base.enabled = crtc->active;
  10076. crtc->primary_enabled = primary_get_hw_state(crtc);
  10077. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  10078. crtc->base.base.id,
  10079. crtc->active ? "enabled" : "disabled");
  10080. }
  10081. /* FIXME: Smash this into the new shared dpll infrastructure. */
  10082. if (HAS_DDI(dev))
  10083. intel_ddi_setup_hw_pll_state(dev);
  10084. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10085. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10086. pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
  10087. pll->active = 0;
  10088. for_each_intel_crtc(dev, crtc) {
  10089. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10090. pll->active++;
  10091. }
  10092. pll->refcount = pll->active;
  10093. DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
  10094. pll->name, pll->refcount, pll->on);
  10095. }
  10096. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  10097. base.head) {
  10098. pipe = 0;
  10099. if (encoder->get_hw_state(encoder, &pipe)) {
  10100. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10101. encoder->base.crtc = &crtc->base;
  10102. encoder->get_config(encoder, &crtc->config);
  10103. } else {
  10104. encoder->base.crtc = NULL;
  10105. }
  10106. encoder->connectors_active = false;
  10107. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  10108. encoder->base.base.id,
  10109. encoder->base.name,
  10110. encoder->base.crtc ? "enabled" : "disabled",
  10111. pipe_name(pipe));
  10112. }
  10113. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10114. base.head) {
  10115. if (connector->get_hw_state(connector)) {
  10116. connector->base.dpms = DRM_MODE_DPMS_ON;
  10117. connector->encoder->connectors_active = true;
  10118. connector->base.encoder = &connector->encoder->base;
  10119. } else {
  10120. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10121. connector->base.encoder = NULL;
  10122. }
  10123. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  10124. connector->base.base.id,
  10125. connector->base.name,
  10126. connector->base.encoder ? "enabled" : "disabled");
  10127. }
  10128. }
  10129. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  10130. * and i915 state tracking structures. */
  10131. void intel_modeset_setup_hw_state(struct drm_device *dev,
  10132. bool force_restore)
  10133. {
  10134. struct drm_i915_private *dev_priv = dev->dev_private;
  10135. enum pipe pipe;
  10136. struct intel_crtc *crtc;
  10137. struct intel_encoder *encoder;
  10138. int i;
  10139. intel_modeset_readout_hw_state(dev);
  10140. /*
  10141. * Now that we have the config, copy it to each CRTC struct
  10142. * Note that this could go away if we move to using crtc_config
  10143. * checking everywhere.
  10144. */
  10145. for_each_intel_crtc(dev, crtc) {
  10146. if (crtc->active && i915.fastboot) {
  10147. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  10148. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  10149. crtc->base.base.id);
  10150. drm_mode_debug_printmodeline(&crtc->base.mode);
  10151. }
  10152. }
  10153. /* HW state is read out, now we need to sanitize this mess. */
  10154. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  10155. base.head) {
  10156. intel_sanitize_encoder(encoder);
  10157. }
  10158. for_each_pipe(pipe) {
  10159. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10160. intel_sanitize_crtc(crtc);
  10161. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  10162. }
  10163. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10164. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10165. if (!pll->on || pll->active)
  10166. continue;
  10167. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  10168. pll->disable(dev_priv, pll);
  10169. pll->on = false;
  10170. }
  10171. if (HAS_PCH_SPLIT(dev))
  10172. ilk_wm_get_hw_state(dev);
  10173. if (force_restore) {
  10174. i915_redisable_vga(dev);
  10175. /*
  10176. * We need to use raw interfaces for restoring state to avoid
  10177. * checking (bogus) intermediate states.
  10178. */
  10179. for_each_pipe(pipe) {
  10180. struct drm_crtc *crtc =
  10181. dev_priv->pipe_to_crtc_mapping[pipe];
  10182. __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  10183. crtc->primary->fb);
  10184. }
  10185. } else {
  10186. intel_modeset_update_staged_output_state(dev);
  10187. }
  10188. intel_modeset_check_state(dev);
  10189. }
  10190. void intel_modeset_gem_init(struct drm_device *dev)
  10191. {
  10192. struct drm_crtc *c;
  10193. struct intel_framebuffer *fb;
  10194. mutex_lock(&dev->struct_mutex);
  10195. intel_init_gt_powersave(dev);
  10196. mutex_unlock(&dev->struct_mutex);
  10197. intel_modeset_init_hw(dev);
  10198. intel_setup_overlay(dev);
  10199. /*
  10200. * Make sure any fbs we allocated at startup are properly
  10201. * pinned & fenced. When we do the allocation it's too early
  10202. * for this.
  10203. */
  10204. mutex_lock(&dev->struct_mutex);
  10205. for_each_crtc(dev, c) {
  10206. if (!c->primary->fb)
  10207. continue;
  10208. fb = to_intel_framebuffer(c->primary->fb);
  10209. if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL)) {
  10210. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  10211. to_intel_crtc(c)->pipe);
  10212. drm_framebuffer_unreference(c->primary->fb);
  10213. c->primary->fb = NULL;
  10214. }
  10215. }
  10216. mutex_unlock(&dev->struct_mutex);
  10217. }
  10218. void intel_connector_unregister(struct intel_connector *intel_connector)
  10219. {
  10220. struct drm_connector *connector = &intel_connector->base;
  10221. intel_panel_destroy_backlight(connector);
  10222. drm_sysfs_connector_remove(connector);
  10223. }
  10224. void intel_modeset_cleanup(struct drm_device *dev)
  10225. {
  10226. struct drm_i915_private *dev_priv = dev->dev_private;
  10227. struct drm_crtc *crtc;
  10228. struct drm_connector *connector;
  10229. /*
  10230. * Interrupts and polling as the first thing to avoid creating havoc.
  10231. * Too much stuff here (turning of rps, connectors, ...) would
  10232. * experience fancy races otherwise.
  10233. */
  10234. drm_irq_uninstall(dev);
  10235. cancel_work_sync(&dev_priv->hotplug_work);
  10236. /*
  10237. * Due to the hpd irq storm handling the hotplug work can re-arm the
  10238. * poll handlers. Hence disable polling after hpd handling is shut down.
  10239. */
  10240. drm_kms_helper_poll_fini(dev);
  10241. mutex_lock(&dev->struct_mutex);
  10242. intel_unregister_dsm_handler();
  10243. for_each_crtc(dev, crtc) {
  10244. /* Skip inactive CRTCs */
  10245. if (!crtc->primary->fb)
  10246. continue;
  10247. intel_increase_pllclock(crtc);
  10248. }
  10249. intel_disable_fbc(dev);
  10250. intel_disable_gt_powersave(dev);
  10251. ironlake_teardown_rc6(dev);
  10252. mutex_unlock(&dev->struct_mutex);
  10253. /* flush any delayed tasks or pending work */
  10254. flush_scheduled_work();
  10255. /* destroy the backlight and sysfs files before encoders/connectors */
  10256. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10257. struct intel_connector *intel_connector;
  10258. intel_connector = to_intel_connector(connector);
  10259. intel_connector->unregister(intel_connector);
  10260. }
  10261. drm_mode_config_cleanup(dev);
  10262. intel_cleanup_overlay(dev);
  10263. mutex_lock(&dev->struct_mutex);
  10264. intel_cleanup_gt_powersave(dev);
  10265. mutex_unlock(&dev->struct_mutex);
  10266. }
  10267. /*
  10268. * Return which encoder is currently attached for connector.
  10269. */
  10270. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  10271. {
  10272. return &intel_attached_encoder(connector)->base;
  10273. }
  10274. void intel_connector_attach_encoder(struct intel_connector *connector,
  10275. struct intel_encoder *encoder)
  10276. {
  10277. connector->encoder = encoder;
  10278. drm_mode_connector_attach_encoder(&connector->base,
  10279. &encoder->base);
  10280. }
  10281. /*
  10282. * set vga decode state - true == enable VGA decode
  10283. */
  10284. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  10285. {
  10286. struct drm_i915_private *dev_priv = dev->dev_private;
  10287. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  10288. u16 gmch_ctrl;
  10289. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  10290. DRM_ERROR("failed to read control word\n");
  10291. return -EIO;
  10292. }
  10293. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  10294. return 0;
  10295. if (state)
  10296. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  10297. else
  10298. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  10299. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  10300. DRM_ERROR("failed to write control word\n");
  10301. return -EIO;
  10302. }
  10303. return 0;
  10304. }
  10305. struct intel_display_error_state {
  10306. u32 power_well_driver;
  10307. int num_transcoders;
  10308. struct intel_cursor_error_state {
  10309. u32 control;
  10310. u32 position;
  10311. u32 base;
  10312. u32 size;
  10313. } cursor[I915_MAX_PIPES];
  10314. struct intel_pipe_error_state {
  10315. bool power_domain_on;
  10316. u32 source;
  10317. u32 stat;
  10318. } pipe[I915_MAX_PIPES];
  10319. struct intel_plane_error_state {
  10320. u32 control;
  10321. u32 stride;
  10322. u32 size;
  10323. u32 pos;
  10324. u32 addr;
  10325. u32 surface;
  10326. u32 tile_offset;
  10327. } plane[I915_MAX_PIPES];
  10328. struct intel_transcoder_error_state {
  10329. bool power_domain_on;
  10330. enum transcoder cpu_transcoder;
  10331. u32 conf;
  10332. u32 htotal;
  10333. u32 hblank;
  10334. u32 hsync;
  10335. u32 vtotal;
  10336. u32 vblank;
  10337. u32 vsync;
  10338. } transcoder[4];
  10339. };
  10340. struct intel_display_error_state *
  10341. intel_display_capture_error_state(struct drm_device *dev)
  10342. {
  10343. struct drm_i915_private *dev_priv = dev->dev_private;
  10344. struct intel_display_error_state *error;
  10345. int transcoders[] = {
  10346. TRANSCODER_A,
  10347. TRANSCODER_B,
  10348. TRANSCODER_C,
  10349. TRANSCODER_EDP,
  10350. };
  10351. int i;
  10352. if (INTEL_INFO(dev)->num_pipes == 0)
  10353. return NULL;
  10354. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  10355. if (error == NULL)
  10356. return NULL;
  10357. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  10358. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  10359. for_each_pipe(i) {
  10360. error->pipe[i].power_domain_on =
  10361. intel_display_power_enabled_sw(dev_priv,
  10362. POWER_DOMAIN_PIPE(i));
  10363. if (!error->pipe[i].power_domain_on)
  10364. continue;
  10365. error->cursor[i].control = I915_READ(CURCNTR(i));
  10366. error->cursor[i].position = I915_READ(CURPOS(i));
  10367. error->cursor[i].base = I915_READ(CURBASE(i));
  10368. error->plane[i].control = I915_READ(DSPCNTR(i));
  10369. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  10370. if (INTEL_INFO(dev)->gen <= 3) {
  10371. error->plane[i].size = I915_READ(DSPSIZE(i));
  10372. error->plane[i].pos = I915_READ(DSPPOS(i));
  10373. }
  10374. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  10375. error->plane[i].addr = I915_READ(DSPADDR(i));
  10376. if (INTEL_INFO(dev)->gen >= 4) {
  10377. error->plane[i].surface = I915_READ(DSPSURF(i));
  10378. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  10379. }
  10380. error->pipe[i].source = I915_READ(PIPESRC(i));
  10381. if (!HAS_PCH_SPLIT(dev))
  10382. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  10383. }
  10384. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  10385. if (HAS_DDI(dev_priv->dev))
  10386. error->num_transcoders++; /* Account for eDP. */
  10387. for (i = 0; i < error->num_transcoders; i++) {
  10388. enum transcoder cpu_transcoder = transcoders[i];
  10389. error->transcoder[i].power_domain_on =
  10390. intel_display_power_enabled_sw(dev_priv,
  10391. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  10392. if (!error->transcoder[i].power_domain_on)
  10393. continue;
  10394. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  10395. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  10396. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  10397. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  10398. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  10399. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  10400. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  10401. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  10402. }
  10403. return error;
  10404. }
  10405. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  10406. void
  10407. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  10408. struct drm_device *dev,
  10409. struct intel_display_error_state *error)
  10410. {
  10411. int i;
  10412. if (!error)
  10413. return;
  10414. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  10415. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  10416. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  10417. error->power_well_driver);
  10418. for_each_pipe(i) {
  10419. err_printf(m, "Pipe [%d]:\n", i);
  10420. err_printf(m, " Power: %s\n",
  10421. error->pipe[i].power_domain_on ? "on" : "off");
  10422. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  10423. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  10424. err_printf(m, "Plane [%d]:\n", i);
  10425. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  10426. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  10427. if (INTEL_INFO(dev)->gen <= 3) {
  10428. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  10429. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  10430. }
  10431. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  10432. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  10433. if (INTEL_INFO(dev)->gen >= 4) {
  10434. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  10435. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  10436. }
  10437. err_printf(m, "Cursor [%d]:\n", i);
  10438. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  10439. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  10440. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  10441. }
  10442. for (i = 0; i < error->num_transcoders; i++) {
  10443. err_printf(m, "CPU transcoder: %c\n",
  10444. transcoder_name(error->transcoder[i].cpu_transcoder));
  10445. err_printf(m, " Power: %s\n",
  10446. error->transcoder[i].power_domain_on ? "on" : "off");
  10447. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  10448. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  10449. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  10450. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  10451. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  10452. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  10453. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  10454. }
  10455. }