intel_display.c 454 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120
  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 "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "i915_gem_clflush.h"
  40. #include "intel_dsi.h"
  41. #include "i915_trace.h"
  42. #include <drm/drm_atomic.h>
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_dp_helper.h>
  45. #include <drm/drm_crtc_helper.h>
  46. #include <drm/drm_plane_helper.h>
  47. #include <drm/drm_rect.h>
  48. #include <linux/dma_remapping.h>
  49. #include <linux/reservation.h>
  50. /* Primary plane formats for gen <= 3 */
  51. static const uint32_t i8xx_primary_formats[] = {
  52. DRM_FORMAT_C8,
  53. DRM_FORMAT_RGB565,
  54. DRM_FORMAT_XRGB1555,
  55. DRM_FORMAT_XRGB8888,
  56. };
  57. /* Primary plane formats for gen >= 4 */
  58. static const uint32_t i965_primary_formats[] = {
  59. DRM_FORMAT_C8,
  60. DRM_FORMAT_RGB565,
  61. DRM_FORMAT_XRGB8888,
  62. DRM_FORMAT_XBGR8888,
  63. DRM_FORMAT_XRGB2101010,
  64. DRM_FORMAT_XBGR2101010,
  65. };
  66. static const uint64_t i9xx_format_modifiers[] = {
  67. I915_FORMAT_MOD_X_TILED,
  68. DRM_FORMAT_MOD_LINEAR,
  69. DRM_FORMAT_MOD_INVALID
  70. };
  71. static const uint32_t skl_primary_formats[] = {
  72. DRM_FORMAT_C8,
  73. DRM_FORMAT_RGB565,
  74. DRM_FORMAT_XRGB8888,
  75. DRM_FORMAT_XBGR8888,
  76. DRM_FORMAT_ARGB8888,
  77. DRM_FORMAT_ABGR8888,
  78. DRM_FORMAT_XRGB2101010,
  79. DRM_FORMAT_XBGR2101010,
  80. DRM_FORMAT_YUYV,
  81. DRM_FORMAT_YVYU,
  82. DRM_FORMAT_UYVY,
  83. DRM_FORMAT_VYUY,
  84. };
  85. static const uint32_t skl_pri_planar_formats[] = {
  86. DRM_FORMAT_C8,
  87. DRM_FORMAT_RGB565,
  88. DRM_FORMAT_XRGB8888,
  89. DRM_FORMAT_XBGR8888,
  90. DRM_FORMAT_ARGB8888,
  91. DRM_FORMAT_ABGR8888,
  92. DRM_FORMAT_XRGB2101010,
  93. DRM_FORMAT_XBGR2101010,
  94. DRM_FORMAT_YUYV,
  95. DRM_FORMAT_YVYU,
  96. DRM_FORMAT_UYVY,
  97. DRM_FORMAT_VYUY,
  98. DRM_FORMAT_NV12,
  99. };
  100. static const uint64_t skl_format_modifiers_noccs[] = {
  101. I915_FORMAT_MOD_Yf_TILED,
  102. I915_FORMAT_MOD_Y_TILED,
  103. I915_FORMAT_MOD_X_TILED,
  104. DRM_FORMAT_MOD_LINEAR,
  105. DRM_FORMAT_MOD_INVALID
  106. };
  107. static const uint64_t skl_format_modifiers_ccs[] = {
  108. I915_FORMAT_MOD_Yf_TILED_CCS,
  109. I915_FORMAT_MOD_Y_TILED_CCS,
  110. I915_FORMAT_MOD_Yf_TILED,
  111. I915_FORMAT_MOD_Y_TILED,
  112. I915_FORMAT_MOD_X_TILED,
  113. DRM_FORMAT_MOD_LINEAR,
  114. DRM_FORMAT_MOD_INVALID
  115. };
  116. /* Cursor formats */
  117. static const uint32_t intel_cursor_formats[] = {
  118. DRM_FORMAT_ARGB8888,
  119. };
  120. static const uint64_t cursor_format_modifiers[] = {
  121. DRM_FORMAT_MOD_LINEAR,
  122. DRM_FORMAT_MOD_INVALID
  123. };
  124. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  125. struct intel_crtc_state *pipe_config);
  126. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  127. struct intel_crtc_state *pipe_config);
  128. static int intel_framebuffer_init(struct intel_framebuffer *ifb,
  129. struct drm_i915_gem_object *obj,
  130. struct drm_mode_fb_cmd2 *mode_cmd);
  131. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  132. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  133. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  134. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  135. struct intel_link_m_n *m_n,
  136. struct intel_link_m_n *m2_n2);
  137. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  138. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  139. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  140. static void vlv_prepare_pll(struct intel_crtc *crtc,
  141. const struct intel_crtc_state *pipe_config);
  142. static void chv_prepare_pll(struct intel_crtc *crtc,
  143. const struct intel_crtc_state *pipe_config);
  144. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  145. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  146. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  147. struct intel_crtc_state *crtc_state);
  148. static void skylake_pfit_enable(struct intel_crtc *crtc);
  149. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  150. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  151. static void intel_modeset_setup_hw_state(struct drm_device *dev,
  152. struct drm_modeset_acquire_ctx *ctx);
  153. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  154. struct intel_limit {
  155. struct {
  156. int min, max;
  157. } dot, vco, n, m, m1, m2, p, p1;
  158. struct {
  159. int dot_limit;
  160. int p2_slow, p2_fast;
  161. } p2;
  162. };
  163. /* returns HPLL frequency in kHz */
  164. int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
  165. {
  166. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  167. /* Obtain SKU information */
  168. mutex_lock(&dev_priv->sb_lock);
  169. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  170. CCK_FUSE_HPLL_FREQ_MASK;
  171. mutex_unlock(&dev_priv->sb_lock);
  172. return vco_freq[hpll_freq] * 1000;
  173. }
  174. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  175. const char *name, u32 reg, int ref_freq)
  176. {
  177. u32 val;
  178. int divider;
  179. mutex_lock(&dev_priv->sb_lock);
  180. val = vlv_cck_read(dev_priv, reg);
  181. mutex_unlock(&dev_priv->sb_lock);
  182. divider = val & CCK_FREQUENCY_VALUES;
  183. WARN((val & CCK_FREQUENCY_STATUS) !=
  184. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  185. "%s change in progress\n", name);
  186. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  187. }
  188. int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  189. const char *name, u32 reg)
  190. {
  191. if (dev_priv->hpll_freq == 0)
  192. dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
  193. return vlv_get_cck_clock(dev_priv, name, reg,
  194. dev_priv->hpll_freq);
  195. }
  196. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  197. {
  198. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  199. return;
  200. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  201. CCK_CZ_CLOCK_CONTROL);
  202. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  203. }
  204. static inline u32 /* units of 100MHz */
  205. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  206. const struct intel_crtc_state *pipe_config)
  207. {
  208. if (HAS_DDI(dev_priv))
  209. return pipe_config->port_clock; /* SPLL */
  210. else
  211. return dev_priv->fdi_pll_freq;
  212. }
  213. static const struct intel_limit intel_limits_i8xx_dac = {
  214. .dot = { .min = 25000, .max = 350000 },
  215. .vco = { .min = 908000, .max = 1512000 },
  216. .n = { .min = 2, .max = 16 },
  217. .m = { .min = 96, .max = 140 },
  218. .m1 = { .min = 18, .max = 26 },
  219. .m2 = { .min = 6, .max = 16 },
  220. .p = { .min = 4, .max = 128 },
  221. .p1 = { .min = 2, .max = 33 },
  222. .p2 = { .dot_limit = 165000,
  223. .p2_slow = 4, .p2_fast = 2 },
  224. };
  225. static const struct intel_limit intel_limits_i8xx_dvo = {
  226. .dot = { .min = 25000, .max = 350000 },
  227. .vco = { .min = 908000, .max = 1512000 },
  228. .n = { .min = 2, .max = 16 },
  229. .m = { .min = 96, .max = 140 },
  230. .m1 = { .min = 18, .max = 26 },
  231. .m2 = { .min = 6, .max = 16 },
  232. .p = { .min = 4, .max = 128 },
  233. .p1 = { .min = 2, .max = 33 },
  234. .p2 = { .dot_limit = 165000,
  235. .p2_slow = 4, .p2_fast = 4 },
  236. };
  237. static const struct intel_limit intel_limits_i8xx_lvds = {
  238. .dot = { .min = 25000, .max = 350000 },
  239. .vco = { .min = 908000, .max = 1512000 },
  240. .n = { .min = 2, .max = 16 },
  241. .m = { .min = 96, .max = 140 },
  242. .m1 = { .min = 18, .max = 26 },
  243. .m2 = { .min = 6, .max = 16 },
  244. .p = { .min = 4, .max = 128 },
  245. .p1 = { .min = 1, .max = 6 },
  246. .p2 = { .dot_limit = 165000,
  247. .p2_slow = 14, .p2_fast = 7 },
  248. };
  249. static const struct intel_limit intel_limits_i9xx_sdvo = {
  250. .dot = { .min = 20000, .max = 400000 },
  251. .vco = { .min = 1400000, .max = 2800000 },
  252. .n = { .min = 1, .max = 6 },
  253. .m = { .min = 70, .max = 120 },
  254. .m1 = { .min = 8, .max = 18 },
  255. .m2 = { .min = 3, .max = 7 },
  256. .p = { .min = 5, .max = 80 },
  257. .p1 = { .min = 1, .max = 8 },
  258. .p2 = { .dot_limit = 200000,
  259. .p2_slow = 10, .p2_fast = 5 },
  260. };
  261. static const struct intel_limit intel_limits_i9xx_lvds = {
  262. .dot = { .min = 20000, .max = 400000 },
  263. .vco = { .min = 1400000, .max = 2800000 },
  264. .n = { .min = 1, .max = 6 },
  265. .m = { .min = 70, .max = 120 },
  266. .m1 = { .min = 8, .max = 18 },
  267. .m2 = { .min = 3, .max = 7 },
  268. .p = { .min = 7, .max = 98 },
  269. .p1 = { .min = 1, .max = 8 },
  270. .p2 = { .dot_limit = 112000,
  271. .p2_slow = 14, .p2_fast = 7 },
  272. };
  273. static const struct intel_limit intel_limits_g4x_sdvo = {
  274. .dot = { .min = 25000, .max = 270000 },
  275. .vco = { .min = 1750000, .max = 3500000},
  276. .n = { .min = 1, .max = 4 },
  277. .m = { .min = 104, .max = 138 },
  278. .m1 = { .min = 17, .max = 23 },
  279. .m2 = { .min = 5, .max = 11 },
  280. .p = { .min = 10, .max = 30 },
  281. .p1 = { .min = 1, .max = 3},
  282. .p2 = { .dot_limit = 270000,
  283. .p2_slow = 10,
  284. .p2_fast = 10
  285. },
  286. };
  287. static const struct intel_limit intel_limits_g4x_hdmi = {
  288. .dot = { .min = 22000, .max = 400000 },
  289. .vco = { .min = 1750000, .max = 3500000},
  290. .n = { .min = 1, .max = 4 },
  291. .m = { .min = 104, .max = 138 },
  292. .m1 = { .min = 16, .max = 23 },
  293. .m2 = { .min = 5, .max = 11 },
  294. .p = { .min = 5, .max = 80 },
  295. .p1 = { .min = 1, .max = 8},
  296. .p2 = { .dot_limit = 165000,
  297. .p2_slow = 10, .p2_fast = 5 },
  298. };
  299. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  300. .dot = { .min = 20000, .max = 115000 },
  301. .vco = { .min = 1750000, .max = 3500000 },
  302. .n = { .min = 1, .max = 3 },
  303. .m = { .min = 104, .max = 138 },
  304. .m1 = { .min = 17, .max = 23 },
  305. .m2 = { .min = 5, .max = 11 },
  306. .p = { .min = 28, .max = 112 },
  307. .p1 = { .min = 2, .max = 8 },
  308. .p2 = { .dot_limit = 0,
  309. .p2_slow = 14, .p2_fast = 14
  310. },
  311. };
  312. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  313. .dot = { .min = 80000, .max = 224000 },
  314. .vco = { .min = 1750000, .max = 3500000 },
  315. .n = { .min = 1, .max = 3 },
  316. .m = { .min = 104, .max = 138 },
  317. .m1 = { .min = 17, .max = 23 },
  318. .m2 = { .min = 5, .max = 11 },
  319. .p = { .min = 14, .max = 42 },
  320. .p1 = { .min = 2, .max = 6 },
  321. .p2 = { .dot_limit = 0,
  322. .p2_slow = 7, .p2_fast = 7
  323. },
  324. };
  325. static const struct intel_limit intel_limits_pineview_sdvo = {
  326. .dot = { .min = 20000, .max = 400000},
  327. .vco = { .min = 1700000, .max = 3500000 },
  328. /* Pineview's Ncounter is a ring counter */
  329. .n = { .min = 3, .max = 6 },
  330. .m = { .min = 2, .max = 256 },
  331. /* Pineview only has one combined m divider, which we treat as m2. */
  332. .m1 = { .min = 0, .max = 0 },
  333. .m2 = { .min = 0, .max = 254 },
  334. .p = { .min = 5, .max = 80 },
  335. .p1 = { .min = 1, .max = 8 },
  336. .p2 = { .dot_limit = 200000,
  337. .p2_slow = 10, .p2_fast = 5 },
  338. };
  339. static const struct intel_limit intel_limits_pineview_lvds = {
  340. .dot = { .min = 20000, .max = 400000 },
  341. .vco = { .min = 1700000, .max = 3500000 },
  342. .n = { .min = 3, .max = 6 },
  343. .m = { .min = 2, .max = 256 },
  344. .m1 = { .min = 0, .max = 0 },
  345. .m2 = { .min = 0, .max = 254 },
  346. .p = { .min = 7, .max = 112 },
  347. .p1 = { .min = 1, .max = 8 },
  348. .p2 = { .dot_limit = 112000,
  349. .p2_slow = 14, .p2_fast = 14 },
  350. };
  351. /* Ironlake / Sandybridge
  352. *
  353. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  354. * the range value for them is (actual_value - 2).
  355. */
  356. static const struct intel_limit intel_limits_ironlake_dac = {
  357. .dot = { .min = 25000, .max = 350000 },
  358. .vco = { .min = 1760000, .max = 3510000 },
  359. .n = { .min = 1, .max = 5 },
  360. .m = { .min = 79, .max = 127 },
  361. .m1 = { .min = 12, .max = 22 },
  362. .m2 = { .min = 5, .max = 9 },
  363. .p = { .min = 5, .max = 80 },
  364. .p1 = { .min = 1, .max = 8 },
  365. .p2 = { .dot_limit = 225000,
  366. .p2_slow = 10, .p2_fast = 5 },
  367. };
  368. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  369. .dot = { .min = 25000, .max = 350000 },
  370. .vco = { .min = 1760000, .max = 3510000 },
  371. .n = { .min = 1, .max = 3 },
  372. .m = { .min = 79, .max = 118 },
  373. .m1 = { .min = 12, .max = 22 },
  374. .m2 = { .min = 5, .max = 9 },
  375. .p = { .min = 28, .max = 112 },
  376. .p1 = { .min = 2, .max = 8 },
  377. .p2 = { .dot_limit = 225000,
  378. .p2_slow = 14, .p2_fast = 14 },
  379. };
  380. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  381. .dot = { .min = 25000, .max = 350000 },
  382. .vco = { .min = 1760000, .max = 3510000 },
  383. .n = { .min = 1, .max = 3 },
  384. .m = { .min = 79, .max = 127 },
  385. .m1 = { .min = 12, .max = 22 },
  386. .m2 = { .min = 5, .max = 9 },
  387. .p = { .min = 14, .max = 56 },
  388. .p1 = { .min = 2, .max = 8 },
  389. .p2 = { .dot_limit = 225000,
  390. .p2_slow = 7, .p2_fast = 7 },
  391. };
  392. /* LVDS 100mhz refclk limits. */
  393. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  394. .dot = { .min = 25000, .max = 350000 },
  395. .vco = { .min = 1760000, .max = 3510000 },
  396. .n = { .min = 1, .max = 2 },
  397. .m = { .min = 79, .max = 126 },
  398. .m1 = { .min = 12, .max = 22 },
  399. .m2 = { .min = 5, .max = 9 },
  400. .p = { .min = 28, .max = 112 },
  401. .p1 = { .min = 2, .max = 8 },
  402. .p2 = { .dot_limit = 225000,
  403. .p2_slow = 14, .p2_fast = 14 },
  404. };
  405. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  406. .dot = { .min = 25000, .max = 350000 },
  407. .vco = { .min = 1760000, .max = 3510000 },
  408. .n = { .min = 1, .max = 3 },
  409. .m = { .min = 79, .max = 126 },
  410. .m1 = { .min = 12, .max = 22 },
  411. .m2 = { .min = 5, .max = 9 },
  412. .p = { .min = 14, .max = 42 },
  413. .p1 = { .min = 2, .max = 6 },
  414. .p2 = { .dot_limit = 225000,
  415. .p2_slow = 7, .p2_fast = 7 },
  416. };
  417. static const struct intel_limit intel_limits_vlv = {
  418. /*
  419. * These are the data rate limits (measured in fast clocks)
  420. * since those are the strictest limits we have. The fast
  421. * clock and actual rate limits are more relaxed, so checking
  422. * them would make no difference.
  423. */
  424. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  425. .vco = { .min = 4000000, .max = 6000000 },
  426. .n = { .min = 1, .max = 7 },
  427. .m1 = { .min = 2, .max = 3 },
  428. .m2 = { .min = 11, .max = 156 },
  429. .p1 = { .min = 2, .max = 3 },
  430. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  431. };
  432. static const struct intel_limit intel_limits_chv = {
  433. /*
  434. * These are the data rate limits (measured in fast clocks)
  435. * since those are the strictest limits we have. The fast
  436. * clock and actual rate limits are more relaxed, so checking
  437. * them would make no difference.
  438. */
  439. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  440. .vco = { .min = 4800000, .max = 6480000 },
  441. .n = { .min = 1, .max = 1 },
  442. .m1 = { .min = 2, .max = 2 },
  443. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  444. .p1 = { .min = 2, .max = 4 },
  445. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  446. };
  447. static const struct intel_limit intel_limits_bxt = {
  448. /* FIXME: find real dot limits */
  449. .dot = { .min = 0, .max = INT_MAX },
  450. .vco = { .min = 4800000, .max = 6700000 },
  451. .n = { .min = 1, .max = 1 },
  452. .m1 = { .min = 2, .max = 2 },
  453. /* FIXME: find real m2 limits */
  454. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  455. .p1 = { .min = 2, .max = 4 },
  456. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  457. };
  458. static void
  459. skl_wa_528(struct drm_i915_private *dev_priv, int pipe, bool enable)
  460. {
  461. if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  462. return;
  463. if (enable)
  464. I915_WRITE(CHICKEN_PIPESL_1(pipe), HSW_FBCQ_DIS);
  465. else
  466. I915_WRITE(CHICKEN_PIPESL_1(pipe), 0);
  467. }
  468. static void
  469. skl_wa_clkgate(struct drm_i915_private *dev_priv, int pipe, bool enable)
  470. {
  471. if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  472. return;
  473. if (enable)
  474. I915_WRITE(CLKGATE_DIS_PSL(pipe),
  475. DUPS1_GATING_DIS | DUPS2_GATING_DIS);
  476. else
  477. I915_WRITE(CLKGATE_DIS_PSL(pipe),
  478. I915_READ(CLKGATE_DIS_PSL(pipe)) &
  479. ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
  480. }
  481. static bool
  482. needs_modeset(const struct drm_crtc_state *state)
  483. {
  484. return drm_atomic_crtc_needs_modeset(state);
  485. }
  486. /*
  487. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  488. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  489. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  490. * The helpers' return value is the rate of the clock that is fed to the
  491. * display engine's pipe which can be the above fast dot clock rate or a
  492. * divided-down version of it.
  493. */
  494. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  495. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  496. {
  497. clock->m = clock->m2 + 2;
  498. clock->p = clock->p1 * clock->p2;
  499. if (WARN_ON(clock->n == 0 || clock->p == 0))
  500. return 0;
  501. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  502. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  503. return clock->dot;
  504. }
  505. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  506. {
  507. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  508. }
  509. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  510. {
  511. clock->m = i9xx_dpll_compute_m(clock);
  512. clock->p = clock->p1 * clock->p2;
  513. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  514. return 0;
  515. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  516. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  517. return clock->dot;
  518. }
  519. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  520. {
  521. clock->m = clock->m1 * clock->m2;
  522. clock->p = clock->p1 * clock->p2;
  523. if (WARN_ON(clock->n == 0 || clock->p == 0))
  524. return 0;
  525. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  526. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  527. return clock->dot / 5;
  528. }
  529. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  530. {
  531. clock->m = clock->m1 * clock->m2;
  532. clock->p = clock->p1 * clock->p2;
  533. if (WARN_ON(clock->n == 0 || clock->p == 0))
  534. return 0;
  535. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  536. clock->n << 22);
  537. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  538. return clock->dot / 5;
  539. }
  540. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  541. /*
  542. * Returns whether the given set of divisors are valid for a given refclk with
  543. * the given connectors.
  544. */
  545. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  546. const struct intel_limit *limit,
  547. const struct dpll *clock)
  548. {
  549. if (clock->n < limit->n.min || limit->n.max < clock->n)
  550. INTELPllInvalid("n out of range\n");
  551. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  552. INTELPllInvalid("p1 out of range\n");
  553. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  554. INTELPllInvalid("m2 out of range\n");
  555. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  556. INTELPllInvalid("m1 out of range\n");
  557. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  558. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  559. if (clock->m1 <= clock->m2)
  560. INTELPllInvalid("m1 <= m2\n");
  561. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  562. !IS_GEN9_LP(dev_priv)) {
  563. if (clock->p < limit->p.min || limit->p.max < clock->p)
  564. INTELPllInvalid("p out of range\n");
  565. if (clock->m < limit->m.min || limit->m.max < clock->m)
  566. INTELPllInvalid("m out of range\n");
  567. }
  568. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  569. INTELPllInvalid("vco out of range\n");
  570. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  571. * connector, etc., rather than just a single range.
  572. */
  573. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  574. INTELPllInvalid("dot out of range\n");
  575. return true;
  576. }
  577. static int
  578. i9xx_select_p2_div(const struct intel_limit *limit,
  579. const struct intel_crtc_state *crtc_state,
  580. int target)
  581. {
  582. struct drm_device *dev = crtc_state->base.crtc->dev;
  583. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  584. /*
  585. * For LVDS just rely on its current settings for dual-channel.
  586. * We haven't figured out how to reliably set up different
  587. * single/dual channel state, if we even can.
  588. */
  589. if (intel_is_dual_link_lvds(dev))
  590. return limit->p2.p2_fast;
  591. else
  592. return limit->p2.p2_slow;
  593. } else {
  594. if (target < limit->p2.dot_limit)
  595. return limit->p2.p2_slow;
  596. else
  597. return limit->p2.p2_fast;
  598. }
  599. }
  600. /*
  601. * Returns a set of divisors for the desired target clock with the given
  602. * refclk, or FALSE. The returned values represent the clock equation:
  603. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  604. *
  605. * Target and reference clocks are specified in kHz.
  606. *
  607. * If match_clock is provided, then best_clock P divider must match the P
  608. * divider from @match_clock used for LVDS downclocking.
  609. */
  610. static bool
  611. i9xx_find_best_dpll(const struct intel_limit *limit,
  612. struct intel_crtc_state *crtc_state,
  613. int target, int refclk, struct dpll *match_clock,
  614. struct dpll *best_clock)
  615. {
  616. struct drm_device *dev = crtc_state->base.crtc->dev;
  617. struct dpll clock;
  618. int err = target;
  619. memset(best_clock, 0, sizeof(*best_clock));
  620. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  621. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  622. clock.m1++) {
  623. for (clock.m2 = limit->m2.min;
  624. clock.m2 <= limit->m2.max; clock.m2++) {
  625. if (clock.m2 >= clock.m1)
  626. break;
  627. for (clock.n = limit->n.min;
  628. clock.n <= limit->n.max; clock.n++) {
  629. for (clock.p1 = limit->p1.min;
  630. clock.p1 <= limit->p1.max; clock.p1++) {
  631. int this_err;
  632. i9xx_calc_dpll_params(refclk, &clock);
  633. if (!intel_PLL_is_valid(to_i915(dev),
  634. limit,
  635. &clock))
  636. continue;
  637. if (match_clock &&
  638. clock.p != match_clock->p)
  639. continue;
  640. this_err = abs(clock.dot - target);
  641. if (this_err < err) {
  642. *best_clock = clock;
  643. err = this_err;
  644. }
  645. }
  646. }
  647. }
  648. }
  649. return (err != target);
  650. }
  651. /*
  652. * Returns a set of divisors for the desired target clock with the given
  653. * refclk, or FALSE. The returned values represent the clock equation:
  654. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  655. *
  656. * Target and reference clocks are specified in kHz.
  657. *
  658. * If match_clock is provided, then best_clock P divider must match the P
  659. * divider from @match_clock used for LVDS downclocking.
  660. */
  661. static bool
  662. pnv_find_best_dpll(const struct intel_limit *limit,
  663. struct intel_crtc_state *crtc_state,
  664. int target, int refclk, struct dpll *match_clock,
  665. struct dpll *best_clock)
  666. {
  667. struct drm_device *dev = crtc_state->base.crtc->dev;
  668. struct dpll clock;
  669. int err = target;
  670. memset(best_clock, 0, sizeof(*best_clock));
  671. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  672. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  673. clock.m1++) {
  674. for (clock.m2 = limit->m2.min;
  675. clock.m2 <= limit->m2.max; clock.m2++) {
  676. for (clock.n = limit->n.min;
  677. clock.n <= limit->n.max; clock.n++) {
  678. for (clock.p1 = limit->p1.min;
  679. clock.p1 <= limit->p1.max; clock.p1++) {
  680. int this_err;
  681. pnv_calc_dpll_params(refclk, &clock);
  682. if (!intel_PLL_is_valid(to_i915(dev),
  683. limit,
  684. &clock))
  685. continue;
  686. if (match_clock &&
  687. clock.p != match_clock->p)
  688. continue;
  689. this_err = abs(clock.dot - target);
  690. if (this_err < err) {
  691. *best_clock = clock;
  692. err = this_err;
  693. }
  694. }
  695. }
  696. }
  697. }
  698. return (err != target);
  699. }
  700. /*
  701. * Returns a set of divisors for the desired target clock with the given
  702. * refclk, or FALSE. The returned values represent the clock equation:
  703. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  704. *
  705. * Target and reference clocks are specified in kHz.
  706. *
  707. * If match_clock is provided, then best_clock P divider must match the P
  708. * divider from @match_clock used for LVDS downclocking.
  709. */
  710. static bool
  711. g4x_find_best_dpll(const struct intel_limit *limit,
  712. struct intel_crtc_state *crtc_state,
  713. int target, int refclk, struct dpll *match_clock,
  714. struct dpll *best_clock)
  715. {
  716. struct drm_device *dev = crtc_state->base.crtc->dev;
  717. struct dpll clock;
  718. int max_n;
  719. bool found = false;
  720. /* approximately equals target * 0.00585 */
  721. int err_most = (target >> 8) + (target >> 9);
  722. memset(best_clock, 0, sizeof(*best_clock));
  723. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  724. max_n = limit->n.max;
  725. /* based on hardware requirement, prefer smaller n to precision */
  726. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  727. /* based on hardware requirement, prefere larger m1,m2 */
  728. for (clock.m1 = limit->m1.max;
  729. clock.m1 >= limit->m1.min; clock.m1--) {
  730. for (clock.m2 = limit->m2.max;
  731. clock.m2 >= limit->m2.min; clock.m2--) {
  732. for (clock.p1 = limit->p1.max;
  733. clock.p1 >= limit->p1.min; clock.p1--) {
  734. int this_err;
  735. i9xx_calc_dpll_params(refclk, &clock);
  736. if (!intel_PLL_is_valid(to_i915(dev),
  737. limit,
  738. &clock))
  739. continue;
  740. this_err = abs(clock.dot - target);
  741. if (this_err < err_most) {
  742. *best_clock = clock;
  743. err_most = this_err;
  744. max_n = clock.n;
  745. found = true;
  746. }
  747. }
  748. }
  749. }
  750. }
  751. return found;
  752. }
  753. /*
  754. * Check if the calculated PLL configuration is more optimal compared to the
  755. * best configuration and error found so far. Return the calculated error.
  756. */
  757. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  758. const struct dpll *calculated_clock,
  759. const struct dpll *best_clock,
  760. unsigned int best_error_ppm,
  761. unsigned int *error_ppm)
  762. {
  763. /*
  764. * For CHV ignore the error and consider only the P value.
  765. * Prefer a bigger P value based on HW requirements.
  766. */
  767. if (IS_CHERRYVIEW(to_i915(dev))) {
  768. *error_ppm = 0;
  769. return calculated_clock->p > best_clock->p;
  770. }
  771. if (WARN_ON_ONCE(!target_freq))
  772. return false;
  773. *error_ppm = div_u64(1000000ULL *
  774. abs(target_freq - calculated_clock->dot),
  775. target_freq);
  776. /*
  777. * Prefer a better P value over a better (smaller) error if the error
  778. * is small. Ensure this preference for future configurations too by
  779. * setting the error to 0.
  780. */
  781. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  782. *error_ppm = 0;
  783. return true;
  784. }
  785. return *error_ppm + 10 < best_error_ppm;
  786. }
  787. /*
  788. * Returns a set of divisors for the desired target clock with the given
  789. * refclk, or FALSE. The returned values represent the clock equation:
  790. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  791. */
  792. static bool
  793. vlv_find_best_dpll(const struct intel_limit *limit,
  794. struct intel_crtc_state *crtc_state,
  795. int target, int refclk, struct dpll *match_clock,
  796. struct dpll *best_clock)
  797. {
  798. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  799. struct drm_device *dev = crtc->base.dev;
  800. struct dpll clock;
  801. unsigned int bestppm = 1000000;
  802. /* min update 19.2 MHz */
  803. int max_n = min(limit->n.max, refclk / 19200);
  804. bool found = false;
  805. target *= 5; /* fast clock */
  806. memset(best_clock, 0, sizeof(*best_clock));
  807. /* based on hardware requirement, prefer smaller n to precision */
  808. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  809. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  810. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  811. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  812. clock.p = clock.p1 * clock.p2;
  813. /* based on hardware requirement, prefer bigger m1,m2 values */
  814. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  815. unsigned int ppm;
  816. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  817. refclk * clock.m1);
  818. vlv_calc_dpll_params(refclk, &clock);
  819. if (!intel_PLL_is_valid(to_i915(dev),
  820. limit,
  821. &clock))
  822. continue;
  823. if (!vlv_PLL_is_optimal(dev, target,
  824. &clock,
  825. best_clock,
  826. bestppm, &ppm))
  827. continue;
  828. *best_clock = clock;
  829. bestppm = ppm;
  830. found = true;
  831. }
  832. }
  833. }
  834. }
  835. return found;
  836. }
  837. /*
  838. * Returns a set of divisors for the desired target clock with the given
  839. * refclk, or FALSE. The returned values represent the clock equation:
  840. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  841. */
  842. static bool
  843. chv_find_best_dpll(const struct intel_limit *limit,
  844. struct intel_crtc_state *crtc_state,
  845. int target, int refclk, struct dpll *match_clock,
  846. struct dpll *best_clock)
  847. {
  848. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  849. struct drm_device *dev = crtc->base.dev;
  850. unsigned int best_error_ppm;
  851. struct dpll clock;
  852. uint64_t m2;
  853. int found = false;
  854. memset(best_clock, 0, sizeof(*best_clock));
  855. best_error_ppm = 1000000;
  856. /*
  857. * Based on hardware doc, the n always set to 1, and m1 always
  858. * set to 2. If requires to support 200Mhz refclk, we need to
  859. * revisit this because n may not 1 anymore.
  860. */
  861. clock.n = 1, clock.m1 = 2;
  862. target *= 5; /* fast clock */
  863. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  864. for (clock.p2 = limit->p2.p2_fast;
  865. clock.p2 >= limit->p2.p2_slow;
  866. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  867. unsigned int error_ppm;
  868. clock.p = clock.p1 * clock.p2;
  869. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  870. clock.n) << 22, refclk * clock.m1);
  871. if (m2 > INT_MAX/clock.m1)
  872. continue;
  873. clock.m2 = m2;
  874. chv_calc_dpll_params(refclk, &clock);
  875. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  876. continue;
  877. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  878. best_error_ppm, &error_ppm))
  879. continue;
  880. *best_clock = clock;
  881. best_error_ppm = error_ppm;
  882. found = true;
  883. }
  884. }
  885. return found;
  886. }
  887. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  888. struct dpll *best_clock)
  889. {
  890. int refclk = 100000;
  891. const struct intel_limit *limit = &intel_limits_bxt;
  892. return chv_find_best_dpll(limit, crtc_state,
  893. target_clock, refclk, NULL, best_clock);
  894. }
  895. bool intel_crtc_active(struct intel_crtc *crtc)
  896. {
  897. /* Be paranoid as we can arrive here with only partial
  898. * state retrieved from the hardware during setup.
  899. *
  900. * We can ditch the adjusted_mode.crtc_clock check as soon
  901. * as Haswell has gained clock readout/fastboot support.
  902. *
  903. * We can ditch the crtc->primary->state->fb check as soon as we can
  904. * properly reconstruct framebuffers.
  905. *
  906. * FIXME: The intel_crtc->active here should be switched to
  907. * crtc->state->active once we have proper CRTC states wired up
  908. * for atomic.
  909. */
  910. return crtc->active && crtc->base.primary->state->fb &&
  911. crtc->config->base.adjusted_mode.crtc_clock;
  912. }
  913. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  914. enum pipe pipe)
  915. {
  916. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  917. return crtc->config->cpu_transcoder;
  918. }
  919. static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
  920. enum pipe pipe)
  921. {
  922. i915_reg_t reg = PIPEDSL(pipe);
  923. u32 line1, line2;
  924. u32 line_mask;
  925. if (IS_GEN2(dev_priv))
  926. line_mask = DSL_LINEMASK_GEN2;
  927. else
  928. line_mask = DSL_LINEMASK_GEN3;
  929. line1 = I915_READ(reg) & line_mask;
  930. msleep(5);
  931. line2 = I915_READ(reg) & line_mask;
  932. return line1 != line2;
  933. }
  934. static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
  935. {
  936. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  937. enum pipe pipe = crtc->pipe;
  938. /* Wait for the display line to settle/start moving */
  939. if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
  940. DRM_ERROR("pipe %c scanline %s wait timed out\n",
  941. pipe_name(pipe), onoff(state));
  942. }
  943. static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
  944. {
  945. wait_for_pipe_scanline_moving(crtc, false);
  946. }
  947. static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
  948. {
  949. wait_for_pipe_scanline_moving(crtc, true);
  950. }
  951. static void
  952. intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
  953. {
  954. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  955. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  956. if (INTEL_GEN(dev_priv) >= 4) {
  957. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  958. i915_reg_t reg = PIPECONF(cpu_transcoder);
  959. /* Wait for the Pipe State to go off */
  960. if (intel_wait_for_register(dev_priv,
  961. reg, I965_PIPECONF_ACTIVE, 0,
  962. 100))
  963. WARN(1, "pipe_off wait timed out\n");
  964. } else {
  965. intel_wait_for_pipe_scanline_stopped(crtc);
  966. }
  967. }
  968. /* Only for pre-ILK configs */
  969. void assert_pll(struct drm_i915_private *dev_priv,
  970. enum pipe pipe, bool state)
  971. {
  972. u32 val;
  973. bool cur_state;
  974. val = I915_READ(DPLL(pipe));
  975. cur_state = !!(val & DPLL_VCO_ENABLE);
  976. I915_STATE_WARN(cur_state != state,
  977. "PLL state assertion failure (expected %s, current %s)\n",
  978. onoff(state), onoff(cur_state));
  979. }
  980. /* XXX: the dsi pll is shared between MIPI DSI ports */
  981. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  982. {
  983. u32 val;
  984. bool cur_state;
  985. mutex_lock(&dev_priv->sb_lock);
  986. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  987. mutex_unlock(&dev_priv->sb_lock);
  988. cur_state = val & DSI_PLL_VCO_EN;
  989. I915_STATE_WARN(cur_state != state,
  990. "DSI PLL state assertion failure (expected %s, current %s)\n",
  991. onoff(state), onoff(cur_state));
  992. }
  993. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  994. enum pipe pipe, bool state)
  995. {
  996. bool cur_state;
  997. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  998. pipe);
  999. if (HAS_DDI(dev_priv)) {
  1000. /* DDI does not have a specific FDI_TX register */
  1001. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1002. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1003. } else {
  1004. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1005. cur_state = !!(val & FDI_TX_ENABLE);
  1006. }
  1007. I915_STATE_WARN(cur_state != state,
  1008. "FDI TX state assertion failure (expected %s, current %s)\n",
  1009. onoff(state), onoff(cur_state));
  1010. }
  1011. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1012. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1013. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1014. enum pipe pipe, bool state)
  1015. {
  1016. u32 val;
  1017. bool cur_state;
  1018. val = I915_READ(FDI_RX_CTL(pipe));
  1019. cur_state = !!(val & FDI_RX_ENABLE);
  1020. I915_STATE_WARN(cur_state != state,
  1021. "FDI RX state assertion failure (expected %s, current %s)\n",
  1022. onoff(state), onoff(cur_state));
  1023. }
  1024. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1025. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1026. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1027. enum pipe pipe)
  1028. {
  1029. u32 val;
  1030. /* ILK FDI PLL is always enabled */
  1031. if (IS_GEN5(dev_priv))
  1032. return;
  1033. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1034. if (HAS_DDI(dev_priv))
  1035. return;
  1036. val = I915_READ(FDI_TX_CTL(pipe));
  1037. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1038. }
  1039. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1040. enum pipe pipe, bool state)
  1041. {
  1042. u32 val;
  1043. bool cur_state;
  1044. val = I915_READ(FDI_RX_CTL(pipe));
  1045. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1046. I915_STATE_WARN(cur_state != state,
  1047. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1048. onoff(state), onoff(cur_state));
  1049. }
  1050. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1051. {
  1052. i915_reg_t pp_reg;
  1053. u32 val;
  1054. enum pipe panel_pipe = INVALID_PIPE;
  1055. bool locked = true;
  1056. if (WARN_ON(HAS_DDI(dev_priv)))
  1057. return;
  1058. if (HAS_PCH_SPLIT(dev_priv)) {
  1059. u32 port_sel;
  1060. pp_reg = PP_CONTROL(0);
  1061. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1062. switch (port_sel) {
  1063. case PANEL_PORT_SELECT_LVDS:
  1064. intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
  1065. break;
  1066. case PANEL_PORT_SELECT_DPA:
  1067. intel_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
  1068. break;
  1069. case PANEL_PORT_SELECT_DPC:
  1070. intel_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
  1071. break;
  1072. case PANEL_PORT_SELECT_DPD:
  1073. intel_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
  1074. break;
  1075. default:
  1076. MISSING_CASE(port_sel);
  1077. break;
  1078. }
  1079. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1080. /* presumably write lock depends on pipe, not port select */
  1081. pp_reg = PP_CONTROL(pipe);
  1082. panel_pipe = pipe;
  1083. } else {
  1084. u32 port_sel;
  1085. pp_reg = PP_CONTROL(0);
  1086. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1087. WARN_ON(port_sel != PANEL_PORT_SELECT_LVDS);
  1088. intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
  1089. }
  1090. val = I915_READ(pp_reg);
  1091. if (!(val & PANEL_POWER_ON) ||
  1092. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1093. locked = false;
  1094. I915_STATE_WARN(panel_pipe == pipe && locked,
  1095. "panel assertion failure, pipe %c regs locked\n",
  1096. pipe_name(pipe));
  1097. }
  1098. void assert_pipe(struct drm_i915_private *dev_priv,
  1099. enum pipe pipe, bool state)
  1100. {
  1101. bool cur_state;
  1102. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1103. pipe);
  1104. enum intel_display_power_domain power_domain;
  1105. /* we keep both pipes enabled on 830 */
  1106. if (IS_I830(dev_priv))
  1107. state = true;
  1108. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1109. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1110. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1111. cur_state = !!(val & PIPECONF_ENABLE);
  1112. intel_display_power_put(dev_priv, power_domain);
  1113. } else {
  1114. cur_state = false;
  1115. }
  1116. I915_STATE_WARN(cur_state != state,
  1117. "pipe %c assertion failure (expected %s, current %s)\n",
  1118. pipe_name(pipe), onoff(state), onoff(cur_state));
  1119. }
  1120. static void assert_plane(struct intel_plane *plane, bool state)
  1121. {
  1122. enum pipe pipe;
  1123. bool cur_state;
  1124. cur_state = plane->get_hw_state(plane, &pipe);
  1125. I915_STATE_WARN(cur_state != state,
  1126. "%s assertion failure (expected %s, current %s)\n",
  1127. plane->base.name, onoff(state), onoff(cur_state));
  1128. }
  1129. #define assert_plane_enabled(p) assert_plane(p, true)
  1130. #define assert_plane_disabled(p) assert_plane(p, false)
  1131. static void assert_planes_disabled(struct intel_crtc *crtc)
  1132. {
  1133. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1134. struct intel_plane *plane;
  1135. for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
  1136. assert_plane_disabled(plane);
  1137. }
  1138. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1139. {
  1140. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1141. drm_crtc_vblank_put(crtc);
  1142. }
  1143. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1144. enum pipe pipe)
  1145. {
  1146. u32 val;
  1147. bool enabled;
  1148. val = I915_READ(PCH_TRANSCONF(pipe));
  1149. enabled = !!(val & TRANS_ENABLE);
  1150. I915_STATE_WARN(enabled,
  1151. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1152. pipe_name(pipe));
  1153. }
  1154. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1155. enum pipe pipe, enum port port,
  1156. i915_reg_t dp_reg)
  1157. {
  1158. enum pipe port_pipe;
  1159. bool state;
  1160. state = intel_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
  1161. I915_STATE_WARN(state && port_pipe == pipe,
  1162. "PCH DP %c enabled on transcoder %c, should be disabled\n",
  1163. port_name(port), pipe_name(pipe));
  1164. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
  1165. "IBX PCH DP %c still using transcoder B\n",
  1166. port_name(port));
  1167. }
  1168. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1169. enum pipe pipe, enum port port,
  1170. i915_reg_t hdmi_reg)
  1171. {
  1172. enum pipe port_pipe;
  1173. bool state;
  1174. state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
  1175. I915_STATE_WARN(state && port_pipe == pipe,
  1176. "PCH HDMI %c enabled on transcoder %c, should be disabled\n",
  1177. port_name(port), pipe_name(pipe));
  1178. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
  1179. "IBX PCH HDMI %c still using transcoder B\n",
  1180. port_name(port));
  1181. }
  1182. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1183. enum pipe pipe)
  1184. {
  1185. enum pipe port_pipe;
  1186. assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
  1187. assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
  1188. assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
  1189. I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
  1190. port_pipe == pipe,
  1191. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1192. pipe_name(pipe));
  1193. I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
  1194. port_pipe == pipe,
  1195. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1196. pipe_name(pipe));
  1197. assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
  1198. assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
  1199. assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
  1200. }
  1201. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1202. const struct intel_crtc_state *pipe_config)
  1203. {
  1204. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1205. enum pipe pipe = crtc->pipe;
  1206. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1207. POSTING_READ(DPLL(pipe));
  1208. udelay(150);
  1209. if (intel_wait_for_register(dev_priv,
  1210. DPLL(pipe),
  1211. DPLL_LOCK_VLV,
  1212. DPLL_LOCK_VLV,
  1213. 1))
  1214. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1215. }
  1216. static void vlv_enable_pll(struct intel_crtc *crtc,
  1217. const struct intel_crtc_state *pipe_config)
  1218. {
  1219. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1220. enum pipe pipe = crtc->pipe;
  1221. assert_pipe_disabled(dev_priv, pipe);
  1222. /* PLL is protected by panel, make sure we can write it */
  1223. assert_panel_unlocked(dev_priv, pipe);
  1224. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1225. _vlv_enable_pll(crtc, pipe_config);
  1226. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1227. POSTING_READ(DPLL_MD(pipe));
  1228. }
  1229. static void _chv_enable_pll(struct intel_crtc *crtc,
  1230. const struct intel_crtc_state *pipe_config)
  1231. {
  1232. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1233. enum pipe pipe = crtc->pipe;
  1234. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1235. u32 tmp;
  1236. mutex_lock(&dev_priv->sb_lock);
  1237. /* Enable back the 10bit clock to display controller */
  1238. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1239. tmp |= DPIO_DCLKP_EN;
  1240. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1241. mutex_unlock(&dev_priv->sb_lock);
  1242. /*
  1243. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1244. */
  1245. udelay(1);
  1246. /* Enable PLL */
  1247. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1248. /* Check PLL is locked */
  1249. if (intel_wait_for_register(dev_priv,
  1250. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1251. 1))
  1252. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1253. }
  1254. static void chv_enable_pll(struct intel_crtc *crtc,
  1255. const struct intel_crtc_state *pipe_config)
  1256. {
  1257. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1258. enum pipe pipe = crtc->pipe;
  1259. assert_pipe_disabled(dev_priv, pipe);
  1260. /* PLL is protected by panel, make sure we can write it */
  1261. assert_panel_unlocked(dev_priv, pipe);
  1262. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1263. _chv_enable_pll(crtc, pipe_config);
  1264. if (pipe != PIPE_A) {
  1265. /*
  1266. * WaPixelRepeatModeFixForC0:chv
  1267. *
  1268. * DPLLCMD is AWOL. Use chicken bits to propagate
  1269. * the value from DPLLBMD to either pipe B or C.
  1270. */
  1271. I915_WRITE(CBR4_VLV, CBR_DPLLBMD_PIPE(pipe));
  1272. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1273. I915_WRITE(CBR4_VLV, 0);
  1274. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1275. /*
  1276. * DPLLB VGA mode also seems to cause problems.
  1277. * We should always have it disabled.
  1278. */
  1279. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1280. } else {
  1281. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1282. POSTING_READ(DPLL_MD(pipe));
  1283. }
  1284. }
  1285. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1286. {
  1287. struct intel_crtc *crtc;
  1288. int count = 0;
  1289. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1290. count += crtc->base.state->active &&
  1291. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1292. }
  1293. return count;
  1294. }
  1295. static void i9xx_enable_pll(struct intel_crtc *crtc,
  1296. const struct intel_crtc_state *crtc_state)
  1297. {
  1298. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1299. i915_reg_t reg = DPLL(crtc->pipe);
  1300. u32 dpll = crtc_state->dpll_hw_state.dpll;
  1301. int i;
  1302. assert_pipe_disabled(dev_priv, crtc->pipe);
  1303. /* PLL is protected by panel, make sure we can write it */
  1304. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1305. assert_panel_unlocked(dev_priv, crtc->pipe);
  1306. /* Enable DVO 2x clock on both PLLs if necessary */
  1307. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1308. /*
  1309. * It appears to be important that we don't enable this
  1310. * for the current pipe before otherwise configuring the
  1311. * PLL. No idea how this should be handled if multiple
  1312. * DVO outputs are enabled simultaneosly.
  1313. */
  1314. dpll |= DPLL_DVO_2X_MODE;
  1315. I915_WRITE(DPLL(!crtc->pipe),
  1316. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1317. }
  1318. /*
  1319. * Apparently we need to have VGA mode enabled prior to changing
  1320. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1321. * dividers, even though the register value does change.
  1322. */
  1323. I915_WRITE(reg, 0);
  1324. I915_WRITE(reg, dpll);
  1325. /* Wait for the clocks to stabilize. */
  1326. POSTING_READ(reg);
  1327. udelay(150);
  1328. if (INTEL_GEN(dev_priv) >= 4) {
  1329. I915_WRITE(DPLL_MD(crtc->pipe),
  1330. crtc_state->dpll_hw_state.dpll_md);
  1331. } else {
  1332. /* The pixel multiplier can only be updated once the
  1333. * DPLL is enabled and the clocks are stable.
  1334. *
  1335. * So write it again.
  1336. */
  1337. I915_WRITE(reg, dpll);
  1338. }
  1339. /* We do this three times for luck */
  1340. for (i = 0; i < 3; i++) {
  1341. I915_WRITE(reg, dpll);
  1342. POSTING_READ(reg);
  1343. udelay(150); /* wait for warmup */
  1344. }
  1345. }
  1346. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1347. {
  1348. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1349. enum pipe pipe = crtc->pipe;
  1350. /* Disable DVO 2x clock on both PLLs if necessary */
  1351. if (IS_I830(dev_priv) &&
  1352. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1353. !intel_num_dvo_pipes(dev_priv)) {
  1354. I915_WRITE(DPLL(PIPE_B),
  1355. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1356. I915_WRITE(DPLL(PIPE_A),
  1357. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1358. }
  1359. /* Don't disable pipe or pipe PLLs if needed */
  1360. if (IS_I830(dev_priv))
  1361. return;
  1362. /* Make sure the pipe isn't still relying on us */
  1363. assert_pipe_disabled(dev_priv, pipe);
  1364. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1365. POSTING_READ(DPLL(pipe));
  1366. }
  1367. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1368. {
  1369. u32 val;
  1370. /* Make sure the pipe isn't still relying on us */
  1371. assert_pipe_disabled(dev_priv, pipe);
  1372. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1373. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1374. if (pipe != PIPE_A)
  1375. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1376. I915_WRITE(DPLL(pipe), val);
  1377. POSTING_READ(DPLL(pipe));
  1378. }
  1379. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1380. {
  1381. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1382. u32 val;
  1383. /* Make sure the pipe isn't still relying on us */
  1384. assert_pipe_disabled(dev_priv, pipe);
  1385. val = DPLL_SSC_REF_CLK_CHV |
  1386. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1387. if (pipe != PIPE_A)
  1388. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1389. I915_WRITE(DPLL(pipe), val);
  1390. POSTING_READ(DPLL(pipe));
  1391. mutex_lock(&dev_priv->sb_lock);
  1392. /* Disable 10bit clock to display controller */
  1393. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1394. val &= ~DPIO_DCLKP_EN;
  1395. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1396. mutex_unlock(&dev_priv->sb_lock);
  1397. }
  1398. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1399. struct intel_digital_port *dport,
  1400. unsigned int expected_mask)
  1401. {
  1402. u32 port_mask;
  1403. i915_reg_t dpll_reg;
  1404. switch (dport->base.port) {
  1405. case PORT_B:
  1406. port_mask = DPLL_PORTB_READY_MASK;
  1407. dpll_reg = DPLL(0);
  1408. break;
  1409. case PORT_C:
  1410. port_mask = DPLL_PORTC_READY_MASK;
  1411. dpll_reg = DPLL(0);
  1412. expected_mask <<= 4;
  1413. break;
  1414. case PORT_D:
  1415. port_mask = DPLL_PORTD_READY_MASK;
  1416. dpll_reg = DPIO_PHY_STATUS;
  1417. break;
  1418. default:
  1419. BUG();
  1420. }
  1421. if (intel_wait_for_register(dev_priv,
  1422. dpll_reg, port_mask, expected_mask,
  1423. 1000))
  1424. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1425. port_name(dport->base.port),
  1426. I915_READ(dpll_reg) & port_mask, expected_mask);
  1427. }
  1428. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1429. enum pipe pipe)
  1430. {
  1431. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1432. pipe);
  1433. i915_reg_t reg;
  1434. uint32_t val, pipeconf_val;
  1435. /* Make sure PCH DPLL is enabled */
  1436. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1437. /* FDI must be feeding us bits for PCH ports */
  1438. assert_fdi_tx_enabled(dev_priv, pipe);
  1439. assert_fdi_rx_enabled(dev_priv, pipe);
  1440. if (HAS_PCH_CPT(dev_priv)) {
  1441. /* Workaround: Set the timing override bit before enabling the
  1442. * pch transcoder. */
  1443. reg = TRANS_CHICKEN2(pipe);
  1444. val = I915_READ(reg);
  1445. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1446. I915_WRITE(reg, val);
  1447. }
  1448. reg = PCH_TRANSCONF(pipe);
  1449. val = I915_READ(reg);
  1450. pipeconf_val = I915_READ(PIPECONF(pipe));
  1451. if (HAS_PCH_IBX(dev_priv)) {
  1452. /*
  1453. * Make the BPC in transcoder be consistent with
  1454. * that in pipeconf reg. For HDMI we must use 8bpc
  1455. * here for both 8bpc and 12bpc.
  1456. */
  1457. val &= ~PIPECONF_BPC_MASK;
  1458. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1459. val |= PIPECONF_8BPC;
  1460. else
  1461. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1462. }
  1463. val &= ~TRANS_INTERLACE_MASK;
  1464. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1465. if (HAS_PCH_IBX(dev_priv) &&
  1466. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1467. val |= TRANS_LEGACY_INTERLACED_ILK;
  1468. else
  1469. val |= TRANS_INTERLACED;
  1470. else
  1471. val |= TRANS_PROGRESSIVE;
  1472. I915_WRITE(reg, val | TRANS_ENABLE);
  1473. if (intel_wait_for_register(dev_priv,
  1474. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1475. 100))
  1476. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1477. }
  1478. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1479. enum transcoder cpu_transcoder)
  1480. {
  1481. u32 val, pipeconf_val;
  1482. /* FDI must be feeding us bits for PCH ports */
  1483. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1484. assert_fdi_rx_enabled(dev_priv, PIPE_A);
  1485. /* Workaround: set timing override bit. */
  1486. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1487. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1488. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1489. val = TRANS_ENABLE;
  1490. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1491. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1492. PIPECONF_INTERLACED_ILK)
  1493. val |= TRANS_INTERLACED;
  1494. else
  1495. val |= TRANS_PROGRESSIVE;
  1496. I915_WRITE(LPT_TRANSCONF, val);
  1497. if (intel_wait_for_register(dev_priv,
  1498. LPT_TRANSCONF,
  1499. TRANS_STATE_ENABLE,
  1500. TRANS_STATE_ENABLE,
  1501. 100))
  1502. DRM_ERROR("Failed to enable PCH transcoder\n");
  1503. }
  1504. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1505. enum pipe pipe)
  1506. {
  1507. i915_reg_t reg;
  1508. uint32_t val;
  1509. /* FDI relies on the transcoder */
  1510. assert_fdi_tx_disabled(dev_priv, pipe);
  1511. assert_fdi_rx_disabled(dev_priv, pipe);
  1512. /* Ports must be off as well */
  1513. assert_pch_ports_disabled(dev_priv, pipe);
  1514. reg = PCH_TRANSCONF(pipe);
  1515. val = I915_READ(reg);
  1516. val &= ~TRANS_ENABLE;
  1517. I915_WRITE(reg, val);
  1518. /* wait for PCH transcoder off, transcoder state */
  1519. if (intel_wait_for_register(dev_priv,
  1520. reg, TRANS_STATE_ENABLE, 0,
  1521. 50))
  1522. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1523. if (HAS_PCH_CPT(dev_priv)) {
  1524. /* Workaround: Clear the timing override chicken bit again. */
  1525. reg = TRANS_CHICKEN2(pipe);
  1526. val = I915_READ(reg);
  1527. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1528. I915_WRITE(reg, val);
  1529. }
  1530. }
  1531. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1532. {
  1533. u32 val;
  1534. val = I915_READ(LPT_TRANSCONF);
  1535. val &= ~TRANS_ENABLE;
  1536. I915_WRITE(LPT_TRANSCONF, val);
  1537. /* wait for PCH transcoder off, transcoder state */
  1538. if (intel_wait_for_register(dev_priv,
  1539. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1540. 50))
  1541. DRM_ERROR("Failed to disable PCH transcoder\n");
  1542. /* Workaround: clear timing override bit. */
  1543. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1544. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1545. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1546. }
  1547. enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1548. {
  1549. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1550. if (HAS_PCH_LPT(dev_priv))
  1551. return PIPE_A;
  1552. else
  1553. return crtc->pipe;
  1554. }
  1555. static void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
  1556. {
  1557. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  1558. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1559. enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
  1560. enum pipe pipe = crtc->pipe;
  1561. i915_reg_t reg;
  1562. u32 val;
  1563. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1564. assert_planes_disabled(crtc);
  1565. /*
  1566. * A pipe without a PLL won't actually be able to drive bits from
  1567. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1568. * need the check.
  1569. */
  1570. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1571. if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
  1572. assert_dsi_pll_enabled(dev_priv);
  1573. else
  1574. assert_pll_enabled(dev_priv, pipe);
  1575. } else {
  1576. if (new_crtc_state->has_pch_encoder) {
  1577. /* if driving the PCH, we need FDI enabled */
  1578. assert_fdi_rx_pll_enabled(dev_priv,
  1579. intel_crtc_pch_transcoder(crtc));
  1580. assert_fdi_tx_pll_enabled(dev_priv,
  1581. (enum pipe) cpu_transcoder);
  1582. }
  1583. /* FIXME: assert CPU port conditions for SNB+ */
  1584. }
  1585. reg = PIPECONF(cpu_transcoder);
  1586. val = I915_READ(reg);
  1587. if (val & PIPECONF_ENABLE) {
  1588. /* we keep both pipes enabled on 830 */
  1589. WARN_ON(!IS_I830(dev_priv));
  1590. return;
  1591. }
  1592. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1593. POSTING_READ(reg);
  1594. /*
  1595. * Until the pipe starts PIPEDSL reads will return a stale value,
  1596. * which causes an apparent vblank timestamp jump when PIPEDSL
  1597. * resets to its proper value. That also messes up the frame count
  1598. * when it's derived from the timestamps. So let's wait for the
  1599. * pipe to start properly before we call drm_crtc_vblank_on()
  1600. */
  1601. if (dev_priv->drm.max_vblank_count == 0)
  1602. intel_wait_for_pipe_scanline_moving(crtc);
  1603. }
  1604. static void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
  1605. {
  1606. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  1607. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1608. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  1609. enum pipe pipe = crtc->pipe;
  1610. i915_reg_t reg;
  1611. u32 val;
  1612. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1613. /*
  1614. * Make sure planes won't keep trying to pump pixels to us,
  1615. * or we might hang the display.
  1616. */
  1617. assert_planes_disabled(crtc);
  1618. reg = PIPECONF(cpu_transcoder);
  1619. val = I915_READ(reg);
  1620. if ((val & PIPECONF_ENABLE) == 0)
  1621. return;
  1622. /*
  1623. * Double wide has implications for planes
  1624. * so best keep it disabled when not needed.
  1625. */
  1626. if (old_crtc_state->double_wide)
  1627. val &= ~PIPECONF_DOUBLE_WIDE;
  1628. /* Don't disable pipe or pipe PLLs if needed */
  1629. if (!IS_I830(dev_priv))
  1630. val &= ~PIPECONF_ENABLE;
  1631. I915_WRITE(reg, val);
  1632. if ((val & PIPECONF_ENABLE) == 0)
  1633. intel_wait_for_pipe_off(old_crtc_state);
  1634. }
  1635. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1636. {
  1637. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1638. }
  1639. static unsigned int
  1640. intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
  1641. {
  1642. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1643. unsigned int cpp = fb->format->cpp[plane];
  1644. switch (fb->modifier) {
  1645. case DRM_FORMAT_MOD_LINEAR:
  1646. return cpp;
  1647. case I915_FORMAT_MOD_X_TILED:
  1648. if (IS_GEN2(dev_priv))
  1649. return 128;
  1650. else
  1651. return 512;
  1652. case I915_FORMAT_MOD_Y_TILED_CCS:
  1653. if (plane == 1)
  1654. return 128;
  1655. /* fall through */
  1656. case I915_FORMAT_MOD_Y_TILED:
  1657. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1658. return 128;
  1659. else
  1660. return 512;
  1661. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1662. if (plane == 1)
  1663. return 128;
  1664. /* fall through */
  1665. case I915_FORMAT_MOD_Yf_TILED:
  1666. switch (cpp) {
  1667. case 1:
  1668. return 64;
  1669. case 2:
  1670. case 4:
  1671. return 128;
  1672. case 8:
  1673. case 16:
  1674. return 256;
  1675. default:
  1676. MISSING_CASE(cpp);
  1677. return cpp;
  1678. }
  1679. break;
  1680. default:
  1681. MISSING_CASE(fb->modifier);
  1682. return cpp;
  1683. }
  1684. }
  1685. static unsigned int
  1686. intel_tile_height(const struct drm_framebuffer *fb, int plane)
  1687. {
  1688. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  1689. return 1;
  1690. else
  1691. return intel_tile_size(to_i915(fb->dev)) /
  1692. intel_tile_width_bytes(fb, plane);
  1693. }
  1694. /* Return the tile dimensions in pixel units */
  1695. static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
  1696. unsigned int *tile_width,
  1697. unsigned int *tile_height)
  1698. {
  1699. unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
  1700. unsigned int cpp = fb->format->cpp[plane];
  1701. *tile_width = tile_width_bytes / cpp;
  1702. *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
  1703. }
  1704. unsigned int
  1705. intel_fb_align_height(const struct drm_framebuffer *fb,
  1706. int plane, unsigned int height)
  1707. {
  1708. unsigned int tile_height = intel_tile_height(fb, plane);
  1709. return ALIGN(height, tile_height);
  1710. }
  1711. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1712. {
  1713. unsigned int size = 0;
  1714. int i;
  1715. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1716. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1717. return size;
  1718. }
  1719. static void
  1720. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1721. const struct drm_framebuffer *fb,
  1722. unsigned int rotation)
  1723. {
  1724. view->type = I915_GGTT_VIEW_NORMAL;
  1725. if (drm_rotation_90_or_270(rotation)) {
  1726. view->type = I915_GGTT_VIEW_ROTATED;
  1727. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1728. }
  1729. }
  1730. static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
  1731. {
  1732. if (IS_I830(dev_priv))
  1733. return 16 * 1024;
  1734. else if (IS_I85X(dev_priv))
  1735. return 256;
  1736. else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1737. return 32;
  1738. else
  1739. return 4 * 1024;
  1740. }
  1741. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1742. {
  1743. if (INTEL_GEN(dev_priv) >= 9)
  1744. return 256 * 1024;
  1745. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1746. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1747. return 128 * 1024;
  1748. else if (INTEL_GEN(dev_priv) >= 4)
  1749. return 4 * 1024;
  1750. else
  1751. return 0;
  1752. }
  1753. static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
  1754. int plane)
  1755. {
  1756. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1757. /* AUX_DIST needs only 4K alignment */
  1758. if (plane == 1)
  1759. return 4096;
  1760. switch (fb->modifier) {
  1761. case DRM_FORMAT_MOD_LINEAR:
  1762. return intel_linear_alignment(dev_priv);
  1763. case I915_FORMAT_MOD_X_TILED:
  1764. if (INTEL_GEN(dev_priv) >= 9)
  1765. return 256 * 1024;
  1766. return 0;
  1767. case I915_FORMAT_MOD_Y_TILED_CCS:
  1768. case I915_FORMAT_MOD_Yf_TILED_CCS:
  1769. case I915_FORMAT_MOD_Y_TILED:
  1770. case I915_FORMAT_MOD_Yf_TILED:
  1771. return 1 * 1024 * 1024;
  1772. default:
  1773. MISSING_CASE(fb->modifier);
  1774. return 0;
  1775. }
  1776. }
  1777. static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
  1778. {
  1779. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  1780. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  1781. return INTEL_GEN(dev_priv) < 4 || plane->has_fbc;
  1782. }
  1783. struct i915_vma *
  1784. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1785. unsigned int rotation,
  1786. bool uses_fence,
  1787. unsigned long *out_flags)
  1788. {
  1789. struct drm_device *dev = fb->dev;
  1790. struct drm_i915_private *dev_priv = to_i915(dev);
  1791. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1792. struct i915_ggtt_view view;
  1793. struct i915_vma *vma;
  1794. unsigned int pinctl;
  1795. u32 alignment;
  1796. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1797. alignment = intel_surf_alignment(fb, 0);
  1798. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1799. /* Note that the w/a also requires 64 PTE of padding following the
  1800. * bo. We currently fill all unused PTE with the shadow page and so
  1801. * we should always have valid PTE following the scanout preventing
  1802. * the VT-d warning.
  1803. */
  1804. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1805. alignment = 256 * 1024;
  1806. /*
  1807. * Global gtt pte registers are special registers which actually forward
  1808. * writes to a chunk of system memory. Which means that there is no risk
  1809. * that the register values disappear as soon as we call
  1810. * intel_runtime_pm_put(), so it is correct to wrap only the
  1811. * pin/unpin/fence and not more.
  1812. */
  1813. intel_runtime_pm_get(dev_priv);
  1814. atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
  1815. pinctl = 0;
  1816. /* Valleyview is definitely limited to scanning out the first
  1817. * 512MiB. Lets presume this behaviour was inherited from the
  1818. * g4x display engine and that all earlier gen are similarly
  1819. * limited. Testing suggests that it is a little more
  1820. * complicated than this. For example, Cherryview appears quite
  1821. * happy to scanout from anywhere within its global aperture.
  1822. */
  1823. if (HAS_GMCH_DISPLAY(dev_priv))
  1824. pinctl |= PIN_MAPPABLE;
  1825. vma = i915_gem_object_pin_to_display_plane(obj,
  1826. alignment, &view, pinctl);
  1827. if (IS_ERR(vma))
  1828. goto err;
  1829. if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
  1830. int ret;
  1831. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1832. * fence, whereas 965+ only requires a fence if using
  1833. * framebuffer compression. For simplicity, we always, when
  1834. * possible, install a fence as the cost is not that onerous.
  1835. *
  1836. * If we fail to fence the tiled scanout, then either the
  1837. * modeset will reject the change (which is highly unlikely as
  1838. * the affected systems, all but one, do not have unmappable
  1839. * space) or we will not be able to enable full powersaving
  1840. * techniques (also likely not to apply due to various limits
  1841. * FBC and the like impose on the size of the buffer, which
  1842. * presumably we violated anyway with this unmappable buffer).
  1843. * Anyway, it is presumably better to stumble onwards with
  1844. * something and try to run the system in a "less than optimal"
  1845. * mode that matches the user configuration.
  1846. */
  1847. ret = i915_vma_pin_fence(vma);
  1848. if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
  1849. i915_gem_object_unpin_from_display_plane(vma);
  1850. vma = ERR_PTR(ret);
  1851. goto err;
  1852. }
  1853. if (ret == 0 && vma->fence)
  1854. *out_flags |= PLANE_HAS_FENCE;
  1855. }
  1856. i915_vma_get(vma);
  1857. err:
  1858. atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
  1859. intel_runtime_pm_put(dev_priv);
  1860. return vma;
  1861. }
  1862. void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
  1863. {
  1864. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1865. if (flags & PLANE_HAS_FENCE)
  1866. i915_vma_unpin_fence(vma);
  1867. i915_gem_object_unpin_from_display_plane(vma);
  1868. i915_vma_put(vma);
  1869. }
  1870. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1871. unsigned int rotation)
  1872. {
  1873. if (drm_rotation_90_or_270(rotation))
  1874. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1875. else
  1876. return fb->pitches[plane];
  1877. }
  1878. /*
  1879. * Convert the x/y offsets into a linear offset.
  1880. * Only valid with 0/180 degree rotation, which is fine since linear
  1881. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1882. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1883. */
  1884. u32 intel_fb_xy_to_linear(int x, int y,
  1885. const struct intel_plane_state *state,
  1886. int plane)
  1887. {
  1888. const struct drm_framebuffer *fb = state->base.fb;
  1889. unsigned int cpp = fb->format->cpp[plane];
  1890. unsigned int pitch = fb->pitches[plane];
  1891. return y * pitch + x * cpp;
  1892. }
  1893. /*
  1894. * Add the x/y offsets derived from fb->offsets[] to the user
  1895. * specified plane src x/y offsets. The resulting x/y offsets
  1896. * specify the start of scanout from the beginning of the gtt mapping.
  1897. */
  1898. void intel_add_fb_offsets(int *x, int *y,
  1899. const struct intel_plane_state *state,
  1900. int plane)
  1901. {
  1902. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1903. unsigned int rotation = state->base.rotation;
  1904. if (drm_rotation_90_or_270(rotation)) {
  1905. *x += intel_fb->rotated[plane].x;
  1906. *y += intel_fb->rotated[plane].y;
  1907. } else {
  1908. *x += intel_fb->normal[plane].x;
  1909. *y += intel_fb->normal[plane].y;
  1910. }
  1911. }
  1912. static u32 __intel_adjust_tile_offset(int *x, int *y,
  1913. unsigned int tile_width,
  1914. unsigned int tile_height,
  1915. unsigned int tile_size,
  1916. unsigned int pitch_tiles,
  1917. u32 old_offset,
  1918. u32 new_offset)
  1919. {
  1920. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1921. unsigned int tiles;
  1922. WARN_ON(old_offset & (tile_size - 1));
  1923. WARN_ON(new_offset & (tile_size - 1));
  1924. WARN_ON(new_offset > old_offset);
  1925. tiles = (old_offset - new_offset) / tile_size;
  1926. *y += tiles / pitch_tiles * tile_height;
  1927. *x += tiles % pitch_tiles * tile_width;
  1928. /* minimize x in case it got needlessly big */
  1929. *y += *x / pitch_pixels * tile_height;
  1930. *x %= pitch_pixels;
  1931. return new_offset;
  1932. }
  1933. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1934. const struct drm_framebuffer *fb, int plane,
  1935. unsigned int rotation,
  1936. u32 old_offset, u32 new_offset)
  1937. {
  1938. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1939. unsigned int cpp = fb->format->cpp[plane];
  1940. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  1941. WARN_ON(new_offset > old_offset);
  1942. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  1943. unsigned int tile_size, tile_width, tile_height;
  1944. unsigned int pitch_tiles;
  1945. tile_size = intel_tile_size(dev_priv);
  1946. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  1947. if (drm_rotation_90_or_270(rotation)) {
  1948. pitch_tiles = pitch / tile_height;
  1949. swap(tile_width, tile_height);
  1950. } else {
  1951. pitch_tiles = pitch / (tile_width * cpp);
  1952. }
  1953. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  1954. tile_size, pitch_tiles,
  1955. old_offset, new_offset);
  1956. } else {
  1957. old_offset += *y * pitch + *x * cpp;
  1958. *y = (old_offset - new_offset) / pitch;
  1959. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  1960. }
  1961. return new_offset;
  1962. }
  1963. /*
  1964. * Adjust the tile offset by moving the difference into
  1965. * the x/y offsets.
  1966. */
  1967. static u32 intel_adjust_tile_offset(int *x, int *y,
  1968. const struct intel_plane_state *state, int plane,
  1969. u32 old_offset, u32 new_offset)
  1970. {
  1971. return _intel_adjust_tile_offset(x, y, state->base.fb, plane,
  1972. state->base.rotation,
  1973. old_offset, new_offset);
  1974. }
  1975. /*
  1976. * Computes the linear offset to the base tile and adjusts
  1977. * x, y. bytes per pixel is assumed to be a power-of-two.
  1978. *
  1979. * In the 90/270 rotated case, x and y are assumed
  1980. * to be already rotated to match the rotated GTT view, and
  1981. * pitch is the tile_height aligned framebuffer height.
  1982. *
  1983. * This function is used when computing the derived information
  1984. * under intel_framebuffer, so using any of that information
  1985. * here is not allowed. Anything under drm_framebuffer can be
  1986. * used. This is why the user has to pass in the pitch since it
  1987. * is specified in the rotated orientation.
  1988. */
  1989. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  1990. int *x, int *y,
  1991. const struct drm_framebuffer *fb, int plane,
  1992. unsigned int pitch,
  1993. unsigned int rotation,
  1994. u32 alignment)
  1995. {
  1996. uint64_t fb_modifier = fb->modifier;
  1997. unsigned int cpp = fb->format->cpp[plane];
  1998. u32 offset, offset_aligned;
  1999. if (alignment)
  2000. alignment--;
  2001. if (fb_modifier != DRM_FORMAT_MOD_LINEAR) {
  2002. unsigned int tile_size, tile_width, tile_height;
  2003. unsigned int tile_rows, tiles, pitch_tiles;
  2004. tile_size = intel_tile_size(dev_priv);
  2005. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  2006. if (drm_rotation_90_or_270(rotation)) {
  2007. pitch_tiles = pitch / tile_height;
  2008. swap(tile_width, tile_height);
  2009. } else {
  2010. pitch_tiles = pitch / (tile_width * cpp);
  2011. }
  2012. tile_rows = *y / tile_height;
  2013. *y %= tile_height;
  2014. tiles = *x / tile_width;
  2015. *x %= tile_width;
  2016. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2017. offset_aligned = offset & ~alignment;
  2018. __intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2019. tile_size, pitch_tiles,
  2020. offset, offset_aligned);
  2021. } else {
  2022. offset = *y * pitch + *x * cpp;
  2023. offset_aligned = offset & ~alignment;
  2024. *y = (offset & alignment) / pitch;
  2025. *x = ((offset & alignment) - *y * pitch) / cpp;
  2026. }
  2027. return offset_aligned;
  2028. }
  2029. u32 intel_compute_tile_offset(int *x, int *y,
  2030. const struct intel_plane_state *state,
  2031. int plane)
  2032. {
  2033. struct intel_plane *intel_plane = to_intel_plane(state->base.plane);
  2034. struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
  2035. const struct drm_framebuffer *fb = state->base.fb;
  2036. unsigned int rotation = state->base.rotation;
  2037. int pitch = intel_fb_pitch(fb, plane, rotation);
  2038. u32 alignment;
  2039. if (intel_plane->id == PLANE_CURSOR)
  2040. alignment = intel_cursor_alignment(dev_priv);
  2041. else
  2042. alignment = intel_surf_alignment(fb, plane);
  2043. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2044. rotation, alignment);
  2045. }
  2046. /* Convert the fb->offset[] into x/y offsets */
  2047. static int intel_fb_offset_to_xy(int *x, int *y,
  2048. const struct drm_framebuffer *fb, int plane)
  2049. {
  2050. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2051. if (fb->modifier != DRM_FORMAT_MOD_LINEAR &&
  2052. fb->offsets[plane] % intel_tile_size(dev_priv))
  2053. return -EINVAL;
  2054. *x = 0;
  2055. *y = 0;
  2056. _intel_adjust_tile_offset(x, y,
  2057. fb, plane, DRM_MODE_ROTATE_0,
  2058. fb->offsets[plane], 0);
  2059. return 0;
  2060. }
  2061. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2062. {
  2063. switch (fb_modifier) {
  2064. case I915_FORMAT_MOD_X_TILED:
  2065. return I915_TILING_X;
  2066. case I915_FORMAT_MOD_Y_TILED:
  2067. case I915_FORMAT_MOD_Y_TILED_CCS:
  2068. return I915_TILING_Y;
  2069. default:
  2070. return I915_TILING_NONE;
  2071. }
  2072. }
  2073. /*
  2074. * From the Sky Lake PRM:
  2075. * "The Color Control Surface (CCS) contains the compression status of
  2076. * the cache-line pairs. The compression state of the cache-line pair
  2077. * is specified by 2 bits in the CCS. Each CCS cache-line represents
  2078. * an area on the main surface of 16 x16 sets of 128 byte Y-tiled
  2079. * cache-line-pairs. CCS is always Y tiled."
  2080. *
  2081. * Since cache line pairs refers to horizontally adjacent cache lines,
  2082. * each cache line in the CCS corresponds to an area of 32x16 cache
  2083. * lines on the main surface. Since each pixel is 4 bytes, this gives
  2084. * us a ratio of one byte in the CCS for each 8x16 pixels in the
  2085. * main surface.
  2086. */
  2087. static const struct drm_format_info ccs_formats[] = {
  2088. { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2089. { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2090. { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2091. { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2, .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
  2092. };
  2093. static const struct drm_format_info *
  2094. lookup_format_info(const struct drm_format_info formats[],
  2095. int num_formats, u32 format)
  2096. {
  2097. int i;
  2098. for (i = 0; i < num_formats; i++) {
  2099. if (formats[i].format == format)
  2100. return &formats[i];
  2101. }
  2102. return NULL;
  2103. }
  2104. static const struct drm_format_info *
  2105. intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
  2106. {
  2107. switch (cmd->modifier[0]) {
  2108. case I915_FORMAT_MOD_Y_TILED_CCS:
  2109. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2110. return lookup_format_info(ccs_formats,
  2111. ARRAY_SIZE(ccs_formats),
  2112. cmd->pixel_format);
  2113. default:
  2114. return NULL;
  2115. }
  2116. }
  2117. static int
  2118. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2119. struct drm_framebuffer *fb)
  2120. {
  2121. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2122. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2123. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2124. u32 gtt_offset_rotated = 0;
  2125. unsigned int max_size = 0;
  2126. int i, num_planes = fb->format->num_planes;
  2127. unsigned int tile_size = intel_tile_size(dev_priv);
  2128. for (i = 0; i < num_planes; i++) {
  2129. unsigned int width, height;
  2130. unsigned int cpp, size;
  2131. u32 offset;
  2132. int x, y;
  2133. int ret;
  2134. cpp = fb->format->cpp[i];
  2135. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2136. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2137. ret = intel_fb_offset_to_xy(&x, &y, fb, i);
  2138. if (ret) {
  2139. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2140. i, fb->offsets[i]);
  2141. return ret;
  2142. }
  2143. if ((fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2144. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) && i == 1) {
  2145. int hsub = fb->format->hsub;
  2146. int vsub = fb->format->vsub;
  2147. int tile_width, tile_height;
  2148. int main_x, main_y;
  2149. int ccs_x, ccs_y;
  2150. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2151. tile_width *= hsub;
  2152. tile_height *= vsub;
  2153. ccs_x = (x * hsub) % tile_width;
  2154. ccs_y = (y * vsub) % tile_height;
  2155. main_x = intel_fb->normal[0].x % tile_width;
  2156. main_y = intel_fb->normal[0].y % tile_height;
  2157. /*
  2158. * CCS doesn't have its own x/y offset register, so the intra CCS tile
  2159. * x/y offsets must match between CCS and the main surface.
  2160. */
  2161. if (main_x != ccs_x || main_y != ccs_y) {
  2162. DRM_DEBUG_KMS("Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
  2163. main_x, main_y,
  2164. ccs_x, ccs_y,
  2165. intel_fb->normal[0].x,
  2166. intel_fb->normal[0].y,
  2167. x, y);
  2168. return -EINVAL;
  2169. }
  2170. }
  2171. /*
  2172. * The fence (if used) is aligned to the start of the object
  2173. * so having the framebuffer wrap around across the edge of the
  2174. * fenced region doesn't really work. We have no API to configure
  2175. * the fence start offset within the object (nor could we probably
  2176. * on gen2/3). So it's just easier if we just require that the
  2177. * fb layout agrees with the fence layout. We already check that the
  2178. * fb stride matches the fence stride elsewhere.
  2179. */
  2180. if (i == 0 && i915_gem_object_is_tiled(obj) &&
  2181. (x + width) * cpp > fb->pitches[i]) {
  2182. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2183. i, fb->offsets[i]);
  2184. return -EINVAL;
  2185. }
  2186. /*
  2187. * First pixel of the framebuffer from
  2188. * the start of the normal gtt mapping.
  2189. */
  2190. intel_fb->normal[i].x = x;
  2191. intel_fb->normal[i].y = y;
  2192. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2193. fb, i, fb->pitches[i],
  2194. DRM_MODE_ROTATE_0, tile_size);
  2195. offset /= tile_size;
  2196. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  2197. unsigned int tile_width, tile_height;
  2198. unsigned int pitch_tiles;
  2199. struct drm_rect r;
  2200. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2201. rot_info->plane[i].offset = offset;
  2202. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2203. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2204. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2205. intel_fb->rotated[i].pitch =
  2206. rot_info->plane[i].height * tile_height;
  2207. /* how many tiles does this plane need */
  2208. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2209. /*
  2210. * If the plane isn't horizontally tile aligned,
  2211. * we need one more tile.
  2212. */
  2213. if (x != 0)
  2214. size++;
  2215. /* rotate the x/y offsets to match the GTT view */
  2216. r.x1 = x;
  2217. r.y1 = y;
  2218. r.x2 = x + width;
  2219. r.y2 = y + height;
  2220. drm_rect_rotate(&r,
  2221. rot_info->plane[i].width * tile_width,
  2222. rot_info->plane[i].height * tile_height,
  2223. DRM_MODE_ROTATE_270);
  2224. x = r.x1;
  2225. y = r.y1;
  2226. /* rotate the tile dimensions to match the GTT view */
  2227. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2228. swap(tile_width, tile_height);
  2229. /*
  2230. * We only keep the x/y offsets, so push all of the
  2231. * gtt offset into the x/y offsets.
  2232. */
  2233. __intel_adjust_tile_offset(&x, &y,
  2234. tile_width, tile_height,
  2235. tile_size, pitch_tiles,
  2236. gtt_offset_rotated * tile_size, 0);
  2237. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2238. /*
  2239. * First pixel of the framebuffer from
  2240. * the start of the rotated gtt mapping.
  2241. */
  2242. intel_fb->rotated[i].x = x;
  2243. intel_fb->rotated[i].y = y;
  2244. } else {
  2245. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2246. x * cpp, tile_size);
  2247. }
  2248. /* how many tiles in total needed in the bo */
  2249. max_size = max(max_size, offset + size);
  2250. }
  2251. if (max_size * tile_size > obj->base.size) {
  2252. DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2253. max_size * tile_size, obj->base.size);
  2254. return -EINVAL;
  2255. }
  2256. return 0;
  2257. }
  2258. static int i9xx_format_to_fourcc(int format)
  2259. {
  2260. switch (format) {
  2261. case DISPPLANE_8BPP:
  2262. return DRM_FORMAT_C8;
  2263. case DISPPLANE_BGRX555:
  2264. return DRM_FORMAT_XRGB1555;
  2265. case DISPPLANE_BGRX565:
  2266. return DRM_FORMAT_RGB565;
  2267. default:
  2268. case DISPPLANE_BGRX888:
  2269. return DRM_FORMAT_XRGB8888;
  2270. case DISPPLANE_RGBX888:
  2271. return DRM_FORMAT_XBGR8888;
  2272. case DISPPLANE_BGRX101010:
  2273. return DRM_FORMAT_XRGB2101010;
  2274. case DISPPLANE_RGBX101010:
  2275. return DRM_FORMAT_XBGR2101010;
  2276. }
  2277. }
  2278. int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2279. {
  2280. switch (format) {
  2281. case PLANE_CTL_FORMAT_RGB_565:
  2282. return DRM_FORMAT_RGB565;
  2283. case PLANE_CTL_FORMAT_NV12:
  2284. return DRM_FORMAT_NV12;
  2285. default:
  2286. case PLANE_CTL_FORMAT_XRGB_8888:
  2287. if (rgb_order) {
  2288. if (alpha)
  2289. return DRM_FORMAT_ABGR8888;
  2290. else
  2291. return DRM_FORMAT_XBGR8888;
  2292. } else {
  2293. if (alpha)
  2294. return DRM_FORMAT_ARGB8888;
  2295. else
  2296. return DRM_FORMAT_XRGB8888;
  2297. }
  2298. case PLANE_CTL_FORMAT_XRGB_2101010:
  2299. if (rgb_order)
  2300. return DRM_FORMAT_XBGR2101010;
  2301. else
  2302. return DRM_FORMAT_XRGB2101010;
  2303. }
  2304. }
  2305. static bool
  2306. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2307. struct intel_initial_plane_config *plane_config)
  2308. {
  2309. struct drm_device *dev = crtc->base.dev;
  2310. struct drm_i915_private *dev_priv = to_i915(dev);
  2311. struct drm_i915_gem_object *obj = NULL;
  2312. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2313. struct drm_framebuffer *fb = &plane_config->fb->base;
  2314. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2315. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2316. PAGE_SIZE);
  2317. size_aligned -= base_aligned;
  2318. if (plane_config->size == 0)
  2319. return false;
  2320. /* If the FB is too big, just don't use it since fbdev is not very
  2321. * important and we should probably use that space with FBC or other
  2322. * features. */
  2323. if (size_aligned * 2 > dev_priv->stolen_usable_size)
  2324. return false;
  2325. mutex_lock(&dev->struct_mutex);
  2326. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2327. base_aligned,
  2328. base_aligned,
  2329. size_aligned);
  2330. mutex_unlock(&dev->struct_mutex);
  2331. if (!obj)
  2332. return false;
  2333. if (plane_config->tiling == I915_TILING_X)
  2334. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2335. mode_cmd.pixel_format = fb->format->format;
  2336. mode_cmd.width = fb->width;
  2337. mode_cmd.height = fb->height;
  2338. mode_cmd.pitches[0] = fb->pitches[0];
  2339. mode_cmd.modifier[0] = fb->modifier;
  2340. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2341. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2342. DRM_DEBUG_KMS("intel fb init failed\n");
  2343. goto out_unref_obj;
  2344. }
  2345. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2346. return true;
  2347. out_unref_obj:
  2348. i915_gem_object_put(obj);
  2349. return false;
  2350. }
  2351. static void
  2352. intel_set_plane_visible(struct intel_crtc_state *crtc_state,
  2353. struct intel_plane_state *plane_state,
  2354. bool visible)
  2355. {
  2356. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2357. plane_state->base.visible = visible;
  2358. /* FIXME pre-g4x don't work like this */
  2359. if (visible) {
  2360. crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
  2361. crtc_state->active_planes |= BIT(plane->id);
  2362. } else {
  2363. crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
  2364. crtc_state->active_planes &= ~BIT(plane->id);
  2365. }
  2366. DRM_DEBUG_KMS("%s active planes 0x%x\n",
  2367. crtc_state->base.crtc->name,
  2368. crtc_state->active_planes);
  2369. }
  2370. static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
  2371. struct intel_plane *plane)
  2372. {
  2373. struct intel_crtc_state *crtc_state =
  2374. to_intel_crtc_state(crtc->base.state);
  2375. struct intel_plane_state *plane_state =
  2376. to_intel_plane_state(plane->base.state);
  2377. intel_set_plane_visible(crtc_state, plane_state, false);
  2378. if (plane->id == PLANE_PRIMARY)
  2379. intel_pre_disable_primary_noatomic(&crtc->base);
  2380. trace_intel_disable_plane(&plane->base, crtc);
  2381. plane->disable_plane(plane, crtc);
  2382. }
  2383. static void
  2384. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2385. struct intel_initial_plane_config *plane_config)
  2386. {
  2387. struct drm_device *dev = intel_crtc->base.dev;
  2388. struct drm_i915_private *dev_priv = to_i915(dev);
  2389. struct drm_crtc *c;
  2390. struct drm_i915_gem_object *obj;
  2391. struct drm_plane *primary = intel_crtc->base.primary;
  2392. struct drm_plane_state *plane_state = primary->state;
  2393. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2394. struct intel_plane *intel_plane = to_intel_plane(primary);
  2395. struct intel_plane_state *intel_state =
  2396. to_intel_plane_state(plane_state);
  2397. struct drm_framebuffer *fb;
  2398. if (!plane_config->fb)
  2399. return;
  2400. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2401. fb = &plane_config->fb->base;
  2402. goto valid_fb;
  2403. }
  2404. kfree(plane_config->fb);
  2405. /*
  2406. * Failed to alloc the obj, check to see if we should share
  2407. * an fb with another CRTC instead
  2408. */
  2409. for_each_crtc(dev, c) {
  2410. struct intel_plane_state *state;
  2411. if (c == &intel_crtc->base)
  2412. continue;
  2413. if (!to_intel_crtc(c)->active)
  2414. continue;
  2415. state = to_intel_plane_state(c->primary->state);
  2416. if (!state->vma)
  2417. continue;
  2418. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2419. fb = state->base.fb;
  2420. drm_framebuffer_get(fb);
  2421. goto valid_fb;
  2422. }
  2423. }
  2424. /*
  2425. * We've failed to reconstruct the BIOS FB. Current display state
  2426. * indicates that the primary plane is visible, but has a NULL FB,
  2427. * which will lead to problems later if we don't fix it up. The
  2428. * simplest solution is to just disable the primary plane now and
  2429. * pretend the BIOS never had it enabled.
  2430. */
  2431. intel_plane_disable_noatomic(intel_crtc, intel_plane);
  2432. return;
  2433. valid_fb:
  2434. mutex_lock(&dev->struct_mutex);
  2435. intel_state->vma =
  2436. intel_pin_and_fence_fb_obj(fb,
  2437. primary->state->rotation,
  2438. intel_plane_uses_fence(intel_state),
  2439. &intel_state->flags);
  2440. mutex_unlock(&dev->struct_mutex);
  2441. if (IS_ERR(intel_state->vma)) {
  2442. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2443. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2444. intel_state->vma = NULL;
  2445. drm_framebuffer_put(fb);
  2446. return;
  2447. }
  2448. obj = intel_fb_obj(fb);
  2449. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  2450. plane_state->src_x = 0;
  2451. plane_state->src_y = 0;
  2452. plane_state->src_w = fb->width << 16;
  2453. plane_state->src_h = fb->height << 16;
  2454. plane_state->crtc_x = 0;
  2455. plane_state->crtc_y = 0;
  2456. plane_state->crtc_w = fb->width;
  2457. plane_state->crtc_h = fb->height;
  2458. intel_state->base.src = drm_plane_state_src(plane_state);
  2459. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2460. if (i915_gem_object_is_tiled(obj))
  2461. dev_priv->preserve_bios_swizzle = true;
  2462. plane_state->fb = fb;
  2463. plane_state->crtc = &intel_crtc->base;
  2464. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2465. to_intel_plane_state(plane_state),
  2466. true);
  2467. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2468. &obj->frontbuffer_bits);
  2469. }
  2470. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2471. unsigned int rotation)
  2472. {
  2473. int cpp = fb->format->cpp[plane];
  2474. switch (fb->modifier) {
  2475. case DRM_FORMAT_MOD_LINEAR:
  2476. case I915_FORMAT_MOD_X_TILED:
  2477. switch (cpp) {
  2478. case 8:
  2479. return 4096;
  2480. case 4:
  2481. case 2:
  2482. case 1:
  2483. return 8192;
  2484. default:
  2485. MISSING_CASE(cpp);
  2486. break;
  2487. }
  2488. break;
  2489. case I915_FORMAT_MOD_Y_TILED_CCS:
  2490. case I915_FORMAT_MOD_Yf_TILED_CCS:
  2491. /* FIXME AUX plane? */
  2492. case I915_FORMAT_MOD_Y_TILED:
  2493. case I915_FORMAT_MOD_Yf_TILED:
  2494. switch (cpp) {
  2495. case 8:
  2496. return 2048;
  2497. case 4:
  2498. return 4096;
  2499. case 2:
  2500. case 1:
  2501. return 8192;
  2502. default:
  2503. MISSING_CASE(cpp);
  2504. break;
  2505. }
  2506. break;
  2507. default:
  2508. MISSING_CASE(fb->modifier);
  2509. }
  2510. return 2048;
  2511. }
  2512. static bool skl_check_main_ccs_coordinates(struct intel_plane_state *plane_state,
  2513. int main_x, int main_y, u32 main_offset)
  2514. {
  2515. const struct drm_framebuffer *fb = plane_state->base.fb;
  2516. int hsub = fb->format->hsub;
  2517. int vsub = fb->format->vsub;
  2518. int aux_x = plane_state->aux.x;
  2519. int aux_y = plane_state->aux.y;
  2520. u32 aux_offset = plane_state->aux.offset;
  2521. u32 alignment = intel_surf_alignment(fb, 1);
  2522. while (aux_offset >= main_offset && aux_y <= main_y) {
  2523. int x, y;
  2524. if (aux_x == main_x && aux_y == main_y)
  2525. break;
  2526. if (aux_offset == 0)
  2527. break;
  2528. x = aux_x / hsub;
  2529. y = aux_y / vsub;
  2530. aux_offset = intel_adjust_tile_offset(&x, &y, plane_state, 1,
  2531. aux_offset, aux_offset - alignment);
  2532. aux_x = x * hsub + aux_x % hsub;
  2533. aux_y = y * vsub + aux_y % vsub;
  2534. }
  2535. if (aux_x != main_x || aux_y != main_y)
  2536. return false;
  2537. plane_state->aux.offset = aux_offset;
  2538. plane_state->aux.x = aux_x;
  2539. plane_state->aux.y = aux_y;
  2540. return true;
  2541. }
  2542. static int skl_check_main_surface(const struct intel_crtc_state *crtc_state,
  2543. struct intel_plane_state *plane_state)
  2544. {
  2545. struct drm_i915_private *dev_priv =
  2546. to_i915(plane_state->base.plane->dev);
  2547. const struct drm_framebuffer *fb = plane_state->base.fb;
  2548. unsigned int rotation = plane_state->base.rotation;
  2549. int x = plane_state->base.src.x1 >> 16;
  2550. int y = plane_state->base.src.y1 >> 16;
  2551. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2552. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2553. int dst_x = plane_state->base.dst.x1;
  2554. int pipe_src_w = crtc_state->pipe_src_w;
  2555. int max_width = skl_max_plane_width(fb, 0, rotation);
  2556. int max_height = 4096;
  2557. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2558. if (w > max_width || h > max_height) {
  2559. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2560. w, h, max_width, max_height);
  2561. return -EINVAL;
  2562. }
  2563. /*
  2564. * Display WA #1175: cnl,glk
  2565. * Planes other than the cursor may cause FIFO underflow and display
  2566. * corruption if starting less than 4 pixels from the right edge of
  2567. * the screen.
  2568. * Besides the above WA fix the similar problem, where planes other
  2569. * than the cursor ending less than 4 pixels from the left edge of the
  2570. * screen may cause FIFO underflow and display corruption.
  2571. */
  2572. if ((IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
  2573. (dst_x + w < 4 || dst_x > pipe_src_w - 4)) {
  2574. DRM_DEBUG_KMS("requested plane X %s position %d invalid (valid range %d-%d)\n",
  2575. dst_x + w < 4 ? "end" : "start",
  2576. dst_x + w < 4 ? dst_x + w : dst_x,
  2577. 4, pipe_src_w - 4);
  2578. return -ERANGE;
  2579. }
  2580. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2581. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2582. alignment = intel_surf_alignment(fb, 0);
  2583. /*
  2584. * AUX surface offset is specified as the distance from the
  2585. * main surface offset, and it must be non-negative. Make
  2586. * sure that is what we will get.
  2587. */
  2588. if (offset > aux_offset)
  2589. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2590. offset, aux_offset & ~(alignment - 1));
  2591. /*
  2592. * When using an X-tiled surface, the plane blows up
  2593. * if the x offset + width exceed the stride.
  2594. *
  2595. * TODO: linear and Y-tiled seem fine, Yf untested,
  2596. */
  2597. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2598. int cpp = fb->format->cpp[0];
  2599. while ((x + w) * cpp > fb->pitches[0]) {
  2600. if (offset == 0) {
  2601. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to X-tiling\n");
  2602. return -EINVAL;
  2603. }
  2604. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2605. offset, offset - alignment);
  2606. }
  2607. }
  2608. /*
  2609. * CCS AUX surface doesn't have its own x/y offsets, we must make sure
  2610. * they match with the main surface x/y offsets.
  2611. */
  2612. if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2613. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2614. while (!skl_check_main_ccs_coordinates(plane_state, x, y, offset)) {
  2615. if (offset == 0)
  2616. break;
  2617. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2618. offset, offset - alignment);
  2619. }
  2620. if (x != plane_state->aux.x || y != plane_state->aux.y) {
  2621. DRM_DEBUG_KMS("Unable to find suitable display surface offset due to CCS\n");
  2622. return -EINVAL;
  2623. }
  2624. }
  2625. plane_state->main.offset = offset;
  2626. plane_state->main.x = x;
  2627. plane_state->main.y = y;
  2628. return 0;
  2629. }
  2630. static int
  2631. skl_check_nv12_surface(const struct intel_crtc_state *crtc_state,
  2632. struct intel_plane_state *plane_state)
  2633. {
  2634. /* Display WA #1106 */
  2635. if (plane_state->base.rotation !=
  2636. (DRM_MODE_REFLECT_X | DRM_MODE_ROTATE_90) &&
  2637. plane_state->base.rotation != DRM_MODE_ROTATE_270)
  2638. return 0;
  2639. /*
  2640. * src coordinates are rotated here.
  2641. * We check height but report it as width
  2642. */
  2643. if (((drm_rect_height(&plane_state->base.src) >> 16) % 4) != 0) {
  2644. DRM_DEBUG_KMS("src width must be multiple "
  2645. "of 4 for rotated NV12\n");
  2646. return -EINVAL;
  2647. }
  2648. return 0;
  2649. }
  2650. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2651. {
  2652. const struct drm_framebuffer *fb = plane_state->base.fb;
  2653. unsigned int rotation = plane_state->base.rotation;
  2654. int max_width = skl_max_plane_width(fb, 1, rotation);
  2655. int max_height = 4096;
  2656. int x = plane_state->base.src.x1 >> 17;
  2657. int y = plane_state->base.src.y1 >> 17;
  2658. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2659. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2660. u32 offset;
  2661. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2662. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2663. /* FIXME not quite sure how/if these apply to the chroma plane */
  2664. if (w > max_width || h > max_height) {
  2665. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2666. w, h, max_width, max_height);
  2667. return -EINVAL;
  2668. }
  2669. plane_state->aux.offset = offset;
  2670. plane_state->aux.x = x;
  2671. plane_state->aux.y = y;
  2672. return 0;
  2673. }
  2674. static int skl_check_ccs_aux_surface(struct intel_plane_state *plane_state)
  2675. {
  2676. const struct drm_framebuffer *fb = plane_state->base.fb;
  2677. int src_x = plane_state->base.src.x1 >> 16;
  2678. int src_y = plane_state->base.src.y1 >> 16;
  2679. int hsub = fb->format->hsub;
  2680. int vsub = fb->format->vsub;
  2681. int x = src_x / hsub;
  2682. int y = src_y / vsub;
  2683. u32 offset;
  2684. if (plane_state->base.rotation & ~(DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180)) {
  2685. DRM_DEBUG_KMS("RC support only with 0/180 degree rotation %x\n",
  2686. plane_state->base.rotation);
  2687. return -EINVAL;
  2688. }
  2689. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2690. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2691. plane_state->aux.offset = offset;
  2692. plane_state->aux.x = x * hsub + src_x % hsub;
  2693. plane_state->aux.y = y * vsub + src_y % vsub;
  2694. return 0;
  2695. }
  2696. int skl_check_plane_surface(const struct intel_crtc_state *crtc_state,
  2697. struct intel_plane_state *plane_state)
  2698. {
  2699. const struct drm_framebuffer *fb = plane_state->base.fb;
  2700. unsigned int rotation = plane_state->base.rotation;
  2701. int ret;
  2702. if (rotation & DRM_MODE_REFLECT_X &&
  2703. fb->modifier == DRM_FORMAT_MOD_LINEAR) {
  2704. DRM_DEBUG_KMS("horizontal flip is not supported with linear surface formats\n");
  2705. return -EINVAL;
  2706. }
  2707. if (!plane_state->base.visible)
  2708. return 0;
  2709. /* Rotate src coordinates to match rotated GTT view */
  2710. if (drm_rotation_90_or_270(rotation))
  2711. drm_rect_rotate(&plane_state->base.src,
  2712. fb->width << 16, fb->height << 16,
  2713. DRM_MODE_ROTATE_270);
  2714. /*
  2715. * Handle the AUX surface first since
  2716. * the main surface setup depends on it.
  2717. */
  2718. if (fb->format->format == DRM_FORMAT_NV12) {
  2719. ret = skl_check_nv12_surface(crtc_state, plane_state);
  2720. if (ret)
  2721. return ret;
  2722. ret = skl_check_nv12_aux_surface(plane_state);
  2723. if (ret)
  2724. return ret;
  2725. } else if (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  2726. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS) {
  2727. ret = skl_check_ccs_aux_surface(plane_state);
  2728. if (ret)
  2729. return ret;
  2730. } else {
  2731. plane_state->aux.offset = ~0xfff;
  2732. plane_state->aux.x = 0;
  2733. plane_state->aux.y = 0;
  2734. }
  2735. ret = skl_check_main_surface(crtc_state, plane_state);
  2736. if (ret)
  2737. return ret;
  2738. return 0;
  2739. }
  2740. static u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
  2741. const struct intel_plane_state *plane_state)
  2742. {
  2743. struct drm_i915_private *dev_priv =
  2744. to_i915(plane_state->base.plane->dev);
  2745. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2746. const struct drm_framebuffer *fb = plane_state->base.fb;
  2747. unsigned int rotation = plane_state->base.rotation;
  2748. u32 dspcntr;
  2749. dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
  2750. if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
  2751. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2752. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2753. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2754. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2755. if (INTEL_GEN(dev_priv) < 5)
  2756. dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
  2757. switch (fb->format->format) {
  2758. case DRM_FORMAT_C8:
  2759. dspcntr |= DISPPLANE_8BPP;
  2760. break;
  2761. case DRM_FORMAT_XRGB1555:
  2762. dspcntr |= DISPPLANE_BGRX555;
  2763. break;
  2764. case DRM_FORMAT_RGB565:
  2765. dspcntr |= DISPPLANE_BGRX565;
  2766. break;
  2767. case DRM_FORMAT_XRGB8888:
  2768. dspcntr |= DISPPLANE_BGRX888;
  2769. break;
  2770. case DRM_FORMAT_XBGR8888:
  2771. dspcntr |= DISPPLANE_RGBX888;
  2772. break;
  2773. case DRM_FORMAT_XRGB2101010:
  2774. dspcntr |= DISPPLANE_BGRX101010;
  2775. break;
  2776. case DRM_FORMAT_XBGR2101010:
  2777. dspcntr |= DISPPLANE_RGBX101010;
  2778. break;
  2779. default:
  2780. MISSING_CASE(fb->format->format);
  2781. return 0;
  2782. }
  2783. if (INTEL_GEN(dev_priv) >= 4 &&
  2784. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2785. dspcntr |= DISPPLANE_TILED;
  2786. if (rotation & DRM_MODE_ROTATE_180)
  2787. dspcntr |= DISPPLANE_ROTATE_180;
  2788. if (rotation & DRM_MODE_REFLECT_X)
  2789. dspcntr |= DISPPLANE_MIRROR;
  2790. return dspcntr;
  2791. }
  2792. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2793. {
  2794. struct drm_i915_private *dev_priv =
  2795. to_i915(plane_state->base.plane->dev);
  2796. int src_x = plane_state->base.src.x1 >> 16;
  2797. int src_y = plane_state->base.src.y1 >> 16;
  2798. u32 offset;
  2799. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2800. if (INTEL_GEN(dev_priv) >= 4)
  2801. offset = intel_compute_tile_offset(&src_x, &src_y,
  2802. plane_state, 0);
  2803. else
  2804. offset = 0;
  2805. /* HSW/BDW do this automagically in hardware */
  2806. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2807. unsigned int rotation = plane_state->base.rotation;
  2808. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2809. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2810. if (rotation & DRM_MODE_ROTATE_180) {
  2811. src_x += src_w - 1;
  2812. src_y += src_h - 1;
  2813. } else if (rotation & DRM_MODE_REFLECT_X) {
  2814. src_x += src_w - 1;
  2815. }
  2816. }
  2817. plane_state->main.offset = offset;
  2818. plane_state->main.x = src_x;
  2819. plane_state->main.y = src_y;
  2820. return 0;
  2821. }
  2822. static void i9xx_update_plane(struct intel_plane *plane,
  2823. const struct intel_crtc_state *crtc_state,
  2824. const struct intel_plane_state *plane_state)
  2825. {
  2826. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2827. const struct drm_framebuffer *fb = plane_state->base.fb;
  2828. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2829. u32 linear_offset;
  2830. u32 dspcntr = plane_state->ctl;
  2831. i915_reg_t reg = DSPCNTR(i9xx_plane);
  2832. int x = plane_state->main.x;
  2833. int y = plane_state->main.y;
  2834. unsigned long irqflags;
  2835. u32 dspaddr_offset;
  2836. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2837. if (INTEL_GEN(dev_priv) >= 4)
  2838. dspaddr_offset = plane_state->main.offset;
  2839. else
  2840. dspaddr_offset = linear_offset;
  2841. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2842. if (INTEL_GEN(dev_priv) < 4) {
  2843. /* pipesrc and dspsize control the size that is scaled from,
  2844. * which should always be the user's requested size.
  2845. */
  2846. I915_WRITE_FW(DSPSIZE(i9xx_plane),
  2847. ((crtc_state->pipe_src_h - 1) << 16) |
  2848. (crtc_state->pipe_src_w - 1));
  2849. I915_WRITE_FW(DSPPOS(i9xx_plane), 0);
  2850. } else if (IS_CHERRYVIEW(dev_priv) && i9xx_plane == PLANE_B) {
  2851. I915_WRITE_FW(PRIMSIZE(i9xx_plane),
  2852. ((crtc_state->pipe_src_h - 1) << 16) |
  2853. (crtc_state->pipe_src_w - 1));
  2854. I915_WRITE_FW(PRIMPOS(i9xx_plane), 0);
  2855. I915_WRITE_FW(PRIMCNSTALPHA(i9xx_plane), 0);
  2856. }
  2857. I915_WRITE_FW(reg, dspcntr);
  2858. I915_WRITE_FW(DSPSTRIDE(i9xx_plane), fb->pitches[0]);
  2859. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2860. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2861. intel_plane_ggtt_offset(plane_state) +
  2862. dspaddr_offset);
  2863. I915_WRITE_FW(DSPOFFSET(i9xx_plane), (y << 16) | x);
  2864. } else if (INTEL_GEN(dev_priv) >= 4) {
  2865. I915_WRITE_FW(DSPSURF(i9xx_plane),
  2866. intel_plane_ggtt_offset(plane_state) +
  2867. dspaddr_offset);
  2868. I915_WRITE_FW(DSPTILEOFF(i9xx_plane), (y << 16) | x);
  2869. I915_WRITE_FW(DSPLINOFF(i9xx_plane), linear_offset);
  2870. } else {
  2871. I915_WRITE_FW(DSPADDR(i9xx_plane),
  2872. intel_plane_ggtt_offset(plane_state) +
  2873. dspaddr_offset);
  2874. }
  2875. POSTING_READ_FW(reg);
  2876. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2877. }
  2878. static void i9xx_disable_plane(struct intel_plane *plane,
  2879. struct intel_crtc *crtc)
  2880. {
  2881. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2882. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2883. unsigned long irqflags;
  2884. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2885. I915_WRITE_FW(DSPCNTR(i9xx_plane), 0);
  2886. if (INTEL_GEN(dev_priv) >= 4)
  2887. I915_WRITE_FW(DSPSURF(i9xx_plane), 0);
  2888. else
  2889. I915_WRITE_FW(DSPADDR(i9xx_plane), 0);
  2890. POSTING_READ_FW(DSPCNTR(i9xx_plane));
  2891. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2892. }
  2893. static bool i9xx_plane_get_hw_state(struct intel_plane *plane,
  2894. enum pipe *pipe)
  2895. {
  2896. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2897. enum intel_display_power_domain power_domain;
  2898. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  2899. bool ret;
  2900. u32 val;
  2901. /*
  2902. * Not 100% correct for planes that can move between pipes,
  2903. * but that's only the case for gen2-4 which don't have any
  2904. * display power wells.
  2905. */
  2906. power_domain = POWER_DOMAIN_PIPE(plane->pipe);
  2907. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  2908. return false;
  2909. val = I915_READ(DSPCNTR(i9xx_plane));
  2910. ret = val & DISPLAY_PLANE_ENABLE;
  2911. if (INTEL_GEN(dev_priv) >= 5)
  2912. *pipe = plane->pipe;
  2913. else
  2914. *pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  2915. DISPPLANE_SEL_PIPE_SHIFT;
  2916. intel_display_power_put(dev_priv, power_domain);
  2917. return ret;
  2918. }
  2919. static u32
  2920. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2921. {
  2922. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2923. return 64;
  2924. else
  2925. return intel_tile_width_bytes(fb, plane);
  2926. }
  2927. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2928. {
  2929. struct drm_device *dev = intel_crtc->base.dev;
  2930. struct drm_i915_private *dev_priv = to_i915(dev);
  2931. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2932. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2933. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2934. }
  2935. /*
  2936. * This function detaches (aka. unbinds) unused scalers in hardware
  2937. */
  2938. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2939. {
  2940. struct intel_crtc_scaler_state *scaler_state;
  2941. int i;
  2942. scaler_state = &intel_crtc->config->scaler_state;
  2943. /* loop through and disable scalers that aren't in use */
  2944. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2945. if (!scaler_state->scalers[i].in_use)
  2946. skl_detach_scaler(intel_crtc, i);
  2947. }
  2948. }
  2949. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2950. unsigned int rotation)
  2951. {
  2952. u32 stride;
  2953. if (plane >= fb->format->num_planes)
  2954. return 0;
  2955. stride = intel_fb_pitch(fb, plane, rotation);
  2956. /*
  2957. * The stride is either expressed as a multiple of 64 bytes chunks for
  2958. * linear buffers or in number of tiles for tiled buffers.
  2959. */
  2960. if (drm_rotation_90_or_270(rotation))
  2961. stride /= intel_tile_height(fb, plane);
  2962. else
  2963. stride /= intel_fb_stride_alignment(fb, plane);
  2964. return stride;
  2965. }
  2966. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  2967. {
  2968. switch (pixel_format) {
  2969. case DRM_FORMAT_C8:
  2970. return PLANE_CTL_FORMAT_INDEXED;
  2971. case DRM_FORMAT_RGB565:
  2972. return PLANE_CTL_FORMAT_RGB_565;
  2973. case DRM_FORMAT_XBGR8888:
  2974. case DRM_FORMAT_ABGR8888:
  2975. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2976. case DRM_FORMAT_XRGB8888:
  2977. case DRM_FORMAT_ARGB8888:
  2978. return PLANE_CTL_FORMAT_XRGB_8888;
  2979. case DRM_FORMAT_XRGB2101010:
  2980. return PLANE_CTL_FORMAT_XRGB_2101010;
  2981. case DRM_FORMAT_XBGR2101010:
  2982. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2983. case DRM_FORMAT_YUYV:
  2984. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2985. case DRM_FORMAT_YVYU:
  2986. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2987. case DRM_FORMAT_UYVY:
  2988. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2989. case DRM_FORMAT_VYUY:
  2990. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2991. case DRM_FORMAT_NV12:
  2992. return PLANE_CTL_FORMAT_NV12;
  2993. default:
  2994. MISSING_CASE(pixel_format);
  2995. }
  2996. return 0;
  2997. }
  2998. /*
  2999. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  3000. * to be already pre-multiplied. We need to add a knob (or a different
  3001. * DRM_FORMAT) for user-space to configure that.
  3002. */
  3003. static u32 skl_plane_ctl_alpha(uint32_t pixel_format)
  3004. {
  3005. switch (pixel_format) {
  3006. case DRM_FORMAT_ABGR8888:
  3007. case DRM_FORMAT_ARGB8888:
  3008. return PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  3009. default:
  3010. return PLANE_CTL_ALPHA_DISABLE;
  3011. }
  3012. }
  3013. static u32 glk_plane_color_ctl_alpha(uint32_t pixel_format)
  3014. {
  3015. switch (pixel_format) {
  3016. case DRM_FORMAT_ABGR8888:
  3017. case DRM_FORMAT_ARGB8888:
  3018. return PLANE_COLOR_ALPHA_SW_PREMULTIPLY;
  3019. default:
  3020. return PLANE_COLOR_ALPHA_DISABLE;
  3021. }
  3022. }
  3023. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  3024. {
  3025. switch (fb_modifier) {
  3026. case DRM_FORMAT_MOD_LINEAR:
  3027. break;
  3028. case I915_FORMAT_MOD_X_TILED:
  3029. return PLANE_CTL_TILED_X;
  3030. case I915_FORMAT_MOD_Y_TILED:
  3031. return PLANE_CTL_TILED_Y;
  3032. case I915_FORMAT_MOD_Y_TILED_CCS:
  3033. return PLANE_CTL_TILED_Y | PLANE_CTL_DECOMPRESSION_ENABLE;
  3034. case I915_FORMAT_MOD_Yf_TILED:
  3035. return PLANE_CTL_TILED_YF;
  3036. case I915_FORMAT_MOD_Yf_TILED_CCS:
  3037. return PLANE_CTL_TILED_YF | PLANE_CTL_DECOMPRESSION_ENABLE;
  3038. default:
  3039. MISSING_CASE(fb_modifier);
  3040. }
  3041. return 0;
  3042. }
  3043. static u32 skl_plane_ctl_rotate(unsigned int rotate)
  3044. {
  3045. switch (rotate) {
  3046. case DRM_MODE_ROTATE_0:
  3047. break;
  3048. /*
  3049. * DRM_MODE_ROTATE_ is counter clockwise to stay compatible with Xrandr
  3050. * while i915 HW rotation is clockwise, thats why this swapping.
  3051. */
  3052. case DRM_MODE_ROTATE_90:
  3053. return PLANE_CTL_ROTATE_270;
  3054. case DRM_MODE_ROTATE_180:
  3055. return PLANE_CTL_ROTATE_180;
  3056. case DRM_MODE_ROTATE_270:
  3057. return PLANE_CTL_ROTATE_90;
  3058. default:
  3059. MISSING_CASE(rotate);
  3060. }
  3061. return 0;
  3062. }
  3063. static u32 cnl_plane_ctl_flip(unsigned int reflect)
  3064. {
  3065. switch (reflect) {
  3066. case 0:
  3067. break;
  3068. case DRM_MODE_REFLECT_X:
  3069. return PLANE_CTL_FLIP_HORIZONTAL;
  3070. case DRM_MODE_REFLECT_Y:
  3071. default:
  3072. MISSING_CASE(reflect);
  3073. }
  3074. return 0;
  3075. }
  3076. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  3077. const struct intel_plane_state *plane_state)
  3078. {
  3079. struct drm_i915_private *dev_priv =
  3080. to_i915(plane_state->base.plane->dev);
  3081. const struct drm_framebuffer *fb = plane_state->base.fb;
  3082. unsigned int rotation = plane_state->base.rotation;
  3083. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  3084. u32 plane_ctl;
  3085. plane_ctl = PLANE_CTL_ENABLE;
  3086. if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
  3087. plane_ctl |= skl_plane_ctl_alpha(fb->format->format);
  3088. plane_ctl |=
  3089. PLANE_CTL_PIPE_GAMMA_ENABLE |
  3090. PLANE_CTL_PIPE_CSC_ENABLE |
  3091. PLANE_CTL_PLANE_GAMMA_DISABLE;
  3092. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  3093. plane_ctl |= PLANE_CTL_YUV_TO_RGB_CSC_FORMAT_BT709;
  3094. if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
  3095. plane_ctl |= PLANE_CTL_YUV_RANGE_CORRECTION_DISABLE;
  3096. }
  3097. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  3098. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  3099. plane_ctl |= skl_plane_ctl_rotate(rotation & DRM_MODE_ROTATE_MASK);
  3100. if (INTEL_GEN(dev_priv) >= 10)
  3101. plane_ctl |= cnl_plane_ctl_flip(rotation &
  3102. DRM_MODE_REFLECT_MASK);
  3103. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  3104. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  3105. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  3106. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  3107. return plane_ctl;
  3108. }
  3109. u32 glk_plane_color_ctl(const struct intel_crtc_state *crtc_state,
  3110. const struct intel_plane_state *plane_state)
  3111. {
  3112. struct drm_i915_private *dev_priv =
  3113. to_i915(plane_state->base.plane->dev);
  3114. const struct drm_framebuffer *fb = plane_state->base.fb;
  3115. u32 plane_color_ctl = 0;
  3116. if (INTEL_GEN(dev_priv) < 11) {
  3117. plane_color_ctl |= PLANE_COLOR_PIPE_GAMMA_ENABLE;
  3118. plane_color_ctl |= PLANE_COLOR_PIPE_CSC_ENABLE;
  3119. }
  3120. plane_color_ctl |= PLANE_COLOR_PLANE_GAMMA_DISABLE;
  3121. plane_color_ctl |= glk_plane_color_ctl_alpha(fb->format->format);
  3122. if (intel_format_is_yuv(fb->format->format)) {
  3123. if (plane_state->base.color_encoding == DRM_COLOR_YCBCR_BT709)
  3124. plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV709_TO_RGB709;
  3125. else
  3126. plane_color_ctl |= PLANE_COLOR_CSC_MODE_YUV601_TO_RGB709;
  3127. if (plane_state->base.color_range == DRM_COLOR_YCBCR_FULL_RANGE)
  3128. plane_color_ctl |= PLANE_COLOR_YUV_RANGE_CORRECTION_DISABLE;
  3129. }
  3130. return plane_color_ctl;
  3131. }
  3132. static int
  3133. __intel_display_resume(struct drm_device *dev,
  3134. struct drm_atomic_state *state,
  3135. struct drm_modeset_acquire_ctx *ctx)
  3136. {
  3137. struct drm_crtc_state *crtc_state;
  3138. struct drm_crtc *crtc;
  3139. int i, ret;
  3140. intel_modeset_setup_hw_state(dev, ctx);
  3141. i915_redisable_vga(to_i915(dev));
  3142. if (!state)
  3143. return 0;
  3144. /*
  3145. * We've duplicated the state, pointers to the old state are invalid.
  3146. *
  3147. * Don't attempt to use the old state until we commit the duplicated state.
  3148. */
  3149. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  3150. /*
  3151. * Force recalculation even if we restore
  3152. * current state. With fast modeset this may not result
  3153. * in a modeset when the state is compatible.
  3154. */
  3155. crtc_state->mode_changed = true;
  3156. }
  3157. /* ignore any reset values/BIOS leftovers in the WM registers */
  3158. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  3159. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3160. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  3161. WARN_ON(ret == -EDEADLK);
  3162. return ret;
  3163. }
  3164. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3165. {
  3166. return intel_has_gpu_reset(dev_priv) &&
  3167. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3168. }
  3169. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3170. {
  3171. struct drm_device *dev = &dev_priv->drm;
  3172. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3173. struct drm_atomic_state *state;
  3174. int ret;
  3175. /* reset doesn't touch the display */
  3176. if (!i915_modparams.force_reset_modeset_test &&
  3177. !gpu_reset_clobbers_display(dev_priv))
  3178. return;
  3179. /* We have a modeset vs reset deadlock, defensively unbreak it. */
  3180. set_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3181. wake_up_all(&dev_priv->gpu_error.wait_queue);
  3182. if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
  3183. DRM_DEBUG_KMS("Modeset potentially stuck, unbreaking through wedging\n");
  3184. i915_gem_set_wedged(dev_priv);
  3185. }
  3186. /*
  3187. * Need mode_config.mutex so that we don't
  3188. * trample ongoing ->detect() and whatnot.
  3189. */
  3190. mutex_lock(&dev->mode_config.mutex);
  3191. drm_modeset_acquire_init(ctx, 0);
  3192. while (1) {
  3193. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3194. if (ret != -EDEADLK)
  3195. break;
  3196. drm_modeset_backoff(ctx);
  3197. }
  3198. /*
  3199. * Disabling the crtcs gracefully seems nicer. Also the
  3200. * g33 docs say we should at least disable all the planes.
  3201. */
  3202. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3203. if (IS_ERR(state)) {
  3204. ret = PTR_ERR(state);
  3205. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3206. return;
  3207. }
  3208. ret = drm_atomic_helper_disable_all(dev, ctx);
  3209. if (ret) {
  3210. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3211. drm_atomic_state_put(state);
  3212. return;
  3213. }
  3214. dev_priv->modeset_restore_state = state;
  3215. state->acquire_ctx = ctx;
  3216. }
  3217. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3218. {
  3219. struct drm_device *dev = &dev_priv->drm;
  3220. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3221. struct drm_atomic_state *state;
  3222. int ret;
  3223. /* reset doesn't touch the display */
  3224. if (!test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  3225. return;
  3226. state = fetch_and_zero(&dev_priv->modeset_restore_state);
  3227. if (!state)
  3228. goto unlock;
  3229. /* reset doesn't touch the display */
  3230. if (!gpu_reset_clobbers_display(dev_priv)) {
  3231. /* for testing only restore the display */
  3232. ret = __intel_display_resume(dev, state, ctx);
  3233. if (ret)
  3234. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3235. } else {
  3236. /*
  3237. * The display has been reset as well,
  3238. * so need a full re-initialization.
  3239. */
  3240. intel_runtime_pm_disable_interrupts(dev_priv);
  3241. intel_runtime_pm_enable_interrupts(dev_priv);
  3242. intel_pps_unlock_regs_wa(dev_priv);
  3243. intel_modeset_init_hw(dev);
  3244. intel_init_clock_gating(dev_priv);
  3245. spin_lock_irq(&dev_priv->irq_lock);
  3246. if (dev_priv->display.hpd_irq_setup)
  3247. dev_priv->display.hpd_irq_setup(dev_priv);
  3248. spin_unlock_irq(&dev_priv->irq_lock);
  3249. ret = __intel_display_resume(dev, state, ctx);
  3250. if (ret)
  3251. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3252. intel_hpd_init(dev_priv);
  3253. }
  3254. drm_atomic_state_put(state);
  3255. unlock:
  3256. drm_modeset_drop_locks(ctx);
  3257. drm_modeset_acquire_fini(ctx);
  3258. mutex_unlock(&dev->mode_config.mutex);
  3259. clear_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags);
  3260. }
  3261. static void intel_update_pipe_config(const struct intel_crtc_state *old_crtc_state,
  3262. const struct intel_crtc_state *new_crtc_state)
  3263. {
  3264. struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
  3265. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3266. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3267. crtc->base.mode = new_crtc_state->base.mode;
  3268. /*
  3269. * Update pipe size and adjust fitter if needed: the reason for this is
  3270. * that in compute_mode_changes we check the native mode (not the pfit
  3271. * mode) to see if we can flip rather than do a full mode set. In the
  3272. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3273. * pfit state, we'll end up with a big fb scanned out into the wrong
  3274. * sized surface.
  3275. */
  3276. I915_WRITE(PIPESRC(crtc->pipe),
  3277. ((new_crtc_state->pipe_src_w - 1) << 16) |
  3278. (new_crtc_state->pipe_src_h - 1));
  3279. /* on skylake this is done by detaching scalers */
  3280. if (INTEL_GEN(dev_priv) >= 9) {
  3281. skl_detach_scalers(crtc);
  3282. if (new_crtc_state->pch_pfit.enabled)
  3283. skylake_pfit_enable(crtc);
  3284. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3285. if (new_crtc_state->pch_pfit.enabled)
  3286. ironlake_pfit_enable(crtc);
  3287. else if (old_crtc_state->pch_pfit.enabled)
  3288. ironlake_pfit_disable(crtc, true);
  3289. }
  3290. }
  3291. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3292. {
  3293. struct drm_device *dev = crtc->base.dev;
  3294. struct drm_i915_private *dev_priv = to_i915(dev);
  3295. int pipe = crtc->pipe;
  3296. i915_reg_t reg;
  3297. u32 temp;
  3298. /* enable normal train */
  3299. reg = FDI_TX_CTL(pipe);
  3300. temp = I915_READ(reg);
  3301. if (IS_IVYBRIDGE(dev_priv)) {
  3302. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3303. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3304. } else {
  3305. temp &= ~FDI_LINK_TRAIN_NONE;
  3306. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3307. }
  3308. I915_WRITE(reg, temp);
  3309. reg = FDI_RX_CTL(pipe);
  3310. temp = I915_READ(reg);
  3311. if (HAS_PCH_CPT(dev_priv)) {
  3312. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3313. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3314. } else {
  3315. temp &= ~FDI_LINK_TRAIN_NONE;
  3316. temp |= FDI_LINK_TRAIN_NONE;
  3317. }
  3318. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3319. /* wait one idle pattern time */
  3320. POSTING_READ(reg);
  3321. udelay(1000);
  3322. /* IVB wants error correction enabled */
  3323. if (IS_IVYBRIDGE(dev_priv))
  3324. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3325. FDI_FE_ERRC_ENABLE);
  3326. }
  3327. /* The FDI link training functions for ILK/Ibexpeak. */
  3328. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3329. const struct intel_crtc_state *crtc_state)
  3330. {
  3331. struct drm_device *dev = crtc->base.dev;
  3332. struct drm_i915_private *dev_priv = to_i915(dev);
  3333. int pipe = crtc->pipe;
  3334. i915_reg_t reg;
  3335. u32 temp, tries;
  3336. /* FDI needs bits from pipe first */
  3337. assert_pipe_enabled(dev_priv, pipe);
  3338. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3339. for train result */
  3340. reg = FDI_RX_IMR(pipe);
  3341. temp = I915_READ(reg);
  3342. temp &= ~FDI_RX_SYMBOL_LOCK;
  3343. temp &= ~FDI_RX_BIT_LOCK;
  3344. I915_WRITE(reg, temp);
  3345. I915_READ(reg);
  3346. udelay(150);
  3347. /* enable CPU FDI TX and PCH FDI RX */
  3348. reg = FDI_TX_CTL(pipe);
  3349. temp = I915_READ(reg);
  3350. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3351. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3352. temp &= ~FDI_LINK_TRAIN_NONE;
  3353. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3354. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3355. reg = FDI_RX_CTL(pipe);
  3356. temp = I915_READ(reg);
  3357. temp &= ~FDI_LINK_TRAIN_NONE;
  3358. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3359. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3360. POSTING_READ(reg);
  3361. udelay(150);
  3362. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3363. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3364. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3365. FDI_RX_PHASE_SYNC_POINTER_EN);
  3366. reg = FDI_RX_IIR(pipe);
  3367. for (tries = 0; tries < 5; tries++) {
  3368. temp = I915_READ(reg);
  3369. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3370. if ((temp & FDI_RX_BIT_LOCK)) {
  3371. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3372. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3373. break;
  3374. }
  3375. }
  3376. if (tries == 5)
  3377. DRM_ERROR("FDI train 1 fail!\n");
  3378. /* Train 2 */
  3379. reg = FDI_TX_CTL(pipe);
  3380. temp = I915_READ(reg);
  3381. temp &= ~FDI_LINK_TRAIN_NONE;
  3382. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3383. I915_WRITE(reg, temp);
  3384. reg = FDI_RX_CTL(pipe);
  3385. temp = I915_READ(reg);
  3386. temp &= ~FDI_LINK_TRAIN_NONE;
  3387. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3388. I915_WRITE(reg, temp);
  3389. POSTING_READ(reg);
  3390. udelay(150);
  3391. reg = FDI_RX_IIR(pipe);
  3392. for (tries = 0; tries < 5; tries++) {
  3393. temp = I915_READ(reg);
  3394. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3395. if (temp & FDI_RX_SYMBOL_LOCK) {
  3396. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3397. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3398. break;
  3399. }
  3400. }
  3401. if (tries == 5)
  3402. DRM_ERROR("FDI train 2 fail!\n");
  3403. DRM_DEBUG_KMS("FDI train done\n");
  3404. }
  3405. static const int snb_b_fdi_train_param[] = {
  3406. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3407. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3408. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3409. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3410. };
  3411. /* The FDI link training functions for SNB/Cougarpoint. */
  3412. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3413. const struct intel_crtc_state *crtc_state)
  3414. {
  3415. struct drm_device *dev = crtc->base.dev;
  3416. struct drm_i915_private *dev_priv = to_i915(dev);
  3417. int pipe = crtc->pipe;
  3418. i915_reg_t reg;
  3419. u32 temp, i, retry;
  3420. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3421. for train result */
  3422. reg = FDI_RX_IMR(pipe);
  3423. temp = I915_READ(reg);
  3424. temp &= ~FDI_RX_SYMBOL_LOCK;
  3425. temp &= ~FDI_RX_BIT_LOCK;
  3426. I915_WRITE(reg, temp);
  3427. POSTING_READ(reg);
  3428. udelay(150);
  3429. /* enable CPU FDI TX and PCH FDI RX */
  3430. reg = FDI_TX_CTL(pipe);
  3431. temp = I915_READ(reg);
  3432. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3433. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3434. temp &= ~FDI_LINK_TRAIN_NONE;
  3435. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3436. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3437. /* SNB-B */
  3438. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3439. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3440. I915_WRITE(FDI_RX_MISC(pipe),
  3441. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3442. reg = FDI_RX_CTL(pipe);
  3443. temp = I915_READ(reg);
  3444. if (HAS_PCH_CPT(dev_priv)) {
  3445. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3446. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3447. } else {
  3448. temp &= ~FDI_LINK_TRAIN_NONE;
  3449. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3450. }
  3451. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3452. POSTING_READ(reg);
  3453. udelay(150);
  3454. for (i = 0; i < 4; i++) {
  3455. reg = FDI_TX_CTL(pipe);
  3456. temp = I915_READ(reg);
  3457. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3458. temp |= snb_b_fdi_train_param[i];
  3459. I915_WRITE(reg, temp);
  3460. POSTING_READ(reg);
  3461. udelay(500);
  3462. for (retry = 0; retry < 5; retry++) {
  3463. reg = FDI_RX_IIR(pipe);
  3464. temp = I915_READ(reg);
  3465. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3466. if (temp & FDI_RX_BIT_LOCK) {
  3467. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3468. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3469. break;
  3470. }
  3471. udelay(50);
  3472. }
  3473. if (retry < 5)
  3474. break;
  3475. }
  3476. if (i == 4)
  3477. DRM_ERROR("FDI train 1 fail!\n");
  3478. /* Train 2 */
  3479. reg = FDI_TX_CTL(pipe);
  3480. temp = I915_READ(reg);
  3481. temp &= ~FDI_LINK_TRAIN_NONE;
  3482. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3483. if (IS_GEN6(dev_priv)) {
  3484. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3485. /* SNB-B */
  3486. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3487. }
  3488. I915_WRITE(reg, temp);
  3489. reg = FDI_RX_CTL(pipe);
  3490. temp = I915_READ(reg);
  3491. if (HAS_PCH_CPT(dev_priv)) {
  3492. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3493. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3494. } else {
  3495. temp &= ~FDI_LINK_TRAIN_NONE;
  3496. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3497. }
  3498. I915_WRITE(reg, temp);
  3499. POSTING_READ(reg);
  3500. udelay(150);
  3501. for (i = 0; i < 4; i++) {
  3502. reg = FDI_TX_CTL(pipe);
  3503. temp = I915_READ(reg);
  3504. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3505. temp |= snb_b_fdi_train_param[i];
  3506. I915_WRITE(reg, temp);
  3507. POSTING_READ(reg);
  3508. udelay(500);
  3509. for (retry = 0; retry < 5; retry++) {
  3510. reg = FDI_RX_IIR(pipe);
  3511. temp = I915_READ(reg);
  3512. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3513. if (temp & FDI_RX_SYMBOL_LOCK) {
  3514. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3515. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3516. break;
  3517. }
  3518. udelay(50);
  3519. }
  3520. if (retry < 5)
  3521. break;
  3522. }
  3523. if (i == 4)
  3524. DRM_ERROR("FDI train 2 fail!\n");
  3525. DRM_DEBUG_KMS("FDI train done.\n");
  3526. }
  3527. /* Manual link training for Ivy Bridge A0 parts */
  3528. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3529. const struct intel_crtc_state *crtc_state)
  3530. {
  3531. struct drm_device *dev = crtc->base.dev;
  3532. struct drm_i915_private *dev_priv = to_i915(dev);
  3533. int pipe = crtc->pipe;
  3534. i915_reg_t reg;
  3535. u32 temp, i, j;
  3536. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3537. for train result */
  3538. reg = FDI_RX_IMR(pipe);
  3539. temp = I915_READ(reg);
  3540. temp &= ~FDI_RX_SYMBOL_LOCK;
  3541. temp &= ~FDI_RX_BIT_LOCK;
  3542. I915_WRITE(reg, temp);
  3543. POSTING_READ(reg);
  3544. udelay(150);
  3545. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3546. I915_READ(FDI_RX_IIR(pipe)));
  3547. /* Try each vswing and preemphasis setting twice before moving on */
  3548. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3549. /* disable first in case we need to retry */
  3550. reg = FDI_TX_CTL(pipe);
  3551. temp = I915_READ(reg);
  3552. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3553. temp &= ~FDI_TX_ENABLE;
  3554. I915_WRITE(reg, temp);
  3555. reg = FDI_RX_CTL(pipe);
  3556. temp = I915_READ(reg);
  3557. temp &= ~FDI_LINK_TRAIN_AUTO;
  3558. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3559. temp &= ~FDI_RX_ENABLE;
  3560. I915_WRITE(reg, temp);
  3561. /* enable CPU FDI TX and PCH FDI RX */
  3562. reg = FDI_TX_CTL(pipe);
  3563. temp = I915_READ(reg);
  3564. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3565. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3566. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3567. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3568. temp |= snb_b_fdi_train_param[j/2];
  3569. temp |= FDI_COMPOSITE_SYNC;
  3570. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3571. I915_WRITE(FDI_RX_MISC(pipe),
  3572. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3573. reg = FDI_RX_CTL(pipe);
  3574. temp = I915_READ(reg);
  3575. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3576. temp |= FDI_COMPOSITE_SYNC;
  3577. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3578. POSTING_READ(reg);
  3579. udelay(1); /* should be 0.5us */
  3580. for (i = 0; i < 4; i++) {
  3581. reg = FDI_RX_IIR(pipe);
  3582. temp = I915_READ(reg);
  3583. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3584. if (temp & FDI_RX_BIT_LOCK ||
  3585. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3586. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3587. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3588. i);
  3589. break;
  3590. }
  3591. udelay(1); /* should be 0.5us */
  3592. }
  3593. if (i == 4) {
  3594. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3595. continue;
  3596. }
  3597. /* Train 2 */
  3598. reg = FDI_TX_CTL(pipe);
  3599. temp = I915_READ(reg);
  3600. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3601. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3602. I915_WRITE(reg, temp);
  3603. reg = FDI_RX_CTL(pipe);
  3604. temp = I915_READ(reg);
  3605. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3606. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3607. I915_WRITE(reg, temp);
  3608. POSTING_READ(reg);
  3609. udelay(2); /* should be 1.5us */
  3610. for (i = 0; i < 4; i++) {
  3611. reg = FDI_RX_IIR(pipe);
  3612. temp = I915_READ(reg);
  3613. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3614. if (temp & FDI_RX_SYMBOL_LOCK ||
  3615. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3616. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3617. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3618. i);
  3619. goto train_done;
  3620. }
  3621. udelay(2); /* should be 1.5us */
  3622. }
  3623. if (i == 4)
  3624. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3625. }
  3626. train_done:
  3627. DRM_DEBUG_KMS("FDI train done.\n");
  3628. }
  3629. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3630. {
  3631. struct drm_device *dev = intel_crtc->base.dev;
  3632. struct drm_i915_private *dev_priv = to_i915(dev);
  3633. int pipe = intel_crtc->pipe;
  3634. i915_reg_t reg;
  3635. u32 temp;
  3636. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3637. reg = FDI_RX_CTL(pipe);
  3638. temp = I915_READ(reg);
  3639. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3640. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3641. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3642. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3643. POSTING_READ(reg);
  3644. udelay(200);
  3645. /* Switch from Rawclk to PCDclk */
  3646. temp = I915_READ(reg);
  3647. I915_WRITE(reg, temp | FDI_PCDCLK);
  3648. POSTING_READ(reg);
  3649. udelay(200);
  3650. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3651. reg = FDI_TX_CTL(pipe);
  3652. temp = I915_READ(reg);
  3653. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3654. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3655. POSTING_READ(reg);
  3656. udelay(100);
  3657. }
  3658. }
  3659. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3660. {
  3661. struct drm_device *dev = intel_crtc->base.dev;
  3662. struct drm_i915_private *dev_priv = to_i915(dev);
  3663. int pipe = intel_crtc->pipe;
  3664. i915_reg_t reg;
  3665. u32 temp;
  3666. /* Switch from PCDclk to Rawclk */
  3667. reg = FDI_RX_CTL(pipe);
  3668. temp = I915_READ(reg);
  3669. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3670. /* Disable CPU FDI TX PLL */
  3671. reg = FDI_TX_CTL(pipe);
  3672. temp = I915_READ(reg);
  3673. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3674. POSTING_READ(reg);
  3675. udelay(100);
  3676. reg = FDI_RX_CTL(pipe);
  3677. temp = I915_READ(reg);
  3678. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3679. /* Wait for the clocks to turn off. */
  3680. POSTING_READ(reg);
  3681. udelay(100);
  3682. }
  3683. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3684. {
  3685. struct drm_device *dev = crtc->dev;
  3686. struct drm_i915_private *dev_priv = to_i915(dev);
  3687. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3688. int pipe = intel_crtc->pipe;
  3689. i915_reg_t reg;
  3690. u32 temp;
  3691. /* disable CPU FDI tx and PCH FDI rx */
  3692. reg = FDI_TX_CTL(pipe);
  3693. temp = I915_READ(reg);
  3694. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3695. POSTING_READ(reg);
  3696. reg = FDI_RX_CTL(pipe);
  3697. temp = I915_READ(reg);
  3698. temp &= ~(0x7 << 16);
  3699. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3700. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3701. POSTING_READ(reg);
  3702. udelay(100);
  3703. /* Ironlake workaround, disable clock pointer after downing FDI */
  3704. if (HAS_PCH_IBX(dev_priv))
  3705. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3706. /* still set train pattern 1 */
  3707. reg = FDI_TX_CTL(pipe);
  3708. temp = I915_READ(reg);
  3709. temp &= ~FDI_LINK_TRAIN_NONE;
  3710. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3711. I915_WRITE(reg, temp);
  3712. reg = FDI_RX_CTL(pipe);
  3713. temp = I915_READ(reg);
  3714. if (HAS_PCH_CPT(dev_priv)) {
  3715. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3716. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3717. } else {
  3718. temp &= ~FDI_LINK_TRAIN_NONE;
  3719. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3720. }
  3721. /* BPC in FDI rx is consistent with that in PIPECONF */
  3722. temp &= ~(0x07 << 16);
  3723. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3724. I915_WRITE(reg, temp);
  3725. POSTING_READ(reg);
  3726. udelay(100);
  3727. }
  3728. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3729. {
  3730. struct drm_crtc *crtc;
  3731. bool cleanup_done;
  3732. drm_for_each_crtc(crtc, &dev_priv->drm) {
  3733. struct drm_crtc_commit *commit;
  3734. spin_lock(&crtc->commit_lock);
  3735. commit = list_first_entry_or_null(&crtc->commit_list,
  3736. struct drm_crtc_commit, commit_entry);
  3737. cleanup_done = commit ?
  3738. try_wait_for_completion(&commit->cleanup_done) : true;
  3739. spin_unlock(&crtc->commit_lock);
  3740. if (cleanup_done)
  3741. continue;
  3742. drm_crtc_wait_one_vblank(crtc);
  3743. return true;
  3744. }
  3745. return false;
  3746. }
  3747. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3748. {
  3749. u32 temp;
  3750. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3751. mutex_lock(&dev_priv->sb_lock);
  3752. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3753. temp |= SBI_SSCCTL_DISABLE;
  3754. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3755. mutex_unlock(&dev_priv->sb_lock);
  3756. }
  3757. /* Program iCLKIP clock to the desired frequency */
  3758. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3759. {
  3760. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3761. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3762. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3763. u32 temp;
  3764. lpt_disable_iclkip(dev_priv);
  3765. /* The iCLK virtual clock root frequency is in MHz,
  3766. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3767. * divisors, it is necessary to divide one by another, so we
  3768. * convert the virtual clock precision to KHz here for higher
  3769. * precision.
  3770. */
  3771. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3772. u32 iclk_virtual_root_freq = 172800 * 1000;
  3773. u32 iclk_pi_range = 64;
  3774. u32 desired_divisor;
  3775. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3776. clock << auxdiv);
  3777. divsel = (desired_divisor / iclk_pi_range) - 2;
  3778. phaseinc = desired_divisor % iclk_pi_range;
  3779. /*
  3780. * Near 20MHz is a corner case which is
  3781. * out of range for the 7-bit divisor
  3782. */
  3783. if (divsel <= 0x7f)
  3784. break;
  3785. }
  3786. /* This should not happen with any sane values */
  3787. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3788. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3789. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3790. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3791. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3792. clock,
  3793. auxdiv,
  3794. divsel,
  3795. phasedir,
  3796. phaseinc);
  3797. mutex_lock(&dev_priv->sb_lock);
  3798. /* Program SSCDIVINTPHASE6 */
  3799. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3800. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3801. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3802. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3803. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3804. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3805. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3806. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3807. /* Program SSCAUXDIV */
  3808. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3809. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3810. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3811. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3812. /* Enable modulator and associated divider */
  3813. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3814. temp &= ~SBI_SSCCTL_DISABLE;
  3815. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3816. mutex_unlock(&dev_priv->sb_lock);
  3817. /* Wait for initialization time */
  3818. udelay(24);
  3819. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3820. }
  3821. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3822. {
  3823. u32 divsel, phaseinc, auxdiv;
  3824. u32 iclk_virtual_root_freq = 172800 * 1000;
  3825. u32 iclk_pi_range = 64;
  3826. u32 desired_divisor;
  3827. u32 temp;
  3828. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3829. return 0;
  3830. mutex_lock(&dev_priv->sb_lock);
  3831. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3832. if (temp & SBI_SSCCTL_DISABLE) {
  3833. mutex_unlock(&dev_priv->sb_lock);
  3834. return 0;
  3835. }
  3836. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3837. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3838. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3839. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3840. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3841. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3842. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3843. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3844. mutex_unlock(&dev_priv->sb_lock);
  3845. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3846. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3847. desired_divisor << auxdiv);
  3848. }
  3849. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3850. enum pipe pch_transcoder)
  3851. {
  3852. struct drm_device *dev = crtc->base.dev;
  3853. struct drm_i915_private *dev_priv = to_i915(dev);
  3854. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3855. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3856. I915_READ(HTOTAL(cpu_transcoder)));
  3857. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3858. I915_READ(HBLANK(cpu_transcoder)));
  3859. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3860. I915_READ(HSYNC(cpu_transcoder)));
  3861. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3862. I915_READ(VTOTAL(cpu_transcoder)));
  3863. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3864. I915_READ(VBLANK(cpu_transcoder)));
  3865. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3866. I915_READ(VSYNC(cpu_transcoder)));
  3867. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3868. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3869. }
  3870. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3871. {
  3872. struct drm_i915_private *dev_priv = to_i915(dev);
  3873. uint32_t temp;
  3874. temp = I915_READ(SOUTH_CHICKEN1);
  3875. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3876. return;
  3877. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3878. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3879. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3880. if (enable)
  3881. temp |= FDI_BC_BIFURCATION_SELECT;
  3882. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3883. I915_WRITE(SOUTH_CHICKEN1, temp);
  3884. POSTING_READ(SOUTH_CHICKEN1);
  3885. }
  3886. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3887. {
  3888. struct drm_device *dev = intel_crtc->base.dev;
  3889. switch (intel_crtc->pipe) {
  3890. case PIPE_A:
  3891. break;
  3892. case PIPE_B:
  3893. if (intel_crtc->config->fdi_lanes > 2)
  3894. cpt_set_fdi_bc_bifurcation(dev, false);
  3895. else
  3896. cpt_set_fdi_bc_bifurcation(dev, true);
  3897. break;
  3898. case PIPE_C:
  3899. cpt_set_fdi_bc_bifurcation(dev, true);
  3900. break;
  3901. default:
  3902. BUG();
  3903. }
  3904. }
  3905. /*
  3906. * Finds the encoder associated with the given CRTC. This can only be
  3907. * used when we know that the CRTC isn't feeding multiple encoders!
  3908. */
  3909. static struct intel_encoder *
  3910. intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
  3911. const struct intel_crtc_state *crtc_state)
  3912. {
  3913. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3914. const struct drm_connector_state *connector_state;
  3915. const struct drm_connector *connector;
  3916. struct intel_encoder *encoder = NULL;
  3917. int num_encoders = 0;
  3918. int i;
  3919. for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
  3920. if (connector_state->crtc != &crtc->base)
  3921. continue;
  3922. encoder = to_intel_encoder(connector_state->best_encoder);
  3923. num_encoders++;
  3924. }
  3925. WARN(num_encoders != 1, "%d encoders for pipe %c\n",
  3926. num_encoders, pipe_name(crtc->pipe));
  3927. return encoder;
  3928. }
  3929. /*
  3930. * Enable PCH resources required for PCH ports:
  3931. * - PCH PLLs
  3932. * - FDI training & RX/TX
  3933. * - update transcoder timings
  3934. * - DP transcoding bits
  3935. * - transcoder
  3936. */
  3937. static void ironlake_pch_enable(const struct intel_atomic_state *state,
  3938. const struct intel_crtc_state *crtc_state)
  3939. {
  3940. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3941. struct drm_device *dev = crtc->base.dev;
  3942. struct drm_i915_private *dev_priv = to_i915(dev);
  3943. int pipe = crtc->pipe;
  3944. u32 temp;
  3945. assert_pch_transcoder_disabled(dev_priv, pipe);
  3946. if (IS_IVYBRIDGE(dev_priv))
  3947. ivybridge_update_fdi_bc_bifurcation(crtc);
  3948. /* Write the TU size bits before fdi link training, so that error
  3949. * detection works. */
  3950. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3951. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3952. /* For PCH output, training FDI link */
  3953. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3954. /* We need to program the right clock selection before writing the pixel
  3955. * mutliplier into the DPLL. */
  3956. if (HAS_PCH_CPT(dev_priv)) {
  3957. u32 sel;
  3958. temp = I915_READ(PCH_DPLL_SEL);
  3959. temp |= TRANS_DPLL_ENABLE(pipe);
  3960. sel = TRANS_DPLLB_SEL(pipe);
  3961. if (crtc_state->shared_dpll ==
  3962. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3963. temp |= sel;
  3964. else
  3965. temp &= ~sel;
  3966. I915_WRITE(PCH_DPLL_SEL, temp);
  3967. }
  3968. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3969. * transcoder, and we actually should do this to not upset any PCH
  3970. * transcoder that already use the clock when we share it.
  3971. *
  3972. * Note that enable_shared_dpll tries to do the right thing, but
  3973. * get_shared_dpll unconditionally resets the pll - we need that to have
  3974. * the right LVDS enable sequence. */
  3975. intel_enable_shared_dpll(crtc);
  3976. /* set transcoder timing, panel must allow it */
  3977. assert_panel_unlocked(dev_priv, pipe);
  3978. ironlake_pch_transcoder_set_timings(crtc, pipe);
  3979. intel_fdi_normal_train(crtc);
  3980. /* For PCH DP, enable TRANS_DP_CTL */
  3981. if (HAS_PCH_CPT(dev_priv) &&
  3982. intel_crtc_has_dp_encoder(crtc_state)) {
  3983. const struct drm_display_mode *adjusted_mode =
  3984. &crtc_state->base.adjusted_mode;
  3985. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3986. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3987. enum port port;
  3988. temp = I915_READ(reg);
  3989. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3990. TRANS_DP_SYNC_MASK |
  3991. TRANS_DP_BPC_MASK);
  3992. temp |= TRANS_DP_OUTPUT_ENABLE;
  3993. temp |= bpc << 9; /* same format but at 11:9 */
  3994. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3995. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3996. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3997. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3998. port = intel_get_crtc_new_encoder(state, crtc_state)->port;
  3999. WARN_ON(port < PORT_B || port > PORT_D);
  4000. temp |= TRANS_DP_PORT_SEL(port);
  4001. I915_WRITE(reg, temp);
  4002. }
  4003. ironlake_enable_pch_transcoder(dev_priv, pipe);
  4004. }
  4005. static void lpt_pch_enable(const struct intel_atomic_state *state,
  4006. const struct intel_crtc_state *crtc_state)
  4007. {
  4008. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4009. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  4010. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  4011. assert_pch_transcoder_disabled(dev_priv, PIPE_A);
  4012. lpt_program_iclkip(crtc);
  4013. /* Set transcoder timing. */
  4014. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  4015. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  4016. }
  4017. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  4018. {
  4019. struct drm_i915_private *dev_priv = to_i915(dev);
  4020. i915_reg_t dslreg = PIPEDSL(pipe);
  4021. u32 temp;
  4022. temp = I915_READ(dslreg);
  4023. udelay(500);
  4024. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  4025. if (wait_for(I915_READ(dslreg) != temp, 5))
  4026. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  4027. }
  4028. }
  4029. /*
  4030. * The hardware phase 0.0 refers to the center of the pixel.
  4031. * We want to start from the top/left edge which is phase
  4032. * -0.5. That matches how the hardware calculates the scaling
  4033. * factors (from top-left of the first pixel to bottom-right
  4034. * of the last pixel, as opposed to the pixel centers).
  4035. *
  4036. * For 4:2:0 subsampled chroma planes we obviously have to
  4037. * adjust that so that the chroma sample position lands in
  4038. * the right spot.
  4039. *
  4040. * Note that for packed YCbCr 4:2:2 formats there is no way to
  4041. * control chroma siting. The hardware simply replicates the
  4042. * chroma samples for both of the luma samples, and thus we don't
  4043. * actually get the expected MPEG2 chroma siting convention :(
  4044. * The same behaviour is observed on pre-SKL platforms as well.
  4045. */
  4046. u16 skl_scaler_calc_phase(int sub, bool chroma_cosited)
  4047. {
  4048. int phase = -0x8000;
  4049. u16 trip = 0;
  4050. if (chroma_cosited)
  4051. phase += (sub - 1) * 0x8000 / sub;
  4052. if (phase < 0)
  4053. phase = 0x10000 + phase;
  4054. else
  4055. trip = PS_PHASE_TRIP;
  4056. return ((phase >> 2) & PS_PHASE_MASK) | trip;
  4057. }
  4058. static int
  4059. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  4060. unsigned int scaler_user, int *scaler_id,
  4061. int src_w, int src_h, int dst_w, int dst_h,
  4062. bool plane_scaler_check,
  4063. uint32_t pixel_format)
  4064. {
  4065. struct intel_crtc_scaler_state *scaler_state =
  4066. &crtc_state->scaler_state;
  4067. struct intel_crtc *intel_crtc =
  4068. to_intel_crtc(crtc_state->base.crtc);
  4069. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  4070. const struct drm_display_mode *adjusted_mode =
  4071. &crtc_state->base.adjusted_mode;
  4072. int need_scaling;
  4073. /*
  4074. * Src coordinates are already rotated by 270 degrees for
  4075. * the 90/270 degree plane rotation cases (to match the
  4076. * GTT mapping), hence no need to account for rotation here.
  4077. */
  4078. need_scaling = src_w != dst_w || src_h != dst_h;
  4079. if (plane_scaler_check)
  4080. if (pixel_format == DRM_FORMAT_NV12)
  4081. need_scaling = true;
  4082. if (crtc_state->ycbcr420 && scaler_user == SKL_CRTC_INDEX)
  4083. need_scaling = true;
  4084. /*
  4085. * Scaling/fitting not supported in IF-ID mode in GEN9+
  4086. * TODO: Interlace fetch mode doesn't support YUV420 planar formats.
  4087. * Once NV12 is enabled, handle it here while allocating scaler
  4088. * for NV12.
  4089. */
  4090. if (INTEL_GEN(dev_priv) >= 9 && crtc_state->base.enable &&
  4091. need_scaling && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4092. DRM_DEBUG_KMS("Pipe/Plane scaling not supported with IF-ID mode\n");
  4093. return -EINVAL;
  4094. }
  4095. /*
  4096. * if plane is being disabled or scaler is no more required or force detach
  4097. * - free scaler binded to this plane/crtc
  4098. * - in order to do this, update crtc->scaler_usage
  4099. *
  4100. * Here scaler state in crtc_state is set free so that
  4101. * scaler can be assigned to other user. Actual register
  4102. * update to free the scaler is done in plane/panel-fit programming.
  4103. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  4104. */
  4105. if (force_detach || !need_scaling) {
  4106. if (*scaler_id >= 0) {
  4107. scaler_state->scaler_users &= ~(1 << scaler_user);
  4108. scaler_state->scalers[*scaler_id].in_use = 0;
  4109. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4110. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  4111. intel_crtc->pipe, scaler_user, *scaler_id,
  4112. scaler_state->scaler_users);
  4113. *scaler_id = -1;
  4114. }
  4115. return 0;
  4116. }
  4117. if (plane_scaler_check && pixel_format == DRM_FORMAT_NV12 &&
  4118. (src_h < SKL_MIN_YUV_420_SRC_H || src_w < SKL_MIN_YUV_420_SRC_W)) {
  4119. DRM_DEBUG_KMS("NV12: src dimensions not met\n");
  4120. return -EINVAL;
  4121. }
  4122. /* range checks */
  4123. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  4124. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  4125. (IS_GEN11(dev_priv) &&
  4126. (src_w > ICL_MAX_SRC_W || src_h > ICL_MAX_SRC_H ||
  4127. dst_w > ICL_MAX_DST_W || dst_h > ICL_MAX_DST_H)) ||
  4128. (!IS_GEN11(dev_priv) &&
  4129. (src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  4130. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H))) {
  4131. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  4132. "size is out of scaler range\n",
  4133. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4134. return -EINVAL;
  4135. }
  4136. /* mark this plane as a scaler user in crtc_state */
  4137. scaler_state->scaler_users |= (1 << scaler_user);
  4138. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4139. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4140. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4141. scaler_state->scaler_users);
  4142. return 0;
  4143. }
  4144. /**
  4145. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4146. *
  4147. * @state: crtc's scaler state
  4148. *
  4149. * Return
  4150. * 0 - scaler_usage updated successfully
  4151. * error - requested scaling cannot be supported or other error condition
  4152. */
  4153. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4154. {
  4155. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4156. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4157. &state->scaler_state.scaler_id,
  4158. state->pipe_src_w, state->pipe_src_h,
  4159. adjusted_mode->crtc_hdisplay,
  4160. adjusted_mode->crtc_vdisplay, false, 0);
  4161. }
  4162. /**
  4163. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4164. * @crtc_state: crtc's scaler state
  4165. * @plane_state: atomic plane state to update
  4166. *
  4167. * Return
  4168. * 0 - scaler_usage updated successfully
  4169. * error - requested scaling cannot be supported or other error condition
  4170. */
  4171. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4172. struct intel_plane_state *plane_state)
  4173. {
  4174. struct intel_plane *intel_plane =
  4175. to_intel_plane(plane_state->base.plane);
  4176. struct drm_framebuffer *fb = plane_state->base.fb;
  4177. int ret;
  4178. bool force_detach = !fb || !plane_state->base.visible;
  4179. ret = skl_update_scaler(crtc_state, force_detach,
  4180. drm_plane_index(&intel_plane->base),
  4181. &plane_state->scaler_id,
  4182. drm_rect_width(&plane_state->base.src) >> 16,
  4183. drm_rect_height(&plane_state->base.src) >> 16,
  4184. drm_rect_width(&plane_state->base.dst),
  4185. drm_rect_height(&plane_state->base.dst),
  4186. fb ? true : false, fb ? fb->format->format : 0);
  4187. if (ret || plane_state->scaler_id < 0)
  4188. return ret;
  4189. /* check colorkey */
  4190. if (plane_state->ckey.flags) {
  4191. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4192. intel_plane->base.base.id,
  4193. intel_plane->base.name);
  4194. return -EINVAL;
  4195. }
  4196. /* Check src format */
  4197. switch (fb->format->format) {
  4198. case DRM_FORMAT_RGB565:
  4199. case DRM_FORMAT_XBGR8888:
  4200. case DRM_FORMAT_XRGB8888:
  4201. case DRM_FORMAT_ABGR8888:
  4202. case DRM_FORMAT_ARGB8888:
  4203. case DRM_FORMAT_XRGB2101010:
  4204. case DRM_FORMAT_XBGR2101010:
  4205. case DRM_FORMAT_YUYV:
  4206. case DRM_FORMAT_YVYU:
  4207. case DRM_FORMAT_UYVY:
  4208. case DRM_FORMAT_VYUY:
  4209. case DRM_FORMAT_NV12:
  4210. break;
  4211. default:
  4212. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4213. intel_plane->base.base.id, intel_plane->base.name,
  4214. fb->base.id, fb->format->format);
  4215. return -EINVAL;
  4216. }
  4217. return 0;
  4218. }
  4219. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4220. {
  4221. int i;
  4222. for (i = 0; i < crtc->num_scalers; i++)
  4223. skl_detach_scaler(crtc, i);
  4224. }
  4225. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4226. {
  4227. struct drm_device *dev = crtc->base.dev;
  4228. struct drm_i915_private *dev_priv = to_i915(dev);
  4229. int pipe = crtc->pipe;
  4230. struct intel_crtc_scaler_state *scaler_state =
  4231. &crtc->config->scaler_state;
  4232. if (crtc->config->pch_pfit.enabled) {
  4233. u16 uv_rgb_hphase, uv_rgb_vphase;
  4234. int id;
  4235. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4236. return;
  4237. uv_rgb_hphase = skl_scaler_calc_phase(1, false);
  4238. uv_rgb_vphase = skl_scaler_calc_phase(1, false);
  4239. id = scaler_state->scaler_id;
  4240. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4241. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4242. I915_WRITE_FW(SKL_PS_VPHASE(pipe, id),
  4243. PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_vphase));
  4244. I915_WRITE_FW(SKL_PS_HPHASE(pipe, id),
  4245. PS_Y_PHASE(0) | PS_UV_RGB_PHASE(uv_rgb_hphase));
  4246. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4247. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4248. }
  4249. }
  4250. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4251. {
  4252. struct drm_device *dev = crtc->base.dev;
  4253. struct drm_i915_private *dev_priv = to_i915(dev);
  4254. int pipe = crtc->pipe;
  4255. if (crtc->config->pch_pfit.enabled) {
  4256. /* Force use of hard-coded filter coefficients
  4257. * as some pre-programmed values are broken,
  4258. * e.g. x201.
  4259. */
  4260. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4261. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4262. PF_PIPE_SEL_IVB(pipe));
  4263. else
  4264. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4265. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4266. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4267. }
  4268. }
  4269. void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
  4270. {
  4271. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4272. struct drm_device *dev = crtc->base.dev;
  4273. struct drm_i915_private *dev_priv = to_i915(dev);
  4274. if (!crtc_state->ips_enabled)
  4275. return;
  4276. /*
  4277. * We can only enable IPS after we enable a plane and wait for a vblank
  4278. * This function is called from post_plane_update, which is run after
  4279. * a vblank wait.
  4280. */
  4281. WARN_ON(!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
  4282. if (IS_BROADWELL(dev_priv)) {
  4283. mutex_lock(&dev_priv->pcu_lock);
  4284. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
  4285. IPS_ENABLE | IPS_PCODE_CONTROL));
  4286. mutex_unlock(&dev_priv->pcu_lock);
  4287. /* Quoting Art Runyan: "its not safe to expect any particular
  4288. * value in IPS_CTL bit 31 after enabling IPS through the
  4289. * mailbox." Moreover, the mailbox may return a bogus state,
  4290. * so we need to just enable it and continue on.
  4291. */
  4292. } else {
  4293. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4294. /* The bit only becomes 1 in the next vblank, so this wait here
  4295. * is essentially intel_wait_for_vblank. If we don't have this
  4296. * and don't wait for vblanks until the end of crtc_enable, then
  4297. * the HW state readout code will complain that the expected
  4298. * IPS_CTL value is not the one we read. */
  4299. if (intel_wait_for_register(dev_priv,
  4300. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4301. 50))
  4302. DRM_ERROR("Timed out waiting for IPS enable\n");
  4303. }
  4304. }
  4305. void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
  4306. {
  4307. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  4308. struct drm_device *dev = crtc->base.dev;
  4309. struct drm_i915_private *dev_priv = to_i915(dev);
  4310. if (!crtc_state->ips_enabled)
  4311. return;
  4312. if (IS_BROADWELL(dev_priv)) {
  4313. mutex_lock(&dev_priv->pcu_lock);
  4314. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4315. mutex_unlock(&dev_priv->pcu_lock);
  4316. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4317. if (intel_wait_for_register(dev_priv,
  4318. IPS_CTL, IPS_ENABLE, 0,
  4319. 42))
  4320. DRM_ERROR("Timed out waiting for IPS disable\n");
  4321. } else {
  4322. I915_WRITE(IPS_CTL, 0);
  4323. POSTING_READ(IPS_CTL);
  4324. }
  4325. /* We need to wait for a vblank before we can disable the plane. */
  4326. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4327. }
  4328. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4329. {
  4330. if (intel_crtc->overlay) {
  4331. struct drm_device *dev = intel_crtc->base.dev;
  4332. mutex_lock(&dev->struct_mutex);
  4333. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4334. mutex_unlock(&dev->struct_mutex);
  4335. }
  4336. /* Let userspace switch the overlay on again. In most cases userspace
  4337. * has to recompute where to put it anyway.
  4338. */
  4339. }
  4340. /**
  4341. * intel_post_enable_primary - Perform operations after enabling primary plane
  4342. * @crtc: the CRTC whose primary plane was just enabled
  4343. * @new_crtc_state: the enabling state
  4344. *
  4345. * Performs potentially sleeping operations that must be done after the primary
  4346. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4347. * called due to an explicit primary plane update, or due to an implicit
  4348. * re-enable that is caused when a sprite plane is updated to no longer
  4349. * completely hide the primary plane.
  4350. */
  4351. static void
  4352. intel_post_enable_primary(struct drm_crtc *crtc,
  4353. const struct intel_crtc_state *new_crtc_state)
  4354. {
  4355. struct drm_device *dev = crtc->dev;
  4356. struct drm_i915_private *dev_priv = to_i915(dev);
  4357. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4358. int pipe = intel_crtc->pipe;
  4359. /*
  4360. * Gen2 reports pipe underruns whenever all planes are disabled.
  4361. * So don't enable underrun reporting before at least some planes
  4362. * are enabled.
  4363. * FIXME: Need to fix the logic to work when we turn off all planes
  4364. * but leave the pipe running.
  4365. */
  4366. if (IS_GEN2(dev_priv))
  4367. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4368. /* Underruns don't always raise interrupts, so check manually. */
  4369. intel_check_cpu_fifo_underruns(dev_priv);
  4370. intel_check_pch_fifo_underruns(dev_priv);
  4371. }
  4372. /* FIXME get rid of this and use pre_plane_update */
  4373. static void
  4374. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4375. {
  4376. struct drm_device *dev = crtc->dev;
  4377. struct drm_i915_private *dev_priv = to_i915(dev);
  4378. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4379. int pipe = intel_crtc->pipe;
  4380. /*
  4381. * Gen2 reports pipe underruns whenever all planes are disabled.
  4382. * So disable underrun reporting before all the planes get disabled.
  4383. */
  4384. if (IS_GEN2(dev_priv))
  4385. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4386. hsw_disable_ips(to_intel_crtc_state(crtc->state));
  4387. /*
  4388. * Vblank time updates from the shadow to live plane control register
  4389. * are blocked if the memory self-refresh mode is active at that
  4390. * moment. So to make sure the plane gets truly disabled, disable
  4391. * first the self-refresh mode. The self-refresh enable bit in turn
  4392. * will be checked/applied by the HW only at the next frame start
  4393. * event which is after the vblank start event, so we need to have a
  4394. * wait-for-vblank between disabling the plane and the pipe.
  4395. */
  4396. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4397. intel_set_memory_cxsr(dev_priv, false))
  4398. intel_wait_for_vblank(dev_priv, pipe);
  4399. }
  4400. static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
  4401. const struct intel_crtc_state *new_crtc_state)
  4402. {
  4403. if (!old_crtc_state->ips_enabled)
  4404. return false;
  4405. if (needs_modeset(&new_crtc_state->base))
  4406. return true;
  4407. return !new_crtc_state->ips_enabled;
  4408. }
  4409. static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
  4410. const struct intel_crtc_state *new_crtc_state)
  4411. {
  4412. if (!new_crtc_state->ips_enabled)
  4413. return false;
  4414. if (needs_modeset(&new_crtc_state->base))
  4415. return true;
  4416. /*
  4417. * We can't read out IPS on broadwell, assume the worst and
  4418. * forcibly enable IPS on the first fastset.
  4419. */
  4420. if (new_crtc_state->update_pipe &&
  4421. old_crtc_state->base.adjusted_mode.private_flags & I915_MODE_FLAG_INHERITED)
  4422. return true;
  4423. return !old_crtc_state->ips_enabled;
  4424. }
  4425. static bool needs_nv12_wa(struct drm_i915_private *dev_priv,
  4426. const struct intel_crtc_state *crtc_state)
  4427. {
  4428. if (!crtc_state->nv12_planes)
  4429. return false;
  4430. if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  4431. return false;
  4432. if ((INTEL_GEN(dev_priv) == 9 && !IS_GEMINILAKE(dev_priv)) ||
  4433. IS_CANNONLAKE(dev_priv))
  4434. return true;
  4435. return false;
  4436. }
  4437. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4438. {
  4439. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4440. struct drm_device *dev = crtc->base.dev;
  4441. struct drm_i915_private *dev_priv = to_i915(dev);
  4442. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4443. struct intel_crtc_state *pipe_config =
  4444. intel_atomic_get_new_crtc_state(to_intel_atomic_state(old_state),
  4445. crtc);
  4446. struct drm_plane *primary = crtc->base.primary;
  4447. struct drm_plane_state *old_primary_state =
  4448. drm_atomic_get_old_plane_state(old_state, primary);
  4449. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4450. if (pipe_config->update_wm_post && pipe_config->base.active)
  4451. intel_update_watermarks(crtc);
  4452. if (hsw_post_update_enable_ips(old_crtc_state, pipe_config))
  4453. hsw_enable_ips(pipe_config);
  4454. if (old_primary_state) {
  4455. struct drm_plane_state *new_primary_state =
  4456. drm_atomic_get_new_plane_state(old_state, primary);
  4457. intel_fbc_post_update(crtc);
  4458. if (new_primary_state->visible &&
  4459. (needs_modeset(&pipe_config->base) ||
  4460. !old_primary_state->visible))
  4461. intel_post_enable_primary(&crtc->base, pipe_config);
  4462. }
  4463. /* Display WA 827 */
  4464. if (needs_nv12_wa(dev_priv, old_crtc_state) &&
  4465. !needs_nv12_wa(dev_priv, pipe_config)) {
  4466. skl_wa_clkgate(dev_priv, crtc->pipe, false);
  4467. skl_wa_528(dev_priv, crtc->pipe, false);
  4468. }
  4469. }
  4470. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4471. struct intel_crtc_state *pipe_config)
  4472. {
  4473. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4474. struct drm_device *dev = crtc->base.dev;
  4475. struct drm_i915_private *dev_priv = to_i915(dev);
  4476. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4477. struct drm_plane *primary = crtc->base.primary;
  4478. struct drm_plane_state *old_primary_state =
  4479. drm_atomic_get_old_plane_state(old_state, primary);
  4480. bool modeset = needs_modeset(&pipe_config->base);
  4481. struct intel_atomic_state *old_intel_state =
  4482. to_intel_atomic_state(old_state);
  4483. if (hsw_pre_update_disable_ips(old_crtc_state, pipe_config))
  4484. hsw_disable_ips(old_crtc_state);
  4485. if (old_primary_state) {
  4486. struct intel_plane_state *new_primary_state =
  4487. intel_atomic_get_new_plane_state(old_intel_state,
  4488. to_intel_plane(primary));
  4489. intel_fbc_pre_update(crtc, pipe_config, new_primary_state);
  4490. /*
  4491. * Gen2 reports pipe underruns whenever all planes are disabled.
  4492. * So disable underrun reporting before all the planes get disabled.
  4493. */
  4494. if (IS_GEN2(dev_priv) && old_primary_state->visible &&
  4495. (modeset || !new_primary_state->base.visible))
  4496. intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
  4497. }
  4498. /* Display WA 827 */
  4499. if (!needs_nv12_wa(dev_priv, old_crtc_state) &&
  4500. needs_nv12_wa(dev_priv, pipe_config)) {
  4501. skl_wa_clkgate(dev_priv, crtc->pipe, true);
  4502. skl_wa_528(dev_priv, crtc->pipe, true);
  4503. }
  4504. /*
  4505. * Vblank time updates from the shadow to live plane control register
  4506. * are blocked if the memory self-refresh mode is active at that
  4507. * moment. So to make sure the plane gets truly disabled, disable
  4508. * first the self-refresh mode. The self-refresh enable bit in turn
  4509. * will be checked/applied by the HW only at the next frame start
  4510. * event which is after the vblank start event, so we need to have a
  4511. * wait-for-vblank between disabling the plane and the pipe.
  4512. */
  4513. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4514. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4515. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4516. /*
  4517. * IVB workaround: must disable low power watermarks for at least
  4518. * one frame before enabling scaling. LP watermarks can be re-enabled
  4519. * when scaling is disabled.
  4520. *
  4521. * WaCxSRDisabledForSpriteScaling:ivb
  4522. */
  4523. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4524. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4525. /*
  4526. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4527. * watermark programming here.
  4528. */
  4529. if (needs_modeset(&pipe_config->base))
  4530. return;
  4531. /*
  4532. * For platforms that support atomic watermarks, program the
  4533. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4534. * will be the intermediate values that are safe for both pre- and
  4535. * post- vblank; when vblank happens, the 'active' values will be set
  4536. * to the final 'target' values and we'll do this again to get the
  4537. * optimal watermarks. For gen9+ platforms, the values we program here
  4538. * will be the final target values which will get automatically latched
  4539. * at vblank time; no further programming will be necessary.
  4540. *
  4541. * If a platform hasn't been transitioned to atomic watermarks yet,
  4542. * we'll continue to update watermarks the old way, if flags tell
  4543. * us to.
  4544. */
  4545. if (dev_priv->display.initial_watermarks != NULL)
  4546. dev_priv->display.initial_watermarks(old_intel_state,
  4547. pipe_config);
  4548. else if (pipe_config->update_wm_pre)
  4549. intel_update_watermarks(crtc);
  4550. }
  4551. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4552. {
  4553. struct drm_device *dev = crtc->dev;
  4554. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4555. struct drm_plane *p;
  4556. int pipe = intel_crtc->pipe;
  4557. intel_crtc_dpms_overlay_disable(intel_crtc);
  4558. drm_for_each_plane_mask(p, dev, plane_mask)
  4559. to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
  4560. /*
  4561. * FIXME: Once we grow proper nuclear flip support out of this we need
  4562. * to compute the mask of flip planes precisely. For the time being
  4563. * consider this a flip to a NULL plane.
  4564. */
  4565. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4566. }
  4567. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4568. struct intel_crtc_state *crtc_state,
  4569. struct drm_atomic_state *old_state)
  4570. {
  4571. struct drm_connector_state *conn_state;
  4572. struct drm_connector *conn;
  4573. int i;
  4574. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4575. struct intel_encoder *encoder =
  4576. to_intel_encoder(conn_state->best_encoder);
  4577. if (conn_state->crtc != crtc)
  4578. continue;
  4579. if (encoder->pre_pll_enable)
  4580. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4581. }
  4582. }
  4583. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4584. struct intel_crtc_state *crtc_state,
  4585. struct drm_atomic_state *old_state)
  4586. {
  4587. struct drm_connector_state *conn_state;
  4588. struct drm_connector *conn;
  4589. int i;
  4590. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4591. struct intel_encoder *encoder =
  4592. to_intel_encoder(conn_state->best_encoder);
  4593. if (conn_state->crtc != crtc)
  4594. continue;
  4595. if (encoder->pre_enable)
  4596. encoder->pre_enable(encoder, crtc_state, conn_state);
  4597. }
  4598. }
  4599. static void intel_encoders_enable(struct drm_crtc *crtc,
  4600. struct intel_crtc_state *crtc_state,
  4601. struct drm_atomic_state *old_state)
  4602. {
  4603. struct drm_connector_state *conn_state;
  4604. struct drm_connector *conn;
  4605. int i;
  4606. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4607. struct intel_encoder *encoder =
  4608. to_intel_encoder(conn_state->best_encoder);
  4609. if (conn_state->crtc != crtc)
  4610. continue;
  4611. encoder->enable(encoder, crtc_state, conn_state);
  4612. intel_opregion_notify_encoder(encoder, true);
  4613. }
  4614. }
  4615. static void intel_encoders_disable(struct drm_crtc *crtc,
  4616. struct intel_crtc_state *old_crtc_state,
  4617. struct drm_atomic_state *old_state)
  4618. {
  4619. struct drm_connector_state *old_conn_state;
  4620. struct drm_connector *conn;
  4621. int i;
  4622. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4623. struct intel_encoder *encoder =
  4624. to_intel_encoder(old_conn_state->best_encoder);
  4625. if (old_conn_state->crtc != crtc)
  4626. continue;
  4627. intel_opregion_notify_encoder(encoder, false);
  4628. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4629. }
  4630. }
  4631. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4632. struct intel_crtc_state *old_crtc_state,
  4633. struct drm_atomic_state *old_state)
  4634. {
  4635. struct drm_connector_state *old_conn_state;
  4636. struct drm_connector *conn;
  4637. int i;
  4638. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4639. struct intel_encoder *encoder =
  4640. to_intel_encoder(old_conn_state->best_encoder);
  4641. if (old_conn_state->crtc != crtc)
  4642. continue;
  4643. if (encoder->post_disable)
  4644. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4645. }
  4646. }
  4647. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4648. struct intel_crtc_state *old_crtc_state,
  4649. struct drm_atomic_state *old_state)
  4650. {
  4651. struct drm_connector_state *old_conn_state;
  4652. struct drm_connector *conn;
  4653. int i;
  4654. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4655. struct intel_encoder *encoder =
  4656. to_intel_encoder(old_conn_state->best_encoder);
  4657. if (old_conn_state->crtc != crtc)
  4658. continue;
  4659. if (encoder->post_pll_disable)
  4660. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4661. }
  4662. }
  4663. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4664. struct drm_atomic_state *old_state)
  4665. {
  4666. struct drm_crtc *crtc = pipe_config->base.crtc;
  4667. struct drm_device *dev = crtc->dev;
  4668. struct drm_i915_private *dev_priv = to_i915(dev);
  4669. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4670. int pipe = intel_crtc->pipe;
  4671. struct intel_atomic_state *old_intel_state =
  4672. to_intel_atomic_state(old_state);
  4673. if (WARN_ON(intel_crtc->active))
  4674. return;
  4675. /*
  4676. * Sometimes spurious CPU pipe underruns happen during FDI
  4677. * training, at least with VGA+HDMI cloning. Suppress them.
  4678. *
  4679. * On ILK we get an occasional spurious CPU pipe underruns
  4680. * between eDP port A enable and vdd enable. Also PCH port
  4681. * enable seems to result in the occasional CPU pipe underrun.
  4682. *
  4683. * Spurious PCH underruns also occur during PCH enabling.
  4684. */
  4685. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4686. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4687. if (intel_crtc->config->has_pch_encoder)
  4688. intel_prepare_shared_dpll(intel_crtc);
  4689. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4690. intel_dp_set_m_n(intel_crtc, M1_N1);
  4691. intel_set_pipe_timings(intel_crtc);
  4692. intel_set_pipe_src_size(intel_crtc);
  4693. if (intel_crtc->config->has_pch_encoder) {
  4694. intel_cpu_transcoder_set_m_n(intel_crtc,
  4695. &intel_crtc->config->fdi_m_n, NULL);
  4696. }
  4697. ironlake_set_pipeconf(crtc);
  4698. intel_crtc->active = true;
  4699. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4700. if (intel_crtc->config->has_pch_encoder) {
  4701. /* Note: FDI PLL enabling _must_ be done before we enable the
  4702. * cpu pipes, hence this is separate from all the other fdi/pch
  4703. * enabling. */
  4704. ironlake_fdi_pll_enable(intel_crtc);
  4705. } else {
  4706. assert_fdi_tx_disabled(dev_priv, pipe);
  4707. assert_fdi_rx_disabled(dev_priv, pipe);
  4708. }
  4709. ironlake_pfit_enable(intel_crtc);
  4710. /*
  4711. * On ILK+ LUT must be loaded before the pipe is running but with
  4712. * clocks enabled
  4713. */
  4714. intel_color_load_luts(&pipe_config->base);
  4715. if (dev_priv->display.initial_watermarks != NULL)
  4716. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4717. intel_enable_pipe(pipe_config);
  4718. if (intel_crtc->config->has_pch_encoder)
  4719. ironlake_pch_enable(old_intel_state, pipe_config);
  4720. assert_vblank_disabled(crtc);
  4721. drm_crtc_vblank_on(crtc);
  4722. intel_encoders_enable(crtc, pipe_config, old_state);
  4723. if (HAS_PCH_CPT(dev_priv))
  4724. cpt_verify_modeset(dev, intel_crtc->pipe);
  4725. /*
  4726. * Must wait for vblank to avoid spurious PCH FIFO underruns.
  4727. * And a second vblank wait is needed at least on ILK with
  4728. * some interlaced HDMI modes. Let's do the double wait always
  4729. * in case there are more corner cases we don't know about.
  4730. */
  4731. if (intel_crtc->config->has_pch_encoder) {
  4732. intel_wait_for_vblank(dev_priv, pipe);
  4733. intel_wait_for_vblank(dev_priv, pipe);
  4734. }
  4735. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4736. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4737. }
  4738. /* IPS only exists on ULT machines and is tied to pipe A. */
  4739. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4740. {
  4741. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4742. }
  4743. static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
  4744. enum pipe pipe, bool apply)
  4745. {
  4746. u32 val = I915_READ(CLKGATE_DIS_PSL(pipe));
  4747. u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
  4748. if (apply)
  4749. val |= mask;
  4750. else
  4751. val &= ~mask;
  4752. I915_WRITE(CLKGATE_DIS_PSL(pipe), val);
  4753. }
  4754. static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
  4755. {
  4756. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  4757. enum pipe pipe = crtc->pipe;
  4758. uint32_t val;
  4759. val = MBUS_DBOX_BW_CREDIT(1) | MBUS_DBOX_A_CREDIT(2);
  4760. /* Program B credit equally to all pipes */
  4761. val |= MBUS_DBOX_B_CREDIT(24 / INTEL_INFO(dev_priv)->num_pipes);
  4762. I915_WRITE(PIPE_MBUS_DBOX_CTL(pipe), val);
  4763. }
  4764. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4765. struct drm_atomic_state *old_state)
  4766. {
  4767. struct drm_crtc *crtc = pipe_config->base.crtc;
  4768. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4769. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4770. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4771. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4772. struct intel_atomic_state *old_intel_state =
  4773. to_intel_atomic_state(old_state);
  4774. bool psl_clkgate_wa;
  4775. if (WARN_ON(intel_crtc->active))
  4776. return;
  4777. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4778. if (intel_crtc->config->shared_dpll)
  4779. intel_enable_shared_dpll(intel_crtc);
  4780. if (INTEL_GEN(dev_priv) >= 11)
  4781. icl_map_plls_to_ports(crtc, pipe_config, old_state);
  4782. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4783. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4784. intel_dp_set_m_n(intel_crtc, M1_N1);
  4785. if (!transcoder_is_dsi(cpu_transcoder))
  4786. intel_set_pipe_timings(intel_crtc);
  4787. intel_set_pipe_src_size(intel_crtc);
  4788. if (cpu_transcoder != TRANSCODER_EDP &&
  4789. !transcoder_is_dsi(cpu_transcoder)) {
  4790. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4791. intel_crtc->config->pixel_multiplier - 1);
  4792. }
  4793. if (intel_crtc->config->has_pch_encoder) {
  4794. intel_cpu_transcoder_set_m_n(intel_crtc,
  4795. &intel_crtc->config->fdi_m_n, NULL);
  4796. }
  4797. if (!transcoder_is_dsi(cpu_transcoder))
  4798. haswell_set_pipeconf(crtc);
  4799. haswell_set_pipemisc(crtc);
  4800. intel_color_set_csc(&pipe_config->base);
  4801. intel_crtc->active = true;
  4802. /* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
  4803. psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
  4804. intel_crtc->config->pch_pfit.enabled;
  4805. if (psl_clkgate_wa)
  4806. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
  4807. if (INTEL_GEN(dev_priv) >= 9)
  4808. skylake_pfit_enable(intel_crtc);
  4809. else
  4810. ironlake_pfit_enable(intel_crtc);
  4811. /*
  4812. * On ILK+ LUT must be loaded before the pipe is running but with
  4813. * clocks enabled
  4814. */
  4815. intel_color_load_luts(&pipe_config->base);
  4816. intel_ddi_set_pipe_settings(pipe_config);
  4817. if (!transcoder_is_dsi(cpu_transcoder))
  4818. intel_ddi_enable_transcoder_func(pipe_config);
  4819. if (dev_priv->display.initial_watermarks != NULL)
  4820. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4821. if (INTEL_GEN(dev_priv) >= 11)
  4822. icl_pipe_mbus_enable(intel_crtc);
  4823. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4824. if (!transcoder_is_dsi(cpu_transcoder))
  4825. intel_enable_pipe(pipe_config);
  4826. if (intel_crtc->config->has_pch_encoder)
  4827. lpt_pch_enable(old_intel_state, pipe_config);
  4828. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4829. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4830. assert_vblank_disabled(crtc);
  4831. drm_crtc_vblank_on(crtc);
  4832. intel_encoders_enable(crtc, pipe_config, old_state);
  4833. if (psl_clkgate_wa) {
  4834. intel_wait_for_vblank(dev_priv, pipe);
  4835. glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
  4836. }
  4837. /* If we change the relative order between pipe/planes enabling, we need
  4838. * to change the workaround. */
  4839. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4840. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4841. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4842. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4843. }
  4844. }
  4845. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4846. {
  4847. struct drm_device *dev = crtc->base.dev;
  4848. struct drm_i915_private *dev_priv = to_i915(dev);
  4849. int pipe = crtc->pipe;
  4850. /* To avoid upsetting the power well on haswell only disable the pfit if
  4851. * it's in use. The hw state code will make sure we get this right. */
  4852. if (force || crtc->config->pch_pfit.enabled) {
  4853. I915_WRITE(PF_CTL(pipe), 0);
  4854. I915_WRITE(PF_WIN_POS(pipe), 0);
  4855. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4856. }
  4857. }
  4858. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4859. struct drm_atomic_state *old_state)
  4860. {
  4861. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4862. struct drm_device *dev = crtc->dev;
  4863. struct drm_i915_private *dev_priv = to_i915(dev);
  4864. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4865. int pipe = intel_crtc->pipe;
  4866. /*
  4867. * Sometimes spurious CPU pipe underruns happen when the
  4868. * pipe is already disabled, but FDI RX/TX is still enabled.
  4869. * Happens at least with VGA+HDMI cloning. Suppress them.
  4870. */
  4871. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4872. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4873. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4874. drm_crtc_vblank_off(crtc);
  4875. assert_vblank_disabled(crtc);
  4876. intel_disable_pipe(old_crtc_state);
  4877. ironlake_pfit_disable(intel_crtc, false);
  4878. if (intel_crtc->config->has_pch_encoder)
  4879. ironlake_fdi_disable(crtc);
  4880. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4881. if (intel_crtc->config->has_pch_encoder) {
  4882. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4883. if (HAS_PCH_CPT(dev_priv)) {
  4884. i915_reg_t reg;
  4885. u32 temp;
  4886. /* disable TRANS_DP_CTL */
  4887. reg = TRANS_DP_CTL(pipe);
  4888. temp = I915_READ(reg);
  4889. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4890. TRANS_DP_PORT_SEL_MASK);
  4891. temp |= TRANS_DP_PORT_SEL_NONE;
  4892. I915_WRITE(reg, temp);
  4893. /* disable DPLL_SEL */
  4894. temp = I915_READ(PCH_DPLL_SEL);
  4895. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4896. I915_WRITE(PCH_DPLL_SEL, temp);
  4897. }
  4898. ironlake_fdi_pll_disable(intel_crtc);
  4899. }
  4900. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4901. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4902. }
  4903. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4904. struct drm_atomic_state *old_state)
  4905. {
  4906. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4907. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4908. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4909. enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
  4910. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4911. drm_crtc_vblank_off(crtc);
  4912. assert_vblank_disabled(crtc);
  4913. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4914. if (!transcoder_is_dsi(cpu_transcoder))
  4915. intel_disable_pipe(old_crtc_state);
  4916. if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
  4917. intel_ddi_set_vc_payload_alloc(old_crtc_state, false);
  4918. if (!transcoder_is_dsi(cpu_transcoder))
  4919. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4920. if (INTEL_GEN(dev_priv) >= 9)
  4921. skylake_scaler_disable(intel_crtc);
  4922. else
  4923. ironlake_pfit_disable(intel_crtc, false);
  4924. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4925. if (INTEL_GEN(dev_priv) >= 11)
  4926. icl_unmap_plls_to_ports(crtc, old_crtc_state, old_state);
  4927. }
  4928. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4929. {
  4930. struct drm_device *dev = crtc->base.dev;
  4931. struct drm_i915_private *dev_priv = to_i915(dev);
  4932. struct intel_crtc_state *pipe_config = crtc->config;
  4933. if (!pipe_config->gmch_pfit.control)
  4934. return;
  4935. /*
  4936. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4937. * according to register description and PRM.
  4938. */
  4939. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4940. assert_pipe_disabled(dev_priv, crtc->pipe);
  4941. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4942. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4943. /* Border color in case we don't scale up to the full screen. Black by
  4944. * default, change to something else for debugging. */
  4945. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4946. }
  4947. bool intel_port_is_tc(struct drm_i915_private *dev_priv, enum port port)
  4948. {
  4949. if (IS_ICELAKE(dev_priv))
  4950. return port >= PORT_C && port <= PORT_F;
  4951. return false;
  4952. }
  4953. enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
  4954. {
  4955. if (!intel_port_is_tc(dev_priv, port))
  4956. return PORT_TC_NONE;
  4957. return port - PORT_C;
  4958. }
  4959. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4960. {
  4961. switch (port) {
  4962. case PORT_A:
  4963. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4964. case PORT_B:
  4965. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4966. case PORT_C:
  4967. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4968. case PORT_D:
  4969. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4970. case PORT_E:
  4971. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4972. case PORT_F:
  4973. return POWER_DOMAIN_PORT_DDI_F_LANES;
  4974. default:
  4975. MISSING_CASE(port);
  4976. return POWER_DOMAIN_PORT_OTHER;
  4977. }
  4978. }
  4979. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4980. struct intel_crtc_state *crtc_state)
  4981. {
  4982. struct drm_device *dev = crtc->dev;
  4983. struct drm_i915_private *dev_priv = to_i915(dev);
  4984. struct drm_encoder *encoder;
  4985. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4986. enum pipe pipe = intel_crtc->pipe;
  4987. u64 mask;
  4988. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4989. if (!crtc_state->base.active)
  4990. return 0;
  4991. mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
  4992. mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
  4993. if (crtc_state->pch_pfit.enabled ||
  4994. crtc_state->pch_pfit.force_thru)
  4995. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4996. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4997. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4998. mask |= BIT_ULL(intel_encoder->power_domain);
  4999. }
  5000. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  5001. mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
  5002. if (crtc_state->shared_dpll)
  5003. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  5004. return mask;
  5005. }
  5006. static u64
  5007. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  5008. struct intel_crtc_state *crtc_state)
  5009. {
  5010. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5011. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5012. enum intel_display_power_domain domain;
  5013. u64 domains, new_domains, old_domains;
  5014. old_domains = intel_crtc->enabled_power_domains;
  5015. intel_crtc->enabled_power_domains = new_domains =
  5016. get_crtc_power_domains(crtc, crtc_state);
  5017. domains = new_domains & ~old_domains;
  5018. for_each_power_domain(domain, domains)
  5019. intel_display_power_get(dev_priv, domain);
  5020. return old_domains & ~new_domains;
  5021. }
  5022. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  5023. u64 domains)
  5024. {
  5025. enum intel_display_power_domain domain;
  5026. for_each_power_domain(domain, domains)
  5027. intel_display_power_put(dev_priv, domain);
  5028. }
  5029. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  5030. struct drm_atomic_state *old_state)
  5031. {
  5032. struct intel_atomic_state *old_intel_state =
  5033. to_intel_atomic_state(old_state);
  5034. struct drm_crtc *crtc = pipe_config->base.crtc;
  5035. struct drm_device *dev = crtc->dev;
  5036. struct drm_i915_private *dev_priv = to_i915(dev);
  5037. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5038. int pipe = intel_crtc->pipe;
  5039. if (WARN_ON(intel_crtc->active))
  5040. return;
  5041. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5042. intel_dp_set_m_n(intel_crtc, M1_N1);
  5043. intel_set_pipe_timings(intel_crtc);
  5044. intel_set_pipe_src_size(intel_crtc);
  5045. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  5046. struct drm_i915_private *dev_priv = to_i915(dev);
  5047. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5048. I915_WRITE(CHV_CANVAS(pipe), 0);
  5049. }
  5050. i9xx_set_pipeconf(intel_crtc);
  5051. intel_crtc->active = true;
  5052. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5053. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  5054. if (IS_CHERRYVIEW(dev_priv)) {
  5055. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5056. chv_enable_pll(intel_crtc, intel_crtc->config);
  5057. } else {
  5058. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5059. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5060. }
  5061. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5062. i9xx_pfit_enable(intel_crtc);
  5063. intel_color_load_luts(&pipe_config->base);
  5064. dev_priv->display.initial_watermarks(old_intel_state,
  5065. pipe_config);
  5066. intel_enable_pipe(pipe_config);
  5067. assert_vblank_disabled(crtc);
  5068. drm_crtc_vblank_on(crtc);
  5069. intel_encoders_enable(crtc, pipe_config, old_state);
  5070. }
  5071. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5072. {
  5073. struct drm_device *dev = crtc->base.dev;
  5074. struct drm_i915_private *dev_priv = to_i915(dev);
  5075. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5076. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5077. }
  5078. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  5079. struct drm_atomic_state *old_state)
  5080. {
  5081. struct intel_atomic_state *old_intel_state =
  5082. to_intel_atomic_state(old_state);
  5083. struct drm_crtc *crtc = pipe_config->base.crtc;
  5084. struct drm_device *dev = crtc->dev;
  5085. struct drm_i915_private *dev_priv = to_i915(dev);
  5086. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5087. enum pipe pipe = intel_crtc->pipe;
  5088. if (WARN_ON(intel_crtc->active))
  5089. return;
  5090. i9xx_set_pll_dividers(intel_crtc);
  5091. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5092. intel_dp_set_m_n(intel_crtc, M1_N1);
  5093. intel_set_pipe_timings(intel_crtc);
  5094. intel_set_pipe_src_size(intel_crtc);
  5095. i9xx_set_pipeconf(intel_crtc);
  5096. intel_crtc->active = true;
  5097. if (!IS_GEN2(dev_priv))
  5098. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5099. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5100. i9xx_enable_pll(intel_crtc, pipe_config);
  5101. i9xx_pfit_enable(intel_crtc);
  5102. intel_color_load_luts(&pipe_config->base);
  5103. if (dev_priv->display.initial_watermarks != NULL)
  5104. dev_priv->display.initial_watermarks(old_intel_state,
  5105. intel_crtc->config);
  5106. else
  5107. intel_update_watermarks(intel_crtc);
  5108. intel_enable_pipe(pipe_config);
  5109. assert_vblank_disabled(crtc);
  5110. drm_crtc_vblank_on(crtc);
  5111. intel_encoders_enable(crtc, pipe_config, old_state);
  5112. }
  5113. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5114. {
  5115. struct drm_device *dev = crtc->base.dev;
  5116. struct drm_i915_private *dev_priv = to_i915(dev);
  5117. if (!crtc->config->gmch_pfit.control)
  5118. return;
  5119. assert_pipe_disabled(dev_priv, crtc->pipe);
  5120. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5121. I915_READ(PFIT_CONTROL));
  5122. I915_WRITE(PFIT_CONTROL, 0);
  5123. }
  5124. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  5125. struct drm_atomic_state *old_state)
  5126. {
  5127. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5128. struct drm_device *dev = crtc->dev;
  5129. struct drm_i915_private *dev_priv = to_i915(dev);
  5130. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5131. int pipe = intel_crtc->pipe;
  5132. /*
  5133. * On gen2 planes are double buffered but the pipe isn't, so we must
  5134. * wait for planes to fully turn off before disabling the pipe.
  5135. */
  5136. if (IS_GEN2(dev_priv))
  5137. intel_wait_for_vblank(dev_priv, pipe);
  5138. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5139. drm_crtc_vblank_off(crtc);
  5140. assert_vblank_disabled(crtc);
  5141. intel_disable_pipe(old_crtc_state);
  5142. i9xx_pfit_disable(intel_crtc);
  5143. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5144. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5145. if (IS_CHERRYVIEW(dev_priv))
  5146. chv_disable_pll(dev_priv, pipe);
  5147. else if (IS_VALLEYVIEW(dev_priv))
  5148. vlv_disable_pll(dev_priv, pipe);
  5149. else
  5150. i9xx_disable_pll(intel_crtc);
  5151. }
  5152. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5153. if (!IS_GEN2(dev_priv))
  5154. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5155. if (!dev_priv->display.initial_watermarks)
  5156. intel_update_watermarks(intel_crtc);
  5157. /* clock the pipe down to 640x480@60 to potentially save power */
  5158. if (IS_I830(dev_priv))
  5159. i830_enable_pipe(dev_priv, pipe);
  5160. }
  5161. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc,
  5162. struct drm_modeset_acquire_ctx *ctx)
  5163. {
  5164. struct intel_encoder *encoder;
  5165. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5166. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5167. enum intel_display_power_domain domain;
  5168. struct intel_plane *plane;
  5169. u64 domains;
  5170. struct drm_atomic_state *state;
  5171. struct intel_crtc_state *crtc_state;
  5172. int ret;
  5173. if (!intel_crtc->active)
  5174. return;
  5175. for_each_intel_plane_on_crtc(&dev_priv->drm, intel_crtc, plane) {
  5176. const struct intel_plane_state *plane_state =
  5177. to_intel_plane_state(plane->base.state);
  5178. if (plane_state->base.visible)
  5179. intel_plane_disable_noatomic(intel_crtc, plane);
  5180. }
  5181. state = drm_atomic_state_alloc(crtc->dev);
  5182. if (!state) {
  5183. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  5184. crtc->base.id, crtc->name);
  5185. return;
  5186. }
  5187. state->acquire_ctx = ctx;
  5188. /* Everything's already locked, -EDEADLK can't happen. */
  5189. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5190. ret = drm_atomic_add_affected_connectors(state, crtc);
  5191. WARN_ON(IS_ERR(crtc_state) || ret);
  5192. dev_priv->display.crtc_disable(crtc_state, state);
  5193. drm_atomic_state_put(state);
  5194. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5195. crtc->base.id, crtc->name);
  5196. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5197. crtc->state->active = false;
  5198. intel_crtc->active = false;
  5199. crtc->enabled = false;
  5200. crtc->state->connector_mask = 0;
  5201. crtc->state->encoder_mask = 0;
  5202. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5203. encoder->base.crtc = NULL;
  5204. intel_fbc_disable(intel_crtc);
  5205. intel_update_watermarks(intel_crtc);
  5206. intel_disable_shared_dpll(intel_crtc);
  5207. domains = intel_crtc->enabled_power_domains;
  5208. for_each_power_domain(domain, domains)
  5209. intel_display_power_put(dev_priv, domain);
  5210. intel_crtc->enabled_power_domains = 0;
  5211. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5212. dev_priv->min_cdclk[intel_crtc->pipe] = 0;
  5213. dev_priv->min_voltage_level[intel_crtc->pipe] = 0;
  5214. }
  5215. /*
  5216. * turn all crtc's off, but do not adjust state
  5217. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5218. */
  5219. int intel_display_suspend(struct drm_device *dev)
  5220. {
  5221. struct drm_i915_private *dev_priv = to_i915(dev);
  5222. struct drm_atomic_state *state;
  5223. int ret;
  5224. state = drm_atomic_helper_suspend(dev);
  5225. ret = PTR_ERR_OR_ZERO(state);
  5226. if (ret)
  5227. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5228. else
  5229. dev_priv->modeset_restore_state = state;
  5230. return ret;
  5231. }
  5232. void intel_encoder_destroy(struct drm_encoder *encoder)
  5233. {
  5234. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5235. drm_encoder_cleanup(encoder);
  5236. kfree(intel_encoder);
  5237. }
  5238. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5239. * internal consistency). */
  5240. static void intel_connector_verify_state(struct drm_crtc_state *crtc_state,
  5241. struct drm_connector_state *conn_state)
  5242. {
  5243. struct intel_connector *connector = to_intel_connector(conn_state->connector);
  5244. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5245. connector->base.base.id,
  5246. connector->base.name);
  5247. if (connector->get_hw_state(connector)) {
  5248. struct intel_encoder *encoder = connector->encoder;
  5249. I915_STATE_WARN(!crtc_state,
  5250. "connector enabled without attached crtc\n");
  5251. if (!crtc_state)
  5252. return;
  5253. I915_STATE_WARN(!crtc_state->active,
  5254. "connector is active, but attached crtc isn't\n");
  5255. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5256. return;
  5257. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5258. "atomic encoder doesn't match attached encoder\n");
  5259. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5260. "attached encoder crtc differs from connector crtc\n");
  5261. } else {
  5262. I915_STATE_WARN(crtc_state && crtc_state->active,
  5263. "attached crtc is active, but connector isn't\n");
  5264. I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
  5265. "best encoder set without crtc!\n");
  5266. }
  5267. }
  5268. int intel_connector_init(struct intel_connector *connector)
  5269. {
  5270. struct intel_digital_connector_state *conn_state;
  5271. /*
  5272. * Allocate enough memory to hold intel_digital_connector_state,
  5273. * This might be a few bytes too many, but for connectors that don't
  5274. * need it we'll free the state and allocate a smaller one on the first
  5275. * succesful commit anyway.
  5276. */
  5277. conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
  5278. if (!conn_state)
  5279. return -ENOMEM;
  5280. __drm_atomic_helper_connector_reset(&connector->base,
  5281. &conn_state->base);
  5282. return 0;
  5283. }
  5284. struct intel_connector *intel_connector_alloc(void)
  5285. {
  5286. struct intel_connector *connector;
  5287. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5288. if (!connector)
  5289. return NULL;
  5290. if (intel_connector_init(connector) < 0) {
  5291. kfree(connector);
  5292. return NULL;
  5293. }
  5294. return connector;
  5295. }
  5296. /*
  5297. * Free the bits allocated by intel_connector_alloc.
  5298. * This should only be used after intel_connector_alloc has returned
  5299. * successfully, and before drm_connector_init returns successfully.
  5300. * Otherwise the destroy callbacks for the connector and the state should
  5301. * take care of proper cleanup/free
  5302. */
  5303. void intel_connector_free(struct intel_connector *connector)
  5304. {
  5305. kfree(to_intel_digital_connector_state(connector->base.state));
  5306. kfree(connector);
  5307. }
  5308. /* Simple connector->get_hw_state implementation for encoders that support only
  5309. * one connector and no cloning and hence the encoder state determines the state
  5310. * of the connector. */
  5311. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5312. {
  5313. enum pipe pipe = 0;
  5314. struct intel_encoder *encoder = connector->encoder;
  5315. return encoder->get_hw_state(encoder, &pipe);
  5316. }
  5317. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5318. {
  5319. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5320. return crtc_state->fdi_lanes;
  5321. return 0;
  5322. }
  5323. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5324. struct intel_crtc_state *pipe_config)
  5325. {
  5326. struct drm_i915_private *dev_priv = to_i915(dev);
  5327. struct drm_atomic_state *state = pipe_config->base.state;
  5328. struct intel_crtc *other_crtc;
  5329. struct intel_crtc_state *other_crtc_state;
  5330. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5331. pipe_name(pipe), pipe_config->fdi_lanes);
  5332. if (pipe_config->fdi_lanes > 4) {
  5333. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5334. pipe_name(pipe), pipe_config->fdi_lanes);
  5335. return -EINVAL;
  5336. }
  5337. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5338. if (pipe_config->fdi_lanes > 2) {
  5339. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5340. pipe_config->fdi_lanes);
  5341. return -EINVAL;
  5342. } else {
  5343. return 0;
  5344. }
  5345. }
  5346. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5347. return 0;
  5348. /* Ivybridge 3 pipe is really complicated */
  5349. switch (pipe) {
  5350. case PIPE_A:
  5351. return 0;
  5352. case PIPE_B:
  5353. if (pipe_config->fdi_lanes <= 2)
  5354. return 0;
  5355. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5356. other_crtc_state =
  5357. intel_atomic_get_crtc_state(state, other_crtc);
  5358. if (IS_ERR(other_crtc_state))
  5359. return PTR_ERR(other_crtc_state);
  5360. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5361. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5362. pipe_name(pipe), pipe_config->fdi_lanes);
  5363. return -EINVAL;
  5364. }
  5365. return 0;
  5366. case PIPE_C:
  5367. if (pipe_config->fdi_lanes > 2) {
  5368. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5369. pipe_name(pipe), pipe_config->fdi_lanes);
  5370. return -EINVAL;
  5371. }
  5372. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5373. other_crtc_state =
  5374. intel_atomic_get_crtc_state(state, other_crtc);
  5375. if (IS_ERR(other_crtc_state))
  5376. return PTR_ERR(other_crtc_state);
  5377. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5378. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5379. return -EINVAL;
  5380. }
  5381. return 0;
  5382. default:
  5383. BUG();
  5384. }
  5385. }
  5386. #define RETRY 1
  5387. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5388. struct intel_crtc_state *pipe_config)
  5389. {
  5390. struct drm_device *dev = intel_crtc->base.dev;
  5391. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5392. int lane, link_bw, fdi_dotclock, ret;
  5393. bool needs_recompute = false;
  5394. retry:
  5395. /* FDI is a binary signal running at ~2.7GHz, encoding
  5396. * each output octet as 10 bits. The actual frequency
  5397. * is stored as a divider into a 100MHz clock, and the
  5398. * mode pixel clock is stored in units of 1KHz.
  5399. * Hence the bw of each lane in terms of the mode signal
  5400. * is:
  5401. */
  5402. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5403. fdi_dotclock = adjusted_mode->crtc_clock;
  5404. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5405. pipe_config->pipe_bpp);
  5406. pipe_config->fdi_lanes = lane;
  5407. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5408. link_bw, &pipe_config->fdi_m_n, false);
  5409. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5410. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5411. pipe_config->pipe_bpp -= 2*3;
  5412. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5413. pipe_config->pipe_bpp);
  5414. needs_recompute = true;
  5415. pipe_config->bw_constrained = true;
  5416. goto retry;
  5417. }
  5418. if (needs_recompute)
  5419. return RETRY;
  5420. return ret;
  5421. }
  5422. bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
  5423. {
  5424. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  5425. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5426. /* IPS only exists on ULT machines and is tied to pipe A. */
  5427. if (!hsw_crtc_supports_ips(crtc))
  5428. return false;
  5429. if (!i915_modparams.enable_ips)
  5430. return false;
  5431. if (crtc_state->pipe_bpp > 24)
  5432. return false;
  5433. /*
  5434. * We compare against max which means we must take
  5435. * the increased cdclk requirement into account when
  5436. * calculating the new cdclk.
  5437. *
  5438. * Should measure whether using a lower cdclk w/o IPS
  5439. */
  5440. if (IS_BROADWELL(dev_priv) &&
  5441. crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
  5442. return false;
  5443. return true;
  5444. }
  5445. static bool hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
  5446. {
  5447. struct drm_i915_private *dev_priv =
  5448. to_i915(crtc_state->base.crtc->dev);
  5449. struct intel_atomic_state *intel_state =
  5450. to_intel_atomic_state(crtc_state->base.state);
  5451. if (!hsw_crtc_state_ips_capable(crtc_state))
  5452. return false;
  5453. if (crtc_state->ips_force_disable)
  5454. return false;
  5455. /* IPS should be fine as long as at least one plane is enabled. */
  5456. if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
  5457. return false;
  5458. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  5459. if (IS_BROADWELL(dev_priv) &&
  5460. crtc_state->pixel_rate > intel_state->cdclk.logical.cdclk * 95 / 100)
  5461. return false;
  5462. return true;
  5463. }
  5464. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5465. {
  5466. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5467. /* GDG double wide on either pipe, otherwise pipe A only */
  5468. return INTEL_GEN(dev_priv) < 4 &&
  5469. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5470. }
  5471. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5472. {
  5473. uint32_t pixel_rate;
  5474. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5475. /*
  5476. * We only use IF-ID interlacing. If we ever use
  5477. * PF-ID we'll need to adjust the pixel_rate here.
  5478. */
  5479. if (pipe_config->pch_pfit.enabled) {
  5480. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5481. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5482. pipe_w = pipe_config->pipe_src_w;
  5483. pipe_h = pipe_config->pipe_src_h;
  5484. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5485. pfit_h = pfit_size & 0xFFFF;
  5486. if (pipe_w < pfit_w)
  5487. pipe_w = pfit_w;
  5488. if (pipe_h < pfit_h)
  5489. pipe_h = pfit_h;
  5490. if (WARN_ON(!pfit_w || !pfit_h))
  5491. return pixel_rate;
  5492. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5493. pfit_w * pfit_h);
  5494. }
  5495. return pixel_rate;
  5496. }
  5497. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5498. {
  5499. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5500. if (HAS_GMCH_DISPLAY(dev_priv))
  5501. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5502. crtc_state->pixel_rate =
  5503. crtc_state->base.adjusted_mode.crtc_clock;
  5504. else
  5505. crtc_state->pixel_rate =
  5506. ilk_pipe_pixel_rate(crtc_state);
  5507. }
  5508. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5509. struct intel_crtc_state *pipe_config)
  5510. {
  5511. struct drm_device *dev = crtc->base.dev;
  5512. struct drm_i915_private *dev_priv = to_i915(dev);
  5513. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5514. int clock_limit = dev_priv->max_dotclk_freq;
  5515. if (INTEL_GEN(dev_priv) < 4) {
  5516. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5517. /*
  5518. * Enable double wide mode when the dot clock
  5519. * is > 90% of the (display) core speed.
  5520. */
  5521. if (intel_crtc_supports_double_wide(crtc) &&
  5522. adjusted_mode->crtc_clock > clock_limit) {
  5523. clock_limit = dev_priv->max_dotclk_freq;
  5524. pipe_config->double_wide = true;
  5525. }
  5526. }
  5527. if (adjusted_mode->crtc_clock > clock_limit) {
  5528. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5529. adjusted_mode->crtc_clock, clock_limit,
  5530. yesno(pipe_config->double_wide));
  5531. return -EINVAL;
  5532. }
  5533. if (pipe_config->ycbcr420 && pipe_config->base.ctm) {
  5534. /*
  5535. * There is only one pipe CSC unit per pipe, and we need that
  5536. * for output conversion from RGB->YCBCR. So if CTM is already
  5537. * applied we can't support YCBCR420 output.
  5538. */
  5539. DRM_DEBUG_KMS("YCBCR420 and CTM together are not possible\n");
  5540. return -EINVAL;
  5541. }
  5542. /*
  5543. * Pipe horizontal size must be even in:
  5544. * - DVO ganged mode
  5545. * - LVDS dual channel mode
  5546. * - Double wide pipe
  5547. */
  5548. if (pipe_config->pipe_src_w & 1) {
  5549. if (pipe_config->double_wide) {
  5550. DRM_DEBUG_KMS("Odd pipe source width not supported with double wide pipe\n");
  5551. return -EINVAL;
  5552. }
  5553. if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5554. intel_is_dual_link_lvds(dev)) {
  5555. DRM_DEBUG_KMS("Odd pipe source width not supported with dual link LVDS\n");
  5556. return -EINVAL;
  5557. }
  5558. }
  5559. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5560. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5561. */
  5562. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5563. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5564. return -EINVAL;
  5565. intel_crtc_compute_pixel_rate(pipe_config);
  5566. if (pipe_config->has_pch_encoder)
  5567. return ironlake_fdi_compute_config(crtc, pipe_config);
  5568. return 0;
  5569. }
  5570. static void
  5571. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5572. {
  5573. while (*num > DATA_LINK_M_N_MASK ||
  5574. *den > DATA_LINK_M_N_MASK) {
  5575. *num >>= 1;
  5576. *den >>= 1;
  5577. }
  5578. }
  5579. static void compute_m_n(unsigned int m, unsigned int n,
  5580. uint32_t *ret_m, uint32_t *ret_n,
  5581. bool reduce_m_n)
  5582. {
  5583. /*
  5584. * Reduce M/N as much as possible without loss in precision. Several DP
  5585. * dongles in particular seem to be fussy about too large *link* M/N
  5586. * values. The passed in values are more likely to have the least
  5587. * significant bits zero than M after rounding below, so do this first.
  5588. */
  5589. if (reduce_m_n) {
  5590. while ((m & 1) == 0 && (n & 1) == 0) {
  5591. m >>= 1;
  5592. n >>= 1;
  5593. }
  5594. }
  5595. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5596. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5597. intel_reduce_m_n_ratio(ret_m, ret_n);
  5598. }
  5599. void
  5600. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5601. int pixel_clock, int link_clock,
  5602. struct intel_link_m_n *m_n,
  5603. bool reduce_m_n)
  5604. {
  5605. m_n->tu = 64;
  5606. compute_m_n(bits_per_pixel * pixel_clock,
  5607. link_clock * nlanes * 8,
  5608. &m_n->gmch_m, &m_n->gmch_n,
  5609. reduce_m_n);
  5610. compute_m_n(pixel_clock, link_clock,
  5611. &m_n->link_m, &m_n->link_n,
  5612. reduce_m_n);
  5613. }
  5614. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5615. {
  5616. if (i915_modparams.panel_use_ssc >= 0)
  5617. return i915_modparams.panel_use_ssc != 0;
  5618. return dev_priv->vbt.lvds_use_ssc
  5619. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5620. }
  5621. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5622. {
  5623. return (1 << dpll->n) << 16 | dpll->m2;
  5624. }
  5625. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5626. {
  5627. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5628. }
  5629. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5630. struct intel_crtc_state *crtc_state,
  5631. struct dpll *reduced_clock)
  5632. {
  5633. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5634. u32 fp, fp2 = 0;
  5635. if (IS_PINEVIEW(dev_priv)) {
  5636. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5637. if (reduced_clock)
  5638. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5639. } else {
  5640. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5641. if (reduced_clock)
  5642. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5643. }
  5644. crtc_state->dpll_hw_state.fp0 = fp;
  5645. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5646. reduced_clock) {
  5647. crtc_state->dpll_hw_state.fp1 = fp2;
  5648. } else {
  5649. crtc_state->dpll_hw_state.fp1 = fp;
  5650. }
  5651. }
  5652. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5653. pipe)
  5654. {
  5655. u32 reg_val;
  5656. /*
  5657. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5658. * and set it to a reasonable value instead.
  5659. */
  5660. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5661. reg_val &= 0xffffff00;
  5662. reg_val |= 0x00000030;
  5663. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5664. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5665. reg_val &= 0x00ffffff;
  5666. reg_val |= 0x8c000000;
  5667. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5668. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5669. reg_val &= 0xffffff00;
  5670. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5671. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5672. reg_val &= 0x00ffffff;
  5673. reg_val |= 0xb0000000;
  5674. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5675. }
  5676. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5677. struct intel_link_m_n *m_n)
  5678. {
  5679. struct drm_device *dev = crtc->base.dev;
  5680. struct drm_i915_private *dev_priv = to_i915(dev);
  5681. int pipe = crtc->pipe;
  5682. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5683. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5684. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5685. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5686. }
  5687. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5688. struct intel_link_m_n *m_n,
  5689. struct intel_link_m_n *m2_n2)
  5690. {
  5691. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5692. int pipe = crtc->pipe;
  5693. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5694. if (INTEL_GEN(dev_priv) >= 5) {
  5695. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5696. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5697. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5698. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5699. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5700. * for gen < 8) and if DRRS is supported (to make sure the
  5701. * registers are not unnecessarily accessed).
  5702. */
  5703. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5704. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5705. I915_WRITE(PIPE_DATA_M2(transcoder),
  5706. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5707. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5708. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5709. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5710. }
  5711. } else {
  5712. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5713. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5714. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5715. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5716. }
  5717. }
  5718. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5719. {
  5720. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5721. if (m_n == M1_N1) {
  5722. dp_m_n = &crtc->config->dp_m_n;
  5723. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5724. } else if (m_n == M2_N2) {
  5725. /*
  5726. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5727. * needs to be programmed into M1_N1.
  5728. */
  5729. dp_m_n = &crtc->config->dp_m2_n2;
  5730. } else {
  5731. DRM_ERROR("Unsupported divider value\n");
  5732. return;
  5733. }
  5734. if (crtc->config->has_pch_encoder)
  5735. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5736. else
  5737. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5738. }
  5739. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5740. struct intel_crtc_state *pipe_config)
  5741. {
  5742. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5743. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5744. if (crtc->pipe != PIPE_A)
  5745. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5746. /* DPLL not used with DSI, but still need the rest set up */
  5747. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5748. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5749. DPLL_EXT_BUFFER_ENABLE_VLV;
  5750. pipe_config->dpll_hw_state.dpll_md =
  5751. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5752. }
  5753. static void chv_compute_dpll(struct intel_crtc *crtc,
  5754. struct intel_crtc_state *pipe_config)
  5755. {
  5756. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5757. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5758. if (crtc->pipe != PIPE_A)
  5759. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5760. /* DPLL not used with DSI, but still need the rest set up */
  5761. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5762. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5763. pipe_config->dpll_hw_state.dpll_md =
  5764. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5765. }
  5766. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5767. const struct intel_crtc_state *pipe_config)
  5768. {
  5769. struct drm_device *dev = crtc->base.dev;
  5770. struct drm_i915_private *dev_priv = to_i915(dev);
  5771. enum pipe pipe = crtc->pipe;
  5772. u32 mdiv;
  5773. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5774. u32 coreclk, reg_val;
  5775. /* Enable Refclk */
  5776. I915_WRITE(DPLL(pipe),
  5777. pipe_config->dpll_hw_state.dpll &
  5778. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5779. /* No need to actually set up the DPLL with DSI */
  5780. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5781. return;
  5782. mutex_lock(&dev_priv->sb_lock);
  5783. bestn = pipe_config->dpll.n;
  5784. bestm1 = pipe_config->dpll.m1;
  5785. bestm2 = pipe_config->dpll.m2;
  5786. bestp1 = pipe_config->dpll.p1;
  5787. bestp2 = pipe_config->dpll.p2;
  5788. /* See eDP HDMI DPIO driver vbios notes doc */
  5789. /* PLL B needs special handling */
  5790. if (pipe == PIPE_B)
  5791. vlv_pllb_recal_opamp(dev_priv, pipe);
  5792. /* Set up Tx target for periodic Rcomp update */
  5793. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5794. /* Disable target IRef on PLL */
  5795. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5796. reg_val &= 0x00ffffff;
  5797. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5798. /* Disable fast lock */
  5799. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5800. /* Set idtafcrecal before PLL is enabled */
  5801. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5802. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5803. mdiv |= ((bestn << DPIO_N_SHIFT));
  5804. mdiv |= (1 << DPIO_K_SHIFT);
  5805. /*
  5806. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5807. * but we don't support that).
  5808. * Note: don't use the DAC post divider as it seems unstable.
  5809. */
  5810. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5811. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5812. mdiv |= DPIO_ENABLE_CALIBRATION;
  5813. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5814. /* Set HBR and RBR LPF coefficients */
  5815. if (pipe_config->port_clock == 162000 ||
  5816. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5817. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5818. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5819. 0x009f0003);
  5820. else
  5821. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5822. 0x00d0000f);
  5823. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5824. /* Use SSC source */
  5825. if (pipe == PIPE_A)
  5826. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5827. 0x0df40000);
  5828. else
  5829. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5830. 0x0df70000);
  5831. } else { /* HDMI or VGA */
  5832. /* Use bend source */
  5833. if (pipe == PIPE_A)
  5834. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5835. 0x0df70000);
  5836. else
  5837. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5838. 0x0df40000);
  5839. }
  5840. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5841. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5842. if (intel_crtc_has_dp_encoder(crtc->config))
  5843. coreclk |= 0x01000000;
  5844. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5845. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5846. mutex_unlock(&dev_priv->sb_lock);
  5847. }
  5848. static void chv_prepare_pll(struct intel_crtc *crtc,
  5849. const struct intel_crtc_state *pipe_config)
  5850. {
  5851. struct drm_device *dev = crtc->base.dev;
  5852. struct drm_i915_private *dev_priv = to_i915(dev);
  5853. enum pipe pipe = crtc->pipe;
  5854. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5855. u32 loopfilter, tribuf_calcntr;
  5856. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5857. u32 dpio_val;
  5858. int vco;
  5859. /* Enable Refclk and SSC */
  5860. I915_WRITE(DPLL(pipe),
  5861. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5862. /* No need to actually set up the DPLL with DSI */
  5863. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5864. return;
  5865. bestn = pipe_config->dpll.n;
  5866. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5867. bestm1 = pipe_config->dpll.m1;
  5868. bestm2 = pipe_config->dpll.m2 >> 22;
  5869. bestp1 = pipe_config->dpll.p1;
  5870. bestp2 = pipe_config->dpll.p2;
  5871. vco = pipe_config->dpll.vco;
  5872. dpio_val = 0;
  5873. loopfilter = 0;
  5874. mutex_lock(&dev_priv->sb_lock);
  5875. /* p1 and p2 divider */
  5876. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5877. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5878. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5879. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5880. 1 << DPIO_CHV_K_DIV_SHIFT);
  5881. /* Feedback post-divider - m2 */
  5882. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5883. /* Feedback refclk divider - n and m1 */
  5884. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5885. DPIO_CHV_M1_DIV_BY_2 |
  5886. 1 << DPIO_CHV_N_DIV_SHIFT);
  5887. /* M2 fraction division */
  5888. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5889. /* M2 fraction division enable */
  5890. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5891. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5892. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5893. if (bestm2_frac)
  5894. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5895. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5896. /* Program digital lock detect threshold */
  5897. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5898. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5899. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5900. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5901. if (!bestm2_frac)
  5902. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5903. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5904. /* Loop filter */
  5905. if (vco == 5400000) {
  5906. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5907. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5908. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5909. tribuf_calcntr = 0x9;
  5910. } else if (vco <= 6200000) {
  5911. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5912. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5913. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5914. tribuf_calcntr = 0x9;
  5915. } else if (vco <= 6480000) {
  5916. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5917. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5918. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5919. tribuf_calcntr = 0x8;
  5920. } else {
  5921. /* Not supported. Apply the same limits as in the max case */
  5922. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5923. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5924. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5925. tribuf_calcntr = 0;
  5926. }
  5927. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5928. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5929. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5930. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5931. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5932. /* AFC Recal */
  5933. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5934. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5935. DPIO_AFC_RECAL);
  5936. mutex_unlock(&dev_priv->sb_lock);
  5937. }
  5938. /**
  5939. * vlv_force_pll_on - forcibly enable just the PLL
  5940. * @dev_priv: i915 private structure
  5941. * @pipe: pipe PLL to enable
  5942. * @dpll: PLL configuration
  5943. *
  5944. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5945. * in cases where we need the PLL enabled even when @pipe is not going to
  5946. * be enabled.
  5947. */
  5948. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5949. const struct dpll *dpll)
  5950. {
  5951. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5952. struct intel_crtc_state *pipe_config;
  5953. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5954. if (!pipe_config)
  5955. return -ENOMEM;
  5956. pipe_config->base.crtc = &crtc->base;
  5957. pipe_config->pixel_multiplier = 1;
  5958. pipe_config->dpll = *dpll;
  5959. if (IS_CHERRYVIEW(dev_priv)) {
  5960. chv_compute_dpll(crtc, pipe_config);
  5961. chv_prepare_pll(crtc, pipe_config);
  5962. chv_enable_pll(crtc, pipe_config);
  5963. } else {
  5964. vlv_compute_dpll(crtc, pipe_config);
  5965. vlv_prepare_pll(crtc, pipe_config);
  5966. vlv_enable_pll(crtc, pipe_config);
  5967. }
  5968. kfree(pipe_config);
  5969. return 0;
  5970. }
  5971. /**
  5972. * vlv_force_pll_off - forcibly disable just the PLL
  5973. * @dev_priv: i915 private structure
  5974. * @pipe: pipe PLL to disable
  5975. *
  5976. * Disable the PLL for @pipe. To be used in cases where we need
  5977. * the PLL enabled even when @pipe is not going to be enabled.
  5978. */
  5979. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5980. {
  5981. if (IS_CHERRYVIEW(dev_priv))
  5982. chv_disable_pll(dev_priv, pipe);
  5983. else
  5984. vlv_disable_pll(dev_priv, pipe);
  5985. }
  5986. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5987. struct intel_crtc_state *crtc_state,
  5988. struct dpll *reduced_clock)
  5989. {
  5990. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5991. u32 dpll;
  5992. struct dpll *clock = &crtc_state->dpll;
  5993. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5994. dpll = DPLL_VGA_MODE_DIS;
  5995. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5996. dpll |= DPLLB_MODE_LVDS;
  5997. else
  5998. dpll |= DPLLB_MODE_DAC_SERIAL;
  5999. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6000. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6001. dpll |= (crtc_state->pixel_multiplier - 1)
  6002. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6003. }
  6004. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6005. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6006. dpll |= DPLL_SDVO_HIGH_SPEED;
  6007. if (intel_crtc_has_dp_encoder(crtc_state))
  6008. dpll |= DPLL_SDVO_HIGH_SPEED;
  6009. /* compute bitmask from p1 value */
  6010. if (IS_PINEVIEW(dev_priv))
  6011. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6012. else {
  6013. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6014. if (IS_G4X(dev_priv) && reduced_clock)
  6015. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6016. }
  6017. switch (clock->p2) {
  6018. case 5:
  6019. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6020. break;
  6021. case 7:
  6022. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6023. break;
  6024. case 10:
  6025. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6026. break;
  6027. case 14:
  6028. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6029. break;
  6030. }
  6031. if (INTEL_GEN(dev_priv) >= 4)
  6032. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6033. if (crtc_state->sdvo_tv_clock)
  6034. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6035. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6036. intel_panel_use_ssc(dev_priv))
  6037. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6038. else
  6039. dpll |= PLL_REF_INPUT_DREFCLK;
  6040. dpll |= DPLL_VCO_ENABLE;
  6041. crtc_state->dpll_hw_state.dpll = dpll;
  6042. if (INTEL_GEN(dev_priv) >= 4) {
  6043. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6044. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6045. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6046. }
  6047. }
  6048. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6049. struct intel_crtc_state *crtc_state,
  6050. struct dpll *reduced_clock)
  6051. {
  6052. struct drm_device *dev = crtc->base.dev;
  6053. struct drm_i915_private *dev_priv = to_i915(dev);
  6054. u32 dpll;
  6055. struct dpll *clock = &crtc_state->dpll;
  6056. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6057. dpll = DPLL_VGA_MODE_DIS;
  6058. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6059. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6060. } else {
  6061. if (clock->p1 == 2)
  6062. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6063. else
  6064. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6065. if (clock->p2 == 4)
  6066. dpll |= PLL_P2_DIVIDE_BY_4;
  6067. }
  6068. if (!IS_I830(dev_priv) &&
  6069. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6070. dpll |= DPLL_DVO_2X_MODE;
  6071. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6072. intel_panel_use_ssc(dev_priv))
  6073. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6074. else
  6075. dpll |= PLL_REF_INPUT_DREFCLK;
  6076. dpll |= DPLL_VCO_ENABLE;
  6077. crtc_state->dpll_hw_state.dpll = dpll;
  6078. }
  6079. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6080. {
  6081. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6082. enum pipe pipe = intel_crtc->pipe;
  6083. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6084. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6085. uint32_t crtc_vtotal, crtc_vblank_end;
  6086. int vsyncshift = 0;
  6087. /* We need to be careful not to changed the adjusted mode, for otherwise
  6088. * the hw state checker will get angry at the mismatch. */
  6089. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6090. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6091. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6092. /* the chip adds 2 halflines automatically */
  6093. crtc_vtotal -= 1;
  6094. crtc_vblank_end -= 1;
  6095. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6096. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6097. else
  6098. vsyncshift = adjusted_mode->crtc_hsync_start -
  6099. adjusted_mode->crtc_htotal / 2;
  6100. if (vsyncshift < 0)
  6101. vsyncshift += adjusted_mode->crtc_htotal;
  6102. }
  6103. if (INTEL_GEN(dev_priv) > 3)
  6104. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6105. I915_WRITE(HTOTAL(cpu_transcoder),
  6106. (adjusted_mode->crtc_hdisplay - 1) |
  6107. ((adjusted_mode->crtc_htotal - 1) << 16));
  6108. I915_WRITE(HBLANK(cpu_transcoder),
  6109. (adjusted_mode->crtc_hblank_start - 1) |
  6110. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6111. I915_WRITE(HSYNC(cpu_transcoder),
  6112. (adjusted_mode->crtc_hsync_start - 1) |
  6113. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6114. I915_WRITE(VTOTAL(cpu_transcoder),
  6115. (adjusted_mode->crtc_vdisplay - 1) |
  6116. ((crtc_vtotal - 1) << 16));
  6117. I915_WRITE(VBLANK(cpu_transcoder),
  6118. (adjusted_mode->crtc_vblank_start - 1) |
  6119. ((crtc_vblank_end - 1) << 16));
  6120. I915_WRITE(VSYNC(cpu_transcoder),
  6121. (adjusted_mode->crtc_vsync_start - 1) |
  6122. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6123. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6124. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6125. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6126. * bits. */
  6127. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  6128. (pipe == PIPE_B || pipe == PIPE_C))
  6129. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6130. }
  6131. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6132. {
  6133. struct drm_device *dev = intel_crtc->base.dev;
  6134. struct drm_i915_private *dev_priv = to_i915(dev);
  6135. enum pipe pipe = intel_crtc->pipe;
  6136. /* pipesrc controls the size that is scaled from, which should
  6137. * always be the user's requested size.
  6138. */
  6139. I915_WRITE(PIPESRC(pipe),
  6140. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6141. (intel_crtc->config->pipe_src_h - 1));
  6142. }
  6143. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6144. struct intel_crtc_state *pipe_config)
  6145. {
  6146. struct drm_device *dev = crtc->base.dev;
  6147. struct drm_i915_private *dev_priv = to_i915(dev);
  6148. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6149. uint32_t tmp;
  6150. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6151. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6152. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6153. tmp = I915_READ(HBLANK(cpu_transcoder));
  6154. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6155. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6156. tmp = I915_READ(HSYNC(cpu_transcoder));
  6157. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6158. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6159. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6160. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6161. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6162. tmp = I915_READ(VBLANK(cpu_transcoder));
  6163. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6164. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6165. tmp = I915_READ(VSYNC(cpu_transcoder));
  6166. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6167. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6168. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6169. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6170. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6171. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6172. }
  6173. }
  6174. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6175. struct intel_crtc_state *pipe_config)
  6176. {
  6177. struct drm_device *dev = crtc->base.dev;
  6178. struct drm_i915_private *dev_priv = to_i915(dev);
  6179. u32 tmp;
  6180. tmp = I915_READ(PIPESRC(crtc->pipe));
  6181. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6182. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6183. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6184. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6185. }
  6186. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6187. struct intel_crtc_state *pipe_config)
  6188. {
  6189. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6190. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6191. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6192. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6193. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6194. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6195. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6196. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6197. mode->flags = pipe_config->base.adjusted_mode.flags;
  6198. mode->type = DRM_MODE_TYPE_DRIVER;
  6199. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6200. mode->hsync = drm_mode_hsync(mode);
  6201. mode->vrefresh = drm_mode_vrefresh(mode);
  6202. drm_mode_set_name(mode);
  6203. }
  6204. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6205. {
  6206. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6207. uint32_t pipeconf;
  6208. pipeconf = 0;
  6209. /* we keep both pipes enabled on 830 */
  6210. if (IS_I830(dev_priv))
  6211. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6212. if (intel_crtc->config->double_wide)
  6213. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6214. /* only g4x and later have fancy bpc/dither controls */
  6215. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6216. IS_CHERRYVIEW(dev_priv)) {
  6217. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6218. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6219. pipeconf |= PIPECONF_DITHER_EN |
  6220. PIPECONF_DITHER_TYPE_SP;
  6221. switch (intel_crtc->config->pipe_bpp) {
  6222. case 18:
  6223. pipeconf |= PIPECONF_6BPC;
  6224. break;
  6225. case 24:
  6226. pipeconf |= PIPECONF_8BPC;
  6227. break;
  6228. case 30:
  6229. pipeconf |= PIPECONF_10BPC;
  6230. break;
  6231. default:
  6232. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6233. BUG();
  6234. }
  6235. }
  6236. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6237. if (INTEL_GEN(dev_priv) < 4 ||
  6238. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6239. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6240. else
  6241. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6242. } else
  6243. pipeconf |= PIPECONF_PROGRESSIVE;
  6244. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6245. intel_crtc->config->limited_color_range)
  6246. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6247. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6248. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6249. }
  6250. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6251. struct intel_crtc_state *crtc_state)
  6252. {
  6253. struct drm_device *dev = crtc->base.dev;
  6254. struct drm_i915_private *dev_priv = to_i915(dev);
  6255. const struct intel_limit *limit;
  6256. int refclk = 48000;
  6257. memset(&crtc_state->dpll_hw_state, 0,
  6258. sizeof(crtc_state->dpll_hw_state));
  6259. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6260. if (intel_panel_use_ssc(dev_priv)) {
  6261. refclk = dev_priv->vbt.lvds_ssc_freq;
  6262. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6263. }
  6264. limit = &intel_limits_i8xx_lvds;
  6265. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6266. limit = &intel_limits_i8xx_dvo;
  6267. } else {
  6268. limit = &intel_limits_i8xx_dac;
  6269. }
  6270. if (!crtc_state->clock_set &&
  6271. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6272. refclk, NULL, &crtc_state->dpll)) {
  6273. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6274. return -EINVAL;
  6275. }
  6276. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6277. return 0;
  6278. }
  6279. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6280. struct intel_crtc_state *crtc_state)
  6281. {
  6282. struct drm_device *dev = crtc->base.dev;
  6283. struct drm_i915_private *dev_priv = to_i915(dev);
  6284. const struct intel_limit *limit;
  6285. int refclk = 96000;
  6286. memset(&crtc_state->dpll_hw_state, 0,
  6287. sizeof(crtc_state->dpll_hw_state));
  6288. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6289. if (intel_panel_use_ssc(dev_priv)) {
  6290. refclk = dev_priv->vbt.lvds_ssc_freq;
  6291. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6292. }
  6293. if (intel_is_dual_link_lvds(dev))
  6294. limit = &intel_limits_g4x_dual_channel_lvds;
  6295. else
  6296. limit = &intel_limits_g4x_single_channel_lvds;
  6297. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6298. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6299. limit = &intel_limits_g4x_hdmi;
  6300. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6301. limit = &intel_limits_g4x_sdvo;
  6302. } else {
  6303. /* The option is for other outputs */
  6304. limit = &intel_limits_i9xx_sdvo;
  6305. }
  6306. if (!crtc_state->clock_set &&
  6307. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6308. refclk, NULL, &crtc_state->dpll)) {
  6309. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6310. return -EINVAL;
  6311. }
  6312. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6313. return 0;
  6314. }
  6315. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6316. struct intel_crtc_state *crtc_state)
  6317. {
  6318. struct drm_device *dev = crtc->base.dev;
  6319. struct drm_i915_private *dev_priv = to_i915(dev);
  6320. const struct intel_limit *limit;
  6321. int refclk = 96000;
  6322. memset(&crtc_state->dpll_hw_state, 0,
  6323. sizeof(crtc_state->dpll_hw_state));
  6324. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6325. if (intel_panel_use_ssc(dev_priv)) {
  6326. refclk = dev_priv->vbt.lvds_ssc_freq;
  6327. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6328. }
  6329. limit = &intel_limits_pineview_lvds;
  6330. } else {
  6331. limit = &intel_limits_pineview_sdvo;
  6332. }
  6333. if (!crtc_state->clock_set &&
  6334. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6335. refclk, NULL, &crtc_state->dpll)) {
  6336. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6337. return -EINVAL;
  6338. }
  6339. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6340. return 0;
  6341. }
  6342. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6343. struct intel_crtc_state *crtc_state)
  6344. {
  6345. struct drm_device *dev = crtc->base.dev;
  6346. struct drm_i915_private *dev_priv = to_i915(dev);
  6347. const struct intel_limit *limit;
  6348. int refclk = 96000;
  6349. memset(&crtc_state->dpll_hw_state, 0,
  6350. sizeof(crtc_state->dpll_hw_state));
  6351. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6352. if (intel_panel_use_ssc(dev_priv)) {
  6353. refclk = dev_priv->vbt.lvds_ssc_freq;
  6354. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6355. }
  6356. limit = &intel_limits_i9xx_lvds;
  6357. } else {
  6358. limit = &intel_limits_i9xx_sdvo;
  6359. }
  6360. if (!crtc_state->clock_set &&
  6361. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6362. refclk, NULL, &crtc_state->dpll)) {
  6363. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6364. return -EINVAL;
  6365. }
  6366. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6367. return 0;
  6368. }
  6369. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6370. struct intel_crtc_state *crtc_state)
  6371. {
  6372. int refclk = 100000;
  6373. const struct intel_limit *limit = &intel_limits_chv;
  6374. memset(&crtc_state->dpll_hw_state, 0,
  6375. sizeof(crtc_state->dpll_hw_state));
  6376. if (!crtc_state->clock_set &&
  6377. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6378. refclk, NULL, &crtc_state->dpll)) {
  6379. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6380. return -EINVAL;
  6381. }
  6382. chv_compute_dpll(crtc, crtc_state);
  6383. return 0;
  6384. }
  6385. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6386. struct intel_crtc_state *crtc_state)
  6387. {
  6388. int refclk = 100000;
  6389. const struct intel_limit *limit = &intel_limits_vlv;
  6390. memset(&crtc_state->dpll_hw_state, 0,
  6391. sizeof(crtc_state->dpll_hw_state));
  6392. if (!crtc_state->clock_set &&
  6393. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6394. refclk, NULL, &crtc_state->dpll)) {
  6395. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6396. return -EINVAL;
  6397. }
  6398. vlv_compute_dpll(crtc, crtc_state);
  6399. return 0;
  6400. }
  6401. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6402. struct intel_crtc_state *pipe_config)
  6403. {
  6404. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6405. uint32_t tmp;
  6406. if (INTEL_GEN(dev_priv) <= 3 &&
  6407. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6408. return;
  6409. tmp = I915_READ(PFIT_CONTROL);
  6410. if (!(tmp & PFIT_ENABLE))
  6411. return;
  6412. /* Check whether the pfit is attached to our pipe. */
  6413. if (INTEL_GEN(dev_priv) < 4) {
  6414. if (crtc->pipe != PIPE_B)
  6415. return;
  6416. } else {
  6417. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6418. return;
  6419. }
  6420. pipe_config->gmch_pfit.control = tmp;
  6421. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6422. }
  6423. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6424. struct intel_crtc_state *pipe_config)
  6425. {
  6426. struct drm_device *dev = crtc->base.dev;
  6427. struct drm_i915_private *dev_priv = to_i915(dev);
  6428. int pipe = pipe_config->cpu_transcoder;
  6429. struct dpll clock;
  6430. u32 mdiv;
  6431. int refclk = 100000;
  6432. /* In case of DSI, DPLL will not be used */
  6433. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6434. return;
  6435. mutex_lock(&dev_priv->sb_lock);
  6436. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6437. mutex_unlock(&dev_priv->sb_lock);
  6438. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6439. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6440. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6441. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6442. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6443. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6444. }
  6445. static void
  6446. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6447. struct intel_initial_plane_config *plane_config)
  6448. {
  6449. struct drm_device *dev = crtc->base.dev;
  6450. struct drm_i915_private *dev_priv = to_i915(dev);
  6451. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  6452. enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
  6453. enum pipe pipe;
  6454. u32 val, base, offset;
  6455. int fourcc, pixel_format;
  6456. unsigned int aligned_height;
  6457. struct drm_framebuffer *fb;
  6458. struct intel_framebuffer *intel_fb;
  6459. if (!plane->get_hw_state(plane, &pipe))
  6460. return;
  6461. WARN_ON(pipe != crtc->pipe);
  6462. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6463. if (!intel_fb) {
  6464. DRM_DEBUG_KMS("failed to alloc fb\n");
  6465. return;
  6466. }
  6467. fb = &intel_fb->base;
  6468. fb->dev = dev;
  6469. val = I915_READ(DSPCNTR(i9xx_plane));
  6470. if (INTEL_GEN(dev_priv) >= 4) {
  6471. if (val & DISPPLANE_TILED) {
  6472. plane_config->tiling = I915_TILING_X;
  6473. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6474. }
  6475. }
  6476. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6477. fourcc = i9xx_format_to_fourcc(pixel_format);
  6478. fb->format = drm_format_info(fourcc);
  6479. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  6480. offset = I915_READ(DSPOFFSET(i9xx_plane));
  6481. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6482. } else if (INTEL_GEN(dev_priv) >= 4) {
  6483. if (plane_config->tiling)
  6484. offset = I915_READ(DSPTILEOFF(i9xx_plane));
  6485. else
  6486. offset = I915_READ(DSPLINOFF(i9xx_plane));
  6487. base = I915_READ(DSPSURF(i9xx_plane)) & 0xfffff000;
  6488. } else {
  6489. base = I915_READ(DSPADDR(i9xx_plane));
  6490. }
  6491. plane_config->base = base;
  6492. val = I915_READ(PIPESRC(pipe));
  6493. fb->width = ((val >> 16) & 0xfff) + 1;
  6494. fb->height = ((val >> 0) & 0xfff) + 1;
  6495. val = I915_READ(DSPSTRIDE(i9xx_plane));
  6496. fb->pitches[0] = val & 0xffffffc0;
  6497. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6498. plane_config->size = fb->pitches[0] * aligned_height;
  6499. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6500. crtc->base.name, plane->base.name, fb->width, fb->height,
  6501. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6502. plane_config->size);
  6503. plane_config->fb = intel_fb;
  6504. }
  6505. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6506. struct intel_crtc_state *pipe_config)
  6507. {
  6508. struct drm_device *dev = crtc->base.dev;
  6509. struct drm_i915_private *dev_priv = to_i915(dev);
  6510. int pipe = pipe_config->cpu_transcoder;
  6511. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6512. struct dpll clock;
  6513. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6514. int refclk = 100000;
  6515. /* In case of DSI, DPLL will not be used */
  6516. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6517. return;
  6518. mutex_lock(&dev_priv->sb_lock);
  6519. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6520. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6521. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6522. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6523. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6524. mutex_unlock(&dev_priv->sb_lock);
  6525. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6526. clock.m2 = (pll_dw0 & 0xff) << 22;
  6527. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6528. clock.m2 |= pll_dw2 & 0x3fffff;
  6529. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6530. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6531. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6532. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6533. }
  6534. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6535. struct intel_crtc_state *pipe_config)
  6536. {
  6537. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6538. enum intel_display_power_domain power_domain;
  6539. uint32_t tmp;
  6540. bool ret;
  6541. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6542. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6543. return false;
  6544. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6545. pipe_config->shared_dpll = NULL;
  6546. ret = false;
  6547. tmp = I915_READ(PIPECONF(crtc->pipe));
  6548. if (!(tmp & PIPECONF_ENABLE))
  6549. goto out;
  6550. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6551. IS_CHERRYVIEW(dev_priv)) {
  6552. switch (tmp & PIPECONF_BPC_MASK) {
  6553. case PIPECONF_6BPC:
  6554. pipe_config->pipe_bpp = 18;
  6555. break;
  6556. case PIPECONF_8BPC:
  6557. pipe_config->pipe_bpp = 24;
  6558. break;
  6559. case PIPECONF_10BPC:
  6560. pipe_config->pipe_bpp = 30;
  6561. break;
  6562. default:
  6563. break;
  6564. }
  6565. }
  6566. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6567. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6568. pipe_config->limited_color_range = true;
  6569. if (INTEL_GEN(dev_priv) < 4)
  6570. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6571. intel_get_pipe_timings(crtc, pipe_config);
  6572. intel_get_pipe_src_size(crtc, pipe_config);
  6573. i9xx_get_pfit_config(crtc, pipe_config);
  6574. if (INTEL_GEN(dev_priv) >= 4) {
  6575. /* No way to read it out on pipes B and C */
  6576. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6577. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6578. else
  6579. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6580. pipe_config->pixel_multiplier =
  6581. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6582. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6583. pipe_config->dpll_hw_state.dpll_md = tmp;
  6584. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6585. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6586. tmp = I915_READ(DPLL(crtc->pipe));
  6587. pipe_config->pixel_multiplier =
  6588. ((tmp & SDVO_MULTIPLIER_MASK)
  6589. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6590. } else {
  6591. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6592. * port and will be fixed up in the encoder->get_config
  6593. * function. */
  6594. pipe_config->pixel_multiplier = 1;
  6595. }
  6596. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6597. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6598. /*
  6599. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6600. * on 830. Filter it out here so that we don't
  6601. * report errors due to that.
  6602. */
  6603. if (IS_I830(dev_priv))
  6604. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6605. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6606. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6607. } else {
  6608. /* Mask out read-only status bits. */
  6609. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6610. DPLL_PORTC_READY_MASK |
  6611. DPLL_PORTB_READY_MASK);
  6612. }
  6613. if (IS_CHERRYVIEW(dev_priv))
  6614. chv_crtc_clock_get(crtc, pipe_config);
  6615. else if (IS_VALLEYVIEW(dev_priv))
  6616. vlv_crtc_clock_get(crtc, pipe_config);
  6617. else
  6618. i9xx_crtc_clock_get(crtc, pipe_config);
  6619. /*
  6620. * Normally the dotclock is filled in by the encoder .get_config()
  6621. * but in case the pipe is enabled w/o any ports we need a sane
  6622. * default.
  6623. */
  6624. pipe_config->base.adjusted_mode.crtc_clock =
  6625. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6626. ret = true;
  6627. out:
  6628. intel_display_power_put(dev_priv, power_domain);
  6629. return ret;
  6630. }
  6631. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6632. {
  6633. struct intel_encoder *encoder;
  6634. int i;
  6635. u32 val, final;
  6636. bool has_lvds = false;
  6637. bool has_cpu_edp = false;
  6638. bool has_panel = false;
  6639. bool has_ck505 = false;
  6640. bool can_ssc = false;
  6641. bool using_ssc_source = false;
  6642. /* We need to take the global config into account */
  6643. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6644. switch (encoder->type) {
  6645. case INTEL_OUTPUT_LVDS:
  6646. has_panel = true;
  6647. has_lvds = true;
  6648. break;
  6649. case INTEL_OUTPUT_EDP:
  6650. has_panel = true;
  6651. if (encoder->port == PORT_A)
  6652. has_cpu_edp = true;
  6653. break;
  6654. default:
  6655. break;
  6656. }
  6657. }
  6658. if (HAS_PCH_IBX(dev_priv)) {
  6659. has_ck505 = dev_priv->vbt.display_clock_mode;
  6660. can_ssc = has_ck505;
  6661. } else {
  6662. has_ck505 = false;
  6663. can_ssc = true;
  6664. }
  6665. /* Check if any DPLLs are using the SSC source */
  6666. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6667. u32 temp = I915_READ(PCH_DPLL(i));
  6668. if (!(temp & DPLL_VCO_ENABLE))
  6669. continue;
  6670. if ((temp & PLL_REF_INPUT_MASK) ==
  6671. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6672. using_ssc_source = true;
  6673. break;
  6674. }
  6675. }
  6676. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6677. has_panel, has_lvds, has_ck505, using_ssc_source);
  6678. /* Ironlake: try to setup display ref clock before DPLL
  6679. * enabling. This is only under driver's control after
  6680. * PCH B stepping, previous chipset stepping should be
  6681. * ignoring this setting.
  6682. */
  6683. val = I915_READ(PCH_DREF_CONTROL);
  6684. /* As we must carefully and slowly disable/enable each source in turn,
  6685. * compute the final state we want first and check if we need to
  6686. * make any changes at all.
  6687. */
  6688. final = val;
  6689. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6690. if (has_ck505)
  6691. final |= DREF_NONSPREAD_CK505_ENABLE;
  6692. else
  6693. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6694. final &= ~DREF_SSC_SOURCE_MASK;
  6695. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6696. final &= ~DREF_SSC1_ENABLE;
  6697. if (has_panel) {
  6698. final |= DREF_SSC_SOURCE_ENABLE;
  6699. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6700. final |= DREF_SSC1_ENABLE;
  6701. if (has_cpu_edp) {
  6702. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6703. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6704. else
  6705. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6706. } else
  6707. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6708. } else if (using_ssc_source) {
  6709. final |= DREF_SSC_SOURCE_ENABLE;
  6710. final |= DREF_SSC1_ENABLE;
  6711. }
  6712. if (final == val)
  6713. return;
  6714. /* Always enable nonspread source */
  6715. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6716. if (has_ck505)
  6717. val |= DREF_NONSPREAD_CK505_ENABLE;
  6718. else
  6719. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6720. if (has_panel) {
  6721. val &= ~DREF_SSC_SOURCE_MASK;
  6722. val |= DREF_SSC_SOURCE_ENABLE;
  6723. /* SSC must be turned on before enabling the CPU output */
  6724. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6725. DRM_DEBUG_KMS("Using SSC on panel\n");
  6726. val |= DREF_SSC1_ENABLE;
  6727. } else
  6728. val &= ~DREF_SSC1_ENABLE;
  6729. /* Get SSC going before enabling the outputs */
  6730. I915_WRITE(PCH_DREF_CONTROL, val);
  6731. POSTING_READ(PCH_DREF_CONTROL);
  6732. udelay(200);
  6733. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6734. /* Enable CPU source on CPU attached eDP */
  6735. if (has_cpu_edp) {
  6736. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6737. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6738. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6739. } else
  6740. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6741. } else
  6742. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6743. I915_WRITE(PCH_DREF_CONTROL, val);
  6744. POSTING_READ(PCH_DREF_CONTROL);
  6745. udelay(200);
  6746. } else {
  6747. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6748. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6749. /* Turn off CPU output */
  6750. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6751. I915_WRITE(PCH_DREF_CONTROL, val);
  6752. POSTING_READ(PCH_DREF_CONTROL);
  6753. udelay(200);
  6754. if (!using_ssc_source) {
  6755. DRM_DEBUG_KMS("Disabling SSC source\n");
  6756. /* Turn off the SSC source */
  6757. val &= ~DREF_SSC_SOURCE_MASK;
  6758. val |= DREF_SSC_SOURCE_DISABLE;
  6759. /* Turn off SSC1 */
  6760. val &= ~DREF_SSC1_ENABLE;
  6761. I915_WRITE(PCH_DREF_CONTROL, val);
  6762. POSTING_READ(PCH_DREF_CONTROL);
  6763. udelay(200);
  6764. }
  6765. }
  6766. BUG_ON(val != final);
  6767. }
  6768. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6769. {
  6770. uint32_t tmp;
  6771. tmp = I915_READ(SOUTH_CHICKEN2);
  6772. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6773. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6774. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6775. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6776. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6777. tmp = I915_READ(SOUTH_CHICKEN2);
  6778. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6779. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6780. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6781. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6782. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6783. }
  6784. /* WaMPhyProgramming:hsw */
  6785. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6786. {
  6787. uint32_t tmp;
  6788. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6789. tmp &= ~(0xFF << 24);
  6790. tmp |= (0x12 << 24);
  6791. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6792. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6793. tmp |= (1 << 11);
  6794. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6795. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6796. tmp |= (1 << 11);
  6797. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6798. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6799. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6800. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6801. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6802. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6803. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6804. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6805. tmp &= ~(7 << 13);
  6806. tmp |= (5 << 13);
  6807. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6808. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6809. tmp &= ~(7 << 13);
  6810. tmp |= (5 << 13);
  6811. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6812. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6813. tmp &= ~0xFF;
  6814. tmp |= 0x1C;
  6815. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6816. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6817. tmp &= ~0xFF;
  6818. tmp |= 0x1C;
  6819. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6820. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6821. tmp &= ~(0xFF << 16);
  6822. tmp |= (0x1C << 16);
  6823. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6824. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6825. tmp &= ~(0xFF << 16);
  6826. tmp |= (0x1C << 16);
  6827. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6828. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6829. tmp |= (1 << 27);
  6830. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6831. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6832. tmp |= (1 << 27);
  6833. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6834. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6835. tmp &= ~(0xF << 28);
  6836. tmp |= (4 << 28);
  6837. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6838. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6839. tmp &= ~(0xF << 28);
  6840. tmp |= (4 << 28);
  6841. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6842. }
  6843. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6844. * Programming" based on the parameters passed:
  6845. * - Sequence to enable CLKOUT_DP
  6846. * - Sequence to enable CLKOUT_DP without spread
  6847. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6848. */
  6849. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6850. bool with_spread, bool with_fdi)
  6851. {
  6852. uint32_t reg, tmp;
  6853. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6854. with_spread = true;
  6855. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6856. with_fdi, "LP PCH doesn't have FDI\n"))
  6857. with_fdi = false;
  6858. mutex_lock(&dev_priv->sb_lock);
  6859. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6860. tmp &= ~SBI_SSCCTL_DISABLE;
  6861. tmp |= SBI_SSCCTL_PATHALT;
  6862. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6863. udelay(24);
  6864. if (with_spread) {
  6865. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6866. tmp &= ~SBI_SSCCTL_PATHALT;
  6867. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6868. if (with_fdi) {
  6869. lpt_reset_fdi_mphy(dev_priv);
  6870. lpt_program_fdi_mphy(dev_priv);
  6871. }
  6872. }
  6873. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6874. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6875. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6876. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6877. mutex_unlock(&dev_priv->sb_lock);
  6878. }
  6879. /* Sequence to disable CLKOUT_DP */
  6880. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6881. {
  6882. uint32_t reg, tmp;
  6883. mutex_lock(&dev_priv->sb_lock);
  6884. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6885. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6886. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6887. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6888. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6889. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6890. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6891. tmp |= SBI_SSCCTL_PATHALT;
  6892. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6893. udelay(32);
  6894. }
  6895. tmp |= SBI_SSCCTL_DISABLE;
  6896. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6897. }
  6898. mutex_unlock(&dev_priv->sb_lock);
  6899. }
  6900. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6901. static const uint16_t sscdivintphase[] = {
  6902. [BEND_IDX( 50)] = 0x3B23,
  6903. [BEND_IDX( 45)] = 0x3B23,
  6904. [BEND_IDX( 40)] = 0x3C23,
  6905. [BEND_IDX( 35)] = 0x3C23,
  6906. [BEND_IDX( 30)] = 0x3D23,
  6907. [BEND_IDX( 25)] = 0x3D23,
  6908. [BEND_IDX( 20)] = 0x3E23,
  6909. [BEND_IDX( 15)] = 0x3E23,
  6910. [BEND_IDX( 10)] = 0x3F23,
  6911. [BEND_IDX( 5)] = 0x3F23,
  6912. [BEND_IDX( 0)] = 0x0025,
  6913. [BEND_IDX( -5)] = 0x0025,
  6914. [BEND_IDX(-10)] = 0x0125,
  6915. [BEND_IDX(-15)] = 0x0125,
  6916. [BEND_IDX(-20)] = 0x0225,
  6917. [BEND_IDX(-25)] = 0x0225,
  6918. [BEND_IDX(-30)] = 0x0325,
  6919. [BEND_IDX(-35)] = 0x0325,
  6920. [BEND_IDX(-40)] = 0x0425,
  6921. [BEND_IDX(-45)] = 0x0425,
  6922. [BEND_IDX(-50)] = 0x0525,
  6923. };
  6924. /*
  6925. * Bend CLKOUT_DP
  6926. * steps -50 to 50 inclusive, in steps of 5
  6927. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6928. * change in clock period = -(steps / 10) * 5.787 ps
  6929. */
  6930. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6931. {
  6932. uint32_t tmp;
  6933. int idx = BEND_IDX(steps);
  6934. if (WARN_ON(steps % 5 != 0))
  6935. return;
  6936. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6937. return;
  6938. mutex_lock(&dev_priv->sb_lock);
  6939. if (steps % 10 != 0)
  6940. tmp = 0xAAAAAAAB;
  6941. else
  6942. tmp = 0x00000000;
  6943. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6944. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6945. tmp &= 0xffff0000;
  6946. tmp |= sscdivintphase[idx];
  6947. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6948. mutex_unlock(&dev_priv->sb_lock);
  6949. }
  6950. #undef BEND_IDX
  6951. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6952. {
  6953. struct intel_encoder *encoder;
  6954. bool has_vga = false;
  6955. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6956. switch (encoder->type) {
  6957. case INTEL_OUTPUT_ANALOG:
  6958. has_vga = true;
  6959. break;
  6960. default:
  6961. break;
  6962. }
  6963. }
  6964. if (has_vga) {
  6965. lpt_bend_clkout_dp(dev_priv, 0);
  6966. lpt_enable_clkout_dp(dev_priv, true, true);
  6967. } else {
  6968. lpt_disable_clkout_dp(dev_priv);
  6969. }
  6970. }
  6971. /*
  6972. * Initialize reference clocks when the driver loads
  6973. */
  6974. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6975. {
  6976. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6977. ironlake_init_pch_refclk(dev_priv);
  6978. else if (HAS_PCH_LPT(dev_priv))
  6979. lpt_init_pch_refclk(dev_priv);
  6980. }
  6981. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6982. {
  6983. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6984. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6985. int pipe = intel_crtc->pipe;
  6986. uint32_t val;
  6987. val = 0;
  6988. switch (intel_crtc->config->pipe_bpp) {
  6989. case 18:
  6990. val |= PIPECONF_6BPC;
  6991. break;
  6992. case 24:
  6993. val |= PIPECONF_8BPC;
  6994. break;
  6995. case 30:
  6996. val |= PIPECONF_10BPC;
  6997. break;
  6998. case 36:
  6999. val |= PIPECONF_12BPC;
  7000. break;
  7001. default:
  7002. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7003. BUG();
  7004. }
  7005. if (intel_crtc->config->dither)
  7006. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7007. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7008. val |= PIPECONF_INTERLACED_ILK;
  7009. else
  7010. val |= PIPECONF_PROGRESSIVE;
  7011. if (intel_crtc->config->limited_color_range)
  7012. val |= PIPECONF_COLOR_RANGE_SELECT;
  7013. I915_WRITE(PIPECONF(pipe), val);
  7014. POSTING_READ(PIPECONF(pipe));
  7015. }
  7016. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7017. {
  7018. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7019. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7020. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7021. u32 val = 0;
  7022. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7023. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7024. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7025. val |= PIPECONF_INTERLACED_ILK;
  7026. else
  7027. val |= PIPECONF_PROGRESSIVE;
  7028. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7029. POSTING_READ(PIPECONF(cpu_transcoder));
  7030. }
  7031. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7032. {
  7033. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7034. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7035. struct intel_crtc_state *config = intel_crtc->config;
  7036. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  7037. u32 val = 0;
  7038. switch (intel_crtc->config->pipe_bpp) {
  7039. case 18:
  7040. val |= PIPEMISC_DITHER_6_BPC;
  7041. break;
  7042. case 24:
  7043. val |= PIPEMISC_DITHER_8_BPC;
  7044. break;
  7045. case 30:
  7046. val |= PIPEMISC_DITHER_10_BPC;
  7047. break;
  7048. case 36:
  7049. val |= PIPEMISC_DITHER_12_BPC;
  7050. break;
  7051. default:
  7052. /* Case prevented by pipe_config_set_bpp. */
  7053. BUG();
  7054. }
  7055. if (intel_crtc->config->dither)
  7056. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7057. if (config->ycbcr420) {
  7058. val |= PIPEMISC_OUTPUT_COLORSPACE_YUV |
  7059. PIPEMISC_YUV420_ENABLE |
  7060. PIPEMISC_YUV420_MODE_FULL_BLEND;
  7061. }
  7062. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7063. }
  7064. }
  7065. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7066. {
  7067. /*
  7068. * Account for spread spectrum to avoid
  7069. * oversubscribing the link. Max center spread
  7070. * is 2.5%; use 5% for safety's sake.
  7071. */
  7072. u32 bps = target_clock * bpp * 21 / 20;
  7073. return DIV_ROUND_UP(bps, link_bw * 8);
  7074. }
  7075. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7076. {
  7077. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7078. }
  7079. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7080. struct intel_crtc_state *crtc_state,
  7081. struct dpll *reduced_clock)
  7082. {
  7083. struct drm_crtc *crtc = &intel_crtc->base;
  7084. struct drm_device *dev = crtc->dev;
  7085. struct drm_i915_private *dev_priv = to_i915(dev);
  7086. u32 dpll, fp, fp2;
  7087. int factor;
  7088. /* Enable autotuning of the PLL clock (if permissible) */
  7089. factor = 21;
  7090. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7091. if ((intel_panel_use_ssc(dev_priv) &&
  7092. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7093. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  7094. factor = 25;
  7095. } else if (crtc_state->sdvo_tv_clock)
  7096. factor = 20;
  7097. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7098. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7099. fp |= FP_CB_TUNE;
  7100. if (reduced_clock) {
  7101. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7102. if (reduced_clock->m < factor * reduced_clock->n)
  7103. fp2 |= FP_CB_TUNE;
  7104. } else {
  7105. fp2 = fp;
  7106. }
  7107. dpll = 0;
  7108. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7109. dpll |= DPLLB_MODE_LVDS;
  7110. else
  7111. dpll |= DPLLB_MODE_DAC_SERIAL;
  7112. dpll |= (crtc_state->pixel_multiplier - 1)
  7113. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7114. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7115. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7116. dpll |= DPLL_SDVO_HIGH_SPEED;
  7117. if (intel_crtc_has_dp_encoder(crtc_state))
  7118. dpll |= DPLL_SDVO_HIGH_SPEED;
  7119. /*
  7120. * The high speed IO clock is only really required for
  7121. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  7122. * possible to share the DPLL between CRT and HDMI. Enabling
  7123. * the clock needlessly does no real harm, except use up a
  7124. * bit of power potentially.
  7125. *
  7126. * We'll limit this to IVB with 3 pipes, since it has only two
  7127. * DPLLs and so DPLL sharing is the only way to get three pipes
  7128. * driving PCH ports at the same time. On SNB we could do this,
  7129. * and potentially avoid enabling the second DPLL, but it's not
  7130. * clear if it''s a win or loss power wise. No point in doing
  7131. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  7132. */
  7133. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  7134. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  7135. dpll |= DPLL_SDVO_HIGH_SPEED;
  7136. /* compute bitmask from p1 value */
  7137. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7138. /* also FPA1 */
  7139. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7140. switch (crtc_state->dpll.p2) {
  7141. case 5:
  7142. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7143. break;
  7144. case 7:
  7145. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7146. break;
  7147. case 10:
  7148. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7149. break;
  7150. case 14:
  7151. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7152. break;
  7153. }
  7154. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7155. intel_panel_use_ssc(dev_priv))
  7156. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7157. else
  7158. dpll |= PLL_REF_INPUT_DREFCLK;
  7159. dpll |= DPLL_VCO_ENABLE;
  7160. crtc_state->dpll_hw_state.dpll = dpll;
  7161. crtc_state->dpll_hw_state.fp0 = fp;
  7162. crtc_state->dpll_hw_state.fp1 = fp2;
  7163. }
  7164. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7165. struct intel_crtc_state *crtc_state)
  7166. {
  7167. struct drm_device *dev = crtc->base.dev;
  7168. struct drm_i915_private *dev_priv = to_i915(dev);
  7169. const struct intel_limit *limit;
  7170. int refclk = 120000;
  7171. memset(&crtc_state->dpll_hw_state, 0,
  7172. sizeof(crtc_state->dpll_hw_state));
  7173. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7174. if (!crtc_state->has_pch_encoder)
  7175. return 0;
  7176. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7177. if (intel_panel_use_ssc(dev_priv)) {
  7178. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7179. dev_priv->vbt.lvds_ssc_freq);
  7180. refclk = dev_priv->vbt.lvds_ssc_freq;
  7181. }
  7182. if (intel_is_dual_link_lvds(dev)) {
  7183. if (refclk == 100000)
  7184. limit = &intel_limits_ironlake_dual_lvds_100m;
  7185. else
  7186. limit = &intel_limits_ironlake_dual_lvds;
  7187. } else {
  7188. if (refclk == 100000)
  7189. limit = &intel_limits_ironlake_single_lvds_100m;
  7190. else
  7191. limit = &intel_limits_ironlake_single_lvds;
  7192. }
  7193. } else {
  7194. limit = &intel_limits_ironlake_dac;
  7195. }
  7196. if (!crtc_state->clock_set &&
  7197. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7198. refclk, NULL, &crtc_state->dpll)) {
  7199. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7200. return -EINVAL;
  7201. }
  7202. ironlake_compute_dpll(crtc, crtc_state, NULL);
  7203. if (!intel_get_shared_dpll(crtc, crtc_state, NULL)) {
  7204. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7205. pipe_name(crtc->pipe));
  7206. return -EINVAL;
  7207. }
  7208. return 0;
  7209. }
  7210. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7211. struct intel_link_m_n *m_n)
  7212. {
  7213. struct drm_device *dev = crtc->base.dev;
  7214. struct drm_i915_private *dev_priv = to_i915(dev);
  7215. enum pipe pipe = crtc->pipe;
  7216. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7217. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7218. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7219. & ~TU_SIZE_MASK;
  7220. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7221. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7222. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7223. }
  7224. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7225. enum transcoder transcoder,
  7226. struct intel_link_m_n *m_n,
  7227. struct intel_link_m_n *m2_n2)
  7228. {
  7229. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7230. enum pipe pipe = crtc->pipe;
  7231. if (INTEL_GEN(dev_priv) >= 5) {
  7232. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7233. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7234. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7235. & ~TU_SIZE_MASK;
  7236. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7237. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7238. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7239. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7240. * gen < 8) and if DRRS is supported (to make sure the
  7241. * registers are not unnecessarily read).
  7242. */
  7243. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  7244. crtc->config->has_drrs) {
  7245. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7246. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7247. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7248. & ~TU_SIZE_MASK;
  7249. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7250. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7251. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7252. }
  7253. } else {
  7254. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7255. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7256. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7257. & ~TU_SIZE_MASK;
  7258. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7259. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7260. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7261. }
  7262. }
  7263. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7264. struct intel_crtc_state *pipe_config)
  7265. {
  7266. if (pipe_config->has_pch_encoder)
  7267. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7268. else
  7269. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7270. &pipe_config->dp_m_n,
  7271. &pipe_config->dp_m2_n2);
  7272. }
  7273. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7274. struct intel_crtc_state *pipe_config)
  7275. {
  7276. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7277. &pipe_config->fdi_m_n, NULL);
  7278. }
  7279. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7280. struct intel_crtc_state *pipe_config)
  7281. {
  7282. struct drm_device *dev = crtc->base.dev;
  7283. struct drm_i915_private *dev_priv = to_i915(dev);
  7284. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7285. uint32_t ps_ctrl = 0;
  7286. int id = -1;
  7287. int i;
  7288. /* find scaler attached to this pipe */
  7289. for (i = 0; i < crtc->num_scalers; i++) {
  7290. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7291. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7292. id = i;
  7293. pipe_config->pch_pfit.enabled = true;
  7294. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7295. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7296. break;
  7297. }
  7298. }
  7299. scaler_state->scaler_id = id;
  7300. if (id >= 0) {
  7301. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7302. } else {
  7303. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7304. }
  7305. }
  7306. static void
  7307. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7308. struct intel_initial_plane_config *plane_config)
  7309. {
  7310. struct drm_device *dev = crtc->base.dev;
  7311. struct drm_i915_private *dev_priv = to_i915(dev);
  7312. struct intel_plane *plane = to_intel_plane(crtc->base.primary);
  7313. enum plane_id plane_id = plane->id;
  7314. enum pipe pipe;
  7315. u32 val, base, offset, stride_mult, tiling, alpha;
  7316. int fourcc, pixel_format;
  7317. unsigned int aligned_height;
  7318. struct drm_framebuffer *fb;
  7319. struct intel_framebuffer *intel_fb;
  7320. if (!plane->get_hw_state(plane, &pipe))
  7321. return;
  7322. WARN_ON(pipe != crtc->pipe);
  7323. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7324. if (!intel_fb) {
  7325. DRM_DEBUG_KMS("failed to alloc fb\n");
  7326. return;
  7327. }
  7328. fb = &intel_fb->base;
  7329. fb->dev = dev;
  7330. val = I915_READ(PLANE_CTL(pipe, plane_id));
  7331. if (INTEL_GEN(dev_priv) >= 11)
  7332. pixel_format = val & ICL_PLANE_CTL_FORMAT_MASK;
  7333. else
  7334. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7335. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
  7336. alpha = I915_READ(PLANE_COLOR_CTL(pipe, plane_id));
  7337. alpha &= PLANE_COLOR_ALPHA_MASK;
  7338. } else {
  7339. alpha = val & PLANE_CTL_ALPHA_MASK;
  7340. }
  7341. fourcc = skl_format_to_fourcc(pixel_format,
  7342. val & PLANE_CTL_ORDER_RGBX, alpha);
  7343. fb->format = drm_format_info(fourcc);
  7344. tiling = val & PLANE_CTL_TILED_MASK;
  7345. switch (tiling) {
  7346. case PLANE_CTL_TILED_LINEAR:
  7347. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7348. break;
  7349. case PLANE_CTL_TILED_X:
  7350. plane_config->tiling = I915_TILING_X;
  7351. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7352. break;
  7353. case PLANE_CTL_TILED_Y:
  7354. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7355. fb->modifier = I915_FORMAT_MOD_Y_TILED_CCS;
  7356. else
  7357. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7358. break;
  7359. case PLANE_CTL_TILED_YF:
  7360. if (val & PLANE_CTL_DECOMPRESSION_ENABLE)
  7361. fb->modifier = I915_FORMAT_MOD_Yf_TILED_CCS;
  7362. else
  7363. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7364. break;
  7365. default:
  7366. MISSING_CASE(tiling);
  7367. goto error;
  7368. }
  7369. base = I915_READ(PLANE_SURF(pipe, plane_id)) & 0xfffff000;
  7370. plane_config->base = base;
  7371. offset = I915_READ(PLANE_OFFSET(pipe, plane_id));
  7372. val = I915_READ(PLANE_SIZE(pipe, plane_id));
  7373. fb->height = ((val >> 16) & 0xfff) + 1;
  7374. fb->width = ((val >> 0) & 0x1fff) + 1;
  7375. val = I915_READ(PLANE_STRIDE(pipe, plane_id));
  7376. stride_mult = intel_fb_stride_alignment(fb, 0);
  7377. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7378. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7379. plane_config->size = fb->pitches[0] * aligned_height;
  7380. DRM_DEBUG_KMS("%s/%s with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7381. crtc->base.name, plane->base.name, fb->width, fb->height,
  7382. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7383. plane_config->size);
  7384. plane_config->fb = intel_fb;
  7385. return;
  7386. error:
  7387. kfree(intel_fb);
  7388. }
  7389. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7390. struct intel_crtc_state *pipe_config)
  7391. {
  7392. struct drm_device *dev = crtc->base.dev;
  7393. struct drm_i915_private *dev_priv = to_i915(dev);
  7394. uint32_t tmp;
  7395. tmp = I915_READ(PF_CTL(crtc->pipe));
  7396. if (tmp & PF_ENABLE) {
  7397. pipe_config->pch_pfit.enabled = true;
  7398. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7399. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7400. /* We currently do not free assignements of panel fitters on
  7401. * ivb/hsw (since we don't use the higher upscaling modes which
  7402. * differentiates them) so just WARN about this case for now. */
  7403. if (IS_GEN7(dev_priv)) {
  7404. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7405. PF_PIPE_SEL_IVB(crtc->pipe));
  7406. }
  7407. }
  7408. }
  7409. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7410. struct intel_crtc_state *pipe_config)
  7411. {
  7412. struct drm_device *dev = crtc->base.dev;
  7413. struct drm_i915_private *dev_priv = to_i915(dev);
  7414. enum intel_display_power_domain power_domain;
  7415. uint32_t tmp;
  7416. bool ret;
  7417. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7418. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7419. return false;
  7420. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7421. pipe_config->shared_dpll = NULL;
  7422. ret = false;
  7423. tmp = I915_READ(PIPECONF(crtc->pipe));
  7424. if (!(tmp & PIPECONF_ENABLE))
  7425. goto out;
  7426. switch (tmp & PIPECONF_BPC_MASK) {
  7427. case PIPECONF_6BPC:
  7428. pipe_config->pipe_bpp = 18;
  7429. break;
  7430. case PIPECONF_8BPC:
  7431. pipe_config->pipe_bpp = 24;
  7432. break;
  7433. case PIPECONF_10BPC:
  7434. pipe_config->pipe_bpp = 30;
  7435. break;
  7436. case PIPECONF_12BPC:
  7437. pipe_config->pipe_bpp = 36;
  7438. break;
  7439. default:
  7440. break;
  7441. }
  7442. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7443. pipe_config->limited_color_range = true;
  7444. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7445. struct intel_shared_dpll *pll;
  7446. enum intel_dpll_id pll_id;
  7447. pipe_config->has_pch_encoder = true;
  7448. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7449. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7450. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7451. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7452. if (HAS_PCH_IBX(dev_priv)) {
  7453. /*
  7454. * The pipe->pch transcoder and pch transcoder->pll
  7455. * mapping is fixed.
  7456. */
  7457. pll_id = (enum intel_dpll_id) crtc->pipe;
  7458. } else {
  7459. tmp = I915_READ(PCH_DPLL_SEL);
  7460. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7461. pll_id = DPLL_ID_PCH_PLL_B;
  7462. else
  7463. pll_id= DPLL_ID_PCH_PLL_A;
  7464. }
  7465. pipe_config->shared_dpll =
  7466. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7467. pll = pipe_config->shared_dpll;
  7468. WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
  7469. &pipe_config->dpll_hw_state));
  7470. tmp = pipe_config->dpll_hw_state.dpll;
  7471. pipe_config->pixel_multiplier =
  7472. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7473. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7474. ironlake_pch_clock_get(crtc, pipe_config);
  7475. } else {
  7476. pipe_config->pixel_multiplier = 1;
  7477. }
  7478. intel_get_pipe_timings(crtc, pipe_config);
  7479. intel_get_pipe_src_size(crtc, pipe_config);
  7480. ironlake_get_pfit_config(crtc, pipe_config);
  7481. ret = true;
  7482. out:
  7483. intel_display_power_put(dev_priv, power_domain);
  7484. return ret;
  7485. }
  7486. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7487. {
  7488. struct drm_device *dev = &dev_priv->drm;
  7489. struct intel_crtc *crtc;
  7490. for_each_intel_crtc(dev, crtc)
  7491. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7492. pipe_name(crtc->pipe));
  7493. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL)),
  7494. "Display power well on\n");
  7495. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7496. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7497. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7498. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7499. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7500. "CPU PWM1 enabled\n");
  7501. if (IS_HASWELL(dev_priv))
  7502. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7503. "CPU PWM2 enabled\n");
  7504. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7505. "PCH PWM1 enabled\n");
  7506. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7507. "Utility pin enabled\n");
  7508. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7509. /*
  7510. * In theory we can still leave IRQs enabled, as long as only the HPD
  7511. * interrupts remain enabled. We used to check for that, but since it's
  7512. * gen-specific and since we only disable LCPLL after we fully disable
  7513. * the interrupts, the check below should be enough.
  7514. */
  7515. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7516. }
  7517. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7518. {
  7519. if (IS_HASWELL(dev_priv))
  7520. return I915_READ(D_COMP_HSW);
  7521. else
  7522. return I915_READ(D_COMP_BDW);
  7523. }
  7524. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7525. {
  7526. if (IS_HASWELL(dev_priv)) {
  7527. mutex_lock(&dev_priv->pcu_lock);
  7528. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7529. val))
  7530. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7531. mutex_unlock(&dev_priv->pcu_lock);
  7532. } else {
  7533. I915_WRITE(D_COMP_BDW, val);
  7534. POSTING_READ(D_COMP_BDW);
  7535. }
  7536. }
  7537. /*
  7538. * This function implements pieces of two sequences from BSpec:
  7539. * - Sequence for display software to disable LCPLL
  7540. * - Sequence for display software to allow package C8+
  7541. * The steps implemented here are just the steps that actually touch the LCPLL
  7542. * register. Callers should take care of disabling all the display engine
  7543. * functions, doing the mode unset, fixing interrupts, etc.
  7544. */
  7545. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7546. bool switch_to_fclk, bool allow_power_down)
  7547. {
  7548. uint32_t val;
  7549. assert_can_disable_lcpll(dev_priv);
  7550. val = I915_READ(LCPLL_CTL);
  7551. if (switch_to_fclk) {
  7552. val |= LCPLL_CD_SOURCE_FCLK;
  7553. I915_WRITE(LCPLL_CTL, val);
  7554. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7555. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7556. DRM_ERROR("Switching to FCLK failed\n");
  7557. val = I915_READ(LCPLL_CTL);
  7558. }
  7559. val |= LCPLL_PLL_DISABLE;
  7560. I915_WRITE(LCPLL_CTL, val);
  7561. POSTING_READ(LCPLL_CTL);
  7562. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7563. DRM_ERROR("LCPLL still locked\n");
  7564. val = hsw_read_dcomp(dev_priv);
  7565. val |= D_COMP_COMP_DISABLE;
  7566. hsw_write_dcomp(dev_priv, val);
  7567. ndelay(100);
  7568. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7569. 1))
  7570. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7571. if (allow_power_down) {
  7572. val = I915_READ(LCPLL_CTL);
  7573. val |= LCPLL_POWER_DOWN_ALLOW;
  7574. I915_WRITE(LCPLL_CTL, val);
  7575. POSTING_READ(LCPLL_CTL);
  7576. }
  7577. }
  7578. /*
  7579. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7580. * source.
  7581. */
  7582. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7583. {
  7584. uint32_t val;
  7585. val = I915_READ(LCPLL_CTL);
  7586. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7587. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7588. return;
  7589. /*
  7590. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7591. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7592. */
  7593. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7594. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7595. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7596. I915_WRITE(LCPLL_CTL, val);
  7597. POSTING_READ(LCPLL_CTL);
  7598. }
  7599. val = hsw_read_dcomp(dev_priv);
  7600. val |= D_COMP_COMP_FORCE;
  7601. val &= ~D_COMP_COMP_DISABLE;
  7602. hsw_write_dcomp(dev_priv, val);
  7603. val = I915_READ(LCPLL_CTL);
  7604. val &= ~LCPLL_PLL_DISABLE;
  7605. I915_WRITE(LCPLL_CTL, val);
  7606. if (intel_wait_for_register(dev_priv,
  7607. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7608. 5))
  7609. DRM_ERROR("LCPLL not locked yet\n");
  7610. if (val & LCPLL_CD_SOURCE_FCLK) {
  7611. val = I915_READ(LCPLL_CTL);
  7612. val &= ~LCPLL_CD_SOURCE_FCLK;
  7613. I915_WRITE(LCPLL_CTL, val);
  7614. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7615. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7616. DRM_ERROR("Switching back to LCPLL failed\n");
  7617. }
  7618. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7619. intel_update_cdclk(dev_priv);
  7620. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  7621. }
  7622. /*
  7623. * Package states C8 and deeper are really deep PC states that can only be
  7624. * reached when all the devices on the system allow it, so even if the graphics
  7625. * device allows PC8+, it doesn't mean the system will actually get to these
  7626. * states. Our driver only allows PC8+ when going into runtime PM.
  7627. *
  7628. * The requirements for PC8+ are that all the outputs are disabled, the power
  7629. * well is disabled and most interrupts are disabled, and these are also
  7630. * requirements for runtime PM. When these conditions are met, we manually do
  7631. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7632. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7633. * hang the machine.
  7634. *
  7635. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7636. * the state of some registers, so when we come back from PC8+ we need to
  7637. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7638. * need to take care of the registers kept by RC6. Notice that this happens even
  7639. * if we don't put the device in PCI D3 state (which is what currently happens
  7640. * because of the runtime PM support).
  7641. *
  7642. * For more, read "Display Sequences for Package C8" on the hardware
  7643. * documentation.
  7644. */
  7645. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7646. {
  7647. uint32_t val;
  7648. DRM_DEBUG_KMS("Enabling package C8+\n");
  7649. if (HAS_PCH_LPT_LP(dev_priv)) {
  7650. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7651. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7652. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7653. }
  7654. lpt_disable_clkout_dp(dev_priv);
  7655. hsw_disable_lcpll(dev_priv, true, true);
  7656. }
  7657. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7658. {
  7659. uint32_t val;
  7660. DRM_DEBUG_KMS("Disabling package C8+\n");
  7661. hsw_restore_lcpll(dev_priv);
  7662. lpt_init_pch_refclk(dev_priv);
  7663. if (HAS_PCH_LPT_LP(dev_priv)) {
  7664. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7665. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7666. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7667. }
  7668. }
  7669. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7670. struct intel_crtc_state *crtc_state)
  7671. {
  7672. struct intel_atomic_state *state =
  7673. to_intel_atomic_state(crtc_state->base.state);
  7674. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7675. struct intel_encoder *encoder =
  7676. intel_get_crtc_new_encoder(state, crtc_state);
  7677. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7678. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7679. pipe_name(crtc->pipe));
  7680. return -EINVAL;
  7681. }
  7682. }
  7683. return 0;
  7684. }
  7685. static void cannonlake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7686. enum port port,
  7687. struct intel_crtc_state *pipe_config)
  7688. {
  7689. enum intel_dpll_id id;
  7690. u32 temp;
  7691. temp = I915_READ(DPCLKA_CFGCR0) & DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7692. id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
  7693. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL2))
  7694. return;
  7695. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7696. }
  7697. static void icelake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7698. enum port port,
  7699. struct intel_crtc_state *pipe_config)
  7700. {
  7701. enum intel_dpll_id id;
  7702. u32 temp;
  7703. /* TODO: TBT pll not implemented. */
  7704. switch (port) {
  7705. case PORT_A:
  7706. case PORT_B:
  7707. temp = I915_READ(DPCLKA_CFGCR0_ICL) &
  7708. DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
  7709. id = temp >> DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(port);
  7710. if (WARN_ON(id != DPLL_ID_ICL_DPLL0 && id != DPLL_ID_ICL_DPLL1))
  7711. return;
  7712. break;
  7713. case PORT_C:
  7714. id = DPLL_ID_ICL_MGPLL1;
  7715. break;
  7716. case PORT_D:
  7717. id = DPLL_ID_ICL_MGPLL2;
  7718. break;
  7719. case PORT_E:
  7720. id = DPLL_ID_ICL_MGPLL3;
  7721. break;
  7722. case PORT_F:
  7723. id = DPLL_ID_ICL_MGPLL4;
  7724. break;
  7725. default:
  7726. MISSING_CASE(port);
  7727. return;
  7728. }
  7729. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7730. }
  7731. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7732. enum port port,
  7733. struct intel_crtc_state *pipe_config)
  7734. {
  7735. enum intel_dpll_id id;
  7736. switch (port) {
  7737. case PORT_A:
  7738. id = DPLL_ID_SKL_DPLL0;
  7739. break;
  7740. case PORT_B:
  7741. id = DPLL_ID_SKL_DPLL1;
  7742. break;
  7743. case PORT_C:
  7744. id = DPLL_ID_SKL_DPLL2;
  7745. break;
  7746. default:
  7747. DRM_ERROR("Incorrect port type\n");
  7748. return;
  7749. }
  7750. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7751. }
  7752. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7753. enum port port,
  7754. struct intel_crtc_state *pipe_config)
  7755. {
  7756. enum intel_dpll_id id;
  7757. u32 temp;
  7758. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7759. id = temp >> (port * 3 + 1);
  7760. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7761. return;
  7762. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7763. }
  7764. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7765. enum port port,
  7766. struct intel_crtc_state *pipe_config)
  7767. {
  7768. enum intel_dpll_id id;
  7769. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7770. switch (ddi_pll_sel) {
  7771. case PORT_CLK_SEL_WRPLL1:
  7772. id = DPLL_ID_WRPLL1;
  7773. break;
  7774. case PORT_CLK_SEL_WRPLL2:
  7775. id = DPLL_ID_WRPLL2;
  7776. break;
  7777. case PORT_CLK_SEL_SPLL:
  7778. id = DPLL_ID_SPLL;
  7779. break;
  7780. case PORT_CLK_SEL_LCPLL_810:
  7781. id = DPLL_ID_LCPLL_810;
  7782. break;
  7783. case PORT_CLK_SEL_LCPLL_1350:
  7784. id = DPLL_ID_LCPLL_1350;
  7785. break;
  7786. case PORT_CLK_SEL_LCPLL_2700:
  7787. id = DPLL_ID_LCPLL_2700;
  7788. break;
  7789. default:
  7790. MISSING_CASE(ddi_pll_sel);
  7791. /* fall through */
  7792. case PORT_CLK_SEL_NONE:
  7793. return;
  7794. }
  7795. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7796. }
  7797. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7798. struct intel_crtc_state *pipe_config,
  7799. u64 *power_domain_mask)
  7800. {
  7801. struct drm_device *dev = crtc->base.dev;
  7802. struct drm_i915_private *dev_priv = to_i915(dev);
  7803. enum intel_display_power_domain power_domain;
  7804. u32 tmp;
  7805. /*
  7806. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7807. * transcoder handled below.
  7808. */
  7809. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7810. /*
  7811. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7812. * consistency and less surprising code; it's in always on power).
  7813. */
  7814. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7815. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7816. enum pipe trans_edp_pipe;
  7817. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7818. default:
  7819. WARN(1, "unknown pipe linked to edp transcoder\n");
  7820. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7821. case TRANS_DDI_EDP_INPUT_A_ON:
  7822. trans_edp_pipe = PIPE_A;
  7823. break;
  7824. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7825. trans_edp_pipe = PIPE_B;
  7826. break;
  7827. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7828. trans_edp_pipe = PIPE_C;
  7829. break;
  7830. }
  7831. if (trans_edp_pipe == crtc->pipe)
  7832. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7833. }
  7834. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7835. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7836. return false;
  7837. *power_domain_mask |= BIT_ULL(power_domain);
  7838. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7839. return tmp & PIPECONF_ENABLE;
  7840. }
  7841. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7842. struct intel_crtc_state *pipe_config,
  7843. u64 *power_domain_mask)
  7844. {
  7845. struct drm_device *dev = crtc->base.dev;
  7846. struct drm_i915_private *dev_priv = to_i915(dev);
  7847. enum intel_display_power_domain power_domain;
  7848. enum port port;
  7849. enum transcoder cpu_transcoder;
  7850. u32 tmp;
  7851. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7852. if (port == PORT_A)
  7853. cpu_transcoder = TRANSCODER_DSI_A;
  7854. else
  7855. cpu_transcoder = TRANSCODER_DSI_C;
  7856. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7857. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7858. continue;
  7859. *power_domain_mask |= BIT_ULL(power_domain);
  7860. /*
  7861. * The PLL needs to be enabled with a valid divider
  7862. * configuration, otherwise accessing DSI registers will hang
  7863. * the machine. See BSpec North Display Engine
  7864. * registers/MIPI[BXT]. We can break out here early, since we
  7865. * need the same DSI PLL to be enabled for both DSI ports.
  7866. */
  7867. if (!intel_dsi_pll_is_enabled(dev_priv))
  7868. break;
  7869. /* XXX: this works for video mode only */
  7870. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7871. if (!(tmp & DPI_ENABLE))
  7872. continue;
  7873. tmp = I915_READ(MIPI_CTRL(port));
  7874. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7875. continue;
  7876. pipe_config->cpu_transcoder = cpu_transcoder;
  7877. break;
  7878. }
  7879. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7880. }
  7881. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7882. struct intel_crtc_state *pipe_config)
  7883. {
  7884. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7885. struct intel_shared_dpll *pll;
  7886. enum port port;
  7887. uint32_t tmp;
  7888. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7889. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7890. if (IS_ICELAKE(dev_priv))
  7891. icelake_get_ddi_pll(dev_priv, port, pipe_config);
  7892. else if (IS_CANNONLAKE(dev_priv))
  7893. cannonlake_get_ddi_pll(dev_priv, port, pipe_config);
  7894. else if (IS_GEN9_BC(dev_priv))
  7895. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7896. else if (IS_GEN9_LP(dev_priv))
  7897. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7898. else
  7899. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7900. pll = pipe_config->shared_dpll;
  7901. if (pll) {
  7902. WARN_ON(!pll->info->funcs->get_hw_state(dev_priv, pll,
  7903. &pipe_config->dpll_hw_state));
  7904. }
  7905. /*
  7906. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7907. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7908. * the PCH transcoder is on.
  7909. */
  7910. if (INTEL_GEN(dev_priv) < 9 &&
  7911. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7912. pipe_config->has_pch_encoder = true;
  7913. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7914. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7915. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7916. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7917. }
  7918. }
  7919. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7920. struct intel_crtc_state *pipe_config)
  7921. {
  7922. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7923. enum intel_display_power_domain power_domain;
  7924. u64 power_domain_mask;
  7925. bool active;
  7926. intel_crtc_init_scalers(crtc, pipe_config);
  7927. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7928. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7929. return false;
  7930. power_domain_mask = BIT_ULL(power_domain);
  7931. pipe_config->shared_dpll = NULL;
  7932. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7933. if (IS_GEN9_LP(dev_priv) &&
  7934. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7935. WARN_ON(active);
  7936. active = true;
  7937. }
  7938. if (!active)
  7939. goto out;
  7940. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7941. haswell_get_ddi_port_state(crtc, pipe_config);
  7942. intel_get_pipe_timings(crtc, pipe_config);
  7943. }
  7944. intel_get_pipe_src_size(crtc, pipe_config);
  7945. pipe_config->gamma_mode =
  7946. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7947. if (IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9) {
  7948. u32 tmp = I915_READ(PIPEMISC(crtc->pipe));
  7949. bool clrspace_yuv = tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV;
  7950. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10) {
  7951. bool blend_mode_420 = tmp &
  7952. PIPEMISC_YUV420_MODE_FULL_BLEND;
  7953. pipe_config->ycbcr420 = tmp & PIPEMISC_YUV420_ENABLE;
  7954. if (pipe_config->ycbcr420 != clrspace_yuv ||
  7955. pipe_config->ycbcr420 != blend_mode_420)
  7956. DRM_DEBUG_KMS("Bad 4:2:0 mode (%08x)\n", tmp);
  7957. } else if (clrspace_yuv) {
  7958. DRM_DEBUG_KMS("YCbCr 4:2:0 Unsupported\n");
  7959. }
  7960. }
  7961. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7962. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7963. power_domain_mask |= BIT_ULL(power_domain);
  7964. if (INTEL_GEN(dev_priv) >= 9)
  7965. skylake_get_pfit_config(crtc, pipe_config);
  7966. else
  7967. ironlake_get_pfit_config(crtc, pipe_config);
  7968. }
  7969. if (hsw_crtc_supports_ips(crtc)) {
  7970. if (IS_HASWELL(dev_priv))
  7971. pipe_config->ips_enabled = I915_READ(IPS_CTL) & IPS_ENABLE;
  7972. else {
  7973. /*
  7974. * We cannot readout IPS state on broadwell, set to
  7975. * true so we can set it to a defined state on first
  7976. * commit.
  7977. */
  7978. pipe_config->ips_enabled = true;
  7979. }
  7980. }
  7981. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7982. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7983. pipe_config->pixel_multiplier =
  7984. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7985. } else {
  7986. pipe_config->pixel_multiplier = 1;
  7987. }
  7988. out:
  7989. for_each_power_domain(power_domain, power_domain_mask)
  7990. intel_display_power_put(dev_priv, power_domain);
  7991. return active;
  7992. }
  7993. static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
  7994. {
  7995. struct drm_i915_private *dev_priv =
  7996. to_i915(plane_state->base.plane->dev);
  7997. const struct drm_framebuffer *fb = plane_state->base.fb;
  7998. const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  7999. u32 base;
  8000. if (INTEL_INFO(dev_priv)->cursor_needs_physical)
  8001. base = obj->phys_handle->busaddr;
  8002. else
  8003. base = intel_plane_ggtt_offset(plane_state);
  8004. base += plane_state->main.offset;
  8005. /* ILK+ do this automagically */
  8006. if (HAS_GMCH_DISPLAY(dev_priv) &&
  8007. plane_state->base.rotation & DRM_MODE_ROTATE_180)
  8008. base += (plane_state->base.crtc_h *
  8009. plane_state->base.crtc_w - 1) * fb->format->cpp[0];
  8010. return base;
  8011. }
  8012. static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
  8013. {
  8014. int x = plane_state->base.crtc_x;
  8015. int y = plane_state->base.crtc_y;
  8016. u32 pos = 0;
  8017. if (x < 0) {
  8018. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8019. x = -x;
  8020. }
  8021. pos |= x << CURSOR_X_SHIFT;
  8022. if (y < 0) {
  8023. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8024. y = -y;
  8025. }
  8026. pos |= y << CURSOR_Y_SHIFT;
  8027. return pos;
  8028. }
  8029. static bool intel_cursor_size_ok(const struct intel_plane_state *plane_state)
  8030. {
  8031. const struct drm_mode_config *config =
  8032. &plane_state->base.plane->dev->mode_config;
  8033. int width = plane_state->base.crtc_w;
  8034. int height = plane_state->base.crtc_h;
  8035. return width > 0 && width <= config->cursor_width &&
  8036. height > 0 && height <= config->cursor_height;
  8037. }
  8038. static int intel_check_cursor(struct intel_crtc_state *crtc_state,
  8039. struct intel_plane_state *plane_state)
  8040. {
  8041. const struct drm_framebuffer *fb = plane_state->base.fb;
  8042. int src_x, src_y;
  8043. u32 offset;
  8044. int ret;
  8045. ret = drm_atomic_helper_check_plane_state(&plane_state->base,
  8046. &crtc_state->base,
  8047. DRM_PLANE_HELPER_NO_SCALING,
  8048. DRM_PLANE_HELPER_NO_SCALING,
  8049. true, true);
  8050. if (ret)
  8051. return ret;
  8052. if (!fb)
  8053. return 0;
  8054. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  8055. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  8056. return -EINVAL;
  8057. }
  8058. src_x = plane_state->base.src_x >> 16;
  8059. src_y = plane_state->base.src_y >> 16;
  8060. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  8061. offset = intel_compute_tile_offset(&src_x, &src_y, plane_state, 0);
  8062. if (src_x != 0 || src_y != 0) {
  8063. DRM_DEBUG_KMS("Arbitrary cursor panning not supported\n");
  8064. return -EINVAL;
  8065. }
  8066. plane_state->main.offset = offset;
  8067. return 0;
  8068. }
  8069. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  8070. const struct intel_plane_state *plane_state)
  8071. {
  8072. const struct drm_framebuffer *fb = plane_state->base.fb;
  8073. return CURSOR_ENABLE |
  8074. CURSOR_GAMMA_ENABLE |
  8075. CURSOR_FORMAT_ARGB |
  8076. CURSOR_STRIDE(fb->pitches[0]);
  8077. }
  8078. static bool i845_cursor_size_ok(const struct intel_plane_state *plane_state)
  8079. {
  8080. int width = plane_state->base.crtc_w;
  8081. /*
  8082. * 845g/865g are only limited by the width of their cursors,
  8083. * the height is arbitrary up to the precision of the register.
  8084. */
  8085. return intel_cursor_size_ok(plane_state) && IS_ALIGNED(width, 64);
  8086. }
  8087. static int i845_check_cursor(struct intel_plane *plane,
  8088. struct intel_crtc_state *crtc_state,
  8089. struct intel_plane_state *plane_state)
  8090. {
  8091. const struct drm_framebuffer *fb = plane_state->base.fb;
  8092. int ret;
  8093. ret = intel_check_cursor(crtc_state, plane_state);
  8094. if (ret)
  8095. return ret;
  8096. /* if we want to turn off the cursor ignore width and height */
  8097. if (!fb)
  8098. return 0;
  8099. /* Check for which cursor types we support */
  8100. if (!i845_cursor_size_ok(plane_state)) {
  8101. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  8102. plane_state->base.crtc_w,
  8103. plane_state->base.crtc_h);
  8104. return -EINVAL;
  8105. }
  8106. switch (fb->pitches[0]) {
  8107. case 256:
  8108. case 512:
  8109. case 1024:
  8110. case 2048:
  8111. break;
  8112. default:
  8113. DRM_DEBUG_KMS("Invalid cursor stride (%u)\n",
  8114. fb->pitches[0]);
  8115. return -EINVAL;
  8116. }
  8117. plane_state->ctl = i845_cursor_ctl(crtc_state, plane_state);
  8118. return 0;
  8119. }
  8120. static void i845_update_cursor(struct intel_plane *plane,
  8121. const struct intel_crtc_state *crtc_state,
  8122. const struct intel_plane_state *plane_state)
  8123. {
  8124. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8125. u32 cntl = 0, base = 0, pos = 0, size = 0;
  8126. unsigned long irqflags;
  8127. if (plane_state && plane_state->base.visible) {
  8128. unsigned int width = plane_state->base.crtc_w;
  8129. unsigned int height = plane_state->base.crtc_h;
  8130. cntl = plane_state->ctl;
  8131. size = (height << 12) | width;
  8132. base = intel_cursor_base(plane_state);
  8133. pos = intel_cursor_position(plane_state);
  8134. }
  8135. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8136. /* On these chipsets we can only modify the base/size/stride
  8137. * whilst the cursor is disabled.
  8138. */
  8139. if (plane->cursor.base != base ||
  8140. plane->cursor.size != size ||
  8141. plane->cursor.cntl != cntl) {
  8142. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  8143. I915_WRITE_FW(CURBASE(PIPE_A), base);
  8144. I915_WRITE_FW(CURSIZE, size);
  8145. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  8146. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  8147. plane->cursor.base = base;
  8148. plane->cursor.size = size;
  8149. plane->cursor.cntl = cntl;
  8150. } else {
  8151. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  8152. }
  8153. POSTING_READ_FW(CURCNTR(PIPE_A));
  8154. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8155. }
  8156. static void i845_disable_cursor(struct intel_plane *plane,
  8157. struct intel_crtc *crtc)
  8158. {
  8159. i845_update_cursor(plane, NULL, NULL);
  8160. }
  8161. static bool i845_cursor_get_hw_state(struct intel_plane *plane,
  8162. enum pipe *pipe)
  8163. {
  8164. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8165. enum intel_display_power_domain power_domain;
  8166. bool ret;
  8167. power_domain = POWER_DOMAIN_PIPE(PIPE_A);
  8168. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8169. return false;
  8170. ret = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  8171. *pipe = PIPE_A;
  8172. intel_display_power_put(dev_priv, power_domain);
  8173. return ret;
  8174. }
  8175. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  8176. const struct intel_plane_state *plane_state)
  8177. {
  8178. struct drm_i915_private *dev_priv =
  8179. to_i915(plane_state->base.plane->dev);
  8180. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  8181. u32 cntl = 0;
  8182. if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  8183. cntl |= MCURSOR_TRICKLE_FEED_DISABLE;
  8184. if (INTEL_GEN(dev_priv) <= 10) {
  8185. cntl |= MCURSOR_GAMMA_ENABLE;
  8186. if (HAS_DDI(dev_priv))
  8187. cntl |= MCURSOR_PIPE_CSC_ENABLE;
  8188. }
  8189. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8190. cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
  8191. switch (plane_state->base.crtc_w) {
  8192. case 64:
  8193. cntl |= MCURSOR_MODE_64_ARGB_AX;
  8194. break;
  8195. case 128:
  8196. cntl |= MCURSOR_MODE_128_ARGB_AX;
  8197. break;
  8198. case 256:
  8199. cntl |= MCURSOR_MODE_256_ARGB_AX;
  8200. break;
  8201. default:
  8202. MISSING_CASE(plane_state->base.crtc_w);
  8203. return 0;
  8204. }
  8205. if (plane_state->base.rotation & DRM_MODE_ROTATE_180)
  8206. cntl |= MCURSOR_ROTATE_180;
  8207. return cntl;
  8208. }
  8209. static bool i9xx_cursor_size_ok(const struct intel_plane_state *plane_state)
  8210. {
  8211. struct drm_i915_private *dev_priv =
  8212. to_i915(plane_state->base.plane->dev);
  8213. int width = plane_state->base.crtc_w;
  8214. int height = plane_state->base.crtc_h;
  8215. if (!intel_cursor_size_ok(plane_state))
  8216. return false;
  8217. /* Cursor width is limited to a few power-of-two sizes */
  8218. switch (width) {
  8219. case 256:
  8220. case 128:
  8221. case 64:
  8222. break;
  8223. default:
  8224. return false;
  8225. }
  8226. /*
  8227. * IVB+ have CUR_FBC_CTL which allows an arbitrary cursor
  8228. * height from 8 lines up to the cursor width, when the
  8229. * cursor is not rotated. Everything else requires square
  8230. * cursors.
  8231. */
  8232. if (HAS_CUR_FBC(dev_priv) &&
  8233. plane_state->base.rotation & DRM_MODE_ROTATE_0) {
  8234. if (height < 8 || height > width)
  8235. return false;
  8236. } else {
  8237. if (height != width)
  8238. return false;
  8239. }
  8240. return true;
  8241. }
  8242. static int i9xx_check_cursor(struct intel_plane *plane,
  8243. struct intel_crtc_state *crtc_state,
  8244. struct intel_plane_state *plane_state)
  8245. {
  8246. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8247. const struct drm_framebuffer *fb = plane_state->base.fb;
  8248. enum pipe pipe = plane->pipe;
  8249. int ret;
  8250. ret = intel_check_cursor(crtc_state, plane_state);
  8251. if (ret)
  8252. return ret;
  8253. /* if we want to turn off the cursor ignore width and height */
  8254. if (!fb)
  8255. return 0;
  8256. /* Check for which cursor types we support */
  8257. if (!i9xx_cursor_size_ok(plane_state)) {
  8258. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  8259. plane_state->base.crtc_w,
  8260. plane_state->base.crtc_h);
  8261. return -EINVAL;
  8262. }
  8263. if (fb->pitches[0] != plane_state->base.crtc_w * fb->format->cpp[0]) {
  8264. DRM_DEBUG_KMS("Invalid cursor stride (%u) (cursor width %d)\n",
  8265. fb->pitches[0], plane_state->base.crtc_w);
  8266. return -EINVAL;
  8267. }
  8268. /*
  8269. * There's something wrong with the cursor on CHV pipe C.
  8270. * If it straddles the left edge of the screen then
  8271. * moving it away from the edge or disabling it often
  8272. * results in a pipe underrun, and often that can lead to
  8273. * dead pipe (constant underrun reported, and it scans
  8274. * out just a solid color). To recover from that, the
  8275. * display power well must be turned off and on again.
  8276. * Refuse the put the cursor into that compromised position.
  8277. */
  8278. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  8279. plane_state->base.visible && plane_state->base.crtc_x < 0) {
  8280. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  8281. return -EINVAL;
  8282. }
  8283. plane_state->ctl = i9xx_cursor_ctl(crtc_state, plane_state);
  8284. return 0;
  8285. }
  8286. static void i9xx_update_cursor(struct intel_plane *plane,
  8287. const struct intel_crtc_state *crtc_state,
  8288. const struct intel_plane_state *plane_state)
  8289. {
  8290. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8291. enum pipe pipe = plane->pipe;
  8292. u32 cntl = 0, base = 0, pos = 0, fbc_ctl = 0;
  8293. unsigned long irqflags;
  8294. if (plane_state && plane_state->base.visible) {
  8295. cntl = plane_state->ctl;
  8296. if (plane_state->base.crtc_h != plane_state->base.crtc_w)
  8297. fbc_ctl = CUR_FBC_CTL_EN | (plane_state->base.crtc_h - 1);
  8298. base = intel_cursor_base(plane_state);
  8299. pos = intel_cursor_position(plane_state);
  8300. }
  8301. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  8302. /*
  8303. * On some platforms writing CURCNTR first will also
  8304. * cause CURPOS to be armed by the CURBASE write.
  8305. * Without the CURCNTR write the CURPOS write would
  8306. * arm itself. Thus we always start the full update
  8307. * with a CURCNTR write.
  8308. *
  8309. * On other platforms CURPOS always requires the
  8310. * CURBASE write to arm the update. Additonally
  8311. * a write to any of the cursor register will cancel
  8312. * an already armed cursor update. Thus leaving out
  8313. * the CURBASE write after CURPOS could lead to a
  8314. * cursor that doesn't appear to move, or even change
  8315. * shape. Thus we always write CURBASE.
  8316. *
  8317. * CURCNTR and CUR_FBC_CTL are always
  8318. * armed by the CURBASE write only.
  8319. */
  8320. if (plane->cursor.base != base ||
  8321. plane->cursor.size != fbc_ctl ||
  8322. plane->cursor.cntl != cntl) {
  8323. I915_WRITE_FW(CURCNTR(pipe), cntl);
  8324. if (HAS_CUR_FBC(dev_priv))
  8325. I915_WRITE_FW(CUR_FBC_CTL(pipe), fbc_ctl);
  8326. I915_WRITE_FW(CURPOS(pipe), pos);
  8327. I915_WRITE_FW(CURBASE(pipe), base);
  8328. plane->cursor.base = base;
  8329. plane->cursor.size = fbc_ctl;
  8330. plane->cursor.cntl = cntl;
  8331. } else {
  8332. I915_WRITE_FW(CURPOS(pipe), pos);
  8333. I915_WRITE_FW(CURBASE(pipe), base);
  8334. }
  8335. POSTING_READ_FW(CURBASE(pipe));
  8336. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  8337. }
  8338. static void i9xx_disable_cursor(struct intel_plane *plane,
  8339. struct intel_crtc *crtc)
  8340. {
  8341. i9xx_update_cursor(plane, NULL, NULL);
  8342. }
  8343. static bool i9xx_cursor_get_hw_state(struct intel_plane *plane,
  8344. enum pipe *pipe)
  8345. {
  8346. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  8347. enum intel_display_power_domain power_domain;
  8348. bool ret;
  8349. u32 val;
  8350. /*
  8351. * Not 100% correct for planes that can move between pipes,
  8352. * but that's only the case for gen2-3 which don't have any
  8353. * display power wells.
  8354. */
  8355. power_domain = POWER_DOMAIN_PIPE(plane->pipe);
  8356. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8357. return false;
  8358. val = I915_READ(CURCNTR(plane->pipe));
  8359. ret = val & MCURSOR_MODE;
  8360. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  8361. *pipe = plane->pipe;
  8362. else
  8363. *pipe = (val & MCURSOR_PIPE_SELECT_MASK) >>
  8364. MCURSOR_PIPE_SELECT_SHIFT;
  8365. intel_display_power_put(dev_priv, power_domain);
  8366. return ret;
  8367. }
  8368. /* VESA 640x480x72Hz mode to set on the pipe */
  8369. static const struct drm_display_mode load_detect_mode = {
  8370. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8371. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8372. };
  8373. struct drm_framebuffer *
  8374. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  8375. struct drm_mode_fb_cmd2 *mode_cmd)
  8376. {
  8377. struct intel_framebuffer *intel_fb;
  8378. int ret;
  8379. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8380. if (!intel_fb)
  8381. return ERR_PTR(-ENOMEM);
  8382. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  8383. if (ret)
  8384. goto err;
  8385. return &intel_fb->base;
  8386. err:
  8387. kfree(intel_fb);
  8388. return ERR_PTR(ret);
  8389. }
  8390. static int intel_modeset_disable_planes(struct drm_atomic_state *state,
  8391. struct drm_crtc *crtc)
  8392. {
  8393. struct drm_plane *plane;
  8394. struct drm_plane_state *plane_state;
  8395. int ret, i;
  8396. ret = drm_atomic_add_affected_planes(state, crtc);
  8397. if (ret)
  8398. return ret;
  8399. for_each_new_plane_in_state(state, plane, plane_state, i) {
  8400. if (plane_state->crtc != crtc)
  8401. continue;
  8402. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  8403. if (ret)
  8404. return ret;
  8405. drm_atomic_set_fb_for_plane(plane_state, NULL);
  8406. }
  8407. return 0;
  8408. }
  8409. int intel_get_load_detect_pipe(struct drm_connector *connector,
  8410. const struct drm_display_mode *mode,
  8411. struct intel_load_detect_pipe *old,
  8412. struct drm_modeset_acquire_ctx *ctx)
  8413. {
  8414. struct intel_crtc *intel_crtc;
  8415. struct intel_encoder *intel_encoder =
  8416. intel_attached_encoder(connector);
  8417. struct drm_crtc *possible_crtc;
  8418. struct drm_encoder *encoder = &intel_encoder->base;
  8419. struct drm_crtc *crtc = NULL;
  8420. struct drm_device *dev = encoder->dev;
  8421. struct drm_i915_private *dev_priv = to_i915(dev);
  8422. struct drm_mode_config *config = &dev->mode_config;
  8423. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8424. struct drm_connector_state *connector_state;
  8425. struct intel_crtc_state *crtc_state;
  8426. int ret, i = -1;
  8427. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8428. connector->base.id, connector->name,
  8429. encoder->base.id, encoder->name);
  8430. old->restore_state = NULL;
  8431. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  8432. /*
  8433. * Algorithm gets a little messy:
  8434. *
  8435. * - if the connector already has an assigned crtc, use it (but make
  8436. * sure it's on first)
  8437. *
  8438. * - try to find the first unused crtc that can drive this connector,
  8439. * and use that if we find one
  8440. */
  8441. /* See if we already have a CRTC for this connector */
  8442. if (connector->state->crtc) {
  8443. crtc = connector->state->crtc;
  8444. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8445. if (ret)
  8446. goto fail;
  8447. /* Make sure the crtc and connector are running */
  8448. goto found;
  8449. }
  8450. /* Find an unused one (if possible) */
  8451. for_each_crtc(dev, possible_crtc) {
  8452. i++;
  8453. if (!(encoder->possible_crtcs & (1 << i)))
  8454. continue;
  8455. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8456. if (ret)
  8457. goto fail;
  8458. if (possible_crtc->state->enable) {
  8459. drm_modeset_unlock(&possible_crtc->mutex);
  8460. continue;
  8461. }
  8462. crtc = possible_crtc;
  8463. break;
  8464. }
  8465. /*
  8466. * If we didn't find an unused CRTC, don't use any.
  8467. */
  8468. if (!crtc) {
  8469. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8470. ret = -ENODEV;
  8471. goto fail;
  8472. }
  8473. found:
  8474. intel_crtc = to_intel_crtc(crtc);
  8475. state = drm_atomic_state_alloc(dev);
  8476. restore_state = drm_atomic_state_alloc(dev);
  8477. if (!state || !restore_state) {
  8478. ret = -ENOMEM;
  8479. goto fail;
  8480. }
  8481. state->acquire_ctx = ctx;
  8482. restore_state->acquire_ctx = ctx;
  8483. connector_state = drm_atomic_get_connector_state(state, connector);
  8484. if (IS_ERR(connector_state)) {
  8485. ret = PTR_ERR(connector_state);
  8486. goto fail;
  8487. }
  8488. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8489. if (ret)
  8490. goto fail;
  8491. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8492. if (IS_ERR(crtc_state)) {
  8493. ret = PTR_ERR(crtc_state);
  8494. goto fail;
  8495. }
  8496. crtc_state->base.active = crtc_state->base.enable = true;
  8497. if (!mode)
  8498. mode = &load_detect_mode;
  8499. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8500. if (ret)
  8501. goto fail;
  8502. ret = intel_modeset_disable_planes(state, crtc);
  8503. if (ret)
  8504. goto fail;
  8505. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8506. if (!ret)
  8507. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8508. if (!ret)
  8509. ret = drm_atomic_add_affected_planes(restore_state, crtc);
  8510. if (ret) {
  8511. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8512. goto fail;
  8513. }
  8514. ret = drm_atomic_commit(state);
  8515. if (ret) {
  8516. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8517. goto fail;
  8518. }
  8519. old->restore_state = restore_state;
  8520. drm_atomic_state_put(state);
  8521. /* let the connector get through one full cycle before testing */
  8522. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8523. return true;
  8524. fail:
  8525. if (state) {
  8526. drm_atomic_state_put(state);
  8527. state = NULL;
  8528. }
  8529. if (restore_state) {
  8530. drm_atomic_state_put(restore_state);
  8531. restore_state = NULL;
  8532. }
  8533. if (ret == -EDEADLK)
  8534. return ret;
  8535. return false;
  8536. }
  8537. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8538. struct intel_load_detect_pipe *old,
  8539. struct drm_modeset_acquire_ctx *ctx)
  8540. {
  8541. struct intel_encoder *intel_encoder =
  8542. intel_attached_encoder(connector);
  8543. struct drm_encoder *encoder = &intel_encoder->base;
  8544. struct drm_atomic_state *state = old->restore_state;
  8545. int ret;
  8546. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8547. connector->base.id, connector->name,
  8548. encoder->base.id, encoder->name);
  8549. if (!state)
  8550. return;
  8551. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8552. if (ret)
  8553. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8554. drm_atomic_state_put(state);
  8555. }
  8556. static int i9xx_pll_refclk(struct drm_device *dev,
  8557. const struct intel_crtc_state *pipe_config)
  8558. {
  8559. struct drm_i915_private *dev_priv = to_i915(dev);
  8560. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8561. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8562. return dev_priv->vbt.lvds_ssc_freq;
  8563. else if (HAS_PCH_SPLIT(dev_priv))
  8564. return 120000;
  8565. else if (!IS_GEN2(dev_priv))
  8566. return 96000;
  8567. else
  8568. return 48000;
  8569. }
  8570. /* Returns the clock of the currently programmed mode of the given pipe. */
  8571. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8572. struct intel_crtc_state *pipe_config)
  8573. {
  8574. struct drm_device *dev = crtc->base.dev;
  8575. struct drm_i915_private *dev_priv = to_i915(dev);
  8576. int pipe = pipe_config->cpu_transcoder;
  8577. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8578. u32 fp;
  8579. struct dpll clock;
  8580. int port_clock;
  8581. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8582. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8583. fp = pipe_config->dpll_hw_state.fp0;
  8584. else
  8585. fp = pipe_config->dpll_hw_state.fp1;
  8586. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8587. if (IS_PINEVIEW(dev_priv)) {
  8588. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8589. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8590. } else {
  8591. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8592. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8593. }
  8594. if (!IS_GEN2(dev_priv)) {
  8595. if (IS_PINEVIEW(dev_priv))
  8596. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8597. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8598. else
  8599. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8600. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8601. switch (dpll & DPLL_MODE_MASK) {
  8602. case DPLLB_MODE_DAC_SERIAL:
  8603. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8604. 5 : 10;
  8605. break;
  8606. case DPLLB_MODE_LVDS:
  8607. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8608. 7 : 14;
  8609. break;
  8610. default:
  8611. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8612. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8613. return;
  8614. }
  8615. if (IS_PINEVIEW(dev_priv))
  8616. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8617. else
  8618. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8619. } else {
  8620. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8621. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8622. if (is_lvds) {
  8623. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8624. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8625. if (lvds & LVDS_CLKB_POWER_UP)
  8626. clock.p2 = 7;
  8627. else
  8628. clock.p2 = 14;
  8629. } else {
  8630. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8631. clock.p1 = 2;
  8632. else {
  8633. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8634. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8635. }
  8636. if (dpll & PLL_P2_DIVIDE_BY_4)
  8637. clock.p2 = 4;
  8638. else
  8639. clock.p2 = 2;
  8640. }
  8641. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8642. }
  8643. /*
  8644. * This value includes pixel_multiplier. We will use
  8645. * port_clock to compute adjusted_mode.crtc_clock in the
  8646. * encoder's get_config() function.
  8647. */
  8648. pipe_config->port_clock = port_clock;
  8649. }
  8650. int intel_dotclock_calculate(int link_freq,
  8651. const struct intel_link_m_n *m_n)
  8652. {
  8653. /*
  8654. * The calculation for the data clock is:
  8655. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8656. * But we want to avoid losing precison if possible, so:
  8657. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8658. *
  8659. * and the link clock is simpler:
  8660. * link_clock = (m * link_clock) / n
  8661. */
  8662. if (!m_n->link_n)
  8663. return 0;
  8664. return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
  8665. }
  8666. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8667. struct intel_crtc_state *pipe_config)
  8668. {
  8669. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8670. /* read out port_clock from the DPLL */
  8671. i9xx_crtc_clock_get(crtc, pipe_config);
  8672. /*
  8673. * In case there is an active pipe without active ports,
  8674. * we may need some idea for the dotclock anyway.
  8675. * Calculate one based on the FDI configuration.
  8676. */
  8677. pipe_config->base.adjusted_mode.crtc_clock =
  8678. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8679. &pipe_config->fdi_m_n);
  8680. }
  8681. /* Returns the currently programmed mode of the given encoder. */
  8682. struct drm_display_mode *
  8683. intel_encoder_current_mode(struct intel_encoder *encoder)
  8684. {
  8685. struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  8686. struct intel_crtc_state *crtc_state;
  8687. struct drm_display_mode *mode;
  8688. struct intel_crtc *crtc;
  8689. enum pipe pipe;
  8690. if (!encoder->get_hw_state(encoder, &pipe))
  8691. return NULL;
  8692. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8693. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8694. if (!mode)
  8695. return NULL;
  8696. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  8697. if (!crtc_state) {
  8698. kfree(mode);
  8699. return NULL;
  8700. }
  8701. crtc_state->base.crtc = &crtc->base;
  8702. if (!dev_priv->display.get_pipe_config(crtc, crtc_state)) {
  8703. kfree(crtc_state);
  8704. kfree(mode);
  8705. return NULL;
  8706. }
  8707. encoder->get_config(encoder, crtc_state);
  8708. intel_mode_from_pipe_config(mode, crtc_state);
  8709. kfree(crtc_state);
  8710. return mode;
  8711. }
  8712. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8713. {
  8714. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8715. drm_crtc_cleanup(crtc);
  8716. kfree(intel_crtc);
  8717. }
  8718. /**
  8719. * intel_wm_need_update - Check whether watermarks need updating
  8720. * @plane: drm plane
  8721. * @state: new plane state
  8722. *
  8723. * Check current plane state versus the new one to determine whether
  8724. * watermarks need to be recalculated.
  8725. *
  8726. * Returns true or false.
  8727. */
  8728. static bool intel_wm_need_update(struct drm_plane *plane,
  8729. struct drm_plane_state *state)
  8730. {
  8731. struct intel_plane_state *new = to_intel_plane_state(state);
  8732. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  8733. /* Update watermarks on tiling or size changes. */
  8734. if (new->base.visible != cur->base.visible)
  8735. return true;
  8736. if (!cur->base.fb || !new->base.fb)
  8737. return false;
  8738. if (cur->base.fb->modifier != new->base.fb->modifier ||
  8739. cur->base.rotation != new->base.rotation ||
  8740. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  8741. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  8742. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  8743. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  8744. return true;
  8745. return false;
  8746. }
  8747. static bool needs_scaling(const struct intel_plane_state *state)
  8748. {
  8749. int src_w = drm_rect_width(&state->base.src) >> 16;
  8750. int src_h = drm_rect_height(&state->base.src) >> 16;
  8751. int dst_w = drm_rect_width(&state->base.dst);
  8752. int dst_h = drm_rect_height(&state->base.dst);
  8753. return (src_w != dst_w || src_h != dst_h);
  8754. }
  8755. int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
  8756. struct drm_crtc_state *crtc_state,
  8757. const struct intel_plane_state *old_plane_state,
  8758. struct drm_plane_state *plane_state)
  8759. {
  8760. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  8761. struct drm_crtc *crtc = crtc_state->crtc;
  8762. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8763. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  8764. struct drm_device *dev = crtc->dev;
  8765. struct drm_i915_private *dev_priv = to_i915(dev);
  8766. bool mode_changed = needs_modeset(crtc_state);
  8767. bool was_crtc_enabled = old_crtc_state->base.active;
  8768. bool is_crtc_enabled = crtc_state->active;
  8769. bool turn_off, turn_on, visible, was_visible;
  8770. struct drm_framebuffer *fb = plane_state->fb;
  8771. int ret;
  8772. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  8773. ret = skl_update_scaler_plane(
  8774. to_intel_crtc_state(crtc_state),
  8775. to_intel_plane_state(plane_state));
  8776. if (ret)
  8777. return ret;
  8778. }
  8779. was_visible = old_plane_state->base.visible;
  8780. visible = plane_state->visible;
  8781. if (!was_crtc_enabled && WARN_ON(was_visible))
  8782. was_visible = false;
  8783. /*
  8784. * Visibility is calculated as if the crtc was on, but
  8785. * after scaler setup everything depends on it being off
  8786. * when the crtc isn't active.
  8787. *
  8788. * FIXME this is wrong for watermarks. Watermarks should also
  8789. * be computed as if the pipe would be active. Perhaps move
  8790. * per-plane wm computation to the .check_plane() hook, and
  8791. * only combine the results from all planes in the current place?
  8792. */
  8793. if (!is_crtc_enabled) {
  8794. plane_state->visible = visible = false;
  8795. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  8796. }
  8797. if (!was_visible && !visible)
  8798. return 0;
  8799. if (fb != old_plane_state->base.fb)
  8800. pipe_config->fb_changed = true;
  8801. turn_off = was_visible && (!visible || mode_changed);
  8802. turn_on = visible && (!was_visible || mode_changed);
  8803. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  8804. intel_crtc->base.base.id, intel_crtc->base.name,
  8805. plane->base.base.id, plane->base.name,
  8806. fb ? fb->base.id : -1);
  8807. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  8808. plane->base.base.id, plane->base.name,
  8809. was_visible, visible,
  8810. turn_off, turn_on, mode_changed);
  8811. if (turn_on) {
  8812. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8813. pipe_config->update_wm_pre = true;
  8814. /* must disable cxsr around plane enable/disable */
  8815. if (plane->id != PLANE_CURSOR)
  8816. pipe_config->disable_cxsr = true;
  8817. } else if (turn_off) {
  8818. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8819. pipe_config->update_wm_post = true;
  8820. /* must disable cxsr around plane enable/disable */
  8821. if (plane->id != PLANE_CURSOR)
  8822. pipe_config->disable_cxsr = true;
  8823. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  8824. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  8825. /* FIXME bollocks */
  8826. pipe_config->update_wm_pre = true;
  8827. pipe_config->update_wm_post = true;
  8828. }
  8829. }
  8830. if (visible || was_visible)
  8831. pipe_config->fb_bits |= plane->frontbuffer_bit;
  8832. /*
  8833. * WaCxSRDisabledForSpriteScaling:ivb
  8834. *
  8835. * cstate->update_wm was already set above, so this flag will
  8836. * take effect when we commit and program watermarks.
  8837. */
  8838. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  8839. needs_scaling(to_intel_plane_state(plane_state)) &&
  8840. !needs_scaling(old_plane_state))
  8841. pipe_config->disable_lp_wm = true;
  8842. return 0;
  8843. }
  8844. static bool encoders_cloneable(const struct intel_encoder *a,
  8845. const struct intel_encoder *b)
  8846. {
  8847. /* masks could be asymmetric, so check both ways */
  8848. return a == b || (a->cloneable & (1 << b->type) &&
  8849. b->cloneable & (1 << a->type));
  8850. }
  8851. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  8852. struct intel_crtc *crtc,
  8853. struct intel_encoder *encoder)
  8854. {
  8855. struct intel_encoder *source_encoder;
  8856. struct drm_connector *connector;
  8857. struct drm_connector_state *connector_state;
  8858. int i;
  8859. for_each_new_connector_in_state(state, connector, connector_state, i) {
  8860. if (connector_state->crtc != &crtc->base)
  8861. continue;
  8862. source_encoder =
  8863. to_intel_encoder(connector_state->best_encoder);
  8864. if (!encoders_cloneable(encoder, source_encoder))
  8865. return false;
  8866. }
  8867. return true;
  8868. }
  8869. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  8870. struct drm_crtc_state *crtc_state)
  8871. {
  8872. struct drm_device *dev = crtc->dev;
  8873. struct drm_i915_private *dev_priv = to_i915(dev);
  8874. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8875. struct intel_crtc_state *pipe_config =
  8876. to_intel_crtc_state(crtc_state);
  8877. struct drm_atomic_state *state = crtc_state->state;
  8878. int ret;
  8879. bool mode_changed = needs_modeset(crtc_state);
  8880. if (mode_changed && !crtc_state->active)
  8881. pipe_config->update_wm_post = true;
  8882. if (mode_changed && crtc_state->enable &&
  8883. dev_priv->display.crtc_compute_clock &&
  8884. !WARN_ON(pipe_config->shared_dpll)) {
  8885. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  8886. pipe_config);
  8887. if (ret)
  8888. return ret;
  8889. }
  8890. if (crtc_state->color_mgmt_changed) {
  8891. ret = intel_color_check(crtc, crtc_state);
  8892. if (ret)
  8893. return ret;
  8894. /*
  8895. * Changing color management on Intel hardware is
  8896. * handled as part of planes update.
  8897. */
  8898. crtc_state->planes_changed = true;
  8899. }
  8900. ret = 0;
  8901. if (dev_priv->display.compute_pipe_wm) {
  8902. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  8903. if (ret) {
  8904. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  8905. return ret;
  8906. }
  8907. }
  8908. if (dev_priv->display.compute_intermediate_wm &&
  8909. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  8910. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  8911. return 0;
  8912. /*
  8913. * Calculate 'intermediate' watermarks that satisfy both the
  8914. * old state and the new state. We can program these
  8915. * immediately.
  8916. */
  8917. ret = dev_priv->display.compute_intermediate_wm(dev,
  8918. intel_crtc,
  8919. pipe_config);
  8920. if (ret) {
  8921. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  8922. return ret;
  8923. }
  8924. } else if (dev_priv->display.compute_intermediate_wm) {
  8925. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  8926. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  8927. }
  8928. if (INTEL_GEN(dev_priv) >= 9) {
  8929. if (mode_changed)
  8930. ret = skl_update_scaler_crtc(pipe_config);
  8931. if (!ret)
  8932. ret = skl_check_pipe_max_pixel_rate(intel_crtc,
  8933. pipe_config);
  8934. if (!ret)
  8935. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  8936. pipe_config);
  8937. }
  8938. if (HAS_IPS(dev_priv))
  8939. pipe_config->ips_enabled = hsw_compute_ips_config(pipe_config);
  8940. return ret;
  8941. }
  8942. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  8943. .atomic_begin = intel_begin_crtc_commit,
  8944. .atomic_flush = intel_finish_crtc_commit,
  8945. .atomic_check = intel_crtc_atomic_check,
  8946. };
  8947. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  8948. {
  8949. struct intel_connector *connector;
  8950. struct drm_connector_list_iter conn_iter;
  8951. drm_connector_list_iter_begin(dev, &conn_iter);
  8952. for_each_intel_connector_iter(connector, &conn_iter) {
  8953. if (connector->base.state->crtc)
  8954. drm_connector_unreference(&connector->base);
  8955. if (connector->base.encoder) {
  8956. connector->base.state->best_encoder =
  8957. connector->base.encoder;
  8958. connector->base.state->crtc =
  8959. connector->base.encoder->crtc;
  8960. drm_connector_reference(&connector->base);
  8961. } else {
  8962. connector->base.state->best_encoder = NULL;
  8963. connector->base.state->crtc = NULL;
  8964. }
  8965. }
  8966. drm_connector_list_iter_end(&conn_iter);
  8967. }
  8968. static void
  8969. connected_sink_compute_bpp(struct intel_connector *connector,
  8970. struct intel_crtc_state *pipe_config)
  8971. {
  8972. const struct drm_display_info *info = &connector->base.display_info;
  8973. int bpp = pipe_config->pipe_bpp;
  8974. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  8975. connector->base.base.id,
  8976. connector->base.name);
  8977. /* Don't use an invalid EDID bpc value */
  8978. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  8979. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  8980. bpp, info->bpc * 3);
  8981. pipe_config->pipe_bpp = info->bpc * 3;
  8982. }
  8983. /* Clamp bpp to 8 on screens without EDID 1.4 */
  8984. if (info->bpc == 0 && bpp > 24) {
  8985. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  8986. bpp);
  8987. pipe_config->pipe_bpp = 24;
  8988. }
  8989. }
  8990. static int
  8991. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  8992. struct intel_crtc_state *pipe_config)
  8993. {
  8994. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8995. struct drm_atomic_state *state;
  8996. struct drm_connector *connector;
  8997. struct drm_connector_state *connector_state;
  8998. int bpp, i;
  8999. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  9000. IS_CHERRYVIEW(dev_priv)))
  9001. bpp = 10*3;
  9002. else if (INTEL_GEN(dev_priv) >= 5)
  9003. bpp = 12*3;
  9004. else
  9005. bpp = 8*3;
  9006. pipe_config->pipe_bpp = bpp;
  9007. state = pipe_config->base.state;
  9008. /* Clamp display bpp to EDID value */
  9009. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9010. if (connector_state->crtc != &crtc->base)
  9011. continue;
  9012. connected_sink_compute_bpp(to_intel_connector(connector),
  9013. pipe_config);
  9014. }
  9015. return bpp;
  9016. }
  9017. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9018. {
  9019. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9020. "type: 0x%x flags: 0x%x\n",
  9021. mode->crtc_clock,
  9022. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9023. mode->crtc_hsync_end, mode->crtc_htotal,
  9024. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9025. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9026. }
  9027. static inline void
  9028. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  9029. unsigned int lane_count, struct intel_link_m_n *m_n)
  9030. {
  9031. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9032. id, lane_count,
  9033. m_n->gmch_m, m_n->gmch_n,
  9034. m_n->link_m, m_n->link_n, m_n->tu);
  9035. }
  9036. #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
  9037. static const char * const output_type_str[] = {
  9038. OUTPUT_TYPE(UNUSED),
  9039. OUTPUT_TYPE(ANALOG),
  9040. OUTPUT_TYPE(DVO),
  9041. OUTPUT_TYPE(SDVO),
  9042. OUTPUT_TYPE(LVDS),
  9043. OUTPUT_TYPE(TVOUT),
  9044. OUTPUT_TYPE(HDMI),
  9045. OUTPUT_TYPE(DP),
  9046. OUTPUT_TYPE(EDP),
  9047. OUTPUT_TYPE(DSI),
  9048. OUTPUT_TYPE(DDI),
  9049. OUTPUT_TYPE(DP_MST),
  9050. };
  9051. #undef OUTPUT_TYPE
  9052. static void snprintf_output_types(char *buf, size_t len,
  9053. unsigned int output_types)
  9054. {
  9055. char *str = buf;
  9056. int i;
  9057. str[0] = '\0';
  9058. for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
  9059. int r;
  9060. if ((output_types & BIT(i)) == 0)
  9061. continue;
  9062. r = snprintf(str, len, "%s%s",
  9063. str != buf ? "," : "", output_type_str[i]);
  9064. if (r >= len)
  9065. break;
  9066. str += r;
  9067. len -= r;
  9068. output_types &= ~BIT(i);
  9069. }
  9070. WARN_ON_ONCE(output_types != 0);
  9071. }
  9072. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9073. struct intel_crtc_state *pipe_config,
  9074. const char *context)
  9075. {
  9076. struct drm_device *dev = crtc->base.dev;
  9077. struct drm_i915_private *dev_priv = to_i915(dev);
  9078. struct drm_plane *plane;
  9079. struct intel_plane *intel_plane;
  9080. struct intel_plane_state *state;
  9081. struct drm_framebuffer *fb;
  9082. char buf[64];
  9083. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  9084. crtc->base.base.id, crtc->base.name, context);
  9085. snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
  9086. DRM_DEBUG_KMS("output_types: %s (0x%x)\n",
  9087. buf, pipe_config->output_types);
  9088. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  9089. transcoder_name(pipe_config->cpu_transcoder),
  9090. pipe_config->pipe_bpp, pipe_config->dither);
  9091. if (pipe_config->has_pch_encoder)
  9092. intel_dump_m_n_config(pipe_config, "fdi",
  9093. pipe_config->fdi_lanes,
  9094. &pipe_config->fdi_m_n);
  9095. if (pipe_config->ycbcr420)
  9096. DRM_DEBUG_KMS("YCbCr 4:2:0 output enabled\n");
  9097. if (intel_crtc_has_dp_encoder(pipe_config)) {
  9098. intel_dump_m_n_config(pipe_config, "dp m_n",
  9099. pipe_config->lane_count, &pipe_config->dp_m_n);
  9100. if (pipe_config->has_drrs)
  9101. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  9102. pipe_config->lane_count,
  9103. &pipe_config->dp_m2_n2);
  9104. }
  9105. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9106. pipe_config->has_audio, pipe_config->has_infoframe);
  9107. DRM_DEBUG_KMS("requested mode:\n");
  9108. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9109. DRM_DEBUG_KMS("adjusted mode:\n");
  9110. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9111. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9112. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  9113. pipe_config->port_clock,
  9114. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  9115. pipe_config->pixel_rate);
  9116. if (INTEL_GEN(dev_priv) >= 9)
  9117. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9118. crtc->num_scalers,
  9119. pipe_config->scaler_state.scaler_users,
  9120. pipe_config->scaler_state.scaler_id);
  9121. if (HAS_GMCH_DISPLAY(dev_priv))
  9122. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9123. pipe_config->gmch_pfit.control,
  9124. pipe_config->gmch_pfit.pgm_ratios,
  9125. pipe_config->gmch_pfit.lvds_border_bits);
  9126. else
  9127. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9128. pipe_config->pch_pfit.pos,
  9129. pipe_config->pch_pfit.size,
  9130. enableddisabled(pipe_config->pch_pfit.enabled));
  9131. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  9132. pipe_config->ips_enabled, pipe_config->double_wide);
  9133. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  9134. DRM_DEBUG_KMS("planes on this crtc\n");
  9135. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9136. struct drm_format_name_buf format_name;
  9137. intel_plane = to_intel_plane(plane);
  9138. if (intel_plane->pipe != crtc->pipe)
  9139. continue;
  9140. state = to_intel_plane_state(plane->state);
  9141. fb = state->base.fb;
  9142. if (!fb) {
  9143. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  9144. plane->base.id, plane->name, state->scaler_id);
  9145. continue;
  9146. }
  9147. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  9148. plane->base.id, plane->name,
  9149. fb->base.id, fb->width, fb->height,
  9150. drm_get_format_name(fb->format->format, &format_name));
  9151. if (INTEL_GEN(dev_priv) >= 9)
  9152. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  9153. state->scaler_id,
  9154. state->base.src.x1 >> 16,
  9155. state->base.src.y1 >> 16,
  9156. drm_rect_width(&state->base.src) >> 16,
  9157. drm_rect_height(&state->base.src) >> 16,
  9158. state->base.dst.x1, state->base.dst.y1,
  9159. drm_rect_width(&state->base.dst),
  9160. drm_rect_height(&state->base.dst));
  9161. }
  9162. }
  9163. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9164. {
  9165. struct drm_device *dev = state->dev;
  9166. struct drm_connector *connector;
  9167. struct drm_connector_list_iter conn_iter;
  9168. unsigned int used_ports = 0;
  9169. unsigned int used_mst_ports = 0;
  9170. bool ret = true;
  9171. /*
  9172. * Walk the connector list instead of the encoder
  9173. * list to detect the problem on ddi platforms
  9174. * where there's just one encoder per digital port.
  9175. */
  9176. drm_connector_list_iter_begin(dev, &conn_iter);
  9177. drm_for_each_connector_iter(connector, &conn_iter) {
  9178. struct drm_connector_state *connector_state;
  9179. struct intel_encoder *encoder;
  9180. connector_state = drm_atomic_get_new_connector_state(state, connector);
  9181. if (!connector_state)
  9182. connector_state = connector->state;
  9183. if (!connector_state->best_encoder)
  9184. continue;
  9185. encoder = to_intel_encoder(connector_state->best_encoder);
  9186. WARN_ON(!connector_state->crtc);
  9187. switch (encoder->type) {
  9188. unsigned int port_mask;
  9189. case INTEL_OUTPUT_DDI:
  9190. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9191. break;
  9192. case INTEL_OUTPUT_DP:
  9193. case INTEL_OUTPUT_HDMI:
  9194. case INTEL_OUTPUT_EDP:
  9195. port_mask = 1 << encoder->port;
  9196. /* the same port mustn't appear more than once */
  9197. if (used_ports & port_mask)
  9198. ret = false;
  9199. used_ports |= port_mask;
  9200. break;
  9201. case INTEL_OUTPUT_DP_MST:
  9202. used_mst_ports |=
  9203. 1 << encoder->port;
  9204. break;
  9205. default:
  9206. break;
  9207. }
  9208. }
  9209. drm_connector_list_iter_end(&conn_iter);
  9210. /* can't mix MST and SST/HDMI on the same port */
  9211. if (used_ports & used_mst_ports)
  9212. return false;
  9213. return ret;
  9214. }
  9215. static void
  9216. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9217. {
  9218. struct drm_i915_private *dev_priv =
  9219. to_i915(crtc_state->base.crtc->dev);
  9220. struct intel_crtc_scaler_state scaler_state;
  9221. struct intel_dpll_hw_state dpll_hw_state;
  9222. struct intel_shared_dpll *shared_dpll;
  9223. struct intel_crtc_wm_state wm_state;
  9224. bool force_thru, ips_force_disable;
  9225. /* FIXME: before the switch to atomic started, a new pipe_config was
  9226. * kzalloc'd. Code that depends on any field being zero should be
  9227. * fixed, so that the crtc_state can be safely duplicated. For now,
  9228. * only fields that are know to not cause problems are preserved. */
  9229. scaler_state = crtc_state->scaler_state;
  9230. shared_dpll = crtc_state->shared_dpll;
  9231. dpll_hw_state = crtc_state->dpll_hw_state;
  9232. force_thru = crtc_state->pch_pfit.force_thru;
  9233. ips_force_disable = crtc_state->ips_force_disable;
  9234. if (IS_G4X(dev_priv) ||
  9235. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9236. wm_state = crtc_state->wm;
  9237. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9238. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9239. memset(&crtc_state->base + 1, 0,
  9240. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9241. crtc_state->scaler_state = scaler_state;
  9242. crtc_state->shared_dpll = shared_dpll;
  9243. crtc_state->dpll_hw_state = dpll_hw_state;
  9244. crtc_state->pch_pfit.force_thru = force_thru;
  9245. crtc_state->ips_force_disable = ips_force_disable;
  9246. if (IS_G4X(dev_priv) ||
  9247. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9248. crtc_state->wm = wm_state;
  9249. }
  9250. static int
  9251. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9252. struct intel_crtc_state *pipe_config)
  9253. {
  9254. struct drm_atomic_state *state = pipe_config->base.state;
  9255. struct intel_encoder *encoder;
  9256. struct drm_connector *connector;
  9257. struct drm_connector_state *connector_state;
  9258. int base_bpp, ret = -EINVAL;
  9259. int i;
  9260. bool retry = true;
  9261. clear_intel_crtc_state(pipe_config);
  9262. pipe_config->cpu_transcoder =
  9263. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9264. /*
  9265. * Sanitize sync polarity flags based on requested ones. If neither
  9266. * positive or negative polarity is requested, treat this as meaning
  9267. * negative polarity.
  9268. */
  9269. if (!(pipe_config->base.adjusted_mode.flags &
  9270. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9271. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9272. if (!(pipe_config->base.adjusted_mode.flags &
  9273. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9274. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9275. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9276. pipe_config);
  9277. if (base_bpp < 0)
  9278. goto fail;
  9279. /*
  9280. * Determine the real pipe dimensions. Note that stereo modes can
  9281. * increase the actual pipe size due to the frame doubling and
  9282. * insertion of additional space for blanks between the frame. This
  9283. * is stored in the crtc timings. We use the requested mode to do this
  9284. * computation to clearly distinguish it from the adjusted mode, which
  9285. * can be changed by the connectors in the below retry loop.
  9286. */
  9287. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9288. &pipe_config->pipe_src_w,
  9289. &pipe_config->pipe_src_h);
  9290. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9291. if (connector_state->crtc != crtc)
  9292. continue;
  9293. encoder = to_intel_encoder(connector_state->best_encoder);
  9294. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9295. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9296. goto fail;
  9297. }
  9298. /*
  9299. * Determine output_types before calling the .compute_config()
  9300. * hooks so that the hooks can use this information safely.
  9301. */
  9302. if (encoder->compute_output_type)
  9303. pipe_config->output_types |=
  9304. BIT(encoder->compute_output_type(encoder, pipe_config,
  9305. connector_state));
  9306. else
  9307. pipe_config->output_types |= BIT(encoder->type);
  9308. }
  9309. encoder_retry:
  9310. /* Ensure the port clock defaults are reset when retrying. */
  9311. pipe_config->port_clock = 0;
  9312. pipe_config->pixel_multiplier = 1;
  9313. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9314. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9315. CRTC_STEREO_DOUBLE);
  9316. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9317. * adjust it according to limitations or connector properties, and also
  9318. * a chance to reject the mode entirely.
  9319. */
  9320. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9321. if (connector_state->crtc != crtc)
  9322. continue;
  9323. encoder = to_intel_encoder(connector_state->best_encoder);
  9324. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9325. DRM_DEBUG_KMS("Encoder config failure\n");
  9326. goto fail;
  9327. }
  9328. }
  9329. /* Set default port clock if not overwritten by the encoder. Needs to be
  9330. * done afterwards in case the encoder adjusts the mode. */
  9331. if (!pipe_config->port_clock)
  9332. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9333. * pipe_config->pixel_multiplier;
  9334. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9335. if (ret < 0) {
  9336. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9337. goto fail;
  9338. }
  9339. if (ret == RETRY) {
  9340. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9341. ret = -EINVAL;
  9342. goto fail;
  9343. }
  9344. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9345. retry = false;
  9346. goto encoder_retry;
  9347. }
  9348. /* Dithering seems to not pass-through bits correctly when it should, so
  9349. * only enable it on 6bpc panels and when its not a compliance
  9350. * test requesting 6bpc video pattern.
  9351. */
  9352. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9353. !pipe_config->dither_force_disable;
  9354. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9355. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9356. fail:
  9357. return ret;
  9358. }
  9359. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9360. {
  9361. int diff;
  9362. if (clock1 == clock2)
  9363. return true;
  9364. if (!clock1 || !clock2)
  9365. return false;
  9366. diff = abs(clock1 - clock2);
  9367. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9368. return true;
  9369. return false;
  9370. }
  9371. static bool
  9372. intel_compare_m_n(unsigned int m, unsigned int n,
  9373. unsigned int m2, unsigned int n2,
  9374. bool exact)
  9375. {
  9376. if (m == m2 && n == n2)
  9377. return true;
  9378. if (exact || !m || !n || !m2 || !n2)
  9379. return false;
  9380. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9381. if (n > n2) {
  9382. while (n > n2) {
  9383. m2 <<= 1;
  9384. n2 <<= 1;
  9385. }
  9386. } else if (n < n2) {
  9387. while (n < n2) {
  9388. m <<= 1;
  9389. n <<= 1;
  9390. }
  9391. }
  9392. if (n != n2)
  9393. return false;
  9394. return intel_fuzzy_clock_check(m, m2);
  9395. }
  9396. static bool
  9397. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9398. struct intel_link_m_n *m2_n2,
  9399. bool adjust)
  9400. {
  9401. if (m_n->tu == m2_n2->tu &&
  9402. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9403. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9404. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9405. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9406. if (adjust)
  9407. *m2_n2 = *m_n;
  9408. return true;
  9409. }
  9410. return false;
  9411. }
  9412. static void __printf(3, 4)
  9413. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9414. {
  9415. struct va_format vaf;
  9416. va_list args;
  9417. va_start(args, format);
  9418. vaf.fmt = format;
  9419. vaf.va = &args;
  9420. if (adjust)
  9421. drm_dbg(DRM_UT_KMS, "mismatch in %s %pV", name, &vaf);
  9422. else
  9423. drm_err("mismatch in %s %pV", name, &vaf);
  9424. va_end(args);
  9425. }
  9426. static bool
  9427. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9428. struct intel_crtc_state *current_config,
  9429. struct intel_crtc_state *pipe_config,
  9430. bool adjust)
  9431. {
  9432. bool ret = true;
  9433. bool fixup_inherited = adjust &&
  9434. (current_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED) &&
  9435. !(pipe_config->base.mode.private_flags & I915_MODE_FLAG_INHERITED);
  9436. #define PIPE_CONF_CHECK_X(name) do { \
  9437. if (current_config->name != pipe_config->name) { \
  9438. pipe_config_err(adjust, __stringify(name), \
  9439. "(expected 0x%08x, found 0x%08x)\n", \
  9440. current_config->name, \
  9441. pipe_config->name); \
  9442. ret = false; \
  9443. } \
  9444. } while (0)
  9445. #define PIPE_CONF_CHECK_I(name) do { \
  9446. if (current_config->name != pipe_config->name) { \
  9447. pipe_config_err(adjust, __stringify(name), \
  9448. "(expected %i, found %i)\n", \
  9449. current_config->name, \
  9450. pipe_config->name); \
  9451. ret = false; \
  9452. } \
  9453. } while (0)
  9454. #define PIPE_CONF_CHECK_BOOL(name) do { \
  9455. if (current_config->name != pipe_config->name) { \
  9456. pipe_config_err(adjust, __stringify(name), \
  9457. "(expected %s, found %s)\n", \
  9458. yesno(current_config->name), \
  9459. yesno(pipe_config->name)); \
  9460. ret = false; \
  9461. } \
  9462. } while (0)
  9463. /*
  9464. * Checks state where we only read out the enabling, but not the entire
  9465. * state itself (like full infoframes or ELD for audio). These states
  9466. * require a full modeset on bootup to fix up.
  9467. */
  9468. #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
  9469. if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
  9470. PIPE_CONF_CHECK_BOOL(name); \
  9471. } else { \
  9472. pipe_config_err(adjust, __stringify(name), \
  9473. "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)\n", \
  9474. yesno(current_config->name), \
  9475. yesno(pipe_config->name)); \
  9476. ret = false; \
  9477. } \
  9478. } while (0)
  9479. #define PIPE_CONF_CHECK_P(name) do { \
  9480. if (current_config->name != pipe_config->name) { \
  9481. pipe_config_err(adjust, __stringify(name), \
  9482. "(expected %p, found %p)\n", \
  9483. current_config->name, \
  9484. pipe_config->name); \
  9485. ret = false; \
  9486. } \
  9487. } while (0)
  9488. #define PIPE_CONF_CHECK_M_N(name) do { \
  9489. if (!intel_compare_link_m_n(&current_config->name, \
  9490. &pipe_config->name,\
  9491. adjust)) { \
  9492. pipe_config_err(adjust, __stringify(name), \
  9493. "(expected tu %i gmch %i/%i link %i/%i, " \
  9494. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9495. current_config->name.tu, \
  9496. current_config->name.gmch_m, \
  9497. current_config->name.gmch_n, \
  9498. current_config->name.link_m, \
  9499. current_config->name.link_n, \
  9500. pipe_config->name.tu, \
  9501. pipe_config->name.gmch_m, \
  9502. pipe_config->name.gmch_n, \
  9503. pipe_config->name.link_m, \
  9504. pipe_config->name.link_n); \
  9505. ret = false; \
  9506. } \
  9507. } while (0)
  9508. /* This is required for BDW+ where there is only one set of registers for
  9509. * switching between high and low RR.
  9510. * This macro can be used whenever a comparison has to be made between one
  9511. * hw state and multiple sw state variables.
  9512. */
  9513. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
  9514. if (!intel_compare_link_m_n(&current_config->name, \
  9515. &pipe_config->name, adjust) && \
  9516. !intel_compare_link_m_n(&current_config->alt_name, \
  9517. &pipe_config->name, adjust)) { \
  9518. pipe_config_err(adjust, __stringify(name), \
  9519. "(expected tu %i gmch %i/%i link %i/%i, " \
  9520. "or tu %i gmch %i/%i link %i/%i, " \
  9521. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9522. current_config->name.tu, \
  9523. current_config->name.gmch_m, \
  9524. current_config->name.gmch_n, \
  9525. current_config->name.link_m, \
  9526. current_config->name.link_n, \
  9527. current_config->alt_name.tu, \
  9528. current_config->alt_name.gmch_m, \
  9529. current_config->alt_name.gmch_n, \
  9530. current_config->alt_name.link_m, \
  9531. current_config->alt_name.link_n, \
  9532. pipe_config->name.tu, \
  9533. pipe_config->name.gmch_m, \
  9534. pipe_config->name.gmch_n, \
  9535. pipe_config->name.link_m, \
  9536. pipe_config->name.link_n); \
  9537. ret = false; \
  9538. } \
  9539. } while (0)
  9540. #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
  9541. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9542. pipe_config_err(adjust, __stringify(name), \
  9543. "(%x) (expected %i, found %i)\n", \
  9544. (mask), \
  9545. current_config->name & (mask), \
  9546. pipe_config->name & (mask)); \
  9547. ret = false; \
  9548. } \
  9549. } while (0)
  9550. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
  9551. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9552. pipe_config_err(adjust, __stringify(name), \
  9553. "(expected %i, found %i)\n", \
  9554. current_config->name, \
  9555. pipe_config->name); \
  9556. ret = false; \
  9557. } \
  9558. } while (0)
  9559. #define PIPE_CONF_QUIRK(quirk) \
  9560. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9561. PIPE_CONF_CHECK_I(cpu_transcoder);
  9562. PIPE_CONF_CHECK_BOOL(has_pch_encoder);
  9563. PIPE_CONF_CHECK_I(fdi_lanes);
  9564. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9565. PIPE_CONF_CHECK_I(lane_count);
  9566. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9567. if (INTEL_GEN(dev_priv) < 8) {
  9568. PIPE_CONF_CHECK_M_N(dp_m_n);
  9569. if (current_config->has_drrs)
  9570. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9571. } else
  9572. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9573. PIPE_CONF_CHECK_X(output_types);
  9574. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9575. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9576. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9577. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9578. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9579. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9580. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9581. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9582. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9583. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9584. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9585. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9586. PIPE_CONF_CHECK_I(pixel_multiplier);
  9587. PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
  9588. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9589. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9590. PIPE_CONF_CHECK_BOOL(limited_color_range);
  9591. PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
  9592. PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
  9593. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_infoframe);
  9594. PIPE_CONF_CHECK_BOOL(ycbcr420);
  9595. PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
  9596. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9597. DRM_MODE_FLAG_INTERLACE);
  9598. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9599. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9600. DRM_MODE_FLAG_PHSYNC);
  9601. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9602. DRM_MODE_FLAG_NHSYNC);
  9603. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9604. DRM_MODE_FLAG_PVSYNC);
  9605. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9606. DRM_MODE_FLAG_NVSYNC);
  9607. }
  9608. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9609. /* pfit ratios are autocomputed by the hw on gen4+ */
  9610. if (INTEL_GEN(dev_priv) < 4)
  9611. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9612. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9613. if (!adjust) {
  9614. PIPE_CONF_CHECK_I(pipe_src_w);
  9615. PIPE_CONF_CHECK_I(pipe_src_h);
  9616. PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
  9617. if (current_config->pch_pfit.enabled) {
  9618. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9619. PIPE_CONF_CHECK_X(pch_pfit.size);
  9620. }
  9621. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9622. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9623. }
  9624. PIPE_CONF_CHECK_BOOL(double_wide);
  9625. PIPE_CONF_CHECK_P(shared_dpll);
  9626. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9627. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9628. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9629. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9630. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9631. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9632. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9633. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9634. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9635. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
  9636. PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
  9637. PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
  9638. PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
  9639. PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
  9640. PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
  9641. PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
  9642. PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
  9643. PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
  9644. PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
  9645. PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
  9646. PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
  9647. PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
  9648. PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
  9649. PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
  9650. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
  9651. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
  9652. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
  9653. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
  9654. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
  9655. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
  9656. PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
  9657. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9658. PIPE_CONF_CHECK_X(dsi_pll.div);
  9659. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9660. PIPE_CONF_CHECK_I(pipe_bpp);
  9661. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9662. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9663. PIPE_CONF_CHECK_I(min_voltage_level);
  9664. #undef PIPE_CONF_CHECK_X
  9665. #undef PIPE_CONF_CHECK_I
  9666. #undef PIPE_CONF_CHECK_BOOL
  9667. #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
  9668. #undef PIPE_CONF_CHECK_P
  9669. #undef PIPE_CONF_CHECK_FLAGS
  9670. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9671. #undef PIPE_CONF_QUIRK
  9672. return ret;
  9673. }
  9674. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9675. const struct intel_crtc_state *pipe_config)
  9676. {
  9677. if (pipe_config->has_pch_encoder) {
  9678. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9679. &pipe_config->fdi_m_n);
  9680. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9681. /*
  9682. * FDI already provided one idea for the dotclock.
  9683. * Yell if the encoder disagrees.
  9684. */
  9685. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9686. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9687. fdi_dotclock, dotclock);
  9688. }
  9689. }
  9690. static void verify_wm_state(struct drm_crtc *crtc,
  9691. struct drm_crtc_state *new_state)
  9692. {
  9693. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9694. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9695. struct skl_pipe_wm hw_wm, *sw_wm;
  9696. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9697. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9698. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9699. const enum pipe pipe = intel_crtc->pipe;
  9700. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9701. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9702. return;
  9703. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9704. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9705. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9706. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9707. if (INTEL_GEN(dev_priv) >= 11)
  9708. if (hw_ddb.enabled_slices != sw_ddb->enabled_slices)
  9709. DRM_ERROR("mismatch in DBUF Slices (expected %u, got %u)\n",
  9710. sw_ddb->enabled_slices,
  9711. hw_ddb.enabled_slices);
  9712. /* planes */
  9713. for_each_universal_plane(dev_priv, pipe, plane) {
  9714. hw_plane_wm = &hw_wm.planes[plane];
  9715. sw_plane_wm = &sw_wm->planes[plane];
  9716. /* Watermarks */
  9717. for (level = 0; level <= max_level; level++) {
  9718. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9719. &sw_plane_wm->wm[level]))
  9720. continue;
  9721. DRM_ERROR("mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9722. pipe_name(pipe), plane + 1, level,
  9723. sw_plane_wm->wm[level].plane_en,
  9724. sw_plane_wm->wm[level].plane_res_b,
  9725. sw_plane_wm->wm[level].plane_res_l,
  9726. hw_plane_wm->wm[level].plane_en,
  9727. hw_plane_wm->wm[level].plane_res_b,
  9728. hw_plane_wm->wm[level].plane_res_l);
  9729. }
  9730. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9731. &sw_plane_wm->trans_wm)) {
  9732. DRM_ERROR("mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9733. pipe_name(pipe), plane + 1,
  9734. sw_plane_wm->trans_wm.plane_en,
  9735. sw_plane_wm->trans_wm.plane_res_b,
  9736. sw_plane_wm->trans_wm.plane_res_l,
  9737. hw_plane_wm->trans_wm.plane_en,
  9738. hw_plane_wm->trans_wm.plane_res_b,
  9739. hw_plane_wm->trans_wm.plane_res_l);
  9740. }
  9741. /* DDB */
  9742. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9743. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9744. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9745. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9746. pipe_name(pipe), plane + 1,
  9747. sw_ddb_entry->start, sw_ddb_entry->end,
  9748. hw_ddb_entry->start, hw_ddb_entry->end);
  9749. }
  9750. }
  9751. /*
  9752. * cursor
  9753. * If the cursor plane isn't active, we may not have updated it's ddb
  9754. * allocation. In that case since the ddb allocation will be updated
  9755. * once the plane becomes visible, we can skip this check
  9756. */
  9757. if (1) {
  9758. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9759. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9760. /* Watermarks */
  9761. for (level = 0; level <= max_level; level++) {
  9762. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9763. &sw_plane_wm->wm[level]))
  9764. continue;
  9765. DRM_ERROR("mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9766. pipe_name(pipe), level,
  9767. sw_plane_wm->wm[level].plane_en,
  9768. sw_plane_wm->wm[level].plane_res_b,
  9769. sw_plane_wm->wm[level].plane_res_l,
  9770. hw_plane_wm->wm[level].plane_en,
  9771. hw_plane_wm->wm[level].plane_res_b,
  9772. hw_plane_wm->wm[level].plane_res_l);
  9773. }
  9774. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9775. &sw_plane_wm->trans_wm)) {
  9776. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9777. pipe_name(pipe),
  9778. sw_plane_wm->trans_wm.plane_en,
  9779. sw_plane_wm->trans_wm.plane_res_b,
  9780. sw_plane_wm->trans_wm.plane_res_l,
  9781. hw_plane_wm->trans_wm.plane_en,
  9782. hw_plane_wm->trans_wm.plane_res_b,
  9783. hw_plane_wm->trans_wm.plane_res_l);
  9784. }
  9785. /* DDB */
  9786. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9787. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9788. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9789. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9790. pipe_name(pipe),
  9791. sw_ddb_entry->start, sw_ddb_entry->end,
  9792. hw_ddb_entry->start, hw_ddb_entry->end);
  9793. }
  9794. }
  9795. }
  9796. static void
  9797. verify_connector_state(struct drm_device *dev,
  9798. struct drm_atomic_state *state,
  9799. struct drm_crtc *crtc)
  9800. {
  9801. struct drm_connector *connector;
  9802. struct drm_connector_state *new_conn_state;
  9803. int i;
  9804. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9805. struct drm_encoder *encoder = connector->encoder;
  9806. struct drm_crtc_state *crtc_state = NULL;
  9807. if (new_conn_state->crtc != crtc)
  9808. continue;
  9809. if (crtc)
  9810. crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
  9811. intel_connector_verify_state(crtc_state, new_conn_state);
  9812. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9813. "connector's atomic encoder doesn't match legacy encoder\n");
  9814. }
  9815. }
  9816. static void
  9817. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  9818. {
  9819. struct intel_encoder *encoder;
  9820. struct drm_connector *connector;
  9821. struct drm_connector_state *old_conn_state, *new_conn_state;
  9822. int i;
  9823. for_each_intel_encoder(dev, encoder) {
  9824. bool enabled = false, found = false;
  9825. enum pipe pipe;
  9826. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9827. encoder->base.base.id,
  9828. encoder->base.name);
  9829. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  9830. new_conn_state, i) {
  9831. if (old_conn_state->best_encoder == &encoder->base)
  9832. found = true;
  9833. if (new_conn_state->best_encoder != &encoder->base)
  9834. continue;
  9835. found = enabled = true;
  9836. I915_STATE_WARN(new_conn_state->crtc !=
  9837. encoder->base.crtc,
  9838. "connector's crtc doesn't match encoder crtc\n");
  9839. }
  9840. if (!found)
  9841. continue;
  9842. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9843. "encoder's enabled state mismatch "
  9844. "(expected %i, found %i)\n",
  9845. !!encoder->base.crtc, enabled);
  9846. if (!encoder->base.crtc) {
  9847. bool active;
  9848. active = encoder->get_hw_state(encoder, &pipe);
  9849. I915_STATE_WARN(active,
  9850. "encoder detached but still enabled on pipe %c.\n",
  9851. pipe_name(pipe));
  9852. }
  9853. }
  9854. }
  9855. static void
  9856. verify_crtc_state(struct drm_crtc *crtc,
  9857. struct drm_crtc_state *old_crtc_state,
  9858. struct drm_crtc_state *new_crtc_state)
  9859. {
  9860. struct drm_device *dev = crtc->dev;
  9861. struct drm_i915_private *dev_priv = to_i915(dev);
  9862. struct intel_encoder *encoder;
  9863. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9864. struct intel_crtc_state *pipe_config, *sw_config;
  9865. struct drm_atomic_state *old_state;
  9866. bool active;
  9867. old_state = old_crtc_state->state;
  9868. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  9869. pipe_config = to_intel_crtc_state(old_crtc_state);
  9870. memset(pipe_config, 0, sizeof(*pipe_config));
  9871. pipe_config->base.crtc = crtc;
  9872. pipe_config->base.state = old_state;
  9873. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  9874. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  9875. /* we keep both pipes enabled on 830 */
  9876. if (IS_I830(dev_priv))
  9877. active = new_crtc_state->active;
  9878. I915_STATE_WARN(new_crtc_state->active != active,
  9879. "crtc active state doesn't match with hw state "
  9880. "(expected %i, found %i)\n", new_crtc_state->active, active);
  9881. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  9882. "transitional active state does not match atomic hw state "
  9883. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  9884. for_each_encoder_on_crtc(dev, crtc, encoder) {
  9885. enum pipe pipe;
  9886. active = encoder->get_hw_state(encoder, &pipe);
  9887. I915_STATE_WARN(active != new_crtc_state->active,
  9888. "[ENCODER:%i] active %i with crtc active %i\n",
  9889. encoder->base.base.id, active, new_crtc_state->active);
  9890. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  9891. "Encoder connected to wrong pipe %c\n",
  9892. pipe_name(pipe));
  9893. if (active)
  9894. encoder->get_config(encoder, pipe_config);
  9895. }
  9896. intel_crtc_compute_pixel_rate(pipe_config);
  9897. if (!new_crtc_state->active)
  9898. return;
  9899. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  9900. sw_config = to_intel_crtc_state(new_crtc_state);
  9901. if (!intel_pipe_config_compare(dev_priv, sw_config,
  9902. pipe_config, false)) {
  9903. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  9904. intel_dump_pipe_config(intel_crtc, pipe_config,
  9905. "[hw state]");
  9906. intel_dump_pipe_config(intel_crtc, sw_config,
  9907. "[sw state]");
  9908. }
  9909. }
  9910. static void
  9911. intel_verify_planes(struct intel_atomic_state *state)
  9912. {
  9913. struct intel_plane *plane;
  9914. const struct intel_plane_state *plane_state;
  9915. int i;
  9916. for_each_new_intel_plane_in_state(state, plane,
  9917. plane_state, i)
  9918. assert_plane(plane, plane_state->base.visible);
  9919. }
  9920. static void
  9921. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  9922. struct intel_shared_dpll *pll,
  9923. struct drm_crtc *crtc,
  9924. struct drm_crtc_state *new_state)
  9925. {
  9926. struct intel_dpll_hw_state dpll_hw_state;
  9927. unsigned crtc_mask;
  9928. bool active;
  9929. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  9930. DRM_DEBUG_KMS("%s\n", pll->info->name);
  9931. active = pll->info->funcs->get_hw_state(dev_priv, pll, &dpll_hw_state);
  9932. if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
  9933. I915_STATE_WARN(!pll->on && pll->active_mask,
  9934. "pll in active use but not on in sw tracking\n");
  9935. I915_STATE_WARN(pll->on && !pll->active_mask,
  9936. "pll is on but not used by any active crtc\n");
  9937. I915_STATE_WARN(pll->on != active,
  9938. "pll on state mismatch (expected %i, found %i)\n",
  9939. pll->on, active);
  9940. }
  9941. if (!crtc) {
  9942. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  9943. "more active pll users than references: %x vs %x\n",
  9944. pll->active_mask, pll->state.crtc_mask);
  9945. return;
  9946. }
  9947. crtc_mask = 1 << drm_crtc_index(crtc);
  9948. if (new_state->active)
  9949. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  9950. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  9951. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9952. else
  9953. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9954. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  9955. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  9956. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  9957. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  9958. crtc_mask, pll->state.crtc_mask);
  9959. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  9960. &dpll_hw_state,
  9961. sizeof(dpll_hw_state)),
  9962. "pll hw state mismatch\n");
  9963. }
  9964. static void
  9965. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  9966. struct drm_crtc_state *old_crtc_state,
  9967. struct drm_crtc_state *new_crtc_state)
  9968. {
  9969. struct drm_i915_private *dev_priv = to_i915(dev);
  9970. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  9971. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  9972. if (new_state->shared_dpll)
  9973. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  9974. if (old_state->shared_dpll &&
  9975. old_state->shared_dpll != new_state->shared_dpll) {
  9976. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  9977. struct intel_shared_dpll *pll = old_state->shared_dpll;
  9978. I915_STATE_WARN(pll->active_mask & crtc_mask,
  9979. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  9980. pipe_name(drm_crtc_index(crtc)));
  9981. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  9982. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  9983. pipe_name(drm_crtc_index(crtc)));
  9984. }
  9985. }
  9986. static void
  9987. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  9988. struct drm_atomic_state *state,
  9989. struct drm_crtc_state *old_state,
  9990. struct drm_crtc_state *new_state)
  9991. {
  9992. if (!needs_modeset(new_state) &&
  9993. !to_intel_crtc_state(new_state)->update_pipe)
  9994. return;
  9995. verify_wm_state(crtc, new_state);
  9996. verify_connector_state(crtc->dev, state, crtc);
  9997. verify_crtc_state(crtc, old_state, new_state);
  9998. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  9999. }
  10000. static void
  10001. verify_disabled_dpll_state(struct drm_device *dev)
  10002. {
  10003. struct drm_i915_private *dev_priv = to_i915(dev);
  10004. int i;
  10005. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10006. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  10007. }
  10008. static void
  10009. intel_modeset_verify_disabled(struct drm_device *dev,
  10010. struct drm_atomic_state *state)
  10011. {
  10012. verify_encoder_state(dev, state);
  10013. verify_connector_state(dev, state, NULL);
  10014. verify_disabled_dpll_state(dev);
  10015. }
  10016. static void update_scanline_offset(struct intel_crtc *crtc)
  10017. {
  10018. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10019. /*
  10020. * The scanline counter increments at the leading edge of hsync.
  10021. *
  10022. * On most platforms it starts counting from vtotal-1 on the
  10023. * first active line. That means the scanline counter value is
  10024. * always one less than what we would expect. Ie. just after
  10025. * start of vblank, which also occurs at start of hsync (on the
  10026. * last active line), the scanline counter will read vblank_start-1.
  10027. *
  10028. * On gen2 the scanline counter starts counting from 1 instead
  10029. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10030. * to keep the value positive), instead of adding one.
  10031. *
  10032. * On HSW+ the behaviour of the scanline counter depends on the output
  10033. * type. For DP ports it behaves like most other platforms, but on HDMI
  10034. * there's an extra 1 line difference. So we need to add two instead of
  10035. * one to the value.
  10036. *
  10037. * On VLV/CHV DSI the scanline counter would appear to increment
  10038. * approx. 1/3 of a scanline before start of vblank. Unfortunately
  10039. * that means we can't tell whether we're in vblank or not while
  10040. * we're on that particular line. We must still set scanline_offset
  10041. * to 1 so that the vblank timestamps come out correct when we query
  10042. * the scanline counter from within the vblank interrupt handler.
  10043. * However if queried just before the start of vblank we'll get an
  10044. * answer that's slightly in the future.
  10045. */
  10046. if (IS_GEN2(dev_priv)) {
  10047. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10048. int vtotal;
  10049. vtotal = adjusted_mode->crtc_vtotal;
  10050. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10051. vtotal /= 2;
  10052. crtc->scanline_offset = vtotal - 1;
  10053. } else if (HAS_DDI(dev_priv) &&
  10054. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  10055. crtc->scanline_offset = 2;
  10056. } else
  10057. crtc->scanline_offset = 1;
  10058. }
  10059. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10060. {
  10061. struct drm_device *dev = state->dev;
  10062. struct drm_i915_private *dev_priv = to_i915(dev);
  10063. struct drm_crtc *crtc;
  10064. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10065. int i;
  10066. if (!dev_priv->display.crtc_compute_clock)
  10067. return;
  10068. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10069. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10070. struct intel_shared_dpll *old_dpll =
  10071. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  10072. if (!needs_modeset(new_crtc_state))
  10073. continue;
  10074. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  10075. if (!old_dpll)
  10076. continue;
  10077. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  10078. }
  10079. }
  10080. /*
  10081. * This implements the workaround described in the "notes" section of the mode
  10082. * set sequence documentation. When going from no pipes or single pipe to
  10083. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10084. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10085. */
  10086. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10087. {
  10088. struct drm_crtc_state *crtc_state;
  10089. struct intel_crtc *intel_crtc;
  10090. struct drm_crtc *crtc;
  10091. struct intel_crtc_state *first_crtc_state = NULL;
  10092. struct intel_crtc_state *other_crtc_state = NULL;
  10093. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10094. int i;
  10095. /* look at all crtc's that are going to be enabled in during modeset */
  10096. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  10097. intel_crtc = to_intel_crtc(crtc);
  10098. if (!crtc_state->active || !needs_modeset(crtc_state))
  10099. continue;
  10100. if (first_crtc_state) {
  10101. other_crtc_state = to_intel_crtc_state(crtc_state);
  10102. break;
  10103. } else {
  10104. first_crtc_state = to_intel_crtc_state(crtc_state);
  10105. first_pipe = intel_crtc->pipe;
  10106. }
  10107. }
  10108. /* No workaround needed? */
  10109. if (!first_crtc_state)
  10110. return 0;
  10111. /* w/a possibly needed, check how many crtc's are already enabled. */
  10112. for_each_intel_crtc(state->dev, intel_crtc) {
  10113. struct intel_crtc_state *pipe_config;
  10114. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10115. if (IS_ERR(pipe_config))
  10116. return PTR_ERR(pipe_config);
  10117. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10118. if (!pipe_config->base.active ||
  10119. needs_modeset(&pipe_config->base))
  10120. continue;
  10121. /* 2 or more enabled crtcs means no need for w/a */
  10122. if (enabled_pipe != INVALID_PIPE)
  10123. return 0;
  10124. enabled_pipe = intel_crtc->pipe;
  10125. }
  10126. if (enabled_pipe != INVALID_PIPE)
  10127. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10128. else if (other_crtc_state)
  10129. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10130. return 0;
  10131. }
  10132. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  10133. {
  10134. struct drm_crtc *crtc;
  10135. /* Add all pipes to the state */
  10136. for_each_crtc(state->dev, crtc) {
  10137. struct drm_crtc_state *crtc_state;
  10138. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10139. if (IS_ERR(crtc_state))
  10140. return PTR_ERR(crtc_state);
  10141. }
  10142. return 0;
  10143. }
  10144. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10145. {
  10146. struct drm_crtc *crtc;
  10147. /*
  10148. * Add all pipes to the state, and force
  10149. * a modeset on all the active ones.
  10150. */
  10151. for_each_crtc(state->dev, crtc) {
  10152. struct drm_crtc_state *crtc_state;
  10153. int ret;
  10154. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10155. if (IS_ERR(crtc_state))
  10156. return PTR_ERR(crtc_state);
  10157. if (!crtc_state->active || needs_modeset(crtc_state))
  10158. continue;
  10159. crtc_state->mode_changed = true;
  10160. ret = drm_atomic_add_affected_connectors(state, crtc);
  10161. if (ret)
  10162. return ret;
  10163. ret = drm_atomic_add_affected_planes(state, crtc);
  10164. if (ret)
  10165. return ret;
  10166. }
  10167. return 0;
  10168. }
  10169. static int intel_modeset_checks(struct drm_atomic_state *state)
  10170. {
  10171. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10172. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10173. struct drm_crtc *crtc;
  10174. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10175. int ret = 0, i;
  10176. if (!check_digital_port_conflicts(state)) {
  10177. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10178. return -EINVAL;
  10179. }
  10180. intel_state->modeset = true;
  10181. intel_state->active_crtcs = dev_priv->active_crtcs;
  10182. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10183. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  10184. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10185. if (new_crtc_state->active)
  10186. intel_state->active_crtcs |= 1 << i;
  10187. else
  10188. intel_state->active_crtcs &= ~(1 << i);
  10189. if (old_crtc_state->active != new_crtc_state->active)
  10190. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10191. }
  10192. /*
  10193. * See if the config requires any additional preparation, e.g.
  10194. * to adjust global state with pipes off. We need to do this
  10195. * here so we can get the modeset_pipe updated config for the new
  10196. * mode set on this crtc. For other crtcs we need to use the
  10197. * adjusted_mode bits in the crtc directly.
  10198. */
  10199. if (dev_priv->display.modeset_calc_cdclk) {
  10200. ret = dev_priv->display.modeset_calc_cdclk(state);
  10201. if (ret < 0)
  10202. return ret;
  10203. /*
  10204. * Writes to dev_priv->cdclk.logical must protected by
  10205. * holding all the crtc locks, even if we don't end up
  10206. * touching the hardware
  10207. */
  10208. if (intel_cdclk_changed(&dev_priv->cdclk.logical,
  10209. &intel_state->cdclk.logical)) {
  10210. ret = intel_lock_all_pipes(state);
  10211. if (ret < 0)
  10212. return ret;
  10213. }
  10214. /* All pipes must be switched off while we change the cdclk. */
  10215. if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
  10216. &intel_state->cdclk.actual)) {
  10217. ret = intel_modeset_all_pipes(state);
  10218. if (ret < 0)
  10219. return ret;
  10220. }
  10221. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10222. intel_state->cdclk.logical.cdclk,
  10223. intel_state->cdclk.actual.cdclk);
  10224. DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
  10225. intel_state->cdclk.logical.voltage_level,
  10226. intel_state->cdclk.actual.voltage_level);
  10227. } else {
  10228. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10229. }
  10230. intel_modeset_clear_plls(state);
  10231. if (IS_HASWELL(dev_priv))
  10232. return haswell_mode_set_planes_workaround(state);
  10233. return 0;
  10234. }
  10235. /*
  10236. * Handle calculation of various watermark data at the end of the atomic check
  10237. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10238. * handlers to ensure that all derived state has been updated.
  10239. */
  10240. static int calc_watermark_data(struct drm_atomic_state *state)
  10241. {
  10242. struct drm_device *dev = state->dev;
  10243. struct drm_i915_private *dev_priv = to_i915(dev);
  10244. /* Is there platform-specific watermark information to calculate? */
  10245. if (dev_priv->display.compute_global_watermarks)
  10246. return dev_priv->display.compute_global_watermarks(state);
  10247. return 0;
  10248. }
  10249. /**
  10250. * intel_atomic_check - validate state object
  10251. * @dev: drm device
  10252. * @state: state to validate
  10253. */
  10254. static int intel_atomic_check(struct drm_device *dev,
  10255. struct drm_atomic_state *state)
  10256. {
  10257. struct drm_i915_private *dev_priv = to_i915(dev);
  10258. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10259. struct drm_crtc *crtc;
  10260. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10261. int ret, i;
  10262. bool any_ms = false;
  10263. /* Catch I915_MODE_FLAG_INHERITED */
  10264. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
  10265. crtc_state, i) {
  10266. if (crtc_state->mode.private_flags !=
  10267. old_crtc_state->mode.private_flags)
  10268. crtc_state->mode_changed = true;
  10269. }
  10270. ret = drm_atomic_helper_check_modeset(dev, state);
  10271. if (ret)
  10272. return ret;
  10273. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10274. struct intel_crtc_state *pipe_config =
  10275. to_intel_crtc_state(crtc_state);
  10276. if (!needs_modeset(crtc_state))
  10277. continue;
  10278. if (!crtc_state->enable) {
  10279. any_ms = true;
  10280. continue;
  10281. }
  10282. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10283. if (ret) {
  10284. intel_dump_pipe_config(to_intel_crtc(crtc),
  10285. pipe_config, "[failed]");
  10286. return ret;
  10287. }
  10288. if (i915_modparams.fastboot &&
  10289. intel_pipe_config_compare(dev_priv,
  10290. to_intel_crtc_state(old_crtc_state),
  10291. pipe_config, true)) {
  10292. crtc_state->mode_changed = false;
  10293. pipe_config->update_pipe = true;
  10294. }
  10295. if (needs_modeset(crtc_state))
  10296. any_ms = true;
  10297. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10298. needs_modeset(crtc_state) ?
  10299. "[modeset]" : "[fastset]");
  10300. }
  10301. if (any_ms) {
  10302. ret = intel_modeset_checks(state);
  10303. if (ret)
  10304. return ret;
  10305. } else {
  10306. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10307. }
  10308. ret = drm_atomic_helper_check_planes(dev, state);
  10309. if (ret)
  10310. return ret;
  10311. intel_fbc_choose_crtc(dev_priv, intel_state);
  10312. return calc_watermark_data(state);
  10313. }
  10314. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10315. struct drm_atomic_state *state)
  10316. {
  10317. return drm_atomic_helper_prepare_planes(dev, state);
  10318. }
  10319. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10320. {
  10321. struct drm_device *dev = crtc->base.dev;
  10322. if (!dev->max_vblank_count)
  10323. return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
  10324. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10325. }
  10326. static void intel_update_crtc(struct drm_crtc *crtc,
  10327. struct drm_atomic_state *state,
  10328. struct drm_crtc_state *old_crtc_state,
  10329. struct drm_crtc_state *new_crtc_state)
  10330. {
  10331. struct drm_device *dev = crtc->dev;
  10332. struct drm_i915_private *dev_priv = to_i915(dev);
  10333. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10334. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10335. bool modeset = needs_modeset(new_crtc_state);
  10336. struct intel_plane_state *new_plane_state =
  10337. intel_atomic_get_new_plane_state(to_intel_atomic_state(state),
  10338. to_intel_plane(crtc->primary));
  10339. if (modeset) {
  10340. update_scanline_offset(intel_crtc);
  10341. dev_priv->display.crtc_enable(pipe_config, state);
  10342. /* vblanks work again, re-enable pipe CRC. */
  10343. intel_crtc_enable_pipe_crc(intel_crtc);
  10344. } else {
  10345. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10346. pipe_config);
  10347. }
  10348. if (new_plane_state)
  10349. intel_fbc_enable(intel_crtc, pipe_config, new_plane_state);
  10350. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10351. }
  10352. static void intel_update_crtcs(struct drm_atomic_state *state)
  10353. {
  10354. struct drm_crtc *crtc;
  10355. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10356. int i;
  10357. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10358. if (!new_crtc_state->active)
  10359. continue;
  10360. intel_update_crtc(crtc, state, old_crtc_state,
  10361. new_crtc_state);
  10362. }
  10363. }
  10364. static void skl_update_crtcs(struct drm_atomic_state *state)
  10365. {
  10366. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10367. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10368. struct drm_crtc *crtc;
  10369. struct intel_crtc *intel_crtc;
  10370. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10371. struct intel_crtc_state *cstate;
  10372. unsigned int updated = 0;
  10373. bool progress;
  10374. enum pipe pipe;
  10375. int i;
  10376. u8 hw_enabled_slices = dev_priv->wm.skl_hw.ddb.enabled_slices;
  10377. u8 required_slices = intel_state->wm_results.ddb.enabled_slices;
  10378. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10379. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10380. /* ignore allocations for crtc's that have been turned off. */
  10381. if (new_crtc_state->active)
  10382. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10383. /* If 2nd DBuf slice required, enable it here */
  10384. if (INTEL_GEN(dev_priv) >= 11 && required_slices > hw_enabled_slices)
  10385. icl_dbuf_slices_update(dev_priv, required_slices);
  10386. /*
  10387. * Whenever the number of active pipes changes, we need to make sure we
  10388. * update the pipes in the right order so that their ddb allocations
  10389. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10390. * cause pipe underruns and other bad stuff.
  10391. */
  10392. do {
  10393. progress = false;
  10394. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10395. bool vbl_wait = false;
  10396. unsigned int cmask = drm_crtc_mask(crtc);
  10397. intel_crtc = to_intel_crtc(crtc);
  10398. cstate = to_intel_crtc_state(new_crtc_state);
  10399. pipe = intel_crtc->pipe;
  10400. if (updated & cmask || !cstate->base.active)
  10401. continue;
  10402. if (skl_ddb_allocation_overlaps(dev_priv,
  10403. entries,
  10404. &cstate->wm.skl.ddb,
  10405. i))
  10406. continue;
  10407. updated |= cmask;
  10408. entries[i] = &cstate->wm.skl.ddb;
  10409. /*
  10410. * If this is an already active pipe, it's DDB changed,
  10411. * and this isn't the last pipe that needs updating
  10412. * then we need to wait for a vblank to pass for the
  10413. * new ddb allocation to take effect.
  10414. */
  10415. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10416. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10417. !new_crtc_state->active_changed &&
  10418. intel_state->wm_results.dirty_pipes != updated)
  10419. vbl_wait = true;
  10420. intel_update_crtc(crtc, state, old_crtc_state,
  10421. new_crtc_state);
  10422. if (vbl_wait)
  10423. intel_wait_for_vblank(dev_priv, pipe);
  10424. progress = true;
  10425. }
  10426. } while (progress);
  10427. /* If 2nd DBuf slice is no more required disable it */
  10428. if (INTEL_GEN(dev_priv) >= 11 && required_slices < hw_enabled_slices)
  10429. icl_dbuf_slices_update(dev_priv, required_slices);
  10430. }
  10431. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10432. {
  10433. struct intel_atomic_state *state, *next;
  10434. struct llist_node *freed;
  10435. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10436. llist_for_each_entry_safe(state, next, freed, freed)
  10437. drm_atomic_state_put(&state->base);
  10438. }
  10439. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10440. {
  10441. struct drm_i915_private *dev_priv =
  10442. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10443. intel_atomic_helper_free_state(dev_priv);
  10444. }
  10445. static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
  10446. {
  10447. struct wait_queue_entry wait_fence, wait_reset;
  10448. struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
  10449. init_wait_entry(&wait_fence, 0);
  10450. init_wait_entry(&wait_reset, 0);
  10451. for (;;) {
  10452. prepare_to_wait(&intel_state->commit_ready.wait,
  10453. &wait_fence, TASK_UNINTERRUPTIBLE);
  10454. prepare_to_wait(&dev_priv->gpu_error.wait_queue,
  10455. &wait_reset, TASK_UNINTERRUPTIBLE);
  10456. if (i915_sw_fence_done(&intel_state->commit_ready)
  10457. || test_bit(I915_RESET_MODESET, &dev_priv->gpu_error.flags))
  10458. break;
  10459. schedule();
  10460. }
  10461. finish_wait(&intel_state->commit_ready.wait, &wait_fence);
  10462. finish_wait(&dev_priv->gpu_error.wait_queue, &wait_reset);
  10463. }
  10464. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10465. {
  10466. struct drm_device *dev = state->dev;
  10467. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10468. struct drm_i915_private *dev_priv = to_i915(dev);
  10469. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10470. struct drm_crtc *crtc;
  10471. struct intel_crtc_state *intel_cstate;
  10472. u64 put_domains[I915_MAX_PIPES] = {};
  10473. int i;
  10474. intel_atomic_commit_fence_wait(intel_state);
  10475. drm_atomic_helper_wait_for_dependencies(state);
  10476. if (intel_state->modeset)
  10477. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10478. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10479. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10480. if (needs_modeset(new_crtc_state) ||
  10481. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10482. put_domains[to_intel_crtc(crtc)->pipe] =
  10483. modeset_get_crtc_power_domains(crtc,
  10484. to_intel_crtc_state(new_crtc_state));
  10485. }
  10486. if (!needs_modeset(new_crtc_state))
  10487. continue;
  10488. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10489. to_intel_crtc_state(new_crtc_state));
  10490. if (old_crtc_state->active) {
  10491. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10492. /*
  10493. * We need to disable pipe CRC before disabling the pipe,
  10494. * or we race against vblank off.
  10495. */
  10496. intel_crtc_disable_pipe_crc(intel_crtc);
  10497. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10498. intel_crtc->active = false;
  10499. intel_fbc_disable(intel_crtc);
  10500. intel_disable_shared_dpll(intel_crtc);
  10501. /*
  10502. * Underruns don't always raise
  10503. * interrupts, so check manually.
  10504. */
  10505. intel_check_cpu_fifo_underruns(dev_priv);
  10506. intel_check_pch_fifo_underruns(dev_priv);
  10507. if (!new_crtc_state->active) {
  10508. /*
  10509. * Make sure we don't call initial_watermarks
  10510. * for ILK-style watermark updates.
  10511. *
  10512. * No clue what this is supposed to achieve.
  10513. */
  10514. if (INTEL_GEN(dev_priv) >= 9)
  10515. dev_priv->display.initial_watermarks(intel_state,
  10516. to_intel_crtc_state(new_crtc_state));
  10517. }
  10518. }
  10519. }
  10520. /* FIXME: Eventually get rid of our intel_crtc->config pointer */
  10521. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i)
  10522. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  10523. if (intel_state->modeset) {
  10524. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10525. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10526. /*
  10527. * SKL workaround: bspec recommends we disable the SAGV when we
  10528. * have more then one pipe enabled
  10529. */
  10530. if (!intel_can_enable_sagv(state))
  10531. intel_disable_sagv(dev_priv);
  10532. intel_modeset_verify_disabled(dev, state);
  10533. }
  10534. /* Complete the events for pipes that have now been disabled */
  10535. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10536. bool modeset = needs_modeset(new_crtc_state);
  10537. /* Complete events for now disable pipes here. */
  10538. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10539. spin_lock_irq(&dev->event_lock);
  10540. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10541. spin_unlock_irq(&dev->event_lock);
  10542. new_crtc_state->event = NULL;
  10543. }
  10544. }
  10545. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10546. dev_priv->display.update_crtcs(state);
  10547. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10548. * already, but still need the state for the delayed optimization. To
  10549. * fix this:
  10550. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10551. * - schedule that vblank worker _before_ calling hw_done
  10552. * - at the start of commit_tail, cancel it _synchrously
  10553. * - switch over to the vblank wait helper in the core after that since
  10554. * we don't need out special handling any more.
  10555. */
  10556. drm_atomic_helper_wait_for_flip_done(dev, state);
  10557. /*
  10558. * Now that the vblank has passed, we can go ahead and program the
  10559. * optimal watermarks on platforms that need two-step watermark
  10560. * programming.
  10561. *
  10562. * TODO: Move this (and other cleanup) to an async worker eventually.
  10563. */
  10564. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10565. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10566. if (dev_priv->display.optimize_watermarks)
  10567. dev_priv->display.optimize_watermarks(intel_state,
  10568. intel_cstate);
  10569. }
  10570. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10571. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10572. if (put_domains[i])
  10573. modeset_put_power_domains(dev_priv, put_domains[i]);
  10574. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10575. }
  10576. if (intel_state->modeset)
  10577. intel_verify_planes(intel_state);
  10578. if (intel_state->modeset && intel_can_enable_sagv(state))
  10579. intel_enable_sagv(dev_priv);
  10580. drm_atomic_helper_commit_hw_done(state);
  10581. if (intel_state->modeset) {
  10582. /* As one of the primary mmio accessors, KMS has a high
  10583. * likelihood of triggering bugs in unclaimed access. After we
  10584. * finish modesetting, see if an error has been flagged, and if
  10585. * so enable debugging for the next modeset - and hope we catch
  10586. * the culprit.
  10587. */
  10588. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10589. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10590. }
  10591. drm_atomic_helper_cleanup_planes(dev, state);
  10592. drm_atomic_helper_commit_cleanup_done(state);
  10593. drm_atomic_state_put(state);
  10594. intel_atomic_helper_free_state(dev_priv);
  10595. }
  10596. static void intel_atomic_commit_work(struct work_struct *work)
  10597. {
  10598. struct drm_atomic_state *state =
  10599. container_of(work, struct drm_atomic_state, commit_work);
  10600. intel_atomic_commit_tail(state);
  10601. }
  10602. static int __i915_sw_fence_call
  10603. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10604. enum i915_sw_fence_notify notify)
  10605. {
  10606. struct intel_atomic_state *state =
  10607. container_of(fence, struct intel_atomic_state, commit_ready);
  10608. switch (notify) {
  10609. case FENCE_COMPLETE:
  10610. /* we do blocking waits in the worker, nothing to do here */
  10611. break;
  10612. case FENCE_FREE:
  10613. {
  10614. struct intel_atomic_helper *helper =
  10615. &to_i915(state->base.dev)->atomic_helper;
  10616. if (llist_add(&state->freed, &helper->free_list))
  10617. schedule_work(&helper->free_work);
  10618. break;
  10619. }
  10620. }
  10621. return NOTIFY_DONE;
  10622. }
  10623. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10624. {
  10625. struct drm_plane_state *old_plane_state, *new_plane_state;
  10626. struct drm_plane *plane;
  10627. int i;
  10628. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10629. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10630. intel_fb_obj(new_plane_state->fb),
  10631. to_intel_plane(plane)->frontbuffer_bit);
  10632. }
  10633. /**
  10634. * intel_atomic_commit - commit validated state object
  10635. * @dev: DRM device
  10636. * @state: the top-level driver state object
  10637. * @nonblock: nonblocking commit
  10638. *
  10639. * This function commits a top-level state object that has been validated
  10640. * with drm_atomic_helper_check().
  10641. *
  10642. * RETURNS
  10643. * Zero for success or -errno.
  10644. */
  10645. static int intel_atomic_commit(struct drm_device *dev,
  10646. struct drm_atomic_state *state,
  10647. bool nonblock)
  10648. {
  10649. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10650. struct drm_i915_private *dev_priv = to_i915(dev);
  10651. int ret = 0;
  10652. drm_atomic_state_get(state);
  10653. i915_sw_fence_init(&intel_state->commit_ready,
  10654. intel_atomic_commit_ready);
  10655. /*
  10656. * The intel_legacy_cursor_update() fast path takes care
  10657. * of avoiding the vblank waits for simple cursor
  10658. * movement and flips. For cursor on/off and size changes,
  10659. * we want to perform the vblank waits so that watermark
  10660. * updates happen during the correct frames. Gen9+ have
  10661. * double buffered watermarks and so shouldn't need this.
  10662. *
  10663. * Unset state->legacy_cursor_update before the call to
  10664. * drm_atomic_helper_setup_commit() because otherwise
  10665. * drm_atomic_helper_wait_for_flip_done() is a noop and
  10666. * we get FIFO underruns because we didn't wait
  10667. * for vblank.
  10668. *
  10669. * FIXME doing watermarks and fb cleanup from a vblank worker
  10670. * (assuming we had any) would solve these problems.
  10671. */
  10672. if (INTEL_GEN(dev_priv) < 9 && state->legacy_cursor_update) {
  10673. struct intel_crtc_state *new_crtc_state;
  10674. struct intel_crtc *crtc;
  10675. int i;
  10676. for_each_new_intel_crtc_in_state(intel_state, crtc, new_crtc_state, i)
  10677. if (new_crtc_state->wm.need_postvbl_update ||
  10678. new_crtc_state->update_wm_post)
  10679. state->legacy_cursor_update = false;
  10680. }
  10681. ret = intel_atomic_prepare_commit(dev, state);
  10682. if (ret) {
  10683. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10684. i915_sw_fence_commit(&intel_state->commit_ready);
  10685. return ret;
  10686. }
  10687. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10688. if (!ret)
  10689. ret = drm_atomic_helper_swap_state(state, true);
  10690. if (ret) {
  10691. i915_sw_fence_commit(&intel_state->commit_ready);
  10692. drm_atomic_helper_cleanup_planes(dev, state);
  10693. return ret;
  10694. }
  10695. dev_priv->wm.distrust_bios_wm = false;
  10696. intel_shared_dpll_swap_state(state);
  10697. intel_atomic_track_fbs(state);
  10698. if (intel_state->modeset) {
  10699. memcpy(dev_priv->min_cdclk, intel_state->min_cdclk,
  10700. sizeof(intel_state->min_cdclk));
  10701. memcpy(dev_priv->min_voltage_level,
  10702. intel_state->min_voltage_level,
  10703. sizeof(intel_state->min_voltage_level));
  10704. dev_priv->active_crtcs = intel_state->active_crtcs;
  10705. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10706. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10707. }
  10708. drm_atomic_state_get(state);
  10709. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  10710. i915_sw_fence_commit(&intel_state->commit_ready);
  10711. if (nonblock && intel_state->modeset) {
  10712. queue_work(dev_priv->modeset_wq, &state->commit_work);
  10713. } else if (nonblock) {
  10714. queue_work(system_unbound_wq, &state->commit_work);
  10715. } else {
  10716. if (intel_state->modeset)
  10717. flush_workqueue(dev_priv->modeset_wq);
  10718. intel_atomic_commit_tail(state);
  10719. }
  10720. return 0;
  10721. }
  10722. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10723. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10724. .set_config = drm_atomic_helper_set_config,
  10725. .destroy = intel_crtc_destroy,
  10726. .page_flip = drm_atomic_helper_page_flip,
  10727. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10728. .atomic_destroy_state = intel_crtc_destroy_state,
  10729. .set_crc_source = intel_crtc_set_crc_source,
  10730. };
  10731. struct wait_rps_boost {
  10732. struct wait_queue_entry wait;
  10733. struct drm_crtc *crtc;
  10734. struct i915_request *request;
  10735. };
  10736. static int do_rps_boost(struct wait_queue_entry *_wait,
  10737. unsigned mode, int sync, void *key)
  10738. {
  10739. struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
  10740. struct i915_request *rq = wait->request;
  10741. /*
  10742. * If we missed the vblank, but the request is already running it
  10743. * is reasonable to assume that it will complete before the next
  10744. * vblank without our intervention, so leave RPS alone.
  10745. */
  10746. if (!i915_request_started(rq))
  10747. gen6_rps_boost(rq, NULL);
  10748. i915_request_put(rq);
  10749. drm_crtc_vblank_put(wait->crtc);
  10750. list_del(&wait->wait.entry);
  10751. kfree(wait);
  10752. return 1;
  10753. }
  10754. static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
  10755. struct dma_fence *fence)
  10756. {
  10757. struct wait_rps_boost *wait;
  10758. if (!dma_fence_is_i915(fence))
  10759. return;
  10760. if (INTEL_GEN(to_i915(crtc->dev)) < 6)
  10761. return;
  10762. if (drm_crtc_vblank_get(crtc))
  10763. return;
  10764. wait = kmalloc(sizeof(*wait), GFP_KERNEL);
  10765. if (!wait) {
  10766. drm_crtc_vblank_put(crtc);
  10767. return;
  10768. }
  10769. wait->request = to_request(dma_fence_get(fence));
  10770. wait->crtc = crtc;
  10771. wait->wait.func = do_rps_boost;
  10772. wait->wait.flags = 0;
  10773. add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
  10774. }
  10775. static int intel_plane_pin_fb(struct intel_plane_state *plane_state)
  10776. {
  10777. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  10778. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10779. struct drm_framebuffer *fb = plane_state->base.fb;
  10780. struct i915_vma *vma;
  10781. if (plane->id == PLANE_CURSOR &&
  10782. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10783. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10784. const int align = intel_cursor_alignment(dev_priv);
  10785. return i915_gem_object_attach_phys(obj, align);
  10786. }
  10787. vma = intel_pin_and_fence_fb_obj(fb,
  10788. plane_state->base.rotation,
  10789. intel_plane_uses_fence(plane_state),
  10790. &plane_state->flags);
  10791. if (IS_ERR(vma))
  10792. return PTR_ERR(vma);
  10793. plane_state->vma = vma;
  10794. return 0;
  10795. }
  10796. static void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
  10797. {
  10798. struct i915_vma *vma;
  10799. vma = fetch_and_zero(&old_plane_state->vma);
  10800. if (vma)
  10801. intel_unpin_fb_vma(vma, old_plane_state->flags);
  10802. }
  10803. static void fb_obj_bump_render_priority(struct drm_i915_gem_object *obj)
  10804. {
  10805. struct i915_sched_attr attr = {
  10806. .priority = I915_PRIORITY_DISPLAY,
  10807. };
  10808. i915_gem_object_wait_priority(obj, 0, &attr);
  10809. }
  10810. /**
  10811. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10812. * @plane: drm plane to prepare for
  10813. * @new_state: the plane state being prepared
  10814. *
  10815. * Prepares a framebuffer for usage on a display plane. Generally this
  10816. * involves pinning the underlying object and updating the frontbuffer tracking
  10817. * bits. Some older platforms need special physical address handling for
  10818. * cursor planes.
  10819. *
  10820. * Must be called with struct_mutex held.
  10821. *
  10822. * Returns 0 on success, negative error code on failure.
  10823. */
  10824. int
  10825. intel_prepare_plane_fb(struct drm_plane *plane,
  10826. struct drm_plane_state *new_state)
  10827. {
  10828. struct intel_atomic_state *intel_state =
  10829. to_intel_atomic_state(new_state->state);
  10830. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10831. struct drm_framebuffer *fb = new_state->fb;
  10832. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10833. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10834. int ret;
  10835. if (old_obj) {
  10836. struct drm_crtc_state *crtc_state =
  10837. drm_atomic_get_new_crtc_state(new_state->state,
  10838. plane->state->crtc);
  10839. /* Big Hammer, we also need to ensure that any pending
  10840. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  10841. * current scanout is retired before unpinning the old
  10842. * framebuffer. Note that we rely on userspace rendering
  10843. * into the buffer attached to the pipe they are waiting
  10844. * on. If not, userspace generates a GPU hang with IPEHR
  10845. * point to the MI_WAIT_FOR_EVENT.
  10846. *
  10847. * This should only fail upon a hung GPU, in which case we
  10848. * can safely continue.
  10849. */
  10850. if (needs_modeset(crtc_state)) {
  10851. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10852. old_obj->resv, NULL,
  10853. false, 0,
  10854. GFP_KERNEL);
  10855. if (ret < 0)
  10856. return ret;
  10857. }
  10858. }
  10859. if (new_state->fence) { /* explicit fencing */
  10860. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  10861. new_state->fence,
  10862. I915_FENCE_TIMEOUT,
  10863. GFP_KERNEL);
  10864. if (ret < 0)
  10865. return ret;
  10866. }
  10867. if (!obj)
  10868. return 0;
  10869. ret = i915_gem_object_pin_pages(obj);
  10870. if (ret)
  10871. return ret;
  10872. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  10873. if (ret) {
  10874. i915_gem_object_unpin_pages(obj);
  10875. return ret;
  10876. }
  10877. ret = intel_plane_pin_fb(to_intel_plane_state(new_state));
  10878. fb_obj_bump_render_priority(obj);
  10879. mutex_unlock(&dev_priv->drm.struct_mutex);
  10880. i915_gem_object_unpin_pages(obj);
  10881. if (ret)
  10882. return ret;
  10883. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  10884. if (!new_state->fence) { /* implicit fencing */
  10885. struct dma_fence *fence;
  10886. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10887. obj->resv, NULL,
  10888. false, I915_FENCE_TIMEOUT,
  10889. GFP_KERNEL);
  10890. if (ret < 0)
  10891. return ret;
  10892. fence = reservation_object_get_excl_rcu(obj->resv);
  10893. if (fence) {
  10894. add_rps_boost_after_vblank(new_state->crtc, fence);
  10895. dma_fence_put(fence);
  10896. }
  10897. } else {
  10898. add_rps_boost_after_vblank(new_state->crtc, new_state->fence);
  10899. }
  10900. return 0;
  10901. }
  10902. /**
  10903. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  10904. * @plane: drm plane to clean up for
  10905. * @old_state: the state from the previous modeset
  10906. *
  10907. * Cleans up a framebuffer that has just been removed from a plane.
  10908. *
  10909. * Must be called with struct_mutex held.
  10910. */
  10911. void
  10912. intel_cleanup_plane_fb(struct drm_plane *plane,
  10913. struct drm_plane_state *old_state)
  10914. {
  10915. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10916. /* Should only be called after a successful intel_prepare_plane_fb()! */
  10917. mutex_lock(&dev_priv->drm.struct_mutex);
  10918. intel_plane_unpin_fb(to_intel_plane_state(old_state));
  10919. mutex_unlock(&dev_priv->drm.struct_mutex);
  10920. }
  10921. int
  10922. skl_max_scale(struct intel_crtc *intel_crtc,
  10923. struct intel_crtc_state *crtc_state,
  10924. uint32_t pixel_format)
  10925. {
  10926. struct drm_i915_private *dev_priv;
  10927. int max_scale, mult;
  10928. int crtc_clock, max_dotclk, tmpclk1, tmpclk2;
  10929. if (!intel_crtc || !crtc_state->base.enable)
  10930. return DRM_PLANE_HELPER_NO_SCALING;
  10931. dev_priv = to_i915(intel_crtc->base.dev);
  10932. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  10933. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  10934. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10)
  10935. max_dotclk *= 2;
  10936. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  10937. return DRM_PLANE_HELPER_NO_SCALING;
  10938. /*
  10939. * skl max scale is lower of:
  10940. * close to 3 but not 3, -1 is for that purpose
  10941. * or
  10942. * cdclk/crtc_clock
  10943. */
  10944. mult = pixel_format == DRM_FORMAT_NV12 ? 2 : 3;
  10945. tmpclk1 = (1 << 16) * mult - 1;
  10946. tmpclk2 = (1 << 8) * ((max_dotclk << 8) / crtc_clock);
  10947. max_scale = min(tmpclk1, tmpclk2);
  10948. return max_scale;
  10949. }
  10950. static int
  10951. intel_check_primary_plane(struct intel_plane *plane,
  10952. struct intel_crtc_state *crtc_state,
  10953. struct intel_plane_state *state)
  10954. {
  10955. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  10956. struct drm_crtc *crtc = state->base.crtc;
  10957. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  10958. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  10959. bool can_position = false;
  10960. int ret;
  10961. uint32_t pixel_format = 0;
  10962. if (INTEL_GEN(dev_priv) >= 9) {
  10963. /* use scaler when colorkey is not required */
  10964. if (!state->ckey.flags) {
  10965. min_scale = 1;
  10966. if (state->base.fb)
  10967. pixel_format = state->base.fb->format->format;
  10968. max_scale = skl_max_scale(to_intel_crtc(crtc),
  10969. crtc_state, pixel_format);
  10970. }
  10971. can_position = true;
  10972. }
  10973. ret = drm_atomic_helper_check_plane_state(&state->base,
  10974. &crtc_state->base,
  10975. min_scale, max_scale,
  10976. can_position, true);
  10977. if (ret)
  10978. return ret;
  10979. if (!state->base.fb)
  10980. return 0;
  10981. if (INTEL_GEN(dev_priv) >= 9) {
  10982. ret = skl_check_plane_surface(crtc_state, state);
  10983. if (ret)
  10984. return ret;
  10985. state->ctl = skl_plane_ctl(crtc_state, state);
  10986. } else {
  10987. ret = i9xx_check_plane_surface(state);
  10988. if (ret)
  10989. return ret;
  10990. state->ctl = i9xx_plane_ctl(crtc_state, state);
  10991. }
  10992. if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
  10993. state->color_ctl = glk_plane_color_ctl(crtc_state, state);
  10994. return 0;
  10995. }
  10996. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  10997. struct drm_crtc_state *old_crtc_state)
  10998. {
  10999. struct drm_device *dev = crtc->dev;
  11000. struct drm_i915_private *dev_priv = to_i915(dev);
  11001. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11002. struct intel_crtc_state *old_intel_cstate =
  11003. to_intel_crtc_state(old_crtc_state);
  11004. struct intel_atomic_state *old_intel_state =
  11005. to_intel_atomic_state(old_crtc_state->state);
  11006. struct intel_crtc_state *intel_cstate =
  11007. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  11008. bool modeset = needs_modeset(&intel_cstate->base);
  11009. if (!modeset &&
  11010. (intel_cstate->base.color_mgmt_changed ||
  11011. intel_cstate->update_pipe)) {
  11012. intel_color_set_csc(&intel_cstate->base);
  11013. intel_color_load_luts(&intel_cstate->base);
  11014. }
  11015. /* Perform vblank evasion around commit operation */
  11016. intel_pipe_update_start(intel_cstate);
  11017. if (modeset)
  11018. goto out;
  11019. if (intel_cstate->update_pipe)
  11020. intel_update_pipe_config(old_intel_cstate, intel_cstate);
  11021. else if (INTEL_GEN(dev_priv) >= 9)
  11022. skl_detach_scalers(intel_crtc);
  11023. out:
  11024. if (dev_priv->display.atomic_update_watermarks)
  11025. dev_priv->display.atomic_update_watermarks(old_intel_state,
  11026. intel_cstate);
  11027. }
  11028. void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
  11029. struct intel_crtc_state *crtc_state)
  11030. {
  11031. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11032. if (!IS_GEN2(dev_priv))
  11033. intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
  11034. if (crtc_state->has_pch_encoder) {
  11035. enum pipe pch_transcoder =
  11036. intel_crtc_pch_transcoder(crtc);
  11037. intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
  11038. }
  11039. }
  11040. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11041. struct drm_crtc_state *old_crtc_state)
  11042. {
  11043. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11044. struct intel_atomic_state *old_intel_state =
  11045. to_intel_atomic_state(old_crtc_state->state);
  11046. struct intel_crtc_state *new_crtc_state =
  11047. intel_atomic_get_new_crtc_state(old_intel_state, intel_crtc);
  11048. intel_pipe_update_end(new_crtc_state);
  11049. if (new_crtc_state->update_pipe &&
  11050. !needs_modeset(&new_crtc_state->base) &&
  11051. old_crtc_state->mode.private_flags & I915_MODE_FLAG_INHERITED)
  11052. intel_crtc_arm_fifo_underrun(intel_crtc, new_crtc_state);
  11053. }
  11054. /**
  11055. * intel_plane_destroy - destroy a plane
  11056. * @plane: plane to destroy
  11057. *
  11058. * Common destruction function for all types of planes (primary, cursor,
  11059. * sprite).
  11060. */
  11061. void intel_plane_destroy(struct drm_plane *plane)
  11062. {
  11063. drm_plane_cleanup(plane);
  11064. kfree(to_intel_plane(plane));
  11065. }
  11066. static bool i8xx_plane_format_mod_supported(struct drm_plane *_plane,
  11067. u32 format, u64 modifier)
  11068. {
  11069. switch (modifier) {
  11070. case DRM_FORMAT_MOD_LINEAR:
  11071. case I915_FORMAT_MOD_X_TILED:
  11072. break;
  11073. default:
  11074. return false;
  11075. }
  11076. switch (format) {
  11077. case DRM_FORMAT_C8:
  11078. case DRM_FORMAT_RGB565:
  11079. case DRM_FORMAT_XRGB1555:
  11080. case DRM_FORMAT_XRGB8888:
  11081. return modifier == DRM_FORMAT_MOD_LINEAR ||
  11082. modifier == I915_FORMAT_MOD_X_TILED;
  11083. default:
  11084. return false;
  11085. }
  11086. }
  11087. static bool i965_plane_format_mod_supported(struct drm_plane *_plane,
  11088. u32 format, u64 modifier)
  11089. {
  11090. switch (modifier) {
  11091. case DRM_FORMAT_MOD_LINEAR:
  11092. case I915_FORMAT_MOD_X_TILED:
  11093. break;
  11094. default:
  11095. return false;
  11096. }
  11097. switch (format) {
  11098. case DRM_FORMAT_C8:
  11099. case DRM_FORMAT_RGB565:
  11100. case DRM_FORMAT_XRGB8888:
  11101. case DRM_FORMAT_XBGR8888:
  11102. case DRM_FORMAT_XRGB2101010:
  11103. case DRM_FORMAT_XBGR2101010:
  11104. return modifier == DRM_FORMAT_MOD_LINEAR ||
  11105. modifier == I915_FORMAT_MOD_X_TILED;
  11106. default:
  11107. return false;
  11108. }
  11109. }
  11110. static bool skl_plane_format_mod_supported(struct drm_plane *_plane,
  11111. u32 format, u64 modifier)
  11112. {
  11113. struct intel_plane *plane = to_intel_plane(_plane);
  11114. switch (modifier) {
  11115. case DRM_FORMAT_MOD_LINEAR:
  11116. case I915_FORMAT_MOD_X_TILED:
  11117. case I915_FORMAT_MOD_Y_TILED:
  11118. case I915_FORMAT_MOD_Yf_TILED:
  11119. break;
  11120. case I915_FORMAT_MOD_Y_TILED_CCS:
  11121. case I915_FORMAT_MOD_Yf_TILED_CCS:
  11122. if (!plane->has_ccs)
  11123. return false;
  11124. break;
  11125. default:
  11126. return false;
  11127. }
  11128. switch (format) {
  11129. case DRM_FORMAT_XRGB8888:
  11130. case DRM_FORMAT_XBGR8888:
  11131. case DRM_FORMAT_ARGB8888:
  11132. case DRM_FORMAT_ABGR8888:
  11133. if (modifier == I915_FORMAT_MOD_Yf_TILED_CCS ||
  11134. modifier == I915_FORMAT_MOD_Y_TILED_CCS)
  11135. return true;
  11136. /* fall through */
  11137. case DRM_FORMAT_RGB565:
  11138. case DRM_FORMAT_XRGB2101010:
  11139. case DRM_FORMAT_XBGR2101010:
  11140. case DRM_FORMAT_YUYV:
  11141. case DRM_FORMAT_YVYU:
  11142. case DRM_FORMAT_UYVY:
  11143. case DRM_FORMAT_VYUY:
  11144. case DRM_FORMAT_NV12:
  11145. if (modifier == I915_FORMAT_MOD_Yf_TILED)
  11146. return true;
  11147. /* fall through */
  11148. case DRM_FORMAT_C8:
  11149. if (modifier == DRM_FORMAT_MOD_LINEAR ||
  11150. modifier == I915_FORMAT_MOD_X_TILED ||
  11151. modifier == I915_FORMAT_MOD_Y_TILED)
  11152. return true;
  11153. /* fall through */
  11154. default:
  11155. return false;
  11156. }
  11157. }
  11158. static bool intel_cursor_format_mod_supported(struct drm_plane *_plane,
  11159. u32 format, u64 modifier)
  11160. {
  11161. return modifier == DRM_FORMAT_MOD_LINEAR &&
  11162. format == DRM_FORMAT_ARGB8888;
  11163. }
  11164. static struct drm_plane_funcs skl_plane_funcs = {
  11165. .update_plane = drm_atomic_helper_update_plane,
  11166. .disable_plane = drm_atomic_helper_disable_plane,
  11167. .destroy = intel_plane_destroy,
  11168. .atomic_get_property = intel_plane_atomic_get_property,
  11169. .atomic_set_property = intel_plane_atomic_set_property,
  11170. .atomic_duplicate_state = intel_plane_duplicate_state,
  11171. .atomic_destroy_state = intel_plane_destroy_state,
  11172. .format_mod_supported = skl_plane_format_mod_supported,
  11173. };
  11174. static struct drm_plane_funcs i965_plane_funcs = {
  11175. .update_plane = drm_atomic_helper_update_plane,
  11176. .disable_plane = drm_atomic_helper_disable_plane,
  11177. .destroy = intel_plane_destroy,
  11178. .atomic_get_property = intel_plane_atomic_get_property,
  11179. .atomic_set_property = intel_plane_atomic_set_property,
  11180. .atomic_duplicate_state = intel_plane_duplicate_state,
  11181. .atomic_destroy_state = intel_plane_destroy_state,
  11182. .format_mod_supported = i965_plane_format_mod_supported,
  11183. };
  11184. static struct drm_plane_funcs i8xx_plane_funcs = {
  11185. .update_plane = drm_atomic_helper_update_plane,
  11186. .disable_plane = drm_atomic_helper_disable_plane,
  11187. .destroy = intel_plane_destroy,
  11188. .atomic_get_property = intel_plane_atomic_get_property,
  11189. .atomic_set_property = intel_plane_atomic_set_property,
  11190. .atomic_duplicate_state = intel_plane_duplicate_state,
  11191. .atomic_destroy_state = intel_plane_destroy_state,
  11192. .format_mod_supported = i8xx_plane_format_mod_supported,
  11193. };
  11194. static int
  11195. intel_legacy_cursor_update(struct drm_plane *plane,
  11196. struct drm_crtc *crtc,
  11197. struct drm_framebuffer *fb,
  11198. int crtc_x, int crtc_y,
  11199. unsigned int crtc_w, unsigned int crtc_h,
  11200. uint32_t src_x, uint32_t src_y,
  11201. uint32_t src_w, uint32_t src_h,
  11202. struct drm_modeset_acquire_ctx *ctx)
  11203. {
  11204. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11205. int ret;
  11206. struct drm_plane_state *old_plane_state, *new_plane_state;
  11207. struct intel_plane *intel_plane = to_intel_plane(plane);
  11208. struct drm_framebuffer *old_fb;
  11209. struct drm_crtc_state *crtc_state = crtc->state;
  11210. /*
  11211. * When crtc is inactive or there is a modeset pending,
  11212. * wait for it to complete in the slowpath
  11213. */
  11214. if (!crtc_state->active || needs_modeset(crtc_state) ||
  11215. to_intel_crtc_state(crtc_state)->update_pipe)
  11216. goto slow;
  11217. old_plane_state = plane->state;
  11218. /*
  11219. * Don't do an async update if there is an outstanding commit modifying
  11220. * the plane. This prevents our async update's changes from getting
  11221. * overridden by a previous synchronous update's state.
  11222. */
  11223. if (old_plane_state->commit &&
  11224. !try_wait_for_completion(&old_plane_state->commit->hw_done))
  11225. goto slow;
  11226. /*
  11227. * If any parameters change that may affect watermarks,
  11228. * take the slowpath. Only changing fb or position should be
  11229. * in the fastpath.
  11230. */
  11231. if (old_plane_state->crtc != crtc ||
  11232. old_plane_state->src_w != src_w ||
  11233. old_plane_state->src_h != src_h ||
  11234. old_plane_state->crtc_w != crtc_w ||
  11235. old_plane_state->crtc_h != crtc_h ||
  11236. !old_plane_state->fb != !fb)
  11237. goto slow;
  11238. new_plane_state = intel_plane_duplicate_state(plane);
  11239. if (!new_plane_state)
  11240. return -ENOMEM;
  11241. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  11242. new_plane_state->src_x = src_x;
  11243. new_plane_state->src_y = src_y;
  11244. new_plane_state->src_w = src_w;
  11245. new_plane_state->src_h = src_h;
  11246. new_plane_state->crtc_x = crtc_x;
  11247. new_plane_state->crtc_y = crtc_y;
  11248. new_plane_state->crtc_w = crtc_w;
  11249. new_plane_state->crtc_h = crtc_h;
  11250. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  11251. to_intel_crtc_state(crtc->state), /* FIXME need a new crtc state? */
  11252. to_intel_plane_state(plane->state),
  11253. to_intel_plane_state(new_plane_state));
  11254. if (ret)
  11255. goto out_free;
  11256. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  11257. if (ret)
  11258. goto out_free;
  11259. ret = intel_plane_pin_fb(to_intel_plane_state(new_plane_state));
  11260. if (ret)
  11261. goto out_unlock;
  11262. intel_fb_obj_flush(intel_fb_obj(fb), ORIGIN_FLIP);
  11263. old_fb = old_plane_state->fb;
  11264. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  11265. intel_plane->frontbuffer_bit);
  11266. /* Swap plane state */
  11267. plane->state = new_plane_state;
  11268. if (plane->state->visible) {
  11269. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  11270. intel_plane->update_plane(intel_plane,
  11271. to_intel_crtc_state(crtc->state),
  11272. to_intel_plane_state(plane->state));
  11273. } else {
  11274. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  11275. intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
  11276. }
  11277. intel_plane_unpin_fb(to_intel_plane_state(old_plane_state));
  11278. out_unlock:
  11279. mutex_unlock(&dev_priv->drm.struct_mutex);
  11280. out_free:
  11281. if (ret)
  11282. intel_plane_destroy_state(plane, new_plane_state);
  11283. else
  11284. intel_plane_destroy_state(plane, old_plane_state);
  11285. return ret;
  11286. slow:
  11287. return drm_atomic_helper_update_plane(plane, crtc, fb,
  11288. crtc_x, crtc_y, crtc_w, crtc_h,
  11289. src_x, src_y, src_w, src_h, ctx);
  11290. }
  11291. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  11292. .update_plane = intel_legacy_cursor_update,
  11293. .disable_plane = drm_atomic_helper_disable_plane,
  11294. .destroy = intel_plane_destroy,
  11295. .atomic_get_property = intel_plane_atomic_get_property,
  11296. .atomic_set_property = intel_plane_atomic_set_property,
  11297. .atomic_duplicate_state = intel_plane_duplicate_state,
  11298. .atomic_destroy_state = intel_plane_destroy_state,
  11299. .format_mod_supported = intel_cursor_format_mod_supported,
  11300. };
  11301. static bool i9xx_plane_has_fbc(struct drm_i915_private *dev_priv,
  11302. enum i9xx_plane_id i9xx_plane)
  11303. {
  11304. if (!HAS_FBC(dev_priv))
  11305. return false;
  11306. if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
  11307. return i9xx_plane == PLANE_A; /* tied to pipe A */
  11308. else if (IS_IVYBRIDGE(dev_priv))
  11309. return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B ||
  11310. i9xx_plane == PLANE_C;
  11311. else if (INTEL_GEN(dev_priv) >= 4)
  11312. return i9xx_plane == PLANE_A || i9xx_plane == PLANE_B;
  11313. else
  11314. return i9xx_plane == PLANE_A;
  11315. }
  11316. static bool skl_plane_has_fbc(struct drm_i915_private *dev_priv,
  11317. enum pipe pipe, enum plane_id plane_id)
  11318. {
  11319. if (!HAS_FBC(dev_priv))
  11320. return false;
  11321. return pipe == PIPE_A && plane_id == PLANE_PRIMARY;
  11322. }
  11323. bool skl_plane_has_planar(struct drm_i915_private *dev_priv,
  11324. enum pipe pipe, enum plane_id plane_id)
  11325. {
  11326. if (plane_id == PLANE_PRIMARY) {
  11327. if (IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  11328. return false;
  11329. else if ((INTEL_GEN(dev_priv) == 9 && pipe == PIPE_C) &&
  11330. !IS_GEMINILAKE(dev_priv))
  11331. return false;
  11332. } else if (plane_id >= PLANE_SPRITE0) {
  11333. if (plane_id == PLANE_CURSOR)
  11334. return false;
  11335. if (IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) == 10) {
  11336. if (plane_id != PLANE_SPRITE0)
  11337. return false;
  11338. } else {
  11339. if (plane_id != PLANE_SPRITE0 || pipe == PIPE_C ||
  11340. IS_SKYLAKE(dev_priv) || IS_BROXTON(dev_priv))
  11341. return false;
  11342. }
  11343. }
  11344. return true;
  11345. }
  11346. static struct intel_plane *
  11347. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11348. {
  11349. struct intel_plane *primary = NULL;
  11350. struct intel_plane_state *state = NULL;
  11351. const struct drm_plane_funcs *plane_funcs;
  11352. const uint32_t *intel_primary_formats;
  11353. unsigned int supported_rotations;
  11354. unsigned int num_formats;
  11355. const uint64_t *modifiers;
  11356. int ret;
  11357. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11358. if (!primary) {
  11359. ret = -ENOMEM;
  11360. goto fail;
  11361. }
  11362. state = intel_create_plane_state(&primary->base);
  11363. if (!state) {
  11364. ret = -ENOMEM;
  11365. goto fail;
  11366. }
  11367. primary->base.state = &state->base;
  11368. primary->can_scale = false;
  11369. primary->max_downscale = 1;
  11370. if (INTEL_GEN(dev_priv) >= 9) {
  11371. primary->can_scale = true;
  11372. state->scaler_id = -1;
  11373. }
  11374. primary->pipe = pipe;
  11375. /*
  11376. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11377. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11378. */
  11379. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11380. primary->i9xx_plane = (enum i9xx_plane_id) !pipe;
  11381. else
  11382. primary->i9xx_plane = (enum i9xx_plane_id) pipe;
  11383. primary->id = PLANE_PRIMARY;
  11384. primary->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, primary->id);
  11385. if (INTEL_GEN(dev_priv) >= 9)
  11386. primary->has_fbc = skl_plane_has_fbc(dev_priv,
  11387. primary->pipe,
  11388. primary->id);
  11389. else
  11390. primary->has_fbc = i9xx_plane_has_fbc(dev_priv,
  11391. primary->i9xx_plane);
  11392. if (primary->has_fbc) {
  11393. struct intel_fbc *fbc = &dev_priv->fbc;
  11394. fbc->possible_framebuffer_bits |= primary->frontbuffer_bit;
  11395. }
  11396. primary->check_plane = intel_check_primary_plane;
  11397. if (INTEL_GEN(dev_priv) >= 9) {
  11398. primary->has_ccs = skl_plane_has_ccs(dev_priv, pipe,
  11399. PLANE_PRIMARY);
  11400. if (skl_plane_has_planar(dev_priv, pipe, PLANE_PRIMARY)) {
  11401. intel_primary_formats = skl_pri_planar_formats;
  11402. num_formats = ARRAY_SIZE(skl_pri_planar_formats);
  11403. } else {
  11404. intel_primary_formats = skl_primary_formats;
  11405. num_formats = ARRAY_SIZE(skl_primary_formats);
  11406. }
  11407. if (primary->has_ccs)
  11408. modifiers = skl_format_modifiers_ccs;
  11409. else
  11410. modifiers = skl_format_modifiers_noccs;
  11411. primary->update_plane = skl_update_plane;
  11412. primary->disable_plane = skl_disable_plane;
  11413. primary->get_hw_state = skl_plane_get_hw_state;
  11414. plane_funcs = &skl_plane_funcs;
  11415. } else if (INTEL_GEN(dev_priv) >= 4) {
  11416. intel_primary_formats = i965_primary_formats;
  11417. num_formats = ARRAY_SIZE(i965_primary_formats);
  11418. modifiers = i9xx_format_modifiers;
  11419. primary->update_plane = i9xx_update_plane;
  11420. primary->disable_plane = i9xx_disable_plane;
  11421. primary->get_hw_state = i9xx_plane_get_hw_state;
  11422. plane_funcs = &i965_plane_funcs;
  11423. } else {
  11424. intel_primary_formats = i8xx_primary_formats;
  11425. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11426. modifiers = i9xx_format_modifiers;
  11427. primary->update_plane = i9xx_update_plane;
  11428. primary->disable_plane = i9xx_disable_plane;
  11429. primary->get_hw_state = i9xx_plane_get_hw_state;
  11430. plane_funcs = &i8xx_plane_funcs;
  11431. }
  11432. if (INTEL_GEN(dev_priv) >= 9)
  11433. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11434. 0, plane_funcs,
  11435. intel_primary_formats, num_formats,
  11436. modifiers,
  11437. DRM_PLANE_TYPE_PRIMARY,
  11438. "plane 1%c", pipe_name(pipe));
  11439. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11440. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11441. 0, plane_funcs,
  11442. intel_primary_formats, num_formats,
  11443. modifiers,
  11444. DRM_PLANE_TYPE_PRIMARY,
  11445. "primary %c", pipe_name(pipe));
  11446. else
  11447. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11448. 0, plane_funcs,
  11449. intel_primary_formats, num_formats,
  11450. modifiers,
  11451. DRM_PLANE_TYPE_PRIMARY,
  11452. "plane %c",
  11453. plane_name(primary->i9xx_plane));
  11454. if (ret)
  11455. goto fail;
  11456. if (INTEL_GEN(dev_priv) >= 10) {
  11457. supported_rotations =
  11458. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11459. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270 |
  11460. DRM_MODE_REFLECT_X;
  11461. } else if (INTEL_GEN(dev_priv) >= 9) {
  11462. supported_rotations =
  11463. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
  11464. DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
  11465. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11466. supported_rotations =
  11467. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
  11468. DRM_MODE_REFLECT_X;
  11469. } else if (INTEL_GEN(dev_priv) >= 4) {
  11470. supported_rotations =
  11471. DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
  11472. } else {
  11473. supported_rotations = DRM_MODE_ROTATE_0;
  11474. }
  11475. if (INTEL_GEN(dev_priv) >= 4)
  11476. drm_plane_create_rotation_property(&primary->base,
  11477. DRM_MODE_ROTATE_0,
  11478. supported_rotations);
  11479. if (INTEL_GEN(dev_priv) >= 9)
  11480. drm_plane_create_color_properties(&primary->base,
  11481. BIT(DRM_COLOR_YCBCR_BT601) |
  11482. BIT(DRM_COLOR_YCBCR_BT709),
  11483. BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
  11484. BIT(DRM_COLOR_YCBCR_FULL_RANGE),
  11485. DRM_COLOR_YCBCR_BT709,
  11486. DRM_COLOR_YCBCR_LIMITED_RANGE);
  11487. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11488. return primary;
  11489. fail:
  11490. kfree(state);
  11491. kfree(primary);
  11492. return ERR_PTR(ret);
  11493. }
  11494. static struct intel_plane *
  11495. intel_cursor_plane_create(struct drm_i915_private *dev_priv,
  11496. enum pipe pipe)
  11497. {
  11498. struct intel_plane *cursor = NULL;
  11499. struct intel_plane_state *state = NULL;
  11500. int ret;
  11501. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11502. if (!cursor) {
  11503. ret = -ENOMEM;
  11504. goto fail;
  11505. }
  11506. state = intel_create_plane_state(&cursor->base);
  11507. if (!state) {
  11508. ret = -ENOMEM;
  11509. goto fail;
  11510. }
  11511. cursor->base.state = &state->base;
  11512. cursor->can_scale = false;
  11513. cursor->max_downscale = 1;
  11514. cursor->pipe = pipe;
  11515. cursor->i9xx_plane = (enum i9xx_plane_id) pipe;
  11516. cursor->id = PLANE_CURSOR;
  11517. cursor->frontbuffer_bit = INTEL_FRONTBUFFER(pipe, cursor->id);
  11518. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  11519. cursor->update_plane = i845_update_cursor;
  11520. cursor->disable_plane = i845_disable_cursor;
  11521. cursor->get_hw_state = i845_cursor_get_hw_state;
  11522. cursor->check_plane = i845_check_cursor;
  11523. } else {
  11524. cursor->update_plane = i9xx_update_cursor;
  11525. cursor->disable_plane = i9xx_disable_cursor;
  11526. cursor->get_hw_state = i9xx_cursor_get_hw_state;
  11527. cursor->check_plane = i9xx_check_cursor;
  11528. }
  11529. cursor->cursor.base = ~0;
  11530. cursor->cursor.cntl = ~0;
  11531. if (IS_I845G(dev_priv) || IS_I865G(dev_priv) || HAS_CUR_FBC(dev_priv))
  11532. cursor->cursor.size = ~0;
  11533. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11534. 0, &intel_cursor_plane_funcs,
  11535. intel_cursor_formats,
  11536. ARRAY_SIZE(intel_cursor_formats),
  11537. cursor_format_modifiers,
  11538. DRM_PLANE_TYPE_CURSOR,
  11539. "cursor %c", pipe_name(pipe));
  11540. if (ret)
  11541. goto fail;
  11542. if (INTEL_GEN(dev_priv) >= 4)
  11543. drm_plane_create_rotation_property(&cursor->base,
  11544. DRM_MODE_ROTATE_0,
  11545. DRM_MODE_ROTATE_0 |
  11546. DRM_MODE_ROTATE_180);
  11547. if (INTEL_GEN(dev_priv) >= 9)
  11548. state->scaler_id = -1;
  11549. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11550. return cursor;
  11551. fail:
  11552. kfree(state);
  11553. kfree(cursor);
  11554. return ERR_PTR(ret);
  11555. }
  11556. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11557. struct intel_crtc_state *crtc_state)
  11558. {
  11559. struct intel_crtc_scaler_state *scaler_state =
  11560. &crtc_state->scaler_state;
  11561. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11562. int i;
  11563. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11564. if (!crtc->num_scalers)
  11565. return;
  11566. for (i = 0; i < crtc->num_scalers; i++) {
  11567. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11568. scaler->in_use = 0;
  11569. scaler->mode = PS_SCALER_MODE_DYN;
  11570. }
  11571. scaler_state->scaler_id = -1;
  11572. }
  11573. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11574. {
  11575. struct intel_crtc *intel_crtc;
  11576. struct intel_crtc_state *crtc_state = NULL;
  11577. struct intel_plane *primary = NULL;
  11578. struct intel_plane *cursor = NULL;
  11579. int sprite, ret;
  11580. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11581. if (!intel_crtc)
  11582. return -ENOMEM;
  11583. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11584. if (!crtc_state) {
  11585. ret = -ENOMEM;
  11586. goto fail;
  11587. }
  11588. intel_crtc->config = crtc_state;
  11589. intel_crtc->base.state = &crtc_state->base;
  11590. crtc_state->base.crtc = &intel_crtc->base;
  11591. primary = intel_primary_plane_create(dev_priv, pipe);
  11592. if (IS_ERR(primary)) {
  11593. ret = PTR_ERR(primary);
  11594. goto fail;
  11595. }
  11596. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11597. for_each_sprite(dev_priv, pipe, sprite) {
  11598. struct intel_plane *plane;
  11599. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11600. if (IS_ERR(plane)) {
  11601. ret = PTR_ERR(plane);
  11602. goto fail;
  11603. }
  11604. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11605. }
  11606. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11607. if (IS_ERR(cursor)) {
  11608. ret = PTR_ERR(cursor);
  11609. goto fail;
  11610. }
  11611. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11612. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11613. &primary->base, &cursor->base,
  11614. &intel_crtc_funcs,
  11615. "pipe %c", pipe_name(pipe));
  11616. if (ret)
  11617. goto fail;
  11618. intel_crtc->pipe = pipe;
  11619. /* initialize shared scalers */
  11620. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11621. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->pipe_to_crtc_mapping) ||
  11622. dev_priv->pipe_to_crtc_mapping[pipe] != NULL);
  11623. dev_priv->pipe_to_crtc_mapping[pipe] = intel_crtc;
  11624. if (INTEL_GEN(dev_priv) < 9) {
  11625. enum i9xx_plane_id i9xx_plane = primary->i9xx_plane;
  11626. BUG_ON(i9xx_plane >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11627. dev_priv->plane_to_crtc_mapping[i9xx_plane] != NULL);
  11628. dev_priv->plane_to_crtc_mapping[i9xx_plane] = intel_crtc;
  11629. }
  11630. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11631. intel_color_init(&intel_crtc->base);
  11632. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11633. return 0;
  11634. fail:
  11635. /*
  11636. * drm_mode_config_cleanup() will free up any
  11637. * crtcs/planes already initialized.
  11638. */
  11639. kfree(crtc_state);
  11640. kfree(intel_crtc);
  11641. return ret;
  11642. }
  11643. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11644. {
  11645. struct drm_device *dev = connector->base.dev;
  11646. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11647. if (!connector->base.state->crtc)
  11648. return INVALID_PIPE;
  11649. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11650. }
  11651. int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
  11652. struct drm_file *file)
  11653. {
  11654. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11655. struct drm_crtc *drmmode_crtc;
  11656. struct intel_crtc *crtc;
  11657. drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
  11658. if (!drmmode_crtc)
  11659. return -ENOENT;
  11660. crtc = to_intel_crtc(drmmode_crtc);
  11661. pipe_from_crtc_id->pipe = crtc->pipe;
  11662. return 0;
  11663. }
  11664. static int intel_encoder_clones(struct intel_encoder *encoder)
  11665. {
  11666. struct drm_device *dev = encoder->base.dev;
  11667. struct intel_encoder *source_encoder;
  11668. int index_mask = 0;
  11669. int entry = 0;
  11670. for_each_intel_encoder(dev, source_encoder) {
  11671. if (encoders_cloneable(encoder, source_encoder))
  11672. index_mask |= (1 << entry);
  11673. entry++;
  11674. }
  11675. return index_mask;
  11676. }
  11677. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11678. {
  11679. if (!IS_MOBILE(dev_priv))
  11680. return false;
  11681. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11682. return false;
  11683. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11684. return false;
  11685. return true;
  11686. }
  11687. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11688. {
  11689. if (INTEL_GEN(dev_priv) >= 9)
  11690. return false;
  11691. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11692. return false;
  11693. if (IS_CHERRYVIEW(dev_priv))
  11694. return false;
  11695. if (HAS_PCH_LPT_H(dev_priv) &&
  11696. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11697. return false;
  11698. /* DDI E can't be used if DDI A requires 4 lanes */
  11699. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11700. return false;
  11701. if (!dev_priv->vbt.int_crt_support)
  11702. return false;
  11703. return true;
  11704. }
  11705. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11706. {
  11707. int pps_num;
  11708. int pps_idx;
  11709. if (HAS_DDI(dev_priv))
  11710. return;
  11711. /*
  11712. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11713. * everywhere where registers can be write protected.
  11714. */
  11715. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11716. pps_num = 2;
  11717. else
  11718. pps_num = 1;
  11719. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11720. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11721. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11722. I915_WRITE(PP_CONTROL(pps_idx), val);
  11723. }
  11724. }
  11725. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11726. {
  11727. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11728. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11729. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11730. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11731. else
  11732. dev_priv->pps_mmio_base = PPS_BASE;
  11733. intel_pps_unlock_regs_wa(dev_priv);
  11734. }
  11735. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11736. {
  11737. struct intel_encoder *encoder;
  11738. bool dpd_is_edp = false;
  11739. intel_pps_init(dev_priv);
  11740. /*
  11741. * intel_edp_init_connector() depends on this completing first, to
  11742. * prevent the registeration of both eDP and LVDS and the incorrect
  11743. * sharing of the PPS.
  11744. */
  11745. intel_lvds_init(dev_priv);
  11746. if (intel_crt_present(dev_priv))
  11747. intel_crt_init(dev_priv);
  11748. if (IS_ICELAKE(dev_priv)) {
  11749. intel_ddi_init(dev_priv, PORT_A);
  11750. intel_ddi_init(dev_priv, PORT_B);
  11751. intel_ddi_init(dev_priv, PORT_C);
  11752. intel_ddi_init(dev_priv, PORT_D);
  11753. intel_ddi_init(dev_priv, PORT_E);
  11754. intel_ddi_init(dev_priv, PORT_F);
  11755. } else if (IS_GEN9_LP(dev_priv)) {
  11756. /*
  11757. * FIXME: Broxton doesn't support port detection via the
  11758. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11759. * detect the ports.
  11760. */
  11761. intel_ddi_init(dev_priv, PORT_A);
  11762. intel_ddi_init(dev_priv, PORT_B);
  11763. intel_ddi_init(dev_priv, PORT_C);
  11764. intel_dsi_init(dev_priv);
  11765. } else if (HAS_DDI(dev_priv)) {
  11766. int found;
  11767. /*
  11768. * Haswell uses DDI functions to detect digital outputs.
  11769. * On SKL pre-D0 the strap isn't connected, so we assume
  11770. * it's there.
  11771. */
  11772. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11773. /* WaIgnoreDDIAStrap: skl */
  11774. if (found || IS_GEN9_BC(dev_priv))
  11775. intel_ddi_init(dev_priv, PORT_A);
  11776. /* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
  11777. * register */
  11778. found = I915_READ(SFUSE_STRAP);
  11779. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11780. intel_ddi_init(dev_priv, PORT_B);
  11781. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11782. intel_ddi_init(dev_priv, PORT_C);
  11783. if (found & SFUSE_STRAP_DDID_DETECTED)
  11784. intel_ddi_init(dev_priv, PORT_D);
  11785. if (found & SFUSE_STRAP_DDIF_DETECTED)
  11786. intel_ddi_init(dev_priv, PORT_F);
  11787. /*
  11788. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11789. */
  11790. if (IS_GEN9_BC(dev_priv) &&
  11791. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11792. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11793. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11794. intel_ddi_init(dev_priv, PORT_E);
  11795. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11796. int found;
  11797. dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
  11798. if (has_edp_a(dev_priv))
  11799. intel_dp_init(dev_priv, DP_A, PORT_A);
  11800. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11801. /* PCH SDVOB multiplex with HDMIB */
  11802. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11803. if (!found)
  11804. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11805. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11806. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11807. }
  11808. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11809. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11810. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11811. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11812. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11813. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11814. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11815. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11816. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11817. bool has_edp, has_port;
  11818. /*
  11819. * The DP_DETECTED bit is the latched state of the DDC
  11820. * SDA pin at boot. However since eDP doesn't require DDC
  11821. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11822. * eDP ports may have been muxed to an alternate function.
  11823. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11824. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11825. * detect eDP ports.
  11826. *
  11827. * Sadly the straps seem to be missing sometimes even for HDMI
  11828. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11829. * and VBT for the presence of the port. Additionally we can't
  11830. * trust the port type the VBT declares as we've seen at least
  11831. * HDMI ports that the VBT claim are DP or eDP.
  11832. */
  11833. has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
  11834. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11835. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11836. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11837. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11838. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11839. has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
  11840. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11841. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11842. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11843. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11844. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11845. if (IS_CHERRYVIEW(dev_priv)) {
  11846. /*
  11847. * eDP not supported on port D,
  11848. * so no need to worry about it
  11849. */
  11850. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11851. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11852. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11853. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11854. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11855. }
  11856. intel_dsi_init(dev_priv);
  11857. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11858. bool found = false;
  11859. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11860. DRM_DEBUG_KMS("probing SDVOB\n");
  11861. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11862. if (!found && IS_G4X(dev_priv)) {
  11863. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11864. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11865. }
  11866. if (!found && IS_G4X(dev_priv))
  11867. intel_dp_init(dev_priv, DP_B, PORT_B);
  11868. }
  11869. /* Before G4X SDVOC doesn't have its own detect register */
  11870. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11871. DRM_DEBUG_KMS("probing SDVOC\n");
  11872. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11873. }
  11874. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11875. if (IS_G4X(dev_priv)) {
  11876. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11877. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11878. }
  11879. if (IS_G4X(dev_priv))
  11880. intel_dp_init(dev_priv, DP_C, PORT_C);
  11881. }
  11882. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11883. intel_dp_init(dev_priv, DP_D, PORT_D);
  11884. } else if (IS_GEN2(dev_priv))
  11885. intel_dvo_init(dev_priv);
  11886. if (SUPPORTS_TV(dev_priv))
  11887. intel_tv_init(dev_priv);
  11888. intel_psr_init(dev_priv);
  11889. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11890. encoder->base.possible_crtcs = encoder->crtc_mask;
  11891. encoder->base.possible_clones =
  11892. intel_encoder_clones(encoder);
  11893. }
  11894. intel_init_pch_refclk(dev_priv);
  11895. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11896. }
  11897. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11898. {
  11899. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11900. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11901. drm_framebuffer_cleanup(fb);
  11902. i915_gem_object_lock(obj);
  11903. WARN_ON(!obj->framebuffer_references--);
  11904. i915_gem_object_unlock(obj);
  11905. i915_gem_object_put(obj);
  11906. kfree(intel_fb);
  11907. }
  11908. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11909. struct drm_file *file,
  11910. unsigned int *handle)
  11911. {
  11912. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11913. if (obj->userptr.mm) {
  11914. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11915. return -EINVAL;
  11916. }
  11917. return drm_gem_handle_create(file, &obj->base, handle);
  11918. }
  11919. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11920. struct drm_file *file,
  11921. unsigned flags, unsigned color,
  11922. struct drm_clip_rect *clips,
  11923. unsigned num_clips)
  11924. {
  11925. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11926. i915_gem_object_flush_if_display(obj);
  11927. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11928. return 0;
  11929. }
  11930. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11931. .destroy = intel_user_framebuffer_destroy,
  11932. .create_handle = intel_user_framebuffer_create_handle,
  11933. .dirty = intel_user_framebuffer_dirty,
  11934. };
  11935. static
  11936. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11937. uint64_t fb_modifier, uint32_t pixel_format)
  11938. {
  11939. u32 gen = INTEL_GEN(dev_priv);
  11940. if (gen >= 9) {
  11941. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11942. /* "The stride in bytes must not exceed the of the size of 8K
  11943. * pixels and 32K bytes."
  11944. */
  11945. return min(8192 * cpp, 32768);
  11946. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11947. return 32*1024;
  11948. } else if (gen >= 4) {
  11949. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11950. return 16*1024;
  11951. else
  11952. return 32*1024;
  11953. } else if (gen >= 3) {
  11954. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11955. return 8*1024;
  11956. else
  11957. return 16*1024;
  11958. } else {
  11959. /* XXX DSPC is limited to 4k tiled */
  11960. return 8*1024;
  11961. }
  11962. }
  11963. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11964. struct drm_i915_gem_object *obj,
  11965. struct drm_mode_fb_cmd2 *mode_cmd)
  11966. {
  11967. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11968. struct drm_framebuffer *fb = &intel_fb->base;
  11969. struct drm_format_name_buf format_name;
  11970. u32 pitch_limit;
  11971. unsigned int tiling, stride;
  11972. int ret = -EINVAL;
  11973. int i;
  11974. i915_gem_object_lock(obj);
  11975. obj->framebuffer_references++;
  11976. tiling = i915_gem_object_get_tiling(obj);
  11977. stride = i915_gem_object_get_stride(obj);
  11978. i915_gem_object_unlock(obj);
  11979. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11980. /*
  11981. * If there's a fence, enforce that
  11982. * the fb modifier and tiling mode match.
  11983. */
  11984. if (tiling != I915_TILING_NONE &&
  11985. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11986. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  11987. goto err;
  11988. }
  11989. } else {
  11990. if (tiling == I915_TILING_X) {
  11991. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11992. } else if (tiling == I915_TILING_Y) {
  11993. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  11994. goto err;
  11995. }
  11996. }
  11997. /* Passed in modifier sanity checking. */
  11998. switch (mode_cmd->modifier[0]) {
  11999. case I915_FORMAT_MOD_Y_TILED_CCS:
  12000. case I915_FORMAT_MOD_Yf_TILED_CCS:
  12001. switch (mode_cmd->pixel_format) {
  12002. case DRM_FORMAT_XBGR8888:
  12003. case DRM_FORMAT_ABGR8888:
  12004. case DRM_FORMAT_XRGB8888:
  12005. case DRM_FORMAT_ARGB8888:
  12006. break;
  12007. default:
  12008. DRM_DEBUG_KMS("RC supported only with RGB8888 formats\n");
  12009. goto err;
  12010. }
  12011. /* fall through */
  12012. case I915_FORMAT_MOD_Y_TILED:
  12013. case I915_FORMAT_MOD_Yf_TILED:
  12014. if (INTEL_GEN(dev_priv) < 9) {
  12015. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  12016. mode_cmd->modifier[0]);
  12017. goto err;
  12018. }
  12019. case DRM_FORMAT_MOD_LINEAR:
  12020. case I915_FORMAT_MOD_X_TILED:
  12021. break;
  12022. default:
  12023. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  12024. mode_cmd->modifier[0]);
  12025. goto err;
  12026. }
  12027. /*
  12028. * gen2/3 display engine uses the fence if present,
  12029. * so the tiling mode must match the fb modifier exactly.
  12030. */
  12031. if (INTEL_GEN(dev_priv) < 4 &&
  12032. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  12033. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  12034. goto err;
  12035. }
  12036. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  12037. mode_cmd->pixel_format);
  12038. if (mode_cmd->pitches[0] > pitch_limit) {
  12039. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  12040. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  12041. "tiled" : "linear",
  12042. mode_cmd->pitches[0], pitch_limit);
  12043. goto err;
  12044. }
  12045. /*
  12046. * If there's a fence, enforce that
  12047. * the fb pitch and fence stride match.
  12048. */
  12049. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  12050. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  12051. mode_cmd->pitches[0], stride);
  12052. goto err;
  12053. }
  12054. /* Reject formats not supported by any plane early. */
  12055. switch (mode_cmd->pixel_format) {
  12056. case DRM_FORMAT_C8:
  12057. case DRM_FORMAT_RGB565:
  12058. case DRM_FORMAT_XRGB8888:
  12059. case DRM_FORMAT_ARGB8888:
  12060. break;
  12061. case DRM_FORMAT_XRGB1555:
  12062. if (INTEL_GEN(dev_priv) > 3) {
  12063. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12064. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12065. goto err;
  12066. }
  12067. break;
  12068. case DRM_FORMAT_ABGR8888:
  12069. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  12070. INTEL_GEN(dev_priv) < 9) {
  12071. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12072. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12073. goto err;
  12074. }
  12075. break;
  12076. case DRM_FORMAT_XBGR8888:
  12077. case DRM_FORMAT_XRGB2101010:
  12078. case DRM_FORMAT_XBGR2101010:
  12079. if (INTEL_GEN(dev_priv) < 4) {
  12080. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12081. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12082. goto err;
  12083. }
  12084. break;
  12085. case DRM_FORMAT_ABGR2101010:
  12086. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  12087. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12088. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12089. goto err;
  12090. }
  12091. break;
  12092. case DRM_FORMAT_YUYV:
  12093. case DRM_FORMAT_UYVY:
  12094. case DRM_FORMAT_YVYU:
  12095. case DRM_FORMAT_VYUY:
  12096. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  12097. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12098. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12099. goto err;
  12100. }
  12101. break;
  12102. case DRM_FORMAT_NV12:
  12103. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_Y_TILED_CCS ||
  12104. mode_cmd->modifier[0] == I915_FORMAT_MOD_Yf_TILED_CCS) {
  12105. DRM_DEBUG_KMS("RC not to be enabled with NV12\n");
  12106. goto err;
  12107. }
  12108. if (INTEL_GEN(dev_priv) < 9 || IS_SKYLAKE(dev_priv) ||
  12109. IS_BROXTON(dev_priv)) {
  12110. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12111. drm_get_format_name(mode_cmd->pixel_format,
  12112. &format_name));
  12113. goto err;
  12114. }
  12115. break;
  12116. default:
  12117. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12118. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12119. goto err;
  12120. }
  12121. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12122. if (mode_cmd->offsets[0] != 0)
  12123. goto err;
  12124. drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
  12125. if (fb->format->format == DRM_FORMAT_NV12 &&
  12126. (fb->width < SKL_MIN_YUV_420_SRC_W ||
  12127. fb->height < SKL_MIN_YUV_420_SRC_H ||
  12128. (fb->width % 4) != 0 || (fb->height % 4) != 0)) {
  12129. DRM_DEBUG_KMS("src dimensions not correct for NV12\n");
  12130. return -EINVAL;
  12131. }
  12132. for (i = 0; i < fb->format->num_planes; i++) {
  12133. u32 stride_alignment;
  12134. if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
  12135. DRM_DEBUG_KMS("bad plane %d handle\n", i);
  12136. goto err;
  12137. }
  12138. stride_alignment = intel_fb_stride_alignment(fb, i);
  12139. /*
  12140. * Display WA #0531: skl,bxt,kbl,glk
  12141. *
  12142. * Render decompression and plane width > 3840
  12143. * combined with horizontal panning requires the
  12144. * plane stride to be a multiple of 4. We'll just
  12145. * require the entire fb to accommodate that to avoid
  12146. * potential runtime errors at plane configuration time.
  12147. */
  12148. if (IS_GEN9(dev_priv) && i == 0 && fb->width > 3840 &&
  12149. (fb->modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
  12150. fb->modifier == I915_FORMAT_MOD_Yf_TILED_CCS))
  12151. stride_alignment *= 4;
  12152. if (fb->pitches[i] & (stride_alignment - 1)) {
  12153. DRM_DEBUG_KMS("plane %d pitch (%d) must be at least %u byte aligned\n",
  12154. i, fb->pitches[i], stride_alignment);
  12155. goto err;
  12156. }
  12157. fb->obj[i] = &obj->base;
  12158. }
  12159. ret = intel_fill_fb_info(dev_priv, fb);
  12160. if (ret)
  12161. goto err;
  12162. ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
  12163. if (ret) {
  12164. DRM_ERROR("framebuffer init failed %d\n", ret);
  12165. goto err;
  12166. }
  12167. return 0;
  12168. err:
  12169. i915_gem_object_lock(obj);
  12170. obj->framebuffer_references--;
  12171. i915_gem_object_unlock(obj);
  12172. return ret;
  12173. }
  12174. static struct drm_framebuffer *
  12175. intel_user_framebuffer_create(struct drm_device *dev,
  12176. struct drm_file *filp,
  12177. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12178. {
  12179. struct drm_framebuffer *fb;
  12180. struct drm_i915_gem_object *obj;
  12181. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12182. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  12183. if (!obj)
  12184. return ERR_PTR(-ENOENT);
  12185. fb = intel_framebuffer_create(obj, &mode_cmd);
  12186. if (IS_ERR(fb))
  12187. i915_gem_object_put(obj);
  12188. return fb;
  12189. }
  12190. static void intel_atomic_state_free(struct drm_atomic_state *state)
  12191. {
  12192. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12193. drm_atomic_state_default_release(state);
  12194. i915_sw_fence_fini(&intel_state->commit_ready);
  12195. kfree(state);
  12196. }
  12197. static enum drm_mode_status
  12198. intel_mode_valid(struct drm_device *dev,
  12199. const struct drm_display_mode *mode)
  12200. {
  12201. struct drm_i915_private *dev_priv = to_i915(dev);
  12202. int hdisplay_max, htotal_max;
  12203. int vdisplay_max, vtotal_max;
  12204. /*
  12205. * Can't reject DBLSCAN here because Xorg ddxen can add piles
  12206. * of DBLSCAN modes to the output's mode list when they detect
  12207. * the scaling mode property on the connector. And they don't
  12208. * ask the kernel to validate those modes in any way until
  12209. * modeset time at which point the client gets a protocol error.
  12210. * So in order to not upset those clients we silently ignore the
  12211. * DBLSCAN flag on such connectors. For other connectors we will
  12212. * reject modes with the DBLSCAN flag in encoder->compute_config().
  12213. * And we always reject DBLSCAN modes in connector->mode_valid()
  12214. * as we never want such modes on the connector's mode list.
  12215. */
  12216. if (mode->vscan > 1)
  12217. return MODE_NO_VSCAN;
  12218. if (mode->flags & DRM_MODE_FLAG_HSKEW)
  12219. return MODE_H_ILLEGAL;
  12220. if (mode->flags & (DRM_MODE_FLAG_CSYNC |
  12221. DRM_MODE_FLAG_NCSYNC |
  12222. DRM_MODE_FLAG_PCSYNC))
  12223. return MODE_HSYNC;
  12224. if (mode->flags & (DRM_MODE_FLAG_BCAST |
  12225. DRM_MODE_FLAG_PIXMUX |
  12226. DRM_MODE_FLAG_CLKDIV2))
  12227. return MODE_BAD;
  12228. if (INTEL_GEN(dev_priv) >= 9 ||
  12229. IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
  12230. hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
  12231. vdisplay_max = 4096;
  12232. htotal_max = 8192;
  12233. vtotal_max = 8192;
  12234. } else if (INTEL_GEN(dev_priv) >= 3) {
  12235. hdisplay_max = 4096;
  12236. vdisplay_max = 4096;
  12237. htotal_max = 8192;
  12238. vtotal_max = 8192;
  12239. } else {
  12240. hdisplay_max = 2048;
  12241. vdisplay_max = 2048;
  12242. htotal_max = 4096;
  12243. vtotal_max = 4096;
  12244. }
  12245. if (mode->hdisplay > hdisplay_max ||
  12246. mode->hsync_start > htotal_max ||
  12247. mode->hsync_end > htotal_max ||
  12248. mode->htotal > htotal_max)
  12249. return MODE_H_ILLEGAL;
  12250. if (mode->vdisplay > vdisplay_max ||
  12251. mode->vsync_start > vtotal_max ||
  12252. mode->vsync_end > vtotal_max ||
  12253. mode->vtotal > vtotal_max)
  12254. return MODE_V_ILLEGAL;
  12255. return MODE_OK;
  12256. }
  12257. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12258. .fb_create = intel_user_framebuffer_create,
  12259. .get_format_info = intel_get_format_info,
  12260. .output_poll_changed = intel_fbdev_output_poll_changed,
  12261. .mode_valid = intel_mode_valid,
  12262. .atomic_check = intel_atomic_check,
  12263. .atomic_commit = intel_atomic_commit,
  12264. .atomic_state_alloc = intel_atomic_state_alloc,
  12265. .atomic_state_clear = intel_atomic_state_clear,
  12266. .atomic_state_free = intel_atomic_state_free,
  12267. };
  12268. /**
  12269. * intel_init_display_hooks - initialize the display modesetting hooks
  12270. * @dev_priv: device private
  12271. */
  12272. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12273. {
  12274. intel_init_cdclk_hooks(dev_priv);
  12275. if (INTEL_GEN(dev_priv) >= 9) {
  12276. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12277. dev_priv->display.get_initial_plane_config =
  12278. skylake_get_initial_plane_config;
  12279. dev_priv->display.crtc_compute_clock =
  12280. haswell_crtc_compute_clock;
  12281. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12282. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12283. } else if (HAS_DDI(dev_priv)) {
  12284. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12285. dev_priv->display.get_initial_plane_config =
  12286. i9xx_get_initial_plane_config;
  12287. dev_priv->display.crtc_compute_clock =
  12288. haswell_crtc_compute_clock;
  12289. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12290. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12291. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12292. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12293. dev_priv->display.get_initial_plane_config =
  12294. i9xx_get_initial_plane_config;
  12295. dev_priv->display.crtc_compute_clock =
  12296. ironlake_crtc_compute_clock;
  12297. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12298. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12299. } else if (IS_CHERRYVIEW(dev_priv)) {
  12300. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12301. dev_priv->display.get_initial_plane_config =
  12302. i9xx_get_initial_plane_config;
  12303. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12304. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12305. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12306. } else if (IS_VALLEYVIEW(dev_priv)) {
  12307. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12308. dev_priv->display.get_initial_plane_config =
  12309. i9xx_get_initial_plane_config;
  12310. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12311. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12312. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12313. } else if (IS_G4X(dev_priv)) {
  12314. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12315. dev_priv->display.get_initial_plane_config =
  12316. i9xx_get_initial_plane_config;
  12317. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12318. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12319. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12320. } else if (IS_PINEVIEW(dev_priv)) {
  12321. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12322. dev_priv->display.get_initial_plane_config =
  12323. i9xx_get_initial_plane_config;
  12324. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12325. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12326. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12327. } else if (!IS_GEN2(dev_priv)) {
  12328. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12329. dev_priv->display.get_initial_plane_config =
  12330. i9xx_get_initial_plane_config;
  12331. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12332. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12333. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12334. } else {
  12335. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12336. dev_priv->display.get_initial_plane_config =
  12337. i9xx_get_initial_plane_config;
  12338. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12339. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12340. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12341. }
  12342. if (IS_GEN5(dev_priv)) {
  12343. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12344. } else if (IS_GEN6(dev_priv)) {
  12345. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12346. } else if (IS_IVYBRIDGE(dev_priv)) {
  12347. /* FIXME: detect B0+ stepping and use auto training */
  12348. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12349. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12350. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12351. }
  12352. if (INTEL_GEN(dev_priv) >= 9)
  12353. dev_priv->display.update_crtcs = skl_update_crtcs;
  12354. else
  12355. dev_priv->display.update_crtcs = intel_update_crtcs;
  12356. }
  12357. /*
  12358. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12359. */
  12360. static void quirk_ssc_force_disable(struct drm_device *dev)
  12361. {
  12362. struct drm_i915_private *dev_priv = to_i915(dev);
  12363. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12364. DRM_INFO("applying lvds SSC disable quirk\n");
  12365. }
  12366. /*
  12367. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12368. * brightness value
  12369. */
  12370. static void quirk_invert_brightness(struct drm_device *dev)
  12371. {
  12372. struct drm_i915_private *dev_priv = to_i915(dev);
  12373. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12374. DRM_INFO("applying inverted panel brightness quirk\n");
  12375. }
  12376. /* Some VBT's incorrectly indicate no backlight is present */
  12377. static void quirk_backlight_present(struct drm_device *dev)
  12378. {
  12379. struct drm_i915_private *dev_priv = to_i915(dev);
  12380. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12381. DRM_INFO("applying backlight present quirk\n");
  12382. }
  12383. /* Toshiba Satellite P50-C-18C requires T12 delay to be min 800ms
  12384. * which is 300 ms greater than eDP spec T12 min.
  12385. */
  12386. static void quirk_increase_t12_delay(struct drm_device *dev)
  12387. {
  12388. struct drm_i915_private *dev_priv = to_i915(dev);
  12389. dev_priv->quirks |= QUIRK_INCREASE_T12_DELAY;
  12390. DRM_INFO("Applying T12 delay quirk\n");
  12391. }
  12392. struct intel_quirk {
  12393. int device;
  12394. int subsystem_vendor;
  12395. int subsystem_device;
  12396. void (*hook)(struct drm_device *dev);
  12397. };
  12398. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12399. struct intel_dmi_quirk {
  12400. void (*hook)(struct drm_device *dev);
  12401. const struct dmi_system_id (*dmi_id_list)[];
  12402. };
  12403. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12404. {
  12405. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12406. return 1;
  12407. }
  12408. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12409. {
  12410. .dmi_id_list = &(const struct dmi_system_id[]) {
  12411. {
  12412. .callback = intel_dmi_reverse_brightness,
  12413. .ident = "NCR Corporation",
  12414. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12415. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12416. },
  12417. },
  12418. { } /* terminating entry */
  12419. },
  12420. .hook = quirk_invert_brightness,
  12421. },
  12422. };
  12423. static struct intel_quirk intel_quirks[] = {
  12424. /* Lenovo U160 cannot use SSC on LVDS */
  12425. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12426. /* Sony Vaio Y cannot use SSC on LVDS */
  12427. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12428. /* Acer Aspire 5734Z must invert backlight brightness */
  12429. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12430. /* Acer/eMachines G725 */
  12431. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12432. /* Acer/eMachines e725 */
  12433. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12434. /* Acer/Packard Bell NCL20 */
  12435. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12436. /* Acer Aspire 4736Z */
  12437. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12438. /* Acer Aspire 5336 */
  12439. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12440. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12441. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12442. /* Acer C720 Chromebook (Core i3 4005U) */
  12443. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12444. /* Apple Macbook 2,1 (Core 2 T7400) */
  12445. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12446. /* Apple Macbook 4,1 */
  12447. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12448. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12449. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12450. /* HP Chromebook 14 (Celeron 2955U) */
  12451. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12452. /* Dell Chromebook 11 */
  12453. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12454. /* Dell Chromebook 11 (2015 version) */
  12455. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12456. /* Toshiba Satellite P50-C-18C */
  12457. { 0x191B, 0x1179, 0xF840, quirk_increase_t12_delay },
  12458. };
  12459. static void intel_init_quirks(struct drm_device *dev)
  12460. {
  12461. struct pci_dev *d = dev->pdev;
  12462. int i;
  12463. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12464. struct intel_quirk *q = &intel_quirks[i];
  12465. if (d->device == q->device &&
  12466. (d->subsystem_vendor == q->subsystem_vendor ||
  12467. q->subsystem_vendor == PCI_ANY_ID) &&
  12468. (d->subsystem_device == q->subsystem_device ||
  12469. q->subsystem_device == PCI_ANY_ID))
  12470. q->hook(dev);
  12471. }
  12472. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12473. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12474. intel_dmi_quirks[i].hook(dev);
  12475. }
  12476. }
  12477. /* Disable the VGA plane that we never use */
  12478. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  12479. {
  12480. struct pci_dev *pdev = dev_priv->drm.pdev;
  12481. u8 sr1;
  12482. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12483. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12484. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12485. outb(SR01, VGA_SR_INDEX);
  12486. sr1 = inb(VGA_SR_DATA);
  12487. outb(sr1 | 1<<5, VGA_SR_DATA);
  12488. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12489. udelay(300);
  12490. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12491. POSTING_READ(vga_reg);
  12492. }
  12493. void intel_modeset_init_hw(struct drm_device *dev)
  12494. {
  12495. struct drm_i915_private *dev_priv = to_i915(dev);
  12496. intel_update_cdclk(dev_priv);
  12497. intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
  12498. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12499. }
  12500. /*
  12501. * Calculate what we think the watermarks should be for the state we've read
  12502. * out of the hardware and then immediately program those watermarks so that
  12503. * we ensure the hardware settings match our internal state.
  12504. *
  12505. * We can calculate what we think WM's should be by creating a duplicate of the
  12506. * current state (which was constructed during hardware readout) and running it
  12507. * through the atomic check code to calculate new watermark values in the
  12508. * state object.
  12509. */
  12510. static void sanitize_watermarks(struct drm_device *dev)
  12511. {
  12512. struct drm_i915_private *dev_priv = to_i915(dev);
  12513. struct drm_atomic_state *state;
  12514. struct intel_atomic_state *intel_state;
  12515. struct drm_crtc *crtc;
  12516. struct drm_crtc_state *cstate;
  12517. struct drm_modeset_acquire_ctx ctx;
  12518. int ret;
  12519. int i;
  12520. /* Only supported on platforms that use atomic watermark design */
  12521. if (!dev_priv->display.optimize_watermarks)
  12522. return;
  12523. /*
  12524. * We need to hold connection_mutex before calling duplicate_state so
  12525. * that the connector loop is protected.
  12526. */
  12527. drm_modeset_acquire_init(&ctx, 0);
  12528. retry:
  12529. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12530. if (ret == -EDEADLK) {
  12531. drm_modeset_backoff(&ctx);
  12532. goto retry;
  12533. } else if (WARN_ON(ret)) {
  12534. goto fail;
  12535. }
  12536. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12537. if (WARN_ON(IS_ERR(state)))
  12538. goto fail;
  12539. intel_state = to_intel_atomic_state(state);
  12540. /*
  12541. * Hardware readout is the only time we don't want to calculate
  12542. * intermediate watermarks (since we don't trust the current
  12543. * watermarks).
  12544. */
  12545. if (!HAS_GMCH_DISPLAY(dev_priv))
  12546. intel_state->skip_intermediate_wm = true;
  12547. ret = intel_atomic_check(dev, state);
  12548. if (ret) {
  12549. /*
  12550. * If we fail here, it means that the hardware appears to be
  12551. * programmed in a way that shouldn't be possible, given our
  12552. * understanding of watermark requirements. This might mean a
  12553. * mistake in the hardware readout code or a mistake in the
  12554. * watermark calculations for a given platform. Raise a WARN
  12555. * so that this is noticeable.
  12556. *
  12557. * If this actually happens, we'll have to just leave the
  12558. * BIOS-programmed watermarks untouched and hope for the best.
  12559. */
  12560. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12561. goto put_state;
  12562. }
  12563. /* Write calculated watermark values back */
  12564. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12565. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12566. cs->wm.need_postvbl_update = true;
  12567. dev_priv->display.optimize_watermarks(intel_state, cs);
  12568. to_intel_crtc_state(crtc->state)->wm = cs->wm;
  12569. }
  12570. put_state:
  12571. drm_atomic_state_put(state);
  12572. fail:
  12573. drm_modeset_drop_locks(&ctx);
  12574. drm_modeset_acquire_fini(&ctx);
  12575. }
  12576. static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
  12577. {
  12578. if (IS_GEN5(dev_priv)) {
  12579. u32 fdi_pll_clk =
  12580. I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
  12581. dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
  12582. } else if (IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
  12583. dev_priv->fdi_pll_freq = 270000;
  12584. } else {
  12585. return;
  12586. }
  12587. DRM_DEBUG_DRIVER("FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
  12588. }
  12589. int intel_modeset_init(struct drm_device *dev)
  12590. {
  12591. struct drm_i915_private *dev_priv = to_i915(dev);
  12592. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12593. enum pipe pipe;
  12594. struct intel_crtc *crtc;
  12595. dev_priv->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
  12596. drm_mode_config_init(dev);
  12597. dev->mode_config.min_width = 0;
  12598. dev->mode_config.min_height = 0;
  12599. dev->mode_config.preferred_depth = 24;
  12600. dev->mode_config.prefer_shadow = 1;
  12601. dev->mode_config.allow_fb_modifiers = true;
  12602. dev->mode_config.funcs = &intel_mode_funcs;
  12603. init_llist_head(&dev_priv->atomic_helper.free_list);
  12604. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12605. intel_atomic_helper_free_state_worker);
  12606. intel_init_quirks(dev);
  12607. intel_init_pm(dev_priv);
  12608. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12609. return 0;
  12610. /*
  12611. * There may be no VBT; and if the BIOS enabled SSC we can
  12612. * just keep using it to avoid unnecessary flicker. Whereas if the
  12613. * BIOS isn't using it, don't assume it will work even if the VBT
  12614. * indicates as much.
  12615. */
  12616. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12617. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12618. DREF_SSC1_ENABLE);
  12619. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12620. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12621. bios_lvds_use_ssc ? "en" : "dis",
  12622. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12623. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12624. }
  12625. }
  12626. /* maximum framebuffer dimensions */
  12627. if (IS_GEN2(dev_priv)) {
  12628. dev->mode_config.max_width = 2048;
  12629. dev->mode_config.max_height = 2048;
  12630. } else if (IS_GEN3(dev_priv)) {
  12631. dev->mode_config.max_width = 4096;
  12632. dev->mode_config.max_height = 4096;
  12633. } else {
  12634. dev->mode_config.max_width = 8192;
  12635. dev->mode_config.max_height = 8192;
  12636. }
  12637. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12638. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12639. dev->mode_config.cursor_height = 1023;
  12640. } else if (IS_GEN2(dev_priv)) {
  12641. dev->mode_config.cursor_width = 64;
  12642. dev->mode_config.cursor_height = 64;
  12643. } else {
  12644. dev->mode_config.cursor_width = 256;
  12645. dev->mode_config.cursor_height = 256;
  12646. }
  12647. dev->mode_config.fb_base = ggtt->gmadr.start;
  12648. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12649. INTEL_INFO(dev_priv)->num_pipes,
  12650. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12651. for_each_pipe(dev_priv, pipe) {
  12652. int ret;
  12653. ret = intel_crtc_init(dev_priv, pipe);
  12654. if (ret) {
  12655. drm_mode_config_cleanup(dev);
  12656. return ret;
  12657. }
  12658. }
  12659. intel_shared_dpll_init(dev);
  12660. intel_update_fdi_pll_freq(dev_priv);
  12661. intel_update_czclk(dev_priv);
  12662. intel_modeset_init_hw(dev);
  12663. if (dev_priv->max_cdclk_freq == 0)
  12664. intel_update_max_cdclk(dev_priv);
  12665. /* Just disable it once at startup */
  12666. i915_disable_vga(dev_priv);
  12667. intel_setup_outputs(dev_priv);
  12668. drm_modeset_lock_all(dev);
  12669. intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
  12670. drm_modeset_unlock_all(dev);
  12671. for_each_intel_crtc(dev, crtc) {
  12672. struct intel_initial_plane_config plane_config = {};
  12673. if (!crtc->active)
  12674. continue;
  12675. /*
  12676. * Note that reserving the BIOS fb up front prevents us
  12677. * from stuffing other stolen allocations like the ring
  12678. * on top. This prevents some ugliness at boot time, and
  12679. * can even allow for smooth boot transitions if the BIOS
  12680. * fb is large enough for the active pipe configuration.
  12681. */
  12682. dev_priv->display.get_initial_plane_config(crtc,
  12683. &plane_config);
  12684. /*
  12685. * If the fb is shared between multiple heads, we'll
  12686. * just get the first one.
  12687. */
  12688. intel_find_initial_plane_obj(crtc, &plane_config);
  12689. }
  12690. /*
  12691. * Make sure hardware watermarks really match the state we read out.
  12692. * Note that we need to do this after reconstructing the BIOS fb's
  12693. * since the watermark calculation done here will use pstate->fb.
  12694. */
  12695. if (!HAS_GMCH_DISPLAY(dev_priv))
  12696. sanitize_watermarks(dev);
  12697. return 0;
  12698. }
  12699. void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12700. {
  12701. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12702. /* 640x480@60Hz, ~25175 kHz */
  12703. struct dpll clock = {
  12704. .m1 = 18,
  12705. .m2 = 7,
  12706. .p1 = 13,
  12707. .p2 = 4,
  12708. .n = 2,
  12709. };
  12710. u32 dpll, fp;
  12711. int i;
  12712. WARN_ON(i9xx_calc_dpll_params(48000, &clock) != 25154);
  12713. DRM_DEBUG_KMS("enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
  12714. pipe_name(pipe), clock.vco, clock.dot);
  12715. fp = i9xx_dpll_compute_fp(&clock);
  12716. dpll = (I915_READ(DPLL(pipe)) & DPLL_DVO_2X_MODE) |
  12717. DPLL_VGA_MODE_DIS |
  12718. ((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
  12719. PLL_P2_DIVIDE_BY_4 |
  12720. PLL_REF_INPUT_DREFCLK |
  12721. DPLL_VCO_ENABLE;
  12722. I915_WRITE(FP0(pipe), fp);
  12723. I915_WRITE(FP1(pipe), fp);
  12724. I915_WRITE(HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
  12725. I915_WRITE(HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
  12726. I915_WRITE(HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
  12727. I915_WRITE(VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
  12728. I915_WRITE(VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
  12729. I915_WRITE(VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
  12730. I915_WRITE(PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
  12731. /*
  12732. * Apparently we need to have VGA mode enabled prior to changing
  12733. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  12734. * dividers, even though the register value does change.
  12735. */
  12736. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
  12737. I915_WRITE(DPLL(pipe), dpll);
  12738. /* Wait for the clocks to stabilize. */
  12739. POSTING_READ(DPLL(pipe));
  12740. udelay(150);
  12741. /* The pixel multiplier can only be updated once the
  12742. * DPLL is enabled and the clocks are stable.
  12743. *
  12744. * So write it again.
  12745. */
  12746. I915_WRITE(DPLL(pipe), dpll);
  12747. /* We do this three times for luck */
  12748. for (i = 0; i < 3 ; i++) {
  12749. I915_WRITE(DPLL(pipe), dpll);
  12750. POSTING_READ(DPLL(pipe));
  12751. udelay(150); /* wait for warmup */
  12752. }
  12753. I915_WRITE(PIPECONF(pipe), PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
  12754. POSTING_READ(PIPECONF(pipe));
  12755. intel_wait_for_pipe_scanline_moving(crtc);
  12756. }
  12757. void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
  12758. {
  12759. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12760. DRM_DEBUG_KMS("disabling pipe %c due to force quirk\n",
  12761. pipe_name(pipe));
  12762. WARN_ON(I915_READ(DSPCNTR(PLANE_A)) & DISPLAY_PLANE_ENABLE);
  12763. WARN_ON(I915_READ(DSPCNTR(PLANE_B)) & DISPLAY_PLANE_ENABLE);
  12764. WARN_ON(I915_READ(DSPCNTR(PLANE_C)) & DISPLAY_PLANE_ENABLE);
  12765. WARN_ON(I915_READ(CURCNTR(PIPE_A)) & MCURSOR_MODE);
  12766. WARN_ON(I915_READ(CURCNTR(PIPE_B)) & MCURSOR_MODE);
  12767. I915_WRITE(PIPECONF(pipe), 0);
  12768. POSTING_READ(PIPECONF(pipe));
  12769. intel_wait_for_pipe_scanline_stopped(crtc);
  12770. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  12771. POSTING_READ(DPLL(pipe));
  12772. }
  12773. static bool intel_plane_mapping_ok(struct intel_crtc *crtc,
  12774. struct intel_plane *plane)
  12775. {
  12776. enum pipe pipe;
  12777. if (!plane->get_hw_state(plane, &pipe))
  12778. return true;
  12779. return pipe == crtc->pipe;
  12780. }
  12781. static void
  12782. intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
  12783. {
  12784. struct intel_crtc *crtc;
  12785. if (INTEL_GEN(dev_priv) >= 4)
  12786. return;
  12787. for_each_intel_crtc(&dev_priv->drm, crtc) {
  12788. struct intel_plane *plane =
  12789. to_intel_plane(crtc->base.primary);
  12790. if (intel_plane_mapping_ok(crtc, plane))
  12791. continue;
  12792. DRM_DEBUG_KMS("%s attached to the wrong pipe, disabling plane\n",
  12793. plane->base.name);
  12794. intel_plane_disable_noatomic(crtc, plane);
  12795. }
  12796. }
  12797. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12798. {
  12799. struct drm_device *dev = crtc->base.dev;
  12800. struct intel_encoder *encoder;
  12801. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12802. return true;
  12803. return false;
  12804. }
  12805. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12806. {
  12807. struct drm_device *dev = encoder->base.dev;
  12808. struct intel_connector *connector;
  12809. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12810. return connector;
  12811. return NULL;
  12812. }
  12813. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12814. enum pipe pch_transcoder)
  12815. {
  12816. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12817. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
  12818. }
  12819. static void intel_sanitize_crtc(struct intel_crtc *crtc,
  12820. struct drm_modeset_acquire_ctx *ctx)
  12821. {
  12822. struct drm_device *dev = crtc->base.dev;
  12823. struct drm_i915_private *dev_priv = to_i915(dev);
  12824. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12825. /* Clear any frame start delays used for debugging left by the BIOS */
  12826. if (crtc->active && !transcoder_is_dsi(cpu_transcoder)) {
  12827. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12828. I915_WRITE(reg,
  12829. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12830. }
  12831. /* restore vblank interrupts to correct state */
  12832. drm_crtc_vblank_reset(&crtc->base);
  12833. if (crtc->active) {
  12834. struct intel_plane *plane;
  12835. drm_crtc_vblank_on(&crtc->base);
  12836. /* Disable everything but the primary plane */
  12837. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12838. const struct intel_plane_state *plane_state =
  12839. to_intel_plane_state(plane->base.state);
  12840. if (plane_state->base.visible &&
  12841. plane->base.type != DRM_PLANE_TYPE_PRIMARY)
  12842. intel_plane_disable_noatomic(crtc, plane);
  12843. }
  12844. }
  12845. /* Adjust the state of the output pipe according to whether we
  12846. * have active connectors/encoders. */
  12847. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12848. intel_crtc_disable_noatomic(&crtc->base, ctx);
  12849. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12850. /*
  12851. * We start out with underrun reporting disabled to avoid races.
  12852. * For correct bookkeeping mark this on active crtcs.
  12853. *
  12854. * Also on gmch platforms we dont have any hardware bits to
  12855. * disable the underrun reporting. Which means we need to start
  12856. * out with underrun reporting disabled also on inactive pipes,
  12857. * since otherwise we'll complain about the garbage we read when
  12858. * e.g. coming up after runtime pm.
  12859. *
  12860. * No protection against concurrent access is required - at
  12861. * worst a fifo underrun happens which also sets this to false.
  12862. */
  12863. crtc->cpu_fifo_underrun_disabled = true;
  12864. /*
  12865. * We track the PCH trancoder underrun reporting state
  12866. * within the crtc. With crtc for pipe A housing the underrun
  12867. * reporting state for PCH transcoder A, crtc for pipe B housing
  12868. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12869. * and marking underrun reporting as disabled for the non-existing
  12870. * PCH transcoders B and C would prevent enabling the south
  12871. * error interrupt (see cpt_can_enable_serr_int()).
  12872. */
  12873. if (has_pch_trancoder(dev_priv, crtc->pipe))
  12874. crtc->pch_fifo_underrun_disabled = true;
  12875. }
  12876. }
  12877. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12878. {
  12879. struct intel_connector *connector;
  12880. /* We need to check both for a crtc link (meaning that the
  12881. * encoder is active and trying to read from a pipe) and the
  12882. * pipe itself being active. */
  12883. bool has_active_crtc = encoder->base.crtc &&
  12884. to_intel_crtc(encoder->base.crtc)->active;
  12885. connector = intel_encoder_find_connector(encoder);
  12886. if (connector && !has_active_crtc) {
  12887. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12888. encoder->base.base.id,
  12889. encoder->base.name);
  12890. /* Connector is active, but has no active pipe. This is
  12891. * fallout from our resume register restoring. Disable
  12892. * the encoder manually again. */
  12893. if (encoder->base.crtc) {
  12894. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12895. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12896. encoder->base.base.id,
  12897. encoder->base.name);
  12898. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12899. if (encoder->post_disable)
  12900. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12901. }
  12902. encoder->base.crtc = NULL;
  12903. /* Inconsistent output/port/pipe state happens presumably due to
  12904. * a bug in one of the get_hw_state functions. Or someplace else
  12905. * in our code, like the register restore mess on resume. Clamp
  12906. * things to off as a safer default. */
  12907. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12908. connector->base.encoder = NULL;
  12909. }
  12910. /* notify opregion of the sanitized encoder state */
  12911. intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
  12912. }
  12913. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12914. {
  12915. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12916. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12917. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12918. i915_disable_vga(dev_priv);
  12919. }
  12920. }
  12921. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12922. {
  12923. /* This function can be called both from intel_modeset_setup_hw_state or
  12924. * at a very early point in our resume sequence, where the power well
  12925. * structures are not yet restored. Since this function is at a very
  12926. * paranoid "someone might have enabled VGA while we were not looking"
  12927. * level, just check if the power well is enabled instead of trying to
  12928. * follow the "don't touch the power well if we don't need it" policy
  12929. * the rest of the driver uses. */
  12930. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12931. return;
  12932. i915_redisable_vga_power_on(dev_priv);
  12933. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12934. }
  12935. /* FIXME read out full plane state for all planes */
  12936. static void readout_plane_state(struct intel_crtc *crtc)
  12937. {
  12938. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12939. struct intel_crtc_state *crtc_state =
  12940. to_intel_crtc_state(crtc->base.state);
  12941. struct intel_plane *plane;
  12942. for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
  12943. struct intel_plane_state *plane_state =
  12944. to_intel_plane_state(plane->base.state);
  12945. enum pipe pipe;
  12946. bool visible;
  12947. visible = plane->get_hw_state(plane, &pipe);
  12948. intel_set_plane_visible(crtc_state, plane_state, visible);
  12949. }
  12950. }
  12951. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12952. {
  12953. struct drm_i915_private *dev_priv = to_i915(dev);
  12954. enum pipe pipe;
  12955. struct intel_crtc *crtc;
  12956. struct intel_encoder *encoder;
  12957. struct intel_connector *connector;
  12958. struct drm_connector_list_iter conn_iter;
  12959. int i;
  12960. dev_priv->active_crtcs = 0;
  12961. for_each_intel_crtc(dev, crtc) {
  12962. struct intel_crtc_state *crtc_state =
  12963. to_intel_crtc_state(crtc->base.state);
  12964. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12965. memset(crtc_state, 0, sizeof(*crtc_state));
  12966. crtc_state->base.crtc = &crtc->base;
  12967. crtc_state->base.active = crtc_state->base.enable =
  12968. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12969. crtc->base.enabled = crtc_state->base.enable;
  12970. crtc->active = crtc_state->base.active;
  12971. if (crtc_state->base.active)
  12972. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12973. readout_plane_state(crtc);
  12974. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12975. crtc->base.base.id, crtc->base.name,
  12976. enableddisabled(crtc_state->base.active));
  12977. }
  12978. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12979. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12980. pll->on = pll->info->funcs->get_hw_state(dev_priv, pll,
  12981. &pll->state.hw_state);
  12982. pll->state.crtc_mask = 0;
  12983. for_each_intel_crtc(dev, crtc) {
  12984. struct intel_crtc_state *crtc_state =
  12985. to_intel_crtc_state(crtc->base.state);
  12986. if (crtc_state->base.active &&
  12987. crtc_state->shared_dpll == pll)
  12988. pll->state.crtc_mask |= 1 << crtc->pipe;
  12989. }
  12990. pll->active_mask = pll->state.crtc_mask;
  12991. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12992. pll->info->name, pll->state.crtc_mask, pll->on);
  12993. }
  12994. for_each_intel_encoder(dev, encoder) {
  12995. pipe = 0;
  12996. if (encoder->get_hw_state(encoder, &pipe)) {
  12997. struct intel_crtc_state *crtc_state;
  12998. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12999. crtc_state = to_intel_crtc_state(crtc->base.state);
  13000. encoder->base.crtc = &crtc->base;
  13001. encoder->get_config(encoder, crtc_state);
  13002. } else {
  13003. encoder->base.crtc = NULL;
  13004. }
  13005. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13006. encoder->base.base.id, encoder->base.name,
  13007. enableddisabled(encoder->base.crtc),
  13008. pipe_name(pipe));
  13009. }
  13010. drm_connector_list_iter_begin(dev, &conn_iter);
  13011. for_each_intel_connector_iter(connector, &conn_iter) {
  13012. if (connector->get_hw_state(connector)) {
  13013. connector->base.dpms = DRM_MODE_DPMS_ON;
  13014. encoder = connector->encoder;
  13015. connector->base.encoder = &encoder->base;
  13016. if (encoder->base.crtc &&
  13017. encoder->base.crtc->state->active) {
  13018. /*
  13019. * This has to be done during hardware readout
  13020. * because anything calling .crtc_disable may
  13021. * rely on the connector_mask being accurate.
  13022. */
  13023. encoder->base.crtc->state->connector_mask |=
  13024. 1 << drm_connector_index(&connector->base);
  13025. encoder->base.crtc->state->encoder_mask |=
  13026. 1 << drm_encoder_index(&encoder->base);
  13027. }
  13028. } else {
  13029. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13030. connector->base.encoder = NULL;
  13031. }
  13032. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13033. connector->base.base.id, connector->base.name,
  13034. enableddisabled(connector->base.encoder));
  13035. }
  13036. drm_connector_list_iter_end(&conn_iter);
  13037. for_each_intel_crtc(dev, crtc) {
  13038. struct intel_crtc_state *crtc_state =
  13039. to_intel_crtc_state(crtc->base.state);
  13040. int min_cdclk = 0;
  13041. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13042. if (crtc_state->base.active) {
  13043. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  13044. crtc->base.mode.hdisplay = crtc_state->pipe_src_w;
  13045. crtc->base.mode.vdisplay = crtc_state->pipe_src_h;
  13046. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  13047. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13048. /*
  13049. * The initial mode needs to be set in order to keep
  13050. * the atomic core happy. It wants a valid mode if the
  13051. * crtc's enabled, so we do the above call.
  13052. *
  13053. * But we don't set all the derived state fully, hence
  13054. * set a flag to indicate that a full recalculation is
  13055. * needed on the next commit.
  13056. */
  13057. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  13058. intel_crtc_compute_pixel_rate(crtc_state);
  13059. if (dev_priv->display.modeset_calc_cdclk) {
  13060. min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
  13061. if (WARN_ON(min_cdclk < 0))
  13062. min_cdclk = 0;
  13063. }
  13064. drm_calc_timestamping_constants(&crtc->base,
  13065. &crtc_state->base.adjusted_mode);
  13066. update_scanline_offset(crtc);
  13067. }
  13068. dev_priv->min_cdclk[crtc->pipe] = min_cdclk;
  13069. dev_priv->min_voltage_level[crtc->pipe] =
  13070. crtc_state->min_voltage_level;
  13071. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  13072. }
  13073. }
  13074. static void
  13075. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  13076. {
  13077. struct intel_encoder *encoder;
  13078. for_each_intel_encoder(&dev_priv->drm, encoder) {
  13079. u64 get_domains;
  13080. enum intel_display_power_domain domain;
  13081. if (!encoder->get_power_domains)
  13082. continue;
  13083. get_domains = encoder->get_power_domains(encoder);
  13084. for_each_power_domain(domain, get_domains)
  13085. intel_display_power_get(dev_priv, domain);
  13086. }
  13087. }
  13088. static void intel_early_display_was(struct drm_i915_private *dev_priv)
  13089. {
  13090. /* Display WA #1185 WaDisableDARBFClkGating:cnl,glk */
  13091. if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
  13092. I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
  13093. DARBF_GATING_DIS);
  13094. if (IS_HASWELL(dev_priv)) {
  13095. /*
  13096. * WaRsPkgCStateDisplayPMReq:hsw
  13097. * System hang if this isn't done before disabling all planes!
  13098. */
  13099. I915_WRITE(CHICKEN_PAR1_1,
  13100. I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
  13101. }
  13102. }
  13103. /* Scan out the current hw modeset state,
  13104. * and sanitizes it to the current state
  13105. */
  13106. static void
  13107. intel_modeset_setup_hw_state(struct drm_device *dev,
  13108. struct drm_modeset_acquire_ctx *ctx)
  13109. {
  13110. struct drm_i915_private *dev_priv = to_i915(dev);
  13111. enum pipe pipe;
  13112. struct intel_crtc *crtc;
  13113. struct intel_encoder *encoder;
  13114. int i;
  13115. intel_early_display_was(dev_priv);
  13116. intel_modeset_readout_hw_state(dev);
  13117. /* HW state is read out, now we need to sanitize this mess. */
  13118. get_encoder_power_domains(dev_priv);
  13119. intel_sanitize_plane_mapping(dev_priv);
  13120. for_each_intel_encoder(dev, encoder) {
  13121. intel_sanitize_encoder(encoder);
  13122. }
  13123. for_each_pipe(dev_priv, pipe) {
  13124. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  13125. intel_sanitize_crtc(crtc, ctx);
  13126. intel_dump_pipe_config(crtc, crtc->config,
  13127. "[setup_hw_state]");
  13128. }
  13129. intel_modeset_update_connector_atomic_state(dev);
  13130. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13131. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13132. if (!pll->on || pll->active_mask)
  13133. continue;
  13134. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n",
  13135. pll->info->name);
  13136. pll->info->funcs->disable(dev_priv, pll);
  13137. pll->on = false;
  13138. }
  13139. if (IS_G4X(dev_priv)) {
  13140. g4x_wm_get_hw_state(dev);
  13141. g4x_wm_sanitize(dev_priv);
  13142. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13143. vlv_wm_get_hw_state(dev);
  13144. vlv_wm_sanitize(dev_priv);
  13145. } else if (INTEL_GEN(dev_priv) >= 9) {
  13146. skl_wm_get_hw_state(dev);
  13147. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13148. ilk_wm_get_hw_state(dev);
  13149. }
  13150. for_each_intel_crtc(dev, crtc) {
  13151. u64 put_domains;
  13152. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  13153. if (WARN_ON(put_domains))
  13154. modeset_put_power_domains(dev_priv, put_domains);
  13155. }
  13156. intel_display_set_init_power(dev_priv, false);
  13157. intel_power_domains_verify_state(dev_priv);
  13158. intel_fbc_init_pipe_state(dev_priv);
  13159. }
  13160. void intel_display_resume(struct drm_device *dev)
  13161. {
  13162. struct drm_i915_private *dev_priv = to_i915(dev);
  13163. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13164. struct drm_modeset_acquire_ctx ctx;
  13165. int ret;
  13166. dev_priv->modeset_restore_state = NULL;
  13167. if (state)
  13168. state->acquire_ctx = &ctx;
  13169. drm_modeset_acquire_init(&ctx, 0);
  13170. while (1) {
  13171. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13172. if (ret != -EDEADLK)
  13173. break;
  13174. drm_modeset_backoff(&ctx);
  13175. }
  13176. if (!ret)
  13177. ret = __intel_display_resume(dev, state, &ctx);
  13178. intel_enable_ipc(dev_priv);
  13179. drm_modeset_drop_locks(&ctx);
  13180. drm_modeset_acquire_fini(&ctx);
  13181. if (ret)
  13182. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13183. if (state)
  13184. drm_atomic_state_put(state);
  13185. }
  13186. int intel_connector_register(struct drm_connector *connector)
  13187. {
  13188. struct intel_connector *intel_connector = to_intel_connector(connector);
  13189. int ret;
  13190. ret = intel_backlight_device_register(intel_connector);
  13191. if (ret)
  13192. goto err;
  13193. return 0;
  13194. err:
  13195. return ret;
  13196. }
  13197. void intel_connector_unregister(struct drm_connector *connector)
  13198. {
  13199. struct intel_connector *intel_connector = to_intel_connector(connector);
  13200. intel_backlight_device_unregister(intel_connector);
  13201. intel_panel_destroy_backlight(connector);
  13202. }
  13203. static void intel_hpd_poll_fini(struct drm_device *dev)
  13204. {
  13205. struct intel_connector *connector;
  13206. struct drm_connector_list_iter conn_iter;
  13207. /* Kill all the work that may have been queued by hpd. */
  13208. drm_connector_list_iter_begin(dev, &conn_iter);
  13209. for_each_intel_connector_iter(connector, &conn_iter) {
  13210. if (connector->modeset_retry_work.func)
  13211. cancel_work_sync(&connector->modeset_retry_work);
  13212. if (connector->hdcp_shim) {
  13213. cancel_delayed_work_sync(&connector->hdcp_check_work);
  13214. cancel_work_sync(&connector->hdcp_prop_work);
  13215. }
  13216. }
  13217. drm_connector_list_iter_end(&conn_iter);
  13218. }
  13219. void intel_modeset_cleanup(struct drm_device *dev)
  13220. {
  13221. struct drm_i915_private *dev_priv = to_i915(dev);
  13222. flush_work(&dev_priv->atomic_helper.free_work);
  13223. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  13224. intel_disable_gt_powersave(dev_priv);
  13225. /*
  13226. * Interrupts and polling as the first thing to avoid creating havoc.
  13227. * Too much stuff here (turning of connectors, ...) would
  13228. * experience fancy races otherwise.
  13229. */
  13230. intel_irq_uninstall(dev_priv);
  13231. /*
  13232. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13233. * poll handlers. Hence disable polling after hpd handling is shut down.
  13234. */
  13235. intel_hpd_poll_fini(dev);
  13236. /* poll work can call into fbdev, hence clean that up afterwards */
  13237. intel_fbdev_fini(dev_priv);
  13238. intel_unregister_dsm_handler();
  13239. intel_fbc_global_disable(dev_priv);
  13240. /* flush any delayed tasks or pending work */
  13241. flush_scheduled_work();
  13242. drm_mode_config_cleanup(dev);
  13243. intel_cleanup_overlay(dev_priv);
  13244. intel_cleanup_gt_powersave(dev_priv);
  13245. intel_teardown_gmbus(dev_priv);
  13246. destroy_workqueue(dev_priv->modeset_wq);
  13247. }
  13248. void intel_connector_attach_encoder(struct intel_connector *connector,
  13249. struct intel_encoder *encoder)
  13250. {
  13251. connector->encoder = encoder;
  13252. drm_mode_connector_attach_encoder(&connector->base,
  13253. &encoder->base);
  13254. }
  13255. /*
  13256. * set vga decode state - true == enable VGA decode
  13257. */
  13258. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  13259. {
  13260. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13261. u16 gmch_ctrl;
  13262. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13263. DRM_ERROR("failed to read control word\n");
  13264. return -EIO;
  13265. }
  13266. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13267. return 0;
  13268. if (state)
  13269. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13270. else
  13271. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13272. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13273. DRM_ERROR("failed to write control word\n");
  13274. return -EIO;
  13275. }
  13276. return 0;
  13277. }
  13278. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  13279. struct intel_display_error_state {
  13280. u32 power_well_driver;
  13281. int num_transcoders;
  13282. struct intel_cursor_error_state {
  13283. u32 control;
  13284. u32 position;
  13285. u32 base;
  13286. u32 size;
  13287. } cursor[I915_MAX_PIPES];
  13288. struct intel_pipe_error_state {
  13289. bool power_domain_on;
  13290. u32 source;
  13291. u32 stat;
  13292. } pipe[I915_MAX_PIPES];
  13293. struct intel_plane_error_state {
  13294. u32 control;
  13295. u32 stride;
  13296. u32 size;
  13297. u32 pos;
  13298. u32 addr;
  13299. u32 surface;
  13300. u32 tile_offset;
  13301. } plane[I915_MAX_PIPES];
  13302. struct intel_transcoder_error_state {
  13303. bool power_domain_on;
  13304. enum transcoder cpu_transcoder;
  13305. u32 conf;
  13306. u32 htotal;
  13307. u32 hblank;
  13308. u32 hsync;
  13309. u32 vtotal;
  13310. u32 vblank;
  13311. u32 vsync;
  13312. } transcoder[4];
  13313. };
  13314. struct intel_display_error_state *
  13315. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13316. {
  13317. struct intel_display_error_state *error;
  13318. int transcoders[] = {
  13319. TRANSCODER_A,
  13320. TRANSCODER_B,
  13321. TRANSCODER_C,
  13322. TRANSCODER_EDP,
  13323. };
  13324. int i;
  13325. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13326. return NULL;
  13327. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13328. if (error == NULL)
  13329. return NULL;
  13330. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13331. error->power_well_driver =
  13332. I915_READ(HSW_PWR_WELL_CTL_DRIVER(HSW_DISP_PW_GLOBAL));
  13333. for_each_pipe(dev_priv, i) {
  13334. error->pipe[i].power_domain_on =
  13335. __intel_display_power_is_enabled(dev_priv,
  13336. POWER_DOMAIN_PIPE(i));
  13337. if (!error->pipe[i].power_domain_on)
  13338. continue;
  13339. error->cursor[i].control = I915_READ(CURCNTR(i));
  13340. error->cursor[i].position = I915_READ(CURPOS(i));
  13341. error->cursor[i].base = I915_READ(CURBASE(i));
  13342. error->plane[i].control = I915_READ(DSPCNTR(i));
  13343. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13344. if (INTEL_GEN(dev_priv) <= 3) {
  13345. error->plane[i].size = I915_READ(DSPSIZE(i));
  13346. error->plane[i].pos = I915_READ(DSPPOS(i));
  13347. }
  13348. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13349. error->plane[i].addr = I915_READ(DSPADDR(i));
  13350. if (INTEL_GEN(dev_priv) >= 4) {
  13351. error->plane[i].surface = I915_READ(DSPSURF(i));
  13352. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13353. }
  13354. error->pipe[i].source = I915_READ(PIPESRC(i));
  13355. if (HAS_GMCH_DISPLAY(dev_priv))
  13356. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13357. }
  13358. /* Note: this does not include DSI transcoders. */
  13359. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13360. if (HAS_DDI(dev_priv))
  13361. error->num_transcoders++; /* Account for eDP. */
  13362. for (i = 0; i < error->num_transcoders; i++) {
  13363. enum transcoder cpu_transcoder = transcoders[i];
  13364. error->transcoder[i].power_domain_on =
  13365. __intel_display_power_is_enabled(dev_priv,
  13366. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13367. if (!error->transcoder[i].power_domain_on)
  13368. continue;
  13369. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13370. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13371. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13372. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13373. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13374. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13375. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13376. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13377. }
  13378. return error;
  13379. }
  13380. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13381. void
  13382. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13383. struct intel_display_error_state *error)
  13384. {
  13385. struct drm_i915_private *dev_priv = m->i915;
  13386. int i;
  13387. if (!error)
  13388. return;
  13389. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  13390. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13391. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13392. error->power_well_driver);
  13393. for_each_pipe(dev_priv, i) {
  13394. err_printf(m, "Pipe [%d]:\n", i);
  13395. err_printf(m, " Power: %s\n",
  13396. onoff(error->pipe[i].power_domain_on));
  13397. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13398. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13399. err_printf(m, "Plane [%d]:\n", i);
  13400. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13401. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13402. if (INTEL_GEN(dev_priv) <= 3) {
  13403. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13404. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13405. }
  13406. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13407. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13408. if (INTEL_GEN(dev_priv) >= 4) {
  13409. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13410. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13411. }
  13412. err_printf(m, "Cursor [%d]:\n", i);
  13413. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13414. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13415. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13416. }
  13417. for (i = 0; i < error->num_transcoders; i++) {
  13418. err_printf(m, "CPU transcoder: %s\n",
  13419. transcoder_name(error->transcoder[i].cpu_transcoder));
  13420. err_printf(m, " Power: %s\n",
  13421. onoff(error->transcoder[i].power_domain_on));
  13422. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13423. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13424. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13425. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13426. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13427. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13428. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13429. }
  13430. }
  13431. #endif