intel_display.c 437 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924
  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_atomic.h>
  40. #include <drm/drm_atomic_helper.h>
  41. #include <drm/drm_dp_helper.h>
  42. #include <drm/drm_crtc_helper.h>
  43. #include <drm/drm_plane_helper.h>
  44. #include <drm/drm_rect.h>
  45. #include <linux/dma_remapping.h>
  46. /* Primary plane formats for gen <= 3 */
  47. static const uint32_t i8xx_primary_formats[] = {
  48. DRM_FORMAT_C8,
  49. DRM_FORMAT_RGB565,
  50. DRM_FORMAT_XRGB1555,
  51. DRM_FORMAT_XRGB8888,
  52. };
  53. /* Primary plane formats for gen >= 4 */
  54. static const uint32_t i965_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB8888,
  58. DRM_FORMAT_XBGR8888,
  59. DRM_FORMAT_XRGB2101010,
  60. DRM_FORMAT_XBGR2101010,
  61. };
  62. static const uint32_t skl_primary_formats[] = {
  63. DRM_FORMAT_C8,
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_ARGB8888,
  68. DRM_FORMAT_ABGR8888,
  69. DRM_FORMAT_XRGB2101010,
  70. DRM_FORMAT_XBGR2101010,
  71. };
  72. /* Cursor formats */
  73. static const uint32_t intel_cursor_formats[] = {
  74. DRM_FORMAT_ARGB8888,
  75. };
  76. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  77. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  78. struct intel_crtc_state *pipe_config);
  79. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  80. struct intel_crtc_state *pipe_config);
  81. static int intel_set_mode(struct drm_atomic_state *state);
  82. static int intel_framebuffer_init(struct drm_device *dev,
  83. struct intel_framebuffer *ifb,
  84. struct drm_mode_fb_cmd2 *mode_cmd,
  85. struct drm_i915_gem_object *obj);
  86. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  87. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  88. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  89. struct intel_link_m_n *m_n,
  90. struct intel_link_m_n *m2_n2);
  91. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  92. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  93. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  94. static void vlv_prepare_pll(struct intel_crtc *crtc,
  95. const struct intel_crtc_state *pipe_config);
  96. static void chv_prepare_pll(struct intel_crtc *crtc,
  97. const struct intel_crtc_state *pipe_config);
  98. static void intel_begin_crtc_commit(struct drm_crtc *crtc);
  99. static void intel_finish_crtc_commit(struct drm_crtc *crtc);
  100. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  101. struct intel_crtc_state *crtc_state);
  102. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  103. int num_connectors);
  104. static void intel_crtc_enable_planes(struct drm_crtc *crtc);
  105. static void intel_crtc_disable_planes(struct drm_crtc *crtc);
  106. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  107. {
  108. if (!connector->mst_port)
  109. return connector->encoder;
  110. else
  111. return &connector->mst_port->mst_encoders[pipe]->base;
  112. }
  113. typedef struct {
  114. int min, max;
  115. } intel_range_t;
  116. typedef struct {
  117. int dot_limit;
  118. int p2_slow, p2_fast;
  119. } intel_p2_t;
  120. typedef struct intel_limit intel_limit_t;
  121. struct intel_limit {
  122. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  123. intel_p2_t p2;
  124. };
  125. int
  126. intel_pch_rawclk(struct drm_device *dev)
  127. {
  128. struct drm_i915_private *dev_priv = dev->dev_private;
  129. WARN_ON(!HAS_PCH_SPLIT(dev));
  130. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  131. }
  132. static inline u32 /* units of 100MHz */
  133. intel_fdi_link_freq(struct drm_device *dev)
  134. {
  135. if (IS_GEN5(dev)) {
  136. struct drm_i915_private *dev_priv = dev->dev_private;
  137. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  138. } else
  139. return 27;
  140. }
  141. static const intel_limit_t intel_limits_i8xx_dac = {
  142. .dot = { .min = 25000, .max = 350000 },
  143. .vco = { .min = 908000, .max = 1512000 },
  144. .n = { .min = 2, .max = 16 },
  145. .m = { .min = 96, .max = 140 },
  146. .m1 = { .min = 18, .max = 26 },
  147. .m2 = { .min = 6, .max = 16 },
  148. .p = { .min = 4, .max = 128 },
  149. .p1 = { .min = 2, .max = 33 },
  150. .p2 = { .dot_limit = 165000,
  151. .p2_slow = 4, .p2_fast = 2 },
  152. };
  153. static const intel_limit_t intel_limits_i8xx_dvo = {
  154. .dot = { .min = 25000, .max = 350000 },
  155. .vco = { .min = 908000, .max = 1512000 },
  156. .n = { .min = 2, .max = 16 },
  157. .m = { .min = 96, .max = 140 },
  158. .m1 = { .min = 18, .max = 26 },
  159. .m2 = { .min = 6, .max = 16 },
  160. .p = { .min = 4, .max = 128 },
  161. .p1 = { .min = 2, .max = 33 },
  162. .p2 = { .dot_limit = 165000,
  163. .p2_slow = 4, .p2_fast = 4 },
  164. };
  165. static const intel_limit_t intel_limits_i8xx_lvds = {
  166. .dot = { .min = 25000, .max = 350000 },
  167. .vco = { .min = 908000, .max = 1512000 },
  168. .n = { .min = 2, .max = 16 },
  169. .m = { .min = 96, .max = 140 },
  170. .m1 = { .min = 18, .max = 26 },
  171. .m2 = { .min = 6, .max = 16 },
  172. .p = { .min = 4, .max = 128 },
  173. .p1 = { .min = 1, .max = 6 },
  174. .p2 = { .dot_limit = 165000,
  175. .p2_slow = 14, .p2_fast = 7 },
  176. };
  177. static const intel_limit_t intel_limits_i9xx_sdvo = {
  178. .dot = { .min = 20000, .max = 400000 },
  179. .vco = { .min = 1400000, .max = 2800000 },
  180. .n = { .min = 1, .max = 6 },
  181. .m = { .min = 70, .max = 120 },
  182. .m1 = { .min = 8, .max = 18 },
  183. .m2 = { .min = 3, .max = 7 },
  184. .p = { .min = 5, .max = 80 },
  185. .p1 = { .min = 1, .max = 8 },
  186. .p2 = { .dot_limit = 200000,
  187. .p2_slow = 10, .p2_fast = 5 },
  188. };
  189. static const intel_limit_t intel_limits_i9xx_lvds = {
  190. .dot = { .min = 20000, .max = 400000 },
  191. .vco = { .min = 1400000, .max = 2800000 },
  192. .n = { .min = 1, .max = 6 },
  193. .m = { .min = 70, .max = 120 },
  194. .m1 = { .min = 8, .max = 18 },
  195. .m2 = { .min = 3, .max = 7 },
  196. .p = { .min = 7, .max = 98 },
  197. .p1 = { .min = 1, .max = 8 },
  198. .p2 = { .dot_limit = 112000,
  199. .p2_slow = 14, .p2_fast = 7 },
  200. };
  201. static const intel_limit_t intel_limits_g4x_sdvo = {
  202. .dot = { .min = 25000, .max = 270000 },
  203. .vco = { .min = 1750000, .max = 3500000},
  204. .n = { .min = 1, .max = 4 },
  205. .m = { .min = 104, .max = 138 },
  206. .m1 = { .min = 17, .max = 23 },
  207. .m2 = { .min = 5, .max = 11 },
  208. .p = { .min = 10, .max = 30 },
  209. .p1 = { .min = 1, .max = 3},
  210. .p2 = { .dot_limit = 270000,
  211. .p2_slow = 10,
  212. .p2_fast = 10
  213. },
  214. };
  215. static const intel_limit_t intel_limits_g4x_hdmi = {
  216. .dot = { .min = 22000, .max = 400000 },
  217. .vco = { .min = 1750000, .max = 3500000},
  218. .n = { .min = 1, .max = 4 },
  219. .m = { .min = 104, .max = 138 },
  220. .m1 = { .min = 16, .max = 23 },
  221. .m2 = { .min = 5, .max = 11 },
  222. .p = { .min = 5, .max = 80 },
  223. .p1 = { .min = 1, .max = 8},
  224. .p2 = { .dot_limit = 165000,
  225. .p2_slow = 10, .p2_fast = 5 },
  226. };
  227. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  228. .dot = { .min = 20000, .max = 115000 },
  229. .vco = { .min = 1750000, .max = 3500000 },
  230. .n = { .min = 1, .max = 3 },
  231. .m = { .min = 104, .max = 138 },
  232. .m1 = { .min = 17, .max = 23 },
  233. .m2 = { .min = 5, .max = 11 },
  234. .p = { .min = 28, .max = 112 },
  235. .p1 = { .min = 2, .max = 8 },
  236. .p2 = { .dot_limit = 0,
  237. .p2_slow = 14, .p2_fast = 14
  238. },
  239. };
  240. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  241. .dot = { .min = 80000, .max = 224000 },
  242. .vco = { .min = 1750000, .max = 3500000 },
  243. .n = { .min = 1, .max = 3 },
  244. .m = { .min = 104, .max = 138 },
  245. .m1 = { .min = 17, .max = 23 },
  246. .m2 = { .min = 5, .max = 11 },
  247. .p = { .min = 14, .max = 42 },
  248. .p1 = { .min = 2, .max = 6 },
  249. .p2 = { .dot_limit = 0,
  250. .p2_slow = 7, .p2_fast = 7
  251. },
  252. };
  253. static const intel_limit_t intel_limits_pineview_sdvo = {
  254. .dot = { .min = 20000, .max = 400000},
  255. .vco = { .min = 1700000, .max = 3500000 },
  256. /* Pineview's Ncounter is a ring counter */
  257. .n = { .min = 3, .max = 6 },
  258. .m = { .min = 2, .max = 256 },
  259. /* Pineview only has one combined m divider, which we treat as m2. */
  260. .m1 = { .min = 0, .max = 0 },
  261. .m2 = { .min = 0, .max = 254 },
  262. .p = { .min = 5, .max = 80 },
  263. .p1 = { .min = 1, .max = 8 },
  264. .p2 = { .dot_limit = 200000,
  265. .p2_slow = 10, .p2_fast = 5 },
  266. };
  267. static const intel_limit_t intel_limits_pineview_lvds = {
  268. .dot = { .min = 20000, .max = 400000 },
  269. .vco = { .min = 1700000, .max = 3500000 },
  270. .n = { .min = 3, .max = 6 },
  271. .m = { .min = 2, .max = 256 },
  272. .m1 = { .min = 0, .max = 0 },
  273. .m2 = { .min = 0, .max = 254 },
  274. .p = { .min = 7, .max = 112 },
  275. .p1 = { .min = 1, .max = 8 },
  276. .p2 = { .dot_limit = 112000,
  277. .p2_slow = 14, .p2_fast = 14 },
  278. };
  279. /* Ironlake / Sandybridge
  280. *
  281. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  282. * the range value for them is (actual_value - 2).
  283. */
  284. static const intel_limit_t intel_limits_ironlake_dac = {
  285. .dot = { .min = 25000, .max = 350000 },
  286. .vco = { .min = 1760000, .max = 3510000 },
  287. .n = { .min = 1, .max = 5 },
  288. .m = { .min = 79, .max = 127 },
  289. .m1 = { .min = 12, .max = 22 },
  290. .m2 = { .min = 5, .max = 9 },
  291. .p = { .min = 5, .max = 80 },
  292. .p1 = { .min = 1, .max = 8 },
  293. .p2 = { .dot_limit = 225000,
  294. .p2_slow = 10, .p2_fast = 5 },
  295. };
  296. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  297. .dot = { .min = 25000, .max = 350000 },
  298. .vco = { .min = 1760000, .max = 3510000 },
  299. .n = { .min = 1, .max = 3 },
  300. .m = { .min = 79, .max = 118 },
  301. .m1 = { .min = 12, .max = 22 },
  302. .m2 = { .min = 5, .max = 9 },
  303. .p = { .min = 28, .max = 112 },
  304. .p1 = { .min = 2, .max = 8 },
  305. .p2 = { .dot_limit = 225000,
  306. .p2_slow = 14, .p2_fast = 14 },
  307. };
  308. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  309. .dot = { .min = 25000, .max = 350000 },
  310. .vco = { .min = 1760000, .max = 3510000 },
  311. .n = { .min = 1, .max = 3 },
  312. .m = { .min = 79, .max = 127 },
  313. .m1 = { .min = 12, .max = 22 },
  314. .m2 = { .min = 5, .max = 9 },
  315. .p = { .min = 14, .max = 56 },
  316. .p1 = { .min = 2, .max = 8 },
  317. .p2 = { .dot_limit = 225000,
  318. .p2_slow = 7, .p2_fast = 7 },
  319. };
  320. /* LVDS 100mhz refclk limits. */
  321. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  322. .dot = { .min = 25000, .max = 350000 },
  323. .vco = { .min = 1760000, .max = 3510000 },
  324. .n = { .min = 1, .max = 2 },
  325. .m = { .min = 79, .max = 126 },
  326. .m1 = { .min = 12, .max = 22 },
  327. .m2 = { .min = 5, .max = 9 },
  328. .p = { .min = 28, .max = 112 },
  329. .p1 = { .min = 2, .max = 8 },
  330. .p2 = { .dot_limit = 225000,
  331. .p2_slow = 14, .p2_fast = 14 },
  332. };
  333. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  334. .dot = { .min = 25000, .max = 350000 },
  335. .vco = { .min = 1760000, .max = 3510000 },
  336. .n = { .min = 1, .max = 3 },
  337. .m = { .min = 79, .max = 126 },
  338. .m1 = { .min = 12, .max = 22 },
  339. .m2 = { .min = 5, .max = 9 },
  340. .p = { .min = 14, .max = 42 },
  341. .p1 = { .min = 2, .max = 6 },
  342. .p2 = { .dot_limit = 225000,
  343. .p2_slow = 7, .p2_fast = 7 },
  344. };
  345. static const intel_limit_t intel_limits_vlv = {
  346. /*
  347. * These are the data rate limits (measured in fast clocks)
  348. * since those are the strictest limits we have. The fast
  349. * clock and actual rate limits are more relaxed, so checking
  350. * them would make no difference.
  351. */
  352. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  353. .vco = { .min = 4000000, .max = 6000000 },
  354. .n = { .min = 1, .max = 7 },
  355. .m1 = { .min = 2, .max = 3 },
  356. .m2 = { .min = 11, .max = 156 },
  357. .p1 = { .min = 2, .max = 3 },
  358. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  359. };
  360. static const intel_limit_t intel_limits_chv = {
  361. /*
  362. * These are the data rate limits (measured in fast clocks)
  363. * since those are the strictest limits we have. The fast
  364. * clock and actual rate limits are more relaxed, so checking
  365. * them would make no difference.
  366. */
  367. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  368. .vco = { .min = 4800000, .max = 6480000 },
  369. .n = { .min = 1, .max = 1 },
  370. .m1 = { .min = 2, .max = 2 },
  371. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  372. .p1 = { .min = 2, .max = 4 },
  373. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  374. };
  375. static const intel_limit_t intel_limits_bxt = {
  376. /* FIXME: find real dot limits */
  377. .dot = { .min = 0, .max = INT_MAX },
  378. .vco = { .min = 4800000, .max = 6480000 },
  379. .n = { .min = 1, .max = 1 },
  380. .m1 = { .min = 2, .max = 2 },
  381. /* FIXME: find real m2 limits */
  382. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  383. .p1 = { .min = 2, .max = 4 },
  384. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  385. };
  386. static void vlv_clock(int refclk, intel_clock_t *clock)
  387. {
  388. clock->m = clock->m1 * clock->m2;
  389. clock->p = clock->p1 * clock->p2;
  390. if (WARN_ON(clock->n == 0 || clock->p == 0))
  391. return;
  392. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  393. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  394. }
  395. static bool
  396. needs_modeset(struct drm_crtc_state *state)
  397. {
  398. return state->mode_changed || state->active_changed;
  399. }
  400. /**
  401. * Returns whether any output on the specified pipe is of the specified type
  402. */
  403. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  404. {
  405. struct drm_device *dev = crtc->base.dev;
  406. struct intel_encoder *encoder;
  407. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  408. if (encoder->type == type)
  409. return true;
  410. return false;
  411. }
  412. /**
  413. * Returns whether any output on the specified pipe will have the specified
  414. * type after a staged modeset is complete, i.e., the same as
  415. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  416. * encoder->crtc.
  417. */
  418. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  419. int type)
  420. {
  421. struct drm_atomic_state *state = crtc_state->base.state;
  422. struct drm_connector *connector;
  423. struct drm_connector_state *connector_state;
  424. struct intel_encoder *encoder;
  425. int i, num_connectors = 0;
  426. for_each_connector_in_state(state, connector, connector_state, i) {
  427. if (connector_state->crtc != crtc_state->base.crtc)
  428. continue;
  429. num_connectors++;
  430. encoder = to_intel_encoder(connector_state->best_encoder);
  431. if (encoder->type == type)
  432. return true;
  433. }
  434. WARN_ON(num_connectors == 0);
  435. return false;
  436. }
  437. static const intel_limit_t *
  438. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  439. {
  440. struct drm_device *dev = crtc_state->base.crtc->dev;
  441. const intel_limit_t *limit;
  442. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  443. if (intel_is_dual_link_lvds(dev)) {
  444. if (refclk == 100000)
  445. limit = &intel_limits_ironlake_dual_lvds_100m;
  446. else
  447. limit = &intel_limits_ironlake_dual_lvds;
  448. } else {
  449. if (refclk == 100000)
  450. limit = &intel_limits_ironlake_single_lvds_100m;
  451. else
  452. limit = &intel_limits_ironlake_single_lvds;
  453. }
  454. } else
  455. limit = &intel_limits_ironlake_dac;
  456. return limit;
  457. }
  458. static const intel_limit_t *
  459. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  460. {
  461. struct drm_device *dev = crtc_state->base.crtc->dev;
  462. const intel_limit_t *limit;
  463. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  464. if (intel_is_dual_link_lvds(dev))
  465. limit = &intel_limits_g4x_dual_channel_lvds;
  466. else
  467. limit = &intel_limits_g4x_single_channel_lvds;
  468. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  469. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  470. limit = &intel_limits_g4x_hdmi;
  471. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  472. limit = &intel_limits_g4x_sdvo;
  473. } else /* The option is for other outputs */
  474. limit = &intel_limits_i9xx_sdvo;
  475. return limit;
  476. }
  477. static const intel_limit_t *
  478. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  479. {
  480. struct drm_device *dev = crtc_state->base.crtc->dev;
  481. const intel_limit_t *limit;
  482. if (IS_BROXTON(dev))
  483. limit = &intel_limits_bxt;
  484. else if (HAS_PCH_SPLIT(dev))
  485. limit = intel_ironlake_limit(crtc_state, refclk);
  486. else if (IS_G4X(dev)) {
  487. limit = intel_g4x_limit(crtc_state);
  488. } else if (IS_PINEVIEW(dev)) {
  489. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  490. limit = &intel_limits_pineview_lvds;
  491. else
  492. limit = &intel_limits_pineview_sdvo;
  493. } else if (IS_CHERRYVIEW(dev)) {
  494. limit = &intel_limits_chv;
  495. } else if (IS_VALLEYVIEW(dev)) {
  496. limit = &intel_limits_vlv;
  497. } else if (!IS_GEN2(dev)) {
  498. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  499. limit = &intel_limits_i9xx_lvds;
  500. else
  501. limit = &intel_limits_i9xx_sdvo;
  502. } else {
  503. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  504. limit = &intel_limits_i8xx_lvds;
  505. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  506. limit = &intel_limits_i8xx_dvo;
  507. else
  508. limit = &intel_limits_i8xx_dac;
  509. }
  510. return limit;
  511. }
  512. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  513. static void pineview_clock(int refclk, intel_clock_t *clock)
  514. {
  515. clock->m = clock->m2 + 2;
  516. clock->p = clock->p1 * clock->p2;
  517. if (WARN_ON(clock->n == 0 || clock->p == 0))
  518. return;
  519. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  520. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  521. }
  522. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  523. {
  524. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  525. }
  526. static void i9xx_clock(int refclk, intel_clock_t *clock)
  527. {
  528. clock->m = i9xx_dpll_compute_m(clock);
  529. clock->p = clock->p1 * clock->p2;
  530. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  531. return;
  532. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  533. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  534. }
  535. static void chv_clock(int refclk, intel_clock_t *clock)
  536. {
  537. clock->m = clock->m1 * clock->m2;
  538. clock->p = clock->p1 * clock->p2;
  539. if (WARN_ON(clock->n == 0 || clock->p == 0))
  540. return;
  541. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  542. clock->n << 22);
  543. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  544. }
  545. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  546. /**
  547. * Returns whether the given set of divisors are valid for a given refclk with
  548. * the given connectors.
  549. */
  550. static bool intel_PLL_is_valid(struct drm_device *dev,
  551. const intel_limit_t *limit,
  552. const intel_clock_t *clock)
  553. {
  554. if (clock->n < limit->n.min || limit->n.max < clock->n)
  555. INTELPllInvalid("n out of range\n");
  556. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  557. INTELPllInvalid("p1 out of range\n");
  558. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  559. INTELPllInvalid("m2 out of range\n");
  560. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  561. INTELPllInvalid("m1 out of range\n");
  562. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  563. if (clock->m1 <= clock->m2)
  564. INTELPllInvalid("m1 <= m2\n");
  565. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  566. if (clock->p < limit->p.min || limit->p.max < clock->p)
  567. INTELPllInvalid("p out of range\n");
  568. if (clock->m < limit->m.min || limit->m.max < clock->m)
  569. INTELPllInvalid("m out of range\n");
  570. }
  571. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  572. INTELPllInvalid("vco out of range\n");
  573. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  574. * connector, etc., rather than just a single range.
  575. */
  576. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  577. INTELPllInvalid("dot out of range\n");
  578. return true;
  579. }
  580. static bool
  581. i9xx_find_best_dpll(const intel_limit_t *limit,
  582. struct intel_crtc_state *crtc_state,
  583. int target, int refclk, intel_clock_t *match_clock,
  584. intel_clock_t *best_clock)
  585. {
  586. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  587. struct drm_device *dev = crtc->base.dev;
  588. intel_clock_t clock;
  589. int err = target;
  590. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  591. /*
  592. * For LVDS just rely on its current settings for dual-channel.
  593. * We haven't figured out how to reliably set up different
  594. * single/dual channel state, if we even can.
  595. */
  596. if (intel_is_dual_link_lvds(dev))
  597. clock.p2 = limit->p2.p2_fast;
  598. else
  599. clock.p2 = limit->p2.p2_slow;
  600. } else {
  601. if (target < limit->p2.dot_limit)
  602. clock.p2 = limit->p2.p2_slow;
  603. else
  604. clock.p2 = limit->p2.p2_fast;
  605. }
  606. memset(best_clock, 0, sizeof(*best_clock));
  607. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  608. clock.m1++) {
  609. for (clock.m2 = limit->m2.min;
  610. clock.m2 <= limit->m2.max; clock.m2++) {
  611. if (clock.m2 >= clock.m1)
  612. break;
  613. for (clock.n = limit->n.min;
  614. clock.n <= limit->n.max; clock.n++) {
  615. for (clock.p1 = limit->p1.min;
  616. clock.p1 <= limit->p1.max; clock.p1++) {
  617. int this_err;
  618. i9xx_clock(refclk, &clock);
  619. if (!intel_PLL_is_valid(dev, limit,
  620. &clock))
  621. continue;
  622. if (match_clock &&
  623. clock.p != match_clock->p)
  624. continue;
  625. this_err = abs(clock.dot - target);
  626. if (this_err < err) {
  627. *best_clock = clock;
  628. err = this_err;
  629. }
  630. }
  631. }
  632. }
  633. }
  634. return (err != target);
  635. }
  636. static bool
  637. pnv_find_best_dpll(const intel_limit_t *limit,
  638. struct intel_crtc_state *crtc_state,
  639. int target, int refclk, intel_clock_t *match_clock,
  640. intel_clock_t *best_clock)
  641. {
  642. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  643. struct drm_device *dev = crtc->base.dev;
  644. intel_clock_t clock;
  645. int err = target;
  646. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  647. /*
  648. * For LVDS just rely on its current settings for dual-channel.
  649. * We haven't figured out how to reliably set up different
  650. * single/dual channel state, if we even can.
  651. */
  652. if (intel_is_dual_link_lvds(dev))
  653. clock.p2 = limit->p2.p2_fast;
  654. else
  655. clock.p2 = limit->p2.p2_slow;
  656. } else {
  657. if (target < limit->p2.dot_limit)
  658. clock.p2 = limit->p2.p2_slow;
  659. else
  660. clock.p2 = limit->p2.p2_fast;
  661. }
  662. memset(best_clock, 0, sizeof(*best_clock));
  663. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  664. clock.m1++) {
  665. for (clock.m2 = limit->m2.min;
  666. clock.m2 <= limit->m2.max; clock.m2++) {
  667. for (clock.n = limit->n.min;
  668. clock.n <= limit->n.max; clock.n++) {
  669. for (clock.p1 = limit->p1.min;
  670. clock.p1 <= limit->p1.max; clock.p1++) {
  671. int this_err;
  672. pineview_clock(refclk, &clock);
  673. if (!intel_PLL_is_valid(dev, limit,
  674. &clock))
  675. continue;
  676. if (match_clock &&
  677. clock.p != match_clock->p)
  678. continue;
  679. this_err = abs(clock.dot - target);
  680. if (this_err < err) {
  681. *best_clock = clock;
  682. err = this_err;
  683. }
  684. }
  685. }
  686. }
  687. }
  688. return (err != target);
  689. }
  690. static bool
  691. g4x_find_best_dpll(const intel_limit_t *limit,
  692. struct intel_crtc_state *crtc_state,
  693. int target, int refclk, intel_clock_t *match_clock,
  694. intel_clock_t *best_clock)
  695. {
  696. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  697. struct drm_device *dev = crtc->base.dev;
  698. intel_clock_t clock;
  699. int max_n;
  700. bool found;
  701. /* approximately equals target * 0.00585 */
  702. int err_most = (target >> 8) + (target >> 9);
  703. found = false;
  704. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  705. if (intel_is_dual_link_lvds(dev))
  706. clock.p2 = limit->p2.p2_fast;
  707. else
  708. clock.p2 = limit->p2.p2_slow;
  709. } else {
  710. if (target < limit->p2.dot_limit)
  711. clock.p2 = limit->p2.p2_slow;
  712. else
  713. clock.p2 = limit->p2.p2_fast;
  714. }
  715. memset(best_clock, 0, sizeof(*best_clock));
  716. max_n = limit->n.max;
  717. /* based on hardware requirement, prefer smaller n to precision */
  718. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  719. /* based on hardware requirement, prefere larger m1,m2 */
  720. for (clock.m1 = limit->m1.max;
  721. clock.m1 >= limit->m1.min; clock.m1--) {
  722. for (clock.m2 = limit->m2.max;
  723. clock.m2 >= limit->m2.min; clock.m2--) {
  724. for (clock.p1 = limit->p1.max;
  725. clock.p1 >= limit->p1.min; clock.p1--) {
  726. int this_err;
  727. i9xx_clock(refclk, &clock);
  728. if (!intel_PLL_is_valid(dev, limit,
  729. &clock))
  730. continue;
  731. this_err = abs(clock.dot - target);
  732. if (this_err < err_most) {
  733. *best_clock = clock;
  734. err_most = this_err;
  735. max_n = clock.n;
  736. found = true;
  737. }
  738. }
  739. }
  740. }
  741. }
  742. return found;
  743. }
  744. /*
  745. * Check if the calculated PLL configuration is more optimal compared to the
  746. * best configuration and error found so far. Return the calculated error.
  747. */
  748. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  749. const intel_clock_t *calculated_clock,
  750. const intel_clock_t *best_clock,
  751. unsigned int best_error_ppm,
  752. unsigned int *error_ppm)
  753. {
  754. /*
  755. * For CHV ignore the error and consider only the P value.
  756. * Prefer a bigger P value based on HW requirements.
  757. */
  758. if (IS_CHERRYVIEW(dev)) {
  759. *error_ppm = 0;
  760. return calculated_clock->p > best_clock->p;
  761. }
  762. if (WARN_ON_ONCE(!target_freq))
  763. return false;
  764. *error_ppm = div_u64(1000000ULL *
  765. abs(target_freq - calculated_clock->dot),
  766. target_freq);
  767. /*
  768. * Prefer a better P value over a better (smaller) error if the error
  769. * is small. Ensure this preference for future configurations too by
  770. * setting the error to 0.
  771. */
  772. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  773. *error_ppm = 0;
  774. return true;
  775. }
  776. return *error_ppm + 10 < best_error_ppm;
  777. }
  778. static bool
  779. vlv_find_best_dpll(const intel_limit_t *limit,
  780. struct intel_crtc_state *crtc_state,
  781. int target, int refclk, intel_clock_t *match_clock,
  782. intel_clock_t *best_clock)
  783. {
  784. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  785. struct drm_device *dev = crtc->base.dev;
  786. intel_clock_t clock;
  787. unsigned int bestppm = 1000000;
  788. /* min update 19.2 MHz */
  789. int max_n = min(limit->n.max, refclk / 19200);
  790. bool found = false;
  791. target *= 5; /* fast clock */
  792. memset(best_clock, 0, sizeof(*best_clock));
  793. /* based on hardware requirement, prefer smaller n to precision */
  794. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  795. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  796. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  797. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  798. clock.p = clock.p1 * clock.p2;
  799. /* based on hardware requirement, prefer bigger m1,m2 values */
  800. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  801. unsigned int ppm;
  802. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  803. refclk * clock.m1);
  804. vlv_clock(refclk, &clock);
  805. if (!intel_PLL_is_valid(dev, limit,
  806. &clock))
  807. continue;
  808. if (!vlv_PLL_is_optimal(dev, target,
  809. &clock,
  810. best_clock,
  811. bestppm, &ppm))
  812. continue;
  813. *best_clock = clock;
  814. bestppm = ppm;
  815. found = true;
  816. }
  817. }
  818. }
  819. }
  820. return found;
  821. }
  822. static bool
  823. chv_find_best_dpll(const intel_limit_t *limit,
  824. struct intel_crtc_state *crtc_state,
  825. int target, int refclk, intel_clock_t *match_clock,
  826. intel_clock_t *best_clock)
  827. {
  828. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  829. struct drm_device *dev = crtc->base.dev;
  830. unsigned int best_error_ppm;
  831. intel_clock_t clock;
  832. uint64_t m2;
  833. int found = false;
  834. memset(best_clock, 0, sizeof(*best_clock));
  835. best_error_ppm = 1000000;
  836. /*
  837. * Based on hardware doc, the n always set to 1, and m1 always
  838. * set to 2. If requires to support 200Mhz refclk, we need to
  839. * revisit this because n may not 1 anymore.
  840. */
  841. clock.n = 1, clock.m1 = 2;
  842. target *= 5; /* fast clock */
  843. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  844. for (clock.p2 = limit->p2.p2_fast;
  845. clock.p2 >= limit->p2.p2_slow;
  846. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  847. unsigned int error_ppm;
  848. clock.p = clock.p1 * clock.p2;
  849. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  850. clock.n) << 22, refclk * clock.m1);
  851. if (m2 > INT_MAX/clock.m1)
  852. continue;
  853. clock.m2 = m2;
  854. chv_clock(refclk, &clock);
  855. if (!intel_PLL_is_valid(dev, limit, &clock))
  856. continue;
  857. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  858. best_error_ppm, &error_ppm))
  859. continue;
  860. *best_clock = clock;
  861. best_error_ppm = error_ppm;
  862. found = true;
  863. }
  864. }
  865. return found;
  866. }
  867. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  868. intel_clock_t *best_clock)
  869. {
  870. int refclk = i9xx_get_refclk(crtc_state, 0);
  871. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  872. target_clock, refclk, NULL, best_clock);
  873. }
  874. bool intel_crtc_active(struct drm_crtc *crtc)
  875. {
  876. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  877. /* Be paranoid as we can arrive here with only partial
  878. * state retrieved from the hardware during setup.
  879. *
  880. * We can ditch the adjusted_mode.crtc_clock check as soon
  881. * as Haswell has gained clock readout/fastboot support.
  882. *
  883. * We can ditch the crtc->primary->fb check as soon as we can
  884. * properly reconstruct framebuffers.
  885. *
  886. * FIXME: The intel_crtc->active here should be switched to
  887. * crtc->state->active once we have proper CRTC states wired up
  888. * for atomic.
  889. */
  890. return intel_crtc->active && crtc->primary->state->fb &&
  891. intel_crtc->config->base.adjusted_mode.crtc_clock;
  892. }
  893. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  894. enum pipe pipe)
  895. {
  896. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  897. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  898. return intel_crtc->config->cpu_transcoder;
  899. }
  900. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  901. {
  902. struct drm_i915_private *dev_priv = dev->dev_private;
  903. u32 reg = PIPEDSL(pipe);
  904. u32 line1, line2;
  905. u32 line_mask;
  906. if (IS_GEN2(dev))
  907. line_mask = DSL_LINEMASK_GEN2;
  908. else
  909. line_mask = DSL_LINEMASK_GEN3;
  910. line1 = I915_READ(reg) & line_mask;
  911. mdelay(5);
  912. line2 = I915_READ(reg) & line_mask;
  913. return line1 == line2;
  914. }
  915. /*
  916. * intel_wait_for_pipe_off - wait for pipe to turn off
  917. * @crtc: crtc whose pipe to wait for
  918. *
  919. * After disabling a pipe, we can't wait for vblank in the usual way,
  920. * spinning on the vblank interrupt status bit, since we won't actually
  921. * see an interrupt when the pipe is disabled.
  922. *
  923. * On Gen4 and above:
  924. * wait for the pipe register state bit to turn off
  925. *
  926. * Otherwise:
  927. * wait for the display line value to settle (it usually
  928. * ends up stopping at the start of the next frame).
  929. *
  930. */
  931. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  932. {
  933. struct drm_device *dev = crtc->base.dev;
  934. struct drm_i915_private *dev_priv = dev->dev_private;
  935. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  936. enum pipe pipe = crtc->pipe;
  937. if (INTEL_INFO(dev)->gen >= 4) {
  938. int reg = PIPECONF(cpu_transcoder);
  939. /* Wait for the Pipe State to go off */
  940. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  941. 100))
  942. WARN(1, "pipe_off wait timed out\n");
  943. } else {
  944. /* Wait for the display line to settle */
  945. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  946. WARN(1, "pipe_off wait timed out\n");
  947. }
  948. }
  949. /*
  950. * ibx_digital_port_connected - is the specified port connected?
  951. * @dev_priv: i915 private structure
  952. * @port: the port to test
  953. *
  954. * Returns true if @port is connected, false otherwise.
  955. */
  956. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  957. struct intel_digital_port *port)
  958. {
  959. u32 bit;
  960. if (HAS_PCH_IBX(dev_priv->dev)) {
  961. switch (port->port) {
  962. case PORT_B:
  963. bit = SDE_PORTB_HOTPLUG;
  964. break;
  965. case PORT_C:
  966. bit = SDE_PORTC_HOTPLUG;
  967. break;
  968. case PORT_D:
  969. bit = SDE_PORTD_HOTPLUG;
  970. break;
  971. default:
  972. return true;
  973. }
  974. } else {
  975. switch (port->port) {
  976. case PORT_B:
  977. bit = SDE_PORTB_HOTPLUG_CPT;
  978. break;
  979. case PORT_C:
  980. bit = SDE_PORTC_HOTPLUG_CPT;
  981. break;
  982. case PORT_D:
  983. bit = SDE_PORTD_HOTPLUG_CPT;
  984. break;
  985. default:
  986. return true;
  987. }
  988. }
  989. return I915_READ(SDEISR) & bit;
  990. }
  991. static const char *state_string(bool enabled)
  992. {
  993. return enabled ? "on" : "off";
  994. }
  995. /* Only for pre-ILK configs */
  996. void assert_pll(struct drm_i915_private *dev_priv,
  997. enum pipe pipe, bool state)
  998. {
  999. int reg;
  1000. u32 val;
  1001. bool cur_state;
  1002. reg = DPLL(pipe);
  1003. val = I915_READ(reg);
  1004. cur_state = !!(val & DPLL_VCO_ENABLE);
  1005. I915_STATE_WARN(cur_state != state,
  1006. "PLL state assertion failure (expected %s, current %s)\n",
  1007. state_string(state), state_string(cur_state));
  1008. }
  1009. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1010. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1011. {
  1012. u32 val;
  1013. bool cur_state;
  1014. mutex_lock(&dev_priv->sb_lock);
  1015. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1016. mutex_unlock(&dev_priv->sb_lock);
  1017. cur_state = val & DSI_PLL_VCO_EN;
  1018. I915_STATE_WARN(cur_state != state,
  1019. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1020. state_string(state), state_string(cur_state));
  1021. }
  1022. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1023. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1024. struct intel_shared_dpll *
  1025. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1026. {
  1027. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1028. if (crtc->config->shared_dpll < 0)
  1029. return NULL;
  1030. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1031. }
  1032. /* For ILK+ */
  1033. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1034. struct intel_shared_dpll *pll,
  1035. bool state)
  1036. {
  1037. bool cur_state;
  1038. struct intel_dpll_hw_state hw_state;
  1039. if (WARN (!pll,
  1040. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1041. return;
  1042. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1043. I915_STATE_WARN(cur_state != state,
  1044. "%s assertion failure (expected %s, current %s)\n",
  1045. pll->name, state_string(state), state_string(cur_state));
  1046. }
  1047. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1048. enum pipe pipe, bool state)
  1049. {
  1050. int reg;
  1051. u32 val;
  1052. bool cur_state;
  1053. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1054. pipe);
  1055. if (HAS_DDI(dev_priv->dev)) {
  1056. /* DDI does not have a specific FDI_TX register */
  1057. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1058. val = I915_READ(reg);
  1059. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1060. } else {
  1061. reg = FDI_TX_CTL(pipe);
  1062. val = I915_READ(reg);
  1063. cur_state = !!(val & FDI_TX_ENABLE);
  1064. }
  1065. I915_STATE_WARN(cur_state != state,
  1066. "FDI TX state assertion failure (expected %s, current %s)\n",
  1067. state_string(state), state_string(cur_state));
  1068. }
  1069. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1070. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1071. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1072. enum pipe pipe, bool state)
  1073. {
  1074. int reg;
  1075. u32 val;
  1076. bool cur_state;
  1077. reg = FDI_RX_CTL(pipe);
  1078. val = I915_READ(reg);
  1079. cur_state = !!(val & FDI_RX_ENABLE);
  1080. I915_STATE_WARN(cur_state != state,
  1081. "FDI RX state assertion failure (expected %s, current %s)\n",
  1082. state_string(state), state_string(cur_state));
  1083. }
  1084. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1085. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1086. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1087. enum pipe pipe)
  1088. {
  1089. int reg;
  1090. u32 val;
  1091. /* ILK FDI PLL is always enabled */
  1092. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1093. return;
  1094. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1095. if (HAS_DDI(dev_priv->dev))
  1096. return;
  1097. reg = FDI_TX_CTL(pipe);
  1098. val = I915_READ(reg);
  1099. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1100. }
  1101. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1102. enum pipe pipe, bool state)
  1103. {
  1104. int reg;
  1105. u32 val;
  1106. bool cur_state;
  1107. reg = FDI_RX_CTL(pipe);
  1108. val = I915_READ(reg);
  1109. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1110. I915_STATE_WARN(cur_state != state,
  1111. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1112. state_string(state), state_string(cur_state));
  1113. }
  1114. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1115. enum pipe pipe)
  1116. {
  1117. struct drm_device *dev = dev_priv->dev;
  1118. int pp_reg;
  1119. u32 val;
  1120. enum pipe panel_pipe = PIPE_A;
  1121. bool locked = true;
  1122. if (WARN_ON(HAS_DDI(dev)))
  1123. return;
  1124. if (HAS_PCH_SPLIT(dev)) {
  1125. u32 port_sel;
  1126. pp_reg = PCH_PP_CONTROL;
  1127. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1128. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1129. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1130. panel_pipe = PIPE_B;
  1131. /* XXX: else fix for eDP */
  1132. } else if (IS_VALLEYVIEW(dev)) {
  1133. /* presumably write lock depends on pipe, not port select */
  1134. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1135. panel_pipe = pipe;
  1136. } else {
  1137. pp_reg = PP_CONTROL;
  1138. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1139. panel_pipe = PIPE_B;
  1140. }
  1141. val = I915_READ(pp_reg);
  1142. if (!(val & PANEL_POWER_ON) ||
  1143. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1144. locked = false;
  1145. I915_STATE_WARN(panel_pipe == pipe && locked,
  1146. "panel assertion failure, pipe %c regs locked\n",
  1147. pipe_name(pipe));
  1148. }
  1149. static void assert_cursor(struct drm_i915_private *dev_priv,
  1150. enum pipe pipe, bool state)
  1151. {
  1152. struct drm_device *dev = dev_priv->dev;
  1153. bool cur_state;
  1154. if (IS_845G(dev) || IS_I865G(dev))
  1155. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1156. else
  1157. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1158. I915_STATE_WARN(cur_state != state,
  1159. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1160. pipe_name(pipe), state_string(state), state_string(cur_state));
  1161. }
  1162. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1163. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1164. void assert_pipe(struct drm_i915_private *dev_priv,
  1165. enum pipe pipe, bool state)
  1166. {
  1167. int reg;
  1168. u32 val;
  1169. bool cur_state;
  1170. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1171. pipe);
  1172. /* if we need the pipe quirk it must be always on */
  1173. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1174. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1175. state = true;
  1176. if (!intel_display_power_is_enabled(dev_priv,
  1177. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1178. cur_state = false;
  1179. } else {
  1180. reg = PIPECONF(cpu_transcoder);
  1181. val = I915_READ(reg);
  1182. cur_state = !!(val & PIPECONF_ENABLE);
  1183. }
  1184. I915_STATE_WARN(cur_state != state,
  1185. "pipe %c assertion failure (expected %s, current %s)\n",
  1186. pipe_name(pipe), state_string(state), state_string(cur_state));
  1187. }
  1188. static void assert_plane(struct drm_i915_private *dev_priv,
  1189. enum plane plane, bool state)
  1190. {
  1191. int reg;
  1192. u32 val;
  1193. bool cur_state;
  1194. reg = DSPCNTR(plane);
  1195. val = I915_READ(reg);
  1196. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1197. I915_STATE_WARN(cur_state != state,
  1198. "plane %c assertion failure (expected %s, current %s)\n",
  1199. plane_name(plane), state_string(state), state_string(cur_state));
  1200. }
  1201. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1202. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1203. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1204. enum pipe pipe)
  1205. {
  1206. struct drm_device *dev = dev_priv->dev;
  1207. int reg, i;
  1208. u32 val;
  1209. int cur_pipe;
  1210. /* Primary planes are fixed to pipes on gen4+ */
  1211. if (INTEL_INFO(dev)->gen >= 4) {
  1212. reg = DSPCNTR(pipe);
  1213. val = I915_READ(reg);
  1214. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1215. "plane %c assertion failure, should be disabled but not\n",
  1216. plane_name(pipe));
  1217. return;
  1218. }
  1219. /* Need to check both planes against the pipe */
  1220. for_each_pipe(dev_priv, i) {
  1221. reg = DSPCNTR(i);
  1222. val = I915_READ(reg);
  1223. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1224. DISPPLANE_SEL_PIPE_SHIFT;
  1225. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1226. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1227. plane_name(i), pipe_name(pipe));
  1228. }
  1229. }
  1230. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1231. enum pipe pipe)
  1232. {
  1233. struct drm_device *dev = dev_priv->dev;
  1234. int reg, sprite;
  1235. u32 val;
  1236. if (INTEL_INFO(dev)->gen >= 9) {
  1237. for_each_sprite(dev_priv, pipe, sprite) {
  1238. val = I915_READ(PLANE_CTL(pipe, sprite));
  1239. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1240. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1241. sprite, pipe_name(pipe));
  1242. }
  1243. } else if (IS_VALLEYVIEW(dev)) {
  1244. for_each_sprite(dev_priv, pipe, sprite) {
  1245. reg = SPCNTR(pipe, sprite);
  1246. val = I915_READ(reg);
  1247. I915_STATE_WARN(val & SP_ENABLE,
  1248. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1249. sprite_name(pipe, sprite), pipe_name(pipe));
  1250. }
  1251. } else if (INTEL_INFO(dev)->gen >= 7) {
  1252. reg = SPRCTL(pipe);
  1253. val = I915_READ(reg);
  1254. I915_STATE_WARN(val & SPRITE_ENABLE,
  1255. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1256. plane_name(pipe), pipe_name(pipe));
  1257. } else if (INTEL_INFO(dev)->gen >= 5) {
  1258. reg = DVSCNTR(pipe);
  1259. val = I915_READ(reg);
  1260. I915_STATE_WARN(val & DVS_ENABLE,
  1261. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1262. plane_name(pipe), pipe_name(pipe));
  1263. }
  1264. }
  1265. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1266. {
  1267. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1268. drm_crtc_vblank_put(crtc);
  1269. }
  1270. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1271. {
  1272. u32 val;
  1273. bool enabled;
  1274. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1275. val = I915_READ(PCH_DREF_CONTROL);
  1276. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1277. DREF_SUPERSPREAD_SOURCE_MASK));
  1278. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1279. }
  1280. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1281. enum pipe pipe)
  1282. {
  1283. int reg;
  1284. u32 val;
  1285. bool enabled;
  1286. reg = PCH_TRANSCONF(pipe);
  1287. val = I915_READ(reg);
  1288. enabled = !!(val & TRANS_ENABLE);
  1289. I915_STATE_WARN(enabled,
  1290. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1291. pipe_name(pipe));
  1292. }
  1293. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1294. enum pipe pipe, u32 port_sel, u32 val)
  1295. {
  1296. if ((val & DP_PORT_EN) == 0)
  1297. return false;
  1298. if (HAS_PCH_CPT(dev_priv->dev)) {
  1299. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1300. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1301. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1302. return false;
  1303. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1304. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1305. return false;
  1306. } else {
  1307. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1308. return false;
  1309. }
  1310. return true;
  1311. }
  1312. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1313. enum pipe pipe, u32 val)
  1314. {
  1315. if ((val & SDVO_ENABLE) == 0)
  1316. return false;
  1317. if (HAS_PCH_CPT(dev_priv->dev)) {
  1318. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1319. return false;
  1320. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1321. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1322. return false;
  1323. } else {
  1324. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1325. return false;
  1326. }
  1327. return true;
  1328. }
  1329. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1330. enum pipe pipe, u32 val)
  1331. {
  1332. if ((val & LVDS_PORT_EN) == 0)
  1333. return false;
  1334. if (HAS_PCH_CPT(dev_priv->dev)) {
  1335. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1336. return false;
  1337. } else {
  1338. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1339. return false;
  1340. }
  1341. return true;
  1342. }
  1343. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1344. enum pipe pipe, u32 val)
  1345. {
  1346. if ((val & ADPA_DAC_ENABLE) == 0)
  1347. return false;
  1348. if (HAS_PCH_CPT(dev_priv->dev)) {
  1349. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1350. return false;
  1351. } else {
  1352. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1353. return false;
  1354. }
  1355. return true;
  1356. }
  1357. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1358. enum pipe pipe, int reg, u32 port_sel)
  1359. {
  1360. u32 val = I915_READ(reg);
  1361. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1362. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1363. reg, pipe_name(pipe));
  1364. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1365. && (val & DP_PIPEB_SELECT),
  1366. "IBX PCH dp port still using transcoder B\n");
  1367. }
  1368. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1369. enum pipe pipe, int reg)
  1370. {
  1371. u32 val = I915_READ(reg);
  1372. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1373. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1374. reg, pipe_name(pipe));
  1375. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1376. && (val & SDVO_PIPE_B_SELECT),
  1377. "IBX PCH hdmi port still using transcoder B\n");
  1378. }
  1379. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1380. enum pipe pipe)
  1381. {
  1382. int reg;
  1383. u32 val;
  1384. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1385. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1386. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1387. reg = PCH_ADPA;
  1388. val = I915_READ(reg);
  1389. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1390. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1391. pipe_name(pipe));
  1392. reg = PCH_LVDS;
  1393. val = I915_READ(reg);
  1394. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1395. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1396. pipe_name(pipe));
  1397. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1398. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1399. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1400. }
  1401. static void intel_init_dpio(struct drm_device *dev)
  1402. {
  1403. struct drm_i915_private *dev_priv = dev->dev_private;
  1404. if (!IS_VALLEYVIEW(dev))
  1405. return;
  1406. /*
  1407. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1408. * CHV x1 PHY (DP/HDMI D)
  1409. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1410. */
  1411. if (IS_CHERRYVIEW(dev)) {
  1412. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1413. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1414. } else {
  1415. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1416. }
  1417. }
  1418. static void vlv_enable_pll(struct intel_crtc *crtc,
  1419. const struct intel_crtc_state *pipe_config)
  1420. {
  1421. struct drm_device *dev = crtc->base.dev;
  1422. struct drm_i915_private *dev_priv = dev->dev_private;
  1423. int reg = DPLL(crtc->pipe);
  1424. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1425. assert_pipe_disabled(dev_priv, crtc->pipe);
  1426. /* No really, not for ILK+ */
  1427. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1428. /* PLL is protected by panel, make sure we can write it */
  1429. if (IS_MOBILE(dev_priv->dev))
  1430. assert_panel_unlocked(dev_priv, crtc->pipe);
  1431. I915_WRITE(reg, dpll);
  1432. POSTING_READ(reg);
  1433. udelay(150);
  1434. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1435. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1436. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1437. POSTING_READ(DPLL_MD(crtc->pipe));
  1438. /* We do this three times for luck */
  1439. I915_WRITE(reg, dpll);
  1440. POSTING_READ(reg);
  1441. udelay(150); /* wait for warmup */
  1442. I915_WRITE(reg, dpll);
  1443. POSTING_READ(reg);
  1444. udelay(150); /* wait for warmup */
  1445. I915_WRITE(reg, dpll);
  1446. POSTING_READ(reg);
  1447. udelay(150); /* wait for warmup */
  1448. }
  1449. static void chv_enable_pll(struct intel_crtc *crtc,
  1450. const struct intel_crtc_state *pipe_config)
  1451. {
  1452. struct drm_device *dev = crtc->base.dev;
  1453. struct drm_i915_private *dev_priv = dev->dev_private;
  1454. int pipe = crtc->pipe;
  1455. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1456. u32 tmp;
  1457. assert_pipe_disabled(dev_priv, crtc->pipe);
  1458. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1459. mutex_lock(&dev_priv->sb_lock);
  1460. /* Enable back the 10bit clock to display controller */
  1461. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1462. tmp |= DPIO_DCLKP_EN;
  1463. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1464. mutex_unlock(&dev_priv->sb_lock);
  1465. /*
  1466. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1467. */
  1468. udelay(1);
  1469. /* Enable PLL */
  1470. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1471. /* Check PLL is locked */
  1472. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1473. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1474. /* not sure when this should be written */
  1475. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1476. POSTING_READ(DPLL_MD(pipe));
  1477. }
  1478. static int intel_num_dvo_pipes(struct drm_device *dev)
  1479. {
  1480. struct intel_crtc *crtc;
  1481. int count = 0;
  1482. for_each_intel_crtc(dev, crtc)
  1483. count += crtc->active &&
  1484. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1485. return count;
  1486. }
  1487. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1488. {
  1489. struct drm_device *dev = crtc->base.dev;
  1490. struct drm_i915_private *dev_priv = dev->dev_private;
  1491. int reg = DPLL(crtc->pipe);
  1492. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1493. assert_pipe_disabled(dev_priv, crtc->pipe);
  1494. /* No really, not for ILK+ */
  1495. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1496. /* PLL is protected by panel, make sure we can write it */
  1497. if (IS_MOBILE(dev) && !IS_I830(dev))
  1498. assert_panel_unlocked(dev_priv, crtc->pipe);
  1499. /* Enable DVO 2x clock on both PLLs if necessary */
  1500. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1501. /*
  1502. * It appears to be important that we don't enable this
  1503. * for the current pipe before otherwise configuring the
  1504. * PLL. No idea how this should be handled if multiple
  1505. * DVO outputs are enabled simultaneosly.
  1506. */
  1507. dpll |= DPLL_DVO_2X_MODE;
  1508. I915_WRITE(DPLL(!crtc->pipe),
  1509. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1510. }
  1511. /* Wait for the clocks to stabilize. */
  1512. POSTING_READ(reg);
  1513. udelay(150);
  1514. if (INTEL_INFO(dev)->gen >= 4) {
  1515. I915_WRITE(DPLL_MD(crtc->pipe),
  1516. crtc->config->dpll_hw_state.dpll_md);
  1517. } else {
  1518. /* The pixel multiplier can only be updated once the
  1519. * DPLL is enabled and the clocks are stable.
  1520. *
  1521. * So write it again.
  1522. */
  1523. I915_WRITE(reg, dpll);
  1524. }
  1525. /* We do this three times for luck */
  1526. I915_WRITE(reg, dpll);
  1527. POSTING_READ(reg);
  1528. udelay(150); /* wait for warmup */
  1529. I915_WRITE(reg, dpll);
  1530. POSTING_READ(reg);
  1531. udelay(150); /* wait for warmup */
  1532. I915_WRITE(reg, dpll);
  1533. POSTING_READ(reg);
  1534. udelay(150); /* wait for warmup */
  1535. }
  1536. /**
  1537. * i9xx_disable_pll - disable a PLL
  1538. * @dev_priv: i915 private structure
  1539. * @pipe: pipe PLL to disable
  1540. *
  1541. * Disable the PLL for @pipe, making sure the pipe is off first.
  1542. *
  1543. * Note! This is for pre-ILK only.
  1544. */
  1545. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1546. {
  1547. struct drm_device *dev = crtc->base.dev;
  1548. struct drm_i915_private *dev_priv = dev->dev_private;
  1549. enum pipe pipe = crtc->pipe;
  1550. /* Disable DVO 2x clock on both PLLs if necessary */
  1551. if (IS_I830(dev) &&
  1552. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1553. intel_num_dvo_pipes(dev) == 1) {
  1554. I915_WRITE(DPLL(PIPE_B),
  1555. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1556. I915_WRITE(DPLL(PIPE_A),
  1557. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1558. }
  1559. /* Don't disable pipe or pipe PLLs if needed */
  1560. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1561. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1562. return;
  1563. /* Make sure the pipe isn't still relying on us */
  1564. assert_pipe_disabled(dev_priv, pipe);
  1565. I915_WRITE(DPLL(pipe), 0);
  1566. POSTING_READ(DPLL(pipe));
  1567. }
  1568. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1569. {
  1570. u32 val = 0;
  1571. /* Make sure the pipe isn't still relying on us */
  1572. assert_pipe_disabled(dev_priv, pipe);
  1573. /*
  1574. * Leave integrated clock source and reference clock enabled for pipe B.
  1575. * The latter is needed for VGA hotplug / manual detection.
  1576. */
  1577. if (pipe == PIPE_B)
  1578. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1579. I915_WRITE(DPLL(pipe), val);
  1580. POSTING_READ(DPLL(pipe));
  1581. }
  1582. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1583. {
  1584. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1585. u32 val;
  1586. /* Make sure the pipe isn't still relying on us */
  1587. assert_pipe_disabled(dev_priv, pipe);
  1588. /* Set PLL en = 0 */
  1589. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1590. if (pipe != PIPE_A)
  1591. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1592. I915_WRITE(DPLL(pipe), val);
  1593. POSTING_READ(DPLL(pipe));
  1594. mutex_lock(&dev_priv->sb_lock);
  1595. /* Disable 10bit clock to display controller */
  1596. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1597. val &= ~DPIO_DCLKP_EN;
  1598. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1599. /* disable left/right clock distribution */
  1600. if (pipe != PIPE_B) {
  1601. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1602. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1603. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1604. } else {
  1605. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1606. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1607. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1608. }
  1609. mutex_unlock(&dev_priv->sb_lock);
  1610. }
  1611. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1612. struct intel_digital_port *dport,
  1613. unsigned int expected_mask)
  1614. {
  1615. u32 port_mask;
  1616. int dpll_reg;
  1617. switch (dport->port) {
  1618. case PORT_B:
  1619. port_mask = DPLL_PORTB_READY_MASK;
  1620. dpll_reg = DPLL(0);
  1621. break;
  1622. case PORT_C:
  1623. port_mask = DPLL_PORTC_READY_MASK;
  1624. dpll_reg = DPLL(0);
  1625. expected_mask <<= 4;
  1626. break;
  1627. case PORT_D:
  1628. port_mask = DPLL_PORTD_READY_MASK;
  1629. dpll_reg = DPIO_PHY_STATUS;
  1630. break;
  1631. default:
  1632. BUG();
  1633. }
  1634. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1635. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1636. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1637. }
  1638. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1639. {
  1640. struct drm_device *dev = crtc->base.dev;
  1641. struct drm_i915_private *dev_priv = dev->dev_private;
  1642. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1643. if (WARN_ON(pll == NULL))
  1644. return;
  1645. WARN_ON(!pll->config.crtc_mask);
  1646. if (pll->active == 0) {
  1647. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1648. WARN_ON(pll->on);
  1649. assert_shared_dpll_disabled(dev_priv, pll);
  1650. pll->mode_set(dev_priv, pll);
  1651. }
  1652. }
  1653. /**
  1654. * intel_enable_shared_dpll - enable PCH PLL
  1655. * @dev_priv: i915 private structure
  1656. * @pipe: pipe PLL to enable
  1657. *
  1658. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1659. * drives the transcoder clock.
  1660. */
  1661. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1662. {
  1663. struct drm_device *dev = crtc->base.dev;
  1664. struct drm_i915_private *dev_priv = dev->dev_private;
  1665. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1666. if (WARN_ON(pll == NULL))
  1667. return;
  1668. if (WARN_ON(pll->config.crtc_mask == 0))
  1669. return;
  1670. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1671. pll->name, pll->active, pll->on,
  1672. crtc->base.base.id);
  1673. if (pll->active++) {
  1674. WARN_ON(!pll->on);
  1675. assert_shared_dpll_enabled(dev_priv, pll);
  1676. return;
  1677. }
  1678. WARN_ON(pll->on);
  1679. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1680. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1681. pll->enable(dev_priv, pll);
  1682. pll->on = true;
  1683. }
  1684. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1685. {
  1686. struct drm_device *dev = crtc->base.dev;
  1687. struct drm_i915_private *dev_priv = dev->dev_private;
  1688. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1689. /* PCH only available on ILK+ */
  1690. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1691. if (WARN_ON(pll == NULL))
  1692. return;
  1693. if (WARN_ON(pll->config.crtc_mask == 0))
  1694. return;
  1695. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1696. pll->name, pll->active, pll->on,
  1697. crtc->base.base.id);
  1698. if (WARN_ON(pll->active == 0)) {
  1699. assert_shared_dpll_disabled(dev_priv, pll);
  1700. return;
  1701. }
  1702. assert_shared_dpll_enabled(dev_priv, pll);
  1703. WARN_ON(!pll->on);
  1704. if (--pll->active)
  1705. return;
  1706. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1707. pll->disable(dev_priv, pll);
  1708. pll->on = false;
  1709. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1710. }
  1711. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1712. enum pipe pipe)
  1713. {
  1714. struct drm_device *dev = dev_priv->dev;
  1715. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1716. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1717. uint32_t reg, val, pipeconf_val;
  1718. /* PCH only available on ILK+ */
  1719. BUG_ON(!HAS_PCH_SPLIT(dev));
  1720. /* Make sure PCH DPLL is enabled */
  1721. assert_shared_dpll_enabled(dev_priv,
  1722. intel_crtc_to_shared_dpll(intel_crtc));
  1723. /* FDI must be feeding us bits for PCH ports */
  1724. assert_fdi_tx_enabled(dev_priv, pipe);
  1725. assert_fdi_rx_enabled(dev_priv, pipe);
  1726. if (HAS_PCH_CPT(dev)) {
  1727. /* Workaround: Set the timing override bit before enabling the
  1728. * pch transcoder. */
  1729. reg = TRANS_CHICKEN2(pipe);
  1730. val = I915_READ(reg);
  1731. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1732. I915_WRITE(reg, val);
  1733. }
  1734. reg = PCH_TRANSCONF(pipe);
  1735. val = I915_READ(reg);
  1736. pipeconf_val = I915_READ(PIPECONF(pipe));
  1737. if (HAS_PCH_IBX(dev_priv->dev)) {
  1738. /*
  1739. * make the BPC in transcoder be consistent with
  1740. * that in pipeconf reg.
  1741. */
  1742. val &= ~PIPECONF_BPC_MASK;
  1743. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1744. }
  1745. val &= ~TRANS_INTERLACE_MASK;
  1746. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1747. if (HAS_PCH_IBX(dev_priv->dev) &&
  1748. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1749. val |= TRANS_LEGACY_INTERLACED_ILK;
  1750. else
  1751. val |= TRANS_INTERLACED;
  1752. else
  1753. val |= TRANS_PROGRESSIVE;
  1754. I915_WRITE(reg, val | TRANS_ENABLE);
  1755. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1756. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1757. }
  1758. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1759. enum transcoder cpu_transcoder)
  1760. {
  1761. u32 val, pipeconf_val;
  1762. /* PCH only available on ILK+ */
  1763. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1764. /* FDI must be feeding us bits for PCH ports */
  1765. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1766. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1767. /* Workaround: set timing override bit. */
  1768. val = I915_READ(_TRANSA_CHICKEN2);
  1769. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1770. I915_WRITE(_TRANSA_CHICKEN2, val);
  1771. val = TRANS_ENABLE;
  1772. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1773. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1774. PIPECONF_INTERLACED_ILK)
  1775. val |= TRANS_INTERLACED;
  1776. else
  1777. val |= TRANS_PROGRESSIVE;
  1778. I915_WRITE(LPT_TRANSCONF, val);
  1779. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1780. DRM_ERROR("Failed to enable PCH transcoder\n");
  1781. }
  1782. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1783. enum pipe pipe)
  1784. {
  1785. struct drm_device *dev = dev_priv->dev;
  1786. uint32_t reg, val;
  1787. /* FDI relies on the transcoder */
  1788. assert_fdi_tx_disabled(dev_priv, pipe);
  1789. assert_fdi_rx_disabled(dev_priv, pipe);
  1790. /* Ports must be off as well */
  1791. assert_pch_ports_disabled(dev_priv, pipe);
  1792. reg = PCH_TRANSCONF(pipe);
  1793. val = I915_READ(reg);
  1794. val &= ~TRANS_ENABLE;
  1795. I915_WRITE(reg, val);
  1796. /* wait for PCH transcoder off, transcoder state */
  1797. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1798. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1799. if (!HAS_PCH_IBX(dev)) {
  1800. /* Workaround: Clear the timing override chicken bit again. */
  1801. reg = TRANS_CHICKEN2(pipe);
  1802. val = I915_READ(reg);
  1803. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1804. I915_WRITE(reg, val);
  1805. }
  1806. }
  1807. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1808. {
  1809. u32 val;
  1810. val = I915_READ(LPT_TRANSCONF);
  1811. val &= ~TRANS_ENABLE;
  1812. I915_WRITE(LPT_TRANSCONF, val);
  1813. /* wait for PCH transcoder off, transcoder state */
  1814. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1815. DRM_ERROR("Failed to disable PCH transcoder\n");
  1816. /* Workaround: clear timing override bit. */
  1817. val = I915_READ(_TRANSA_CHICKEN2);
  1818. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1819. I915_WRITE(_TRANSA_CHICKEN2, val);
  1820. }
  1821. /**
  1822. * intel_enable_pipe - enable a pipe, asserting requirements
  1823. * @crtc: crtc responsible for the pipe
  1824. *
  1825. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1826. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1827. */
  1828. static void intel_enable_pipe(struct intel_crtc *crtc)
  1829. {
  1830. struct drm_device *dev = crtc->base.dev;
  1831. struct drm_i915_private *dev_priv = dev->dev_private;
  1832. enum pipe pipe = crtc->pipe;
  1833. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1834. pipe);
  1835. enum pipe pch_transcoder;
  1836. int reg;
  1837. u32 val;
  1838. assert_planes_disabled(dev_priv, pipe);
  1839. assert_cursor_disabled(dev_priv, pipe);
  1840. assert_sprites_disabled(dev_priv, pipe);
  1841. if (HAS_PCH_LPT(dev_priv->dev))
  1842. pch_transcoder = TRANSCODER_A;
  1843. else
  1844. pch_transcoder = pipe;
  1845. /*
  1846. * A pipe without a PLL won't actually be able to drive bits from
  1847. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1848. * need the check.
  1849. */
  1850. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1851. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1852. assert_dsi_pll_enabled(dev_priv);
  1853. else
  1854. assert_pll_enabled(dev_priv, pipe);
  1855. else {
  1856. if (crtc->config->has_pch_encoder) {
  1857. /* if driving the PCH, we need FDI enabled */
  1858. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1859. assert_fdi_tx_pll_enabled(dev_priv,
  1860. (enum pipe) cpu_transcoder);
  1861. }
  1862. /* FIXME: assert CPU port conditions for SNB+ */
  1863. }
  1864. reg = PIPECONF(cpu_transcoder);
  1865. val = I915_READ(reg);
  1866. if (val & PIPECONF_ENABLE) {
  1867. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1868. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1869. return;
  1870. }
  1871. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1872. POSTING_READ(reg);
  1873. }
  1874. /**
  1875. * intel_disable_pipe - disable a pipe, asserting requirements
  1876. * @crtc: crtc whose pipes is to be disabled
  1877. *
  1878. * Disable the pipe of @crtc, making sure that various hardware
  1879. * specific requirements are met, if applicable, e.g. plane
  1880. * disabled, panel fitter off, etc.
  1881. *
  1882. * Will wait until the pipe has shut down before returning.
  1883. */
  1884. static void intel_disable_pipe(struct intel_crtc *crtc)
  1885. {
  1886. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1887. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1888. enum pipe pipe = crtc->pipe;
  1889. int reg;
  1890. u32 val;
  1891. /*
  1892. * Make sure planes won't keep trying to pump pixels to us,
  1893. * or we might hang the display.
  1894. */
  1895. assert_planes_disabled(dev_priv, pipe);
  1896. assert_cursor_disabled(dev_priv, pipe);
  1897. assert_sprites_disabled(dev_priv, pipe);
  1898. reg = PIPECONF(cpu_transcoder);
  1899. val = I915_READ(reg);
  1900. if ((val & PIPECONF_ENABLE) == 0)
  1901. return;
  1902. /*
  1903. * Double wide has implications for planes
  1904. * so best keep it disabled when not needed.
  1905. */
  1906. if (crtc->config->double_wide)
  1907. val &= ~PIPECONF_DOUBLE_WIDE;
  1908. /* Don't disable pipe or pipe PLLs if needed */
  1909. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1910. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1911. val &= ~PIPECONF_ENABLE;
  1912. I915_WRITE(reg, val);
  1913. if ((val & PIPECONF_ENABLE) == 0)
  1914. intel_wait_for_pipe_off(crtc);
  1915. }
  1916. /**
  1917. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1918. * @plane: plane to be enabled
  1919. * @crtc: crtc for the plane
  1920. *
  1921. * Enable @plane on @crtc, making sure that the pipe is running first.
  1922. */
  1923. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1924. struct drm_crtc *crtc)
  1925. {
  1926. struct drm_device *dev = plane->dev;
  1927. struct drm_i915_private *dev_priv = dev->dev_private;
  1928. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1929. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1930. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1931. to_intel_plane_state(plane->state)->visible = true;
  1932. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1933. crtc->x, crtc->y);
  1934. }
  1935. static bool need_vtd_wa(struct drm_device *dev)
  1936. {
  1937. #ifdef CONFIG_INTEL_IOMMU
  1938. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1939. return true;
  1940. #endif
  1941. return false;
  1942. }
  1943. unsigned int
  1944. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1945. uint64_t fb_format_modifier)
  1946. {
  1947. unsigned int tile_height;
  1948. uint32_t pixel_bytes;
  1949. switch (fb_format_modifier) {
  1950. case DRM_FORMAT_MOD_NONE:
  1951. tile_height = 1;
  1952. break;
  1953. case I915_FORMAT_MOD_X_TILED:
  1954. tile_height = IS_GEN2(dev) ? 16 : 8;
  1955. break;
  1956. case I915_FORMAT_MOD_Y_TILED:
  1957. tile_height = 32;
  1958. break;
  1959. case I915_FORMAT_MOD_Yf_TILED:
  1960. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1961. switch (pixel_bytes) {
  1962. default:
  1963. case 1:
  1964. tile_height = 64;
  1965. break;
  1966. case 2:
  1967. case 4:
  1968. tile_height = 32;
  1969. break;
  1970. case 8:
  1971. tile_height = 16;
  1972. break;
  1973. case 16:
  1974. WARN_ONCE(1,
  1975. "128-bit pixels are not supported for display!");
  1976. tile_height = 16;
  1977. break;
  1978. }
  1979. break;
  1980. default:
  1981. MISSING_CASE(fb_format_modifier);
  1982. tile_height = 1;
  1983. break;
  1984. }
  1985. return tile_height;
  1986. }
  1987. unsigned int
  1988. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1989. uint32_t pixel_format, uint64_t fb_format_modifier)
  1990. {
  1991. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1992. fb_format_modifier));
  1993. }
  1994. static int
  1995. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1996. const struct drm_plane_state *plane_state)
  1997. {
  1998. struct intel_rotation_info *info = &view->rotation_info;
  1999. *view = i915_ggtt_view_normal;
  2000. if (!plane_state)
  2001. return 0;
  2002. if (!intel_rotation_90_or_270(plane_state->rotation))
  2003. return 0;
  2004. *view = i915_ggtt_view_rotated;
  2005. info->height = fb->height;
  2006. info->pixel_format = fb->pixel_format;
  2007. info->pitch = fb->pitches[0];
  2008. info->fb_modifier = fb->modifier[0];
  2009. return 0;
  2010. }
  2011. int
  2012. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2013. struct drm_framebuffer *fb,
  2014. const struct drm_plane_state *plane_state,
  2015. struct intel_engine_cs *pipelined)
  2016. {
  2017. struct drm_device *dev = fb->dev;
  2018. struct drm_i915_private *dev_priv = dev->dev_private;
  2019. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2020. struct i915_ggtt_view view;
  2021. u32 alignment;
  2022. int ret;
  2023. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2024. switch (fb->modifier[0]) {
  2025. case DRM_FORMAT_MOD_NONE:
  2026. if (INTEL_INFO(dev)->gen >= 9)
  2027. alignment = 256 * 1024;
  2028. else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  2029. alignment = 128 * 1024;
  2030. else if (INTEL_INFO(dev)->gen >= 4)
  2031. alignment = 4 * 1024;
  2032. else
  2033. alignment = 64 * 1024;
  2034. break;
  2035. case I915_FORMAT_MOD_X_TILED:
  2036. if (INTEL_INFO(dev)->gen >= 9)
  2037. alignment = 256 * 1024;
  2038. else {
  2039. /* pin() will align the object as required by fence */
  2040. alignment = 0;
  2041. }
  2042. break;
  2043. case I915_FORMAT_MOD_Y_TILED:
  2044. case I915_FORMAT_MOD_Yf_TILED:
  2045. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2046. "Y tiling bo slipped through, driver bug!\n"))
  2047. return -EINVAL;
  2048. alignment = 1 * 1024 * 1024;
  2049. break;
  2050. default:
  2051. MISSING_CASE(fb->modifier[0]);
  2052. return -EINVAL;
  2053. }
  2054. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2055. if (ret)
  2056. return ret;
  2057. /* Note that the w/a also requires 64 PTE of padding following the
  2058. * bo. We currently fill all unused PTE with the shadow page and so
  2059. * we should always have valid PTE following the scanout preventing
  2060. * the VT-d warning.
  2061. */
  2062. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2063. alignment = 256 * 1024;
  2064. /*
  2065. * Global gtt pte registers are special registers which actually forward
  2066. * writes to a chunk of system memory. Which means that there is no risk
  2067. * that the register values disappear as soon as we call
  2068. * intel_runtime_pm_put(), so it is correct to wrap only the
  2069. * pin/unpin/fence and not more.
  2070. */
  2071. intel_runtime_pm_get(dev_priv);
  2072. dev_priv->mm.interruptible = false;
  2073. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2074. &view);
  2075. if (ret)
  2076. goto err_interruptible;
  2077. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2078. * fence, whereas 965+ only requires a fence if using
  2079. * framebuffer compression. For simplicity, we always install
  2080. * a fence as the cost is not that onerous.
  2081. */
  2082. ret = i915_gem_object_get_fence(obj);
  2083. if (ret)
  2084. goto err_unpin;
  2085. i915_gem_object_pin_fence(obj);
  2086. dev_priv->mm.interruptible = true;
  2087. intel_runtime_pm_put(dev_priv);
  2088. return 0;
  2089. err_unpin:
  2090. i915_gem_object_unpin_from_display_plane(obj, &view);
  2091. err_interruptible:
  2092. dev_priv->mm.interruptible = true;
  2093. intel_runtime_pm_put(dev_priv);
  2094. return ret;
  2095. }
  2096. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2097. const struct drm_plane_state *plane_state)
  2098. {
  2099. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2100. struct i915_ggtt_view view;
  2101. int ret;
  2102. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2103. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2104. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2105. i915_gem_object_unpin_fence(obj);
  2106. i915_gem_object_unpin_from_display_plane(obj, &view);
  2107. }
  2108. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2109. * is assumed to be a power-of-two. */
  2110. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  2111. unsigned int tiling_mode,
  2112. unsigned int cpp,
  2113. unsigned int pitch)
  2114. {
  2115. if (tiling_mode != I915_TILING_NONE) {
  2116. unsigned int tile_rows, tiles;
  2117. tile_rows = *y / 8;
  2118. *y %= 8;
  2119. tiles = *x / (512/cpp);
  2120. *x %= 512/cpp;
  2121. return tile_rows * pitch * 8 + tiles * 4096;
  2122. } else {
  2123. unsigned int offset;
  2124. offset = *y * pitch + *x * cpp;
  2125. *y = 0;
  2126. *x = (offset & 4095) / cpp;
  2127. return offset & -4096;
  2128. }
  2129. }
  2130. static int i9xx_format_to_fourcc(int format)
  2131. {
  2132. switch (format) {
  2133. case DISPPLANE_8BPP:
  2134. return DRM_FORMAT_C8;
  2135. case DISPPLANE_BGRX555:
  2136. return DRM_FORMAT_XRGB1555;
  2137. case DISPPLANE_BGRX565:
  2138. return DRM_FORMAT_RGB565;
  2139. default:
  2140. case DISPPLANE_BGRX888:
  2141. return DRM_FORMAT_XRGB8888;
  2142. case DISPPLANE_RGBX888:
  2143. return DRM_FORMAT_XBGR8888;
  2144. case DISPPLANE_BGRX101010:
  2145. return DRM_FORMAT_XRGB2101010;
  2146. case DISPPLANE_RGBX101010:
  2147. return DRM_FORMAT_XBGR2101010;
  2148. }
  2149. }
  2150. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2151. {
  2152. switch (format) {
  2153. case PLANE_CTL_FORMAT_RGB_565:
  2154. return DRM_FORMAT_RGB565;
  2155. default:
  2156. case PLANE_CTL_FORMAT_XRGB_8888:
  2157. if (rgb_order) {
  2158. if (alpha)
  2159. return DRM_FORMAT_ABGR8888;
  2160. else
  2161. return DRM_FORMAT_XBGR8888;
  2162. } else {
  2163. if (alpha)
  2164. return DRM_FORMAT_ARGB8888;
  2165. else
  2166. return DRM_FORMAT_XRGB8888;
  2167. }
  2168. case PLANE_CTL_FORMAT_XRGB_2101010:
  2169. if (rgb_order)
  2170. return DRM_FORMAT_XBGR2101010;
  2171. else
  2172. return DRM_FORMAT_XRGB2101010;
  2173. }
  2174. }
  2175. static bool
  2176. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2177. struct intel_initial_plane_config *plane_config)
  2178. {
  2179. struct drm_device *dev = crtc->base.dev;
  2180. struct drm_i915_gem_object *obj = NULL;
  2181. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2182. struct drm_framebuffer *fb = &plane_config->fb->base;
  2183. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2184. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2185. PAGE_SIZE);
  2186. size_aligned -= base_aligned;
  2187. if (plane_config->size == 0)
  2188. return false;
  2189. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2190. base_aligned,
  2191. base_aligned,
  2192. size_aligned);
  2193. if (!obj)
  2194. return false;
  2195. obj->tiling_mode = plane_config->tiling;
  2196. if (obj->tiling_mode == I915_TILING_X)
  2197. obj->stride = fb->pitches[0];
  2198. mode_cmd.pixel_format = fb->pixel_format;
  2199. mode_cmd.width = fb->width;
  2200. mode_cmd.height = fb->height;
  2201. mode_cmd.pitches[0] = fb->pitches[0];
  2202. mode_cmd.modifier[0] = fb->modifier[0];
  2203. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2204. mutex_lock(&dev->struct_mutex);
  2205. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2206. &mode_cmd, obj)) {
  2207. DRM_DEBUG_KMS("intel fb init failed\n");
  2208. goto out_unref_obj;
  2209. }
  2210. mutex_unlock(&dev->struct_mutex);
  2211. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2212. return true;
  2213. out_unref_obj:
  2214. drm_gem_object_unreference(&obj->base);
  2215. mutex_unlock(&dev->struct_mutex);
  2216. return false;
  2217. }
  2218. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2219. static void
  2220. update_state_fb(struct drm_plane *plane)
  2221. {
  2222. if (plane->fb == plane->state->fb)
  2223. return;
  2224. if (plane->state->fb)
  2225. drm_framebuffer_unreference(plane->state->fb);
  2226. plane->state->fb = plane->fb;
  2227. if (plane->state->fb)
  2228. drm_framebuffer_reference(plane->state->fb);
  2229. }
  2230. static void
  2231. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2232. struct intel_initial_plane_config *plane_config)
  2233. {
  2234. struct drm_device *dev = intel_crtc->base.dev;
  2235. struct drm_i915_private *dev_priv = dev->dev_private;
  2236. struct drm_crtc *c;
  2237. struct intel_crtc *i;
  2238. struct drm_i915_gem_object *obj;
  2239. struct drm_plane *primary = intel_crtc->base.primary;
  2240. struct drm_framebuffer *fb;
  2241. if (!plane_config->fb)
  2242. return;
  2243. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2244. fb = &plane_config->fb->base;
  2245. goto valid_fb;
  2246. }
  2247. kfree(plane_config->fb);
  2248. /*
  2249. * Failed to alloc the obj, check to see if we should share
  2250. * an fb with another CRTC instead
  2251. */
  2252. for_each_crtc(dev, c) {
  2253. i = to_intel_crtc(c);
  2254. if (c == &intel_crtc->base)
  2255. continue;
  2256. if (!i->active)
  2257. continue;
  2258. fb = c->primary->fb;
  2259. if (!fb)
  2260. continue;
  2261. obj = intel_fb_obj(fb);
  2262. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2263. drm_framebuffer_reference(fb);
  2264. goto valid_fb;
  2265. }
  2266. }
  2267. return;
  2268. valid_fb:
  2269. obj = intel_fb_obj(fb);
  2270. if (obj->tiling_mode != I915_TILING_NONE)
  2271. dev_priv->preserve_bios_swizzle = true;
  2272. primary->fb = fb;
  2273. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2274. update_state_fb(primary);
  2275. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2276. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2277. }
  2278. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2279. struct drm_framebuffer *fb,
  2280. int x, int y)
  2281. {
  2282. struct drm_device *dev = crtc->dev;
  2283. struct drm_i915_private *dev_priv = dev->dev_private;
  2284. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2285. struct drm_plane *primary = crtc->primary;
  2286. bool visible = to_intel_plane_state(primary->state)->visible;
  2287. struct drm_i915_gem_object *obj;
  2288. int plane = intel_crtc->plane;
  2289. unsigned long linear_offset;
  2290. u32 dspcntr;
  2291. u32 reg = DSPCNTR(plane);
  2292. int pixel_size;
  2293. if (!visible || !fb) {
  2294. I915_WRITE(reg, 0);
  2295. if (INTEL_INFO(dev)->gen >= 4)
  2296. I915_WRITE(DSPSURF(plane), 0);
  2297. else
  2298. I915_WRITE(DSPADDR(plane), 0);
  2299. POSTING_READ(reg);
  2300. return;
  2301. }
  2302. obj = intel_fb_obj(fb);
  2303. if (WARN_ON(obj == NULL))
  2304. return;
  2305. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2306. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2307. dspcntr |= DISPLAY_PLANE_ENABLE;
  2308. if (INTEL_INFO(dev)->gen < 4) {
  2309. if (intel_crtc->pipe == PIPE_B)
  2310. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2311. /* pipesrc and dspsize control the size that is scaled from,
  2312. * which should always be the user's requested size.
  2313. */
  2314. I915_WRITE(DSPSIZE(plane),
  2315. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2316. (intel_crtc->config->pipe_src_w - 1));
  2317. I915_WRITE(DSPPOS(plane), 0);
  2318. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2319. I915_WRITE(PRIMSIZE(plane),
  2320. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2321. (intel_crtc->config->pipe_src_w - 1));
  2322. I915_WRITE(PRIMPOS(plane), 0);
  2323. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2324. }
  2325. switch (fb->pixel_format) {
  2326. case DRM_FORMAT_C8:
  2327. dspcntr |= DISPPLANE_8BPP;
  2328. break;
  2329. case DRM_FORMAT_XRGB1555:
  2330. dspcntr |= DISPPLANE_BGRX555;
  2331. break;
  2332. case DRM_FORMAT_RGB565:
  2333. dspcntr |= DISPPLANE_BGRX565;
  2334. break;
  2335. case DRM_FORMAT_XRGB8888:
  2336. dspcntr |= DISPPLANE_BGRX888;
  2337. break;
  2338. case DRM_FORMAT_XBGR8888:
  2339. dspcntr |= DISPPLANE_RGBX888;
  2340. break;
  2341. case DRM_FORMAT_XRGB2101010:
  2342. dspcntr |= DISPPLANE_BGRX101010;
  2343. break;
  2344. case DRM_FORMAT_XBGR2101010:
  2345. dspcntr |= DISPPLANE_RGBX101010;
  2346. break;
  2347. default:
  2348. BUG();
  2349. }
  2350. if (INTEL_INFO(dev)->gen >= 4 &&
  2351. obj->tiling_mode != I915_TILING_NONE)
  2352. dspcntr |= DISPPLANE_TILED;
  2353. if (IS_G4X(dev))
  2354. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2355. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2356. if (INTEL_INFO(dev)->gen >= 4) {
  2357. intel_crtc->dspaddr_offset =
  2358. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2359. pixel_size,
  2360. fb->pitches[0]);
  2361. linear_offset -= intel_crtc->dspaddr_offset;
  2362. } else {
  2363. intel_crtc->dspaddr_offset = linear_offset;
  2364. }
  2365. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2366. dspcntr |= DISPPLANE_ROTATE_180;
  2367. x += (intel_crtc->config->pipe_src_w - 1);
  2368. y += (intel_crtc->config->pipe_src_h - 1);
  2369. /* Finding the last pixel of the last line of the display
  2370. data and adding to linear_offset*/
  2371. linear_offset +=
  2372. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2373. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2374. }
  2375. I915_WRITE(reg, dspcntr);
  2376. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2377. if (INTEL_INFO(dev)->gen >= 4) {
  2378. I915_WRITE(DSPSURF(plane),
  2379. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2380. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2381. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2382. } else
  2383. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2384. POSTING_READ(reg);
  2385. }
  2386. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2387. struct drm_framebuffer *fb,
  2388. int x, int y)
  2389. {
  2390. struct drm_device *dev = crtc->dev;
  2391. struct drm_i915_private *dev_priv = dev->dev_private;
  2392. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2393. struct drm_plane *primary = crtc->primary;
  2394. bool visible = to_intel_plane_state(primary->state)->visible;
  2395. struct drm_i915_gem_object *obj;
  2396. int plane = intel_crtc->plane;
  2397. unsigned long linear_offset;
  2398. u32 dspcntr;
  2399. u32 reg = DSPCNTR(plane);
  2400. int pixel_size;
  2401. if (!visible || !fb) {
  2402. I915_WRITE(reg, 0);
  2403. I915_WRITE(DSPSURF(plane), 0);
  2404. POSTING_READ(reg);
  2405. return;
  2406. }
  2407. obj = intel_fb_obj(fb);
  2408. if (WARN_ON(obj == NULL))
  2409. return;
  2410. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2411. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2412. dspcntr |= DISPLAY_PLANE_ENABLE;
  2413. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2414. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2415. switch (fb->pixel_format) {
  2416. case DRM_FORMAT_C8:
  2417. dspcntr |= DISPPLANE_8BPP;
  2418. break;
  2419. case DRM_FORMAT_RGB565:
  2420. dspcntr |= DISPPLANE_BGRX565;
  2421. break;
  2422. case DRM_FORMAT_XRGB8888:
  2423. dspcntr |= DISPPLANE_BGRX888;
  2424. break;
  2425. case DRM_FORMAT_XBGR8888:
  2426. dspcntr |= DISPPLANE_RGBX888;
  2427. break;
  2428. case DRM_FORMAT_XRGB2101010:
  2429. dspcntr |= DISPPLANE_BGRX101010;
  2430. break;
  2431. case DRM_FORMAT_XBGR2101010:
  2432. dspcntr |= DISPPLANE_RGBX101010;
  2433. break;
  2434. default:
  2435. BUG();
  2436. }
  2437. if (obj->tiling_mode != I915_TILING_NONE)
  2438. dspcntr |= DISPPLANE_TILED;
  2439. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2440. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2441. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2442. intel_crtc->dspaddr_offset =
  2443. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2444. pixel_size,
  2445. fb->pitches[0]);
  2446. linear_offset -= intel_crtc->dspaddr_offset;
  2447. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2448. dspcntr |= DISPPLANE_ROTATE_180;
  2449. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2450. x += (intel_crtc->config->pipe_src_w - 1);
  2451. y += (intel_crtc->config->pipe_src_h - 1);
  2452. /* Finding the last pixel of the last line of the display
  2453. data and adding to linear_offset*/
  2454. linear_offset +=
  2455. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2456. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2457. }
  2458. }
  2459. I915_WRITE(reg, dspcntr);
  2460. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2461. I915_WRITE(DSPSURF(plane),
  2462. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2463. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2464. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2465. } else {
  2466. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2467. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2468. }
  2469. POSTING_READ(reg);
  2470. }
  2471. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2472. uint32_t pixel_format)
  2473. {
  2474. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2475. /*
  2476. * The stride is either expressed as a multiple of 64 bytes
  2477. * chunks for linear buffers or in number of tiles for tiled
  2478. * buffers.
  2479. */
  2480. switch (fb_modifier) {
  2481. case DRM_FORMAT_MOD_NONE:
  2482. return 64;
  2483. case I915_FORMAT_MOD_X_TILED:
  2484. if (INTEL_INFO(dev)->gen == 2)
  2485. return 128;
  2486. return 512;
  2487. case I915_FORMAT_MOD_Y_TILED:
  2488. /* No need to check for old gens and Y tiling since this is
  2489. * about the display engine and those will be blocked before
  2490. * we get here.
  2491. */
  2492. return 128;
  2493. case I915_FORMAT_MOD_Yf_TILED:
  2494. if (bits_per_pixel == 8)
  2495. return 64;
  2496. else
  2497. return 128;
  2498. default:
  2499. MISSING_CASE(fb_modifier);
  2500. return 64;
  2501. }
  2502. }
  2503. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2504. struct drm_i915_gem_object *obj)
  2505. {
  2506. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2507. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2508. view = &i915_ggtt_view_rotated;
  2509. return i915_gem_obj_ggtt_offset_view(obj, view);
  2510. }
  2511. /*
  2512. * This function detaches (aka. unbinds) unused scalers in hardware
  2513. */
  2514. void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2515. {
  2516. struct drm_device *dev;
  2517. struct drm_i915_private *dev_priv;
  2518. struct intel_crtc_scaler_state *scaler_state;
  2519. int i;
  2520. if (!intel_crtc || !intel_crtc->config)
  2521. return;
  2522. dev = intel_crtc->base.dev;
  2523. dev_priv = dev->dev_private;
  2524. scaler_state = &intel_crtc->config->scaler_state;
  2525. /* loop through and disable scalers that aren't in use */
  2526. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2527. if (!scaler_state->scalers[i].in_use) {
  2528. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
  2529. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
  2530. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
  2531. DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
  2532. intel_crtc->base.base.id, intel_crtc->pipe, i);
  2533. }
  2534. }
  2535. }
  2536. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2537. {
  2538. switch (pixel_format) {
  2539. case DRM_FORMAT_C8:
  2540. return PLANE_CTL_FORMAT_INDEXED;
  2541. case DRM_FORMAT_RGB565:
  2542. return PLANE_CTL_FORMAT_RGB_565;
  2543. case DRM_FORMAT_XBGR8888:
  2544. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2545. case DRM_FORMAT_XRGB8888:
  2546. return PLANE_CTL_FORMAT_XRGB_8888;
  2547. /*
  2548. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2549. * to be already pre-multiplied. We need to add a knob (or a different
  2550. * DRM_FORMAT) for user-space to configure that.
  2551. */
  2552. case DRM_FORMAT_ABGR8888:
  2553. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2554. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2555. case DRM_FORMAT_ARGB8888:
  2556. return PLANE_CTL_FORMAT_XRGB_8888 |
  2557. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2558. case DRM_FORMAT_XRGB2101010:
  2559. return PLANE_CTL_FORMAT_XRGB_2101010;
  2560. case DRM_FORMAT_XBGR2101010:
  2561. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2562. case DRM_FORMAT_YUYV:
  2563. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2564. case DRM_FORMAT_YVYU:
  2565. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2566. case DRM_FORMAT_UYVY:
  2567. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2568. case DRM_FORMAT_VYUY:
  2569. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2570. default:
  2571. MISSING_CASE(pixel_format);
  2572. }
  2573. return 0;
  2574. }
  2575. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2576. {
  2577. switch (fb_modifier) {
  2578. case DRM_FORMAT_MOD_NONE:
  2579. break;
  2580. case I915_FORMAT_MOD_X_TILED:
  2581. return PLANE_CTL_TILED_X;
  2582. case I915_FORMAT_MOD_Y_TILED:
  2583. return PLANE_CTL_TILED_Y;
  2584. case I915_FORMAT_MOD_Yf_TILED:
  2585. return PLANE_CTL_TILED_YF;
  2586. default:
  2587. MISSING_CASE(fb_modifier);
  2588. }
  2589. return 0;
  2590. }
  2591. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2592. {
  2593. switch (rotation) {
  2594. case BIT(DRM_ROTATE_0):
  2595. break;
  2596. /*
  2597. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2598. * while i915 HW rotation is clockwise, thats why this swapping.
  2599. */
  2600. case BIT(DRM_ROTATE_90):
  2601. return PLANE_CTL_ROTATE_270;
  2602. case BIT(DRM_ROTATE_180):
  2603. return PLANE_CTL_ROTATE_180;
  2604. case BIT(DRM_ROTATE_270):
  2605. return PLANE_CTL_ROTATE_90;
  2606. default:
  2607. MISSING_CASE(rotation);
  2608. }
  2609. return 0;
  2610. }
  2611. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2612. struct drm_framebuffer *fb,
  2613. int x, int y)
  2614. {
  2615. struct drm_device *dev = crtc->dev;
  2616. struct drm_i915_private *dev_priv = dev->dev_private;
  2617. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2618. struct drm_plane *plane = crtc->primary;
  2619. bool visible = to_intel_plane_state(plane->state)->visible;
  2620. struct drm_i915_gem_object *obj;
  2621. int pipe = intel_crtc->pipe;
  2622. u32 plane_ctl, stride_div, stride;
  2623. u32 tile_height, plane_offset, plane_size;
  2624. unsigned int rotation;
  2625. int x_offset, y_offset;
  2626. unsigned long surf_addr;
  2627. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2628. struct intel_plane_state *plane_state;
  2629. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2630. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2631. int scaler_id = -1;
  2632. plane_state = to_intel_plane_state(plane->state);
  2633. if (!visible || !fb) {
  2634. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2635. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2636. POSTING_READ(PLANE_CTL(pipe, 0));
  2637. return;
  2638. }
  2639. plane_ctl = PLANE_CTL_ENABLE |
  2640. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2641. PLANE_CTL_PIPE_CSC_ENABLE;
  2642. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2643. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2644. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2645. rotation = plane->state->rotation;
  2646. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2647. obj = intel_fb_obj(fb);
  2648. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2649. fb->pixel_format);
  2650. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2651. /*
  2652. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2653. * update_plane helpers are called from legacy paths.
  2654. * Once full atomic crtc is available, below check can be avoided.
  2655. */
  2656. if (drm_rect_width(&plane_state->src)) {
  2657. scaler_id = plane_state->scaler_id;
  2658. src_x = plane_state->src.x1 >> 16;
  2659. src_y = plane_state->src.y1 >> 16;
  2660. src_w = drm_rect_width(&plane_state->src) >> 16;
  2661. src_h = drm_rect_height(&plane_state->src) >> 16;
  2662. dst_x = plane_state->dst.x1;
  2663. dst_y = plane_state->dst.y1;
  2664. dst_w = drm_rect_width(&plane_state->dst);
  2665. dst_h = drm_rect_height(&plane_state->dst);
  2666. WARN_ON(x != src_x || y != src_y);
  2667. } else {
  2668. src_w = intel_crtc->config->pipe_src_w;
  2669. src_h = intel_crtc->config->pipe_src_h;
  2670. }
  2671. if (intel_rotation_90_or_270(rotation)) {
  2672. /* stride = Surface height in tiles */
  2673. tile_height = intel_tile_height(dev, fb->pixel_format,
  2674. fb->modifier[0]);
  2675. stride = DIV_ROUND_UP(fb->height, tile_height);
  2676. x_offset = stride * tile_height - y - src_h;
  2677. y_offset = x;
  2678. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2679. } else {
  2680. stride = fb->pitches[0] / stride_div;
  2681. x_offset = x;
  2682. y_offset = y;
  2683. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2684. }
  2685. plane_offset = y_offset << 16 | x_offset;
  2686. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2687. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2688. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2689. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2690. if (scaler_id >= 0) {
  2691. uint32_t ps_ctrl = 0;
  2692. WARN_ON(!dst_w || !dst_h);
  2693. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2694. crtc_state->scaler_state.scalers[scaler_id].mode;
  2695. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2696. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2697. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2698. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2699. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2700. } else {
  2701. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2702. }
  2703. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2704. POSTING_READ(PLANE_SURF(pipe, 0));
  2705. }
  2706. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2707. static int
  2708. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2709. int x, int y, enum mode_set_atomic state)
  2710. {
  2711. struct drm_device *dev = crtc->dev;
  2712. struct drm_i915_private *dev_priv = dev->dev_private;
  2713. if (dev_priv->display.disable_fbc)
  2714. dev_priv->display.disable_fbc(dev);
  2715. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2716. return 0;
  2717. }
  2718. static void intel_complete_page_flips(struct drm_device *dev)
  2719. {
  2720. struct drm_crtc *crtc;
  2721. for_each_crtc(dev, crtc) {
  2722. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2723. enum plane plane = intel_crtc->plane;
  2724. intel_prepare_page_flip(dev, plane);
  2725. intel_finish_page_flip_plane(dev, plane);
  2726. }
  2727. }
  2728. static void intel_update_primary_planes(struct drm_device *dev)
  2729. {
  2730. struct drm_i915_private *dev_priv = dev->dev_private;
  2731. struct drm_crtc *crtc;
  2732. for_each_crtc(dev, crtc) {
  2733. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2734. drm_modeset_lock(&crtc->mutex, NULL);
  2735. /*
  2736. * FIXME: Once we have proper support for primary planes (and
  2737. * disabling them without disabling the entire crtc) allow again
  2738. * a NULL crtc->primary->fb.
  2739. */
  2740. if (intel_crtc->active && crtc->primary->fb)
  2741. dev_priv->display.update_primary_plane(crtc,
  2742. crtc->primary->fb,
  2743. crtc->x,
  2744. crtc->y);
  2745. drm_modeset_unlock(&crtc->mutex);
  2746. }
  2747. }
  2748. void intel_prepare_reset(struct drm_device *dev)
  2749. {
  2750. /* no reset support for gen2 */
  2751. if (IS_GEN2(dev))
  2752. return;
  2753. /* reset doesn't touch the display */
  2754. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2755. return;
  2756. drm_modeset_lock_all(dev);
  2757. /*
  2758. * Disabling the crtcs gracefully seems nicer. Also the
  2759. * g33 docs say we should at least disable all the planes.
  2760. */
  2761. intel_display_suspend(dev);
  2762. }
  2763. void intel_finish_reset(struct drm_device *dev)
  2764. {
  2765. struct drm_i915_private *dev_priv = to_i915(dev);
  2766. /*
  2767. * Flips in the rings will be nuked by the reset,
  2768. * so complete all pending flips so that user space
  2769. * will get its events and not get stuck.
  2770. */
  2771. intel_complete_page_flips(dev);
  2772. /* no reset support for gen2 */
  2773. if (IS_GEN2(dev))
  2774. return;
  2775. /* reset doesn't touch the display */
  2776. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2777. /*
  2778. * Flips in the rings have been nuked by the reset,
  2779. * so update the base address of all primary
  2780. * planes to the the last fb to make sure we're
  2781. * showing the correct fb after a reset.
  2782. */
  2783. intel_update_primary_planes(dev);
  2784. return;
  2785. }
  2786. /*
  2787. * The display has been reset as well,
  2788. * so need a full re-initialization.
  2789. */
  2790. intel_runtime_pm_disable_interrupts(dev_priv);
  2791. intel_runtime_pm_enable_interrupts(dev_priv);
  2792. intel_modeset_init_hw(dev);
  2793. spin_lock_irq(&dev_priv->irq_lock);
  2794. if (dev_priv->display.hpd_irq_setup)
  2795. dev_priv->display.hpd_irq_setup(dev);
  2796. spin_unlock_irq(&dev_priv->irq_lock);
  2797. intel_modeset_setup_hw_state(dev, true);
  2798. intel_hpd_init(dev_priv);
  2799. drm_modeset_unlock_all(dev);
  2800. }
  2801. static void
  2802. intel_finish_fb(struct drm_framebuffer *old_fb)
  2803. {
  2804. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2805. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2806. bool was_interruptible = dev_priv->mm.interruptible;
  2807. int ret;
  2808. /* Big Hammer, we also need to ensure that any pending
  2809. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2810. * current scanout is retired before unpinning the old
  2811. * framebuffer. Note that we rely on userspace rendering
  2812. * into the buffer attached to the pipe they are waiting
  2813. * on. If not, userspace generates a GPU hang with IPEHR
  2814. * point to the MI_WAIT_FOR_EVENT.
  2815. *
  2816. * This should only fail upon a hung GPU, in which case we
  2817. * can safely continue.
  2818. */
  2819. dev_priv->mm.interruptible = false;
  2820. ret = i915_gem_object_wait_rendering(obj, true);
  2821. dev_priv->mm.interruptible = was_interruptible;
  2822. WARN_ON(ret);
  2823. }
  2824. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2825. {
  2826. struct drm_device *dev = crtc->dev;
  2827. struct drm_i915_private *dev_priv = dev->dev_private;
  2828. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2829. bool pending;
  2830. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2831. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2832. return false;
  2833. spin_lock_irq(&dev->event_lock);
  2834. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2835. spin_unlock_irq(&dev->event_lock);
  2836. return pending;
  2837. }
  2838. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2839. {
  2840. struct drm_device *dev = crtc->base.dev;
  2841. struct drm_i915_private *dev_priv = dev->dev_private;
  2842. const struct drm_display_mode *adjusted_mode;
  2843. if (!i915.fastboot)
  2844. return;
  2845. /*
  2846. * Update pipe size and adjust fitter if needed: the reason for this is
  2847. * that in compute_mode_changes we check the native mode (not the pfit
  2848. * mode) to see if we can flip rather than do a full mode set. In the
  2849. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2850. * pfit state, we'll end up with a big fb scanned out into the wrong
  2851. * sized surface.
  2852. *
  2853. * To fix this properly, we need to hoist the checks up into
  2854. * compute_mode_changes (or above), check the actual pfit state and
  2855. * whether the platform allows pfit disable with pipe active, and only
  2856. * then update the pipesrc and pfit state, even on the flip path.
  2857. */
  2858. adjusted_mode = &crtc->config->base.adjusted_mode;
  2859. I915_WRITE(PIPESRC(crtc->pipe),
  2860. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2861. (adjusted_mode->crtc_vdisplay - 1));
  2862. if (!crtc->config->pch_pfit.enabled &&
  2863. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2864. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2865. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2866. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2867. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2868. }
  2869. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2870. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2871. }
  2872. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2873. {
  2874. struct drm_device *dev = crtc->dev;
  2875. struct drm_i915_private *dev_priv = dev->dev_private;
  2876. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2877. int pipe = intel_crtc->pipe;
  2878. u32 reg, temp;
  2879. /* enable normal train */
  2880. reg = FDI_TX_CTL(pipe);
  2881. temp = I915_READ(reg);
  2882. if (IS_IVYBRIDGE(dev)) {
  2883. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2884. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2885. } else {
  2886. temp &= ~FDI_LINK_TRAIN_NONE;
  2887. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2888. }
  2889. I915_WRITE(reg, temp);
  2890. reg = FDI_RX_CTL(pipe);
  2891. temp = I915_READ(reg);
  2892. if (HAS_PCH_CPT(dev)) {
  2893. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2894. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2895. } else {
  2896. temp &= ~FDI_LINK_TRAIN_NONE;
  2897. temp |= FDI_LINK_TRAIN_NONE;
  2898. }
  2899. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2900. /* wait one idle pattern time */
  2901. POSTING_READ(reg);
  2902. udelay(1000);
  2903. /* IVB wants error correction enabled */
  2904. if (IS_IVYBRIDGE(dev))
  2905. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2906. FDI_FE_ERRC_ENABLE);
  2907. }
  2908. /* The FDI link training functions for ILK/Ibexpeak. */
  2909. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2910. {
  2911. struct drm_device *dev = crtc->dev;
  2912. struct drm_i915_private *dev_priv = dev->dev_private;
  2913. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2914. int pipe = intel_crtc->pipe;
  2915. u32 reg, temp, tries;
  2916. /* FDI needs bits from pipe first */
  2917. assert_pipe_enabled(dev_priv, pipe);
  2918. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2919. for train result */
  2920. reg = FDI_RX_IMR(pipe);
  2921. temp = I915_READ(reg);
  2922. temp &= ~FDI_RX_SYMBOL_LOCK;
  2923. temp &= ~FDI_RX_BIT_LOCK;
  2924. I915_WRITE(reg, temp);
  2925. I915_READ(reg);
  2926. udelay(150);
  2927. /* enable CPU FDI TX and PCH FDI RX */
  2928. reg = FDI_TX_CTL(pipe);
  2929. temp = I915_READ(reg);
  2930. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2931. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2932. temp &= ~FDI_LINK_TRAIN_NONE;
  2933. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2934. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2935. reg = FDI_RX_CTL(pipe);
  2936. temp = I915_READ(reg);
  2937. temp &= ~FDI_LINK_TRAIN_NONE;
  2938. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2939. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2940. POSTING_READ(reg);
  2941. udelay(150);
  2942. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2943. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2944. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2945. FDI_RX_PHASE_SYNC_POINTER_EN);
  2946. reg = FDI_RX_IIR(pipe);
  2947. for (tries = 0; tries < 5; tries++) {
  2948. temp = I915_READ(reg);
  2949. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2950. if ((temp & FDI_RX_BIT_LOCK)) {
  2951. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2952. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2953. break;
  2954. }
  2955. }
  2956. if (tries == 5)
  2957. DRM_ERROR("FDI train 1 fail!\n");
  2958. /* Train 2 */
  2959. reg = FDI_TX_CTL(pipe);
  2960. temp = I915_READ(reg);
  2961. temp &= ~FDI_LINK_TRAIN_NONE;
  2962. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2963. I915_WRITE(reg, temp);
  2964. reg = FDI_RX_CTL(pipe);
  2965. temp = I915_READ(reg);
  2966. temp &= ~FDI_LINK_TRAIN_NONE;
  2967. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2968. I915_WRITE(reg, temp);
  2969. POSTING_READ(reg);
  2970. udelay(150);
  2971. reg = FDI_RX_IIR(pipe);
  2972. for (tries = 0; tries < 5; tries++) {
  2973. temp = I915_READ(reg);
  2974. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2975. if (temp & FDI_RX_SYMBOL_LOCK) {
  2976. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2977. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2978. break;
  2979. }
  2980. }
  2981. if (tries == 5)
  2982. DRM_ERROR("FDI train 2 fail!\n");
  2983. DRM_DEBUG_KMS("FDI train done\n");
  2984. }
  2985. static const int snb_b_fdi_train_param[] = {
  2986. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2987. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2988. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2989. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2990. };
  2991. /* The FDI link training functions for SNB/Cougarpoint. */
  2992. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2993. {
  2994. struct drm_device *dev = crtc->dev;
  2995. struct drm_i915_private *dev_priv = dev->dev_private;
  2996. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2997. int pipe = intel_crtc->pipe;
  2998. u32 reg, temp, i, retry;
  2999. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3000. for train result */
  3001. reg = FDI_RX_IMR(pipe);
  3002. temp = I915_READ(reg);
  3003. temp &= ~FDI_RX_SYMBOL_LOCK;
  3004. temp &= ~FDI_RX_BIT_LOCK;
  3005. I915_WRITE(reg, temp);
  3006. POSTING_READ(reg);
  3007. udelay(150);
  3008. /* enable CPU FDI TX and PCH FDI RX */
  3009. reg = FDI_TX_CTL(pipe);
  3010. temp = I915_READ(reg);
  3011. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3012. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3013. temp &= ~FDI_LINK_TRAIN_NONE;
  3014. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3015. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3016. /* SNB-B */
  3017. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3018. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3019. I915_WRITE(FDI_RX_MISC(pipe),
  3020. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3021. reg = FDI_RX_CTL(pipe);
  3022. temp = I915_READ(reg);
  3023. if (HAS_PCH_CPT(dev)) {
  3024. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3025. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3026. } else {
  3027. temp &= ~FDI_LINK_TRAIN_NONE;
  3028. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3029. }
  3030. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3031. POSTING_READ(reg);
  3032. udelay(150);
  3033. for (i = 0; i < 4; i++) {
  3034. reg = FDI_TX_CTL(pipe);
  3035. temp = I915_READ(reg);
  3036. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3037. temp |= snb_b_fdi_train_param[i];
  3038. I915_WRITE(reg, temp);
  3039. POSTING_READ(reg);
  3040. udelay(500);
  3041. for (retry = 0; retry < 5; retry++) {
  3042. reg = FDI_RX_IIR(pipe);
  3043. temp = I915_READ(reg);
  3044. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3045. if (temp & FDI_RX_BIT_LOCK) {
  3046. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3047. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3048. break;
  3049. }
  3050. udelay(50);
  3051. }
  3052. if (retry < 5)
  3053. break;
  3054. }
  3055. if (i == 4)
  3056. DRM_ERROR("FDI train 1 fail!\n");
  3057. /* Train 2 */
  3058. reg = FDI_TX_CTL(pipe);
  3059. temp = I915_READ(reg);
  3060. temp &= ~FDI_LINK_TRAIN_NONE;
  3061. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3062. if (IS_GEN6(dev)) {
  3063. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3064. /* SNB-B */
  3065. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3066. }
  3067. I915_WRITE(reg, temp);
  3068. reg = FDI_RX_CTL(pipe);
  3069. temp = I915_READ(reg);
  3070. if (HAS_PCH_CPT(dev)) {
  3071. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3072. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3073. } else {
  3074. temp &= ~FDI_LINK_TRAIN_NONE;
  3075. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3076. }
  3077. I915_WRITE(reg, temp);
  3078. POSTING_READ(reg);
  3079. udelay(150);
  3080. for (i = 0; i < 4; i++) {
  3081. reg = FDI_TX_CTL(pipe);
  3082. temp = I915_READ(reg);
  3083. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3084. temp |= snb_b_fdi_train_param[i];
  3085. I915_WRITE(reg, temp);
  3086. POSTING_READ(reg);
  3087. udelay(500);
  3088. for (retry = 0; retry < 5; retry++) {
  3089. reg = FDI_RX_IIR(pipe);
  3090. temp = I915_READ(reg);
  3091. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3092. if (temp & FDI_RX_SYMBOL_LOCK) {
  3093. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3094. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3095. break;
  3096. }
  3097. udelay(50);
  3098. }
  3099. if (retry < 5)
  3100. break;
  3101. }
  3102. if (i == 4)
  3103. DRM_ERROR("FDI train 2 fail!\n");
  3104. DRM_DEBUG_KMS("FDI train done.\n");
  3105. }
  3106. /* Manual link training for Ivy Bridge A0 parts */
  3107. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3108. {
  3109. struct drm_device *dev = crtc->dev;
  3110. struct drm_i915_private *dev_priv = dev->dev_private;
  3111. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3112. int pipe = intel_crtc->pipe;
  3113. u32 reg, temp, i, j;
  3114. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3115. for train result */
  3116. reg = FDI_RX_IMR(pipe);
  3117. temp = I915_READ(reg);
  3118. temp &= ~FDI_RX_SYMBOL_LOCK;
  3119. temp &= ~FDI_RX_BIT_LOCK;
  3120. I915_WRITE(reg, temp);
  3121. POSTING_READ(reg);
  3122. udelay(150);
  3123. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3124. I915_READ(FDI_RX_IIR(pipe)));
  3125. /* Try each vswing and preemphasis setting twice before moving on */
  3126. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3127. /* disable first in case we need to retry */
  3128. reg = FDI_TX_CTL(pipe);
  3129. temp = I915_READ(reg);
  3130. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3131. temp &= ~FDI_TX_ENABLE;
  3132. I915_WRITE(reg, temp);
  3133. reg = FDI_RX_CTL(pipe);
  3134. temp = I915_READ(reg);
  3135. temp &= ~FDI_LINK_TRAIN_AUTO;
  3136. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3137. temp &= ~FDI_RX_ENABLE;
  3138. I915_WRITE(reg, temp);
  3139. /* enable CPU FDI TX and PCH FDI RX */
  3140. reg = FDI_TX_CTL(pipe);
  3141. temp = I915_READ(reg);
  3142. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3143. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3144. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3145. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3146. temp |= snb_b_fdi_train_param[j/2];
  3147. temp |= FDI_COMPOSITE_SYNC;
  3148. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3149. I915_WRITE(FDI_RX_MISC(pipe),
  3150. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3151. reg = FDI_RX_CTL(pipe);
  3152. temp = I915_READ(reg);
  3153. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3154. temp |= FDI_COMPOSITE_SYNC;
  3155. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3156. POSTING_READ(reg);
  3157. udelay(1); /* should be 0.5us */
  3158. for (i = 0; i < 4; i++) {
  3159. reg = FDI_RX_IIR(pipe);
  3160. temp = I915_READ(reg);
  3161. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3162. if (temp & FDI_RX_BIT_LOCK ||
  3163. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3164. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3165. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3166. i);
  3167. break;
  3168. }
  3169. udelay(1); /* should be 0.5us */
  3170. }
  3171. if (i == 4) {
  3172. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3173. continue;
  3174. }
  3175. /* Train 2 */
  3176. reg = FDI_TX_CTL(pipe);
  3177. temp = I915_READ(reg);
  3178. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3179. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3180. I915_WRITE(reg, temp);
  3181. reg = FDI_RX_CTL(pipe);
  3182. temp = I915_READ(reg);
  3183. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3184. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3185. I915_WRITE(reg, temp);
  3186. POSTING_READ(reg);
  3187. udelay(2); /* should be 1.5us */
  3188. for (i = 0; i < 4; i++) {
  3189. reg = FDI_RX_IIR(pipe);
  3190. temp = I915_READ(reg);
  3191. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3192. if (temp & FDI_RX_SYMBOL_LOCK ||
  3193. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3194. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3195. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3196. i);
  3197. goto train_done;
  3198. }
  3199. udelay(2); /* should be 1.5us */
  3200. }
  3201. if (i == 4)
  3202. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3203. }
  3204. train_done:
  3205. DRM_DEBUG_KMS("FDI train done.\n");
  3206. }
  3207. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3208. {
  3209. struct drm_device *dev = intel_crtc->base.dev;
  3210. struct drm_i915_private *dev_priv = dev->dev_private;
  3211. int pipe = intel_crtc->pipe;
  3212. u32 reg, temp;
  3213. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3214. reg = FDI_RX_CTL(pipe);
  3215. temp = I915_READ(reg);
  3216. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3217. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3218. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3219. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3220. POSTING_READ(reg);
  3221. udelay(200);
  3222. /* Switch from Rawclk to PCDclk */
  3223. temp = I915_READ(reg);
  3224. I915_WRITE(reg, temp | FDI_PCDCLK);
  3225. POSTING_READ(reg);
  3226. udelay(200);
  3227. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3228. reg = FDI_TX_CTL(pipe);
  3229. temp = I915_READ(reg);
  3230. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3231. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3232. POSTING_READ(reg);
  3233. udelay(100);
  3234. }
  3235. }
  3236. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3237. {
  3238. struct drm_device *dev = intel_crtc->base.dev;
  3239. struct drm_i915_private *dev_priv = dev->dev_private;
  3240. int pipe = intel_crtc->pipe;
  3241. u32 reg, temp;
  3242. /* Switch from PCDclk to Rawclk */
  3243. reg = FDI_RX_CTL(pipe);
  3244. temp = I915_READ(reg);
  3245. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3246. /* Disable CPU FDI TX PLL */
  3247. reg = FDI_TX_CTL(pipe);
  3248. temp = I915_READ(reg);
  3249. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3250. POSTING_READ(reg);
  3251. udelay(100);
  3252. reg = FDI_RX_CTL(pipe);
  3253. temp = I915_READ(reg);
  3254. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3255. /* Wait for the clocks to turn off. */
  3256. POSTING_READ(reg);
  3257. udelay(100);
  3258. }
  3259. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3260. {
  3261. struct drm_device *dev = crtc->dev;
  3262. struct drm_i915_private *dev_priv = dev->dev_private;
  3263. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3264. int pipe = intel_crtc->pipe;
  3265. u32 reg, temp;
  3266. /* disable CPU FDI tx and PCH FDI rx */
  3267. reg = FDI_TX_CTL(pipe);
  3268. temp = I915_READ(reg);
  3269. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3270. POSTING_READ(reg);
  3271. reg = FDI_RX_CTL(pipe);
  3272. temp = I915_READ(reg);
  3273. temp &= ~(0x7 << 16);
  3274. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3275. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3276. POSTING_READ(reg);
  3277. udelay(100);
  3278. /* Ironlake workaround, disable clock pointer after downing FDI */
  3279. if (HAS_PCH_IBX(dev))
  3280. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3281. /* still set train pattern 1 */
  3282. reg = FDI_TX_CTL(pipe);
  3283. temp = I915_READ(reg);
  3284. temp &= ~FDI_LINK_TRAIN_NONE;
  3285. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3286. I915_WRITE(reg, temp);
  3287. reg = FDI_RX_CTL(pipe);
  3288. temp = I915_READ(reg);
  3289. if (HAS_PCH_CPT(dev)) {
  3290. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3291. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3292. } else {
  3293. temp &= ~FDI_LINK_TRAIN_NONE;
  3294. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3295. }
  3296. /* BPC in FDI rx is consistent with that in PIPECONF */
  3297. temp &= ~(0x07 << 16);
  3298. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3299. I915_WRITE(reg, temp);
  3300. POSTING_READ(reg);
  3301. udelay(100);
  3302. }
  3303. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3304. {
  3305. struct intel_crtc *crtc;
  3306. /* Note that we don't need to be called with mode_config.lock here
  3307. * as our list of CRTC objects is static for the lifetime of the
  3308. * device and so cannot disappear as we iterate. Similarly, we can
  3309. * happily treat the predicates as racy, atomic checks as userspace
  3310. * cannot claim and pin a new fb without at least acquring the
  3311. * struct_mutex and so serialising with us.
  3312. */
  3313. for_each_intel_crtc(dev, crtc) {
  3314. if (atomic_read(&crtc->unpin_work_count) == 0)
  3315. continue;
  3316. if (crtc->unpin_work)
  3317. intel_wait_for_vblank(dev, crtc->pipe);
  3318. return true;
  3319. }
  3320. return false;
  3321. }
  3322. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3323. {
  3324. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3325. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3326. /* ensure that the unpin work is consistent wrt ->pending. */
  3327. smp_rmb();
  3328. intel_crtc->unpin_work = NULL;
  3329. if (work->event)
  3330. drm_send_vblank_event(intel_crtc->base.dev,
  3331. intel_crtc->pipe,
  3332. work->event);
  3333. drm_crtc_vblank_put(&intel_crtc->base);
  3334. wake_up_all(&dev_priv->pending_flip_queue);
  3335. queue_work(dev_priv->wq, &work->work);
  3336. trace_i915_flip_complete(intel_crtc->plane,
  3337. work->pending_flip_obj);
  3338. }
  3339. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3340. {
  3341. struct drm_device *dev = crtc->dev;
  3342. struct drm_i915_private *dev_priv = dev->dev_private;
  3343. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3344. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3345. !intel_crtc_has_pending_flip(crtc),
  3346. 60*HZ) == 0)) {
  3347. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3348. spin_lock_irq(&dev->event_lock);
  3349. if (intel_crtc->unpin_work) {
  3350. WARN_ONCE(1, "Removing stuck page flip\n");
  3351. page_flip_completed(intel_crtc);
  3352. }
  3353. spin_unlock_irq(&dev->event_lock);
  3354. }
  3355. if (crtc->primary->fb) {
  3356. mutex_lock(&dev->struct_mutex);
  3357. intel_finish_fb(crtc->primary->fb);
  3358. mutex_unlock(&dev->struct_mutex);
  3359. }
  3360. }
  3361. /* Program iCLKIP clock to the desired frequency */
  3362. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3363. {
  3364. struct drm_device *dev = crtc->dev;
  3365. struct drm_i915_private *dev_priv = dev->dev_private;
  3366. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3367. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3368. u32 temp;
  3369. mutex_lock(&dev_priv->sb_lock);
  3370. /* It is necessary to ungate the pixclk gate prior to programming
  3371. * the divisors, and gate it back when it is done.
  3372. */
  3373. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3374. /* Disable SSCCTL */
  3375. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3376. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3377. SBI_SSCCTL_DISABLE,
  3378. SBI_ICLK);
  3379. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3380. if (clock == 20000) {
  3381. auxdiv = 1;
  3382. divsel = 0x41;
  3383. phaseinc = 0x20;
  3384. } else {
  3385. /* The iCLK virtual clock root frequency is in MHz,
  3386. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3387. * divisors, it is necessary to divide one by another, so we
  3388. * convert the virtual clock precision to KHz here for higher
  3389. * precision.
  3390. */
  3391. u32 iclk_virtual_root_freq = 172800 * 1000;
  3392. u32 iclk_pi_range = 64;
  3393. u32 desired_divisor, msb_divisor_value, pi_value;
  3394. desired_divisor = (iclk_virtual_root_freq / clock);
  3395. msb_divisor_value = desired_divisor / iclk_pi_range;
  3396. pi_value = desired_divisor % iclk_pi_range;
  3397. auxdiv = 0;
  3398. divsel = msb_divisor_value - 2;
  3399. phaseinc = pi_value;
  3400. }
  3401. /* This should not happen with any sane values */
  3402. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3403. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3404. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3405. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3406. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3407. clock,
  3408. auxdiv,
  3409. divsel,
  3410. phasedir,
  3411. phaseinc);
  3412. /* Program SSCDIVINTPHASE6 */
  3413. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3414. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3415. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3416. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3417. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3418. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3419. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3420. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3421. /* Program SSCAUXDIV */
  3422. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3423. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3424. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3425. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3426. /* Enable modulator and associated divider */
  3427. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3428. temp &= ~SBI_SSCCTL_DISABLE;
  3429. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3430. /* Wait for initialization time */
  3431. udelay(24);
  3432. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3433. mutex_unlock(&dev_priv->sb_lock);
  3434. }
  3435. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3436. enum pipe pch_transcoder)
  3437. {
  3438. struct drm_device *dev = crtc->base.dev;
  3439. struct drm_i915_private *dev_priv = dev->dev_private;
  3440. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3441. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3442. I915_READ(HTOTAL(cpu_transcoder)));
  3443. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3444. I915_READ(HBLANK(cpu_transcoder)));
  3445. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3446. I915_READ(HSYNC(cpu_transcoder)));
  3447. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3448. I915_READ(VTOTAL(cpu_transcoder)));
  3449. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3450. I915_READ(VBLANK(cpu_transcoder)));
  3451. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3452. I915_READ(VSYNC(cpu_transcoder)));
  3453. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3454. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3455. }
  3456. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3457. {
  3458. struct drm_i915_private *dev_priv = dev->dev_private;
  3459. uint32_t temp;
  3460. temp = I915_READ(SOUTH_CHICKEN1);
  3461. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3462. return;
  3463. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3464. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3465. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3466. if (enable)
  3467. temp |= FDI_BC_BIFURCATION_SELECT;
  3468. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3469. I915_WRITE(SOUTH_CHICKEN1, temp);
  3470. POSTING_READ(SOUTH_CHICKEN1);
  3471. }
  3472. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3473. {
  3474. struct drm_device *dev = intel_crtc->base.dev;
  3475. switch (intel_crtc->pipe) {
  3476. case PIPE_A:
  3477. break;
  3478. case PIPE_B:
  3479. if (intel_crtc->config->fdi_lanes > 2)
  3480. cpt_set_fdi_bc_bifurcation(dev, false);
  3481. else
  3482. cpt_set_fdi_bc_bifurcation(dev, true);
  3483. break;
  3484. case PIPE_C:
  3485. cpt_set_fdi_bc_bifurcation(dev, true);
  3486. break;
  3487. default:
  3488. BUG();
  3489. }
  3490. }
  3491. /*
  3492. * Enable PCH resources required for PCH ports:
  3493. * - PCH PLLs
  3494. * - FDI training & RX/TX
  3495. * - update transcoder timings
  3496. * - DP transcoding bits
  3497. * - transcoder
  3498. */
  3499. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3500. {
  3501. struct drm_device *dev = crtc->dev;
  3502. struct drm_i915_private *dev_priv = dev->dev_private;
  3503. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3504. int pipe = intel_crtc->pipe;
  3505. u32 reg, temp;
  3506. assert_pch_transcoder_disabled(dev_priv, pipe);
  3507. if (IS_IVYBRIDGE(dev))
  3508. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3509. /* Write the TU size bits before fdi link training, so that error
  3510. * detection works. */
  3511. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3512. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3513. /* For PCH output, training FDI link */
  3514. dev_priv->display.fdi_link_train(crtc);
  3515. /* We need to program the right clock selection before writing the pixel
  3516. * mutliplier into the DPLL. */
  3517. if (HAS_PCH_CPT(dev)) {
  3518. u32 sel;
  3519. temp = I915_READ(PCH_DPLL_SEL);
  3520. temp |= TRANS_DPLL_ENABLE(pipe);
  3521. sel = TRANS_DPLLB_SEL(pipe);
  3522. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3523. temp |= sel;
  3524. else
  3525. temp &= ~sel;
  3526. I915_WRITE(PCH_DPLL_SEL, temp);
  3527. }
  3528. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3529. * transcoder, and we actually should do this to not upset any PCH
  3530. * transcoder that already use the clock when we share it.
  3531. *
  3532. * Note that enable_shared_dpll tries to do the right thing, but
  3533. * get_shared_dpll unconditionally resets the pll - we need that to have
  3534. * the right LVDS enable sequence. */
  3535. intel_enable_shared_dpll(intel_crtc);
  3536. /* set transcoder timing, panel must allow it */
  3537. assert_panel_unlocked(dev_priv, pipe);
  3538. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3539. intel_fdi_normal_train(crtc);
  3540. /* For PCH DP, enable TRANS_DP_CTL */
  3541. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3542. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3543. reg = TRANS_DP_CTL(pipe);
  3544. temp = I915_READ(reg);
  3545. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3546. TRANS_DP_SYNC_MASK |
  3547. TRANS_DP_BPC_MASK);
  3548. temp |= TRANS_DP_OUTPUT_ENABLE;
  3549. temp |= bpc << 9; /* same format but at 11:9 */
  3550. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3551. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3552. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3553. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3554. switch (intel_trans_dp_port_sel(crtc)) {
  3555. case PCH_DP_B:
  3556. temp |= TRANS_DP_PORT_SEL_B;
  3557. break;
  3558. case PCH_DP_C:
  3559. temp |= TRANS_DP_PORT_SEL_C;
  3560. break;
  3561. case PCH_DP_D:
  3562. temp |= TRANS_DP_PORT_SEL_D;
  3563. break;
  3564. default:
  3565. BUG();
  3566. }
  3567. I915_WRITE(reg, temp);
  3568. }
  3569. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3570. }
  3571. static void lpt_pch_enable(struct drm_crtc *crtc)
  3572. {
  3573. struct drm_device *dev = crtc->dev;
  3574. struct drm_i915_private *dev_priv = dev->dev_private;
  3575. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3576. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3577. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3578. lpt_program_iclkip(crtc);
  3579. /* Set transcoder timing. */
  3580. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3581. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3582. }
  3583. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3584. struct intel_crtc_state *crtc_state)
  3585. {
  3586. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3587. struct intel_shared_dpll *pll;
  3588. struct intel_shared_dpll_config *shared_dpll;
  3589. enum intel_dpll_id i;
  3590. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3591. if (HAS_PCH_IBX(dev_priv->dev)) {
  3592. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3593. i = (enum intel_dpll_id) crtc->pipe;
  3594. pll = &dev_priv->shared_dplls[i];
  3595. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3596. crtc->base.base.id, pll->name);
  3597. WARN_ON(shared_dpll[i].crtc_mask);
  3598. goto found;
  3599. }
  3600. if (IS_BROXTON(dev_priv->dev)) {
  3601. /* PLL is attached to port in bxt */
  3602. struct intel_encoder *encoder;
  3603. struct intel_digital_port *intel_dig_port;
  3604. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3605. if (WARN_ON(!encoder))
  3606. return NULL;
  3607. intel_dig_port = enc_to_dig_port(&encoder->base);
  3608. /* 1:1 mapping between ports and PLLs */
  3609. i = (enum intel_dpll_id)intel_dig_port->port;
  3610. pll = &dev_priv->shared_dplls[i];
  3611. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3612. crtc->base.base.id, pll->name);
  3613. WARN_ON(shared_dpll[i].crtc_mask);
  3614. goto found;
  3615. }
  3616. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3617. pll = &dev_priv->shared_dplls[i];
  3618. /* Only want to check enabled timings first */
  3619. if (shared_dpll[i].crtc_mask == 0)
  3620. continue;
  3621. if (memcmp(&crtc_state->dpll_hw_state,
  3622. &shared_dpll[i].hw_state,
  3623. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3624. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3625. crtc->base.base.id, pll->name,
  3626. shared_dpll[i].crtc_mask,
  3627. pll->active);
  3628. goto found;
  3629. }
  3630. }
  3631. /* Ok no matching timings, maybe there's a free one? */
  3632. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3633. pll = &dev_priv->shared_dplls[i];
  3634. if (shared_dpll[i].crtc_mask == 0) {
  3635. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3636. crtc->base.base.id, pll->name);
  3637. goto found;
  3638. }
  3639. }
  3640. return NULL;
  3641. found:
  3642. if (shared_dpll[i].crtc_mask == 0)
  3643. shared_dpll[i].hw_state =
  3644. crtc_state->dpll_hw_state;
  3645. crtc_state->shared_dpll = i;
  3646. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3647. pipe_name(crtc->pipe));
  3648. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3649. return pll;
  3650. }
  3651. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3652. {
  3653. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3654. struct intel_shared_dpll_config *shared_dpll;
  3655. struct intel_shared_dpll *pll;
  3656. enum intel_dpll_id i;
  3657. if (!to_intel_atomic_state(state)->dpll_set)
  3658. return;
  3659. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3660. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3661. pll = &dev_priv->shared_dplls[i];
  3662. pll->config = shared_dpll[i];
  3663. }
  3664. }
  3665. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3666. {
  3667. struct drm_i915_private *dev_priv = dev->dev_private;
  3668. int dslreg = PIPEDSL(pipe);
  3669. u32 temp;
  3670. temp = I915_READ(dslreg);
  3671. udelay(500);
  3672. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3673. if (wait_for(I915_READ(dslreg) != temp, 5))
  3674. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3675. }
  3676. }
  3677. /**
  3678. * skl_update_scaler_users - Stages update to crtc's scaler state
  3679. * @intel_crtc: crtc
  3680. * @crtc_state: crtc_state
  3681. * @plane: plane (NULL indicates crtc is requesting update)
  3682. * @plane_state: plane's state
  3683. * @force_detach: request unconditional detachment of scaler
  3684. *
  3685. * This function updates scaler state for requested plane or crtc.
  3686. * To request scaler usage update for a plane, caller shall pass plane pointer.
  3687. * To request scaler usage update for crtc, caller shall pass plane pointer
  3688. * as NULL.
  3689. *
  3690. * Return
  3691. * 0 - scaler_usage updated successfully
  3692. * error - requested scaling cannot be supported or other error condition
  3693. */
  3694. int
  3695. skl_update_scaler_users(
  3696. struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
  3697. struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
  3698. int force_detach)
  3699. {
  3700. int need_scaling;
  3701. int idx;
  3702. int src_w, src_h, dst_w, dst_h;
  3703. int *scaler_id;
  3704. struct drm_framebuffer *fb;
  3705. struct intel_crtc_scaler_state *scaler_state;
  3706. unsigned int rotation;
  3707. if (!intel_crtc || !crtc_state)
  3708. return 0;
  3709. scaler_state = &crtc_state->scaler_state;
  3710. idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
  3711. fb = intel_plane ? plane_state->base.fb : NULL;
  3712. if (intel_plane) {
  3713. src_w = drm_rect_width(&plane_state->src) >> 16;
  3714. src_h = drm_rect_height(&plane_state->src) >> 16;
  3715. dst_w = drm_rect_width(&plane_state->dst);
  3716. dst_h = drm_rect_height(&plane_state->dst);
  3717. scaler_id = &plane_state->scaler_id;
  3718. rotation = plane_state->base.rotation;
  3719. } else {
  3720. struct drm_display_mode *adjusted_mode =
  3721. &crtc_state->base.adjusted_mode;
  3722. src_w = crtc_state->pipe_src_w;
  3723. src_h = crtc_state->pipe_src_h;
  3724. dst_w = adjusted_mode->hdisplay;
  3725. dst_h = adjusted_mode->vdisplay;
  3726. scaler_id = &scaler_state->scaler_id;
  3727. rotation = DRM_ROTATE_0;
  3728. }
  3729. need_scaling = intel_rotation_90_or_270(rotation) ?
  3730. (src_h != dst_w || src_w != dst_h):
  3731. (src_w != dst_w || src_h != dst_h);
  3732. /*
  3733. * if plane is being disabled or scaler is no more required or force detach
  3734. * - free scaler binded to this plane/crtc
  3735. * - in order to do this, update crtc->scaler_usage
  3736. *
  3737. * Here scaler state in crtc_state is set free so that
  3738. * scaler can be assigned to other user. Actual register
  3739. * update to free the scaler is done in plane/panel-fit programming.
  3740. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3741. */
  3742. if (force_detach || !need_scaling || (intel_plane &&
  3743. (!fb || !plane_state->visible))) {
  3744. if (*scaler_id >= 0) {
  3745. scaler_state->scaler_users &= ~(1 << idx);
  3746. scaler_state->scalers[*scaler_id].in_use = 0;
  3747. DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
  3748. "crtc_state = %p scaler_users = 0x%x\n",
  3749. intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
  3750. intel_plane ? intel_plane->base.base.id :
  3751. intel_crtc->base.base.id, crtc_state,
  3752. scaler_state->scaler_users);
  3753. *scaler_id = -1;
  3754. }
  3755. return 0;
  3756. }
  3757. /* range checks */
  3758. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3759. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3760. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3761. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3762. DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
  3763. "size is out of scaler range\n",
  3764. intel_plane ? "PLANE" : "CRTC",
  3765. intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
  3766. intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
  3767. return -EINVAL;
  3768. }
  3769. /* check colorkey */
  3770. if (WARN_ON(intel_plane &&
  3771. intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
  3772. DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
  3773. intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
  3774. return -EINVAL;
  3775. }
  3776. /* Check src format */
  3777. if (intel_plane) {
  3778. switch (fb->pixel_format) {
  3779. case DRM_FORMAT_RGB565:
  3780. case DRM_FORMAT_XBGR8888:
  3781. case DRM_FORMAT_XRGB8888:
  3782. case DRM_FORMAT_ABGR8888:
  3783. case DRM_FORMAT_ARGB8888:
  3784. case DRM_FORMAT_XRGB2101010:
  3785. case DRM_FORMAT_XBGR2101010:
  3786. case DRM_FORMAT_YUYV:
  3787. case DRM_FORMAT_YVYU:
  3788. case DRM_FORMAT_UYVY:
  3789. case DRM_FORMAT_VYUY:
  3790. break;
  3791. default:
  3792. DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
  3793. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3794. return -EINVAL;
  3795. }
  3796. }
  3797. /* mark this plane as a scaler user in crtc_state */
  3798. scaler_state->scaler_users |= (1 << idx);
  3799. DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
  3800. "crtc_state = %p scaler_users = 0x%x\n",
  3801. intel_plane ? "PLANE" : "CRTC",
  3802. intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
  3803. src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
  3804. return 0;
  3805. }
  3806. static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
  3807. {
  3808. struct drm_device *dev = crtc->base.dev;
  3809. struct drm_i915_private *dev_priv = dev->dev_private;
  3810. int pipe = crtc->pipe;
  3811. struct intel_crtc_scaler_state *scaler_state =
  3812. &crtc->config->scaler_state;
  3813. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3814. /* To update pfit, first update scaler state */
  3815. skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
  3816. intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
  3817. skl_detach_scalers(crtc);
  3818. if (!enable)
  3819. return;
  3820. if (crtc->config->pch_pfit.enabled) {
  3821. int id;
  3822. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3823. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3824. return;
  3825. }
  3826. id = scaler_state->scaler_id;
  3827. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3828. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3829. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3830. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3831. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3832. }
  3833. }
  3834. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3835. {
  3836. struct drm_device *dev = crtc->base.dev;
  3837. struct drm_i915_private *dev_priv = dev->dev_private;
  3838. int pipe = crtc->pipe;
  3839. if (crtc->config->pch_pfit.enabled) {
  3840. /* Force use of hard-coded filter coefficients
  3841. * as some pre-programmed values are broken,
  3842. * e.g. x201.
  3843. */
  3844. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3845. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3846. PF_PIPE_SEL_IVB(pipe));
  3847. else
  3848. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3849. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3850. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3851. }
  3852. }
  3853. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3854. {
  3855. struct drm_device *dev = crtc->dev;
  3856. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3857. struct drm_plane *plane;
  3858. struct intel_plane *intel_plane;
  3859. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3860. intel_plane = to_intel_plane(plane);
  3861. if (intel_plane->pipe == pipe)
  3862. intel_plane_restore(&intel_plane->base);
  3863. }
  3864. }
  3865. void hsw_enable_ips(struct intel_crtc *crtc)
  3866. {
  3867. struct drm_device *dev = crtc->base.dev;
  3868. struct drm_i915_private *dev_priv = dev->dev_private;
  3869. if (!crtc->config->ips_enabled)
  3870. return;
  3871. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3872. intel_wait_for_vblank(dev, crtc->pipe);
  3873. assert_plane_enabled(dev_priv, crtc->plane);
  3874. if (IS_BROADWELL(dev)) {
  3875. mutex_lock(&dev_priv->rps.hw_lock);
  3876. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3877. mutex_unlock(&dev_priv->rps.hw_lock);
  3878. /* Quoting Art Runyan: "its not safe to expect any particular
  3879. * value in IPS_CTL bit 31 after enabling IPS through the
  3880. * mailbox." Moreover, the mailbox may return a bogus state,
  3881. * so we need to just enable it and continue on.
  3882. */
  3883. } else {
  3884. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3885. /* The bit only becomes 1 in the next vblank, so this wait here
  3886. * is essentially intel_wait_for_vblank. If we don't have this
  3887. * and don't wait for vblanks until the end of crtc_enable, then
  3888. * the HW state readout code will complain that the expected
  3889. * IPS_CTL value is not the one we read. */
  3890. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3891. DRM_ERROR("Timed out waiting for IPS enable\n");
  3892. }
  3893. }
  3894. void hsw_disable_ips(struct intel_crtc *crtc)
  3895. {
  3896. struct drm_device *dev = crtc->base.dev;
  3897. struct drm_i915_private *dev_priv = dev->dev_private;
  3898. if (!crtc->config->ips_enabled)
  3899. return;
  3900. assert_plane_enabled(dev_priv, crtc->plane);
  3901. if (IS_BROADWELL(dev)) {
  3902. mutex_lock(&dev_priv->rps.hw_lock);
  3903. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3904. mutex_unlock(&dev_priv->rps.hw_lock);
  3905. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3906. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3907. DRM_ERROR("Timed out waiting for IPS disable\n");
  3908. } else {
  3909. I915_WRITE(IPS_CTL, 0);
  3910. POSTING_READ(IPS_CTL);
  3911. }
  3912. /* We need to wait for a vblank before we can disable the plane. */
  3913. intel_wait_for_vblank(dev, crtc->pipe);
  3914. }
  3915. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3916. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3917. {
  3918. struct drm_device *dev = crtc->dev;
  3919. struct drm_i915_private *dev_priv = dev->dev_private;
  3920. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3921. enum pipe pipe = intel_crtc->pipe;
  3922. int palreg = PALETTE(pipe);
  3923. int i;
  3924. bool reenable_ips = false;
  3925. /* The clocks have to be on to load the palette. */
  3926. if (!crtc->state->active)
  3927. return;
  3928. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3929. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3930. assert_dsi_pll_enabled(dev_priv);
  3931. else
  3932. assert_pll_enabled(dev_priv, pipe);
  3933. }
  3934. /* use legacy palette for Ironlake */
  3935. if (!HAS_GMCH_DISPLAY(dev))
  3936. palreg = LGC_PALETTE(pipe);
  3937. /* Workaround : Do not read or write the pipe palette/gamma data while
  3938. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3939. */
  3940. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3941. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3942. GAMMA_MODE_MODE_SPLIT)) {
  3943. hsw_disable_ips(intel_crtc);
  3944. reenable_ips = true;
  3945. }
  3946. for (i = 0; i < 256; i++) {
  3947. I915_WRITE(palreg + 4 * i,
  3948. (intel_crtc->lut_r[i] << 16) |
  3949. (intel_crtc->lut_g[i] << 8) |
  3950. intel_crtc->lut_b[i]);
  3951. }
  3952. if (reenable_ips)
  3953. hsw_enable_ips(intel_crtc);
  3954. }
  3955. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3956. {
  3957. if (intel_crtc->overlay) {
  3958. struct drm_device *dev = intel_crtc->base.dev;
  3959. struct drm_i915_private *dev_priv = dev->dev_private;
  3960. mutex_lock(&dev->struct_mutex);
  3961. dev_priv->mm.interruptible = false;
  3962. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3963. dev_priv->mm.interruptible = true;
  3964. mutex_unlock(&dev->struct_mutex);
  3965. }
  3966. /* Let userspace switch the overlay on again. In most cases userspace
  3967. * has to recompute where to put it anyway.
  3968. */
  3969. }
  3970. /**
  3971. * intel_post_enable_primary - Perform operations after enabling primary plane
  3972. * @crtc: the CRTC whose primary plane was just enabled
  3973. *
  3974. * Performs potentially sleeping operations that must be done after the primary
  3975. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3976. * called due to an explicit primary plane update, or due to an implicit
  3977. * re-enable that is caused when a sprite plane is updated to no longer
  3978. * completely hide the primary plane.
  3979. */
  3980. static void
  3981. intel_post_enable_primary(struct drm_crtc *crtc)
  3982. {
  3983. struct drm_device *dev = crtc->dev;
  3984. struct drm_i915_private *dev_priv = dev->dev_private;
  3985. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3986. int pipe = intel_crtc->pipe;
  3987. /*
  3988. * BDW signals flip done immediately if the plane
  3989. * is disabled, even if the plane enable is already
  3990. * armed to occur at the next vblank :(
  3991. */
  3992. if (IS_BROADWELL(dev))
  3993. intel_wait_for_vblank(dev, pipe);
  3994. /*
  3995. * FIXME IPS should be fine as long as one plane is
  3996. * enabled, but in practice it seems to have problems
  3997. * when going from primary only to sprite only and vice
  3998. * versa.
  3999. */
  4000. hsw_enable_ips(intel_crtc);
  4001. mutex_lock(&dev->struct_mutex);
  4002. intel_fbc_update(dev);
  4003. mutex_unlock(&dev->struct_mutex);
  4004. /*
  4005. * Gen2 reports pipe underruns whenever all planes are disabled.
  4006. * So don't enable underrun reporting before at least some planes
  4007. * are enabled.
  4008. * FIXME: Need to fix the logic to work when we turn off all planes
  4009. * but leave the pipe running.
  4010. */
  4011. if (IS_GEN2(dev))
  4012. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4013. /* Underruns don't raise interrupts, so check manually. */
  4014. if (HAS_GMCH_DISPLAY(dev))
  4015. i9xx_check_fifo_underruns(dev_priv);
  4016. }
  4017. /**
  4018. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4019. * @crtc: the CRTC whose primary plane is to be disabled
  4020. *
  4021. * Performs potentially sleeping operations that must be done before the
  4022. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4023. * be called due to an explicit primary plane update, or due to an implicit
  4024. * disable that is caused when a sprite plane completely hides the primary
  4025. * plane.
  4026. */
  4027. static void
  4028. intel_pre_disable_primary(struct drm_crtc *crtc)
  4029. {
  4030. struct drm_device *dev = crtc->dev;
  4031. struct drm_i915_private *dev_priv = dev->dev_private;
  4032. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4033. int pipe = intel_crtc->pipe;
  4034. /*
  4035. * Gen2 reports pipe underruns whenever all planes are disabled.
  4036. * So diasble underrun reporting before all the planes get disabled.
  4037. * FIXME: Need to fix the logic to work when we turn off all planes
  4038. * but leave the pipe running.
  4039. */
  4040. if (IS_GEN2(dev))
  4041. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4042. /*
  4043. * Vblank time updates from the shadow to live plane control register
  4044. * are blocked if the memory self-refresh mode is active at that
  4045. * moment. So to make sure the plane gets truly disabled, disable
  4046. * first the self-refresh mode. The self-refresh enable bit in turn
  4047. * will be checked/applied by the HW only at the next frame start
  4048. * event which is after the vblank start event, so we need to have a
  4049. * wait-for-vblank between disabling the plane and the pipe.
  4050. */
  4051. if (HAS_GMCH_DISPLAY(dev))
  4052. intel_set_memory_cxsr(dev_priv, false);
  4053. mutex_lock(&dev->struct_mutex);
  4054. if (dev_priv->fbc.crtc == intel_crtc)
  4055. intel_fbc_disable(dev);
  4056. mutex_unlock(&dev->struct_mutex);
  4057. /*
  4058. * FIXME IPS should be fine as long as one plane is
  4059. * enabled, but in practice it seems to have problems
  4060. * when going from primary only to sprite only and vice
  4061. * versa.
  4062. */
  4063. hsw_disable_ips(intel_crtc);
  4064. }
  4065. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  4066. {
  4067. struct drm_device *dev = crtc->dev;
  4068. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4069. int pipe = intel_crtc->pipe;
  4070. intel_enable_primary_hw_plane(crtc->primary, crtc);
  4071. intel_enable_sprite_planes(crtc);
  4072. intel_crtc_update_cursor(crtc, true);
  4073. intel_post_enable_primary(crtc);
  4074. /*
  4075. * FIXME: Once we grow proper nuclear flip support out of this we need
  4076. * to compute the mask of flip planes precisely. For the time being
  4077. * consider this a flip to a NULL plane.
  4078. */
  4079. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4080. }
  4081. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  4082. {
  4083. struct drm_device *dev = crtc->dev;
  4084. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4085. struct intel_plane *intel_plane;
  4086. int pipe = intel_crtc->pipe;
  4087. intel_crtc_wait_for_pending_flips(crtc);
  4088. intel_pre_disable_primary(crtc);
  4089. intel_crtc_dpms_overlay_disable(intel_crtc);
  4090. for_each_intel_plane(dev, intel_plane) {
  4091. if (intel_plane->pipe == pipe) {
  4092. struct drm_crtc *from = intel_plane->base.crtc;
  4093. intel_plane->disable_plane(&intel_plane->base,
  4094. from ?: crtc, true);
  4095. }
  4096. }
  4097. /*
  4098. * FIXME: Once we grow proper nuclear flip support out of this we need
  4099. * to compute the mask of flip planes precisely. For the time being
  4100. * consider this a flip to a NULL plane.
  4101. */
  4102. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4103. }
  4104. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4105. {
  4106. struct drm_device *dev = crtc->dev;
  4107. struct drm_i915_private *dev_priv = dev->dev_private;
  4108. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4109. struct intel_encoder *encoder;
  4110. int pipe = intel_crtc->pipe;
  4111. if (WARN_ON(intel_crtc->active))
  4112. return;
  4113. if (intel_crtc->config->has_pch_encoder)
  4114. intel_prepare_shared_dpll(intel_crtc);
  4115. if (intel_crtc->config->has_dp_encoder)
  4116. intel_dp_set_m_n(intel_crtc, M1_N1);
  4117. intel_set_pipe_timings(intel_crtc);
  4118. if (intel_crtc->config->has_pch_encoder) {
  4119. intel_cpu_transcoder_set_m_n(intel_crtc,
  4120. &intel_crtc->config->fdi_m_n, NULL);
  4121. }
  4122. ironlake_set_pipeconf(crtc);
  4123. intel_crtc->active = true;
  4124. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4125. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4126. for_each_encoder_on_crtc(dev, crtc, encoder)
  4127. if (encoder->pre_enable)
  4128. encoder->pre_enable(encoder);
  4129. if (intel_crtc->config->has_pch_encoder) {
  4130. /* Note: FDI PLL enabling _must_ be done before we enable the
  4131. * cpu pipes, hence this is separate from all the other fdi/pch
  4132. * enabling. */
  4133. ironlake_fdi_pll_enable(intel_crtc);
  4134. } else {
  4135. assert_fdi_tx_disabled(dev_priv, pipe);
  4136. assert_fdi_rx_disabled(dev_priv, pipe);
  4137. }
  4138. ironlake_pfit_enable(intel_crtc);
  4139. /*
  4140. * On ILK+ LUT must be loaded before the pipe is running but with
  4141. * clocks enabled
  4142. */
  4143. intel_crtc_load_lut(crtc);
  4144. intel_update_watermarks(crtc);
  4145. intel_enable_pipe(intel_crtc);
  4146. if (intel_crtc->config->has_pch_encoder)
  4147. ironlake_pch_enable(crtc);
  4148. assert_vblank_disabled(crtc);
  4149. drm_crtc_vblank_on(crtc);
  4150. for_each_encoder_on_crtc(dev, crtc, encoder)
  4151. encoder->enable(encoder);
  4152. if (HAS_PCH_CPT(dev))
  4153. cpt_verify_modeset(dev, intel_crtc->pipe);
  4154. }
  4155. /* IPS only exists on ULT machines and is tied to pipe A. */
  4156. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4157. {
  4158. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4159. }
  4160. /*
  4161. * This implements the workaround described in the "notes" section of the mode
  4162. * set sequence documentation. When going from no pipes or single pipe to
  4163. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  4164. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  4165. */
  4166. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  4167. {
  4168. struct drm_device *dev = crtc->base.dev;
  4169. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  4170. /* We want to get the other_active_crtc only if there's only 1 other
  4171. * active crtc. */
  4172. for_each_intel_crtc(dev, crtc_it) {
  4173. if (!crtc_it->active || crtc_it == crtc)
  4174. continue;
  4175. if (other_active_crtc)
  4176. return;
  4177. other_active_crtc = crtc_it;
  4178. }
  4179. if (!other_active_crtc)
  4180. return;
  4181. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  4182. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  4183. }
  4184. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4185. {
  4186. struct drm_device *dev = crtc->dev;
  4187. struct drm_i915_private *dev_priv = dev->dev_private;
  4188. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4189. struct intel_encoder *encoder;
  4190. int pipe = intel_crtc->pipe;
  4191. if (WARN_ON(intel_crtc->active))
  4192. return;
  4193. if (intel_crtc_to_shared_dpll(intel_crtc))
  4194. intel_enable_shared_dpll(intel_crtc);
  4195. if (intel_crtc->config->has_dp_encoder)
  4196. intel_dp_set_m_n(intel_crtc, M1_N1);
  4197. intel_set_pipe_timings(intel_crtc);
  4198. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4199. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4200. intel_crtc->config->pixel_multiplier - 1);
  4201. }
  4202. if (intel_crtc->config->has_pch_encoder) {
  4203. intel_cpu_transcoder_set_m_n(intel_crtc,
  4204. &intel_crtc->config->fdi_m_n, NULL);
  4205. }
  4206. haswell_set_pipeconf(crtc);
  4207. intel_set_pipe_csc(crtc);
  4208. intel_crtc->active = true;
  4209. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4210. for_each_encoder_on_crtc(dev, crtc, encoder)
  4211. if (encoder->pre_enable)
  4212. encoder->pre_enable(encoder);
  4213. if (intel_crtc->config->has_pch_encoder) {
  4214. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4215. true);
  4216. dev_priv->display.fdi_link_train(crtc);
  4217. }
  4218. intel_ddi_enable_pipe_clock(intel_crtc);
  4219. if (INTEL_INFO(dev)->gen == 9)
  4220. skylake_pfit_update(intel_crtc, 1);
  4221. else if (INTEL_INFO(dev)->gen < 9)
  4222. ironlake_pfit_enable(intel_crtc);
  4223. else
  4224. MISSING_CASE(INTEL_INFO(dev)->gen);
  4225. /*
  4226. * On ILK+ LUT must be loaded before the pipe is running but with
  4227. * clocks enabled
  4228. */
  4229. intel_crtc_load_lut(crtc);
  4230. intel_ddi_set_pipe_settings(crtc);
  4231. intel_ddi_enable_transcoder_func(crtc);
  4232. intel_update_watermarks(crtc);
  4233. intel_enable_pipe(intel_crtc);
  4234. if (intel_crtc->config->has_pch_encoder)
  4235. lpt_pch_enable(crtc);
  4236. if (intel_crtc->config->dp_encoder_is_mst)
  4237. intel_ddi_set_vc_payload_alloc(crtc, true);
  4238. assert_vblank_disabled(crtc);
  4239. drm_crtc_vblank_on(crtc);
  4240. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4241. encoder->enable(encoder);
  4242. intel_opregion_notify_encoder(encoder, true);
  4243. }
  4244. /* If we change the relative order between pipe/planes enabling, we need
  4245. * to change the workaround. */
  4246. haswell_mode_set_planes_workaround(intel_crtc);
  4247. }
  4248. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4249. {
  4250. struct drm_device *dev = crtc->base.dev;
  4251. struct drm_i915_private *dev_priv = dev->dev_private;
  4252. int pipe = crtc->pipe;
  4253. /* To avoid upsetting the power well on haswell only disable the pfit if
  4254. * it's in use. The hw state code will make sure we get this right. */
  4255. if (crtc->config->pch_pfit.enabled) {
  4256. I915_WRITE(PF_CTL(pipe), 0);
  4257. I915_WRITE(PF_WIN_POS(pipe), 0);
  4258. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4259. }
  4260. }
  4261. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4262. {
  4263. struct drm_device *dev = crtc->dev;
  4264. struct drm_i915_private *dev_priv = dev->dev_private;
  4265. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4266. struct intel_encoder *encoder;
  4267. int pipe = intel_crtc->pipe;
  4268. u32 reg, temp;
  4269. if (WARN_ON(!intel_crtc->active))
  4270. return;
  4271. for_each_encoder_on_crtc(dev, crtc, encoder)
  4272. encoder->disable(encoder);
  4273. drm_crtc_vblank_off(crtc);
  4274. assert_vblank_disabled(crtc);
  4275. if (intel_crtc->config->has_pch_encoder)
  4276. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4277. intel_disable_pipe(intel_crtc);
  4278. ironlake_pfit_disable(intel_crtc);
  4279. if (intel_crtc->config->has_pch_encoder)
  4280. ironlake_fdi_disable(crtc);
  4281. for_each_encoder_on_crtc(dev, crtc, encoder)
  4282. if (encoder->post_disable)
  4283. encoder->post_disable(encoder);
  4284. if (intel_crtc->config->has_pch_encoder) {
  4285. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4286. if (HAS_PCH_CPT(dev)) {
  4287. /* disable TRANS_DP_CTL */
  4288. reg = TRANS_DP_CTL(pipe);
  4289. temp = I915_READ(reg);
  4290. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4291. TRANS_DP_PORT_SEL_MASK);
  4292. temp |= TRANS_DP_PORT_SEL_NONE;
  4293. I915_WRITE(reg, temp);
  4294. /* disable DPLL_SEL */
  4295. temp = I915_READ(PCH_DPLL_SEL);
  4296. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4297. I915_WRITE(PCH_DPLL_SEL, temp);
  4298. }
  4299. /* disable PCH DPLL */
  4300. intel_disable_shared_dpll(intel_crtc);
  4301. ironlake_fdi_pll_disable(intel_crtc);
  4302. }
  4303. intel_crtc->active = false;
  4304. intel_update_watermarks(crtc);
  4305. mutex_lock(&dev->struct_mutex);
  4306. intel_fbc_update(dev);
  4307. mutex_unlock(&dev->struct_mutex);
  4308. }
  4309. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4310. {
  4311. struct drm_device *dev = crtc->dev;
  4312. struct drm_i915_private *dev_priv = dev->dev_private;
  4313. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4314. struct intel_encoder *encoder;
  4315. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4316. if (WARN_ON(!intel_crtc->active))
  4317. return;
  4318. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4319. intel_opregion_notify_encoder(encoder, false);
  4320. encoder->disable(encoder);
  4321. }
  4322. drm_crtc_vblank_off(crtc);
  4323. assert_vblank_disabled(crtc);
  4324. if (intel_crtc->config->has_pch_encoder)
  4325. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4326. false);
  4327. intel_disable_pipe(intel_crtc);
  4328. if (intel_crtc->config->dp_encoder_is_mst)
  4329. intel_ddi_set_vc_payload_alloc(crtc, false);
  4330. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4331. if (INTEL_INFO(dev)->gen == 9)
  4332. skylake_pfit_update(intel_crtc, 0);
  4333. else if (INTEL_INFO(dev)->gen < 9)
  4334. ironlake_pfit_disable(intel_crtc);
  4335. else
  4336. MISSING_CASE(INTEL_INFO(dev)->gen);
  4337. intel_ddi_disable_pipe_clock(intel_crtc);
  4338. if (intel_crtc->config->has_pch_encoder) {
  4339. lpt_disable_pch_transcoder(dev_priv);
  4340. intel_ddi_fdi_disable(crtc);
  4341. }
  4342. for_each_encoder_on_crtc(dev, crtc, encoder)
  4343. if (encoder->post_disable)
  4344. encoder->post_disable(encoder);
  4345. intel_crtc->active = false;
  4346. intel_update_watermarks(crtc);
  4347. mutex_lock(&dev->struct_mutex);
  4348. intel_fbc_update(dev);
  4349. mutex_unlock(&dev->struct_mutex);
  4350. if (intel_crtc_to_shared_dpll(intel_crtc))
  4351. intel_disable_shared_dpll(intel_crtc);
  4352. }
  4353. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4354. {
  4355. struct drm_device *dev = crtc->base.dev;
  4356. struct drm_i915_private *dev_priv = dev->dev_private;
  4357. struct intel_crtc_state *pipe_config = crtc->config;
  4358. if (!pipe_config->gmch_pfit.control)
  4359. return;
  4360. /*
  4361. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4362. * according to register description and PRM.
  4363. */
  4364. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4365. assert_pipe_disabled(dev_priv, crtc->pipe);
  4366. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4367. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4368. /* Border color in case we don't scale up to the full screen. Black by
  4369. * default, change to something else for debugging. */
  4370. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4371. }
  4372. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4373. {
  4374. switch (port) {
  4375. case PORT_A:
  4376. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4377. case PORT_B:
  4378. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4379. case PORT_C:
  4380. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4381. case PORT_D:
  4382. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4383. default:
  4384. WARN_ON_ONCE(1);
  4385. return POWER_DOMAIN_PORT_OTHER;
  4386. }
  4387. }
  4388. #define for_each_power_domain(domain, mask) \
  4389. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4390. if ((1 << (domain)) & (mask))
  4391. enum intel_display_power_domain
  4392. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4393. {
  4394. struct drm_device *dev = intel_encoder->base.dev;
  4395. struct intel_digital_port *intel_dig_port;
  4396. switch (intel_encoder->type) {
  4397. case INTEL_OUTPUT_UNKNOWN:
  4398. /* Only DDI platforms should ever use this output type */
  4399. WARN_ON_ONCE(!HAS_DDI(dev));
  4400. case INTEL_OUTPUT_DISPLAYPORT:
  4401. case INTEL_OUTPUT_HDMI:
  4402. case INTEL_OUTPUT_EDP:
  4403. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4404. return port_to_power_domain(intel_dig_port->port);
  4405. case INTEL_OUTPUT_DP_MST:
  4406. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4407. return port_to_power_domain(intel_dig_port->port);
  4408. case INTEL_OUTPUT_ANALOG:
  4409. return POWER_DOMAIN_PORT_CRT;
  4410. case INTEL_OUTPUT_DSI:
  4411. return POWER_DOMAIN_PORT_DSI;
  4412. default:
  4413. return POWER_DOMAIN_PORT_OTHER;
  4414. }
  4415. }
  4416. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4417. {
  4418. struct drm_device *dev = crtc->dev;
  4419. struct intel_encoder *intel_encoder;
  4420. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4421. enum pipe pipe = intel_crtc->pipe;
  4422. unsigned long mask;
  4423. enum transcoder transcoder;
  4424. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4425. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4426. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4427. if (intel_crtc->config->pch_pfit.enabled ||
  4428. intel_crtc->config->pch_pfit.force_thru)
  4429. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4430. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4431. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4432. return mask;
  4433. }
  4434. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4435. {
  4436. struct drm_device *dev = state->dev;
  4437. struct drm_i915_private *dev_priv = dev->dev_private;
  4438. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4439. struct intel_crtc *crtc;
  4440. /*
  4441. * First get all needed power domains, then put all unneeded, to avoid
  4442. * any unnecessary toggling of the power wells.
  4443. */
  4444. for_each_intel_crtc(dev, crtc) {
  4445. enum intel_display_power_domain domain;
  4446. if (!crtc->base.state->enable)
  4447. continue;
  4448. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4449. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4450. intel_display_power_get(dev_priv, domain);
  4451. }
  4452. if (dev_priv->display.modeset_global_resources)
  4453. dev_priv->display.modeset_global_resources(state);
  4454. for_each_intel_crtc(dev, crtc) {
  4455. enum intel_display_power_domain domain;
  4456. for_each_power_domain(domain, crtc->enabled_power_domains)
  4457. intel_display_power_put(dev_priv, domain);
  4458. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4459. }
  4460. intel_display_set_init_power(dev_priv, false);
  4461. }
  4462. static void intel_update_max_cdclk(struct drm_device *dev)
  4463. {
  4464. struct drm_i915_private *dev_priv = dev->dev_private;
  4465. if (IS_SKYLAKE(dev)) {
  4466. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4467. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4468. dev_priv->max_cdclk_freq = 675000;
  4469. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4470. dev_priv->max_cdclk_freq = 540000;
  4471. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4472. dev_priv->max_cdclk_freq = 450000;
  4473. else
  4474. dev_priv->max_cdclk_freq = 337500;
  4475. } else if (IS_BROADWELL(dev)) {
  4476. /*
  4477. * FIXME with extra cooling we can allow
  4478. * 540 MHz for ULX and 675 Mhz for ULT.
  4479. * How can we know if extra cooling is
  4480. * available? PCI ID, VTB, something else?
  4481. */
  4482. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4483. dev_priv->max_cdclk_freq = 450000;
  4484. else if (IS_BDW_ULX(dev))
  4485. dev_priv->max_cdclk_freq = 450000;
  4486. else if (IS_BDW_ULT(dev))
  4487. dev_priv->max_cdclk_freq = 540000;
  4488. else
  4489. dev_priv->max_cdclk_freq = 675000;
  4490. } else if (IS_VALLEYVIEW(dev)) {
  4491. dev_priv->max_cdclk_freq = 400000;
  4492. } else {
  4493. /* otherwise assume cdclk is fixed */
  4494. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4495. }
  4496. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4497. dev_priv->max_cdclk_freq);
  4498. }
  4499. static void intel_update_cdclk(struct drm_device *dev)
  4500. {
  4501. struct drm_i915_private *dev_priv = dev->dev_private;
  4502. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4503. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4504. dev_priv->cdclk_freq);
  4505. /*
  4506. * Program the gmbus_freq based on the cdclk frequency.
  4507. * BSpec erroneously claims we should aim for 4MHz, but
  4508. * in fact 1MHz is the correct frequency.
  4509. */
  4510. if (IS_VALLEYVIEW(dev)) {
  4511. /*
  4512. * Program the gmbus_freq based on the cdclk frequency.
  4513. * BSpec erroneously claims we should aim for 4MHz, but
  4514. * in fact 1MHz is the correct frequency.
  4515. */
  4516. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4517. }
  4518. if (dev_priv->max_cdclk_freq == 0)
  4519. intel_update_max_cdclk(dev);
  4520. }
  4521. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4522. {
  4523. struct drm_i915_private *dev_priv = dev->dev_private;
  4524. uint32_t divider;
  4525. uint32_t ratio;
  4526. uint32_t current_freq;
  4527. int ret;
  4528. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4529. switch (frequency) {
  4530. case 144000:
  4531. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4532. ratio = BXT_DE_PLL_RATIO(60);
  4533. break;
  4534. case 288000:
  4535. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4536. ratio = BXT_DE_PLL_RATIO(60);
  4537. break;
  4538. case 384000:
  4539. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4540. ratio = BXT_DE_PLL_RATIO(60);
  4541. break;
  4542. case 576000:
  4543. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4544. ratio = BXT_DE_PLL_RATIO(60);
  4545. break;
  4546. case 624000:
  4547. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4548. ratio = BXT_DE_PLL_RATIO(65);
  4549. break;
  4550. case 19200:
  4551. /*
  4552. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4553. * to suppress GCC warning.
  4554. */
  4555. ratio = 0;
  4556. divider = 0;
  4557. break;
  4558. default:
  4559. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4560. return;
  4561. }
  4562. mutex_lock(&dev_priv->rps.hw_lock);
  4563. /* Inform power controller of upcoming frequency change */
  4564. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4565. 0x80000000);
  4566. mutex_unlock(&dev_priv->rps.hw_lock);
  4567. if (ret) {
  4568. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4569. ret, frequency);
  4570. return;
  4571. }
  4572. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4573. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4574. current_freq = current_freq * 500 + 1000;
  4575. /*
  4576. * DE PLL has to be disabled when
  4577. * - setting to 19.2MHz (bypass, PLL isn't used)
  4578. * - before setting to 624MHz (PLL needs toggling)
  4579. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4580. */
  4581. if (frequency == 19200 || frequency == 624000 ||
  4582. current_freq == 624000) {
  4583. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4584. /* Timeout 200us */
  4585. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4586. 1))
  4587. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4588. }
  4589. if (frequency != 19200) {
  4590. uint32_t val;
  4591. val = I915_READ(BXT_DE_PLL_CTL);
  4592. val &= ~BXT_DE_PLL_RATIO_MASK;
  4593. val |= ratio;
  4594. I915_WRITE(BXT_DE_PLL_CTL, val);
  4595. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4596. /* Timeout 200us */
  4597. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4598. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4599. val = I915_READ(CDCLK_CTL);
  4600. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4601. val |= divider;
  4602. /*
  4603. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4604. * enable otherwise.
  4605. */
  4606. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4607. if (frequency >= 500000)
  4608. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4609. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4610. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4611. val |= (frequency - 1000) / 500;
  4612. I915_WRITE(CDCLK_CTL, val);
  4613. }
  4614. mutex_lock(&dev_priv->rps.hw_lock);
  4615. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4616. DIV_ROUND_UP(frequency, 25000));
  4617. mutex_unlock(&dev_priv->rps.hw_lock);
  4618. if (ret) {
  4619. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4620. ret, frequency);
  4621. return;
  4622. }
  4623. intel_update_cdclk(dev);
  4624. }
  4625. void broxton_init_cdclk(struct drm_device *dev)
  4626. {
  4627. struct drm_i915_private *dev_priv = dev->dev_private;
  4628. uint32_t val;
  4629. /*
  4630. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4631. * or else the reset will hang because there is no PCH to respond.
  4632. * Move the handshake programming to initialization sequence.
  4633. * Previously was left up to BIOS.
  4634. */
  4635. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4636. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4637. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4638. /* Enable PG1 for cdclk */
  4639. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4640. /* check if cd clock is enabled */
  4641. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4642. DRM_DEBUG_KMS("Display already initialized\n");
  4643. return;
  4644. }
  4645. /*
  4646. * FIXME:
  4647. * - The initial CDCLK needs to be read from VBT.
  4648. * Need to make this change after VBT has changes for BXT.
  4649. * - check if setting the max (or any) cdclk freq is really necessary
  4650. * here, it belongs to modeset time
  4651. */
  4652. broxton_set_cdclk(dev, 624000);
  4653. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4654. POSTING_READ(DBUF_CTL);
  4655. udelay(10);
  4656. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4657. DRM_ERROR("DBuf power enable timeout!\n");
  4658. }
  4659. void broxton_uninit_cdclk(struct drm_device *dev)
  4660. {
  4661. struct drm_i915_private *dev_priv = dev->dev_private;
  4662. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4663. POSTING_READ(DBUF_CTL);
  4664. udelay(10);
  4665. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4666. DRM_ERROR("DBuf power disable timeout!\n");
  4667. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4668. broxton_set_cdclk(dev, 19200);
  4669. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4670. }
  4671. static const struct skl_cdclk_entry {
  4672. unsigned int freq;
  4673. unsigned int vco;
  4674. } skl_cdclk_frequencies[] = {
  4675. { .freq = 308570, .vco = 8640 },
  4676. { .freq = 337500, .vco = 8100 },
  4677. { .freq = 432000, .vco = 8640 },
  4678. { .freq = 450000, .vco = 8100 },
  4679. { .freq = 540000, .vco = 8100 },
  4680. { .freq = 617140, .vco = 8640 },
  4681. { .freq = 675000, .vco = 8100 },
  4682. };
  4683. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4684. {
  4685. return (freq - 1000) / 500;
  4686. }
  4687. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4688. {
  4689. unsigned int i;
  4690. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4691. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4692. if (e->freq == freq)
  4693. return e->vco;
  4694. }
  4695. return 8100;
  4696. }
  4697. static void
  4698. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4699. {
  4700. unsigned int min_freq;
  4701. u32 val;
  4702. /* select the minimum CDCLK before enabling DPLL 0 */
  4703. val = I915_READ(CDCLK_CTL);
  4704. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4705. val |= CDCLK_FREQ_337_308;
  4706. if (required_vco == 8640)
  4707. min_freq = 308570;
  4708. else
  4709. min_freq = 337500;
  4710. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4711. I915_WRITE(CDCLK_CTL, val);
  4712. POSTING_READ(CDCLK_CTL);
  4713. /*
  4714. * We always enable DPLL0 with the lowest link rate possible, but still
  4715. * taking into account the VCO required to operate the eDP panel at the
  4716. * desired frequency. The usual DP link rates operate with a VCO of
  4717. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4718. * The modeset code is responsible for the selection of the exact link
  4719. * rate later on, with the constraint of choosing a frequency that
  4720. * works with required_vco.
  4721. */
  4722. val = I915_READ(DPLL_CTRL1);
  4723. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4724. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4725. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4726. if (required_vco == 8640)
  4727. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4728. SKL_DPLL0);
  4729. else
  4730. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4731. SKL_DPLL0);
  4732. I915_WRITE(DPLL_CTRL1, val);
  4733. POSTING_READ(DPLL_CTRL1);
  4734. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4735. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4736. DRM_ERROR("DPLL0 not locked\n");
  4737. }
  4738. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4739. {
  4740. int ret;
  4741. u32 val;
  4742. /* inform PCU we want to change CDCLK */
  4743. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4744. mutex_lock(&dev_priv->rps.hw_lock);
  4745. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4746. mutex_unlock(&dev_priv->rps.hw_lock);
  4747. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4748. }
  4749. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4750. {
  4751. unsigned int i;
  4752. for (i = 0; i < 15; i++) {
  4753. if (skl_cdclk_pcu_ready(dev_priv))
  4754. return true;
  4755. udelay(10);
  4756. }
  4757. return false;
  4758. }
  4759. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4760. {
  4761. struct drm_device *dev = dev_priv->dev;
  4762. u32 freq_select, pcu_ack;
  4763. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4764. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4765. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4766. return;
  4767. }
  4768. /* set CDCLK_CTL */
  4769. switch(freq) {
  4770. case 450000:
  4771. case 432000:
  4772. freq_select = CDCLK_FREQ_450_432;
  4773. pcu_ack = 1;
  4774. break;
  4775. case 540000:
  4776. freq_select = CDCLK_FREQ_540;
  4777. pcu_ack = 2;
  4778. break;
  4779. case 308570:
  4780. case 337500:
  4781. default:
  4782. freq_select = CDCLK_FREQ_337_308;
  4783. pcu_ack = 0;
  4784. break;
  4785. case 617140:
  4786. case 675000:
  4787. freq_select = CDCLK_FREQ_675_617;
  4788. pcu_ack = 3;
  4789. break;
  4790. }
  4791. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4792. POSTING_READ(CDCLK_CTL);
  4793. /* inform PCU of the change */
  4794. mutex_lock(&dev_priv->rps.hw_lock);
  4795. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4796. mutex_unlock(&dev_priv->rps.hw_lock);
  4797. intel_update_cdclk(dev);
  4798. }
  4799. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4800. {
  4801. /* disable DBUF power */
  4802. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4803. POSTING_READ(DBUF_CTL);
  4804. udelay(10);
  4805. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4806. DRM_ERROR("DBuf power disable timeout\n");
  4807. /* disable DPLL0 */
  4808. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4809. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4810. DRM_ERROR("Couldn't disable DPLL0\n");
  4811. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4812. }
  4813. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4814. {
  4815. u32 val;
  4816. unsigned int required_vco;
  4817. /* enable PCH reset handshake */
  4818. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4819. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4820. /* enable PG1 and Misc I/O */
  4821. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4822. /* DPLL0 already enabed !? */
  4823. if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
  4824. DRM_DEBUG_DRIVER("DPLL0 already running\n");
  4825. return;
  4826. }
  4827. /* enable DPLL0 */
  4828. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4829. skl_dpll0_enable(dev_priv, required_vco);
  4830. /* set CDCLK to the frequency the BIOS chose */
  4831. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4832. /* enable DBUF power */
  4833. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4834. POSTING_READ(DBUF_CTL);
  4835. udelay(10);
  4836. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4837. DRM_ERROR("DBuf power enable timeout\n");
  4838. }
  4839. /* returns HPLL frequency in kHz */
  4840. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4841. {
  4842. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4843. /* Obtain SKU information */
  4844. mutex_lock(&dev_priv->sb_lock);
  4845. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4846. CCK_FUSE_HPLL_FREQ_MASK;
  4847. mutex_unlock(&dev_priv->sb_lock);
  4848. return vco_freq[hpll_freq] * 1000;
  4849. }
  4850. /* Adjust CDclk dividers to allow high res or save power if possible */
  4851. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4852. {
  4853. struct drm_i915_private *dev_priv = dev->dev_private;
  4854. u32 val, cmd;
  4855. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4856. != dev_priv->cdclk_freq);
  4857. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4858. cmd = 2;
  4859. else if (cdclk == 266667)
  4860. cmd = 1;
  4861. else
  4862. cmd = 0;
  4863. mutex_lock(&dev_priv->rps.hw_lock);
  4864. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4865. val &= ~DSPFREQGUAR_MASK;
  4866. val |= (cmd << DSPFREQGUAR_SHIFT);
  4867. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4868. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4869. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4870. 50)) {
  4871. DRM_ERROR("timed out waiting for CDclk change\n");
  4872. }
  4873. mutex_unlock(&dev_priv->rps.hw_lock);
  4874. mutex_lock(&dev_priv->sb_lock);
  4875. if (cdclk == 400000) {
  4876. u32 divider;
  4877. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4878. /* adjust cdclk divider */
  4879. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4880. val &= ~DISPLAY_FREQUENCY_VALUES;
  4881. val |= divider;
  4882. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4883. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4884. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4885. 50))
  4886. DRM_ERROR("timed out waiting for CDclk change\n");
  4887. }
  4888. /* adjust self-refresh exit latency value */
  4889. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4890. val &= ~0x7f;
  4891. /*
  4892. * For high bandwidth configs, we set a higher latency in the bunit
  4893. * so that the core display fetch happens in time to avoid underruns.
  4894. */
  4895. if (cdclk == 400000)
  4896. val |= 4500 / 250; /* 4.5 usec */
  4897. else
  4898. val |= 3000 / 250; /* 3.0 usec */
  4899. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4900. mutex_unlock(&dev_priv->sb_lock);
  4901. intel_update_cdclk(dev);
  4902. }
  4903. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4904. {
  4905. struct drm_i915_private *dev_priv = dev->dev_private;
  4906. u32 val, cmd;
  4907. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4908. != dev_priv->cdclk_freq);
  4909. switch (cdclk) {
  4910. case 333333:
  4911. case 320000:
  4912. case 266667:
  4913. case 200000:
  4914. break;
  4915. default:
  4916. MISSING_CASE(cdclk);
  4917. return;
  4918. }
  4919. /*
  4920. * Specs are full of misinformation, but testing on actual
  4921. * hardware has shown that we just need to write the desired
  4922. * CCK divider into the Punit register.
  4923. */
  4924. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4925. mutex_lock(&dev_priv->rps.hw_lock);
  4926. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4927. val &= ~DSPFREQGUAR_MASK_CHV;
  4928. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4929. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4930. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4931. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4932. 50)) {
  4933. DRM_ERROR("timed out waiting for CDclk change\n");
  4934. }
  4935. mutex_unlock(&dev_priv->rps.hw_lock);
  4936. intel_update_cdclk(dev);
  4937. }
  4938. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4939. int max_pixclk)
  4940. {
  4941. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4942. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4943. /*
  4944. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4945. * 200MHz
  4946. * 267MHz
  4947. * 320/333MHz (depends on HPLL freq)
  4948. * 400MHz (VLV only)
  4949. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4950. * of the lower bin and adjust if needed.
  4951. *
  4952. * We seem to get an unstable or solid color picture at 200MHz.
  4953. * Not sure what's wrong. For now use 200MHz only when all pipes
  4954. * are off.
  4955. */
  4956. if (!IS_CHERRYVIEW(dev_priv) &&
  4957. max_pixclk > freq_320*limit/100)
  4958. return 400000;
  4959. else if (max_pixclk > 266667*limit/100)
  4960. return freq_320;
  4961. else if (max_pixclk > 0)
  4962. return 266667;
  4963. else
  4964. return 200000;
  4965. }
  4966. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4967. int max_pixclk)
  4968. {
  4969. /*
  4970. * FIXME:
  4971. * - remove the guardband, it's not needed on BXT
  4972. * - set 19.2MHz bypass frequency if there are no active pipes
  4973. */
  4974. if (max_pixclk > 576000*9/10)
  4975. return 624000;
  4976. else if (max_pixclk > 384000*9/10)
  4977. return 576000;
  4978. else if (max_pixclk > 288000*9/10)
  4979. return 384000;
  4980. else if (max_pixclk > 144000*9/10)
  4981. return 288000;
  4982. else
  4983. return 144000;
  4984. }
  4985. /* Compute the max pixel clock for new configuration. Uses atomic state if
  4986. * that's non-NULL, look at current state otherwise. */
  4987. static int intel_mode_max_pixclk(struct drm_device *dev,
  4988. struct drm_atomic_state *state)
  4989. {
  4990. struct intel_crtc *intel_crtc;
  4991. struct intel_crtc_state *crtc_state;
  4992. int max_pixclk = 0;
  4993. for_each_intel_crtc(dev, intel_crtc) {
  4994. if (state)
  4995. crtc_state =
  4996. intel_atomic_get_crtc_state(state, intel_crtc);
  4997. else
  4998. crtc_state = intel_crtc->config;
  4999. if (IS_ERR(crtc_state))
  5000. return PTR_ERR(crtc_state);
  5001. if (!crtc_state->base.enable)
  5002. continue;
  5003. max_pixclk = max(max_pixclk,
  5004. crtc_state->base.adjusted_mode.crtc_clock);
  5005. }
  5006. return max_pixclk;
  5007. }
  5008. static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
  5009. {
  5010. struct drm_i915_private *dev_priv = to_i915(state->dev);
  5011. struct drm_crtc *crtc;
  5012. struct drm_crtc_state *crtc_state;
  5013. int max_pixclk = intel_mode_max_pixclk(state->dev, state);
  5014. int cdclk, ret = 0;
  5015. if (max_pixclk < 0)
  5016. return max_pixclk;
  5017. if (IS_VALLEYVIEW(dev_priv))
  5018. cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  5019. else
  5020. cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  5021. if (cdclk == dev_priv->cdclk_freq)
  5022. return 0;
  5023. /* add all active pipes to the state */
  5024. for_each_crtc(state->dev, crtc) {
  5025. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  5026. if (IS_ERR(crtc_state))
  5027. return PTR_ERR(crtc_state);
  5028. if (!crtc_state->active || needs_modeset(crtc_state))
  5029. continue;
  5030. crtc_state->mode_changed = true;
  5031. ret = drm_atomic_add_affected_connectors(state, crtc);
  5032. if (ret)
  5033. break;
  5034. ret = drm_atomic_add_affected_planes(state, crtc);
  5035. if (ret)
  5036. break;
  5037. }
  5038. return ret;
  5039. }
  5040. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5041. {
  5042. unsigned int credits, default_credits;
  5043. if (IS_CHERRYVIEW(dev_priv))
  5044. default_credits = PFI_CREDIT(12);
  5045. else
  5046. default_credits = PFI_CREDIT(8);
  5047. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  5048. /* CHV suggested value is 31 or 63 */
  5049. if (IS_CHERRYVIEW(dev_priv))
  5050. credits = PFI_CREDIT_31;
  5051. else
  5052. credits = PFI_CREDIT(15);
  5053. } else {
  5054. credits = default_credits;
  5055. }
  5056. /*
  5057. * WA - write default credits before re-programming
  5058. * FIXME: should we also set the resend bit here?
  5059. */
  5060. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5061. default_credits);
  5062. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5063. credits | PFI_CREDIT_RESEND);
  5064. /*
  5065. * FIXME is this guaranteed to clear
  5066. * immediately or should we poll for it?
  5067. */
  5068. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5069. }
  5070. static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
  5071. {
  5072. struct drm_device *dev = old_state->dev;
  5073. struct drm_i915_private *dev_priv = dev->dev_private;
  5074. int max_pixclk = intel_mode_max_pixclk(dev, NULL);
  5075. int req_cdclk;
  5076. /* The path in intel_mode_max_pixclk() with a NULL atomic state should
  5077. * never fail. */
  5078. if (WARN_ON(max_pixclk < 0))
  5079. return;
  5080. req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  5081. if (req_cdclk != dev_priv->cdclk_freq) {
  5082. /*
  5083. * FIXME: We can end up here with all power domains off, yet
  5084. * with a CDCLK frequency other than the minimum. To account
  5085. * for this take the PIPE-A power domain, which covers the HW
  5086. * blocks needed for the following programming. This can be
  5087. * removed once it's guaranteed that we get here either with
  5088. * the minimum CDCLK set, or the required power domains
  5089. * enabled.
  5090. */
  5091. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5092. if (IS_CHERRYVIEW(dev))
  5093. cherryview_set_cdclk(dev, req_cdclk);
  5094. else
  5095. valleyview_set_cdclk(dev, req_cdclk);
  5096. vlv_program_pfi_credits(dev_priv);
  5097. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5098. }
  5099. }
  5100. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5101. {
  5102. struct drm_device *dev = crtc->dev;
  5103. struct drm_i915_private *dev_priv = to_i915(dev);
  5104. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5105. struct intel_encoder *encoder;
  5106. int pipe = intel_crtc->pipe;
  5107. bool is_dsi;
  5108. if (WARN_ON(intel_crtc->active))
  5109. return;
  5110. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5111. if (!is_dsi) {
  5112. if (IS_CHERRYVIEW(dev))
  5113. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5114. else
  5115. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5116. }
  5117. if (intel_crtc->config->has_dp_encoder)
  5118. intel_dp_set_m_n(intel_crtc, M1_N1);
  5119. intel_set_pipe_timings(intel_crtc);
  5120. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5121. struct drm_i915_private *dev_priv = dev->dev_private;
  5122. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5123. I915_WRITE(CHV_CANVAS(pipe), 0);
  5124. }
  5125. i9xx_set_pipeconf(intel_crtc);
  5126. intel_crtc->active = true;
  5127. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5128. for_each_encoder_on_crtc(dev, crtc, encoder)
  5129. if (encoder->pre_pll_enable)
  5130. encoder->pre_pll_enable(encoder);
  5131. if (!is_dsi) {
  5132. if (IS_CHERRYVIEW(dev))
  5133. chv_enable_pll(intel_crtc, intel_crtc->config);
  5134. else
  5135. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5136. }
  5137. for_each_encoder_on_crtc(dev, crtc, encoder)
  5138. if (encoder->pre_enable)
  5139. encoder->pre_enable(encoder);
  5140. i9xx_pfit_enable(intel_crtc);
  5141. intel_crtc_load_lut(crtc);
  5142. intel_update_watermarks(crtc);
  5143. intel_enable_pipe(intel_crtc);
  5144. assert_vblank_disabled(crtc);
  5145. drm_crtc_vblank_on(crtc);
  5146. for_each_encoder_on_crtc(dev, crtc, encoder)
  5147. encoder->enable(encoder);
  5148. }
  5149. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5150. {
  5151. struct drm_device *dev = crtc->base.dev;
  5152. struct drm_i915_private *dev_priv = dev->dev_private;
  5153. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5154. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5155. }
  5156. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5157. {
  5158. struct drm_device *dev = crtc->dev;
  5159. struct drm_i915_private *dev_priv = to_i915(dev);
  5160. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5161. struct intel_encoder *encoder;
  5162. int pipe = intel_crtc->pipe;
  5163. if (WARN_ON(intel_crtc->active))
  5164. return;
  5165. i9xx_set_pll_dividers(intel_crtc);
  5166. if (intel_crtc->config->has_dp_encoder)
  5167. intel_dp_set_m_n(intel_crtc, M1_N1);
  5168. intel_set_pipe_timings(intel_crtc);
  5169. i9xx_set_pipeconf(intel_crtc);
  5170. intel_crtc->active = true;
  5171. if (!IS_GEN2(dev))
  5172. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5173. for_each_encoder_on_crtc(dev, crtc, encoder)
  5174. if (encoder->pre_enable)
  5175. encoder->pre_enable(encoder);
  5176. i9xx_enable_pll(intel_crtc);
  5177. i9xx_pfit_enable(intel_crtc);
  5178. intel_crtc_load_lut(crtc);
  5179. intel_update_watermarks(crtc);
  5180. intel_enable_pipe(intel_crtc);
  5181. assert_vblank_disabled(crtc);
  5182. drm_crtc_vblank_on(crtc);
  5183. for_each_encoder_on_crtc(dev, crtc, encoder)
  5184. encoder->enable(encoder);
  5185. }
  5186. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5187. {
  5188. struct drm_device *dev = crtc->base.dev;
  5189. struct drm_i915_private *dev_priv = dev->dev_private;
  5190. if (!crtc->config->gmch_pfit.control)
  5191. return;
  5192. assert_pipe_disabled(dev_priv, crtc->pipe);
  5193. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5194. I915_READ(PFIT_CONTROL));
  5195. I915_WRITE(PFIT_CONTROL, 0);
  5196. }
  5197. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5198. {
  5199. struct drm_device *dev = crtc->dev;
  5200. struct drm_i915_private *dev_priv = dev->dev_private;
  5201. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5202. struct intel_encoder *encoder;
  5203. int pipe = intel_crtc->pipe;
  5204. if (WARN_ON(!intel_crtc->active))
  5205. return;
  5206. /*
  5207. * On gen2 planes are double buffered but the pipe isn't, so we must
  5208. * wait for planes to fully turn off before disabling the pipe.
  5209. * We also need to wait on all gmch platforms because of the
  5210. * self-refresh mode constraint explained above.
  5211. */
  5212. intel_wait_for_vblank(dev, pipe);
  5213. for_each_encoder_on_crtc(dev, crtc, encoder)
  5214. encoder->disable(encoder);
  5215. drm_crtc_vblank_off(crtc);
  5216. assert_vblank_disabled(crtc);
  5217. intel_disable_pipe(intel_crtc);
  5218. i9xx_pfit_disable(intel_crtc);
  5219. for_each_encoder_on_crtc(dev, crtc, encoder)
  5220. if (encoder->post_disable)
  5221. encoder->post_disable(encoder);
  5222. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5223. if (IS_CHERRYVIEW(dev))
  5224. chv_disable_pll(dev_priv, pipe);
  5225. else if (IS_VALLEYVIEW(dev))
  5226. vlv_disable_pll(dev_priv, pipe);
  5227. else
  5228. i9xx_disable_pll(intel_crtc);
  5229. }
  5230. if (!IS_GEN2(dev))
  5231. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5232. intel_crtc->active = false;
  5233. intel_update_watermarks(crtc);
  5234. mutex_lock(&dev->struct_mutex);
  5235. intel_fbc_update(dev);
  5236. mutex_unlock(&dev->struct_mutex);
  5237. }
  5238. /*
  5239. * turn all crtc's off, but do not adjust state
  5240. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5241. */
  5242. int intel_display_suspend(struct drm_device *dev)
  5243. {
  5244. struct drm_mode_config *config = &dev->mode_config;
  5245. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5246. struct drm_atomic_state *state;
  5247. struct drm_crtc *crtc;
  5248. unsigned crtc_mask = 0;
  5249. int ret = 0;
  5250. if (WARN_ON(!ctx))
  5251. return 0;
  5252. lockdep_assert_held(&ctx->ww_ctx);
  5253. state = drm_atomic_state_alloc(dev);
  5254. if (WARN_ON(!state))
  5255. return -ENOMEM;
  5256. state->acquire_ctx = ctx;
  5257. state->allow_modeset = true;
  5258. for_each_crtc(dev, crtc) {
  5259. struct drm_crtc_state *crtc_state =
  5260. drm_atomic_get_crtc_state(state, crtc);
  5261. ret = PTR_ERR_OR_ZERO(crtc_state);
  5262. if (ret)
  5263. goto free;
  5264. if (!crtc_state->active)
  5265. continue;
  5266. crtc_state->active = false;
  5267. crtc_mask |= 1 << drm_crtc_index(crtc);
  5268. }
  5269. if (crtc_mask) {
  5270. ret = intel_set_mode(state);
  5271. if (!ret) {
  5272. for_each_crtc(dev, crtc)
  5273. if (crtc_mask & (1 << drm_crtc_index(crtc)))
  5274. crtc->state->active = true;
  5275. return ret;
  5276. }
  5277. }
  5278. free:
  5279. if (ret)
  5280. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5281. drm_atomic_state_free(state);
  5282. return ret;
  5283. }
  5284. /* Master function to enable/disable CRTC and corresponding power wells */
  5285. int intel_crtc_control(struct drm_crtc *crtc, bool enable)
  5286. {
  5287. struct drm_device *dev = crtc->dev;
  5288. struct drm_mode_config *config = &dev->mode_config;
  5289. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5290. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5291. struct intel_crtc_state *pipe_config;
  5292. struct drm_atomic_state *state;
  5293. int ret;
  5294. if (enable == intel_crtc->active)
  5295. return 0;
  5296. if (enable && !crtc->state->enable)
  5297. return 0;
  5298. /* this function should be called with drm_modeset_lock_all for now */
  5299. if (WARN_ON(!ctx))
  5300. return -EIO;
  5301. lockdep_assert_held(&ctx->ww_ctx);
  5302. state = drm_atomic_state_alloc(dev);
  5303. if (WARN_ON(!state))
  5304. return -ENOMEM;
  5305. state->acquire_ctx = ctx;
  5306. state->allow_modeset = true;
  5307. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  5308. if (IS_ERR(pipe_config)) {
  5309. ret = PTR_ERR(pipe_config);
  5310. goto err;
  5311. }
  5312. pipe_config->base.active = enable;
  5313. ret = intel_set_mode(state);
  5314. if (!ret)
  5315. return ret;
  5316. err:
  5317. DRM_ERROR("Updating crtc active failed with %i\n", ret);
  5318. drm_atomic_state_free(state);
  5319. return ret;
  5320. }
  5321. /**
  5322. * Sets the power management mode of the pipe and plane.
  5323. */
  5324. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  5325. {
  5326. struct drm_device *dev = crtc->dev;
  5327. struct intel_encoder *intel_encoder;
  5328. bool enable = false;
  5329. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  5330. enable |= intel_encoder->connectors_active;
  5331. intel_crtc_control(crtc, enable);
  5332. }
  5333. void intel_encoder_destroy(struct drm_encoder *encoder)
  5334. {
  5335. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5336. drm_encoder_cleanup(encoder);
  5337. kfree(intel_encoder);
  5338. }
  5339. /* Simple dpms helper for encoders with just one connector, no cloning and only
  5340. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  5341. * state of the entire output pipe. */
  5342. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  5343. {
  5344. if (mode == DRM_MODE_DPMS_ON) {
  5345. encoder->connectors_active = true;
  5346. intel_crtc_update_dpms(encoder->base.crtc);
  5347. } else {
  5348. encoder->connectors_active = false;
  5349. intel_crtc_update_dpms(encoder->base.crtc);
  5350. }
  5351. }
  5352. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5353. * internal consistency). */
  5354. static void intel_connector_check_state(struct intel_connector *connector)
  5355. {
  5356. if (connector->get_hw_state(connector)) {
  5357. struct intel_encoder *encoder = connector->encoder;
  5358. struct drm_crtc *crtc;
  5359. bool encoder_enabled;
  5360. enum pipe pipe;
  5361. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5362. connector->base.base.id,
  5363. connector->base.name);
  5364. /* there is no real hw state for MST connectors */
  5365. if (connector->mst_port)
  5366. return;
  5367. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  5368. "wrong connector dpms state\n");
  5369. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  5370. "active connector not linked to encoder\n");
  5371. if (encoder) {
  5372. I915_STATE_WARN(!encoder->connectors_active,
  5373. "encoder->connectors_active not set\n");
  5374. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  5375. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  5376. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  5377. return;
  5378. crtc = encoder->base.crtc;
  5379. I915_STATE_WARN(!crtc->state->enable,
  5380. "crtc not enabled\n");
  5381. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  5382. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  5383. "encoder active on the wrong pipe\n");
  5384. }
  5385. }
  5386. }
  5387. int intel_connector_init(struct intel_connector *connector)
  5388. {
  5389. struct drm_connector_state *connector_state;
  5390. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5391. if (!connector_state)
  5392. return -ENOMEM;
  5393. connector->base.state = connector_state;
  5394. return 0;
  5395. }
  5396. struct intel_connector *intel_connector_alloc(void)
  5397. {
  5398. struct intel_connector *connector;
  5399. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5400. if (!connector)
  5401. return NULL;
  5402. if (intel_connector_init(connector) < 0) {
  5403. kfree(connector);
  5404. return NULL;
  5405. }
  5406. return connector;
  5407. }
  5408. /* Even simpler default implementation, if there's really no special case to
  5409. * consider. */
  5410. void intel_connector_dpms(struct drm_connector *connector, int mode)
  5411. {
  5412. /* All the simple cases only support two dpms states. */
  5413. if (mode != DRM_MODE_DPMS_ON)
  5414. mode = DRM_MODE_DPMS_OFF;
  5415. if (mode == connector->dpms)
  5416. return;
  5417. connector->dpms = mode;
  5418. /* Only need to change hw state when actually enabled */
  5419. if (connector->encoder)
  5420. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  5421. intel_modeset_check_state(connector->dev);
  5422. }
  5423. /* Simple connector->get_hw_state implementation for encoders that support only
  5424. * one connector and no cloning and hence the encoder state determines the state
  5425. * of the connector. */
  5426. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5427. {
  5428. enum pipe pipe = 0;
  5429. struct intel_encoder *encoder = connector->encoder;
  5430. return encoder->get_hw_state(encoder, &pipe);
  5431. }
  5432. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5433. {
  5434. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5435. return crtc_state->fdi_lanes;
  5436. return 0;
  5437. }
  5438. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5439. struct intel_crtc_state *pipe_config)
  5440. {
  5441. struct drm_atomic_state *state = pipe_config->base.state;
  5442. struct intel_crtc *other_crtc;
  5443. struct intel_crtc_state *other_crtc_state;
  5444. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5445. pipe_name(pipe), pipe_config->fdi_lanes);
  5446. if (pipe_config->fdi_lanes > 4) {
  5447. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5448. pipe_name(pipe), pipe_config->fdi_lanes);
  5449. return -EINVAL;
  5450. }
  5451. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5452. if (pipe_config->fdi_lanes > 2) {
  5453. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5454. pipe_config->fdi_lanes);
  5455. return -EINVAL;
  5456. } else {
  5457. return 0;
  5458. }
  5459. }
  5460. if (INTEL_INFO(dev)->num_pipes == 2)
  5461. return 0;
  5462. /* Ivybridge 3 pipe is really complicated */
  5463. switch (pipe) {
  5464. case PIPE_A:
  5465. return 0;
  5466. case PIPE_B:
  5467. if (pipe_config->fdi_lanes <= 2)
  5468. return 0;
  5469. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5470. other_crtc_state =
  5471. intel_atomic_get_crtc_state(state, other_crtc);
  5472. if (IS_ERR(other_crtc_state))
  5473. return PTR_ERR(other_crtc_state);
  5474. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5475. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5476. pipe_name(pipe), pipe_config->fdi_lanes);
  5477. return -EINVAL;
  5478. }
  5479. return 0;
  5480. case PIPE_C:
  5481. if (pipe_config->fdi_lanes > 2) {
  5482. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5483. pipe_name(pipe), pipe_config->fdi_lanes);
  5484. return -EINVAL;
  5485. }
  5486. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5487. other_crtc_state =
  5488. intel_atomic_get_crtc_state(state, other_crtc);
  5489. if (IS_ERR(other_crtc_state))
  5490. return PTR_ERR(other_crtc_state);
  5491. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5492. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5493. return -EINVAL;
  5494. }
  5495. return 0;
  5496. default:
  5497. BUG();
  5498. }
  5499. }
  5500. #define RETRY 1
  5501. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5502. struct intel_crtc_state *pipe_config)
  5503. {
  5504. struct drm_device *dev = intel_crtc->base.dev;
  5505. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5506. int lane, link_bw, fdi_dotclock, ret;
  5507. bool needs_recompute = false;
  5508. retry:
  5509. /* FDI is a binary signal running at ~2.7GHz, encoding
  5510. * each output octet as 10 bits. The actual frequency
  5511. * is stored as a divider into a 100MHz clock, and the
  5512. * mode pixel clock is stored in units of 1KHz.
  5513. * Hence the bw of each lane in terms of the mode signal
  5514. * is:
  5515. */
  5516. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5517. fdi_dotclock = adjusted_mode->crtc_clock;
  5518. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5519. pipe_config->pipe_bpp);
  5520. pipe_config->fdi_lanes = lane;
  5521. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5522. link_bw, &pipe_config->fdi_m_n);
  5523. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5524. intel_crtc->pipe, pipe_config);
  5525. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5526. pipe_config->pipe_bpp -= 2*3;
  5527. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5528. pipe_config->pipe_bpp);
  5529. needs_recompute = true;
  5530. pipe_config->bw_constrained = true;
  5531. goto retry;
  5532. }
  5533. if (needs_recompute)
  5534. return RETRY;
  5535. return ret;
  5536. }
  5537. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5538. struct intel_crtc_state *pipe_config)
  5539. {
  5540. if (pipe_config->pipe_bpp > 24)
  5541. return false;
  5542. /* HSW can handle pixel rate up to cdclk? */
  5543. if (IS_HASWELL(dev_priv->dev))
  5544. return true;
  5545. /*
  5546. * We compare against max which means we must take
  5547. * the increased cdclk requirement into account when
  5548. * calculating the new cdclk.
  5549. *
  5550. * Should measure whether using a lower cdclk w/o IPS
  5551. */
  5552. return ilk_pipe_pixel_rate(pipe_config) <=
  5553. dev_priv->max_cdclk_freq * 95 / 100;
  5554. }
  5555. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5556. struct intel_crtc_state *pipe_config)
  5557. {
  5558. struct drm_device *dev = crtc->base.dev;
  5559. struct drm_i915_private *dev_priv = dev->dev_private;
  5560. pipe_config->ips_enabled = i915.enable_ips &&
  5561. hsw_crtc_supports_ips(crtc) &&
  5562. pipe_config_supports_ips(dev_priv, pipe_config);
  5563. }
  5564. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5565. struct intel_crtc_state *pipe_config)
  5566. {
  5567. struct drm_device *dev = crtc->base.dev;
  5568. struct drm_i915_private *dev_priv = dev->dev_private;
  5569. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5570. int ret;
  5571. /* FIXME should check pixel clock limits on all platforms */
  5572. if (INTEL_INFO(dev)->gen < 4) {
  5573. int clock_limit = dev_priv->max_cdclk_freq;
  5574. /*
  5575. * Enable pixel doubling when the dot clock
  5576. * is > 90% of the (display) core speed.
  5577. *
  5578. * GDG double wide on either pipe,
  5579. * otherwise pipe A only.
  5580. */
  5581. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5582. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5583. clock_limit *= 2;
  5584. pipe_config->double_wide = true;
  5585. }
  5586. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5587. return -EINVAL;
  5588. }
  5589. /*
  5590. * Pipe horizontal size must be even in:
  5591. * - DVO ganged mode
  5592. * - LVDS dual channel mode
  5593. * - Double wide pipe
  5594. */
  5595. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5596. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5597. pipe_config->pipe_src_w &= ~1;
  5598. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5599. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5600. */
  5601. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5602. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5603. return -EINVAL;
  5604. if (HAS_IPS(dev))
  5605. hsw_compute_ips_config(crtc, pipe_config);
  5606. if (pipe_config->has_pch_encoder)
  5607. return ironlake_fdi_compute_config(crtc, pipe_config);
  5608. /* FIXME: remove below call once atomic mode set is place and all crtc
  5609. * related checks called from atomic_crtc_check function */
  5610. ret = 0;
  5611. DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
  5612. crtc, pipe_config->base.state);
  5613. ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
  5614. return ret;
  5615. }
  5616. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5617. {
  5618. struct drm_i915_private *dev_priv = to_i915(dev);
  5619. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5620. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5621. uint32_t linkrate;
  5622. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5623. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5624. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5625. return 540000;
  5626. linkrate = (I915_READ(DPLL_CTRL1) &
  5627. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5628. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5629. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5630. /* vco 8640 */
  5631. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5632. case CDCLK_FREQ_450_432:
  5633. return 432000;
  5634. case CDCLK_FREQ_337_308:
  5635. return 308570;
  5636. case CDCLK_FREQ_675_617:
  5637. return 617140;
  5638. default:
  5639. WARN(1, "Unknown cd freq selection\n");
  5640. }
  5641. } else {
  5642. /* vco 8100 */
  5643. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5644. case CDCLK_FREQ_450_432:
  5645. return 450000;
  5646. case CDCLK_FREQ_337_308:
  5647. return 337500;
  5648. case CDCLK_FREQ_675_617:
  5649. return 675000;
  5650. default:
  5651. WARN(1, "Unknown cd freq selection\n");
  5652. }
  5653. }
  5654. /* error case, do as if DPLL0 isn't enabled */
  5655. return 24000;
  5656. }
  5657. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5658. {
  5659. struct drm_i915_private *dev_priv = dev->dev_private;
  5660. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5661. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5662. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5663. return 800000;
  5664. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5665. return 450000;
  5666. else if (freq == LCPLL_CLK_FREQ_450)
  5667. return 450000;
  5668. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5669. return 540000;
  5670. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5671. return 337500;
  5672. else
  5673. return 675000;
  5674. }
  5675. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5676. {
  5677. struct drm_i915_private *dev_priv = dev->dev_private;
  5678. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5679. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5680. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5681. return 800000;
  5682. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5683. return 450000;
  5684. else if (freq == LCPLL_CLK_FREQ_450)
  5685. return 450000;
  5686. else if (IS_HSW_ULT(dev))
  5687. return 337500;
  5688. else
  5689. return 540000;
  5690. }
  5691. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5692. {
  5693. struct drm_i915_private *dev_priv = dev->dev_private;
  5694. u32 val;
  5695. int divider;
  5696. if (dev_priv->hpll_freq == 0)
  5697. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5698. mutex_lock(&dev_priv->sb_lock);
  5699. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5700. mutex_unlock(&dev_priv->sb_lock);
  5701. divider = val & DISPLAY_FREQUENCY_VALUES;
  5702. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5703. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5704. "cdclk change in progress\n");
  5705. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5706. }
  5707. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5708. {
  5709. return 450000;
  5710. }
  5711. static int i945_get_display_clock_speed(struct drm_device *dev)
  5712. {
  5713. return 400000;
  5714. }
  5715. static int i915_get_display_clock_speed(struct drm_device *dev)
  5716. {
  5717. return 333333;
  5718. }
  5719. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5720. {
  5721. return 200000;
  5722. }
  5723. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5724. {
  5725. u16 gcfgc = 0;
  5726. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5727. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5728. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5729. return 266667;
  5730. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5731. return 333333;
  5732. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5733. return 444444;
  5734. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5735. return 200000;
  5736. default:
  5737. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5738. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5739. return 133333;
  5740. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5741. return 166667;
  5742. }
  5743. }
  5744. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5745. {
  5746. u16 gcfgc = 0;
  5747. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5748. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5749. return 133333;
  5750. else {
  5751. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5752. case GC_DISPLAY_CLOCK_333_MHZ:
  5753. return 333333;
  5754. default:
  5755. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5756. return 190000;
  5757. }
  5758. }
  5759. }
  5760. static int i865_get_display_clock_speed(struct drm_device *dev)
  5761. {
  5762. return 266667;
  5763. }
  5764. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5765. {
  5766. u16 hpllcc = 0;
  5767. /*
  5768. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5769. * encoding is different :(
  5770. * FIXME is this the right way to detect 852GM/852GMV?
  5771. */
  5772. if (dev->pdev->revision == 0x1)
  5773. return 133333;
  5774. pci_bus_read_config_word(dev->pdev->bus,
  5775. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5776. /* Assume that the hardware is in the high speed state. This
  5777. * should be the default.
  5778. */
  5779. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5780. case GC_CLOCK_133_200:
  5781. case GC_CLOCK_133_200_2:
  5782. case GC_CLOCK_100_200:
  5783. return 200000;
  5784. case GC_CLOCK_166_250:
  5785. return 250000;
  5786. case GC_CLOCK_100_133:
  5787. return 133333;
  5788. case GC_CLOCK_133_266:
  5789. case GC_CLOCK_133_266_2:
  5790. case GC_CLOCK_166_266:
  5791. return 266667;
  5792. }
  5793. /* Shouldn't happen */
  5794. return 0;
  5795. }
  5796. static int i830_get_display_clock_speed(struct drm_device *dev)
  5797. {
  5798. return 133333;
  5799. }
  5800. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5801. {
  5802. struct drm_i915_private *dev_priv = dev->dev_private;
  5803. static const unsigned int blb_vco[8] = {
  5804. [0] = 3200000,
  5805. [1] = 4000000,
  5806. [2] = 5333333,
  5807. [3] = 4800000,
  5808. [4] = 6400000,
  5809. };
  5810. static const unsigned int pnv_vco[8] = {
  5811. [0] = 3200000,
  5812. [1] = 4000000,
  5813. [2] = 5333333,
  5814. [3] = 4800000,
  5815. [4] = 2666667,
  5816. };
  5817. static const unsigned int cl_vco[8] = {
  5818. [0] = 3200000,
  5819. [1] = 4000000,
  5820. [2] = 5333333,
  5821. [3] = 6400000,
  5822. [4] = 3333333,
  5823. [5] = 3566667,
  5824. [6] = 4266667,
  5825. };
  5826. static const unsigned int elk_vco[8] = {
  5827. [0] = 3200000,
  5828. [1] = 4000000,
  5829. [2] = 5333333,
  5830. [3] = 4800000,
  5831. };
  5832. static const unsigned int ctg_vco[8] = {
  5833. [0] = 3200000,
  5834. [1] = 4000000,
  5835. [2] = 5333333,
  5836. [3] = 6400000,
  5837. [4] = 2666667,
  5838. [5] = 4266667,
  5839. };
  5840. const unsigned int *vco_table;
  5841. unsigned int vco;
  5842. uint8_t tmp = 0;
  5843. /* FIXME other chipsets? */
  5844. if (IS_GM45(dev))
  5845. vco_table = ctg_vco;
  5846. else if (IS_G4X(dev))
  5847. vco_table = elk_vco;
  5848. else if (IS_CRESTLINE(dev))
  5849. vco_table = cl_vco;
  5850. else if (IS_PINEVIEW(dev))
  5851. vco_table = pnv_vco;
  5852. else if (IS_G33(dev))
  5853. vco_table = blb_vco;
  5854. else
  5855. return 0;
  5856. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5857. vco = vco_table[tmp & 0x7];
  5858. if (vco == 0)
  5859. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5860. else
  5861. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5862. return vco;
  5863. }
  5864. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5865. {
  5866. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5867. uint16_t tmp = 0;
  5868. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5869. cdclk_sel = (tmp >> 12) & 0x1;
  5870. switch (vco) {
  5871. case 2666667:
  5872. case 4000000:
  5873. case 5333333:
  5874. return cdclk_sel ? 333333 : 222222;
  5875. case 3200000:
  5876. return cdclk_sel ? 320000 : 228571;
  5877. default:
  5878. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5879. return 222222;
  5880. }
  5881. }
  5882. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5883. {
  5884. static const uint8_t div_3200[] = { 16, 10, 8 };
  5885. static const uint8_t div_4000[] = { 20, 12, 10 };
  5886. static const uint8_t div_5333[] = { 24, 16, 14 };
  5887. const uint8_t *div_table;
  5888. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5889. uint16_t tmp = 0;
  5890. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5891. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5892. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5893. goto fail;
  5894. switch (vco) {
  5895. case 3200000:
  5896. div_table = div_3200;
  5897. break;
  5898. case 4000000:
  5899. div_table = div_4000;
  5900. break;
  5901. case 5333333:
  5902. div_table = div_5333;
  5903. break;
  5904. default:
  5905. goto fail;
  5906. }
  5907. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5908. fail:
  5909. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5910. return 200000;
  5911. }
  5912. static int g33_get_display_clock_speed(struct drm_device *dev)
  5913. {
  5914. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5915. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5916. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5917. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5918. const uint8_t *div_table;
  5919. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5920. uint16_t tmp = 0;
  5921. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5922. cdclk_sel = (tmp >> 4) & 0x7;
  5923. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5924. goto fail;
  5925. switch (vco) {
  5926. case 3200000:
  5927. div_table = div_3200;
  5928. break;
  5929. case 4000000:
  5930. div_table = div_4000;
  5931. break;
  5932. case 4800000:
  5933. div_table = div_4800;
  5934. break;
  5935. case 5333333:
  5936. div_table = div_5333;
  5937. break;
  5938. default:
  5939. goto fail;
  5940. }
  5941. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5942. fail:
  5943. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5944. return 190476;
  5945. }
  5946. static void
  5947. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5948. {
  5949. while (*num > DATA_LINK_M_N_MASK ||
  5950. *den > DATA_LINK_M_N_MASK) {
  5951. *num >>= 1;
  5952. *den >>= 1;
  5953. }
  5954. }
  5955. static void compute_m_n(unsigned int m, unsigned int n,
  5956. uint32_t *ret_m, uint32_t *ret_n)
  5957. {
  5958. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5959. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5960. intel_reduce_m_n_ratio(ret_m, ret_n);
  5961. }
  5962. void
  5963. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5964. int pixel_clock, int link_clock,
  5965. struct intel_link_m_n *m_n)
  5966. {
  5967. m_n->tu = 64;
  5968. compute_m_n(bits_per_pixel * pixel_clock,
  5969. link_clock * nlanes * 8,
  5970. &m_n->gmch_m, &m_n->gmch_n);
  5971. compute_m_n(pixel_clock, link_clock,
  5972. &m_n->link_m, &m_n->link_n);
  5973. }
  5974. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5975. {
  5976. if (i915.panel_use_ssc >= 0)
  5977. return i915.panel_use_ssc != 0;
  5978. return dev_priv->vbt.lvds_use_ssc
  5979. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5980. }
  5981. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5982. int num_connectors)
  5983. {
  5984. struct drm_device *dev = crtc_state->base.crtc->dev;
  5985. struct drm_i915_private *dev_priv = dev->dev_private;
  5986. int refclk;
  5987. WARN_ON(!crtc_state->base.state);
  5988. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5989. refclk = 100000;
  5990. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5991. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5992. refclk = dev_priv->vbt.lvds_ssc_freq;
  5993. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5994. } else if (!IS_GEN2(dev)) {
  5995. refclk = 96000;
  5996. } else {
  5997. refclk = 48000;
  5998. }
  5999. return refclk;
  6000. }
  6001. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6002. {
  6003. return (1 << dpll->n) << 16 | dpll->m2;
  6004. }
  6005. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6006. {
  6007. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6008. }
  6009. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6010. struct intel_crtc_state *crtc_state,
  6011. intel_clock_t *reduced_clock)
  6012. {
  6013. struct drm_device *dev = crtc->base.dev;
  6014. u32 fp, fp2 = 0;
  6015. if (IS_PINEVIEW(dev)) {
  6016. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6017. if (reduced_clock)
  6018. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6019. } else {
  6020. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6021. if (reduced_clock)
  6022. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6023. }
  6024. crtc_state->dpll_hw_state.fp0 = fp;
  6025. crtc->lowfreq_avail = false;
  6026. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6027. reduced_clock) {
  6028. crtc_state->dpll_hw_state.fp1 = fp2;
  6029. crtc->lowfreq_avail = true;
  6030. } else {
  6031. crtc_state->dpll_hw_state.fp1 = fp;
  6032. }
  6033. }
  6034. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6035. pipe)
  6036. {
  6037. u32 reg_val;
  6038. /*
  6039. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6040. * and set it to a reasonable value instead.
  6041. */
  6042. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6043. reg_val &= 0xffffff00;
  6044. reg_val |= 0x00000030;
  6045. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6046. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6047. reg_val &= 0x8cffffff;
  6048. reg_val = 0x8c000000;
  6049. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6050. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6051. reg_val &= 0xffffff00;
  6052. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6053. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6054. reg_val &= 0x00ffffff;
  6055. reg_val |= 0xb0000000;
  6056. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6057. }
  6058. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6059. struct intel_link_m_n *m_n)
  6060. {
  6061. struct drm_device *dev = crtc->base.dev;
  6062. struct drm_i915_private *dev_priv = dev->dev_private;
  6063. int pipe = crtc->pipe;
  6064. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6065. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6066. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6067. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6068. }
  6069. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6070. struct intel_link_m_n *m_n,
  6071. struct intel_link_m_n *m2_n2)
  6072. {
  6073. struct drm_device *dev = crtc->base.dev;
  6074. struct drm_i915_private *dev_priv = dev->dev_private;
  6075. int pipe = crtc->pipe;
  6076. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6077. if (INTEL_INFO(dev)->gen >= 5) {
  6078. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6079. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6080. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6081. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6082. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6083. * for gen < 8) and if DRRS is supported (to make sure the
  6084. * registers are not unnecessarily accessed).
  6085. */
  6086. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6087. crtc->config->has_drrs) {
  6088. I915_WRITE(PIPE_DATA_M2(transcoder),
  6089. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6090. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6091. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6092. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6093. }
  6094. } else {
  6095. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6096. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6097. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6098. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6099. }
  6100. }
  6101. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6102. {
  6103. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6104. if (m_n == M1_N1) {
  6105. dp_m_n = &crtc->config->dp_m_n;
  6106. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6107. } else if (m_n == M2_N2) {
  6108. /*
  6109. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6110. * needs to be programmed into M1_N1.
  6111. */
  6112. dp_m_n = &crtc->config->dp_m2_n2;
  6113. } else {
  6114. DRM_ERROR("Unsupported divider value\n");
  6115. return;
  6116. }
  6117. if (crtc->config->has_pch_encoder)
  6118. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6119. else
  6120. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6121. }
  6122. static void vlv_update_pll(struct intel_crtc *crtc,
  6123. struct intel_crtc_state *pipe_config)
  6124. {
  6125. u32 dpll, dpll_md;
  6126. /*
  6127. * Enable DPIO clock input. We should never disable the reference
  6128. * clock for pipe B, since VGA hotplug / manual detection depends
  6129. * on it.
  6130. */
  6131. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  6132. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  6133. /* We should never disable this, set it here for state tracking */
  6134. if (crtc->pipe == PIPE_B)
  6135. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6136. dpll |= DPLL_VCO_ENABLE;
  6137. pipe_config->dpll_hw_state.dpll = dpll;
  6138. dpll_md = (pipe_config->pixel_multiplier - 1)
  6139. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6140. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6141. }
  6142. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6143. const struct intel_crtc_state *pipe_config)
  6144. {
  6145. struct drm_device *dev = crtc->base.dev;
  6146. struct drm_i915_private *dev_priv = dev->dev_private;
  6147. int pipe = crtc->pipe;
  6148. u32 mdiv;
  6149. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6150. u32 coreclk, reg_val;
  6151. mutex_lock(&dev_priv->sb_lock);
  6152. bestn = pipe_config->dpll.n;
  6153. bestm1 = pipe_config->dpll.m1;
  6154. bestm2 = pipe_config->dpll.m2;
  6155. bestp1 = pipe_config->dpll.p1;
  6156. bestp2 = pipe_config->dpll.p2;
  6157. /* See eDP HDMI DPIO driver vbios notes doc */
  6158. /* PLL B needs special handling */
  6159. if (pipe == PIPE_B)
  6160. vlv_pllb_recal_opamp(dev_priv, pipe);
  6161. /* Set up Tx target for periodic Rcomp update */
  6162. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6163. /* Disable target IRef on PLL */
  6164. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6165. reg_val &= 0x00ffffff;
  6166. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6167. /* Disable fast lock */
  6168. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6169. /* Set idtafcrecal before PLL is enabled */
  6170. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6171. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6172. mdiv |= ((bestn << DPIO_N_SHIFT));
  6173. mdiv |= (1 << DPIO_K_SHIFT);
  6174. /*
  6175. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6176. * but we don't support that).
  6177. * Note: don't use the DAC post divider as it seems unstable.
  6178. */
  6179. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6180. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6181. mdiv |= DPIO_ENABLE_CALIBRATION;
  6182. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6183. /* Set HBR and RBR LPF coefficients */
  6184. if (pipe_config->port_clock == 162000 ||
  6185. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6186. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6187. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6188. 0x009f0003);
  6189. else
  6190. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6191. 0x00d0000f);
  6192. if (pipe_config->has_dp_encoder) {
  6193. /* Use SSC source */
  6194. if (pipe == PIPE_A)
  6195. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6196. 0x0df40000);
  6197. else
  6198. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6199. 0x0df70000);
  6200. } else { /* HDMI or VGA */
  6201. /* Use bend source */
  6202. if (pipe == PIPE_A)
  6203. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6204. 0x0df70000);
  6205. else
  6206. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6207. 0x0df40000);
  6208. }
  6209. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6210. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6211. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6212. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6213. coreclk |= 0x01000000;
  6214. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6215. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6216. mutex_unlock(&dev_priv->sb_lock);
  6217. }
  6218. static void chv_update_pll(struct intel_crtc *crtc,
  6219. struct intel_crtc_state *pipe_config)
  6220. {
  6221. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  6222. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6223. DPLL_VCO_ENABLE;
  6224. if (crtc->pipe != PIPE_A)
  6225. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6226. pipe_config->dpll_hw_state.dpll_md =
  6227. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6228. }
  6229. static void chv_prepare_pll(struct intel_crtc *crtc,
  6230. const struct intel_crtc_state *pipe_config)
  6231. {
  6232. struct drm_device *dev = crtc->base.dev;
  6233. struct drm_i915_private *dev_priv = dev->dev_private;
  6234. int pipe = crtc->pipe;
  6235. int dpll_reg = DPLL(crtc->pipe);
  6236. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6237. u32 loopfilter, tribuf_calcntr;
  6238. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6239. u32 dpio_val;
  6240. int vco;
  6241. bestn = pipe_config->dpll.n;
  6242. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6243. bestm1 = pipe_config->dpll.m1;
  6244. bestm2 = pipe_config->dpll.m2 >> 22;
  6245. bestp1 = pipe_config->dpll.p1;
  6246. bestp2 = pipe_config->dpll.p2;
  6247. vco = pipe_config->dpll.vco;
  6248. dpio_val = 0;
  6249. loopfilter = 0;
  6250. /*
  6251. * Enable Refclk and SSC
  6252. */
  6253. I915_WRITE(dpll_reg,
  6254. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6255. mutex_lock(&dev_priv->sb_lock);
  6256. /* p1 and p2 divider */
  6257. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6258. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6259. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6260. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6261. 1 << DPIO_CHV_K_DIV_SHIFT);
  6262. /* Feedback post-divider - m2 */
  6263. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6264. /* Feedback refclk divider - n and m1 */
  6265. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6266. DPIO_CHV_M1_DIV_BY_2 |
  6267. 1 << DPIO_CHV_N_DIV_SHIFT);
  6268. /* M2 fraction division */
  6269. if (bestm2_frac)
  6270. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6271. /* M2 fraction division enable */
  6272. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6273. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6274. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6275. if (bestm2_frac)
  6276. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6277. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6278. /* Program digital lock detect threshold */
  6279. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6280. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6281. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6282. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6283. if (!bestm2_frac)
  6284. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6285. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6286. /* Loop filter */
  6287. if (vco == 5400000) {
  6288. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6289. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6290. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6291. tribuf_calcntr = 0x9;
  6292. } else if (vco <= 6200000) {
  6293. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6294. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6295. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6296. tribuf_calcntr = 0x9;
  6297. } else if (vco <= 6480000) {
  6298. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6299. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6300. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6301. tribuf_calcntr = 0x8;
  6302. } else {
  6303. /* Not supported. Apply the same limits as in the max case */
  6304. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6305. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6306. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6307. tribuf_calcntr = 0;
  6308. }
  6309. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6310. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6311. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6312. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6313. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6314. /* AFC Recal */
  6315. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6316. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6317. DPIO_AFC_RECAL);
  6318. mutex_unlock(&dev_priv->sb_lock);
  6319. }
  6320. /**
  6321. * vlv_force_pll_on - forcibly enable just the PLL
  6322. * @dev_priv: i915 private structure
  6323. * @pipe: pipe PLL to enable
  6324. * @dpll: PLL configuration
  6325. *
  6326. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6327. * in cases where we need the PLL enabled even when @pipe is not going to
  6328. * be enabled.
  6329. */
  6330. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6331. const struct dpll *dpll)
  6332. {
  6333. struct intel_crtc *crtc =
  6334. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6335. struct intel_crtc_state pipe_config = {
  6336. .base.crtc = &crtc->base,
  6337. .pixel_multiplier = 1,
  6338. .dpll = *dpll,
  6339. };
  6340. if (IS_CHERRYVIEW(dev)) {
  6341. chv_update_pll(crtc, &pipe_config);
  6342. chv_prepare_pll(crtc, &pipe_config);
  6343. chv_enable_pll(crtc, &pipe_config);
  6344. } else {
  6345. vlv_update_pll(crtc, &pipe_config);
  6346. vlv_prepare_pll(crtc, &pipe_config);
  6347. vlv_enable_pll(crtc, &pipe_config);
  6348. }
  6349. }
  6350. /**
  6351. * vlv_force_pll_off - forcibly disable just the PLL
  6352. * @dev_priv: i915 private structure
  6353. * @pipe: pipe PLL to disable
  6354. *
  6355. * Disable the PLL for @pipe. To be used in cases where we need
  6356. * the PLL enabled even when @pipe is not going to be enabled.
  6357. */
  6358. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6359. {
  6360. if (IS_CHERRYVIEW(dev))
  6361. chv_disable_pll(to_i915(dev), pipe);
  6362. else
  6363. vlv_disable_pll(to_i915(dev), pipe);
  6364. }
  6365. static void i9xx_update_pll(struct intel_crtc *crtc,
  6366. struct intel_crtc_state *crtc_state,
  6367. intel_clock_t *reduced_clock,
  6368. int num_connectors)
  6369. {
  6370. struct drm_device *dev = crtc->base.dev;
  6371. struct drm_i915_private *dev_priv = dev->dev_private;
  6372. u32 dpll;
  6373. bool is_sdvo;
  6374. struct dpll *clock = &crtc_state->dpll;
  6375. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6376. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6377. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6378. dpll = DPLL_VGA_MODE_DIS;
  6379. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6380. dpll |= DPLLB_MODE_LVDS;
  6381. else
  6382. dpll |= DPLLB_MODE_DAC_SERIAL;
  6383. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6384. dpll |= (crtc_state->pixel_multiplier - 1)
  6385. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6386. }
  6387. if (is_sdvo)
  6388. dpll |= DPLL_SDVO_HIGH_SPEED;
  6389. if (crtc_state->has_dp_encoder)
  6390. dpll |= DPLL_SDVO_HIGH_SPEED;
  6391. /* compute bitmask from p1 value */
  6392. if (IS_PINEVIEW(dev))
  6393. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6394. else {
  6395. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6396. if (IS_G4X(dev) && reduced_clock)
  6397. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6398. }
  6399. switch (clock->p2) {
  6400. case 5:
  6401. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6402. break;
  6403. case 7:
  6404. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6405. break;
  6406. case 10:
  6407. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6408. break;
  6409. case 14:
  6410. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6411. break;
  6412. }
  6413. if (INTEL_INFO(dev)->gen >= 4)
  6414. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6415. if (crtc_state->sdvo_tv_clock)
  6416. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6417. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6418. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6419. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6420. else
  6421. dpll |= PLL_REF_INPUT_DREFCLK;
  6422. dpll |= DPLL_VCO_ENABLE;
  6423. crtc_state->dpll_hw_state.dpll = dpll;
  6424. if (INTEL_INFO(dev)->gen >= 4) {
  6425. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6426. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6427. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6428. }
  6429. }
  6430. static void i8xx_update_pll(struct intel_crtc *crtc,
  6431. struct intel_crtc_state *crtc_state,
  6432. intel_clock_t *reduced_clock,
  6433. int num_connectors)
  6434. {
  6435. struct drm_device *dev = crtc->base.dev;
  6436. struct drm_i915_private *dev_priv = dev->dev_private;
  6437. u32 dpll;
  6438. struct dpll *clock = &crtc_state->dpll;
  6439. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6440. dpll = DPLL_VGA_MODE_DIS;
  6441. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6442. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6443. } else {
  6444. if (clock->p1 == 2)
  6445. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6446. else
  6447. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6448. if (clock->p2 == 4)
  6449. dpll |= PLL_P2_DIVIDE_BY_4;
  6450. }
  6451. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6452. dpll |= DPLL_DVO_2X_MODE;
  6453. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6454. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6455. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6456. else
  6457. dpll |= PLL_REF_INPUT_DREFCLK;
  6458. dpll |= DPLL_VCO_ENABLE;
  6459. crtc_state->dpll_hw_state.dpll = dpll;
  6460. }
  6461. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6462. {
  6463. struct drm_device *dev = intel_crtc->base.dev;
  6464. struct drm_i915_private *dev_priv = dev->dev_private;
  6465. enum pipe pipe = intel_crtc->pipe;
  6466. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6467. struct drm_display_mode *adjusted_mode =
  6468. &intel_crtc->config->base.adjusted_mode;
  6469. uint32_t crtc_vtotal, crtc_vblank_end;
  6470. int vsyncshift = 0;
  6471. /* We need to be careful not to changed the adjusted mode, for otherwise
  6472. * the hw state checker will get angry at the mismatch. */
  6473. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6474. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6475. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6476. /* the chip adds 2 halflines automatically */
  6477. crtc_vtotal -= 1;
  6478. crtc_vblank_end -= 1;
  6479. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6480. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6481. else
  6482. vsyncshift = adjusted_mode->crtc_hsync_start -
  6483. adjusted_mode->crtc_htotal / 2;
  6484. if (vsyncshift < 0)
  6485. vsyncshift += adjusted_mode->crtc_htotal;
  6486. }
  6487. if (INTEL_INFO(dev)->gen > 3)
  6488. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6489. I915_WRITE(HTOTAL(cpu_transcoder),
  6490. (adjusted_mode->crtc_hdisplay - 1) |
  6491. ((adjusted_mode->crtc_htotal - 1) << 16));
  6492. I915_WRITE(HBLANK(cpu_transcoder),
  6493. (adjusted_mode->crtc_hblank_start - 1) |
  6494. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6495. I915_WRITE(HSYNC(cpu_transcoder),
  6496. (adjusted_mode->crtc_hsync_start - 1) |
  6497. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6498. I915_WRITE(VTOTAL(cpu_transcoder),
  6499. (adjusted_mode->crtc_vdisplay - 1) |
  6500. ((crtc_vtotal - 1) << 16));
  6501. I915_WRITE(VBLANK(cpu_transcoder),
  6502. (adjusted_mode->crtc_vblank_start - 1) |
  6503. ((crtc_vblank_end - 1) << 16));
  6504. I915_WRITE(VSYNC(cpu_transcoder),
  6505. (adjusted_mode->crtc_vsync_start - 1) |
  6506. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6507. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6508. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6509. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6510. * bits. */
  6511. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6512. (pipe == PIPE_B || pipe == PIPE_C))
  6513. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6514. /* pipesrc controls the size that is scaled from, which should
  6515. * always be the user's requested size.
  6516. */
  6517. I915_WRITE(PIPESRC(pipe),
  6518. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6519. (intel_crtc->config->pipe_src_h - 1));
  6520. }
  6521. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6522. struct intel_crtc_state *pipe_config)
  6523. {
  6524. struct drm_device *dev = crtc->base.dev;
  6525. struct drm_i915_private *dev_priv = dev->dev_private;
  6526. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6527. uint32_t tmp;
  6528. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6529. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6530. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6531. tmp = I915_READ(HBLANK(cpu_transcoder));
  6532. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6533. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6534. tmp = I915_READ(HSYNC(cpu_transcoder));
  6535. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6536. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6537. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6538. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6539. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6540. tmp = I915_READ(VBLANK(cpu_transcoder));
  6541. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6542. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6543. tmp = I915_READ(VSYNC(cpu_transcoder));
  6544. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6545. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6546. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6547. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6548. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6549. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6550. }
  6551. tmp = I915_READ(PIPESRC(crtc->pipe));
  6552. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6553. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6554. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6555. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6556. }
  6557. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6558. struct intel_crtc_state *pipe_config)
  6559. {
  6560. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6561. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6562. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6563. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6564. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6565. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6566. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6567. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6568. mode->flags = pipe_config->base.adjusted_mode.flags;
  6569. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6570. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6571. }
  6572. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6573. {
  6574. struct drm_device *dev = intel_crtc->base.dev;
  6575. struct drm_i915_private *dev_priv = dev->dev_private;
  6576. uint32_t pipeconf;
  6577. pipeconf = 0;
  6578. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6579. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6580. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6581. if (intel_crtc->config->double_wide)
  6582. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6583. /* only g4x and later have fancy bpc/dither controls */
  6584. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6585. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6586. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6587. pipeconf |= PIPECONF_DITHER_EN |
  6588. PIPECONF_DITHER_TYPE_SP;
  6589. switch (intel_crtc->config->pipe_bpp) {
  6590. case 18:
  6591. pipeconf |= PIPECONF_6BPC;
  6592. break;
  6593. case 24:
  6594. pipeconf |= PIPECONF_8BPC;
  6595. break;
  6596. case 30:
  6597. pipeconf |= PIPECONF_10BPC;
  6598. break;
  6599. default:
  6600. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6601. BUG();
  6602. }
  6603. }
  6604. if (HAS_PIPE_CXSR(dev)) {
  6605. if (intel_crtc->lowfreq_avail) {
  6606. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6607. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6608. } else {
  6609. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6610. }
  6611. }
  6612. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6613. if (INTEL_INFO(dev)->gen < 4 ||
  6614. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6615. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6616. else
  6617. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6618. } else
  6619. pipeconf |= PIPECONF_PROGRESSIVE;
  6620. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6621. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6622. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6623. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6624. }
  6625. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6626. struct intel_crtc_state *crtc_state)
  6627. {
  6628. struct drm_device *dev = crtc->base.dev;
  6629. struct drm_i915_private *dev_priv = dev->dev_private;
  6630. int refclk, num_connectors = 0;
  6631. intel_clock_t clock, reduced_clock;
  6632. bool ok, has_reduced_clock = false;
  6633. bool is_lvds = false, is_dsi = false;
  6634. struct intel_encoder *encoder;
  6635. const intel_limit_t *limit;
  6636. struct drm_atomic_state *state = crtc_state->base.state;
  6637. struct drm_connector *connector;
  6638. struct drm_connector_state *connector_state;
  6639. int i;
  6640. memset(&crtc_state->dpll_hw_state, 0,
  6641. sizeof(crtc_state->dpll_hw_state));
  6642. for_each_connector_in_state(state, connector, connector_state, i) {
  6643. if (connector_state->crtc != &crtc->base)
  6644. continue;
  6645. encoder = to_intel_encoder(connector_state->best_encoder);
  6646. switch (encoder->type) {
  6647. case INTEL_OUTPUT_LVDS:
  6648. is_lvds = true;
  6649. break;
  6650. case INTEL_OUTPUT_DSI:
  6651. is_dsi = true;
  6652. break;
  6653. default:
  6654. break;
  6655. }
  6656. num_connectors++;
  6657. }
  6658. if (is_dsi)
  6659. return 0;
  6660. if (!crtc_state->clock_set) {
  6661. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6662. /*
  6663. * Returns a set of divisors for the desired target clock with
  6664. * the given refclk, or FALSE. The returned values represent
  6665. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6666. * 2) / p1 / p2.
  6667. */
  6668. limit = intel_limit(crtc_state, refclk);
  6669. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6670. crtc_state->port_clock,
  6671. refclk, NULL, &clock);
  6672. if (!ok) {
  6673. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6674. return -EINVAL;
  6675. }
  6676. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6677. /*
  6678. * Ensure we match the reduced clock's P to the target
  6679. * clock. If the clocks don't match, we can't switch
  6680. * the display clock by using the FP0/FP1. In such case
  6681. * we will disable the LVDS downclock feature.
  6682. */
  6683. has_reduced_clock =
  6684. dev_priv->display.find_dpll(limit, crtc_state,
  6685. dev_priv->lvds_downclock,
  6686. refclk, &clock,
  6687. &reduced_clock);
  6688. }
  6689. /* Compat-code for transition, will disappear. */
  6690. crtc_state->dpll.n = clock.n;
  6691. crtc_state->dpll.m1 = clock.m1;
  6692. crtc_state->dpll.m2 = clock.m2;
  6693. crtc_state->dpll.p1 = clock.p1;
  6694. crtc_state->dpll.p2 = clock.p2;
  6695. }
  6696. if (IS_GEN2(dev)) {
  6697. i8xx_update_pll(crtc, crtc_state,
  6698. has_reduced_clock ? &reduced_clock : NULL,
  6699. num_connectors);
  6700. } else if (IS_CHERRYVIEW(dev)) {
  6701. chv_update_pll(crtc, crtc_state);
  6702. } else if (IS_VALLEYVIEW(dev)) {
  6703. vlv_update_pll(crtc, crtc_state);
  6704. } else {
  6705. i9xx_update_pll(crtc, crtc_state,
  6706. has_reduced_clock ? &reduced_clock : NULL,
  6707. num_connectors);
  6708. }
  6709. return 0;
  6710. }
  6711. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6712. struct intel_crtc_state *pipe_config)
  6713. {
  6714. struct drm_device *dev = crtc->base.dev;
  6715. struct drm_i915_private *dev_priv = dev->dev_private;
  6716. uint32_t tmp;
  6717. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6718. return;
  6719. tmp = I915_READ(PFIT_CONTROL);
  6720. if (!(tmp & PFIT_ENABLE))
  6721. return;
  6722. /* Check whether the pfit is attached to our pipe. */
  6723. if (INTEL_INFO(dev)->gen < 4) {
  6724. if (crtc->pipe != PIPE_B)
  6725. return;
  6726. } else {
  6727. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6728. return;
  6729. }
  6730. pipe_config->gmch_pfit.control = tmp;
  6731. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6732. if (INTEL_INFO(dev)->gen < 5)
  6733. pipe_config->gmch_pfit.lvds_border_bits =
  6734. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6735. }
  6736. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6737. struct intel_crtc_state *pipe_config)
  6738. {
  6739. struct drm_device *dev = crtc->base.dev;
  6740. struct drm_i915_private *dev_priv = dev->dev_private;
  6741. int pipe = pipe_config->cpu_transcoder;
  6742. intel_clock_t clock;
  6743. u32 mdiv;
  6744. int refclk = 100000;
  6745. /* In case of MIPI DPLL will not even be used */
  6746. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6747. return;
  6748. mutex_lock(&dev_priv->sb_lock);
  6749. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6750. mutex_unlock(&dev_priv->sb_lock);
  6751. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6752. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6753. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6754. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6755. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6756. vlv_clock(refclk, &clock);
  6757. /* clock.dot is the fast clock */
  6758. pipe_config->port_clock = clock.dot / 5;
  6759. }
  6760. static void
  6761. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6762. struct intel_initial_plane_config *plane_config)
  6763. {
  6764. struct drm_device *dev = crtc->base.dev;
  6765. struct drm_i915_private *dev_priv = dev->dev_private;
  6766. u32 val, base, offset;
  6767. int pipe = crtc->pipe, plane = crtc->plane;
  6768. int fourcc, pixel_format;
  6769. unsigned int aligned_height;
  6770. struct drm_framebuffer *fb;
  6771. struct intel_framebuffer *intel_fb;
  6772. val = I915_READ(DSPCNTR(plane));
  6773. if (!(val & DISPLAY_PLANE_ENABLE))
  6774. return;
  6775. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6776. if (!intel_fb) {
  6777. DRM_DEBUG_KMS("failed to alloc fb\n");
  6778. return;
  6779. }
  6780. fb = &intel_fb->base;
  6781. if (INTEL_INFO(dev)->gen >= 4) {
  6782. if (val & DISPPLANE_TILED) {
  6783. plane_config->tiling = I915_TILING_X;
  6784. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6785. }
  6786. }
  6787. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6788. fourcc = i9xx_format_to_fourcc(pixel_format);
  6789. fb->pixel_format = fourcc;
  6790. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6791. if (INTEL_INFO(dev)->gen >= 4) {
  6792. if (plane_config->tiling)
  6793. offset = I915_READ(DSPTILEOFF(plane));
  6794. else
  6795. offset = I915_READ(DSPLINOFF(plane));
  6796. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6797. } else {
  6798. base = I915_READ(DSPADDR(plane));
  6799. }
  6800. plane_config->base = base;
  6801. val = I915_READ(PIPESRC(pipe));
  6802. fb->width = ((val >> 16) & 0xfff) + 1;
  6803. fb->height = ((val >> 0) & 0xfff) + 1;
  6804. val = I915_READ(DSPSTRIDE(pipe));
  6805. fb->pitches[0] = val & 0xffffffc0;
  6806. aligned_height = intel_fb_align_height(dev, fb->height,
  6807. fb->pixel_format,
  6808. fb->modifier[0]);
  6809. plane_config->size = fb->pitches[0] * aligned_height;
  6810. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6811. pipe_name(pipe), plane, fb->width, fb->height,
  6812. fb->bits_per_pixel, base, fb->pitches[0],
  6813. plane_config->size);
  6814. plane_config->fb = intel_fb;
  6815. }
  6816. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6817. struct intel_crtc_state *pipe_config)
  6818. {
  6819. struct drm_device *dev = crtc->base.dev;
  6820. struct drm_i915_private *dev_priv = dev->dev_private;
  6821. int pipe = pipe_config->cpu_transcoder;
  6822. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6823. intel_clock_t clock;
  6824. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  6825. int refclk = 100000;
  6826. mutex_lock(&dev_priv->sb_lock);
  6827. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6828. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6829. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6830. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6831. mutex_unlock(&dev_priv->sb_lock);
  6832. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6833. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  6834. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6835. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6836. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6837. chv_clock(refclk, &clock);
  6838. /* clock.dot is the fast clock */
  6839. pipe_config->port_clock = clock.dot / 5;
  6840. }
  6841. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6842. struct intel_crtc_state *pipe_config)
  6843. {
  6844. struct drm_device *dev = crtc->base.dev;
  6845. struct drm_i915_private *dev_priv = dev->dev_private;
  6846. uint32_t tmp;
  6847. if (!intel_display_power_is_enabled(dev_priv,
  6848. POWER_DOMAIN_PIPE(crtc->pipe)))
  6849. return false;
  6850. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6851. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6852. tmp = I915_READ(PIPECONF(crtc->pipe));
  6853. if (!(tmp & PIPECONF_ENABLE))
  6854. return false;
  6855. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6856. switch (tmp & PIPECONF_BPC_MASK) {
  6857. case PIPECONF_6BPC:
  6858. pipe_config->pipe_bpp = 18;
  6859. break;
  6860. case PIPECONF_8BPC:
  6861. pipe_config->pipe_bpp = 24;
  6862. break;
  6863. case PIPECONF_10BPC:
  6864. pipe_config->pipe_bpp = 30;
  6865. break;
  6866. default:
  6867. break;
  6868. }
  6869. }
  6870. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6871. pipe_config->limited_color_range = true;
  6872. if (INTEL_INFO(dev)->gen < 4)
  6873. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6874. intel_get_pipe_timings(crtc, pipe_config);
  6875. i9xx_get_pfit_config(crtc, pipe_config);
  6876. if (INTEL_INFO(dev)->gen >= 4) {
  6877. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6878. pipe_config->pixel_multiplier =
  6879. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6880. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6881. pipe_config->dpll_hw_state.dpll_md = tmp;
  6882. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6883. tmp = I915_READ(DPLL(crtc->pipe));
  6884. pipe_config->pixel_multiplier =
  6885. ((tmp & SDVO_MULTIPLIER_MASK)
  6886. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6887. } else {
  6888. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6889. * port and will be fixed up in the encoder->get_config
  6890. * function. */
  6891. pipe_config->pixel_multiplier = 1;
  6892. }
  6893. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6894. if (!IS_VALLEYVIEW(dev)) {
  6895. /*
  6896. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6897. * on 830. Filter it out here so that we don't
  6898. * report errors due to that.
  6899. */
  6900. if (IS_I830(dev))
  6901. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6902. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6903. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6904. } else {
  6905. /* Mask out read-only status bits. */
  6906. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6907. DPLL_PORTC_READY_MASK |
  6908. DPLL_PORTB_READY_MASK);
  6909. }
  6910. if (IS_CHERRYVIEW(dev))
  6911. chv_crtc_clock_get(crtc, pipe_config);
  6912. else if (IS_VALLEYVIEW(dev))
  6913. vlv_crtc_clock_get(crtc, pipe_config);
  6914. else
  6915. i9xx_crtc_clock_get(crtc, pipe_config);
  6916. return true;
  6917. }
  6918. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6919. {
  6920. struct drm_i915_private *dev_priv = dev->dev_private;
  6921. struct intel_encoder *encoder;
  6922. u32 val, final;
  6923. bool has_lvds = false;
  6924. bool has_cpu_edp = false;
  6925. bool has_panel = false;
  6926. bool has_ck505 = false;
  6927. bool can_ssc = false;
  6928. /* We need to take the global config into account */
  6929. for_each_intel_encoder(dev, encoder) {
  6930. switch (encoder->type) {
  6931. case INTEL_OUTPUT_LVDS:
  6932. has_panel = true;
  6933. has_lvds = true;
  6934. break;
  6935. case INTEL_OUTPUT_EDP:
  6936. has_panel = true;
  6937. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6938. has_cpu_edp = true;
  6939. break;
  6940. default:
  6941. break;
  6942. }
  6943. }
  6944. if (HAS_PCH_IBX(dev)) {
  6945. has_ck505 = dev_priv->vbt.display_clock_mode;
  6946. can_ssc = has_ck505;
  6947. } else {
  6948. has_ck505 = false;
  6949. can_ssc = true;
  6950. }
  6951. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6952. has_panel, has_lvds, has_ck505);
  6953. /* Ironlake: try to setup display ref clock before DPLL
  6954. * enabling. This is only under driver's control after
  6955. * PCH B stepping, previous chipset stepping should be
  6956. * ignoring this setting.
  6957. */
  6958. val = I915_READ(PCH_DREF_CONTROL);
  6959. /* As we must carefully and slowly disable/enable each source in turn,
  6960. * compute the final state we want first and check if we need to
  6961. * make any changes at all.
  6962. */
  6963. final = val;
  6964. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6965. if (has_ck505)
  6966. final |= DREF_NONSPREAD_CK505_ENABLE;
  6967. else
  6968. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6969. final &= ~DREF_SSC_SOURCE_MASK;
  6970. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6971. final &= ~DREF_SSC1_ENABLE;
  6972. if (has_panel) {
  6973. final |= DREF_SSC_SOURCE_ENABLE;
  6974. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6975. final |= DREF_SSC1_ENABLE;
  6976. if (has_cpu_edp) {
  6977. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6978. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6979. else
  6980. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6981. } else
  6982. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6983. } else {
  6984. final |= DREF_SSC_SOURCE_DISABLE;
  6985. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6986. }
  6987. if (final == val)
  6988. return;
  6989. /* Always enable nonspread source */
  6990. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6991. if (has_ck505)
  6992. val |= DREF_NONSPREAD_CK505_ENABLE;
  6993. else
  6994. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6995. if (has_panel) {
  6996. val &= ~DREF_SSC_SOURCE_MASK;
  6997. val |= DREF_SSC_SOURCE_ENABLE;
  6998. /* SSC must be turned on before enabling the CPU output */
  6999. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7000. DRM_DEBUG_KMS("Using SSC on panel\n");
  7001. val |= DREF_SSC1_ENABLE;
  7002. } else
  7003. val &= ~DREF_SSC1_ENABLE;
  7004. /* Get SSC going before enabling the outputs */
  7005. I915_WRITE(PCH_DREF_CONTROL, val);
  7006. POSTING_READ(PCH_DREF_CONTROL);
  7007. udelay(200);
  7008. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7009. /* Enable CPU source on CPU attached eDP */
  7010. if (has_cpu_edp) {
  7011. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7012. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7013. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7014. } else
  7015. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7016. } else
  7017. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7018. I915_WRITE(PCH_DREF_CONTROL, val);
  7019. POSTING_READ(PCH_DREF_CONTROL);
  7020. udelay(200);
  7021. } else {
  7022. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  7023. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7024. /* Turn off CPU output */
  7025. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7026. I915_WRITE(PCH_DREF_CONTROL, val);
  7027. POSTING_READ(PCH_DREF_CONTROL);
  7028. udelay(200);
  7029. /* Turn off the SSC source */
  7030. val &= ~DREF_SSC_SOURCE_MASK;
  7031. val |= DREF_SSC_SOURCE_DISABLE;
  7032. /* Turn off SSC1 */
  7033. val &= ~DREF_SSC1_ENABLE;
  7034. I915_WRITE(PCH_DREF_CONTROL, val);
  7035. POSTING_READ(PCH_DREF_CONTROL);
  7036. udelay(200);
  7037. }
  7038. BUG_ON(val != final);
  7039. }
  7040. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7041. {
  7042. uint32_t tmp;
  7043. tmp = I915_READ(SOUTH_CHICKEN2);
  7044. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7045. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7046. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7047. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7048. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7049. tmp = I915_READ(SOUTH_CHICKEN2);
  7050. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7051. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7052. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7053. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7054. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7055. }
  7056. /* WaMPhyProgramming:hsw */
  7057. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7058. {
  7059. uint32_t tmp;
  7060. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7061. tmp &= ~(0xFF << 24);
  7062. tmp |= (0x12 << 24);
  7063. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7064. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7065. tmp |= (1 << 11);
  7066. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7067. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7068. tmp |= (1 << 11);
  7069. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7070. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7071. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7072. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7073. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7074. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7075. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7076. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7077. tmp &= ~(7 << 13);
  7078. tmp |= (5 << 13);
  7079. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7080. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7081. tmp &= ~(7 << 13);
  7082. tmp |= (5 << 13);
  7083. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7084. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7085. tmp &= ~0xFF;
  7086. tmp |= 0x1C;
  7087. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7088. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7089. tmp &= ~0xFF;
  7090. tmp |= 0x1C;
  7091. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7092. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7093. tmp &= ~(0xFF << 16);
  7094. tmp |= (0x1C << 16);
  7095. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7096. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7097. tmp &= ~(0xFF << 16);
  7098. tmp |= (0x1C << 16);
  7099. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7100. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7101. tmp |= (1 << 27);
  7102. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7103. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7104. tmp |= (1 << 27);
  7105. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7106. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7107. tmp &= ~(0xF << 28);
  7108. tmp |= (4 << 28);
  7109. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7110. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7111. tmp &= ~(0xF << 28);
  7112. tmp |= (4 << 28);
  7113. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7114. }
  7115. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7116. * Programming" based on the parameters passed:
  7117. * - Sequence to enable CLKOUT_DP
  7118. * - Sequence to enable CLKOUT_DP without spread
  7119. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7120. */
  7121. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7122. bool with_fdi)
  7123. {
  7124. struct drm_i915_private *dev_priv = dev->dev_private;
  7125. uint32_t reg, tmp;
  7126. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7127. with_spread = true;
  7128. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  7129. with_fdi, "LP PCH doesn't have FDI\n"))
  7130. with_fdi = false;
  7131. mutex_lock(&dev_priv->sb_lock);
  7132. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7133. tmp &= ~SBI_SSCCTL_DISABLE;
  7134. tmp |= SBI_SSCCTL_PATHALT;
  7135. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7136. udelay(24);
  7137. if (with_spread) {
  7138. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7139. tmp &= ~SBI_SSCCTL_PATHALT;
  7140. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7141. if (with_fdi) {
  7142. lpt_reset_fdi_mphy(dev_priv);
  7143. lpt_program_fdi_mphy(dev_priv);
  7144. }
  7145. }
  7146. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7147. SBI_GEN0 : SBI_DBUFF0;
  7148. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7149. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7150. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7151. mutex_unlock(&dev_priv->sb_lock);
  7152. }
  7153. /* Sequence to disable CLKOUT_DP */
  7154. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7155. {
  7156. struct drm_i915_private *dev_priv = dev->dev_private;
  7157. uint32_t reg, tmp;
  7158. mutex_lock(&dev_priv->sb_lock);
  7159. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7160. SBI_GEN0 : SBI_DBUFF0;
  7161. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7162. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7163. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7164. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7165. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7166. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7167. tmp |= SBI_SSCCTL_PATHALT;
  7168. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7169. udelay(32);
  7170. }
  7171. tmp |= SBI_SSCCTL_DISABLE;
  7172. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7173. }
  7174. mutex_unlock(&dev_priv->sb_lock);
  7175. }
  7176. static void lpt_init_pch_refclk(struct drm_device *dev)
  7177. {
  7178. struct intel_encoder *encoder;
  7179. bool has_vga = false;
  7180. for_each_intel_encoder(dev, encoder) {
  7181. switch (encoder->type) {
  7182. case INTEL_OUTPUT_ANALOG:
  7183. has_vga = true;
  7184. break;
  7185. default:
  7186. break;
  7187. }
  7188. }
  7189. if (has_vga)
  7190. lpt_enable_clkout_dp(dev, true, true);
  7191. else
  7192. lpt_disable_clkout_dp(dev);
  7193. }
  7194. /*
  7195. * Initialize reference clocks when the driver loads
  7196. */
  7197. void intel_init_pch_refclk(struct drm_device *dev)
  7198. {
  7199. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7200. ironlake_init_pch_refclk(dev);
  7201. else if (HAS_PCH_LPT(dev))
  7202. lpt_init_pch_refclk(dev);
  7203. }
  7204. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7205. {
  7206. struct drm_device *dev = crtc_state->base.crtc->dev;
  7207. struct drm_i915_private *dev_priv = dev->dev_private;
  7208. struct drm_atomic_state *state = crtc_state->base.state;
  7209. struct drm_connector *connector;
  7210. struct drm_connector_state *connector_state;
  7211. struct intel_encoder *encoder;
  7212. int num_connectors = 0, i;
  7213. bool is_lvds = false;
  7214. for_each_connector_in_state(state, connector, connector_state, i) {
  7215. if (connector_state->crtc != crtc_state->base.crtc)
  7216. continue;
  7217. encoder = to_intel_encoder(connector_state->best_encoder);
  7218. switch (encoder->type) {
  7219. case INTEL_OUTPUT_LVDS:
  7220. is_lvds = true;
  7221. break;
  7222. default:
  7223. break;
  7224. }
  7225. num_connectors++;
  7226. }
  7227. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7228. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7229. dev_priv->vbt.lvds_ssc_freq);
  7230. return dev_priv->vbt.lvds_ssc_freq;
  7231. }
  7232. return 120000;
  7233. }
  7234. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7235. {
  7236. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7237. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7238. int pipe = intel_crtc->pipe;
  7239. uint32_t val;
  7240. val = 0;
  7241. switch (intel_crtc->config->pipe_bpp) {
  7242. case 18:
  7243. val |= PIPECONF_6BPC;
  7244. break;
  7245. case 24:
  7246. val |= PIPECONF_8BPC;
  7247. break;
  7248. case 30:
  7249. val |= PIPECONF_10BPC;
  7250. break;
  7251. case 36:
  7252. val |= PIPECONF_12BPC;
  7253. break;
  7254. default:
  7255. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7256. BUG();
  7257. }
  7258. if (intel_crtc->config->dither)
  7259. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7260. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7261. val |= PIPECONF_INTERLACED_ILK;
  7262. else
  7263. val |= PIPECONF_PROGRESSIVE;
  7264. if (intel_crtc->config->limited_color_range)
  7265. val |= PIPECONF_COLOR_RANGE_SELECT;
  7266. I915_WRITE(PIPECONF(pipe), val);
  7267. POSTING_READ(PIPECONF(pipe));
  7268. }
  7269. /*
  7270. * Set up the pipe CSC unit.
  7271. *
  7272. * Currently only full range RGB to limited range RGB conversion
  7273. * is supported, but eventually this should handle various
  7274. * RGB<->YCbCr scenarios as well.
  7275. */
  7276. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7277. {
  7278. struct drm_device *dev = crtc->dev;
  7279. struct drm_i915_private *dev_priv = dev->dev_private;
  7280. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7281. int pipe = intel_crtc->pipe;
  7282. uint16_t coeff = 0x7800; /* 1.0 */
  7283. /*
  7284. * TODO: Check what kind of values actually come out of the pipe
  7285. * with these coeff/postoff values and adjust to get the best
  7286. * accuracy. Perhaps we even need to take the bpc value into
  7287. * consideration.
  7288. */
  7289. if (intel_crtc->config->limited_color_range)
  7290. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7291. /*
  7292. * GY/GU and RY/RU should be the other way around according
  7293. * to BSpec, but reality doesn't agree. Just set them up in
  7294. * a way that results in the correct picture.
  7295. */
  7296. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7297. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7298. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7299. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7300. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7301. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7302. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7303. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7304. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7305. if (INTEL_INFO(dev)->gen > 6) {
  7306. uint16_t postoff = 0;
  7307. if (intel_crtc->config->limited_color_range)
  7308. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7309. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7310. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7311. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7312. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7313. } else {
  7314. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7315. if (intel_crtc->config->limited_color_range)
  7316. mode |= CSC_BLACK_SCREEN_OFFSET;
  7317. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7318. }
  7319. }
  7320. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7321. {
  7322. struct drm_device *dev = crtc->dev;
  7323. struct drm_i915_private *dev_priv = dev->dev_private;
  7324. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7325. enum pipe pipe = intel_crtc->pipe;
  7326. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7327. uint32_t val;
  7328. val = 0;
  7329. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7330. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7331. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7332. val |= PIPECONF_INTERLACED_ILK;
  7333. else
  7334. val |= PIPECONF_PROGRESSIVE;
  7335. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7336. POSTING_READ(PIPECONF(cpu_transcoder));
  7337. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7338. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7339. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7340. val = 0;
  7341. switch (intel_crtc->config->pipe_bpp) {
  7342. case 18:
  7343. val |= PIPEMISC_DITHER_6_BPC;
  7344. break;
  7345. case 24:
  7346. val |= PIPEMISC_DITHER_8_BPC;
  7347. break;
  7348. case 30:
  7349. val |= PIPEMISC_DITHER_10_BPC;
  7350. break;
  7351. case 36:
  7352. val |= PIPEMISC_DITHER_12_BPC;
  7353. break;
  7354. default:
  7355. /* Case prevented by pipe_config_set_bpp. */
  7356. BUG();
  7357. }
  7358. if (intel_crtc->config->dither)
  7359. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7360. I915_WRITE(PIPEMISC(pipe), val);
  7361. }
  7362. }
  7363. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7364. struct intel_crtc_state *crtc_state,
  7365. intel_clock_t *clock,
  7366. bool *has_reduced_clock,
  7367. intel_clock_t *reduced_clock)
  7368. {
  7369. struct drm_device *dev = crtc->dev;
  7370. struct drm_i915_private *dev_priv = dev->dev_private;
  7371. int refclk;
  7372. const intel_limit_t *limit;
  7373. bool ret, is_lvds = false;
  7374. is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
  7375. refclk = ironlake_get_refclk(crtc_state);
  7376. /*
  7377. * Returns a set of divisors for the desired target clock with the given
  7378. * refclk, or FALSE. The returned values represent the clock equation:
  7379. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7380. */
  7381. limit = intel_limit(crtc_state, refclk);
  7382. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7383. crtc_state->port_clock,
  7384. refclk, NULL, clock);
  7385. if (!ret)
  7386. return false;
  7387. if (is_lvds && dev_priv->lvds_downclock_avail) {
  7388. /*
  7389. * Ensure we match the reduced clock's P to the target clock.
  7390. * If the clocks don't match, we can't switch the display clock
  7391. * by using the FP0/FP1. In such case we will disable the LVDS
  7392. * downclock feature.
  7393. */
  7394. *has_reduced_clock =
  7395. dev_priv->display.find_dpll(limit, crtc_state,
  7396. dev_priv->lvds_downclock,
  7397. refclk, clock,
  7398. reduced_clock);
  7399. }
  7400. return true;
  7401. }
  7402. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7403. {
  7404. /*
  7405. * Account for spread spectrum to avoid
  7406. * oversubscribing the link. Max center spread
  7407. * is 2.5%; use 5% for safety's sake.
  7408. */
  7409. u32 bps = target_clock * bpp * 21 / 20;
  7410. return DIV_ROUND_UP(bps, link_bw * 8);
  7411. }
  7412. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7413. {
  7414. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7415. }
  7416. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7417. struct intel_crtc_state *crtc_state,
  7418. u32 *fp,
  7419. intel_clock_t *reduced_clock, u32 *fp2)
  7420. {
  7421. struct drm_crtc *crtc = &intel_crtc->base;
  7422. struct drm_device *dev = crtc->dev;
  7423. struct drm_i915_private *dev_priv = dev->dev_private;
  7424. struct drm_atomic_state *state = crtc_state->base.state;
  7425. struct drm_connector *connector;
  7426. struct drm_connector_state *connector_state;
  7427. struct intel_encoder *encoder;
  7428. uint32_t dpll;
  7429. int factor, num_connectors = 0, i;
  7430. bool is_lvds = false, is_sdvo = false;
  7431. for_each_connector_in_state(state, connector, connector_state, i) {
  7432. if (connector_state->crtc != crtc_state->base.crtc)
  7433. continue;
  7434. encoder = to_intel_encoder(connector_state->best_encoder);
  7435. switch (encoder->type) {
  7436. case INTEL_OUTPUT_LVDS:
  7437. is_lvds = true;
  7438. break;
  7439. case INTEL_OUTPUT_SDVO:
  7440. case INTEL_OUTPUT_HDMI:
  7441. is_sdvo = true;
  7442. break;
  7443. default:
  7444. break;
  7445. }
  7446. num_connectors++;
  7447. }
  7448. /* Enable autotuning of the PLL clock (if permissible) */
  7449. factor = 21;
  7450. if (is_lvds) {
  7451. if ((intel_panel_use_ssc(dev_priv) &&
  7452. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7453. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7454. factor = 25;
  7455. } else if (crtc_state->sdvo_tv_clock)
  7456. factor = 20;
  7457. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7458. *fp |= FP_CB_TUNE;
  7459. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7460. *fp2 |= FP_CB_TUNE;
  7461. dpll = 0;
  7462. if (is_lvds)
  7463. dpll |= DPLLB_MODE_LVDS;
  7464. else
  7465. dpll |= DPLLB_MODE_DAC_SERIAL;
  7466. dpll |= (crtc_state->pixel_multiplier - 1)
  7467. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7468. if (is_sdvo)
  7469. dpll |= DPLL_SDVO_HIGH_SPEED;
  7470. if (crtc_state->has_dp_encoder)
  7471. dpll |= DPLL_SDVO_HIGH_SPEED;
  7472. /* compute bitmask from p1 value */
  7473. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7474. /* also FPA1 */
  7475. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7476. switch (crtc_state->dpll.p2) {
  7477. case 5:
  7478. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7479. break;
  7480. case 7:
  7481. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7482. break;
  7483. case 10:
  7484. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7485. break;
  7486. case 14:
  7487. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7488. break;
  7489. }
  7490. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7491. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7492. else
  7493. dpll |= PLL_REF_INPUT_DREFCLK;
  7494. return dpll | DPLL_VCO_ENABLE;
  7495. }
  7496. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7497. struct intel_crtc_state *crtc_state)
  7498. {
  7499. struct drm_device *dev = crtc->base.dev;
  7500. intel_clock_t clock, reduced_clock;
  7501. u32 dpll = 0, fp = 0, fp2 = 0;
  7502. bool ok, has_reduced_clock = false;
  7503. bool is_lvds = false;
  7504. struct intel_shared_dpll *pll;
  7505. memset(&crtc_state->dpll_hw_state, 0,
  7506. sizeof(crtc_state->dpll_hw_state));
  7507. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7508. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7509. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7510. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7511. &has_reduced_clock, &reduced_clock);
  7512. if (!ok && !crtc_state->clock_set) {
  7513. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7514. return -EINVAL;
  7515. }
  7516. /* Compat-code for transition, will disappear. */
  7517. if (!crtc_state->clock_set) {
  7518. crtc_state->dpll.n = clock.n;
  7519. crtc_state->dpll.m1 = clock.m1;
  7520. crtc_state->dpll.m2 = clock.m2;
  7521. crtc_state->dpll.p1 = clock.p1;
  7522. crtc_state->dpll.p2 = clock.p2;
  7523. }
  7524. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7525. if (crtc_state->has_pch_encoder) {
  7526. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7527. if (has_reduced_clock)
  7528. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7529. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7530. &fp, &reduced_clock,
  7531. has_reduced_clock ? &fp2 : NULL);
  7532. crtc_state->dpll_hw_state.dpll = dpll;
  7533. crtc_state->dpll_hw_state.fp0 = fp;
  7534. if (has_reduced_clock)
  7535. crtc_state->dpll_hw_state.fp1 = fp2;
  7536. else
  7537. crtc_state->dpll_hw_state.fp1 = fp;
  7538. pll = intel_get_shared_dpll(crtc, crtc_state);
  7539. if (pll == NULL) {
  7540. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7541. pipe_name(crtc->pipe));
  7542. return -EINVAL;
  7543. }
  7544. }
  7545. if (is_lvds && has_reduced_clock)
  7546. crtc->lowfreq_avail = true;
  7547. else
  7548. crtc->lowfreq_avail = false;
  7549. return 0;
  7550. }
  7551. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7552. struct intel_link_m_n *m_n)
  7553. {
  7554. struct drm_device *dev = crtc->base.dev;
  7555. struct drm_i915_private *dev_priv = dev->dev_private;
  7556. enum pipe pipe = crtc->pipe;
  7557. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7558. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7559. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7560. & ~TU_SIZE_MASK;
  7561. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7562. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7563. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7564. }
  7565. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7566. enum transcoder transcoder,
  7567. struct intel_link_m_n *m_n,
  7568. struct intel_link_m_n *m2_n2)
  7569. {
  7570. struct drm_device *dev = crtc->base.dev;
  7571. struct drm_i915_private *dev_priv = dev->dev_private;
  7572. enum pipe pipe = crtc->pipe;
  7573. if (INTEL_INFO(dev)->gen >= 5) {
  7574. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7575. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7576. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7577. & ~TU_SIZE_MASK;
  7578. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7579. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7580. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7581. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7582. * gen < 8) and if DRRS is supported (to make sure the
  7583. * registers are not unnecessarily read).
  7584. */
  7585. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7586. crtc->config->has_drrs) {
  7587. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7588. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7589. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7590. & ~TU_SIZE_MASK;
  7591. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7592. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7593. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7594. }
  7595. } else {
  7596. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7597. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7598. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7599. & ~TU_SIZE_MASK;
  7600. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7601. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7602. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7603. }
  7604. }
  7605. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7606. struct intel_crtc_state *pipe_config)
  7607. {
  7608. if (pipe_config->has_pch_encoder)
  7609. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7610. else
  7611. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7612. &pipe_config->dp_m_n,
  7613. &pipe_config->dp_m2_n2);
  7614. }
  7615. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7616. struct intel_crtc_state *pipe_config)
  7617. {
  7618. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7619. &pipe_config->fdi_m_n, NULL);
  7620. }
  7621. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7622. struct intel_crtc_state *pipe_config)
  7623. {
  7624. struct drm_device *dev = crtc->base.dev;
  7625. struct drm_i915_private *dev_priv = dev->dev_private;
  7626. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7627. uint32_t ps_ctrl = 0;
  7628. int id = -1;
  7629. int i;
  7630. /* find scaler attached to this pipe */
  7631. for (i = 0; i < crtc->num_scalers; i++) {
  7632. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7633. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7634. id = i;
  7635. pipe_config->pch_pfit.enabled = true;
  7636. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7637. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7638. break;
  7639. }
  7640. }
  7641. scaler_state->scaler_id = id;
  7642. if (id >= 0) {
  7643. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7644. } else {
  7645. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7646. }
  7647. }
  7648. static void
  7649. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7650. struct intel_initial_plane_config *plane_config)
  7651. {
  7652. struct drm_device *dev = crtc->base.dev;
  7653. struct drm_i915_private *dev_priv = dev->dev_private;
  7654. u32 val, base, offset, stride_mult, tiling;
  7655. int pipe = crtc->pipe;
  7656. int fourcc, pixel_format;
  7657. unsigned int aligned_height;
  7658. struct drm_framebuffer *fb;
  7659. struct intel_framebuffer *intel_fb;
  7660. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7661. if (!intel_fb) {
  7662. DRM_DEBUG_KMS("failed to alloc fb\n");
  7663. return;
  7664. }
  7665. fb = &intel_fb->base;
  7666. val = I915_READ(PLANE_CTL(pipe, 0));
  7667. if (!(val & PLANE_CTL_ENABLE))
  7668. goto error;
  7669. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7670. fourcc = skl_format_to_fourcc(pixel_format,
  7671. val & PLANE_CTL_ORDER_RGBX,
  7672. val & PLANE_CTL_ALPHA_MASK);
  7673. fb->pixel_format = fourcc;
  7674. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7675. tiling = val & PLANE_CTL_TILED_MASK;
  7676. switch (tiling) {
  7677. case PLANE_CTL_TILED_LINEAR:
  7678. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7679. break;
  7680. case PLANE_CTL_TILED_X:
  7681. plane_config->tiling = I915_TILING_X;
  7682. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7683. break;
  7684. case PLANE_CTL_TILED_Y:
  7685. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7686. break;
  7687. case PLANE_CTL_TILED_YF:
  7688. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7689. break;
  7690. default:
  7691. MISSING_CASE(tiling);
  7692. goto error;
  7693. }
  7694. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7695. plane_config->base = base;
  7696. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7697. val = I915_READ(PLANE_SIZE(pipe, 0));
  7698. fb->height = ((val >> 16) & 0xfff) + 1;
  7699. fb->width = ((val >> 0) & 0x1fff) + 1;
  7700. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7701. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7702. fb->pixel_format);
  7703. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7704. aligned_height = intel_fb_align_height(dev, fb->height,
  7705. fb->pixel_format,
  7706. fb->modifier[0]);
  7707. plane_config->size = fb->pitches[0] * aligned_height;
  7708. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7709. pipe_name(pipe), fb->width, fb->height,
  7710. fb->bits_per_pixel, base, fb->pitches[0],
  7711. plane_config->size);
  7712. plane_config->fb = intel_fb;
  7713. return;
  7714. error:
  7715. kfree(fb);
  7716. }
  7717. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7718. struct intel_crtc_state *pipe_config)
  7719. {
  7720. struct drm_device *dev = crtc->base.dev;
  7721. struct drm_i915_private *dev_priv = dev->dev_private;
  7722. uint32_t tmp;
  7723. tmp = I915_READ(PF_CTL(crtc->pipe));
  7724. if (tmp & PF_ENABLE) {
  7725. pipe_config->pch_pfit.enabled = true;
  7726. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7727. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7728. /* We currently do not free assignements of panel fitters on
  7729. * ivb/hsw (since we don't use the higher upscaling modes which
  7730. * differentiates them) so just WARN about this case for now. */
  7731. if (IS_GEN7(dev)) {
  7732. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7733. PF_PIPE_SEL_IVB(crtc->pipe));
  7734. }
  7735. }
  7736. }
  7737. static void
  7738. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7739. struct intel_initial_plane_config *plane_config)
  7740. {
  7741. struct drm_device *dev = crtc->base.dev;
  7742. struct drm_i915_private *dev_priv = dev->dev_private;
  7743. u32 val, base, offset;
  7744. int pipe = crtc->pipe;
  7745. int fourcc, pixel_format;
  7746. unsigned int aligned_height;
  7747. struct drm_framebuffer *fb;
  7748. struct intel_framebuffer *intel_fb;
  7749. val = I915_READ(DSPCNTR(pipe));
  7750. if (!(val & DISPLAY_PLANE_ENABLE))
  7751. return;
  7752. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7753. if (!intel_fb) {
  7754. DRM_DEBUG_KMS("failed to alloc fb\n");
  7755. return;
  7756. }
  7757. fb = &intel_fb->base;
  7758. if (INTEL_INFO(dev)->gen >= 4) {
  7759. if (val & DISPPLANE_TILED) {
  7760. plane_config->tiling = I915_TILING_X;
  7761. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7762. }
  7763. }
  7764. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7765. fourcc = i9xx_format_to_fourcc(pixel_format);
  7766. fb->pixel_format = fourcc;
  7767. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7768. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7769. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7770. offset = I915_READ(DSPOFFSET(pipe));
  7771. } else {
  7772. if (plane_config->tiling)
  7773. offset = I915_READ(DSPTILEOFF(pipe));
  7774. else
  7775. offset = I915_READ(DSPLINOFF(pipe));
  7776. }
  7777. plane_config->base = base;
  7778. val = I915_READ(PIPESRC(pipe));
  7779. fb->width = ((val >> 16) & 0xfff) + 1;
  7780. fb->height = ((val >> 0) & 0xfff) + 1;
  7781. val = I915_READ(DSPSTRIDE(pipe));
  7782. fb->pitches[0] = val & 0xffffffc0;
  7783. aligned_height = intel_fb_align_height(dev, fb->height,
  7784. fb->pixel_format,
  7785. fb->modifier[0]);
  7786. plane_config->size = fb->pitches[0] * aligned_height;
  7787. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7788. pipe_name(pipe), fb->width, fb->height,
  7789. fb->bits_per_pixel, base, fb->pitches[0],
  7790. plane_config->size);
  7791. plane_config->fb = intel_fb;
  7792. }
  7793. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7794. struct intel_crtc_state *pipe_config)
  7795. {
  7796. struct drm_device *dev = crtc->base.dev;
  7797. struct drm_i915_private *dev_priv = dev->dev_private;
  7798. uint32_t tmp;
  7799. if (!intel_display_power_is_enabled(dev_priv,
  7800. POWER_DOMAIN_PIPE(crtc->pipe)))
  7801. return false;
  7802. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7803. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7804. tmp = I915_READ(PIPECONF(crtc->pipe));
  7805. if (!(tmp & PIPECONF_ENABLE))
  7806. return false;
  7807. switch (tmp & PIPECONF_BPC_MASK) {
  7808. case PIPECONF_6BPC:
  7809. pipe_config->pipe_bpp = 18;
  7810. break;
  7811. case PIPECONF_8BPC:
  7812. pipe_config->pipe_bpp = 24;
  7813. break;
  7814. case PIPECONF_10BPC:
  7815. pipe_config->pipe_bpp = 30;
  7816. break;
  7817. case PIPECONF_12BPC:
  7818. pipe_config->pipe_bpp = 36;
  7819. break;
  7820. default:
  7821. break;
  7822. }
  7823. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7824. pipe_config->limited_color_range = true;
  7825. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7826. struct intel_shared_dpll *pll;
  7827. pipe_config->has_pch_encoder = true;
  7828. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7829. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7830. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7831. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7832. if (HAS_PCH_IBX(dev_priv->dev)) {
  7833. pipe_config->shared_dpll =
  7834. (enum intel_dpll_id) crtc->pipe;
  7835. } else {
  7836. tmp = I915_READ(PCH_DPLL_SEL);
  7837. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7838. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7839. else
  7840. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7841. }
  7842. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7843. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7844. &pipe_config->dpll_hw_state));
  7845. tmp = pipe_config->dpll_hw_state.dpll;
  7846. pipe_config->pixel_multiplier =
  7847. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7848. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7849. ironlake_pch_clock_get(crtc, pipe_config);
  7850. } else {
  7851. pipe_config->pixel_multiplier = 1;
  7852. }
  7853. intel_get_pipe_timings(crtc, pipe_config);
  7854. ironlake_get_pfit_config(crtc, pipe_config);
  7855. return true;
  7856. }
  7857. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7858. {
  7859. struct drm_device *dev = dev_priv->dev;
  7860. struct intel_crtc *crtc;
  7861. for_each_intel_crtc(dev, crtc)
  7862. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7863. pipe_name(crtc->pipe));
  7864. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7865. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7866. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7867. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7868. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7869. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7870. "CPU PWM1 enabled\n");
  7871. if (IS_HASWELL(dev))
  7872. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7873. "CPU PWM2 enabled\n");
  7874. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7875. "PCH PWM1 enabled\n");
  7876. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7877. "Utility pin enabled\n");
  7878. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7879. /*
  7880. * In theory we can still leave IRQs enabled, as long as only the HPD
  7881. * interrupts remain enabled. We used to check for that, but since it's
  7882. * gen-specific and since we only disable LCPLL after we fully disable
  7883. * the interrupts, the check below should be enough.
  7884. */
  7885. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7886. }
  7887. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7888. {
  7889. struct drm_device *dev = dev_priv->dev;
  7890. if (IS_HASWELL(dev))
  7891. return I915_READ(D_COMP_HSW);
  7892. else
  7893. return I915_READ(D_COMP_BDW);
  7894. }
  7895. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7896. {
  7897. struct drm_device *dev = dev_priv->dev;
  7898. if (IS_HASWELL(dev)) {
  7899. mutex_lock(&dev_priv->rps.hw_lock);
  7900. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7901. val))
  7902. DRM_ERROR("Failed to write to D_COMP\n");
  7903. mutex_unlock(&dev_priv->rps.hw_lock);
  7904. } else {
  7905. I915_WRITE(D_COMP_BDW, val);
  7906. POSTING_READ(D_COMP_BDW);
  7907. }
  7908. }
  7909. /*
  7910. * This function implements pieces of two sequences from BSpec:
  7911. * - Sequence for display software to disable LCPLL
  7912. * - Sequence for display software to allow package C8+
  7913. * The steps implemented here are just the steps that actually touch the LCPLL
  7914. * register. Callers should take care of disabling all the display engine
  7915. * functions, doing the mode unset, fixing interrupts, etc.
  7916. */
  7917. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7918. bool switch_to_fclk, bool allow_power_down)
  7919. {
  7920. uint32_t val;
  7921. assert_can_disable_lcpll(dev_priv);
  7922. val = I915_READ(LCPLL_CTL);
  7923. if (switch_to_fclk) {
  7924. val |= LCPLL_CD_SOURCE_FCLK;
  7925. I915_WRITE(LCPLL_CTL, val);
  7926. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7927. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7928. DRM_ERROR("Switching to FCLK failed\n");
  7929. val = I915_READ(LCPLL_CTL);
  7930. }
  7931. val |= LCPLL_PLL_DISABLE;
  7932. I915_WRITE(LCPLL_CTL, val);
  7933. POSTING_READ(LCPLL_CTL);
  7934. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7935. DRM_ERROR("LCPLL still locked\n");
  7936. val = hsw_read_dcomp(dev_priv);
  7937. val |= D_COMP_COMP_DISABLE;
  7938. hsw_write_dcomp(dev_priv, val);
  7939. ndelay(100);
  7940. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7941. 1))
  7942. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7943. if (allow_power_down) {
  7944. val = I915_READ(LCPLL_CTL);
  7945. val |= LCPLL_POWER_DOWN_ALLOW;
  7946. I915_WRITE(LCPLL_CTL, val);
  7947. POSTING_READ(LCPLL_CTL);
  7948. }
  7949. }
  7950. /*
  7951. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7952. * source.
  7953. */
  7954. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7955. {
  7956. uint32_t val;
  7957. val = I915_READ(LCPLL_CTL);
  7958. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7959. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7960. return;
  7961. /*
  7962. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7963. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7964. */
  7965. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7966. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7967. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7968. I915_WRITE(LCPLL_CTL, val);
  7969. POSTING_READ(LCPLL_CTL);
  7970. }
  7971. val = hsw_read_dcomp(dev_priv);
  7972. val |= D_COMP_COMP_FORCE;
  7973. val &= ~D_COMP_COMP_DISABLE;
  7974. hsw_write_dcomp(dev_priv, val);
  7975. val = I915_READ(LCPLL_CTL);
  7976. val &= ~LCPLL_PLL_DISABLE;
  7977. I915_WRITE(LCPLL_CTL, val);
  7978. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7979. DRM_ERROR("LCPLL not locked yet\n");
  7980. if (val & LCPLL_CD_SOURCE_FCLK) {
  7981. val = I915_READ(LCPLL_CTL);
  7982. val &= ~LCPLL_CD_SOURCE_FCLK;
  7983. I915_WRITE(LCPLL_CTL, val);
  7984. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7985. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7986. DRM_ERROR("Switching back to LCPLL failed\n");
  7987. }
  7988. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7989. intel_update_cdclk(dev_priv->dev);
  7990. }
  7991. /*
  7992. * Package states C8 and deeper are really deep PC states that can only be
  7993. * reached when all the devices on the system allow it, so even if the graphics
  7994. * device allows PC8+, it doesn't mean the system will actually get to these
  7995. * states. Our driver only allows PC8+ when going into runtime PM.
  7996. *
  7997. * The requirements for PC8+ are that all the outputs are disabled, the power
  7998. * well is disabled and most interrupts are disabled, and these are also
  7999. * requirements for runtime PM. When these conditions are met, we manually do
  8000. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8001. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8002. * hang the machine.
  8003. *
  8004. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8005. * the state of some registers, so when we come back from PC8+ we need to
  8006. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8007. * need to take care of the registers kept by RC6. Notice that this happens even
  8008. * if we don't put the device in PCI D3 state (which is what currently happens
  8009. * because of the runtime PM support).
  8010. *
  8011. * For more, read "Display Sequences for Package C8" on the hardware
  8012. * documentation.
  8013. */
  8014. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8015. {
  8016. struct drm_device *dev = dev_priv->dev;
  8017. uint32_t val;
  8018. DRM_DEBUG_KMS("Enabling package C8+\n");
  8019. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  8020. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8021. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8022. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8023. }
  8024. lpt_disable_clkout_dp(dev);
  8025. hsw_disable_lcpll(dev_priv, true, true);
  8026. }
  8027. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8028. {
  8029. struct drm_device *dev = dev_priv->dev;
  8030. uint32_t val;
  8031. DRM_DEBUG_KMS("Disabling package C8+\n");
  8032. hsw_restore_lcpll(dev_priv);
  8033. lpt_init_pch_refclk(dev);
  8034. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  8035. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8036. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8037. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8038. }
  8039. intel_prepare_ddi(dev);
  8040. }
  8041. static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
  8042. {
  8043. struct drm_device *dev = old_state->dev;
  8044. struct drm_i915_private *dev_priv = dev->dev_private;
  8045. int max_pixclk = intel_mode_max_pixclk(dev, NULL);
  8046. int req_cdclk;
  8047. /* see the comment in valleyview_modeset_global_resources */
  8048. if (WARN_ON(max_pixclk < 0))
  8049. return;
  8050. req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  8051. if (req_cdclk != dev_priv->cdclk_freq)
  8052. broxton_set_cdclk(dev, req_cdclk);
  8053. }
  8054. /* compute the max rate for new configuration */
  8055. static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
  8056. {
  8057. struct drm_device *dev = dev_priv->dev;
  8058. struct intel_crtc *intel_crtc;
  8059. struct drm_crtc *crtc;
  8060. int max_pixel_rate = 0;
  8061. int pixel_rate;
  8062. for_each_crtc(dev, crtc) {
  8063. if (!crtc->state->enable)
  8064. continue;
  8065. intel_crtc = to_intel_crtc(crtc);
  8066. pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
  8067. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8068. if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
  8069. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8070. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  8071. }
  8072. return max_pixel_rate;
  8073. }
  8074. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8075. {
  8076. struct drm_i915_private *dev_priv = dev->dev_private;
  8077. uint32_t val, data;
  8078. int ret;
  8079. if (WARN((I915_READ(LCPLL_CTL) &
  8080. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8081. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8082. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8083. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8084. "trying to change cdclk frequency with cdclk not enabled\n"))
  8085. return;
  8086. mutex_lock(&dev_priv->rps.hw_lock);
  8087. ret = sandybridge_pcode_write(dev_priv,
  8088. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8089. mutex_unlock(&dev_priv->rps.hw_lock);
  8090. if (ret) {
  8091. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8092. return;
  8093. }
  8094. val = I915_READ(LCPLL_CTL);
  8095. val |= LCPLL_CD_SOURCE_FCLK;
  8096. I915_WRITE(LCPLL_CTL, val);
  8097. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  8098. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8099. DRM_ERROR("Switching to FCLK failed\n");
  8100. val = I915_READ(LCPLL_CTL);
  8101. val &= ~LCPLL_CLK_FREQ_MASK;
  8102. switch (cdclk) {
  8103. case 450000:
  8104. val |= LCPLL_CLK_FREQ_450;
  8105. data = 0;
  8106. break;
  8107. case 540000:
  8108. val |= LCPLL_CLK_FREQ_54O_BDW;
  8109. data = 1;
  8110. break;
  8111. case 337500:
  8112. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8113. data = 2;
  8114. break;
  8115. case 675000:
  8116. val |= LCPLL_CLK_FREQ_675_BDW;
  8117. data = 3;
  8118. break;
  8119. default:
  8120. WARN(1, "invalid cdclk frequency\n");
  8121. return;
  8122. }
  8123. I915_WRITE(LCPLL_CTL, val);
  8124. val = I915_READ(LCPLL_CTL);
  8125. val &= ~LCPLL_CD_SOURCE_FCLK;
  8126. I915_WRITE(LCPLL_CTL, val);
  8127. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8128. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8129. DRM_ERROR("Switching back to LCPLL failed\n");
  8130. mutex_lock(&dev_priv->rps.hw_lock);
  8131. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8132. mutex_unlock(&dev_priv->rps.hw_lock);
  8133. intel_update_cdclk(dev);
  8134. WARN(cdclk != dev_priv->cdclk_freq,
  8135. "cdclk requested %d kHz but got %d kHz\n",
  8136. cdclk, dev_priv->cdclk_freq);
  8137. }
  8138. static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
  8139. int max_pixel_rate)
  8140. {
  8141. int cdclk;
  8142. /*
  8143. * FIXME should also account for plane ratio
  8144. * once 64bpp pixel formats are supported.
  8145. */
  8146. if (max_pixel_rate > 540000)
  8147. cdclk = 675000;
  8148. else if (max_pixel_rate > 450000)
  8149. cdclk = 540000;
  8150. else if (max_pixel_rate > 337500)
  8151. cdclk = 450000;
  8152. else
  8153. cdclk = 337500;
  8154. /*
  8155. * FIXME move the cdclk caclulation to
  8156. * compute_config() so we can fail gracegully.
  8157. */
  8158. if (cdclk > dev_priv->max_cdclk_freq) {
  8159. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8160. cdclk, dev_priv->max_cdclk_freq);
  8161. cdclk = dev_priv->max_cdclk_freq;
  8162. }
  8163. return cdclk;
  8164. }
  8165. static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
  8166. {
  8167. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8168. struct drm_crtc *crtc;
  8169. struct drm_crtc_state *crtc_state;
  8170. int max_pixclk = ilk_max_pixel_rate(dev_priv);
  8171. int cdclk, i;
  8172. cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
  8173. if (cdclk == dev_priv->cdclk_freq)
  8174. return 0;
  8175. /* add all active pipes to the state */
  8176. for_each_crtc(state->dev, crtc) {
  8177. if (!crtc->state->enable)
  8178. continue;
  8179. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  8180. if (IS_ERR(crtc_state))
  8181. return PTR_ERR(crtc_state);
  8182. }
  8183. /* disable/enable all currently active pipes while we change cdclk */
  8184. for_each_crtc_in_state(state, crtc, crtc_state, i)
  8185. if (crtc_state->enable)
  8186. crtc_state->mode_changed = true;
  8187. return 0;
  8188. }
  8189. static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
  8190. {
  8191. struct drm_device *dev = state->dev;
  8192. struct drm_i915_private *dev_priv = dev->dev_private;
  8193. int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
  8194. int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
  8195. if (req_cdclk != dev_priv->cdclk_freq)
  8196. broadwell_set_cdclk(dev, req_cdclk);
  8197. }
  8198. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8199. struct intel_crtc_state *crtc_state)
  8200. {
  8201. if (!intel_ddi_pll_select(crtc, crtc_state))
  8202. return -EINVAL;
  8203. crtc->lowfreq_avail = false;
  8204. return 0;
  8205. }
  8206. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8207. enum port port,
  8208. struct intel_crtc_state *pipe_config)
  8209. {
  8210. switch (port) {
  8211. case PORT_A:
  8212. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8213. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8214. break;
  8215. case PORT_B:
  8216. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8217. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8218. break;
  8219. case PORT_C:
  8220. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8221. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8222. break;
  8223. default:
  8224. DRM_ERROR("Incorrect port type\n");
  8225. }
  8226. }
  8227. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8228. enum port port,
  8229. struct intel_crtc_state *pipe_config)
  8230. {
  8231. u32 temp, dpll_ctl1;
  8232. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8233. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8234. switch (pipe_config->ddi_pll_sel) {
  8235. case SKL_DPLL0:
  8236. /*
  8237. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8238. * of the shared DPLL framework and thus needs to be read out
  8239. * separately
  8240. */
  8241. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8242. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8243. break;
  8244. case SKL_DPLL1:
  8245. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8246. break;
  8247. case SKL_DPLL2:
  8248. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8249. break;
  8250. case SKL_DPLL3:
  8251. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8252. break;
  8253. }
  8254. }
  8255. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8256. enum port port,
  8257. struct intel_crtc_state *pipe_config)
  8258. {
  8259. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8260. switch (pipe_config->ddi_pll_sel) {
  8261. case PORT_CLK_SEL_WRPLL1:
  8262. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8263. break;
  8264. case PORT_CLK_SEL_WRPLL2:
  8265. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8266. break;
  8267. }
  8268. }
  8269. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8270. struct intel_crtc_state *pipe_config)
  8271. {
  8272. struct drm_device *dev = crtc->base.dev;
  8273. struct drm_i915_private *dev_priv = dev->dev_private;
  8274. struct intel_shared_dpll *pll;
  8275. enum port port;
  8276. uint32_t tmp;
  8277. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8278. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8279. if (IS_SKYLAKE(dev))
  8280. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8281. else if (IS_BROXTON(dev))
  8282. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8283. else
  8284. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8285. if (pipe_config->shared_dpll >= 0) {
  8286. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8287. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8288. &pipe_config->dpll_hw_state));
  8289. }
  8290. /*
  8291. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8292. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8293. * the PCH transcoder is on.
  8294. */
  8295. if (INTEL_INFO(dev)->gen < 9 &&
  8296. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8297. pipe_config->has_pch_encoder = true;
  8298. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8299. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8300. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8301. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8302. }
  8303. }
  8304. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8305. struct intel_crtc_state *pipe_config)
  8306. {
  8307. struct drm_device *dev = crtc->base.dev;
  8308. struct drm_i915_private *dev_priv = dev->dev_private;
  8309. enum intel_display_power_domain pfit_domain;
  8310. uint32_t tmp;
  8311. if (!intel_display_power_is_enabled(dev_priv,
  8312. POWER_DOMAIN_PIPE(crtc->pipe)))
  8313. return false;
  8314. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8315. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8316. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8317. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8318. enum pipe trans_edp_pipe;
  8319. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8320. default:
  8321. WARN(1, "unknown pipe linked to edp transcoder\n");
  8322. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8323. case TRANS_DDI_EDP_INPUT_A_ON:
  8324. trans_edp_pipe = PIPE_A;
  8325. break;
  8326. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8327. trans_edp_pipe = PIPE_B;
  8328. break;
  8329. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8330. trans_edp_pipe = PIPE_C;
  8331. break;
  8332. }
  8333. if (trans_edp_pipe == crtc->pipe)
  8334. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8335. }
  8336. if (!intel_display_power_is_enabled(dev_priv,
  8337. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8338. return false;
  8339. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8340. if (!(tmp & PIPECONF_ENABLE))
  8341. return false;
  8342. haswell_get_ddi_port_state(crtc, pipe_config);
  8343. intel_get_pipe_timings(crtc, pipe_config);
  8344. if (INTEL_INFO(dev)->gen >= 9) {
  8345. skl_init_scalers(dev, crtc, pipe_config);
  8346. }
  8347. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8348. if (INTEL_INFO(dev)->gen >= 9) {
  8349. pipe_config->scaler_state.scaler_id = -1;
  8350. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8351. }
  8352. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8353. if (INTEL_INFO(dev)->gen == 9)
  8354. skylake_get_pfit_config(crtc, pipe_config);
  8355. else if (INTEL_INFO(dev)->gen < 9)
  8356. ironlake_get_pfit_config(crtc, pipe_config);
  8357. else
  8358. MISSING_CASE(INTEL_INFO(dev)->gen);
  8359. }
  8360. if (IS_HASWELL(dev))
  8361. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8362. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8363. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8364. pipe_config->pixel_multiplier =
  8365. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8366. } else {
  8367. pipe_config->pixel_multiplier = 1;
  8368. }
  8369. return true;
  8370. }
  8371. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8372. {
  8373. struct drm_device *dev = crtc->dev;
  8374. struct drm_i915_private *dev_priv = dev->dev_private;
  8375. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8376. uint32_t cntl = 0, size = 0;
  8377. if (base) {
  8378. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8379. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8380. unsigned int stride = roundup_pow_of_two(width) * 4;
  8381. switch (stride) {
  8382. default:
  8383. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8384. width, stride);
  8385. stride = 256;
  8386. /* fallthrough */
  8387. case 256:
  8388. case 512:
  8389. case 1024:
  8390. case 2048:
  8391. break;
  8392. }
  8393. cntl |= CURSOR_ENABLE |
  8394. CURSOR_GAMMA_ENABLE |
  8395. CURSOR_FORMAT_ARGB |
  8396. CURSOR_STRIDE(stride);
  8397. size = (height << 12) | width;
  8398. }
  8399. if (intel_crtc->cursor_cntl != 0 &&
  8400. (intel_crtc->cursor_base != base ||
  8401. intel_crtc->cursor_size != size ||
  8402. intel_crtc->cursor_cntl != cntl)) {
  8403. /* On these chipsets we can only modify the base/size/stride
  8404. * whilst the cursor is disabled.
  8405. */
  8406. I915_WRITE(_CURACNTR, 0);
  8407. POSTING_READ(_CURACNTR);
  8408. intel_crtc->cursor_cntl = 0;
  8409. }
  8410. if (intel_crtc->cursor_base != base) {
  8411. I915_WRITE(_CURABASE, base);
  8412. intel_crtc->cursor_base = base;
  8413. }
  8414. if (intel_crtc->cursor_size != size) {
  8415. I915_WRITE(CURSIZE, size);
  8416. intel_crtc->cursor_size = size;
  8417. }
  8418. if (intel_crtc->cursor_cntl != cntl) {
  8419. I915_WRITE(_CURACNTR, cntl);
  8420. POSTING_READ(_CURACNTR);
  8421. intel_crtc->cursor_cntl = cntl;
  8422. }
  8423. }
  8424. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8425. {
  8426. struct drm_device *dev = crtc->dev;
  8427. struct drm_i915_private *dev_priv = dev->dev_private;
  8428. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8429. int pipe = intel_crtc->pipe;
  8430. uint32_t cntl;
  8431. cntl = 0;
  8432. if (base) {
  8433. cntl = MCURSOR_GAMMA_ENABLE;
  8434. switch (intel_crtc->base.cursor->state->crtc_w) {
  8435. case 64:
  8436. cntl |= CURSOR_MODE_64_ARGB_AX;
  8437. break;
  8438. case 128:
  8439. cntl |= CURSOR_MODE_128_ARGB_AX;
  8440. break;
  8441. case 256:
  8442. cntl |= CURSOR_MODE_256_ARGB_AX;
  8443. break;
  8444. default:
  8445. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8446. return;
  8447. }
  8448. cntl |= pipe << 28; /* Connect to correct pipe */
  8449. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  8450. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8451. }
  8452. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8453. cntl |= CURSOR_ROTATE_180;
  8454. if (intel_crtc->cursor_cntl != cntl) {
  8455. I915_WRITE(CURCNTR(pipe), cntl);
  8456. POSTING_READ(CURCNTR(pipe));
  8457. intel_crtc->cursor_cntl = cntl;
  8458. }
  8459. /* and commit changes on next vblank */
  8460. I915_WRITE(CURBASE(pipe), base);
  8461. POSTING_READ(CURBASE(pipe));
  8462. intel_crtc->cursor_base = base;
  8463. }
  8464. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8465. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8466. bool on)
  8467. {
  8468. struct drm_device *dev = crtc->dev;
  8469. struct drm_i915_private *dev_priv = dev->dev_private;
  8470. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8471. int pipe = intel_crtc->pipe;
  8472. int x = crtc->cursor_x;
  8473. int y = crtc->cursor_y;
  8474. u32 base = 0, pos = 0;
  8475. if (on)
  8476. base = intel_crtc->cursor_addr;
  8477. if (x >= intel_crtc->config->pipe_src_w)
  8478. base = 0;
  8479. if (y >= intel_crtc->config->pipe_src_h)
  8480. base = 0;
  8481. if (x < 0) {
  8482. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  8483. base = 0;
  8484. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8485. x = -x;
  8486. }
  8487. pos |= x << CURSOR_X_SHIFT;
  8488. if (y < 0) {
  8489. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  8490. base = 0;
  8491. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8492. y = -y;
  8493. }
  8494. pos |= y << CURSOR_Y_SHIFT;
  8495. if (base == 0 && intel_crtc->cursor_base == 0)
  8496. return;
  8497. I915_WRITE(CURPOS(pipe), pos);
  8498. /* ILK+ do this automagically */
  8499. if (HAS_GMCH_DISPLAY(dev) &&
  8500. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8501. base += (intel_crtc->base.cursor->state->crtc_h *
  8502. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8503. }
  8504. if (IS_845G(dev) || IS_I865G(dev))
  8505. i845_update_cursor(crtc, base);
  8506. else
  8507. i9xx_update_cursor(crtc, base);
  8508. }
  8509. static bool cursor_size_ok(struct drm_device *dev,
  8510. uint32_t width, uint32_t height)
  8511. {
  8512. if (width == 0 || height == 0)
  8513. return false;
  8514. /*
  8515. * 845g/865g are special in that they are only limited by
  8516. * the width of their cursors, the height is arbitrary up to
  8517. * the precision of the register. Everything else requires
  8518. * square cursors, limited to a few power-of-two sizes.
  8519. */
  8520. if (IS_845G(dev) || IS_I865G(dev)) {
  8521. if ((width & 63) != 0)
  8522. return false;
  8523. if (width > (IS_845G(dev) ? 64 : 512))
  8524. return false;
  8525. if (height > 1023)
  8526. return false;
  8527. } else {
  8528. switch (width | height) {
  8529. case 256:
  8530. case 128:
  8531. if (IS_GEN2(dev))
  8532. return false;
  8533. case 64:
  8534. break;
  8535. default:
  8536. return false;
  8537. }
  8538. }
  8539. return true;
  8540. }
  8541. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8542. u16 *blue, uint32_t start, uint32_t size)
  8543. {
  8544. int end = (start + size > 256) ? 256 : start + size, i;
  8545. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8546. for (i = start; i < end; i++) {
  8547. intel_crtc->lut_r[i] = red[i] >> 8;
  8548. intel_crtc->lut_g[i] = green[i] >> 8;
  8549. intel_crtc->lut_b[i] = blue[i] >> 8;
  8550. }
  8551. intel_crtc_load_lut(crtc);
  8552. }
  8553. /* VESA 640x480x72Hz mode to set on the pipe */
  8554. static struct drm_display_mode load_detect_mode = {
  8555. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8556. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8557. };
  8558. struct drm_framebuffer *
  8559. __intel_framebuffer_create(struct drm_device *dev,
  8560. struct drm_mode_fb_cmd2 *mode_cmd,
  8561. struct drm_i915_gem_object *obj)
  8562. {
  8563. struct intel_framebuffer *intel_fb;
  8564. int ret;
  8565. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8566. if (!intel_fb) {
  8567. drm_gem_object_unreference(&obj->base);
  8568. return ERR_PTR(-ENOMEM);
  8569. }
  8570. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8571. if (ret)
  8572. goto err;
  8573. return &intel_fb->base;
  8574. err:
  8575. drm_gem_object_unreference(&obj->base);
  8576. kfree(intel_fb);
  8577. return ERR_PTR(ret);
  8578. }
  8579. static struct drm_framebuffer *
  8580. intel_framebuffer_create(struct drm_device *dev,
  8581. struct drm_mode_fb_cmd2 *mode_cmd,
  8582. struct drm_i915_gem_object *obj)
  8583. {
  8584. struct drm_framebuffer *fb;
  8585. int ret;
  8586. ret = i915_mutex_lock_interruptible(dev);
  8587. if (ret)
  8588. return ERR_PTR(ret);
  8589. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8590. mutex_unlock(&dev->struct_mutex);
  8591. return fb;
  8592. }
  8593. static u32
  8594. intel_framebuffer_pitch_for_width(int width, int bpp)
  8595. {
  8596. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8597. return ALIGN(pitch, 64);
  8598. }
  8599. static u32
  8600. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8601. {
  8602. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8603. return PAGE_ALIGN(pitch * mode->vdisplay);
  8604. }
  8605. static struct drm_framebuffer *
  8606. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8607. struct drm_display_mode *mode,
  8608. int depth, int bpp)
  8609. {
  8610. struct drm_i915_gem_object *obj;
  8611. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8612. obj = i915_gem_alloc_object(dev,
  8613. intel_framebuffer_size_for_mode(mode, bpp));
  8614. if (obj == NULL)
  8615. return ERR_PTR(-ENOMEM);
  8616. mode_cmd.width = mode->hdisplay;
  8617. mode_cmd.height = mode->vdisplay;
  8618. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8619. bpp);
  8620. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8621. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8622. }
  8623. static struct drm_framebuffer *
  8624. mode_fits_in_fbdev(struct drm_device *dev,
  8625. struct drm_display_mode *mode)
  8626. {
  8627. #ifdef CONFIG_DRM_I915_FBDEV
  8628. struct drm_i915_private *dev_priv = dev->dev_private;
  8629. struct drm_i915_gem_object *obj;
  8630. struct drm_framebuffer *fb;
  8631. if (!dev_priv->fbdev)
  8632. return NULL;
  8633. if (!dev_priv->fbdev->fb)
  8634. return NULL;
  8635. obj = dev_priv->fbdev->fb->obj;
  8636. BUG_ON(!obj);
  8637. fb = &dev_priv->fbdev->fb->base;
  8638. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8639. fb->bits_per_pixel))
  8640. return NULL;
  8641. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8642. return NULL;
  8643. return fb;
  8644. #else
  8645. return NULL;
  8646. #endif
  8647. }
  8648. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8649. struct drm_crtc *crtc,
  8650. struct drm_display_mode *mode,
  8651. struct drm_framebuffer *fb,
  8652. int x, int y)
  8653. {
  8654. struct drm_plane_state *plane_state;
  8655. int hdisplay, vdisplay;
  8656. int ret;
  8657. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8658. if (IS_ERR(plane_state))
  8659. return PTR_ERR(plane_state);
  8660. if (mode)
  8661. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8662. else
  8663. hdisplay = vdisplay = 0;
  8664. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8665. if (ret)
  8666. return ret;
  8667. drm_atomic_set_fb_for_plane(plane_state, fb);
  8668. plane_state->crtc_x = 0;
  8669. plane_state->crtc_y = 0;
  8670. plane_state->crtc_w = hdisplay;
  8671. plane_state->crtc_h = vdisplay;
  8672. plane_state->src_x = x << 16;
  8673. plane_state->src_y = y << 16;
  8674. plane_state->src_w = hdisplay << 16;
  8675. plane_state->src_h = vdisplay << 16;
  8676. return 0;
  8677. }
  8678. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8679. struct drm_display_mode *mode,
  8680. struct intel_load_detect_pipe *old,
  8681. struct drm_modeset_acquire_ctx *ctx)
  8682. {
  8683. struct intel_crtc *intel_crtc;
  8684. struct intel_encoder *intel_encoder =
  8685. intel_attached_encoder(connector);
  8686. struct drm_crtc *possible_crtc;
  8687. struct drm_encoder *encoder = &intel_encoder->base;
  8688. struct drm_crtc *crtc = NULL;
  8689. struct drm_device *dev = encoder->dev;
  8690. struct drm_framebuffer *fb;
  8691. struct drm_mode_config *config = &dev->mode_config;
  8692. struct drm_atomic_state *state = NULL;
  8693. struct drm_connector_state *connector_state;
  8694. struct intel_crtc_state *crtc_state;
  8695. int ret, i = -1;
  8696. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8697. connector->base.id, connector->name,
  8698. encoder->base.id, encoder->name);
  8699. retry:
  8700. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8701. if (ret)
  8702. goto fail;
  8703. /*
  8704. * Algorithm gets a little messy:
  8705. *
  8706. * - if the connector already has an assigned crtc, use it (but make
  8707. * sure it's on first)
  8708. *
  8709. * - try to find the first unused crtc that can drive this connector,
  8710. * and use that if we find one
  8711. */
  8712. /* See if we already have a CRTC for this connector */
  8713. if (encoder->crtc) {
  8714. crtc = encoder->crtc;
  8715. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8716. if (ret)
  8717. goto fail;
  8718. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8719. if (ret)
  8720. goto fail;
  8721. old->dpms_mode = connector->dpms;
  8722. old->load_detect_temp = false;
  8723. /* Make sure the crtc and connector are running */
  8724. if (connector->dpms != DRM_MODE_DPMS_ON)
  8725. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8726. return true;
  8727. }
  8728. /* Find an unused one (if possible) */
  8729. for_each_crtc(dev, possible_crtc) {
  8730. i++;
  8731. if (!(encoder->possible_crtcs & (1 << i)))
  8732. continue;
  8733. if (possible_crtc->state->enable)
  8734. continue;
  8735. crtc = possible_crtc;
  8736. break;
  8737. }
  8738. /*
  8739. * If we didn't find an unused CRTC, don't use any.
  8740. */
  8741. if (!crtc) {
  8742. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8743. goto fail;
  8744. }
  8745. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8746. if (ret)
  8747. goto fail;
  8748. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8749. if (ret)
  8750. goto fail;
  8751. intel_crtc = to_intel_crtc(crtc);
  8752. old->dpms_mode = connector->dpms;
  8753. old->load_detect_temp = true;
  8754. old->release_fb = NULL;
  8755. state = drm_atomic_state_alloc(dev);
  8756. if (!state)
  8757. return false;
  8758. state->acquire_ctx = ctx;
  8759. connector_state = drm_atomic_get_connector_state(state, connector);
  8760. if (IS_ERR(connector_state)) {
  8761. ret = PTR_ERR(connector_state);
  8762. goto fail;
  8763. }
  8764. connector_state->crtc = crtc;
  8765. connector_state->best_encoder = &intel_encoder->base;
  8766. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8767. if (IS_ERR(crtc_state)) {
  8768. ret = PTR_ERR(crtc_state);
  8769. goto fail;
  8770. }
  8771. crtc_state->base.active = crtc_state->base.enable = true;
  8772. if (!mode)
  8773. mode = &load_detect_mode;
  8774. /* We need a framebuffer large enough to accommodate all accesses
  8775. * that the plane may generate whilst we perform load detection.
  8776. * We can not rely on the fbcon either being present (we get called
  8777. * during its initialisation to detect all boot displays, or it may
  8778. * not even exist) or that it is large enough to satisfy the
  8779. * requested mode.
  8780. */
  8781. fb = mode_fits_in_fbdev(dev, mode);
  8782. if (fb == NULL) {
  8783. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8784. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8785. old->release_fb = fb;
  8786. } else
  8787. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8788. if (IS_ERR(fb)) {
  8789. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8790. goto fail;
  8791. }
  8792. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8793. if (ret)
  8794. goto fail;
  8795. drm_mode_copy(&crtc_state->base.mode, mode);
  8796. if (intel_set_mode(state)) {
  8797. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8798. if (old->release_fb)
  8799. old->release_fb->funcs->destroy(old->release_fb);
  8800. goto fail;
  8801. }
  8802. crtc->primary->crtc = crtc;
  8803. /* let the connector get through one full cycle before testing */
  8804. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8805. return true;
  8806. fail:
  8807. drm_atomic_state_free(state);
  8808. state = NULL;
  8809. if (ret == -EDEADLK) {
  8810. drm_modeset_backoff(ctx);
  8811. goto retry;
  8812. }
  8813. return false;
  8814. }
  8815. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8816. struct intel_load_detect_pipe *old,
  8817. struct drm_modeset_acquire_ctx *ctx)
  8818. {
  8819. struct drm_device *dev = connector->dev;
  8820. struct intel_encoder *intel_encoder =
  8821. intel_attached_encoder(connector);
  8822. struct drm_encoder *encoder = &intel_encoder->base;
  8823. struct drm_crtc *crtc = encoder->crtc;
  8824. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8825. struct drm_atomic_state *state;
  8826. struct drm_connector_state *connector_state;
  8827. struct intel_crtc_state *crtc_state;
  8828. int ret;
  8829. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8830. connector->base.id, connector->name,
  8831. encoder->base.id, encoder->name);
  8832. if (old->load_detect_temp) {
  8833. state = drm_atomic_state_alloc(dev);
  8834. if (!state)
  8835. goto fail;
  8836. state->acquire_ctx = ctx;
  8837. connector_state = drm_atomic_get_connector_state(state, connector);
  8838. if (IS_ERR(connector_state))
  8839. goto fail;
  8840. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8841. if (IS_ERR(crtc_state))
  8842. goto fail;
  8843. connector_state->best_encoder = NULL;
  8844. connector_state->crtc = NULL;
  8845. crtc_state->base.enable = crtc_state->base.active = false;
  8846. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8847. 0, 0);
  8848. if (ret)
  8849. goto fail;
  8850. ret = intel_set_mode(state);
  8851. if (ret)
  8852. goto fail;
  8853. if (old->release_fb) {
  8854. drm_framebuffer_unregister_private(old->release_fb);
  8855. drm_framebuffer_unreference(old->release_fb);
  8856. }
  8857. return;
  8858. }
  8859. /* Switch crtc and encoder back off if necessary */
  8860. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8861. connector->funcs->dpms(connector, old->dpms_mode);
  8862. return;
  8863. fail:
  8864. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8865. drm_atomic_state_free(state);
  8866. }
  8867. static int i9xx_pll_refclk(struct drm_device *dev,
  8868. const struct intel_crtc_state *pipe_config)
  8869. {
  8870. struct drm_i915_private *dev_priv = dev->dev_private;
  8871. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8872. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8873. return dev_priv->vbt.lvds_ssc_freq;
  8874. else if (HAS_PCH_SPLIT(dev))
  8875. return 120000;
  8876. else if (!IS_GEN2(dev))
  8877. return 96000;
  8878. else
  8879. return 48000;
  8880. }
  8881. /* Returns the clock of the currently programmed mode of the given pipe. */
  8882. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8883. struct intel_crtc_state *pipe_config)
  8884. {
  8885. struct drm_device *dev = crtc->base.dev;
  8886. struct drm_i915_private *dev_priv = dev->dev_private;
  8887. int pipe = pipe_config->cpu_transcoder;
  8888. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8889. u32 fp;
  8890. intel_clock_t clock;
  8891. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8892. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8893. fp = pipe_config->dpll_hw_state.fp0;
  8894. else
  8895. fp = pipe_config->dpll_hw_state.fp1;
  8896. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8897. if (IS_PINEVIEW(dev)) {
  8898. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8899. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8900. } else {
  8901. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8902. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8903. }
  8904. if (!IS_GEN2(dev)) {
  8905. if (IS_PINEVIEW(dev))
  8906. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8907. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8908. else
  8909. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8910. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8911. switch (dpll & DPLL_MODE_MASK) {
  8912. case DPLLB_MODE_DAC_SERIAL:
  8913. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8914. 5 : 10;
  8915. break;
  8916. case DPLLB_MODE_LVDS:
  8917. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8918. 7 : 14;
  8919. break;
  8920. default:
  8921. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8922. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8923. return;
  8924. }
  8925. if (IS_PINEVIEW(dev))
  8926. pineview_clock(refclk, &clock);
  8927. else
  8928. i9xx_clock(refclk, &clock);
  8929. } else {
  8930. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8931. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8932. if (is_lvds) {
  8933. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8934. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8935. if (lvds & LVDS_CLKB_POWER_UP)
  8936. clock.p2 = 7;
  8937. else
  8938. clock.p2 = 14;
  8939. } else {
  8940. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8941. clock.p1 = 2;
  8942. else {
  8943. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8944. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8945. }
  8946. if (dpll & PLL_P2_DIVIDE_BY_4)
  8947. clock.p2 = 4;
  8948. else
  8949. clock.p2 = 2;
  8950. }
  8951. i9xx_clock(refclk, &clock);
  8952. }
  8953. /*
  8954. * This value includes pixel_multiplier. We will use
  8955. * port_clock to compute adjusted_mode.crtc_clock in the
  8956. * encoder's get_config() function.
  8957. */
  8958. pipe_config->port_clock = clock.dot;
  8959. }
  8960. int intel_dotclock_calculate(int link_freq,
  8961. const struct intel_link_m_n *m_n)
  8962. {
  8963. /*
  8964. * The calculation for the data clock is:
  8965. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8966. * But we want to avoid losing precison if possible, so:
  8967. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8968. *
  8969. * and the link clock is simpler:
  8970. * link_clock = (m * link_clock) / n
  8971. */
  8972. if (!m_n->link_n)
  8973. return 0;
  8974. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8975. }
  8976. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8977. struct intel_crtc_state *pipe_config)
  8978. {
  8979. struct drm_device *dev = crtc->base.dev;
  8980. /* read out port_clock from the DPLL */
  8981. i9xx_crtc_clock_get(crtc, pipe_config);
  8982. /*
  8983. * This value does not include pixel_multiplier.
  8984. * We will check that port_clock and adjusted_mode.crtc_clock
  8985. * agree once we know their relationship in the encoder's
  8986. * get_config() function.
  8987. */
  8988. pipe_config->base.adjusted_mode.crtc_clock =
  8989. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8990. &pipe_config->fdi_m_n);
  8991. }
  8992. /** Returns the currently programmed mode of the given pipe. */
  8993. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8994. struct drm_crtc *crtc)
  8995. {
  8996. struct drm_i915_private *dev_priv = dev->dev_private;
  8997. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8998. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8999. struct drm_display_mode *mode;
  9000. struct intel_crtc_state pipe_config;
  9001. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9002. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9003. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9004. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9005. enum pipe pipe = intel_crtc->pipe;
  9006. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9007. if (!mode)
  9008. return NULL;
  9009. /*
  9010. * Construct a pipe_config sufficient for getting the clock info
  9011. * back out of crtc_clock_get.
  9012. *
  9013. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9014. * to use a real value here instead.
  9015. */
  9016. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  9017. pipe_config.pixel_multiplier = 1;
  9018. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9019. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9020. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9021. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  9022. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  9023. mode->hdisplay = (htot & 0xffff) + 1;
  9024. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9025. mode->hsync_start = (hsync & 0xffff) + 1;
  9026. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9027. mode->vdisplay = (vtot & 0xffff) + 1;
  9028. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9029. mode->vsync_start = (vsync & 0xffff) + 1;
  9030. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9031. drm_mode_set_name(mode);
  9032. return mode;
  9033. }
  9034. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  9035. {
  9036. struct drm_device *dev = crtc->dev;
  9037. struct drm_i915_private *dev_priv = dev->dev_private;
  9038. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9039. if (!HAS_GMCH_DISPLAY(dev))
  9040. return;
  9041. if (!dev_priv->lvds_downclock_avail)
  9042. return;
  9043. /*
  9044. * Since this is called by a timer, we should never get here in
  9045. * the manual case.
  9046. */
  9047. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  9048. int pipe = intel_crtc->pipe;
  9049. int dpll_reg = DPLL(pipe);
  9050. int dpll;
  9051. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  9052. assert_panel_unlocked(dev_priv, pipe);
  9053. dpll = I915_READ(dpll_reg);
  9054. dpll |= DISPLAY_RATE_SELECT_FPA1;
  9055. I915_WRITE(dpll_reg, dpll);
  9056. intel_wait_for_vblank(dev, pipe);
  9057. dpll = I915_READ(dpll_reg);
  9058. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  9059. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  9060. }
  9061. }
  9062. void intel_mark_busy(struct drm_device *dev)
  9063. {
  9064. struct drm_i915_private *dev_priv = dev->dev_private;
  9065. if (dev_priv->mm.busy)
  9066. return;
  9067. intel_runtime_pm_get(dev_priv);
  9068. i915_update_gfx_val(dev_priv);
  9069. if (INTEL_INFO(dev)->gen >= 6)
  9070. gen6_rps_busy(dev_priv);
  9071. dev_priv->mm.busy = true;
  9072. }
  9073. void intel_mark_idle(struct drm_device *dev)
  9074. {
  9075. struct drm_i915_private *dev_priv = dev->dev_private;
  9076. struct drm_crtc *crtc;
  9077. if (!dev_priv->mm.busy)
  9078. return;
  9079. dev_priv->mm.busy = false;
  9080. for_each_crtc(dev, crtc) {
  9081. if (!crtc->primary->fb)
  9082. continue;
  9083. intel_decrease_pllclock(crtc);
  9084. }
  9085. if (INTEL_INFO(dev)->gen >= 6)
  9086. gen6_rps_idle(dev->dev_private);
  9087. intel_runtime_pm_put(dev_priv);
  9088. }
  9089. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9090. {
  9091. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9092. struct drm_device *dev = crtc->dev;
  9093. struct intel_unpin_work *work;
  9094. spin_lock_irq(&dev->event_lock);
  9095. work = intel_crtc->unpin_work;
  9096. intel_crtc->unpin_work = NULL;
  9097. spin_unlock_irq(&dev->event_lock);
  9098. if (work) {
  9099. cancel_work_sync(&work->work);
  9100. kfree(work);
  9101. }
  9102. drm_crtc_cleanup(crtc);
  9103. kfree(intel_crtc);
  9104. }
  9105. static void intel_unpin_work_fn(struct work_struct *__work)
  9106. {
  9107. struct intel_unpin_work *work =
  9108. container_of(__work, struct intel_unpin_work, work);
  9109. struct drm_device *dev = work->crtc->dev;
  9110. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  9111. mutex_lock(&dev->struct_mutex);
  9112. intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
  9113. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9114. intel_fbc_update(dev);
  9115. if (work->flip_queued_req)
  9116. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9117. mutex_unlock(&dev->struct_mutex);
  9118. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9119. drm_framebuffer_unreference(work->old_fb);
  9120. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  9121. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  9122. kfree(work);
  9123. }
  9124. static void do_intel_finish_page_flip(struct drm_device *dev,
  9125. struct drm_crtc *crtc)
  9126. {
  9127. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9128. struct intel_unpin_work *work;
  9129. unsigned long flags;
  9130. /* Ignore early vblank irqs */
  9131. if (intel_crtc == NULL)
  9132. return;
  9133. /*
  9134. * This is called both by irq handlers and the reset code (to complete
  9135. * lost pageflips) so needs the full irqsave spinlocks.
  9136. */
  9137. spin_lock_irqsave(&dev->event_lock, flags);
  9138. work = intel_crtc->unpin_work;
  9139. /* Ensure we don't miss a work->pending update ... */
  9140. smp_rmb();
  9141. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  9142. spin_unlock_irqrestore(&dev->event_lock, flags);
  9143. return;
  9144. }
  9145. page_flip_completed(intel_crtc);
  9146. spin_unlock_irqrestore(&dev->event_lock, flags);
  9147. }
  9148. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9149. {
  9150. struct drm_i915_private *dev_priv = dev->dev_private;
  9151. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9152. do_intel_finish_page_flip(dev, crtc);
  9153. }
  9154. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9155. {
  9156. struct drm_i915_private *dev_priv = dev->dev_private;
  9157. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9158. do_intel_finish_page_flip(dev, crtc);
  9159. }
  9160. /* Is 'a' after or equal to 'b'? */
  9161. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9162. {
  9163. return !((a - b) & 0x80000000);
  9164. }
  9165. static bool page_flip_finished(struct intel_crtc *crtc)
  9166. {
  9167. struct drm_device *dev = crtc->base.dev;
  9168. struct drm_i915_private *dev_priv = dev->dev_private;
  9169. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9170. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9171. return true;
  9172. /*
  9173. * The relevant registers doen't exist on pre-ctg.
  9174. * As the flip done interrupt doesn't trigger for mmio
  9175. * flips on gmch platforms, a flip count check isn't
  9176. * really needed there. But since ctg has the registers,
  9177. * include it in the check anyway.
  9178. */
  9179. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9180. return true;
  9181. /*
  9182. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9183. * used the same base address. In that case the mmio flip might
  9184. * have completed, but the CS hasn't even executed the flip yet.
  9185. *
  9186. * A flip count check isn't enough as the CS might have updated
  9187. * the base address just after start of vblank, but before we
  9188. * managed to process the interrupt. This means we'd complete the
  9189. * CS flip too soon.
  9190. *
  9191. * Combining both checks should get us a good enough result. It may
  9192. * still happen that the CS flip has been executed, but has not
  9193. * yet actually completed. But in case the base address is the same
  9194. * anyway, we don't really care.
  9195. */
  9196. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9197. crtc->unpin_work->gtt_offset &&
  9198. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  9199. crtc->unpin_work->flip_count);
  9200. }
  9201. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9202. {
  9203. struct drm_i915_private *dev_priv = dev->dev_private;
  9204. struct intel_crtc *intel_crtc =
  9205. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9206. unsigned long flags;
  9207. /*
  9208. * This is called both by irq handlers and the reset code (to complete
  9209. * lost pageflips) so needs the full irqsave spinlocks.
  9210. *
  9211. * NB: An MMIO update of the plane base pointer will also
  9212. * generate a page-flip completion irq, i.e. every modeset
  9213. * is also accompanied by a spurious intel_prepare_page_flip().
  9214. */
  9215. spin_lock_irqsave(&dev->event_lock, flags);
  9216. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9217. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9218. spin_unlock_irqrestore(&dev->event_lock, flags);
  9219. }
  9220. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  9221. {
  9222. /* Ensure that the work item is consistent when activating it ... */
  9223. smp_wmb();
  9224. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  9225. /* and that it is marked active as soon as the irq could fire. */
  9226. smp_wmb();
  9227. }
  9228. static int intel_gen2_queue_flip(struct drm_device *dev,
  9229. struct drm_crtc *crtc,
  9230. struct drm_framebuffer *fb,
  9231. struct drm_i915_gem_object *obj,
  9232. struct intel_engine_cs *ring,
  9233. uint32_t flags)
  9234. {
  9235. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9236. u32 flip_mask;
  9237. int ret;
  9238. ret = intel_ring_begin(ring, 6);
  9239. if (ret)
  9240. return ret;
  9241. /* Can't queue multiple flips, so wait for the previous
  9242. * one to finish before executing the next.
  9243. */
  9244. if (intel_crtc->plane)
  9245. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9246. else
  9247. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9248. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9249. intel_ring_emit(ring, MI_NOOP);
  9250. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9251. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9252. intel_ring_emit(ring, fb->pitches[0]);
  9253. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9254. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9255. intel_mark_page_flip_active(intel_crtc);
  9256. __intel_ring_advance(ring);
  9257. return 0;
  9258. }
  9259. static int intel_gen3_queue_flip(struct drm_device *dev,
  9260. struct drm_crtc *crtc,
  9261. struct drm_framebuffer *fb,
  9262. struct drm_i915_gem_object *obj,
  9263. struct intel_engine_cs *ring,
  9264. uint32_t flags)
  9265. {
  9266. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9267. u32 flip_mask;
  9268. int ret;
  9269. ret = intel_ring_begin(ring, 6);
  9270. if (ret)
  9271. return ret;
  9272. if (intel_crtc->plane)
  9273. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9274. else
  9275. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9276. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9277. intel_ring_emit(ring, MI_NOOP);
  9278. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9279. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9280. intel_ring_emit(ring, fb->pitches[0]);
  9281. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9282. intel_ring_emit(ring, MI_NOOP);
  9283. intel_mark_page_flip_active(intel_crtc);
  9284. __intel_ring_advance(ring);
  9285. return 0;
  9286. }
  9287. static int intel_gen4_queue_flip(struct drm_device *dev,
  9288. struct drm_crtc *crtc,
  9289. struct drm_framebuffer *fb,
  9290. struct drm_i915_gem_object *obj,
  9291. struct intel_engine_cs *ring,
  9292. uint32_t flags)
  9293. {
  9294. struct drm_i915_private *dev_priv = dev->dev_private;
  9295. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9296. uint32_t pf, pipesrc;
  9297. int ret;
  9298. ret = intel_ring_begin(ring, 4);
  9299. if (ret)
  9300. return ret;
  9301. /* i965+ uses the linear or tiled offsets from the
  9302. * Display Registers (which do not change across a page-flip)
  9303. * so we need only reprogram the base address.
  9304. */
  9305. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9306. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9307. intel_ring_emit(ring, fb->pitches[0]);
  9308. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9309. obj->tiling_mode);
  9310. /* XXX Enabling the panel-fitter across page-flip is so far
  9311. * untested on non-native modes, so ignore it for now.
  9312. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9313. */
  9314. pf = 0;
  9315. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9316. intel_ring_emit(ring, pf | pipesrc);
  9317. intel_mark_page_flip_active(intel_crtc);
  9318. __intel_ring_advance(ring);
  9319. return 0;
  9320. }
  9321. static int intel_gen6_queue_flip(struct drm_device *dev,
  9322. struct drm_crtc *crtc,
  9323. struct drm_framebuffer *fb,
  9324. struct drm_i915_gem_object *obj,
  9325. struct intel_engine_cs *ring,
  9326. uint32_t flags)
  9327. {
  9328. struct drm_i915_private *dev_priv = dev->dev_private;
  9329. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9330. uint32_t pf, pipesrc;
  9331. int ret;
  9332. ret = intel_ring_begin(ring, 4);
  9333. if (ret)
  9334. return ret;
  9335. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9336. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9337. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9338. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9339. /* Contrary to the suggestions in the documentation,
  9340. * "Enable Panel Fitter" does not seem to be required when page
  9341. * flipping with a non-native mode, and worse causes a normal
  9342. * modeset to fail.
  9343. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9344. */
  9345. pf = 0;
  9346. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9347. intel_ring_emit(ring, pf | pipesrc);
  9348. intel_mark_page_flip_active(intel_crtc);
  9349. __intel_ring_advance(ring);
  9350. return 0;
  9351. }
  9352. static int intel_gen7_queue_flip(struct drm_device *dev,
  9353. struct drm_crtc *crtc,
  9354. struct drm_framebuffer *fb,
  9355. struct drm_i915_gem_object *obj,
  9356. struct intel_engine_cs *ring,
  9357. uint32_t flags)
  9358. {
  9359. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9360. uint32_t plane_bit = 0;
  9361. int len, ret;
  9362. switch (intel_crtc->plane) {
  9363. case PLANE_A:
  9364. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9365. break;
  9366. case PLANE_B:
  9367. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9368. break;
  9369. case PLANE_C:
  9370. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9371. break;
  9372. default:
  9373. WARN_ONCE(1, "unknown plane in flip command\n");
  9374. return -ENODEV;
  9375. }
  9376. len = 4;
  9377. if (ring->id == RCS) {
  9378. len += 6;
  9379. /*
  9380. * On Gen 8, SRM is now taking an extra dword to accommodate
  9381. * 48bits addresses, and we need a NOOP for the batch size to
  9382. * stay even.
  9383. */
  9384. if (IS_GEN8(dev))
  9385. len += 2;
  9386. }
  9387. /*
  9388. * BSpec MI_DISPLAY_FLIP for IVB:
  9389. * "The full packet must be contained within the same cache line."
  9390. *
  9391. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9392. * cacheline, if we ever start emitting more commands before
  9393. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9394. * then do the cacheline alignment, and finally emit the
  9395. * MI_DISPLAY_FLIP.
  9396. */
  9397. ret = intel_ring_cacheline_align(ring);
  9398. if (ret)
  9399. return ret;
  9400. ret = intel_ring_begin(ring, len);
  9401. if (ret)
  9402. return ret;
  9403. /* Unmask the flip-done completion message. Note that the bspec says that
  9404. * we should do this for both the BCS and RCS, and that we must not unmask
  9405. * more than one flip event at any time (or ensure that one flip message
  9406. * can be sent by waiting for flip-done prior to queueing new flips).
  9407. * Experimentation says that BCS works despite DERRMR masking all
  9408. * flip-done completion events and that unmasking all planes at once
  9409. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9410. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9411. */
  9412. if (ring->id == RCS) {
  9413. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9414. intel_ring_emit(ring, DERRMR);
  9415. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9416. DERRMR_PIPEB_PRI_FLIP_DONE |
  9417. DERRMR_PIPEC_PRI_FLIP_DONE));
  9418. if (IS_GEN8(dev))
  9419. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  9420. MI_SRM_LRM_GLOBAL_GTT);
  9421. else
  9422. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  9423. MI_SRM_LRM_GLOBAL_GTT);
  9424. intel_ring_emit(ring, DERRMR);
  9425. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9426. if (IS_GEN8(dev)) {
  9427. intel_ring_emit(ring, 0);
  9428. intel_ring_emit(ring, MI_NOOP);
  9429. }
  9430. }
  9431. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9432. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9433. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9434. intel_ring_emit(ring, (MI_NOOP));
  9435. intel_mark_page_flip_active(intel_crtc);
  9436. __intel_ring_advance(ring);
  9437. return 0;
  9438. }
  9439. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9440. struct drm_i915_gem_object *obj)
  9441. {
  9442. /*
  9443. * This is not being used for older platforms, because
  9444. * non-availability of flip done interrupt forces us to use
  9445. * CS flips. Older platforms derive flip done using some clever
  9446. * tricks involving the flip_pending status bits and vblank irqs.
  9447. * So using MMIO flips there would disrupt this mechanism.
  9448. */
  9449. if (ring == NULL)
  9450. return true;
  9451. if (INTEL_INFO(ring->dev)->gen < 5)
  9452. return false;
  9453. if (i915.use_mmio_flip < 0)
  9454. return false;
  9455. else if (i915.use_mmio_flip > 0)
  9456. return true;
  9457. else if (i915.enable_execlists)
  9458. return true;
  9459. else
  9460. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9461. }
  9462. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  9463. {
  9464. struct drm_device *dev = intel_crtc->base.dev;
  9465. struct drm_i915_private *dev_priv = dev->dev_private;
  9466. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9467. const enum pipe pipe = intel_crtc->pipe;
  9468. u32 ctl, stride;
  9469. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9470. ctl &= ~PLANE_CTL_TILED_MASK;
  9471. switch (fb->modifier[0]) {
  9472. case DRM_FORMAT_MOD_NONE:
  9473. break;
  9474. case I915_FORMAT_MOD_X_TILED:
  9475. ctl |= PLANE_CTL_TILED_X;
  9476. break;
  9477. case I915_FORMAT_MOD_Y_TILED:
  9478. ctl |= PLANE_CTL_TILED_Y;
  9479. break;
  9480. case I915_FORMAT_MOD_Yf_TILED:
  9481. ctl |= PLANE_CTL_TILED_YF;
  9482. break;
  9483. default:
  9484. MISSING_CASE(fb->modifier[0]);
  9485. }
  9486. /*
  9487. * The stride is either expressed as a multiple of 64 bytes chunks for
  9488. * linear buffers or in number of tiles for tiled buffers.
  9489. */
  9490. stride = fb->pitches[0] /
  9491. intel_fb_stride_alignment(dev, fb->modifier[0],
  9492. fb->pixel_format);
  9493. /*
  9494. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9495. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9496. */
  9497. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9498. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9499. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  9500. POSTING_READ(PLANE_SURF(pipe, 0));
  9501. }
  9502. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  9503. {
  9504. struct drm_device *dev = intel_crtc->base.dev;
  9505. struct drm_i915_private *dev_priv = dev->dev_private;
  9506. struct intel_framebuffer *intel_fb =
  9507. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9508. struct drm_i915_gem_object *obj = intel_fb->obj;
  9509. u32 dspcntr;
  9510. u32 reg;
  9511. reg = DSPCNTR(intel_crtc->plane);
  9512. dspcntr = I915_READ(reg);
  9513. if (obj->tiling_mode != I915_TILING_NONE)
  9514. dspcntr |= DISPPLANE_TILED;
  9515. else
  9516. dspcntr &= ~DISPPLANE_TILED;
  9517. I915_WRITE(reg, dspcntr);
  9518. I915_WRITE(DSPSURF(intel_crtc->plane),
  9519. intel_crtc->unpin_work->gtt_offset);
  9520. POSTING_READ(DSPSURF(intel_crtc->plane));
  9521. }
  9522. /*
  9523. * XXX: This is the temporary way to update the plane registers until we get
  9524. * around to using the usual plane update functions for MMIO flips
  9525. */
  9526. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9527. {
  9528. struct drm_device *dev = intel_crtc->base.dev;
  9529. bool atomic_update;
  9530. u32 start_vbl_count;
  9531. intel_mark_page_flip_active(intel_crtc);
  9532. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9533. if (INTEL_INFO(dev)->gen >= 9)
  9534. skl_do_mmio_flip(intel_crtc);
  9535. else
  9536. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9537. ilk_do_mmio_flip(intel_crtc);
  9538. if (atomic_update)
  9539. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9540. }
  9541. static void intel_mmio_flip_work_func(struct work_struct *work)
  9542. {
  9543. struct intel_mmio_flip *mmio_flip =
  9544. container_of(work, struct intel_mmio_flip, work);
  9545. if (mmio_flip->req)
  9546. WARN_ON(__i915_wait_request(mmio_flip->req,
  9547. mmio_flip->crtc->reset_counter,
  9548. false, NULL,
  9549. &mmio_flip->i915->rps.mmioflips));
  9550. intel_do_mmio_flip(mmio_flip->crtc);
  9551. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9552. kfree(mmio_flip);
  9553. }
  9554. static int intel_queue_mmio_flip(struct drm_device *dev,
  9555. struct drm_crtc *crtc,
  9556. struct drm_framebuffer *fb,
  9557. struct drm_i915_gem_object *obj,
  9558. struct intel_engine_cs *ring,
  9559. uint32_t flags)
  9560. {
  9561. struct intel_mmio_flip *mmio_flip;
  9562. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9563. if (mmio_flip == NULL)
  9564. return -ENOMEM;
  9565. mmio_flip->i915 = to_i915(dev);
  9566. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9567. mmio_flip->crtc = to_intel_crtc(crtc);
  9568. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9569. schedule_work(&mmio_flip->work);
  9570. return 0;
  9571. }
  9572. static int intel_default_queue_flip(struct drm_device *dev,
  9573. struct drm_crtc *crtc,
  9574. struct drm_framebuffer *fb,
  9575. struct drm_i915_gem_object *obj,
  9576. struct intel_engine_cs *ring,
  9577. uint32_t flags)
  9578. {
  9579. return -ENODEV;
  9580. }
  9581. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9582. struct drm_crtc *crtc)
  9583. {
  9584. struct drm_i915_private *dev_priv = dev->dev_private;
  9585. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9586. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9587. u32 addr;
  9588. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9589. return true;
  9590. if (!work->enable_stall_check)
  9591. return false;
  9592. if (work->flip_ready_vblank == 0) {
  9593. if (work->flip_queued_req &&
  9594. !i915_gem_request_completed(work->flip_queued_req, true))
  9595. return false;
  9596. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9597. }
  9598. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9599. return false;
  9600. /* Potential stall - if we see that the flip has happened,
  9601. * assume a missed interrupt. */
  9602. if (INTEL_INFO(dev)->gen >= 4)
  9603. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9604. else
  9605. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9606. /* There is a potential issue here with a false positive after a flip
  9607. * to the same address. We could address this by checking for a
  9608. * non-incrementing frame counter.
  9609. */
  9610. return addr == work->gtt_offset;
  9611. }
  9612. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9613. {
  9614. struct drm_i915_private *dev_priv = dev->dev_private;
  9615. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9616. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9617. struct intel_unpin_work *work;
  9618. WARN_ON(!in_interrupt());
  9619. if (crtc == NULL)
  9620. return;
  9621. spin_lock(&dev->event_lock);
  9622. work = intel_crtc->unpin_work;
  9623. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9624. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9625. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9626. page_flip_completed(intel_crtc);
  9627. work = NULL;
  9628. }
  9629. if (work != NULL &&
  9630. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9631. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9632. spin_unlock(&dev->event_lock);
  9633. }
  9634. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9635. struct drm_framebuffer *fb,
  9636. struct drm_pending_vblank_event *event,
  9637. uint32_t page_flip_flags)
  9638. {
  9639. struct drm_device *dev = crtc->dev;
  9640. struct drm_i915_private *dev_priv = dev->dev_private;
  9641. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9642. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9643. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9644. struct drm_plane *primary = crtc->primary;
  9645. enum pipe pipe = intel_crtc->pipe;
  9646. struct intel_unpin_work *work;
  9647. struct intel_engine_cs *ring;
  9648. bool mmio_flip;
  9649. int ret;
  9650. /*
  9651. * drm_mode_page_flip_ioctl() should already catch this, but double
  9652. * check to be safe. In the future we may enable pageflipping from
  9653. * a disabled primary plane.
  9654. */
  9655. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9656. return -EBUSY;
  9657. /* Can't change pixel format via MI display flips. */
  9658. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9659. return -EINVAL;
  9660. /*
  9661. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9662. * Note that pitch changes could also affect these register.
  9663. */
  9664. if (INTEL_INFO(dev)->gen > 3 &&
  9665. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9666. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9667. return -EINVAL;
  9668. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9669. goto out_hang;
  9670. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9671. if (work == NULL)
  9672. return -ENOMEM;
  9673. work->event = event;
  9674. work->crtc = crtc;
  9675. work->old_fb = old_fb;
  9676. INIT_WORK(&work->work, intel_unpin_work_fn);
  9677. ret = drm_crtc_vblank_get(crtc);
  9678. if (ret)
  9679. goto free_work;
  9680. /* We borrow the event spin lock for protecting unpin_work */
  9681. spin_lock_irq(&dev->event_lock);
  9682. if (intel_crtc->unpin_work) {
  9683. /* Before declaring the flip queue wedged, check if
  9684. * the hardware completed the operation behind our backs.
  9685. */
  9686. if (__intel_pageflip_stall_check(dev, crtc)) {
  9687. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9688. page_flip_completed(intel_crtc);
  9689. } else {
  9690. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9691. spin_unlock_irq(&dev->event_lock);
  9692. drm_crtc_vblank_put(crtc);
  9693. kfree(work);
  9694. return -EBUSY;
  9695. }
  9696. }
  9697. intel_crtc->unpin_work = work;
  9698. spin_unlock_irq(&dev->event_lock);
  9699. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9700. flush_workqueue(dev_priv->wq);
  9701. /* Reference the objects for the scheduled work. */
  9702. drm_framebuffer_reference(work->old_fb);
  9703. drm_gem_object_reference(&obj->base);
  9704. crtc->primary->fb = fb;
  9705. update_state_fb(crtc->primary);
  9706. work->pending_flip_obj = obj;
  9707. ret = i915_mutex_lock_interruptible(dev);
  9708. if (ret)
  9709. goto cleanup;
  9710. atomic_inc(&intel_crtc->unpin_work_count);
  9711. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9712. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9713. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9714. if (IS_VALLEYVIEW(dev)) {
  9715. ring = &dev_priv->ring[BCS];
  9716. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9717. /* vlv: DISPLAY_FLIP fails to change tiling */
  9718. ring = NULL;
  9719. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9720. ring = &dev_priv->ring[BCS];
  9721. } else if (INTEL_INFO(dev)->gen >= 7) {
  9722. ring = i915_gem_request_get_ring(obj->last_write_req);
  9723. if (ring == NULL || ring->id != RCS)
  9724. ring = &dev_priv->ring[BCS];
  9725. } else {
  9726. ring = &dev_priv->ring[RCS];
  9727. }
  9728. mmio_flip = use_mmio_flip(ring, obj);
  9729. /* When using CS flips, we want to emit semaphores between rings.
  9730. * However, when using mmio flips we will create a task to do the
  9731. * synchronisation, so all we want here is to pin the framebuffer
  9732. * into the display plane and skip any waits.
  9733. */
  9734. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9735. crtc->primary->state,
  9736. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
  9737. if (ret)
  9738. goto cleanup_pending;
  9739. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9740. + intel_crtc->dspaddr_offset;
  9741. if (mmio_flip) {
  9742. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9743. page_flip_flags);
  9744. if (ret)
  9745. goto cleanup_unpin;
  9746. i915_gem_request_assign(&work->flip_queued_req,
  9747. obj->last_write_req);
  9748. } else {
  9749. if (obj->last_write_req) {
  9750. ret = i915_gem_check_olr(obj->last_write_req);
  9751. if (ret)
  9752. goto cleanup_unpin;
  9753. }
  9754. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  9755. page_flip_flags);
  9756. if (ret)
  9757. goto cleanup_unpin;
  9758. i915_gem_request_assign(&work->flip_queued_req,
  9759. intel_ring_get_request(ring));
  9760. }
  9761. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9762. work->enable_stall_check = true;
  9763. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9764. INTEL_FRONTBUFFER_PRIMARY(pipe));
  9765. intel_fbc_disable(dev);
  9766. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9767. mutex_unlock(&dev->struct_mutex);
  9768. trace_i915_flip_request(intel_crtc->plane, obj);
  9769. return 0;
  9770. cleanup_unpin:
  9771. intel_unpin_fb_obj(fb, crtc->primary->state);
  9772. cleanup_pending:
  9773. atomic_dec(&intel_crtc->unpin_work_count);
  9774. mutex_unlock(&dev->struct_mutex);
  9775. cleanup:
  9776. crtc->primary->fb = old_fb;
  9777. update_state_fb(crtc->primary);
  9778. drm_gem_object_unreference_unlocked(&obj->base);
  9779. drm_framebuffer_unreference(work->old_fb);
  9780. spin_lock_irq(&dev->event_lock);
  9781. intel_crtc->unpin_work = NULL;
  9782. spin_unlock_irq(&dev->event_lock);
  9783. drm_crtc_vblank_put(crtc);
  9784. free_work:
  9785. kfree(work);
  9786. if (ret == -EIO) {
  9787. out_hang:
  9788. ret = intel_plane_restore(primary);
  9789. if (ret == 0 && event) {
  9790. spin_lock_irq(&dev->event_lock);
  9791. drm_send_vblank_event(dev, pipe, event);
  9792. spin_unlock_irq(&dev->event_lock);
  9793. }
  9794. }
  9795. return ret;
  9796. }
  9797. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9798. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9799. .load_lut = intel_crtc_load_lut,
  9800. .atomic_begin = intel_begin_crtc_commit,
  9801. .atomic_flush = intel_finish_crtc_commit,
  9802. };
  9803. /* Transitional helper to copy current connector/encoder state to
  9804. * connector->state. This is needed so that code that is partially
  9805. * converted to atomic does the right thing.
  9806. */
  9807. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9808. {
  9809. struct intel_connector *connector;
  9810. for_each_intel_connector(dev, connector) {
  9811. if (connector->base.encoder) {
  9812. connector->base.state->best_encoder =
  9813. connector->base.encoder;
  9814. connector->base.state->crtc =
  9815. connector->base.encoder->crtc;
  9816. } else {
  9817. connector->base.state->best_encoder = NULL;
  9818. connector->base.state->crtc = NULL;
  9819. }
  9820. }
  9821. }
  9822. static void
  9823. connected_sink_compute_bpp(struct intel_connector *connector,
  9824. struct intel_crtc_state *pipe_config)
  9825. {
  9826. int bpp = pipe_config->pipe_bpp;
  9827. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9828. connector->base.base.id,
  9829. connector->base.name);
  9830. /* Don't use an invalid EDID bpc value */
  9831. if (connector->base.display_info.bpc &&
  9832. connector->base.display_info.bpc * 3 < bpp) {
  9833. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9834. bpp, connector->base.display_info.bpc*3);
  9835. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9836. }
  9837. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9838. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9839. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9840. bpp);
  9841. pipe_config->pipe_bpp = 24;
  9842. }
  9843. }
  9844. static int
  9845. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9846. struct intel_crtc_state *pipe_config)
  9847. {
  9848. struct drm_device *dev = crtc->base.dev;
  9849. struct drm_atomic_state *state;
  9850. struct drm_connector *connector;
  9851. struct drm_connector_state *connector_state;
  9852. int bpp, i;
  9853. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9854. bpp = 10*3;
  9855. else if (INTEL_INFO(dev)->gen >= 5)
  9856. bpp = 12*3;
  9857. else
  9858. bpp = 8*3;
  9859. pipe_config->pipe_bpp = bpp;
  9860. state = pipe_config->base.state;
  9861. /* Clamp display bpp to EDID value */
  9862. for_each_connector_in_state(state, connector, connector_state, i) {
  9863. if (connector_state->crtc != &crtc->base)
  9864. continue;
  9865. connected_sink_compute_bpp(to_intel_connector(connector),
  9866. pipe_config);
  9867. }
  9868. return bpp;
  9869. }
  9870. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9871. {
  9872. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9873. "type: 0x%x flags: 0x%x\n",
  9874. mode->crtc_clock,
  9875. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9876. mode->crtc_hsync_end, mode->crtc_htotal,
  9877. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9878. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9879. }
  9880. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9881. struct intel_crtc_state *pipe_config,
  9882. const char *context)
  9883. {
  9884. struct drm_device *dev = crtc->base.dev;
  9885. struct drm_plane *plane;
  9886. struct intel_plane *intel_plane;
  9887. struct intel_plane_state *state;
  9888. struct drm_framebuffer *fb;
  9889. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9890. context, pipe_config, pipe_name(crtc->pipe));
  9891. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  9892. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9893. pipe_config->pipe_bpp, pipe_config->dither);
  9894. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9895. pipe_config->has_pch_encoder,
  9896. pipe_config->fdi_lanes,
  9897. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9898. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9899. pipe_config->fdi_m_n.tu);
  9900. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9901. pipe_config->has_dp_encoder,
  9902. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  9903. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  9904. pipe_config->dp_m_n.tu);
  9905. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  9906. pipe_config->has_dp_encoder,
  9907. pipe_config->dp_m2_n2.gmch_m,
  9908. pipe_config->dp_m2_n2.gmch_n,
  9909. pipe_config->dp_m2_n2.link_m,
  9910. pipe_config->dp_m2_n2.link_n,
  9911. pipe_config->dp_m2_n2.tu);
  9912. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9913. pipe_config->has_audio,
  9914. pipe_config->has_infoframe);
  9915. DRM_DEBUG_KMS("requested mode:\n");
  9916. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9917. DRM_DEBUG_KMS("adjusted mode:\n");
  9918. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9919. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9920. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  9921. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  9922. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  9923. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9924. crtc->num_scalers,
  9925. pipe_config->scaler_state.scaler_users,
  9926. pipe_config->scaler_state.scaler_id);
  9927. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9928. pipe_config->gmch_pfit.control,
  9929. pipe_config->gmch_pfit.pgm_ratios,
  9930. pipe_config->gmch_pfit.lvds_border_bits);
  9931. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9932. pipe_config->pch_pfit.pos,
  9933. pipe_config->pch_pfit.size,
  9934. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  9935. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  9936. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  9937. if (IS_BROXTON(dev)) {
  9938. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
  9939. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  9940. "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
  9941. pipe_config->ddi_pll_sel,
  9942. pipe_config->dpll_hw_state.ebb0,
  9943. pipe_config->dpll_hw_state.pll0,
  9944. pipe_config->dpll_hw_state.pll1,
  9945. pipe_config->dpll_hw_state.pll2,
  9946. pipe_config->dpll_hw_state.pll3,
  9947. pipe_config->dpll_hw_state.pll6,
  9948. pipe_config->dpll_hw_state.pll8,
  9949. pipe_config->dpll_hw_state.pcsdw12);
  9950. } else if (IS_SKYLAKE(dev)) {
  9951. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  9952. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  9953. pipe_config->ddi_pll_sel,
  9954. pipe_config->dpll_hw_state.ctrl1,
  9955. pipe_config->dpll_hw_state.cfgcr1,
  9956. pipe_config->dpll_hw_state.cfgcr2);
  9957. } else if (HAS_DDI(dev)) {
  9958. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  9959. pipe_config->ddi_pll_sel,
  9960. pipe_config->dpll_hw_state.wrpll);
  9961. } else {
  9962. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  9963. "fp0: 0x%x, fp1: 0x%x\n",
  9964. pipe_config->dpll_hw_state.dpll,
  9965. pipe_config->dpll_hw_state.dpll_md,
  9966. pipe_config->dpll_hw_state.fp0,
  9967. pipe_config->dpll_hw_state.fp1);
  9968. }
  9969. DRM_DEBUG_KMS("planes on this crtc\n");
  9970. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9971. intel_plane = to_intel_plane(plane);
  9972. if (intel_plane->pipe != crtc->pipe)
  9973. continue;
  9974. state = to_intel_plane_state(plane->state);
  9975. fb = state->base.fb;
  9976. if (!fb) {
  9977. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  9978. "disabled, scaler_id = %d\n",
  9979. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  9980. plane->base.id, intel_plane->pipe,
  9981. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  9982. drm_plane_index(plane), state->scaler_id);
  9983. continue;
  9984. }
  9985. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  9986. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  9987. plane->base.id, intel_plane->pipe,
  9988. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  9989. drm_plane_index(plane));
  9990. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  9991. fb->base.id, fb->width, fb->height, fb->pixel_format);
  9992. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  9993. state->scaler_id,
  9994. state->src.x1 >> 16, state->src.y1 >> 16,
  9995. drm_rect_width(&state->src) >> 16,
  9996. drm_rect_height(&state->src) >> 16,
  9997. state->dst.x1, state->dst.y1,
  9998. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  9999. }
  10000. }
  10001. static bool encoders_cloneable(const struct intel_encoder *a,
  10002. const struct intel_encoder *b)
  10003. {
  10004. /* masks could be asymmetric, so check both ways */
  10005. return a == b || (a->cloneable & (1 << b->type) &&
  10006. b->cloneable & (1 << a->type));
  10007. }
  10008. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10009. struct intel_crtc *crtc,
  10010. struct intel_encoder *encoder)
  10011. {
  10012. struct intel_encoder *source_encoder;
  10013. struct drm_connector *connector;
  10014. struct drm_connector_state *connector_state;
  10015. int i;
  10016. for_each_connector_in_state(state, connector, connector_state, i) {
  10017. if (connector_state->crtc != &crtc->base)
  10018. continue;
  10019. source_encoder =
  10020. to_intel_encoder(connector_state->best_encoder);
  10021. if (!encoders_cloneable(encoder, source_encoder))
  10022. return false;
  10023. }
  10024. return true;
  10025. }
  10026. static bool check_encoder_cloning(struct drm_atomic_state *state,
  10027. struct intel_crtc *crtc)
  10028. {
  10029. struct intel_encoder *encoder;
  10030. struct drm_connector *connector;
  10031. struct drm_connector_state *connector_state;
  10032. int i;
  10033. for_each_connector_in_state(state, connector, connector_state, i) {
  10034. if (connector_state->crtc != &crtc->base)
  10035. continue;
  10036. encoder = to_intel_encoder(connector_state->best_encoder);
  10037. if (!check_single_encoder_cloning(state, crtc, encoder))
  10038. return false;
  10039. }
  10040. return true;
  10041. }
  10042. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10043. {
  10044. struct drm_device *dev = state->dev;
  10045. struct intel_encoder *encoder;
  10046. struct drm_connector *connector;
  10047. struct drm_connector_state *connector_state;
  10048. unsigned int used_ports = 0;
  10049. int i;
  10050. /*
  10051. * Walk the connector list instead of the encoder
  10052. * list to detect the problem on ddi platforms
  10053. * where there's just one encoder per digital port.
  10054. */
  10055. for_each_connector_in_state(state, connector, connector_state, i) {
  10056. if (!connector_state->best_encoder)
  10057. continue;
  10058. encoder = to_intel_encoder(connector_state->best_encoder);
  10059. WARN_ON(!connector_state->crtc);
  10060. switch (encoder->type) {
  10061. unsigned int port_mask;
  10062. case INTEL_OUTPUT_UNKNOWN:
  10063. if (WARN_ON(!HAS_DDI(dev)))
  10064. break;
  10065. case INTEL_OUTPUT_DISPLAYPORT:
  10066. case INTEL_OUTPUT_HDMI:
  10067. case INTEL_OUTPUT_EDP:
  10068. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10069. /* the same port mustn't appear more than once */
  10070. if (used_ports & port_mask)
  10071. return false;
  10072. used_ports |= port_mask;
  10073. default:
  10074. break;
  10075. }
  10076. }
  10077. return true;
  10078. }
  10079. static void
  10080. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10081. {
  10082. struct drm_crtc_state tmp_state;
  10083. struct intel_crtc_scaler_state scaler_state;
  10084. struct intel_dpll_hw_state dpll_hw_state;
  10085. enum intel_dpll_id shared_dpll;
  10086. uint32_t ddi_pll_sel;
  10087. /* FIXME: before the switch to atomic started, a new pipe_config was
  10088. * kzalloc'd. Code that depends on any field being zero should be
  10089. * fixed, so that the crtc_state can be safely duplicated. For now,
  10090. * only fields that are know to not cause problems are preserved. */
  10091. tmp_state = crtc_state->base;
  10092. scaler_state = crtc_state->scaler_state;
  10093. shared_dpll = crtc_state->shared_dpll;
  10094. dpll_hw_state = crtc_state->dpll_hw_state;
  10095. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10096. memset(crtc_state, 0, sizeof *crtc_state);
  10097. crtc_state->base = tmp_state;
  10098. crtc_state->scaler_state = scaler_state;
  10099. crtc_state->shared_dpll = shared_dpll;
  10100. crtc_state->dpll_hw_state = dpll_hw_state;
  10101. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10102. }
  10103. static int
  10104. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10105. struct drm_atomic_state *state)
  10106. {
  10107. struct drm_crtc_state *crtc_state;
  10108. struct intel_crtc_state *pipe_config;
  10109. struct intel_encoder *encoder;
  10110. struct drm_connector *connector;
  10111. struct drm_connector_state *connector_state;
  10112. int base_bpp, ret = -EINVAL;
  10113. int i;
  10114. bool retry = true;
  10115. if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
  10116. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10117. return -EINVAL;
  10118. }
  10119. if (!check_digital_port_conflicts(state)) {
  10120. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10121. return -EINVAL;
  10122. }
  10123. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10124. if (WARN_ON(!crtc_state))
  10125. return -EINVAL;
  10126. pipe_config = to_intel_crtc_state(crtc_state);
  10127. /*
  10128. * XXX: Add all connectors to make the crtc state match the encoders.
  10129. */
  10130. if (!needs_modeset(&pipe_config->base)) {
  10131. ret = drm_atomic_add_affected_connectors(state, crtc);
  10132. if (ret)
  10133. return ret;
  10134. }
  10135. clear_intel_crtc_state(pipe_config);
  10136. pipe_config->cpu_transcoder =
  10137. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10138. /*
  10139. * Sanitize sync polarity flags based on requested ones. If neither
  10140. * positive or negative polarity is requested, treat this as meaning
  10141. * negative polarity.
  10142. */
  10143. if (!(pipe_config->base.adjusted_mode.flags &
  10144. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10145. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10146. if (!(pipe_config->base.adjusted_mode.flags &
  10147. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10148. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10149. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  10150. * plane pixel format and any sink constraints into account. Returns the
  10151. * source plane bpp so that dithering can be selected on mismatches
  10152. * after encoders and crtc also have had their say. */
  10153. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10154. pipe_config);
  10155. if (base_bpp < 0)
  10156. goto fail;
  10157. /*
  10158. * Determine the real pipe dimensions. Note that stereo modes can
  10159. * increase the actual pipe size due to the frame doubling and
  10160. * insertion of additional space for blanks between the frame. This
  10161. * is stored in the crtc timings. We use the requested mode to do this
  10162. * computation to clearly distinguish it from the adjusted mode, which
  10163. * can be changed by the connectors in the below retry loop.
  10164. */
  10165. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10166. &pipe_config->pipe_src_w,
  10167. &pipe_config->pipe_src_h);
  10168. encoder_retry:
  10169. /* Ensure the port clock defaults are reset when retrying. */
  10170. pipe_config->port_clock = 0;
  10171. pipe_config->pixel_multiplier = 1;
  10172. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10173. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10174. CRTC_STEREO_DOUBLE);
  10175. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10176. * adjust it according to limitations or connector properties, and also
  10177. * a chance to reject the mode entirely.
  10178. */
  10179. for_each_connector_in_state(state, connector, connector_state, i) {
  10180. if (connector_state->crtc != crtc)
  10181. continue;
  10182. encoder = to_intel_encoder(connector_state->best_encoder);
  10183. if (!(encoder->compute_config(encoder, pipe_config))) {
  10184. DRM_DEBUG_KMS("Encoder config failure\n");
  10185. goto fail;
  10186. }
  10187. }
  10188. /* Set default port clock if not overwritten by the encoder. Needs to be
  10189. * done afterwards in case the encoder adjusts the mode. */
  10190. if (!pipe_config->port_clock)
  10191. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10192. * pipe_config->pixel_multiplier;
  10193. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10194. if (ret < 0) {
  10195. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10196. goto fail;
  10197. }
  10198. if (ret == RETRY) {
  10199. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10200. ret = -EINVAL;
  10201. goto fail;
  10202. }
  10203. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10204. retry = false;
  10205. goto encoder_retry;
  10206. }
  10207. pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
  10208. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  10209. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10210. /* Check if we need to force a modeset */
  10211. if (pipe_config->has_audio !=
  10212. to_intel_crtc_state(crtc->state)->has_audio) {
  10213. pipe_config->base.mode_changed = true;
  10214. ret = drm_atomic_add_affected_planes(state, crtc);
  10215. }
  10216. /*
  10217. * Note we have an issue here with infoframes: current code
  10218. * only updates them on the full mode set path per hw
  10219. * requirements. So here we should be checking for any
  10220. * required changes and forcing a mode set.
  10221. */
  10222. fail:
  10223. return ret;
  10224. }
  10225. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  10226. {
  10227. struct drm_encoder *encoder;
  10228. struct drm_device *dev = crtc->dev;
  10229. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  10230. if (encoder->crtc == crtc)
  10231. return true;
  10232. return false;
  10233. }
  10234. static void
  10235. intel_modeset_update_state(struct drm_atomic_state *state)
  10236. {
  10237. struct drm_device *dev = state->dev;
  10238. struct intel_encoder *intel_encoder;
  10239. struct drm_crtc *crtc;
  10240. struct drm_crtc_state *crtc_state;
  10241. struct drm_connector *connector;
  10242. intel_shared_dpll_commit(state);
  10243. drm_atomic_helper_swap_state(state->dev, state);
  10244. for_each_intel_encoder(dev, intel_encoder) {
  10245. if (!intel_encoder->base.crtc)
  10246. continue;
  10247. crtc = intel_encoder->base.crtc;
  10248. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10249. if (!crtc_state || !needs_modeset(crtc->state))
  10250. continue;
  10251. intel_encoder->connectors_active = false;
  10252. }
  10253. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10254. /* Double check state. */
  10255. for_each_crtc(dev, crtc) {
  10256. WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
  10257. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10258. }
  10259. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10260. if (!connector->encoder || !connector->encoder->crtc)
  10261. continue;
  10262. crtc = connector->encoder->crtc;
  10263. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10264. if (!crtc_state || !needs_modeset(crtc->state))
  10265. continue;
  10266. if (crtc->state->active) {
  10267. struct drm_property *dpms_property =
  10268. dev->mode_config.dpms_property;
  10269. connector->dpms = DRM_MODE_DPMS_ON;
  10270. drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
  10271. intel_encoder = to_intel_encoder(connector->encoder);
  10272. intel_encoder->connectors_active = true;
  10273. } else
  10274. connector->dpms = DRM_MODE_DPMS_OFF;
  10275. }
  10276. }
  10277. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10278. {
  10279. int diff;
  10280. if (clock1 == clock2)
  10281. return true;
  10282. if (!clock1 || !clock2)
  10283. return false;
  10284. diff = abs(clock1 - clock2);
  10285. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10286. return true;
  10287. return false;
  10288. }
  10289. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10290. list_for_each_entry((intel_crtc), \
  10291. &(dev)->mode_config.crtc_list, \
  10292. base.head) \
  10293. if (mask & (1 <<(intel_crtc)->pipe))
  10294. static bool
  10295. intel_pipe_config_compare(struct drm_device *dev,
  10296. struct intel_crtc_state *current_config,
  10297. struct intel_crtc_state *pipe_config)
  10298. {
  10299. #define PIPE_CONF_CHECK_X(name) \
  10300. if (current_config->name != pipe_config->name) { \
  10301. DRM_ERROR("mismatch in " #name " " \
  10302. "(expected 0x%08x, found 0x%08x)\n", \
  10303. current_config->name, \
  10304. pipe_config->name); \
  10305. return false; \
  10306. }
  10307. #define PIPE_CONF_CHECK_I(name) \
  10308. if (current_config->name != pipe_config->name) { \
  10309. DRM_ERROR("mismatch in " #name " " \
  10310. "(expected %i, found %i)\n", \
  10311. current_config->name, \
  10312. pipe_config->name); \
  10313. return false; \
  10314. }
  10315. /* This is required for BDW+ where there is only one set of registers for
  10316. * switching between high and low RR.
  10317. * This macro can be used whenever a comparison has to be made between one
  10318. * hw state and multiple sw state variables.
  10319. */
  10320. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10321. if ((current_config->name != pipe_config->name) && \
  10322. (current_config->alt_name != pipe_config->name)) { \
  10323. DRM_ERROR("mismatch in " #name " " \
  10324. "(expected %i or %i, found %i)\n", \
  10325. current_config->name, \
  10326. current_config->alt_name, \
  10327. pipe_config->name); \
  10328. return false; \
  10329. }
  10330. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10331. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10332. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  10333. "(expected %i, found %i)\n", \
  10334. current_config->name & (mask), \
  10335. pipe_config->name & (mask)); \
  10336. return false; \
  10337. }
  10338. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10339. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10340. DRM_ERROR("mismatch in " #name " " \
  10341. "(expected %i, found %i)\n", \
  10342. current_config->name, \
  10343. pipe_config->name); \
  10344. return false; \
  10345. }
  10346. #define PIPE_CONF_QUIRK(quirk) \
  10347. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10348. PIPE_CONF_CHECK_I(cpu_transcoder);
  10349. PIPE_CONF_CHECK_I(has_pch_encoder);
  10350. PIPE_CONF_CHECK_I(fdi_lanes);
  10351. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  10352. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  10353. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  10354. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  10355. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  10356. PIPE_CONF_CHECK_I(has_dp_encoder);
  10357. if (INTEL_INFO(dev)->gen < 8) {
  10358. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  10359. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  10360. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  10361. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  10362. PIPE_CONF_CHECK_I(dp_m_n.tu);
  10363. if (current_config->has_drrs) {
  10364. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  10365. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  10366. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  10367. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  10368. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  10369. }
  10370. } else {
  10371. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  10372. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  10373. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  10374. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  10375. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  10376. }
  10377. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10378. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10379. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10380. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10381. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10382. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10383. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10384. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10385. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10386. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10387. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10388. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10389. PIPE_CONF_CHECK_I(pixel_multiplier);
  10390. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10391. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10392. IS_VALLEYVIEW(dev))
  10393. PIPE_CONF_CHECK_I(limited_color_range);
  10394. PIPE_CONF_CHECK_I(has_infoframe);
  10395. PIPE_CONF_CHECK_I(has_audio);
  10396. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10397. DRM_MODE_FLAG_INTERLACE);
  10398. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10399. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10400. DRM_MODE_FLAG_PHSYNC);
  10401. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10402. DRM_MODE_FLAG_NHSYNC);
  10403. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10404. DRM_MODE_FLAG_PVSYNC);
  10405. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10406. DRM_MODE_FLAG_NVSYNC);
  10407. }
  10408. PIPE_CONF_CHECK_I(pipe_src_w);
  10409. PIPE_CONF_CHECK_I(pipe_src_h);
  10410. /*
  10411. * FIXME: BIOS likes to set up a cloned config with lvds+external
  10412. * screen. Since we don't yet re-compute the pipe config when moving
  10413. * just the lvds port away to another pipe the sw tracking won't match.
  10414. *
  10415. * Proper atomic modesets with recomputed global state will fix this.
  10416. * Until then just don't check gmch state for inherited modes.
  10417. */
  10418. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  10419. PIPE_CONF_CHECK_I(gmch_pfit.control);
  10420. /* pfit ratios are autocomputed by the hw on gen4+ */
  10421. if (INTEL_INFO(dev)->gen < 4)
  10422. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10423. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  10424. }
  10425. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10426. if (current_config->pch_pfit.enabled) {
  10427. PIPE_CONF_CHECK_I(pch_pfit.pos);
  10428. PIPE_CONF_CHECK_I(pch_pfit.size);
  10429. }
  10430. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10431. /* BDW+ don't expose a synchronous way to read the state */
  10432. if (IS_HASWELL(dev))
  10433. PIPE_CONF_CHECK_I(ips_enabled);
  10434. PIPE_CONF_CHECK_I(double_wide);
  10435. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10436. PIPE_CONF_CHECK_I(shared_dpll);
  10437. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10438. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10439. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10440. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10441. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10442. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10443. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10444. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10445. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10446. PIPE_CONF_CHECK_I(pipe_bpp);
  10447. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10448. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10449. #undef PIPE_CONF_CHECK_X
  10450. #undef PIPE_CONF_CHECK_I
  10451. #undef PIPE_CONF_CHECK_I_ALT
  10452. #undef PIPE_CONF_CHECK_FLAGS
  10453. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10454. #undef PIPE_CONF_QUIRK
  10455. return true;
  10456. }
  10457. static void check_wm_state(struct drm_device *dev)
  10458. {
  10459. struct drm_i915_private *dev_priv = dev->dev_private;
  10460. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10461. struct intel_crtc *intel_crtc;
  10462. int plane;
  10463. if (INTEL_INFO(dev)->gen < 9)
  10464. return;
  10465. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10466. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10467. for_each_intel_crtc(dev, intel_crtc) {
  10468. struct skl_ddb_entry *hw_entry, *sw_entry;
  10469. const enum pipe pipe = intel_crtc->pipe;
  10470. if (!intel_crtc->active)
  10471. continue;
  10472. /* planes */
  10473. for_each_plane(dev_priv, pipe, plane) {
  10474. hw_entry = &hw_ddb.plane[pipe][plane];
  10475. sw_entry = &sw_ddb->plane[pipe][plane];
  10476. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10477. continue;
  10478. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10479. "(expected (%u,%u), found (%u,%u))\n",
  10480. pipe_name(pipe), plane + 1,
  10481. sw_entry->start, sw_entry->end,
  10482. hw_entry->start, hw_entry->end);
  10483. }
  10484. /* cursor */
  10485. hw_entry = &hw_ddb.cursor[pipe];
  10486. sw_entry = &sw_ddb->cursor[pipe];
  10487. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10488. continue;
  10489. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10490. "(expected (%u,%u), found (%u,%u))\n",
  10491. pipe_name(pipe),
  10492. sw_entry->start, sw_entry->end,
  10493. hw_entry->start, hw_entry->end);
  10494. }
  10495. }
  10496. static void
  10497. check_connector_state(struct drm_device *dev)
  10498. {
  10499. struct intel_connector *connector;
  10500. for_each_intel_connector(dev, connector) {
  10501. struct drm_encoder *encoder = connector->base.encoder;
  10502. struct drm_connector_state *state = connector->base.state;
  10503. /* This also checks the encoder/connector hw state with the
  10504. * ->get_hw_state callbacks. */
  10505. intel_connector_check_state(connector);
  10506. I915_STATE_WARN(state->best_encoder != encoder,
  10507. "connector's staged encoder doesn't match current encoder\n");
  10508. }
  10509. }
  10510. static void
  10511. check_encoder_state(struct drm_device *dev)
  10512. {
  10513. struct intel_encoder *encoder;
  10514. struct intel_connector *connector;
  10515. for_each_intel_encoder(dev, encoder) {
  10516. bool enabled = false;
  10517. bool active = false;
  10518. enum pipe pipe, tracked_pipe;
  10519. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10520. encoder->base.base.id,
  10521. encoder->base.name);
  10522. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  10523. "encoder's active_connectors set, but no crtc\n");
  10524. for_each_intel_connector(dev, connector) {
  10525. if (connector->base.encoder != &encoder->base)
  10526. continue;
  10527. enabled = true;
  10528. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  10529. active = true;
  10530. I915_STATE_WARN(connector->base.state->crtc != encoder->base.crtc,
  10531. "encoder's stage crtc doesn't match current crtc\n");
  10532. }
  10533. /*
  10534. * for MST connectors if we unplug the connector is gone
  10535. * away but the encoder is still connected to a crtc
  10536. * until a modeset happens in response to the hotplug.
  10537. */
  10538. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  10539. continue;
  10540. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10541. "encoder's enabled state mismatch "
  10542. "(expected %i, found %i)\n",
  10543. !!encoder->base.crtc, enabled);
  10544. I915_STATE_WARN(active && !encoder->base.crtc,
  10545. "active encoder with no crtc\n");
  10546. I915_STATE_WARN(encoder->connectors_active != active,
  10547. "encoder's computed active state doesn't match tracked active state "
  10548. "(expected %i, found %i)\n", active, encoder->connectors_active);
  10549. active = encoder->get_hw_state(encoder, &pipe);
  10550. I915_STATE_WARN(active != encoder->connectors_active,
  10551. "encoder's hw state doesn't match sw tracking "
  10552. "(expected %i, found %i)\n",
  10553. encoder->connectors_active, active);
  10554. if (!encoder->base.crtc)
  10555. continue;
  10556. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  10557. I915_STATE_WARN(active && pipe != tracked_pipe,
  10558. "active encoder's pipe doesn't match"
  10559. "(expected %i, found %i)\n",
  10560. tracked_pipe, pipe);
  10561. }
  10562. }
  10563. static void
  10564. check_crtc_state(struct drm_device *dev)
  10565. {
  10566. struct drm_i915_private *dev_priv = dev->dev_private;
  10567. struct intel_crtc *crtc;
  10568. struct intel_encoder *encoder;
  10569. struct intel_crtc_state pipe_config;
  10570. for_each_intel_crtc(dev, crtc) {
  10571. bool enabled = false;
  10572. bool active = false;
  10573. memset(&pipe_config, 0, sizeof(pipe_config));
  10574. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10575. crtc->base.base.id);
  10576. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  10577. "active crtc, but not enabled in sw tracking\n");
  10578. for_each_intel_encoder(dev, encoder) {
  10579. if (encoder->base.crtc != &crtc->base)
  10580. continue;
  10581. enabled = true;
  10582. if (encoder->connectors_active)
  10583. active = true;
  10584. }
  10585. I915_STATE_WARN(active != crtc->active,
  10586. "crtc's computed active state doesn't match tracked active state "
  10587. "(expected %i, found %i)\n", active, crtc->active);
  10588. I915_STATE_WARN(enabled != crtc->base.state->enable,
  10589. "crtc's computed enabled state doesn't match tracked enabled state "
  10590. "(expected %i, found %i)\n", enabled,
  10591. crtc->base.state->enable);
  10592. active = dev_priv->display.get_pipe_config(crtc,
  10593. &pipe_config);
  10594. /* hw state is inconsistent with the pipe quirk */
  10595. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10596. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10597. active = crtc->active;
  10598. for_each_intel_encoder(dev, encoder) {
  10599. enum pipe pipe;
  10600. if (encoder->base.crtc != &crtc->base)
  10601. continue;
  10602. if (encoder->get_hw_state(encoder, &pipe))
  10603. encoder->get_config(encoder, &pipe_config);
  10604. }
  10605. I915_STATE_WARN(crtc->active != active,
  10606. "crtc active state doesn't match with hw state "
  10607. "(expected %i, found %i)\n", crtc->active, active);
  10608. I915_STATE_WARN(crtc->active != crtc->base.state->active,
  10609. "transitional active state does not match atomic hw state "
  10610. "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
  10611. if (active &&
  10612. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  10613. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10614. intel_dump_pipe_config(crtc, &pipe_config,
  10615. "[hw state]");
  10616. intel_dump_pipe_config(crtc, crtc->config,
  10617. "[sw state]");
  10618. }
  10619. }
  10620. }
  10621. static void
  10622. check_shared_dpll_state(struct drm_device *dev)
  10623. {
  10624. struct drm_i915_private *dev_priv = dev->dev_private;
  10625. struct intel_crtc *crtc;
  10626. struct intel_dpll_hw_state dpll_hw_state;
  10627. int i;
  10628. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10629. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10630. int enabled_crtcs = 0, active_crtcs = 0;
  10631. bool active;
  10632. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10633. DRM_DEBUG_KMS("%s\n", pll->name);
  10634. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10635. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10636. "more active pll users than references: %i vs %i\n",
  10637. pll->active, hweight32(pll->config.crtc_mask));
  10638. I915_STATE_WARN(pll->active && !pll->on,
  10639. "pll in active use but not on in sw tracking\n");
  10640. I915_STATE_WARN(pll->on && !pll->active,
  10641. "pll in on but not on in use in sw tracking\n");
  10642. I915_STATE_WARN(pll->on != active,
  10643. "pll on state mismatch (expected %i, found %i)\n",
  10644. pll->on, active);
  10645. for_each_intel_crtc(dev, crtc) {
  10646. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10647. enabled_crtcs++;
  10648. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10649. active_crtcs++;
  10650. }
  10651. I915_STATE_WARN(pll->active != active_crtcs,
  10652. "pll active crtcs mismatch (expected %i, found %i)\n",
  10653. pll->active, active_crtcs);
  10654. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10655. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10656. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10657. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10658. sizeof(dpll_hw_state)),
  10659. "pll hw state mismatch\n");
  10660. }
  10661. }
  10662. void
  10663. intel_modeset_check_state(struct drm_device *dev)
  10664. {
  10665. check_wm_state(dev);
  10666. check_connector_state(dev);
  10667. check_encoder_state(dev);
  10668. check_crtc_state(dev);
  10669. check_shared_dpll_state(dev);
  10670. }
  10671. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10672. int dotclock)
  10673. {
  10674. /*
  10675. * FDI already provided one idea for the dotclock.
  10676. * Yell if the encoder disagrees.
  10677. */
  10678. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10679. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10680. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10681. }
  10682. static void update_scanline_offset(struct intel_crtc *crtc)
  10683. {
  10684. struct drm_device *dev = crtc->base.dev;
  10685. /*
  10686. * The scanline counter increments at the leading edge of hsync.
  10687. *
  10688. * On most platforms it starts counting from vtotal-1 on the
  10689. * first active line. That means the scanline counter value is
  10690. * always one less than what we would expect. Ie. just after
  10691. * start of vblank, which also occurs at start of hsync (on the
  10692. * last active line), the scanline counter will read vblank_start-1.
  10693. *
  10694. * On gen2 the scanline counter starts counting from 1 instead
  10695. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10696. * to keep the value positive), instead of adding one.
  10697. *
  10698. * On HSW+ the behaviour of the scanline counter depends on the output
  10699. * type. For DP ports it behaves like most other platforms, but on HDMI
  10700. * there's an extra 1 line difference. So we need to add two instead of
  10701. * one to the value.
  10702. */
  10703. if (IS_GEN2(dev)) {
  10704. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10705. int vtotal;
  10706. vtotal = mode->crtc_vtotal;
  10707. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10708. vtotal /= 2;
  10709. crtc->scanline_offset = vtotal - 1;
  10710. } else if (HAS_DDI(dev) &&
  10711. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10712. crtc->scanline_offset = 2;
  10713. } else
  10714. crtc->scanline_offset = 1;
  10715. }
  10716. static int intel_modeset_setup_plls(struct drm_atomic_state *state)
  10717. {
  10718. struct drm_device *dev = state->dev;
  10719. struct drm_i915_private *dev_priv = to_i915(dev);
  10720. unsigned clear_pipes = 0;
  10721. struct intel_crtc *intel_crtc;
  10722. struct intel_crtc_state *intel_crtc_state;
  10723. struct drm_crtc *crtc;
  10724. struct drm_crtc_state *crtc_state;
  10725. int ret = 0;
  10726. int i;
  10727. if (!dev_priv->display.crtc_compute_clock)
  10728. return 0;
  10729. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10730. intel_crtc = to_intel_crtc(crtc);
  10731. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10732. if (needs_modeset(crtc_state)) {
  10733. clear_pipes |= 1 << intel_crtc->pipe;
  10734. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10735. }
  10736. }
  10737. if (clear_pipes) {
  10738. struct intel_shared_dpll_config *shared_dpll =
  10739. intel_atomic_get_shared_dpll_state(state);
  10740. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10741. shared_dpll[i].crtc_mask &= ~clear_pipes;
  10742. }
  10743. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10744. if (!needs_modeset(crtc_state) || !crtc_state->enable)
  10745. continue;
  10746. intel_crtc = to_intel_crtc(crtc);
  10747. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10748. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10749. intel_crtc_state);
  10750. if (ret)
  10751. return ret;
  10752. }
  10753. return ret;
  10754. }
  10755. /* Code that should eventually be part of atomic_check() */
  10756. static int intel_modeset_checks(struct drm_atomic_state *state)
  10757. {
  10758. struct drm_device *dev = state->dev;
  10759. int ret;
  10760. /*
  10761. * See if the config requires any additional preparation, e.g.
  10762. * to adjust global state with pipes off. We need to do this
  10763. * here so we can get the modeset_pipe updated config for the new
  10764. * mode set on this crtc. For other crtcs we need to use the
  10765. * adjusted_mode bits in the crtc directly.
  10766. */
  10767. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
  10768. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
  10769. ret = valleyview_modeset_global_pipes(state);
  10770. else
  10771. ret = broadwell_modeset_global_pipes(state);
  10772. if (ret)
  10773. return ret;
  10774. }
  10775. return intel_modeset_setup_plls(state);
  10776. }
  10777. static int
  10778. intel_modeset_compute_config(struct drm_atomic_state *state)
  10779. {
  10780. struct drm_crtc *crtc;
  10781. struct drm_crtc_state *crtc_state;
  10782. int ret, i;
  10783. ret = drm_atomic_helper_check_modeset(state->dev, state);
  10784. if (ret)
  10785. return ret;
  10786. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10787. if (!crtc_state->enable &&
  10788. WARN_ON(crtc_state->active))
  10789. crtc_state->active = false;
  10790. if (!crtc_state->enable)
  10791. continue;
  10792. ret = intel_modeset_pipe_config(crtc, state);
  10793. if (ret)
  10794. return ret;
  10795. intel_dump_pipe_config(to_intel_crtc(crtc),
  10796. to_intel_crtc_state(crtc_state),
  10797. "[modeset]");
  10798. }
  10799. ret = intel_modeset_checks(state);
  10800. if (ret)
  10801. return ret;
  10802. return drm_atomic_helper_check_planes(state->dev, state);
  10803. }
  10804. static int __intel_set_mode(struct drm_atomic_state *state)
  10805. {
  10806. struct drm_device *dev = state->dev;
  10807. struct drm_i915_private *dev_priv = dev->dev_private;
  10808. struct drm_crtc *crtc;
  10809. struct drm_crtc_state *crtc_state;
  10810. int ret = 0;
  10811. int i;
  10812. ret = drm_atomic_helper_prepare_planes(dev, state);
  10813. if (ret)
  10814. return ret;
  10815. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10816. if (!needs_modeset(crtc_state) || !crtc->state->active)
  10817. continue;
  10818. intel_crtc_disable_planes(crtc);
  10819. dev_priv->display.crtc_disable(crtc);
  10820. }
  10821. /* Only after disabling all output pipelines that will be changed can we
  10822. * update the the output configuration. */
  10823. intel_modeset_update_state(state);
  10824. /* The state has been swaped above, so state actually contains the
  10825. * old state now. */
  10826. modeset_update_crtc_power_domains(state);
  10827. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10828. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10829. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  10830. if (!needs_modeset(crtc->state) || !crtc->state->active)
  10831. continue;
  10832. update_scanline_offset(to_intel_crtc(crtc));
  10833. dev_priv->display.crtc_enable(crtc);
  10834. intel_crtc_enable_planes(crtc);
  10835. }
  10836. /* FIXME: add subpixel order */
  10837. drm_atomic_helper_cleanup_planes(dev, state);
  10838. drm_atomic_state_free(state);
  10839. return 0;
  10840. }
  10841. static int intel_set_mode_checked(struct drm_atomic_state *state)
  10842. {
  10843. struct drm_device *dev = state->dev;
  10844. int ret;
  10845. ret = __intel_set_mode(state);
  10846. if (ret == 0)
  10847. intel_modeset_check_state(dev);
  10848. return ret;
  10849. }
  10850. static int intel_set_mode(struct drm_atomic_state *state)
  10851. {
  10852. int ret;
  10853. ret = intel_modeset_compute_config(state);
  10854. if (ret)
  10855. return ret;
  10856. return intel_set_mode_checked(state);
  10857. }
  10858. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  10859. {
  10860. struct drm_device *dev = crtc->dev;
  10861. struct drm_atomic_state *state;
  10862. struct intel_crtc *intel_crtc;
  10863. struct intel_encoder *encoder;
  10864. struct intel_connector *connector;
  10865. struct drm_connector_state *connector_state;
  10866. struct intel_crtc_state *crtc_state;
  10867. int ret;
  10868. state = drm_atomic_state_alloc(dev);
  10869. if (!state) {
  10870. DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
  10871. crtc->base.id);
  10872. return;
  10873. }
  10874. state->acquire_ctx = dev->mode_config.acquire_ctx;
  10875. /* The force restore path in the HW readout code relies on the staged
  10876. * config still keeping the user requested config while the actual
  10877. * state has been overwritten by the configuration read from HW. We
  10878. * need to copy the staged config to the atomic state, otherwise the
  10879. * mode set will just reapply the state the HW is already in. */
  10880. for_each_intel_encoder(dev, encoder) {
  10881. if (encoder->base.crtc != crtc)
  10882. continue;
  10883. for_each_intel_connector(dev, connector) {
  10884. if (connector->base.state->best_encoder != &encoder->base)
  10885. continue;
  10886. connector_state = drm_atomic_get_connector_state(state, &connector->base);
  10887. if (IS_ERR(connector_state)) {
  10888. DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
  10889. connector->base.base.id,
  10890. connector->base.name,
  10891. PTR_ERR(connector_state));
  10892. continue;
  10893. }
  10894. connector_state->crtc = crtc;
  10895. }
  10896. }
  10897. for_each_intel_crtc(dev, intel_crtc) {
  10898. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  10899. if (IS_ERR(crtc_state)) {
  10900. DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
  10901. intel_crtc->base.base.id,
  10902. PTR_ERR(crtc_state));
  10903. continue;
  10904. }
  10905. if (&intel_crtc->base == crtc)
  10906. drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
  10907. }
  10908. intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
  10909. crtc->primary->fb, crtc->x, crtc->y);
  10910. ret = intel_set_mode(state);
  10911. if (ret)
  10912. drm_atomic_state_free(state);
  10913. }
  10914. #undef for_each_intel_crtc_masked
  10915. static bool intel_connector_in_mode_set(struct intel_connector *connector,
  10916. struct drm_mode_set *set)
  10917. {
  10918. int ro;
  10919. for (ro = 0; ro < set->num_connectors; ro++)
  10920. if (set->connectors[ro] == &connector->base)
  10921. return true;
  10922. return false;
  10923. }
  10924. static int
  10925. intel_modeset_stage_output_state(struct drm_device *dev,
  10926. struct drm_mode_set *set,
  10927. struct drm_atomic_state *state)
  10928. {
  10929. struct intel_connector *connector;
  10930. struct drm_connector *drm_connector;
  10931. struct drm_connector_state *connector_state;
  10932. struct drm_crtc *crtc;
  10933. struct drm_crtc_state *crtc_state;
  10934. int i, ret;
  10935. /* The upper layers ensure that we either disable a crtc or have a list
  10936. * of connectors. For paranoia, double-check this. */
  10937. WARN_ON(!set->fb && (set->num_connectors != 0));
  10938. WARN_ON(set->fb && (set->num_connectors == 0));
  10939. for_each_intel_connector(dev, connector) {
  10940. bool in_mode_set = intel_connector_in_mode_set(connector, set);
  10941. if (!in_mode_set && connector->base.state->crtc != set->crtc)
  10942. continue;
  10943. connector_state =
  10944. drm_atomic_get_connector_state(state, &connector->base);
  10945. if (IS_ERR(connector_state))
  10946. return PTR_ERR(connector_state);
  10947. if (in_mode_set) {
  10948. int pipe = to_intel_crtc(set->crtc)->pipe;
  10949. connector_state->best_encoder =
  10950. &intel_find_encoder(connector, pipe)->base;
  10951. }
  10952. if (connector->base.state->crtc != set->crtc)
  10953. continue;
  10954. /* If we disable the crtc, disable all its connectors. Also, if
  10955. * the connector is on the changing crtc but not on the new
  10956. * connector list, disable it. */
  10957. if (!set->fb || !in_mode_set) {
  10958. connector_state->best_encoder = NULL;
  10959. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  10960. connector->base.base.id,
  10961. connector->base.name);
  10962. }
  10963. }
  10964. /* connector->new_encoder is now updated for all connectors. */
  10965. for_each_connector_in_state(state, drm_connector, connector_state, i) {
  10966. connector = to_intel_connector(drm_connector);
  10967. if (!connector_state->best_encoder) {
  10968. ret = drm_atomic_set_crtc_for_connector(connector_state,
  10969. NULL);
  10970. if (ret)
  10971. return ret;
  10972. continue;
  10973. }
  10974. if (intel_connector_in_mode_set(connector, set)) {
  10975. struct drm_crtc *crtc = connector->base.state->crtc;
  10976. /* If this connector was in a previous crtc, add it
  10977. * to the state. We might need to disable it. */
  10978. if (crtc) {
  10979. crtc_state =
  10980. drm_atomic_get_crtc_state(state, crtc);
  10981. if (IS_ERR(crtc_state))
  10982. return PTR_ERR(crtc_state);
  10983. }
  10984. ret = drm_atomic_set_crtc_for_connector(connector_state,
  10985. set->crtc);
  10986. if (ret)
  10987. return ret;
  10988. }
  10989. /* Make sure the new CRTC will work with the encoder */
  10990. if (!drm_encoder_crtc_ok(connector_state->best_encoder,
  10991. connector_state->crtc)) {
  10992. return -EINVAL;
  10993. }
  10994. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  10995. connector->base.base.id,
  10996. connector->base.name,
  10997. connector_state->crtc->base.id);
  10998. if (connector_state->best_encoder != &connector->encoder->base)
  10999. connector->encoder =
  11000. to_intel_encoder(connector_state->best_encoder);
  11001. }
  11002. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11003. bool has_connectors;
  11004. ret = drm_atomic_add_affected_connectors(state, crtc);
  11005. if (ret)
  11006. return ret;
  11007. has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
  11008. if (has_connectors != crtc_state->enable)
  11009. crtc_state->enable =
  11010. crtc_state->active = has_connectors;
  11011. }
  11012. ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
  11013. set->fb, set->x, set->y);
  11014. if (ret)
  11015. return ret;
  11016. crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
  11017. if (IS_ERR(crtc_state))
  11018. return PTR_ERR(crtc_state);
  11019. if (set->mode)
  11020. drm_mode_copy(&crtc_state->mode, set->mode);
  11021. if (set->num_connectors)
  11022. crtc_state->active = true;
  11023. return 0;
  11024. }
  11025. static int intel_crtc_set_config(struct drm_mode_set *set)
  11026. {
  11027. struct drm_device *dev;
  11028. struct drm_atomic_state *state = NULL;
  11029. int ret;
  11030. BUG_ON(!set);
  11031. BUG_ON(!set->crtc);
  11032. BUG_ON(!set->crtc->helper_private);
  11033. /* Enforce sane interface api - has been abused by the fb helper. */
  11034. BUG_ON(!set->mode && set->fb);
  11035. BUG_ON(set->fb && set->num_connectors == 0);
  11036. if (set->fb) {
  11037. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  11038. set->crtc->base.id, set->fb->base.id,
  11039. (int)set->num_connectors, set->x, set->y);
  11040. } else {
  11041. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  11042. }
  11043. dev = set->crtc->dev;
  11044. state = drm_atomic_state_alloc(dev);
  11045. if (!state)
  11046. return -ENOMEM;
  11047. state->acquire_ctx = dev->mode_config.acquire_ctx;
  11048. ret = intel_modeset_stage_output_state(dev, set, state);
  11049. if (ret)
  11050. goto out;
  11051. ret = intel_modeset_compute_config(state);
  11052. if (ret)
  11053. goto out;
  11054. intel_update_pipe_size(to_intel_crtc(set->crtc));
  11055. ret = intel_set_mode_checked(state);
  11056. if (ret) {
  11057. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  11058. set->crtc->base.id, ret);
  11059. }
  11060. out:
  11061. if (ret)
  11062. drm_atomic_state_free(state);
  11063. return ret;
  11064. }
  11065. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11066. .gamma_set = intel_crtc_gamma_set,
  11067. .set_config = intel_crtc_set_config,
  11068. .destroy = intel_crtc_destroy,
  11069. .page_flip = intel_crtc_page_flip,
  11070. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11071. .atomic_destroy_state = intel_crtc_destroy_state,
  11072. };
  11073. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11074. struct intel_shared_dpll *pll,
  11075. struct intel_dpll_hw_state *hw_state)
  11076. {
  11077. uint32_t val;
  11078. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11079. return false;
  11080. val = I915_READ(PCH_DPLL(pll->id));
  11081. hw_state->dpll = val;
  11082. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11083. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11084. return val & DPLL_VCO_ENABLE;
  11085. }
  11086. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11087. struct intel_shared_dpll *pll)
  11088. {
  11089. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11090. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11091. }
  11092. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11093. struct intel_shared_dpll *pll)
  11094. {
  11095. /* PCH refclock must be enabled first */
  11096. ibx_assert_pch_refclk_enabled(dev_priv);
  11097. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11098. /* Wait for the clocks to stabilize. */
  11099. POSTING_READ(PCH_DPLL(pll->id));
  11100. udelay(150);
  11101. /* The pixel multiplier can only be updated once the
  11102. * DPLL is enabled and the clocks are stable.
  11103. *
  11104. * So write it again.
  11105. */
  11106. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11107. POSTING_READ(PCH_DPLL(pll->id));
  11108. udelay(200);
  11109. }
  11110. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11111. struct intel_shared_dpll *pll)
  11112. {
  11113. struct drm_device *dev = dev_priv->dev;
  11114. struct intel_crtc *crtc;
  11115. /* Make sure no transcoder isn't still depending on us. */
  11116. for_each_intel_crtc(dev, crtc) {
  11117. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11118. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11119. }
  11120. I915_WRITE(PCH_DPLL(pll->id), 0);
  11121. POSTING_READ(PCH_DPLL(pll->id));
  11122. udelay(200);
  11123. }
  11124. static char *ibx_pch_dpll_names[] = {
  11125. "PCH DPLL A",
  11126. "PCH DPLL B",
  11127. };
  11128. static void ibx_pch_dpll_init(struct drm_device *dev)
  11129. {
  11130. struct drm_i915_private *dev_priv = dev->dev_private;
  11131. int i;
  11132. dev_priv->num_shared_dpll = 2;
  11133. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11134. dev_priv->shared_dplls[i].id = i;
  11135. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11136. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11137. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11138. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11139. dev_priv->shared_dplls[i].get_hw_state =
  11140. ibx_pch_dpll_get_hw_state;
  11141. }
  11142. }
  11143. static void intel_shared_dpll_init(struct drm_device *dev)
  11144. {
  11145. struct drm_i915_private *dev_priv = dev->dev_private;
  11146. intel_update_cdclk(dev);
  11147. if (HAS_DDI(dev))
  11148. intel_ddi_pll_init(dev);
  11149. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11150. ibx_pch_dpll_init(dev);
  11151. else
  11152. dev_priv->num_shared_dpll = 0;
  11153. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11154. }
  11155. /**
  11156. * intel_wm_need_update - Check whether watermarks need updating
  11157. * @plane: drm plane
  11158. * @state: new plane state
  11159. *
  11160. * Check current plane state versus the new one to determine whether
  11161. * watermarks need to be recalculated.
  11162. *
  11163. * Returns true or false.
  11164. */
  11165. bool intel_wm_need_update(struct drm_plane *plane,
  11166. struct drm_plane_state *state)
  11167. {
  11168. /* Update watermarks on tiling changes. */
  11169. if (!plane->state->fb || !state->fb ||
  11170. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  11171. plane->state->rotation != state->rotation)
  11172. return true;
  11173. return false;
  11174. }
  11175. /**
  11176. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11177. * @plane: drm plane to prepare for
  11178. * @fb: framebuffer to prepare for presentation
  11179. *
  11180. * Prepares a framebuffer for usage on a display plane. Generally this
  11181. * involves pinning the underlying object and updating the frontbuffer tracking
  11182. * bits. Some older platforms need special physical address handling for
  11183. * cursor planes.
  11184. *
  11185. * Returns 0 on success, negative error code on failure.
  11186. */
  11187. int
  11188. intel_prepare_plane_fb(struct drm_plane *plane,
  11189. struct drm_framebuffer *fb,
  11190. const struct drm_plane_state *new_state)
  11191. {
  11192. struct drm_device *dev = plane->dev;
  11193. struct intel_plane *intel_plane = to_intel_plane(plane);
  11194. enum pipe pipe = intel_plane->pipe;
  11195. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11196. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11197. unsigned frontbuffer_bits = 0;
  11198. int ret = 0;
  11199. if (!obj)
  11200. return 0;
  11201. switch (plane->type) {
  11202. case DRM_PLANE_TYPE_PRIMARY:
  11203. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11204. break;
  11205. case DRM_PLANE_TYPE_CURSOR:
  11206. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  11207. break;
  11208. case DRM_PLANE_TYPE_OVERLAY:
  11209. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  11210. break;
  11211. }
  11212. mutex_lock(&dev->struct_mutex);
  11213. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11214. INTEL_INFO(dev)->cursor_needs_physical) {
  11215. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11216. ret = i915_gem_object_attach_phys(obj, align);
  11217. if (ret)
  11218. DRM_DEBUG_KMS("failed to attach phys object\n");
  11219. } else {
  11220. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
  11221. }
  11222. if (ret == 0)
  11223. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  11224. mutex_unlock(&dev->struct_mutex);
  11225. return ret;
  11226. }
  11227. /**
  11228. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11229. * @plane: drm plane to clean up for
  11230. * @fb: old framebuffer that was on plane
  11231. *
  11232. * Cleans up a framebuffer that has just been removed from a plane.
  11233. */
  11234. void
  11235. intel_cleanup_plane_fb(struct drm_plane *plane,
  11236. struct drm_framebuffer *fb,
  11237. const struct drm_plane_state *old_state)
  11238. {
  11239. struct drm_device *dev = plane->dev;
  11240. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11241. if (WARN_ON(!obj))
  11242. return;
  11243. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11244. !INTEL_INFO(dev)->cursor_needs_physical) {
  11245. mutex_lock(&dev->struct_mutex);
  11246. intel_unpin_fb_obj(fb, old_state);
  11247. mutex_unlock(&dev->struct_mutex);
  11248. }
  11249. }
  11250. int
  11251. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11252. {
  11253. int max_scale;
  11254. struct drm_device *dev;
  11255. struct drm_i915_private *dev_priv;
  11256. int crtc_clock, cdclk;
  11257. if (!intel_crtc || !crtc_state)
  11258. return DRM_PLANE_HELPER_NO_SCALING;
  11259. dev = intel_crtc->base.dev;
  11260. dev_priv = dev->dev_private;
  11261. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11262. cdclk = dev_priv->display.get_display_clock_speed(dev);
  11263. if (!crtc_clock || !cdclk)
  11264. return DRM_PLANE_HELPER_NO_SCALING;
  11265. /*
  11266. * skl max scale is lower of:
  11267. * close to 3 but not 3, -1 is for that purpose
  11268. * or
  11269. * cdclk/crtc_clock
  11270. */
  11271. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11272. return max_scale;
  11273. }
  11274. static int
  11275. intel_check_primary_plane(struct drm_plane *plane,
  11276. struct intel_plane_state *state)
  11277. {
  11278. struct drm_device *dev = plane->dev;
  11279. struct drm_i915_private *dev_priv = dev->dev_private;
  11280. struct drm_crtc *crtc = state->base.crtc;
  11281. struct intel_crtc *intel_crtc;
  11282. struct intel_crtc_state *crtc_state;
  11283. struct drm_framebuffer *fb = state->base.fb;
  11284. struct drm_rect *dest = &state->dst;
  11285. struct drm_rect *src = &state->src;
  11286. const struct drm_rect *clip = &state->clip;
  11287. bool can_position = false;
  11288. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11289. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11290. int ret;
  11291. crtc = crtc ? crtc : plane->crtc;
  11292. intel_crtc = to_intel_crtc(crtc);
  11293. crtc_state = state->base.state ?
  11294. intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
  11295. if (INTEL_INFO(dev)->gen >= 9) {
  11296. /* use scaler when colorkey is not required */
  11297. if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
  11298. min_scale = 1;
  11299. max_scale = skl_max_scale(intel_crtc, crtc_state);
  11300. }
  11301. can_position = true;
  11302. }
  11303. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11304. src, dest, clip,
  11305. min_scale,
  11306. max_scale,
  11307. can_position, true,
  11308. &state->visible);
  11309. if (ret)
  11310. return ret;
  11311. if (intel_crtc->active) {
  11312. struct intel_plane_state *old_state =
  11313. to_intel_plane_state(plane->state);
  11314. intel_crtc->atomic.wait_for_flips = true;
  11315. /*
  11316. * FBC does not work on some platforms for rotated
  11317. * planes, so disable it when rotation is not 0 and
  11318. * update it when rotation is set back to 0.
  11319. *
  11320. * FIXME: This is redundant with the fbc update done in
  11321. * the primary plane enable function except that that
  11322. * one is done too late. We eventually need to unify
  11323. * this.
  11324. */
  11325. if (state->visible &&
  11326. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  11327. dev_priv->fbc.crtc == intel_crtc &&
  11328. state->base.rotation != BIT(DRM_ROTATE_0)) {
  11329. intel_crtc->atomic.disable_fbc = true;
  11330. }
  11331. if (state->visible && !old_state->visible) {
  11332. /*
  11333. * BDW signals flip done immediately if the plane
  11334. * is disabled, even if the plane enable is already
  11335. * armed to occur at the next vblank :(
  11336. */
  11337. if (IS_BROADWELL(dev))
  11338. intel_crtc->atomic.wait_vblank = true;
  11339. if (crtc_state && !needs_modeset(&crtc_state->base))
  11340. intel_crtc->atomic.post_enable_primary = true;
  11341. }
  11342. if (!state->visible && old_state->visible &&
  11343. crtc_state && !needs_modeset(&crtc_state->base))
  11344. intel_crtc->atomic.pre_disable_primary = true;
  11345. intel_crtc->atomic.fb_bits |=
  11346. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  11347. intel_crtc->atomic.update_fbc = true;
  11348. if (intel_wm_need_update(plane, &state->base))
  11349. intel_crtc->atomic.update_wm = true;
  11350. }
  11351. if (INTEL_INFO(dev)->gen >= 9) {
  11352. ret = skl_update_scaler_users(intel_crtc, crtc_state,
  11353. to_intel_plane(plane), state, 0);
  11354. if (ret)
  11355. return ret;
  11356. }
  11357. return 0;
  11358. }
  11359. static void
  11360. intel_commit_primary_plane(struct drm_plane *plane,
  11361. struct intel_plane_state *state)
  11362. {
  11363. struct drm_crtc *crtc = state->base.crtc;
  11364. struct drm_framebuffer *fb = state->base.fb;
  11365. struct drm_device *dev = plane->dev;
  11366. struct drm_i915_private *dev_priv = dev->dev_private;
  11367. struct intel_crtc *intel_crtc;
  11368. struct drm_rect *src = &state->src;
  11369. crtc = crtc ? crtc : plane->crtc;
  11370. intel_crtc = to_intel_crtc(crtc);
  11371. plane->fb = fb;
  11372. crtc->x = src->x1 >> 16;
  11373. crtc->y = src->y1 >> 16;
  11374. if (intel_crtc->active) {
  11375. if (state->visible)
  11376. /* FIXME: kill this fastboot hack */
  11377. intel_update_pipe_size(intel_crtc);
  11378. dev_priv->display.update_primary_plane(crtc, plane->fb,
  11379. crtc->x, crtc->y);
  11380. }
  11381. }
  11382. static void
  11383. intel_disable_primary_plane(struct drm_plane *plane,
  11384. struct drm_crtc *crtc,
  11385. bool force)
  11386. {
  11387. struct drm_device *dev = plane->dev;
  11388. struct drm_i915_private *dev_priv = dev->dev_private;
  11389. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11390. }
  11391. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  11392. {
  11393. struct drm_device *dev = crtc->dev;
  11394. struct drm_i915_private *dev_priv = dev->dev_private;
  11395. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11396. struct intel_plane *intel_plane;
  11397. struct drm_plane *p;
  11398. unsigned fb_bits = 0;
  11399. /* Track fb's for any planes being disabled */
  11400. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  11401. intel_plane = to_intel_plane(p);
  11402. if (intel_crtc->atomic.disabled_planes &
  11403. (1 << drm_plane_index(p))) {
  11404. switch (p->type) {
  11405. case DRM_PLANE_TYPE_PRIMARY:
  11406. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  11407. break;
  11408. case DRM_PLANE_TYPE_CURSOR:
  11409. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  11410. break;
  11411. case DRM_PLANE_TYPE_OVERLAY:
  11412. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  11413. break;
  11414. }
  11415. mutex_lock(&dev->struct_mutex);
  11416. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  11417. mutex_unlock(&dev->struct_mutex);
  11418. }
  11419. }
  11420. if (intel_crtc->atomic.wait_for_flips)
  11421. intel_crtc_wait_for_pending_flips(crtc);
  11422. if (intel_crtc->atomic.disable_fbc)
  11423. intel_fbc_disable(dev);
  11424. if (intel_crtc->atomic.pre_disable_primary)
  11425. intel_pre_disable_primary(crtc);
  11426. if (intel_crtc->atomic.update_wm)
  11427. intel_update_watermarks(crtc);
  11428. intel_runtime_pm_get(dev_priv);
  11429. /* Perform vblank evasion around commit operation */
  11430. if (intel_crtc->active)
  11431. intel_crtc->atomic.evade =
  11432. intel_pipe_update_start(intel_crtc,
  11433. &intel_crtc->atomic.start_vbl_count);
  11434. }
  11435. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  11436. {
  11437. struct drm_device *dev = crtc->dev;
  11438. struct drm_i915_private *dev_priv = dev->dev_private;
  11439. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11440. struct drm_plane *p;
  11441. if (intel_crtc->atomic.evade)
  11442. intel_pipe_update_end(intel_crtc,
  11443. intel_crtc->atomic.start_vbl_count);
  11444. intel_runtime_pm_put(dev_priv);
  11445. if (intel_crtc->atomic.wait_vblank && intel_crtc->active)
  11446. intel_wait_for_vblank(dev, intel_crtc->pipe);
  11447. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  11448. if (intel_crtc->atomic.update_fbc) {
  11449. mutex_lock(&dev->struct_mutex);
  11450. intel_fbc_update(dev);
  11451. mutex_unlock(&dev->struct_mutex);
  11452. }
  11453. if (intel_crtc->atomic.post_enable_primary)
  11454. intel_post_enable_primary(crtc);
  11455. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  11456. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  11457. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  11458. false, false);
  11459. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  11460. }
  11461. /**
  11462. * intel_plane_destroy - destroy a plane
  11463. * @plane: plane to destroy
  11464. *
  11465. * Common destruction function for all types of planes (primary, cursor,
  11466. * sprite).
  11467. */
  11468. void intel_plane_destroy(struct drm_plane *plane)
  11469. {
  11470. struct intel_plane *intel_plane = to_intel_plane(plane);
  11471. drm_plane_cleanup(plane);
  11472. kfree(intel_plane);
  11473. }
  11474. const struct drm_plane_funcs intel_plane_funcs = {
  11475. .update_plane = drm_atomic_helper_update_plane,
  11476. .disable_plane = drm_atomic_helper_disable_plane,
  11477. .destroy = intel_plane_destroy,
  11478. .set_property = drm_atomic_helper_plane_set_property,
  11479. .atomic_get_property = intel_plane_atomic_get_property,
  11480. .atomic_set_property = intel_plane_atomic_set_property,
  11481. .atomic_duplicate_state = intel_plane_duplicate_state,
  11482. .atomic_destroy_state = intel_plane_destroy_state,
  11483. };
  11484. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11485. int pipe)
  11486. {
  11487. struct intel_plane *primary;
  11488. struct intel_plane_state *state;
  11489. const uint32_t *intel_primary_formats;
  11490. int num_formats;
  11491. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11492. if (primary == NULL)
  11493. return NULL;
  11494. state = intel_create_plane_state(&primary->base);
  11495. if (!state) {
  11496. kfree(primary);
  11497. return NULL;
  11498. }
  11499. primary->base.state = &state->base;
  11500. primary->can_scale = false;
  11501. primary->max_downscale = 1;
  11502. if (INTEL_INFO(dev)->gen >= 9) {
  11503. primary->can_scale = true;
  11504. state->scaler_id = -1;
  11505. }
  11506. primary->pipe = pipe;
  11507. primary->plane = pipe;
  11508. primary->check_plane = intel_check_primary_plane;
  11509. primary->commit_plane = intel_commit_primary_plane;
  11510. primary->disable_plane = intel_disable_primary_plane;
  11511. primary->ckey.flags = I915_SET_COLORKEY_NONE;
  11512. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11513. primary->plane = !pipe;
  11514. if (INTEL_INFO(dev)->gen >= 9) {
  11515. intel_primary_formats = skl_primary_formats;
  11516. num_formats = ARRAY_SIZE(skl_primary_formats);
  11517. } else if (INTEL_INFO(dev)->gen >= 4) {
  11518. intel_primary_formats = i965_primary_formats;
  11519. num_formats = ARRAY_SIZE(i965_primary_formats);
  11520. } else {
  11521. intel_primary_formats = i8xx_primary_formats;
  11522. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11523. }
  11524. drm_universal_plane_init(dev, &primary->base, 0,
  11525. &intel_plane_funcs,
  11526. intel_primary_formats, num_formats,
  11527. DRM_PLANE_TYPE_PRIMARY);
  11528. if (INTEL_INFO(dev)->gen >= 4)
  11529. intel_create_rotation_property(dev, primary);
  11530. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11531. return &primary->base;
  11532. }
  11533. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11534. {
  11535. if (!dev->mode_config.rotation_property) {
  11536. unsigned long flags = BIT(DRM_ROTATE_0) |
  11537. BIT(DRM_ROTATE_180);
  11538. if (INTEL_INFO(dev)->gen >= 9)
  11539. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11540. dev->mode_config.rotation_property =
  11541. drm_mode_create_rotation_property(dev, flags);
  11542. }
  11543. if (dev->mode_config.rotation_property)
  11544. drm_object_attach_property(&plane->base.base,
  11545. dev->mode_config.rotation_property,
  11546. plane->base.state->rotation);
  11547. }
  11548. static int
  11549. intel_check_cursor_plane(struct drm_plane *plane,
  11550. struct intel_plane_state *state)
  11551. {
  11552. struct drm_crtc *crtc = state->base.crtc;
  11553. struct drm_device *dev = plane->dev;
  11554. struct drm_framebuffer *fb = state->base.fb;
  11555. struct drm_rect *dest = &state->dst;
  11556. struct drm_rect *src = &state->src;
  11557. const struct drm_rect *clip = &state->clip;
  11558. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11559. struct intel_crtc *intel_crtc;
  11560. unsigned stride;
  11561. int ret;
  11562. crtc = crtc ? crtc : plane->crtc;
  11563. intel_crtc = to_intel_crtc(crtc);
  11564. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11565. src, dest, clip,
  11566. DRM_PLANE_HELPER_NO_SCALING,
  11567. DRM_PLANE_HELPER_NO_SCALING,
  11568. true, true, &state->visible);
  11569. if (ret)
  11570. return ret;
  11571. /* if we want to turn off the cursor ignore width and height */
  11572. if (!obj)
  11573. goto finish;
  11574. /* Check for which cursor types we support */
  11575. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  11576. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11577. state->base.crtc_w, state->base.crtc_h);
  11578. return -EINVAL;
  11579. }
  11580. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11581. if (obj->base.size < stride * state->base.crtc_h) {
  11582. DRM_DEBUG_KMS("buffer is too small\n");
  11583. return -ENOMEM;
  11584. }
  11585. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11586. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11587. ret = -EINVAL;
  11588. }
  11589. finish:
  11590. if (intel_crtc->active) {
  11591. if (plane->state->crtc_w != state->base.crtc_w)
  11592. intel_crtc->atomic.update_wm = true;
  11593. intel_crtc->atomic.fb_bits |=
  11594. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  11595. }
  11596. return ret;
  11597. }
  11598. static void
  11599. intel_disable_cursor_plane(struct drm_plane *plane,
  11600. struct drm_crtc *crtc,
  11601. bool force)
  11602. {
  11603. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11604. if (!force) {
  11605. plane->fb = NULL;
  11606. intel_crtc->cursor_bo = NULL;
  11607. intel_crtc->cursor_addr = 0;
  11608. }
  11609. intel_crtc_update_cursor(crtc, false);
  11610. }
  11611. static void
  11612. intel_commit_cursor_plane(struct drm_plane *plane,
  11613. struct intel_plane_state *state)
  11614. {
  11615. struct drm_crtc *crtc = state->base.crtc;
  11616. struct drm_device *dev = plane->dev;
  11617. struct intel_crtc *intel_crtc;
  11618. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11619. uint32_t addr;
  11620. crtc = crtc ? crtc : plane->crtc;
  11621. intel_crtc = to_intel_crtc(crtc);
  11622. plane->fb = state->base.fb;
  11623. crtc->cursor_x = state->base.crtc_x;
  11624. crtc->cursor_y = state->base.crtc_y;
  11625. if (intel_crtc->cursor_bo == obj)
  11626. goto update;
  11627. if (!obj)
  11628. addr = 0;
  11629. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11630. addr = i915_gem_obj_ggtt_offset(obj);
  11631. else
  11632. addr = obj->phys_handle->busaddr;
  11633. intel_crtc->cursor_addr = addr;
  11634. intel_crtc->cursor_bo = obj;
  11635. update:
  11636. if (intel_crtc->active)
  11637. intel_crtc_update_cursor(crtc, state->visible);
  11638. }
  11639. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11640. int pipe)
  11641. {
  11642. struct intel_plane *cursor;
  11643. struct intel_plane_state *state;
  11644. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11645. if (cursor == NULL)
  11646. return NULL;
  11647. state = intel_create_plane_state(&cursor->base);
  11648. if (!state) {
  11649. kfree(cursor);
  11650. return NULL;
  11651. }
  11652. cursor->base.state = &state->base;
  11653. cursor->can_scale = false;
  11654. cursor->max_downscale = 1;
  11655. cursor->pipe = pipe;
  11656. cursor->plane = pipe;
  11657. cursor->check_plane = intel_check_cursor_plane;
  11658. cursor->commit_plane = intel_commit_cursor_plane;
  11659. cursor->disable_plane = intel_disable_cursor_plane;
  11660. drm_universal_plane_init(dev, &cursor->base, 0,
  11661. &intel_plane_funcs,
  11662. intel_cursor_formats,
  11663. ARRAY_SIZE(intel_cursor_formats),
  11664. DRM_PLANE_TYPE_CURSOR);
  11665. if (INTEL_INFO(dev)->gen >= 4) {
  11666. if (!dev->mode_config.rotation_property)
  11667. dev->mode_config.rotation_property =
  11668. drm_mode_create_rotation_property(dev,
  11669. BIT(DRM_ROTATE_0) |
  11670. BIT(DRM_ROTATE_180));
  11671. if (dev->mode_config.rotation_property)
  11672. drm_object_attach_property(&cursor->base.base,
  11673. dev->mode_config.rotation_property,
  11674. state->base.rotation);
  11675. }
  11676. if (INTEL_INFO(dev)->gen >=9)
  11677. state->scaler_id = -1;
  11678. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11679. return &cursor->base;
  11680. }
  11681. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11682. struct intel_crtc_state *crtc_state)
  11683. {
  11684. int i;
  11685. struct intel_scaler *intel_scaler;
  11686. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11687. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11688. intel_scaler = &scaler_state->scalers[i];
  11689. intel_scaler->in_use = 0;
  11690. intel_scaler->id = i;
  11691. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11692. }
  11693. scaler_state->scaler_id = -1;
  11694. }
  11695. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11696. {
  11697. struct drm_i915_private *dev_priv = dev->dev_private;
  11698. struct intel_crtc *intel_crtc;
  11699. struct intel_crtc_state *crtc_state = NULL;
  11700. struct drm_plane *primary = NULL;
  11701. struct drm_plane *cursor = NULL;
  11702. int i, ret;
  11703. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11704. if (intel_crtc == NULL)
  11705. return;
  11706. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11707. if (!crtc_state)
  11708. goto fail;
  11709. intel_crtc->config = crtc_state;
  11710. intel_crtc->base.state = &crtc_state->base;
  11711. crtc_state->base.crtc = &intel_crtc->base;
  11712. /* initialize shared scalers */
  11713. if (INTEL_INFO(dev)->gen >= 9) {
  11714. if (pipe == PIPE_C)
  11715. intel_crtc->num_scalers = 1;
  11716. else
  11717. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11718. skl_init_scalers(dev, intel_crtc, crtc_state);
  11719. }
  11720. primary = intel_primary_plane_create(dev, pipe);
  11721. if (!primary)
  11722. goto fail;
  11723. cursor = intel_cursor_plane_create(dev, pipe);
  11724. if (!cursor)
  11725. goto fail;
  11726. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11727. cursor, &intel_crtc_funcs);
  11728. if (ret)
  11729. goto fail;
  11730. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11731. for (i = 0; i < 256; i++) {
  11732. intel_crtc->lut_r[i] = i;
  11733. intel_crtc->lut_g[i] = i;
  11734. intel_crtc->lut_b[i] = i;
  11735. }
  11736. /*
  11737. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11738. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11739. */
  11740. intel_crtc->pipe = pipe;
  11741. intel_crtc->plane = pipe;
  11742. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11743. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11744. intel_crtc->plane = !pipe;
  11745. }
  11746. intel_crtc->cursor_base = ~0;
  11747. intel_crtc->cursor_cntl = ~0;
  11748. intel_crtc->cursor_size = ~0;
  11749. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11750. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11751. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11752. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11753. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11754. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11755. return;
  11756. fail:
  11757. if (primary)
  11758. drm_plane_cleanup(primary);
  11759. if (cursor)
  11760. drm_plane_cleanup(cursor);
  11761. kfree(crtc_state);
  11762. kfree(intel_crtc);
  11763. }
  11764. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11765. {
  11766. struct drm_encoder *encoder = connector->base.encoder;
  11767. struct drm_device *dev = connector->base.dev;
  11768. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11769. if (!encoder || WARN_ON(!encoder->crtc))
  11770. return INVALID_PIPE;
  11771. return to_intel_crtc(encoder->crtc)->pipe;
  11772. }
  11773. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11774. struct drm_file *file)
  11775. {
  11776. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11777. struct drm_crtc *drmmode_crtc;
  11778. struct intel_crtc *crtc;
  11779. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11780. if (!drmmode_crtc) {
  11781. DRM_ERROR("no such CRTC id\n");
  11782. return -ENOENT;
  11783. }
  11784. crtc = to_intel_crtc(drmmode_crtc);
  11785. pipe_from_crtc_id->pipe = crtc->pipe;
  11786. return 0;
  11787. }
  11788. static int intel_encoder_clones(struct intel_encoder *encoder)
  11789. {
  11790. struct drm_device *dev = encoder->base.dev;
  11791. struct intel_encoder *source_encoder;
  11792. int index_mask = 0;
  11793. int entry = 0;
  11794. for_each_intel_encoder(dev, source_encoder) {
  11795. if (encoders_cloneable(encoder, source_encoder))
  11796. index_mask |= (1 << entry);
  11797. entry++;
  11798. }
  11799. return index_mask;
  11800. }
  11801. static bool has_edp_a(struct drm_device *dev)
  11802. {
  11803. struct drm_i915_private *dev_priv = dev->dev_private;
  11804. if (!IS_MOBILE(dev))
  11805. return false;
  11806. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11807. return false;
  11808. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11809. return false;
  11810. return true;
  11811. }
  11812. static bool intel_crt_present(struct drm_device *dev)
  11813. {
  11814. struct drm_i915_private *dev_priv = dev->dev_private;
  11815. if (INTEL_INFO(dev)->gen >= 9)
  11816. return false;
  11817. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11818. return false;
  11819. if (IS_CHERRYVIEW(dev))
  11820. return false;
  11821. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11822. return false;
  11823. return true;
  11824. }
  11825. static void intel_setup_outputs(struct drm_device *dev)
  11826. {
  11827. struct drm_i915_private *dev_priv = dev->dev_private;
  11828. struct intel_encoder *encoder;
  11829. bool dpd_is_edp = false;
  11830. intel_lvds_init(dev);
  11831. if (intel_crt_present(dev))
  11832. intel_crt_init(dev);
  11833. if (IS_BROXTON(dev)) {
  11834. /*
  11835. * FIXME: Broxton doesn't support port detection via the
  11836. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11837. * detect the ports.
  11838. */
  11839. intel_ddi_init(dev, PORT_A);
  11840. intel_ddi_init(dev, PORT_B);
  11841. intel_ddi_init(dev, PORT_C);
  11842. } else if (HAS_DDI(dev)) {
  11843. int found;
  11844. /*
  11845. * Haswell uses DDI functions to detect digital outputs.
  11846. * On SKL pre-D0 the strap isn't connected, so we assume
  11847. * it's there.
  11848. */
  11849. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11850. /* WaIgnoreDDIAStrap: skl */
  11851. if (found ||
  11852. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  11853. intel_ddi_init(dev, PORT_A);
  11854. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11855. * register */
  11856. found = I915_READ(SFUSE_STRAP);
  11857. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11858. intel_ddi_init(dev, PORT_B);
  11859. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11860. intel_ddi_init(dev, PORT_C);
  11861. if (found & SFUSE_STRAP_DDID_DETECTED)
  11862. intel_ddi_init(dev, PORT_D);
  11863. } else if (HAS_PCH_SPLIT(dev)) {
  11864. int found;
  11865. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11866. if (has_edp_a(dev))
  11867. intel_dp_init(dev, DP_A, PORT_A);
  11868. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11869. /* PCH SDVOB multiplex with HDMIB */
  11870. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11871. if (!found)
  11872. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11873. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11874. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11875. }
  11876. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11877. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11878. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11879. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11880. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11881. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11882. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11883. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11884. } else if (IS_VALLEYVIEW(dev)) {
  11885. /*
  11886. * The DP_DETECTED bit is the latched state of the DDC
  11887. * SDA pin at boot. However since eDP doesn't require DDC
  11888. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11889. * eDP ports may have been muxed to an alternate function.
  11890. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11891. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11892. * detect eDP ports.
  11893. */
  11894. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  11895. !intel_dp_is_edp(dev, PORT_B))
  11896. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  11897. PORT_B);
  11898. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  11899. intel_dp_is_edp(dev, PORT_B))
  11900. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  11901. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  11902. !intel_dp_is_edp(dev, PORT_C))
  11903. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  11904. PORT_C);
  11905. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  11906. intel_dp_is_edp(dev, PORT_C))
  11907. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  11908. if (IS_CHERRYVIEW(dev)) {
  11909. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  11910. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  11911. PORT_D);
  11912. /* eDP not supported on port D, so don't check VBT */
  11913. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  11914. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  11915. }
  11916. intel_dsi_init(dev);
  11917. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  11918. bool found = false;
  11919. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11920. DRM_DEBUG_KMS("probing SDVOB\n");
  11921. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  11922. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  11923. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11924. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11925. }
  11926. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  11927. intel_dp_init(dev, DP_B, PORT_B);
  11928. }
  11929. /* Before G4X SDVOC doesn't have its own detect register */
  11930. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11931. DRM_DEBUG_KMS("probing SDVOC\n");
  11932. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  11933. }
  11934. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11935. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  11936. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11937. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11938. }
  11939. if (SUPPORTS_INTEGRATED_DP(dev))
  11940. intel_dp_init(dev, DP_C, PORT_C);
  11941. }
  11942. if (SUPPORTS_INTEGRATED_DP(dev) &&
  11943. (I915_READ(DP_D) & DP_DETECTED))
  11944. intel_dp_init(dev, DP_D, PORT_D);
  11945. } else if (IS_GEN2(dev))
  11946. intel_dvo_init(dev);
  11947. if (SUPPORTS_TV(dev))
  11948. intel_tv_init(dev);
  11949. intel_psr_init(dev);
  11950. for_each_intel_encoder(dev, encoder) {
  11951. encoder->base.possible_crtcs = encoder->crtc_mask;
  11952. encoder->base.possible_clones =
  11953. intel_encoder_clones(encoder);
  11954. }
  11955. intel_init_pch_refclk(dev);
  11956. drm_helper_move_panel_connectors_to_head(dev);
  11957. }
  11958. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11959. {
  11960. struct drm_device *dev = fb->dev;
  11961. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11962. drm_framebuffer_cleanup(fb);
  11963. mutex_lock(&dev->struct_mutex);
  11964. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11965. drm_gem_object_unreference(&intel_fb->obj->base);
  11966. mutex_unlock(&dev->struct_mutex);
  11967. kfree(intel_fb);
  11968. }
  11969. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11970. struct drm_file *file,
  11971. unsigned int *handle)
  11972. {
  11973. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11974. struct drm_i915_gem_object *obj = intel_fb->obj;
  11975. return drm_gem_handle_create(file, &obj->base, handle);
  11976. }
  11977. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11978. .destroy = intel_user_framebuffer_destroy,
  11979. .create_handle = intel_user_framebuffer_create_handle,
  11980. };
  11981. static
  11982. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11983. uint32_t pixel_format)
  11984. {
  11985. u32 gen = INTEL_INFO(dev)->gen;
  11986. if (gen >= 9) {
  11987. /* "The stride in bytes must not exceed the of the size of 8K
  11988. * pixels and 32K bytes."
  11989. */
  11990. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  11991. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  11992. return 32*1024;
  11993. } else if (gen >= 4) {
  11994. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11995. return 16*1024;
  11996. else
  11997. return 32*1024;
  11998. } else if (gen >= 3) {
  11999. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12000. return 8*1024;
  12001. else
  12002. return 16*1024;
  12003. } else {
  12004. /* XXX DSPC is limited to 4k tiled */
  12005. return 8*1024;
  12006. }
  12007. }
  12008. static int intel_framebuffer_init(struct drm_device *dev,
  12009. struct intel_framebuffer *intel_fb,
  12010. struct drm_mode_fb_cmd2 *mode_cmd,
  12011. struct drm_i915_gem_object *obj)
  12012. {
  12013. unsigned int aligned_height;
  12014. int ret;
  12015. u32 pitch_limit, stride_alignment;
  12016. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12017. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12018. /* Enforce that fb modifier and tiling mode match, but only for
  12019. * X-tiled. This is needed for FBC. */
  12020. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12021. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12022. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12023. return -EINVAL;
  12024. }
  12025. } else {
  12026. if (obj->tiling_mode == I915_TILING_X)
  12027. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12028. else if (obj->tiling_mode == I915_TILING_Y) {
  12029. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12030. return -EINVAL;
  12031. }
  12032. }
  12033. /* Passed in modifier sanity checking. */
  12034. switch (mode_cmd->modifier[0]) {
  12035. case I915_FORMAT_MOD_Y_TILED:
  12036. case I915_FORMAT_MOD_Yf_TILED:
  12037. if (INTEL_INFO(dev)->gen < 9) {
  12038. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12039. mode_cmd->modifier[0]);
  12040. return -EINVAL;
  12041. }
  12042. case DRM_FORMAT_MOD_NONE:
  12043. case I915_FORMAT_MOD_X_TILED:
  12044. break;
  12045. default:
  12046. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12047. mode_cmd->modifier[0]);
  12048. return -EINVAL;
  12049. }
  12050. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  12051. mode_cmd->pixel_format);
  12052. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12053. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12054. mode_cmd->pitches[0], stride_alignment);
  12055. return -EINVAL;
  12056. }
  12057. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12058. mode_cmd->pixel_format);
  12059. if (mode_cmd->pitches[0] > pitch_limit) {
  12060. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12061. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12062. "tiled" : "linear",
  12063. mode_cmd->pitches[0], pitch_limit);
  12064. return -EINVAL;
  12065. }
  12066. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12067. mode_cmd->pitches[0] != obj->stride) {
  12068. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12069. mode_cmd->pitches[0], obj->stride);
  12070. return -EINVAL;
  12071. }
  12072. /* Reject formats not supported by any plane early. */
  12073. switch (mode_cmd->pixel_format) {
  12074. case DRM_FORMAT_C8:
  12075. case DRM_FORMAT_RGB565:
  12076. case DRM_FORMAT_XRGB8888:
  12077. case DRM_FORMAT_ARGB8888:
  12078. break;
  12079. case DRM_FORMAT_XRGB1555:
  12080. if (INTEL_INFO(dev)->gen > 3) {
  12081. DRM_DEBUG("unsupported pixel format: %s\n",
  12082. drm_get_format_name(mode_cmd->pixel_format));
  12083. return -EINVAL;
  12084. }
  12085. break;
  12086. case DRM_FORMAT_ABGR8888:
  12087. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  12088. DRM_DEBUG("unsupported pixel format: %s\n",
  12089. drm_get_format_name(mode_cmd->pixel_format));
  12090. return -EINVAL;
  12091. }
  12092. break;
  12093. case DRM_FORMAT_XBGR8888:
  12094. case DRM_FORMAT_XRGB2101010:
  12095. case DRM_FORMAT_XBGR2101010:
  12096. if (INTEL_INFO(dev)->gen < 4) {
  12097. DRM_DEBUG("unsupported pixel format: %s\n",
  12098. drm_get_format_name(mode_cmd->pixel_format));
  12099. return -EINVAL;
  12100. }
  12101. break;
  12102. case DRM_FORMAT_ABGR2101010:
  12103. if (!IS_VALLEYVIEW(dev)) {
  12104. DRM_DEBUG("unsupported pixel format: %s\n",
  12105. drm_get_format_name(mode_cmd->pixel_format));
  12106. return -EINVAL;
  12107. }
  12108. break;
  12109. case DRM_FORMAT_YUYV:
  12110. case DRM_FORMAT_UYVY:
  12111. case DRM_FORMAT_YVYU:
  12112. case DRM_FORMAT_VYUY:
  12113. if (INTEL_INFO(dev)->gen < 5) {
  12114. DRM_DEBUG("unsupported pixel format: %s\n",
  12115. drm_get_format_name(mode_cmd->pixel_format));
  12116. return -EINVAL;
  12117. }
  12118. break;
  12119. default:
  12120. DRM_DEBUG("unsupported pixel format: %s\n",
  12121. drm_get_format_name(mode_cmd->pixel_format));
  12122. return -EINVAL;
  12123. }
  12124. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12125. if (mode_cmd->offsets[0] != 0)
  12126. return -EINVAL;
  12127. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12128. mode_cmd->pixel_format,
  12129. mode_cmd->modifier[0]);
  12130. /* FIXME drm helper for size checks (especially planar formats)? */
  12131. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12132. return -EINVAL;
  12133. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12134. intel_fb->obj = obj;
  12135. intel_fb->obj->framebuffer_references++;
  12136. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12137. if (ret) {
  12138. DRM_ERROR("framebuffer init failed %d\n", ret);
  12139. return ret;
  12140. }
  12141. return 0;
  12142. }
  12143. static struct drm_framebuffer *
  12144. intel_user_framebuffer_create(struct drm_device *dev,
  12145. struct drm_file *filp,
  12146. struct drm_mode_fb_cmd2 *mode_cmd)
  12147. {
  12148. struct drm_i915_gem_object *obj;
  12149. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12150. mode_cmd->handles[0]));
  12151. if (&obj->base == NULL)
  12152. return ERR_PTR(-ENOENT);
  12153. return intel_framebuffer_create(dev, mode_cmd, obj);
  12154. }
  12155. #ifndef CONFIG_DRM_I915_FBDEV
  12156. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12157. {
  12158. }
  12159. #endif
  12160. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12161. .fb_create = intel_user_framebuffer_create,
  12162. .output_poll_changed = intel_fbdev_output_poll_changed,
  12163. .atomic_check = intel_atomic_check,
  12164. .atomic_commit = intel_atomic_commit,
  12165. .atomic_state_alloc = intel_atomic_state_alloc,
  12166. .atomic_state_clear = intel_atomic_state_clear,
  12167. };
  12168. /* Set up chip specific display functions */
  12169. static void intel_init_display(struct drm_device *dev)
  12170. {
  12171. struct drm_i915_private *dev_priv = dev->dev_private;
  12172. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12173. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12174. else if (IS_CHERRYVIEW(dev))
  12175. dev_priv->display.find_dpll = chv_find_best_dpll;
  12176. else if (IS_VALLEYVIEW(dev))
  12177. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12178. else if (IS_PINEVIEW(dev))
  12179. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12180. else
  12181. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12182. if (INTEL_INFO(dev)->gen >= 9) {
  12183. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12184. dev_priv->display.get_initial_plane_config =
  12185. skylake_get_initial_plane_config;
  12186. dev_priv->display.crtc_compute_clock =
  12187. haswell_crtc_compute_clock;
  12188. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12189. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12190. dev_priv->display.update_primary_plane =
  12191. skylake_update_primary_plane;
  12192. } else if (HAS_DDI(dev)) {
  12193. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12194. dev_priv->display.get_initial_plane_config =
  12195. ironlake_get_initial_plane_config;
  12196. dev_priv->display.crtc_compute_clock =
  12197. haswell_crtc_compute_clock;
  12198. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12199. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12200. dev_priv->display.update_primary_plane =
  12201. ironlake_update_primary_plane;
  12202. } else if (HAS_PCH_SPLIT(dev)) {
  12203. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12204. dev_priv->display.get_initial_plane_config =
  12205. ironlake_get_initial_plane_config;
  12206. dev_priv->display.crtc_compute_clock =
  12207. ironlake_crtc_compute_clock;
  12208. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12209. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12210. dev_priv->display.update_primary_plane =
  12211. ironlake_update_primary_plane;
  12212. } else if (IS_VALLEYVIEW(dev)) {
  12213. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12214. dev_priv->display.get_initial_plane_config =
  12215. i9xx_get_initial_plane_config;
  12216. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12217. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12218. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12219. dev_priv->display.update_primary_plane =
  12220. i9xx_update_primary_plane;
  12221. } else {
  12222. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12223. dev_priv->display.get_initial_plane_config =
  12224. i9xx_get_initial_plane_config;
  12225. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12226. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12227. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12228. dev_priv->display.update_primary_plane =
  12229. i9xx_update_primary_plane;
  12230. }
  12231. /* Returns the core display clock speed */
  12232. if (IS_SKYLAKE(dev))
  12233. dev_priv->display.get_display_clock_speed =
  12234. skylake_get_display_clock_speed;
  12235. else if (IS_BROADWELL(dev))
  12236. dev_priv->display.get_display_clock_speed =
  12237. broadwell_get_display_clock_speed;
  12238. else if (IS_HASWELL(dev))
  12239. dev_priv->display.get_display_clock_speed =
  12240. haswell_get_display_clock_speed;
  12241. else if (IS_VALLEYVIEW(dev))
  12242. dev_priv->display.get_display_clock_speed =
  12243. valleyview_get_display_clock_speed;
  12244. else if (IS_GEN5(dev))
  12245. dev_priv->display.get_display_clock_speed =
  12246. ilk_get_display_clock_speed;
  12247. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12248. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12249. dev_priv->display.get_display_clock_speed =
  12250. i945_get_display_clock_speed;
  12251. else if (IS_GM45(dev))
  12252. dev_priv->display.get_display_clock_speed =
  12253. gm45_get_display_clock_speed;
  12254. else if (IS_CRESTLINE(dev))
  12255. dev_priv->display.get_display_clock_speed =
  12256. i965gm_get_display_clock_speed;
  12257. else if (IS_PINEVIEW(dev))
  12258. dev_priv->display.get_display_clock_speed =
  12259. pnv_get_display_clock_speed;
  12260. else if (IS_G33(dev) || IS_G4X(dev))
  12261. dev_priv->display.get_display_clock_speed =
  12262. g33_get_display_clock_speed;
  12263. else if (IS_I915G(dev))
  12264. dev_priv->display.get_display_clock_speed =
  12265. i915_get_display_clock_speed;
  12266. else if (IS_I945GM(dev) || IS_845G(dev))
  12267. dev_priv->display.get_display_clock_speed =
  12268. i9xx_misc_get_display_clock_speed;
  12269. else if (IS_PINEVIEW(dev))
  12270. dev_priv->display.get_display_clock_speed =
  12271. pnv_get_display_clock_speed;
  12272. else if (IS_I915GM(dev))
  12273. dev_priv->display.get_display_clock_speed =
  12274. i915gm_get_display_clock_speed;
  12275. else if (IS_I865G(dev))
  12276. dev_priv->display.get_display_clock_speed =
  12277. i865_get_display_clock_speed;
  12278. else if (IS_I85X(dev))
  12279. dev_priv->display.get_display_clock_speed =
  12280. i85x_get_display_clock_speed;
  12281. else { /* 830 */
  12282. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12283. dev_priv->display.get_display_clock_speed =
  12284. i830_get_display_clock_speed;
  12285. }
  12286. if (IS_GEN5(dev)) {
  12287. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12288. } else if (IS_GEN6(dev)) {
  12289. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12290. } else if (IS_IVYBRIDGE(dev)) {
  12291. /* FIXME: detect B0+ stepping and use auto training */
  12292. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12293. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12294. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12295. if (IS_BROADWELL(dev))
  12296. dev_priv->display.modeset_global_resources =
  12297. broadwell_modeset_global_resources;
  12298. } else if (IS_VALLEYVIEW(dev)) {
  12299. dev_priv->display.modeset_global_resources =
  12300. valleyview_modeset_global_resources;
  12301. } else if (IS_BROXTON(dev)) {
  12302. dev_priv->display.modeset_global_resources =
  12303. broxton_modeset_global_resources;
  12304. }
  12305. switch (INTEL_INFO(dev)->gen) {
  12306. case 2:
  12307. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12308. break;
  12309. case 3:
  12310. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12311. break;
  12312. case 4:
  12313. case 5:
  12314. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12315. break;
  12316. case 6:
  12317. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12318. break;
  12319. case 7:
  12320. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12321. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12322. break;
  12323. case 9:
  12324. /* Drop through - unsupported since execlist only. */
  12325. default:
  12326. /* Default just returns -ENODEV to indicate unsupported */
  12327. dev_priv->display.queue_flip = intel_default_queue_flip;
  12328. }
  12329. intel_panel_init_backlight_funcs(dev);
  12330. mutex_init(&dev_priv->pps_mutex);
  12331. }
  12332. /*
  12333. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12334. * resume, or other times. This quirk makes sure that's the case for
  12335. * affected systems.
  12336. */
  12337. static void quirk_pipea_force(struct drm_device *dev)
  12338. {
  12339. struct drm_i915_private *dev_priv = dev->dev_private;
  12340. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12341. DRM_INFO("applying pipe a force quirk\n");
  12342. }
  12343. static void quirk_pipeb_force(struct drm_device *dev)
  12344. {
  12345. struct drm_i915_private *dev_priv = dev->dev_private;
  12346. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12347. DRM_INFO("applying pipe b force quirk\n");
  12348. }
  12349. /*
  12350. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12351. */
  12352. static void quirk_ssc_force_disable(struct drm_device *dev)
  12353. {
  12354. struct drm_i915_private *dev_priv = dev->dev_private;
  12355. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12356. DRM_INFO("applying lvds SSC disable quirk\n");
  12357. }
  12358. /*
  12359. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12360. * brightness value
  12361. */
  12362. static void quirk_invert_brightness(struct drm_device *dev)
  12363. {
  12364. struct drm_i915_private *dev_priv = dev->dev_private;
  12365. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12366. DRM_INFO("applying inverted panel brightness quirk\n");
  12367. }
  12368. /* Some VBT's incorrectly indicate no backlight is present */
  12369. static void quirk_backlight_present(struct drm_device *dev)
  12370. {
  12371. struct drm_i915_private *dev_priv = dev->dev_private;
  12372. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12373. DRM_INFO("applying backlight present quirk\n");
  12374. }
  12375. struct intel_quirk {
  12376. int device;
  12377. int subsystem_vendor;
  12378. int subsystem_device;
  12379. void (*hook)(struct drm_device *dev);
  12380. };
  12381. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12382. struct intel_dmi_quirk {
  12383. void (*hook)(struct drm_device *dev);
  12384. const struct dmi_system_id (*dmi_id_list)[];
  12385. };
  12386. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12387. {
  12388. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12389. return 1;
  12390. }
  12391. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12392. {
  12393. .dmi_id_list = &(const struct dmi_system_id[]) {
  12394. {
  12395. .callback = intel_dmi_reverse_brightness,
  12396. .ident = "NCR Corporation",
  12397. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12398. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12399. },
  12400. },
  12401. { } /* terminating entry */
  12402. },
  12403. .hook = quirk_invert_brightness,
  12404. },
  12405. };
  12406. static struct intel_quirk intel_quirks[] = {
  12407. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12408. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12409. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12410. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12411. /* 830 needs to leave pipe A & dpll A up */
  12412. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12413. /* 830 needs to leave pipe B & dpll B up */
  12414. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12415. /* Lenovo U160 cannot use SSC on LVDS */
  12416. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12417. /* Sony Vaio Y cannot use SSC on LVDS */
  12418. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12419. /* Acer Aspire 5734Z must invert backlight brightness */
  12420. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12421. /* Acer/eMachines G725 */
  12422. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12423. /* Acer/eMachines e725 */
  12424. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12425. /* Acer/Packard Bell NCL20 */
  12426. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12427. /* Acer Aspire 4736Z */
  12428. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12429. /* Acer Aspire 5336 */
  12430. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12431. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12432. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12433. /* Acer C720 Chromebook (Core i3 4005U) */
  12434. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12435. /* Apple Macbook 2,1 (Core 2 T7400) */
  12436. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12437. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12438. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12439. /* HP Chromebook 14 (Celeron 2955U) */
  12440. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12441. /* Dell Chromebook 11 */
  12442. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12443. };
  12444. static void intel_init_quirks(struct drm_device *dev)
  12445. {
  12446. struct pci_dev *d = dev->pdev;
  12447. int i;
  12448. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12449. struct intel_quirk *q = &intel_quirks[i];
  12450. if (d->device == q->device &&
  12451. (d->subsystem_vendor == q->subsystem_vendor ||
  12452. q->subsystem_vendor == PCI_ANY_ID) &&
  12453. (d->subsystem_device == q->subsystem_device ||
  12454. q->subsystem_device == PCI_ANY_ID))
  12455. q->hook(dev);
  12456. }
  12457. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12458. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12459. intel_dmi_quirks[i].hook(dev);
  12460. }
  12461. }
  12462. /* Disable the VGA plane that we never use */
  12463. static void i915_disable_vga(struct drm_device *dev)
  12464. {
  12465. struct drm_i915_private *dev_priv = dev->dev_private;
  12466. u8 sr1;
  12467. u32 vga_reg = i915_vgacntrl_reg(dev);
  12468. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12469. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12470. outb(SR01, VGA_SR_INDEX);
  12471. sr1 = inb(VGA_SR_DATA);
  12472. outb(sr1 | 1<<5, VGA_SR_DATA);
  12473. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12474. udelay(300);
  12475. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12476. POSTING_READ(vga_reg);
  12477. }
  12478. void intel_modeset_init_hw(struct drm_device *dev)
  12479. {
  12480. intel_update_cdclk(dev);
  12481. intel_prepare_ddi(dev);
  12482. intel_init_clock_gating(dev);
  12483. intel_enable_gt_powersave(dev);
  12484. }
  12485. void intel_modeset_init(struct drm_device *dev)
  12486. {
  12487. struct drm_i915_private *dev_priv = dev->dev_private;
  12488. int sprite, ret;
  12489. enum pipe pipe;
  12490. struct intel_crtc *crtc;
  12491. drm_mode_config_init(dev);
  12492. dev->mode_config.min_width = 0;
  12493. dev->mode_config.min_height = 0;
  12494. dev->mode_config.preferred_depth = 24;
  12495. dev->mode_config.prefer_shadow = 1;
  12496. dev->mode_config.allow_fb_modifiers = true;
  12497. dev->mode_config.funcs = &intel_mode_funcs;
  12498. intel_init_quirks(dev);
  12499. intel_init_pm(dev);
  12500. if (INTEL_INFO(dev)->num_pipes == 0)
  12501. return;
  12502. intel_init_display(dev);
  12503. intel_init_audio(dev);
  12504. if (IS_GEN2(dev)) {
  12505. dev->mode_config.max_width = 2048;
  12506. dev->mode_config.max_height = 2048;
  12507. } else if (IS_GEN3(dev)) {
  12508. dev->mode_config.max_width = 4096;
  12509. dev->mode_config.max_height = 4096;
  12510. } else {
  12511. dev->mode_config.max_width = 8192;
  12512. dev->mode_config.max_height = 8192;
  12513. }
  12514. if (IS_845G(dev) || IS_I865G(dev)) {
  12515. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12516. dev->mode_config.cursor_height = 1023;
  12517. } else if (IS_GEN2(dev)) {
  12518. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12519. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12520. } else {
  12521. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12522. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12523. }
  12524. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12525. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12526. INTEL_INFO(dev)->num_pipes,
  12527. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12528. for_each_pipe(dev_priv, pipe) {
  12529. intel_crtc_init(dev, pipe);
  12530. for_each_sprite(dev_priv, pipe, sprite) {
  12531. ret = intel_plane_init(dev, pipe, sprite);
  12532. if (ret)
  12533. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12534. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12535. }
  12536. }
  12537. intel_init_dpio(dev);
  12538. intel_shared_dpll_init(dev);
  12539. /* Just disable it once at startup */
  12540. i915_disable_vga(dev);
  12541. intel_setup_outputs(dev);
  12542. /* Just in case the BIOS is doing something questionable. */
  12543. intel_fbc_disable(dev);
  12544. drm_modeset_lock_all(dev);
  12545. intel_modeset_setup_hw_state(dev, false);
  12546. drm_modeset_unlock_all(dev);
  12547. for_each_intel_crtc(dev, crtc) {
  12548. if (!crtc->active)
  12549. continue;
  12550. /*
  12551. * Note that reserving the BIOS fb up front prevents us
  12552. * from stuffing other stolen allocations like the ring
  12553. * on top. This prevents some ugliness at boot time, and
  12554. * can even allow for smooth boot transitions if the BIOS
  12555. * fb is large enough for the active pipe configuration.
  12556. */
  12557. if (dev_priv->display.get_initial_plane_config) {
  12558. dev_priv->display.get_initial_plane_config(crtc,
  12559. &crtc->plane_config);
  12560. /*
  12561. * If the fb is shared between multiple heads, we'll
  12562. * just get the first one.
  12563. */
  12564. intel_find_initial_plane_obj(crtc, &crtc->plane_config);
  12565. }
  12566. }
  12567. }
  12568. static void intel_enable_pipe_a(struct drm_device *dev)
  12569. {
  12570. struct intel_connector *connector;
  12571. struct drm_connector *crt = NULL;
  12572. struct intel_load_detect_pipe load_detect_temp;
  12573. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12574. /* We can't just switch on the pipe A, we need to set things up with a
  12575. * proper mode and output configuration. As a gross hack, enable pipe A
  12576. * by enabling the load detect pipe once. */
  12577. for_each_intel_connector(dev, connector) {
  12578. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12579. crt = &connector->base;
  12580. break;
  12581. }
  12582. }
  12583. if (!crt)
  12584. return;
  12585. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12586. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12587. }
  12588. static bool
  12589. intel_check_plane_mapping(struct intel_crtc *crtc)
  12590. {
  12591. struct drm_device *dev = crtc->base.dev;
  12592. struct drm_i915_private *dev_priv = dev->dev_private;
  12593. u32 reg, val;
  12594. if (INTEL_INFO(dev)->num_pipes == 1)
  12595. return true;
  12596. reg = DSPCNTR(!crtc->plane);
  12597. val = I915_READ(reg);
  12598. if ((val & DISPLAY_PLANE_ENABLE) &&
  12599. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12600. return false;
  12601. return true;
  12602. }
  12603. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12604. {
  12605. struct drm_device *dev = crtc->base.dev;
  12606. struct drm_i915_private *dev_priv = dev->dev_private;
  12607. u32 reg;
  12608. /* Clear any frame start delays used for debugging left by the BIOS */
  12609. reg = PIPECONF(crtc->config->cpu_transcoder);
  12610. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12611. /* restore vblank interrupts to correct state */
  12612. drm_crtc_vblank_reset(&crtc->base);
  12613. if (crtc->active) {
  12614. update_scanline_offset(crtc);
  12615. drm_crtc_vblank_on(&crtc->base);
  12616. }
  12617. /* We need to sanitize the plane -> pipe mapping first because this will
  12618. * disable the crtc (and hence change the state) if it is wrong. Note
  12619. * that gen4+ has a fixed plane -> pipe mapping. */
  12620. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12621. struct intel_connector *connector;
  12622. bool plane;
  12623. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12624. crtc->base.base.id);
  12625. /* Pipe has the wrong plane attached and the plane is active.
  12626. * Temporarily change the plane mapping and disable everything
  12627. * ... */
  12628. plane = crtc->plane;
  12629. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12630. crtc->base.primary->crtc = &crtc->base;
  12631. crtc->plane = !plane;
  12632. intel_crtc_control(&crtc->base, false);
  12633. crtc->plane = plane;
  12634. /* ... and break all links. */
  12635. for_each_intel_connector(dev, connector) {
  12636. if (connector->encoder->base.crtc != &crtc->base)
  12637. continue;
  12638. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12639. connector->base.encoder = NULL;
  12640. }
  12641. /* multiple connectors may have the same encoder:
  12642. * handle them and break crtc link separately */
  12643. for_each_intel_connector(dev, connector)
  12644. if (connector->encoder->base.crtc == &crtc->base) {
  12645. connector->encoder->base.crtc = NULL;
  12646. connector->encoder->connectors_active = false;
  12647. }
  12648. WARN_ON(crtc->active);
  12649. crtc->base.state->enable = false;
  12650. crtc->base.state->active = false;
  12651. crtc->base.enabled = false;
  12652. }
  12653. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12654. crtc->pipe == PIPE_A && !crtc->active) {
  12655. /* BIOS forgot to enable pipe A, this mostly happens after
  12656. * resume. Force-enable the pipe to fix this, the update_dpms
  12657. * call below we restore the pipe to the right state, but leave
  12658. * the required bits on. */
  12659. intel_enable_pipe_a(dev);
  12660. }
  12661. /* Adjust the state of the output pipe according to whether we
  12662. * have active connectors/encoders. */
  12663. intel_crtc_update_dpms(&crtc->base);
  12664. if (crtc->active != crtc->base.state->active) {
  12665. struct intel_encoder *encoder;
  12666. /* This can happen either due to bugs in the get_hw_state
  12667. * functions or because the pipe is force-enabled due to the
  12668. * pipe A quirk. */
  12669. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12670. crtc->base.base.id,
  12671. crtc->base.state->enable ? "enabled" : "disabled",
  12672. crtc->active ? "enabled" : "disabled");
  12673. crtc->base.state->enable = crtc->active;
  12674. crtc->base.state->active = crtc->active;
  12675. crtc->base.enabled = crtc->active;
  12676. /* Because we only establish the connector -> encoder ->
  12677. * crtc links if something is active, this means the
  12678. * crtc is now deactivated. Break the links. connector
  12679. * -> encoder links are only establish when things are
  12680. * actually up, hence no need to break them. */
  12681. WARN_ON(crtc->active);
  12682. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12683. WARN_ON(encoder->connectors_active);
  12684. encoder->base.crtc = NULL;
  12685. }
  12686. }
  12687. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12688. /*
  12689. * We start out with underrun reporting disabled to avoid races.
  12690. * For correct bookkeeping mark this on active crtcs.
  12691. *
  12692. * Also on gmch platforms we dont have any hardware bits to
  12693. * disable the underrun reporting. Which means we need to start
  12694. * out with underrun reporting disabled also on inactive pipes,
  12695. * since otherwise we'll complain about the garbage we read when
  12696. * e.g. coming up after runtime pm.
  12697. *
  12698. * No protection against concurrent access is required - at
  12699. * worst a fifo underrun happens which also sets this to false.
  12700. */
  12701. crtc->cpu_fifo_underrun_disabled = true;
  12702. crtc->pch_fifo_underrun_disabled = true;
  12703. }
  12704. }
  12705. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12706. {
  12707. struct intel_connector *connector;
  12708. struct drm_device *dev = encoder->base.dev;
  12709. /* We need to check both for a crtc link (meaning that the
  12710. * encoder is active and trying to read from a pipe) and the
  12711. * pipe itself being active. */
  12712. bool has_active_crtc = encoder->base.crtc &&
  12713. to_intel_crtc(encoder->base.crtc)->active;
  12714. if (encoder->connectors_active && !has_active_crtc) {
  12715. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12716. encoder->base.base.id,
  12717. encoder->base.name);
  12718. /* Connector is active, but has no active pipe. This is
  12719. * fallout from our resume register restoring. Disable
  12720. * the encoder manually again. */
  12721. if (encoder->base.crtc) {
  12722. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12723. encoder->base.base.id,
  12724. encoder->base.name);
  12725. encoder->disable(encoder);
  12726. if (encoder->post_disable)
  12727. encoder->post_disable(encoder);
  12728. }
  12729. encoder->base.crtc = NULL;
  12730. encoder->connectors_active = false;
  12731. /* Inconsistent output/port/pipe state happens presumably due to
  12732. * a bug in one of the get_hw_state functions. Or someplace else
  12733. * in our code, like the register restore mess on resume. Clamp
  12734. * things to off as a safer default. */
  12735. for_each_intel_connector(dev, connector) {
  12736. if (connector->encoder != encoder)
  12737. continue;
  12738. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12739. connector->base.encoder = NULL;
  12740. }
  12741. }
  12742. /* Enabled encoders without active connectors will be fixed in
  12743. * the crtc fixup. */
  12744. }
  12745. void i915_redisable_vga_power_on(struct drm_device *dev)
  12746. {
  12747. struct drm_i915_private *dev_priv = dev->dev_private;
  12748. u32 vga_reg = i915_vgacntrl_reg(dev);
  12749. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12750. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12751. i915_disable_vga(dev);
  12752. }
  12753. }
  12754. void i915_redisable_vga(struct drm_device *dev)
  12755. {
  12756. struct drm_i915_private *dev_priv = dev->dev_private;
  12757. /* This function can be called both from intel_modeset_setup_hw_state or
  12758. * at a very early point in our resume sequence, where the power well
  12759. * structures are not yet restored. Since this function is at a very
  12760. * paranoid "someone might have enabled VGA while we were not looking"
  12761. * level, just check if the power well is enabled instead of trying to
  12762. * follow the "don't touch the power well if we don't need it" policy
  12763. * the rest of the driver uses. */
  12764. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12765. return;
  12766. i915_redisable_vga_power_on(dev);
  12767. }
  12768. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12769. {
  12770. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12771. if (!crtc->base.enabled)
  12772. return false;
  12773. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  12774. }
  12775. static int readout_hw_crtc_state(struct drm_atomic_state *state,
  12776. struct intel_crtc *crtc)
  12777. {
  12778. struct drm_i915_private *dev_priv = to_i915(state->dev);
  12779. struct intel_crtc_state *crtc_state;
  12780. struct drm_plane *primary = crtc->base.primary;
  12781. struct drm_plane_state *drm_plane_state;
  12782. struct intel_plane_state *plane_state;
  12783. int ret;
  12784. crtc_state = intel_atomic_get_crtc_state(state, crtc);
  12785. if (IS_ERR(crtc_state))
  12786. return PTR_ERR(crtc_state);
  12787. ret = drm_atomic_add_affected_planes(state, &crtc->base);
  12788. if (ret)
  12789. return ret;
  12790. memset(crtc_state, 0, sizeof(*crtc_state));
  12791. crtc_state->base.crtc = &crtc->base;
  12792. crtc_state->base.state = state;
  12793. crtc_state->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  12794. crtc_state->base.enable = crtc_state->base.active =
  12795. crtc->base.enabled = dev_priv->display.get_pipe_config(crtc, crtc_state);
  12796. /* update transitional state */
  12797. crtc->active = crtc_state->base.active;
  12798. crtc->config = crtc_state;
  12799. drm_plane_state = drm_atomic_get_plane_state(state, primary);
  12800. if (IS_ERR(drm_plane_state))
  12801. return PTR_ERR(drm_plane_state);
  12802. plane_state = to_intel_plane_state(drm_plane_state);
  12803. plane_state->visible = primary_get_hw_state(crtc);
  12804. if (plane_state->visible) {
  12805. primary->crtc = &crtc->base;
  12806. crtc_state->base.plane_mask |= 1 << drm_plane_index(primary);
  12807. } else
  12808. crtc_state->base.plane_mask &= ~(1 << drm_plane_index(primary));
  12809. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12810. crtc->base.base.id,
  12811. crtc_state->base.active ? "enabled" : "disabled");
  12812. return 0;
  12813. }
  12814. static int readout_hw_pll_state(struct drm_atomic_state *state)
  12815. {
  12816. struct drm_i915_private *dev_priv = to_i915(state->dev);
  12817. struct intel_shared_dpll_config *shared_dpll;
  12818. struct intel_crtc *crtc;
  12819. struct intel_crtc_state *crtc_state;
  12820. int i;
  12821. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  12822. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12823. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12824. pll->on = pll->get_hw_state(dev_priv, pll,
  12825. &shared_dpll[i].hw_state);
  12826. pll->active = 0;
  12827. shared_dpll[i].crtc_mask = 0;
  12828. for_each_intel_crtc(state->dev, crtc) {
  12829. crtc_state = intel_atomic_get_crtc_state(state, crtc);
  12830. if (IS_ERR(crtc_state))
  12831. return PTR_ERR(crtc_state);
  12832. if (crtc_state->base.active &&
  12833. crtc_state->shared_dpll == i) {
  12834. pll->active++;
  12835. shared_dpll[i].crtc_mask |=
  12836. 1 << crtc->pipe;
  12837. }
  12838. }
  12839. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12840. pll->name, shared_dpll[i].crtc_mask,
  12841. pll->on);
  12842. if (shared_dpll[i].crtc_mask)
  12843. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12844. }
  12845. return 0;
  12846. }
  12847. static struct drm_connector_state *
  12848. get_connector_state_for_encoder(struct drm_atomic_state *state,
  12849. struct intel_encoder *encoder)
  12850. {
  12851. struct drm_connector *connector;
  12852. struct drm_connector_state *connector_state;
  12853. int i;
  12854. for_each_connector_in_state(state, connector, connector_state, i)
  12855. if (connector_state->best_encoder == &encoder->base)
  12856. return connector_state;
  12857. return NULL;
  12858. }
  12859. static int readout_hw_connector_encoder_state(struct drm_atomic_state *state)
  12860. {
  12861. struct drm_device *dev = state->dev;
  12862. struct drm_i915_private *dev_priv = to_i915(state->dev);
  12863. struct intel_crtc *crtc;
  12864. struct drm_crtc_state *drm_crtc_state;
  12865. struct intel_crtc_state *crtc_state;
  12866. struct intel_encoder *encoder;
  12867. struct intel_connector *connector;
  12868. struct drm_connector_state *connector_state;
  12869. enum pipe pipe;
  12870. for_each_intel_connector(dev, connector) {
  12871. connector_state =
  12872. drm_atomic_get_connector_state(state, &connector->base);
  12873. if (IS_ERR(connector_state))
  12874. return PTR_ERR(connector_state);
  12875. if (connector->get_hw_state(connector)) {
  12876. connector->base.dpms = DRM_MODE_DPMS_ON;
  12877. connector->base.encoder = &connector->encoder->base;
  12878. } else {
  12879. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12880. connector->base.encoder = NULL;
  12881. }
  12882. /* We'll update the crtc field when reading encoder state */
  12883. connector_state->crtc = NULL;
  12884. connector_state->best_encoder = connector->base.encoder;
  12885. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12886. connector->base.base.id,
  12887. connector->base.name,
  12888. connector->base.encoder ? "enabled" : "disabled");
  12889. }
  12890. for_each_intel_encoder(dev, encoder) {
  12891. pipe = 0;
  12892. connector_state =
  12893. get_connector_state_for_encoder(state, encoder);
  12894. encoder->connectors_active = !!connector_state;
  12895. if (encoder->get_hw_state(encoder, &pipe)) {
  12896. encoder->base.crtc =
  12897. dev_priv->pipe_to_crtc_mapping[pipe];
  12898. crtc = to_intel_crtc(encoder->base.crtc);
  12899. drm_crtc_state =
  12900. state->crtc_states[drm_crtc_index(&crtc->base)];
  12901. crtc_state = to_intel_crtc_state(drm_crtc_state);
  12902. encoder->get_config(encoder, crtc_state);
  12903. if (connector_state)
  12904. connector_state->crtc = &crtc->base;
  12905. } else {
  12906. encoder->base.crtc = NULL;
  12907. }
  12908. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12909. encoder->base.base.id,
  12910. encoder->base.name,
  12911. encoder->base.crtc ? "enabled" : "disabled",
  12912. pipe_name(pipe));
  12913. }
  12914. return 0;
  12915. }
  12916. static struct drm_atomic_state *
  12917. intel_modeset_readout_hw_state(struct drm_device *dev)
  12918. {
  12919. struct intel_crtc *crtc;
  12920. int ret = 0;
  12921. struct drm_atomic_state *state;
  12922. state = drm_atomic_state_alloc(dev);
  12923. if (!state)
  12924. return ERR_PTR(-ENOMEM);
  12925. state->acquire_ctx = dev->mode_config.acquire_ctx;
  12926. for_each_intel_crtc(dev, crtc) {
  12927. ret = readout_hw_crtc_state(state, crtc);
  12928. if (ret)
  12929. goto err_free;
  12930. }
  12931. ret = readout_hw_pll_state(state);
  12932. if (ret)
  12933. goto err_free;
  12934. ret = readout_hw_connector_encoder_state(state);
  12935. if (ret)
  12936. goto err_free;
  12937. return state;
  12938. err_free:
  12939. drm_atomic_state_free(state);
  12940. return ERR_PTR(ret);
  12941. }
  12942. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  12943. * and i915 state tracking structures. */
  12944. void intel_modeset_setup_hw_state(struct drm_device *dev,
  12945. bool force_restore)
  12946. {
  12947. struct drm_i915_private *dev_priv = dev->dev_private;
  12948. struct drm_crtc *crtc;
  12949. struct drm_crtc_state *crtc_state;
  12950. struct intel_encoder *encoder;
  12951. struct drm_atomic_state *state;
  12952. struct intel_shared_dpll_config shared_dplls[I915_NUM_PLLS];
  12953. int i;
  12954. state = intel_modeset_readout_hw_state(dev);
  12955. if (IS_ERR(state)) {
  12956. DRM_ERROR("Failed to read out hw state\n");
  12957. return;
  12958. }
  12959. drm_atomic_helper_swap_state(dev, state);
  12960. /* swap sw/hw dpll state */
  12961. intel_atomic_duplicate_dpll_state(dev_priv, shared_dplls);
  12962. intel_shared_dpll_commit(state);
  12963. memcpy(to_intel_atomic_state(state)->shared_dpll,
  12964. shared_dplls, sizeof(*shared_dplls) * dev_priv->num_shared_dpll);
  12965. /* HW state is read out, now we need to sanitize this mess. */
  12966. for_each_intel_encoder(dev, encoder) {
  12967. intel_sanitize_encoder(encoder);
  12968. }
  12969. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  12970. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12971. /* prevent unnneeded restores with force_restore */
  12972. crtc_state->active_changed =
  12973. crtc_state->mode_changed =
  12974. crtc_state->planes_changed = false;
  12975. if (crtc->enabled) {
  12976. intel_mode_from_pipe_config(&crtc->state->mode,
  12977. to_intel_crtc_state(crtc->state));
  12978. drm_mode_copy(&crtc->mode, &crtc->state->mode);
  12979. drm_mode_copy(&crtc->hwmode,
  12980. &crtc->state->adjusted_mode);
  12981. }
  12982. intel_sanitize_crtc(intel_crtc);
  12983. /*
  12984. * sanitize_crtc may have forced an update of crtc->state,
  12985. * so reload in intel_dump_pipe_config
  12986. */
  12987. intel_dump_pipe_config(intel_crtc,
  12988. to_intel_crtc_state(crtc->state),
  12989. "[setup_hw_state]");
  12990. }
  12991. intel_modeset_update_connector_atomic_state(dev);
  12992. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12993. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12994. if (!pll->on || pll->active)
  12995. continue;
  12996. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12997. pll->disable(dev_priv, pll);
  12998. pll->on = false;
  12999. }
  13000. if (IS_GEN9(dev))
  13001. skl_wm_get_hw_state(dev);
  13002. else if (HAS_PCH_SPLIT(dev))
  13003. ilk_wm_get_hw_state(dev);
  13004. if (force_restore) {
  13005. int ret;
  13006. i915_redisable_vga(dev);
  13007. ret = intel_set_mode(state);
  13008. if (ret) {
  13009. DRM_ERROR("Failed to restore previous mode\n");
  13010. drm_atomic_state_free(state);
  13011. }
  13012. } else {
  13013. drm_atomic_state_free(state);
  13014. }
  13015. intel_modeset_check_state(dev);
  13016. }
  13017. void intel_modeset_gem_init(struct drm_device *dev)
  13018. {
  13019. struct drm_i915_private *dev_priv = dev->dev_private;
  13020. struct drm_crtc *c;
  13021. struct drm_i915_gem_object *obj;
  13022. int ret;
  13023. mutex_lock(&dev->struct_mutex);
  13024. intel_init_gt_powersave(dev);
  13025. mutex_unlock(&dev->struct_mutex);
  13026. /*
  13027. * There may be no VBT; and if the BIOS enabled SSC we can
  13028. * just keep using it to avoid unnecessary flicker. Whereas if the
  13029. * BIOS isn't using it, don't assume it will work even if the VBT
  13030. * indicates as much.
  13031. */
  13032. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  13033. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13034. DREF_SSC1_ENABLE);
  13035. intel_modeset_init_hw(dev);
  13036. intel_setup_overlay(dev);
  13037. /*
  13038. * Make sure any fbs we allocated at startup are properly
  13039. * pinned & fenced. When we do the allocation it's too early
  13040. * for this.
  13041. */
  13042. for_each_crtc(dev, c) {
  13043. obj = intel_fb_obj(c->primary->fb);
  13044. if (obj == NULL)
  13045. continue;
  13046. mutex_lock(&dev->struct_mutex);
  13047. ret = intel_pin_and_fence_fb_obj(c->primary,
  13048. c->primary->fb,
  13049. c->primary->state,
  13050. NULL);
  13051. mutex_unlock(&dev->struct_mutex);
  13052. if (ret) {
  13053. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13054. to_intel_crtc(c)->pipe);
  13055. drm_framebuffer_unreference(c->primary->fb);
  13056. c->primary->fb = NULL;
  13057. c->primary->crtc = c->primary->state->crtc = NULL;
  13058. update_state_fb(c->primary);
  13059. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13060. }
  13061. }
  13062. intel_backlight_register(dev);
  13063. }
  13064. void intel_connector_unregister(struct intel_connector *intel_connector)
  13065. {
  13066. struct drm_connector *connector = &intel_connector->base;
  13067. intel_panel_destroy_backlight(connector);
  13068. drm_connector_unregister(connector);
  13069. }
  13070. void intel_modeset_cleanup(struct drm_device *dev)
  13071. {
  13072. struct drm_i915_private *dev_priv = dev->dev_private;
  13073. struct drm_connector *connector;
  13074. intel_disable_gt_powersave(dev);
  13075. intel_backlight_unregister(dev);
  13076. /*
  13077. * Interrupts and polling as the first thing to avoid creating havoc.
  13078. * Too much stuff here (turning of connectors, ...) would
  13079. * experience fancy races otherwise.
  13080. */
  13081. intel_irq_uninstall(dev_priv);
  13082. /*
  13083. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13084. * poll handlers. Hence disable polling after hpd handling is shut down.
  13085. */
  13086. drm_kms_helper_poll_fini(dev);
  13087. mutex_lock(&dev->struct_mutex);
  13088. intel_unregister_dsm_handler();
  13089. intel_fbc_disable(dev);
  13090. mutex_unlock(&dev->struct_mutex);
  13091. /* flush any delayed tasks or pending work */
  13092. flush_scheduled_work();
  13093. /* destroy the backlight and sysfs files before encoders/connectors */
  13094. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  13095. struct intel_connector *intel_connector;
  13096. intel_connector = to_intel_connector(connector);
  13097. intel_connector->unregister(intel_connector);
  13098. }
  13099. drm_mode_config_cleanup(dev);
  13100. intel_cleanup_overlay(dev);
  13101. mutex_lock(&dev->struct_mutex);
  13102. intel_cleanup_gt_powersave(dev);
  13103. mutex_unlock(&dev->struct_mutex);
  13104. }
  13105. /*
  13106. * Return which encoder is currently attached for connector.
  13107. */
  13108. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13109. {
  13110. return &intel_attached_encoder(connector)->base;
  13111. }
  13112. void intel_connector_attach_encoder(struct intel_connector *connector,
  13113. struct intel_encoder *encoder)
  13114. {
  13115. connector->encoder = encoder;
  13116. drm_mode_connector_attach_encoder(&connector->base,
  13117. &encoder->base);
  13118. }
  13119. /*
  13120. * set vga decode state - true == enable VGA decode
  13121. */
  13122. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13123. {
  13124. struct drm_i915_private *dev_priv = dev->dev_private;
  13125. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13126. u16 gmch_ctrl;
  13127. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13128. DRM_ERROR("failed to read control word\n");
  13129. return -EIO;
  13130. }
  13131. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13132. return 0;
  13133. if (state)
  13134. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13135. else
  13136. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13137. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13138. DRM_ERROR("failed to write control word\n");
  13139. return -EIO;
  13140. }
  13141. return 0;
  13142. }
  13143. struct intel_display_error_state {
  13144. u32 power_well_driver;
  13145. int num_transcoders;
  13146. struct intel_cursor_error_state {
  13147. u32 control;
  13148. u32 position;
  13149. u32 base;
  13150. u32 size;
  13151. } cursor[I915_MAX_PIPES];
  13152. struct intel_pipe_error_state {
  13153. bool power_domain_on;
  13154. u32 source;
  13155. u32 stat;
  13156. } pipe[I915_MAX_PIPES];
  13157. struct intel_plane_error_state {
  13158. u32 control;
  13159. u32 stride;
  13160. u32 size;
  13161. u32 pos;
  13162. u32 addr;
  13163. u32 surface;
  13164. u32 tile_offset;
  13165. } plane[I915_MAX_PIPES];
  13166. struct intel_transcoder_error_state {
  13167. bool power_domain_on;
  13168. enum transcoder cpu_transcoder;
  13169. u32 conf;
  13170. u32 htotal;
  13171. u32 hblank;
  13172. u32 hsync;
  13173. u32 vtotal;
  13174. u32 vblank;
  13175. u32 vsync;
  13176. } transcoder[4];
  13177. };
  13178. struct intel_display_error_state *
  13179. intel_display_capture_error_state(struct drm_device *dev)
  13180. {
  13181. struct drm_i915_private *dev_priv = dev->dev_private;
  13182. struct intel_display_error_state *error;
  13183. int transcoders[] = {
  13184. TRANSCODER_A,
  13185. TRANSCODER_B,
  13186. TRANSCODER_C,
  13187. TRANSCODER_EDP,
  13188. };
  13189. int i;
  13190. if (INTEL_INFO(dev)->num_pipes == 0)
  13191. return NULL;
  13192. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13193. if (error == NULL)
  13194. return NULL;
  13195. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13196. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13197. for_each_pipe(dev_priv, i) {
  13198. error->pipe[i].power_domain_on =
  13199. __intel_display_power_is_enabled(dev_priv,
  13200. POWER_DOMAIN_PIPE(i));
  13201. if (!error->pipe[i].power_domain_on)
  13202. continue;
  13203. error->cursor[i].control = I915_READ(CURCNTR(i));
  13204. error->cursor[i].position = I915_READ(CURPOS(i));
  13205. error->cursor[i].base = I915_READ(CURBASE(i));
  13206. error->plane[i].control = I915_READ(DSPCNTR(i));
  13207. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13208. if (INTEL_INFO(dev)->gen <= 3) {
  13209. error->plane[i].size = I915_READ(DSPSIZE(i));
  13210. error->plane[i].pos = I915_READ(DSPPOS(i));
  13211. }
  13212. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13213. error->plane[i].addr = I915_READ(DSPADDR(i));
  13214. if (INTEL_INFO(dev)->gen >= 4) {
  13215. error->plane[i].surface = I915_READ(DSPSURF(i));
  13216. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13217. }
  13218. error->pipe[i].source = I915_READ(PIPESRC(i));
  13219. if (HAS_GMCH_DISPLAY(dev))
  13220. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13221. }
  13222. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13223. if (HAS_DDI(dev_priv->dev))
  13224. error->num_transcoders++; /* Account for eDP. */
  13225. for (i = 0; i < error->num_transcoders; i++) {
  13226. enum transcoder cpu_transcoder = transcoders[i];
  13227. error->transcoder[i].power_domain_on =
  13228. __intel_display_power_is_enabled(dev_priv,
  13229. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13230. if (!error->transcoder[i].power_domain_on)
  13231. continue;
  13232. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13233. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13234. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13235. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13236. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13237. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13238. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13239. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13240. }
  13241. return error;
  13242. }
  13243. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13244. void
  13245. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13246. struct drm_device *dev,
  13247. struct intel_display_error_state *error)
  13248. {
  13249. struct drm_i915_private *dev_priv = dev->dev_private;
  13250. int i;
  13251. if (!error)
  13252. return;
  13253. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13254. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13255. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13256. error->power_well_driver);
  13257. for_each_pipe(dev_priv, i) {
  13258. err_printf(m, "Pipe [%d]:\n", i);
  13259. err_printf(m, " Power: %s\n",
  13260. error->pipe[i].power_domain_on ? "on" : "off");
  13261. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13262. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13263. err_printf(m, "Plane [%d]:\n", i);
  13264. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13265. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13266. if (INTEL_INFO(dev)->gen <= 3) {
  13267. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13268. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13269. }
  13270. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13271. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13272. if (INTEL_INFO(dev)->gen >= 4) {
  13273. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13274. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13275. }
  13276. err_printf(m, "Cursor [%d]:\n", i);
  13277. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13278. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13279. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13280. }
  13281. for (i = 0; i < error->num_transcoders; i++) {
  13282. err_printf(m, "CPU transcoder: %c\n",
  13283. transcoder_name(error->transcoder[i].cpu_transcoder));
  13284. err_printf(m, " Power: %s\n",
  13285. error->transcoder[i].power_domain_on ? "on" : "off");
  13286. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13287. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13288. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13289. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13290. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13291. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13292. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13293. }
  13294. }
  13295. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13296. {
  13297. struct intel_crtc *crtc;
  13298. for_each_intel_crtc(dev, crtc) {
  13299. struct intel_unpin_work *work;
  13300. spin_lock_irq(&dev->event_lock);
  13301. work = crtc->unpin_work;
  13302. if (work && work->event &&
  13303. work->event->base.file_priv == file) {
  13304. kfree(work->event);
  13305. work->event = NULL;
  13306. }
  13307. spin_unlock_irq(&dev->event_lock);
  13308. }
  13309. }